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