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; |
| 67 | objPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 68 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 69 | auto dhcp = getDHCPValue(intfInfo.first); |
| 70 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 71 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 72 | intfInfo.first, |
| 73 | std::make_unique< |
| 74 | phosphor::network::EthernetInterface> |
| 75 | (bus, |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 76 | objPath.string(), |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 77 | dhcp, |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 78 | *this))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 79 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 80 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 81 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 84 | void Manager::createChildObjects() |
| 85 | { |
| 86 | // creates the ethernet interface dbus object. |
| 87 | createInterfaces(); |
| 88 | // create the system conf object. |
| 89 | fs::path objPath = objectPath; |
| 90 | objPath /= "config"; |
| 91 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 92 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 93 | // create the dhcp conf object. |
| 94 | objPath /= "dhcp"; |
| 95 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 96 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 97 | |
| 98 | } |
| 99 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 100 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 101 | { |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 102 | auto& intf = interfaces[interfaceName]; |
| 103 | intf->createVLAN(id); |
| 104 | writeToConfigurationFile(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 105 | } |
| 106 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 107 | void Manager::reset() |
| 108 | { |
| 109 | const std::string networkConfig = "/etc/systemd/network/"; |
| 110 | bool filesExist, interfacesMapped = false; |
| 111 | |
| 112 | if(fs::is_directory(networkConfig)) |
| 113 | { |
| 114 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 115 | { |
| 116 | std::string filename = file.path().filename().c_str(); |
| 117 | |
| 118 | if(filename.substr(filename.find_last_of(".") + 1) == "network") |
| 119 | { |
| 120 | fs::remove(file.path()); |
| 121 | filesExist = true; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if(!filesExist) |
| 126 | { |
| 127 | log<level::INFO>("No existing network configuration was found."); |
| 128 | } |
| 129 | |
| 130 | for (auto& intf : interfaces) |
| 131 | { |
| 132 | std::string filename = networkConfig + "00-bmc-" + intf.first + |
| 133 | ".network"; |
| 134 | |
| 135 | bmc::writeDHCPDefault(filename, intf.first); |
| 136 | interfacesMapped = true; |
| 137 | } |
| 138 | |
| 139 | if(interfacesMapped) |
| 140 | { |
| 141 | log<level::INFO>("Network configuration reset to DHCP."); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | log<level::ERR>("No network interfaces are mapped."); |
| 146 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | log<level::ERR>("Network configuration directory not found!"); |
| 152 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
| 153 | } |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 158 | // Need to merge the below function with the code which writes the |
| 159 | // config file during factory reset. |
| 160 | //TODO openbmc/openbmc#1751 |
| 161 | void Manager::writeToConfigurationFile() |
| 162 | { |
| 163 | // write all the static ip address in the systemd-network conf file |
| 164 | |
| 165 | using namespace std::string_literals; |
| 166 | using AddressOrigin = |
| 167 | sdbusplus::xyz::openbmc_project::Network::server::IP::AddressOrigin; |
| 168 | namespace fs = std::experimental::filesystem; |
| 169 | |
| 170 | for (const auto& intf : interfaces) |
| 171 | { |
| 172 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 173 | fs::path confPath = confDir; |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 174 | std::string fileName = "00-bmc-"s + intf.first + ".network"s; |
| 175 | confPath /= fileName; |
| 176 | std::fstream stream; |
| 177 | stream.open(confPath.c_str(), std::fstream::out); |
| 178 | |
| 179 | // Write the device |
| 180 | stream << "[" << "Match" << "]\n"; |
| 181 | stream << "Name=" << intf.first << "\n"; |
| 182 | |
| 183 | auto addrs = intf.second->getAddresses(); |
| 184 | |
| 185 | // write the network section |
| 186 | stream << "[" << "Network" << "]\n"; |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 187 | // DHCP |
| 188 | if (intf.second->dHCPEnabled() == true) |
| 189 | { |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 190 | // write the dhcp section if interface is |
| 191 | // configured as dhcp. |
| 192 | writeDHCPSection(stream); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 193 | stream.close(); |
| 194 | continue; |
| 195 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 196 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 197 | // Static |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 198 | for (const auto& addr : addrs) |
| 199 | { |
| 200 | if (addr.second->origin() == AddressOrigin::Static) |
| 201 | { |
| 202 | std::string address = addr.second->address() + "/" + std::to_string( |
| 203 | addr.second->prefixLength()); |
| 204 | |
| 205 | stream << "Address=" << address << "\n"; |
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 | { |
| 209 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 210 | } |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 211 | |
| 212 | } |
| 213 | } |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame^] | 214 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 215 | stream << "Gateway=" << systemConf->defaultGateway() << "\n"; |
| 216 | // write the route section |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 217 | stream << "[" << "Route" << "]\n"; |
| 218 | for(const auto& addr : addrs) |
| 219 | { |
| 220 | if (addr.second->origin() == AddressOrigin::Static) |
| 221 | { |
| 222 | int addressFamily = addr.second->type() == IP::Protocol::IPv4 ? AF_INET : AF_INET6; |
| 223 | std::string destination = getNetworkID( |
| 224 | addressFamily, |
| 225 | addr.second->address(), |
| 226 | addr.second->prefixLength()); |
| 227 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 228 | if (addr.second->gateway() != "0.0.0.0" && |
| 229 | addr.second->gateway() != "" && |
| 230 | destination != "0.0.0.0" && |
| 231 | destination != "") |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 232 | { |
Ratan Gupta | fc2c724 | 2017-05-29 08:46:06 +0530 | [diff] [blame] | 233 | stream << "Gateway=" << addr.second->gateway() << "\n"; |
| 234 | stream << "Destination=" << destination << "\n"; |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 240 | stream.close(); |
| 241 | } |
Ratan Gupta | 068a8cf | 2017-07-11 19:18:29 +0530 | [diff] [blame] | 242 | restartSystemdUnit("systemd-networkd.service"); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 243 | } |
| 244 | |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 245 | void Manager::writeDHCPSection(std::fstream& stream) |
| 246 | { |
| 247 | using namespace std::string_literals; |
| 248 | stream << "DHCP=true\n"; |
| 249 | // write the dhcp section |
| 250 | stream << "[DHCP]\n"; |
| 251 | |
| 252 | // Hardcoding the client identifier to mac, to address below issue |
| 253 | // https://github.com/openbmc/openbmc/issues/1280 |
| 254 | stream << "ClientIdentifier=mac\n"; |
| 255 | |
| 256 | auto value = dhcpConf->dNSEnabled() ? "true"s : "false"s; |
| 257 | stream << "UseDNS="s + value + "\n"; |
| 258 | |
| 259 | value = dhcpConf->nTPEnabled() ? "true"s : "false"s; |
| 260 | stream << "UseNTP="s + value + "\n"; |
| 261 | |
| 262 | value = dhcpConf->hostNameEnabled() ? "true"s : "false"s; |
| 263 | stream << "UseHostname="s + value + "\n"; |
| 264 | } |
| 265 | |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 266 | bool Manager::getDHCPValue(const std::string& intf) |
| 267 | { |
| 268 | bool dhcp = false; |
| 269 | // Get the interface mode value from systemd conf |
| 270 | using namespace std::string_literals; |
| 271 | fs::path confPath = confDir; |
| 272 | std::string fileName = "00-bmc-"s + intf + ".network"s; |
| 273 | confPath /= fileName; |
| 274 | |
| 275 | try |
| 276 | { |
| 277 | config::Parser parser(confPath.string()); |
| 278 | auto values = parser.getValues("Network","DHCP"); |
| 279 | // There will be only single value for DHCP key. |
| 280 | if (values[0] == "true") |
| 281 | { |
| 282 | dhcp = true; |
| 283 | } |
| 284 | } |
| 285 | catch (InternalFailure& e) |
| 286 | { |
Ratan Gupta | dea3ead | 2017-08-02 18:09:25 +0530 | [diff] [blame] | 287 | log<level::INFO>("Exception occured during getting of DHCP value"); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 288 | } |
| 289 | return dhcp; |
| 290 | } |
| 291 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 292 | }//namespace network |
| 293 | }//namespace phosphor |