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 | |
William A. Kennington III | 09f3a4a | 2022-10-25 02:59:16 -0700 | [diff] [blame] | 5 | #include "config_parser.hpp" |
Ratan Gupta | 5978dd1 | 2017-07-25 13:47:13 +0530 | [diff] [blame] | 6 | #include "ipaddress.hpp" |
William A. Kennington III | 2e09d27 | 2022-10-14 17:15:00 -0700 | [diff] [blame] | 7 | #include "system_queries.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 8 | #include "types.hpp" |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 9 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 10 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 14 | #include <sdbusplus/message.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 16 | |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 17 | constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1"; |
| 18 | constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1"; |
| 19 | constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 20 | constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 21 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 22 | constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1"; |
| 23 | constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1"; |
| 24 | constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager"; |
| 25 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 26 | namespace phosphor |
| 27 | { |
| 28 | namespace network |
| 29 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 30 | |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 31 | extern std::unique_ptr<Timer> refreshObjectTimer; |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 32 | extern std::unique_ptr<Timer> reloadTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 33 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 34 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 35 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 36 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 37 | static constexpr const char enabledMatch[] = |
| 38 | "type='signal',sender='org.freedesktop.network1',path_namespace='/org/" |
| 39 | "freedesktop/network1/" |
| 40 | "link',interface='org.freedesktop.DBus.Properties',member='" |
| 41 | "PropertiesChanged',arg0='org.freedesktop.network1.Link',"; |
| 42 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 43 | Manager::Manager(sdbusplus::bus_t& bus, const char* objPath, |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 44 | const fs::path& confDir) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 45 | details::VLANCreateIface(bus, objPath, |
| 46 | details::VLANCreateIface::action::defer_emit), |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 47 | bus(bus), objectPath(objPath), |
| 48 | systemdNetworkdEnabledMatch( |
| 49 | bus, enabledMatch, [&](sdbusplus::message_t& m) { |
| 50 | std::string intf; |
| 51 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 52 | try |
| 53 | { |
| 54 | m.read(intf, values); |
| 55 | auto it = values.find("AdministrativeState"); |
| 56 | if (it == values.end()) |
| 57 | { |
| 58 | return; |
| 59 | } |
| 60 | const std::string_view obj = m.get_path(); |
| 61 | auto sep = obj.rfind('/'); |
| 62 | if (sep == obj.npos || sep + 3 > obj.size()) |
| 63 | { |
| 64 | throw std::invalid_argument("Invalid obj path"); |
| 65 | } |
| 66 | auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3)); |
| 67 | const auto& state = std::get<std::string>(it->second); |
| 68 | handleAdminState(state, ifidx); |
| 69 | } |
| 70 | catch (const std::exception& e) |
| 71 | { |
| 72 | log<level::ERR>( |
| 73 | fmt::format("AdministrativeState match parsing failed: {}", |
| 74 | e.what()) |
| 75 | .c_str(), |
| 76 | entry("ERROR=%s", e.what())); |
| 77 | } |
| 78 | }) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 79 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 80 | setConfDir(confDir); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 81 | std::vector< |
| 82 | std::tuple<int32_t, std::string, sdbusplus::message::object_path>> |
| 83 | links; |
| 84 | try |
| 85 | { |
| 86 | auto rsp = |
| 87 | bus.new_method_call("org.freedesktop.network1", |
| 88 | "/org/freedesktop/network1", |
| 89 | "org.freedesktop.network1.Manager", "ListLinks") |
| 90 | .call(); |
| 91 | rsp.read(links); |
| 92 | } |
| 93 | catch (const sdbusplus::exception::SdBusError& e) |
| 94 | { |
| 95 | // Any failures are systemd-network not being ready |
| 96 | } |
| 97 | for (const auto& link : links) |
| 98 | { |
| 99 | unsigned ifidx = std::get<0>(link); |
| 100 | auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx); |
| 101 | auto req = |
| 102 | bus.new_method_call("org.freedesktop.network1", obj.c_str(), |
| 103 | "org.freedesktop.DBus.Properties", "Get"); |
| 104 | req.append("org.freedesktop.network1.Link", "AdministrativeState"); |
| 105 | auto rsp = req.call(); |
| 106 | std::variant<std::string> val; |
| 107 | rsp.read(val); |
| 108 | handleAdminState(std::get<std::string>(val), ifidx); |
| 109 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void Manager::setConfDir(const fs::path& dir) |
| 113 | { |
| 114 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 115 | |
| 116 | if (!fs::exists(confDir)) |
| 117 | { |
| 118 | if (!fs::create_directories(confDir)) |
| 119 | { |
| 120 | log<level::ERR>("Unable to create the network conf dir", |
| 121 | entry("DIR=%s", confDir.c_str())); |
| 122 | elog<InternalFailure>(); |
| 123 | } |
| 124 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 125 | } |
| 126 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 127 | void Manager::addInterface(InterfaceInfo& info, bool enabled) |
| 128 | { |
| 129 | config::Parser config(config::pathForIntfConf(confDir, *info.name)); |
| 130 | auto intf = std::make_unique<EthernetInterface>( |
| 131 | bus, *this, info, objectPath, config, true, enabled); |
| 132 | intf->createIPAddressObjects(); |
| 133 | intf->createStaticNeighborObjects(); |
| 134 | intf->loadNameServers(config); |
| 135 | intf->loadNTPServers(config); |
| 136 | auto ptr = intf.get(); |
| 137 | interfaces.emplace(std::move(*info.name), std::move(intf)); |
| 138 | interfacesByIdx.emplace(info.idx, ptr); |
| 139 | } |
| 140 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 141 | void Manager::createInterfaces() |
| 142 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 143 | // clear all the interfaces first |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 144 | interfaces.clear(); |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 145 | interfacesByIdx.clear(); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 146 | for (auto& info : system::getInterfaces()) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 147 | { |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 148 | auto it = systemdNetworkdEnabled.find(info.idx); |
| 149 | if (it != systemdNetworkdEnabled.end()) |
| 150 | { |
| 151 | addInterface(info, it->second); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | undiscoveredIntfInfo.insert_or_assign(info.idx, std::move(info)); |
| 156 | } |
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 | { |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 162 | routeTable.refresh(); |
| 163 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 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>( |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 175 | bus, objPath.string()); |
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>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 179 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 180 | } |
| 181 | |
William A. Kennington III | 085bbdc | 2022-10-05 02:45:37 -0700 | [diff] [blame] | 182 | ObjectPath Manager::vlan(std::string interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 183 | { |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 184 | if (id == 0 || id >= 4095) |
| 185 | { |
| 186 | log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id)); |
| 187 | elog<InvalidArgument>( |
| 188 | Argument::ARGUMENT_NAME("VLANId"), |
| 189 | Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); |
| 190 | } |
| 191 | |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 192 | auto it = interfaces.find(interfaceName); |
| 193 | if (it == interfaces.end()) |
| 194 | { |
| 195 | using ResourceErr = |
| 196 | phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; |
| 197 | elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); |
| 198 | } |
| 199 | return it->second->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 200 | } |
| 201 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 202 | void Manager::reset() |
| 203 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 204 | if (fs::is_directory(confDir)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 205 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 206 | for (const auto& file : fs::directory_iterator(confDir)) |
| 207 | { |
| 208 | fs::remove(file.path()); |
| 209 | } |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 210 | } |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 211 | log<level::INFO>("Network Factory Reset queued."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 214 | // Need to merge the below function with the code which writes the |
| 215 | // config file during factory reset. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 216 | // TODO openbmc/openbmc#1751 |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 217 | void Manager::writeToConfigurationFile() |
| 218 | { |
| 219 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 220 | for (const auto& intf : interfaces) |
| 221 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 222 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 223 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 224 | } |
| 225 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 226 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 227 | void Manager::setFistBootMACOnInterface( |
| 228 | const std::pair<std::string, std::string>& inventoryEthPair) |
| 229 | { |
| 230 | for (const auto& interface : interfaces) |
| 231 | { |
| 232 | if (interface.first == inventoryEthPair.first) |
| 233 | { |
| 234 | auto returnMAC = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 235 | interface.second->macAddress(inventoryEthPair.second); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 236 | if (returnMAC == inventoryEthPair.second) |
| 237 | { |
| 238 | log<level::INFO>("Set the MAC on "), |
| 239 | entry("interface : ", interface.first.c_str()), |
| 240 | entry("MAC : ", inventoryEthPair.second.c_str()); |
| 241 | std::error_code ec; |
| 242 | if (std::filesystem::is_directory("/var/lib/network", ec)) |
| 243 | { |
| 244 | std::ofstream persistentFile(FirstBootFile + |
| 245 | interface.first); |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | else |
| 250 | { |
| 251 | log<level::INFO>("MAC is Not Set on ethernet Interface"); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #endif |
| 258 | |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 259 | void Manager::reloadConfigsNoRefresh() |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 260 | { |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 261 | reloadTimer->restartOnce(reloadTimeout); |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void Manager::reloadConfigs() |
| 265 | { |
| 266 | reloadConfigsNoRefresh(); |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 267 | // Ensure that the next refresh happens after reconfiguration |
| 268 | refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout); |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void Manager::doReloadConfigs() |
| 272 | { |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 273 | for (auto& hook : reloadPreHooks) |
| 274 | { |
| 275 | try |
| 276 | { |
| 277 | hook(); |
| 278 | } |
| 279 | catch (const std::exception& ex) |
| 280 | { |
| 281 | log<level::ERR>("Failed executing reload hook, ignoring", |
| 282 | entry("ERR=%s", ex.what())); |
| 283 | } |
| 284 | } |
| 285 | reloadPreHooks.clear(); |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 286 | try |
| 287 | { |
| 288 | auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, |
| 289 | NETWORKD_INTERFACE, "Reload"); |
| 290 | bus.call_noreply(method); |
| 291 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 292 | catch (const sdbusplus::exception_t& ex) |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 293 | { |
| 294 | log<level::ERR>("Failed to reload configuration", |
| 295 | entry("ERR=%s", ex.what())); |
| 296 | elog<InternalFailure>(); |
| 297 | } |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 298 | // Ensure reconfiguration has enough time |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 299 | if (refreshObjectTimer->isEnabled()) |
| 300 | { |
| 301 | refreshObjectTimer->setRemaining(refreshTimeout); |
| 302 | } |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 303 | } |
| 304 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame^] | 305 | void Manager::handleAdminState(std::string_view state, unsigned ifidx) |
| 306 | { |
| 307 | if (state == "initialized" || state == "linger") |
| 308 | { |
| 309 | systemdNetworkdEnabled.erase(ifidx); |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | bool managed = state != "unmanaged"; |
| 314 | systemdNetworkdEnabled.insert_or_assign(ifidx, managed); |
| 315 | if (auto it = undiscoveredIntfInfo.find(ifidx); |
| 316 | it != undiscoveredIntfInfo.end()) |
| 317 | { |
| 318 | auto info = std::move(it->second); |
| 319 | undiscoveredIntfInfo.erase(it); |
| 320 | addInterface(info, managed); |
| 321 | } |
| 322 | else if (auto it = interfacesByIdx.find(ifidx); |
| 323 | it != interfacesByIdx.end()) |
| 324 | { |
| 325 | it->second->EthernetInterfaceIntf::nicEnabled(managed); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 330 | } // namespace network |
| 331 | } // namespace phosphor |