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" |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 9 | #include "util.hpp" |
| 10 | |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame^] | 11 | #include <linux/if_addr.h> |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 12 | #include <net/if.h> |
Ratan Gupta | 738a67f | 2017-04-21 10:38:05 +0530 | [diff] [blame] | 13 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 14 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 15 | #include <fstream> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 16 | #include <phosphor-logging/elog-errors.hpp> |
| 17 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 18 | #include <sdbusplus/message.hpp> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 19 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 20 | |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 21 | constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1"; |
| 22 | constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1"; |
| 23 | constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager"; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 24 | constexpr auto FirstBootFile = "/var/lib/network/firstBoot_"; |
William A. Kennington III | f1aa51c | 2019-02-12 19:58:11 -0800 | [diff] [blame] | 25 | |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 26 | constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1"; |
| 27 | constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1"; |
| 28 | constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager"; |
| 29 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 30 | namespace phosphor |
| 31 | { |
| 32 | namespace network |
| 33 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 34 | |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 35 | extern std::unique_ptr<Timer> refreshObjectTimer; |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 36 | extern std::unique_ptr<Timer> reloadTimer; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 37 | using namespace phosphor::logging; |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 38 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 39 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 40 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 41 | static constexpr const char enabledMatch[] = |
| 42 | "type='signal',sender='org.freedesktop.network1',path_namespace='/org/" |
| 43 | "freedesktop/network1/" |
| 44 | "link',interface='org.freedesktop.DBus.Properties',member='" |
| 45 | "PropertiesChanged',arg0='org.freedesktop.network1.Link',"; |
| 46 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 47 | Manager::Manager(sdbusplus::bus_t& bus, const char* objPath, |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 48 | const fs::path& confDir) : |
Patrick Williams | 166b959 | 2022-03-30 16:09:16 -0500 | [diff] [blame] | 49 | details::VLANCreateIface(bus, objPath, |
| 50 | details::VLANCreateIface::action::defer_emit), |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 51 | bus(bus), objectPath(objPath), |
| 52 | systemdNetworkdEnabledMatch( |
| 53 | bus, enabledMatch, [&](sdbusplus::message_t& m) { |
| 54 | std::string intf; |
| 55 | std::unordered_map<std::string, std::variant<std::string>> values; |
| 56 | try |
| 57 | { |
| 58 | m.read(intf, values); |
| 59 | auto it = values.find("AdministrativeState"); |
| 60 | if (it == values.end()) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | const std::string_view obj = m.get_path(); |
| 65 | auto sep = obj.rfind('/'); |
| 66 | if (sep == obj.npos || sep + 3 > obj.size()) |
| 67 | { |
| 68 | throw std::invalid_argument("Invalid obj path"); |
| 69 | } |
| 70 | auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3)); |
| 71 | const auto& state = std::get<std::string>(it->second); |
| 72 | handleAdminState(state, ifidx); |
| 73 | } |
| 74 | catch (const std::exception& e) |
| 75 | { |
| 76 | log<level::ERR>( |
| 77 | fmt::format("AdministrativeState match parsing failed: {}", |
| 78 | e.what()) |
| 79 | .c_str(), |
| 80 | entry("ERROR=%s", e.what())); |
| 81 | } |
| 82 | }) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 83 | { |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 84 | setConfDir(confDir); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 85 | std::vector< |
| 86 | std::tuple<int32_t, std::string, sdbusplus::message::object_path>> |
| 87 | links; |
| 88 | try |
| 89 | { |
| 90 | auto rsp = |
| 91 | bus.new_method_call("org.freedesktop.network1", |
| 92 | "/org/freedesktop/network1", |
| 93 | "org.freedesktop.network1.Manager", "ListLinks") |
| 94 | .call(); |
| 95 | rsp.read(links); |
| 96 | } |
| 97 | catch (const sdbusplus::exception::SdBusError& e) |
| 98 | { |
| 99 | // Any failures are systemd-network not being ready |
| 100 | } |
| 101 | for (const auto& link : links) |
| 102 | { |
| 103 | unsigned ifidx = std::get<0>(link); |
| 104 | auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx); |
| 105 | auto req = |
| 106 | bus.new_method_call("org.freedesktop.network1", obj.c_str(), |
| 107 | "org.freedesktop.DBus.Properties", "Get"); |
| 108 | req.append("org.freedesktop.network1.Link", "AdministrativeState"); |
| 109 | auto rsp = req.call(); |
| 110 | std::variant<std::string> val; |
| 111 | rsp.read(val); |
| 112 | handleAdminState(std::get<std::string>(val), ifidx); |
| 113 | } |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void Manager::setConfDir(const fs::path& dir) |
| 117 | { |
| 118 | confDir = dir; |
Ratan Gupta | 255d514 | 2017-08-10 09:02:08 +0530 | [diff] [blame] | 119 | |
| 120 | if (!fs::exists(confDir)) |
| 121 | { |
| 122 | if (!fs::create_directories(confDir)) |
| 123 | { |
| 124 | log<level::ERR>("Unable to create the network conf dir", |
| 125 | entry("DIR=%s", confDir.c_str())); |
| 126 | elog<InternalFailure>(); |
| 127 | } |
| 128 | } |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 129 | } |
| 130 | |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 131 | void Manager::createInterface(const UndiscoveredInfo& info, bool enabled) |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 132 | { |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 133 | removeInterface(info.intf); |
| 134 | config::Parser config(config::pathForIntfConf(confDir, *info.intf.name)); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 135 | auto intf = std::make_unique<EthernetInterface>( |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 136 | bus, *this, info.intf, objectPath, config, true, enabled); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 137 | intf->createIPAddressObjects(); |
| 138 | intf->createStaticNeighborObjects(); |
| 139 | intf->loadNameServers(config); |
| 140 | intf->loadNTPServers(config); |
| 141 | auto ptr = intf.get(); |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 142 | interfaces.insert_or_assign(*info.intf.name, std::move(intf)); |
| 143 | interfacesByIdx.insert_or_assign(info.intf.idx, ptr); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 144 | } |
| 145 | |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 146 | void Manager::addInterface(const InterfaceInfo& info) |
| 147 | { |
William A. Kennington III | b800612 | 2022-11-13 18:15:15 -0800 | [diff] [blame] | 148 | if (info.flags & IFF_LOOPBACK) |
| 149 | { |
| 150 | return; |
| 151 | } |
| 152 | if (!info.name) |
| 153 | { |
| 154 | throw std::invalid_argument("Interface missing name"); |
| 155 | } |
| 156 | const auto& ignored = internal::getIgnoredInterfaces(); |
| 157 | if (ignored.find(*info.name) != ignored.end()) |
| 158 | { |
| 159 | auto msg = fmt::format("Ignoring interface {}\n", *info.name); |
| 160 | log<level::INFO>(msg.c_str()); |
| 161 | return; |
| 162 | } |
| 163 | |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 164 | auto it = systemdNetworkdEnabled.find(info.idx); |
| 165 | if (it != systemdNetworkdEnabled.end()) |
| 166 | { |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 167 | createInterface({info}, it->second); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 168 | } |
| 169 | else |
| 170 | { |
William A. Kennington III | f30d560 | 2022-11-13 17:09:55 -0800 | [diff] [blame] | 171 | undiscoveredIntfInfo.insert_or_assign( |
| 172 | info.idx, UndiscoveredInfo{std::move(info)}); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | void Manager::removeInterface(const InterfaceInfo& info) |
| 177 | { |
| 178 | auto iit = interfacesByIdx.find(info.idx); |
| 179 | auto nit = interfaces.end(); |
| 180 | if (info.name) |
| 181 | { |
| 182 | nit = interfaces.find(*info.name); |
| 183 | if (nit != interfaces.end() && iit != interfacesByIdx.end() && |
| 184 | nit->second.get() != iit->second) |
| 185 | { |
| 186 | fmt::print(stderr, "Removed interface desync detected\n"); |
| 187 | fflush(stderr); |
| 188 | std::abort(); |
| 189 | } |
| 190 | } |
| 191 | else if (iit != interfacesByIdx.end()) |
| 192 | { |
| 193 | for (nit = interfaces.begin(); nit != interfaces.end(); ++nit) |
| 194 | { |
| 195 | if (nit->second.get() == iit->second) |
| 196 | { |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (iit != interfacesByIdx.end()) |
| 203 | { |
| 204 | interfacesByIdx.erase(iit); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | undiscoveredIntfInfo.erase(info.idx); |
| 209 | } |
| 210 | if (nit != interfaces.end()) |
| 211 | { |
| 212 | interfaces.erase(nit); |
| 213 | } |
| 214 | } |
| 215 | |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 216 | inline void getIntfOrLog(const decltype(Manager::interfacesByIdx)& intfs, |
| 217 | unsigned idx, auto&& cb) |
| 218 | { |
| 219 | auto it = intfs.find(idx); |
| 220 | if (it == intfs.end()) |
| 221 | { |
| 222 | auto msg = fmt::format("Interface `{}` not found", idx); |
| 223 | log<level::ERR>(msg.c_str(), entry("IFIDX=%u", idx)); |
| 224 | return; |
| 225 | } |
| 226 | cb(*it->second); |
| 227 | } |
| 228 | |
| 229 | void Manager::addAddress(const AddressInfo& info) |
| 230 | { |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame^] | 231 | if (info.flags & IFA_F_DEPRECATED) |
| 232 | { |
| 233 | return; |
| 234 | } |
| 235 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 236 | { |
| 237 | it->second->addAddr(info); |
| 238 | } |
| 239 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 240 | it != undiscoveredIntfInfo.end()) |
| 241 | { |
| 242 | it->second.addrs.insert_or_assign(info.ifaddr, info); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | throw std::runtime_error( |
| 247 | fmt::format("Interface `{}` not found for addr", info.ifidx)); |
| 248 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void Manager::removeAddress(const AddressInfo& info) |
| 252 | { |
William A. Kennington III | 57ca961 | 2022-11-14 15:26:47 -0800 | [diff] [blame^] | 253 | if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end()) |
| 254 | { |
| 255 | it->second->addrs.erase(info.ifaddr); |
| 256 | } |
| 257 | else if (auto it = undiscoveredIntfInfo.find(info.ifidx); |
| 258 | it != undiscoveredIntfInfo.end()) |
| 259 | { |
| 260 | it->second.addrs.erase(info.ifaddr); |
| 261 | } |
William A. Kennington III | ed5ff47 | 2022-11-12 16:24:02 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void Manager::addNeighbor(const NeighborInfo& info) |
| 265 | { |
| 266 | getIntfOrLog(interfacesByIdx, info.ifidx, |
| 267 | [&](auto& intf) { intf.addStaticNeigh(info); }); |
| 268 | } |
| 269 | |
| 270 | void Manager::removeNeighbor(const NeighborInfo& info) |
| 271 | { |
| 272 | if (info.addr) |
| 273 | { |
| 274 | getIntfOrLog(interfacesByIdx, info.ifidx, [&](auto& intf) { |
| 275 | intf.staticNeighbors.erase(*info.addr); |
| 276 | }); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void Manager::addDefGw(unsigned ifidx, InAddrAny addr) |
| 281 | { |
| 282 | getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) { |
| 283 | std::visit( |
| 284 | [&](auto addr) { |
| 285 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 286 | { |
| 287 | intf.EthernetInterfaceIntf::defaultGateway( |
| 288 | std::to_string(addr)); |
| 289 | } |
| 290 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 291 | { |
| 292 | intf.EthernetInterfaceIntf::defaultGateway6( |
| 293 | std::to_string(addr)); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 298 | } |
| 299 | }, |
| 300 | addr); |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | void Manager::removeDefGw(unsigned ifidx, InAddrAny addr) |
| 305 | { |
| 306 | getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) { |
| 307 | std::visit( |
| 308 | [&](auto addr) { |
| 309 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 310 | { |
| 311 | if (intf.defaultGateway() == std::to_string(addr)) |
| 312 | { |
| 313 | intf.EthernetInterfaceIntf::defaultGateway(""); |
| 314 | } |
| 315 | } |
| 316 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 317 | { |
| 318 | if (intf.defaultGateway6() == std::to_string(addr)) |
| 319 | { |
| 320 | intf.EthernetInterfaceIntf::defaultGateway6(""); |
| 321 | } |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 326 | } |
| 327 | }, |
| 328 | addr); |
| 329 | }); |
| 330 | } |
| 331 | |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 332 | void Manager::createInterfaces() |
| 333 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 334 | // clear all the interfaces first |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 335 | interfaces.clear(); |
William A. Kennington III | 67b09da | 2022-10-31 14:09:53 -0700 | [diff] [blame] | 336 | interfacesByIdx.clear(); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 337 | for (auto& info : system::getInterfaces()) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 338 | { |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 339 | addInterface(info); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 343 | void Manager::createChildObjects() |
| 344 | { |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 345 | routeTable.refresh(); |
| 346 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 347 | // creates the ethernet interface dbus object. |
| 348 | createInterfaces(); |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 349 | |
| 350 | systemConf.reset(nullptr); |
| 351 | dhcpConf.reset(nullptr); |
| 352 | |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 353 | fs::path objPath = objectPath; |
| 354 | objPath /= "config"; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 355 | |
| 356 | // create the system conf object. |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 357 | systemConf = std::make_unique<phosphor::network::SystemConfiguration>( |
Jiaqing Zhao | 24b5a61 | 2022-04-11 16:46:16 +0800 | [diff] [blame] | 358 | bus, objPath.string()); |
Ratan Gupta | d16f88c | 2017-07-11 17:47:57 +0530 | [diff] [blame] | 359 | // create the dhcp conf object. |
| 360 | objPath /= "dhcp"; |
| 361 | dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>( |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 362 | bus, objPath.string(), *this); |
Ratan Gupta | ef85eb9 | 2017-06-15 08:57:54 +0530 | [diff] [blame] | 363 | } |
| 364 | |
William A. Kennington III | 085bbdc | 2022-10-05 02:45:37 -0700 | [diff] [blame] | 365 | ObjectPath Manager::vlan(std::string interfaceName, uint32_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 366 | { |
Jiaqing Zhao | b685cb6 | 2022-04-12 22:57:34 +0800 | [diff] [blame] | 367 | if (id == 0 || id >= 4095) |
| 368 | { |
| 369 | log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id)); |
| 370 | elog<InvalidArgument>( |
| 371 | Argument::ARGUMENT_NAME("VLANId"), |
| 372 | Argument::ARGUMENT_VALUE(std::to_string(id).c_str())); |
| 373 | } |
| 374 | |
William A. Kennington III | 9644479 | 2022-10-05 15:16:22 -0700 | [diff] [blame] | 375 | auto it = interfaces.find(interfaceName); |
| 376 | if (it == interfaces.end()) |
| 377 | { |
| 378 | using ResourceErr = |
| 379 | phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound; |
| 380 | elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str())); |
| 381 | } |
| 382 | return it->second->createVLAN(id); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 383 | } |
| 384 | |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 385 | void Manager::reset() |
| 386 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 387 | if (fs::is_directory(confDir)) |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 388 | { |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 389 | for (const auto& file : fs::directory_iterator(confDir)) |
| 390 | { |
| 391 | fs::remove(file.path()); |
| 392 | } |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 393 | } |
William A. Kennington III | 9a1d9af | 2021-11-09 17:51:05 -0800 | [diff] [blame] | 394 | log<level::INFO>("Network Factory Reset queued."); |
Michael Tritz | 29f2fd6 | 2017-05-22 15:27:26 -0500 | [diff] [blame] | 395 | } |
| 396 | |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 397 | // Need to merge the below function with the code which writes the |
| 398 | // config file during factory reset. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 399 | // TODO openbmc/openbmc#1751 |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 400 | void Manager::writeToConfigurationFile() |
| 401 | { |
| 402 | // write all the static ip address in the systemd-network conf file |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 403 | for (const auto& intf : interfaces) |
| 404 | { |
Ratan Gupta | 2b10653 | 2017-07-25 16:05:02 +0530 | [diff] [blame] | 405 | intf.second->writeConfigurationFile(); |
Ratan Gupta | 4f1c18b | 2017-05-25 12:59:35 +0530 | [diff] [blame] | 406 | } |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 407 | } |
| 408 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 409 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 410 | void Manager::setFistBootMACOnInterface( |
| 411 | const std::pair<std::string, std::string>& inventoryEthPair) |
| 412 | { |
| 413 | for (const auto& interface : interfaces) |
| 414 | { |
| 415 | if (interface.first == inventoryEthPair.first) |
| 416 | { |
| 417 | auto returnMAC = |
Patrick Williams | 6aef769 | 2021-05-01 06:39:41 -0500 | [diff] [blame] | 418 | interface.second->macAddress(inventoryEthPair.second); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 419 | if (returnMAC == inventoryEthPair.second) |
| 420 | { |
| 421 | log<level::INFO>("Set the MAC on "), |
| 422 | entry("interface : ", interface.first.c_str()), |
| 423 | entry("MAC : ", inventoryEthPair.second.c_str()); |
| 424 | std::error_code ec; |
| 425 | if (std::filesystem::is_directory("/var/lib/network", ec)) |
| 426 | { |
| 427 | std::ofstream persistentFile(FirstBootFile + |
| 428 | interface.first); |
| 429 | } |
| 430 | break; |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | log<level::INFO>("MAC is Not Set on ethernet Interface"); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | #endif |
| 441 | |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 442 | void Manager::reloadConfigsNoRefresh() |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 443 | { |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 444 | reloadTimer->restartOnce(reloadTimeout); |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void Manager::reloadConfigs() |
| 448 | { |
| 449 | reloadConfigsNoRefresh(); |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 450 | // Ensure that the next refresh happens after reconfiguration |
| 451 | refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout); |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void Manager::doReloadConfigs() |
| 455 | { |
William A. Kennington III | 6ff633a | 2021-11-09 17:09:12 -0800 | [diff] [blame] | 456 | for (auto& hook : reloadPreHooks) |
| 457 | { |
| 458 | try |
| 459 | { |
| 460 | hook(); |
| 461 | } |
| 462 | catch (const std::exception& ex) |
| 463 | { |
| 464 | log<level::ERR>("Failed executing reload hook, ignoring", |
| 465 | entry("ERR=%s", ex.what())); |
| 466 | } |
| 467 | } |
| 468 | reloadPreHooks.clear(); |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 469 | try |
| 470 | { |
| 471 | auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH, |
| 472 | NETWORKD_INTERFACE, "Reload"); |
| 473 | bus.call_noreply(method); |
| 474 | } |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 475 | catch (const sdbusplus::exception_t& ex) |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 476 | { |
| 477 | log<level::ERR>("Failed to reload configuration", |
| 478 | entry("ERR=%s", ex.what())); |
| 479 | elog<InternalFailure>(); |
| 480 | } |
William A. Kennington III | d41db38 | 2021-11-09 20:42:29 -0800 | [diff] [blame] | 481 | // Ensure reconfiguration has enough time |
William A. Kennington III | 85dc57a | 2022-11-07 16:53:24 -0800 | [diff] [blame] | 482 | if (refreshObjectTimer->isEnabled()) |
| 483 | { |
| 484 | refreshObjectTimer->setRemaining(refreshTimeout); |
| 485 | } |
William A. Kennington III | 56ecc78 | 2021-10-07 18:44:50 -0700 | [diff] [blame] | 486 | } |
| 487 | |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 488 | void Manager::handleAdminState(std::string_view state, unsigned ifidx) |
| 489 | { |
| 490 | if (state == "initialized" || state == "linger") |
| 491 | { |
| 492 | systemdNetworkdEnabled.erase(ifidx); |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | bool managed = state != "unmanaged"; |
| 497 | systemdNetworkdEnabled.insert_or_assign(ifidx, managed); |
| 498 | if (auto it = undiscoveredIntfInfo.find(ifidx); |
| 499 | it != undiscoveredIntfInfo.end()) |
| 500 | { |
| 501 | auto info = std::move(it->second); |
| 502 | undiscoveredIntfInfo.erase(it); |
William A. Kennington III | 0813a24 | 2022-11-12 18:07:11 -0800 | [diff] [blame] | 503 | createInterface(info, managed); |
William A. Kennington III | 80d2901 | 2022-11-12 02:31:40 -0800 | [diff] [blame] | 504 | } |
| 505 | else if (auto it = interfacesByIdx.find(ifidx); |
| 506 | it != interfacesByIdx.end()) |
| 507 | { |
| 508 | it->second->EthernetInterfaceIntf::nicEnabled(managed); |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 513 | } // namespace network |
| 514 | } // namespace phosphor |