DPDK patches and discussions
 help / color / mirror / Atom feed
From: Shani Peretz <shperetz@nvidia.com>
To: <dev@dpdk.org>
Cc: <ferruh.yigit@amd.com>, <stephen@networkplumber.org>,
	Shani Peretz <shperetz@nvidia.com>,
	Aman Singh <aman.deep.singh@intel.com>
Subject: [PATCH v2 2/3] app/testpmd: added testpmd commands to enable/disable xstat
Date: Fri, 24 Jan 2025 13:43:34 +0200	[thread overview]
Message-ID: <20250124114336.16610-3-shperetz@nvidia.com> (raw)
In-Reply-To: <20250124114336.16610-1-shperetz@nvidia.com>

Added the following testpmd subcommands:
toggle the counter on and off
      > port (port_id) enable|disable <counter_name>
query the state of counters:
      > set xstats-hide-disabled on|off

Signed-off-by: Shani Peretz <shperetz@nvidia.com>
---
 app/test-pmd/cmdline.c           | 125 +++++++++++++++++++++++++++++++
 app/test-pmd/config.c            |  96 ++++++++++++++++++++++++
 app/test-pmd/testpmd.c           |  10 +++
 app/test-pmd/testpmd.h           |   5 ++
 lib/cmdline/cmdline_parse_bool.c |  49 ++++++++++++
 lib/cmdline/cmdline_parse_bool.h |  39 ++++++++++
 lib/cmdline/cmdline_parse_num.c  |   2 +-
 lib/cmdline/cmdline_parse_num.h  |   1 +
 lib/cmdline/meson.build          |   2 +
 lib/cmdline/version.map          |   7 ++
 10 files changed, 335 insertions(+), 1 deletion(-)
 create mode 100644 lib/cmdline/cmdline_parse_bool.c
 create mode 100644 lib/cmdline/cmdline_parse_bool.h

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 7e0666e9f6..f12715b8eb 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -45,6 +45,7 @@
 #include <cmdline_rdline.h>
 #include <cmdline_parse.h>
 #include <cmdline_parse_num.h>
+#include <cmdline_parse_bool.h>
 #include <cmdline_parse_string.h>
 #include <cmdline_parse_ipaddr.h>
 #include <cmdline_parse_etheraddr.h>
@@ -7960,6 +7961,127 @@ static cmdline_parse_inst_t cmd_set_xstats_hide_zero = {
 	},
 };
 
