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 | |
| 42 | void Manager::setConfDir(const fs::path& dir) |
| 43 | { |
| 44 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 45 | |
| 46 | if (!fs::exists(confDir)) |
| 47 | { |
| 48 | if (!fs::create_directories(confDir)) |
| 49 | { |
| 50 | log<level::ERR>("Unable to create the network conf dir", |
| 51 | entry("DIR=%s", confDir.c_str())); |
| 52 | elog<InternalFailure>(); |
| 53 | } |
| 54 | } |
| 55 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void Manager::createInterfaces() |
| 59 | { |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 60 | //clear all the interfaces first |
| 61 | interfaces.clear(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 62 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 63 | auto interfaceStrList = getInterfaces(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 64 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 65 | for (auto& interface : interfaceStrList) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 66 | { |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 67 | fs::path objPath = objectPath; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 68 | auto index = interface.find("."); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 69 | |
| 70 | // interface can be of vlan type or normal ethernet interface. |
| 71 | // vlan interface looks like "interface.vlanid",so here by looking |
| 72 | // at the interface name we decide that we need |
| 73 | // to create the vlaninterface or normal physical interface. |
| 74 | if (index != std::string::npos) |
| 75 | { |
| 76 | //it is vlan interface |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 77 | auto interfaceName = interface.substr(0, index); |
| 78 | auto vlanid = interface.substr(index + 1); |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 79 | uint32_t vlanInt = std::stoul(vlanid); |
| 80 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 81 | interfaces[interfaceName]->loadVLAN(vlanInt); |
Ratan Gupta | 6e8df63 | 2017-08-13 09:41:58 +0530 | [diff] [blame] | 82 | continue; |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 83 | } |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 84 | // normal ethernet interface |
| 85 | objPath /= interface; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 86 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 87 | auto dhcp = getDHCPValue(confDir, interface); |
Ratan Gupta | 34f96d6 | 2017-06-15 09:16:22 +0530 | [diff] [blame] | 88 | |
Ratan Gupta | 92bc2fe | 2017-07-26 22:40:21 +0530 | [diff] [blame] | 89 | auto intf = std::make_shared<phosphor::network::EthernetInterface>( |
| 90 | bus, |
| 91 | objPath.string(), |
| 92 | dhcp, |
| 93 | *this); |
| 94 | |
| 95 | |
| 96 | intf->createIPAddressObjects(); |
| 97 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 98 | this->interfaces.emplace(std::make_pair( |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 99 | std::move(interface), std::move(intf))); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 100 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 101 | } |
Ratan Gupta | 87c1398 | 2017-06-15 09:27:27 +0530 | [diff] [blame] | 102 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 103 | } |
| 104 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 105 | void Manager::createChildObjects() |
| 106 | { |
| 107 | // creates the ethernet interface dbus object. |
| 108 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame^] | 109 | |
| 110 | systemConf.reset(nullptr); |
| 111 | dhcpConf.reset(nullptr); |
| 112 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 113 | fs::path objPath = objectPath; |
| 114 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame^] | 115 | |
| 116 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 117 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
| 118 | bus, objPath.string(), *this); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 119 | // create the dhcp conf object. |
| 120 | objPath /= "dhcp"; |
| 121 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
| 122 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | |
Ratan Gupta | 584df83 | 2017-07-31 16:21:54 +0530 | [diff] [blame] | 126 | void Manager::vLAN(IntfName interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 127 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 128 | interfaces[interfaceName]->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 129 | } |
| 130 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 131 | void Manager::reset() |
| 132 | { |
Ratan Gupta | 1bc8ed3 | 2017-07-25 22:34:19 +0530 | [diff] [blame] | 133 | const std::string networkConfig = confDir.string(); |
| 134 | bool interfacesMapped = false; |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 135 | |
| 136 | if(fs::is_directory(networkConfig)) |
| 137 | { |
| 138 | for(auto& file : fs::directory_iterator(networkConfig)) |
| 139 | { |
Ratan Gupta | 1bc8ed3 | 2017-07-25 22:34:19 +0530 | [diff] [blame] | 140 | fs::remove(file.path()); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | for (auto& intf : interfaces) |
| 144 | { |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 145 | auto fileName = systemd::config::networkFilePrefix + intf.first + |
| 146 | systemd::config::networkFileSuffix; |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 147 | |
Ratan Gupta | 4f67dac | 2017-08-28 22:18:21 +0530 | [diff] [blame] | 148 | fs::path filePath = networkConfig; |
| 149 | filePath /= fileName; |
| 150 | |
| 151 | bmc::writeDHCPDefault(filePath.string(), intf.first); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 152 | interfacesMapped = true; |
| 153 | } |
| 154 | |
| 155 | if(interfacesMapped) |
| 156 | { |
| 157 | log<level::INFO>("Network configuration reset to DHCP."); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | log<level::ERR>("No network interfaces are mapped."); |
Michael Tritz | d8cd8ed | 2017-08-31 15:05:18 -0500 | [diff] [blame] | 162 | elog<InternalFailure>(); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | log<level::ERR>("Network configuration directory not found!"); |
Michael Tritz | d8cd8ed | 2017-08-31 15:05:18 -0500 | [diff] [blame] | 168 | elog<InternalFailure>(); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return; |
| 172 | } |
| 173 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 174 | // Need to merge the below function with the code which writes the |
| 175 | // config file during factory reset. |
| 176 | //TODO openbmc/openbmc#1751 |
| 177 | void Manager::writeToConfigurationFile() |
| 178 | { |
| 179 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 180 | for (const auto& intf : interfaces) |
| 181 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 182 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 183 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 184 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame^] | 185 | restartNetwork(); |
| 186 | } |
| 187 | |
| 188 | void Manager::restartNetwork() |
| 189 | { |
| 190 | using namespace std::chrono; |
| 191 | |
| 192 | if (refreshTimer && !refreshTimer->isExpired()) |
| 193 | { |
| 194 | auto time = duration_cast<microseconds>( |
| 195 | phosphor::network::networkChangeTimeout); |
| 196 | refreshTimer->startTimer(time); |
| 197 | } |
| 198 | restartSystemdUnit("systemd-networkd.service"); |
Ratan Gupta | 70c7e5b | 2017-07-12 11:41:55 +0530 | [diff] [blame] | 199 | } |
| 200 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 201 | }//namespace network |
| 202 | }//namespace phosphor |