blob: efe054db2733d98e6eb2c0c3e1393e4a33bf66fd [file] [log] [blame]
Ratan Gupta233524c2017-05-27 11:47:31 +05301#pragma once
Ratan Gupta233524c2017-05-27 11:47:31 +05302#include <linux/netlink.h>
3
Ratan Gupta233524c2017-05-27 11:47:31 +05304#include <map>
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include <string>
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -07006#include <string_view>
Ratan Gupta233524c2017-05-27 11:47:31 +05307
8namespace phosphor
9{
10namespace network
11{
12namespace route
13{
Ratan Guptab46497f2017-06-09 12:00:34 +053014constexpr auto BUFSIZE = 4096;
Ratan Gupta233524c2017-05-27 11:47:31 +053015
16struct Entry
17{
18 // destination network
19 std::string destination;
20 // gateway for this network.
21 std::string gateway;
22 // interface for this route
23 std::string interface;
Patrick Venture6d669962018-10-13 09:57:36 -070024 Entry(const std::string& dest, const std::string& gtw,
25 const std::string& intf) :
26 destination(dest),
27 gateway(gtw), interface(intf)
Gunnar Mills57d9c502018-09-14 14:42:34 -050028 {
29 }
Ratan Gupta233524c2017-05-27 11:47:31 +053030
31 bool operator==(const Entry& rhs)
32 {
33 return this->destination == rhs.destination &&
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 this->gateway == rhs.gateway && this->interface == rhs.interface;
Ratan Gupta233524c2017-05-27 11:47:31 +053035 }
36};
37
38// Map of network address and the route entry
39using Map = std::map<std::string, struct Entry>;
40
41class Table
42{
Gunnar Mills57d9c502018-09-14 14:42:34 -050043 public:
William A. Kennington IIIe0564842021-10-23 16:02:22 -070044 /** @brief Rebuilds the routing table from the kernel */
45 void refresh();
Ratan Gupta233524c2017-05-27 11:47:31 +053046
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080048 * @brief gets the default v4 gateway.
Gunnar Mills57d9c502018-09-14 14:42:34 -050049 *
Ravi Tejaa5a09442020-07-17 00:57:33 -050050 * @returns the default v4 gateway list.
Gunnar Mills57d9c502018-09-14 14:42:34 -050051 */
William A. Kennington IIIe0564842021-10-23 16:02:22 -070052 inline const auto& getDefaultGateway() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050053 {
54 return defaultGateway;
William A. Kennington IIIe0564842021-10-23 16:02:22 -070055 }
Ratan Gupta233524c2017-05-27 11:47:31 +053056
Gunnar Mills57d9c502018-09-14 14:42:34 -050057 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080058 * @brief gets the default v6 gateway.
59 *
Ravi Tejaa5a09442020-07-17 00:57:33 -050060 * @returns the default v6 gateway list.
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080061 */
William A. Kennington IIIe0564842021-10-23 16:02:22 -070062 inline const auto& getDefaultGateway6() const
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080063 {
64 return defaultGateway6;
65 };
66
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 private:
68 /**
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 * @brief Parse the route and add it to the route list.
70 *
71 * @param[in] nlHdr - net link message header.
72 */
William A. Kennington IIIe0b8fd62021-10-23 14:35:08 -070073 void parseRoutes(const struct nlmsghdr& nlHdr, std::string_view msg);
Ratan Gupta233524c2017-05-27 11:47:31 +053074
Ravi Tejaa5a09442020-07-17 00:57:33 -050075 std::map<std::string, std::string> defaultGateway; // default gateway list
76 std::map<std::string, std::string> defaultGateway6; // default gateway list
77 Map routeList; // List of routes
Ratan Gupta233524c2017-05-27 11:47:31 +053078};
79
Gunnar Mills57d9c502018-09-14 14:42:34 -050080} // namespace route
81} // namespace network
82} // namespace phosphor