+/* *** SET OPTION TO DISPLAY XSTAT STATE *** */
+struct cmd_set_xstats_show_state_result {
+	cmdline_fixed_string_t keyword;
+	cmdline_fixed_string_t name;
+	uint8_t on_off;
+};
+static void
+cmd_set_xstats_show_state_parsed(void *parsed_result,
+			__rte_unused struct cmdline *cl,
+			__rte_unused void *data)
+{
+	struct cmd_set_xstats_show_state_result *res = parsed_result;
+	set_xstats_show_state(res->on_off);
+}
+
+static cmdline_parse_token_string_t cmd_set_xstats_show_state_keyword =
+	TOKEN_STRING_INITIALIZER(struct cmd_set_xstats_show_state_result,
+				 keyword, "set");
+static cmdline_parse_token_string_t cmd_set_xstats_show_state_name =
+	TOKEN_STRING_INITIALIZER(struct cmd_set_xstats_show_state_result,
+				 name, "xstats-show-state");
+static cmdline_parse_token_bool_t cmd_set_xstats_show_state_on_off =
+	TOKEN_BOOL_INITIALIZER(struct cmd_set_xstats_show_state_result,
+				 on_off);
+
+static cmdline_parse_inst_t cmd_set_xstats_show_state = {
+	.f = cmd_set_xstats_show_state_parsed,
+	.data = NULL,
+	.help_str = "set xstats-show-state on|off",
+	.tokens = {
+		(void *)&cmd_set_xstats_show_state_keyword,
+		(void *)&cmd_set_xstats_show_state_name,
+		(void *)&cmd_set_xstats_show_state_on_off,
+		NULL,
+	},
+};
+
+/* *** SET OPTION TO HIDE DISABLED XSTAT FOR XSTATS DISPLAY *** */
+struct cmd_set_xstats_hide_disabled_result {
+	cmdline_fixed_string_t keyword;
+	cmdline_fixed_string_t name;
+	uint8_t on_off;
+};
+
+static void
+cmd_set_xstats_hide_disabled_parsed(void *parsed_result,
+			__rte_unused struct cmdline *cl,
+			__rte_unused void *data)
+{
+	struct cmd_set_xstats_hide_disabled_result *res = parsed_result;
+	set_xstats_hide_disabled(res->on_off);
+}
+
+static cmdline_parse_token_string_t cmd_set_xstats_hide_disabled_keyword =
+	TOKEN_STRING_INITIALIZER(struct cmd_set_xstats_hide_disabled_result,
+				 keyword, "set");
+static cmdline_parse_token_string_t cmd_set_xstats_hide_disabled_name =
+	TOKEN_STRING_INITIALIZER(struct cmd_set_xstats_hide_disabled_result,
+				 name, "xstats-hide-disabled");
+static cmdline_parse_token_bool_t cmd_set_xstats_hide_disabled_on_off =
+	TOKEN_BOOL_INITIALIZER(struct cmd_set_xstats_hide_disabled_result,
+				 on_off);
+
+static cmdline_parse_inst_t cmd_set_xstats_hide_disabled = {
+	.f = cmd_set_xstats_hide_disabled_parsed,
+	.data = NULL,
+	.help_str = "set xstats-hide-disabled on|off",
+	.tokens = {
+		(void *)&cmd_set_xstats_hide_disabled_keyword,
+		(void *)&cmd_set_xstats_hide_disabled_name,
+		(void *)&cmd_set_xstats_hide_disabled_on_off,
+		NULL,
+	},
+};
+
+/* *** enable/disable counter by name *** */
+struct cmd_operate_set_counter_result {
+	cmdline_fixed_string_t port;
+	portid_t port_id;
+	cmdline_fixed_string_t what;
+	cmdline_multi_string_t counter_name;
+};
+
+static void
+cmd_operate_set_counter_parsed(void *parsed_result,
+				__rte_unused struct cmdline *cl,
+				__rte_unused void *data)
+{
+	struct cmd_operate_set_counter_result *res = parsed_result;
+	uint16_t on_off = strcmp(res->what, "enable") ? 0 : 1;
+
+	if ((strcmp(res->port, "port") == 0))
+		nic_xstats_set_counter(res->port_id, res->counter_name, on_off);
+}
+
+static cmdline_parse_token_string_t cmd_operate_set_counter_port =
+	TOKEN_STRING_INITIALIZER(struct cmd_operate_set_counter_result,
+			port, "port");
+static cmdline_parse_token_num_t cmd_operate_set_counter_port_id =
+	TOKEN_NUM_INITIALIZER(struct cmd_operate_set_counter_result,
+			port_id, RTE_UINT16);
+static cmdline_parse_token_string_t cmd_operate_set_counter_what =
+	TOKEN_STRING_INITIALIZER(struct cmd_operate_set_counter_result,
+				 what, "enable#disable");
+static cmdline_parse_token_string_t cmd_operate_set_counter_name =
+	TOKEN_STRING_INITIALIZER(struct cmd_operate_set_counter_result,
+			counter_name, TOKEN_STRING_MULTI);
+
+static cmdline_parse_inst_t cmd_operate_set_counter = {
+	.f = cmd_operate_set_counter_parsed,
+	.data = NULL,
+	.help_str = "port (port_id) enable|disable <counter_name>",
+	.tokens = {
+		(void *)&cmd_operate_set_counter_port,
+		(void *)&cmd_operate_set_counter_port_id,
+		(void *)&cmd_operate_set_counter_what,
+		(void *)&cmd_operate_set_counter_name,
+		NULL,
+	},
+};
+
 /* *** SET OPTION TO ENABLE MEASUREMENT OF CPU CYCLES *** */
 struct cmd_set_record_core_cycles_result {
 	cmdline_fixed_string_t keyword;
@@ -13648,6 +13770,9 @@ static cmdline_parse_ctx_t builtin_ctx[] = {
 	&cmd_set_fwd_eth_peer,
 	&cmd_set_qmap,
 	&cmd_set_xstats_hide_zero,
+	&cmd_set_xstats_show_state,
+	&cmd_set_xstats_hide_disabled,
+	&cmd_operate_set_counter,
 	&cmd_set_record_core_cycles,
 	&cmd_set_record_burst_stats,
 	&cmd_operate_port,
diff --git a/app/test-pmd/config.c b/app/test-pmd/config.c
index 4e7fb69183..c77a34fddc 100644
--- a/app/test-pmd/config.c
+++ b/app/test-pmd/config.c
@@ -393,6 +393,7 @@ nic_xstats_display(portid_t port_id)
 	struct rte_eth_xstat *xstats;
 	int cnt_xstats, idx_xstat;
 	struct rte_eth_xstat_name *xstats_names;
+	int state;
 
 	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
 		print_valid_ports();
@@ -442,6 +443,17 @@ nic_xstats_display(portid_t port_id)
 	for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
 		if (xstats_hide_zero && !xstats[idx_xstat].value)
 			continue;
+		if (xstats_hide_disabled) {
+			state = rte_eth_xstats_query_state(port_id, idx_xstat);
+			if (state == 0)
+				continue;
+		}
+		if (xstats_show_state) {
+			char opt[3] = {'D', 'E', '-'};
+			state = rte_eth_xstats_query_state(port_id, idx_xstat);
+			printf("state: %c	", state < 0 ? opt[2] : opt[state]);
+		}
+
 		printf("%s: %"PRIu64"\n",
 			xstats_names[idx_xstat].name,
 			xstats[idx_xstat].value);
@@ -6437,6 +6449,78 @@ rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
 			__func__, port_id, queue_id, on, diag);
 }
 
