Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 2 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 3 | #include "network_manager.hpp" |
| 4 | #include "rtnetlink_server.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 5 | #include "types.hpp" |
William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 6 | #ifdef SYNC_MAC_FROM_INVENTORY |
| 7 | #include "util.hpp" |
| 8 | #endif |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 9 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 10 | #include <fmt/format.h> |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 11 | #include <linux/netlink.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 12 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 13 | #include <filesystem> |
| 14 | #include <fstream> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 15 | #include <functional> |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 16 | #include <memory> |
William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 17 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 18 | #include <nlohmann/json.hpp> |
William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 19 | #endif |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 20 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 22 | #include <sdbusplus/bus.hpp> |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 23 | #include <sdbusplus/bus/match.hpp> |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 24 | #include <sdbusplus/server/manager.hpp> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 25 | #include <sdeventplus/event.hpp> |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 26 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 27 | |
William A. Kennington III | 09095f8 | 2018-09-27 12:05:12 -0700 | [diff] [blame] | 28 | using phosphor::logging::elog; |
| 29 | using phosphor::logging::entry; |
| 30 | using phosphor::logging::level; |
| 31 | using phosphor::logging::log; |
| 32 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 33 | using DbusObjectPath = std::string; |
| 34 | using DbusInterface = std::string; |
| 35 | using PropertyValue = std::string; |
William A. Kennington III | 09095f8 | 2018-09-27 12:05:12 -0700 | [diff] [blame] | 36 | |
William A. Kennington III | 483e777 | 2019-02-12 19:03:15 -0800 | [diff] [blame] | 37 | constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network"; |
| 38 | |
William A. Kennington III | 5f1eb46 | 2019-02-12 19:47:51 -0800 | [diff] [blame] | 39 | constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network"; |
| 40 | |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 41 | constexpr auto firstBootPath = "/var/lib/network/firstBoot_"; |
| 42 | constexpr auto configFile = "/usr/share/network/config.json"; |
| 43 | |
| 44 | constexpr auto invNetworkIntf = |
| 45 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; |
| 46 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 47 | namespace phosphor |
| 48 | { |
| 49 | namespace network |
| 50 | { |
| 51 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 52 | std::unique_ptr<Manager> manager = nullptr; |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 53 | std::unique_ptr<Timer> refreshObjectTimer = nullptr; |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 54 | std::unique_ptr<Timer> reloadTimer = nullptr; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 55 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 56 | #ifdef SYNC_MAC_FROM_INVENTORY |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 57 | std::unique_ptr<sdbusplus::bus::match_t> EthInterfaceMatch = nullptr; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 58 | std::vector<std::string> first_boot_status; |
| 59 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 60 | bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 61 | const nlohmann::json& configJson, |
| 62 | const std::string& intfname) |
| 63 | { |
| 64 | try |
| 65 | { |
| 66 | auto inventoryMAC = mac_address::getfromInventory(bus, intfname); |
| 67 | if (!mac_address::toString(inventoryMAC).empty()) |
| 68 | { |
| 69 | log<level::INFO>("Mac Address in Inventory on "), |
| 70 | entry("Interface : ", intfname.c_str()), |
| 71 | entry("MAC Address :", |
| 72 | (mac_address::toString(inventoryMAC)).c_str()); |
| 73 | manager->setFistBootMACOnInterface(std::make_pair( |
| 74 | intfname.c_str(), mac_address::toString(inventoryMAC))); |
| 75 | first_boot_status.push_back(intfname.c_str()); |
| 76 | bool status = true; |
| 77 | for (const auto& keys : configJson.items()) |
| 78 | { |
| 79 | if (!(std::find(first_boot_status.begin(), |
| 80 | first_boot_status.end(), |
| 81 | keys.key()) != first_boot_status.end())) |
| 82 | { |
| 83 | log<level::INFO>("Interface MAC is NOT set from VPD"), |
| 84 | entry("INTERFACE", keys.key().c_str()); |
| 85 | status = false; |
| 86 | } |
| 87 | } |
| 88 | if (status) |
| 89 | { |
| 90 | log<level::INFO>("Removing the match for ethernet interfaces"); |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 91 | EthInterfaceMatch = nullptr; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | log<level::INFO>("Nothing is present in Inventory"); |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | catch (const std::exception& e) |
| 101 | { |
| 102 | log<level::ERR>("Exception occurred during getting of MAC " |
| 103 | "address from Inventory"); |
| 104 | return false; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // register the macthes to be monitored from inventory manager |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 110 | void registerSignals(sdbusplus::bus_t& bus, const nlohmann::json& configJson) |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 111 | { |
| 112 | log<level::INFO>("Registering the Inventory Signals Matcher"); |
| 113 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 114 | static std::unique_ptr<sdbusplus::bus::match_t> MacAddressMatch; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 115 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 116 | auto callback = [&](sdbusplus::message_t& m) { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 117 | std::map<DbusObjectPath, |
| 118 | std::map<DbusInterface, std::variant<PropertyValue>>> |
| 119 | interfacesProperties; |
| 120 | |
| 121 | sdbusplus::message::object_path objPath; |
| 122 | std::pair<std::string, std::string> ethPair; |
| 123 | m.read(objPath, interfacesProperties); |
| 124 | |
| 125 | for (const auto& pattern : configJson.items()) |
| 126 | { |
| 127 | if (objPath.str.find(pattern.value()) != std::string::npos) |
| 128 | { |
| 129 | for (auto& interface : interfacesProperties) |
| 130 | { |
| 131 | if (interface.first == invNetworkIntf) |
| 132 | { |
| 133 | for (const auto& property : interface.second) |
| 134 | { |
| 135 | if (property.first == "MACAddress") |
| 136 | { |
| 137 | ethPair = std::make_pair( |
| 138 | pattern.key(), |
| 139 | std::get<std::string>(property.second)); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | if (!(ethPair.first.empty() || ethPair.second.empty())) |
| 147 | { |
| 148 | manager->setFistBootMACOnInterface(ethPair); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | }; |
| 153 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 154 | MacAddressMatch = std::make_unique<sdbusplus::bus::match_t>( |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 155 | bus, |
| 156 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 157 | "member='InterfacesAdded',path='/xyz/openbmc_project/" |
| 158 | "inventory'", |
| 159 | callback); |
| 160 | } |
| 161 | |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 162 | void watchEthernetInterface(sdbusplus::bus_t& bus, |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 163 | const nlohmann::json& configJson) |
| 164 | { |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 165 | auto mycallback = [&](sdbusplus::message_t& m) { |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 166 | std::map<DbusObjectPath, |
| 167 | std::map<DbusInterface, std::variant<PropertyValue>>> |
| 168 | interfacesProperties; |
| 169 | |
| 170 | sdbusplus::message::object_path objPath; |
| 171 | std::pair<std::string, std::string> ethPair; |
| 172 | m.read(objPath, interfacesProperties); |
| 173 | for (const auto& interfaces : interfacesProperties) |
| 174 | { |
| 175 | if (interfaces.first == |
| 176 | "xyz.openbmc_project.Network.EthernetInterface") |
| 177 | { |
| 178 | for (const auto& property : interfaces.second) |
| 179 | { |
| 180 | if (property.first == "InterfaceName") |
| 181 | { |
| 182 | std::string infname = |
| 183 | std::get<std::string>(property.second); |
| 184 | |
| 185 | if (configJson.find(infname) == configJson.end()) |
| 186 | { |
| 187 | // ethernet interface not found in configJSON |
| 188 | // check if it is not sit0 interface, as it is |
| 189 | // expected. |
| 190 | if (infname != "sit0") |
| 191 | { |
| 192 | log<level::ERR>( |
| 193 | "Wrong Interface Name in Config Json"); |
| 194 | } |
| 195 | } |
| 196 | else |
| 197 | { |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 198 | if (!setInventoryMACOnSystem(bus, configJson, |
| 199 | infname)) |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 200 | { |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 201 | registerSignals(bus, configJson); |
| 202 | EthInterfaceMatch = nullptr; |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | // Incase if phosphor-inventory-manager started early and the VPD is already |
| 213 | // collected by the time network service has come up, better to check the |
| 214 | // VPD directly and set the MAC Address on the respective Interface. |
| 215 | |
| 216 | bool registeredSignals = false; |
| 217 | for (const auto& interfaceString : configJson.items()) |
| 218 | { |
| 219 | if (!std::filesystem::exists(firstBootPath + interfaceString.key()) && |
| 220 | !registeredSignals) |
| 221 | { |
| 222 | |
| 223 | log<level::INFO>( |
| 224 | "First boot file is not present, check VPD for MAC"); |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 225 | EthInterfaceMatch = std::make_unique<sdbusplus::bus::match_t>( |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 226 | bus, |
| 227 | "interface='org.freedesktop.DBus.ObjectManager',type='signal'," |
| 228 | "member='InterfacesAdded',path='/xyz/openbmc_project/network'", |
| 229 | mycallback); |
| 230 | registeredSignals = true; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #endif |
| 236 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 237 | /** @brief refresh the network objects. */ |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 238 | void refreshObjects() |
| 239 | { |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 240 | if (manager) |
| 241 | { |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 242 | log<level::INFO>("Refreshing the objects."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 243 | manager->createChildObjects(); |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 244 | log<level::INFO>("Refreshing complete."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 248 | void reloadNetworkd() |
| 249 | { |
| 250 | if (manager) |
| 251 | { |
| 252 | log<level::INFO>("Sending networkd reload"); |
| 253 | manager->doReloadConfigs(); |
| 254 | log<level::INFO>("Done networkd reload"); |
| 255 | } |
| 256 | } |
| 257 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 258 | void initializeTimers() |
| 259 | { |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 260 | auto event = sdeventplus::Event::get_default(); |
| 261 | refreshObjectTimer = |
| 262 | std::make_unique<Timer>(event, std::bind(refreshObjects)); |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 263 | reloadTimer = std::make_unique<Timer>(event, std::bind(reloadNetworkd)); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 264 | } |
| 265 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 266 | int main() |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 267 | { |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 268 | initializeTimers(); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 269 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 270 | auto bus = sdbusplus::bus::new_default(); |
| 271 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 272 | // Need sd_event to watch for OCC device errors |
| 273 | sd_event* event = nullptr; |
| 274 | auto r = sd_event_default(&event); |
| 275 | if (r < 0) |
| 276 | { |
| 277 | log<level::ERR>("Error creating a default sd_event handler"); |
| 278 | return r; |
| 279 | } |
| 280 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 281 | EventPtr eventPtr{event}; |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 282 | event = nullptr; |
| 283 | |
| 284 | // Attach the bus to sd_event to service user requests |
| 285 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 286 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 287 | // Add sdbusplus Object Manager for the 'root' path of the network manager. |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 288 | sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH); |
William A. Kennington III | 5f1eb46 | 2019-02-12 19:47:51 -0800 | [diff] [blame] | 289 | bus.request_name(DEFAULT_BUSNAME); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 290 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 291 | manager = std::make_unique<Manager>(bus, DEFAULT_OBJPATH, NETWORK_CONF_DIR); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 292 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 293 | // create the default network files if the network file |
| 294 | // is not there for any interface. |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 295 | if (manager->createDefaultNetworkFiles()) |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 296 | { |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 297 | manager->reloadConfigs(); |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 298 | } |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 299 | |
| 300 | // RTNETLINK event handler |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 301 | rtnetlink::Server svr(eventPtr); |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 302 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 303 | #ifdef SYNC_MAC_FROM_INVENTORY |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 304 | std::ifstream in(configFile); |
| 305 | nlohmann::json configJson; |
| 306 | in >> configJson; |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 307 | watchEthernetInterface(bus, configJson); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 308 | #endif |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 309 | |
| 310 | // Trigger the initial object scan |
William A. Kennington III | 26c40a4 | 2021-11-03 18:27:52 -0700 | [diff] [blame] | 311 | // This is intentionally deferred, to ensure that systemd-networkd is |
| 312 | // fully configured. |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 313 | refreshObjectTimer->restartOnce(refreshTimeout); |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 314 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 315 | return sd_event_loop(eventPtr.get()); |
| 316 | } |
| 317 | |
| 318 | } // namespace network |
| 319 | } // namespace phosphor |
| 320 | |
| 321 | int main(int /*argc*/, char** /*argv*/) |
| 322 | { |
| 323 | try |
| 324 | { |
| 325 | return phosphor::network::main(); |
| 326 | } |
| 327 | catch (const std::exception& e) |
| 328 | { |
| 329 | auto msg = fmt::format("FAILED: {}", e.what()); |
| 330 | log<level::ERR>(msg.c_str(), entry("ERROR", e.what())); |
| 331 | return 1; |
| 332 | } |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 333 | } |