1919*/
2020
2121#include " ../../../../iocore/cache/P_CacheDir.h"
22+ #include " iocore/net/ConnectionTracker.h"
2223#include " mgmt/rpc/handlers/server/Server.h"
2324#include " mgmt/rpc/handlers/common/ErrorUtils.h"
2425#include " mgmt/rpc/handlers/common/Utils.h"
@@ -42,6 +43,33 @@ namespace field_names
4243struct DrainInfo {
4344 bool noNewConnections{false };
4445};
46+
47+ struct ConnectionTrackingInfo {
48+ enum TableFlags {
49+ NOT_SET = 0 ,
50+ INBOUND = 1 << 0 , // Inbound table
51+ OUTBOUND = 1 << 1 // Outbound table
52+ };
53+ TableFlags table{TableFlags::OUTBOUND}; // default.
54+ };
55+ using TFLags = ConnectionTrackingInfo::TableFlags;
56+ constexpr TFLags
57+ operator |(const TFLags rhs, const TFLags lhs)
58+ {
59+ return static_cast <TFLags>(static_cast <uint32_t >(rhs) | static_cast <uint32_t >(lhs));
60+ }
61+
62+ constexpr TFLags &
63+ operator |=(TFLags &rhs, TFLags lhs)
64+ {
65+ return rhs = rhs | lhs;
66+ }
67+
68+ constexpr TFLags
69+ operator &(TFLags rhs, TFLags lhs)
70+ {
71+ return static_cast <TFLags>(static_cast <uint32_t >(rhs) & static_cast <uint32_t >(lhs));
72+ }
4573} // namespace rpc::handlers::server
4674namespace YAML
4775{
@@ -61,6 +89,31 @@ template <> struct convert<rpc::handlers::server::DrainInfo> {
6189 return true ;
6290 }
6391};
92+ template <> struct convert <rpc::handlers::server::ConnectionTrackingInfo> {
93+ static constexpr auto table{" table" };
94+ static bool
95+ decode (const Node &node, rpc::handlers::server::ConnectionTrackingInfo &rhs)
96+ {
97+ namespace field = rpc::handlers::server::field_names;
98+ if (!node.IsMap ()) {
99+ return false ;
100+ }
101+ // optional
102+ if (auto n = node[table]; !n.IsNull ()) {
103+ auto const &val = n.as <std::string>();
104+ if (val == " both" ) {
105+ rhs.table = rpc::handlers::server::TFLags::INBOUND | rpc::handlers::server::TFLags::OUTBOUND;
106+ } else if (val == " inbound" ) {
107+ rhs.table = rpc::handlers::server::TFLags::INBOUND;
108+ } else if (val == " outbound" ) {
109+ rhs.table = rpc::handlers::server::TFLags::OUTBOUND;
110+ } else {
111+ throw std::runtime_error (" Invalid table type. Use [both|inbound|outbound]" );
112+ }
113+ }
114+ return true ;
115+ }
116+ };
64117} // namespace YAML
65118
66119namespace rpc ::handlers::server
@@ -150,4 +203,32 @@ get_server_status(std::string_view const & /* params ATS_UNUSED */, YAML::Node c
150203 }
151204 return resp;
152205}
206+
207+ swoc::Rv<YAML::Node>
208+ get_connection_tracker_info (std::string_view const & /* params ATS_UNUSED */ , YAML::Node const ¶ms)
209+ {
210+ swoc::Rv<YAML::Node> resp;
211+ try {
212+ ConnectionTrackingInfo p;
213+ if (!params.IsNull ()) {
214+ p = params.as <ConnectionTrackingInfo>();
215+ }
216+
217+ if (p.table & TFLags::OUTBOUND) {
218+ std::string json{ConnectionTracker::outbound_to_json_string ()};
219+ resp.result ()[" outbound" ] = YAML::Load (json);
220+ }
221+
222+ if (p.table & TFLags::INBOUND) {
223+ std::string json{ConnectionTracker::inbound_to_json_string ()};
224+ resp.result ()[" inbound" ] = YAML::Load (json);
225+ }
226+
227+ } catch (std::exception const &ex) {
228+ resp.errata ()
229+ .assign (std::error_code{errors::Codes::SERVER})
230+ .note (" Error found when calling get_connection_tracker_info API: {}" , ex.what ());
231+ }
232+ return resp;
233+ }
153234} // namespace rpc::handlers::server
0 commit comments