Skip to contentSkip to conversation

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

PATCH In review Nicolas Maier 2 participants 2 messages Jan 14, 2024 – Jan 20, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---

v2:
* add 'traffic_flags' parameter to bcm_rx_update_and_send and bcm_rx_cmp_to_index, to put the flags in the can frame after its data has been copied

 Documentation/networking/can.rst | 34 +++++++++--------
 net/can/bcm.c                    | 65 ++++++++++++++++++++++++++------
 2 files changed, 72 insertions(+), 27 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 +54 -11

165 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#1 lore raw

Hi Nicolas,

Quoted 4 lines · Nicolas Maier

v2:
* add 'traffic_flags' parameter to bcm_rx_update_and_send and bcm_rx_cmp_to_index, to put the flags in the can frame after its data has been copied

That looks great now!

net/can/bcm.c static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head, · line 365

Quoted 8 lines
net/can/bcm.c +3 -1
@@ -344,8 +360,16 @@ static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
  		 * relevant for updates that are generated by the
  		 * BCM, where nframes is 1
  		 */
-		if (head->nframes == 1)
+		if (head->nframes == 1) {
+			pflags = bcm_flags(skb);
+			*pflags = 0;

This initialization of the msg flags always has to be done.

I've sent a 'v3' patch that also simplifies the sock_skb_cb_check_size() usage:

https://lore.kernel.org/linux-can/[email protected]/

Please check if the changes fit for you.

I also created some user space testing code in my can-tests repo to prove the correct msg flags implementation:

https://github.com/hartkopp/can-tests/commit/67957def0b0035d79000f5baad93f8764ec11fc4

Works fine ;-)

Best regards,
Oliver