blob: 72e0d03a2914ea217e3eb88dd38d5a842515f5fe [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>
Ratan Gupta738a67f2017-04-21 10:38:05 +053012
Patrick Venture189d44e2018-07-09 12:30:59 -070013#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -070014#include <phosphor-logging/lg2.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080015#include <sdbusplus/message.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080016#include <stdplus/pinned.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053018
Patrick Williams89d734b2023-05-10 07:50:25 -050019#include <filesystem>
20
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080021constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
22constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
23constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
24
William A. Kennington III56ecc782021-10-07 18:44:50 -070025constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
26constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
27constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
28
Ratan Gupta6811f822017-04-14 16:34:56 +053029namespace phosphor
30{
31namespace network
32{
Ratan Gupta82549cc2017-04-21 08:45:23 +053033
Ratan Gupta6811f822017-04-14 16:34:56 +053034using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080036using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053037
William A. Kennington III80d29012022-11-12 02:31:40 -080038static constexpr const char enabledMatch[] =
39 "type='signal',sender='org.freedesktop.network1',path_namespace='/org/"
40 "freedesktop/network1/"
41 "link',interface='org.freedesktop.DBus.Properties',member='"
42 "PropertiesChanged',arg0='org.freedesktop.network1.Link',";
43
William A. Kennington III9ede1b72022-11-21 01:59:28 -080044Manager::Manager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
45 DelayedExecutor& reload, stdplus::zstring_view objPath,
William A. Kennington III5b179382022-11-15 15:23:26 -080046 const std::filesystem::path& confDir) :
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080047 ManagerIface(bus, objPath.c_str(), ManagerIface::action::defer_emit),
48 reload(reload), bus(bus), objPath(std::string(objPath)), confDir(confDir),
William A. Kennington III80d29012022-11-12 02:31:40 -080049 systemdNetworkdEnabledMatch(
William A. Kennington III9ede1b72022-11-21 01:59:28 -080050 bus, enabledMatch,
51 [man = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
Patrick Williams89d734b2023-05-10 07:50:25 -050052 std::string intf;
53 std::unordered_map<std::string, std::variant<std::string>> values;
54 try
55 {
56 m.read(intf, values);
57 auto it = values.find("AdministrativeState");
58 if (it == values.end())
59 {
60 return;
61 }
62 const std::string_view obj = m.get_path();
63 auto sep = obj.rfind('/');
64 if (sep == obj.npos || sep + 3 > obj.size())
65 {
66 throw std::invalid_argument("Invalid obj path");
67 }
68 auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3));
69 const auto& state = std::get<std::string>(it->second);
70 man.get().handleAdminState(state, ifidx);
71 }
72 catch (const std::exception& e)
73 {
74 lg2::error("AdministrativeState match parsing failed: {ERROR}", "ERROR",
75 e);
76 }
William A. Kennington III80d29012022-11-12 02:31:40 -080077 })
Ratan Gupta6811f822017-04-14 16:34:56 +053078{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080079 reload.setCallback([&]() {
80 for (auto& hook : reloadPreHooks)
81 {
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 }
92 reloadPreHooks.clear();
93 try
94 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -080095 bus.get()
96 .new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
97 NETWORKD_INTERFACE, "Reload")
98 .call();
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -070099 lg2::info("Reloaded systemd-networkd");
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800100 }
101 catch (const sdbusplus::exception_t& ex)
102 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700103 lg2::error("Failed to reload configuration: {ERROR}", "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800104 reloadPostHooks.clear();
105 }
106 for (auto& hook : reloadPostHooks)
107 {
108 try
109 {
110 hook();
111 }
112 catch (const std::exception& ex)
113 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700114 lg2::error("Failed executing reload hook, ignoring: {ERROR}",
115 "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800116 }
117 }
118 reloadPostHooks.clear();
119 });
William A. Kennington III80d29012022-11-12 02:31:40 -0800120 std::vector<
121 std::tuple<int32_t, std::string, sdbusplus::message::object_path>>
122 links;
123 try
124 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800125 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 III80d29012022-11-12 02:31:40 -0800131 rsp.read(links);
132 }
133 catch (const sdbusplus::exception::SdBusError& e)
134 {
135 // Any failures are systemd-network not being ready
136 }
137 for (const auto& link : links)
138 {
139 unsigned ifidx = std::get<0>(link);
140 auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx);
141 auto req =
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800142 bus.get().new_method_call("org.freedesktop.network1", obj.c_str(),
143 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III80d29012022-11-12 02:31:40 -0800144 req.append("org.freedesktop.network1.Link", "AdministrativeState");
145 auto rsp = req.call();
146 std::variant<std::string> val;
147 rsp.read(val);
148 handleAdminState(std::get<std::string>(val), ifidx);
149 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530150
William A. Kennington III5b179382022-11-15 15:23:26 -0800151 std::filesystem::create_directories(confDir);
152 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
153 bus, (this->objPath / "config").str);
154 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
155 bus, (this->objPath / "dhcp").str, *this);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530156}
157
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800158void Manager::createInterface(const AllIntfInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800159{
William A. Kennington III93f5c6d2022-11-17 16:23:44 -0800160 if (ignoredIntf.find(info.intf.idx) != ignoredIntf.end())
161 {
162 return;
163 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800164 if (auto it = interfacesByIdx.find(info.intf.idx);
165 it != interfacesByIdx.end())
166 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800167 if (info.intf.name && *info.intf.name != it->second->interfaceName())
168 {
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800169 interfaces.erase(it->second->interfaceName());
170 interfacesByIdx.erase(it);
William A. Kennington III5b179382022-11-15 15:23:26 -0800171 }
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800172 else
173 {
174 it->second->updateInfo(info.intf);
175 return;
176 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800177 }
William A. Kennington III876927c2022-11-17 16:20:15 -0800178 else if (info.intf.name)
179 {
180 auto it = interfaces.find(*info.intf.name);
181 if (it != interfaces.end())
182 {
183 it->second->updateInfo(info.intf);
184 return;
185 }
186 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800187 if (!info.intf.name)
188 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700189 lg2::error("Can't create interface without name: {IFIDX}", "IFIDX",
190 info.intf.idx);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800191 return;
192 }
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800193 config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
William A. Kennington III80d29012022-11-12 02:31:40 -0800194 auto intf = std::make_unique<EthernetInterface>(
William A. Kennington III13d665c2022-11-15 20:34:40 -0800195 bus, *this, info, objPath.str, config, enabled);
William A. Kennington III80d29012022-11-12 02:31:40 -0800196 intf->loadNameServers(config);
197 intf->loadNTPServers(config);
198 auto ptr = intf.get();
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800199 interfaces.insert_or_assign(*info.intf.name, std::move(intf));
200 interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
William A. Kennington III80d29012022-11-12 02:31:40 -0800201}
202
William A. Kennington III0813a242022-11-12 18:07:11 -0800203void Manager::addInterface(const InterfaceInfo& info)
204{
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800205 if (info.flags & IFF_LOOPBACK)
206 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800207 ignoredIntf.emplace(info.idx);
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800208 return;
209 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800210 if (info.name)
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800211 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800212 const auto& ignored = internal::getIgnoredInterfaces();
213 if (ignored.find(*info.name) != ignored.end())
214 {
215 static std::unordered_set<std::string> ignored;
216 if (!ignored.contains(*info.name))
217 {
218 ignored.emplace(*info.name);
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700219 lg2::info("Ignoring interface {INTERFACE_NAME}",
220 "INTERFACE_NAME", *info.name);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800221 }
222 ignoredIntf.emplace(info.idx);
223 return;
224 }
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800225 }
226
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800227 auto infoIt = intfInfo.find(info.idx);
228 if (infoIt != intfInfo.end())
William A. Kennington III0813a242022-11-12 18:07:11 -0800229 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800230 infoIt->second.intf = info;
William A. Kennington III0813a242022-11-12 18:07:11 -0800231 }
232 else
233 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800234 infoIt = std::get<0>(intfInfo.emplace(info.idx, AllIntfInfo{info}));
235 }
236
237 if (auto it = systemdNetworkdEnabled.find(info.idx);
238 it != systemdNetworkdEnabled.end())
239 {
240 createInterface(infoIt->second, it->second);
William A. Kennington III0813a242022-11-12 18:07:11 -0800241 }
242}
243
244void Manager::removeInterface(const InterfaceInfo& info)
245{
246 auto iit = interfacesByIdx.find(info.idx);
247 auto nit = interfaces.end();
248 if (info.name)
249 {
250 nit = interfaces.find(*info.name);
251 if (nit != interfaces.end() && iit != interfacesByIdx.end() &&
252 nit->second.get() != iit->second)
253 {
254 fmt::print(stderr, "Removed interface desync detected\n");
255 fflush(stderr);
256 std::abort();
257 }
258 }
259 else if (iit != interfacesByIdx.end())
260 {
261 for (nit = interfaces.begin(); nit != interfaces.end(); ++nit)
262 {
263 if (nit->second.get() == iit->second)
264 {
265 break;
266 }
267 }
268 }
269
270 if (iit != interfacesByIdx.end())
271 {
272 interfacesByIdx.erase(iit);
273 }
274 else
275 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800276 ignoredIntf.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800277 }
278 if (nit != interfaces.end())
279 {
280 interfaces.erase(nit);
281 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800282 intfInfo.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800283}
284
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800285void Manager::addAddress(const AddressInfo& info)
286{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800287 if (info.flags & IFA_F_DEPRECATED)
288 {
289 return;
290 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800291 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III57ca9612022-11-14 15:26:47 -0800292 {
293 it->second.addrs.insert_or_assign(info.ifaddr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800294 if (auto it = interfacesByIdx.find(info.ifidx);
295 it != interfacesByIdx.end())
296 {
297 it->second->addAddr(info);
298 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800299 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800300 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III57ca9612022-11-14 15:26:47 -0800301 {
302 throw std::runtime_error(
303 fmt::format("Interface `{}` not found for addr", info.ifidx));
304 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800305}
306
307void Manager::removeAddress(const AddressInfo& info)
308{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800309 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
310 {
311 it->second->addrs.erase(info.ifaddr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800312 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
313 {
314 it->second.addrs.erase(info.ifaddr);
315 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800316 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800317}
318
319void Manager::addNeighbor(const NeighborInfo& info)
320{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800321 if (!(info.state & NUD_PERMANENT) || !info.addr)
322 {
323 return;
324 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800325 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800326 {
327 it->second.staticNeighs.insert_or_assign(*info.addr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800328 if (auto it = interfacesByIdx.find(info.ifidx);
329 it != interfacesByIdx.end())
330 {
331 it->second->addStaticNeigh(info);
332 }
William A. Kennington III7310ac72022-11-14 15:44:00 -0800333 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800334 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III7310ac72022-11-14 15:44:00 -0800335 {
336 throw std::runtime_error(
337 fmt::format("Interface `{}` not found for neigh", info.ifidx));
338 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800339}
340
341void Manager::removeNeighbor(const NeighborInfo& info)
342{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800343 if (!info.addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800344 {
William A. Kennington III7310ac72022-11-14 15:44:00 -0800345 return;
346 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800347 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800348 {
349 it->second.staticNeighs.erase(*info.addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800350 if (auto it = interfacesByIdx.find(info.ifidx);
351 it != interfacesByIdx.end())
352 {
353 it->second->staticNeighbors.erase(*info.addr);
354 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800355 }
356}
357
358void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
359{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800360 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800361 {
362 std::visit(
363 [&](auto addr) {
Patrick Williams89d734b2023-05-10 07:50:25 -0500364 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
365 {
366 it->second.defgw4.emplace(addr);
367 }
368 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
369 {
370 it->second.defgw6.emplace(addr);
371 }
372 else
373 {
374 static_assert(!std::is_same_v<void, decltype(addr)>);
375 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800376 },
377 addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800378 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
379 {
380 std::visit(
381 [&](auto addr) {
Patrick Williams89d734b2023-05-10 07:50:25 -0500382 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
383 {
384 it->second->EthernetInterfaceIntf::defaultGateway(
385 std::to_string(addr));
386 }
387 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
388 {
389 it->second->EthernetInterfaceIntf::defaultGateway6(
390 std::to_string(addr));
391 }
392 else
393 {
394 static_assert(!std::is_same_v<void, decltype(addr)>);
395 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800396 },
397 addr);
398 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800399 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800400 else if (!ignoredIntf.contains(ifidx))
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800401 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700402 lg2::error("Interface {IFIDX} not found for gw", "IFIDX", ifidx);
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800403 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800404}
405
406void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
407{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800408 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800409 {
410 std::visit(
411 [&](auto addr) {
Patrick Williams89d734b2023-05-10 07:50:25 -0500412 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
413 {
414 if (it->second.defgw4 == addr)
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800415 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500416 it->second.defgw4.reset();
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800417 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500418 }
419 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
420 {
421 if (it->second.defgw6 == addr)
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800422 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500423 it->second.defgw6.reset();
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800424 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500425 }
426 else
427 {
428 static_assert(!std::is_same_v<void, decltype(addr)>);
429 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800430 },
431 addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800432 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
433 {
434 std::visit(
435 [&](auto addr) {
Patrick Williams89d734b2023-05-10 07:50:25 -0500436 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
437 {
438 if (it->second->defaultGateway() == std::to_string(addr))
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800439 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500440 it->second->EthernetInterfaceIntf::defaultGateway("");
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800441 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500442 }
443 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
444 {
445 if (it->second->defaultGateway6() == std::to_string(addr))
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800446 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500447 it->second->EthernetInterfaceIntf::defaultGateway6("");
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800448 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500449 }
450 else
451 {
452 static_assert(!std::is_same_v<void, decltype(addr)>);
453 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800454 },
455 addr);
456 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800457 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800458}
459
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700460ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530461{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800462 if (id == 0 || id >= 4095)
463 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700464 lg2::error("VLAN ID {VLAN_ID} is not valid", "VLAN_ID", id);
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800465 elog<InvalidArgument>(
466 Argument::ARGUMENT_NAME("VLANId"),
467 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
468 }
469
William A. Kennington III96444792022-10-05 15:16:22 -0700470 auto it = interfaces.find(interfaceName);
471 if (it == interfaces.end())
472 {
473 using ResourceErr =
474 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
475 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
476 }
477 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530478}
479
Michael Tritz29f2fd62017-05-22 15:27:26 -0500480void Manager::reset()
481{
William A. Kennington III5b179382022-11-15 15:23:26 -0800482 for (const auto& dirent : std::filesystem::directory_iterator(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500483 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800484 std::error_code ec;
485 std::filesystem::remove(dirent.path(), ec);
Michael Tritz29f2fd62017-05-22 15:27:26 -0500486 }
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700487 lg2::info("Network data purged.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500488}
489
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530490// Need to merge the below function with the code which writes the
491// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500492// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530493void Manager::writeToConfigurationFile()
494{
495 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530496 for (const auto& intf : interfaces)
497 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530498 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530499 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530500}
501
William A. Kennington III80d29012022-11-12 02:31:40 -0800502void Manager::handleAdminState(std::string_view state, unsigned ifidx)
503{
504 if (state == "initialized" || state == "linger")
505 {
506 systemdNetworkdEnabled.erase(ifidx);
507 }
508 else
509 {
510 bool managed = state != "unmanaged";
511 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800512 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
William A. Kennington III80d29012022-11-12 02:31:40 -0800513 {
514 it->second->EthernetInterfaceIntf::nicEnabled(managed);
515 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800516 else if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
517 {
518 createInterface(it->second, managed);
519 }
William A. Kennington III80d29012022-11-12 02:31:40 -0800520 }
521}
522
Gunnar Mills57d9c502018-09-14 14:42:34 -0500523} // namespace network
524} // namespace phosphor