Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 3 | #include "ipaddress.hpp" |
| 4 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 5 | #include <ifaddrs.h> |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 6 | #include <netinet/in.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include <systemd/sd-event.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 8 | |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 9 | #include <array> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <chrono> |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 11 | #include <cstddef> |
Brad Bishop | ced3592 | 2018-02-21 12:57:21 -0500 | [diff] [blame] | 12 | #include <functional> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 13 | #include <list> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 14 | #include <map> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 15 | #include <memory> |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 16 | #include <sdeventplus/clock.hpp> |
| 17 | #include <sdeventplus/utility/timer.hpp> |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 18 | #include <set> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 19 | #include <string> |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 20 | #include <variant> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 21 | #include <vector> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 22 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace network |
| 26 | { |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 27 | |
| 28 | using namespace std::chrono_literals; |
Ratan Gupta | 16f1288 | 2017-09-22 18:26:11 +0530 | [diff] [blame] | 29 | |
| 30 | // wait for three seconds before restarting the networkd |
| 31 | constexpr auto restartTimeout = 3s; |
| 32 | |
| 33 | // refresh the objets after five seconds as network |
| 34 | // configuration takes 3-4 sec after systemd-networkd restart. |
Ratan Gupta | 310a0b1 | 2017-11-15 17:40:24 +0530 | [diff] [blame] | 35 | constexpr auto refreshTimeout = restartTimeout + 7s; |
Ratan Gupta | e05083a | 2017-09-16 07:12:11 +0530 | [diff] [blame] | 36 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 37 | namespace systemd |
| 38 | { |
| 39 | namespace config |
| 40 | { |
| 41 | |
| 42 | constexpr auto networkFilePrefix = "00-bmc-"; |
| 43 | constexpr auto networkFileSuffix = ".network"; |
| 44 | constexpr auto deviceFileSuffix = ".netdev"; |
| 45 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 46 | } // namespace config |
| 47 | } // namespace systemd |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 48 | |
| 49 | using IntfName = std::string; |
| 50 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 51 | struct AddrInfo |
| 52 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 53 | uint8_t addrType; |
| 54 | std::string ipaddress; |
| 55 | uint16_t prefix; |
| 56 | }; |
| 57 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 58 | using Addr_t = ifaddrs*; |
| 59 | |
| 60 | struct AddrDeleter |
| 61 | { |
| 62 | void operator()(Addr_t ptr) const |
| 63 | { |
| 64 | freeifaddrs(ptr); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>; |
| 69 | |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 70 | /* Need a custom deleter for freeing up sd_event */ |
| 71 | struct EventDeleter |
| 72 | { |
| 73 | void operator()(sd_event* event) const |
| 74 | { |
William A. Kennington III | dc2f1b6 | 2021-05-13 21:20:19 -0700 | [diff] [blame] | 75 | sd_event_unref(event); |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 76 | } |
| 77 | }; |
| 78 | using EventPtr = std::unique_ptr<sd_event, EventDeleter>; |
| 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | template <typename T> |
Ratan Gupta | 0f9dc1b | 2017-09-03 17:57:50 +0530 | [diff] [blame] | 81 | using UniquePtr = std::unique_ptr<T, std::function<void(T*)>>; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 82 | |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 83 | // Byte representations for common address types in network byte order |
| 84 | using InAddrAny = std::variant<struct in_addr, struct in6_addr>; |
William A. Kennington III | 0d7ce48 | 2019-01-30 17:14:23 -0800 | [diff] [blame] | 85 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 86 | using AddrList = std::list<AddrInfo>; |
| 87 | using IntfAddrMap = std::map<IntfName, AddrList>; |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 88 | using InterfaceList = std::set<IntfName>; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame] | 89 | |
William A. Kennington III | 3a70fa2 | 2018-09-20 18:48:20 -0700 | [diff] [blame] | 90 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 91 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 92 | } // namespace network |
| 93 | } // namespace phosphor |