Skip to content

Commit cb04df7

Browse files
authored
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.
1 parent fd02ff9 commit cb04df7

2 files changed

Lines changed: 107 additions & 31 deletions

File tree

include/iocore/net/ConnectionTracker.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,27 @@ class ConnectionTracker
212212
*/
213213
static TxnState obtain_outbound(TxnConfig const &txn_cnf, std::string_view fqdn, IpEndpoint const &addr);
214214

215+
/** Get the currently existing inbound groups.
216+
* @param [out] groups parameter - pointers to the groups are pushed in to this container.
217+
*
218+
* The groups are loaded in to @a groups, which is cleared before loading. Note the groups returned will remain valid
219+
* although data inside the groups is volatile.
220+
*/
221+
static void get_inbound_groups(std::vector<std::shared_ptr<Group const>> &groups);
222+
215223
/** Get the currently existing outbound groups.
216224
* @param [out] groups parameter - pointers to the groups are pushed in to this container.
217225
*
218226
* The groups are loaded in to @a groups, which is cleared before loading. Note the groups returned will remain valid
219227
* although data inside the groups is volatile.
220228
*/
221229
static void get_outbound_groups(std::vector<std::shared_ptr<Group const>> &groups);
230+
231+
/** Write the inbound connection tracking data to JSON.
232+
* @return string containing a JSON encoding of the table.
233+
*/
234+
static std::string inbound_to_json_string();
235+
222236
/** Write the outbound connection tracking data to JSON.
223237
* @return string containing a JSON encoding of the table.
224238
*/
@@ -227,6 +241,15 @@ class ConnectionTracker
227241
* @param f Output file.
228242
*/
229243
static void dump(FILE *f);
244+
/** Write the groups to @a f.
245+
* @param f Output file.
246+
*/
247+
static void dump_inbound(FILE *f);
248+
/** Write the groups to @a f.
249+
* @param f Output file.
250+
*/
251+
static void dump_outbound(FILE *f);
252+
230253
/** Do global initialization.
231254
*
232255
* This sets up the global configuration and any configuration update callbacks needed. It is presumed

src/iocore/net/ConnectionTracker.cc

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ Config_Update_Conntrack_Client_Alert_Delay(const char *name, RecDataT dtype, Rec
149149
return Config_Update_Conntrack_Server_Alert_Delay_Helper(name, dtype, data, cookie, config->client_alert_delay);
150150
}
151151

152+
// // helper function to build up a json string from the passed conn groups.
153+
std::string
154+
Groups_To_JSON(std::vector<std::shared_ptr<ConnectionTracker::Group const>> const &groups)
155+
{
156+
std::string text;
157+
size_t extent = 0;
158+
static const swoc::bwf::Format header_fmt{R"({{"count": {}, "list": [ )"};
159+
static const swoc::bwf::Format item_fmt{
160+
R"( {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, "blocked": {}, "alert": {}}},
161+
)"};
162+
static const std::string_view trailer{" \n]}"};
163+
164+
static const auto printer = [](swoc::BufferWriter &w, ConnectionTracker::Group const *g) -> swoc::BufferWriter & {
165+
w.print(item_fmt, g->_match_type, g->_addr, g->_fqdn, g->_count.load(), g->_count_max.load(), g->_blocked.load(),
166+
g->get_last_alert_epoch_time());
167+
return w;
168+
};
169+
170+
swoc::FixedBufferWriter null_bw{nullptr}; // Empty buffer for sizing work.
171+
172+
null_bw.print(header_fmt, groups.size()).extent();
173+
for (auto g : groups) {
174+
printer(null_bw, g.get());
175+
}
176+
extent = null_bw.extent() + trailer.size() - 2; // 2 for the trailing comma newline that will get clipped.
177+
178+
text.resize(extent);
179+
swoc::FixedBufferWriter w(const_cast<char *>(text.data()), text.size());
180+
w.restrict(trailer.size());
181+
w.print(header_fmt, groups.size());
182+
for (auto g : groups) {
183+
printer(w, g.get());
184+
}
185+
w.restore(trailer.size());
186+
w.write(trailer);
187+
return text;
188+
}
189+
152190
} // namespace
153191

154192
void
@@ -318,56 +356,71 @@ ConnectionTracker::get_outbound_groups(std::vector<std::shared_ptr<Group const>>
318356
}
319357
}
320358

359+
void
360+
ConnectionTracker::get_inbound_groups(std::vector<std::shared_ptr<Group const>> &groups)
361+
{
362+
std::lock_guard<std::mutex> lock(_inbound_table._mutex); // TABLE LOCK
363+
groups.resize(0);
364+
groups.reserve(_inbound_table._table.size());
365+
for (auto &&[key, group] : _inbound_table._table) {
366+
groups.push_back(group);
367+
}
368+
}
369+
321370
std::string
322-
ConnectionTracker::outbound_to_json_string()
371+
ConnectionTracker::inbound_to_json_string()
323372
{
324-
std::string text;
325-
size_t extent = 0;
326-
static const swoc::bwf::Format header_fmt{R"({{"count": {}, "list": [
327-
)"};
328-
static const swoc::bwf::Format item_fmt{
329-
R"( {{"type": "{}", "ip": "{}", "fqdn": "{}", "current": {}, "max": {}, "blocked": {}, "alert": {}}},
330-
)"};
331-
static const std::string_view trailer{" \n]}"};
373+
std::vector<std::shared_ptr<Group const>> groups;
374+
self_type::get_inbound_groups(groups);
375+
return Groups_To_JSON(groups);
376+
}
332377

333-
static const auto printer = [](swoc::BufferWriter &w, Group const *g) -> swoc::BufferWriter & {
334-
w.print(item_fmt, g->_match_type, g->_addr, g->_fqdn, g->_count.load(), g->_count_max.load(), g->_blocked.load(),
335-
g->get_last_alert_epoch_time());
336-
return w;
337-
};
378+
std::string
379+
ConnectionTracker::outbound_to_json_string()
380+
{
381+
std::vector<std::shared_ptr<Group const>> groups;
382+
self_type::get_outbound_groups(groups);
383+
return Groups_To_JSON(groups);
384+
}
338385

339-
swoc::FixedBufferWriter null_bw{nullptr}; // Empty buffer for sizing work.
386+
void
387+
ConnectionTracker::dump_outbound(FILE *f)
388+
{
340389
std::vector<std::shared_ptr<Group const>> groups;
341390

342391
self_type::get_outbound_groups(groups);
343392

344-
null_bw.print(header_fmt, groups.size()).extent();
345-
for (auto g : groups) {
346-
printer(null_bw, g.get());
347-
}
348-
extent = null_bw.extent() + trailer.size() - 2; // 2 for the trailing comma newline that will get clipped.
393+
if (groups.size()) {
394+
fprintf(f, "\n[O] Peer Connection Tracking\n%7s | %5s | %24s | %33s | %8s |\n", "Current", "Block", "Address", "Hostname Hash",
395+
"Match");
396+
fprintf(f, "------|-------|--------------------------|-----------------------------------|----------|\n");
349397

350-
text.resize(extent);
351-
swoc::FixedBufferWriter w(const_cast<char *>(text.data()), text.size());
352-
w.restrict(trailer.size());
353-
w.print(header_fmt, groups.size());
354-
for (auto g : groups) {
355-
printer(w, g.get());
398+
for (std::shared_ptr<Group const> g : groups) {
399+
swoc::LocalBufferWriter<128> w;
400+
w.print("{:7} | {:5} | {:24} | {:33} | {:8} |\n", g->_count.load(), g->_blocked.load(), g->_addr, g->_hash, g->_match_type);
401+
fwrite(w.data(), w.size(), 1, f);
402+
}
403+
404+
fprintf(f, "------|-------|--------------------------|-----------------------------------|----------|\n");
356405
}
357-
w.restore(trailer.size());
358-
w.write(trailer);
359-
return text;
360406
}
361407

362408
void
363409
ConnectionTracker::dump(FILE *f)
410+
{
411+
dump_outbound(f);
412+
dump_inbound(f);
413+
}
414+
415+
void
416+
ConnectionTracker::dump_inbound(FILE *f)
364417
{
365418
std::vector<std::shared_ptr<Group const>> groups;
366419

367-
self_type::get_outbound_groups(groups);
420+
self_type::get_inbound_groups(groups);
368421

369422
if (groups.size()) {
370-
fprintf(f, "\nPeer Connection Tracking\n%7s | %5s | %24s | %33s | %8s |\n", "Current", "Block", "Address", "Hostname Hash",
423+
fprintf(f, "\n[I] Peer Connection Tracking\n%7s | %5s | %24s | %33s | %8s |\n", "Current", "Block", "Address", "Hostname Hash",
371424
"Match");
372425
fprintf(f, "------|-------|--------------------------|-----------------------------------|----------|\n");
373426

0 commit comments

Comments
 (0)