blob: df9bf9b37d5857087393b7ab3d7781648509ae68 [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 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
William A. Kennington III085bbdc2022-10-05 02:45:37 -070010#include <charconv>
Manojkiran Edacc099a82020-05-11 14:25:16 +053011#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070013#include <phosphor-logging/elog-errors.hpp>
14#include <phosphor-logging/log.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
Patrick Williamsc38b0712022-07-22 19:26:54 -050037Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070038 const fs::path& confDir) :
Patrick Williams166b9592022-03-30 16:09:16 -050039 details::VLANCreateIface(bus, objPath,
40 details::VLANCreateIface::action::defer_emit),
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053042{
Ratan Gupta255d5142017-08-10 09:02:08 +053043 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053044}
45
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080046bool Manager::createDefaultNetworkFiles()
Ratan Guptab610caf2017-09-19 09:33:51 +053047{
48 auto isCreated = false;
49 try
50 {
William A. Kennington III2e09d272022-10-14 17:15:00 -070051 auto interfaceStrList = system::getInterfaces();
Ratan Guptab610caf2017-09-19 09:33:51 +053052 for (const auto& interface : interfaceStrList)
53 {
Michael Tritz08c34f42017-10-16 14:59:09 -050054 // if the interface has '.' in the name, it means that this is a
55 // VLAN - don't create the network file.
56 if (interface.find(".") != std::string::npos)
57 {
58 continue;
59 }
60
William A. Kennington IIIa520a392022-08-08 12:17:34 -070061 fs::path filePath = config::pathForIntfConf(confDir, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053062
63 // create the interface specific network file
William A. Kennington III9a1d9af2021-11-09 17:51:05 -080064 // if not existing.
William A. Kennington III87f68132022-08-21 02:20:43 -070065 if (!fs::is_regular_file(filePath))
Ratan Guptab610caf2017-09-19 09:33:51 +053066 {
William A. Kennington III87f68132022-08-21 02:20:43 -070067 bmc::writeDHCPDefault(filePath, interface);
Ratan Guptab610caf2017-09-19 09:33:51 +053068 log<level::INFO>("Created the default network file.",
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptab610caf2017-09-19 09:33:51 +053070 isCreated = true;
71 }
72 }
73 }
Patrick Williams5758db32021-10-06 12:29:22 -050074 catch (const std::exception& e)
Ratan Guptab610caf2017-09-19 09:33:51 +053075 {
76 log<level::ERR>("Unable to create the default network file");
77 }
Alexander Filippov1ea35992021-03-26 13:10:05 +030078
Ratan Guptab610caf2017-09-19 09:33:51 +053079 return isCreated;
80}
81
Ratan Guptaef85eb92017-06-15 08:57:54 +053082void Manager::setConfDir(const fs::path& dir)
83{
84 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053085
86 if (!fs::exists(confDir))
87 {
88 if (!fs::create_directories(confDir))
89 {
90 log<level::ERR>("Unable to create the network conf dir",
91 entry("DIR=%s", confDir.c_str()));
92 elog<InternalFailure>();
93 }
94 }
Ratan Gupta29b0e432017-05-25 12:51:40 +053095}
96
97void Manager::createInterfaces()
98{
Gunnar Mills57d9c502018-09-14 14:42:34 -050099 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +0530100 interfaces.clear();
William A. Kennington III2e09d272022-10-14 17:15:00 -0700101 auto interfaceStrList = system::getInterfaces();
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530102 for (auto& interface : interfaceStrList)
Ratan Gupta6811f822017-04-14 16:34:56 +0530103 {
Ratan Gupta29b0e432017-05-25 12:51:40 +0530104 fs::path objPath = objectPath;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530105 auto index = interface.find(".");
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530106
107 // interface can be of vlan type or normal ethernet interface.
108 // vlan interface looks like "interface.vlanid",so here by looking
109 // at the interface name we decide that we need
110 // to create the vlaninterface or normal physical interface.
111 if (index != std::string::npos)
112 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500113 // it is vlan interface
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700114 auto sv = std::string_view(interface);
115 auto interfaceName = sv.substr(0, index);
116 auto vlanStr = sv.substr(index + 1);
117 uint16_t vlanId;
118 auto res = std::from_chars(vlanStr.begin(), vlanStr.end(), vlanId);
119 if (res.ec != std::errc() || res.ptr != vlanStr.end())
120 {
121 auto msg = fmt::format("Invalid VLAN: {}", vlanStr);
122 log<level::ERR>(msg.c_str());
123 continue;
124 }
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -0700125 auto it = interfaces.find(interfaceName);
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700126 if (it == interfaces.end())
127 {
128 auto msg = fmt::format("Missing interface({}) for VLAN({}): {}",
129 interfaceName, vlanId, interface);
130 log<level::ERR>(msg.c_str());
131 continue;
132 }
133 it->second->loadVLAN(vlanId);
Ratan Gupta6e8df632017-08-13 09:41:58 +0530134 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530135 }
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530136 // normal ethernet interface
137 objPath /= interface;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700138 config::Parser config(config::pathForIntfConf(confDir, interface));
Ratan Gupta34f96d62017-06-15 09:16:22 +0530139
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -0700140 auto intf = std::make_unique<phosphor::network::EthernetInterface>(
William A. Kennington III0caf2212022-08-18 18:15:51 -0700141 bus, objPath.string(), config, *this);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530142
143 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800144 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700145 intf->loadNameServers(config);
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600146 intf->loadNTPServers(config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530147
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700148 this->interfaces.emplace(std::move(interface), std::move(intf));
Ratan Gupta6811f822017-04-14 16:34:56 +0530149 }
150}
151
Ratan Guptaef85eb92017-06-15 08:57:54 +0530152void Manager::createChildObjects()
153{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700154 routeTable.refresh();
155
Ratan Guptaef85eb92017-06-15 08:57:54 +0530156 // creates the ethernet interface dbus object.
157 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530158
159 systemConf.reset(nullptr);
160 dhcpConf.reset(nullptr);
161
Ratan Guptaef85eb92017-06-15 08:57:54 +0530162 fs::path objPath = objectPath;
163 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530164
165 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530166 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800167 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530168 // create the dhcp conf object.
169 objPath /= "dhcp";
170 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500171 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530172}
173
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700174ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530175{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800176 if (id == 0 || id >= 4095)
177 {
178 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
179 elog<InvalidArgument>(
180 Argument::ARGUMENT_NAME("VLANId"),
181 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
182 }
183
William A. Kennington III96444792022-10-05 15:16:22 -0700184 auto it = interfaces.find(interfaceName);
185 if (it == interfaces.end())
186 {
187 using ResourceErr =
188 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
189 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
190 }
191 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530192}
193
Michael Tritz29f2fd62017-05-22 15:27:26 -0500194void Manager::reset()
195{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800196 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500197 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800198 for (const auto& file : fs::directory_iterator(confDir))
199 {
200 fs::remove(file.path());
201 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500202 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800203 createDefaultNetworkFiles();
204 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500205}
206
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530207// Need to merge the below function with the code which writes the
208// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500209// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530210void Manager::writeToConfigurationFile()
211{
212 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530213 for (const auto& intf : interfaces)
214 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530215 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530216 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530217}
218
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700219#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530220void Manager::setFistBootMACOnInterface(
221 const std::pair<std::string, std::string>& inventoryEthPair)
222{
223 for (const auto& interface : interfaces)
224 {
225 if (interface.first == inventoryEthPair.first)
226 {
227 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500228 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530229 if (returnMAC == inventoryEthPair.second)
230 {
231 log<level::INFO>("Set the MAC on "),
232 entry("interface : ", interface.first.c_str()),
233 entry("MAC : ", inventoryEthPair.second.c_str());
234 std::error_code ec;
235 if (std::filesystem::is_directory("/var/lib/network", ec))
236 {
237 std::ofstream persistentFile(FirstBootFile +
238 interface.first);
239 }
240 break;
241 }
242 else
243 {
244 log<level::INFO>("MAC is Not Set on ethernet Interface");
245 }
246 }
247 }
248}
249
250#endif
251
William A. Kennington III56ecc782021-10-07 18:44:50 -0700252void Manager::reloadConfigs()
253{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800254 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800255 // Ensure that the next refresh happens after reconfiguration
256 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800257}
258
259void Manager::doReloadConfigs()
260{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800261 for (auto& hook : reloadPreHooks)
262 {
263 try
264 {
265 hook();
266 }
267 catch (const std::exception& ex)
268 {
269 log<level::ERR>("Failed executing reload hook, ignoring",
270 entry("ERR=%s", ex.what()));
271 }
272 }
273 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700274 try
275 {
276 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
277 NETWORKD_INTERFACE, "Reload");
278 bus.call_noreply(method);
279 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500280 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700281 {
282 log<level::ERR>("Failed to reload configuration",
283 entry("ERR=%s", ex.what()));
284 elog<InternalFailure>();
285 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800286 // Ensure reconfiguration has enough time
287 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700288}
289
Gunnar Mills57d9c502018-09-14 14:42:34 -0500290} // namespace network
291} // namespace phosphor