blob: 0032525e47de291a3a5bb3ed85fcdc5453c4908e [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"
William A. Kennington III2e09d272022-10-14 17:15:00 -07006#include "system_queries.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07007#include "types.hpp"
Ratan Gupta738a67f2017-04-21 10:38:05 +05308
William A. Kennington III085bbdc2022-10-05 02:45:37 -07009#include <charconv>
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 IIIfd862be2022-10-09 18:40:55 -070064 for (auto& interface : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +053065 {
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053066 // interface can be of vlan type or normal ethernet interface.
67 // vlan interface looks like "interface.vlanid",so here by looking
68 // at the interface name we decide that we need
69 // to create the vlaninterface or normal physical interface.
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070070 if (const auto index = interface.name->find(".");
71 index != std::string::npos)
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053072 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 // it is vlan interface
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070074 auto sv = std::string_view(*interface.name);
William A. Kennington III085bbdc2022-10-05 02:45:37 -070075 auto interfaceName = sv.substr(0, index);
76 auto vlanStr = sv.substr(index + 1);
77 uint16_t vlanId;
78 auto res = std::from_chars(vlanStr.begin(), vlanStr.end(), vlanId);
79 if (res.ec != std::errc() || res.ptr != vlanStr.end())
80 {
81 auto msg = fmt::format("Invalid VLAN: {}", vlanStr);
82 log<level::ERR>(msg.c_str());
83 continue;
84 }
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070085 auto it = interfaces.find(interfaceName);
William A. Kennington III085bbdc2022-10-05 02:45:37 -070086 if (it == interfaces.end())
87 {
88 auto msg = fmt::format("Missing interface({}) for VLAN({}): {}",
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070089 interfaceName, vlanId, *interface.name);
William A. Kennington III085bbdc2022-10-05 02:45:37 -070090 log<level::ERR>(msg.c_str());
91 continue;
92 }
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070093 it->second->loadVLAN(objectPath, vlanId, std::move(interface));
Ratan Gupta6e8df632017-08-13 09:41:58 +053094 continue;
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053095 }
Ratan Gupta34f96d62017-06-15 09:16:22 +053096
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070097 config::Parser config(
98 config::pathForIntfConf(confDir, *interface.name));
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070099 auto intf = std::make_unique<phosphor::network::EthernetInterface>(
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700100 bus, *this, interface, objectPath, config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +0530101 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -0800102 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700103 intf->loadNameServers(config);
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -0600104 intf->loadNTPServers(config);
William A. Kennington IIIfd862be2022-10-09 18:40:55 -0700105 this->interfaces.emplace(std::move(*interface.name), std::move(intf));
Ratan Gupta6811f822017-04-14 16:34:56 +0530106 }
107}
108
Ratan Guptaef85eb92017-06-15 08:57:54 +0530109void Manager::createChildObjects()
110{
William A. Kennington IIIe0564842021-10-23 16:02:22 -0700111 routeTable.refresh();
112
Ratan Guptaef85eb92017-06-15 08:57:54 +0530113 // creates the ethernet interface dbus object.
114 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +0530115
116 systemConf.reset(nullptr);
117 dhcpConf.reset(nullptr);
118
Ratan Guptaef85eb92017-06-15 08:57:54 +0530119 fs::path objPath = objectPath;
120 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +0530121
122 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +0530123 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +0800124 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +0530125 // create the dhcp conf object.
126 objPath /= "dhcp";
127 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -0500128 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +0530129}
130
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700131ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530132{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800133 if (id == 0 || id >= 4095)
134 {
135 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
136 elog<InvalidArgument>(
137 Argument::ARGUMENT_NAME("VLANId"),
138 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
139 }
140
William A. Kennington III96444792022-10-05 15:16:22 -0700141 auto it = interfaces.find(interfaceName);
142 if (it == interfaces.end())
143 {
144 using ResourceErr =
145 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
146 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
147 }
148 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530149}
150
Michael Tritz29f2fd62017-05-22 15:27:26 -0500151void Manager::reset()
152{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800153 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500154 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800155 for (const auto& file : fs::directory_iterator(confDir))
156 {
157 fs::remove(file.path());
158 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500159 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800160 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500161}
162
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530163// Need to merge the below function with the code which writes the
164// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500165// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530166void Manager::writeToConfigurationFile()
167{
168 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530169 for (const auto& intf : interfaces)
170 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530171 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530172 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530173}
174
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700175#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530176void Manager::setFistBootMACOnInterface(
177 const std::pair<std::string, std::string>& inventoryEthPair)
178{
179 for (const auto& interface : interfaces)
180 {
181 if (interface.first == inventoryEthPair.first)
182 {
183 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500184 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530185 if (returnMAC == inventoryEthPair.second)
186 {
187 log<level::INFO>("Set the MAC on "),
188 entry("interface : ", interface.first.c_str()),
189 entry("MAC : ", inventoryEthPair.second.c_str());
190 std::error_code ec;
191 if (std::filesystem::is_directory("/var/lib/network", ec))
192 {
193 std::ofstream persistentFile(FirstBootFile +
194 interface.first);
195 }
196 break;
197 }
198 else
199 {
200 log<level::INFO>("MAC is Not Set on ethernet Interface");
201 }
202 }
203 }
204}
205
206#endif
207
William A. Kennington III56ecc782021-10-07 18:44:50 -0700208void Manager::reloadConfigs()
209{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800210 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800211 // Ensure that the next refresh happens after reconfiguration
212 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800213}
214
215void Manager::doReloadConfigs()
216{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800217 for (auto& hook : reloadPreHooks)
218 {
219 try
220 {
221 hook();
222 }
223 catch (const std::exception& ex)
224 {
225 log<level::ERR>("Failed executing reload hook, ignoring",
226 entry("ERR=%s", ex.what()));
227 }
228 }
229 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700230 try
231 {
232 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
233 NETWORKD_INTERFACE, "Reload");
234 bus.call_noreply(method);
235 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500236 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700237 {
238 log<level::ERR>("Failed to reload configuration",
239 entry("ERR=%s", ex.what()));
240 elog<InternalFailure>();
241 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800242 // Ensure reconfiguration has enough time
243 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700244}
245
Gunnar Mills57d9c502018-09-14 14:42:34 -0500246} // namespace network
247} // namespace phosphor