William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "inventory_mac.hpp" |
| 4 | |
| 5 | #include "network_manager.hpp" |
| 6 | #include "types.hpp" |
| 7 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 8 | #include <nlohmann/json.hpp> |
| 9 | #include <phosphor-logging/elog-errors.hpp> |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
| 12 | #include <sdbusplus/bus/match.hpp> |
William A. Kennington III | 60903ee | 2023-06-02 15:17:49 -0700 | [diff] [blame] | 13 | #include <stdplus/str/maps.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Common/error.hpp> |
| 15 | |
| 16 | #include <filesystem> |
| 17 | #include <fstream> |
| 18 | #include <memory> |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 21 | |
| 22 | namespace phosphor::network::inventory |
| 23 | { |
| 24 | |
| 25 | using phosphor::logging::elog; |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 26 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 27 | |
| 28 | using DbusObjectPath = std::string; |
| 29 | using DbusInterface = std::string; |
| 30 | using PropertyValue = std::string; |
| 31 | using DbusService = std::string; |
William A. Kennington III | 60903ee | 2023-06-02 15:17:49 -0700 | [diff] [blame] | 32 | using ObjectTree = |
| 33 | stdplus::string_umap<stdplus::string_umap<std::vector<std::string>>>; |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 34 | |
| 35 | constexpr auto firstBootPath = "/var/lib/network/firstBoot_"; |
| 36 | constexpr auto configFile = "/usr/share/network/config.json"; |
| 37 | |
| 38 | constexpr auto invNetworkIntf = |
| 39 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; |
| 40 | constexpr auto invRoot = "/xyz/openbmc_project/inventory"; |
| 41 | constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; |
| 42 | constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; |
| 43 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 44 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 45 | constexpr auto methodGet = "Get"; |
| 46 | |
| 47 | Manager* manager = nullptr; |
| 48 | std::unique_ptr<sdbusplus::bus::match_t> EthInterfaceMatch = nullptr; |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 49 | std::unique_ptr<sdbusplus::bus::match_t> MacAddressMatch = nullptr; |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 50 | std::vector<std::string> first_boot_status; |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 51 | nlohmann::json configJson; |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 52 | |
| 53 | void 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 III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 62 | lg2::info("Setting MAC {NET_MAC} on interface {NET_INTF}", |
| 63 | "NET_MAC", mac, "NET_INTF", intf); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 64 | 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 Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 73 | lg2::info("MAC is Not Set on ethernet Interface"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
William A. Kennington III | b7d6a1a | 2023-06-17 02:00:53 -0700 | [diff] [blame] | 79 | stdplus::EtherAddr getfromInventory(sdbusplus::bus_t& bus, |
| 80 | const std::string& intfName) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 81 | { |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 82 | std::string interfaceName = configJson[intfName]; |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 83 | |
| 84 | std::vector<DbusInterface> interfaces; |
| 85 | interfaces.emplace_back(invNetworkIntf); |
| 86 | |
| 87 | auto depth = 0; |
| 88 | |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame^] | 89 | auto mapperCall = |
| 90 | bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 91 | |
| 92 | mapperCall.append(invRoot, depth, interfaces); |
| 93 | |
| 94 | auto mapperReply = bus.call(mapperCall); |
| 95 | if (mapperReply.is_method_error()) |
| 96 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 97 | lg2::error("Error in mapper call"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 98 | elog<InternalFailure>(); |
| 99 | } |
| 100 | |
| 101 | ObjectTree objectTree; |
| 102 | mapperReply.read(objectTree); |
| 103 | |
| 104 | if (objectTree.empty()) |
| 105 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 106 | lg2::error("No Object has implemented the interface {NET_INTF}", |
| 107 | "NET_INTF", invNetworkIntf); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 108 | 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 Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 123 | for (const auto& object : objectTree) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 124 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 125 | lg2::info("Get info on interface {NET_INTF}, object {OBJ}", |
| 126 | "NET_INTF", interfaceName, "OBJ", object.first); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 127 | |
| 128 | if (std::string::npos != object.first.find(interfaceName.c_str())) |
| 129 | { |
| 130 | objPath = object.first; |
| 131 | service = object.second.begin()->first; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (objPath.empty()) |
| 137 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 138 | lg2::error("Can't find the object for the interface {NET_INTF}", |
| 139 | "NET_INTF", interfaceName); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 140 | elog<InternalFailure>(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 145 | propIntf, methodGet); |
| 146 | |
| 147 | method.append(invNetworkIntf, "MACAddress"); |
| 148 | |
| 149 | auto reply = bus.call(method); |
| 150 | if (reply.is_method_error()) |
| 151 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 152 | lg2::error( |
| 153 | "Failed to get MACAddress for path {DBUS_PATH} interface {DBUS_INTF}", |
| 154 | "DBUS_PATH", objPath, "DBUS_INTF", invNetworkIntf); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 155 | elog<InternalFailure>(); |
| 156 | } |
| 157 | |
| 158 | std::variant<std::string> value; |
| 159 | reply.read(value); |
William A. Kennington III | b7d6a1a | 2023-06-17 02:00:53 -0700 | [diff] [blame] | 160 | return stdplus::fromStr<stdplus::EtherAddr>(std::get<std::string>(value)); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 163 | bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, const std::string& intfname) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 164 | { |
| 165 | try |
| 166 | { |
| 167 | auto inventoryMAC = getfromInventory(bus, intfname); |
William A. Kennington III | b7d6a1a | 2023-06-17 02:00:53 -0700 | [diff] [blame] | 168 | if (inventoryMAC != stdplus::EtherAddr{}) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 169 | { |
William A. Kennington III | b7d6a1a | 2023-06-17 02:00:53 -0700 | [diff] [blame] | 170 | auto macStr = stdplus::toStr(inventoryMAC); |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 171 | lg2::info( |
| 172 | "Mac Address {NET_MAC} in Inventory on Interface {NET_INTF}", |
| 173 | "NET_MAC", macStr, "NET_INTF", intfname); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 174 | setFirstBootMACOnInterface(intfname, macStr); |
| 175 | first_boot_status.push_back(intfname); |
| 176 | bool status = true; |
| 177 | for (const auto& keys : configJson.items()) |
| 178 | { |
| 179 | if (!(std::find(first_boot_status.begin(), |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame^] | 180 | first_boot_status.end(), keys.key()) != |
| 181 | first_boot_status.end())) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 182 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 183 | lg2::info("Interface {NET_INTF} MAC is NOT set from VPD", |
| 184 | "NET_INTF", keys.key()); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 185 | status = false; |
| 186 | } |
| 187 | } |
| 188 | if (status) |
| 189 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 190 | lg2::info("Removing the match for ethernet interfaces"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 191 | EthInterfaceMatch = nullptr; |
| 192 | } |
| 193 | } |
| 194 | else |
| 195 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 196 | lg2::info("Nothing is present in Inventory"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | catch (const std::exception& e) |
| 201 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 202 | lg2::error("Exception occurred during getting of MAC " |
| 203 | "address from Inventory"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
Manojkiran Eda | d92826d | 2024-06-17 14:43:14 +0530 | [diff] [blame] | 209 | // register the matches to be monitored from inventory manager |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 210 | void registerSignals(sdbusplus::bus_t& bus) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 211 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 212 | lg2::info("Registering the Inventory Signals Matcher"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 213 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 214 | auto callback = [&](sdbusplus::message_t& m) { |
| 215 | std::map<DbusObjectPath, |
| 216 | std::map<DbusInterface, std::variant<PropertyValue>>> |
| 217 | interfacesProperties; |
| 218 | |
| 219 | sdbusplus::message::object_path objPath; |
| 220 | m.read(objPath, interfacesProperties); |
| 221 | |
| 222 | for (const auto& pattern : configJson.items()) |
| 223 | { |
| 224 | if (objPath.str.find(pattern.value()) != std::string::npos) |
| 225 | { |
| 226 | for (auto& interface : interfacesProperties) |
| 227 | { |
| 228 | if (interface.first == invNetworkIntf) |
| 229 | { |
| 230 | for (const auto& property : interface.second) |
| 231 | { |
| 232 | if (property.first == "MACAddress") |
| 233 | { |
| 234 | setFirstBootMACOnInterface( |
| 235 | pattern.key(), |
| 236 | std::get<std::string>(property.second)); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | MacAddressMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 248 | bus, |
| 249 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 250 | "member='InterfacesAdded',path='/xyz/openbmc_project/" |
| 251 | "inventory'", |
| 252 | callback); |
| 253 | } |
| 254 | |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 255 | void watchEthernetInterface(sdbusplus::bus_t& bus) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 256 | { |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 257 | auto handle_interface = [&](auto infname) { |
| 258 | if (configJson.find(infname) == configJson.end()) |
| 259 | { |
| 260 | // ethernet interface not found in configJSON |
| 261 | // check if it is not sit0 interface, as it is |
| 262 | // expected. |
| 263 | if (infname != "sit0") |
| 264 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 265 | lg2::error("Wrong Interface Name in Config Json"); |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | registerSignals(bus); |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 271 | |
| 272 | if (setInventoryMACOnSystem(bus, infname)) |
| 273 | { |
| 274 | MacAddressMatch = nullptr; |
| 275 | } |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | auto mycallback = [&, handle_interface](sdbusplus::message_t& m) { |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 280 | std::map<DbusObjectPath, |
| 281 | std::map<DbusInterface, std::variant<PropertyValue>>> |
| 282 | interfacesProperties; |
| 283 | |
| 284 | sdbusplus::message::object_path objPath; |
| 285 | std::pair<std::string, std::string> ethPair; |
| 286 | m.read(objPath, interfacesProperties); |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 287 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 288 | for (const auto& interfaces : interfacesProperties) |
| 289 | { |
William A. Kennington III | 1d25ca4 | 2023-05-30 16:55:28 -0700 | [diff] [blame] | 290 | lg2::info("Check {DBUS_INTF} for sdbus response", "DBUS_INTF", |
| 291 | interfaces.first); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 292 | if (interfaces.first == |
| 293 | "xyz.openbmc_project.Network.EthernetInterface") |
| 294 | { |
| 295 | for (const auto& property : interfaces.second) |
| 296 | { |
| 297 | if (property.first == "InterfaceName") |
| 298 | { |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 299 | handle_interface( |
| 300 | std::get<std::string>(property.second)); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 301 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | }; |
Manojkiran Eda | d92826d | 2024-06-17 14:43:14 +0530 | [diff] [blame] | 309 | |
| 310 | // The VPD may already have been assigned because phosphor-inventory-manager |
| 311 | // started ahead of the network service. Read the VPD directly and assign |
| 312 | // the MAC address despite this possibility. |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 313 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 314 | for (const auto& interfaceString : configJson.items()) |
| 315 | { |
Asmitha Karunanithi | 2eec06d | 2023-09-13 10:50:50 -0500 | [diff] [blame] | 316 | if (FORCE_SYNC_MAC_FROM_INVENTORY || |
| 317 | !std::filesystem::exists(firstBootPath + interfaceString.key())) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 318 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 319 | lg2::info("Check VPD for MAC: {REASON}", "REASON", |
| 320 | (FORCE_SYNC_MAC_FROM_INVENTORY) |
| 321 | ? "Force sync enabled" |
| 322 | : "First boot file is not present"); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 323 | EthInterfaceMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 324 | bus, |
| 325 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 326 | "member='InterfacesAdded',path='/xyz/openbmc_project/network'", |
| 327 | mycallback); |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 328 | |
| 329 | for (const auto& intf : manager->interfaces) |
| 330 | { |
| 331 | if (intf.first == interfaceString.key()) |
| 332 | { |
| 333 | handle_interface(intf.first); |
| 334 | } |
| 335 | } |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 340 | std::unique_ptr<Runtime> watch(stdplus::PinnedRef<sdbusplus::bus_t> bus, |
| 341 | stdplus::PinnedRef<Manager> m) |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 342 | { |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 343 | manager = &m.get(); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 344 | std::ifstream in(configFile); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 345 | in >> configJson; |
Patrick Williams | de333e1 | 2023-02-10 15:50:04 -0600 | [diff] [blame] | 346 | watchEthernetInterface(bus); |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 347 | return nullptr; |
| 348 | } |
| 349 | |
| 350 | } // namespace phosphor::network::inventory |