Ratan Gupta | a54d8f8 | 2017-09-08 17:05:46 +0530 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 2 | |
William A. Kennington III | 0b111d4 | 2022-10-04 18:06:11 -0700 | [diff] [blame] | 3 | #ifdef SYNC_MAC_FROM_INVENTORY |
| 4 | #include "inventory_mac.hpp" |
| 5 | #endif |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 6 | #include "network_manager.hpp" |
| 7 | #include "rtnetlink_server.hpp" |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 8 | #include "types.hpp" |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 9 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 10 | #include <fmt/format.h> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 11 | |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
| 14 | #include <sdbusplus/server/manager.hpp> |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 15 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 16 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 17 | #include <sdeventplus/source/signal.hpp> |
William A. Kennington III | 52d7091 | 2022-11-21 18:40:00 -0800 | [diff] [blame] | 18 | #include <sdeventplus/utility/sdbus.hpp> |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 19 | #include <sdeventplus/utility/timer.hpp> |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 20 | #include <stdplus/pinned.hpp> |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 21 | #include <stdplus/signal.hpp> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 22 | |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 23 | #include <chrono> |
| 24 | |
William A. Kennington III | 5f1eb46 | 2019-02-12 19:47:51 -0800 | [diff] [blame] | 25 | constexpr char DEFAULT_OBJPATH[] = "/xyz/openbmc_project/network"; |
| 26 | |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 27 | namespace phosphor::network |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 28 | { |
| 29 | |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 30 | class TimerExecutor : public DelayedExecutor |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 31 | { |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 32 | private: |
| 33 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 34 | |
| 35 | public: |
William A. Kennington III | f5151aa | 2023-06-30 20:34:16 -0700 | [diff] [blame] | 36 | TimerExecutor(sdeventplus::Event& event, std::chrono::milliseconds delay) : |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 37 | delay(delay), timer(event, nullptr) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 38 | {} |
William A. Kennington III | c7cf25f | 2021-11-09 16:16:59 -0800 | [diff] [blame] | 39 | |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 40 | 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 III | f5151aa | 2023-06-30 20:34:16 -0700 | [diff] [blame] | 51 | std::chrono::milliseconds delay; |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 52 | Timer timer; |
| 53 | }; |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 54 | |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 55 | void termCb(sdeventplus::source::Signal& signal, const struct signalfd_siginfo*) |
| 56 | { |
Jagpal Singh Gill | 49f9d25 | 2023-04-17 15:15:39 -0700 | [diff] [blame] | 57 | lg2::notice("Received request to terminate, exiting"); |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 58 | signal.get_event().exit(0); |
| 59 | } |
| 60 | |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 61 | int main() |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 62 | { |
William A. Kennington III | 217bb3f | 2022-09-29 13:43:13 -0700 | [diff] [blame] | 63 | 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 III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 67 | stdplus::Pinned bus = sdbusplus::bus::new_default(); |
Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 68 | sdbusplus::server::manager_t objManager(bus, DEFAULT_OBJPATH); |
Ratan Gupta | cb7098d | 2017-04-14 17:46:05 +0530 | [diff] [blame] | 69 | |
William A. Kennington III | 9ede1b7 | 2022-11-21 01:59:28 -0800 | [diff] [blame] | 70 | stdplus::Pinned<TimerExecutor> reload(event, std::chrono::seconds(3)); |
William A. Kennington III | d40046b | 2023-06-30 20:36:11 -0700 | [diff] [blame^] | 71 | stdplus::Pinned<Manager> manager(bus, reload, DEFAULT_OBJPATH, |
| 72 | "/etc/systemd/network"); |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 73 | netlink::Server svr(event, manager); |
Vishwanatha Subbanna | 18891c6 | 2017-10-17 15:22:46 +0530 | [diff] [blame] | 74 | |
William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 75 | #ifdef SYNC_MAC_FROM_INVENTORY |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 76 | auto runtime = inventory::watch(bus, manager); |
Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 77 | #endif |
William A. Kennington III | bd649af | 2021-10-08 17:55:13 -0700 | [diff] [blame] | 78 | |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 79 | bus.request_name(DEFAULT_BUSNAME); |
William A. Kennington III | 52d7091 | 2022-11-21 18:40:00 -0800 | [diff] [blame] | 80 | return sdeventplus::utility::loopWithBus(event, bus); |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 81 | } |
| 82 | |
William A. Kennington III | de70ccf | 2022-11-20 17:18:01 -0800 | [diff] [blame] | 83 | } // namespace phosphor::network |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 84 | |
| 85 | int 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 III | be1af44 | 2022-11-12 01:49:21 -0800 | [diff] [blame] | 93 | fmt::print(stderr, "FAILED: {}", e.what()); |
| 94 | fflush(stderr); |
William A. Kennington III | 4fd52ae | 2022-09-30 13:49:39 -0700 | [diff] [blame] | 95 | return 1; |
| 96 | } |
Ratan Gupta | 8c83493 | 2017-04-14 16:30:24 +0530 | [diff] [blame] | 97 | } |