blob: 3828c9b478239b7163856c37c1764be223f98754 [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
Ratan Guptaf6657382017-11-10 17:58:17 +05308#include <linux/netlink.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
Ratan Guptaf6657382017-11-10 17:58:17 +053010#include <memory>
11#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053012#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053013#include <sdbusplus/bus.hpp>
14#include <sdbusplus/server/manager.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053015#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053016
17namespace phosphor
18{
19namespace network
20{
21
22std::unique_ptr<phosphor::network::Manager> manager = nullptr;
Ratan Gupta16f12882017-09-22 18:26:11 +053023std::unique_ptr<phosphor::network::Timer> refreshObjectTimer = nullptr;
24std::unique_ptr<phosphor::network::Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053025
Ratan Gupta16f12882017-09-22 18:26:11 +053026/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053027void refreshObjects()
28{
Ratan Gupta16f12882017-09-22 18:26:11 +053029 if (manager)
30 {
31 manager->createChildObjects();
32 }
33}
34
35/** @brief restart the systemd networkd. */
36void restartNetwork()
37{
38 restartSystemdUnit("systemd-networkd.service");
Ratan Guptaa54d8f82017-09-08 17:05:46 +053039}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053040
41} //namespace network
42} //namespace phosphor
Ratan Gupta8c834932017-04-14 16:30:24 +053043
Ratan Gupta16f12882017-09-22 18:26:11 +053044void 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 Guptaf6657382017-11-10 17:58:17 +053059void 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 Gupta8c834932017-04-14 16:30:24 +053078int main(int argc, char *argv[])
79{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053080 using namespace phosphor::logging;
81
Ratan Gupta16f12882017-09-22 18:26:11 +053082 initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053083
Ratan Guptacb7098d2017-04-14 17:46:05 +053084 auto bus = sdbusplus::bus::new_default();
85
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053086 // 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 Guptacb7098d2017-04-14 17:46:05 +0530101 // Add sdbusplus Object Manager for the 'root' path of the network manager.
102 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +0530103 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530104
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530105 phosphor::network::manager =
106 std::make_unique<phosphor::network::Manager>(bus,
107 OBJ_NETWORK,
108 NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530109
Ratan Guptab610caf2017-09-19 09:33:51 +0530110 // create the network interface dbus objects and system config
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530111 phosphor::network::manager->createChildObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530112
Ratan Guptab610caf2017-09-19 09:33:51 +0530113 // 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 Gupta16f12882017-09-22 18:26:11 +0530122 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530123 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530124
Ratan Guptaf6657382017-11-10 17:58:17 +0530125 //RtnetLink socket
126 phosphor::Descriptor smartSock;
127 createNetLinkSocket(smartSock);
128
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530129 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530130 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530131
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 Subbanna18891c62017-10-17 15:22:46 +0530143 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530144}
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530145