blob: 8f8a89ea7e2c76a6109eb6c0744f08cf5555e530 [file] [log] [blame]
Ratan Guptaa54d8f82017-09-08 17:05:46 +05301#include "config.h"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05303#include "network_manager.hpp"
4#include "rtnetlink_server.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07005#include "types.hpp"
William A. Kennington III95530ec2022-08-19 01:44:39 -07006#ifdef SYNC_MAC_FROM_INVENTORY
7#include "util.hpp"
8#endif
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070010#include <fmt/format.h>
Ratan Guptaf6657382017-11-10 17:58:17 +053011#include <linux/netlink.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053012
Manojkiran Edacc099a82020-05-11 14:25:16 +053013#include <filesystem>
14#include <fstream>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070015#include <functional>
Ratan Guptaf6657382017-11-10 17:58:17 +053016#include <memory>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070017#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053018#include <nlohmann/json.hpp>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070019#endif
Ratan Guptaf6657382017-11-10 17:58:17 +053020#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053021#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053022#include <sdbusplus/bus.hpp>
Manojkiran Edacc099a82020-05-11 14:25:16 +053023#include <sdbusplus/bus/match.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053024#include <sdbusplus/server/manager.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070025#include <sdeventplus/event.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070026#include <sdeventplus/source/signal.hpp>
27#include <stdplus/signal.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053028#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053029
William A. Kennington III09095f82018-09-27 12:05:12 -070030using phosphor::logging::elog;
31using phosphor::logging::entry;
32using phosphor::logging::level;
33using phosphor::logging::log;
34using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Manojkiran Edacc099a82020-05-11 14:25:16 +053035using DbusObjectPath = std::string;
36using DbusInterface = std::string;
37using PropertyValue = std::string;
William A. Kennington III09095f82018-09-27 12:05:12 -070038
William A. Kennington III483e7772019-02-12 19:03:15 -080039constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network";
40
William A. Kennington III5f1eb462019-02-12 19:47:51 -080041constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
42
Manojkiran Edacc099a82020-05-11 14:25:16 +053043constexpr auto firstBootPath = "/var/lib/network/firstBoot_";
44constexpr auto configFile = "/usr/share/network/config.json";
45
46constexpr auto invNetworkIntf =
47 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
48
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053049namespace phosphor
50{
51namespace network
52{
53
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070054std::unique_ptr<Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070055std::unique_ptr<Timer> refreshObjectTimer = nullptr;
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080056std::unique_ptr<Timer> reloadTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053057
William A. Kennington III6f39c5e2021-05-13 18:39:23 -070058#ifdef SYNC_MAC_FROM_INVENTORY
Patrick Williamsc38b0712022-07-22 19:26:54 -050059std::unique_ptr<sdbusplus::bus::match_t> EthInterfaceMatch = nullptr;
Manojkiran Edacc099a82020-05-11 14:25:16 +053060std::vector<std::string> first_boot_status;
61
Patrick Williamsc38b0712022-07-22 19:26:54 -050062bool setInventoryMACOnSystem(sdbusplus::bus_t& bus,
Manojkiran Edacc099a82020-05-11 14:25:16 +053063 const nlohmann::json& configJson,
64 const std::string& intfname)
65{
66 try
67 {
68 auto inventoryMAC = mac_address::getfromInventory(bus, intfname);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070069 if (inventoryMAC != ether_addr{})
Manojkiran Edacc099a82020-05-11 14:25:16 +053070 {
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070071 auto macStr = std::to_string(inventoryMAC);
72 log<level::INFO>("Mac Address in Inventory on ",
73 entry("Interface : ", intfname.c_str()),
74 entry("MAC Address :", macStr.c_str()));
75 manager->setFistBootMACOnInterface(
76 std::make_pair(intfname.c_str(), std::move(macStr)));
Manojkiran Edacc099a82020-05-11 14:25:16 +053077 first_boot_status.push_back(intfname.c_str());
78 bool status = true;
79 for (const auto& keys : configJson.items())
80 {
81 if (!(std::find(first_boot_status.begin(),
82 first_boot_status.end(),
83 keys.key()) != first_boot_status.end()))
84 {
85 log<level::INFO>("Interface MAC is NOT set from VPD"),
86 entry("INTERFACE", keys.key().c_str());
87 status = false;
88 }
89 }
90 if (status)
91 {
92 log<level::INFO>("Removing the match for ethernet interfaces");
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070093 EthInterfaceMatch = nullptr;
Manojkiran Edacc099a82020-05-11 14:25:16 +053094 }
95 }
96 else
97 {
98 log<level::INFO>("Nothing is present in Inventory");
99 return false;
100 }
101 }
102 catch (const std::exception& e)
103 {
104 log<level::ERR>("Exception occurred during getting of MAC "
105 "address from Inventory");
106 return false;
107 }
108 return true;
109}
110
111// register the macthes to be monitored from inventory manager
Patrick Williamsc38b0712022-07-22 19:26:54 -0500112void registerSignals(sdbusplus::bus_t& bus, const nlohmann::json& configJson)
Manojkiran Edacc099a82020-05-11 14:25:16 +0530113{
114 log<level::INFO>("Registering the Inventory Signals Matcher");
115
Patrick Williamsc38b0712022-07-22 19:26:54 -0500116 static std::unique_ptr<sdbusplus::bus::match_t> MacAddressMatch;
Manojkiran Edacc099a82020-05-11 14:25:16 +0530117
Patrick Williamsc38b0712022-07-22 19:26:54 -0500118 auto callback = [&](sdbusplus::message_t& m) {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530119 std::map<DbusObjectPath,
120 std::map<DbusInterface, std::variant<PropertyValue>>>
121 interfacesProperties;
122
123 sdbusplus::message::object_path objPath;
124 std::pair<std::string, std::string> ethPair;
125 m.read(objPath, interfacesProperties);
126
127 for (const auto& pattern : configJson.items())
128 {
129 if (objPath.str.find(pattern.value()) != std::string::npos)
130 {
131 for (auto& interface : interfacesProperties)
132 {
133 if (interface.first == invNetworkIntf)
134 {
135 for (const auto& property : interface.second)
136 {
137 if (property.first == "MACAddress")
138 {
139 ethPair = std::make_pair(
140 pattern.key(),
141 std::get<std::string>(property.second));
142 break;
143 }
144 }
145 break;
146 }
147 }
148 if (!(ethPair.first.empty() || ethPair.second.empty()))
149 {
150 manager->setFistBootMACOnInterface(ethPair);
151 }
152 }
153 }
154 };
155
Patrick Williamsc38b0712022-07-22 19:26:54 -0500156 MacAddressMatch = std::make_unique<sdbusplus::bus::match_t>(
Manojkiran Edacc099a82020-05-11 14:25:16 +0530157 bus,
158 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
159 "member='InterfacesAdded',path='/xyz/openbmc_project/"
160 "inventory'",
161 callback);
162}
163
Patrick Williamsc38b0712022-07-22 19:26:54 -0500164void watchEthernetInterface(sdbusplus::bus_t& bus,
Manojkiran Edacc099a82020-05-11 14:25:16 +0530165 const nlohmann::json& configJson)
166{
Patrick Williamsc38b0712022-07-22 19:26:54 -0500167 auto mycallback = [&](sdbusplus::message_t& m) {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530168 std::map<DbusObjectPath,
169 std::map<DbusInterface, std::variant<PropertyValue>>>
170 interfacesProperties;
171
172 sdbusplus::message::object_path objPath;
173 std::pair<std::string, std::string> ethPair;
174 m.read(objPath, interfacesProperties);
175 for (const auto& interfaces : interfacesProperties)
176 {
177 if (interfaces.first ==
178 "xyz.openbmc_project.Network.EthernetInterface")
179 {
180 for (const auto& property : interfaces.second)
181 {
182 if (property.first == "InterfaceName")
183 {
184 std::string infname =
185 std::get<std::string>(property.second);
186
187 if (configJson.find(infname) == configJson.end())
188 {
189 // ethernet interface not found in configJSON
190 // check if it is not sit0 interface, as it is
191 // expected.
192 if (infname != "sit0")
193 {
194 log<level::ERR>(
195 "Wrong Interface Name in Config Json");
196 }
197 }
198 else
199 {
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700200 if (!setInventoryMACOnSystem(bus, configJson,
201 infname))
Manojkiran Edacc099a82020-05-11 14:25:16 +0530202 {
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700203 registerSignals(bus, configJson);
204 EthInterfaceMatch = nullptr;
Manojkiran Edacc099a82020-05-11 14:25:16 +0530205 }
206 }
207 break;
208 }
209 }
210 break;
211 }
212 }
213 };
214 // Incase if phosphor-inventory-manager started early and the VPD is already
215 // collected by the time network service has come up, better to check the
216 // VPD directly and set the MAC Address on the respective Interface.
217
218 bool registeredSignals = false;
219 for (const auto& interfaceString : configJson.items())
220 {
221 if (!std::filesystem::exists(firstBootPath + interfaceString.key()) &&
222 !registeredSignals)
223 {
224
225 log<level::INFO>(
226 "First boot file is not present, check VPD for MAC");
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700227 EthInterfaceMatch = std::make_unique<sdbusplus::bus::match_t>(
Manojkiran Edacc099a82020-05-11 14:25:16 +0530228 bus,
229 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
230 "member='InterfacesAdded',path='/xyz/openbmc_project/network'",
231 mycallback);
232 registeredSignals = true;
233 }
234 }
235}
236
237#endif
238
Ratan Gupta16f12882017-09-22 18:26:11 +0530239/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530240void refreshObjects()
241{
Ratan Gupta16f12882017-09-22 18:26:11 +0530242 if (manager)
243 {
Ratan Gupta310a0b12017-11-15 17:40:24 +0530244 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +0530245 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +0530246 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +0530247 }
248}
249
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800250void reloadNetworkd()
251{
252 if (manager)
253 {
254 log<level::INFO>("Sending networkd reload");
255 manager->doReloadConfigs();
256 log<level::INFO>("Done networkd reload");
257 }
258}
259
William A. Kennington III217bb3f2022-09-29 13:43:13 -0700260void initializeTimers(sdeventplus::Event& event)
Ratan Gupta16f12882017-09-22 18:26:11 +0530261{
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700262 refreshObjectTimer =
263 std::make_unique<Timer>(event, std::bind(refreshObjects));
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -0800264 reloadTimer = std::make_unique<Timer>(event, std::bind(reloadNetworkd));
Ratan Gupta16f12882017-09-22 18:26:11 +0530265}
266
William A. Kennington III217bb3f2022-09-29 13:43:13 -0700267void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
268{
269 log<level::NOTICE>("Got TERM, exiting");
270 signal.get_event().exit(0);
271}
272
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700273int main()
Ratan Gupta8c834932017-04-14 16:30:24 +0530274{
William A. Kennington III217bb3f2022-09-29 13:43:13 -0700275 auto event = sdeventplus::Event::get_default();
276 stdplus::signal::block(SIGTERM);
277 sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
278
279 initializeTimers(event);
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530280
Ratan Guptacb7098d2017-04-14 17:46:05 +0530281 auto bus = sdbusplus::bus::new_default();
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530282 // Attach the bus to sd_event to service user requests
William A. Kennington III217bb3f2022-09-29 13:43:13 -0700283 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530284
Ratan Guptacb7098d2017-04-14 17:46:05 +0530285 // Add sdbusplus Object Manager for the 'root' path of the network manager.
Patrick Williamsc38b0712022-07-22 19:26:54 -0500286 sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800287 bus.request_name(DEFAULT_BUSNAME);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530288
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700289 manager = std::make_unique<Manager>(bus, DEFAULT_OBJPATH, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530290
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530291 // RTNETLINK event handler
William A. Kennington III5f165dc2022-10-24 15:58:55 -0700292 netlink::Server svr(event);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530293
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700294#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530295 std::ifstream in(configFile);
296 nlohmann::json configJson;
297 in >> configJson;
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700298 watchEthernetInterface(bus, configJson);
Manojkiran Edacc099a82020-05-11 14:25:16 +0530299#endif
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700300
301 // Trigger the initial object scan
William A. Kennington III26c40a42021-11-03 18:27:52 -0700302 // This is intentionally deferred, to ensure that systemd-networkd is
303 // fully configured.
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700304 refreshObjectTimer->restartOnce(refreshTimeout);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700305
William A. Kennington III217bb3f2022-09-29 13:43:13 -0700306 return event.loop();
William A. Kennington III4fd52ae2022-09-30 13:49:39 -0700307}
308
309} // namespace network
310} // namespace phosphor
311
312int main(int /*argc*/, char** /*argv*/)
313{
314 try
315 {
316 return phosphor::network::main();
317 }
318 catch (const std::exception& e)
319 {
320 auto msg = fmt::format("FAILED: {}", e.what());
321 log<level::ERR>(msg.c_str(), entry("ERROR", e.what()));
322 return 1;
323 }
Ratan Gupta8c834932017-04-14 16:30:24 +0530324}