Skip to content

Commit 6cc8e0f

Browse files
committed
net: lib: shell: Add options t display vendor data
Along with key-value pairs, add an option to dump vendor values as a blob, use can choose either one or both. Signed-off-by: Chaitanya Tata <[email protected]>
1 parent 778e17b commit 6cc8e0f

File tree

1 file changed

+85
-15
lines changed

1 file changed

+85
-15
lines changed

subsys/net/lib/shell/stats.c

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ LOG_MODULE_DECLARE(net_shell);
1414

1515
#include "../ip/net_stats.h"
1616

17+
enum net_shell_stats_format {
18+
NET_SHELL_STATS_FORMAT_DEFAULT,
19+
NET_SHELL_STATS_FORMAT_KEY_VALUE,
20+
NET_SHELL_STATS_FORMAT_HEX_BLOB,
21+
NET_SHELL_STATS_FORMAT_BOTH
22+
};
23+
1724
#if defined(CONFIG_NET_STATISTICS)
1825

26+
1927
#if NET_TC_COUNT > 1
2028
static const char *priority2str(enum net_priority priority)
2129
{
@@ -45,7 +53,7 @@ static const char *priority2str(enum net_priority priority)
4553
#if defined(CONFIG_NET_STATISTICS_ETHERNET) && \
4654
defined(CONFIG_NET_STATISTICS_USER_API)
4755
static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
48-
const struct shell *sh)
56+
const struct shell *sh, struct net_shell_user_data *user_data)
4957
{
5058
PR("Statistics for Ethernet interface %p [%d]\n", iface,
5159
net_if_get_by_iface(iface));
@@ -110,16 +118,42 @@ static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
110118

111119
#if defined(CONFIG_NET_STATISTICS_ETHERNET_VENDOR)
112120
if (data->vendor) {
113-
PR("Vendor specific statistics for Ethernet "
114-
"interface %p [%d]:\n",
115-
iface, net_if_get_by_iface(iface));
116121
size_t i = 0;
122+
enum net_shell_stats_format format = NET_SHELL_STATS_FORMAT_DEFAULT;
123+
124+
if (user_data) {
125+
format = *(enum net_shell_stats_format *)user_data->user_data;
126+
}
117127

118-
do {
119-
PR("%s : %u\n", data->vendor[i].key,
120-
data->vendor[i].value);
121-
i++;
122-
} while (data->vendor[i].key);
128+
PR("Vendor specific statistics for Ethernet interface %p [%d]:\n",
129+
iface, net_if_get_by_iface(iface));
130+
131+
/* Print key-value pairs if requested */
132+
if (format == NET_SHELL_STATS_FORMAT_DEFAULT ||
133+
format == NET_SHELL_STATS_FORMAT_KEY_VALUE ||
134+
format == NET_SHELL_STATS_FORMAT_BOTH) {
135+
do {
136+
PR("%s : %u\n", data->vendor[i].key, data->vendor[i].value);
137+
i++;
138+
} while (data->vendor[i].key != NULL);
139+
}
140+
141+
/* Print hex blob if requested */
142+
if (format == NET_SHELL_STATS_FORMAT_HEX_BLOB ||
143+
format == NET_SHELL_STATS_FORMAT_BOTH) {
144+
/* Suitable for parsing */
145+
PR("Vendor stats hex blob: ");
146+
for (i = 0; data->vendor[i].key != NULL; i++) {
147+
uint32_t v = data->vendor[i].value;
148+
149+
PR("%02x%02x%02x%02x",
150+
(uint8_t)(v & 0xFF),
151+
(uint8_t)((v >> 8) & 0xFF),
152+
(uint8_t)((v >> 16) & 0xFF),
153+
(uint8_t)((v >> 24) & 0xFF));
154+
}
155+
PR("\n");
156+
}
123157
}
124158
#endif /* CONFIG_NET_STATISTICS_ETHERNET_VENDOR */
125159
}
@@ -591,7 +625,7 @@ static void net_shell_print_statistics(struct net_if *iface, void *user_data)
591625
ret = net_mgmt(NET_REQUEST_STATS_GET_ETHERNET, iface,
592626
&eth_data, sizeof(eth_data));
593627
if (!ret) {
594-
print_eth_stats(iface, &eth_data, sh);
628+
print_eth_stats(iface, &eth_data, sh, data);
595629
}
596630
}
597631
#endif /* CONFIG_NET_STATISTICS_ETHERNET && CONFIG_NET_STATISTICS_USER_API */
@@ -628,8 +662,23 @@ int cmd_net_stats_all(const struct shell *sh, size_t argc, char *argv[])
628662
#endif
629663

