Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 1 | #include "config.h" |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 2 | #include "network_manager.hpp" |
| 3 | #include "rtnetlink_server.hpp" |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 4 | #include "timer.hpp" |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 5 | #include "watch.hpp" |
| 6 | #include "dns_updater.hpp" |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> |
| 12 | #include <sdbusplus/server/manager.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | |
| 19 | std::unique_ptr<phosphor::network::Manager> manager = nullptr; |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 20 | std::unique_ptr<phosphor::network::Timer> refreshObjectTimer = nullptr; |
| 21 | std::unique_ptr<phosphor::network::Timer> restartTimer = nullptr; |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 22 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 23 | /** @brief refresh the network objects. */ |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 24 | void refreshObjects() |
| 25 | { |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 26 | if (manager) |
| 27 | { |
| 28 | manager->createChildObjects(); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** @brief restart the systemd networkd. */ |
| 33 | void restartNetwork() |
| 34 | { |
| 35 | restartSystemdUnit("systemd-networkd.service"); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 36 | } |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 37 | |
| 38 | } //namespace network |
| 39 | } //namespace phosphor |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 40 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 41 | void initializeTimers() |
| 42 | { |
| 43 | std::function<void()> refreshFunc( |
| 44 | std::bind(&phosphor::network::refreshObjects)); |
| 45 | |
| 46 | std::function<void()> restartFunc( |
| 47 | std::bind(&phosphor::network::restartNetwork)); |
| 48 | |
| 49 | phosphor::network::refreshObjectTimer = |
| 50 | std::make_unique<phosphor::network::Timer>(refreshFunc); |
| 51 | |
| 52 | phosphor::network::restartTimer = |
| 53 | std::make_unique<phosphor::network::Timer>(restartFunc); |
| 54 | } |
| 55 | |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 56 | int main(int argc, char *argv[]) |
| 57 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 58 | using namespace phosphor::logging; |
| 59 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 60 | initializeTimers(); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 61 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 62 | auto bus = sdbusplus::bus::new_default(); |
| 63 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 64 | // Need sd_event to watch for OCC device errors |
| 65 | sd_event* event = nullptr; |
| 66 | auto r = sd_event_default(&event); |
| 67 | if (r < 0) |
| 68 | { |
| 69 | log<level::ERR>("Error creating a default sd_event handler"); |
| 70 | return r; |
| 71 | } |
| 72 | |
| 73 | phosphor::network::EventPtr eventPtr{event}; |
| 74 | event = nullptr; |
| 75 | |
| 76 | // Attach the bus to sd_event to service user requests |
| 77 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 78 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 79 | // Add sdbusplus Object Manager for the 'root' path of the network manager. |
| 80 | sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK); |
Ratan Gupta | 26e87a0 | 2017-08-18 01:08:40 +0530 | [diff] [blame] | 81 | bus.request_name(BUSNAME_NETWORK); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 82 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 83 | phosphor::network::manager = |
| 84 | std::make_unique<phosphor::network::Manager>(bus, |
| 85 | OBJ_NETWORK, |
| 86 | NETWORK_CONF_DIR); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 87 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 88 | // create the network interface dbus objects and system config |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 89 | phosphor::network::manager->createChildObjects(); |
Ratan Gupta | 29b0e43 | 2017-05-25 12:51:40 +0530 | [diff] [blame] | 90 | |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 91 | // create the default network files if the network file |
| 92 | // is not there for any interface. |
| 93 | // Parameter false means don't create the network |
| 94 | // files forcefully. |
| 95 | if (phosphor::network::manager->createDefaultNetworkFiles(false)) |
| 96 | { |
| 97 | // if files created restart the network. |
| 98 | // don't need to call the create child objects as eventhandler |
| 99 | // will create it. |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 100 | phosphor::network::restartNetwork(); |
Ratan Gupta | b610caf | 2017-09-19 09:33:51 +0530 | [diff] [blame] | 101 | } |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 102 | |
| 103 | // RTNETLINK event handler |
| 104 | phosphor::network::rtnetlink::Server svr(eventPtr); |
| 105 | |
| 106 | // DNS entry handler |
| 107 | phosphor::network::inotify::Watch watch(eventPtr, DNS_ENTRY_FILE, |
| 108 | std::bind(&phosphor::network::dns::updater::processDNSEntries, |
| 109 | std::placeholders::_1)); |
| 110 | |
| 111 | // At this point, we have registered for the notifications for future |
| 112 | // events. However, if the file is already populated before this, then |
| 113 | // they won't ever get notified and thus we need to read once before |
| 114 | // waiting on change events |
| 115 | phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE); |
| 116 | |
| 117 | // Run the server |
| 118 | sd_event_loop(eventPtr.get()); |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 119 | } |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 120 | |