blob: fedb73d95a623d9eeb154d4110f98682f399ca38 [file] [log] [blame]
Ratan Guptaa54d8f82017-09-08 17:05:46 +05301#include "config.h"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05303#include "network_manager.hpp"
4#include "rtnetlink_server.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07005#include "types.hpp"
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +05306#include "watch.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
William A. Kennington III3a70fa22018-09-20 18:48:20 -070010#include <functional>
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>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070016#include <sdeventplus/event.hpp>
Ratan Guptaf6657382017-11-10 17:58:17 +053017#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053018
William A. Kennington III09095f82018-09-27 12:05:12 -070019using phosphor::logging::elog;
20using phosphor::logging::entry;
21using phosphor::logging::level;
22using phosphor::logging::log;
23using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24
William A. Kennington III483e7772019-02-12 19:03:15 -080025constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network";
26
William A. Kennington III5f1eb462019-02-12 19:47:51 -080027constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
28
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053029namespace phosphor
30{
31namespace network
32{
33
34std::unique_ptr<phosphor::network::Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070035std::unique_ptr<Timer> refreshObjectTimer = nullptr;
36std::unique_ptr<Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053037
Ratan Gupta16f12882017-09-22 18:26:11 +053038/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053039void refreshObjects()
40{
Ratan Gupta16f12882017-09-22 18:26:11 +053041 if (manager)
42 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053043 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053044 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053045 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053046 }
47}
48
49/** @brief restart the systemd networkd. */
50void restartNetwork()
51{
Ratan Gupta895f9e52018-11-26 20:57:34 +053052 if (manager)
53 {
54 manager->restartSystemdUnit("systemd-networkd.service");
55 }
Ratan Guptaa54d8f82017-09-08 17:05:46 +053056}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053057
Ratan Gupta16f12882017-09-22 18:26:11 +053058void initializeTimers()
59{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070060 auto event = sdeventplus::Event::get_default();
61 refreshObjectTimer =
62 std::make_unique<Timer>(event, std::bind(refreshObjects));
63 restartTimer = std::make_unique<Timer>(event, std::bind(restartNetwork));
Ratan Gupta16f12882017-09-22 18:26:11 +053064}
65
William A. Kennington III3a70fa22018-09-20 18:48:20 -070066} // namespace network
67} // namespace phosphor
68
Ratan Guptaf6657382017-11-10 17:58:17 +053069void createNetLinkSocket(phosphor::Descriptor& smartSock)
70{
Gunnar Mills57d9c502018-09-14 14:42:34 -050071 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +053072 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
73 if (fd < 0)
74 {
Ratan Guptaf6657382017-11-10 17:58:17 +053075 log<level::ERR>("Unable to create the net link socket",
William A. Kennington III798c2812018-09-27 12:07:39 -070076 entry("ERRNO=%d", errno));
Ratan Guptaf6657382017-11-10 17:58:17 +053077 elog<InternalFailure>();
78 }
79 smartSock.set(fd);
80}
81
Gunnar Mills57d9c502018-09-14 14:42:34 -050082int main(int argc, char* argv[])
Ratan Gupta8c834932017-04-14 16:30:24 +053083{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070084 phosphor::network::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.
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800104 sdbusplus::server::manager::manager objManager(bus, DEFAULT_OBJPATH);
105 bus.request_name(DEFAULT_BUSNAME);
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>(
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800108 bus, DEFAULT_OBJPATH, 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
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530137 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530138}