blob: 79a4e416f81ee128216551f457b8f2a66d46d5fb [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 IIIf30d5602022-11-13 17:09:55 -0800127void Manager::createInterface(const UndiscoveredInfo& info, bool enabled)
William A. Kennington III80d29012022-11-12 02:31:40 -0800128{
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800129 removeInterface(info.intf);
130 config::Parser config(config::pathForIntfConf(confDir, *info.intf.name));
William A. Kennington III80d29012022-11-12 02:31:40 -0800131 auto intf = std::make_unique<EthernetInterface>(
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800132 bus, *this, info.intf, objectPath, config, true, enabled);
William A. Kennington III80d29012022-11-12 02:31:40 -0800133 intf->createIPAddressObjects();
134 intf->createStaticNeighborObjects();
135 intf->loadNameServers(config);
136 intf->loadNTPServers(config);
137 auto ptr = intf.get();
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800138 interfaces.insert_or_assign(*info.intf.name, std::move(intf));
139 interfacesByIdx.insert_or_assign(info.intf.idx, ptr);
William A. Kennington III80d29012022-11-12 02:31:40 -0800140}
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 {
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800147 createInterface({info}, it->second);
William A. Kennington III0813a242022-11-12 18:07:11 -0800148 }
149 else
150 {
William A. Kennington IIIf30d5602022-11-13 17:09:55 -0800151 undiscoveredIntfInfo.insert_or_assign(
152 info.idx, UndiscoveredInfo{std::move(info)});
William A. Kennington III0813a242022-11-12 18:07:11 -0800153 }
154}
155
156void Manager::removeInterface(const InterfaceInfo& info)
157{
158 auto iit = interfacesByIdx.find(info.idx);
159 auto nit = interfaces.end();
160 if (info.name)
161 {
162 nit = interfaces.find(*info.name);
163 if (nit != interfaces.end() && iit != interfacesByIdx.end() &&
164 nit->second.get() != iit->second)
165 {
166 fmt::print(stderr, "Removed interface desync detected\n");
167 fflush(stderr);
168 std::abort();
169 }
170 }
171 else if (iit != interfacesByIdx.end())
172 {
173 for (nit = interfaces.begin(); nit != interfaces.end(); ++nit)
174 {
175 if (nit->second.get() == iit->second)
176 {
177 break;
178 }
179 }
180 }
181
182 if (iit != interfacesByIdx.end())
183 {
184 interfacesByIdx.erase(iit);
185 }
186 else
187 {
188 undiscoveredIntfInfo.erase(info.idx);
189 }
190 if (nit != interfaces.end())
191 {
192 interfaces.erase(nit);
193 }
194}
195
William A. Kennington IIIed5ff472022-11-12 16:24:02 -0800196inline void getIntfOrLog(const decltype(Manager::interfacesByIdx)& intfs,
197 unsigned idx, auto&& cb)
198{
199 auto it = intfs.find(idx);
200 if (it == intfs.end())
201 {
202 auto msg = fmt::format("Interface `{}` not found", idx);
203 log<level::ERR>(msg.c_str(), entry("IFIDX=%u", idx));
204 return;
205 }
206 cb(*it->second);
207}
208
209void Manager::addAddress(const AddressInfo& info)
210{
211 getIntfOrLog(interfacesByIdx, info.ifidx,
212 [&](auto& intf) { intf.addAddr(info); });
213}
214
215void Manager::removeAddress(const AddressInfo& info)
216{
217 getIntfOrLog(interfacesByIdx, info.ifidx,
218 [&](auto& intf) { intf.addrs.erase(info.ifaddr); });
219}
220
221void Manager::addNeighbor(const NeighborInfo& info)
222{
223 getIntfOrLog(interfacesByIdx, info.ifidx,
224 [&](auto& intf) { intf.addStaticNeigh(info); });
225}
226
227void Manager::removeNeighbor(const NeighborInfo& info)
228{
229 if (info.addr)
230 {
231 getIntfOrLog(interfacesByIdx, info.ifidx, [&](auto& intf) {
232 intf.staticNeighbors.erase(*info.addr);
233 });
234 }
235}
236
237void Manager::addDefGw(unsigned ifidx, InAddrAny addr)
238{
239 getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) {
240 std::visit(
241 [&](auto addr) {
242 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
243 {
244 intf.EthernetInterfaceIntf::defaultGateway(
245 std::to_string(addr));
246 }
247 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
248 {
249 intf.EthernetInterfaceIntf::defaultGateway6(
250 std::to_string(addr));
251 }
252 else
253 {
254 static_assert(!std::is_same_v<void, decltype(addr)>);
255 }
256 },
257 addr);
258 });
259}
260
261void Manager::removeDefGw(unsigned ifidx, InAddrAny addr)
262{
263 getIntfOrLog(interfacesByIdx, ifidx, [&](auto& intf) {
264 std::visit(
265 [&](auto addr) {
266 if constexpr (std::is_same_v<in_addr, decltype(addr)>)
267 {
268 if (intf.defaultGateway() == std::to_string(addr))
269 {
270 intf.EthernetInterfaceIntf::defaultGateway("");
271 }
272 }
273 else if constexpr (std::is_same_v<in6_addr, decltype(addr)>)
274 {
275 if (intf.defaultGateway6() == std::to_string(addr))
276 {
277 intf.EthernetInterfaceIntf::defaultGateway6("");
278 }
279 }
280 else
281 {
282 static_assert(!std::is_same_v<void, decltype(addr)>);
283 }
284 },
285 addr);
286 });
287}
288
Ratan Gupta29b0e432017-05-25 12:51:40 +0530289void Manager::createInterfaces()
290{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500291 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530292 interfaces.clear();
William A. Kennington III67b09da2022-10-31 14:09:53 -0700293 interfacesByIdx.clear();
William A. Kennington III80d29012022-11-12 02:31:40 -0800294 for (auto& info : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +0530295 {
William A. Kennington III0813a242022-11-12 18:07:11 -0800296 addInterface(info);
Ratan Gupta6811f822017-04-14 16:34:56 +0530297 }
298}
299
Ratan Guptaef85eb92017-06-15 08:57:54 +0530300void Manager::createChildObjects()
301{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700302 routeTable.refresh();
303
Ratan Guptaef85eb92017-06-15 08:57:54 +0530304 // creates the ethernet interface dbus object.
305 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530306
307 systemConf.reset(nullptr);
308 dhcpConf.reset(nullptr);
309
Ratan Guptaef85eb92017-06-15 08:57:54 +0530310 fs::path objPath = objectPath;
311 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530312
313 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530314 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800315 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530316 // create the dhcp conf object.
317 objPath /= "dhcp";
318 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500319 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530320}
321
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700322ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530323{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800324 if (id == 0 || id >= 4095)
325 {
326 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
327 elog<InvalidArgument>(
328 Argument::ARGUMENT_NAME("VLANId"),
329 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
330 }
331
William A. Kennington III96444792022-10-05 15:16:22 -0700332 auto it = interfaces.find(interfaceName);
333 if (it == interfaces.end())
334 {
335 using ResourceErr =
336 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
337 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
338 }
339 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530340}
341
Michael Tritz29f2fd62017-05-22 15:27:26 -0500342void Manager::reset()
343{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800344 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500345 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800346 for (const auto& file : fs::directory_iterator(confDir))
347 {
348 fs::remove(file.path());
349 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500350 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800351 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500352}
353
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530354// Need to merge the below function with the code which writes the
355// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500356// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530357void Manager::writeToConfigurationFile()
358{
359 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530360 for (const auto& intf : interfaces)
361 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530362 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530363 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530364}
365
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700366#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530367void Manager::setFistBootMACOnInterface(
368 const std::pair<std::string, std::string>& inventoryEthPair)
369{
370 for (const auto& interface : interfaces)
371 {
372 if (interface.first == inventoryEthPair.first)
373 {
374 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500375 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530376 if (returnMAC == inventoryEthPair.second)
377 {
378 log<level::INFO>("Set the MAC on "),
379 entry("interface : ", interface.first.c_str()),
380 entry("MAC : ", inventoryEthPair.second.c_str());
381 std::error_code ec;
382 if (std::filesystem::is_directory("/var/lib/network", ec))
383 {
384 std::ofstream persistentFile(FirstBootFile +
385 interface.first);
386 }
387 break;
388 }
389 else
390 {
391 log<level::INFO>("MAC is Not Set on ethernet Interface");
392 }
393 }
394 }
395}
396
397#endif
398
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800399void Manager::reloadConfigsNoRefresh()
William A. Kennington III56ecc782021-10-07 18:44:50 -0700400{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800401 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800402}
403
404void Manager::reloadConfigs()
405{
406 reloadConfigsNoRefresh();
William A. Kennington IIId41db382021-11-09 20:42:29 -0800407 // Ensure that the next refresh happens after reconfiguration
408 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800409}
410
411void Manager::doReloadConfigs()
412{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800413 for (auto& hook : reloadPreHooks)
414 {
415 try
416 {
417 hook();
418 }
419 catch (const std::exception& ex)
420 {
421 log<level::ERR>("Failed executing reload hook, ignoring",
422 entry("ERR=%s", ex.what()));
423 }
424 }
425 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700426 try
427 {
428 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
429 NETWORKD_INTERFACE, "Reload");
430 bus.call_noreply(method);
431 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500432 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700433 {
434 log<level::ERR>("Failed to reload configuration",
435 entry("ERR=%s", ex.what()));
436 elog<InternalFailure>();
437 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800438 // Ensure reconfiguration has enough time
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800439 if (refreshObjectTimer->isEnabled())
440 {
441 refreshObjectTimer->setRemaining(refreshTimeout);
442 }
William A. Kennington III56ecc782021-10-07 18:44:50 -0700443}
444
William A. Kennington III80d29012022-11-12 02:31:40 -0800445void Manager::handleAdminState(std::string_view state, unsigned ifidx)
446{
447 if (state == "initialized" || state == "linger")
448 {
449 systemdNetworkdEnabled.erase(ifidx);
450 }
451 else
452 {
453 bool managed = state != "unmanaged";
454 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
455 if (auto it = undiscoveredIntfInfo.find(ifidx);
456 it != undiscoveredIntfInfo.end())
457 {
458 auto info = std::move(it->second);
459 undiscoveredIntfInfo.erase(it);
William A. Kennington III0813a242022-11-12 18:07:11 -0800460 createInterface(info, managed);
William A. Kennington III80d29012022-11-12 02:31:40 -0800461 }
462 else if (auto it = interfacesByIdx.find(ifidx);
463 it != interfacesByIdx.end())
464 {
465 it->second->EthernetInterfaceIntf::nicEnabled(managed);
466 }
467 }
468}
469
Gunnar Mills57d9c502018-09-14 14:42:34 -0500470} // namespace network
471} // namespace phosphor