Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "network_manager.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
| 3 | #include "config.h" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 4 | #include "ipaddress.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include "network_config.hpp" |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 6 | #include "timer.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include "util.hpp" |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 8 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 9 | #include <arpa/inet.h> |
| 10 | #include <dirent.h> |
| 11 | #include <net/if.h> |
| 12 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <bitset> |
| 15 | #include <fstream> |
| 16 | #include <map> |
| 17 | #include <phosphor-logging/elog-errors.hpp> |
| 18 | #include <phosphor-logging/log.hpp> |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 19 | #include <string> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 20 | #include <xyz/openbmc_project/Common/error.hpp> |
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 | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 27 | extern std::unique_ptr<phosphor::network::Timer> refreshObjectTimer; |
| 28 | extern std::unique_ptr<phosphor::network::Timer> restartTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 30 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 31 | |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 32 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath, |
| 33 | const std::string& path): |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 34 | details::VLANCreateIface(bus, objPath, true), |
| 35 | bus(bus), |
| 36 | objectPath(objPath) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 37 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 38 | fs::path confDir(path); |
| 39 | setConfDir(confDir); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 40 | } |
| 41 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 42 | bool Manager::createDefaultNetworkFiles(bool force) |
| 43 | { |
| 44 | auto isCreated = false; |
| 45 | try |
| 46 | { |
| 47 | // Directory would have created before with |
| 48 | // setConfDir function. |
| 49 | if (force) |
| 50 | { |
| 51 | // Factory Reset case |
| 52 | // we need to forcefully write the files |
| 53 | // so delete the existing ones. |
| 54 | if (fs::is_directory(confDir)) |
| 55 | { |
| 56 | for (const auto& file : fs::directory_iterator(confDir)) |
| 57 | { |
| 58 | fs::remove(file.path()); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | auto interfaceStrList = getInterfaces(); |
| 64 | for (const auto& interface : interfaceStrList) |
| 65 | { |
Michael Tritz | 08c34f4 | 2017-10-16 14:59:09 -0500 | [diff] [blame] | 66 | // if the interface has '.' in the name, it means that this is a |
| 67 | // VLAN - don't create the network file. |
| 68 | if (interface.find(".") != std::string::npos) |
| 69 | { |
| 70 | continue; |
| 71 | } |
| 72 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 73 | auto fileName = systemd::config::networkFilePrefix + interface + |
| 74 | systemd::config::networkFileSuffix; |
| 75 | |
| 76 | fs::path filePath = confDir; |
| 77 | filePath /= fileName; |
| 78 | |
| 79 | // create the interface specific network file |
| 80 | // if not exist or we forcefully wants to write |
| 81 | // the network file. |
| 82 | |
| 83 | if (force || !fs::is_regular_file(filePath.string())) |
| 84 | { |
| 85 | bmc::writeDHCPDefault(filePath.string(), interface); |
| 86 | log<level::INFO>("Created the default network file.", |
| 87 | entry("INTERFACE=%s", interface.c_str())); |
| 88 | isCreated = true; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | catch (std::exception& e) |
| 93 | { |
| 94 | log<level::ERR>("Unable to create the default network file"); |
| 95 | } |
| 96 | return isCreated; |
| 97 | } |
| 98 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 99 | void Manager::setConfDir(const fs::path& dir) |
| 100 | { |
| 101 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 102 | |
| 103 | if (!fs::exists(confDir)) |
| 104 | { |
| 105 | if (!fs::create_directories(confDir)) |
| 106 | { |
| 107 | log<level::ERR>("Unable to create the network conf dir", |
| 108 | entry("DIR=%s", confDir.c_str())); |
| 109 | elog<InternalFailure>(); |
| 110 | } |
| 111 | } |
| 112 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void Manager::createInterfaces() |
| 116 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 117 | //clear all the interfaces first |
| 118 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 119 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 120 | auto interfaceStrList = getInterfaces(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 121 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 122 | for (auto& interface : interfaceStrList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 123 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 124 | fs::path objPath = objectPath; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 125 | auto index = interface.find("."); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 126 | |
| 127 | // interface can be of vlan type or normal ethernet interface. |
| 128 | // vlan interface looks like "interface.vlanid",so here by looking |
| 129 | // at the interface name we decide that we need |
| 130 | // to create the vlaninterface or normal physical interface. |
| 131 | if (index != std::string::npos) |
| 132 | { |
| 133 | //it is vlan interface |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 134 | auto interfaceName = interface.substr(0, index); |
| 135 | auto vlanid = interface.substr(index + 1); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 136 | uint32_t vlanInt = std::stoul(vlanid); |
| 137 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 138 | interfaces[interfaceName]->loadVLAN(vlanInt); |
Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 139 | continue; |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 140 | } |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 141 | // normal ethernet interface |
| 142 | objPath /= interface; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 143 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 144 | auto dhcp = getDHCPValue(confDir, interface); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 145 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 146 | auto intf = std::make_shared<phosphor::network::EthernetInterface>( |
| 147 | bus, |
| 148 | objPath.string(), |
| 149 | dhcp, |
| 150 | *this); |
| 151 | |
| 152 | |
| 153 | intf->createIPAddressObjects(); |
| 154 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 155 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 156 | std::move(interface), std::move(intf))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 157 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 158 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 159 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 160 | } |
| 161 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 162 | void Manager::createChildObjects() |
| 163 | { |
| 164 | // creates the ethernet interface dbus object. |
| 165 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 166 | |
| 167 | systemConf.reset(nullptr); |
| 168 | dhcpConf.reset(nullptr); |
| 169 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 170 | fs::path objPath = objectPath; |
| 171 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 172 | |
| 173 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 174 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 175 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 176 | // create the dhcp conf object. |
| 177 | objPath /= "dhcp"; |
| 178 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 179 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 180 | |
| 181 | } |
| 182 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 183 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 184 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 185 | interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 186 | } |
| 187 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 188 | void Manager::reset() |
| 189 | { |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 190 | if(!createDefaultNetworkFiles(true)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 191 | { |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 192 | log<level::ERR>("Network Factory Reset failed."); |
| 193 | return; |
| 194 | // TODO: openbmc/openbmc#1721 - Log ResetFailed error here. |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 197 | log<level::INFO>("Network Factory Reset done."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 200 | // Need to merge the below function with the code which writes the |
| 201 | // config file during factory reset. |
| 202 | //TODO openbmc/openbmc#1751 |
| 203 | void Manager::writeToConfigurationFile() |
| 204 | { |
| 205 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 206 | for (const auto& intf : interfaces) |
| 207 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 208 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 209 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 210 | } |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 211 | restartTimers(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 212 | } |
| 213 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 214 | void Manager::restartTimers() |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 215 | { |
| 216 | using namespace std::chrono; |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 217 | if (refreshObjectTimer && restartTimer) |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 218 | { |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 219 | // start the restart timer. |
| 220 | auto restartTime = duration_cast<microseconds>( |
| 221 | phosphor::network::restartTimeout); |
| 222 | restartTimer->startTimer(restartTime); |
| 223 | |
| 224 | // start the refresh timer. |
| 225 | auto refreshTime = duration_cast<microseconds>( |
| 226 | phosphor::network::refreshTimeout); |
| 227 | refreshObjectTimer->startTimer(refreshTime); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 228 | } |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 229 | } |
| 230 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 231 | }//namespace network |
| 232 | }//namespace phosphor |