* [PATCH rework v1] canxl: add virtual CAN network identifier support
@ 2024-02-12 15:14 Oliver Hartkopp
2024-02-12 15:22 ` Marc Kleine-Budde
2024-02-12 15:26 ` Oliver Hartkopp
0 siblings, 2 replies; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-12 15:14 UTC (permalink / raw)
To: linux-can; +Cc: Guy Harris, Marc Kleine-Budde, Vincent Mailhol, Oliver Hartkopp
CAN XL data frames contain an 8-bit virtual CAN network identifier (VCID).
A VCID value of zero represents an 'untagged' CAN XL frame.
To receive and send these optional VCIDs via CAN_RAW sockets a new socket
option CAN_RAW_XL_VCID_OPTS is introduced to define/access VCID content:
- tx: set the outgoing VCID value by the kernel (one fixed 8-bit value)
- tx: pass through VCID values from the user space (e.g. for traffic replay)
- rx: apply VCID receive filter (value/mask) to be passed to the user space
With the 'tx pass through' option CAN_RAW_XL_VCID_TX_PASS all valid VCID
values can be sent, e.g. to replay full qualified CAN XL traffic.
The VCID value provided for the CAN_RAW_XL_VCID_TX_SET option will
override the VCID value in the struct canxl_frame.prio defined for
CAN_RAW_XL_VCID_TX_PASS when both flags are set.
With a rx_vcid_mask of zero all possible VCID values (0x00 - 0xFF) are passed
to the user space when the CAN_RAW_XL_VCID_RX_FILTER flag is set. Without this
flag only untagged CAN XL frames (VCID = 0x00, CANXL_VCID flag unset) are
delivered to the user space.
The 8-bit VCID is stored inside the CAN XL prio element (only in CAN XL
frames!) to not interfere with other CAN content or the CAN filters
provided by the CAN_RAW sockets and kernel infrastruture.
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
---
include/uapi/linux/can.h | 9 +++-
include/uapi/linux/can/raw.h | 16 +++++++
net/can/af_can.c | 2 +
net/can/raw.c | 93 ++++++++++++++++++++++++++++++++----
4 files changed, 110 insertions(+), 10 deletions(-)
diff --git a/include/uapi/linux/can.h b/include/uapi/linux/can.h
index 939db2388208..e78cbd85ce7c 100644
--- a/include/uapi/linux/can.h
+++ b/include/uapi/linux/can.h
@@ -191,23 +191,28 @@ struct canfd_frame {
* 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) */
+/* the 8-bit VCID is optionally placed in the canxl_frame.prio element */
+#define CANXL_VCID_OFFSET 16 /* bit offset of VCID in prio element */
+#define CANXL_VCID_VAL_MASK 0xFFUL /* VCID is an 8-bit value */
+#define CANXL_VCID_MASK (CANXL_VCID_VAL_MASK << CANXL_VCID_OFFSET)
+
/**
* struct canxl_frame - CAN with e'X'tended frame 'L'ength frame structure
- * @prio: 11 bit arbitration priority with zero'ed CAN_*_FLAG flags
+ * @prio: 11 bit arbitration priority with zero'ed CAN_*_FLAG flags / VCID
* @flags: additional flags for CAN XL
* @sdt: SDU (service data unit) type
* @len: frame payload length in byte (CANXL_MIN_DLEN .. CANXL_MAX_DLEN)
* @af: acceptance field
* @data: CAN XL frame payload (CANXL_MIN_DLEN .. 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) */
+ canid_t prio; /* 11 bit priority for arbitration / 8 bit VCID */
__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];
diff --git a/include/uapi/linux/can/raw.h b/include/uapi/linux/can/raw.h
index 31622c9b7988..e024d896e278 100644
--- a/include/uapi/linux/can/raw.h
+++ b/include/uapi/linux/can/raw.h
@@ -63,8 +63,24 @@ enum {
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_RAW_XL_VCID_OPTS, /* CAN XL VCID configuration options */
};
+/* configuration for CAN XL virtual CAN identifier (VCID) handling */
+struct can_raw_vcid_options {
+
+ __u8 flags; /* flags for vcid (filter) behaviour */
+ __u8 tx_vcid; /* VCID value set into canxl_frame.prio */
+ __u8 rx_vcid; /* VCID value for VCID filter */
+ __u8 rx_vcid_mask; /* VCID mask for VCID filter */
+
+};
+
+/* can_raw_vcid_options.flags for CAN XL virtual CAN identifier handling */
+#define CAN_RAW_XL_VCID_TX_SET 0x01
+#define CAN_RAW_XL_VCID_TX_PASS 0x02
+#define CAN_RAW_XL_VCID_RX_FILTER 0x04
+
#endif /* !_UAPI_CAN_RAW_H */
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 7343fd487dbe..707576eeeb58 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -863,10 +863,12 @@ static __init int can_init(void)
int err;
/* check for correct padding to be able to use the structs similarly */
BUILD_BUG_ON(offsetof(struct can_frame, len) !=
offsetof(struct canfd_frame, len) ||
+ offsetof(struct can_frame, len) !=
+ offsetof(struct canxl_frame, flags) ||
offsetof(struct can_frame, data) !=
offsetof(struct canfd_frame, data));
pr_info("can: controller area network core\n");
diff --git a/net/can/raw.c b/net/can/raw.c
index e6b822624ba2..cb8e6f788af8 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -89,10 +89,14 @@ struct raw_sock {
struct list_head notifier;
int loopback;
int recv_own_msgs;
int fd_frames;
int xl_frames;
+ struct can_raw_vcid_options raw_vcid_opts;
+ canid_t tx_vcid_shifted;
+ canid_t rx_vcid_shifted;
+ canid_t rx_vcid_mask_shifted;
int join_filters;
int count; /* number of active filters */
struct can_filter dfilter; /* default/single filter */
struct can_filter *filter; /* pointer to filter(s) */
can_err_mask_t err_mask;
@@ -132,14 +136,33 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
/* check the received tx sock reference */
if (!ro->recv_own_msgs && oskb->sk == sk)
return;
/* make sure to not pass oversized frames to the socket */
- if ((!ro->fd_frames && can_is_canfd_skb(oskb)) ||
- (!ro->xl_frames && can_is_canxl_skb(oskb)))
+ if (!ro->fd_frames && can_is_canfd_skb(oskb))
return;
+ if (can_is_canxl_skb(oskb)) {
+ struct canxl_frame *cxl = (struct canxl_frame *)oskb->data;
+
+ /* make sure to not pass oversized frames to the socket */
+ if (!ro->xl_frames)
+ return;
+
+ /* filter CAN XL VCID content */
+ if (ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_RX_FILTER) {
+ /* apply VCID filter if user enabled the filter */
+ if ((cxl->prio & ro->rx_vcid_mask_shifted) !=
+ (ro->rx_vcid_shifted & ro->rx_vcid_mask_shifted))
+ return;
+ } else {
+ /* no filter => do not forward VCID tagged frames */
+ if (cxl->prio & CANXL_VCID_MASK)
+ return;
+ }
+ }
+
/* eliminate multiple filter matches for the same skb */
if (this_cpu_ptr(ro->uniq)->skb == oskb &&
this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
if (!ro->join_filters)
return;
@@ -696,10 +719,23 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
/* Enabling CAN XL includes CAN FD */
if (ro->xl_frames)
ro->fd_frames = ro->xl_frames;
break;
+ case CAN_RAW_XL_VCID_OPTS:
+ if (optlen != sizeof(ro->raw_vcid_opts))
+ return -EINVAL;
+
+ if (copy_from_sockptr(&ro->raw_vcid_opts, optval, optlen))
+ return -EFAULT;
+
+ /* prepare 32 bit values for handling in hot path */
+ ro->tx_vcid_shifted = ro->raw_vcid_opts.tx_vcid << CANXL_VCID_OFFSET;
+ ro->rx_vcid_shifted = ro->raw_vcid_opts.rx_vcid << CANXL_VCID_OFFSET;
+ ro->rx_vcid_mask_shifted = ro->raw_vcid_opts.rx_vcid_mask << CANXL_VCID_OFFSET;
+ break;
+
case CAN_RAW_JOIN_FILTERS:
if (optlen != sizeof(ro->join_filters))
return -EINVAL;
if (copy_from_sockptr(&ro->join_filters, optval, optlen))
@@ -784,10 +820,25 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
if (len > sizeof(int))
len = sizeof(int);
val = &ro->xl_frames;
break;
+ case CAN_RAW_XL_VCID_OPTS:
+ /* user space buffer to small for VCID opts? */
+ if (len < sizeof(ro->raw_vcid_opts)) {
+ /* return -ERANGE and needed space in optlen */
+ err = -ERANGE;
+ if (put_user(sizeof(ro->raw_vcid_opts), optlen))
+ err = -EFAULT;
+ } else {
+ if (len > sizeof(ro->raw_vcid_opts))
+ len = sizeof(ro->raw_vcid_opts);
+ if (copy_to_user(optval, &ro->raw_vcid_opts, len))
+ err = -EFAULT;
+ }
+ break;
+
case CAN_RAW_JOIN_FILTERS:
if (len > sizeof(int))
len = sizeof(int);
val = &ro->join_filters;
break;
@@ -801,36 +852,55 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
if (copy_to_user(optval, val, len))
return -EFAULT;
return 0;
}
-static bool raw_bad_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
+static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
+{
+ struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
+
+ /* sanitize non CAN XL bits */
+ cxl->prio &= (CANXL_PRIO_MASK | CANXL_VCID_MASK);
+
+ /* clear VCID in CAN XL frame if pass through is disabled */
+ if (!(ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_TX_PASS))
+ cxl->prio &= CANXL_PRIO_MASK;
+
+ /* set VCID in CAN XL frame if enabled */
+ if (ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_TX_SET) {
+ cxl->prio &= CANXL_PRIO_MASK;
+ cxl->prio |= ro->tx_vcid_shifted;
+ }
+}
+
+static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
{
/* Classical CAN -> no checks for flags and device capabilities */
if (can_is_can_skb(skb))
- return false;
+ return CAN_MTU;
/* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
if (ro->fd_frames && can_is_canfd_skb(skb) &&
(mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
- return false;
+ return CANFD_MTU;
/* CAN XL -> needs to be enabled and a CAN XL device */
if (ro->xl_frames && can_is_canxl_skb(skb) &&
can_is_canxl_dev_mtu(mtu))
- return false;
+ return CANXL_MTU;
- return true;
+ return 0;
}
static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
{
struct sock *sk = sock->sk;
struct raw_sock *ro = raw_sk(sk);
struct sockcm_cookie sockc;
struct sk_buff *skb;
struct net_device *dev;
+ unsigned int txmtu;
int ifindex;
int err = -EINVAL;
/* check for valid CAN frame sizes */
if (size < CANXL_HDR_SIZE + CANXL_MIN_DLEN || size > CANXL_MTU)
@@ -867,13 +937,20 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
err = memcpy_from_msg(skb_put(skb, size), msg, size);
if (err < 0)
goto free_skb;
err = -EINVAL;
- if (raw_bad_txframe(ro, skb, dev->mtu))
+
+ /* check for valid CAN (CC/FD/XL) frame content */
+ txmtu = raw_check_txframe(ro, skb, dev->mtu);
+ if (!txmtu)
goto free_skb;
+ /* only CANXL: clear/forward/set VCID value */
+ if (txmtu == CANXL_MTU)
+ raw_put_canxl_vcid(ro, skb);
+
sockcm_init(&sockc, sk);
if (msg->msg_controllen) {
err = sock_cmsg_send(sk, msg, &sockc);
if (unlikely(err))
goto free_skb;
--
2.39.2
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:14 [PATCH rework v1] canxl: add virtual CAN network identifier support Oliver Hartkopp
@ 2024-02-12 15:22 ` Marc Kleine-Budde
2024-02-12 15:29 ` Oliver Hartkopp
2024-02-12 15:26 ` Oliver Hartkopp
1 sibling, 1 reply; 10+ messages in thread
From: Marc Kleine-Budde @ 2024-02-12 15:22 UTC (permalink / raw)
To: Oliver Hartkopp; +Cc: linux-can, Guy Harris, Vincent Mailhol
[-- Attachment #1: Type: text/plain, Size: 1858 bytes --]
On 12.02.2024 16:14:04, Oliver Hartkopp wrote:
> CAN XL data frames contain an 8-bit virtual CAN network identifier (VCID).
> A VCID value of zero represents an 'untagged' CAN XL frame.
>
> To receive and send these optional VCIDs via CAN_RAW sockets a new socket
> option CAN_RAW_XL_VCID_OPTS is introduced to define/access VCID content:
>
> - tx: set the outgoing VCID value by the kernel (one fixed 8-bit value)
> - tx: pass through VCID values from the user space (e.g. for traffic replay)
> - rx: apply VCID receive filter (value/mask) to be passed to the user space
>
> With the 'tx pass through' option CAN_RAW_XL_VCID_TX_PASS all valid VCID
> values can be sent, e.g. to replay full qualified CAN XL traffic.
>
> The VCID value provided for the CAN_RAW_XL_VCID_TX_SET option will
> override the VCID value in the struct canxl_frame.prio defined for
> CAN_RAW_XL_VCID_TX_PASS when both flags are set.
>
> With a rx_vcid_mask of zero all possible VCID values (0x00 - 0xFF) are passed
> to the user space when the CAN_RAW_XL_VCID_RX_FILTER flag is set. Without this
> flag only untagged CAN XL frames (VCID = 0x00, CANXL_VCID flag unset) are
> delivered to the user space.
>
> The 8-bit VCID is stored inside the CAN XL prio element (only in CAN XL
> frames!) to not interfere with other CAN content or the CAN filters
> provided by the CAN_RAW sockets and kernel infrastruture.
>
> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
What does that mean for
https://lore.kernel.org/all/20240128183758.68401-1-socketcan@hartkopp.net/
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:14 [PATCH rework v1] canxl: add virtual CAN network identifier support Oliver Hartkopp
2024-02-12 15:22 ` Marc Kleine-Budde
@ 2024-02-12 15:26 ` Oliver Hartkopp
2024-02-12 15:53 ` Marc Kleine-Budde
2024-02-12 19:27 ` Guy Harris
1 sibling, 2 replies; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-12 15:26 UTC (permalink / raw)
To: Guy Harris, Marc Kleine-Budde, Vincent Mailhol; +Cc: linux-can
Hi all,
I'm currently in discussion with Guy Harris who's working on the
Linux-CAN support in Wireshark.
We current have two approaches how to integrate the VCID into the struct
canxl_frame:
1. Add the VCID at a specific 16 bit offset (above the 11 bit Prio ID)
2. Create a endian-dependent data structure with a separate uint8 VCID
1:
https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
2:
https://lore.kernel.org/linux-can/20240128183758.68401-1-socketcan@hartkopp.net/
The endian-dependent data structure looks like this:
struct canxl_frame {
#if defined(__LITTLE_ENDIAN)
__u16 prio; /* 11 bit priority for arbitration */
__u8 vcid; /* virtual CAN network identifier */
__u8 __res0; /* reserved / padding must be set to zero */
#elif defined(__BIG_ENDIAN)
__u8 __res0; /* reserved / padding must be set to zero */
__u8 vcid; /* virtual CAN network identifier */
__u16 prio; /* 11 bit priority for arbitration */
#else
#error "Unknown endianness"
#endif
__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];
};
@Guy: Besides the fact that suggestion 2 does not win a design prize I'm
not sure whether solution 1 or 2 are better to maintain over lifetime.
Feedback about the two suggestions is highly appreciated.
Many thanks,
Oliver
ps. @Vincent: The reworked patch below includes all the readability and
typo fixes as well as the code cleanup we have discussed until the V5
patch of solution 2.
On 12.02.24 16:14, Oliver Hartkopp wrote:
> CAN XL data frames contain an 8-bit virtual CAN network identifier (VCID).
> A VCID value of zero represents an 'untagged' CAN XL frame.
>
> To receive and send these optional VCIDs via CAN_RAW sockets a new socket
> option CAN_RAW_XL_VCID_OPTS is introduced to define/access VCID content:
>
> - tx: set the outgoing VCID value by the kernel (one fixed 8-bit value)
> - tx: pass through VCID values from the user space (e.g. for traffic replay)
> - rx: apply VCID receive filter (value/mask) to be passed to the user space
>
> With the 'tx pass through' option CAN_RAW_XL_VCID_TX_PASS all valid VCID
> values can be sent, e.g. to replay full qualified CAN XL traffic.
>
> The VCID value provided for the CAN_RAW_XL_VCID_TX_SET option will
> override the VCID value in the struct canxl_frame.prio defined for
> CAN_RAW_XL_VCID_TX_PASS when both flags are set.
>
> With a rx_vcid_mask of zero all possible VCID values (0x00 - 0xFF) are passed
> to the user space when the CAN_RAW_XL_VCID_RX_FILTER flag is set. Without this
> flag only untagged CAN XL frames (VCID = 0x00, CANXL_VCID flag unset) are
> delivered to the user space.
>
> The 8-bit VCID is stored inside the CAN XL prio element (only in CAN XL
> frames!) to not interfere with other CAN content or the CAN filters
> provided by the CAN_RAW sockets and kernel infrastruture.
>
> Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
> ---
> include/uapi/linux/can.h | 9 +++-
> include/uapi/linux/can/raw.h | 16 +++++++
> net/can/af_can.c | 2 +
> net/can/raw.c | 93 ++++++++++++++++++++++++++++++++----
> 4 files changed, 110 insertions(+), 10 deletions(-)
>
> diff --git a/include/uapi/linux/can.h b/include/uapi/linux/can.h
> index 939db2388208..e78cbd85ce7c 100644
> --- a/include/uapi/linux/can.h
> +++ b/include/uapi/linux/can.h
> @@ -191,23 +191,28 @@ struct canfd_frame {
> * 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) */
>
> +/* the 8-bit VCID is optionally placed in the canxl_frame.prio element */
> +#define CANXL_VCID_OFFSET 16 /* bit offset of VCID in prio element */
> +#define CANXL_VCID_VAL_MASK 0xFFUL /* VCID is an 8-bit value */
> +#define CANXL_VCID_MASK (CANXL_VCID_VAL_MASK << CANXL_VCID_OFFSET)
> +
> /**
> * struct canxl_frame - CAN with e'X'tended frame 'L'ength frame structure
> - * @prio: 11 bit arbitration priority with zero'ed CAN_*_FLAG flags
> + * @prio: 11 bit arbitration priority with zero'ed CAN_*_FLAG flags / VCID
> * @flags: additional flags for CAN XL
> * @sdt: SDU (service data unit) type
> * @len: frame payload length in byte (CANXL_MIN_DLEN .. CANXL_MAX_DLEN)
> * @af: acceptance field
> * @data: CAN XL frame payload (CANXL_MIN_DLEN .. 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) */
> + canid_t prio; /* 11 bit priority for arbitration / 8 bit VCID */
> __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];
> diff --git a/include/uapi/linux/can/raw.h b/include/uapi/linux/can/raw.h
> index 31622c9b7988..e024d896e278 100644
> --- a/include/uapi/linux/can/raw.h
> +++ b/include/uapi/linux/can/raw.h
> @@ -63,8 +63,24 @@ enum {
> 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_RAW_XL_VCID_OPTS, /* CAN XL VCID configuration options */
> };
>
> +/* configuration for CAN XL virtual CAN identifier (VCID) handling */
> +struct can_raw_vcid_options {
> +
> + __u8 flags; /* flags for vcid (filter) behaviour */
> + __u8 tx_vcid; /* VCID value set into canxl_frame.prio */
> + __u8 rx_vcid; /* VCID value for VCID filter */
> + __u8 rx_vcid_mask; /* VCID mask for VCID filter */
> +
> +};
> +
> +/* can_raw_vcid_options.flags for CAN XL virtual CAN identifier handling */
> +#define CAN_RAW_XL_VCID_TX_SET 0x01
> +#define CAN_RAW_XL_VCID_TX_PASS 0x02
> +#define CAN_RAW_XL_VCID_RX_FILTER 0x04
> +
> #endif /* !_UAPI_CAN_RAW_H */
> diff --git a/net/can/af_can.c b/net/can/af_can.c
> index 7343fd487dbe..707576eeeb58 100644
> --- a/net/can/af_can.c
> +++ b/net/can/af_can.c
> @@ -863,10 +863,12 @@ static __init int can_init(void)
> int err;
>
> /* check for correct padding to be able to use the structs similarly */
> BUILD_BUG_ON(offsetof(struct can_frame, len) !=
> offsetof(struct canfd_frame, len) ||
> + offsetof(struct can_frame, len) !=
> + offsetof(struct canxl_frame, flags) ||
> offsetof(struct can_frame, data) !=
> offsetof(struct canfd_frame, data));
>
> pr_info("can: controller area network core\n");
>
> diff --git a/net/can/raw.c b/net/can/raw.c
> index e6b822624ba2..cb8e6f788af8 100644
> --- a/net/can/raw.c
> +++ b/net/can/raw.c
> @@ -89,10 +89,14 @@ struct raw_sock {
> struct list_head notifier;
> int loopback;
> int recv_own_msgs;
> int fd_frames;
> int xl_frames;
> + struct can_raw_vcid_options raw_vcid_opts;
> + canid_t tx_vcid_shifted;
> + canid_t rx_vcid_shifted;
> + canid_t rx_vcid_mask_shifted;
> int join_filters;
> int count; /* number of active filters */
> struct can_filter dfilter; /* default/single filter */
> struct can_filter *filter; /* pointer to filter(s) */
> can_err_mask_t err_mask;
> @@ -132,14 +136,33 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
> /* check the received tx sock reference */
> if (!ro->recv_own_msgs && oskb->sk == sk)
> return;
>
> /* make sure to not pass oversized frames to the socket */
> - if ((!ro->fd_frames && can_is_canfd_skb(oskb)) ||
> - (!ro->xl_frames && can_is_canxl_skb(oskb)))
> + if (!ro->fd_frames && can_is_canfd_skb(oskb))
> return;
>
> + if (can_is_canxl_skb(oskb)) {
> + struct canxl_frame *cxl = (struct canxl_frame *)oskb->data;
> +
> + /* make sure to not pass oversized frames to the socket */
> + if (!ro->xl_frames)
> + return;
> +
> + /* filter CAN XL VCID content */
> + if (ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_RX_FILTER) {
> + /* apply VCID filter if user enabled the filter */
> + if ((cxl->prio & ro->rx_vcid_mask_shifted) !=
> + (ro->rx_vcid_shifted & ro->rx_vcid_mask_shifted))
> + return;
> + } else {
> + /* no filter => do not forward VCID tagged frames */
> + if (cxl->prio & CANXL_VCID_MASK)
> + return;
> + }
> + }
> +
> /* eliminate multiple filter matches for the same skb */
> if (this_cpu_ptr(ro->uniq)->skb == oskb &&
> this_cpu_ptr(ro->uniq)->skbcnt == can_skb_prv(oskb)->skbcnt) {
> if (!ro->join_filters)
> return;
> @@ -696,10 +719,23 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
> /* Enabling CAN XL includes CAN FD */
> if (ro->xl_frames)
> ro->fd_frames = ro->xl_frames;
> break;
>
> + case CAN_RAW_XL_VCID_OPTS:
> + if (optlen != sizeof(ro->raw_vcid_opts))
> + return -EINVAL;
> +
> + if (copy_from_sockptr(&ro->raw_vcid_opts, optval, optlen))
> + return -EFAULT;
> +
> + /* prepare 32 bit values for handling in hot path */
> + ro->tx_vcid_shifted = ro->raw_vcid_opts.tx_vcid << CANXL_VCID_OFFSET;
> + ro->rx_vcid_shifted = ro->raw_vcid_opts.rx_vcid << CANXL_VCID_OFFSET;
> + ro->rx_vcid_mask_shifted = ro->raw_vcid_opts.rx_vcid_mask << CANXL_VCID_OFFSET;
> + break;
> +
> case CAN_RAW_JOIN_FILTERS:
> if (optlen != sizeof(ro->join_filters))
> return -EINVAL;
>
> if (copy_from_sockptr(&ro->join_filters, optval, optlen))
> @@ -784,10 +820,25 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> if (len > sizeof(int))
> len = sizeof(int);
> val = &ro->xl_frames;
> break;
>
> + case CAN_RAW_XL_VCID_OPTS:
> + /* user space buffer to small for VCID opts? */
> + if (len < sizeof(ro->raw_vcid_opts)) {
> + /* return -ERANGE and needed space in optlen */
> + err = -ERANGE;
> + if (put_user(sizeof(ro->raw_vcid_opts), optlen))
> + err = -EFAULT;
> + } else {
> + if (len > sizeof(ro->raw_vcid_opts))
> + len = sizeof(ro->raw_vcid_opts);
> + if (copy_to_user(optval, &ro->raw_vcid_opts, len))
> + err = -EFAULT;
> + }
> + break;
> +
> case CAN_RAW_JOIN_FILTERS:
> if (len > sizeof(int))
> len = sizeof(int);
> val = &ro->join_filters;
> break;
> @@ -801,36 +852,55 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
> if (copy_to_user(optval, val, len))
> return -EFAULT;
> return 0;
> }
>
> -static bool raw_bad_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
> +static void raw_put_canxl_vcid(struct raw_sock *ro, struct sk_buff *skb)
> +{
> + struct canxl_frame *cxl = (struct canxl_frame *)skb->data;
> +
> + /* sanitize non CAN XL bits */
> + cxl->prio &= (CANXL_PRIO_MASK | CANXL_VCID_MASK);
> +
> + /* clear VCID in CAN XL frame if pass through is disabled */
> + if (!(ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_TX_PASS))
> + cxl->prio &= CANXL_PRIO_MASK;
> +
> + /* set VCID in CAN XL frame if enabled */
> + if (ro->raw_vcid_opts.flags & CAN_RAW_XL_VCID_TX_SET) {
> + cxl->prio &= CANXL_PRIO_MASK;
> + cxl->prio |= ro->tx_vcid_shifted;
> + }
> +}
> +
> +static unsigned int raw_check_txframe(struct raw_sock *ro, struct sk_buff *skb, int mtu)
> {
> /* Classical CAN -> no checks for flags and device capabilities */
> if (can_is_can_skb(skb))
> - return false;
> + return CAN_MTU;
>
> /* CAN FD -> needs to be enabled and a CAN FD or CAN XL device */
> if (ro->fd_frames && can_is_canfd_skb(skb) &&
> (mtu == CANFD_MTU || can_is_canxl_dev_mtu(mtu)))
> - return false;
> + return CANFD_MTU;
>
> /* CAN XL -> needs to be enabled and a CAN XL device */
> if (ro->xl_frames && can_is_canxl_skb(skb) &&
> can_is_canxl_dev_mtu(mtu))
> - return false;
> + return CANXL_MTU;
>
> - return true;
> + return 0;
> }
>
> static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
> {
> struct sock *sk = sock->sk;
> struct raw_sock *ro = raw_sk(sk);
> struct sockcm_cookie sockc;
> struct sk_buff *skb;
> struct net_device *dev;
> + unsigned int txmtu;
> int ifindex;
> int err = -EINVAL;
>
> /* check for valid CAN frame sizes */
> if (size < CANXL_HDR_SIZE + CANXL_MIN_DLEN || size > CANXL_MTU)
> @@ -867,13 +937,20 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
> err = memcpy_from_msg(skb_put(skb, size), msg, size);
> if (err < 0)
> goto free_skb;
>
> err = -EINVAL;
> - if (raw_bad_txframe(ro, skb, dev->mtu))
> +
> + /* check for valid CAN (CC/FD/XL) frame content */
> + txmtu = raw_check_txframe(ro, skb, dev->mtu);
> + if (!txmtu)
> goto free_skb;
>
> + /* only CANXL: clear/forward/set VCID value */
> + if (txmtu == CANXL_MTU)
> + raw_put_canxl_vcid(ro, skb);
> +
> sockcm_init(&sockc, sk);
> if (msg->msg_controllen) {
> err = sock_cmsg_send(sk, msg, &sockc);
> if (unlikely(err))
> goto free_skb;
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:22 ` Marc Kleine-Budde
@ 2024-02-12 15:29 ` Oliver Hartkopp
2024-02-12 15:51 ` Marc Kleine-Budde
0 siblings, 1 reply; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-12 15:29 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: linux-can, Guy Harris, Vincent Mailhol
> What does that mean for
> https://lore.kernel.org/all/20240128183758.68401-1-socketcan@hartkopp.net/
I've send another mail on this question. I just had to wait for the
patch URL to show up on the ML archive. Did not expect such fast reply ;-)
Thanks,
Oliver
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:29 ` Oliver Hartkopp
@ 2024-02-12 15:51 ` Marc Kleine-Budde
0 siblings, 0 replies; 10+ messages in thread
From: Marc Kleine-Budde @ 2024-02-12 15:51 UTC (permalink / raw)
To: Oliver Hartkopp; +Cc: linux-can, Guy Harris, Vincent Mailhol
[-- Attachment #1: Type: text/plain, Size: 671 bytes --]
On 12.02.2024 16:29:28, Oliver Hartkopp wrote:
>
> > What does that mean for
> > https://lore.kernel.org/all/20240128183758.68401-1-socketcan@hartkopp.net/
>
> I've send another mail on this question. I just had to wait for the patch
> URL to show up on the ML archive. Did not expect such fast reply ;-)
The URL is predicable, it's:
https://lore.kernel.org/all/ + Message-Id
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:26 ` Oliver Hartkopp
@ 2024-02-12 15:53 ` Marc Kleine-Budde
2024-02-12 16:10 ` Oliver Hartkopp
2024-02-12 19:27 ` Guy Harris
1 sibling, 1 reply; 10+ messages in thread
From: Marc Kleine-Budde @ 2024-02-12 15:53 UTC (permalink / raw)
To: Oliver Hartkopp; +Cc: Guy Harris, Vincent Mailhol, linux-can
[-- Attachment #1: Type: text/plain, Size: 2014 bytes --]
On 12.02.2024 16:26:34, Oliver Hartkopp wrote:
> I'm currently in discussion with Guy Harris who's working on the Linux-CAN
> support in Wireshark.
>
> We current have two approaches how to integrate the VCID into the struct
> canxl_frame:
>
> 1. Add the VCID at a specific 16 bit offset (above the 11 bit Prio ID)
> 2. Create a endian-dependent data structure with a separate uint8 VCID
>
> 1: https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
> 2: https://lore.kernel.org/linux-can/20240128183758.68401-1-socketcan@hartkopp.net/
>
> The endian-dependent data structure looks like this:
>
> struct canxl_frame {
> #if defined(__LITTLE_ENDIAN)
> __u16 prio; /* 11 bit priority for arbitration */
> __u8 vcid; /* virtual CAN network identifier */
> __u8 __res0; /* reserved / padding must be set to zero */
> #elif defined(__BIG_ENDIAN)
> __u8 __res0; /* reserved / padding must be set to zero */
> __u8 vcid; /* virtual CAN network identifier */
> __u16 prio; /* 11 bit priority for arbitration */
> #else
> #error "Unknown endianness"
> #endif
> __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];
> };
>
> @Guy: Besides the fact that suggestion 2 does not win a design prize I'm not
> sure whether solution 1 or 2 are better to maintain over lifetime.
>
> Feedback about the two suggestions is highly appreciated.
Then I'll skip the virtual CAN network identifier support patches for
now.
Marc
--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:53 ` Marc Kleine-Budde
@ 2024-02-12 16:10 ` Oliver Hartkopp
2024-02-13 9:45 ` Oliver Hartkopp
0 siblings, 1 reply; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-12 16:10 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: Guy Harris, Vincent Mailhol, linux-can
On 2024-02-12 16:53, Marc Kleine-Budde wrote:
> On 12.02.2024 16:26:34, Oliver Hartkopp wrote:
>> I'm currently in discussion with Guy Harris who's working on the Linux-CAN
>> support in Wireshark.
>>
>> We current have two approaches how to integrate the VCID into the struct
>> canxl_frame:
>>
>> 1. Add the VCID at a specific 16 bit offset (above the 11 bit Prio ID)
>> 2. Create a endian-dependent data structure with a separate uint8 VCID
>>
>> 1: https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
>> 2: https://lore.kernel.org/linux-can/20240128183758.68401-1-socketcan@hartkopp.net/
>>
>> The endian-dependent data structure looks like this:
>>
>> struct canxl_frame {
>> #if defined(__LITTLE_ENDIAN)
>> __u16 prio; /* 11 bit priority for arbitration */
>> __u8 vcid; /* virtual CAN network identifier */
>> __u8 __res0; /* reserved / padding must be set to zero */
>> #elif defined(__BIG_ENDIAN)
>> __u8 __res0; /* reserved / padding must be set to zero */
>> __u8 vcid; /* virtual CAN network identifier */
>> __u16 prio; /* 11 bit priority for arbitration */
>> #else
>> #error "Unknown endianness"
>> #endif
>> __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];
>> };
>>
>> @Guy: Besides the fact that suggestion 2 does not win a design prize I'm not
>> sure whether solution 1 or 2 are better to maintain over lifetime.
>>
>> Feedback about the two suggestions is highly appreciated.
>
> Then I'll skip the virtual CAN network identifier support patches for
> now.
I assume the discussion to be resolved within a day or two.
Do you have a favorite patch?
Best regards,
Oliver
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 15:26 ` Oliver Hartkopp
2024-02-12 15:53 ` Marc Kleine-Budde
@ 2024-02-12 19:27 ` Guy Harris
2024-02-12 21:06 ` Oliver Hartkopp
1 sibling, 1 reply; 10+ messages in thread
From: Guy Harris @ 2024-02-12 19:27 UTC (permalink / raw)
To: Oliver Hartkopp; +Cc: Marc Kleine-Budde, Vincent Mailhol, linux-can
On Feb 12, 2024, at 7:26 AM, Oliver Hartkopp <socketcan@hartkopp.net> wrote:
> I'm currently in discussion with Guy Harris who's working on the Linux-CAN support in Wireshark.
*AND* in libpcap. Note that anything done with the metadata headers of SocketCAN packets affects both of them - and any programs *other* than Wireshark that capture on SocketCAN adapters or read pcap or pcapng files with SocketCAN packets.
> We current have two approaches how to integrate the VCID into the struct canxl_frame:
>
> 1. Add the VCID at a specific 16 bit offset (above the 11 bit Prio ID)
> 2. Create a endian-dependent data structure with a separate uint8 VCID
>
> 1: https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
> 2: https://lore.kernel.org/linux-can/20240128183758.68401-1-socketcan@hartkopp.net/
>
> The endian-dependent data structure looks like this:
>
> struct canxl_frame {
> #if defined(__LITTLE_ENDIAN)
> __u16 prio; /* 11 bit priority for arbitration */
> __u8 vcid; /* virtual CAN network identifier */
> __u8 __res0; /* reserved / padding must be set to zero */
> #elif defined(__BIG_ENDIAN)
> __u8 __res0; /* reserved / padding must be set to zero */
> __u8 vcid; /* virtual CAN network identifier */
> __u16 prio; /* 11 bit priority for arbitration */
> #else
> #error "Unknown endianness"
> #endif
> __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];
> };
>
> @Guy: Besides the fact that suggestion 2 does not win a design prize I'm not sure whether solution 1 or 2 are better to maintain over lifetime.
The first question to ask is "which solution breaks less existing source and binary code"?
Currently, the SocketCAN metadata header for CAN XL frames begins with a 4-byte integral-valued field. I'm not seeing code in the tip of the main branch, in either the git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git repository or the git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git repository, that sets or gets the prio field of the canxl_frame structure, so I'm guessing that the prio field in a canal_frame structure would be in *host* byte order when received or sent.
Presumably any software that sees that structure would either
1) treat the prio field as containing the priority in the low-order 11 bits and zeroes in the upper 21 bits
or
2) tread the prio field as containing the priority in the low-order 11 bits and would extract the priority by ANDing the prio field with CANXL_PRIO_MASK.
At the binary level, code that does 1) would be broken by putting the VCID in that field; the only way not to break it would be to add an additional field for the VCID, but *that* would change the length of the metadata header, which would break, at the binary level, any code that thinks the header is 12 bytes long, so there's nothing you can to to add the VCID without breaking that software, so that code shouldn't be taken into account.
At the *source* level, code that does 1) would be broken by putting the VCID in the 8 bits above the lower 16 bits, and wouldn't be broken by changing the structure to have the priority in a 16-bit field, with additional 8-bit vcid and reserved fields.
Code that does 2) would not be broken by *either* solution, at the source or binary level.
Having that as a 32-bit field named "priority" gives the field a name that doesn't indicate that it also includes the VCID. Renaming of the field as "priority_vcid" would break source compatibility; adding
#define priority priority_vcid
would fix that, at the expense of possibly breaking or, at least, uglifying debugging of code that has a variable or function or structure name or... named "priority".
Having that as a 16-bit field named "priority", an 8-bit field named "vcid", and an 8-bit reserved field involves, for structure layout compatibility, two different sets of structure members, with byte-order tests at compile time, which is a bit ugly, but does make it clear that one field contains the priority (and 5 reserved bits) and another field contains the VCID.
*Both* solutions result in the metadata header's first 4 bytes being:
byte 0: lower 8 bits of the priority;
byte 1: 5 reserved bits and upper 3 bits of the priority;
byte 2: VCID
byte 3: 8 reserved bits
on little-endian machines and
byte 0: 8 reserved bits
byte 1: VCID
byte 3: 5 reserved bits and upper 3 bits of the priority;
byte 4: lower 8 bits of the priority
on big-endian machines, i.e. the layout of the data in memory is the same for *both* structures.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 19:27 ` Guy Harris
@ 2024-02-12 21:06 ` Oliver Hartkopp
0 siblings, 0 replies; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-12 21:06 UTC (permalink / raw)
To: Guy Harris; +Cc: Marc Kleine-Budde, Vincent Mailhol, linux-can
Hello Guy,
On 2024-02-12 20:27, Guy Harris wrote:
> On Feb 12, 2024, at 7:26 AM, Oliver Hartkopp <socketcan@hartkopp.net> wrote:
>
>> I'm currently in discussion with Guy Harris who's working on the Linux-CAN support in Wireshark.
>
> *AND* in libpcap. Note that anything done with the metadata headers of SocketCAN packets affects both of them - and any programs *other* than Wireshark that capture on SocketCAN adapters or read pcap or pcapng files with SocketCAN packets.
Right.
>> We current have two approaches how to integrate the VCID into the struct canxl_frame:
>>
>> 1. Add the VCID at a specific 16 bit offset (above the 11 bit Prio ID)
>> 2. Create a endian-dependent data structure with a separate uint8 VCID
>>
>> 1: https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
>> 2: https://lore.kernel.org/linux-can/20240128183758.68401-1-socketcan@hartkopp.net/
>>
>> The endian-dependent data structure looks like this:
>>
>> struct canxl_frame {
>> #if defined(__LITTLE_ENDIAN)
>> __u16 prio; /* 11 bit priority for arbitration */
>> __u8 vcid; /* virtual CAN network identifier */
>> __u8 __res0; /* reserved / padding must be set to zero */
>> #elif defined(__BIG_ENDIAN)
>> __u8 __res0; /* reserved / padding must be set to zero */
>> __u8 vcid; /* virtual CAN network identifier */
>> __u16 prio; /* 11 bit priority for arbitration */
>> #else
>> #error "Unknown endianness"
>> #endif
>> __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];
>> };
>>
>> @Guy: Besides the fact that suggestion 2 does not win a design prize I'm not sure whether solution 1 or 2 are better to maintain over lifetime.
>
> The first question to ask is "which solution breaks less existing source and binary code"?
Although the CAN XL support was introduced in Linux 6.1, there is no
hardware CAN XL driver available today and only a low 1-digit number of
developers working on PoC code for CAN CiA protocols on virtual CAN
interfaces.
We would not break any user space programs BUT the ABI and the socket
API should be maintained as we can not change the uapi defines down to
Linux 6.1.
> Currently, the SocketCAN metadata header for CAN XL frames begins with a 4-byte integral-valued field. I'm not seeing code in the tip of the main branch, in either the git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git repository or the git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git repository, that sets or gets the prio field of the canxl_frame structure, so I'm guessing that the prio field in a canal_frame structure would be in *host* byte order when received or sent.
>
> Presumably any software that sees that structure would either
>
> 1) treat the prio field as containing the priority in the low-order 11 bits and zeroes in the upper 21 bits
>
> or
>
> 2) tread the prio field as containing the priority in the low-order 11 bits and would extract the priority by ANDing the prio field with CANXL_PRIO_MASK.
>
> At the binary level, code that does 1) would be broken by putting the VCID in that field; the only way not to break it would be to add an additional field for the VCID, but *that* would change the length of the metadata header, which would break, at the binary level, any code that thinks the header is 12 bytes long, so there's nothing you can to to add the VCID without breaking that software, so that code shouldn't be taken into account.
>
> At the *source* level, code that does 1) would be broken by putting the VCID in the 8 bits above the lower 16 bits, and wouldn't be broken by changing the structure to have the priority in a 16-bit field, with additional 8-bit vcid and reserved fields.
>
> Code that does 2) would not be broken by *either* solution, at the source or binary level.
>
> Having that as a 32-bit field named "priority" gives the field a name that doesn't indicate that it also includes the VCID. Renaming of the field as "priority_vcid" would break source compatibility; adding
>
> #define priority priority_vcid
>
> would fix that, at the expense of possibly breaking or, at least, uglifying debugging of code that has a variable or function or structure name or... named "priority".
>
> Having that as a 16-bit field named "priority", an 8-bit field named "vcid", and an 8-bit reserved field involves, for structure layout compatibility, two different sets of structure members, with byte-order tests at compile time, which is a bit ugly, but does make it clear that one field contains the priority (and 5 reserved bits) and another field contains the VCID.
>
> *Both* solutions result in the metadata header's first 4 bytes being:
>
> byte 0: lower 8 bits of the priority;
> byte 1: 5 reserved bits and upper 3 bits of the priority;
> byte 2: VCID
> byte 3: 8 reserved bits
>
> on little-endian machines and
>
> byte 0: 8 reserved bits
> byte 1: VCID
> byte 3: 5 reserved bits and upper 3 bits of the priority;
> byte 4: lower 8 bits of the priority
>
> on big-endian machines, i.e. the layout of the data in memory is the same for *both* structures.
Many thanks for your feedback!!
I finally have a clear view on the requirements and would finally vote
for this patch that maintains the 32 bit prio element and "shift" the
vcid at bit position 16:
https://lore.kernel.org/linux-can/20240212151404.60828-1-socketcan@hartkopp.net/
And here are the reasons why:
1. It doesn't break existing code and ABI
- the applications can use the existing CAN XL frames
- there is no VCID content visible by default
- it is intended that applications do not necessarily know the VCID
2. So far there are no CAN XL hardware drivers which are the really
relevant users of the VCID information
3. The user explicitly has to switch on the CAN_RAW_XL_VCID_OPTS
sockopt() for CAN_RAW sockets to enable and handle the VCID content
By now the VCID place in the prio is always set to zero which means an
"untagged" CAN XL frame, which is fine.
So there is a new feature which will likely emerge in Linux 6.9 which is
not enabled by default and which does not break the existing ABI and code.
When a VCID capable application runs on an older kernel the
CAN_RAW_XL_VCID_OPTS sockopt() will fail and there will be no VCID
content. So it is backwards compatible.
And when the sockopt() succeeds, the (new) application knows how to
handle the prio content.
Best regards,
Oliver
ps. @Marc: I found a small issue in the commit message of the above
patch as I removed the obsolete CANXL_VCID from an early implementation.
So I'll send a "rework v2" patch.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH rework v1] canxl: add virtual CAN network identifier support
2024-02-12 16:10 ` Oliver Hartkopp
@ 2024-02-13 9:45 ` Oliver Hartkopp
0 siblings, 0 replies; 10+ messages in thread
From: Oliver Hartkopp @ 2024-02-13 9:45 UTC (permalink / raw)
To: Marc Kleine-Budde; +Cc: Guy Harris, Vincent Mailhol, linux-can
Hi Marc,
On 2024-02-12 17:10, Oliver Hartkopp wrote:
> On 2024-02-12 16:53, Marc Kleine-Budde wrote:
>>>
>>> Feedback about the two suggestions is highly appreciated.
>>
>> Then I'll skip the virtual CAN network identifier support patches for
>> now.
>
> I assume the discussion to be resolved within a day or two.
It is done.
Please pick this "rework v2" patch for can-next upstream:
https://lore.kernel.org/linux-can/20240212213550.18516-1-socketcan@hartkopp.net/
Many thanks,
Oliver
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2024-02-13 9:45 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-12 15:14 [PATCH rework v1] canxl: add virtual CAN network identifier support Oliver Hartkopp
2024-02-12 15:22 ` Marc Kleine-Budde
2024-02-12 15:29 ` Oliver Hartkopp
2024-02-12 15:51 ` Marc Kleine-Budde
2024-02-12 15:26 ` Oliver Hartkopp
2024-02-12 15:53 ` Marc Kleine-Budde
2024-02-12 16:10 ` Oliver Hartkopp
2024-02-13 9:45 ` Oliver Hartkopp
2024-02-12 19:27 ` Guy Harris
2024-02-12 21:06 ` Oliver Hartkopp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox