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 | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 30 | if (manager) |
| 31 | { |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 32 | log<level::INFO>("Refreshing the objects."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 33 | manager->createChildObjects(); |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 34 | log<level::INFO>("Refreshing complete."); |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | /** @brief restart the systemd networkd. */ |
| 39 | void restartNetwork() |
| 40 | { |
| 41 | restartSystemdUnit("systemd-networkd.service"); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 42 | } |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 43 | |
| 44 | } //namespace network |
| 45 | } //namespace phosphor |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 46 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 47 | void initializeTimers() |
| 48 | { |
| 49 | std::function<void()> refreshFunc( |
| 50 | std::bind(&phosphor::network::refreshObjects)); |
| 51 | |
| 52 | std::function<void()> restartFunc( |
| 53 | std::bind(&phosphor::network::restartNetwork)); |
| 54 | |
| 55 | phosphor::network::refreshObjectTimer = |
| 56 | std::make_unique<phosphor::network::Timer>(refreshFunc); |
| 57 | |
| 58 | phosphor::network::restartTimer = |
| 59 | std::make_unique<phosphor::network::Timer>(restartFunc); |
| 60 | } |
| 61 | |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 62 | void createNetLinkSocket(phosphor::Descriptor& smartSock) |
| 63 | { |
| 64 | using namespace phosphor::logging; |
| 65 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 66 | Error::InternalFailure; |
| 67 | //RtnetLink socket |
| 68 | auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE); |
| 69 | if (fd < 0) |
| 70 | { |
| 71 | auto r = -errno; |
| 72 | log<level::ERR>("Unable to create the net link socket", |
| 73 | entry("ERRNO=%d", r)); |
| 74 | elog<InternalFailure>(); |
| 75 | } |
| 76 | smartSock.set(fd); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 81 | int main(int argc, char *argv[]) |
| 82 | { |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 83 | using namespace phosphor::logging; |
| 84 | |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 85 | initializeTimers(); |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 86 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 87 | auto bus = sdbusplus::bus::new_default(); |
| 88 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 89 | // Need sd_event to watch for OCC device errors |
| 90 | sd_event* event = nullptr; |
| 91 | auto r = sd_event_default(&event); |
| 92 | if (r < 0) |
| 93 | { |
| 94 | log<level::ERR>("Error creating a default sd_event handler"); |
| 95 | return r; |
| 96 | } |
| 97 | |
| 98 | phosphor::network::EventPtr eventPtr{event}; |
| 99 | event = nullptr; |
| 100 | |
| 101 | // Attach the bus to sd_event to service user requests |
| 102 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 103 | |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 104 | // Add sdbusplus Object Manager for the 'root' path of the network manager. |
| 105 | sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK); |
Ratan Gupta | 26e87a0 | 2017-08-18 01:08:40 +0530 | [diff] [blame] | 106 | bus.request_name(BUSNAME_NETWORK); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 107 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 108 | phosphor::network::manager = |
| 109 | std::make_unique<phosphor::network::Manager>(bus, |
| 110 | OBJ_NETWORK, |
| 111 | NETWORK_CONF_DIR); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +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 | } |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 124 | else |
| 125 | { |
| 126 | // this will add the additional fixes which is needed |
| 127 | // in the existing network file. |
| 128 | phosphor::network::manager->writeToConfigurationFile(); |
Ratan Gupta | 2939f18 | 2018-02-14 23:14:54 +0530 | [diff] [blame] | 129 | // whenever the configuration file gets written it restart |
| 130 | // the network which creates the network objects |
Ratan Gupta | e962941 | 2017-12-21 08:20:25 +0530 | [diff] [blame] | 131 | } |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 132 | |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 133 | //RtnetLink socket |
| 134 | phosphor::Descriptor smartSock; |
| 135 | createNetLinkSocket(smartSock); |
| 136 | |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 137 | // RTNETLINK event handler |
Ratan Gupta | f665738 | 2017-11-10 17:58:17 +0530 | [diff] [blame] | 138 | phosphor::network::rtnetlink::Server svr(eventPtr, smartSock); |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 139 | |
| 140 | // DNS entry handler |
| 141 | phosphor::network::inotify::Watch watch(eventPtr, DNS_ENTRY_FILE, |
| 142 | std::bind(&phosphor::network::dns::updater::processDNSEntries, |
| 143 | std::placeholders::_1)); |
| 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 |
| 149 | phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE); |
| 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 | } |
Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 153 | |