Skip to contentSkip to conversation

can: support CAN XL

RFC PATCH Superseded Oliver Hartkopp 3 participants 11 messages Jul 17, 2022 – Jul 20, 2022

Subsystems (3)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Oliver Hartkopp

author maintainer
#0 lore raw

v3 cover can: support CAN XL

The CAN with eXtended data Length (CAN XL) is a new CAN protocol with a 10Mbit/s data transfer with a new physical layer transceiver (for this data section). CAN XL allows up to 2048 byte of payload and shares the arbitration principle (11 bit priority) known from Classical CAN and CAN FD. RTR and 29 bit identifiers are not implemented in CAN XL.

A short introdution to CAN XL can be found here:
https://www.bosch-semiconductors.com/media/ip_modules/pdf_2/can_xl_1/canxl_intro_20210225.pdf

V2: Major rework after discussion and feedback on Linux-CAN ML

- rework of struct canxl_frame
- CANXL_XLF flag is now the switch between CAN XL and CAN/CANFD
- variable length in r/w operations for CAN XL frames
- write CAN XL frame to raw socket enforces size <-> canxl_frame.len sync

V3: Fix length for CAN XL frames inside the sk_buff

- extend the CAN_RAW sockopt to handle fixed/truncated read/write operations

Oliver Hartkopp (5):
can: canxl: introduce CAN XL data structure
can: canxl: introduce ETH_P_CANXL ethernet protocol handling
can: dev: add CAN XL support
can: vcan: add CAN XL support
can: raw: add CAN XL support

 drivers/net/can/dev/rx-offload.c |  2 +-
 drivers/net/can/dev/skb.c        | 49 +++++++++++++----
 drivers/net/can/vcan.c           | 11 ++--
 include/linux/can/skb.h          | 44 +++++++++++++++-
 include/uapi/linux/can.h         | 49 +++++++++++++++++
 include/uapi/linux/can/raw.h     |  6 +++
 include/uapi/linux/if_ether.h    |  1 +
 net/can/af_can.c                 | 32 ++++++++++--
 net/can/raw.c                    | 90 ++++++++++++++++++++++++++++----
 9 files changed, 252 insertions(+), 32 deletions(-)
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

RFC PATCH v3 1/5 can: canxl: introduce CAN XL data structure

This patch adds defines for data structures and length information for CAN XL (CAN with eXtended data Length) which can transfer up to 2048 byte insinde a single frame.

Notable changes from CAN FD:

- the 11 bit arbitration field is now named 'priority' instead of 'can_id' (there are no 29 bit identifiers nor RTR frames anymore)
- the data length needs a uint16 value to cover up to 2048 byte (the length element position is different to struct can[fd]_frame)
- new fields (SDT, AF) and a SEC bit have been introduced
- the virtual CAN interface identifier is not part if the CAN XL frame struct as this VCID value is stored in struct skbuff (analog to vlan id)

---
 include/uapi/linux/can.h | 49 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
include/uapi/linux/can.h +49 -0
index 90801ada2bbe..e736ed7be32d 100644
@@ -46,10 +46,11 @@
 #ifndef _UAPI_CAN_H
 #define _UAPI_CAN_H
 
 #include <linux/types.h>
 #include <linux/socket.h>
+#include <linux/stddef.h> /* for offsetof */
 
 /* controller area network (CAN) kernel definitions */
 
 /* special address description flags for the CAN_ID */
 #define CAN_EFF_FLAG 0x80000000U /* EFF/SFF is set in the MSB */
@@ -58,10 +59,11 @@
 
 /* valid bits in CAN ID for frame formats */
 #define CAN_SFF_MASK 0x000007FFU /* standard frame format (SFF) */
 #define CAN_EFF_MASK 0x1FFFFFFFU /* extended frame format (EFF) */
 #define CAN_ERR_MASK 0x1FFFFFFFU /* omit EFF, RTR, ERR flags */
+#define CANXL_PRIO_MASK CAN_SFF_MASK /* 11 bit priority mask */
 
 /*
  * Controller Area Network Identifier structure
  *
  * bit 0-28	: CAN identifier (11/29 bit)
@@ -71,10 +73,11 @@
  */
 typedef __u32 canid_t;
 
 #define CAN_SFF_ID_BITS		11
 #define CAN_EFF_ID_BITS		29
