blob: 2d7ce98f6b279ef097be4a9ff61b6bd95a57987b [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"
Ratan Gupta738a67f2017-04-21 10:38:05 +05309
Manojkiran Edacc099a82020-05-11 14:25:16 +053010#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <phosphor-logging/elog-errors.hpp>
13#include <phosphor-logging/log.hpp>
William A. Kennington III80d29012022-11-12 02:31:40 -080014#include <sdbusplus/message.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070015#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053016
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080017constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
18constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
19constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053020constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080021
William A. Kennington III56ecc782021-10-07 18:44:50 -070022constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
23constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
24constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
25
Ratan Gupta6811f822017-04-14 16:34:56 +053026namespace phosphor
27{
28namespace network
29{
Ratan Gupta82549cc2017-04-21 08:45:23 +053030
William A. Kennington IIId41db382021-11-09 20:42:29 -080031extern std::unique_ptr<Timer> refreshObjectTimer;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080032extern std::unique_ptr<Timer> reloadTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053033using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053034using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080035using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053036
William A. Kennington III80d29012022-11-12 02:31:40 -080037static constexpr const char enabledMatch[] =
38 "type='signal',sender='org.freedesktop.network1',path_namespace='/org/"
39 "freedesktop/network1/"
40 "link',interface='org.freedesktop.DBus.Properties',member='"
41 "PropertiesChanged',arg0='org.freedesktop.network1.Link',";
42
Patrick Williamsc38b0712022-07-22 19:26:54 -050043Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070044 const fs::path& confDir) :
Patrick Williams166b9592022-03-30 16:09:16 -050045 details::VLANCreateIface(bus, objPath,
46 details::VLANCreateIface::action::defer_emit),
William A. Kennington III80d29012022-11-12 02:31:40 -080047 bus(bus), objectPath(objPath),
48 systemdNetworkdEnabledMatch(
49 bus, enabledMatch, [&](sdbusplus::message_t& m) {
50 std::string intf;
51 std::unordered_map<std::string, std::variant<std::string>> values;
52 try
53 {
54 m.read(intf, values);
55 auto it = values.find("AdministrativeState");
56 if (it == values.end())
57 {
58 return;
59 }
60 const std::string_view obj = m.get_path();
61 auto sep = obj.rfind('/');
62 if (sep == obj.npos || sep + 3 > obj.size())
63 {
64 throw std::invalid_argument("Invalid obj path");
65 }
66 auto ifidx = DecodeInt<unsigned, 10>{}(obj.substr(sep + 3));
67 const auto& state = std::get<std::string>(it->second);
68 handleAdminState(state, ifidx);
69 }
70 catch (const std::exception& e)
71 {
72 log<level::ERR>(
73 fmt::format("AdministrativeState match parsing failed: {}",
74 e.what())
75 .c_str(),
76 entry("ERROR=%s", e.what()));
77 }
78 })
Ratan Gupta6811f822017-04-14 16:34:56 +053079{
Ratan Gupta255d5142017-08-10 09:02:08 +053080 setConfDir(confDir);
William A. Kennington III80d29012022-11-12 02:31:40 -080081 std::vector<
82 std::tuple<int32_t, std::string, sdbusplus::message::object_path>>
83 links;
84 try
85 {
86 auto rsp =
87 bus.new_method_call("org.freedesktop.network1",
88 "/org/freedesktop/network1",
89 "org.freedesktop.network1.Manager", "ListLinks")
90 .call();
91 rsp.read(links);
92 }
93 catch (const sdbusplus::exception::SdBusError& e)
94 {
95 // Any failures are systemd-network not being ready
96 }
97 for (const auto& link : links)
98 {
99 unsigned ifidx = std::get<0>(link);
100 auto obj = fmt::format("/org/freedesktop/network1/link/_3{}", ifidx);
101 auto req =
102 bus.new_method_call("org.freedesktop.network1", obj.c_str(),
103 "org.freedesktop.DBus.Properties", "Get");
104 req.append("org.freedesktop.network1.Link", "AdministrativeState");
105 auto rsp = req.call();
106 std::variant<std::string> val;
107 rsp.read(val);
108 handleAdminState(std::get<std::string>(val), ifidx);
109 }
Ratan Guptaef85eb92017-06-15 08:57:54 +0530110}
111
112void Manager::setConfDir(const fs::path& dir)
113{
114 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530115
116 if (!fs::exists(confDir))
117 {
118 if (!fs::create_directories(confDir))
119 {
120 log<level::ERR>("Unable to create the network conf dir",
121 entry("DIR=%s", confDir.c_str()));
122 elog<InternalFailure>();
123 }
124 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530125}
126
William A. Kennington III0813a242022-11-12 18:07:11 -0800127void Manager::createInterface(const InterfaceInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800128{
William A. Kennington III0813a242022-11-12 18:07:11 -0800129 removeInterface(info);
William A. Kennington III80d29012022-11-12 02:31:40 -0800130 config::Parser config(config::pathForIntfConf(confDir, *info.name));
131 auto intf = std::make_unique<EthernetInterface>(
132 bus, *this, info, objectPath, config, true, enabled);
133 intf->createIPAddressObjects();
134 intf->createStaticNeighborObjects();
135 intf->loadNameServers(config);
136 intf->loadNTPServers(config);
137 auto ptr = intf.get();
William A. Kennington III0813a242022-11-12 18:07:11 -0800138 interfaces.emplace(*info.name, std::move(intf));
William A. Kennington III80d29012022-11-12 02:31:40 -0800139 interfacesByIdx.emplace(info.idx, ptr);
140}
141
William A. Kennington III0813a242022-11-12 18:07:11 -0800142void Manager::addInterface(const InterfaceInfo& info)
143{
144 auto it = systemdNetworkdEnabled.find(info.idx);
145 if (it != systemdNetworkdEnabled.end())
146 {
147 createInterface(info, it->second);
148 }
149 else
150 {
151 undiscoveredIntfInfo.insert_or_assign(info.idx, std::move(info));
152 }
153}
154
155void Manager::removeInterface(const InterfaceInfo& info)
156{
157 auto iit = interfacesByIdx.find(info.idx);
158 auto nit = interfaces.end();
159 if (info.name)
160 {
161 nit = interfaces.find(*info.name);
162 if (nit != interfaces.end() && iit != interfacesByIdx.end() &&
163 nit->second.get() != iit->second)
164 {
165 fmt::print(stderr, "Removed interface desync detected\n");
166 fflush(stderr);
167 std::abort();
168 }
169 }
170 else if (iit != interfacesByIdx.end())
171 {
172 for (nit = interfaces.begin(); nit != interfaces.end(); ++nit)
173 {
174 if (nit->second.get() == iit->second)
175 {
176 break;
177 }
178 }
179 }
180
181 if (iit != interfacesByIdx.end())
182 {
183 interfacesByIdx.erase(iit);
184 }
185 else
186 {
187 undiscoveredIntfInfo.erase(info.idx);
188 }
189 if (nit != interfaces.end())
190 {
191 interfaces.erase(nit);
192 }
193}
194
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800195inline void getIntfOrLog(const decltype(Manager::interfacesByIdx)& intfs,
196 unsigned idx, auto&& cb)
197{
198 auto it = intfs.find(idx);
199 if (it == intfs.end())
200 {
201 auto msg = fmt::format("Interface `{}` not found", idx);
202 log<level::ERR>(msg.c_str(), entry("IFIDX=%u", idx));
203 return;
204 }
205 cb(*it->second);
206}
207
208void Manager::addAddress(const AddressInfo& info)
209{
210 getIntfOrLog(interfacesByIdx, info.ifidx,
211 [&](auto& intf) { intf.addAddr(info); });
212}
213
214void Manager::removeAddress(const AddressInfo& info)
215{
216 getIntfOrLog(interfacesByIdx, info.ifidx,
217 [&](auto& intf) { intf.addrs.erase(info.ifaddr); });
218}
219
220void Manager::addNeighbor(const NeighborInfo& info)
221{
222 getIntfOrLog(interfacesByIdx, info.ifidx,
223 [&](auto& intf) { intf.addStaticNeigh(info); });
224}
225
226void Manager::removeNeighbor(const NeighborInfo& info)
227{
228 if (info.addr)
229 {
230 getIntfOrLog(interfacesByIdx, info.ifidx, [&](auto& intf) {
231 intf.staticNeighbors.erase(*info.addr);
232 });
233 }
234}
235
236void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
237{
238 getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) {
239 std::visit(
240 [&](auto addr) {
241 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
242 {
243 intf.EthernetInterfaceIntf::defaultGateway(
244 std::to_string(addr));
245 }
246 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
247 {
248 intf.EthernetInterfaceIntf::defaultGateway6(
249 std::to_string(addr));
250 }
251 else
252 {
253 static_assert(!std::is_same_v<void, decltype(addr)>);
254 }
255 },
256 addr);
257 });
258}
259
260void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
261{
262 getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) {
263 std::visit(
264 [&](auto addr) {
265 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
266 {
267 if (intf.defaultGateway() == std::to_string(addr))
268 {
269 intf.EthernetInterfaceIntf::defaultGateway("");
270 }
271 }
272 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
273 {
274 if (intf.defaultGateway6() == std::to_string(addr))
275 {
276 intf.EthernetInterfaceIntf::defaultGateway6("");
277 }
278 }
279 else
280 {
281 static_assert(!std::is_same_v<void, decltype(addr)>);
282 }
283 },
284 addr);
285 });
286}
287
Ratan Gupta29b0e432017-05-25 12:51:40 +0530288void Manager::createInterfaces()
289{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500290 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530291 interfaces.clear();
William A. Kennington III67b09da2022-10-31 14:09:53 -0700292 interfacesByIdx.clear();
William A. Kennington III80d29012022-11-12 02:31:40 -0800293 for (auto& info : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +0530294 {
William A. Kennington III0813a242022-11-12 18:07:11 -0800295 addInterface(info);
Ratan Gupta6811f822017-04-14 16:34:56 +0530296 }
297}
298
Ratan Guptaef85eb92017-06-15 08:57:54 +0530299void Manager::createChildObjects()
300{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700301 routeTable.refresh();
302
Ratan Guptaef85eb92017-06-15 08:57:54 +0530303 // creates the ethernet interface dbus object.
304 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530305
306 systemConf.reset(nullptr);
307 dhcpConf.reset(nullptr);
308
Ratan Guptaef85eb92017-06-15 08:57:54 +0530309 fs::path objPath = objectPath;
310 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530311
312 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530313 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800314 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530315 // create the dhcp conf object.
316 objPath /= "dhcp";
317 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500318 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530319}
320
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700321ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530322{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800323 if (id == 0 || id >= 4095)
324 {
325 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
326 elog<InvalidArgument>(
327 Argument::ARGUMENT_NAME("VLANId"),
328 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
329 }
330
William A. Kennington III96444792022-10-05 15:16:22 -0700331 auto it = interfaces.find(interfaceName);
332 if (it == interfaces.end())
333 {
334 using ResourceErr =
335 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
336 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
337 }
338 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530339}
340
Michael Tritz29f2fd62017-05-22 15:27:26 -0500341void Manager::reset()
342{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800343 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500344 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800345 for (const auto& file : fs::directory_iterator(confDir))
346 {
347 fs::remove(file.path());
348 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500349 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800350 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500351}
352
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530353// Need to merge the below function with the code which writes the
354// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500355// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530356void Manager::writeToConfigurationFile()
357{
358 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530359 for (const auto& intf : interfaces)
360 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530361 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530362 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530363}
364
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700365#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530366void Manager::setFistBootMACOnInterface(
367 const std::pair<std::string, std::string>& inventoryEthPair)
368{
369 for (const auto& interface : interfaces)
370 {
371 if (interface.first == inventoryEthPair.first)
372 {
373 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500374 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530375 if (returnMAC == inventoryEthPair.second)
376 {
377 log<level::INFO>("Set the MAC on "),
378 entry("interface : ", interface.first.c_str()),
379 entry("MAC : ", inventoryEthPair.second.c_str());
380 std::error_code ec;
381 if (std::filesystem::is_directory("/var/lib/network", ec))
382 {
383 std::ofstream persistentFile(FirstBootFile +
384 interface.first);
385 }
386 break;
387 }
388 else
389 {
390 log<level::INFO>("MAC is Not Set on ethernet Interface");
391 }
392 }
393 }
394}
395
396#endif
397
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800398void Manager::reloadConfigsNoRefresh()
William A. Kennington III56ecc782021-10-07 18:44:50 -0700399{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800400 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800401}
402
403void Manager::reloadConfigs()
404{
405 reloadConfigsNoRefresh();
William A. Kennington IIId41db382021-11-09 20:42:29 -0800406 // Ensure that the next refresh happens after reconfiguration
407 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800408}
409
410void Manager::doReloadConfigs()
411{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800412 for (auto& hook : reloadPreHooks)
413 {
414 try
415 {
416 hook();
417 }
418 catch (const std::exception& ex)
419 {
420 log<level::ERR>("Failed executing reload hook, ignoring",
421 entry("ERR=%s", ex.what()));
422 }
423 }
424 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700425 try
426 {
427 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
428 NETWORKD_INTERFACE, "Reload");
429 bus.call_noreply(method);
430 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500431 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700432 {
433 log<level::ERR>("Failed to reload configuration",
434 entry("ERR=%s", ex.what()));
435 elog<InternalFailure>();
436 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800437 // Ensure reconfiguration has enough time
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800438 if (refreshObjectTimer->isEnabled())
439 {
440 refreshObjectTimer->setRemaining(refreshTimeout);
441 }
William A. Kennington III56ecc782021-10-07 18:44:50 -0700442}
443
William A. Kennington III80d29012022-11-12 02:31:40 -0800444void Manager::handleAdminState(std::string_view state, unsigned ifidx)
445{
446 if (state == "initialized" || state == "linger")
447 {
448 systemdNetworkdEnabled.erase(ifidx);
449 }
450 else
451 {
452 bool managed = state != "unmanaged";
453 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
454 if (auto it = undiscoveredIntfInfo.find(ifidx);
455 it != undiscoveredIntfInfo.end())
456 {
457 auto info = std::move(it->second);
458 undiscoveredIntfInfo.erase(it);
William A. Kennington III0813a242022-11-12 18:07:11 -0800459 createInterface(info, managed);
William A. Kennington III80d29012022-11-12 02:31:40 -0800460 }
461 else if (auto it = interfacesByIdx.find(ifidx);
462 it != interfacesByIdx.end())
463 {
464 it->second->EthernetInterfaceIntf::nicEnabled(managed);
465 }
466 }
467}
468
Gunnar Mills57d9c502018-09-14 14:42:34 -0500469} // namespace network
470} // namespace phosphor