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