DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	Aman Singh <aman.deep.singh@intel.com>
Subject: [PATCH v9 8/8] test-pmd: add a JSON packet output
Date: Sat, 28 Sep 2024 09:18:38 -0700	[thread overview]
Message-ID: <20240928162035.849326-9-stephen@networkplumber.org> (raw)
In-Reply-To: <20240928162035.849326-1-stephen@networkplumber.org>

When doing automated testing it is useful to show packet meta
data in JSON.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test-pmd/cmdline.c                      |   6 +-
 app/test-pmd/config.c                       |   3 +
 app/test-pmd/testpmd.h                      |   1 +
 app/test-pmd/util.c                         | 167 ++++++++++++++++++++
 doc/guides/testpmd_app_ug/testpmd_funcs.rst |   7 +-
 5 files changed, 178 insertions(+), 6 deletions(-)

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 72be5a0c06..3d52ad4c72 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -305,7 +305,7 @@ static void cmd_help_long_parsed(void *parsed_result,
 			"set output (filename)\n"
 			"    Set the packet debug log file\n\n"
 
-			"set format (dissect|hex|verbose)\n"
+			"set format (dissect|hex|json|verbose)\n"
 			"    Set the format of packet log\\n"
 
 			"set log global|(type) (level)\n"
@@ -3918,12 +3918,12 @@ static cmdline_parse_token_string_t cmd_set_format_set =
 static cmdline_parse_token_string_t cmd_set_format_output =
 	TOKEN_STRING_INITIALIZER(struct cmd_set_format_result, format, "format");
 static cmdline_parse_token_string_t cmd_set_format_value =
-	TOKEN_STRING_INITIALIZER(struct cmd_set_format_result, value, "dissect#hex#verbose");
+	TOKEN_STRING_INITIALIZER(struct cmd_set_format_result, value, "dissect#hex#json#verbose");
 
 static cmdline_parse_inst_t cmd_set_format = {
 	.f = cmd_set_format_parsed,
 	.data = NULL,
-	.help_str = "set format dissect|hex|verbose",
+	.help_str = "set format dissect|hex|json|verbose",
 	.tokens = {
 		(void *)&cmd_set_format_set,
 		(void *)&cmd_set_format_output,
diff --git a/app/test-pmd/config.c b/app/test-pmd/config.c
index 36b7aa1307..10ed58c08b 100644
--- a/app/test-pmd/config.c
+++ b/app/test-pmd/config.c
@@ -6275,6 +6275,9 @@ set_output_format(const char *mode)
 		[OUTPUT_MODE_VERBOSE] = "verbose",
 		[OUTPUT_MODE_HEX]     = "hex",
 		[OUTPUT_MODE_DISSECT] = "dissect",
+#ifdef RTE_HAS_JANSSON
+		[OUTPUT_MODE_JSON]    = "json",
+#endif
 	};
 
 	printf("Change output format from %s to %s\n",
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index 66b0317b61..f8bf9b7126 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -492,6 +492,7 @@ enum output_mode {
 	OUTPUT_MODE_VERBOSE = 0,
 	OUTPUT_MODE_HEX,
 	OUTPUT_MODE_DISSECT,
+	OUTPUT_MODE_JSON,
 };
 
 extern uint8_t xstats_hide_zero; /**< Hide zero values for xstats display */
diff --git a/app/test-pmd/util.c b/app/test-pmd/util.c
index 551e684e4c..7da59988db 100644
--- a/app/test-pmd/util.c
+++ b/app/test-pmd/util.c
@@ -299,6 +299,168 @@ dump_pkt_verbose(FILE *outf, uint16_t port_id, uint16_t queue,
 	}
 }
 
+#ifdef RTE_HAS_JANSSON
+
+/* Encode offload flags as JSON array */
+static json_t *
+encode_ol_flags(uint64_t flags, int is_rx)
+{
+	json_t *ol_array = json_array();
+	unsigned int i;
+
+	if (is_rx)
+		flags &= ~RTE_MBUF_F_TX_OFFLOAD_MASK;
+	else
+		flags &= RTE_MBUF_F_TX_OFFLOAD_MASK;
+
+	for (i = 0; i < 64; i++) {
+		uint64_t mask = (uint64_t)1 << i;
+		const char *name;
+
+		if (!(mask & flags))
+			continue;
+
+		if (is_rx)
+			name = rte_get_rx_ol_flag_name(mask);
+		else
+			name = rte_get_tx_ol_flag_name(mask);
+		json_array_append_new(ol_array, json_string(name));
+	}
+	return ol_array;
+}
+
+/* Encode packet type fields as JSON object */
+static json_t *
+encode_ptype(uint32_t ptype)
+{
+	if ((ptype & RTE_PTYPE_ALL_MASK) == RTE_PTYPE_UNKNOWN)
+		return json_string("UNKNOWN");
+
+	json_t *ptypes = json_array();
+	if (ptype & RTE_PTYPE_L2_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_l2_name(ptype)));
+	if (ptype & RTE_PTYPE_L3_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_l3_name(ptype)));
+	if (ptype & RTE_PTYPE_L4_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_l4_name(ptype)));
+	if (ptype & RTE_PTYPE_TUNNEL_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_tunnel_name(ptype)));
+	if (ptype & RTE_PTYPE_INNER_L2_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_inner_l2_name(ptype)));
+	if (ptype & RTE_PTYPE_INNER_L3_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_inner_l3_name(ptype)));
+	if (ptype & RTE_PTYPE_INNER_L4_MASK)
+		json_array_append(ptypes, json_string(rte_get_ptype_inner_l4_name(ptype)));
+
+	return ptypes;
+}
+
+static void
+dump_pkt_json(FILE *outf, uint16_t port_id, uint16_t queue,
+	      struct rte_mbuf *pkts[], uint16_t nb_pkts, int is_rx)
+{
+	char buf[256];
+
+	for (uint16_t i = 0; i < nb_pkts; i++) {
+		const struct rte_mbuf *mb = pkts[i];
+		struct rte_net_hdr_lens hdr_lens;
+		const struct rte_ether_hdr *eth_hdr;
+		struct rte_ether_hdr _eth_hdr;
+		uint16_t eth_type;
+		uint64_t ol_flags = mb->ol_flags;
+		const char *reason = NULL;
+		json_t *jobj;
+
+		jobj = json_object();
+		json_object_set_new(jobj, "port", json_integer(port_id));
+		json_object_set_new(jobj, "queue", json_integer(queue));
+		json_object_set_new(jobj, "is_rx", json_boolean(is_rx));
+
+		if (rte_mbuf_check(mb, 1, &reason) < 0) {
+			json_object_set_new(jobj, "invalid", json_string(reason));
+			continue;
+		}
+
+		eth_hdr = rte_pktmbuf_read(mb, 0, sizeof(_eth_hdr), &_eth_hdr);
+		eth_type = RTE_BE_TO_CPU_16(eth_hdr->ether_type);
+
+		rte_ether_format_addr(buf, sizeof(buf), &eth_hdr->dst_addr);
+		json_object_set_new(jobj, "dst", json_string(buf));
+		rte_ether_format_addr(buf, sizeof(buf), &eth_hdr->src_addr);
+		json_object_set_new(jobj, "src", json_string(buf));
+
+		snprintf(buf, sizeof(buf), "0x%04x", eth_type);
+		json_object_set_new(jobj, "type", json_string(buf));
+
+		json_object_set_new(jobj, "length", json_integer(mb->pkt_len));
+		json_object_set_new(jobj, "nb_segs", json_integer(mb->nb_segs));
+
+		if (ol_flags & RTE_MBUF_F_RX_RSS_HASH)
+			json_object_set_new(jobj, "rss_hash", json_integer(mb->hash.rss));
+
+		if (ol_flags & RTE_MBUF_F_RX_FDIR) {
+			json_t *fdir = json_object();
+
+			if (ol_flags & RTE_MBUF_F_RX_FDIR_ID)
+				json_object_set_new(fdir, "id",
+						    json_integer(mb->hash.fdir.hi));
+			else if (ol_flags & RTE_MBUF_F_RX_FDIR_FLX) {
+				json_t *bytes = json_array();
+
+				json_array_append(bytes, json_integer(mb->hash.fdir.hi));
+				json_array_append(bytes, json_integer(mb->hash.fdir.lo));
+				json_object_set_new(fdir, "flex", bytes);
+			} else {
+				json_object_set_new(fdir, "hash", json_integer(mb->hash.fdir.hash));
+				json_object_set_new(fdir, "id",  json_integer(mb->hash.fdir.id));
+			}
+		}
+
+		if (is_timestamp_enabled(mb))
+			json_object_set_new(jobj, "timestamp", json_integer(get_timestamp(mb)));
+
+		if ((is_rx && (ol_flags & RTE_MBUF_F_RX_QINQ) != 0) ||
+		    (!is_rx && (ol_flags & RTE_MBUF_F_TX_QINQ) != 0)) {
+			json_object_set_new(jobj, "vlan_tci", json_integer(mb->vlan_tci));
+			json_object_set_new(jobj, "vlan_outer_tci",
+					    json_integer(mb->vlan_tci_outer));
+		} else if ((is_rx && (ol_flags & RTE_MBUF_F_RX_VLAN) != 0) ||
+			   (!is_rx && (ol_flags & RTE_MBUF_F_TX_VLAN) != 0)) {
+			json_object_set_new(jobj, "vlan_tci", json_integer(mb->vlan_tci));
+		}
+
+		if (mb->packet_type)
+			json_object_set_new(jobj, "hw_ptype", encode_ptype(mb->packet_type));
+
+		uint32_t sw_packet_type = rte_net_get_ptype(mb, &hdr_lens, RTE_PTYPE_ALL_MASK);
+		json_object_set_new(jobj, "sw_ptype", encode_ptype(sw_packet_type));
+
+		if (sw_packet_type & RTE_PTYPE_L2_MASK)
+			json_object_set_new(jobj, "l2_len", json_integer(hdr_lens.l2_len));
+		if (sw_packet_type & RTE_PTYPE_L3_MASK)
+			json_object_set_new(jobj, "l3_len", json_integer(hdr_lens.l3_len));
+		if (sw_packet_type & RTE_PTYPE_L4_MASK)
+			json_object_set_new(jobj, "l4_len", json_integer(hdr_lens.l4_len));
+		if (sw_packet_type & RTE_PTYPE_TUNNEL_MASK)
+			json_object_set_new(jobj, "tunnel_len", json_integer(hdr_lens.tunnel_len));
+		if (sw_packet_type & RTE_PTYPE_INNER_L2_MASK)
+			json_object_set_new(jobj, "inner_l2_len",
+					    json_integer(hdr_lens.inner_l2_len));
+		if (sw_packet_type & RTE_PTYPE_INNER_L3_MASK)
+			json_object_set_new(jobj, "inner_l3_len",
+					    json_integer(hdr_lens.inner_l3_len));
+		if (sw_packet_type & RTE_PTYPE_INNER_L4_MASK)
+			json_object_set_new(jobj, "inner_l4_len",
+					    json_integer(hdr_lens.inner_l4_len));
+
+		json_object_set_new(jobj, "ol_flags", encode_ol_flags(mb->ol_flags, is_rx));
+
+		json_dumpf(jobj, outf, JSON_INDENT(4));
+		json_decref(jobj);
+	}
+}
+#endif
+
 static void
 dump_pkt_hex(FILE *outf, uint16_t port_id, uint16_t queue,
 	     struct rte_mbuf *pkts[], uint16_t nb_pkts, int is_rx)
