blob: c79c3950eae9896fcad59664a564ed98d5b80304 [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 IIIfd862be2022-10-09 18:40:55 -070064 for (auto& interface : system::getInterfaces())
Ratan Gupta6811f822017-04-14 16:34:56 +053065 {
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070066 config::Parser config(
67 config::pathForIntfConf(confDir, *interface.name));
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070068 auto intf = std::make_unique<EthernetInterface>(bus, *this, interface,
69 objectPath, config);
Ratan Gupta92bc2fe2017-07-26 22:40:21 +053070 intf->createIPAddressObjects();
William A. Kennington III08505792019-01-30 16:00:04 -080071 intf->createStaticNeighborObjects();
William A. Kennington IIIa520a392022-08-08 12:17:34 -070072 intf->loadNameServers(config);
Asmitha Karunanithi003b8b72022-01-06 04:17:59 -060073 intf->loadNTPServers(config);
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070074 this->interfaces.emplace(std::move(*interface.name), std::move(intf));
Ratan Gupta6811f822017-04-14 16:34:56 +053075 }
76}
77
Ratan Guptaef85eb92017-06-15 08:57:54 +053078void Manager::createChildObjects()
79{
William A. Kennington IIIe0564842021-10-23 16:02:22 -070080 routeTable.refresh();
81
Ratan Guptaef85eb92017-06-15 08:57:54 +053082 // creates the ethernet interface dbus object.
83 createInterfaces();
Ratan Guptae05083a2017-09-16 07:12:11 +053084
85 systemConf.reset(nullptr);
86 dhcpConf.reset(nullptr);
87
Ratan Guptaef85eb92017-06-15 08:57:54 +053088 fs::path objPath = objectPath;
89 objPath /= "config";
Ratan Guptae05083a2017-09-16 07:12:11 +053090
91 // create the system conf object.
Ratan Guptaef85eb92017-06-15 08:57:54 +053092 systemConf = std::make_unique<phosphor::network::SystemConfiguration>(
Jiaqing Zhao24b5a612022-04-11 16:46:16 +080093 bus, objPath.string());
Ratan Guptad16f88c2017-07-11 17:47:57 +053094 // create the dhcp conf object.
95 objPath /= "dhcp";
96 dhcpConf = std::make_unique<phosphor::network::dhcp::Configuration>(
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 bus, objPath.string(), *this);
Ratan Guptaef85eb92017-06-15 08:57:54 +053098}
99
William A. Kennington III085bbdc2022-10-05 02:45:37 -0700100ObjectPath Manager::vlan(std::string interfaceName, uint32_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +0530101{
Jiaqing Zhaob685cb62022-04-12 22:57:34 +0800102 if (id == 0 || id >= 4095)
103 {
104 log<level::ERR>("VLAN ID is not valid", entry("VLANID=%u", id));
105 elog<InvalidArgument>(
106 Argument::ARGUMENT_NAME("VLANId"),
107 Argument::ARGUMENT_VALUE(std::to_string(id).c_str()));
108 }
109
William A. Kennington III96444792022-10-05 15:16:22 -0700110 auto it = interfaces.find(interfaceName);
111 if (it == interfaces.end())
112 {
113 using ResourceErr =
114 phosphor::logging::xyz::openbmc_project::Common::ResourceNotFound;
115 elog<ResourceNotFound>(ResourceErr::RESOURCE(interfaceName.c_str()));
116 }
117 return it->second->createVLAN(id);
Ratan Gupta6811f822017-04-14 16:34:56 +0530118}
119
Michael Tritz29f2fd62017-05-22 15:27:26 -0500120void Manager::reset()
121{
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800122 if (fs::is_directory(confDir))
Michael Tritz29f2fd62017-05-22 15:27:26 -0500123 {
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800124 for (const auto& file : fs::directory_iterator(confDir))
125 {
126 fs::remove(file.path());
127 }
Michael Tritz29f2fd62017-05-22 15:27:26 -0500128 }
William A. Kennington III9a1d9af2021-11-09 17:51:05 -0800129 log<level::INFO>("Network Factory Reset queued.");
Michael Tritz29f2fd62017-05-22 15:27:26 -0500130}
131
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530132// Need to merge the below function with the code which writes the
133// config file during factory reset.
Gunnar Mills57d9c502018-09-14 14:42:34 -0500134// TODO openbmc/openbmc#1751
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530135void Manager::writeToConfigurationFile()
136{
137 // write all the static ip address in the systemd-network conf file
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530138 for (const auto& intf : interfaces)
139 {
Ratan Gupta2b106532017-07-25 16:05:02 +0530140 intf.second->writeConfigurationFile();
Ratan Gupta4f1c18b2017-05-25 12:59:35 +0530141 }
Ratan Guptae05083a2017-09-16 07:12:11 +0530142}
143
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700144#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530145void Manager::setFistBootMACOnInterface(
146 const std::pair<std::string, std::string>& inventoryEthPair)
147{
148 for (const auto& interface : interfaces)
149 {
150 if (interface.first == inventoryEthPair.first)
151 {
152 auto returnMAC =
Patrick Williams6aef7692021-05-01 06:39:41 -0500153 interface.second->macAddress(inventoryEthPair.second);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530154 if (returnMAC == inventoryEthPair.second)
155 {
156 log<level::INFO>("Set the MAC on "),
157 entry("interface : ", interface.first.c_str()),
158 entry("MAC : ", inventoryEthPair.second.c_str());
159 std::error_code ec;
160 if (std::filesystem::is_directory("/var/lib/network", ec))
161 {
162 std::ofstream persistentFile(FirstBootFile +
163 interface.first);
164 }
165 break;
166 }
167 else
168 {
169 log<level::INFO>("MAC is Not Set on ethernet Interface");
170 }
171 }
172 }
173}
174
175#endif
176
William A. Kennington III56ecc782021-10-07 18:44:50 -0700177void Manager::reloadConfigs()
178{
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800179 reloadTimer->restartOnce(reloadTimeout);
William A. Kennington IIId41db382021-11-09 20:42:29 -0800180 // Ensure that the next refresh happens after reconfiguration
181 refreshObjectTimer->setRemaining(reloadTimeout + refreshTimeout);
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800182}
183
184void Manager::doReloadConfigs()
185{
William A. Kennington III6ff633a2021-11-09 17:09:12 -0800186 for (auto& hook : reloadPreHooks)
187 {
188 try
189 {
190 hook();
191 }
192 catch (const std::exception& ex)
193 {
194 log<level::ERR>("Failed executing reload hook, ignoring",
195 entry("ERR=%s", ex.what()));
196 }
197 }
198 reloadPreHooks.clear();
William A. Kennington III56ecc782021-10-07 18:44:50 -0700199 try
200 {
201 auto method = bus.new_method_call(NETWORKD_BUSNAME, NETWORKD_PATH,
202 NETWORKD_INTERFACE, "Reload");
203 bus.call_noreply(method);
204 }
Patrick Williamsc38b0712022-07-22 19:26:54 -0500205 catch (const sdbusplus::exception_t& ex)
William A. Kennington III56ecc782021-10-07 18:44:50 -0700206 {
207 log<level::ERR>("Failed to reload configuration",
208 entry("ERR=%s", ex.what()));
209 elog<InternalFailure>();
210 }
William A. Kennington IIId41db382021-11-09 20:42:29 -0800211 // Ensure reconfiguration has enough time
212 refreshObjectTimer->setRemaining(refreshTimeout);
William A. Kennington III56ecc782021-10-07 18:44:50 -0700213}
214
Gunnar Mills57d9c502018-09-14 14:42:34 -0500215} // namespace network
216} // namespace phosphor