blob: d6285a7aa5a5bd122ffcf5af9bb154f8323f7944 [file] [log] [blame]
Ratan Gupta233524c2017-05-27 11:47:31 +05301#include "routing_table.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07002
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -07003#include "netlink.hpp"
William A. Kennington IIIa7344c32022-10-31 14:12:27 -07004#include "rtnetlink.hpp"
Ratan Gupta233524c2017-05-27 11:47:31 +05305
William A. Kennington IIId81a0982019-02-01 21:10:27 -08006#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -07007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +05309#include <stdexcept>
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070010#include <stdplus/raw.hpp>
William A. Kennington IIId81a0982019-02-01 21:10:27 -080011#include <string_view>
Patrick Venture189d44e2018-07-09 12:30:59 -070012#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta233524c2017-05-27 11:47:31 +053013
14namespace phosphor
15{
16namespace network
17{
18namespace route
19{
20
21using namespace phosphor::logging;
22using namespace sdbusplus::xyz::openbmc_project::Common::Error;
23
William A. Kennington III2bd35d62022-10-26 19:20:29 -070024static 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 III2bd35d62022-10-26 19:20:29 -070031
William A. Kennington IIIa7344c32022-10-31 14:12:27 -070032 if (auto ret = netlink::gatewayFromRtm(msg); ret)
William A. Kennington III2bd35d62022-10-26 19:20:29 -070033 {
William A. Kennington IIIa7344c32022-10-31 14:12:27 -070034 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 III2bd35d62022-10-26 19:20:29 -070050 }
51}
52
William A. Kennington IIIe0564842021-10-23 16:02:22 -070053void Table::refresh()
Ratan Gupta233524c2017-05-27 11:47:31 +053054{
William A. Kennington III2bd35d62022-10-26 19:20:29 -070055 gws4.clear();
56 gws6.clear();
Ratan Gupta233524c2017-05-27 11:47:31 +053057 try
58 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070059 rtmsg msg{};
60 netlink::performRequest(NETLINK_ROUTE, RTM_GETROUTE, NLM_F_DUMP, msg,
61 [&](const nlmsghdr& hdr, std::string_view msg) {
William A. Kennington III2bd35d62022-10-26 19:20:29 -070062 parseRoute(gws4, gws6, hdr, msg);
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070063 });
Ratan Gupta233524c2017-05-27 11:47:31 +053064 }
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070065 catch (const std::exception& e)
Ratan Gupta233524c2017-05-27 11:47:31 +053066 {
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070067 log<level::ERR>("Reading routes failed", entry("ERROR=%s", e.what()));
Ratan Gupta233524c2017-05-27 11:47:31 +053068 commit<InternalFailure>();
69 }
Ratan Gupta233524c2017-05-27 11:47:31 +053070}
Gunnar Mills57d9c502018-09-14 14:42:34 -050071} // namespace route
72} // namespace network
73} // namespace phosphor