+void
+nic_xstats_set_counter(portid_t port_id, char *counter_name, int on)
+{
+	struct rte_eth_xstat_name *xstats_names;
+	int cnt_xstats;
+	int ret;
+	uint64_t id;
+
+	if (port_id_is_invalid(port_id, ENABLED_WARN)) {
+		print_valid_ports();
+		return;
+	}
+	if (!rte_eth_dev_is_valid_port(port_id)) {
+		fprintf(stderr, "Error: Invalid port number %i\n", port_id);
+		return;
+	}
+
+	if (counter_name == NULL) {
+		fprintf(stderr, "Error: Invalid counter name\n");
+		return;
+	}
+
+	/* Get count */
+	cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
+	if (cnt_xstats  < 0) {
+		fprintf(stderr, "Error: Cannot get count of xstats\n");
+		return;
+	}
+
+	/* Get id-name lookup table */
+	xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
+	if (xstats_names == NULL) {
+		fprintf(stderr, "Cannot allocate memory for xstats lookup\n");
+		return;
+	}
+	if (cnt_xstats != rte_eth_xstats_get_names(
+			port_id, xstats_names, cnt_xstats)) {
+		fprintf(stderr, "Error: Cannot get xstats lookup\n");
+		free(xstats_names);
+		return;
+	}
+
+	if (rte_eth_xstats_get_id_by_name(port_id, counter_name, &id) < 0) {
+		fprintf(stderr, "Cannot find xstats with a given name\n");
+		free(xstats_names);
+		return;
+	}
+
+	/* set counter */
+	ret = rte_eth_xstats_set_counter(port_id, id, on);
+	if (ret < 0) {
+		switch (ret) {
+		case -EINVAL:
+			fprintf(stderr, "failed to find %s\n", counter_name);
+			break;
+		case -ENOTSUP:
+			fprintf(stderr, "operation not supported by device\n");
+			break;
+		case -ENOSPC:
+			fprintf(stderr, "there were not enough available counters\n");
+			break;
+		case -EPERM:
+			fprintf(stderr, "operation not premitted\n");
+			break;
+		default:
+			fprintf(stderr, "operation failed - diag=%d\n", ret);
+			break;
+		}
+	}
+	free(xstats_names);
+}
+
 void
 rx_vlan_filter_set(portid_t port_id, int on)
 {
@@ -6665,6 +6749,18 @@ set_xstats_hide_zero(uint8_t on_off)
 	xstats_hide_zero = on_off;
 }
 
