net: Add statistics gathering support

Collect network statistics if CONFIG_NET_STATISTICS is
defined.

Change-Id: Id217daa3e19142c95396dc391ba2dc4123b18b22
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2016-05-09 17:39:09 +03:00
commit 875ea297d4
2 changed files with 84 additions and 0 deletions

View file

@ -116,3 +116,10 @@ config NET_SLIP
to receive and send network packets via the SLIP driver. to receive and send network packets via the SLIP driver.
The SLIP driver Kconfig options can be tweaked in drivers The SLIP driver Kconfig options can be tweaked in drivers
section. section.
config NET_STATISTICS
bool "Print network statistics"
default n
help
Print network send/receive statistics to console.
This takes memory so say 'n' if unsure.

View file

@ -37,6 +37,7 @@
#include <net/net_if.h> #include <net/net_if.h>
#include <net/nbuf.h> #include <net/nbuf.h>
#include <net/net_core.h> #include <net/net_core.h>
#include <net/net_stats.h>
#include "net_private.h" #include "net_private.h"
@ -49,6 +50,80 @@ static char __noinit __stack rx_fiber_stack[CONFIG_NET_RX_STACK_SIZE];
static struct nano_fifo rx_queue; static struct nano_fifo rx_queue;
static nano_thread_id_t rx_fiber_id; static nano_thread_id_t rx_fiber_id;
#if defined(CONFIG_NET_STATISTICS)
#define PRINT_STAT(fmt, ...) printk(fmt, ##__VA_ARGS__)
struct net_stats net_stats;
#define GET_STAT(s) net_stats.s
#define PRINT_STATISTICS_INTERVAL (30 * sys_clock_ticks_per_sec)
#define net_print_statistics stats /* to make the debug print line shorter */
static inline void stats(void)
{
static uint32_t next_print;
uint32_t curr = sys_tick_get_32();
if (!next_print || (next_print < curr &&
(!((curr - next_print) > PRINT_STATISTICS_INTERVAL)))) {
uint32_t new_print;
#if defined(CONFIG_NET_IPV6)
PRINT_STAT("IPv6 recv %d\tsent\t%d\tdrop\t%d\tforwarded\t%d\n",
GET_STAT(ipv6.recv),
GET_STAT(ipv6.sent),
GET_STAT(ipv6.drop),
GET_STAT(ipv6.forwarded));
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4)
PRINT_STAT("IPv4 recv %d\tsent\t%d\tdrop\t%d\tforwarded\t%d\n",
GET_STAT(ipv4.recv),
GET_STAT(ipv4.sent),
GET_STAT(ipv4.drop),
GET_STAT(ipv4.forwarded));
#endif /* CONFIG_NET_IPV6 */
PRINT_STAT("IP vhlerr %d\thblener\t%d\tlblener\t%d\n",
GET_STAT(ip_errors.vhlerr),
GET_STAT(ip_errors.hblenerr),
GET_STAT(ip_errors.lblenerr));
PRINT_STAT("IP fragerr %d\tchkerr\t%d\tprotoer\t%d\n",
GET_STAT(ip_errors.fragerr),
GET_STAT(ip_errors.chkerr),
GET_STAT(ip_errors.protoerr));
PRINT_STAT("ICMP recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(icmp.recv),
GET_STAT(icmp.sent),
GET_STAT(icmp.drop));
PRINT_STAT("ICMP typeer %d\tchkerr\t%d\n",
GET_STAT(icmp.typeerr),
GET_STAT(icmp.chkerr));
#if defined(CONFIG_NET_UDP)
PRINT_STAT("UDP recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(udp.recv),
GET_STAT(udp.sent),
GET_STAT(udp.drop));
PRINT_STAT("UDP chkerr %d\n",
GET_STAT(icmp.chkerr));
#endif
PRINT_STAT("Processing err %d\n", GET_STAT(processing_error));
new_print = curr + PRINT_STATISTICS_INTERVAL;
if (new_print > curr) {
next_print = new_print;
} else {
/* Overflow */
next_print = PRINT_STATISTICS_INTERVAL -
(0xffffffff - curr);
}
}
}
#else /* CONFIG_NET_STATISTICS */
#define net_print_statistics()
#endif /* CONFIG_NET_STATISTICS */
static inline enum net_verdict process_data(struct net_buf *buf) static inline enum net_verdict process_data(struct net_buf *buf)
{ {
return NET_DROP; return NET_DROP;
@ -80,6 +155,8 @@ static void net_rx_fiber(int unused1, int unused2)
net_nbuf_unref(buf); net_nbuf_unref(buf);
break; break;
} }
net_print_statistics();
} }
} }