blob: 36694296ff0ac83be6d194e86d310d8d407f87c9 [file] [log] [blame]
Ratan Guptaa54d8f82017-09-08 17:05:46 +05301#include "config.h"
Gunnar Mills57d9c502018-09-14 14:42:34 -05002
William A. Kennington III0b111d42022-10-04 18:06:11 -07003#ifdef SYNC_MAC_FROM_INVENTORY
4#include "inventory_mac.hpp"
5#endif
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05306#include "network_manager.hpp"
7#include "rtnetlink_server.hpp"
William A. Kennington III3a70fa22018-09-20 18:48:20 -07008#include "types.hpp"
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +05309
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070010#include <fmt/format.h>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053011
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080012#include <chrono>
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070013#include <phosphor-logging/lg2.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053014#include <sdbusplus/bus.hpp>
15#include <sdbusplus/server/manager.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080016#include <sdeventplus/clock.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070017#include <sdeventplus/event.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070018#include <sdeventplus/source/signal.hpp>
William A. Kennington III52d70912022-11-21 18:40:00 -080019#include <sdeventplus/utility/sdbus.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080020#include <sdeventplus/utility/timer.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080021#include <stdplus/pinned.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070022#include <stdplus/signal.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053023
William A. Kennington III5f1eb462019-02-12 19:47:51 -080024constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
25
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080026namespace phosphor::network
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053027{
28
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080029class TimerExecutor : public DelayedExecutor
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080030{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080031 private:
32 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
33
34 public:
35 TimerExecutor(sdeventplus::Event& event, std::chrono::seconds delay) :
36 delay(delay), timer(event, nullptr)
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080037 {
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080038 }
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080039
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080040 void schedule() override
41 {
42 timer.restartOnce(delay);
43 }
44
45 void setCallback(fu2::unique_function<void()>&& cb) override
46 {
47 timer.set_callback([cb = std::move(cb)](Timer&) mutable { cb(); });
48 }
49
50 private:
51 std::chrono::seconds delay;
52 Timer timer;
53};
Ratan Gupta16f12882017-09-22 18:26:11 +053054
William A. Kennington III217bb3f2022-09-29 13:43:13 -070055void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
56{
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070057 lg2::notice("Received request to terminate, exiting");
William A. Kennington III217bb3f2022-09-29 13:43:13 -070058 signal.get_event().exit(0);
59}
60
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070061int main()
Ratan Gupta8c834932017-04-14 16:30:24 +053062{
William A. Kennington III217bb3f2022-09-29 13:43:13 -070063 auto event = sdeventplus::Event::get_default();
64 stdplus::signal::block(SIGTERM);
65 sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
66
William A. Kennington III9ede1b72022-11-21 01:59:28 -080067 stdplus::Pinned bus = sdbusplus::bus::new_default();
Patrick Williamsc38b0712022-07-22 19:26:54 -050068 sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
Ratan Guptacb7098d2017-04-14 17:46:05 +053069
William A. Kennington III9ede1b72022-11-21 01:59:28 -080070 stdplus::Pinned<TimerExecutor> reload(event, std::chrono::seconds(3));
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080071 Manager manager(bus, reload, DEFAULT_OBJPATH, "/etc/systemd/network");
72 netlink::Server svr(event, manager);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +053073
William A. Kennington III6f39c5e2021-05-13 18:39:23 -070074#ifdef SYNC_MAC_FROM_INVENTORY
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080075 auto runtime = inventory::watch(bus, manager);
Manojkiran Edacc099a82020-05-11 14:25:16 +053076#endif
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070077
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080078 bus.request_name(DEFAULT_BUSNAME);
William A. Kennington III52d70912022-11-21 18:40:00 -080079 return sdeventplus::utility::loopWithBus(event, bus);
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070080}
81
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080082} // namespace phosphor::network
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070083
84int main(int /*argc*/, char** /*argv*/)
85{
86 try
87 {
88 return phosphor::network::main();
89 }
90 catch (const std::exception& e)
91 {
William A. Kennington IIIbe1af442022-11-12 01:49:21 -080092 fmt::print(stderr, "FAILED: {}", e.what());
93 fflush(stderr);
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070094 return 1;
95 }
Ratan Gupta8c834932017-04-14 16:30:24 +053096}