blob: b4150a13274b93cf4496fe50c57ab5532bf1a20a [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
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070012#include <phosphor-logging/lg2.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053013#include <sdbusplus/bus.hpp>
14#include <sdbusplus/server/manager.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080015#include <sdeventplus/clock.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070016#include <sdeventplus/event.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070017#include <sdeventplus/source/signal.hpp>
William A. Kennington III52d70912022-11-21 18:40:00 -080018#include <sdeventplus/utility/sdbus.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080019#include <sdeventplus/utility/timer.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080020#include <stdplus/pinned.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070021#include <stdplus/signal.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053022
Patrick Williams89d734b2023-05-10 07:50:25 -050023#include <chrono>
24
William A. Kennington III5f1eb462019-02-12 19:47:51 -080025constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network";
26
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080027namespace phosphor::network
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053028{
29
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080030class TimerExecutor : public DelayedExecutor
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080031{
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080032 private:
33 using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
34
35 public:
William A. Kennington IIIf5151aa2023-06-30 20:34:16 -070036 TimerExecutor(sdeventplus::Event& event, std::chrono::milliseconds delay) :
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080037 delay(delay), timer(event, nullptr)
Patrick Williams89d734b2023-05-10 07:50:25 -050038 {}
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:
William A. Kennington IIIf5151aa2023-06-30 20:34:16 -070051 std::chrono::milliseconds delay;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080052 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 IIId40046b2023-06-30 20:36:11 -070071 stdplus::Pinned<Manager> manager(bus, reload, DEFAULT_OBJPATH,
72 "/etc/systemd/network");
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080073 netlink::Server svr(event, manager);
Vishwanatha Subbanna18891c62017-10-17 15:22:46 +053074
William A. Kennington III6f39c5e2021-05-13 18:39:23 -070075#ifdef SYNC_MAC_FROM_INVENTORY
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080076 auto runtime = inventory::watch(bus, manager);
Manojkiran Edacc099a82020-05-11 14:25:16 +053077#endif
William A. Kennington IIIbd649af2021-10-08 17:55:13 -070078
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080079 bus.request_name(DEFAULT_BUSNAME);
William A. Kennington III52d70912022-11-21 18:40:00 -080080 return sdeventplus::utility::loopWithBus(event, bus);
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070081}
82
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080083} // namespace phosphor::network
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070084
85int main(int /*argc*/, char** /*argv*/)
86{
87 try
88 {
89 return phosphor::network::main();
90 }
91 catch (const std::exception& e)
92 {
William A. Kennington IIIbe1af442022-11-12 01:49:21 -080093 fmt::print(stderr, "FAILED: {}", e.what());
94 fflush(stderr);
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070095 return 1;
96 }
Ratan Gupta8c834932017-04-14 16:30:24 +053097}