blob: dfaeecc5e3472a40ec84ef25da6c8a6d51722adf [file] [log] [blame]
Ratan Guptaa54d8f82017-09-08 17:05:46 +05301#include "config.h"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include "dns_updater.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05304#include "network_manager.hpp"
5#include "rtnetlink_server.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07006#include "types.hpp"
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +05307#include "watch.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05308
Ratan Guptaf6657382017-11-10 17:58:17 +05309#include <linux/netlink.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053010
William A. Kennington III3a70fa22018-09-20 18:48:20 -070011#include <functional>
Ratan Guptaf6657382017-11-10 17:58:17 +053012#include <memory>
13#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053014#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053015#include <sdbusplus/bus.hpp>
16#include <sdbusplus/server/manager.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070017#include <sdeventplus/event.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053018#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053019
William A. Kennington III09095f82018-09-27 12:05:12 -070020using phosphor::logging::elog;
21using phosphor::logging::entry;
22using phosphor::logging::level;
23using phosphor::logging::log;
24using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
25
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053026namespace phosphor
27{
28namespace network
29{
30
31std::unique_ptr<phosphor::network::Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070032std::unique_ptr<Timer> refreshObjectTimer = nullptr;
33std::unique_ptr<Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053034
Ratan Gupta16f12882017-09-22 18:26:11 +053035/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053036void refreshObjects()
37{
Ratan Gupta16f12882017-09-22 18:26:11 +053038 if (manager)
39 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053040 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053041 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053042 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053043 }
44}
45
46/** @brief restart the systemd networkd. */
47void restartNetwork()
48{
Ratan Gupta895f9e52018-11-26 20:57:34 +053049 if (manager)
50 {
51 manager->restartSystemdUnit("systemd-networkd.service");
52 }
Ratan Guptaa54d8f82017-09-08 17:05:46 +053053}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053054
Ratan Gupta16f12882017-09-22 18:26:11 +053055void initializeTimers()
56{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070057 auto event = sdeventplus::Event::get_default();
58 refreshObjectTimer =
59 std::make_unique<Timer>(event, std::bind(refreshObjects));
60 restartTimer = std::make_unique<Timer>(event, std::bind(restartNetwork));
Ratan Gupta16f12882017-09-22 18:26:11 +053061}
62
William A. Kennington III3a70fa22018-09-20 18:48:20 -070063} // namespace network
64} // namespace phosphor
65
Ratan Guptaf6657382017-11-10 17:58:17 +053066void createNetLinkSocket(phosphor::Descriptor& smartSock)
67{
Gunnar Mills57d9c502018-09-14 14:42:34 -050068 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +053069 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
70 if (fd < 0)
71 {
Ratan Guptaf6657382017-11-10 17:58:17 +053072 log<level::ERR>("Unable to create the net link socket",
William A. Kennington III798c2812018-09-27 12:07:39 -070073 entry("ERRNO=%d", errno));
Ratan Guptaf6657382017-11-10 17:58:17 +053074 elog<InternalFailure>();
75 }
76 smartSock.set(fd);
77}
78
Gunnar Mills57d9c502018-09-14 14:42:34 -050079int main(int argc, char* argv[])
Ratan Gupta8c834932017-04-14 16:30:24 +053080{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070081 phosphor::network::initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053082
Ratan Guptacb7098d2017-04-14 17:46:05 +053083 auto bus = sdbusplus::bus::new_default();
84
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053085 // Need sd_event to watch for OCC device errors
86 sd_event* event = nullptr;
87 auto r = sd_event_default(&event);
88 if (r < 0)
89 {
90 log<level::ERR>("Error creating a default sd_event handler");
91 return r;
92 }
93
94 phosphor::network::EventPtr eventPtr{event};
95 event = nullptr;
96
97 // Attach the bus to sd_event to service user requests
98 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
99
Ratan Guptacb7098d2017-04-14 17:46:05 +0530100 // Add sdbusplus Object Manager for the 'root' path of the network manager.
101 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +0530102 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530103
Gunnar Mills57d9c502018-09-14 14:42:34 -0500104 phosphor::network::manager = std::make_unique<phosphor::network::Manager>(
105 bus, OBJ_NETWORK, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530106
Ratan Guptab610caf2017-09-19 09:33:51 +0530107 // create the default network files if the network file
108 // is not there for any interface.
109 // Parameter false means don't create the network
110 // files forcefully.
111 if (phosphor::network::manager->createDefaultNetworkFiles(false))
112 {
113 // if files created restart the network.
114 // don't need to call the create child objects as eventhandler
115 // will create it.
Ratan Gupta16f12882017-09-22 18:26:11 +0530116 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530117 }
Ratan Guptae9629412017-12-21 08:20:25 +0530118 else
119 {
120 // this will add the additional fixes which is needed
121 // in the existing network file.
122 phosphor::network::manager->writeToConfigurationFile();
Ratan Gupta2939f182018-02-14 23:14:54 +0530123 // whenever the configuration file gets written it restart
124 // the network which creates the network objects
Ratan Guptae9629412017-12-21 08:20:25 +0530125 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530126
Gunnar Mills57d9c502018-09-14 14:42:34 -0500127 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530128 phosphor::Descriptor smartSock;
129 createNetLinkSocket(smartSock);
130
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530131 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530132 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530133
134 // DNS entry handler
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135 phosphor::network::inotify::Watch watch(
136 eventPtr, DNS_ENTRY_FILE,
137 std::bind(&phosphor::network::dns::updater::processDNSEntries,
138 std::placeholders::_1));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530139
140 // At this point, we have registered for the notifications for future
141 // events. However, if the file is already populated before this, then
142 // they won't ever get notified and thus we need to read once before
143 // waiting on change events
144 phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE);
145
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530146 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530147}