+void
+set_xstats_show_state(uint8_t on_off)
+{
+	xstats_show_state = on_off;
+}
+
+void
+set_xstats_hide_disabled(uint8_t on_off)
+{
+	xstats_hide_disabled = on_off;
+}
+
 void
 set_record_core_cycles(uint8_t on_off)
 {
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index ac654048df..2bafe730b9 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -500,6 +500,16 @@ volatile int test_done = 1; /* stop packet forwarding when set to 1. */
  */
 uint8_t xstats_hide_zero;
 
+/*
+ * Display of xstats without their state disabled by default
+ */
+uint8_t xstats_show_state;
+
+/*
+ * Display disabled xstat by default for xstats
+ */
+uint8_t xstats_hide_disabled;
+
 /*
  * Measure of CPU cycles disabled by default
  */
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index 260e4761bd..a88c1aeaf6 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -498,6 +498,8 @@ enum dcb_mode_enable
 };
 
 extern uint8_t xstats_hide_zero; /**< Hide zero values for xstats display */
+extern uint8_t xstats_hide_disabled; /**< Hide disabled xstat for xstats display */
+extern uint8_t xstats_show_state; /**< Show xstat state in xstats display */
 
 /* globals used for configuration */
 extern uint8_t record_core_cycles; /**< Enables measurement of CPU cycles */
@@ -933,6 +935,7 @@ void nic_stats_display(portid_t port_id);
 void nic_stats_clear(portid_t port_id);
 void nic_xstats_display(portid_t port_id);
 void nic_xstats_clear(portid_t port_id);
+void nic_xstats_set_counter(portid_t port_id, char *counter_name, int on);
 void device_infos_display(const char *identifier);
 void port_infos_display(portid_t port_id);
 void port_summary_display(portid_t port_id);
@@ -1111,6 +1114,8 @@ void tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on);
 void set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value);
 
 void set_xstats_hide_zero(uint8_t on_off);
+void set_xstats_show_state(uint8_t on_off);
+void set_xstats_hide_disabled(uint8_t on_off);
 
 void set_record_core_cycles(uint8_t on_off);
 void set_record_burst_stats(uint8_t on_off);
diff --git a/lib/cmdline/cmdline_parse_bool.c b/lib/cmdline/cmdline_parse_bool.c
new file mode 100644
index 0000000000..aa7bf88d5a
--- /dev/null
+++ b/lib/cmdline/cmdline_parse_bool.c
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2023 NVIDIA Corporation & Affiliates
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <string.h>
+#include <rte_string_fns.h>
+#include <stdlib.h>
+
+#include "cmdline_parse.h"
+#include "cmdline_parse_bool.h"
+
+
+struct cmdline_token_ops cmdline_token_bool_ops = {
+	.parse = cmdline_parse_bool,
+	.complete_get_nb = NULL,
+	.complete_get_elt = NULL,
+	.get_help = cmdline_get_help_string,
+};
+
+static cmdline_parse_token_string_t cmd_parse_token_bool = {
+	{
+		&cmdline_token_string_ops,
+		0
+	},
+	{
+		"on#off"
+	}
+};
+
+/* parse string to bool */
+int
+cmdline_parse_bool(__rte_unused cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res,
+	__rte_unused unsigned int ressize)
+{
+	cmdline_fixed_string_t on_off = {0};
+	if (cmdline_token_string_ops.parse
+			(&cmd_parse_token_bool.hdr, srcbuf, on_off, sizeof(on_off)) < 0)
+		return -1;
+
+	if (strcmp((char *)on_off, "on") == 0)
+		*(uint8_t *)res = 1;
+	else if (strcmp((char *)on_off, "off") == 0)
+		*(uint8_t *)res = 0;
+
+	return strlen(on_off);
+}
diff --git a/lib/cmdline/cmdline_parse_bool.h b/lib/cmdline/cmdline_parse_bool.h
new file mode 100644
index 0000000000..a601e4f54b
--- /dev/null
+++ b/lib/cmdline/cmdline_parse_bool.h
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 2023 NVIDIA Corporation & Affiliates
+ */
+
+#ifndef _PARSE_BOOL_H_
+#define _PARSE_BOOL_H_
+
+#include <cmdline_parse.h>
+#include <cmdline_parse_string.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct cmdline_token_bool {
+	struct cmdline_token_hdr hdr;
+};
+
+typedef struct cmdline_token_bool cmdline_parse_token_bool_t;
+extern struct cmdline_token_ops cmdline_token_bool_ops;
+
+int cmdline_parse_bool(cmdline_parse_token_hdr_t *tk,
+	const char *srcbuf, void *res, unsigned int ressize);
+
+#define TOKEN_BOOL_INITIALIZER(structure, field)			\
+{                                                           \
+	/* hdr */                                               \
+	{                                                       \
+		&cmdline_token_bool_ops,         /* ops */			\
+		offsetof(structure, field),     /* offset */		\
+	}                                                       \
+}
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _PARSE_BOOL_H_ */
diff --git a/lib/cmdline/cmdline_parse_num.c b/lib/cmdline/cmdline_parse_num.c
index e8498788fa..6dc325e6da 100644
--- a/lib/cmdline/cmdline_parse_num.c
+++ b/lib/cmdline/cmdline_parse_num.c
@@ -9,6 +9,7 @@
 #include <inttypes.h>
 #include <string.h>
 #include <rte_string_fns.h>
