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