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" |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 4 | |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 5 | #include <optional> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <phosphor-logging/elog-errors.hpp> |
| 7 | #include <phosphor-logging/log.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 8 | #include <stdexcept> |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 9 | #include <stdplus/raw.hpp> |
William A. Kennington III | d81a098 | 2019-02-01 21:10:27 -0800 | [diff] [blame] | 10 | #include <string_view> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | namespace route |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 23 | template <typename Addr> |
| 24 | static void parse(auto& gws, std::string_view msg) |
| 25 | { |
| 26 | std::optional<unsigned> ifIdx; |
| 27 | std::optional<Addr> gw; |
| 28 | while (!msg.empty()) |
| 29 | { |
| 30 | auto [hdr, data] = netlink::extractRtAttr(msg); |
| 31 | switch (hdr.rta_type) |
| 32 | { |
| 33 | case RTA_OIF: |
| 34 | ifIdx.emplace(stdplus::raw::copyFrom<int>(data)); |
| 35 | break; |
| 36 | case RTA_GATEWAY: |
| 37 | gw.emplace(stdplus::raw::copyFrom<Addr>(data)); |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | if (ifIdx && gw) |
| 42 | { |
| 43 | gws.emplace(*ifIdx, *gw); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void parseRoute(auto& gws4, auto& gws6, const nlmsghdr& hdr, |
| 48 | std::string_view msg) |
| 49 | { |
| 50 | if (hdr.nlmsg_type != RTM_NEWROUTE) |
| 51 | { |
| 52 | throw std::runtime_error("Not a route msg"); |
| 53 | } |
| 54 | const auto& rtm = netlink::extractRtData<rtmsg>(msg); |
| 55 | |
| 56 | if (rtm.rtm_table != RT_TABLE_MAIN || rtm.rtm_dst_len != 0) |
| 57 | { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | switch (rtm.rtm_family) |
| 62 | { |
| 63 | case AF_INET: |
| 64 | return parse<in_addr>(gws4, msg); |
| 65 | case AF_INET6: |
| 66 | return parse<in6_addr>(gws6, msg); |
| 67 | } |
| 68 | } |
| 69 | |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 70 | void Table::refresh() |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 71 | { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 72 | gws4.clear(); |
| 73 | gws6.clear(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 74 | try |
| 75 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 76 | rtmsg msg{}; |
| 77 | netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg, |
| 78 | [&](const nlmsghdr& hdr, std::string_view msg) { |
William A. Kennington III | 2bd35d6 | 2022-10-26 19:20:29 -0700 | [diff] [blame] | 79 | parseRoute(gws4, gws6, hdr, msg); |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 80 | }); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 81 | } |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 82 | catch (const std::exception& e) |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 83 | { |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 84 | log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what())); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 85 | commit<InternalFailure>(); |
| 86 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 87 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | } // namespace route |
| 89 | } // namespace network |
| 90 | } // namespace phosphor |