Skip to content

Commit acbddd1

Browse files
wengzhexiaoxiang781216
authored andcommitted
net/netdev: Add periodic log for netdev statistics
Work for every network device using `CONFIG_NETDEV_STATISTICS`. Log style: <interface>:T{done}/{total},R({v4}+{v6})/{total} {Protocol}:T{tx},R{rx},D{drop} Example: wlan0:T10/10,R(10+20)/31 TCP:T0,R0,D0 UDP:T0,R10,D0 ICMP:T0,R0,D0 ICMP6:T0,R0,D0 Signed-off-by: Zhe Weng <[email protected]>
1 parent f5fe764 commit acbddd1

File tree

6 files changed

+168
-2
lines changed

6 files changed

+168
-2
lines changed

drivers/net/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ config NETDEV_STATISTICS
131131
Enable to collect statistics from the network drivers (if supported
132132
by the network driver).
133133

134+
config NETDEV_STATISTICS_LOG_PERIOD
135+
int "Network device statistics log period"
136+
default 0
137+
depends on NETDEV_STATISTICS && DEBUG_FEATURES
138+
---help---
139+
Period in seconds to log network device statistics. Zero means
140+
disable logging.
141+
134142
config NET_DUMPPACKET
135143
bool "Enable packet dumping"
136144
depends on DEBUG_FEATURES

include/nuttx/net/netdev.h

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
# include <nuttx/net/mld.h>
7474
#endif
7575

76+
#ifndef CONFIG_NETDEV_STATISTICS_LOG_PERIOD
77+
# define CONFIG_NETDEV_STATISTICS_LOG_PERIOD 0
78+
#endif
79+
80+
#if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
81+
# include <nuttx/wqueue.h>
82+
#endif
83+
7684
/****************************************************************************
7785
* Pre-processor Definitions
7886
****************************************************************************/
@@ -112,7 +120,26 @@
112120
} \
113121
while (0)
114122

115-
# define NETDEV_RXPACKETS(dev) _NETDEV_STATISTIC(dev,rx_packets)
123+
# if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
124+
# define NETDEV_STATISTICS_WORK LPWORK
125+
# define _NETDEV_STATISTIC_LOG(dev,name) \
126+
do \
127+
{ \
128+
_NETDEV_STATISTIC(dev,name); \
129+
if (work_available(&(dev)->d_statistics.logwork)) \
130+
{ \
131+
work_queue(NETDEV_STATISTICS_WORK, \
132+
&(dev)->d_statistics.logwork, \
133+
netdev_statistics_log, (dev), \
134+
SEC2TICK(CONFIG_NETDEV_STATISTICS_LOG_PERIOD)); \
135+
} \
136+
} \
137+
while (0)
138+
# else
139+
# define _NETDEV_STATISTIC_LOG(dev,name) _NETDEV_STATISTIC(dev,name)
140+
# endif
141+
142+
# define NETDEV_RXPACKETS(dev) _NETDEV_STATISTIC_LOG(dev,rx_packets)
116143
# define NETDEV_RXFRAGMENTS(dev) _NETDEV_STATISTIC(dev,rx_fragments)
117144
# define NETDEV_RXERRORS(dev) _NETDEV_ERROR(dev,rx_errors)
118145
# ifdef CONFIG_NET_IPv4
@@ -132,7 +159,7 @@
132159
# endif
133160
# define NETDEV_RXDROPPED(dev) _NETDEV_STATISTIC(dev,rx_dropped)
134161

135-
# define NETDEV_TXPACKETS(dev) _NETDEV_STATISTIC(dev,tx_packets)
162+
# define NETDEV_TXPACKETS(dev) _NETDEV_STATISTIC_LOG(dev,tx_packets)
136163
# define NETDEV_TXDONE(dev) _NETDEV_STATISTIC(dev,tx_done)
137164
# define NETDEV_TXERRORS(dev) _NETDEV_ERROR(dev,tx_errors)
138165
# define NETDEV_TXTIMEOUTS(dev) _NETDEV_ERROR(dev,tx_timeouts)
@@ -219,6 +246,10 @@ struct netdev_statistics_s
219246
/* Other status */
220247

221248
uint32_t errors; /* Total number of errors */
249+
250+
#if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
251+
struct work_s logwork; /* For periodic log work */
252+
#endif
222253
};
223254
#endif
224255

@@ -1196,4 +1227,20 @@ int netdev_ipv6_foreach(FAR struct net_driver_s *dev,
11961227
devif_ipv6_callback_t callback, FAR void *arg);
11971228
#endif
11981229

