Add ability to get decode packet in summary tshark style format.

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

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 37cce4868e..72be5a0c06 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 (verbose|hex)\n"
+                       "set format (dissect|hex|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, 
"verbose#hex");
+       TOKEN_STRING_INITIALIZER(struct cmd_set_format_result, value, 
"dissect#hex#verbose");
 
 static cmdline_parse_inst_t cmd_set_format = {
        .f = cmd_set_format_parsed,
        .data = NULL,
-       .help_str = "set format verbose|hex",
+       .help_str = "set format dissect|hex|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 f30bdfc7ff..36b7aa1307 100644
--- a/app/test-pmd/config.c
+++ b/app/test-pmd/config.c
@@ -6271,12 +6271,23 @@ set_verbose_level(uint16_t vb_level)
 void
 set_output_format(const char *mode)
 {
-       if (!strcmp(mode, "verbose"))
-               output_format = OUTPUT_MODE_VERBOSE;
-       else if (!strcmp(mode, "hex"))
-               output_format = OUTPUT_MODE_HEX;
-       else
-               fprintf(stderr, "Unknown output format '%s'\n", mode);
+       static const char * const output_formats[] = {
+               [OUTPUT_MODE_VERBOSE] = "verbose",
+               [OUTPUT_MODE_HEX]     = "hex",
+               [OUTPUT_MODE_DISSECT] = "dissect",
+       };
+
+       printf("Change output format from %s to %s\n",
+              output_formats[output_format], mode);
+
+       for (unsigned int i = 0; i < RTE_DIM(output_formats); i++) {
+               if (strcasecmp(mode, output_formats[i]) == 0) {
+                       output_format = i;
+                       return;
+               }
+       }
+
+       fprintf(stderr, "Unknown output format '%s'\n", mode);
 }
 
 void
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index 94e8f59ef0..66b0317b61 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -491,6 +491,7 @@ enum dcb_mode_enable
 enum output_mode {
        OUTPUT_MODE_VERBOSE = 0,
        OUTPUT_MODE_HEX,
+       OUTPUT_MODE_DISSECT,
 };
 
 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 130821fddb..551e684e4c 100644
--- a/app/test-pmd/util.c
+++ b/app/test-pmd/util.c
@@ -8,6 +8,7 @@
 #include <rte_bitops.h>
 #include <rte_net.h>
 #include <rte_mbuf.h>
+#include <rte_dissect.h>
 #include <rte_ether.h>
 #include <rte_vxlan.h>
 #include <rte_ethdev.h>
@@ -311,6 +312,38 @@ dump_pkt_hex(FILE *outf, uint16_t port_id, uint16_t queue,
        }
 }
 
+/* Brief tshark style one line output which is
+ * number time_delta Source Destination Protocol len info
+ */
+static void
+dump_pkt_brief(FILE *outf, uint16_t port, uint16_t queue,
+              struct rte_mbuf *pkts[], uint16_t nb_pkts, int is_rx)
+{
+       static uint64_t start_cycles;
+       static RTE_ATOMIC(uint64_t) packet_count = 1;
+       uint64_t now, count;
+       double interval;
+
+       /* Compute time interval from the first packet received */
+       now = rte_rdtsc();
+       if (start_cycles == 0) {
+               start_cycles = now;
+               printf("Seq#   Time        Port:Que R Description\n");
+       }
+       interval = (double)(now - start_cycles) / (double)rte_get_tsc_hz();
+
+       /* Packet counter needs to be thread safe */
+       count = rte_atomic_fetch_add_explicit(&packet_count, nb_pkts, 
rte_memory_order_relaxed);
+
+       for (uint16_t i = 0; i < nb_pkts; i++) {
+               const struct rte_mbuf *mb = pkts[i];
+               char str[256];
+
+               rte_dissect_mbuf(str, sizeof(str), mb, 0);
+               fprintf(outf, "%6"PRIu64" %11.9f %4u:%-3u %c %s\n",
+                      count + i, interval, port, queue, is_rx ? 'R' : 'T', 
str);
+       }
+}
 
 static void
 dump_pkt_burst(uint16_t port_id, uint16_t queue, struct rte_mbuf *pkts[],
@@ -332,6 +365,9 @@ dump_pkt_burst(uint16_t port_id, uint16_t queue, struct 
rte_mbuf *pkts[],
        case OUTPUT_MODE_HEX:
                dump_pkt_hex(outf, port_id, queue, pkts, nb_pkts, is_rx);
                break;
+       case OUTPUT_MODE_DISSECT:
+               dump_pkt_brief(outf, port_id, queue, pkts, nb_pkts, is_rx);
+               break;
        default:
                return;
        }
diff --git a/doc/guides/testpmd_app_ug/testpmd_funcs.rst 
b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
index 9406af3225..705b3dc3d5 100644
--- a/doc/guides/testpmd_app_ug/testpmd_funcs.rst
+++ b/doc/guides/testpmd_app_ug/testpmd_funcs.rst
@@ -677,12 +677,13 @@ set format
 
 Chose the output format for packet debug log::
 
-   testpmd> set format verbose|hex
+   testpmd> set format dissect|hex|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
 
 
 set verbose
-- 
2.45.2

Reply via email to