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 | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 7 | |
| 8 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 44ae86e | 2017-05-15 21:52:14 +0530 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 12 | #include <bitset> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | #include <map> |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 14 | #include <fstream> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 15 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 16 | #include <arpa/inet.h> |
| 17 | #include <dirent.h> |
| 18 | #include <net/if.h> |
| 19 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 20 | #include <string> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 21 | |
| 22 | namespace phosphor |
| 23 | { |
| 24 | namespace network |
| 25 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 26 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 29 | |
| 30 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 31 | details::VLANCreateIface(bus, objPath, true), |
| 32 | bus(bus), |
| 33 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 34 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 35 | confDir = NETWORK_CONF_DIR; |
| 36 | } |
| 37 | |
| 38 | void Manager::setConfDir(const fs::path& dir) |
| 39 | { |
| 40 | confDir = dir; |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void Manager::createInterfaces() |
| 44 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 45 | //clear all the interfaces first |
| 46 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 47 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 48 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 49 | |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 50 | for (const auto& intfInfo : interfaceInfoList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 51 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 52 | fs::path objPath = objectPath; |
| 53 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 54 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 55 | auto dhcp = getDHCPValue(intfInfo.first); |
| 56 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 57 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 58 | intfInfo.first, |
| 59 | std::make_unique< |
| 60 | phosphor::network::EthernetInterface> |
| 61 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 62 | objPath.string(), |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 63 | dhcp, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 64 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 65 | |
| 66 | interfaces[intfInfo.first]->setAddressList(intfInfo.second); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 70 | void Manager::createChildObjects() |
| 71 | { |
| 72 | // creates the ethernet interface dbus object. |
| 73 | createInterfaces(); |
| 74 | // create the system conf object. |
| 75 | fs::path objPath = objectPath; |
| 76 | objPath /= "config"; |
| 77 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 78 | bus, objPath.string(), *this); |
| 79 | |
| 80 | } |
| 81 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 82 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 83 | { |
| 84 | } |
| 85 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 86 | void Manager::reset() |
| 87 | { |
| 88 | const std::string networkConfig = "/etc/systemd/network/"; |
| 89 | bool filesExist, interfacesMapped = false; |
| 90 | |
| 91 | if(fs::is_directory(networkConfig)) |
| 92 | { |
| 93 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 94 | { |
| 95 | std::string filename = file.path().filename().c_str(); |
| 96 | |
| 97 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 98 | { |
| 99 | fs::remove(file.path()); |
| 100 | filesExist = true; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if(!filesExist) |
| 105 | { |
| 106 | log<level::INFO>("No existing network configuration was found."); |
| 107 | } |
| 108 | |
| 109 | for (auto& intf : interfaces) |
| 110 | { |
| 111 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 112 | ".network"; |
| 113 | |
| 114 | bmc::writeDHCPDefault(filename, intf.first); |
| 115 | interfacesMapped = true; |
| 116 | } |
| 117 | |
| 118 | if(interfacesMapped) |
| 119 | { |
| 120 | log<level::INFO>("Network configuration reset to DHCP."); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | log<level::ERR>("No network interfaces are mapped."); |
| 125 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | log<level::ERR>("Network configuration directory not found!"); |
| 131 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 132 | } |
| 133 | |
| 134 | return; |
| 135 | } |
| 136 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 137 | // Need to merge the below function with the code which writes the |
| 138 | // config file during factory reset. |
| 139 | //TODO openbmc/openbmc#1751 |
| 140 | void Manager::writeToConfigurationFile() |
| 141 | { |
| 142 | // write all the static ip address in the systemd-network conf file |
| 143 | |
| 144 | using namespace std::string_literals; |
| 145 | using AddressOrigin = |
| 146 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 147 | namespace fs = std::experimental::filesystem; |
| 148 | |
| 149 | for (const auto& intf : interfaces) |
| 150 | { |
| 151 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 152 | fs::path confPath = confDir; |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 153 | std::string fileName = "00-bmc-"s + intf.first + ".network"s; |
| 154 | confPath /= fileName; |
| 155 | std::fstream stream; |
| 156 | stream.open(confPath.c_str(), std::fstream::out); |
| 157 | |
| 158 | // Write the device |
| 159 | stream << "[" << "Match" << "]\n"; |
| 160 | stream << "Name=" << intf.first << "\n"; |
| 161 | |
| 162 | auto addrs = intf.second->getAddresses(); |
| 163 | |
| 164 | // write the network section |
| 165 | stream << "[" << "Network" << "]\n"; |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 166 | // DHCP |
| 167 | if (intf.second->dHCPEnabled() == true) |
| 168 | { |
| 169 | stream << "DHCP=true\n"; |
| 170 | // write the dhcp section |
| 171 | stream << "[DHCP]\n"; |
| 172 | stream << "ClientIdentifier=mac\n"; |
| 173 | stream.close(); |
| 174 | continue; |
| 175 | } |
| 176 | // Static |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 177 | for (const auto& addr : addrs) |
| 178 | { |
| 179 | if (addr.second->origin() == AddressOrigin::Static) |
| 180 | { |
| 181 | std::string address = addr.second->address() + "/" + std::to_string( |
| 182 | addr.second->prefixLength()); |
| 183 | |
| 184 | stream << "Address=" << address << "\n"; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 185 | if (addr.second->gateway() != "0.0.0.0" && |
| 186 | addr.second->gateway() != "") |
| 187 | { |
| 188 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 189 | } |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 190 | |
| 191 | } |
| 192 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 193 | stream << "Gateway=" << systemConf->defaultGateway() << "\n"; |
| 194 | // write the route section |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 195 | stream << "[" << "Route" << "]\n"; |
| 196 | for(const auto& addr : addrs) |
| 197 | { |
| 198 | if (addr.second->origin() == AddressOrigin::Static) |
| 199 | { |
| 200 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6; |
| 201 | std::string destination = getNetworkID( |
| 202 | addressFamily, |
| 203 | addr.second->address(), |
| 204 | addr.second->prefixLength()); |
| 205 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 206 | if (addr.second->gateway() != "0.0.0.0" && |
| 207 | addr.second->gateway() != "" && |
| 208 | destination != "0.0.0.0" && |
| 209 | destination != "") |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 210 | { |
| 211 | |
| 212 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 213 | stream << "Destination=" << destination << "\n"; |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | } |
| 218 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 219 | stream.close(); |
| 220 | } |
| 221 | restartSystemdNetworkd(); |
| 222 | } |
| 223 | |
| 224 | void Manager::restartSystemdNetworkd() |
| 225 | { |
| 226 | constexpr auto systemdNetworkdService = "systemd-networkd.service"; |
| 227 | |
| 228 | auto method = bus.new_method_call( |
| 229 | SYSTEMD_BUSNAME, |
| 230 | SYSTEMD_PATH, |
| 231 | SYSTEMD_INTERFACE, |
| 232 | "RestartUnit"); |
| 233 | |
| 234 | method.append(systemdNetworkdService, |
| 235 | "replace"); |
| 236 | |
| 237 | bus.call_noreply(method); |
| 238 | } |
| 239 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 240 | bool Manager::getDHCPValue(const std::string& intf) |
| 241 | { |
| 242 | bool dhcp = false; |
| 243 | // Get the interface mode value from systemd conf |
| 244 | using namespace std::string_literals; |
| 245 | fs::path confPath = confDir; |
| 246 | std::string fileName = "00-bmc-"s + intf + ".network"s; |
| 247 | confPath /= fileName; |
| 248 | |
| 249 | try |
| 250 | { |
| 251 | config::Parser parser(confPath.string()); |
| 252 | auto values = parser.getValues("Network","DHCP"); |
| 253 | // There will be only single value for DHCP key. |
| 254 | if (values[0] == "true") |
| 255 | { |
| 256 | dhcp = true; |
| 257 | } |
| 258 | } |
| 259 | catch (InternalFailure& e) |
| 260 | { |
| 261 | commit<InternalFailure>(); |
| 262 | } |
| 263 | return dhcp; |
| 264 | } |
| 265 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 266 | }//namespace network |
| 267 | }//namespace phosphor |