blob: 89925b25bd996edceb7f015a30887049040a7b82 [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 IIId41db382021-11-09 20:42:29 -080038extern std::unique_ptr<Timer> refreshObjectTimer;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080039extern std::unique_ptr<Timer> reloadTimer;
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;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080042using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053043
Patrick Williamsc38b0712022-07-22 19:26:54 -050044Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
Gunnar Mills57d9c502018-09-14 14:42:34 -050045 const std::string& path) :
Patrick Williams166b9592022-03-30 16:09:16 -050046 details::VLANCreateIface(bus, objPath,
47 details::VLANCreateIface::action::defer_emit),
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053049{
Ratan Gupta255d5142017-08-10 09:02:08 +053050 fs::path confDir(path);
51 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053052}
53
Ratan Guptab610caf2017-09-19 09:33:51 +053054bool Manager::createDefaultNetworkFiles(bool force)
55{
56 auto isCreated = false;
57 try
58 {
59 // Directory would have created before with
60 // setConfDir function.
61 if (force)
62 {
63 // Factory Reset case
64 // we need to forcefully write the files
65 // so delete the existing ones.
66 if (fs::is_directory(confDir))
67 {
68 for (const auto& file : fs::directory_iterator(confDir))
69 {
70 fs::remove(file.path());
71 }
72 }
73 }
74
75 auto interfaceStrList = getInterfaces();
76 for (const auto& interface : interfaceStrList)
77 {
Michael Tritz08c34f42017-10-16 14:59:09 -050078 // if the interface has '.' in the name, it means that this is a
79 // VLAN - don't create the network file.
80 if (interface.find(".") != std::string::npos)
81 {
82 continue;
83 }
84
Ratan Guptab610caf2017-09-19 09:33:51 +053085 auto fileName = systemd::config::networkFilePrefix + interface +
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 systemd::config::networkFileSuffix;
Ratan Guptab610caf2017-09-19 09:33:51 +053087
88 fs::path filePath = confDir;
89 filePath /= fileName;
90
91 // create the interface specific network file
92 // if not exist or we forcefully wants to write
93 // the network file.
94
95 if (force || !fs::is_regular_file(filePath.string()))
96 {
97 bmc::writeDHCPDefault(filePath.string(), interface);
98 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050099 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +0530100 isCreated = true;
101 }
102 }
103 }
Patrick Williams5758db32021-10-06 12:29:22 -0500104 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +0530105 {
106 log<level::ERR>("Unable to create the default network file");
107 }
Alexander Filippov1ea35992021-03-26 13:10:05 +0300108
Ratan Guptab610caf2017-09-19 09:33:51 +0530109 return isCreated;
110}
111
Ratan Guptaef85eb92017-06-15 08:57:54 +0530112void 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
127void Manager::createInterfaces()
128{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500129 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530130 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530131
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530132 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530133
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530134 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530135 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530136 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530137 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530138
139 // interface can be of vlan type or normal ethernet interface.
140 // vlan interface looks like "interface.vlanid",so here by looking
141 // at the interface name we decide that we need
142 // to create the vlaninterface or normal physical interface.
143 if (index != std::string::npos)
144 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530146 auto interfaceName = interface.substr(0, index);
147 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530148 uint32_t vlanInt = std::stoul(vlanid);
149
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530150 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530151 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530152 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530153 // normal ethernet interface
154 objPath /= interface;
Ratan Gupta6811f822017-04-14 16:34:56 +0530155
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530156 auto dhcp = getDHCPValue(confDir, interface);
Ratan Gupta34f96d62017-06-15 09:16:22 +0530157
Gunnar Mills57d9c502018-09-14 14:42:34 -0500158 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
159 bus, objPath.string(), dhcp, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530160
161 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800162 intf->createStaticNeighborObjects();
Manojkiran Edaacd6dd52019-10-15 15:00:51 +0530163 intf->loadNameServers();
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530164
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165 this->interfaces.emplace(
166 std::make_pair(std::move(interface), std::move(intf)));
Ratan Gupta6811f822017-04-14 16:34:56 +0530167 }
168}
169
Ratan Guptaef85eb92017-06-15 08:57:54 +0530170void Manager::createChildObjects()
171{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700172 routeTable.refresh();
173
Ratan Guptaef85eb92017-06-15 08:57:54 +0530174 // creates the ethernet interface dbus object.
175 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530176
177 systemConf.reset(nullptr);
178 dhcpConf.reset(nullptr);
179
Ratan Guptaef85eb92017-06-15 08:57:54 +0530180 fs::path objPath = objectPath;
181 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530182
183 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530184 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800185 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530186 // create the dhcp conf object.
187 objPath /= "dhcp";
188 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500189 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530190}
191
Patrick Williams8173e022021-05-01 06:39:41 -0500192ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530193{
sureshvijayv1ee2cba82021-11-18 15:28:50 +0530194 if (!hasInterface(interfaceName))
195 {
Patrick Williams97399842022-03-16 14:14:02 -0500196 using ResourceErr =
197 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
198 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
sureshvijayv1ee2cba82021-11-18 15:28:50 +0530199 }
200
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800201 if (id == 0 || id >= 4095)
202 {
203 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
204 elog<InvalidArgument>(
205 Argument::ARGUMENT_NAME("VLANId"),
206 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
207 }
208
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700209 return interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530210}
211
Michael Tritz29f2fd62017-05-22 15:27:26 -0500212void Manager::reset()
213{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500214 if (!createDefaultNetworkFiles(true))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500215 {
Ratan Guptab610caf2017-09-19 09:33:51 +0530216 log<level::ERR>("Network Factory Reset failed.");
217 return;
218 // TODO: openbmc/openbmc#1721 - Log ResetFailed error here.
Michael Tritz29f2fd62017-05-22 15:27:26 -0500219 }
220
Ratan Guptab610caf2017-09-19 09:33:51 +0530221 log<level::INFO>("Network Factory Reset done.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500222}
223
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530224// Need to merge the below function with the code which writes the
225// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530227void Manager::writeToConfigurationFile()
228{
229 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530230 for (const auto& intf : interfaces)
231 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530232 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530233 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530234}
235
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700236#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530237void Manager::setFistBootMACOnInterface(
238 const std::pair<std::string, std::string>& inventoryEthPair)
239{
240 for (const auto& interface : interfaces)
241 {
242 if (interface.first == inventoryEthPair.first)
243 {
244 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500245 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530246 if (returnMAC == inventoryEthPair.second)
247 {
248 log<level::INFO>("Set the MAC on "),
249 entry("interface : ", interface.first.c_str()),
250 entry("MAC : ", inventoryEthPair.second.c_str());
251 std::error_code ec;
252 if (std::filesystem::is_directory("/var/lib/network", ec))
253 {
254 std::ofstream persistentFile(FirstBootFile +
255 interface.first);
256 }
257 break;
258 }
259 else
260 {
261 log<level::INFO>("MAC is Not Set on ethernet Interface");
262 }
263 }
264 }
265}
266
267#endif
268
William A. Kennington III56ecc782021-10-07 18:44:50 -0700269void Manager::reloadConfigs()
270{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800271 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800272 // Ensure that the next refresh happens after reconfiguration
273 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800274}
275
276void Manager::doReloadConfigs()
277{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800278 for (auto& hook : reloadPreHooks)
279 {
280 try
281 {
282 hook();
283 }
284 catch (const std::exception& ex)
285 {
286 log<level::ERR>("Failed executing reload hook, ignoring",
287 entry("ERR=%s", ex.what()));
288 }
289 }
290 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700291 try
292 {
293 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
294 NETWORKD_INTERFACE, "Reload");
295 bus.call_noreply(method);
296 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500297 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700298 {
299 log<level::ERR>("Failed to reload configuration",
300 entry("ERR=%s", ex.what()));
301 elog<InternalFailure>();
302 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800303 // Ensure reconfiguration has enough time
304 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700305}
306
Gunnar Mills57d9c502018-09-14 14:42:34 -0500307} // namespace network
308} // namespace phosphor