Skip to contentSkip to conversation

ematch: support JSON output

PATCH In review Stephen Hemminger 2 participants 2 messages Mar 14, 2024 – Mar 15, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ematch: support JSON output

The ematch in filter was missing support JSON output and therefore would generate bogus output.

Note: support for JSON output with ipt would be difficult to implement since xtables API doesn't have a JSON API, and anyway ipt is legacy. Therefore attempts to JSON output for ematch ipt generates an error.

---
 tc/em_canid.c | 31 +++++++++-------
 tc/em_cmp.c   | 30 ++++++++++------
 tc/em_ipset.c |  9 +++--
 tc/em_ipt.c   |  6 ++++
 tc/em_meta.c  | 99 ++++++++++++++++++++++++++++-----------------------
 tc/em_nbyte.c | 12 ++++---
 tc/em_u32.c   | 10 +++---
 tc/m_ematch.c | 47 +++++++++++++++---------
 8 files changed, 149 insertions(+), 95 deletions(-)
tc/em_canid.c +18 -13
index 228547529134..815ed8c7bce0 100644
@@ -154,24 +154,29 @@ static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 
 		if (pcfltr->can_id & CAN_EFF_FLAG) {
 			if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_EFF_MASK))
-				fprintf(fd, "eff 0x%"PRIX32,
-						pcfltr->can_id & CAN_EFF_MASK);
-			else
-				fprintf(fd, "eff 0x%"PRIX32":0x%"PRIX32,
-						pcfltr->can_id & CAN_EFF_MASK,
-						pcfltr->can_mask & CAN_EFF_MASK);
+				print_0xhex(PRINT_ANY, "eff", "eff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_EFF_MASK);
+			else {
+				print_0xhex(PRINT_ANY, "eff", "eff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_EFF_MASK);
+				print_0xhex(PRINT_ANY, "mask", ":0x%"PRIX32,
+					    pcfltr->can_mask & CAN_EFF_MASK);
+			}
 		} else {
+			
 			if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_SFF_MASK))
-				fprintf(fd, "sff 0x%"PRIX32,
-						pcfltr->can_id & CAN_SFF_MASK);
-			else
-				fprintf(fd, "sff 0x%"PRIX32":0x%"PRIX32,
-						pcfltr->can_id & CAN_SFF_MASK,
-						pcfltr->can_mask & CAN_SFF_MASK);
+				print_0xhex(PRINT_ANY, "sff", "sff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_SFF_MASK);
+			else {
+				print_0xhex(PRINT_ANY, "sff", "sff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_SFF_MASK);
+				print_0xhex(PRINT_ANY, "mask", ":0x%"PRIX32,
+					    pcfltr->can_mask & CAN_SFF_MASK);
+			}
 		}
 
 		if ((i + 1) < rules_count)
-			fprintf(fd, " ");
+			print_string(PRINT_FP, NULL, " ", NULL);
 	}
 
 	return 0;
tc/em_cmp.c +20 -10
index dfd123df1e10..9e2d14077c6c 100644
@@ -138,6 +138,8 @@ static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 			  int data_len)
 {
 	struct tcf_em_cmp *cmp = data;
+	const char *align = NULL;
+	const char *op = NULL;
 
 	if (data_len < sizeof(*cmp)) {
 		fprintf(stderr, "CMP header size mismatch\n");
@@ -145,28 +147,36 @@ static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 	}
 
 	if (cmp->align == TCF_EM_ALIGN_U8)
-		fprintf(fd, "u8 ");
+		align = "u8";
 	else if (cmp->align == TCF_EM_ALIGN_U16)
-		fprintf(fd, "u16 ");
+		align = "u16";
 	else if (cmp->align == TCF_EM_ALIGN_U32)
-		fprintf(fd, "u32 ");
+		align = "u32";
 
-	fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);
+	print_uint(PRINT_JSON, "align", "%u ", cmp->align);
+	if (align)
+		print_string(PRINT_FP, NULL, "%s ", align);
+
+	print_uint(PRINT_ANY, "offset", "at %u ", cmp->off);
+	print_uint(PRINT_ANY, "layer", "layer %u ", cmp->layer);
 
 	if (cmp->mask)
-		fprintf(fd, "mask 0x%x ", cmp->mask);
+		print_0xhex(PRINT_ANY, "mask", "mask 0x%x ", cmp->mask);
 
 	if (cmp->flags & TCF_EM_CMP_TRANS)
-		fprintf(fd, "trans ");
+		print_null(PRINT_ANY, "trans", "trans ", NULL);
 
 	if (cmp->opnd == TCF_EM_OPND_EQ)
-		fprintf(fd, "eq ");
+		op = "eq";
 	else if (cmp->opnd == TCF_EM_OPND_LT)
-		fprintf(fd, "lt ");
+		op = "lt";
 	else if (cmp->opnd == TCF_EM_OPND_GT)
-		fprintf(fd, "gt ");
+		op = "gt";
+
+	if (op)
+		print_string(PRINT_ANY, "opnd", "%s ", op);
 
