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