blob: 082d5885174e1d41a3a2dc6b340730a62c62e5bb [file] [log] [blame]
Ratan Gupta82549cc2017-04-21 08:45:23 +05301#pragma once
2
Johnathan Mantey817012a2020-01-30 15:07:39 -08003#include "ipaddress.hpp"
4
Ratan Gupta3681a502017-06-17 19:20:04 +05305#include <ifaddrs.h>
William A. Kennington III0d7ce482019-01-30 17:14:23 -08006#include <netinet/in.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include <systemd/sd-event.h>
Ratan Gupta3681a502017-06-17 19:20:04 +05308
William A. Kennington III0d7ce482019-01-30 17:14:23 -08009#include <array>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <chrono>
William A. Kennington III0d7ce482019-01-30 17:14:23 -080011#include <cstddef>
Brad Bishopced35922018-02-21 12:57:21 -050012#include <functional>
Ratan Gupta82549cc2017-04-21 08:45:23 +053013#include <list>
Ratan Gupta82549cc2017-04-21 08:45:23 +053014#include <map>
Ratan Gupta3681a502017-06-17 19:20:04 +053015#include <memory>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070016#include <sdeventplus/clock.hpp>
17#include <sdeventplus/utility/timer.hpp>
Ratan Guptafd4b0f02017-09-16 06:01:24 +053018#include <set>
Patrick Venture189d44e2018-07-09 12:30:59 -070019#include <string>
William A. Kennington III0d7ce482019-01-30 17:14:23 -080020#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070021#include <vector>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053022
Ratan Gupta82549cc2017-04-21 08:45:23 +053023namespace phosphor
24{
25namespace network
26{
Ratan Guptae05083a2017-09-16 07:12:11 +053027
28using namespace std::chrono_literals;
Ratan Gupta16f12882017-09-22 18:26:11 +053029
30// wait for three seconds before restarting the networkd
31constexpr auto restartTimeout = 3s;
32
33// refresh the objets after five seconds as network
34// configuration takes 3-4 sec after systemd-networkd restart.
Ratan Gupta310a0b12017-11-15 17:40:24 +053035constexpr auto refreshTimeout = restartTimeout + 7s;
Ratan Guptae05083a2017-09-16 07:12:11 +053036
Ratan Guptabc886292017-07-25 18:29:57 +053037namespace systemd
38{
39namespace config
40{
41
42constexpr auto networkFilePrefix = "00-bmc-";
43constexpr auto networkFileSuffix = ".network";
44constexpr auto deviceFileSuffix = ".netdev";
45
Gunnar Mills57d9c502018-09-14 14:42:34 -050046} // namespace config
47} // namespace systemd
Ratan Gupta82549cc2017-04-21 08:45:23 +053048
49using IntfName = std::string;
50
Gunnar Mills57d9c502018-09-14 14:42:34 -050051struct AddrInfo
52{
Ratan Gupta82549cc2017-04-21 08:45:23 +053053 uint8_t addrType;
54 std::string ipaddress;
55 uint16_t prefix;
56};
57
Ratan Gupta3681a502017-06-17 19:20:04 +053058using Addr_t = ifaddrs*;
59
60struct AddrDeleter
61{
62 void operator()(Addr_t ptr) const
63 {
64 freeifaddrs(ptr);
65 }
66};
67
68using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
69
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053070/* Need a custom deleter for freeing up sd_event */
71struct EventDeleter
72{
73 void operator()(sd_event* event) const
74 {
William A. Kennington IIIdc2f1b62021-05-13 21:20:19 -070075 sd_event_unref(event);
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053076 }
77};
78using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
79
Gunnar Mills57d9c502018-09-14 14:42:34 -050080template <typename T>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053081using UniquePtr = std::unique_ptr<T, std::function<void(T*)>>;
Ratan Gupta3681a502017-06-17 19:20:04 +053082
William A. Kennington III0d7ce482019-01-30 17:14:23 -080083// Byte representations for common address types in network byte order
84using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
William A. Kennington III0d7ce482019-01-30 17:14:23 -080085
Ratan Gupta82549cc2017-04-21 08:45:23 +053086using AddrList = std::list<AddrInfo>;
87using IntfAddrMap = std::map<IntfName, AddrList>;
Ratan Guptafd4b0f02017-09-16 06:01:24 +053088using InterfaceList = std::set<IntfName>;
Ratan Gupta82549cc2017-04-21 08:45:23 +053089
William A. Kennington III3a70fa22018-09-20 18:48:20 -070090using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
91
Gunnar Mills57d9c502018-09-14 14:42:34 -050092} // namespace network
93} // namespace phosphor