Linux CAN drivers development
 help / color / mirror / Atom feed
* [PATCH] can: rx-offload: do not count echo frames as received frames
@ 2026-12-02 12:37 Oliver Hartkopp
  0 siblings, 0 replies; only message in thread
From: Oliver Hartkopp @ 2026-12-02 12:37 UTC (permalink / raw)
  To: linux-can; +Cc: Oliver Hartkopp

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 12:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:37 [PATCH] can: rx-offload: do not count echo frames as received frames Oliver Hartkopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox