Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 2 | #include "config_parser.hpp" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 3 | #include "util.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 4 | #include "network_manager.hpp" |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 5 | #include "network_config.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 7 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 8 | |
| 9 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 11 | |
| 12 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 14 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 15 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 16 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <dirent.h> |
| 19 | #include <net/if.h> |
| 20 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 21 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace network |
| 26 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 27 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 28 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 29 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 30 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 31 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath, |
| 32 | const std::string& path): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 33 | details::VLANCreateIface(bus, objPath, true), |
| 34 | bus(bus), |
| 35 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 36 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 37 | fs::path confDir(path); |
| 38 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void Manager::setConfDir(const fs::path& dir) |
| 42 | { |
| 43 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 44 | |
| 45 | if (!fs::exists(confDir)) |
| 46 | { |
| 47 | if (!fs::create_directories(confDir)) |
| 48 | { |
| 49 | log<level::ERR>("Unable to create the network conf dir", |
| 50 | entry("DIR=%s", confDir.c_str())); |
| 51 | elog<InternalFailure>(); |
| 52 | } |
| 53 | } |
| 54 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void Manager::createInterfaces() |
| 58 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 59 | //clear all the interfaces first |
| 60 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 61 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 62 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 63 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 64 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 65 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 66 | fs::path objPath = objectPath; |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 67 | auto index = intfInfo.first.find("."); |
| 68 | |
| 69 | // interface can be of vlan type or normal ethernet interface. |
| 70 | // vlan interface looks like "interface.vlanid",so here by looking |
| 71 | // at the interface name we decide that we need |
| 72 | // to create the vlaninterface or normal physical interface. |
| 73 | if (index != std::string::npos) |
| 74 | { |
| 75 | //it is vlan interface |
| 76 | auto interface = intfInfo.first.substr(0, index); |
| 77 | auto vlanid = intfInfo.first.substr(index + 1); |
| 78 | uint32_t vlanInt = std::stoul(vlanid); |
| 79 | |
| 80 | interfaces[interface]->loadVLAN(vlanInt); |
| 81 | return; |
| 82 | } |
| 83 | // normal ethernet inetrface |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 84 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 85 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 86 | auto dhcp = getDHCPValue(intfInfo.first); |
| 87 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 88 | auto intf = std::make_shared<phosphor::network::EthernetInterface>( |
| 89 | bus, |
| 90 | objPath.string(), |
| 91 | dhcp, |
| 92 | *this); |
| 93 | |
| 94 | |
| 95 | intf->createIPAddressObjects(); |
| 96 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 97 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 98 | intfInfo.first, std::move(intf))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 99 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 100 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 101 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 104 | void Manager::createChildObjects() |
| 105 | { |
| 106 | // creates the ethernet interface dbus object. |
| 107 | createInterfaces(); |
| 108 | // create the system conf object. |
| 109 | fs::path objPath = objectPath; |
| 110 | objPath /= "config"; |
| 111 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 112 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 113 | // create the dhcp conf object. |
| 114 | objPath /= "dhcp"; |
| 115 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 116 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 120 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 121 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 122 | interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 125 | void Manager::reset() |
| 126 | { |
| 127 | const std::string networkConfig = "/etc/systemd/network/"; |
| 128 | bool filesExist, interfacesMapped = false; |
| 129 | |
| 130 | if(fs::is_directory(networkConfig)) |
| 131 | { |
| 132 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 133 | { |
| 134 | std::string filename = file.path().filename().c_str(); |
| 135 | |
| 136 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 137 | { |
| 138 | fs::remove(file.path()); |
| 139 | filesExist = true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if(!filesExist) |
| 144 | { |
| 145 | log<level::INFO>("No existing network configuration was found."); |
| 146 | } |
| 147 | |
| 148 | for (auto& intf : interfaces) |
| 149 | { |
| 150 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 151 | ".network"; |
| 152 | |
| 153 | bmc::writeDHCPDefault(filename, intf.first); |
| 154 | interfacesMapped = true; |
| 155 | } |
| 156 | |
| 157 | if(interfacesMapped) |
| 158 | { |
| 159 | log<level::INFO>("Network configuration reset to DHCP."); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | log<level::ERR>("No network interfaces are mapped."); |
| 164 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 165 | } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | log<level::ERR>("Network configuration directory not found!"); |
| 170 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 171 | } |
| 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 176 | // Need to merge the below function with the code which writes the |
| 177 | // config file during factory reset. |
| 178 | //TODO openbmc/openbmc#1751 |
| 179 | void Manager::writeToConfigurationFile() |
| 180 | { |
| 181 | // write all the static ip address in the systemd-network conf file |
| 182 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 183 | for (const auto& intf : interfaces) |
| 184 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 185 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 186 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 187 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 188 | } |
| 189 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 190 | bool Manager::getDHCPValue(const std::string& intf) |
| 191 | { |
| 192 | bool dhcp = false; |
| 193 | // Get the interface mode value from systemd conf |
| 194 | using namespace std::string_literals; |
| 195 | fs::path confPath = confDir; |
| 196 | std::string fileName = "00-bmc-"s + intf + ".network"s; |
| 197 | confPath /= fileName; |
| 198 | |
| 199 | try |
| 200 | { |
| 201 | config::Parser parser(confPath.string()); |
| 202 | auto values = parser.getValues("Network","DHCP"); |
| 203 | // There will be only single value for DHCP key. |
| 204 | if (values[0] == "true") |
| 205 | { |
| 206 | dhcp = true; |
| 207 | } |
| 208 | } |
| 209 | catch (InternalFailure& e) |
| 210 | { |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 211 | log<level::INFO>("Exception occured during getting of DHCP value"); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 212 | } |
| 213 | return dhcp; |
| 214 | } |
| 215 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 216 | }//namespace network |
| 217 | }//namespace phosphor |