Skip to content

Commit 90c41d0

Browse files
committed
samples: net: stats: Print ethernet statistics
If ethernet statistics collection is enabled, then print it to the user. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
1 parent 7ee27ec commit 90c41d0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

samples/net/stats/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CONFIG_NET_TCP=y
1111
CONFIG_NET_STATISTICS=y
1212
CONFIG_NET_STATISTICS_USER_API=y
1313
CONFIG_NET_STATISTICS_PER_INTERFACE=y
14+
CONFIG_NET_STATISTICS_ETHERNET=y
1415

1516
# How often to print current statistics
1617
CONFIG_SAMPLE_PERIOD=30

samples/net/stats/src/main.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ static void iface_cb(struct net_if *iface, void *user_data)
159159
}
160160
#endif
161161

162+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
163+
static void print_eth_stats(struct net_if *iface, struct net_stats_eth *data)
164+
{
165+
printk("Statistics for Ethernet interface %p [%d]\n", iface,
166+
net_if_get_by_iface(iface));
167+
168+
printk("Bytes received : %u\n", data->bytes.received);
169+
printk("Bytes sent : %u\n", data->bytes.sent);
170+
printk("Packets received : %u\n", data->pkts.rx);
171+
printk("Packets sent : %u\n", data->pkts.tx);
172+
printk("Bcast received : %u\n", data->broadcast.rx);
173+
printk("Bcast sent : %u\n", data->broadcast.tx);
174+
printk("Mcast received : %u\n", data->multicast.rx);
175+
printk("Mcast sent : %u\n", data->multicast.tx);
176+
}
177+
178+
static void eth_iface_cb(struct net_if *iface, void *user_data)
179+
{
180+
struct net_stats_eth eth_data;
181+
int ret;
182+
183+
if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
184+
return;
185+
}
186+
187+
ret = net_mgmt(NET_REQUEST_STATS_GET_ETHERNET, iface, &eth_data,
188+
sizeof(eth_data));
189+
if (ret < 0) {
190+
return;
191+
}
192+
193+
print_eth_stats(iface, &eth_data);
194+
}
195+
#endif
196+
162197
static void stats(struct k_work *work)
163198
{
164199
struct net_stats data;
@@ -175,6 +210,10 @@ static void stats(struct k_work *work)
175210
net_if_foreach(iface_cb, &data);
176211
#endif
177212

213+
#if defined(CONFIG_NET_STATISTICS_ETHERNET)
214+
net_if_foreach(eth_iface_cb, &data);
215+
#endif
216+
178217
k_delayed_work_submit(&stats_timer, K_SECONDS(CONFIG_SAMPLE_PERIOD));
179218
}
180219

0 commit comments

Comments
 (0)