Skip to content

Commit

Permalink
net/can/: add statistics for recv, sent and drop
Browse files Browse the repository at this point in the history
Add support for network statistics for CAN.
It includes counters for receive, sent
and drop frames.

Signed-off-by: Javier Casas <[email protected]>
  • Loading branch information
occam25 committed Jan 29, 2025
1 parent 7240565 commit 2aef6a4
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
17 changes: 16 additions & 1 deletion include/nuttx/net/can.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* SPDX-FileContributor: Adam Dunkels <[email protected]>
Expand Down Expand Up @@ -43,6 +44,7 @@

#include <nuttx/config.h>
#include <nuttx/can.h>
#include <nuttx/net/netconfig.h>
#include <stdint.h>

/****************************************************************************
Expand All @@ -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
****************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions include/nuttx/net/netstats.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
#ifdef CONFIG_NET_MLD
# include <nuttx/net/mld.h>
#endif
#ifdef CONFIG_NET_CAN
# include <nuttx/net/can.h>
#endif

#ifdef CONFIG_NET_STATISTICS

Expand Down Expand Up @@ -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
};

/****************************************************************************
Expand Down
7 changes: 3 additions & 4 deletions net/can/can_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <nuttx/net/netconfig.h>
#include <nuttx/net/netdev.h>
#include <nuttx/mm/iob.h>
#include <nuttx/net/netstats.h>

#include "devif/devif.h"
#include "can/can.h"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -236,6 +234,7 @@ uint16_t can_datahandler(FAR struct net_driver_s *dev,
else
{
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
ret = 0;
goto errout;
}

Expand Down
15 changes: 14 additions & 1 deletion net/can/can_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <nuttx/net/netdev.h>
#include <nuttx/net/can.h>
#include <nuttx/net/netstats.h>

#include "devif/devif.h"
#include "can/can.h"
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
9 changes: 8 additions & 1 deletion net/can/can_recvmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <nuttx/semaphore.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/netstats.h>

#include "netdev/netdev.h"
#include "devif/devif.h"
Expand Down Expand Up @@ -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 */
Expand Down
5 changes: 5 additions & 0 deletions net/can/can_sendmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <nuttx/net/netdev.h>
#include <nuttx/net/net.h>
#include <nuttx/net/ip.h>
#include <nuttx/net/netstats.h>

#include "netdev/netdev.h"
#include "devif/devif.h"
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion net/devif/devif_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 27 additions & 2 deletions net/procfs/net_statistics.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

#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
Expand Down Expand Up @@ -135,7 +135,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");
Expand Down Expand Up @@ -178,6 +181,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;
}
Expand Down Expand Up @@ -217,6 +225,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;
Expand Down Expand Up @@ -280,6 +292,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;
Expand Down Expand Up @@ -344,6 +359,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;
Expand Down Expand Up @@ -384,6 +402,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;
Expand Down Expand Up @@ -417,6 +439,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;
Expand Down

0 comments on commit 2aef6a4

Please sign in to comment.