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