blob: 7709f7d955628fc0a4942888b8a97e5f019d2df4 [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
20namespace phosphor
21{
22namespace network
23{
24
25std::unique_ptr<phosphor::network::Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070026std::unique_ptr<Timer> refreshObjectTimer = nullptr;
27std::unique_ptr<Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053028
Ratan Gupta16f12882017-09-22 18:26:11 +053029/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053030void refreshObjects()
31{
Ratan Gupta310a0b12017-11-15 17:40:24 +053032 using namespace phosphor::logging;
Ratan Gupta16f12882017-09-22 18:26:11 +053033 if (manager)
34 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053035 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053036 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053037 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053038 }
39}
40
41/** @brief restart the systemd networkd. */
42void restartNetwork()
43{
44 restartSystemdUnit("systemd-networkd.service");
Ratan Guptaa54d8f82017-09-08 17:05:46 +053045}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053046
Ratan Gupta16f12882017-09-22 18:26:11 +053047void initializeTimers()
48{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070049 auto event = sdeventplus::Event::get_default();
50 refreshObjectTimer =
51 std::make_unique<Timer>(event, std::bind(refreshObjects));
52 restartTimer = std::make_unique<Timer>(event, std::bind(restartNetwork));
Ratan Gupta16f12882017-09-22 18:26:11 +053053}
54
William A. Kennington III3a70fa22018-09-20 18:48:20 -070055} // namespace network
56} // namespace phosphor
57
Ratan Guptaf6657382017-11-10 17:58:17 +053058void createNetLinkSocket(phosphor::Descriptor& smartSock)
59{
60 using namespace phosphor::logging;
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 using InternalFailure =
62 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
63 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +053064 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
65 if (fd < 0)
66 {
67 auto r = -errno;
68 log<level::ERR>("Unable to create the net link socket",
69 entry("ERRNO=%d", r));
70 elog<InternalFailure>();
71 }
72 smartSock.set(fd);
73}
74
Gunnar Mills57d9c502018-09-14 14:42:34 -050075int main(int argc, char* argv[])
Ratan Gupta8c834932017-04-14 16:30:24 +053076{
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053077 using namespace phosphor::logging;
78
William A. Kennington III3a70fa22018-09-20 18:48:20 -070079 phosphor::network::initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053080
Ratan Guptacb7098d2017-04-14 17:46:05 +053081 auto bus = sdbusplus::bus::new_default();
82
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053083 // Need sd_event to watch for OCC device errors
84 sd_event* event = nullptr;
85 auto r = sd_event_default(&event);
86 if (r < 0)
87 {
88 log<level::ERR>("Error creating a default sd_event handler");
89 return r;
90 }
91
92 phosphor::network::EventPtr eventPtr{event};
93 event = nullptr;
94
95 // Attach the bus to sd_event to service user requests
96 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
97
Ratan Guptacb7098d2017-04-14 17:46:05 +053098 // Add sdbusplus Object Manager for the 'root' path of the network manager.
99 sdbusplus::server::manager::manager objManager(bus, OBJ_NETWORK);
Ratan Gupta26e87a02017-08-18 01:08:40 +0530100 bus.request_name(BUSNAME_NETWORK);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530101
Gunnar Mills57d9c502018-09-14 14:42:34 -0500102 phosphor::network::manager = std::make_unique<phosphor::network::Manager>(
103 bus, OBJ_NETWORK, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530104
Ratan Guptab610caf2017-09-19 09:33:51 +0530105 // create the default network files if the network file
106 // is not there for any interface.
107 // Parameter false means don't create the network
108 // files forcefully.
109 if (phosphor::network::manager->createDefaultNetworkFiles(false))
110 {
111 // if files created restart the network.
112 // don't need to call the create child objects as eventhandler
113 // will create it.
Ratan Gupta16f12882017-09-22 18:26:11 +0530114 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530115 }
Ratan Guptae9629412017-12-21 08:20:25 +0530116 else
117 {
118 // this will add the additional fixes which is needed
119 // in the existing network file.
120 phosphor::network::manager->writeToConfigurationFile();
Ratan Gupta2939f182018-02-14 23:14:54 +0530121 // whenever the configuration file gets written it restart
122 // the network which creates the network objects
Ratan Guptae9629412017-12-21 08:20:25 +0530123 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530124
Gunnar Mills57d9c502018-09-14 14:42:34 -0500125 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530126 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
Gunnar Mills57d9c502018-09-14 14:42:34 -0500133 phosphor::network::inotify::Watch watch(
134 eventPtr, DNS_ENTRY_FILE,
135 std::bind(&phosphor::network::dns::updater::processDNSEntries,
136 std::placeholders::_1));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530137
138 // At this point, we have registered for the notifications for future
139 // events. However, if the file is already populated before this, then
140 // they won't ever get notified and thus we need to read once before
141 // waiting on change events
142 phosphor::network::dns::updater::processDNSEntries(DNS_ENTRY_FILE);
143
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530144 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530145}