-	fprintf(fd, "%d", cmp->val);
+	print_uint(PRINT_ANY, "val", "%u", cmp->val);
 
 	return 0;
 }
tc/em_ipset.c +7 -2
index f97abaf3cfb7..ce2c8e75fc58 100644
@@ -243,10 +243,15 @@ static int ipset_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 
 	if (get_set_byid(setname, set_info->index))
 		return -1;
-	fputs(setname, fd);
+
+	open_json_array(PRINT_ANY, setname);
+
 	for (i = 1; i <= set_info->dim; i++) {
-		fprintf(fd, "%s%s", i == 1 ? " " : ",", set_info->flags & (1 << i) ? "src" : "dst");
+		print_string(PRINT_FP, NULL, "%s", i == 1 ? " " : ",");
+		print_string(PRINT_ANY, NULL, "%s", 
+			     set_info->flags & (1 << i) ? "src" : "dst");
 	}
+	close_json_array(PRINT_JSON, NULL);
 
 	return 0;
 }
tc/em_ipt.c +6 -0
index 69efefd8c5e3..6102b5513853 100644
@@ -175,6 +175,12 @@ static int em_ipt_print_epot(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 	const char *mname;
 	__u8 nfproto;
 
+	/* xtables-legacy doesn't support JSON print so skip it */
+	if (is_json_context()) {
+		fprintf(stderr, "xtables-legacy json not supported\n");
+		return -1;
+	}
+	
 	if (parse_rtattr(tb, TCA_EM_IPT_MAX, data, data_len) < 0)
 		return -1;
 
tc/em_meta.c +55 -44

165 lines not on this page · raw message

tc/em_nbyte.c +8 -4
index 9f421fb423a6..cfcd1b413baa 100644
@@ -116,13 +116,17 @@ static int nbyte_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 
 	needle = data + sizeof(*nb);
 
+	open_json_array(PRINT_JSON, "needle");
 	for (i = 0; i < nb->len; i++)
-		fprintf(fd, "%02x ", needle[i]);
+		print_0xhex(PRINT_ANY, NULL, "%02x ", needle[i]);
 
-	fprintf(fd, "\"");
+	close_json_array(PRINT_ANY, "\"");
 	for (i = 0; i < nb->len; i++)
-		fprintf(fd, "%c", isprint(needle[i]) ? needle[i] : '.');
-	fprintf(fd, "\" at %d layer %d", nb->off, nb->layer);
+		print_0xhex(PRINT_FP, NULL, "%c",
+			    isprint(needle[i]) ? needle[i] : '.');
+
+	print_uint(PRINT_ANY, "offset", "\" at %u ", nb->off);
+	print_uint(PRINT_ANY, "layer", "layer %u", nb->layer);
 
 	return 0;
 }
tc/em_u32.c +5 -5
index a83382ba4417..88feb0de8317 100644
@@ -153,11 +153,11 @@ static int u32_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 		return -1;
 	}
 
-	fprintf(fd, "%08x/%08x at %s%d",
-	    (unsigned int) ntohl(u_key->val),
-	    (unsigned int) ntohl(u_key->mask),
-	    u_key->offmask ? "nexthdr+" : "",
-	    u_key->off);
+	print_0xhex(PRINT_ANY, "val", "%08x", ntohl(u_key->val));
+	print_0xhex(PRINT_ANY, "mask", "/%08x at ", ntohl(u_key->mask));
+	if (u_key->offmask)
+		print_null(PRINT_ANY, "nexthdr", "nexthdr+", NULL);
+	print_int(PRINT_ANY, "offset", "%d", u_key->off);
 
 	return 0;
 }
tc/m_ematch.c +30 -17

94 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 12 lines · Stephen Hemminger
tc/em_canid.c +0 -4
index 228547529134..815ed8c7bce0 100644
@@ -154,24 +154,29 @@ static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 
 		if (pcfltr->can_id & CAN_EFF_FLAG) {
 			if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_EFF_MASK))
