Skip to contentSkip to conversation

can: bcm: add recvmsg flags for own, local and remote traffic

PATCH Superseded Nicolas Maier 2 participants 3 messages Jan 7, 2024 – Jan 14, 2024

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 can: bcm: add recvmsg flags for own, local and remote traffic

CAN RAW sockets allow userspace to tell if a received CAN frame comes from the same socket, another socket on the same host, or another host. See commit 1e55659ce6dd ("can-raw: add msg_flags to distinguish local traffic"). However, this feature is missing in CAN BCM sockets.

Add the same feature to CAN BCM sockets. When reading a received frame (opcode RX_CHANGED) using recvmsg, two flags in msg->msg_flags may be set following the previous convention (from CAN RAW), to distinguish between 'own', 'local' and 'remote' CAN traffic.

Update the documentation to reflect this change.

---
 Documentation/networking/can.rst | 34 ++++++++++++++------------
 net/can/bcm.c                    | 42 +++++++++++++++++++++++++++++---
 2 files changed, 56 insertions(+), 20 deletions(-)
Documentation/networking/can.rst +18 -16
index d7e1ada905b2..62519d38c58b 100644
@@ -444,6 +444,24 @@ definitions are specified for CAN specific MTUs in include/linux/can.h:
   #define CANFD_MTU (sizeof(struct canfd_frame)) == 72  => CAN FD frame
 
 
+Returned Message Flags
+----------------------
+
+When using the system call recvmsg(2) on a RAW or a BCM socket, the
+msg->msg_flags field may contain the following flags:
+
+MSG_DONTROUTE:
+	set when the received frame was created on the local host.
+
+MSG_CONFIRM:
+	set when the frame was sent via the socket it is received on.
+	This flag can be interpreted as a 'transmission confirmation' when the
+	CAN driver supports the echo of frames on driver level, see
+	:ref:`socketcan-local-loopback1` and :ref:`socketcan-local-loopback2`.
+	(Note: In order to receive such messages on a RAW socket,
+	CAN_RAW_RECV_OWN_MSGS must be set.)
+
+
 .. _socketcan-raw-sockets:
 
 RAW Protocol Sockets with can_filters (SOCK_RAW)
@@ -693,22 +711,6 @@ where the CAN_INV_FILTER flag is set in order to notch single CAN IDs or
 CAN ID ranges from the incoming traffic.
 
 
-RAW Socket Returned Message Flags
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-When using recvmsg() call, the msg->msg_flags may contain following flags:
-
-MSG_DONTROUTE:
-	set when the received frame was created on the local host.
-
-MSG_CONFIRM:
-	set when the frame was sent via the socket it is received on.
-	This flag can be interpreted as a 'transmission confirmation' when the
-	CAN driver supports the echo of frames on driver level, see
-	:ref:`socketcan-local-loopback1` and :ref:`socketcan-local-loopback2`.
-	In order to receive such messages, CAN_RAW_RECV_OWN_MSGS must be set.
-
-
 Broadcast Manager Protocol Sockets (SOCK_DGRAM)
 -----------------------------------------------
 
net/can/bcm.c +38 -4

93 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Nicolas,

thanks for the patch!

Quoted 13 lines · Nicolas Maier
net/can/bcm.c +1 -1
@@ -642,7 +666,7 @@ static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
  static void bcm_rx_handler(struct sk_buff *skb, void *data)
  {
  	struct bcm_op *op = (struct bcm_op *)data;
-	const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
+	struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
  	unsigned int i;
 
  	if (op->can_id != rxframe->can_id)
@@ -657,6 +681,13 @@ static void bcm_rx_handler(struct sk_buff *skb, void *data)
  			return;
  	}
 

net/can/bcm.c static void bcm_rx_handler(struct sk_buff *skb, void *data) · line 693

Quoted 10 lines
net/can/bcm.c +7 -0
+	/* add flags to distinguish between own/local/remote CAN traffic */
+	if (skb->sk) {
+		rxframe->flags |= RX_LOCAL;
+		if (skb->sk == op->sk)
+			rxframe->flags |= RX_OWN;
+	}
+
  	/* disable timeout */
  	hrtimer_cancel(&op->timer);
 

No. You are writing to a read-only skbuff, which is not yet cloned or copied. The read-only handling of the skb is done in the NET_RX_SOFTIRQ triggering can_rcv_filter() and friends.

See this note:

https://elixir.bootlin.com/linux/v6.7/source/net/can/af_can.c#L430

When you are also changing the CAN frames' content you need to skb_copy() the provided skb, see:

https://elixir.bootlin.com/linux/v6.7/source/net/can/gw.c#L504

So if you want to pass these new flags to the user space you should think of extending the parameter list of bcm_rx_update_and_send() and bcm_rx_cmp_to_index().

But in the first place I'm interested to know what the use-case for this extension is.

Best regards,
Oliver

#2 lore raw

Hello,

Thanks a lot for the feedback!

Quoted 2 lines · Oliver Hartkopp

But in the first place I'm interested to know what the use-case for this extension is.

My goal with this extension is to be able to implement a reliable 'CAN send' function in userspace which would check that the message has been correctly transmitted, or retry if no confirmation was received after a timeout (I am using a device which empties some TX buffer when recovering from the Bus-Off state, therefore some messages will be lost, which is why the reliability needs to be handled elsewhere). For this purpose, knowing the origin of a received message is needed.

This was already possible with RAW sockets, and the goal here is to provide the same information when using BCM sockets.

Best regards,
Nicolas