Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 1 | #include "routing_table.hpp" |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 3 | #include "netlink.hpp" |
William A. Kennington III | a7344c3 | 2022-10-31 14:12:27 -0700 | [diff] [blame] | 4 | #include "rtnetlink.hpp" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 5 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 6 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 7 | #include <phosphor-logging/elog-errors.hpp> |
| 8 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 9 | #include <stdexcept> |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 10 | #include <stdplus/raw.hpp> |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 11 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace route |
| 19 | { |
| 20 | |
| 21 | using namespace phosphor::logging; |
| 22 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 23 | |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 24 | static void parseRoute(auto& gws4, auto& gws6, const nlmsghdr& hdr, |
| 25 | std::string_view msg) |
| 26 | { |
| 27 | if (hdr.nlmsg_type != RTM_NEWROUTE) |
| 28 | { |
| 29 | throw std::runtime_error("Not a route msg"); |
| 30 | } |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 31 | |
William A. Kennington III | a7344c3 | 2022-10-31 14:12:27 -0700 | [diff] [blame] | 32 | if (auto ret = netlink::gatewayFromRtm(msg); ret) |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 33 | { |
William A. Kennington III | a7344c3 | 2022-10-31 14:12:27 -0700 | [diff] [blame] | 34 | std::visit( |
| 35 | [&](auto addr) { |
| 36 | if constexpr (std::is_same_v<in_addr, decltype(addr)>) |
| 37 | { |
| 38 | gws4.emplace(std::get<unsigned>(*ret), addr); |
| 39 | } |
| 40 | else if constexpr (std::is_same_v<in6_addr, decltype(addr)>) |
| 41 | { |
| 42 | gws6.emplace(std::get<unsigned>(*ret), addr); |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | static_assert(!std::is_same_v<void, decltype(addr)>); |
| 47 | } |
| 48 | }, |
| 49 | std::get<InAddrAny>(*ret)); |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 53 | void Table::refresh() |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 54 | { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 55 | gws4.clear(); |
| 56 | gws6.clear(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 57 | try |
| 58 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 59 | rtmsg msg{}; |
| 60 | netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg, |
| 61 | [&](const nlmsghdr& hdr, std::string_view msg) { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 62 | parseRoute(gws4, gws6, hdr, msg); |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 63 | }); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 64 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 65 | catch (const std::exception& e) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 66 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 67 | log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what())); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 68 | commit<InternalFailure>(); |
| 69 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 70 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 71 | } // namespace route |
| 72 | } // namespace network |
| 73 | } // namespace phosphor |