blob: fb15e67bde9b6cd3f0ce61cadc4fc905d11bb7ae [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
Patrick Williams3c4f35e2025-11-04 22:51:04 -050094 auto mapperReply = [&]() {
95 try
96 {
97 return bus.call(mapperCall);
98 }
99 catch (const sdbusplus::exception::SdBusError& e)
100 {
101 lg2::error("Error in mapper call");
102 elog<InternalFailure>();
103 }
104 }();
William A. Kennington III0b111d42022-10-04 18:06:11 -0700105
Patrick Williams137d9442025-11-05 00:17:55 -0500106 auto objectTree = mapperReply.unpack<ObjectTree>();
William A. Kennington III0b111d42022-10-04 18:06:11 -0700107
108 if (objectTree.empty())
109 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700110 lg2::error("No Object has implemented the interface {NET_INTF}",
111 "NET_INTF", invNetworkIntf);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700112 elog<InternalFailure>();
113 }
114
115 DbusObjectPath objPath;
116 DbusService service;
117
118 if (1 == objectTree.size())
119 {
120 objPath = objectTree.begin()->first;
121 service = objectTree.begin()->second.begin()->first;
122 }
123 else
124 {
125 // If there are more than 2 objects, object path must contain the
126 // interface name
Patrick Williams89d734b2023-05-10 07:50:25 -0500127 for (const auto& object : objectTree)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700128 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700129 lg2::info("Get info on interface {NET_INTF}, object {OBJ}",
130 "NET_INTF", interfaceName, "OBJ", object.first);
Anderson Kuoef080362025-06-05 19:12:37 +0800131 if (object.first.ends_with("/" + interfaceName))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700132 {
133 objPath = object.first;
134 service = object.second.begin()->first;
135 break;
136 }
137 }
138
139 if (objPath.empty())
140 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700141 lg2::error("Can't find the object for the interface {NET_INTF}",
142 "NET_INTF", interfaceName);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700143 elog<InternalFailure>();
144 }
145 }
146
147 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
148 propIntf, methodGet);
149
150 method.append(invNetworkIntf, "MACAddress");
151
Patrick Williams3c4f35e2025-11-04 22:51:04 -0500152 auto reply = [&]() {
153 try
154 {
155 return bus.call(method);
156 }
157 catch (const sdbusplus::exception::SdBusError& e)
158 {
159 lg2::error(
160 "Failed to get MACAddress for path {DBUS_PATH} interface {DBUS_INTF}",
161 "DBUS_PATH", objPath, "DBUS_INTF", invNetworkIntf);
162 elog<InternalFailure>();
163 }
164 }();
William A. Kennington III0b111d42022-10-04 18:06:11 -0700165
Patrick Williams137d9442025-11-05 00:17:55 -0500166 auto value = reply.unpack<std::variant<std::string>>();
167
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700168 return stdplus::fromStr<stdplus::EtherAddr>(std::get<std::string>(value));
William A. Kennington III0b111d42022-10-04 18:06:11 -0700169}
170
Patrick Williamsde333e12023-02-10 15:50:04 -0600171bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, const std::string& intfname)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700172{
173 try
174 {
175 auto inventoryMAC = getfromInventory(bus, intfname);
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700176 if (inventoryMAC != stdplus::EtherAddr{})
William A. Kennington III0b111d42022-10-04 18:06:11 -0700177 {
William A. Kennington IIIb7d6a1a2023-06-17 02:00:53 -0700178 auto macStr = stdplus::toStr(inventoryMAC);
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700179 lg2::info(
180 "Mac Address {NET_MAC} in Inventory on Interface {NET_INTF}",
181 "NET_MAC", macStr, "NET_INTF", intfname);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700182 setFirstBootMACOnInterface(intfname, macStr);
183 first_boot_status.push_back(intfname);
184 bool status = true;
185 for (const auto& keys : configJson.items())
186 {
187 if (!(std::find(first_boot_status.begin(),
Patrick Williamsad205022024-08-16 15:20:07 -0400188 first_boot_status.end(), keys.key()) !=
189 first_boot_status.end()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700190 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700191 lg2::info("Interface {NET_INTF} MAC is NOT set from VPD",
192 "NET_INTF", keys.key());
William A. Kennington III0b111d42022-10-04 18:06:11 -0700193 status = false;
194 }
195 }
196 if (status)
197 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700198 lg2::info("Removing the match for ethernet interfaces");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700199 EthInterfaceMatch = nullptr;
200 }
201 }
202 else
203 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700204 lg2::info("Nothing is present in Inventory");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700205 return false;
206 }
207 }
208 catch (const std::exception& e)
209 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700210 lg2::error("Exception occurred during getting of MAC "
211 "address from Inventory");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700212 return false;
213 }
214 return true;
215}
216
Manojkiran Edad92826d2024-06-17 14:43:14 +0530217// register the matches to be monitored from inventory manager
Patrick Williamsde333e12023-02-10 15:50:04 -0600218void registerSignals(sdbusplus::bus_t& bus)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700219{
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700220 lg2::info("Registering the Inventory Signals Matcher");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700221
William A. Kennington III0b111d42022-10-04 18:06:11 -0700222 auto callback = [&](sdbusplus::message_t& m) {
223 std::map<DbusObjectPath,
224 std::map<DbusInterface, std::variant<PropertyValue>>>
225 interfacesProperties;
226
227 sdbusplus::message::object_path objPath;
228 m.read(objPath, interfacesProperties);
229
230 for (const auto& pattern : configJson.items())
231 {
Anderson Kuoef080362025-06-05 19:12:37 +0800232 if (objPath.str.ends_with("/" + pattern.value().get<std::string>()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700233 {
234 for (auto& interface : interfacesProperties)
235 {
236 if (interface.first == invNetworkIntf)
237 {
238 for (const auto& property : interface.second)
239 {
240 if (property.first == "MACAddress")
241 {
Chanh Nguyen947454b2024-09-11 09:37:23 +0000242 // Only set mac address on interface once the
243 // firstboot file does not exist or it is being
244 // FORCE_SYNC_MAC_FROM_INVENTORY
245 if (FORCE_SYNC_MAC_FROM_INVENTORY ||
246 !std::filesystem::exists(
247 firstBootPath + pattern.key()))
248 {
249 setFirstBootMACOnInterface(
250 pattern.key(),
251 std::get<std::string>(property.second));
252 }
William A. Kennington III0b111d42022-10-04 18:06:11 -0700253 break;
254 }
255 }
256 break;
257 }
258 }
259 }
260 }
261 };
262
263 MacAddressMatch = std::make_unique<sdbusplus::bus::match_t>(
264 bus,
265 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
266 "member='InterfacesAdded',path='/xyz/openbmc_project/"
267 "inventory'",
268 callback);
269}
270
Patrick Williamsde333e12023-02-10 15:50:04 -0600271void watchEthernetInterface(sdbusplus::bus_t& bus)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700272{
Patrick Williamsde333e12023-02-10 15:50:04 -0600273 auto handle_interface = [&](auto infname) {
274 if (configJson.find(infname) == configJson.end())
275 {
276 // ethernet interface not found in configJSON
277 // check if it is not sit0 interface, as it is
278 // expected.
279 if (infname != "sit0")
280 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700281 lg2::error("Wrong Interface Name in Config Json");
Patrick Williamsde333e12023-02-10 15:50:04 -0600282 }
283 }
284 else
285 {
286 registerSignals(bus);
Patrick Williamsde333e12023-02-10 15:50:04 -0600287
288 if (setInventoryMACOnSystem(bus, infname))
289 {
290 MacAddressMatch = nullptr;
291 }
292 }
293 };
294
295 auto mycallback = [&, handle_interface](sdbusplus::message_t& m) {
William A. Kennington III0b111d42022-10-04 18:06:11 -0700296 std::map<DbusObjectPath,
297 std::map<DbusInterface, std::variant<PropertyValue>>>
298 interfacesProperties;
299
300 sdbusplus::message::object_path objPath;
301 std::pair<std::string, std::string> ethPair;
302 m.read(objPath, interfacesProperties);
Patrick Williamsde333e12023-02-10 15:50:04 -0600303
William A. Kennington III0b111d42022-10-04 18:06:11 -0700304 for (const auto& interfaces : interfacesProperties)
305 {
William A. Kennington III1d25ca42023-05-30 16:55:28 -0700306 lg2::info("Check {DBUS_INTF} for sdbus response", "DBUS_INTF",
307 interfaces.first);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700308 if (interfaces.first ==
309 "xyz.openbmc_project.Network.EthernetInterface")
310 {
311 for (const auto& property : interfaces.second)
312 {
313 if (property.first == "InterfaceName")
314 {
Patrick Williamsde333e12023-02-10 15:50:04 -0600315 handle_interface(
316 std::get<std::string>(property.second));
William A. Kennington III0b111d42022-10-04 18:06:11 -0700317
William A. Kennington III0b111d42022-10-04 18:06:11 -0700318 break;
319 }
320 }
321 break;
322 }
323 }
324 };
Manojkiran Edad92826d2024-06-17 14:43:14 +0530325
326 // The VPD may already have been assigned because phosphor-inventory-manager
327 // started ahead of the network service. Read the VPD directly and assign
328 // the MAC address despite this possibility.
William A. Kennington III0b111d42022-10-04 18:06:11 -0700329
William A. Kennington III0b111d42022-10-04 18:06:11 -0700330 for (const auto& interfaceString : configJson.items())
331 {
Asmitha Karunanithi2eec06d2023-09-13 10:50:50 -0500332 if (FORCE_SYNC_MAC_FROM_INVENTORY ||
333 !std::filesystem::exists(firstBootPath + interfaceString.key()))
William A. Kennington III0b111d42022-10-04 18:06:11 -0700334 {
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -0700335 lg2::info("Check VPD for MAC: {REASON}", "REASON",
336 (FORCE_SYNC_MAC_FROM_INVENTORY)
337 ? "Force sync enabled"
338 : "First boot file is not present");
William A. Kennington III0b111d42022-10-04 18:06:11 -0700339 EthInterfaceMatch = std::make_unique<sdbusplus::bus::match_t>(
340 bus,
341 "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
342 "member='InterfacesAdded',path='/xyz/openbmc_project/network'",
343 mycallback);
Patrick Williamsde333e12023-02-10 15:50:04 -0600344
345 for (const auto& intf : manager->interfaces)
346 {
347 if (intf.first == interfaceString.key())
348 {
349 handle_interface(intf.first);
350 }
351 }
William A. Kennington III0b111d42022-10-04 18:06:11 -0700352 }
353 }
354}
355
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800356std::unique_ptr<Runtime> watch(stdplus::PinnedRef<sdbusplus::bus_t> bus,
357 stdplus::PinnedRef<Manager> m)
William A. Kennington III0b111d42022-10-04 18:06:11 -0700358{
William A. Kennington III9ede1b72022-11-21 01:59:28 -0800359 manager = &m.get();
William A. Kennington III0b111d42022-10-04 18:06:11 -0700360 std::ifstream in(configFile);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700361 in >> configJson;
Patrick Williamsde333e12023-02-10 15:50:04 -0600362 watchEthernetInterface(bus);
William A. Kennington III0b111d42022-10-04 18:06:11 -0700363 return nullptr;
364}
365
366} // namespace phosphor::network::inventory