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