-				fprintf(fd, "eff 0x%"PRIX32,
-						pcfltr->can_id & CAN_EFF_MASK);
-			else
-				fprintf(fd, "eff 0x%"PRIX32":0x%"PRIX32,

tc/em_canid.c static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, · line 164

Quoted 10 lines
tc/em_canid.c +8 -2
-						pcfltr->can_id & CAN_EFF_MASK,
-						pcfltr->can_mask & CAN_EFF_MASK);
+				print_0xhex(PRINT_ANY, "eff", "eff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_EFF_MASK);
+			else {
+				print_0xhex(PRINT_ANY, "eff", "eff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_EFF_MASK);
+				print_0xhex(PRINT_ANY, "mask", ":0x%"PRIX32,
+					    pcfltr->can_mask & CAN_EFF_MASK);
+			}

if the else branch has {}, the first one should as well.

tc/em_canid.c static int canid_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, · line 166

Quoted 2 lines
tc/em_canid.c +1 -0
 		} else {
+			

unneeded extra newline

Quoted 67 lines
tc/em_canid.c +9 -7
 			if (pcfltr->can_mask == (CAN_EFF_FLAG | CAN_SFF_MASK))
-				fprintf(fd, "sff 0x%"PRIX32,
-						pcfltr->can_id & CAN_SFF_MASK);
-			else
-				fprintf(fd, "sff 0x%"PRIX32":0x%"PRIX32,
-						pcfltr->can_id & CAN_SFF_MASK,
-						pcfltr->can_mask & CAN_SFF_MASK);
+				print_0xhex(PRINT_ANY, "sff", "sff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_SFF_MASK);
+			else {
+				print_0xhex(PRINT_ANY, "sff", "sff 0x%"PRIX32,
+					    pcfltr->can_id & CAN_SFF_MASK);
+				print_0xhex(PRINT_ANY, "mask", ":0x%"PRIX32,
+					    pcfltr->can_mask & CAN_SFF_MASK);
+			}
 		}
 
 		if ((i + 1) < rules_count)
-			fprintf(fd, " ");
+			print_string(PRINT_FP, NULL, " ", NULL);
 	}
 
 	return 0;
tc/em_cmp.c +13 -7
index dfd123df1e10..9e2d14077c6c 100644
@@ -138,6 +138,8 @@ static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 			  int data_len)
 {
 	struct tcf_em_cmp *cmp = data;
+	const char *align = NULL;
+	const char *op = NULL;
 
 	if (data_len < sizeof(*cmp)) {
 		fprintf(stderr, "CMP header size mismatch\n");
@@ -145,28 +147,36 @@ static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data,
 	}
 
 	if (cmp->align == TCF_EM_ALIGN_U8)
-		fprintf(fd, "u8 ");
+		align = "u8";
 	else if (cmp->align == TCF_EM_ALIGN_U16)
-		fprintf(fd, "u16 ");
+		align = "u16";
 	else if (cmp->align == TCF_EM_ALIGN_U32)
-		fprintf(fd, "u32 ");
+		align = "u32";
 
-	fprintf(fd, "at %d layer %d ", cmp->off, cmp->layer);
+	print_uint(PRINT_JSON, "align", "%u ", cmp->align);
+	if (align)
+		print_string(PRINT_FP, NULL, "%s ", align);
+
+	print_uint(PRINT_ANY, "offset", "at %u ", cmp->off);
+	print_uint(PRINT_ANY, "layer", "layer %u ", cmp->layer);
 
 	if (cmp->mask)
-		fprintf(fd, "mask 0x%x ", cmp->mask);
+		print_0xhex(PRINT_ANY, "mask", "mask 0x%x ", cmp->mask);
 
 	if (cmp->flags & TCF_EM_CMP_TRANS)
-		fprintf(fd, "trans ");
+		print_null(PRINT_ANY, "trans", "trans ", NULL);
 
 	if (cmp->opnd == TCF_EM_OPND_EQ)
-		fprintf(fd, "eq ");

tc/em_cmp.c static int cmp_print_eopt(FILE *fd, struct tcf_ematch_hdr *hdr, void *data, · line 177

Quoted 10 lines
tc/em_cmp.c +6 -2
+		op = "eq";
 	else if (cmp->opnd == TCF_EM_OPND_LT)
-		fprintf(fd, "lt ");
+		op = "lt";
 	else if (cmp->opnd == TCF_EM_OPND_GT)
-		fprintf(fd, "gt ");
+		op = "gt";
+
+	if (op)
+		print_string(PRINT_ANY, "opnd", "%s ", op);

seems like a change in output which tends to break the tdc suite. Please cc Jamal on tc patches.

Quoted 26 lines
tc/em_cmp.c +7 -9
-	fprintf(fd, "%d", cmp->val);
+	print_uint(PRINT_ANY, "val", "%u", cmp->val);
 
 	return 0;
 }
 
@@ -436,53 +445,51 @@ static inline int print_value(FILE *fd, int type, struct rtattr *rta)
 	}
 
 	switch (type) {
-		case TCF_META_TYPE_INT:
-			if (RTA_PAYLOAD(rta) < sizeof(__u32)) {
-				fprintf(stderr, "meta int type value TLV " \
-				    "size mismatch.\n");
-				return -1;
-			}
-			fprintf(fd, "%d", rta_getattr_u32(rta));
-			break;
+	case TCF_META_TYPE_INT:
+		if (RTA_PAYLOAD(rta) < sizeof(__u32)) {
+			fprintf(stderr,
+				"meta int type value TLV size mismatch.\n");
+			return -1;
+		}

tc/em_cmp.c static inline int print_value(FILE *fd, int type, struct rtattr *rta) · line 460

Quoted 10 lines
tc/em_cmp.c +5 -3
+		print_uint(PRINT_ANY, "value", "%u", rta_getattr_u32(rta));
+		break;
 
-		case TCF_META_TYPE_VAR:
-			print_binary(fd, RTA_DATA(rta), RTA_PAYLOAD(rta));
-			break;
+	case TCF_META_TYPE_VAR:
+		print_binary(RTA_DATA(rta), RTA_PAYLOAD(rta));
+		break;
 	}

whitespace cleanup should be a separate patch.

in general this is a large patch. It would be better as a series

pw-bot: cr