blob: 3c0b6d9f0529cbb3c4213cf8f532fd6c7a475daf [file] [log] [blame]
Ratan Gupta82549cc2017-04-21 08:45:23 +05301#pragma once
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07002#include <fmt/core.h>
3#include <net/ethernet.h>
William A. Kennington III0d7ce482019-01-30 17:14:23 -08004#include <netinet/in.h>
Ratan Gupta3681a502017-06-17 19:20:04 +05305
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -07006#include <algorithm>
7#include <array>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <chrono>
William A. Kennington III71de63a2022-11-08 10:50:54 -08009#include <numeric>
William A. Kennington III3a70fa22018-09-20 18:48:20 -070010#include <sdeventplus/clock.hpp>
11#include <sdeventplus/utility/timer.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <string>
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070013#include <string_view>
14#include <type_traits>
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070015#include <unordered_map>
Willy Tuf7dce2e2022-10-07 05:48:08 +000016#include <unordered_set>
William A. Kennington III0d7ce482019-01-30 17:14:23 -080017#include <variant>
Ratan Gupta0f9dc1b2017-09-03 17:57:50 +053018
Ratan Gupta82549cc2017-04-21 08:45:23 +053019namespace phosphor
20{
21namespace network
22{
Ratan Guptae05083a2017-09-16 07:12:11 +053023
24using namespace std::chrono_literals;
Ratan Gupta16f12882017-09-22 18:26:11 +053025
William A. Kennington IIIc7cf25f2021-11-09 16:16:59 -080026// wait for three seconds before reloading systemd-networkd
27constexpr auto reloadTimeout = 3s;
Ratan Gupta16f12882017-09-22 18:26:11 +053028
William A. Kennington IIId41db382021-11-09 20:42:29 -080029// refresh the objets after four seconds as network
30// configuration takes 3-4 sec to reconfigure at most.
31constexpr auto refreshTimeout = 4s;
Ratan Guptae05083a2017-09-16 07:12:11 +053032
William A. Kennington III0d7ce482019-01-30 17:14:23 -080033// Byte representations for common address types in network byte order
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -070034using InAddrAny = std::variant<in_addr, in6_addr>;
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -080035class IfAddr
36{
37 private:
38 InAddrAny addr;
39 uint8_t pfx;
40
41 static void invalidPfx(uint8_t pfx);
42
43 public:
44 constexpr IfAddr() : addr({}), pfx(0)
45 {
46 }
47
48 constexpr IfAddr(InAddrAny addr, uint8_t pfx) : addr(addr), pfx(pfx)
49 {
50 std::visit(
51 [pfx](auto v) {
52 if (sizeof(v) * 8 < pfx)
53 {
54 invalidPfx(pfx);
55 }
56 },
57 addr);
58 }
59
60 constexpr auto getAddr() const
61 {
62 return addr;
63 }
64
65 constexpr auto getPfx() const
66 {
67 return pfx;
68 }
69
70 constexpr bool operator==(phosphor::network::IfAddr rhs) const noexcept
71 {
72 return addr == rhs.addr && pfx == rhs.pfx;
73 }
74};
William A. Kennington III0d7ce482019-01-30 17:14:23 -080075
William A. Kennington III3a70fa22018-09-20 18:48:20 -070076using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
77
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070078struct string_hash : public std::hash<std::string_view>
79{
80 using is_transparent = void;
81};
82template <typename V>
83using string_umap =
84 std::unordered_map<std::string, V, string_hash, std::equal_to<>>;
William A. Kennington III96444792022-10-05 15:16:22 -070085using string_uset =
86 std::unordered_set<std::string, string_hash, std::equal_to<>>;
William A. Kennington IIIdd9ef812022-10-05 02:08:02 -070087
William A. Kennington III3e471c52022-10-27 19:46:07 -070088constexpr std::size_t hash_multi() noexcept
William A. Kennington III991a8e82022-10-11 15:02:47 -070089{
90 return 0;
91}
92
93template <typename T, typename... Args>
William A. Kennington IIIbecda1a2022-11-03 12:44:59 -070094constexpr std::size_t hash_multi(const T& v, const Args&... args) noexcept
William A. Kennington III991a8e82022-10-11 15:02:47 -070095{
96 const std::size_t seed = hash_multi(args...);
97 return seed ^ (std::hash<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
98}
99
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700100namespace detail
101{
102
William A. Kennington III71de63a2022-11-08 10:50:54 -0800103template <typename T, uint8_t size = sizeof(T)>
104struct BswapAlign
105{
106 using type = T;
107};
108
109template <typename T>
110struct BswapAlign<T, 2>
111{
112 using type alignas(uint16_t) = T;
113};
114
115template <typename T>
116struct BswapAlign<T, 4>
117{
118 using type alignas(uint32_t) = T;
119};
120
121template <typename T>
122struct BswapAlign<T, 8>
123{
124 using type alignas(uint64_t) = T;
125};
126
127template <typename T>
128constexpr T bswapInt(typename BswapAlign<T>::type n) noexcept
129{
130 static_assert(std::is_trivially_copyable_v<T>);
131 if constexpr (sizeof(T) == 2)
132 {
133 reinterpret_cast<uint16_t&>(n) =
134 __builtin_bswap16(reinterpret_cast<uint16_t&>(n));
135 }
136 else if constexpr (sizeof(T) == 4)
137 {
138 reinterpret_cast<uint32_t&>(n) =
139 __builtin_bswap32(reinterpret_cast<uint32_t&>(n));
140 }
141 else if constexpr (sizeof(T) == 8)
142 {
143 reinterpret_cast<uint64_t&>(n) =
144 __builtin_bswap64(reinterpret_cast<uint64_t&>(n));
145 }
146 else
147 {
148 auto b = reinterpret_cast<std::byte*>(&n);
149 std::reverse(b, b + sizeof(n));
150 }
151 return n;
152}
153
154} // namespace detail
155
156template <typename T>
157constexpr T bswap(T n) noexcept
158{
159 return detail::bswapInt<T>(n);
160}
161
162template <typename T>
163constexpr T hton(T n) noexcept
164{
165 if constexpr (std::endian::native == std::endian::big)
166 {
167 return n;
168 }
169 else if constexpr (std::endian::native == std::endian::little)
170 {
171 return bswap(n);
172 }
173 else
174 {
175 static_assert(std::is_same_v<T, void>);
176 }
177}
178
179template <typename T>
180constexpr T ntoh(T n) noexcept
181{
182 return hton(n);
183}
184
185namespace detail
186{
William A. Kennington III238ef992022-11-03 12:47:49 -0700187inline constexpr auto charLookup = []() {
188 std::array<int8_t, 256> ret;
189 std::fill(ret.begin(), ret.end(), -1);
190 for (int8_t i = 0; i < 10; ++i)
191 {
192 ret[i + '0'] = i;
193 }
194 for (int8_t i = 0; i < 26; ++i)
195 {
196 ret[i + 'A'] = i + 10;
197 ret[i + 'a'] = i + 10;
198 }
199 return ret;
200}();
201}
202
203template <typename T, uint8_t base>
204struct DecodeInt
205{
206 static_assert(base > 1 && base <= 36);
207 static_assert(std::is_unsigned_v<T>);
208
209 constexpr T operator()(std::string_view str) const
210 {
211 if (str.empty())
212 {
213 throw std::invalid_argument("Empty Str");
214 }
215 constexpr auto max = std::numeric_limits<T>::max();
216 auto ret =
217 std::accumulate(str.begin(), str.end(), T{}, [&](T r, char c) {
218 auto v = detail::charLookup[c];
219 if (v < 0 || v >= base)
220 {
221 throw std::invalid_argument("Invalid numeral");
222 }
223 if constexpr (std::popcount(base) == 1)
224 {
225 constexpr auto shift = std::countr_zero(base);
226 constexpr auto maxshift = max >> shift;
227 if (r > maxshift)
228 {
229 throw std::overflow_error("Integer Decode");
230 }
231 return (r << shift) | v;
232 }
233 else
234 {
235 constexpr auto maxbase = max / base;
236 if (r > maxbase)
237 {
238 throw std::overflow_error("Integer Decode");
239 }
240 r *= base;
241 if (max - v < r)
242 {
243 throw std::overflow_error("Integer Decode");
244 }
245 return r + v;
246 }
247 });
248 return ret;
249 }
250};
251
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -0700252template <typename T>
253struct ToAddr
254{
255};
256
257template <>
258struct ToAddr<ether_addr>
259{
260 constexpr ether_addr operator()(std::string_view str) const
261 {
262 constexpr DecodeInt<uint8_t, 16> di;
263 ether_addr ret;
264 if (str.size() == 12 && str.find(":") == str.npos)
265 {
266 for (size_t i = 0; i < 6; ++i)
267 {
268 ret.ether_addr_octet[i] = di(str.substr(i * 2, 2));
269 }
270 }
271 else
272 {
273 for (size_t i = 0; i < 5; ++i)
274 {
275 auto loc = str.find(":");
276 ret.ether_addr_octet[i] = di(str.substr(0, loc));
277 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
278 if (str.empty())
279 {
280 throw std::invalid_argument("Missing mac data");
281 }
282 }
283 ret.ether_addr_octet[5] = di(str);
284 }
285 return ret;
286 }
287};
288
William A. Kennington III238ef992022-11-03 12:47:49 -0700289namespace detail
290{
William A. Kennington III71de63a2022-11-08 10:50:54 -0800291
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700292template <typename T>
293constexpr bool vcontains() noexcept
294{
295 return false;
296}
297
298template <typename T, typename V, typename... Vs>
299constexpr bool vcontains() noexcept
300{
301 return vcontains<T, Vs...>() || std::is_same_v<T, V>;
302}
303
304template <typename T, typename... Types>
305constexpr std::enable_if_t<vcontains<T, Types...>(), bool>
306 veq(T t, std::variant<Types...> v) noexcept
307{
308 return std::visit(
309 [t](auto v) {
310 if constexpr (std::is_same_v<T, decltype(v)>)
311 {
312 return v == t;
313 }
314 else
315 {
316 return false;
317 }
318 },
319 v);
320}
321
322template <typename T>
323struct AddrBufMaker
324{
325};
326
327template <>
328struct AddrBufMaker<ether_addr>
329{
330 public:
331 std::string_view operator()(ether_addr val) noexcept;
332
333 private:
334 std::array<char, /*octet*/ 2 * /*octets*/ 6 + /*seps*/ 5> buf;
335};
336
337template <>
338struct AddrBufMaker<in_addr>
339{
340 public:
341 std::string_view operator()(in_addr val) noexcept;
342
343 private:
344 std::array<char, /*octet*/ 3 * /*octets*/ 4 + /*seps*/ 3> buf;
345};
346
347template <>
348struct AddrBufMaker<in6_addr>
349{
350 public:
351 std::string_view operator()(in6_addr val) noexcept;
352
353 private:
354 std::array<char, /*hextet*/ 4 * /*hextets*/ 8 + /*seps*/ 7> buf;
355};
356
357template <typename BufMaker>
358struct FormatFromBuf
359{
360 private:
361 fmt::formatter<std::string_view> formatter;
362
363 public:
364 template <typename ParseContext>
365 constexpr auto parse(ParseContext& ctx)
366 {
367 return ctx.begin();
368 }
369
370 template <typename FormatContext>
371 auto format(auto v, FormatContext& ctx) const
372 {
373 return formatter.format(BufMaker{}(v), ctx);
374 }
375};
376} // namespace detail
Gunnar Mills57d9c502018-09-14 14:42:34 -0500377} // namespace network
378} // namespace phosphor
William A. Kennington III3e471c52022-10-27 19:46:07 -0700379
380template <typename... Ts>
381struct std::hash<std::tuple<Ts...>>
382{
383 constexpr auto operator()(const std::tuple<Ts...>& t) const noexcept
384 {
385 return std::apply(phosphor::network::hash_multi<Ts...>, t);
386 }
387};
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700388
William A. Kennington III653114f2022-11-01 22:34:04 -0700389template <>
390struct std::hash<in_addr>
391{
392 std::size_t operator()(in_addr addr) const noexcept;
393};
394
395template <>
396struct std::hash<in6_addr>
397{
398 std::size_t operator()(in6_addr addr) const noexcept;
399};
400
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800401template <>
402struct std::hash<phosphor::network::IfAddr>
403{
404 std::size_t operator()(phosphor::network::IfAddr addr) const noexcept;
405};
406
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700407namespace fmt
408{
409template <>
410struct formatter<ether_addr>
411 : phosphor::network::detail::FormatFromBuf<
412 phosphor::network::detail::AddrBufMaker<ether_addr>>
413{
414};
415template <>
416struct formatter<in_addr>
417 : phosphor::network::detail::FormatFromBuf<
418 phosphor::network::detail::AddrBufMaker<in_addr>>
419{
420};
421template <>
422struct formatter<in6_addr>
423 : phosphor::network::detail::FormatFromBuf<
424 phosphor::network::detail::AddrBufMaker<in6_addr>>
425{
426};
427template <>
428struct formatter<phosphor::network::InAddrAny>
429{
430 private:
431 fmt::formatter<std::string_view> formatter;
432
433 public:
434 template <typename ParseContext>
435 constexpr auto parse(ParseContext& ctx)
436 {
437 return ctx.begin();
438 }
439
440 template <typename FormatContext>
441 auto format(auto v, FormatContext& ctx) const
442 {
443 return std::visit(
444 [&](auto v) {
445 auto abm =
446 phosphor::network::detail::AddrBufMaker<decltype(v)>{};
447 return formatter.format(abm(v), ctx);
448 },
449 v);
450 }
451};
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800452template <>
453struct formatter<phosphor::network::IfAddr>
454{
455 private:
456 fmt::formatter<phosphor::network::InAddrAny> addrF;
457 fmt::formatter<char> strF;
458 fmt::formatter<uint8_t> numF;
459
460 public:
461 template <typename ParseContext>
462 constexpr auto parse(ParseContext& ctx)
463 {
464 return ctx.begin();
465 }
466
467 template <typename FormatContext>
468 auto format(auto v, FormatContext& ctx) const
469 {
470 addrF.format(v.getAddr(), ctx);
471 strF.format('/', ctx);
472 return numF.format(v.getPfx(), ctx);
473 }
474};
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700475} // namespace fmt
476
477namespace std
478{
479string to_string(ether_addr value);
480string to_string(in_addr value);
481string to_string(in6_addr value);
482string to_string(phosphor::network::InAddrAny value);
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800483string to_string(phosphor::network::IfAddr value);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700484} // namespace std
485
486constexpr bool operator==(ether_addr lhs, ether_addr rhs) noexcept
487{
488 return std::equal(lhs.ether_addr_octet, lhs.ether_addr_octet + 6,
489 rhs.ether_addr_octet);
490}
491
492constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
493{
494 return lhs.s_addr == rhs.s_addr;
495}
496
497constexpr bool operator==(in6_addr lhs, in6_addr rhs) noexcept
498{
499 return std::equal(lhs.s6_addr32, lhs.s6_addr32 + 4, rhs.s6_addr32);
500}
501
502template <typename T>
503constexpr std::enable_if_t<!std::is_same_v<phosphor::network::InAddrAny, T>,
504 bool>
505 operator==(phosphor::network::InAddrAny lhs, T rhs) noexcept
506{
507 return phosphor::network::detail::veq(rhs, lhs);
508}
509
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700510auto& operator<<(auto& os, ether_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700511{
512 return os << phosphor::network::detail::AddrBufMaker<ether_addr>{}(v);
513}
514
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700515auto& operator<<(auto& os, in_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700516{
517 return os << phosphor::network::detail::AddrBufMaker<in_addr>{}(v);
518}
519
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700520auto& operator<<(auto& os, in6_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700521{
522 return os << phosphor::network::detail::AddrBufMaker<in6_addr>{}(v);
523}
524
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700525auto& operator<<(auto& os, phosphor::network::InAddrAny v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700526{
527 return os << std::visit(
528 [](auto v) {
529 return phosphor::network::detail::AddrBufMaker<
530 decltype(v)>{}(v);
531 },
532 v);
533}
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800534
535auto& operator<<(auto& os, phosphor::network::IfAddr v)
536{
537 return os << v.getAddr() << "/" << std::dec << int{v.getPfx()};
538}