blob: 562c3a15fc1f773177e3c556985973bdd262ab12 [file] [log] [blame]
Ratan Gupta82549cc2017-04-21 08:45:23 +05301#pragma once
2
Ratan Gupta3681a502017-06-17 19:20:04 +05303#include <ifaddrs.h>
William A. Kennington III0d7ce482019-01-30 17:14:23 -08004#include <netinet/in.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include <systemd/sd-event.h>
Ratan Gupta3681a502017-06-17 19:20:04 +05306
William A. Kennington III0d7ce482019-01-30 17:14:23 -08007#include <array>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <chrono>
William A. Kennington III0d7ce482019-01-30 17:14:23 -08009#include <cstddef>
Brad Bishopced35922018-02-21 12:57:21 -050010#include <functional>
Ratan Gupta82549cc2017-04-21 08:45:23 +053011#include <list>
Ratan Gupta82549cc2017-04-21 08:45:23 +053012#include <map>
Ratan Gupta3681a502017-06-17 19:20:04 +053013#include <memory>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070014#include <sdeventplus/clock.hpp>
15#include <sdeventplus/utility/timer.hpp>
Ratan Guptafd4b0f02017-09-16 06:01:24 +053016#include <set>
Patrick Venture189d44e2018-07-09 12:30:59 -070017#include <string>
William A. Kennington III0d7ce482019-01-30 17:14:23 -080018#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070019#include <vector>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053020
Ratan Gupta82549cc2017-04-21 08:45:23 +053021namespace phosphor
22{
23namespace network
24{
Ratan Guptae05083a2017-09-16 07:12:11 +053025
26using namespace std::chrono_literals;
Ratan Gupta16f12882017-09-22 18:26:11 +053027
28// wait for three seconds before restarting the networkd
29constexpr auto restartTimeout = 3s;
30
31// refresh the objets after five seconds as network
32// configuration takes 3-4 sec after systemd-networkd restart.
Ratan Gupta310a0b12017-11-15 17:40:24 +053033constexpr auto refreshTimeout = restartTimeout + 7s;
Ratan Guptae05083a2017-09-16 07:12:11 +053034
Ratan Guptabc886292017-07-25 18:29:57 +053035namespace systemd
36{
37namespace config
38{
39
40constexpr auto networkFilePrefix = "00-bmc-";
41constexpr auto networkFileSuffix = ".network";
42constexpr auto deviceFileSuffix = ".netdev";
43
Gunnar Mills57d9c502018-09-14 14:42:34 -050044} // namespace config
45} // namespace systemd
Ratan Gupta82549cc2017-04-21 08:45:23 +053046
47using IntfName = std::string;
48
Gunnar Mills57d9c502018-09-14 14:42:34 -050049struct AddrInfo
50{
Ratan Gupta82549cc2017-04-21 08:45:23 +053051 uint8_t addrType;
52 std::string ipaddress;
53 uint16_t prefix;
54};
55
Ratan Gupta3681a502017-06-17 19:20:04 +053056using Addr_t = ifaddrs*;
57
58struct AddrDeleter
59{
60 void operator()(Addr_t ptr) const
61 {
62 freeifaddrs(ptr);
63 }
64};
65
66using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
67
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053068/* Need a custom deleter for freeing up sd_event */
69struct EventDeleter
70{
71 void operator()(sd_event* event) const
72 {
73 event = sd_event_unref(event);
74 }
75};
76using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
77
Gunnar Mills57d9c502018-09-14 14:42:34 -050078template <typename T>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053079using UniquePtr = std::unique_ptr<T, std::function<void(T*)>>;
Ratan Gupta3681a502017-06-17 19:20:04 +053080
William A. Kennington III0d7ce482019-01-30 17:14:23 -080081// Byte representations for common address types in network byte order
82using InAddrAny = std::variant<struct in_addr, struct in6_addr>;
83using MacAddr = std::array<std::byte, 6>;
84
Ratan Gupta82549cc2017-04-21 08:45:23 +053085using AddrList = std::list<AddrInfo>;
86using IntfAddrMap = std::map<IntfName, AddrList>;
Ratan Guptafd4b0f02017-09-16 06:01:24 +053087using InterfaceList = std::set<IntfName>;
Ratan Gupta82549cc2017-04-21 08:45:23 +053088
William A. Kennington III3a70fa22018-09-20 18:48:20 -070089using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
90
Gunnar Mills57d9c502018-09-14 14:42:34 -050091} // namespace network
92} // namespace phosphor