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