blob: 62d12afdb339a8fb289c751ec6840e6239858d37 [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 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 }
Ratan Guptae9629412017-12-21 08:20:25 +0530124 else
125 {
126 // this will add the additional fixes which is needed
127 // in the existing network file.
128 phosphor::network::manager->writeToConfigurationFile();
Ratan Gupta2939f182018-02-14 23:14:54 +0530129 // whenever the configuration file gets written it restart
130 // the network which creates the network objects
Ratan Guptae9629412017-12-21 08:20:25 +0530131 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530132
Ratan Guptaf6657382017-11-10 17:58:17 +0530133 //RtnetLink socket
134 phosphor::Descriptor smartSock;
135 createNetLinkSocket(smartSock);
136
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530137 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530138 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530139
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 Subbanna18891c62017-10-17 15:22:46 +0530151 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530152}
Ratan Guptaa54d8f82017-09-08 17:05:46 +0530153