blob: b08d5acaf5eaf128fc5f370516565d65d6525cf4 [file] [log] [blame]
Ratan Guptaa54d8f82017-09-08 17:05:46 +05301#include "config.h"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05302#include "network_manager.hpp"
3#include "rtnetlink_server.hpp"
Ratan Guptaa54d8f82017-09-08 17:05:46 +05304#include "timer.hpp"
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +05305#include "watch.hpp"
6#include "dns_updater.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05307
8#include <memory>
9
10#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/manager.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053013
14namespace phosphor
15{
16namespace network
17{
18
19std::unique_ptr<phosphor::network::Manager> manager = nullptr;
Ratan Gupta16f12882017-09-22 18:26:11 +053020std::unique_ptr<phosphor::network::Timer> refreshObjectTimer = nullptr;
21std::unique_ptr<phosphor::network::Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053022
Ratan Gupta16f12882017-09-22 18:26:11 +053023/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053024void refreshObjects()
25{
Ratan Gupta16f12882017-09-22 18:26:11 +053026 if (manager)
27 {
28 manager->createChildObjects();
29 }
30}
31
32/** @brief restart the systemd networkd. */
33void restartNetwork()
34{
35 restartSystemdUnit("systemd-networkd.service");
Ratan Guptaa54d8f82017-09-08 17:05:46 +053036}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053037
38} //namespace network
39} //namespace phosphor
Ratan Gupta8c834932017-04-14 16:30:24 +053040
Ratan Gupta16f12882017-09-22 18:26:11 +053041void 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 Gupta8c834932017-04-14 16:30:24 +053056int main(int argc, char *argv[])
57{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053058 using namespace phosphor::logging;
59
Ratan Gupta16f12882017-09-22 18:26:11 +053060 initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053061
Ratan Guptacb7098d2017-04-14 17:46:05 +053062 auto bus = sdbusplus::bus::new_default();
63
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053064 // 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 Guptacb7098d2017-04-14 17:46:05 +053079 // Add sdbusplus Object Manager for the 'root' path of the network manager.
80 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +053081 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +053082
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053083 phosphor::network::manager =
84 std::make_unique<phosphor::network::Manager>(bus,
85 OBJ_NETWORK,
86 NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +053087
Ratan Guptab610caf2017-09-19 09:33:51 +053088 // create the network interface dbus objects and system config
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053089 phosphor::network::manager->createChildObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +053090
Ratan Guptab610caf2017-09-19 09:33:51 +053091 // 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 Gupta16f12882017-09-22 18:26:11 +0530100 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530101 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530102
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 Gupta8c834932017-04-14 16:30:24 +0530119}
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530120