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