blob: f862e28dd1c13197eb9a84821812cdd7af4213ee [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 III80d29012022-11-12 02:31:40 -0800127void Manager::addInterface(InterfaceInfo& info, bool enabled)
128{
129 config::Parser config(config::pathForIntfConf(confDir, *info.name));
130 auto intf = std::make_unique<EthernetInterface>(
131 bus, *this, info, objectPath, config, true, enabled);
132 intf->createIPAddressObjects();
133 intf->createStaticNeighborObjects();
134 intf->loadNameServers(config);
135 intf->loadNTPServers(config);
136 auto ptr = intf.get();
137 interfaces.emplace(std::move(*info.name), std::move(intf));
138 interfacesByIdx.emplace(info.idx, ptr);
139}
140
Ratan Gupta29b0e432017-05-25 12:51:40 +0530141void Manager::createInterfaces()
142{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500143 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530144 interfaces.clear();
William A. Kennington III67b09da2022-10-31 14:09:53 -0700145 interfacesByIdx.clear();
William A. Kennington III80d29012022-11-12 02:31:40 -0800146 for (auto& info : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +0530147 {
William A. Kennington III80d29012022-11-12 02:31:40 -0800148 auto it = systemdNetworkdEnabled.find(info.idx);
149 if (it != systemdNetworkdEnabled.end())
150 {
151 addInterface(info, it->second);
152 }
153 else
154 {
155 undiscoveredIntfInfo.insert_or_assign(info.idx, std::move(info));
156 }
Ratan Gupta6811f822017-04-14 16:34:56 +0530157 }
158}
159
Ratan Guptaef85eb92017-06-15 08:57:54 +0530160void Manager::createChildObjects()
161{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700162 routeTable.refresh();
163
Ratan Guptaef85eb92017-06-15 08:57:54 +0530164 // creates the ethernet interface dbus object.
165 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530166
167 systemConf.reset(nullptr);
168 dhcpConf.reset(nullptr);
169
Ratan Guptaef85eb92017-06-15 08:57:54 +0530170 fs::path objPath = objectPath;
171 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530172
173 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530174 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800175 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530176 // create the dhcp conf object.
177 objPath /= "dhcp";
178 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500179 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530180}
181
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700182ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530183{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800184 if (id == 0 || id >= 4095)
185 {
186 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
187 elog<InvalidArgument>(
188 Argument::ARGUMENT_NAME("VLANId"),
189 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
190 }
191
William A. Kennington III96444792022-10-05 15:16:22 -0700192 auto it = interfaces.find(interfaceName);
193 if (it == interfaces.end())
194 {
195 using ResourceErr =
196 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
197 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
198 }
199 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530200}
201
Michael Tritz29f2fd62017-05-22 15:27:26 -0500202void Manager::reset()
203{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800204 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500205 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800206 for (const auto& file : fs::directory_iterator(confDir))
207 {
208 fs::remove(file.path());
209 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500210 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800211 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500212}
213
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530214// Need to merge the below function with the code which writes the
215// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500216// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530217void Manager::writeToConfigurationFile()
218{
219 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530220 for (const auto& intf : interfaces)
221 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530222 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530223 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530224}
225
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700226#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530227void Manager::setFistBootMACOnInterface(
228 const std::pair<std::string, std::string>& inventoryEthPair)
229{
230 for (const auto& interface : interfaces)
231 {
232 if (interface.first == inventoryEthPair.first)
233 {
234 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500235 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530236 if (returnMAC == inventoryEthPair.second)
237 {
238 log<level::INFO>("Set the MAC on "),
239 entry("interface : ", interface.first.c_str()),
240 entry("MAC : ", inventoryEthPair.second.c_str());
241 std::error_code ec;
242 if (std::filesystem::is_directory("/var/lib/network", ec))
243 {
244 std::ofstream persistentFile(FirstBootFile +
245 interface.first);
246 }
247 break;
248 }
249 else
250 {
251 log<level::INFO>("MAC is Not Set on ethernet Interface");
252 }
253 }
254 }
255}
256
257#endif
258
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800259void Manager::reloadConfigsNoRefresh()
William A. Kennington III56ecc782021-10-07 18:44:50 -0700260{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800261 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800262}
263
264void Manager::reloadConfigs()
265{
266 reloadConfigsNoRefresh();
William A. Kennington IIId41db382021-11-09 20:42:29 -0800267 // Ensure that the next refresh happens after reconfiguration
268 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800269}
270
271void Manager::doReloadConfigs()
272{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800273 for (auto& hook : reloadPreHooks)
274 {
275 try
276 {
277 hook();
278 }
279 catch (const std::exception& ex)
280 {
281 log<level::ERR>("Failed executing reload hook, ignoring",
282 entry("ERR=%s", ex.what()));
283 }
284 }
285 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700286 try
287 {
288 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
289 NETWORKD_INTERFACE, "Reload");
290 bus.call_noreply(method);
291 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500292 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700293 {
294 log<level::ERR>("Failed to reload configuration",
295 entry("ERR=%s", ex.what()));
296 elog<InternalFailure>();
297 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800298 // Ensure reconfiguration has enough time
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800299 if (refreshObjectTimer->isEnabled())
300 {
301 refreshObjectTimer->setRemaining(refreshTimeout);
302 }
William A. Kennington III56ecc782021-10-07 18:44:50 -0700303}
304
William A. Kennington III80d29012022-11-12 02:31:40 -0800305void Manager::handleAdminState(std::string_view state, unsigned ifidx)
306{
307 if (state == "initialized" || state == "linger")
308 {
309 systemdNetworkdEnabled.erase(ifidx);
310 }
311 else
312 {
313 bool managed = state != "unmanaged";
314 systemdNetworkdEnabled.insert_or_assign(ifidx, managed);
315 if (auto it = undiscoveredIntfInfo.find(ifidx);
316 it != undiscoveredIntfInfo.end())
317 {
318 auto info = std::move(it->second);
319 undiscoveredIntfInfo.erase(it);
320 addInterface(info, managed);
321 }
322 else if (auto it = interfacesByIdx.find(ifidx);
323 it != interfacesByIdx.end())
324 {
325 it->second->EthernetInterfaceIntf::nicEnabled(managed);
326 }
327 }
328}
329
Gunnar Mills57d9c502018-09-14 14:42:34 -0500330} // namespace network
331} // namespace phosphor