1230+
/****************************************************************************
1231+
* Name: netdev_statistics_log
1232+
*
1233+
* Description:
1234+
* The actual implementation of the network statistics logging. Log
1235+
* network statistics at regular intervals.
1236+
*
1237+
* Input Parameters:
1238+
* arg - The pointer to the network device
1239+
*
1240+
****************************************************************************/
1241+
1242+
#if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
1243+
void netdev_statistics_log(FAR void *arg);
1244+
#endif
1245+
11991246
#endif /* __INCLUDE_NUTTX_NET_NETDEV_H */

net/netdev/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ if(CONFIG_NET_IPv6)
4646
list(APPEND SRCS netdev_ipv6.c)
4747
endif()
4848

49+
if(CONFIG_NETDEV_STATISTICS)
50+
list(APPEND SRCS netdev_stats.c)
51+
endif()
52+
4953
target_sources(net PRIVATE ${SRCS})

net/netdev/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ ifeq ($(CONFIG_NET_IPv6),y)
3838
NETDEV_CSRCS += netdev_ipv6.c
3939
endif
4040

41+
ifeq ($(CONFIG_NETDEV_STATISTICS),y)
42+
NETDEV_CSRCS += netdev_stats.c
43+
endif
44+
4145
# Include netdev build support
4246

4347
DEPPATH += --dep-path netdev

net/netdev/netdev_stats.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/****************************************************************************
2+
* net/netdev/netdev_stats.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <syslog.h>
28+
29+
#include <nuttx/net/netdev.h>
30+
#include <nuttx/net/netstats.h>
31+
32+
/****************************************************************************
33+
* Pre-processor Definitions
34+
****************************************************************************/
35+
36+
#define stats_log(format, ...) syslog(LOG_NOTICE, format, ##__VA_ARGS__)
37+
38+
/****************************************************************************
39+
* Public Functions
40+
****************************************************************************/
41+
42+
/****************************************************************************
43+
* Name: netdev_statistics_log
44+
*
45+
* Description:
46+
* The actual implementation of the network statistics logging. Log
47+
* network statistics at regular intervals.
48+
*
49+
* Input Parameters:
50+
* arg - The pointer to the network device
51+
*
52+
****************************************************************************/
53+
54+
#if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
55+
void netdev_statistics_log(FAR void *arg)
56+
{
57+
FAR struct net_driver_s *dev = arg;
58+
FAR struct netdev_statistics_s *stats = &dev->d_statistics;
59+
60+
stats_log("%s:T%" PRIu32 "/%" PRIu32 ",R"
61+
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
62+
"(%" PRIu32 "+%" PRIu32 ")/"
63+
#endif
64+
"%" PRIu32
65+
#ifdef CONFIG_NET_TCP
66+
" TCP:T%" PRIu16 ",R%" PRIu16 ",D%" PRIu16
67+
#endif
68+
#ifdef CONFIG_NET_UDP
69+
" UDP:T%" PRIu16 ",R%" PRIu16 ",D%" PRIu16
70+
#endif
71+
#ifdef CONFIG_NET_ICMP
72+
" ICMP:T%" PRIu16 ",R%" PRIu16 ",D%" PRIu16
73+
#endif
74+
#ifdef CONFIG_NET_ICMPv6
75+
" ICMP6:T%" PRIu16 ",R%" PRIu16 ",D%" PRIu16
76+
#endif
77+
"\n",
78+
dev->d_ifname, stats->tx_done, stats->tx_packets
79+
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
80+
, stats->rx_ipv4, stats->rx_ipv6
81+
#endif
82+
, stats->rx_packets
83+
#ifdef CONFIG_NET_TCP
84+
, g_netstats.tcp.sent, g_netstats.tcp.recv, g_netstats.tcp.drop
85+
#endif
86+
#ifdef CONFIG_NET_UDP
87+
, g_netstats.udp.sent, g_netstats.udp.recv, g_netstats.udp.drop
88+
#endif
89+
#ifdef CONFIG_NET_ICMP
90+
, g_netstats.icmp.sent, g_netstats.icmp.recv,
91+
g_netstats.icmp.drop
92+
#endif
93+
#ifdef CONFIG_NET_ICMPv6
94+
, g_netstats.icmpv6.sent, g_netstats.icmpv6.recv,
95+
g_netstats.icmpv6.drop
96+
#endif
97+
);
98+
}
99+
#endif

net/netdev/netdev_unregister.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ int netdev_unregister(FAR struct net_driver_s *dev)
150150
#endif
151151
net_unlock();
152152

153+
#if CONFIG_NETDEV_STATISTICS_LOG_PERIOD > 0
154+
work_cancel_sync(NETDEV_STATISTICS_WORK, &dev->d_statistics.logwork);
155+
#endif
156+
153157
#ifdef CONFIG_NET_ETHERNET
154158
ninfo("Unregistered MAC: %02x:%02x:%02x:%02x:%02x:%02x as dev: %s\n",
155159
dev->d_mac.ether.ether_addr_octet[0],

0 commit comments

Comments
 (0)