Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 1 | #pragma once |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 2 | #include <linux/netlink.h> |
| 3 | |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 4 | #include <map> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include <string> |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 6 | #include <string_view> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace network |
| 11 | { |
| 12 | namespace route |
| 13 | { |
Ratan Gupta | b46497f | 2017-06-09 12:00:34 +0530 | [diff] [blame] | 14 | constexpr auto BUFSIZE = 4096; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 15 | |
| 16 | struct 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 Venture | 6d66996 | 2018-10-13 09:57:36 -0700 | [diff] [blame] | 24 | Entry(const std::string& dest, const std::string& gtw, |
| 25 | const std::string& intf) : |
| 26 | destination(dest), |
| 27 | gateway(gtw), interface(intf) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 28 | { |
| 29 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 30 | |
| 31 | bool operator==(const Entry& rhs) |
| 32 | { |
| 33 | return this->destination == rhs.destination && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 34 | this->gateway == rhs.gateway && this->interface == rhs.interface; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 35 | } |
| 36 | }; |
| 37 | |
| 38 | // Map of network address and the route entry |
| 39 | using Map = std::map<std::string, struct Entry>; |
| 40 | |
| 41 | class Table |
| 42 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 43 | public: |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 44 | /** @brief Rebuilds the routing table from the kernel */ |
| 45 | void refresh(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 46 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 47 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 48 | * @brief gets the default v4 gateway. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 49 | * |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 50 | * @returns the default v4 gateway list. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 51 | */ |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 52 | inline const auto& getDefaultGateway() const |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 53 | { |
| 54 | return defaultGateway; |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 55 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 56 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 57 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 58 | * @brief gets the default v6 gateway. |
| 59 | * |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 60 | * @returns the default v6 gateway list. |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 61 | */ |
William A. Kennington III | e056484 | 2021-10-23 16:02:22 -0700 | [diff] [blame] | 62 | inline const auto& getDefaultGateway6() const |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 63 | { |
| 64 | return defaultGateway6; |
| 65 | }; |
| 66 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 67 | private: |
| 68 | /** |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 69 | * @brief Parse the route and add it to the route list. |
| 70 | * |
| 71 | * @param[in] nlHdr - net link message header. |
| 72 | */ |
William A. Kennington III | e0b8fd6 | 2021-10-23 14:35:08 -0700 | [diff] [blame] | 73 | void parseRoutes(const struct nlmsghdr& nlHdr, std::string_view msg); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 74 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 75 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 78 | }; |
| 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 80 | } // namespace route |
| 81 | } // namespace network |
| 82 | } // namespace phosphor |