Skip to contentSkip to conversation

Solves Bug 1462 - `nft -j list set` does not show counters

PATCH In review Gopal Yadav 2 participants 5 messages Oct 3, 2020 – Oct 7, 2020 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Solves Bug 1462 - `nft -j list set` does not show counters

Solves Bug 1462 - nft -j list set does not show counters

---
 src/json.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/json.c +5 -1
index 5856f9fc..6ad48fdd 100644
@@ -589,7 +589,7 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
 		return NULL;
 
 	/* these element attributes require formal set elem syntax */
-	if (expr->timeout || expr->expiration || expr->comment) {
+	if (expr->timeout || expr->expiration || expr->comment || expr->stmt) {
 		root = json_pack("{s:o}", "val", root);
 
 		if (expr->timeout) {
@@ -604,6 +604,10 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
 			tmp = json_string(expr->comment);
 			json_object_set_new(root, "comment", tmp);
 		}
+		if(expr->stmt) {
+			tmp = expr->stmt->ops->json(expr->stmt, octx);
+			json_object_update(root, tmp);
+		}
 		return json_pack("{s:o}", "elem", root);
 	}
 
Signature · 1 line
2.17.1
#1 lore raw

Hi Gopal,

Quoted 21 lines · Gopal Yadav

Solves Bug 1462 - nft -j list set does not show counters

---
  src/json.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
src/json.c +1 -1
index 5856f9fc..6ad48fdd 100644
@@ -589,7 +589,7 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
  		return NULL;
 
  	/* these element attributes require formal set elem syntax */
-	if (expr->timeout || expr->expiration || expr->comment) {
+	if (expr->timeout || expr->expiration || expr->comment || expr->stmt) {
  		root = json_pack("{s:o}", "val", root);
 
  		if (expr->timeout) {

src/json.c json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx) · line 608

Quoted 5 lines
src/json.c +2 -0
@@ -604,6 +604,10 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx)
  			tmp = json_string(expr->comment);
  			json_object_set_new(root, "comment", tmp);
  		}
+		if(expr->stmt) {
+			tmp = expr->stmt->ops->json(expr->stmt, octx);

You can compact this using stmt_print_json

src/json.c json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx) · line 609

Quoted 1 line
src/json.c +1 -0
+			json_object_update(root, tmp);

ASAN reports memleaks when using json_object_update. You should use json_object_update_new, or maybe json_object_update_missing_new to ensure only new keys are created.

Quoted 5 lines
src/json.c +1 -0
+		}
  		return json_pack("{s:o}", "elem", root);
  	}
 
#2 lore raw
Quoted 6 lines · Jose M. Guisado

Hi Gopal,

Quoted 1 line · Gopal Yadav

+ tmp = expr->stmt->ops->json(expr->stmt, octx);

Quoted 7 lines

You can compact this using stmt_print_json

Quoted 1 line

+ json_object_update(root, tmp);

ASAN reports memleaks when using json_object_update. You should use json_object_update_new, or maybe json_object_update_missing_new to ensure only new keys are created.

Should I always run ASAN before submitting patches as a regular practice? json_object_update_missing_new() was raising a warning so I have used json_object_update_missing() in the updated patch.

#3 lore raw
Quoted 1 line · Gopal Yadav

Should I always run ASAN before submitting patches as a regular practice?

I usually check for leaks when submitting patches, using either ASAN or Valgrind.

Quoted 2 lines

json_object_update_missing_new() was raising a warning so I have used json_object_update_missing() in the updated patch.

I've been unable to reproduce said warning when using json_object_update_missing_new.

You need to use the *_new function, because it will call json_decref on tmp for you. If not the reference to tmp is leaked.

You should version your patches, have a look at the patch submission guidelines in the nftables wiki [1]

[1] https://wiki.nftables.org/wiki-nftables/index.php/Portal:DeveloperDocs/Patch_submission_guidelines

#4 lore raw
Quoted 7 lines · Jose M. Guisado
Quoted 1 line · Gopal Yadav

Should I always run ASAN before submitting patches as a regular practice?

I usually check for leaks when submitting patches, using either ASAN or Valgrind.

Quoted 8 lines
Quoted 2 lines

json_object_update_missing_new() was raising a warning so I have used json_object_update_missing() in the updated patch.

I've been unable to reproduce said warning when using json_object_update_missing_new.

You need to use the *_new function, because it will call json_decref on tmp for you. If not the reference to tmp is leaked.

Since on using *_new() build fails, should I call json_decref(tmp) explicitly after json_object_update_missing()?

I couldn't get ASAN to run, but I ran valgrind by doing valgrind nft list ruleset on both versions, with & without json_decref(tmp). Both of them produce the same output which says no leaks:

==5967== Memcheck, a memory error detector
==5967== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==5967== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==5967== Command: nft list ruleset
==5967==

table inet dev {
    set ports_udp {
        type inet_service
        size 65536
        flags dynamic,timeout
        timeout 30d
        elements = { 53 expires 29d6h14m26s268ms counter packets 0 bytes 0 }
    }
}

==5967==
==5967== HEAP SUMMARY:
==5967== in use at exit: 0 bytes in 0 blocks
==5967== total heap usage: 78 allocs, 78 frees, 390,728 bytes allocated
==5967==
==5967== All heap blocks were freed -- no leaks are possible ==5967==
==5967== For counts of detected and suppressed errors, rerun with: -v ==5967== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)