Linux CAN drivers development
 help / color / mirror / Atom feed
From: Oliver Hartkopp <socketcan@hartkopp.net>
To: linux-can@vger.kernel.org
Cc: Oliver Hartkopp <socketcan@hartkopp.net>
Subject: [PATCH] can: rx-offload: do not count echo frames as received frames
Date: Wed,  2 Dec 2026 13:37:55 +0100	[thread overview]
Message-ID: <20261202123755.3856-1-socketcan@hartkopp.net> (raw)

Drivers using rx-offload do not account received frames themselves.
The RX statistics (rx_packets, rx_bytes) are updated centrally in
can_rx_offload_napi_poll() for every delivered skb except error frames.

This also includes the echo skbs of transmitted frames, which drivers
queue with can_rx_offload_get_echo_skb_queue_timestamp() or
can_rx_offload_get_echo_skb_queue_tail() to deliver them in the right
order with the received frames. The drivers already account these
frames in the TX statistics (tx_packets, tx_bytes) with the length
returned by these functions. As a result the RX statistics of
rx-offload based drivers (e.g. mcp251xfd, flexcan, gs_usb,
rockchip_canfd, m_can in peripheral mode) increase with every
transmitted frame.

can_rx_offload_napi_poll() can not distinguish echo skbs from received
skbs: __can_get_echo_skb() resets skb->pkt_type from PACKET_LOOPBACK to
PACKET_BROADCAST, and skb->sk is not set for all transmitted frames
(e.g. frames forwarded by can-gw).

Mark the echo skbs in the rx-offload control buffer, which already
holds the timestamp for the sorted queue, when they are queued and skip
them in the RX statistics. The flag is initialized in all queueing
paths as the control buffer of an echo skb may still contain data from
the transmit path.

Fixes: 55059f2b7f86 ("can: rx-offload: introduce can_rx_offload_get_echo_skb() and can_rx_offload_queue_sorted() functions")
Fixes: 8e0e2950c9ef ("can: rx-offload: add can_rx_offload_get_echo_skb_queue_tail()")
Tested-by: Oliver Hartkopp <socketcan@hartkopp.net>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
 drivers/net/can/dev/rx-offload.c | 36 +++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/drivers/net/can/dev/rx-offload.c b/drivers/net/can/dev/rx-offload.c
index 46e7b6db4a1e..52d138865e84 100644
--- a/drivers/net/can/dev/rx-offload.c
+++ b/drivers/net/can/dev/rx-offload.c
@@ -8,10 +8,11 @@
 #include <linux/can/dev.h>
 #include <linux/can/rx-offload.h>
 
 struct can_rx_offload_cb {
 	u32 timestamp;
+	bool echo;	/* looped back TX frame, not counted as RX */
 };
 
 static inline struct can_rx_offload_cb *
 can_rx_offload_get_cb(struct sk_buff *skb)
 {
@@ -50,13 +51,14 @@ static int can_rx_offload_napi_poll(struct napi_struct *napi, int quota)
 	int work_done = 0;
 
 	while ((work_done < quota) &&
 	       (skb = skb_dequeue(&offload->skb_queue))) {
 		struct can_frame *cf = (struct can_frame *)skb->data;
+		const struct can_rx_offload_cb *cb = can_rx_offload_get_cb(skb);
 
 		work_done++;
-		if (!(cf->can_id & CAN_ERR_FLAG)) {
+		if (!(cf->can_id & CAN_ERR_FLAG) && !cb->echo) {
 			stats->rx_packets++;
 			if (!(cf->can_id & CAN_RTR_FLAG))
 				stats->rx_bytes += cf->len;
 		}
 		netif_receive_skb(skb);
@@ -166,10 +168,11 @@ can_rx_offload_offload_one(struct can_rx_offload *offload, unsigned int n)
 	}
 
 	/* Mailbox was read. */
 	cb = can_rx_offload_get_cb(skb);
 	cb->timestamp = timestamp;
+	cb->echo = false;
 
 	return skb;
 }
 
 int can_rx_offload_irq_offload_timestamp(struct can_rx_offload *offload,
@@ -217,12 +220,13 @@ int can_rx_offload_irq_offload_fifo(struct can_rx_offload *offload)
 
 	return received;
 }
 EXPORT_SYMBOL_GPL(can_rx_offload_irq_offload_fifo);
 
-int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
-				   struct sk_buff *skb, u32 timestamp)
+static int __can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
+					    struct sk_buff *skb, u32 timestamp,
+					    bool echo)
 {
 	struct can_rx_offload_cb *cb;
 
 	if (skb_queue_len(&offload->skb_queue) >
 	    offload->skb_queue_len_max) {
@@ -230,16 +234,23 @@ int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
 		return -ENOBUFS;
 	}
 
 	cb = can_rx_offload_get_cb(skb);
 	cb->timestamp = timestamp;
+	cb->echo = echo;
 
 	__skb_queue_add_sort(&offload->skb_irq_queue, skb,
 			     can_rx_offload_compare);
 
 	return 0;
 }
