blob: 4bbff0dc1ccb21500d067771e5f57a1dbfcab62b [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}();
William A. Kennington IIIdd7c7b32022-11-04 15:19:36 -0700201inline constexpr auto intLookup = []() {
202 std::array<char, 36> ret;
203 for (int8_t i = 0; i < 10; ++i)
204 {
205 ret[i] = i + '0';
206 }
207 for (int8_t i = 0; i < 26; ++i)
208 {
209 ret[i + 10] = i + 'a';
210 }
211 return ret;
212}();
213} // namespace detail
William A. Kennington III238ef992022-11-03 12:47:49 -0700214
215template <typename T, uint8_t base>
216struct DecodeInt
217{
218 static_assert(base > 1 && base <= 36);
219 static_assert(std::is_unsigned_v<T>);
220
221 constexpr T operator()(std::string_view str) const
222 {
223 if (str.empty())
224 {
225 throw std::invalid_argument("Empty Str");
226 }
227 constexpr auto max = std::numeric_limits<T>::max();
228 auto ret =
229 std::accumulate(str.begin(), str.end(), T{}, [&](T r, char c) {
230 auto v = detail::charLookup[c];
231 if (v < 0 || v >= base)
232 {
233 throw std::invalid_argument("Invalid numeral");
234 }
235 if constexpr (std::popcount(base) == 1)
236 {
237 constexpr auto shift = std::countr_zero(base);
238 constexpr auto maxshift = max >> shift;
239 if (r > maxshift)
240 {
241 throw std::overflow_error("Integer Decode");
242 }
243 return (r << shift) | v;
244 }
245 else
246 {
247 constexpr auto maxbase = max / base;
248 if (r > maxbase)
249 {
250 throw std::overflow_error("Integer Decode");
251 }
252 r *= base;
253 if (max - v < r)
254 {
255 throw std::overflow_error("Integer Decode");
256 }
257 return r + v;
258 }
259 });
260 return ret;
261 }
262};
263
William A. Kennington IIIdd7c7b32022-11-04 15:19:36 -0700264template <typename T, uint8_t base>
265struct EncodeInt
266{
267 static_assert(base > 1 && base <= 36);
268 static_assert(std::is_unsigned_v<T>);
269
270 static constexpr uint8_t buf_size = []() {
271 T v = std::numeric_limits<T>::max();
272 uint8_t i = 0;
273 for (; v != 0; ++i)
274 {
275 v /= base;
276 }
277 return i;
278 }();
279 using buf_type = std::array<char, buf_size>;
280
281 constexpr uint8_t reverseFill(char* buf, T v) const noexcept
282 {
283 uint8_t i = 0;
284 do
285 {
286 if constexpr (std::popcount(base) == 1)
287 {
288 buf[i++] = detail::intLookup[v & 0xf];
289 v >>= 4;
290 }
291 else
292 {
293 buf[i++] = detail::intLookup[v % base];
294 v /= base;
295 }
296 } while (v > 0);
297 return i;
298 }
299
300 constexpr char* operator()(char* buf, T v) const noexcept
301 {
302 uint8_t i = reverseFill(buf, v);
303 std::reverse(buf, buf + i);
304 return buf + i;
305 }
306
307 constexpr char* operator()(char* buf, T v, uint8_t min_width) const noexcept
308 {
309 uint8_t i = reverseFill(buf, v);
310 auto end = buf + std::max(i, min_width);
311 std::fill(buf + i, end, '0');
312 std::reverse(buf, end);
313 return end;
314 }
315};
316
William A. Kennington IIIb01d08f2022-11-03 12:50:00 -0700317template <typename T>
318struct ToAddr
319{
320};
321
322template <>
323struct ToAddr<ether_addr>
324{
325 constexpr ether_addr operator()(std::string_view str) const
326 {
327 constexpr DecodeInt<uint8_t, 16> di;
328 ether_addr ret;
329 if (str.size() == 12 && str.find(":") == str.npos)
330 {
331 for (size_t i = 0; i < 6; ++i)
332 {
333 ret.ether_addr_octet[i] = di(str.substr(i * 2, 2));
334 }
335 }
336 else
337 {
338 for (size_t i = 0; i < 5; ++i)
339 {
340 auto loc = str.find(":");
341 ret.ether_addr_octet[i] = di(str.substr(0, loc));
342 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
343 if (str.empty())
344 {
345 throw std::invalid_argument("Missing mac data");
346 }
347 }
348 ret.ether_addr_octet[5] = di(str);
349 }
350 return ret;
351 }
352};
353
William A. Kennington IIIdf1178e2022-11-03 12:56:33 -0700354template <>
355struct ToAddr<in_addr>
356{
357 constexpr in_addr operator()(std::string_view str) const
358 {
359 constexpr DecodeInt<uint8_t, 10> di;
360 uint32_t addr = {};
361 for (size_t i = 0; i < 3; ++i)
362 {
363 auto loc = str.find(".");
364 addr |= di(str.substr(0, loc));
365 addr <<= 8;
366 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
367 if (str.empty())
368 {
369 throw std::invalid_argument("Missing addr data");
370 }
371 }
372 addr |= di(str);
373 return {hton(addr)};
374 }
375};
376
William A. Kennington IIIec496a82022-11-04 02:17:20 -0700377template <>
378struct ToAddr<in6_addr>
379{
380 constexpr in6_addr operator()(std::string_view str) const
381 {
382 constexpr DecodeInt<uint16_t, 16> di;
383 in6_addr ret = {};
384 size_t i = 0;
385 while (i < 8)
386 {
387 auto loc = str.find(':');
388 if (i == 6 && loc == str.npos)
389 {
390 ret.s6_addr32[3] = ToAddr<in_addr>{}(str).s_addr;
391 return ret;
392 }
393 if (loc != 0 && !str.empty())
394 {
395 ret.s6_addr16[i++] = hton(di(str.substr(0, loc)));
396 }
397 if (i < 8 && str.size() > loc + 1 && str[loc + 1] == ':')
398 {
399 str.remove_prefix(loc + 2);
400 break;
401 }
402 else if (str.empty())
403 {
404 throw std::invalid_argument("IPv6 Data");
405 }
406 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
407 }
408 if (str.starts_with(':'))
409 {
410 throw std::invalid_argument("Extra separator");
411 }
412 size_t j = 7;
413 if (!str.empty() && i < 6 && str.find('.') != str.npos)
414 {
415 auto loc = str.rfind(':');
416 ret.s6_addr32[3] =
417 ToAddr<in_addr>{}(str.substr(loc == str.npos ? 0 : loc + 1))
418 .s_addr;
419 str.remove_suffix(loc == str.npos ? str.size() : str.size() - loc);
420 j -= 2;
421 }
422 while (!str.empty() && j > i)
423 {
424 auto loc = str.rfind(':');
425 ret.s6_addr16[j--] =
426 hton(di(str.substr(loc == str.npos ? 0 : loc + 1)));
427 str.remove_suffix(loc == str.npos ? str.size() : str.size() - loc);
428 }
429 if (!str.empty())
430 {
431 throw std::invalid_argument("Too much data");
432 }
433 return ret;
434 }
435};
436
William A. Kennington IIIead71982022-11-04 02:18:10 -0700437template <>
438struct ToAddr<InAddrAny>
439{
440 constexpr InAddrAny operator()(std::string_view str) const
441 {
442 if (str.find(':') == str.npos)
443 {
444 return ToAddr<in_addr>{}(str);
445 }
446 return ToAddr<in6_addr>{}(str);
447 }
448};
449
450template <>
451struct ToAddr<IfAddr>
452{
453 constexpr IfAddr operator()(std::string_view str) const
454 {
455 auto pos = str.rfind('/');
456 if (pos == str.npos)
457 {
458 throw std::invalid_argument("Invalid IfAddr");
459 }
460 return {ToAddr<InAddrAny>{}(str.substr(0, pos)),
461 DecodeInt<uint8_t, 10>{}(str.substr(pos + 1))};
462 }
463};
464
William A. Kennington III238ef992022-11-03 12:47:49 -0700465namespace detail
466{
William A. Kennington III71de63a2022-11-08 10:50:54 -0800467
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700468template <typename T>
469constexpr bool vcontains() noexcept
470{
471 return false;
472}
473
474template <typename T, typename V, typename... Vs>
475constexpr bool vcontains() noexcept
476{
477 return vcontains<T, Vs...>() || std::is_same_v<T, V>;
478}
479
480template <typename T, typename... Types>
481constexpr std::enable_if_t<vcontains<T, Types...>(), bool>
482 veq(T t, std::variant<Types...> v) noexcept
483{
484 return std::visit(
485 [t](auto v) {
486 if constexpr (std::is_same_v<T, decltype(v)>)
487 {
488 return v == t;
489 }
490 else
491 {
492 return false;
493 }
494 },
495 v);
496}
497
498template <typename T>
499struct AddrBufMaker
500{
501};
502
503template <>
504struct AddrBufMaker<ether_addr>
505{
506 public:
507 std::string_view operator()(ether_addr val) noexcept;
508
509 private:
510 std::array<char, /*octet*/ 2 * /*octets*/ 6 + /*seps*/ 5> buf;
511};
512
513template <>
514struct AddrBufMaker<in_addr>
515{
516 public:
517 std::string_view operator()(in_addr val) noexcept;
518
519 private:
520 std::array<char, /*octet*/ 3 * /*octets*/ 4 + /*seps*/ 3> buf;
521};
522
523template <>
524struct AddrBufMaker<in6_addr>
525{
526 public:
527 std::string_view operator()(in6_addr val) noexcept;
528
529 private:
530 std::array<char, /*hextet*/ 4 * /*hextets*/ 8 + /*seps*/ 7> buf;
531};
532
533template <typename BufMaker>
534struct FormatFromBuf
535{
536 private:
537 fmt::formatter<std::string_view> formatter;
538
539 public:
540 template <typename ParseContext>
541 constexpr auto parse(ParseContext& ctx)
542 {
543 return ctx.begin();
544 }
545
546 template <typename FormatContext>
547 auto format(auto v, FormatContext& ctx) const
548 {
549 return formatter.format(BufMaker{}(v), ctx);
550 }
551};
552} // namespace detail
Gunnar Mills57d9c502018-09-14 14:42:34 -0500553} // namespace network
554} // namespace phosphor
William A. Kennington III3e471c52022-10-27 19:46:07 -0700555
556template <typename... Ts>
557struct std::hash<std::tuple<Ts...>>
558{
559 constexpr auto operator()(const std::tuple<Ts...>& t) const noexcept
560 {
561 return std::apply(phosphor::network::hash_multi<Ts...>, t);
562 }
563};
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700564
William A. Kennington III653114f2022-11-01 22:34:04 -0700565template <>
566struct std::hash<in_addr>
567{
568 std::size_t operator()(in_addr addr) const noexcept;
569};
570
571template <>
572struct std::hash<in6_addr>
573{
574 std::size_t operator()(in6_addr addr) const noexcept;
575};
576
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800577template <>
578struct std::hash<phosphor::network::IfAddr>
579{
580 std::size_t operator()(phosphor::network::IfAddr addr) const noexcept;
581};
582
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700583namespace fmt
584{
585template <>
586struct formatter<ether_addr>
587 : phosphor::network::detail::FormatFromBuf<
588 phosphor::network::detail::AddrBufMaker<ether_addr>>
589{
590};
591template <>
592struct formatter<in_addr>
593 : phosphor::network::detail::FormatFromBuf<
594 phosphor::network::detail::AddrBufMaker<in_addr>>
595{
596};
597template <>
598struct formatter<in6_addr>
599 : phosphor::network::detail::FormatFromBuf<
600 phosphor::network::detail::AddrBufMaker<in6_addr>>
601{
602};
603template <>
604struct formatter<phosphor::network::InAddrAny>
605{
606 private:
607 fmt::formatter<std::string_view> formatter;
608
609 public:
610 template <typename ParseContext>
611 constexpr auto parse(ParseContext& ctx)
612 {
613 return ctx.begin();
614 }
615
616 template <typename FormatContext>
617 auto format(auto v, FormatContext& ctx) const
618 {
619 return std::visit(
620 [&](auto v) {
621 auto abm =
622 phosphor::network::detail::AddrBufMaker<decltype(v)>{};
623 return formatter.format(abm(v), ctx);
624 },
625 v);
626 }
627};
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800628template <>
629struct formatter<phosphor::network::IfAddr>
630{
631 private:
632 fmt::formatter<phosphor::network::InAddrAny> addrF;
633 fmt::formatter<char> strF;
634 fmt::formatter<uint8_t> numF;
635
636 public:
637 template <typename ParseContext>
638 constexpr auto parse(ParseContext& ctx)
639 {
640 return ctx.begin();
641 }
642
643 template <typename FormatContext>
644 auto format(auto v, FormatContext& ctx) const
645 {
646 addrF.format(v.getAddr(), ctx);
647 strF.format('/', ctx);
648 return numF.format(v.getPfx(), ctx);
649 }
650};
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700651} // namespace fmt
652
653namespace std
654{
655string to_string(ether_addr value);
656string to_string(in_addr value);
657string to_string(in6_addr value);
658string to_string(phosphor::network::InAddrAny value);
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800659string to_string(phosphor::network::IfAddr value);
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700660} // namespace std
661
662constexpr bool operator==(ether_addr lhs, ether_addr rhs) noexcept
663{
664 return std::equal(lhs.ether_addr_octet, lhs.ether_addr_octet + 6,
665 rhs.ether_addr_octet);
666}
667
668constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
669{
670 return lhs.s_addr == rhs.s_addr;
671}
672
673constexpr bool operator==(in6_addr lhs, in6_addr rhs) noexcept
674{
675 return std::equal(lhs.s6_addr32, lhs.s6_addr32 + 4, rhs.s6_addr32);
676}
677
678template <typename T>
679constexpr std::enable_if_t<!std::is_same_v<phosphor::network::InAddrAny, T>,
680 bool>
681 operator==(phosphor::network::InAddrAny lhs, T rhs) noexcept
682{
683 return phosphor::network::detail::veq(rhs, lhs);
684}
685
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700686auto& operator<<(auto& os, ether_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700687{
688 return os << phosphor::network::detail::AddrBufMaker<ether_addr>{}(v);
689}
690
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700691auto& operator<<(auto& os, in_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700692{
693 return os << phosphor::network::detail::AddrBufMaker<in_addr>{}(v);
694}
695
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700696auto& operator<<(auto& os, in6_addr v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700697{
698 return os << phosphor::network::detail::AddrBufMaker<in6_addr>{}(v);
699}
700
William A. Kennington III86eb8b72022-11-01 22:35:04 -0700701auto& operator<<(auto& os, phosphor::network::InAddrAny v)
William A. Kennington IIIbb0eacc2022-10-21 15:22:06 -0700702{
703 return os << std::visit(
704 [](auto v) {
705 return phosphor::network::detail::AddrBufMaker<
706 decltype(v)>{}(v);
707 },
708 v);
709}
William A. Kennington IIIb9d7cba2022-11-08 10:54:11 -0800710
711auto& operator<<(auto& os, phosphor::network::IfAddr v)
712{
713 return os << v.getAddr() << "/" << std::dec << int{v.getPfx()};
714}