630664
#if defined(CONFIG_NET_STATISTICS)
665+
enum net_shell_stats_format format = NET_SHELL_STATS_FORMAT_DEFAULT;
666+
631667
user_data.sh = sh;
632668

669+
/* Parse format argument if provided */
670+
if (argc > 1) {
671+
if (strcmp(argv[1], "key-value") == 0) {
672+
format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
673+
} else if (strcmp(argv[1], "hex-blob") == 0) {
674+
format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
675+
} else if (strcmp(argv[1], "both") == 0) {
676+
format = NET_SHELL_STATS_FORMAT_BOTH;
677+
}
678+
}
679+
680+
user_data.user_data = &format;
681+
633682
/* Print global network statistics */
634683
net_shell_print_statistics_all(&user_data);
635684
#else
@@ -673,8 +722,23 @@ int cmd_net_stats_iface(const struct shell *sh, size_t argc, char *argv[])
673722
return -ENOEXEC;
674723
}
675724

725+
enum net_shell_stats_format format = NET_SHELL_STATS_FORMAT_DEFAULT;
726+
676727
data.sh = sh;
677728

729+
/* Parse format argument if provided */
730+
if (argc > 2) {
731+
if (strcmp(argv[2], "key-value") == 0) {
732+
format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
733+
} else if (strcmp(argv[2], "hex-blob") == 0) {
734+
format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
735+
} else if (strcmp(argv[2], "both") == 0) {
736+
format = NET_SHELL_STATS_FORMAT_BOTH;
737+
}
738+
}
739+
740+
data.user_data = &format;
741+
678742
net_shell_print_statistics(iface, &data);
679743
#else
680744
PR_INFO("Per network interface statistics not collected.\n");
@@ -702,7 +766,8 @@ static int cmd_net_stats(const struct shell *sh, size_t argc, char *argv[])
702766
if (strcmp(argv[1], "reset") == 0) {
703767
net_stats_reset(NULL);
704768
} else {
705-
cmd_net_stats_iface(sh, argc, argv);
769+
/* Shift arguments for iface command */
770+
cmd_net_stats_iface(sh, argc - 1, &argv[1]);
706771
}
707772
#else
708773
ARG_UNUSED(argc);
@@ -723,15 +788,20 @@ static int cmd_net_stats(const struct shell *sh, size_t argc, char *argv[])
723788

724789
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_stats,
725790
SHELL_CMD(all, NULL,
726-
"Show network statistics for all network interfaces.",
791+
"Show network statistics for all network interfaces.\n"
792+
"Usage: net stats all [key-value|hex-blob|both]",
727793
cmd_net_stats_all),
728794
SHELL_CMD(iface, IFACE_DYN_CMD,
729-
"'net stats <index>' shows network statistics for "
730-
"one specific network interface.",
795+
"'net stats <index> [key-value|hex-blob|both]' shows network statistics for "
796+
"one specific network interface.\n"
797+
"Format options:\n"
798+
" key-value: Show vendor stats as key-value pairs (default)\n"
799+
" hex-blob: Show vendor stats as hex blob for parsing\n"
800+
" both: Show both key-value and hex blob formats",
731801
cmd_net_stats_iface),
732802
SHELL_SUBCMD_SET_END
733803
);
734804

735805
SHELL_SUBCMD_ADD((net), stats, &net_cmd_stats,
736806
"Show network statistics.",
737-
cmd_net_stats, 1, 1);
807+
cmd_net_stats, 1, 3);

0 commit comments

Comments
 (0)