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 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 10 | #include <functional> |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 11 | #include <memory> |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 14 | #include <sdbusplus/bus.hpp> |
| 15 | #include <sdbusplus/server/manager.hpp> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 16 | #include <sdeventplus/event.hpp> |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 18 | |
William A. Kennington III | 09095f8 | 2018-09-27 12:05:12 -0700 | [diff] [blame] | 19 | using phosphor::logging::elog; |
| 20 | using phosphor::logging::entry; |
| 21 | using phosphor::logging::level; |
| 22 | using phosphor::logging::log; |
| 23 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 24 | |
William A. Kennington III | 483e777 | 2019-02-12 19:03:15 -0800 | [diff] [blame] | 25 | constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network"; |
| 26 | |
William A. Kennington III | 5f1eb46 | 2019-02-12 19:47:51 -0800 | [diff] [blame] | 27 | constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network"; |
| 28 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 29 | namespace phosphor |
| 30 | { |
| 31 | namespace network |
| 32 | { |
| 33 | |
| 34 | std::unique_ptr<phosphor::network::Manager> manager = nullptr; |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 35 | std::unique_ptr<Timer> refreshObjectTimer = nullptr; |
| 36 | std::unique_ptr<Timer> restartTimer = nullptr; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 37 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 38 | /** @brief refresh the network objects. */ |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 39 | void refreshObjects() |
| 40 | { |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 41 | if (manager) |
| 42 | { |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 43 | log<level::INFO>("Refreshing the objects."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 44 | manager->createChildObjects(); |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 45 | log<level::INFO>("Refreshing complete."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | /** @brief restart the systemd networkd. */ |
| 50 | void restartNetwork() |
| 51 | { |
Ratan Gupta | 895f9e5 | 2018-11-26 20:57:34 +0530 | [diff] [blame] | 52 | if (manager) |
| 53 | { |
| 54 | manager->restartSystemdUnit("systemd-networkd.service"); |
| 55 | } |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 56 | } |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 57 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 58 | void initializeTimers() |
| 59 | { |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 60 | auto event = sdeventplus::Event::get_default(); |
| 61 | refreshObjectTimer = |
| 62 | std::make_unique<Timer>(event, std::bind(refreshObjects)); |
| 63 | restartTimer = std::make_unique<Timer>(event, std::bind(restartNetwork)); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 64 | } |
| 65 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 66 | } // namespace network |
| 67 | } // namespace phosphor |
| 68 | |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 69 | void createNetLinkSocket(phosphor::Descriptor& smartSock) |
| 70 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 71 | // RtnetLink socket |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 72 | auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); |
| 73 | if (fd < 0) |
| 74 | { |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 75 | log<level::ERR>("Unable to create the net link socket", |
William A. Kennington III | 798c281 | 2018-09-27 12:07:39 -0700 | [diff] [blame] | 76 | entry("ERRNO=%d", errno)); |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 77 | elog<InternalFailure>(); |
| 78 | } |
| 79 | smartSock.set(fd); |
| 80 | } |
| 81 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | int main(int argc, char* argv[]) |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 83 | { |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 84 | phosphor::network::initializeTimers(); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 85 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 86 | auto bus = sdbusplus::bus::new_default(); |
| 87 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 88 | // Need sd_event to watch for OCC device errors |
| 89 | sd_event* event = nullptr; |
| 90 | auto r = sd_event_default(&event); |
| 91 | if (r < 0) |
| 92 | { |
| 93 | log<level::ERR>("Error creating a default sd_event handler"); |
| 94 | return r; |
| 95 | } |
| 96 | |
| 97 | phosphor::network::EventPtr eventPtr{event}; |
| 98 | event = nullptr; |
| 99 | |
| 100 | // Attach the bus to sd_event to service user requests |
| 101 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 102 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 103 | // 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] | 104 | sdbusplus::server::manager::manager objManager(bus, DEFAULT_OBJPATH); |
| 105 | bus.request_name(DEFAULT_BUSNAME); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 106 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 107 | phosphor::network::manager = std::make_unique<phosphor::network::Manager>( |
William A. Kennington III | 5f1eb46 | 2019-02-12 19:47:51 -0800 | [diff] [blame] | 108 | bus, DEFAULT_OBJPATH, NETWORK_CONF_DIR); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 109 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 110 | // create the default network files if the network file |
| 111 | // is not there for any interface. |
| 112 | // Parameter false means don't create the network |
| 113 | // files forcefully. |
| 114 | if (phosphor::network::manager->createDefaultNetworkFiles(false)) |
| 115 | { |
| 116 | // if files created restart the network. |
| 117 | // don't need to call the create child objects as eventhandler |
| 118 | // will create it. |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 119 | phosphor::network::restartNetwork(); |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 120 | } |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 121 | else |
| 122 | { |
| 123 | // this will add the additional fixes which is needed |
| 124 | // in the existing network file. |
| 125 | phosphor::network::manager->writeToConfigurationFile(); |
Ratan Gupta | 2939f18 | 2018-02-14 23:14:54 +0530 | [diff] [blame] | 126 | // whenever the configuration file gets written it restart |
| 127 | // the network which creates the network objects |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 128 | } |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 129 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 130 | // RtnetLink socket |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 131 | phosphor::Descriptor smartSock; |
| 132 | createNetLinkSocket(smartSock); |
| 133 | |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 134 | // RTNETLINK event handler |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 135 | phosphor::network::rtnetlink::Server svr(eventPtr, smartSock); |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 136 | |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 137 | sd_event_loop(eventPtr.get()); |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 138 | } |