From d52ff33e78fb7b3b45dd55a0af6516368d87dbea Mon Sep 17 00:00:00 2001 From: Javier Casas Date: Wed, 29 Jan 2025 14:30:11 +0100 Subject: [PATCH] net/can/: add statistics for recv, sent and drop Add support for network statistics for CAN. It includes counters for receive, sent and drop frames. Signed-off-by: Javier Casas --- include/nuttx/net/can.h | 17 ++++++++++++++++- include/nuttx/net/netstats.h | 7 +++++++ net/can/can_callback.c | 6 ++---- net/can/can_input.c | 15 ++++++++++++++- net/can/can_recvmsg.c | 9 ++++++++- net/can/can_sendmsg.c | 5 +++++ net/devif/devif_initialize.c | 2 +- net/procfs/net_statistics.c | 30 ++++++++++++++++++++++++++++-- 8 files changed, 81 insertions(+), 10 deletions(-) diff --git a/include/nuttx/net/can.h b/include/nuttx/net/can.h index 46e39f41a3..3d3856968b 100644 --- a/include/nuttx/net/can.h +++ b/include/nuttx/net/can.h @@ -2,7 +2,8 @@ * include/nuttx/net/can.h * * SPDX-License-Identifier: BSD-3-Clause - * SPDX-FileCopyrightText: 2007, 2009-2012, 2015 Gregory Nutt. All rights reserved. + * SPDX-FileCopyrightText: 2007, 2009-2012, 2015 Gregory Nutt. All + * rights reserved. * SPDX-FileCopyrightText: 2001-2003, Adam Dunkels. All rights reserved. * SPDX-FileContributor: Gregory Nutt * SPDX-FileContributor: Adam Dunkels @@ -43,6 +44,7 @@ #include #include +#include #include /**************************************************************************** @@ -59,6 +61,19 @@ * Public Types ****************************************************************************/ +/* The structure holding the CAN statistics that are gathered if + * CONFIG_NET_STATISTICS is defined. + */ + +#ifdef CONFIG_NET_STATISTICS +struct can_stats_s +{ + net_stats_t drop; /* Number of dropped CAN frames */ + net_stats_t recv; /* Number of received CAN frames */ + net_stats_t sent; /* Number of sent CAN frames */ +}; +#endif + /**************************************************************************** * Public Data ****************************************************************************/ diff --git a/include/nuttx/net/netstats.h b/include/nuttx/net/netstats.h index 686a2dcda5..e4895c9a15 100644 --- a/include/nuttx/net/netstats.h +++ b/include/nuttx/net/netstats.h @@ -66,6 +66,9 @@ #ifdef CONFIG_NET_MLD # include #endif +#ifdef CONFIG_NET_CAN +# include +#endif #ifdef CONFIG_NET_STATISTICS @@ -114,6 +117,10 @@ struct net_stats_s #ifdef CONFIG_NET_UDP struct udp_stats_s udp; /* UDP statistics */ #endif + +#ifdef CONFIG_NET_CAN + struct can_stats_s can; /* CAN statistics */ +#endif }; /**************************************************************************** diff --git a/net/can/can_callback.c b/net/can/can_callback.c index 85db2783d2..6ea110f757 100644 --- a/net/can/can_callback.c +++ b/net/can/can_callback.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "devif/devif.h" #include "can/can.h" @@ -83,10 +84,7 @@ can_data_event(FAR struct net_driver_s *dev, FAR struct can_conn_s *conn, ninfo("Dropped %d bytes\n", dev->d_len); #ifdef CONFIG_NET_STATISTICS - /* No support CAN net statistics yet */ - - /* g_netstats.tcp.drop++; */ - + g_netstats.can.drop++; #endif } diff --git a/net/can/can_input.c b/net/can/can_input.c index f93120b4ee..679c69d85b 100644 --- a/net/can/can_input.c +++ b/net/can/can_input.c @@ -32,6 +32,7 @@ #include #include +#include #include "devif/devif.h" #include "can/can.h" @@ -270,6 +271,10 @@ int can_input(FAR struct net_driver_s *dev) FAR uint8_t *buf; int ret; +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.recv++; +#endif + if (dev->d_iob != NULL) { buf = dev->d_buf; @@ -284,7 +289,15 @@ int can_input(FAR struct net_driver_s *dev) return ret; } - return netdev_input(dev, can_in, false); + ret = netdev_input(dev, can_in, false); + if (ret < 0) + { +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.drop++; +#endif + } + + return ret; } #endif /* CONFIG_NET && CONFIG_NET_CAN */ diff --git a/net/can/can_recvmsg.c b/net/can/can_recvmsg.c index d4c6f34d1b..2d10c6f5fe 100644 --- a/net/can/can_recvmsg.c +++ b/net/can/can_recvmsg.c @@ -41,6 +41,7 @@ #include #include #include +#include #include "netdev/netdev.h" #include "devif/devif.h" @@ -199,7 +200,13 @@ static inline void can_newdata(FAR struct net_driver_s *dev, if (recvlen < dev->d_len) { - can_datahandler(dev, pstate->pr_conn); + if (can_datahandler(dev, pstate->pr_conn) < dev->d_len) + { + ninfo("Dropped %d bytes\n", dev->d_len); +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.drop++; +#endif + } } /* Indicate no data in the buffer */ diff --git a/net/can/can_sendmsg.c b/net/can/can_sendmsg.c index 9dc764dc58..c85242dc89 100644 --- a/net/can/can_sendmsg.c +++ b/net/can/can_sendmsg.c @@ -42,6 +42,7 @@ #include #include #include +#include #include "netdev/netdev.h" #include "devif/devif.h" @@ -301,6 +302,10 @@ ssize_t can_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg, return ret; } +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.sent++; +#endif + /* Return the number of bytes actually sent */ return state.snd_sent; diff --git a/net/devif/devif_initialize.c b/net/devif/devif_initialize.c index 03b9e1d682..e53c1cf8a1 100644 --- a/net/devif/devif_initialize.c +++ b/net/devif/devif_initialize.c @@ -58,7 +58,7 @@ * Public Data ****************************************************************************/ -/* IP/TCP/UDP/ICMP statistics for all network interfaces */ +/* IP/TCP/UDP/ICMP/CAN statistics for all network interfaces */ #ifdef CONFIG_NET_STATISTICS struct net_stats_s g_netstats; diff --git a/net/procfs/net_statistics.c b/net/procfs/net_statistics.c index e868e93195..b4956d40c1 100644 --- a/net/procfs/net_statistics.c +++ b/net/procfs/net_statistics.c @@ -40,7 +40,8 @@ #if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6) || \ defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP) || \ - defined(CONFIG_NET_ICMP) || defined(CONFIG_NET_ICMPv6) + defined(CONFIG_NET_ICMP) || defined(CONFIG_NET_ICMPv6) || \ + defined(CONFIG_NET_CAN) /**************************************************************************** * Private Function Prototypes @@ -135,7 +136,10 @@ static int netprocfs_header(FAR struct netprocfs_file_s *netfile) len += snprintf(&netfile->line[len], NET_LINELEN - len, " ICMP"); #endif #ifdef CONFIG_NET_ICMPv6 - len += snprintf(&netfile->line[len], NET_LINELEN - len, " ICMPv6"); + len += snprintf(&netfile->line[len], NET_LINELEN - len, " ICMPv6"); +#endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " CAN"); #endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); @@ -178,6 +182,11 @@ static int netprocfs_received(FAR struct netprocfs_file_s *netfile) g_netstats.icmpv6.recv); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", + g_netstats.can.recv); +#endif + len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len; } @@ -217,6 +226,10 @@ static int netprocfs_dropped(FAR struct netprocfs_file_s *netfile) len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", g_netstats.icmpv6.drop); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", + g_netstats.can.drop); +#endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len; @@ -280,6 +293,9 @@ static int netprocfs_checksum(FAR struct netprocfs_file_s *netfile) #ifdef CONFIG_NET_ICMPv6 len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----"); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----"); +#endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len; @@ -344,6 +360,9 @@ static int netprocfs_prototype(FAR struct netprocfs_file_s *netfile) len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", g_netstats.icmpv6.typeerr); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----"); +#endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len; @@ -384,6 +403,10 @@ static int netprocfs_sent(FAR struct netprocfs_file_s *netfile) len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", g_netstats.icmpv6.sent); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " %04x", + g_netstats.can.sent); +#endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len; @@ -417,6 +440,9 @@ static int netprocfs_retransmissions(FAR struct netprocfs_file_s *netfile) #ifdef CONFIG_NET_ICMPv6 len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----"); #endif +#ifdef CONFIG_NET_CAN + len += snprintf(&netfile->line[len], NET_LINELEN - len, " ----"); +#endif len += snprintf(&netfile->line[len], NET_LINELEN - len, "\n"); return len;