blob: 5f6b81bd593e1612a0913b06d69a64d034ef4798 [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"
Ratan Guptaa54d8f82017-09-08 17:05:46 +05306#include "timer.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
Ratan Guptaf6657382017-11-10 17:58:17 +053011#include <memory>
12#include <phosphor-logging/elog-errors.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053013#include <phosphor-logging/log.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053014#include <sdbusplus/bus.hpp>
15#include <sdbusplus/server/manager.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053016#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053017
18namespace phosphor
19{
20namespace network
21{
22
23std::unique_ptr<phosphor::network::Manager> manager = nullptr;
Ratan Gupta16f12882017-09-22 18:26:11 +053024std::unique_ptr<phosphor::network::Timer> refreshObjectTimer = nullptr;
25std::unique_ptr<phosphor::network::Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053026
Ratan Gupta16f12882017-09-22 18:26:11 +053027/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053028void refreshObjects()
29{
Ratan Gupta310a0b12017-11-15 17:40:24 +053030 using namespace phosphor::logging;
Ratan Gupta16f12882017-09-22 18:26:11 +053031 if (manager)
32 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053033 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053034 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053035 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053036 }
37}
38
39/** @brief restart the systemd networkd. */
40void restartNetwork()
41{
42 restartSystemdUnit("systemd-networkd.service");
Ratan Guptaa54d8f82017-09-08 17:05:46 +053043}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053044
Gunnar Mills57d9c502018-09-14 14:42:34 -050045} // namespace network
46} // namespace phosphor
Ratan Gupta8c834932017-04-14 16:30:24 +053047
Ratan Gupta16f12882017-09-22 18:26:11 +053048void initializeTimers()
49{
50 std::function<void()> refreshFunc(
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 std::bind(&phosphor::network::refreshObjects));
Ratan Gupta16f12882017-09-22 18:26:11 +053052
53 std::function<void()> restartFunc(
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 std::bind(&phosphor::network::restartNetwork));
Ratan Gupta16f12882017-09-22 18:26:11 +053055
56 phosphor::network::refreshObjectTimer =
57 std::make_unique<phosphor::network::Timer>(refreshFunc);
58
59 phosphor::network::restartTimer =
60 std::make_unique<phosphor::network::Timer>(restartFunc);
61}
62
Ratan Guptaf6657382017-11-10 17:58:17 +053063void createNetLinkSocket(phosphor::Descriptor& smartSock)
64{
65 using namespace phosphor::logging;
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 using InternalFailure =
67 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
68 // 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 {
72 auto r = -errno;
73 log<level::ERR>("Unable to create the net link socket",
74 entry("ERRNO=%d", r));
75 elog<InternalFailure>();
76 }
77 smartSock.set(fd);
78}
79
Gunnar Mills57d9c502018-09-14 14:42:34 -050080int main(int argc, char* argv[])
Ratan Gupta8c834932017-04-14 16:30:24 +053081{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053082 using namespace phosphor::logging;
83
Ratan Gupta16f12882017-09-22 18:26:11 +053084 initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053085
Ratan Guptacb7098d2017-04-14 17:46:05 +053086 auto bus = sdbusplus::bus::new_default();
87
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053088 // Need sd_event to watch for OCC device errors
89 sd_event* event = nullptr;
90 auto r = sd_event_default(&event);
91 if (r < 0)
92 {
93 log<level::ERR>("Error creating a default sd_event handler");
94 return r;
95 }
96
97 phosphor::network::EventPtr eventPtr{event};
98 event = nullptr;
99
100 // Attach the bus to sd_event to service user requests
101 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
102
Ratan Guptacb7098d2017-04-14 17:46:05 +0530103 // Add sdbusplus Object Manager for the 'root' path of the network manager.
104 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +0530105 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530106
Gunnar Mills57d9c502018-09-14 14:42:34 -0500107 phosphor::network::manager = std::make_unique<phosphor::network::Manager>(
108 bus, OBJ_NETWORK, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530109
Ratan Guptab610caf2017-09-19 09:33:51 +0530110 // create the default network files if the network file
111 // is not there for any interface.
112 // Parameter false means don't create the network
113 // files forcefully.
114 if (phosphor::network::manager->createDefaultNetworkFiles(false))
115 {
116 // if files created restart the network.
117 // don't need to call the create child objects as eventhandler
118 // will create it.
Ratan Gupta16f12882017-09-22 18:26:11 +0530119 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530120 }
Ratan Guptae9629412017-12-21 08:20:25 +0530121 else
122 {
123 // this will add the additional fixes which is needed
124 // in the existing network file.
125 phosphor::network::manager->writeToConfigurationFile();
Ratan Gupta2939f182018-02-14 23:14:54 +0530126 // whenever the configuration file gets written it restart
127 // the network which creates the network objects
Ratan Guptae9629412017-12-21 08:20:25 +0530128 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530129
Gunnar Mills57d9c502018-09-14 14:42:34 -0500130 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530131 phosphor::Descriptor smartSock;
132 createNetLinkSocket(smartSock);
133
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530134 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530135 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530136
137 // DNS entry handler
Gunnar Mills57d9c502018-09-14 14:42:34 -0500138 phosphor::network::inotify::Watch watch(
139 eventPtr, DNS_ENTRY_FILE,
140 std::bind(&phosphor::network::dns::updater::processDNSEntries,
141 std::placeholders::_1));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530142
143 // At this point, we have registered for the notifications for future
144 // events. However, if the file is already populated before this, then
145 // they won't ever get notified and thus we need to read once before
146 // waiting on change events
147 phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE);
148
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530149 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530150}