blob: ab8a636752b34df5e0d7d8a185b08f0d9a8f78d3 [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
Ratan Gupta5978dd12017-07-25 13:47:13 +05305#include "ipaddress.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include "network_config.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07007#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include "util.hpp"
Ratan Gupta738a67f2017-04-21 10:38:05 +05309
Ratan Gupta6811f822017-04-14 16:34:56 +053010#include <arpa/inet.h>
11#include <dirent.h>
12#include <net/if.h>
13
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <algorithm>
15#include <bitset>
Manojkiran Edacc099a82020-05-11 14:25:16 +053016#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <fstream>
18#include <map>
19#include <phosphor-logging/elog-errors.hpp>
20#include <phosphor-logging/log.hpp>
Michael Tritz29f2fd62017-05-22 15:27:26 -050021#include <string>
Patrick Venture189d44e2018-07-09 12:30:59 -070022#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053023
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080024constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
25constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
26constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053027constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080028
William A. Kennington III56ecc782021-10-07 18:44:50 -070029constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
30constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
31constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
32
Ratan Gupta6811f822017-04-14 16:34:56 +053033namespace phosphor
34{
35namespace network
36{
Ratan Gupta82549cc2017-04-21 08:45:23 +053037
William A. Kennington III3a70fa22018-09-20 18:48:20 -070038extern std::unique_ptr<Timer> refreshObjectTimer;
39extern std::unique_ptr<Timer> restartTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053040using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053041using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta6811f822017-04-14 16:34:56 +053042
Ratan Gupta255d5142017-08-10 09:02:08 +053043Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050044 const std::string& path) :
Ratan Gupta29b0e432017-05-25 12:51:40 +053045 details::VLANCreateIface(bus, objPath, true),
Gunnar Mills57d9c502018-09-14 14:42:34 -050046 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053047{
Ratan Gupta255d5142017-08-10 09:02:08 +053048 fs::path confDir(path);
49 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053050}
51
Ratan Guptab610caf2017-09-19 09:33:51 +053052bool Manager::createDefaultNetworkFiles(bool force)
53{
54 auto isCreated = false;
55 try
56 {
57 // Directory would have created before with
58 // setConfDir function.
59 if (force)
60 {
61 // Factory Reset case
62 // we need to forcefully write the files
63 // so delete the existing ones.
64 if (fs::is_directory(confDir))
65 {
66 for (const auto& file : fs::directory_iterator(confDir))
67 {
68 fs::remove(file.path());
69 }
70 }
71 }
72
73 auto interfaceStrList = getInterfaces();
74 for (const auto& interface : interfaceStrList)
75 {
Michael Tritz08c34f42017-10-16 14:59:09 -050076 // if the interface has '.' in the name, it means that this is a
77 // VLAN - don't create the network file.
78 if (interface.find(".") != std::string::npos)
79 {
80 continue;
81 }
82
Ratan Guptab610caf2017-09-19 09:33:51 +053083 auto fileName = systemd::config::networkFilePrefix + interface +
Gunnar Mills57d9c502018-09-14 14:42:34 -050084 systemd::config::networkFileSuffix;
Ratan Guptab610caf2017-09-19 09:33:51 +053085
86 fs::path filePath = confDir;
87 filePath /= fileName;
88
89 // create the interface specific network file
90 // if not exist or we forcefully wants to write
91 // the network file.
92
93 if (force || !fs::is_regular_file(filePath.string()))
94 {
95 bmc::writeDHCPDefault(filePath.string(), interface);
96 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053098 isCreated = true;
99 }
100 }
101 }
Patrick Williams5758db32021-10-06 12:29:22 -0500102 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +0530103 {
104 log<level::ERR>("Unable to create the default network file");
105 }
Alexander Filippov1ea35992021-03-26 13:10:05 +0300106
107 if (isCreated)
108 {
109 // if files created restart the network.
110 // don't need to call the create child objects as eventhandler
111 // will create it.
112 restartSystemdUnit(phosphor::network::networkdService);
113 }
114
Ratan Guptab610caf2017-09-19 09:33:51 +0530115 return isCreated;
116}
117
Ratan Guptaef85eb92017-06-15 08:57:54 +0530118void Manager::setConfDir(const fs::path& dir)
119{
120 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +0530121
122 if (!fs::exists(confDir))
123 {
124 if (!fs::create_directories(confDir))
125 {
126 log<level::ERR>("Unable to create the network conf dir",
127 entry("DIR=%s", confDir.c_str()));
128 elog<InternalFailure>();
129 }
130 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530131}
132
133void Manager::createInterfaces()
134{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530136 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530137
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530138 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530139
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530140 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530141 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530142 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530143 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530144
145 // interface can be of vlan type or normal ethernet interface.
146 // vlan interface looks like "interface.vlanid",so here by looking
147 // at the interface name we decide that we need
148 // to create the vlaninterface or normal physical interface.
149 if (index != std::string::npos)
150 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530152 auto interfaceName = interface.substr(0, index);
153 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530154 uint32_t vlanInt = std::stoul(vlanid);
155
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530156 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530157 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530158 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530159 // normal ethernet interface
160 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530161
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530162 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530163
Gunnar Mills57d9c502018-09-14 14:42:34 -0500164 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
165 bus, objPath.string(), dhcp, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530166
167 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800168 intf->createStaticNeighborObjects();
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530169 intf->loadNameServers();
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530170
Gunnar Mills57d9c502018-09-14 14:42:34 -0500171 this->interfaces.emplace(
172 std::make_pair(std::move(interface), std::move(intf)));
Ratan Gupta6811f822017-04-14 16:34:56 +0530173 }
174}
175
Ratan Guptaef85eb92017-06-15 08:57:54 +0530176void Manager::createChildObjects()
177{
178 // creates the ethernet interface dbus object.
179 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530180
181 systemConf.reset(nullptr);
182 dhcpConf.reset(nullptr);
183
Ratan Guptaef85eb92017-06-15 08:57:54 +0530184 fs::path objPath = objectPath;
185 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530186
187 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530188 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 bus, objPath.string(), *this);
Ratan Guptad16f88c2017-07-11 17:47:57 +0530190 // create the dhcp conf object.
191 objPath /= "dhcp";
192 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500193 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530194}
195
Patrick Williams8173e022021-05-01 06:39:41 -0500196ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530197{
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700198 return interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530199}
200
Michael Tritz29f2fd62017-05-22 15:27:26 -0500201void Manager::reset()
202{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203 if (!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500204 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530205 log<level::ERR>("Network Factory Reset failed.");
206 return;
207 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500208 }
209
Ratan Guptab610caf2017-09-19 09:33:51 +0530210 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500211}
212
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530213// Need to merge the below function with the code which writes the
214// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530216void Manager::writeToConfigurationFile()
217{
218 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530219 for (const auto& intf : interfaces)
220 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530221 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530222 }
Ratan Gupta16f12882017-09-22 18:26:11 +0530223 restartTimers();
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
Ratan Gupta16f12882017-09-22 18:26:11 +0530259void Manager::restartTimers()
Ratan Guptae05083a2017-09-16 07:12:11 +0530260{
261 using namespace std::chrono;
Ratan Gupta16f12882017-09-22 18:26:11 +0530262 if (refreshObjectTimer && restartTimer)
Ratan Guptae05083a2017-09-16 07:12:11 +0530263 {
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700264 restartTimer->restartOnce(restartTimeout);
265 refreshObjectTimer->restartOnce(refreshTimeout);
Ratan Guptae05083a2017-09-16 07:12:11 +0530266 }
Ratan Gupta70c7e5b2017-07-12 11:41:55 +0530267}
268
Ratan Gupta35297172018-11-28 18:40:16 +0530269void Manager::restartSystemdUnit(const std::string& unit)
270{
271 try
272 {
273 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
William A. Kennington IIIa5b1b0d2018-11-30 17:03:57 -0800274 SYSTEMD_INTERFACE, "ResetFailedUnit");
275 method.append(unit);
276 bus.call_noreply(method);
277 }
Patrick Williamsb108fd72021-09-02 09:45:39 -0500278 catch (const sdbusplus::exception::exception& ex)
William A. Kennington IIIa5b1b0d2018-11-30 17:03:57 -0800279 {
280 log<level::ERR>("Failed to reset failed unit",
281 entry("UNIT=%s", unit.c_str()),
282 entry("ERR=%s", ex.what()));
283 elog<InternalFailure>();
284 }
285
286 try
287 {
288 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
Ratan Gupta35297172018-11-28 18:40:16 +0530289 SYSTEMD_INTERFACE, "RestartUnit");
290 method.append(unit.c_str(), "replace");
291 bus.call_noreply(method);
292 }
Patrick Williamsb108fd72021-09-02 09:45:39 -0500293 catch (const sdbusplus::exception::exception& ex)
Ratan Gupta35297172018-11-28 18:40:16 +0530294 {
Ratan Guptaabbd85c2020-06-12 09:57:02 +0530295 log<level::ERR>("Failed to restart service", entry("ERR=%s", ex.what()),
296 entry("UNIT=%s", unit.c_str()));
Ratan Gupta35297172018-11-28 18:40:16 +0530297 elog<InternalFailure>();
298 }
299}
300
William A. Kennington III56ecc782021-10-07 18:44:50 -0700301void Manager::reloadConfigs()
302{
303 try
304 {
305 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
306 NETWORKD_INTERFACE, "Reload");
307 bus.call_noreply(method);
308 }
309 catch (const sdbusplus::exception::exception& ex)
310 {
311 log<level::ERR>("Failed to reload configuration",
312 entry("ERR=%s", ex.what()));
313 elog<InternalFailure>();
314 }
315}
316
Gunnar Mills57d9c502018-09-14 14:42:34 -0500317} // namespace network
318} // namespace phosphor