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