#include "config.h" #include "inventory_mac.hpp" #include "network_manager.hpp" #include "types.hpp" #include #include #include #include #include #include #include #include #include #include #include namespace phosphor::network::inventory { using phosphor::logging::elog; using phosphor::logging::entry; using phosphor::logging::level; using phosphor::logging::log; using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; using DbusObjectPath = std::string; using DbusInterface = std::string; using PropertyValue = std::string; using DbusService = std::string; using ObjectTree = string_umap>>; constexpr auto firstBootPath = "/var/lib/network/firstBoot_"; constexpr auto configFile = "/usr/share/network/config.json"; constexpr auto invNetworkIntf = "xyz.openbmc_project.Inventory.Item.NetworkInterface"; constexpr auto invRoot = "/xyz/openbmc_project/inventory"; constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; constexpr auto propIntf = "org.freedesktop.DBus.Properties"; constexpr auto methodGet = "Get"; Manager* manager = nullptr; std::unique_ptr EthInterfaceMatch = nullptr; std::vector first_boot_status; void setFirstBootMACOnInterface(const std::string& intf, const std::string& mac) { for (const auto& interface : manager->interfaces) { if (interface.first == intf) { auto returnMAC = interface.second->macAddress(mac); if (returnMAC == mac) { log(fmt::format("Setting MAC on {}", intf).c_str(), entry("INTF=%s", intf.c_str()), entry("MAC=%s", mac.c_str())); std::error_code ec; if (std::filesystem::is_directory("/var/lib/network", ec)) { std::ofstream persistentFile(firstBootPath + intf); } break; } else { log("MAC is Not Set on ethernet Interface"); } } } } ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName) { std::string interfaceName = intfName; // load the config JSON from the Read Only Path std::ifstream in(configFile); nlohmann::json configJson; in >> configJson; interfaceName = configJson[intfName]; std::vector interfaces; interfaces.emplace_back(invNetworkIntf); auto depth = 0; auto mapperCall = bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); mapperCall.append(invRoot, depth, interfaces); auto mapperReply = bus.call(mapperCall); if (mapperReply.is_method_error()) { log("Error in mapper call"); elog(); } ObjectTree objectTree; mapperReply.read(objectTree); if (objectTree.empty()) { log("No Object has implemented the interface", entry("INTERFACE=%s", invNetworkIntf)); elog(); } DbusObjectPath objPath; DbusService service; if (1 == objectTree.size()) { objPath = objectTree.begin()->first; service = objectTree.begin()->second.begin()->first; } else { // If there are more than 2 objects, object path must contain the // interface name for (auto const& object : objectTree) { log("interface", entry("INT=%s", interfaceName.c_str())); log("object", entry("OBJ=%s", object.first.c_str())); if (std::string::npos != object.first.find(interfaceName.c_str())) { objPath = object.first; service = object.second.begin()->first; break; } } if (objPath.empty()) { log("Can't find the object for the interface", entry("intfName=%s", interfaceName.c_str())); elog(); } } auto method = bus.new_method_call(service.c_str(), objPath.c_str(), propIntf, methodGet); method.append(invNetworkIntf, "MACAddress"); auto reply = bus.call(method); if (reply.is_method_error()) { log("Failed to get MACAddress", entry("PATH=%s", objPath.c_str()), entry("INTERFACE=%s", invNetworkIntf)); elog(); } std::variant value; reply.read(value); return ToAddr{}(std::get(value)); } bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, const nlohmann::json& configJson, const std::string& intfname) { try { auto inventoryMAC = getfromInventory(bus, intfname); if (inventoryMAC != ether_addr{}) { auto macStr = std::to_string(inventoryMAC); log("Mac Address in Inventory on ", entry("Interface : ", intfname.c_str()), entry("MAC Address :", macStr.c_str())); setFirstBootMACOnInterface(intfname, macStr); first_boot_status.push_back(intfname); bool status = true; for (const auto& keys : configJson.items()) { if (!(std::find(first_boot_status.begin(), first_boot_status.end(), keys.key()) != first_boot_status.end())) { log("Interface MAC is NOT set from VPD"), entry("INTERFACE", keys.key().c_str()); status = false; } } if (status) { log("Removing the match for ethernet interfaces"); EthInterfaceMatch = nullptr; } } else { log("Nothing is present in Inventory"); return false; } } catch (const std::exception& e) { log("Exception occurred during getting of MAC " "address from Inventory"); return false; } return true; } // register the macthes to be monitored from inventory manager void registerSignals(sdbusplus::bus_t& bus, const nlohmann::json& configJson) { log("Registering the Inventory Signals Matcher"); static std::unique_ptr MacAddressMatch; auto callback = [&](sdbusplus::message_t& m) { std::map>> interfacesProperties; sdbusplus::message::object_path objPath; m.read(objPath, interfacesProperties); for (const auto& pattern : configJson.items()) { if (objPath.str.find(pattern.value()) != std::string::npos) { for (auto& interface : interfacesProperties) { if (interface.first == invNetworkIntf) { for (const auto& property : interface.second) { if (property.first == "MACAddress") { setFirstBootMACOnInterface( pattern.key(), std::get(property.second)); break; } } break; } } } } }; MacAddressMatch = std::make_unique( bus, "interface='org.freedesktop.DBus.ObjectManager',type='signal'," "member='InterfacesAdded',path='/xyz/openbmc_project/" "inventory'", callback); } void watchEthernetInterface(sdbusplus::bus_t& bus, const nlohmann::json& configJson) { auto mycallback = [&](sdbusplus::message_t& m) { std::map>> interfacesProperties; sdbusplus::message::object_path objPath; std::pair ethPair; m.read(objPath, interfacesProperties); for (const auto& interfaces : interfacesProperties) { if (interfaces.first == "xyz.openbmc_project.Network.EthernetInterface") { for (const auto& property : interfaces.second) { if (property.first == "InterfaceName") { std::string infname = std::get(property.second); if (configJson.find(infname) == configJson.end()) { // ethernet interface not found in configJSON // check if it is not sit0 interface, as it is // expected. if (infname != "sit0") { log( "Wrong Interface Name in Config Json"); } } else { if (!setInventoryMACOnSystem(bus, configJson, infname)) { registerSignals(bus, configJson); EthInterfaceMatch = nullptr; } } break; } } break; } } }; // Incase if phosphor-inventory-manager started early and the VPD is already // collected by the time network service has come up, better to check the // VPD directly and set the MAC Address on the respective Interface. bool registeredSignals = false; for (const auto& interfaceString : configJson.items()) { if ((FORCE_SYNC_MAC_FROM_INVENTORY || !std::filesystem::exists(firstBootPath + interfaceString.key())) && !registeredSignals) { auto msg = fmt::format("{}, check VPD for MAC", (FORCE_SYNC_MAC_FROM_INVENTORY) ? "Force sync enabled" : "First boot file is not present"); log(msg.c_str()); EthInterfaceMatch = std::make_unique( bus, "interface='org.freedesktop.DBus.ObjectManager',type='signal'," "member='InterfacesAdded',path='/xyz/openbmc_project/network'", mycallback); registeredSignals = true; } } } std::unique_ptr watch(sdbusplus::bus_t& bus, Manager& m) { manager = &m; std::ifstream in(configFile); nlohmann::json configJson; in >> configJson; watchEthernetInterface(bus, configJson); return nullptr; } } // namespace phosphor::network::inventory