blob: 3dd171a01fd72b2cc1462649d6f2eb7b283069ee [file] [log] [blame]
Gunnar Mills57d9c502018-09-14 14:42:34 -05001#include "config.h"
2
Ratan Gupta6811f822017-04-14 16:34:56 +05303#include "network_manager.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07004
William A. Kennington III09f3a4a2022-10-25 02:59:16 -07005#include "config_parser.hpp"
Ratan Gupta5978dd12017-07-25 13:47:13 +05306#include "ipaddress.hpp"
William A. Kennington III2e09d272022-10-14 17:15:00 -07007#include "system_queries.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07008#include "types.hpp"
William A. Kennington IIIb8006122022-11-13 18:15:15 -08009#include "util.hpp"
10
William A. Kennington III57ca9612022-11-14 15:26:47 -080011#include <linux/if_addr.h>
William A. Kennington III7310ac72022-11-14 15:44:00 -080012#include <linux/neighbour.h>
William A. Kennington IIIb8006122022-11-13 18:15:15 -080013#include <net/if.h>
Ratan Gupta738a67f2017-04-21 10:38:05 +053014
Manojkiran Edacc099a82020-05-11 14:25:16 +053015#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070016#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <phosphor-logging/elog-errors.hpp>
18#include <phosphor-logging/log.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080019#include <sdbusplus/message.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070020#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053021
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080022constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
23constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
24constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053025constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080026
William A. Kennington III56ecc782021-10-07 18:44:50 -070027constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
28constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
29constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
30
Ratan Gupta6811f822017-04-14 16:34:56 +053031namespace phosphor
32{
33namespace network
34{
Ratan Gupta82549cc2017-04-21 08:45:23 +053035
William A. Kennington IIId41db382021-11-09 20:42:29 -080036extern std::unique_ptr<Timer> refreshObjectTimer;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080037extern std::unique_ptr<Timer> reloadTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053038using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053039using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080040using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053041
William A. Kennington III80d29012022-11-12 02:31:40 -080042static constexpr const char enabledMatch[] =
43 "type='signal',sender='org.freedesktop.network1',path_namespace='/org/"
44 "freedesktop/network1/"
45 "link',interface='org.freedesktop.DBus.Properties',member='"
46 "PropertiesChanged',arg0='org.freedesktop.network1.Link',";
47
Patrick Williamsc38b0712022-07-22 19:26:54 -050048Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070049 const fs::path& confDir) :
Patrick Williams166b9592022-03-30 16:09:16 -050050 details::VLANCreateIface(bus, objPath,
51 details::VLANCreateIface::action::defer_emit),
William A. Kennington III80d29012022-11-12 02:31:40 -080052 bus(bus), objectPath(objPath),
53 systemdNetworkdEnabledMatch(
54 bus, enabledMatch, [&](sdbusplus::message_t& m) {
55 std::string intf;
56 std::unordered_map<std::string, std::variant<std::string>> values;
57 try
58 {
59 m.read(intf, values);
60 auto it = values.find("AdministrativeState");
61 if (it == values.end())
62 {
63 return;
64 }
65 const std::string_view obj = m.get_path();
66 auto sep = obj.rfind('/');
67 if (sep == obj.npos || sep + 3 > obj.size())
68 {
69 throw std::invalid_argument("Invalid obj path");
70 }
71 auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3));
72 const auto& state = std::get<std::string>(it->second);
73 handleAdminState(state, ifidx);
74 }
75 catch (const std::exception& e)
76 {
77 log<level::ERR>(
78 fmt::format("AdministrativeState match parsing failed: {}",
79 e.what())
80 .c_str(),
81 entry("ERROR=%s", e.what()));
82 }
83 })
Ratan Gupta6811f822017-04-14 16:34:56 +053084{
Ratan Gupta255d5142017-08-10 09:02:08 +053085 setConfDir(confDir);
William A. Kennington III80d29012022-11-12 02:31:40 -080086 std::vector<
87 std::tuple<int32_t, std::string, sdbusplus::message::object_path>>
88 links;
89 try
90 {
91 auto rsp =
92 bus.new_method_call("org.freedesktop.network1",
93 "/org/freedesktop/network1",
94 "org.freedesktop.network1.Manager", "ListLinks")
95 .call();
96 rsp.read(links);
97 }
98 catch (const sdbusplus::exception::SdBusError& e)
99 {
100 // Any failures are systemd-network not being ready
101 }
102 for (const auto& link : links)
103 {
104 unsigned ifidx = std::get<0>(link);
105 auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx);
106 auto req =
107 bus.new_method_call("org.freedesktop.network1", obj.c_str(),
108 "org.freedesktop.DBus.Properties", "Get");
109 req.append("org.freedesktop.network1.Link", "AdministrativeState");
110 auto rsp = req.call();
111 std::variant<std::string> val;
112 rsp.read(val);
113 handleAdminState(std::get<std::string>(val), ifidx);
114 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530115}
116
117void Manager::setConfDir(const fs::path& dir)
118{
119 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530120
121 if (!fs::exists(confDir))
122 {
123 if (!fs::create_directories(confDir))
124 {
125 log<level::ERR>("Unable to create the network conf dir",
126 entry("DIR=%s", confDir.c_str()));
127 elog<InternalFailure>();
128 }
129 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530130}
131
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800132void Manager::createInterface(const UndiscoveredInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800133{
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800134 removeInterface(info.intf);
135 config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
William A. Kennington III80d29012022-11-12 02:31:40 -0800136 auto intf = std::make_unique<EthernetInterface>(
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800137 bus, *this, info.intf, objectPath, config, true, enabled);
William A. Kennington III80d29012022-11-12 02:31:40 -0800138 intf->createIPAddressObjects();
139 intf->createStaticNeighborObjects();
140 intf->loadNameServers(config);
141 intf->loadNTPServers(config);
142 auto ptr = intf.get();
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800143 interfaces.insert_or_assign(*info.intf.name, std::move(intf));
144 interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
William A. Kennington III80d29012022-11-12 02:31:40 -0800145}
146
William A. Kennington III0813a242022-11-12 18:07:11 -0800147void Manager::addInterface(const InterfaceInfo& info)
148{
William A. Kennington IIIb8006122022-11-13 18:15:15 -0800149 if (info.flags & IFF_LOOPBACK)
150 {
151 return;
152 }
153 if (!info.name)
154 {
155 throw std::invalid_argument("Interface missing name");
156 }
157 const auto& ignored = internal::getIgnoredInterfaces();
158 if (ignored.find(*info.name) != ignored.end())
159 {
160 auto msg = fmt::format("Ignoring interface {}\n", *info.name);
161 log<level::INFO>(msg.c_str());
162 return;
163 }
164
William A. Kennington III0813a242022-11-12 18:07:11 -0800165 auto it = systemdNetworkdEnabled.find(info.idx);
166 if (it != systemdNetworkdEnabled.end())
167 {
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800168 createInterface({info}, it->second);
William A. Kennington III0813a242022-11-12 18:07:11 -0800169 }
170 else
171 {
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800172 undiscoveredIntfInfo.insert_or_assign(
173 info.idx, UndiscoveredInfo{std::move(info)});
William A. Kennington III0813a242022-11-12 18:07:11 -0800174 }
175}
176
177void Manager::removeInterface(const InterfaceInfo& info)
178{
179 auto iit = interfacesByIdx.find(info.idx);
180 auto nit = interfaces.end();
181 if (info.name)
182 {
183 nit = interfaces.find(*info.name);
184 if (nit != interfaces.end() && iit != interfacesByIdx.end() &&
185 nit->second.get() != iit->second)
186 {
187 fmt::print(stderr, "Removed interface desync detected\n");
188 fflush(stderr);
189 std::abort();
190 }
191 }
192 else if (iit != interfacesByIdx.end())
193 {
194 for (nit = interfaces.begin(); nit != interfaces.end(); ++nit)
195 {
196 if (nit->second.get() == iit->second)
197 {
198 break;
199 }
200 }
201 }
202
203 if (iit != interfacesByIdx.end())
204 {
205 interfacesByIdx.erase(iit);
206 }
207 else
208 {
209 undiscoveredIntfInfo.erase(info.idx);
210 }
211 if (nit != interfaces.end())
212 {
213 interfaces.erase(nit);
214 }
215}
216
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800217void Manager::addAddress(const AddressInfo& info)
218{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800219 if (info.flags & IFA_F_DEPRECATED)
220 {
221 return;
222 }
223 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
224 {
225 it->second->addAddr(info);
226 }
227 else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
228 it != undiscoveredIntfInfo.end())
229 {
230 it->second.addrs.insert_or_assign(info.ifaddr, info);
231 }
232 else
233 {
234 throw std::runtime_error(
235 fmt::format("Interface `{}` not found for addr", info.ifidx));
236 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800237}
238
239void Manager::removeAddress(const AddressInfo& info)
240{
William A. Kennington III57ca9612022-11-14 15:26:47 -0800241 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
242 {
243 it->second->addrs.erase(info.ifaddr);
244 }
245 else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
246 it != undiscoveredIntfInfo.end())
247 {
248 it->second.addrs.erase(info.ifaddr);
249 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800250}
251
252void Manager::addNeighbor(const NeighborInfo& info)
253{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800254 if (!(info.state & NUD_PERMANENT) || !info.addr)
255 {
256 return;
257 }
258 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
259 {
260 it->second->addStaticNeigh(info);
261 }
262 else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
263 it != undiscoveredIntfInfo.end())
264 {
265 it->second.staticNeighs.insert_or_assign(*info.addr, info);
266 }
267 else
268 {
269 throw std::runtime_error(
270 fmt::format("Interface `{}` not found for neigh", info.ifidx));
271 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800272}
273
274void Manager::removeNeighbor(const NeighborInfo& info)
275{
William A. Kennington III7310ac72022-11-14 15:44:00 -0800276 if (!info.addr)
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800277 {
William A. Kennington III7310ac72022-11-14 15:44:00 -0800278 return;
279 }
280 if (auto it = interfacesByIdx.find(info.ifidx); it != interfacesByIdx.end())
281 {
282 it->second->staticNeighbors.erase(*info.addr);
283 }
284 else if (auto it = undiscoveredIntfInfo.find(info.ifidx);
285 it != undiscoveredIntfInfo.end())
286 {
287 it->second.staticNeighs.erase(*info.addr);
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800288 }
289}
290
291void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
292{
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800293 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
294 {
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800295 std::visit(
296 [&](auto addr) {
297 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
298 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800299 it->second->EthernetInterfaceIntf::defaultGateway(
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800300 std::to_string(addr));
301 }
302 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
303 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800304 it->second->EthernetInterfaceIntf::defaultGateway6(
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800305 std::to_string(addr));
306 }
307 else
308 {
309 static_assert(!std::is_same_v<void, decltype(addr)>);
310 }
311 },
312 addr);
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800313 }
314 else if (auto it = undiscoveredIntfInfo.find(ifidx);
315 it != undiscoveredIntfInfo.end())
316 {
317 std::visit(
318 [&](auto addr) {
319 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
320 {
321 it->second.defgw4.emplace(addr);
322 }
323 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
324 {
325 it->second.defgw6.emplace(addr);
326 }
327 else
328 {
329 static_assert(!std::is_same_v<void, decltype(addr)>);
330 }
331 },
332 addr);
333 }
334 else
335 {
336 auto msg = fmt::format("Interface `{}` not found for gw", ifidx);
337 log<level::ERR>(msg.c_str(), entry("IFIDX=%u", ifidx));
338 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800339}
340
341void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
342{
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800343 if (auto it = interfacesByIdx.find(ifidx); it != interfacesByIdx.end())
344 {
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800345 std::visit(
346 [&](auto addr) {
347 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
348 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800349 if (it->second->defaultGateway() == std::to_string(addr))
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800350 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800351 it->second->EthernetInterfaceIntf::defaultGateway("");
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800352 }
353 }
354 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
355 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800356 if (it->second->defaultGateway6() == std::to_string(addr))
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800357 {
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800358 it->second->EthernetInterfaceIntf::defaultGateway6("");
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800359 }
360 }
361 else
362 {
363 static_assert(!std::is_same_v<void, decltype(addr)>);
364 }
365 },
366 addr);
William A. Kennington IIIbb9e9092022-11-14 15:58:02 -0800367 }
368 else if (auto it = undiscoveredIntfInfo.find(ifidx);
369 it != undiscoveredIntfInfo.end())
370 {
371 std::visit(
372 [&](auto addr) {
373 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
374 {
375 if (it->second.defgw4 == addr)
376 {
377 it->second.defgw4.reset();
378 }
379 }
380 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
381 {
382 if (it->second.defgw6 == addr)
383 {
384 it->second.defgw6.reset();
385 }
386 }
387 else
388 {
389 static_assert(!std::is_same_v<void, decltype(addr)>);
390 }
391 },
392 addr);
393 }
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800394}
395
Ratan Gupta29b0e432017-05-25 12:51:40 +0530396void Manager::createInterfaces()
397{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500398 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530399 interfaces.clear();
William A. Kennington III67b09da2022-10-31 14:09:53 -0700400 interfacesByIdx.clear();
William A. Kennington III80d29012022-11-12 02:31:40 -0800401 for (auto& info : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +0530402 {
William A. Kennington III0813a242022-11-12 18:07:11 -0800403 addInterface(info);
Ratan Gupta6811f822017-04-14 16:34:56 +0530404 }
405}
406
Ratan Guptaef85eb92017-06-15 08:57:54 +0530407void Manager::createChildObjects()
408{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700409 routeTable.refresh();
410
Ratan Guptaef85eb92017-06-15 08:57:54 +0530411 // creates the ethernet interface dbus object.
412 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530413
414 systemConf.reset(nullptr);
415 dhcpConf.reset(nullptr);
416
Ratan Guptaef85eb92017-06-15 08:57:54 +0530417 fs::path objPath = objectPath;
418 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530419
420 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530421 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800422 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530423 // create the dhcp conf object.
424 objPath /= "dhcp";
425 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500426 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530427}
428
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700429ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530430{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800431 if (id == 0 || id >= 4095)
432 {
433 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
434 elog<InvalidArgument>(
435 Argument::ARGUMENT_NAME("VLANId"),
436 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
437 }
438
William A. Kennington III96444792022-10-05 15:16:22 -0700439 auto it = interfaces.find(interfaceName);
440 if (it == interfaces.end())
441 {
442 using ResourceErr =
443 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
444 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
445 }
446 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530447}
448
Michael Tritz29f2fd62017-05-22 15:27:26 -0500449void Manager::reset()
450{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800451 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500452 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800453 for (const auto& file : fs::directory_iterator(confDir))
454 {
455 fs::remove(file.path());
456 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500457 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800458 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500459}
460
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530461// Need to merge the below function with the code which writes the
462// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500463// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530464void Manager::writeToConfigurationFile()
465{
466 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530467 for (const auto& intf : interfaces)
468 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530469 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530470 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530471}
472
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700473#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530474void Manager::setFistBootMACOnInterface(
475 const std::pair<std::string, std::string>& inventoryEthPair)
476{
477 for (const auto& interface : interfaces)
478 {
479 if (interface.first == inventoryEthPair.first)
480 {
481 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500482 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530483 if (returnMAC == inventoryEthPair.second)
484 {
485 log<level::INFO>("Set the MAC on "),
486 entry("interface : ", interface.first.c_str()),
487 entry("MAC : ", inventoryEthPair.second.c_str());
488 std::error_code ec;
489 if (std::filesystem::is_directory("/var/lib/network", ec))
490 {
491 std::ofstream persistentFile(FirstBootFile +
492 interface.first);
493 }
494 break;
495 }
496 else
497 {
498 log<level::INFO>("MAC is Not Set on ethernet Interface");
499 }
500 }
501 }
502}
503
504#endif
505
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800506void Manager::reloadConfigsNoRefresh()
William A. Kennington III56ecc782021-10-07 18:44:50 -0700507{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800508 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800509}
510
511void Manager::reloadConfigs()
512{
513 reloadConfigsNoRefresh();
William A. Kennington IIId41db382021-11-09 20:42:29 -0800514 // Ensure that the next refresh happens after reconfiguration
515 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800516}
517
518void Manager::doReloadConfigs()
519{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800520 for (auto& hook : reloadPreHooks)
521 {
522 try
523 {
524 hook();
525 }
526 catch (const std::exception& ex)
527 {
528 log<level::ERR>("Failed executing reload hook, ignoring",
529 entry("ERR=%s", ex.what()));
530 }
531 }
532 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700533 try
534 {
535 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
536 NETWORKD_INTERFACE, "Reload");
537 bus.call_noreply(method);
538 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500539 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700540 {
541 log<level::ERR>("Failed to reload configuration",
542 entry("ERR=%s", ex.what()));
543 elog<InternalFailure>();
544 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800545 // Ensure reconfiguration has enough time
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800546 if (refreshObjectTimer->isEnabled())
547 {
548 refreshObjectTimer->setRemaining(refreshTimeout);
549 }
William A. Kennington III56ecc782021-10-07 18:44:50 -0700550}
551
William A. Kennington III80d29012022-11-12 02:31:40 -0800552void Manager::handleAdminState(std::string_view state, unsigned ifidx)
553{
554 if (state == "initialized" || state == "linger")
555 {
556 systemdNetworkdEnabled.erase(ifidx);
557 }
558 else
559 {
560 bool managed = state != "unmanaged";
561 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
562 if (auto it = undiscoveredIntfInfo.find(ifidx);
563 it != undiscoveredIntfInfo.end())
564 {
565 auto info = std::move(it->second);
566 undiscoveredIntfInfo.erase(it);
William A. Kennington III0813a242022-11-12 18:07:11 -0800567 createInterface(info, managed);
William A. Kennington III80d29012022-11-12 02:31:40 -0800568 }
569 else if (auto it = interfacesByIdx.find(ifidx);
570 it != interfacesByIdx.end())
571 {
572 it->second->EthernetInterfaceIntf::nicEnabled(managed);
573 }
574 }
575}
576
Gunnar Mills57d9c502018-09-14 14:42:34 -0500577} // namespace network
578} // namespace phosphor