+#define CANXL_PRIO_BITS		CAN_SFF_ID_BITS
 
 /*
  * Controller Area Network Error Message Frame Mask structure
  *
  * bit 0-28	: error class mask (see include/uapi/linux/can/error.h)
@@ -89,10 +92,20 @@ typedef __u32 can_err_mask_t;
 
 /* CAN FD payload length and DLC definitions according to ISO 11898-7 */
 #define CANFD_MAX_DLC 15
 #define CANFD_MAX_DLEN 64
 
+/*
+ * CAN XL payload length and DLC definitions according to ISO 11898-1
+ * CAN XL DLC ranges from 0 .. 2047 => data length from 1 .. 2048 byte
+ */
+#define CANXL_MIN_DLC 0
+#define CANXL_MAX_DLC 2047
+#define CANXL_MAX_DLC_MASK 0x07FF
+#define CANXL_MIN_DLEN 1
+#define CANXL_MAX_DLEN 2048
+
 /**
  * struct can_frame - Classical CAN frame structure (aka CAN 2.0B)
  * @can_id:   CAN ID of the frame and CAN_*_FLAG flags, see canid_t definition
  * @len:      CAN frame payload length in byte (0 .. 8)
  * @can_dlc:  deprecated name for CAN frame payload length in byte (0 .. 8)
@@ -164,12 +177,48 @@ struct canfd_frame {
 	__u8    __res0;  /* reserved / padding */
 	__u8    __res1;  /* reserved / padding */
 	__u8    data[CANFD_MAX_DLEN] __attribute__((aligned(8)));
 };
 
+/*
+ * defined bits for canxl_frame.flags
+ *
+ * The canxl_frame.flags element contains two bits CANXL_XLF and CANXL_SEC
+ * and shares the relative position of the struct can[fd]_frame.len element.
+ * The CANXL_XLF bit ALWAYS needs to be set to indicate a valid CAN XL frame.
+ * As a side effect setting this bit intentionally breaks the length checks
+ * for Classical CAN and CAN FD frames.
+ *
+ * Undefined bits in canxl_frame.flags are reserved and shall be set to zero.
+ */
+#define CANXL_XLF 0x80 /* mandatory CAN XL frame flag (must always be set!) */
+#define CANXL_SEC 0x01 /* Simple Extended Content (security/segmentation) */
+
+/**
+ * struct canxl_frame - CAN with e'X'tended frame 'L'ength frame structure
+ * @prio:  11 bit arbitration priority with zero'ed CAN_*_FLAG flags
+ * @flags: additional flags for CAN XL
+ * @sdt:   SDU (service data unit) type
+ * @len:   frame payload length in byte (1 .. CANXL_MAX_DLEN)
+ * @af:    acceptance field
+ * @data:  CAN XL frame payload (up to CANXL_MAX_DLEN byte)
+ *
+ * @prio shares the same position as @can_id from struct can[fd]_frame.
+ */
+struct canxl_frame {
+	canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
+	__u8    flags; /* additional flags for CAN XL */
+	__u8    sdt;   /* SDU (service data unit) type */
+	__u16   len;   /* frame payload length in byte */
+	__u32   af;    /* acceptance field */
+	__u8    data[CANXL_MAX_DLEN];
+};
+
 #define CAN_MTU		(sizeof(struct can_frame))
 #define CANFD_MTU	(sizeof(struct canfd_frame))
+#define CANXL_MTU	(sizeof(struct canxl_frame))
+#define CANXL_HDR_SZ	(offsetof(struct canxl_frame, data))
 
 /* particular protocols of the protocol family PF_CAN */
 #define CAN_RAW		1 /* RAW sockets */
 #define CAN_BCM		2 /* Broadcast Manager */
 #define CAN_TP16	3 /* VAG Transport Protocol v1.6 */
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

RFC PATCH v3 2/5 can: canxl: introduce ETH_P_CANXL ethernet protocol handling

In reply to Oliver Hartkopp

Enable the PF_CAN infrastructure to handle CAN XL frames. A new ethernet protocol type ETH_P_CANXL is defined to tag skbuffs containing the CAN XL frame data structure.

As the length information is now a uint16 value for CAN XL three new helper functions have been introduced to retrieve the data length from all types of CAN frames.

---
 include/linux/can/skb.h       | 39 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/if_ether.h |  1 +
 net/can/af_can.c              | 32 +++++++++++++++++++++++-----
 3 files changed, 67 insertions(+), 5 deletions(-)
include/linux/can/skb.h +39 -0
index 182749e858b3..51481f5afe62 100644
@@ -101,6 +101,45 @@ static inline bool can_is_canfd_skb(const struct sk_buff *skb)
 {
 	/* the CAN specific type of skb is identified by its data length */
 	return skb->len == CANFD_MTU;
 }
 
