blob: c2959964358342f153c27c1596d70e57882aa92a [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"
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +05306#include "watch.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05307
Ratan Guptaf6657382017-11-10 17:58:17 +05308#include <linux/netlink.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
Manojkiran Edacc099a82020-05-11 14:25:16 +053010#include <filesystem>
11#include <fstream>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070012#include <functional>
Ratan Guptaf6657382017-11-10 17:58:17 +053013#include <memory>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070014#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053015#include <nlohmann/json.hpp>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070016#endif
Ratan Guptaf6657382017-11-10 17:58:17 +053017#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053018#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053019#include <sdbusplus/bus.hpp>
Manojkiran Edacc099a82020-05-11 14:25:16 +053020#include <sdbusplus/bus/match.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053021#include <sdbusplus/server/manager.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070022#include <sdeventplus/event.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053023#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053024
William A. Kennington III09095f82018-09-27 12:05:12 -070025using phosphor::logging::elog;
26using phosphor::logging::entry;
27using phosphor::logging::level;
28using phosphor::logging::log;
29using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Manojkiran Edacc099a82020-05-11 14:25:16 +053030using DbusObjectPath = std::string;
31using DbusInterface = std::string;
32using PropertyValue = std::string;
William A. Kennington III09095f82018-09-27 12:05:12 -070033
William A. Kennington III483e7772019-02-12 19:03:15 -080034constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network";
35
William A. Kennington III5f1eb462019-02-12 19:47:51 -080036constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
37
Manojkiran Edacc099a82020-05-11 14:25:16 +053038constexpr auto firstBootPath = "/var/lib/network/firstBoot_";
39constexpr auto configFile = "/usr/share/network/config.json";
40
41constexpr auto invNetworkIntf =
42 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
43
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053044namespace phosphor
45{
46namespace network
47{
48
49std::unique_ptr<phosphor::network::Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070050std::unique_ptr<Timer> refreshObjectTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053051
William A. Kennington III6f39c5e2021-05-13 18:39:23 -070052#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053053std::unique_ptr<sdbusplus::bus::match::match> EthInterfaceMatch = nullptr;
54std::vector<std::string> first_boot_status;
55
56bool setInventoryMACOnSystem(sdbusplus::bus::bus& bus,
57 const nlohmann::json& configJson,
58 const std::string& intfname)
59{
60 try
61 {
62 auto inventoryMAC = mac_address::getfromInventory(bus, intfname);
63 if (!mac_address::toString(inventoryMAC).empty())
64 {
65 log<level::INFO>("Mac Address in Inventory on "),
66 entry("Interface : ", intfname.c_str()),
67 entry("MAC Address :",
68 (mac_address::toString(inventoryMAC)).c_str());
69 manager->setFistBootMACOnInterface(std::make_pair(
70 intfname.c_str(), mac_address::toString(inventoryMAC)));
71 first_boot_status.push_back(intfname.c_str());
72 bool status = true;
73 for (const auto& keys : configJson.items())
74 {
75 if (!(std::find(first_boot_status.begin(),
76 first_boot_status.end(),
77 keys.key()) != first_boot_status.end()))
78 {
79 log<level::INFO>("Interface MAC is NOT set from VPD"),
80 entry("INTERFACE", keys.key().c_str());
81 status = false;
82 }
83 }
84 if (status)
85 {
86 log<level::INFO>("Removing the match for ethernet interfaces");
87 phosphor::network::EthInterfaceMatch = nullptr;
88 }
89 }
90 else
91 {
92 log<level::INFO>("Nothing is present in Inventory");
93 return false;
94 }
95 }
96 catch (const std::exception& e)
97 {
98 log<level::ERR>("Exception occurred during getting of MAC "
99 "address from Inventory");
100 return false;
101 }
102 return true;
103}
104
105// register the macthes to be monitored from inventory manager
106void registerSignals(sdbusplus::bus::bus& bus, const nlohmann::json& configJson)
107{
108 log<level::INFO>("Registering the Inventory Signals Matcher");
109
110 static std::unique_ptr<sdbusplus::bus::match::match> MacAddressMatch;
111
112 auto callback = [&](sdbusplus::message::message& m) {
113 std::map<DbusObjectPath,
114 std::map<DbusInterface, std::variant<PropertyValue>>>
115 interfacesProperties;
116
117 sdbusplus::message::object_path objPath;
118 std::pair<std::string, std::string> ethPair;
119 m.read(objPath, interfacesProperties);
120
121 for (const auto& pattern : configJson.items())
122 {
123 if (objPath.str.find(pattern.value()) != std::string::npos)
124 {
125 for (auto& interface : interfacesProperties)
126 {
127 if (interface.first == invNetworkIntf)
128 {
129 for (const auto& property : interface.second)
130 {
131 if (property.first == "MACAddress")
132 {
133 ethPair = std::make_pair(
134 pattern.key(),
135 std::get<std::string>(property.second));
136 break;
137 }
138 }
139 break;
140 }
141 }
142 if (!(ethPair.first.empty() || ethPair.second.empty()))
143 {
144 manager->setFistBootMACOnInterface(ethPair);
145 }
146 }
147 }
148 };
149
150 MacAddressMatch = std::make_unique<sdbusplus::bus::match::match>(
151 bus,
152 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
153 "member='InterfacesAdded',path='/xyz/openbmc_project/"
154 "inventory'",
155 callback);
156}
157
158void watchEthernetInterface(sdbusplus::bus::bus& bus,
159 const nlohmann::json& configJson)
160{
161 auto mycallback = [&](sdbusplus::message::message& m) {
162 std::map<DbusObjectPath,
163 std::map<DbusInterface, std::variant<PropertyValue>>>
164 interfacesProperties;
165
166 sdbusplus::message::object_path objPath;
167 std::pair<std::string, std::string> ethPair;
168 m.read(objPath, interfacesProperties);
169 for (const auto& interfaces : interfacesProperties)
170 {
171 if (interfaces.first ==
172 "xyz.openbmc_project.Network.EthernetInterface")
173 {
174 for (const auto& property : interfaces.second)
175 {
176 if (property.first == "InterfaceName")
177 {
178 std::string infname =
179 std::get<std::string>(property.second);
180
181 if (configJson.find(infname) == configJson.end())
182 {
183 // ethernet interface not found in configJSON
184 // check if it is not sit0 interface, as it is
185 // expected.
186 if (infname != "sit0")
187 {
188 log<level::ERR>(
189 "Wrong Interface Name in Config Json");
190 }
191 }
192 else
193 {
194 if (!phosphor::network::setInventoryMACOnSystem(
195 bus, configJson, infname))
196 {
197 phosphor::network::registerSignals(bus,
198 configJson);
199 phosphor::network::EthInterfaceMatch = nullptr;
200 }
201 }
202 break;
203 }
204 }
205 break;
206 }
207 }
208 };
209 // Incase if phosphor-inventory-manager started early and the VPD is already
210 // collected by the time network service has come up, better to check the
211 // VPD directly and set the MAC Address on the respective Interface.
212
213 bool registeredSignals = false;
214 for (const auto& interfaceString : configJson.items())
215 {
216 if (!std::filesystem::exists(firstBootPath + interfaceString.key()) &&
217 !registeredSignals)
218 {
219
220 log<level::INFO>(
221 "First boot file is not present, check VPD for MAC");
222 phosphor::network::EthInterfaceMatch = std::make_unique<
223 sdbusplus::bus::match::match>(
224 bus,
225 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
226 "member='InterfacesAdded',path='/xyz/openbmc_project/network'",
227 mycallback);
228 registeredSignals = true;
229 }
230 }
231}
232
233#endif
234
Ratan Gupta16f12882017-09-22 18:26:11 +0530235/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530236void refreshObjects()
237{
Ratan Gupta16f12882017-09-22 18:26:11 +0530238 if (manager)
239 {
Ratan Gupta310a0b12017-11-15 17:40:24 +0530240 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +0530241 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +0530242 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +0530243 }
244}
245
Ratan Gupta16f12882017-09-22 18:26:11 +0530246void initializeTimers()
247{
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700248 auto event = sdeventplus::Event::get_default();
249 refreshObjectTimer =
250 std::make_unique<Timer>(event, std::bind(refreshObjects));
Ratan Gupta16f12882017-09-22 18:26:11 +0530251}
252
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700253} // namespace network
254} // namespace phosphor
255
Ratan Guptaf6657382017-11-10 17:58:17 +0530256void createNetLinkSocket(phosphor::Descriptor& smartSock)
257{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500258 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530259 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
260 if (fd < 0)
261 {
Ratan Guptaf6657382017-11-10 17:58:17 +0530262 log<level::ERR>("Unable to create the net link socket",
William A. Kennington III798c2812018-09-27 12:07:39 -0700263 entry("ERRNO=%d", errno));
Ratan Guptaf6657382017-11-10 17:58:17 +0530264 elog<InternalFailure>();
265 }
266 smartSock.set(fd);
267}
268
Manojkiran Edaaa57fa52020-06-13 14:59:53 +0530269int main(int /*argc*/, char** /*argv*/)
Ratan Gupta8c834932017-04-14 16:30:24 +0530270{
William A. Kennington III3a70fa22018-09-20 18:48:20 -0700271 phosphor::network::initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530272
Ratan Guptacb7098d2017-04-14 17:46:05 +0530273 auto bus = sdbusplus::bus::new_default();
274
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530275 // Need sd_event to watch for OCC device errors
276 sd_event* event = nullptr;
277 auto r = sd_event_default(&event);
278 if (r < 0)
279 {
280 log<level::ERR>("Error creating a default sd_event handler");
281 return r;
282 }
283
284 phosphor::network::EventPtr eventPtr{event};
285 event = nullptr;
286
287 // Attach the bus to sd_event to service user requests
288 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
289
Ratan Guptacb7098d2017-04-14 17:46:05 +0530290 // Add sdbusplus Object Manager for the 'root' path of the network manager.
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800291 sdbusplus::server::manager::manager objManager(bus, DEFAULT_OBJPATH);
292 bus.request_name(DEFAULT_BUSNAME);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530293
Gunnar Mills57d9c502018-09-14 14:42:34 -0500294 phosphor::network::manager = std::make_unique<phosphor::network::Manager>(
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800295 bus, DEFAULT_OBJPATH, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530296
Ratan Guptab610caf2017-09-19 09:33:51 +0530297 // create the default network files if the network file
298 // is not there for any interface.
299 // Parameter false means don't create the network
300 // files forcefully.
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700301 if (phosphor::network::manager->createDefaultNetworkFiles(false))
Ratan Guptae9629412017-12-21 08:20:25 +0530302 {
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700303 phosphor::network::manager->reloadConfigs();
Ratan Guptae9629412017-12-21 08:20:25 +0530304 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530305
Gunnar Mills57d9c502018-09-14 14:42:34 -0500306 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530307 phosphor::Descriptor smartSock;
308 createNetLinkSocket(smartSock);
309
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530310 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530311 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530312
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700313#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530314 std::ifstream in(configFile);
315 nlohmann::json configJson;
316 in >> configJson;
317 phosphor::network::watchEthernetInterface(bus, configJson);
318#endif
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700319
320 // Trigger the initial object scan
William A. Kennington III26c40a42021-11-03 18:27:52 -0700321 // This is intentionally deferred, to ensure that systemd-networkd is
322 // fully configured.
323 phosphor::network::refreshObjectTimer->restartOnce(
324 phosphor::network::refreshTimeout);
William A. Kennington IIIbd649af2021-10-08 17:55:13 -0700325
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530326 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530327}