@@ -368,6 +530,11 @@ dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
 	case OUTPUT_MODE_DISSECT:
 		dump_pkt_brief(outf, port_id, queue, pkts, nb_pkts, is_rx);
 		break;
+#ifdef RTE_HAS_JANSSON
+	case OUTPUT_MODE_JSON:
+		dump_pkt_json(outf, port_id, queue, pkts, nb_pkts, is_rx);
+		break;
+#endif
 	default:
 		return;
 	}
diff --git a/doc/guides/testpmd_app_ug/testpmd_funcs.rst b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
index 705b3dc3d5..bb62960582 100644
--- a/doc/guides/testpmd_app_ug/testpmd_funcs.rst
+++ b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
@@ -677,13 +677,14 @@ set format
 
 Chose the output format for packet debug log::
 
-   testpmd> set format dissect|hex|verbose
+   testpmd> set format dissect|hex|json|verbose
 
 Available formats are:
 
-* ``verbose`` print the packet meta data information
-* ``hex`` print the mbuf flags and data in hex
 * ``dissect`` print the packet in tshark summary format
+* ``hex`` print the mbuf flags and data in hex
+* ``json`` print the packet meta data in json
+* ``verbose`` print the packet meta data information
 
 
 set verbose
-- 
2.45.2


  parent reply	other threads:[~2024-09-28 16:21 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-12 22:01 [PATCH] test-pmd: add more packet decode options (verbose) Stephen Hemminger
2024-03-13 21:49 ` Stephen Hemminger
2024-07-05  7:13 ` David Marchand
2024-07-23  2:44 ` [PATCH v2 0/3] Add packet dissector Stephen Hemminger
2024-07-23  2:44   ` [PATCH v2 1/3] net: add new " Stephen Hemminger
2024-07-23  2:44   ` [PATCH v2 2/3] test: add test for " Stephen Hemminger
2024-07-23  2:44   ` [PATCH v2 3/3] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-07-23 20:33 ` [PATCH v3 0/3] add packet dissector function Stephen Hemminger
2024-07-23 20:33   ` [PATCH v3 1/3] net: add new packet dissector Stephen Hemminger
2024-07-23 20:33   ` [PATCH v3 2/3] test: add test for " Stephen Hemminger
2024-07-23 20:33   ` [PATCH v3 3/3] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-07-24 18:46 ` [PATCH v4 0/3] Add packet dissector Stephen Hemminger
2024-07-24 18:46   ` [PATCH v4 1/3] net: add new " Stephen Hemminger
2024-07-24 18:46   ` [PATCH v4 2/3] test: add test for " Stephen Hemminger
2024-07-24 18:46   ` [PATCH v4 3/3] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-08-01 19:04 ` [PATCH v5 0/4] Add network packet dissector Stephen Hemminger
2024-08-01 19:04   ` [PATCH v5 1/4] net: add more icmp types Stephen Hemminger
2024-08-01 19:04   ` [PATCH v5 2/4] net: add new packet dissector Stephen Hemminger
2024-08-01 19:04   ` [PATCH v5 3/4] test: add test for " Stephen Hemminger
2024-08-02  8:38     ` Bruce Richardson
2024-08-02 15:31       ` Stephen Hemminger
2024-08-02 18:06       ` Stephen Hemminger
2024-08-01 19:04   ` [PATCH v5 4/4] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-08-02 18:07 ` [PATCH v6 0/4] Add network packet dissector Stephen Hemminger
2024-08-02 18:07   ` [PATCH v6 1/4] net: add more icmp types Stephen Hemminger
2024-08-02 18:07   ` [PATCH v6 2/4] net: add new packet dissector Stephen Hemminger
2024-08-02 18:07   ` [PATCH v6 3/4] test: add test for " Stephen Hemminger
2024-08-02 18:07   ` [PATCH v6 4/4] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-08-02 19:56 ` [PATCH v7 0/4] Add network packet dissector Stephen Hemminger
2024-08-02 19:56   ` [PATCH v7 1/4] net: add more icmp types Stephen Hemminger
2024-08-02 19:56   ` [PATCH v7 2/4] net: add new packet dissector Stephen Hemminger
2024-08-02 19:56   ` [PATCH v7 3/4] test: add test for " Stephen Hemminger
2024-08-02 19:56   ` [PATCH v7 4/4] test-pmd: add more packet verbose decode options Stephen Hemminger
2024-08-20 13:42     ` Alex Chapman
2024-08-20 15:54       ` Stephen Hemminger
2024-08-22  9:04         ` Paul Szczepanek
2024-09-28 18:36           ` Stephen Hemminger
2024-09-17  3:27 ` [PATCH v8 0/7] Test-pmd packet decode enhancements Stephen Hemminger
2024-09-17  3:27   ` [PATCH v8 1/7] net: add more icmp types Stephen Hemminger
2024-09-17  3:27   ` [PATCH v8 2/7] net: add new packet dissector Stephen Hemminger
2024-09-17  3:28   ` [PATCH v8 3/7] test: add test for " Stephen Hemminger
2024-09-17  3:28   ` [PATCH v8 4/7] test-pmd: add option to redirect packet log Stephen Hemminger
2024-09-17  3:28   ` [PATCH v8 5/7] test-pmd: add hex decode Stephen Hemminger
2024-09-17  3:28   ` [PATCH v8 6/7] test-pmd: add packet dissect format Stephen Hemminger
2024-09-17  3:28   ` [PATCH v8 7/7] test-pmd: add a JSON packet output Stephen Hemminger
2024-09-28 16:18 ` [PATCH v9 0/8] test-pmd packet decoding enhancements Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 1/8] net: add more icmp types Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 2/8] net: add new packet dissector Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 3/8] mbuf: decode the hash and fdir info in rte_pktmbuf_dump Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 4/8] test: add test for packet dissector Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 5/8] test-pmd: add option to redirect packet log Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 6/8] test-pmd: add hex decode Stephen Hemminger
2024-09-28 16:18   ` [PATCH v9 7/8] test-pmd: add packet dissect format Stephen Hemminger
2024-09-28 16:18   ` Stephen Hemminger [this message]
2024-10-28  2:19 ` [PATCH v10 0/8] test-pmd packet decoding enhancements Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 1/8] net: add more icmp types Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 2/8] net: add new packet dissector Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 3/8] mbuf: decode the hash and fdir info in rte_pktmbuf_dump Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 4/8] test: add test for packet dissector Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 5/8] test-pmd: add option to redirect packet log Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 6/8] test-pmd: add hex decode Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 7/8] test-pmd: add packet dissect format Stephen Hemminger
2024-10-28  2:19   ` [PATCH v10 8/8] test-pmd: add a JSON packet output Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240928162035.849326-9-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=aman.deep.singh@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).