blob: 1b85bfaf8f2c6114136c155d625fc31568a30132 [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
William A. Kennington III483e7772019-02-12 19:03:15 -080026constexpr char NETWORK_STATE_FILE[] = "/run/systemd/netif/state";
27constexpr char NETWORK_CONF_DIR[] = "/etc/systemd/network";
28
William A. Kennington III5f1eb462019-02-12 19:47:51 -080029constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
30
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053031namespace phosphor
32{
33namespace network
34{
35
36std::unique_ptr<phosphor::network::Manager> manager = nullptr;
William A. Kennington III3a70fa22018-09-20 18:48:20 -070037std::unique_ptr<Timer> refreshObjectTimer = nullptr;
38std::unique_ptr<Timer> restartTimer = nullptr;
Ratan Guptaa54d8f82017-09-08 17:05:46 +053039
Ratan Gupta16f12882017-09-22 18:26:11 +053040/** @brief refresh the network objects. */
Ratan Guptaa54d8f82017-09-08 17:05:46 +053041void refreshObjects()
42{
Ratan Gupta16f12882017-09-22 18:26:11 +053043 if (manager)
44 {
Ratan Gupta310a0b12017-11-15 17:40:24 +053045 log<level::INFO>("Refreshing the objects.");
Ratan Gupta16f12882017-09-22 18:26:11 +053046 manager->createChildObjects();
Ratan Gupta310a0b12017-11-15 17:40:24 +053047 log<level::INFO>("Refreshing complete.");
Ratan Gupta16f12882017-09-22 18:26:11 +053048 }
49}
50
51/** @brief restart the systemd networkd. */
52void restartNetwork()
53{
Ratan Gupta895f9e52018-11-26 20:57:34 +053054 if (manager)
55 {
56 manager->restartSystemdUnit("systemd-networkd.service");
57 }
Ratan Guptaa54d8f82017-09-08 17:05:46 +053058}
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053059
Ratan Gupta16f12882017-09-22 18:26:11 +053060void initializeTimers()
61{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070062 auto event = sdeventplus::Event::get_default();
63 refreshObjectTimer =
64 std::make_unique<Timer>(event, std::bind(refreshObjects));
65 restartTimer = std::make_unique<Timer>(event, std::bind(restartNetwork));
Ratan Gupta16f12882017-09-22 18:26:11 +053066}
67
William A. Kennington III3a70fa22018-09-20 18:48:20 -070068} // namespace network
69} // namespace phosphor
70
Ratan Guptaf6657382017-11-10 17:58:17 +053071void createNetLinkSocket(phosphor::Descriptor& smartSock)
72{
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +053074 auto fd = socket(PF_NETLINK, SOCK_RAW | SOCK_NONBLOCK, NETLINK_ROUTE);
75 if (fd < 0)
76 {
Ratan Guptaf6657382017-11-10 17:58:17 +053077 log<level::ERR>("Unable to create the net link socket",
William A. Kennington III798c2812018-09-27 12:07:39 -070078 entry("ERRNO=%d", errno));
Ratan Guptaf6657382017-11-10 17:58:17 +053079 elog<InternalFailure>();
80 }
81 smartSock.set(fd);
82}
83
Gunnar Mills57d9c502018-09-14 14:42:34 -050084int main(int argc, char* argv[])
Ratan Gupta8c834932017-04-14 16:30:24 +053085{
William A. Kennington III3a70fa22018-09-20 18:48:20 -070086 phosphor::network::initializeTimers();
Ratan Guptaa54d8f82017-09-08 17:05:46 +053087
Ratan Guptacb7098d2017-04-14 17:46:05 +053088 auto bus = sdbusplus::bus::new_default();
89
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053090 // Need sd_event to watch for OCC device errors
91 sd_event* event = nullptr;
92 auto r = sd_event_default(&event);
93 if (r < 0)
94 {
95 log<level::ERR>("Error creating a default sd_event handler");
96 return r;
97 }
98
99 phosphor::network::EventPtr eventPtr{event};
100 event = nullptr;
101
102 // Attach the bus to sd_event to service user requests
103 bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
104
Ratan Guptacb7098d2017-04-14 17:46:05 +0530105 // Add sdbusplus Object Manager for the 'root' path of the network manager.
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800106 sdbusplus::server::manager::manager objManager(bus, DEFAULT_OBJPATH);
107 bus.request_name(DEFAULT_BUSNAME);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530108
Gunnar Mills57d9c502018-09-14 14:42:34 -0500109 phosphor::network::manager = std::make_unique<phosphor::network::Manager>(
William A. Kennington III5f1eb462019-02-12 19:47:51 -0800110 bus, DEFAULT_OBJPATH, NETWORK_CONF_DIR);
Ratan Guptacb7098d2017-04-14 17:46:05 +0530111
Ratan Guptab610caf2017-09-19 09:33:51 +0530112 // create the default network files if the network file
113 // is not there for any interface.
114 // Parameter false means don't create the network
115 // files forcefully.
116 if (phosphor::network::manager->createDefaultNetworkFiles(false))
117 {
118 // if files created restart the network.
119 // don't need to call the create child objects as eventhandler
120 // will create it.
Ratan Gupta16f12882017-09-22 18:26:11 +0530121 phosphor::network::restartNetwork();
Ratan Guptab610caf2017-09-19 09:33:51 +0530122 }
Ratan Guptae9629412017-12-21 08:20:25 +0530123 else
124 {
125 // this will add the additional fixes which is needed
126 // in the existing network file.
127 phosphor::network::manager->writeToConfigurationFile();
Ratan Gupta2939f182018-02-14 23:14:54 +0530128 // whenever the configuration file gets written it restart
129 // the network which creates the network objects
Ratan Guptae9629412017-12-21 08:20:25 +0530130 }
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530131
Gunnar Mills57d9c502018-09-14 14:42:34 -0500132 // RtnetLink socket
Ratan Guptaf6657382017-11-10 17:58:17 +0530133 phosphor::Descriptor smartSock;
134 createNetLinkSocket(smartSock);
135
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530136 // RTNETLINK event handler
Ratan Guptaf6657382017-11-10 17:58:17 +0530137 phosphor::network::rtnetlink::Server svr(eventPtr, smartSock);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530138
139 // DNS entry handler
Gunnar Mills57d9c502018-09-14 14:42:34 -0500140 phosphor::network::inotify::Watch watch(
William A. Kennington III483e7772019-02-12 19:03:15 -0800141 eventPtr, NETWORK_STATE_FILE,
Gunnar Mills57d9c502018-09-14 14:42:34 -0500142 std::bind(&phosphor::network::dns::updater::processDNSEntries,
143 std::placeholders::_1));
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530144
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
William A. Kennington III483e7772019-02-12 19:03:15 -0800149 phosphor::network::dns::updater::processDNSEntries(NETWORK_STATE_FILE);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530150
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +0530151 sd_event_loop(eventPtr.get());
Ratan Gupta8c834932017-04-14 16:30:24 +0530152}