blob: 3af3a84404e61a514047c97a69be5d99a3fd6c86 [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
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070010#include <phosphor-logging/lg2.hpp>
Ratan Guptacb7098d2017-04-14 17:46:05 +053011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/manager.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080013#include <sdeventplus/clock.hpp>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070014#include <sdeventplus/event.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070015#include <sdeventplus/source/signal.hpp>
William A. Kennington III52d70912022-11-21 18:40:00 -080016#include <sdeventplus/utility/sdbus.hpp>
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080017#include <sdeventplus/utility/timer.hpp>
William A. Kennington III9ede1b72022-11-21 01:59:28 -080018#include <stdplus/pinned.hpp>
William A. Kennington IIIb7401962023-07-21 16:36:11 -070019#include <stdplus/print.hpp>
William A. Kennington III217bb3f2022-09-29 13:43:13 -070020#include <stdplus/signal.hpp>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053021
Patrick Williams89d734b2023-05-10 07:50:25 -050022#include <chrono>
23
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:
William A. Kennington IIIf5151aa2023-06-30 20:34:16 -070035 TimerExecutor(sdeventplus::Event& event, std::chrono::milliseconds delay) :
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080036 delay(delay), timer(event, nullptr)
Patrick Williams89d734b2023-05-10 07:50:25 -050037 {}
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080038
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080039 void schedule() override
40 {
41 timer.restartOnce(delay);
42 }
43
44 void setCallback(fu2::unique_function<void()>&& cb) override
45 {
46 timer.set_callback([cb = std::move(cb)](Timer&) mutable { cb(); });
47 }
48
49 private:
William A. Kennington IIIf5151aa2023-06-30 20:34:16 -070050 std::chrono::milliseconds delay;
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080051 Timer timer;
52};
Ratan Gupta16f12882017-09-22 18:26:11 +053053
William A. Kennington III217bb3f2022-09-29 13:43:13 -070054void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*)
55{
Jagpal Singh Gill49f9d252023-04-17 15:15:39 -070056 lg2::notice("Received request to terminate, exiting");
William A. Kennington III217bb3f2022-09-29 13:43:13 -070057 signal.get_event().exit(0);
58}
59
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070060int main()
Ratan Gupta8c834932017-04-14 16:30:24 +053061{
William A. Kennington III217bb3f2022-09-29 13:43:13 -070062 auto event = sdeventplus::Event::get_default();
63 stdplus::signal::block(SIGTERM);
64 sdeventplus::source::Signal(event, SIGTERM, termCb).set_floating(true);
65
William A. Kennington III9ede1b72022-11-21 01:59:28 -080066 stdplus::Pinned bus = sdbusplus::bus::new_default();
Patrick Williamsc38b0712022-07-22 19:26:54 -050067 sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH);
Ratan Guptacb7098d2017-04-14 17:46:05 +053068
William A. Kennington III9ede1b72022-11-21 01:59:28 -080069 stdplus::Pinned<TimerExecutor> reload(event, std::chrono::seconds(3));
William A. Kennington IIId40046b2023-06-30 20:36:11 -070070 stdplus::Pinned<Manager> manager(bus, reload, DEFAULT_OBJPATH,
71 "/etc/systemd/network");
William A. Kennington IIIde70ccf2022-11-20 17:18:01 -080072 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 IIIb7401962023-07-21 16:36:11 -070092 stdplus::print(stderr, "FAILED: {}", e.what());
William A. Kennington IIIbe1af442022-11-12 01:49:21 -080093 fflush(stderr);
William A. Kennington III4fd52ae2022-09-30 13:49:39 -070094 return 1;
95 }
Ratan Gupta8c834932017-04-14 16:30:24 +053096}