+static inline bool can_is_canxl_skb(const struct sk_buff *skb)
+{
+	const struct canxl_frame *cfx = (struct canxl_frame *)skb->data;
+
+	if (skb->len != CANXL_MTU)
+		return false;
+
+	/* check valid CAN XL data length boundaries */
+	if (cfx->len < CANXL_MIN_DLEN || cfx->len > CANXL_MAX_DLEN)
+		return false;
+
+	return cfx->flags & CANXL_XLF;
+}
+
+/* get length element value from can[|fd|xl]_frame structure */
+static inline unsigned int can_skb_get_len_val(struct sk_buff *skb)
+{
+	const struct canxl_frame *cfx = (struct canxl_frame *)skb->data;
+	const struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
+
+	if (can_is_canxl_skb(skb))
+		return cfx->len;
+
+	return cfd->len;
+}
+
+/* get needed data length inside of CAN frame for all frame types (RTR aware) */
+static inline unsigned int can_skb_get_data_len(struct sk_buff *skb)
+{
+	unsigned int len = can_skb_get_len_val(skb);
+	const struct can_frame *cf = (struct can_frame *)skb->data;
+
+	/* RTR frames have an actual length of zero */
+	if (skb->len == CAN_MTU && cf->can_id & CAN_RTR_FLAG)
+		return 0;
+
+	return len;
+}
+
 #endif /* !_CAN_SKB_H */
include/uapi/linux/if_ether.h +1 -0
index d370165bc621..69e0457eb200 100644
@@ -136,10 +136,11 @@
 #define ETH_P_WAN_PPP   0x0007          /* Dummy type for WAN PPP frames*/
 #define ETH_P_PPP_MP    0x0008          /* Dummy type for PPP MP frames */
 #define ETH_P_LOCALTALK 0x0009		/* Localtalk pseudo type 	*/
 #define ETH_P_CAN	0x000C		/* CAN: Controller Area Network */
 #define ETH_P_CANFD	0x000D		/* CANFD: CAN flexible data rate*/
+#define ETH_P_CANXL	0x000E		/* CANXL: eXtended frame Length */
 #define ETH_P_PPPTALK	0x0010		/* Dummy type for Atalk over PPP*/
 #define ETH_P_TR_802_2	0x0011		/* 802.2 frames 		*/
 #define ETH_P_MOBITEX	0x0015		/* Mobitex ([email protected])	*/
 #define ETH_P_CONTROL	0x0016		/* Card specific control frames */
 #define ETH_P_IRDA	0x0017		/* Linux-IrDA			*/
net/can/af_can.c +27 -5
index 1fb49d51b25d..23e56e4e2457 100644
@@ -209,19 +209,20 @@ int can_send(struct sk_buff *skb, int loop)
 			goto inval_skb;
 	} else if (skb->len == CANFD_MTU) {
 		skb->protocol = htons(ETH_P_CANFD);
 		if (unlikely(cfd->len > CANFD_MAX_DLEN))
 			goto inval_skb;
+	} else if (skb->len == CANXL_MTU) {
+		skb->protocol = htons(ETH_P_CANXL);
+		if (unlikely(!can_is_canxl_skb(skb)))
+			goto inval_skb;
 	} else {
 		goto inval_skb;
 	}
 
