This is an automated email from the ASF dual-hosted git repository.

dmeden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new cb04df7f18 Connection tracker: Add functions to query inbound groups. 
(#12245)
cb04df7f18 is described below

commit cb04df7f18ae0e32fa1aca23ca286308c97ce029
Author: Damian Meden <[email protected]>
AuthorDate: Fri May 30 10:26:21 2025 +0200

    Connection tracker: Add functions to query inbound groups. (#12245)
    
    * Connection tracker: Add functions to query inbound groups.
    
    This, as well as the outbound groups will be used to report its data
    using the jsonrpc handlers.
    This also fixes an issue in the build of the output json stream.
---
 include/iocore/net/ConnectionTracker.h |  23 +++++++
 src/iocore/net/ConnectionTracker.cc    | 115 ++++++++++++++++++++++++---------
 2 files changed, 107 insertions(+), 31 deletions(-)

diff --git a/include/iocore/net/ConnectionTracker.h 
b/include/iocore/net/ConnectionTracker.h
index d272b78f8d..0a40b7814d 100644
--- a/include/iocore/net/ConnectionTracker.h
+++ b/include/iocore/net/ConnectionTracker.h
@@ -212,6 +212,14 @@ public:
    */
   static TxnState obtain_outbound(TxnConfig const &txn_cnf, std::string_view 
fqdn, IpEndpoint const &addr);
 
+  /** Get the currently existing inbound groups.
+   * @param [out] groups parameter - pointers to the groups are pushed in to 
this container.
+   *
+   * The groups are loaded in to @a groups, which is cleared before loading. 
Note the groups returned will remain valid
+   * although data inside the groups is volatile.
+   */
+  static void get_inbound_groups(std::vector<std::shared_ptr<Group const>> 
&groups);
+
   /** Get the currently existing outbound groups.
    * @param [out] groups parameter - pointers to the groups are pushed in to 
this container.
    *
@@ -219,6 +227,12 @@ public:
    * although data inside the groups is volatile.
    */
   static void get_outbound_groups(std::vector<std::shared_ptr<Group const>> 
&groups);
+
+  /** Write the inbound connection tracking data to JSON.
+   * @return string containing a JSON encoding of the table.
+   */
+  static std::string inbound_to_json_string();
+
   /** Write the outbound connection tracking data to JSON.
    * @return string containing a JSON encoding of the table.
    */
@@ -227,6 +241,15 @@ public:
    * @param f Output file.
    */
   static void dump(FILE *f);
+  /** Write the groups to @a f.
+   * @param f Output file.
+   */
+  static void dump_inbound(FILE *f);
+  /** Write the groups to @a f.
+   * @param f Output file.
+   */
+  static void dump_outbound(FILE *f);
+
   /** Do global initialization.
    *
    * This sets up the global configuration and any configuration update 
callbacks needed. It is presumed
diff --git a/src/iocore/net/ConnectionTracker.cc 
b/src/iocore/net/ConnectionTracker.cc
index 8401ecbf06..64e5ff3dd2 100644
--- a/src/iocore/net/ConnectionTracker.cc
+++ b/src/iocore/net/ConnectionTracker.cc
@@ -149,6 +149,44 @@ Config_Update_Conntrack_Client_Alert_Delay(const char 
*name, RecDataT dtype, Rec
   return Config_Update_Conntrack_Server_Alert_Delay_Helper(name, dtype, data, 
cookie, config->client_alert_delay);
 }
 
+// // helper function to build up a json string from the passed conn groups.
+std::string
+Groups_To_JSON(std::vector<std::shared_ptr<ConnectionTracker::Group const>> 
const &groups)
+{
+  std::string                    text;
+  size_t                         extent = 0;
+  static const swoc::bwf::Format header_fmt{R"({{"count": {}, "list": [  )"};
+  static const swoc::bwf::Format item_fmt{
+    R"(  {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, 
"blocked": {}, "alert": {}}},
+)"};
+  static const std::string_view trailer{" \n]}"};
+
+  static const auto printer = [](swoc::BufferWriter &w, 
ConnectionTracker::Group const *g) -> swoc::BufferWriter & {
+    w.print(item_fmt, g->_match_type, g->_addr, g->_fqdn, g->_count.load(), 
g->_count_max.load(), g->_blocked.load(),
+            g->get_last_alert_epoch_time());
+    return w;
+  };
+
+  swoc::FixedBufferWriter null_bw{nullptr}; // Empty buffer for sizing work.
+
+  null_bw.print(header_fmt, groups.size()).extent();
+  for (auto g : groups) {
+    printer(null_bw, g.get());
+  }
+  extent = null_bw.extent() + trailer.size() - 2; // 2 for the trailing comma 
newline that will get clipped.
+
+  text.resize(extent);
+  swoc::FixedBufferWriter w(const_cast<char *>(text.data()), text.size());
+  w.restrict(trailer.size());
+  w.print(header_fmt, groups.size());
+  for (auto g : groups) {
+    printer(w, g.get());
+  }
+  w.restore(trailer.size());
+  w.write(trailer);
+  return text;
+}
+
 } // namespace
 
 void
@@ -318,56 +356,71 @@ 
ConnectionTracker::get_outbound_groups(std::vector<std::shared_ptr<Group const>>
   }
 }
 
+void
+ConnectionTracker::get_inbound_groups(std::vector<std::shared_ptr<Group 
const>> &groups)
+{
+  std::lock_guard<std::mutex> lock(_inbound_table._mutex); // TABLE LOCK
+  groups.resize(0);
+  groups.reserve(_inbound_table._table.size());
+  for (auto &&[key, group] : _inbound_table._table) {
+    groups.push_back(group);
+  }
+}
+
 std::string
-ConnectionTracker::outbound_to_json_string()
+ConnectionTracker::inbound_to_json_string()
 {
-  std::string                    text;
-  size_t                         extent = 0;
-  static const swoc::bwf::Format header_fmt{R"({{"count": {}, "list": [
-)"};
-  static const swoc::bwf::Format item_fmt{
-    R"(  {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, 
"blocked": {}, "alert": {}}},
-)"};
-  static const std::string_view trailer{" \n]}"};
+  std::vector<std::shared_ptr<Group const>> groups;
+  self_type::get_inbound_groups(groups);
+  return Groups_To_JSON(groups);
+}
 
-  static const auto printer = [](swoc::BufferWriter &w, Group const *g) -> 
swoc::BufferWriter & {
-    w.print(item_fmt, g->_match_type, g->_addr, g->_fqdn, g->_count.load(), 
g->_count_max.load(), g->_blocked.load(),
-            g->get_last_alert_epoch_time());
-    return w;
-  };
+std::string
+ConnectionTracker::outbound_to_json_string()
+{
+  std::vector<std::shared_ptr<Group const>> groups;
+  self_type::get_outbound_groups(groups);
+  return Groups_To_JSON(groups);
+}
 
-  swoc::FixedBufferWriter                   null_bw{nullptr}; // Empty buffer 
for sizing work.
+void
+ConnectionTracker::dump_outbound(FILE *f)
+{
   std::vector<std::shared_ptr<Group const>> groups;
 
   self_type::get_outbound_groups(groups);
 
-  null_bw.print(header_fmt, groups.size()).extent();
-  for (auto g : groups) {
-    printer(null_bw, g.get());
-  }
-  extent = null_bw.extent() + trailer.size() - 2; // 2 for the trailing comma 
newline that will get clipped.
+  if (groups.size()) {
+    fprintf(f, "\n[O] Peer Connection Tracking\n%7s | %5s | %24s | %33s | %8s 
|\n", "Current", "Block", "Address", "Hostname Hash",
+            "Match");
+    fprintf(f, 
"------|-------|--------------------------|-----------------------------------|----------|\n");
 
-  text.resize(extent);
-  swoc::FixedBufferWriter w(const_cast<char *>(text.data()), text.size());
-  w.restrict(trailer.size());
-  w.print(header_fmt, groups.size());
-  for (auto g : groups) {
-    printer(w, g.get());
+    for (std::shared_ptr<Group const> g : groups) {
+      swoc::LocalBufferWriter<128> w;
+      w.print("{:7} | {:5} | {:24} | {:33} | {:8} |\n", g->_count.load(), 
g->_blocked.load(), g->_addr, g->_hash, g->_match_type);
+      fwrite(w.data(), w.size(), 1, f);
+    }
+
+    fprintf(f, 
"------|-------|--------------------------|-----------------------------------|----------|\n");
   }
-  w.restore(trailer.size());
-  w.write(trailer);
-  return text;
 }
 
 void
 ConnectionTracker::dump(FILE *f)
+{
+  dump_outbound(f);
+  dump_inbound(f);
+}
+
+void
+ConnectionTracker::dump_inbound(FILE *f)
 {
   std::vector<std::shared_ptr<Group const>> groups;
 
-  self_type::get_outbound_groups(groups);
+  self_type::get_inbound_groups(groups);
 
   if (groups.size()) {
-    fprintf(f, "\nPeer Connection Tracking\n%7s | %5s | %24s | %33s | %8s 
|\n", "Current", "Block", "Address", "Hostname Hash",
+    fprintf(f, "\n[I] Peer Connection Tracking\n%7s | %5s | %24s | %33s | %8s 
|\n", "Current", "Block", "Address", "Hostname Hash",
             "Match");
     fprintf(f, 
"------|-------|--------------------------|-----------------------------------|----------|\n");
 

Reply via email to