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