Skip to content

Commit ac2df96

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 df5d762 commit ac2df96

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

subsys/net/lib/shell/net_shell_private.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@
5656
#include "net_private.h"
5757
#include "../ip/ipv6.h"
5858

59+
enum net_shell_stats_format {
60+
NET_SHELL_STATS_FORMAT_DEFAULT,
61+
NET_SHELL_STATS_FORMAT_KEY_VALUE,
62+
NET_SHELL_STATS_FORMAT_HEX_BLOB,
63+
NET_SHELL_STATS_FORMAT_BOTH
64+
};
65+
5966
struct net_shell_user_data {
6067
const struct shell *sh;
6168
void *user_data;
69+
enum net_shell_stats_format vendor_stats_format;
6270
};
6371

6472
#if !defined(NET_VLAN_MAX_COUNT)

subsys/net/lib/shell/stats.c

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static const char *priority2str(enum net_priority priority)
4545
#if defined(CONFIG_NET_STATISTICS_ETHERNET) && \
4646
defined(CONFIG_NET_STATISTICS_USER_API)
4747
static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
48-
const struct shell *sh)
48+
const struct shell *sh, struct net_shell_user_data *user_data)
4949
{
5050
PR("Statistics for Ethernet interface %p [%d]\n", iface,
5151
net_if_get_by_iface(iface));
@@ -110,16 +110,42 @@ static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
110110

111111
#if defined(CONFIG_NET_STATISTICS_ETHERNET_VENDOR)
112112
if (data->vendor) {
113-
PR("Vendor specific statistics for Ethernet "
114-
"interface %p [%d]:\n",
115-
iface, net_if_get_by_iface(iface));
116113
size_t i = 0;
114+
enum net_shell_stats_format format = NET_SHELL_STATS_FORMAT_DEFAULT;
115+
116+
if (user_data) {
117+
format = user_data->vendor_stats_format;
118+
}
119+
120+
PR("Vendor specific statistics for Ethernet interface %p [%d]:\n",
121+
iface, net_if_get_by_iface(iface));
122+
123+
/* Print key-value pairs if requested */
124+
if (format == NET_SHELL_STATS_FORMAT_DEFAULT ||
125+
format == NET_SHELL_STATS_FORMAT_KEY_VALUE ||
126+
format == NET_SHELL_STATS_FORMAT_BOTH) {
127+
do {
128+
PR("%s : %u\n", data->vendor[i].key, data->vendor[i].value);
129+
i++;
130+
} while (data->vendor[i].key);
131+
}
117132

118-
do {
119-
PR("%s : %u\n", data->vendor[i].key,
120-
data->vendor[i].value);
121-
i++;
122-
} while (data->vendor[i].key);
133+
/* Print hex blob if requested */
134+
if (format == NET_SHELL_STATS_FORMAT_HEX_BLOB ||
135+
format == NET_SHELL_STATS_FORMAT_BOTH) {
136+
/* Suitable for parsing */
137+
PR("Vendor stats hex blob: ");
138+
for (i = 0; data->vendor[i].key; i++) {
139+
uint32_t v = data->vendor[i].value;
140+
141+
PR("%02x%02x%02x%02x",
142+
(uint8_t)(v & 0xFF),
143+
(uint8_t)((v >> 8) & 0xFF),
144+
(uint8_t)((v >> 16) & 0xFF),
145+
(uint8_t)((v >> 24) & 0xFF));
146+
}
147+
PR("\n");
148+
}
123149
}
124150
#endif /* CONFIG_NET_STATISTICS_ETHERNET_VENDOR */
125151
}
@@ -591,7 +617,7 @@ static void net_shell_print_statistics(struct net_if *iface, void *user_data)
591617
ret = net_mgmt(NET_REQUEST_STATS_GET_ETHERNET, iface,
592618
&eth_data, sizeof(eth_data));
593619
if (!ret) {
594-
print_eth_stats(iface, &eth_data, sh);
620+
print_eth_stats(iface, &eth_data, sh, data);
595621
}
596622
}
597623
#endif /* CONFIG_NET_STATISTICS_ETHERNET && CONFIG_NET_STATISTICS_USER_API */
@@ -629,6 +655,18 @@ int cmd_net_stats_all(const struct shell *sh, size_t argc, char *argv[])
629655

630656
#if defined(CONFIG_NET_STATISTICS)
631657
user_data.sh = sh;
658+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_DEFAULT;
659+
660+
/* Parse format argument if provided */
661+
if (argc > 1) {
662+
if (strcmp(argv[1], "key-value") == 0) {
663+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
664+
} else if (strcmp(argv[1], "hex-blob") == 0) {
665+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
666+
} else if (strcmp(argv[1], "both") == 0) {
667+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_BOTH;
668+
}
669+
}
632670

633671
/* Print global network statistics */
634672
net_shell_print_statistics_all(&user_data);
@@ -674,6 +712,18 @@ int cmd_net_stats_iface(const struct shell *sh, size_t argc, char *argv[])
674712
}
675713

676714
data.sh = sh;
715+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_DEFAULT;
716+
717+
/* Parse format argument if provided */
718+
if (argc > 2) {
719+
if (strcmp(argv[2], "key-value") == 0) {
720+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
721+
} else if (strcmp(argv[2], "hex-blob") == 0) {
722+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
723+
} else if (strcmp(argv[2], "both") == 0) {
724+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_BOTH;
725+
}
726+
}
677727

678728
net_shell_print_statistics(iface, &data);
679729
#else
@@ -702,7 +752,8 @@ static int cmd_net_stats(const struct shell *sh, size_t argc, char *argv[])
702752
if (strcmp(argv[1], "reset") == 0) {
703753
net_stats_reset(NULL);
704754
} else {
705-
cmd_net_stats_iface(sh, argc, argv);
755+
/* Shift arguments for iface command */
756+
cmd_net_stats_iface(sh, argc - 1, &argv[1]);
706757
}
707758
#else
708759
ARG_UNUSED(argc);
@@ -723,15 +774,20 @@ static int cmd_net_stats(const struct shell *sh, size_t argc, char *argv[])
723774

724775
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_stats,
725776
SHELL_CMD(all, NULL,
726-
"Show network statistics for all network interfaces.",
777+
"Show network statistics for all network interfaces.\n"
778+
"Usage: net stats all [key-value|hex-blob|both]",
727779
cmd_net_stats_all),
728780
SHELL_CMD(iface, IFACE_DYN_CMD,
729-
"'net stats <index>' shows network statistics for "
730-
"one specific network interface.",
781+
"'net stats <index> [key-value|hex-blob|both]' shows network statistics for "
782+
"one specific network interface.\n"
783+
"Format options:\n"
784+
" key-value: Show vendor stats as key-value pairs (default)\n"
785+
" hex-blob: Show vendor stats as hex blob for parsing\n"
786+
" both: Show both key-value and hex blob formats",
731787
cmd_net_stats_iface),
732788
SHELL_SUBCMD_SET_END
733789
);
734790

735791
SHELL_SUBCMD_ADD((net), stats, &net_cmd_stats,
736792
"Show network statistics.",
737-
cmd_net_stats, 1, 1);
793+
cmd_net_stats, 1, 3);

0 commit comments

Comments
 (0)