blob: d13a857edcffdc8d7fdcdb05088b825711926760 [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 Gupta310a0b12017-11-15 17:40:24 +053029 using namespace phosphor::logging;
Ratan Gupta16f12882017-09-22 18:26:11 +053030 if (manager)
31 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053032 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053033 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053034 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053035 }
36}
37
38/** @brief restart the systemd networkd. */
39void restartNetwork()
40{
41 restartSystemdUnit("systemd-networkd.service");
Ratan Guptaa54d8f82017-09-08 17:05:46 +053042}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053043
44} //namespace network
45} //namespace phosphor
Ratan Gupta8c834932017-04-14 16:30:24 +053046
Ratan Gupta16f12882017-09-22 18:26:11 +053047void 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 Guptaf6657382017-11-10 17:58:17 +053062void 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 Gupta8c834932017-04-14 16:30:24 +053081int main(int argc, char *argv[])
82{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053083 using namespace phosphor::logging;
84
Ratan Gupta16f12882017-09-22 18:26:11 +053085 initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053086
Ratan Guptacb7098d2017-04-14 17:46:05 +053087 auto bus = sdbusplus::bus::new_default();
88
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053089 // 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 Guptacb7098d2017-04-14 17:46:05 +0530104 // Add sdbusplus Object Manager for the 'root' path of the network manager.
105 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +0530106 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530107
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530108 phosphor::network::manager =
109 std::make_unique<phosphor::network::Manager>(bus,
110 OBJ_NETWORK,
111 NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530112
Ratan Guptab610caf2017-09-19 09:33:51 +0530113 // create the network interface dbus objects and system config
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +0530114 phosphor::network::manager->createChildObjects();
Ratan Gupta29b0e432017-05-25 12:51:40 +0530115
Ratan Guptab610caf2017-09-19 09:33:51 +0530116 // create the default network files if the network file
117 // is not there for any interface.
118 // Parameter false means don't create the network
119 // files forcefully.
120 if (phosphor::network::manager->createDefaultNetworkFiles(false))
121 {
122 // if files created restart the network.
123 // don't need to call the create child objects as eventhandler
124 // will create it.
Ratan Gupta16f12882017-09-22 18:26:11 +0530125 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530126 }
Ratan Guptae9629412017-12-21 08:20:25 +0530127 else
128 {
129 // this will add the additional fixes which is needed
130 // in the existing network file.
131 phosphor::network::manager->writeToConfigurationFile();
132 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530133
Ratan Guptaf6657382017-11-10 17:58:17 +0530134 //RtnetLink socket
135 phosphor::Descriptor smartSock;
136 createNetLinkSocket(smartSock);
137
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530138 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530139 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530140
141 // DNS entry handler
142 phosphor::network::inotify::Watch watch(eventPtr, DNS_ENTRY_FILE,
143 std::bind(&phosphor::network::dns::updater::processDNSEntries,
144 std::placeholders::_1));
145
146 // At this point, we have registered for the notifications for future
147 // events. However, if the file is already populated before this, then
148 // they won't ever get notified and thus we need to read once before
149 // waiting on change events
150 phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE);
151
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530152 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530153}
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530154