+#include <stdlib.h>
 
 #include "cmdline_parse.h"
 #include "cmdline_parse_num.h"
@@ -26,7 +27,6 @@ struct cmdline_token_ops cmdline_token_num_ops = {
 	.get_help = cmdline_get_help_num,
 };
 
-
 enum num_parse_state_t {
 	START,
 	DEC_NEG,
diff --git a/lib/cmdline/cmdline_parse_num.h b/lib/cmdline/cmdline_parse_num.h
index 02133bb21c..bdd0267612 100644
--- a/lib/cmdline/cmdline_parse_num.h
+++ b/lib/cmdline/cmdline_parse_num.h
@@ -8,6 +8,7 @@
 #define _PARSE_NUM_H_
 
 #include <cmdline_parse.h>
+#include <cmdline_parse_string.h>
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/lib/cmdline/meson.build b/lib/cmdline/meson.build
index 63fb69100d..e38e05893a 100644
--- a/lib/cmdline/meson.build
+++ b/lib/cmdline/meson.build
@@ -7,6 +7,7 @@ sources = files('cmdline.c',
     'cmdline_parse_etheraddr.c',
     'cmdline_parse_ipaddr.c',
     'cmdline_parse_num.c',
+    'cmdline_parse_bool.c',
     'cmdline_parse_portlist.c',
     'cmdline_parse_string.c',
     'cmdline_rdline.c',
@@ -16,6 +17,7 @@ sources = files('cmdline.c',
 headers = files('cmdline.h',
     'cmdline_parse.h',
     'cmdline_parse_num.h',
+    'cmdline_parse_bool.h',
     'cmdline_parse_ipaddr.h',
     'cmdline_parse_etheraddr.h',
     'cmdline_parse_string.h',
diff --git a/lib/cmdline/version.map b/lib/cmdline/version.map
index 6bcfebfcec..08050ea3f3 100644
--- a/lib/cmdline/version.map
+++ b/lib/cmdline/version.map
@@ -73,3 +73,10 @@ DPDK_25 {
 
 	local: *;
 };
+
+EXPERIMENTAL {
+	global:
+
+	# added in 25.03
+	cmdline_token_bool_ops;
+};
\ No newline at end of file
-- 
2.34.1


  parent reply	other threads:[~2025-01-24 11:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-22 15:38 [RFC PATCH] ethdev: add new API for enable/disable xstat counters by ID Shani Peretz
2024-12-22 16:46 ` Stephen Hemminger
2024-12-23 10:46   ` Shani Peretz
2024-12-24 17:19     ` Stephen Hemminger
2025-01-01 19:05       ` Shani Peretz
2025-01-10  4:36 ` Stephen Hemminger
2025-01-24 11:43 ` [PATCH v2 0/3] add new API to enable/disable xstat counters Shani Peretz
2025-01-24 11:43   ` [PATCH v2 1/3] ethdev: add new API to enable/disable xstat by counters ID Shani Peretz
2025-01-24 11:43   ` Shani Peretz [this message]
2025-01-24 11:43   ` [PATCH v2 3/3] net/mlx5: add port and queue level counters of hairpin drops Shani Peretz

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=20250124114336.16610-3-shperetz@nvidia.com \
    --to=shperetz@nvidia.com \
    --cc=aman.deep.singh@intel.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@amd.com \
    --cc=stephen@networkplumber.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).