+
+int can_rx_offload_queue_timestamp(struct can_rx_offload *offload,
+				   struct sk_buff *skb, u32 timestamp)
+{
+	return __can_rx_offload_queue_timestamp(offload, skb, timestamp, false);
+}
 EXPORT_SYMBOL_GPL(can_rx_offload_queue_timestamp);
 
 unsigned int
 can_rx_offload_get_echo_skb_queue_timestamp(struct can_rx_offload *offload,
 					    unsigned int idx, u32 timestamp,
@@ -253,33 +264,44 @@ can_rx_offload_get_echo_skb_queue_timestamp(struct can_rx_offload *offload,
 
 	skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
 	if (!skb)
 		return 0;
 
-	err = can_rx_offload_queue_timestamp(offload, skb, timestamp);
+	err = __can_rx_offload_queue_timestamp(offload, skb, timestamp, true);
 	if (err) {
 		stats->rx_errors++;
 		stats->tx_fifo_errors++;
 	}
 
 	return len;
 }
 EXPORT_SYMBOL_GPL(can_rx_offload_get_echo_skb_queue_timestamp);
 
-int can_rx_offload_queue_tail(struct can_rx_offload *offload,
-			      struct sk_buff *skb)
+static int __can_rx_offload_queue_tail(struct can_rx_offload *offload,
+				       struct sk_buff *skb, bool echo)
 {
+	struct can_rx_offload_cb *cb;
+
 	if (skb_queue_len(&offload->skb_queue) >
 	    offload->skb_queue_len_max) {
 		dev_kfree_skb_any(skb);
 		return -ENOBUFS;
 	}
 
+	cb = can_rx_offload_get_cb(skb);
+	cb->echo = echo;
+
 	__skb_queue_tail(&offload->skb_irq_queue, skb);
 
 	return 0;
 }
+
+int can_rx_offload_queue_tail(struct can_rx_offload *offload,
+			      struct sk_buff *skb)
+{
+	return __can_rx_offload_queue_tail(offload, skb, false);
+}
 EXPORT_SYMBOL_GPL(can_rx_offload_queue_tail);
 
 unsigned int
 can_rx_offload_get_echo_skb_queue_tail(struct can_rx_offload *offload,
 				       unsigned int idx,
@@ -293,11 +315,11 @@ can_rx_offload_get_echo_skb_queue_tail(struct can_rx_offload *offload,
 
 	skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
 	if (!skb)
 		return 0;
 
-	err = can_rx_offload_queue_tail(offload, skb);
+	err = __can_rx_offload_queue_tail(offload, skb, true);
 	if (err) {
 		stats->rx_errors++;
 		stats->tx_fifo_errors++;
 	}
 
-- 
2.53.0


                 reply	other threads:[~2026-12-02 12:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261202123755.3856-1-socketcan@hartkopp.net \
    --to=socketcan@hartkopp.net \
    --cc=linux-can@vger.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