blob: 86d892fb3e947ad527bc2ba94efa0bf31b8fc069 [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
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080054bool Manager::createDefaultNetworkFiles()
Ratan Guptab610caf2017-09-19 09:33:51 +053055{
56 auto isCreated = false;
57 try
58 {
Ratan Guptab610caf2017-09-19 09:33:51 +053059 auto interfaceStrList = getInterfaces();
60 for (const auto& interface : interfaceStrList)
61 {
Michael Tritz08c34f42017-10-16 14:59:09 -050062 // if the interface has '.' in the name, it means that this is a
63 // VLAN - don't create the network file.
64 if (interface.find(".") != std::string::npos)
65 {
66 continue;
67 }
68
William A. Kennington IIIa520a392022-08-08 12:17:34 -070069 fs::path filePath = config::pathForIntfConf(confDir, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053070
71 // create the interface specific network file
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080072 // if not existing.
William A. Kennington III87f68132022-08-21 02:20:43 -070073 if (!fs::is_regular_file(filePath))
Ratan Guptab610caf2017-09-19 09:33:51 +053074 {
William A. Kennington III87f68132022-08-21 02:20:43 -070075 bmc::writeDHCPDefault(filePath, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053076 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050077 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053078 isCreated = true;
79 }
80 }
81 }
Patrick Williams5758db32021-10-06 12:29:22 -050082 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +053083 {
84 log<level::ERR>("Unable to create the default network file");
85 }
Alexander Filippov1ea35992021-03-26 13:10:05 +030086
Ratan Guptab610caf2017-09-19 09:33:51 +053087 return isCreated;
88}
89
Ratan Guptaef85eb92017-06-15 08:57:54 +053090void Manager::setConfDir(const fs::path& dir)
91{
92 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053093
94 if (!fs::exists(confDir))
95 {
96 if (!fs::create_directories(confDir))
97 {
98 log<level::ERR>("Unable to create the network conf dir",
99 entry("DIR=%s", confDir.c_str()));
100 elog<InternalFailure>();
101 }
102 }
Ratan Gupta29b0e432017-05-25 12:51:40 +0530103}
104
105void Manager::createInterfaces()
106{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500107 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530108 interfaces.clear();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530109
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530110 auto interfaceStrList = getInterfaces();
Ratan Gupta6811f822017-04-14 16:34:56 +0530111
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530112 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530113 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530114 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530115 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530116
117 // interface can be of vlan type or normal ethernet interface.
118 // vlan interface looks like "interface.vlanid",so here by looking
119 // at the interface name we decide that we need
120 // to create the vlaninterface or normal physical interface.
121 if (index != std::string::npos)
122 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500123 // it is vlan interface
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530124 auto interfaceName = interface.substr(0, index);
125 auto vlanid = interface.substr(index + 1);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530126 uint32_t vlanInt = std::stoul(vlanid);
127
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530128 interfaces[interfaceName]->loadVLAN(vlanInt);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530129 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530130 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530131 // normal ethernet interface
132 objPath /= interface;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700133 config::Parser config(config::pathForIntfConf(confDir, interface));
Ratan Gupta34f96d62017-06-15 09:16:22 +0530134
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 auto intf = std::make_shared<phosphor::network::EthernetInterface>(
William A. Kennington III0caf2212022-08-18 18:15:51 -0700136 bus, objPath.string(), config, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530137
138 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800139 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700140 intf->loadNameServers(config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530141
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700142 this->interfaces.emplace(std::move(interface), std::move(intf));
Ratan Gupta6811f822017-04-14 16:34:56 +0530143 }
144}
145
Ratan Guptaef85eb92017-06-15 08:57:54 +0530146void Manager::createChildObjects()
147{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700148 routeTable.refresh();
149
Ratan Guptaef85eb92017-06-15 08:57:54 +0530150 // creates the ethernet interface dbus object.
151 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530152
153 systemConf.reset(nullptr);
154 dhcpConf.reset(nullptr);
155
Ratan Guptaef85eb92017-06-15 08:57:54 +0530156 fs::path objPath = objectPath;
157 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530158
159 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530160 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800161 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530162 // create the dhcp conf object.
163 objPath /= "dhcp";
164 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530166}
167
Patrick Williams8173e022021-05-01 06:39:41 -0500168ObjectPath Manager::vlan(IntfName interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530169{
sureshvijayv1ee2cba82021-11-18 15:28:50 +0530170 if (!hasInterface(interfaceName))
171 {
Patrick Williams97399842022-03-16 14:14:02 -0500172 using ResourceErr =
173 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
174 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
sureshvijayv1ee2cba82021-11-18 15:28:50 +0530175 }
176
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800177 if (id == 0 || id >= 4095)
178 {
179 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
180 elog<InvalidArgument>(
181 Argument::ARGUMENT_NAME("VLANId"),
182 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
183 }
184
William A. Kennington IIIf4b4ff82019-04-09 19:06:52 -0700185 return interfaces[interfaceName]->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530186}
187
Michael Tritz29f2fd62017-05-22 15:27:26 -0500188void Manager::reset()
189{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800190 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500191 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800192 for (const auto& file : fs::directory_iterator(confDir))
193 {
194 fs::remove(file.path());
195 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500196 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800197 createDefaultNetworkFiles();
198 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500199}
200
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530201// Need to merge the below function with the code which writes the
202// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500203// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530204void Manager::writeToConfigurationFile()
205{
206 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530207 for (const auto& intf : interfaces)
208 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530209 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530210 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530211}
212
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700213#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530214void Manager::setFistBootMACOnInterface(
215 const std::pair<std::string, std::string>& inventoryEthPair)
216{
217 for (const auto& interface : interfaces)
218 {
219 if (interface.first == inventoryEthPair.first)
220 {
221 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500222 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530223 if (returnMAC == inventoryEthPair.second)
224 {
225 log<level::INFO>("Set the MAC on "),
226 entry("interface : ", interface.first.c_str()),
227 entry("MAC : ", inventoryEthPair.second.c_str());
228 std::error_code ec;
229 if (std::filesystem::is_directory("/var/lib/network", ec))
230 {
231 std::ofstream persistentFile(FirstBootFile +
232 interface.first);
233 }
234 break;
235 }
236 else
237 {
238 log<level::INFO>("MAC is Not Set on ethernet Interface");
239 }
240 }
241 }
242}
243
244#endif
245
William A. Kennington III56ecc782021-10-07 18:44:50 -0700246void Manager::reloadConfigs()
247{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800248 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800249 // Ensure that the next refresh happens after reconfiguration
250 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800251}
252
253void Manager::doReloadConfigs()
254{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800255 for (auto& hook : reloadPreHooks)
256 {
257 try
258 {
259 hook();
260 }
261 catch (const std::exception& ex)
262 {
263 log<level::ERR>("Failed executing reload hook, ignoring",
264 entry("ERR=%s", ex.what()));
265 }
266 }
267 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700268 try
269 {
270 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
271 NETWORKD_INTERFACE, "Reload");
272 bus.call_noreply(method);
273 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500274 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700275 {
276 log<level::ERR>("Failed to reload configuration",
277 entry("ERR=%s", ex.what()));
278 elog<InternalFailure>();
279 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800280 // Ensure reconfiguration has enough time
281 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700282}
283
Gunnar Mills57d9c502018-09-14 14:42:34 -0500284} // namespace network
285} // namespace phosphor