blob: beb677a35a7fd9d5464cc55de8da9760479c592d [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>
Patrick Venture189d44e2018-07-09 12:30:59 -070014#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta6811f822017-04-14 16:34:56 +053015
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080016constexpr char SYSTEMD_BUSNAME[] = "org.freedesktop.systemd1";
17constexpr char SYSTEMD_PATH[] = "/org/freedesktop/systemd1";
18constexpr char SYSTEMD_INTERFACE[] = "org.freedesktop.systemd1.Manager";
Manojkiran Edacc099a82020-05-11 14:25:16 +053019constexpr auto FirstBootFile = "/var/lib/network/firstBoot_";
William A. Kennington IIIf1aa51c2019-02-12 19:58:11 -080020
William A. Kennington III56ecc782021-10-07 18:44:50 -070021constexpr char NETWORKD_BUSNAME[] = "org.freedesktop.network1";
22constexpr char NETWORKD_PATH[] = "/org/freedesktop/network1";
23constexpr char NETWORKD_INTERFACE[] = "org.freedesktop.network1.Manager";
24
Ratan Gupta6811f822017-04-14 16:34:56 +053025namespace phosphor
26{
27namespace network
28{
Ratan Gupta82549cc2017-04-21 08:45:23 +053029
William A. Kennington IIId41db382021-11-09 20:42:29 -080030extern std::unique_ptr<Timer> refreshObjectTimer;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080031extern std::unique_ptr<Timer> reloadTimer;
Ratan Gupta6811f822017-04-14 16:34:56 +053032using namespace phosphor::logging;
Ratan Guptaef85eb92017-06-15 08:57:54 +053033using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Jiaqing Zhaob685cb62022-04-12 22:57:34 +080034using Argument = xyz::openbmc_project::Common::InvalidArgument;
Ratan Gupta6811f822017-04-14 16:34:56 +053035
Patrick Williamsc38b0712022-07-22 19:26:54 -050036Manager::Manager(sdbusplus::bus_t& bus, const char* objPath,
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070037 const fs::path& confDir) :
Patrick Williams166b9592022-03-30 16:09:16 -050038 details::VLANCreateIface(bus, objPath,
39 details::VLANCreateIface::action::defer_emit),
Gunnar Mills57d9c502018-09-14 14:42:34 -050040 bus(bus), objectPath(objPath)
Ratan Gupta6811f822017-04-14 16:34:56 +053041{
Ratan Gupta255d5142017-08-10 09:02:08 +053042 setConfDir(confDir);
Ratan Guptaef85eb92017-06-15 08:57:54 +053043}
44
45void Manager::setConfDir(const fs::path& dir)
46{
47 confDir = dir;
Ratan Gupta255d5142017-08-10 09:02:08 +053048
49 if (!fs::exists(confDir))
50 {
51 if (!fs::create_directories(confDir))
52 {
53 log<level::ERR>("Unable to create the network conf dir",
54 entry("DIR=%s", confDir.c_str()));
55 elog<InternalFailure>();
56 }
57 }
Ratan Gupta29b0e432017-05-25 12:51:40 +053058}
59
60void Manager::createInterfaces()
61{
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 // clear all the interfaces first
Ratan Guptaef85eb92017-06-15 08:57:54 +053063 interfaces.clear();
William A. Kennington III67b09da2022-10-31 14:09:53 -070064 interfacesByIdx.clear();
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070065 for (auto& interface : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +053066 {
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070067 config::Parser config(
68 config::pathForIntfConf(confDir, *interface.name));
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070069 auto intf = std::make_unique<EthernetInterface>(bus, *this, interface,
70 objectPath, config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053071 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -080072 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -070073 intf->loadNameServers(config);
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060074 intf->loadNTPServers(config);
William A. Kennington III67b09da2022-10-31 14:09:53 -070075 auto ptr = intf.get();
76 interfaces.emplace(std::move(*interface.name), std::move(intf));
77 interfacesByIdx.emplace(interface.idx, ptr);
Ratan Gupta6811f822017-04-14 16:34:56 +053078 }
79}
80
Ratan Guptaef85eb92017-06-15 08:57:54 +053081void Manager::createChildObjects()
82{
William A. Kennington IIIe0564842021-10-23 16:02:22 -070083 routeTable.refresh();
84
Ratan Guptaef85eb92017-06-15 08:57:54 +053085 // creates the ethernet interface dbus object.
86 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +053087
88 systemConf.reset(nullptr);
89 dhcpConf.reset(nullptr);
90
Ratan Guptaef85eb92017-06-15 08:57:54 +053091 fs::path objPath = objectPath;
92 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +053093
94 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +053095 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080096 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +053097 // create the dhcp conf object.
98 objPath /= "dhcp";
99 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530101}
102
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700103ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530104{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800105 if (id == 0 || id >= 4095)
106 {
107 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
108 elog<InvalidArgument>(
109 Argument::ARGUMENT_NAME("VLANId"),
110 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
111 }
112
William A. Kennington III96444792022-10-05 15:16:22 -0700113 auto it = interfaces.find(interfaceName);
114 if (it == interfaces.end())
115 {
116 using ResourceErr =
117 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
118 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
119 }
120 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530121}
122
Michael Tritz29f2fd62017-05-22 15:27:26 -0500123void Manager::reset()
124{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800125 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500126 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800127 for (const auto& file : fs::directory_iterator(confDir))
128 {
129 fs::remove(file.path());
130 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500131 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800132 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500133}
134
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530135// Need to merge the below function with the code which writes the
136// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500137// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530138void Manager::writeToConfigurationFile()
139{
140 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530141 for (const auto& intf : interfaces)
142 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530143 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530144 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530145}
146
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700147#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530148void Manager::setFistBootMACOnInterface(
149 const std::pair<std::string, std::string>& inventoryEthPair)
150{
151 for (const auto& interface : interfaces)
152 {
153 if (interface.first == inventoryEthPair.first)
154 {
155 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500156 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530157 if (returnMAC == inventoryEthPair.second)
158 {
159 log<level::INFO>("Set the MAC on "),
160 entry("interface : ", interface.first.c_str()),
161 entry("MAC : ", inventoryEthPair.second.c_str());
162 std::error_code ec;
163 if (std::filesystem::is_directory("/var/lib/network", ec))
164 {
165 std::ofstream persistentFile(FirstBootFile +
166 interface.first);
167 }
168 break;
169 }
170 else
171 {
172 log<level::INFO>("MAC is Not Set on ethernet Interface");
173 }
174 }
175 }
176}
177
178#endif
179
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800180void Manager::reloadConfigsNoRefresh()
William A. Kennington III56ecc782021-10-07 18:44:50 -0700181{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800182 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800183}
184
185void Manager::reloadConfigs()
186{
187 reloadConfigsNoRefresh();
William A. Kennington IIId41db382021-11-09 20:42:29 -0800188 // Ensure that the next refresh happens after reconfiguration
189 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800190}
191
192void Manager::doReloadConfigs()
193{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800194 for (auto& hook : reloadPreHooks)
195 {
196 try
197 {
198 hook();
199 }
200 catch (const std::exception& ex)
201 {
202 log<level::ERR>("Failed executing reload hook, ignoring",
203 entry("ERR=%s", ex.what()));
204 }
205 }
206 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700207 try
208 {
209 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
210 NETWORKD_INTERFACE, "Reload");
211 bus.call_noreply(method);
212 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500213 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700214 {
215 log<level::ERR>("Failed to reload configuration",
216 entry("ERR=%s", ex.what()));
217 elog<InternalFailure>();
218 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800219 // Ensure reconfiguration has enough time
William A. Kennington III85dc57a2022-11-07 16:53:24 -0800220 if (refreshObjectTimer->isEnabled())
221 {
222 refreshObjectTimer->setRemaining(refreshTimeout);
223 }
William A. Kennington III56ecc782021-10-07 18:44:50 -0700224}
225
Gunnar Mills57d9c502018-09-14 14:42:34 -0500226} // namespace network
227} // namespace phosphor