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