blob: 089e0e75489ebaaf1901750d05bdbfde9d9dba39 [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 Williams89d734b2023-05-10 07:50:25 -050051 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 III9b2a20d2023-06-17 14:05:48 -070067 auto ifidx = stdplus::StrToInt<10, uint16_t>{}(obj.substr(sep + 3));
Patrick Williams89d734b2023-05-10 07:50:25 -050068 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 }
Patrick Williams6f256bc2023-10-20 11:18:34 -050076})
Ratan Gupta6811f822017-04-14 16:34:56 +053077{
William A. Kennington III5c1e1602023-09-08 13:26:56 -070078 reload.get().setCallback([self = stdplus::PinnedRef(*this)]() {
79 for (auto& hook : self.get().reloadPreHooks)
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080080 {
81 try
82 {
83 hook();
84 }
85 catch (const std::exception& ex)
86 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -070087 lg2::error("Failed executing reload hook, ignoring: {ERROR}",
88 "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080089 }
90 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -070091 self.get().reloadPreHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080092 try
93 {
William A. Kennington III5c1e1602023-09-08 13:26:56 -070094 self.get()
95 .bus.get()
William A. Kennington IIIaf77f822023-06-23 02:17:20 -070096 .new_method_call("org.freedesktop.network1",
97 "/org/freedesktop/network1",
98 "org.freedesktop.network1.Manager", "Reload")
William A. Kennington III9ede1b72022-11-21 01:59:28 -080099 .call();
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700100 lg2::info("Reloaded systemd-networkd");
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800101 }
102 catch (const sdbusplus::exception_t& ex)
103 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700104 lg2::error("Failed to reload configuration: {ERROR}", "ERROR", ex);
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700105 self.get().reloadPostHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800106 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700107 for (auto& hook : self.get().reloadPostHooks)
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800108 {
109 try
110 {
111 hook();
112 }
113 catch (const std::exception& ex)
114 {
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700115 lg2::error("Failed executing reload hook, ignoring: {ERROR}",
116 "ERROR", ex);
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800117 }
118 }
William A. Kennington III5c1e1602023-09-08 13:26:56 -0700119 self.get().reloadPostHooks.clear();
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -0800120 });
William A. Kennington III80d29012022-11-12 02:31:40 -0800121 std::vector<
122 std::tuple<int32_t, std::string, sdbusplus::message::object_path>>
123 links;
124 try
125 {
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800126 auto rsp = bus.get()
127 .new_method_call("org.freedesktop.network1",
128 "/org/freedesktop/network1",
129 "org.freedesktop.network1.Manager",
130 "ListLinks")
131 .call();
William A. Kennington III80d29012022-11-12 02:31:40 -0800132 rsp.read(links);
133 }
134 catch (const sdbusplus::exception::SdBusError& e)
135 {
136 // Any failures are systemd-network not being ready
137 }
138 for (const auto& link : links)
139 {
140 unsigned ifidx = std::get<0>(link);
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700141 stdplus::ToStrHandle<stdplus::IntToStr<10, unsigned>> tsh;
142 auto obj = stdplus::strCat("/org/freedesktop/network1/link/_3"sv,
143 tsh(ifidx));
William A. Kennington III80d29012022-11-12 02:31:40 -0800144 auto req =
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800145 bus.get().new_method_call("org.freedesktop.network1", obj.c_str(),
146 "org.freedesktop.DBus.Properties", "Get");
William A. Kennington III80d29012022-11-12 02:31:40 -0800147 req.append("org.freedesktop.network1.Link", "AdministrativeState");
148 auto rsp = req.call();
149 std::variant<std::string> val;
150 rsp.read(val);
151 handleAdminState(std::get<std::string>(val), ifidx);
152 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530153
William A. Kennington III5b179382022-11-15 15:23:26 -0800154 std::filesystem::create_directories(confDir);
155 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
156 bus, (this->objPath / "config").str);
Ratan Gupta29b0e432017-05-25 12:51:40 +0530157}
158
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800159void Manager::createInterface(const AllIntfInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800160{
William A. Kennington III93f5c6d2022-11-17 16:23:44 -0800161 if (ignoredIntf.find(info.intf.idx) != ignoredIntf.end())
162 {
163 return;
164 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800165 if (auto it = interfacesByIdx.find(info.intf.idx);
166 it != interfacesByIdx.end())
167 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800168 if (info.intf.name && *info.intf.name != it->second->interfaceName())
169 {
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800170 interfaces.erase(it->second->interfaceName());
171 interfacesByIdx.erase(it);
William A. Kennington III5b179382022-11-15 15:23:26 -0800172 }
William A. Kennington IIIbf290462022-11-15 19:44:25 -0800173 else
174 {
175 it->second->updateInfo(info.intf);
176 return;
177 }
William A. Kennington III5b179382022-11-15 15:23:26 -0800178 }
William A. Kennington III876927c2022-11-17 16:20:15 -0800179 else if (info.intf.name)
180 {
181 auto it = interfaces.find(*info.intf.name);
182 if (it != interfaces.end())
183 {
184 it->second->updateInfo(info.intf);
185 return;
186 }
187 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800188 if (!info.intf.name)
189 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700190 lg2::error("Can't create interface without name: {NET_IDX}", "NET_IDX",
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700191 info.intf.idx);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800192 return;
193 }
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800194 config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
William A. Kennington III80d29012022-11-12 02:31:40 -0800195 auto intf = std::make_unique<EthernetInterface>(
William A. Kennington III13d665c2022-11-15 20:34:40 -0800196 bus, *this, info, objPath.str, config, enabled);
William A. Kennington III80d29012022-11-12 02:31:40 -0800197 intf->loadNameServers(config);
198 intf->loadNTPServers(config);
199 auto ptr = intf.get();
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800200 interfaces.insert_or_assign(*info.intf.name, std::move(intf));
201 interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
William A. Kennington III80d29012022-11-12 02:31:40 -0800202}
203
William A. Kennington III0813a242022-11-12 18:07:11 -0800204void Manager::addInterface(const InterfaceInfo& info)
205{
William A. Kennington III9c441fd2023-02-24 13:40:01 -0800206 if (info.type != ARPHRD_ETHER)
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800207 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800208 ignoredIntf.emplace(info.idx);
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800209 return;
210 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800211 if (info.name)
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800212 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800213 const auto& ignored = internal::getIgnoredInterfaces();
214 if (ignored.find(*info.name) != ignored.end())
215 {
216 static std::unordered_set<std::string> ignored;
217 if (!ignored.contains(*info.name))
218 {
219 ignored.emplace(*info.name);
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700220 lg2::info("Ignoring interface {NET_INTF}", "NET_INTF",
221 *info.name);
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800222 }
223 ignoredIntf.emplace(info.idx);
224 return;
225 }
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800226 }
227
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800228 auto infoIt = intfInfo.find(info.idx);
229 if (infoIt != intfInfo.end())
William A. Kennington III0813a242022-11-12 18:07:11 -0800230 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800231 infoIt->second.intf = info;
William A. Kennington III0813a242022-11-12 18:07:11 -0800232 }
233 else
234 {
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800235 infoIt = std::get<0>(intfInfo.emplace(info.idx, AllIntfInfo{info}));
236 }
237
238 if (auto it = systemdNetworkdEnabled.find(info.idx);
239 it != systemdNetworkdEnabled.end())
240 {
241 createInterface(infoIt->second, it->second);
William A. Kennington III0813a242022-11-12 18:07:11 -0800242 }
243}
244
245void Manager::removeInterface(const InterfaceInfo& info)
246{
247 auto iit = interfacesByIdx.find(info.idx);
248 auto nit = interfaces.end();
249 if (info.name)
250 {
251 nit = interfaces.find(*info.name);
252 if (nit != interfaces.end() && iit != interfacesByIdx.end() &&
253 nit->second.get() != iit->second)
254 {
William A. Kennington IIIb7401962023-07-21 16:36:11 -0700255 stdplus::print(stderr, "Removed interface desync detected\n");
William A. Kennington III0813a242022-11-12 18:07:11 -0800256 fflush(stderr);
257 std::abort();
258 }
259 }
260 else if (iit != interfacesByIdx.end())
261 {
262 for (nit = interfaces.begin(); nit != interfaces.end(); ++nit)
263 {
264 if (nit->second.get() == iit->second)
265 {
266 break;
267 }
268 }
269 }
270
271 if (iit != interfacesByIdx.end())
272 {
273 interfacesByIdx.erase(iit);
274 }
275 else
276 {
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800277 ignoredIntf.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800278 }
279 if (nit != interfaces.end())
280 {
281 interfaces.erase(nit);
282 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800283 intfInfo.erase(info.idx);
William A. Kennington III0813a242022-11-12 18:07:11 -0800284}
285
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800286void Manager::addAddress(const AddressInfo& info)
287{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800288 if (info.flags & IFA_F_DEPRECATED)
289 {
290 return;
291 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800292 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III57ca9612022-11-14 15:26:47 -0800293 {
294 it->second.addrs.insert_or_assign(info.ifaddr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800295 if (auto it = interfacesByIdx.find(info.ifidx);
296 it != interfacesByIdx.end())
297 {
298 it->second->addAddr(info);
299 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800300 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800301 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III57ca9612022-11-14 15:26:47 -0800302 {
303 throw std::runtime_error(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700304 std::format("Interface `{}` not found for addr", info.ifidx));
William A. Kennington III57ca9612022-11-14 15:26:47 -0800305 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800306}
307
308void Manager::removeAddress(const AddressInfo& info)
309{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800310 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
311 {
312 it->second->addrs.erase(info.ifaddr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800313 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
314 {
315 it->second.addrs.erase(info.ifaddr);
316 }
William A. Kennington III57ca9612022-11-14 15:26:47 -0800317 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800318}
319
320void Manager::addNeighbor(const NeighborInfo& info)
321{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800322 if (!(info.state & NUD_PERMANENT) || !info.addr)
323 {
324 return;
325 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800326 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800327 {
328 it->second.staticNeighs.insert_or_assign(*info.addr, info);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800329 if (auto it = interfacesByIdx.find(info.ifidx);
330 it != interfacesByIdx.end())
331 {
332 it->second->addStaticNeigh(info);
333 }
William A. Kennington III7310ac72022-11-14 15:44:00 -0800334 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800335 else if (!ignoredIntf.contains(info.ifidx))
William A. Kennington III7310ac72022-11-14 15:44:00 -0800336 {
337 throw std::runtime_error(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700338 std::format("Interface `{}` not found for neigh", info.ifidx));
William A. Kennington III7310ac72022-11-14 15:44:00 -0800339 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800340}
341
342void Manager::removeNeighbor(const NeighborInfo& info)
343{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800344 if (!info.addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800345 {
William A. Kennington III7310ac72022-11-14 15:44:00 -0800346 return;
347 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800348 if (auto it = intfInfo.find(info.ifidx); it != intfInfo.end())
William A. Kennington III7310ac72022-11-14 15:44:00 -0800349 {
350 it->second.staticNeighs.erase(*info.addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800351 if (auto it = interfacesByIdx.find(info.ifidx);
352 it != interfacesByIdx.end())
353 {
354 it->second->staticNeighbors.erase(*info.addr);
355 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800356 }
357}
358
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700359void Manager::addDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800360{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800361 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800362 {
363 std::visit(
364 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700365 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500366 {
367 it->second.defgw4.emplace(addr);
368 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500369 else
370 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700371 static_assert(std::is_same_v<stdplus::In6Addr, decltype(addr)>);
372 it->second.defgw6.emplace(addr);
Patrick Williams89d734b2023-05-10 07:50:25 -0500373 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500374 },
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800375 addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800376 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
377 {
378 std::visit(
379 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700380 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500381 {
382 it->second->EthernetInterfaceIntf::defaultGateway(
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700383 stdplus::toStr(addr));
Patrick Williams89d734b2023-05-10 07:50:25 -0500384 }
385 else
386 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700387 static_assert(
388 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
389 it->second->EthernetInterfaceIntf::defaultGateway6(
390 stdplus::toStr(addr));
Patrick Williams89d734b2023-05-10 07:50:25 -0500391 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500392 },
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800393 addr);
394 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800395 }
William A. Kennington III3ee5b7e2022-11-15 15:04:37 -0800396 else if (!ignoredIntf.contains(ifidx))
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800397 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700398 lg2::error("Interface {NET_IDX} not found for gw", "NET_IDX", ifidx);
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800399 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800400}
401
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700402void Manager::removeDefGw(unsigned ifidx, stdplus::InAnyAddr addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800403{
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800404 if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800405 {
406 std::visit(
407 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700408 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500409 {
410 if (it->second.defgw4 == addr)
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800411 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500412 it->second.defgw4.reset();
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800413 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500414 }
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700415 else
Patrick Williams89d734b2023-05-10 07:50:25 -0500416 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700417 static_assert(std::is_same_v<stdplus::In6Addr, decltype(addr)>);
Patrick Williams89d734b2023-05-10 07:50:25 -0500418 if (it->second.defgw6 == addr)
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800419 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500420 it->second.defgw6.reset();
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800421 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500422 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500423 },
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800424 addr);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800425 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
426 {
427 std::visit(
428 [&](auto addr) {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700429 if constexpr (std::is_same_v<stdplus::In4Addr, decltype(addr)>)
Patrick Williams89d734b2023-05-10 07:50:25 -0500430 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700431 stdplus::ToStrHandle<stdplus::ToStr<stdplus::In4Addr>> tsh;
432 if (it->second->defaultGateway() == tsh(addr))
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800433 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500434 it->second->EthernetInterfaceIntf::defaultGateway("");
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800435 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500436 }
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700437 else
Patrick Williams89d734b2023-05-10 07:50:25 -0500438 {
William A. Kennington III9b2a20d2023-06-17 14:05:48 -0700439 static_assert(
440 std::is_same_v<stdplus::In6Addr, decltype(addr)>);
441 stdplus::ToStrHandle<stdplus::ToStr<stdplus::In6Addr>> tsh;
442 if (it->second->defaultGateway6() == tsh(addr))
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800443 {
Patrick Williams89d734b2023-05-10 07:50:25 -0500444 it->second->EthernetInterfaceIntf::defaultGateway6("");
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800445 }
Patrick Williams89d734b2023-05-10 07:50:25 -0500446 }
Patrick Williams6f256bc2023-10-20 11:18:34 -0500447 },
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800448 addr);
449 }
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800450 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800451}
452
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700453ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530454{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800455 if (id == 0 || id >= 4095)
456 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700457 lg2::error("VLAN ID {NET_VLAN} is not valid", "NET_VLAN", id);
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800458 elog<InvalidArgument>(
459 Argument::ARGUMENT_NAME("VLANId"),
460 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
461 }
462
William A. Kennington III96444792022-10-05 15:16:22 -0700463 auto it = interfaces.find(interfaceName);
464 if (it == interfaces.end())
465 {
466 using ResourceErr =
467 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
468 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
469 }
470 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530471}
472
Michael Tritz29f2fd62017-05-22 15:27:26 -0500473void Manager::reset()
474{
William A. Kennington III5b179382022-11-15 15:23:26 -0800475 for (const auto& dirent : std::filesystem::directory_iterator(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500476 {
William A. Kennington III5b179382022-11-15 15:23:26 -0800477 std::error_code ec;
478 std::filesystem::remove(dirent.path(), ec);
Michael Tritz29f2fd62017-05-22 15:27:26 -0500479 }
Jagpal Singh Gillf78a4152023-04-17 21:17:56 -0700480 lg2::info("Network data purged.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500481}
482
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530483void Manager::writeToConfigurationFile()
484{
485 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530486 for (const auto& intf : interfaces)
487 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530488 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530489 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530490}
491
William A. Kennington III80d29012022-11-12 02:31:40 -0800492void Manager::handleAdminState(std::string_view state, unsigned ifidx)
493{
494 if (state == "initialized" || state == "linger")
495 {
496 systemdNetworkdEnabled.erase(ifidx);
497 }
498 else
499 {
500 bool managed = state != "unmanaged";
501 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800502 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
William A. Kennington III80d29012022-11-12 02:31:40 -0800503 {
504 it->second->EthernetInterfaceIntf::nicEnabled(managed);
505 }
William A. Kennington III2fb0c872022-11-15 19:35:39 -0800506 else if (auto it = intfInfo.find(ifidx); it != intfInfo.end())
507 {
508 createInterface(it->second, managed);
509 }
William A. Kennington III80d29012022-11-12 02:31:40 -0800510 }
511}
512
Gunnar Mills57d9c502018-09-14 14:42:34 -0500513} // namespace network
514} // namespace phosphor