Skip to contentSkip to conversation

json: support element output

PATCH In review Georg Pfuetzenreuter 2 participants 2 messages Dec 3, 2025 – Jan 14 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 json: support element output

From: Georg Pfuetzenreuter <[email protected]>

JSON was skipped for get element operations. Resolve this by introducing JSON output handling for set elements - the structure is kept close to what's already implemented for list set.

---
 include/json.h |  1 +
 src/json.c     | 36 ++++++++++++++++++++++++++++++++++++
 src/rule.c     |  3 +++
 3 files changed, 40 insertions(+)
include/json.h +1 -0
index 3b8d045f87bb..8c15e92ed90f 100644
@@ -98,6 +98,7 @@ json_t *optstrip_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 json_t *xt_stmt_json(const struct stmt *stmt, struct output_ctx *octx);
 
 int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd);
+int do_get_setelems_json(struct netlink_ctx *ctx, struct cmd *cmd, bool reset);
 
 int nft_parse_json_buffer(struct nft_ctx *nft, const char *buf,
 			  struct list_head *msgs, struct list_head *cmds);
src/json.c +36 -0
index 9fb6d715a53d..e205c508e36b 100644
@@ -2170,6 +2170,42 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	return 0;
 }
 
+int do_get_setelems_json(struct netlink_ctx *ctx, struct cmd *cmd, bool reset)
+{
+	struct set *set, *new_set;
+	struct expr *init;
+	json_t *root = json_array();
+	int err;
+
+	set = cmd->elem.set;
+
+	if (set_is_non_concat_range(set))
+		init = get_set_intervals(set, cmd->expr);
+	else
+		init = cmd->expr;
+
+	new_set = set_clone(set);
+
+	json_array_insert_new(root, 0, generate_json_metainfo());
+
+	err = netlink_get_setelem(ctx, &cmd->handle, &cmd->location,
+				  cmd->elem.set, new_set, init, reset);
+	if (err >= 0)
+		json_array_append_new(root, set_print_json(&ctx->nft->output, new_set));
+
+	if (set_is_non_concat_range(set))
+		expr_free(init);
+
+	set_free(new_set);
+
+	root = nft_json_pack("{s:o}", "nftables", root);
+	json_dumpf(root, ctx->nft->output.output_fp, 0);
+	json_decref(root);
+	fprintf(ctx->nft->output.output_fp, "\n");
+	fflush(ctx->nft->output.output_fp);
+	return 0;
+}
+
 static void monitor_print_json(struct netlink_mon_handler *monh,
 			       const char *cmd, json_t *obj)
 {
src/rule.c +3 -0
index 8f8b77f1e883..5d3382632728 100644
@@ -2681,6 +2681,9 @@ static int do_get_setelems(struct netlink_ctx *ctx, struct cmd *cmd, bool reset)
 	struct expr *init;
 	int err;
 
+	if (nft_output_json(&ctx->nft->output))
+		return do_get_setelems_json(ctx, cmd, reset);
+
 	set = cmd->elem.set;
 
 	/* Create a list of elements based of what we got from command line. */
Signature · 1 line
2.52.0
reviewer
#1 lore raw

Hi Georg,

Sorry for the late reply, I missed your mail last year and am still recovering from the "big reset". ;)

Quoted 7 lines · Georg Pfuetzenreuter

From: Georg Pfuetzenreuter <[email protected]>

JSON was skipped for get element operations. Resolve this by introducing JSON output handling for set elements - the structure is kept close to what's already implemented for list set.

Patch looks fine apart from:

[...]

src/json.c int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd) · line 2178

Quoted 6 lines
src/json.c +6 -0
+int do_get_setelems_json(struct netlink_ctx *ctx, struct cmd *cmd, bool reset)
+{
+	struct set *set, *new_set;
+	struct expr *init;
+	json_t *root = json_array();
+	int err;

Please stick to reverse christmas tree notation here, i.e. put the json_t *root on top of the list.

Quoted 6 lines
src/json.c +6 -0
+
+	set = cmd->elem.set;
+
+	if (set_is_non_concat_range(set))
+		init = get_set_intervals(set, cmd->expr);
+	else

src/json.c int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd) · line 2194

Quoted 10 lines
src/json.c +10 -0
+		init = cmd->expr;
+
+	new_set = set_clone(set);
+
+	json_array_insert_new(root, 0, generate_json_metainfo());
+
+	err = netlink_get_setelem(ctx, &cmd->handle, &cmd->location,
+				  cmd->elem.set, new_set, init, reset);
+	if (err >= 0)
+		json_array_append_new(root, set_print_json(&ctx->nft->output, new_set));

This line exceeds 80 columns, no?

Also, could you please add a test case? A simple one in tests/shell/testcases/json/ to make sure 'nft -j get element' output is as expected should be fine.

Thanks, Phil