Skip to content

Commit 66d8979

Browse files
committed
[nrf fromlist] net: Extend display options for vendor stats
Add few options for dumping vendor stats, esp. for easier parsing. Upstream PR: zephyrproject-rtos/zephyr#96753 Signed-off-by: Chaitanya Tata <[email protected]>
1 parent c0864fc commit 66d8979

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
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: 69 additions & 14 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));
@@ -69,16 +69,42 @@ static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data,
6969

7070
#if defined(CONFIG_NET_STATISTICS_ETHERNET_VENDOR)
7171
if (data->vendor) {
72-
PR("Vendor specific statistics for Ethernet "
73-
"interface %p [%d]:\n",
74-
iface, net_if_get_by_iface(iface));
7572
size_t i = 0;
73+
enum net_shell_stats_format format = NET_SHELL_STATS_FORMAT_DEFAULT;
74+
75+
if (user_data) {
76+
format = user_data->vendor_stats_format;
77+
}
78+
79+
PR("Vendor specific statistics for Ethernet interface %p [%d]:\n",
80+
iface, net_if_get_by_iface(iface));
81+
82+
/* Print key-value pairs if requested */
83+
if (format == NET_SHELL_STATS_FORMAT_DEFAULT ||
84+
format == NET_SHELL_STATS_FORMAT_KEY_VALUE ||
85+
format == NET_SHELL_STATS_FORMAT_BOTH) {
86+
do {
87+
PR("%s : %u\n", data->vendor[i].key, data->vendor[i].value);
88+
i++;
89+
} while (data->vendor[i].key);
90+
}
7691

77-
do {
78-
PR("%s : %u\n", data->vendor[i].key,
79-
data->vendor[i].value);
80-
i++;
81-
} while (data->vendor[i].key);
92+
/* Print hex blob if requested */
93+
if (format == NET_SHELL_STATS_FORMAT_HEX_BLOB ||
94+
format == NET_SHELL_STATS_FORMAT_BOTH) {
95+
/* Suitable for parsing */
96+
PR("Vendor stats hex blob: ");
97+
for (i = 0; data->vendor[i].key; i++) {
98+
uint32_t v = data->vendor[i].value;
99+
100+
PR("%02x%02x%02x%02x",
101+
(uint8_t)(v & 0xFF),
102+
(uint8_t)((v >> 8) & 0xFF),
103+
(uint8_t)((v >> 16) & 0xFF),
104+
(uint8_t)((v >> 24) & 0xFF));
105+
}
106+
PR("\n");
107+
}
82108
}
83109
#endif /* CONFIG_NET_STATISTICS_ETHERNET_VENDOR */
84110
}
@@ -513,7 +539,7 @@ static void net_shell_print_statistics(struct net_if *iface, void *user_data)
513539
ret = net_mgmt(NET_REQUEST_STATS_GET_ETHERNET, iface,
514540
&eth_data, sizeof(eth_data));
515541
if (!ret) {
516-
print_eth_stats(iface, &eth_data, sh);
542+
print_eth_stats(iface, &eth_data, sh, data);
517543
}
518544
}
519545
#endif /* CONFIG_NET_STATISTICS_ETHERNET && CONFIG_NET_STATISTICS_USER_API */
@@ -551,6 +577,18 @@ int cmd_net_stats_all(const struct shell *sh, size_t argc, char *argv[])
551577

552578
#if defined(CONFIG_NET_STATISTICS)
553579
user_data.sh = sh;
580+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_DEFAULT;
581+
582+
/* Parse format argument if provided */
583+
if (argc > 1) {
584+
if (strcmp(argv[1], "key-value") == 0) {
585+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
586+
} else if (strcmp(argv[1], "hex-blob") == 0) {
587+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
588+
} else if (strcmp(argv[1], "both") == 0) {
589+
user_data.vendor_stats_format = NET_SHELL_STATS_FORMAT_BOTH;
590+
}
591+
}
554592

555593
/* Print global network statistics */
556594
net_shell_print_statistics_all(&user_data);
@@ -596,6 +634,18 @@ int cmd_net_stats_iface(const struct shell *sh, size_t argc, char *argv[])
596634
}
597635

598636
data.sh = sh;
637+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_DEFAULT;
638+
639+
/* Parse format argument if provided */
640+
if (argc > 2) {
641+
if (strcmp(argv[2], "key-value") == 0) {
642+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_KEY_VALUE;
643+
} else if (strcmp(argv[2], "hex-blob") == 0) {
644+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_HEX_BLOB;
645+
} else if (strcmp(argv[2], "both") == 0) {
646+
data.vendor_stats_format = NET_SHELL_STATS_FORMAT_BOTH;
647+
}
648+
}
599649

600650
net_shell_print_statistics(iface, &data);
601651
#else
@@ -645,15 +695,20 @@ static int cmd_net_stats(const struct shell *sh, size_t argc, char *argv[])
645695

646696
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_stats,
647697
SHELL_CMD(all, NULL,
648-
"Show network statistics for all network interfaces.",
698+
"Show network statistics for all network interfaces.\n"
699+
"Usage: net stats all [key-value|hex-blob|both]",
649700
cmd_net_stats_all),
650701
SHELL_CMD(iface, IFACE_DYN_CMD,
651-
"'net stats <index>' shows network statistics for "
652-
"one specific network interface.",
702+
"'net stats <index> [key-value|hex-blob|both]' shows network statistics for "
703+
"one specific network interface.\n"
704+
"Format options:\n"
705+
" key-value: Show vendor stats as key-value pairs (default)\n"
706+
" hex-blob: Show vendor stats as hex blob for parsing\n"
707+
" both: Show both key-value and hex blob formats",
653708
cmd_net_stats_iface),
654709
SHELL_SUBCMD_SET_END
655710
);
656711

657712
SHELL_SUBCMD_ADD((net), stats, &net_cmd_stats,
658713
"Show network statistics.",
659-
cmd_net_stats, 1, 1);
714+
cmd_net_stats, 1, 3);

0 commit comments

Comments
 (0)