blob: 8d38c37428273ee1f768c30b47dbe432c6e03b75 [file] [log] [blame]
William A. Kennington III0b111d42022-10-04 18:06:11 -07001#include "config.h"
2
3#include "inventory_mac.hpp"
4
5#include "network_manager.hpp"
6#include "types.hpp"
7
William A. Kennington III0b111d42022-10-04 18:06:11 -07008#include <nlohmann/json.hpp>
9#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070010#include <phosphor-logging/lg2.hpp>
William A. Kennington III0b111d42022-10-04 18:06:11 -070011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/bus/match.hpp>
William A. Kennington III60903ee2023-06-02 15:17:49 -070013#include <stdplus/str/maps.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050014#include <xyz/openbmc_project/Common/error.hpp>
15
16#include <filesystem>
17#include <fstream>
18#include <memory>
William A. Kennington III0b111d42022-10-04 18:06:11 -070019#include <string>
20#include <vector>
William A. Kennington III0b111d42022-10-04 18:06:11 -070021
22namespace phosphor::network::inventory
23{
24
25using phosphor::logging::elog;
William A. Kennington III0b111d42022-10-04 18:06:11 -070026using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
27
28using DbusObjectPath = std::string;
29using DbusInterface = std::string;
30using PropertyValue = std::string;
31using DbusService = std::string;
William A. Kennington III60903ee2023-06-02 15:17:49 -070032using ObjectTree =
33 stdplus::string_umap<stdplus::string_umap<std::vector<std::string>>>;
William A. Kennington III0b111d42022-10-04 18:06:11 -070034
35constexpr auto firstBootPath = "/var/lib/network/firstBoot_";
36constexpr auto configFile = "/usr/share/network/config.json";
37
38constexpr auto invNetworkIntf =
39 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
40constexpr auto invRoot = "/xyz/openbmc_project/inventory";
41constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
42constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
43constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
44constexpr auto propIntf = "org.freedesktop.DBus.Properties";
45constexpr auto methodGet = "Get";
46
47Manager* manager = nullptr;
48std::unique_ptr<sdbusplus::bus::match_t> EthInterfaceMatch = nullptr;
Patrick Williamsde333e12023-02-10 15:50:04 -060049std::unique_ptr<sdbusplus::bus::match_t> MacAddressMatch = nullptr;
William A. Kennington III0b111d42022-10-04 18:06:11 -070050std::vector<std::string> first_boot_status;
Patrick Williamsde333e12023-02-10 15:50:04 -060051nlohmann::json configJson;
William A. Kennington III0b111d42022-10-04 18:06:11 -070052
53void setFirstBootMACOnInterface(const std::string& intf, const std::string& mac)
54{
55 for (const auto& interface : manager->interfaces)
56 {
57 if (interface.first == intf)
58 {
59 auto returnMAC = interface.second->macAddress(mac);
60 if (returnMAC == mac)
61 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -070062 lg2::info("Setting MAC {NET_MAC} on interface {NET_INTF}",
63 "NET_MAC", mac, "NET_INTF", intf);
William A. Kennington III0b111d42022-10-04 18:06:11 -070064 std::error_code ec;
65 if (std::filesystem::is_directory("/var/lib/network", ec))
66 {
67 std::ofstream persistentFile(firstBootPath + intf);
68 }
69 break;
70 }
71 else
72 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070073 lg2::info("MAC is Not Set on ethernet Interface");
William A. Kennington III0b111d42022-10-04 18:06:11 -070074 }
75 }
76 }
77}
78
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -070079stdplus::EtherAddr getfromInventory(sdbusplus::bus_t& bus,
80 const std::string& intfName)
William A. Kennington III0b111d42022-10-04 18:06:11 -070081{
Patrick Williamsde333e12023-02-10 15:50:04 -060082 std::string interfaceName = configJson[intfName];
William A. Kennington III0b111d42022-10-04 18:06:11 -070083
84 std::vector<DbusInterface> interfaces;
85 interfaces.emplace_back(invNetworkIntf);
86
87 auto depth = 0;
88
Patrick Williamsad205022024-08-16 15:20:07 -040089 auto mapperCall =
90 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
William A. Kennington III0b111d42022-10-04 18:06:11 -070091
92 mapperCall.append(invRoot, depth, interfaces);
93
94 auto mapperReply = bus.call(mapperCall);
95 if (mapperReply.is_method_error())
96 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070097 lg2::error("Error in mapper call");
William A. Kennington III0b111d42022-10-04 18:06:11 -070098 elog<InternalFailure>();
99 }
100
101 ObjectTree objectTree;
102 mapperReply.read(objectTree);
103
104 if (objectTree.empty())
105 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700106 lg2::error("No Object has implemented the interface {NET_INTF}",
107 "NET_INTF", invNetworkIntf);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700108 elog<InternalFailure>();
109 }
110
111 DbusObjectPath objPath;
112 DbusService service;
113
114 if (1 == objectTree.size())
115 {
116 objPath = objectTree.begin()->first;
117 service = objectTree.begin()->second.begin()->first;
118 }
119 else
120 {
121 // If there are more than 2 objects, object path must contain the
122 // interface name
Patrick Williams89d734b2023-05-10 07:50:25 -0500123 for (const auto& object : objectTree)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700124 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700125 lg2::info("Get info on interface {NET_INTF}, object {OBJ}",
126 "NET_INTF", interfaceName, "OBJ", object.first);
Anderson Kuoef080362025-06-05 19:12:37 +0800127 if (object.first.ends_with("/" + interfaceName))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700128 {
129 objPath = object.first;
130 service = object.second.begin()->first;
131 break;
132 }
133 }
134
135 if (objPath.empty())
136 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700137 lg2::error("Can't find the object for the interface {NET_INTF}",
138 "NET_INTF", interfaceName);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700139 elog<InternalFailure>();
140 }
141 }
142
143 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
144 propIntf, methodGet);
145
146 method.append(invNetworkIntf, "MACAddress");
147
148 auto reply = bus.call(method);
149 if (reply.is_method_error())
150 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700151 lg2::error(
152 "Failed to get MACAddress for path {DBUS_PATH} interface {DBUS_INTF}",
153 "DBUS_PATH", objPath, "DBUS_INTF", invNetworkIntf);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700154 elog<InternalFailure>();
155 }
156
157 std::variant<std::string> value;
158 reply.read(value);
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700159 return stdplus::fromStr<stdplus::EtherAddr>(std::get<std::string>(value));
William A. Kennington III0b111d42022-10-04 18:06:11 -0700160}
161
Patrick Williamsde333e12023-02-10 15:50:04 -0600162bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, const std::string& intfname)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700163{
164 try
165 {
166 auto inventoryMAC = getfromInventory(bus, intfname);
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700167 if (inventoryMAC != stdplus::EtherAddr{})
William A. Kennington III0b111d42022-10-04 18:06:11 -0700168 {
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700169 auto macStr = stdplus::toStr(inventoryMAC);
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700170 lg2::info(
171 "Mac Address {NET_MAC} in Inventory on Interface {NET_INTF}",
172 "NET_MAC", macStr, "NET_INTF", intfname);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700173 setFirstBootMACOnInterface(intfname, macStr);
174 first_boot_status.push_back(intfname);
175 bool status = true;
176 for (const auto& keys : configJson.items())
177 {
178 if (!(std::find(first_boot_status.begin(),
Patrick Williamsad205022024-08-16 15:20:07 -0400179 first_boot_status.end(), keys.key()) !=
180 first_boot_status.end()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700181 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700182 lg2::info("Interface {NET_INTF} MAC is NOT set from VPD",
183 "NET_INTF", keys.key());
William A. Kennington III0b111d42022-10-04 18:06:11 -0700184 status = false;
185 }
186 }
187 if (status)
188 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700189 lg2::info("Removing the match for ethernet interfaces");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700190 EthInterfaceMatch = nullptr;
191 }
192 }
193 else
194 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700195 lg2::info("Nothing is present in Inventory");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700196 return false;
197 }
198 }
199 catch (const std::exception& e)
200 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700201 lg2::error("Exception occurred during getting of MAC "
202 "address from Inventory");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700203 return false;
204 }
205 return true;
206}
207
Manojkiran Edad92826d2024-06-17 14:43:14 +0530208// register the matches to be monitored from inventory manager
Patrick Williamsde333e12023-02-10 15:50:04 -0600209void registerSignals(sdbusplus::bus_t& bus)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700210{
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700211 lg2::info("Registering the Inventory Signals Matcher");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700212
William A. Kennington III0b111d42022-10-04 18:06:11 -0700213 auto callback = [&](sdbusplus::message_t& m) {
214 std::map<DbusObjectPath,
215 std::map<DbusInterface, std::variant<PropertyValue>>>
216 interfacesProperties;
217
218 sdbusplus::message::object_path objPath;
219 m.read(objPath, interfacesProperties);
220
221 for (const auto& pattern : configJson.items())
222 {
Anderson Kuoef080362025-06-05 19:12:37 +0800223 if (objPath.str.ends_with("/" + pattern.value().get<std::string>()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700224 {
225 for (auto& interface : interfacesProperties)
226 {
227 if (interface.first == invNetworkIntf)
228 {
229 for (const auto& property : interface.second)
230 {
231 if (property.first == "MACAddress")
232 {
Chanh Nguyen947454b2024-09-11 09:37:23 +0000233 // Only set mac address on interface once the
234 // firstboot file does not exist or it is being
235 // FORCE_SYNC_MAC_FROM_INVENTORY
236 if (FORCE_SYNC_MAC_FROM_INVENTORY ||
237 !std::filesystem::exists(
238 firstBootPath + pattern.key()))
239 {
240 setFirstBootMACOnInterface(
241 pattern.key(),
242 std::get<std::string>(property.second));
243 }
William A. Kennington III0b111d42022-10-04 18:06:11 -0700244 break;
245 }
246 }
247 break;
248 }
249 }
250 }
251 }
252 };
253
254 MacAddressMatch = std::make_unique<sdbusplus::bus::match_t>(
255 bus,
256 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
257 "member='InterfacesAdded',path='/xyz/openbmc_project/"
258 "inventory'",
259 callback);
260}
261
Patrick Williamsde333e12023-02-10 15:50:04 -0600262void watchEthernetInterface(sdbusplus::bus_t& bus)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700263{
Patrick Williamsde333e12023-02-10 15:50:04 -0600264 auto handle_interface = [&](auto infname) {
265 if (configJson.find(infname) == configJson.end())
266 {
267 // ethernet interface not found in configJSON
268 // check if it is not sit0 interface, as it is
269 // expected.
270 if (infname != "sit0")
271 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700272 lg2::error("Wrong Interface Name in Config Json");
Patrick Williamsde333e12023-02-10 15:50:04 -0600273 }
274 }
275 else
276 {
277 registerSignals(bus);
Patrick Williamsde333e12023-02-10 15:50:04 -0600278
279 if (setInventoryMACOnSystem(bus, infname))
280 {
281 MacAddressMatch = nullptr;
282 }
283 }
284 };
285
286 auto mycallback = [&, handle_interface](sdbusplus::message_t& m) {
William A. Kennington III0b111d42022-10-04 18:06:11 -0700287 std::map<DbusObjectPath,
288 std::map<DbusInterface, std::variant<PropertyValue>>>
289 interfacesProperties;
290
291 sdbusplus::message::object_path objPath;
292 std::pair<std::string, std::string> ethPair;
293 m.read(objPath, interfacesProperties);
Patrick Williamsde333e12023-02-10 15:50:04 -0600294
William A. Kennington III0b111d42022-10-04 18:06:11 -0700295 for (const auto& interfaces : interfacesProperties)
296 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700297 lg2::info("Check {DBUS_INTF} for sdbus response", "DBUS_INTF",
298 interfaces.first);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700299 if (interfaces.first ==
300 "xyz.openbmc_project.Network.EthernetInterface")
301 {
302 for (const auto& property : interfaces.second)
303 {
304 if (property.first == "InterfaceName")
305 {
Patrick Williamsde333e12023-02-10 15:50:04 -0600306 handle_interface(
307 std::get<std::string>(property.second));
William A. Kennington III0b111d42022-10-04 18:06:11 -0700308
William A. Kennington III0b111d42022-10-04 18:06:11 -0700309 break;
310 }
311 }
312 break;
313 }
314 }
315 };
Manojkiran Edad92826d2024-06-17 14:43:14 +0530316
317 // The VPD may already have been assigned because phosphor-inventory-manager
318 // started ahead of the network service. Read the VPD directly and assign
319 // the MAC address despite this possibility.
William A. Kennington III0b111d42022-10-04 18:06:11 -0700320
William A. Kennington III0b111d42022-10-04 18:06:11 -0700321 for (const auto& interfaceString : configJson.items())
322 {
Asmitha Karunanithi2eec06d2023-09-13 10:50:50 -0500323 if (FORCE_SYNC_MAC_FROM_INVENTORY ||
324 !std::filesystem::exists(firstBootPath + interfaceString.key()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700325 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700326 lg2::info("Check VPD for MAC: {REASON}", "REASON",
327 (FORCE_SYNC_MAC_FROM_INVENTORY)
328 ? "Force sync enabled"
329 : "First boot file is not present");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700330 EthInterfaceMatch = std::make_unique<sdbusplus::bus::match_t>(
331 bus,
332 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
333 "member='InterfacesAdded',path='/xyz/openbmc_project/network'",
334 mycallback);
Patrick Williamsde333e12023-02-10 15:50:04 -0600335
336 for (const auto& intf : manager->interfaces)
337 {
338 if (intf.first == interfaceString.key())
339 {
340 handle_interface(intf.first);
341 }
342 }
William A. Kennington III0b111d42022-10-04 18:06:11 -0700343 }
344 }
345}
346
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800347std::unique_ptr<Runtime> watch(stdplus::PinnedRef<sdbusplus::bus_t> bus,
348 stdplus::PinnedRef<Manager> m)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700349{
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800350 manager = &m.get();
William A. Kennington III0b111d42022-10-04 18:06:11 -0700351 std::ifstream in(configFile);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700352 in >> configJson;
Patrick Williamsde333e12023-02-10 15:50:04 -0600353 watchEthernetInterface(bus);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700354 return nullptr;
355}
356
357} // namespace phosphor::network::inventory