-	/* Make sure the CAN frame can pass the selected CAN netdevice.
-	 * As structs can_frame and canfd_frame are similar, we can provide
-	 * CAN FD frames to legacy CAN drivers as long as the length is <= 8
-	 */
-	if (unlikely(skb->len > skb->dev->mtu && cfd->len > CAN_MAX_DLEN)) {
+	/* Make sure the CAN frame can pass the selected CAN netdevice */
+	if (unlikely(skb->len > skb->dev->mtu)) {
 		err = -EMSGSIZE;
 		goto inval_skb;
 	}
 
 	if (unlikely(skb->dev->type != ARPHRD_CAN)) {
@@ -725,10 +726,25 @@ static int canfd_rcv(struct sk_buff *skb, struct net_device *dev,
 free_skb:
 	kfree_skb(skb);
 	return NET_RX_DROP;
 }
 
+static int canxl_rcv(struct sk_buff *skb, struct net_device *dev,
+		     struct packet_type *pt, struct net_device *orig_dev)
+{
+	if (unlikely(dev->type != ARPHRD_CAN || (!can_is_canxl_skb(skb)))) {
+		pr_warn_once("PF_CAN: dropped non conform CAN XL skbuff: dev type %d, len %d\n",
+			     dev->type, skb->len);
+
+		kfree_skb(skb);
+		return NET_RX_DROP;
+	}
+
+	can_receive(skb, dev);
+	return NET_RX_SUCCESS;
+}
+
 /* af_can protocol functions */
 
 /**
  * can_proto_register - register CAN transport protocol
  * @cp: pointer to CAN protocol structure
@@ -849,10 +865,15 @@ static struct packet_type can_packet __read_mostly = {
 static struct packet_type canfd_packet __read_mostly = {
 	.type = cpu_to_be16(ETH_P_CANFD),
 	.func = canfd_rcv,
 };
 
+static struct packet_type canxl_packet __read_mostly = {
+	.type = cpu_to_be16(ETH_P_CANXL),
+	.func = canxl_rcv,
+};
+
 static const struct net_proto_family can_family_ops = {
 	.family = PF_CAN,
 	.create = can_create,
 	.owner  = THIS_MODULE,
 };
@@ -888,10 +909,11 @@ static __init int can_init(void)
 	if (err)
 		goto out_sock;
 
 	dev_add_pack(&can_packet);
 	dev_add_pack(&canfd_packet);
+	dev_add_pack(&canxl_packet);
 
 	return 0;
 
 out_sock:
 	unregister_pernet_subsys(&can_pernet_ops);
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

RFC PATCH v3 3/5 can: dev: add CAN XL support

In reply to Oliver Hartkopp

Extend the CAN device driver infrastructure to handle CAN XL frames. This especially addresses the increased data length which is extended to uint16 for CAN XL.

---
 drivers/net/can/dev/rx-offload.c |  2 +-
 drivers/net/can/dev/skb.c        | 49 ++++++++++++++++++++++++++------
 include/linux/can/skb.h          |  5 +++-
 3 files changed, 45 insertions(+), 11 deletions(-)
drivers/net/can/dev/rx-offload.c +1 -1
index a32a01c172d4..8505e547e922 100644
@@ -245,11 +245,11 @@ unsigned int can_rx_offload_get_echo_skb(struct can_rx_offload *offload,
 					 unsigned int *frame_len_ptr)
 {
 	struct net_device *dev = offload->dev;
 	struct net_device_stats *stats = &dev->stats;
 	struct sk_buff *skb;
-	u8 len;
+	unsigned int len;
 	int err;
 
 	skb = __can_get_echo_skb(dev, idx, &len, frame_len_ptr);
 	if (!skb)
 		return 0;
drivers/net/can/dev/skb.c +40 -9

111 lines not on this page · raw message

include/linux/can/skb.h +4 -1
index 51481f5afe62..9972c9bd73bc 100644
@@ -18,19 +18,22 @@
 
 void can_flush_echo_skb(struct net_device *dev);
 int can_put_echo_skb(struct sk_buff *skb, struct net_device *dev,
 		     unsigned int idx, unsigned int frame_len);
 struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx,
-				   u8 *len_ptr, unsigned int *frame_len_ptr);
+				   unsigned int *len_ptr,
+				   unsigned int *frame_len_ptr);
 unsigned int __must_check can_get_echo_skb(struct net_device *dev,
 					   unsigned int idx,
 					   unsigned int *frame_len_ptr);
 void can_free_echo_skb(struct net_device *dev, unsigned int idx,
 		       unsigned int *frame_len_ptr);
 struct sk_buff *alloc_can_skb(struct net_device *dev, struct can_frame **cf);
 struct sk_buff *alloc_canfd_skb(struct net_device *dev,
 				struct canfd_frame **cfd);
+struct sk_buff *alloc_canxl_skb(struct net_device *dev,
+				struct canxl_frame **cfx);
 struct sk_buff *alloc_can_err_skb(struct net_device *dev,
 				  struct can_frame **cf);
 bool can_dropped_invalid_skb(struct net_device *dev, struct sk_buff *skb);
 
 /*
Signature · 1 line
2.30.2
author maintainer
#4 lore raw

RFC PATCH v3 4/5 can: vcan: add CAN XL support

In reply to Oliver Hartkopp
---
 drivers/net/can/vcan.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/net/can/vcan.c +5 -6
index a15619d883ec..d72c0727b440 100644
@@ -68,33 +68,32 @@ static bool echo; /* echo testing. Default: 0 (Off) */
 module_param(echo, bool, 0444);
 MODULE_PARM_DESC(echo, "Echo sent frames (for testing). Default: 0 (Off)");
 
 static void vcan_rx(struct sk_buff *skb, struct net_device *dev)
 {
-	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 	struct net_device_stats *stats = &dev->stats;
 
 	stats->rx_packets++;
-	stats->rx_bytes += cfd->len;
+	stats->rx_bytes += can_skb_get_data_len(skb);
 
 	skb->pkt_type  = PACKET_BROADCAST;
 	skb->dev       = dev;
 	skb->ip_summed = CHECKSUM_UNNECESSARY;
 
 	netif_rx(skb);
 }
 
 static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)
 {
-	struct canfd_frame *cfd = (struct canfd_frame *)skb->data;
 	struct net_device_stats *stats = &dev->stats;
-	int loop, len;
+	unsigned int len;
+	int loop;
 
 	if (can_dropped_invalid_skb(dev, skb))
 		return NETDEV_TX_OK;
 
-	len = cfd->can_id & CAN_RTR_FLAG ? 0 : cfd->len;
+	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;
@@ -132,11 +131,11 @@ static int vcan_change_mtu(struct net_device *dev, int new_mtu)
 {
 	/* Do not allow changing the MTU while running */
 	if (dev->flags & IFF_UP)
 		return -EBUSY;
 
-	if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU)
+	if (new_mtu != CAN_MTU && new_mtu != CANFD_MTU && new_mtu != CANXL_MTU)
 		return -EINVAL;
 
 	dev->mtu = new_mtu;
 	return 0;
 }
Signature · 1 line
2.30.2
author maintainer
#5 lore raw

RFC PATCH v3 5/5 can: raw: add CAN XL support

In reply to Oliver Hartkopp

Enable CAN_RAW sockets to read and write CAN XL frames analogue to the CAN FD extension (new CAN_RAW_XL_FRAMES sockopt).

A CAN XL network interface is capable to handle Classical CAN, CAN FD and CAN XL frames. When CAN_RAW_XL_FRAMES is enabled, the CAN_RAW socket checks whether the addressed CAN network interface is capable to handle the provided CAN frame.

---
 include/uapi/linux/can/raw.h |  6 +++
 net/can/raw.c                | 90 ++++++++++++++++++++++++++++++++----
 2 files changed, 86 insertions(+), 10 deletions(-)
include/uapi/linux/can/raw.h +6 -0
index 3386aa81fdf2..0c55d48ae867 100644
@@ -60,8 +60,14 @@ enum {
 	CAN_RAW_ERR_FILTER,	/* set filter for error frames       */
 	CAN_RAW_LOOPBACK,	/* local loopback (default:on)       */
 	CAN_RAW_RECV_OWN_MSGS,	/* receive my own msgs (default:off) */
 	CAN_RAW_FD_FRAMES,	/* allow CAN FD frames (default:off) */
 	CAN_RAW_JOIN_FILTERS,	/* all filters must match to trigger */
+	CAN_RAW_XL_FRAMES,	/* allow CAN XL frames (default:off) */
 };
 
+/* CAN XL data transfer modes for CAN_RAW_XL_FRAMES sockopt */
+#define CAN_RAW_XL_ENABLE (1 << 0) /* enable CAN XL frames on this socket */
+#define CAN_RAW_XL_RX_DYN (1 << 1) /* allow truncated data[] for read() */
+#define CAN_RAW_XL_TX_DYN (1 << 2) /* allow truncated data[] for write() */
+
 #endif /* !_UAPI_CAN_RAW_H */
net/can/raw.c +80 -10

187 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#6 lore raw

Re: [RFC PATCH v3 5/5] can: raw: add CAN XL support

Quoted 21 lines · Oliver Hartkopp

Enable CAN_RAW sockets to read and write CAN XL frames analogue to the CAN FD extension (new CAN_RAW_XL_FRAMES sockopt).

A CAN XL network interface is capable to handle Classical CAN, CAN FD and CAN XL frames. When CAN_RAW_XL_FRAMES is enabled, the CAN_RAW socket checks whether the addressed CAN network interface is capable to handle the provided CAN frame.

---
  include/uapi/linux/can/raw.h |  6 +++
  net/can/raw.c                | 90 ++++++++++++++++++++++++++++++++----
  2 files changed, 86 insertions(+), 10 deletions(-)
include/uapi/linux/can/raw.h +0 -0
index 3386aa81fdf2..0c55d48ae867 100644
@@ -60,8 +60,14 @@ enum {
  	CAN_RAW_ERR_FILTER,	/* set filter for error frames       */
  	CAN_RAW_LOOPBACK,	/* local loopback (default:on)       */

include/uapi/linux/can/raw.h enum { · line 71

Quoted 10 lines
include/uapi/linux/can/raw.h +5 -0
  	CAN_RAW_RECV_OWN_MSGS,	/* receive my own msgs (default:off) */
  	CAN_RAW_FD_FRAMES,	/* allow CAN FD frames (default:off) */
  	CAN_RAW_JOIN_FILTERS,	/* all filters must match to trigger */
+	CAN_RAW_XL_FRAMES,	/* allow CAN XL frames (default:off) */
  };
 
+/* CAN XL data transfer modes for CAN_RAW_XL_FRAMES sockopt */
+#define CAN_RAW_XL_ENABLE (1 << 0) /* enable CAN XL frames on this socket */
+#define CAN_RAW_XL_RX_DYN (1 << 1) /* allow truncated data[] for read() */
+#define CAN_RAW_XL_TX_DYN (1 << 2) /* allow truncated data[] for write() */

s/allow/enable/

When CAN_RAW_XL_TX_DYN is enabled the truncated data size is enforced. So there is no mix of truncated/fixed CAN XL frames allowed ...

Regards,
Oliver

#7 lore raw
In reply to Oliver Hartkopp

Hello Oliver,

Quoted 6 lines · On Sunday 17 of July 2022 15:27:25 Oliver Hartkopp wrote:

V2: Major rework after discussion and feedback on Linux-CAN ML

- rework of struct canxl_frame
- CANXL_XLF flag is now the switch between CAN XL and CAN/CANFD
- variable length in r/w operations for CAN XL frames
- write CAN XL frame to raw socket enforces size <-> canxl_frame.len sync

I generally like the idea but I would like even to extend it to process all CAN messages types through same API.

+struct canxl_frame {

+       canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
+       __u8    flags; /* additional flags for CAN XL */
+       __u8    sdt;   /* SDU (service data unit) type */
+       __u16   len;   /* frame payload length in byte */
+       __u32   af;    /* acceptance field */
+       __u8    data[CANXL_MAX_DLEN];

+};

I would suggest to think about possibility to have single structure type for processing of all CAN frames types in usersace. When you define field prio as anonyous union of prio and can_id

  union {
    canid_t prio;
    canid_t can_id;
  }

then it is possible to define flags such way, that canxl_frame is alternative for all other formats

+#define CANXL_XLF 0x80 /* mandatory CAN XL frame flag (must always be set!) */ So CANXL_XLF will be changed to CANXLS_SELECT frame structure selected, then rest of bits can be used for CANXLS_XLF, CANXLS_FD, in ideal case even RTR, BRS etc.. or RTR can be left as part of ID if that is easier. This way only single structure can be used to receive and send all frames over single interface when XL option is selected
by software.

Yes, there would be redundancy in the kernel handling which has to accept two types of encoding of CAN FD and standard messages but actual complexity in usespace when you want to support all variants and for example forward frames between interfaces or process them inside QEMU etc. is really quite high.

Best wishes,

Pavel

Signature · 12 lines
                Pavel Pisa
    phone:      +420 603531357
    e-mail:     [email protected]
    Department of Control Engineering FEE CVUT
    Karlovo namesti 13, 121 35, Prague 2
    university: http://control.fel.cvut.cz/
    personal:   http://cmp.felk.cvut.cz/~pisa
    projects:   https://www.openhub.net/accounts/ppisa
    CAN related:http://canbus.pages.fel.cvut.cz/
    RISC-V education: https://comparch.edu.cvut.cz/
    Open Technologies Research Education and Exchange Services
    https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home
author maintainer
#8 lore raw

Hello Pavel,

Quoted 22 lines · Pavel Pisa

Hello Oliver,

Quoted 6 lines · On Sunday 17 of July 2022 15:27:25 Oliver Hartkopp wrote:

V2: Major rework after discussion and feedback on Linux-CAN ML

- rework of struct canxl_frame
- CANXL_XLF flag is now the switch between CAN XL and CAN/CANFD
- variable length in r/w operations for CAN XL frames
- write CAN XL frame to raw socket enforces size <-> canxl_frame.len sync

I generally like the idea but I would like even to extend it to process all CAN messages types through same API.

(quoted diff) +8 -0
+struct canxl_frame {
+       canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
+       __u8    flags; /* additional flags for CAN XL */
+       __u8    sdt;   /* SDU (service data unit) type */
+       __u16   len;   /* frame payload length in byte */
+       __u32   af;    /* acceptance field */
+       __u8    data[CANXL_MAX_DLEN];
+};
Quoted 2 lines

I would suggest to think about possibility to have single structure type for processing of all CAN frames types in usersace.

Yes, I would love to do this too.

But the length information in the CAN/CANFD frame is a 8 bit (u8) element while we need 16 bit in CAN XL frames which can cover the 2048 bytes available in CAN XL.

So we can not place canxl_frame.len at the same place as can[fd]_frame.len.

And this leads to 'the trick' that canxl_frame.flags shares its position with can[fd]_frame.len.

With CANXL_XLF = 0x80 we switch between CAN XL and CAN/CANFD which would lead to can[fd]_frame.len = 128 which is way beyond a valid CAN/CANFD length value.

Quoted 7 lines

When you define field
prio as anonyous union of prio and can_id

   union {
     canid_t prio;
     canid_t can_id;
   }

The CAN XL specification has no can_id anymore - but canxl_frame.prio and can[fd]_frame.can_id share the same position, length and type (canid_t).

This allows us to re-use the CAN-ID filters in af_can.c for the canxl_frame.prio.

Quoted 2 lines

then it is possible to define flags such way, that canxl_frame is alternative for all other formats

Ah, now I got your point.

But e.g. you don't need 'sdt' and 'af' elements in CAN[FD] frames and you would need raw-DLC fields for Classical CAN.

This will break all in-kernel users (bcm, gw, isotp, j1939).

Quoted 8 lines

+#define CANXL_XLF 0x80 /* mandatory CAN XL frame flag (must always be set!) */ So CANXL_XLF will be changed to CANXLS_SELECT frame structure selected, then rest of bits can be used for CANXLS_XLF, CANXLS_FD, in ideal case even RTR, BRS etc.. or RTR can be left as part of ID if that is easier. This way only single structure can be used to receive and send all frames over single interface when XL option is selected
by software.

Quoted 5 lines

Yes, there would be redundancy in the kernel handling which has to accept two types of encoding of CAN FD and standard messages but actual complexity in usespace when you want to support all variants and for example forward frames between interfaces or process them inside QEMU etc. is really quite high.

IMHO it is very easy for CAN interfaces (as you can see in the vcan driver (patch 4/5).

CAN XL is quite different to CAN[FD]. I would name it "Ethernet with CAN arbitration" and for that reason I'm currently not thinking of adapting the CAN modification features in gw.c for CAN XL.

If you go through the patches 2/5 and 3/5 you can see the current infrastructure effort to handle CAN, CAN FD and CAN XL frames in parallel.

When working with a mix of CAN[|FD|XL] frames in user space I would simply read into a struct canxl_frame and cast it to CAN[FD] when CANXL_XLF is not set (and the sizeof(struct can[fd]_frame) and the len value fits.

Don't you think, that is easy enough?

Best regards,
Oliver

#9 lore raw
In reply to Pavel Pisa
Quoted 22 lines · Pavel Pisa

Hello Oliver,

Quoted 6 lines · On Sunday 17 of July 2022 15:27:25 Oliver Hartkopp wrote:

V2: Major rework after discussion and feedback on Linux-CAN ML

- rework of struct canxl_frame
- CANXL_XLF flag is now the switch between CAN XL and CAN/CANFD
- variable length in r/w operations for CAN XL frames
- write CAN XL frame to raw socket enforces size <-> canxl_frame.len sync

I generally like the idea but I would like even to extend it to process all CAN messages types through same API.

(quoted diff) +8 -0
+struct canxl_frame {
+       canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
+       __u8    flags; /* additional flags for CAN XL */
+       __u8    sdt;   /* SDU (service data unit) type */
+       __u16   len;   /* frame payload length in byte */
+       __u32   af;    /* acceptance field */
+       __u8    data[CANXL_MAX_DLEN];
+};
Quoted 2 lines

I would suggest to think about possibility to have single structure type for processing of all CAN frames types in usersace.

I thought about it.

The prio is 11 bits, the XLF and SEC flags are 2 bits together, the SDT is 8 bits, the length is 12 bits (1~2048) and finally the af is 32 bits. The total is thus 11+2+8+12+32 = 65 bits. Arg… one bit short to fit in the struct can(fd)_frame!
However, if we use the DLC instead, the length information can fit on 11 bits (0~2047). With this, we have just enough to squeeze in the CAN-XL flags.

And so, let me introduce you the Frankenstein monster version of the canxl_frame:

#include <asm/byteorder.h>

struct canxl_frame {
        union {
                canid_t can_id; /* CAN(-FD) ID */
                __u32 af; /* CAN-XL acceptance field */
        };
        union {
                struct {
                        __u8 len; /* CAN(-FD) frame length */
                        __u8 flags; /* additional flags for CAN FD */
                        __u8 __res0;  /* reserved / padding */
                        __u8 len8_dlc; /* optional Classic CAN DLC for
8 byte payload length (9 .. 15) */
                };
                struct {
#if defined(__LITTLE_ENDIAN_BITFIELD)
                        __u32 xlf:1; /* is CAN-XL frame? */
#endif
                        __u32 sec:1; /* Simple Extended Content
(security/segmentation) */
                        __u32 xl_dlc:11; /* CAN-XL data length code (0..2047) */
                        __u32 prio:11; /* 11 bit priority for arbitration */
                        __u32 sdt:8; /* SDU (service data unit) type */
#if defined(__BIG_ENDIAN_BITFIELD)
                        __u32 xlf:1; /* is CAN-XL frame? */
#endif
                };
        };
        __u8 data[] __attribute__((aligned(8)));
};

Pretty scary, isn’t it?

To be honest, I am sharing this for fun. No need to disprove this idea, I do *not* support it myself.

Yours sincerely,
Vincent Mailhol

author maintainer
#10 lore raw
Quoted 71 lines · Vincent Mailhol
Quoted 22 lines · Pavel Pisa

Hello Oliver,

Quoted 6 lines · On Sunday 17 of July 2022 15:27:25 Oliver Hartkopp wrote:

V2: Major rework after discussion and feedback on Linux-CAN ML

- rework of struct canxl_frame
- CANXL_XLF flag is now the switch between CAN XL and CAN/CANFD
- variable length in r/w operations for CAN XL frames
- write CAN XL frame to raw socket enforces size <-> canxl_frame.len sync

I generally like the idea but I would like even to extend it to process all CAN messages types through same API.

(quoted diff) +8 -0
+struct canxl_frame {
+       canid_t prio;  /* 11 bit priority for arbitration (canid_t) */
+       __u8    flags; /* additional flags for CAN XL */
+       __u8    sdt;   /* SDU (service data unit) type */
+       __u16   len;   /* frame payload length in byte */
+       __u32   af;    /* acceptance field */
+       __u8    data[CANXL_MAX_DLEN];
+};
Quoted 2 lines

I would suggest to think about possibility to have single structure type for processing of all CAN frames types in usersace.

I thought about it.

The prio is 11 bits, the XLF and SEC flags are 2 bits together, the SDT is 8 bits, the length is 12 bits (1~2048) and finally the af is 32 bits. The total is thus 11+2+8+12+32 = 65 bits. Arg… one bit short to fit in the struct can(fd)_frame!
However, if we use the DLC instead, the length information can fit on 11 bits (0~2047). With this, we have just enough to squeeze in the CAN-XL flags.

And so, let me introduce you the Frankenstein monster version of the canxl_frame:

#include <asm/byteorder.h>

struct canxl_frame {
         union {
                 canid_t can_id; /* CAN(-FD) ID */
                 __u32 af; /* CAN-XL acceptance field */
         };
         union {
                 struct {
                         __u8 len; /* CAN(-FD) frame length */
                         __u8 flags; /* additional flags for CAN FD */
                         __u8 __res0;  /* reserved / padding */
                         __u8 len8_dlc; /* optional Classic CAN DLC for
8 byte payload length (9 .. 15) */
                 };
                 struct {
#if defined(__LITTLE_ENDIAN_BITFIELD)
                         __u32 xlf:1; /* is CAN-XL frame? */
#endif
                         __u32 sec:1; /* Simple Extended Content
(security/segmentation) */
                         __u32 xl_dlc:11; /* CAN-XL data length code (0..2047) */
                         __u32 prio:11; /* 11 bit priority for arbitration */
                         __u32 sdt:8; /* SDU (service data unit) type */
#if defined(__BIG_ENDIAN_BITFIELD)
                         __u32 xlf:1; /* is CAN-XL frame? */
#endif
                 };
         };
         __u8 data[] __attribute__((aligned(8)));
};
Quoted 4 lines

Pretty scary, isn’t it?

To be honest, I am sharing this for fun. No need to disprove this idea, I do *not* support it myself.

But sometimes it is valuable to formulate an idea as code to see that this is definitely NOT what you want ;-)

Best regards,
Oliver