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