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