blob: c41292394765af44634eeed22a1b5c19df1a0ae6 [file] [log] [blame]
Ratan Gupta6811f822017-04-14 16:34:56 +05301#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07003#include "config_parser.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05304#include "ipaddress.hpp"
William A. Kennington III2e09d272022-10-14 17:15:00 -07005#include "system_queries.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07006#include "types.hpp"
William A. Kennington IIIb8006122022-11-13 18:15:15 -08007#include "util.hpp"
8
William A. Kennington III57ca9612022-11-14 15:26:47 -08009#include <linux/if_addr.h>
William A. Kennington III7310ac72022-11-14 15:44:00 -080010#include <linux/neighbour.h>
William A. Kennington IIIb8006122022-11-13 18:15:15 -080011#include <net/if.h>
William A. Kennington III9c441fd2023-02-24 13:40:01 -080012#include <net/if_arp.h>
Ratan Gupta738a67f2017-04-21 10:38:05 +053013
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -070015#include <phosphor-logging/lg2.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080016#include <sdbusplus/message.hpp>
William A. Kennington III9b2a20d2023-06-17 14:05:48 -070017#include <stdplus/numeric/str.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080018#include <stdplus/pinned.hpp>
William A. Kennington IIIb7401962023-07-21 16:36:11 -070019#include <stdplus/print.hpp>
William A. Kennington IIIcafc1512023-07-25 02:22:32 -070020#include <stdplus/str/cat.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070021#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053022
Patrick Williams89d734b2023-05-10 07:50:25 -050023#include <filesystem>
William A. Kennington IIIcafc1512023-07-25 02:22:32 -070024#include <format>
Patrick Williams89d734b2023-05-10 07:50:25 -050025
Ratan Gupta6811f822017-04-14 16:34:56 +053026namespace phosphor
27{
28namespace network
29{
Ratan Gupta82549cc2017-04-21 08:45:23 +053030
Ratan Gupta6811f822017-04-14 16:34:56 +053031using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053032using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080033using Argument = xyz::openbmc_project::Common::InvalidArgument;
William A. Kennington IIIcafc1512023-07-25 02:22:32 -070034using std::literals::string_view_literals::operator""sv;
Ratan Gupta6811f822017-04-14 16:34:56 +053035
William A. Kennington III80d29012022-11-12 02:31:40 -080036static 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 III9ede1b72022-11-21 01:59:28 -080042Manager::Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
William A. Kennington IIId40046b2023-06-30 20:36:11 -070043 stdplus::PinnedRef<DelayedExecutor> reload,
44 stdplus::zstring_view objPath,
William A. Kennington III5b179382022-11-15 15:23:26 -080045 const std::filesystem::path& confDir) :
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080046 ManagerIface(bus, objPath.c_str(), ManagerIface::action::defer_emit),
47 reload(reload), bus(bus), objPath(std::string(objPath)), confDir(confDir),
William A. Kennington III80d29012022-11-12 02:31:40 -080048 systemdNetworkdEnabledMatch(
William A. Kennington III9ede1b72022-11-21 01:59:28 -080049 bus, enabledMatch,
50 [man = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
Patrick Williamsad205022024-08-16 15:20:07 -040051 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 =
68 stdplus::StrToInt<10, uint16_t>{}(obj.substr(sep + 3));
69 const auto& state = std::get<std::string>(it->second);
70 man.get().handleAdminState(state, ifidx);
71 }
72 catch (const std::exception& e)
73 {
74 lg2::error("AdministrativeState match parsing failed: {ERROR}",
75 "ERROR", e);
76 }
77 })
Ratan Gupta6811f822017-04-14 16:34:56 +053078{
William A. Kennington III5c1e1602023-09-08 13:26:56 -070079 reload.get().setCallback([self = stdplus::PinnedRef(*this)]() {
80 for (auto& hook : self.get().reloadPreHooks)
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080081 {
82 try
83 {
84 hook();
85 }
86 catch (const std::exception& ex)
87 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -070088 lg2::error("Failed executing reload hook, ignoring: {ERROR}",
89 "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080090 }
91 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -070092 self.get().reloadPreHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080093 try
94 {
William A. Kennington III5c1e1602023-09-08 13:26:56 -070095 self.get()
96 .bus.get()
William A. Kennington IIIaf77f822023-06-23 02:17:20 -070097 .new_method_call("org.freedesktop.network1",
98 "/org/freedesktop/network1",
99 "org.freedesktop.network1.Manager", "Reload")
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800100 .call();
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700101 lg2::info("Reloaded systemd-networkd");
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800102 }
103 catch (const sdbusplus::exception_t& ex)
104 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700105 lg2::error("Failed to reload configuration: {ERROR}", "ERROR", ex);
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700106 self.get().reloadPostHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800107 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700108 for (auto& hook : self.get().reloadPostHooks)
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800109 {
110 try
111 {
112 hook();
113 }
114 catch (const std::exception& ex)
115 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700116 lg2::error("Failed executing reload hook, ignoring: {ERROR}",
117 "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800118 }
119 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700120 self.get().reloadPostHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800121 });
William A. Kennington III80d29012022-11-12 02:31:40 -0800122 std::vector<
123 std::tuple<int32_t, std::string, sdbusplus::message::object_path>>
124 links;
125 try
126 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800127 auto rsp = bus.get()
128 .new_method_call("org.freedesktop.network1",
129 "/org/freedesktop/network1",
130 "org.freedesktop.network1.Manager",
131 "ListLinks")
132 .call();
William A. Kennington III80d29012022-11-12 02:31:40 -0800133 rsp.read(links);
134 }
135 catch (const sdbusplus::exception::SdBusError& e)
136 {
137 // Any failures are systemd-network not being ready
138 }
139 for (const auto& link : links)
140 {
141 unsigned ifidx = std::get<0>(link);
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700142 stdplus::ToStrHandle<stdplus::IntToStr<10, unsigned>> tsh;
Patrick Williamsad205022024-08-16 15:20:07 -0400143 auto obj =
144 stdplus::strCat("/org/freedesktop/network1/link/_3"sv, tsh(ifidx));
William A. Kennington III80d29012022-11-12 02:31:40 -0800145 auto req =
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800146 bus.get().new_method_call("org.freedesktop.network1", obj.c_str(),
147 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III80d29012022-11-12 02:31:40 -0800148 req.append("org.freedesktop.network1.Link", "AdministrativeState");
149 auto rsp = req.call();
150 std::variant<std::string> val;
151 rsp.read(val);
152 handleAdminState(std::get<std::string>(val), ifidx);
153 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530154
William A. Kennington III5b179382022-11-15 15:23:26 -0800155 std::filesystem::create_directories(confDir);
156 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
157 bus, (this->objPath / "config").str);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530158}
159
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800160void Manager::createInterface(const AllIntfInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800161{
William A. Kennington III93f5c6d2022-11-17 16:23:44 -0800162 if (ignoredIntf.find(info.intf.idx) != ignoredIntf.end())
163 {
164 return;
165 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800166 if (auto it = interfacesByIdx.find(info.intf.idx);
167 it != interfacesByIdx.end())
168 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800169 if (info.intf.name && *info.intf.name != it->second->interfaceName())
170 {
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800171 interfaces.erase(it->second->interfaceName());
172 interfacesByIdx.erase(it);
William A. Kennington III5b179382022-11-15 15:23:26 -0800173 }
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800174 else
175 {
176 it->second->updateInfo(info.intf);
177 return;
178 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800179 }
William A. Kennington III876927c2022-11-17 16:20:15 -0800180 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 III3ee5b7e2022-11-15 15:04:37 -0800189 if (!info.intf.name)
190 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700191 lg2::error("Can't create interface without name: {NET_IDX}", "NET_IDX",
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700192 info.intf.idx);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800193 return;
194 }
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800195 config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
William A. Kennington III80d29012022-11-12 02:31:40 -0800196 auto intf = std::make_unique<EthernetInterface>(
William A. Kennington III13d665c2022-11-15 20:34:40 -0800197 bus, *this, info, objPath.str, config, enabled);
William A. Kennington III80d29012022-11-12 02:31:40 -0800198 intf->loadNameServers(config);
199 intf->loadNTPServers(config);
200 auto ptr = intf.get();
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800201 interfaces.insert_or_assign(*info.intf.name, std::move(intf));
202 interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
William A. Kennington III80d29012022-11-12 02:31:40 -0800203}
204
William A. Kennington III0813a242022-11-12 18:07:11 -0800205void Manager::addInterface(const InterfaceInfo& info)
206{
William A. Kennington III9c441fd2023-02-24 13:40:01 -0800207 if (info.type != ARPHRD_ETHER)
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800208 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800209 ignoredIntf.emplace(info.idx);
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800210 return;
211 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800212 if (info.name)
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800213 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800214 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 III1d25ca42023-05-30 16:55:28 -0700221 lg2::info("Ignoring interface {NET_INTF}", "NET_INTF",
222 *info.name);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800223 }
224 ignoredIntf.emplace(info.idx);
225 return;
226 }
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800227 }
228
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800229 auto infoIt = intfInfo.find(info.idx);
230 if (infoIt != intfInfo.end())
William A. Kennington III0813a242022-11-12 18:07:11 -0800231 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800232 infoIt->second.intf = info;
William A. Kennington III0813a242022-11-12 18:07:11 -0800233 }
234 else
235 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800236 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 III0813a242022-11-12 18:07:11 -0800243 }
244}
245
246void 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 IIIb7401962023-07-21 16:36:11 -0700256 stdplus::print(stderr, "Removed interface desync detected\n");
William A. Kennington III0813a242022-11-12 18:07:11 -0800257 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 III3ee5b7e2022-11-15 15:04:37 -0800278 ignoredIntf.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800279 }
280 if (nit != interfaces.end())
281 {
282 interfaces.erase(nit);
283 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800284 intfInfo.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800285}
286
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800287void Manager::addAddress(const AddressInfo& info)
288{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800289 if (info.flags & IFA_F_DEPRECATED)
290 {
291 return;
292 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800293 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III57ca9612022-11-14 15:26:47 -0800294 {
295 it->second.addrs.insert_or_assign(info.ifaddr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800296 if (auto it = interfacesByIdx.find(info.ifidx);
297 it != interfacesByIdx.end())
298 {
299 it->second->addAddr(info);
300 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800301 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800302 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III57ca9612022-11-14 15:26:47 -0800303 {
304 throw std::runtime_error(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700305 std::format("Interface `{}` not found for addr", info.ifidx));
William A. Kennington III57ca9612022-11-14 15:26:47 -0800306 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800307}
308
309void Manager::removeAddress(const AddressInfo& info)
310{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800311 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
312 {
313 it->second->addrs.erase(info.ifaddr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800314 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
315 {
316 it->second.addrs.erase(info.ifaddr);
317 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800318 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800319}
320
321void Manager::addNeighbor(const NeighborInfo& info)
322{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800323 if (!(info.state & NUD_PERMANENT) || !info.addr)
324 {
325 return;
326 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800327 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800328 {
329 it->second.staticNeighs.insert_or_assign(*info.addr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800330 if (auto it = interfacesByIdx.find(info.ifidx);
331 it != interfacesByIdx.end())
332 {
333 it->second->addStaticNeigh(info);
334 }
William A. Kennington III7310ac72022-11-14 15:44:00 -0800335 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800336 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III7310ac72022-11-14 15:44:00 -0800337 {
338 throw std::runtime_error(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700339 std::format("Interface `{}` not found for neigh", info.ifidx));
William A. Kennington III7310ac72022-11-14 15:44:00 -0800340 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800341}
342
343void Manager::removeNeighbor(const NeighborInfo& info)
344{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800345 if (!info.addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800346 {
William A. Kennington III7310ac72022-11-14 15:44:00 -0800347 return;
348 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800349 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800350 {
351 it->second.staticNeighs.erase(*info.addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800352 if (auto it = interfacesByIdx.find(info.ifidx);
353 it != interfacesByIdx.end())
354 {
355 it->second->staticNeighbors.erase(*info.addr);
356 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800357 }
358}
359
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700360void Manager::addDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800361{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800362 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800363 {
364 std::visit(
365 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700366 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500367 {
Patrick Williamsad205022024-08-16 15:20:07 -0400368 it->second.defgw4.emplace(addr);
Patrick Williams89d734b2023-05-10 07:50:25 -0500369 }
370 else
371 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700372 static_assert(
373 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
Patrick Williamsad205022024-08-16 15:20:07 -0400374 it->second.defgw6.emplace(addr);
Patrick Williams89d734b2023-05-10 07:50:25 -0500375 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500376 },
Patrick Williamsad205022024-08-16 15:20:07 -0400377 addr);
378 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
379 {
380 std::visit(
381 [&](auto addr) {
382 if constexpr (std::is_same_v<stdplus::In4Addr,
383 decltype(addr)>)
384 {
385 it->second->EthernetInterfaceIntf::defaultGateway(
386 stdplus::toStr(addr));
387 }
388 else
389 {
390 static_assert(
391 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
392 it->second->EthernetInterfaceIntf::defaultGateway6(
393 stdplus::toStr(addr));
394 }
395 },
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800396 addr);
397 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800398 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800399 else if (!ignoredIntf.contains(ifidx))
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800400 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700401 lg2::error("Interface {NET_IDX} not found for gw", "NET_IDX", ifidx);
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800402 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800403}
404
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700405void Manager::removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800406{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800407 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800408 {
409 std::visit(
410 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700411 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500412 {
Patrick Williamsad205022024-08-16 15:20:07 -0400413 if (it->second.defgw4 == addr)
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800414 {
Patrick Williamsad205022024-08-16 15:20:07 -0400415 it->second.defgw4.reset();
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800416 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500417 }
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700418 else
Patrick Williams89d734b2023-05-10 07:50:25 -0500419 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700420 static_assert(
421 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
Patrick Williamsad205022024-08-16 15:20:07 -0400422 if (it->second.defgw6 == addr)
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800423 {
Patrick Williamsad205022024-08-16 15:20:07 -0400424 it->second.defgw6.reset();
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800425 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500426 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500427 },
Patrick Williamsad205022024-08-16 15:20:07 -0400428 addr);
429 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
430 {
431 std::visit(
432 [&](auto addr) {
433 if constexpr (std::is_same_v<stdplus::In4Addr,
434 decltype(addr)>)
435 {
436 stdplus::ToStrHandle<stdplus::ToStr<stdplus::In4Addr>>
437 tsh;
438 if (it->second->defaultGateway() == tsh(addr))
439 {
440 it->second->EthernetInterfaceIntf::defaultGateway(
441 "");
442 }
443 }
444 else
445 {
446 static_assert(
447 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
448 stdplus::ToStrHandle<stdplus::ToStr<stdplus::In6Addr>>
449 tsh;
450 if (it->second->defaultGateway6() == tsh(addr))
451 {
452 it->second->EthernetInterfaceIntf::defaultGateway6(
453 "");
454 }
455 }
456 },
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800457 addr);
458 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800459 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800460}
461
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700462ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530463{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800464 if (id == 0 || id >= 4095)
465 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700466 lg2::error("VLAN ID {NET_VLAN} is not valid", "NET_VLAN", id);
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800467 elog<InvalidArgument>(
468 Argument::ARGUMENT_NAME("VLANId"),
469 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
470 }
471
William A. Kennington III96444792022-10-05 15:16:22 -0700472 auto it = interfaces.find(interfaceName);
473 if (it == interfaces.end())
474 {
475 using ResourceErr =
476 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
477 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
478 }
479 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530480}
481
Michael Tritz29f2fd62017-05-22 15:27:26 -0500482void Manager::reset()
483{
William A. Kennington III5b179382022-11-15 15:23:26 -0800484 for (const auto& dirent : std::filesystem::directory_iterator(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500485 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800486 std::error_code ec;
487 std::filesystem::remove(dirent.path(), ec);
Michael Tritz29f2fd62017-05-22 15:27:26 -0500488 }
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700489 lg2::info("Network data purged.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500490}
491
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530492void Manager::writeToConfigurationFile()
493{
494 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530495 for (const auto& intf : interfaces)
496 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530497 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530498 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530499}
500
William A. Kennington III80d29012022-11-12 02:31:40 -0800501void Manager::handleAdminState(std::string_view state, unsigned ifidx)
502{
503 if (state == "initialized" || state == "linger")
504 {
505 systemdNetworkdEnabled.erase(ifidx);
506 }
507 else
508 {
509 bool managed = state != "unmanaged";
510 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800511 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
William A. Kennington III80d29012022-11-12 02:31:40 -0800512 {
513 it->second->EthernetInterfaceIntf::nicEnabled(managed);
514 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800515 else if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
516 {
517 createInterface(it->second, managed);
518 }
William A. Kennington III80d29012022-11-12 02:31:40 -0800519 }
520}
521
Gunnar Mills57d9c502018-09-14 14:42:34 -0500522} // namespace network
523} // namespace phosphor