public inbox for linux-can@vger.kernel.org
 help / color / mirror / Atom feed
From: Oliver Hartkopp <socketcan@hartkopp.net>
To: linux-can@vger.kernel.org
Cc: Oliver Hartkopp <socketcan@hartkopp.net>,
	Vincent Mailhol <mailhol@kernel.org>
Subject: [PATCH] dummy_can: fix packet statistics
Date: Sat, 24 Jan 2026 22:17:04 +0100	[thread overview]
Message-ID: <20260124211704.16430-1-socketcan@hartkopp.net> (raw)

The former implementation was only counting the tx_packets value.
Adopt the skb handling from vcan.c to correctly count the statistics
and fix the packet flow for looped packets.
The CAN echo support (IFF_ECHO) is enabled in dummy_can_init() to provide
a more realistic behaviour of real CAN hardware interfaces.

Fixes: 816cf430e84b ("can: add dummy_can driver")
Cc: Vincent Mailhol <mailhol@kernel.org>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 drivers/net/can/dummy_can.c | 50 ++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/drivers/net/can/dummy_can.c b/drivers/net/can/dummy_can.c
index 41953655e3d3..a77f5fc5cfda 100644
--- a/drivers/net/can/dummy_can.c
+++ b/drivers/net/can/dummy_can.c
@@ -207,17 +207,60 @@ static int dummy_can_netdev_close(struct net_device *dev)
 }
 
 static netdev_tx_t dummy_can_start_xmit(struct sk_buff *skb,
 					struct net_device *dev)
 {
+	struct net_device_stats *stats = &dev->stats;
+	unsigned int len;
+	bool loop;
+
 	if (can_dev_dropped_skb(dev, skb))
 		return NETDEV_TX_OK;
 
-	can_put_echo_skb(skb, dev, 0, 0);
-	dev->stats.tx_packets++;
-	dev->stats.tx_bytes += can_get_echo_skb(dev, 0, NULL);
+	len = can_skb_get_data_len(skb);
+	stats->tx_packets++;
+	stats->tx_bytes += len;
+
+	/* set flag whether this packet has to be looped back */
+	loop = skb->pkt_type == PACKET_LOOPBACK;
+
+	skb_tx_timestamp(skb);
+
+	/* driver supports echo handling - see dummy_can_init() */
+	if (!(dev->flags & IFF_ECHO)) {
+		/* no echo handling available inside this driver */
+		if (loop) {
+			/* only count the packets here, because the
+			 * CAN core already did the echo for us
+			 */
+			stats->rx_packets++;
+			stats->rx_bytes += len;
+		}
+		consume_skb(skb);
+		return NETDEV_TX_OK;
+	}
+
+	/* perform standard echo handling for CAN network interfaces */
+
+	if (loop) {
+		skb = can_create_echo_skb(skb);
+		if (!skb)
+			return NETDEV_TX_OK;
 
+		/* receive with packet counting */
+		stats->rx_packets++;
+		stats->rx_bytes += len;
+
+		skb->pkt_type  = PACKET_BROADCAST;
+		skb->dev       = dev;
+		skb->ip_summed = CHECKSUM_UNNECESSARY;
+
+		netif_rx(skb);
+	} else {
+		/* no looped packets => no counting */
+		consume_skb(skb);
+	}
 	return NETDEV_TX_OK;
 }
 
 static const struct net_device_ops dummy_can_netdev_ops = {
 	.ndo_open = dummy_can_netdev_open,
@@ -239,10 +282,11 @@ static int __init dummy_can_init(void)
 	if (!dev)
 		return -ENOMEM;
 
 	dev->netdev_ops = &dummy_can_netdev_ops;
 	dev->ethtool_ops = &dummy_can_ethtool_ops;
+	dev->flags |= IFF_ECHO; /* enable echo handling */
 	priv = netdev_priv(dev);
 	priv->can.bittiming_const = &dummy_can_bittiming_const;
 	priv->can.bitrate_max = 20 * MEGA /* BPS */;
 	priv->can.clock.freq = 160 * MEGA /* Hz */;
 	priv->can.fd.data_bittiming_const = &dummy_can_fd_databittiming_const;
-- 
2.47.3


             reply	other threads:[~2026-01-24 21:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-24 21:17 Oliver Hartkopp [this message]
2026-01-25 19:31 ` [PATCH] dummy_can: fix packet statistics Vincent Mailhol
2026-01-26 10:47   ` Oliver Hartkopp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260124211704.16430-1-socketcan@hartkopp.net \
    --to=socketcan@hartkopp.net \
    --cc=linux-can@vger.kernel.org \
    --cc=mailhol@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox