can: support CAN XL
Subsystems (3)
Oliver Hartkopp
maintainerv4 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
V4: Fix patch 5 (can: raw: add CAN XL support)
- fix return value (move 'err = -EINVAL' in raw_sendmsg())
- add CAN XL frame handling in can_rcv()
- change comment for CAN_RAW_XL_[RT]X_DYN definition (allow -> enable)
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 | 97 +++++++++++++++++++++++++++----- 9 files changed, 256 insertions(+), 35 deletions(-)
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerRFC PATCH v4 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
@@ -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
Oliver Hartkopp
maintainerRFC PATCH v4 2/5 can: canxl: introduce ETH_P_CANXL ethernet protocol handling
In reply to Oliver HartkoppEnable 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
@@ -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
@@ -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
@@ -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
Oliver Hartkopp
maintainerRFC PATCH v4 3/5 can: dev: add CAN XL support
In reply to Oliver HartkoppExtend 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
@@ -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
@@ -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
Oliver Hartkopp
maintainerRFC PATCH v4 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
@@ -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
Oliver Hartkopp
maintainerRFC PATCH v4 5/5 can: raw: add CAN XL support
In reply to Oliver HartkoppEnable 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 | 97 +++++++++++++++++++++++++++++++----- 2 files changed, 90 insertions(+), 13 deletions(-)
include/uapi/linux/can/raw.h +6 -0
@@ -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)/* enable truncated data[] for read() */ +#define CAN_RAW_XL_TX_DYN (1 << 2)/* enable truncated data[] for write() */ +#endif /* !_UAPI_CAN_RAW_H */
net/can/raw.c +84 -13
204 lines not on this page · raw message
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerRe: [RFC PATCH v4 5/5] can: raw: add CAN XL support
Moving the 'err = -EINVAL;' was correct but introduced a missing initialization for sock_alloc_send_skb() failure :-/
Will fix in V5.
Regards,
Oliver
net/can/raw.c +1 -1
@@ -797,11 +797,11 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)struct sockcm_cookie sockc;struct sk_buff *skb;struct net_device *dev;unsigned int skb_sz = size;int ifindex; -int err; +int err = -EINVAL ;/* valid CAN frame sizes */ if (size <CANXL_HDR_SZ +CANXL_MIN_DLEN || size >CANXL_MTU )return -EINVAL ;
Quoted 250 lines · Oliver Hartkopp
Enable
CAN_RAWsockets to read and write CAN XL frames analogue to the CAN FD extension (newCAN_RAW_XL_FRAMESsockopt).A CAN XL network interface is capable to handle Classical CAN, CAN FD and CAN XL frames. When
CAN_RAW_XL_FRAMESis enabled, theCAN_RAWsocket checks whether the addressed CAN network interface is capable to handle the provided CAN frame.Signed-off-by Oliver Hartkopp <[email protected]>--- include/uapi/linux/can/raw.h | 6 +++ net/can/raw.c | 97 +++++++++++++++++++++++++++++++----- 2 files changed, 90 insertions(+), 13 deletions(-)include/uapi/linux/can/raw.h +6
-0@@ -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)/* enable truncated data[] for read() */ +#define CAN_RAW_XL_TX_DYN (1 << 2)/* enable truncated data[] for write() */ +#endif /* !_UAPI_CAN_RAW_H */ net/can/raw.c +84
-13@@ -85,10 +85,11 @@ struct raw_sock {int ifindex;struct list_head notifier;int loopback;int recv_own_msgs;int fd_frames; +int xl_frames;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;@@ -127,12 +128,13 @@ 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 ; -/* do not pass non-CAN2.0 frames to a legacy socket */ -if (!ro->fd_frames && oskb->len !=CAN_MTU ) +/* make sure to not pass oversized frames to the socket */ +if ((oskb->len >CAN_MTU && !ro->fd_frames && !ro->xl_frames) || + (oskb->len ==CANXL_MTU && !ro->xl_frames))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) {@@ -344,10 +346,11 @@ static int raw_init(struct sock *sk)/* set default loopback behaviour */ ro->loopback =1 ; ro->recv_own_msgs =0 ; ro->fd_frames =0 ; + ro->xl_frames =0 ; ro->join_filters =0 ;/* alloc_percpu provides zero'ed memory */ ro->uniq = alloc_percpu(struct uniqframe);if (unlikely (!ro->uniq))@@ -667,10 +670,23 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,if (copy_from_sockptr (&ro->fd_frames, optval, optlen))return -EFAULT ;break ; +case CAN_RAW_XL_FRAMES : +if (optlen !=sizeof (ro->xl_frames)) +return -EINVAL ; + +if (copy_from_sockptr (&ro->xl_frames, optval, optlen)) +return -EFAULT ; + +/* sanitize flags */ +if (ro->xl_frames & (CAN_RAW_XL_RX_DYN |CAN_RAW_XL_TX_DYN )) + ro->xl_frames |=CAN_RAW_XL_ENABLE ; + +break ; +case CAN_RAW_JOIN_FILTERS :if (optlen !=sizeof (ro->join_filters))return -EINVAL ;if (copy_from_sockptr (&ro->join_filters, optval, optlen))@@ -749,10 +765,16 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,if (len >sizeof (int )) len =sizeof (int ); val = &ro->fd_frames;break ; +case CAN_RAW_XL_FRAMES : +if (len >sizeof (int )) + len =sizeof (int ); + val = &ro->xl_frames; +break ; +case CAN_RAW_JOIN_FILTERS :if (len >sizeof (int )) len =sizeof (int ); val = &ro->join_filters;break ;@@ -773,13 +795,18 @@ 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 skb_sz = size;int ifindex;int err; +/* valid CAN frame sizes */ +if (size <CANXL_HDR_SZ +CANXL_MIN_DLEN || size >CANXL_MTU ) +return -EINVAL ; +if (msg->msg_name) { DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);if (msg->msg_namelen <RAW_MIN_NAMELEN )return -EINVAL ;@@ -794,20 +821,15 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) dev =dev_get_by_index (sock_net (sk), ifindex);if (!dev)return -ENXIO ; - err = -EINVAL ; -if (ro->fd_frames && dev->mtu ==CANFD_MTU ) { -if (unlikely (size !=CANFD_MTU && size !=CAN_MTU )) - goto put_dev; - }else { -if (unlikely (size !=CAN_MTU )) - goto put_dev; - } +/* alloc full CAN XL space */ +if (ro->xl_frames && dev->mtu ==CANXL_MTU ) + skb_sz =CANXL_MTU ; - skb =sock_alloc_send_skb (sk, size +sizeof (struct can_skb_priv ), + skb =sock_alloc_send_skb (sk, skb_sz +sizeof (struct can_skb_priv ), msg->msg_flags &MSG_DONTWAIT , &err);if (!skb) goto put_dev;can_skb_reserve (skb);@@ -816,10 +838,49 @@ 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 (ro->xl_frames && dev->mtu ==CANXL_MTU ) { +struct canxl_frame *cfx = (struct canxl_frame *)skb->data; + +if (cfx->flags &CANXL_XLF ) { +/* check for valid CAN XL frame */ +if (cfx->len <CANXL_MIN_DLEN || + cfx->len >CANXL_MAX_DLEN ) + goto free_skb; + +if (ro->xl_frames &CAN_RAW_XL_TX_DYN ) { +/* check truncated CAN XL frame structure */ +if (size != cfx->len +CANXL_HDR_SZ ) + goto free_skb; + +/* push skb->len to CANXL_MTU */ +if (size <CANXL_MTU ) +skb_put_zero (skb,CANXL_MTU - size); + }else { +/* check complete CAN XL frame structure */ +if (size !=CANXL_MTU ) + goto free_skb; + } + }else { +/* check for CAN FD and Classical CAN */ +if (unlikely (size !=CANFD_MTU && size !=CAN_MTU )) + goto free_skb; + } + }else if (ro->fd_frames && dev->mtu ==CANFD_MTU ) { +/* CAN FD and Classical CAN */ +if (unlikely (size !=CANFD_MTU && size !=CAN_MTU )) + goto free_skb; + }else { +/* Classical CAN */ +if (unlikely (size !=CAN_MTU )) + goto free_skb; + } +sockcm_init (&sockc, sk);if (msg->msg_controllen) { err =sock_cmsg_send (sk, msg, &sockc);if (unlikely (err)) goto free_skb;@@ -850,25 +911,35 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)static int raw_recvmsg (struct socket *sock,struct msghdr *msg,size_t size,int flags) {struct sock *sk = sock->sk; +struct raw_sock *ro =raw_sk (sk);struct sk_buff *skb; +struct canxl_frame *cfx; +unsigned int rx_len;int err =0 ;if (flags &MSG_ERRQUEUE )return sock_recv_errqueue (sk, msg, size,SOL_CAN_RAW ,SCM_CAN_RAW_ERRQUEUE ); skb =skb_recv_datagram (sk, flags, &err);if (!skb)return err; -if (size < skb->len) + rx_len = skb->len; +if (rx_len ==CANXL_MTU && ro->xl_frames &CAN_RAW_XL_RX_DYN ) { + cfx = (struct canxl_frame *)skb->data; +if (cfx->len >=CANXL_MIN_DLEN && cfx->len <CANXL_MAX_DLEN ) + rx_len =CANXL_HDR_SZ + cfx->len; + } + +if (size < rx_len) msg->msg_flags |=MSG_TRUNC ;else - size = skb->len; + size = rx_len; err =memcpy_to_msg (msg, skb->data, size);if (err <0 ) {skb_free_datagram (sk, skb);return err;
Re: [RFC PATCH v4 5/5] can: raw: add CAN XL support
In reply to Oliver HartkoppQuoted 21 lines · Oliver Hartkopp
Enable
CAN_RAWsockets to read and write CAN XL frames analogue to the CAN FD extension (newCAN_RAW_XL_FRAMESsockopt).A CAN XL network interface is capable to handle Classical CAN, CAN FD and CAN XL frames. When
CAN_RAW_XL_FRAMESis enabled, theCAN_RAWsocket checks whether the addressed CAN network interface is capable to handle the provided CAN frame.Signed-off-by Oliver Hartkopp <[email protected]>--- include/uapi/linux/can/raw.h | 6 +++ net/can/raw.c | 97 +++++++++++++++++++++++++++++++----- 2 files changed, 90 insertions(+), 13 deletions(-)include/uapi/linux/can/raw.h +0
-0@@ -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
-0CAN_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)/* enable truncated data[] for read() */ +#define CAN_RAW_XL_TX_DYN (1 << 2)/* enable truncated data[] for write() */
This change leaves me perplexed. If I understand correctly, the only purpose is to provide those two APIs is to have:
* one for the beginner users: fixed packet size in order to keep the
approach of CAN(-FD).
* one the advanced users: variable packet size.
I don’t think that there are any precedent cases where the kernel exposed two differents APIs for the sake of pleasing both beginner and advanced users. Sending packets of variable sizes between the kernel and the userland is a solved problem and I feel uncomfortable introducing custom solutions in order to prevent hypothetical misuses.
I am not even convinced that this solution is safer than the V2. If you introduce different options, there will always be someone who will mix up things by copy pasting some random code snippets and get an unsafe result. IMHO, it is better to build a simple interface and give a reference implementation rather than trying to add complexity to anticipate misuses.
Yours sincerely,
Vincent Mailhol
Oliver Hartkopp
maintainerRe: [RFC PATCH v4 5/5] can: raw: add CAN XL support
Quoted 28 lines · Vincent Mailhol
Quoted 27 lines · Oliver Hartkopp
Enable
CAN_RAWsockets to read and write CAN XL frames analogue to the CAN FD extension (newCAN_RAW_XL_FRAMESsockopt).A CAN XL network interface is capable to handle Classical CAN, CAN FD and CAN XL frames. When
CAN_RAW_XL_FRAMESis enabled, theCAN_RAWsocket checks whether the addressed CAN network interface is capable to handle the provided CAN frame.Signed-off-by Oliver Hartkopp <[email protected]>--- include/uapi/linux/can/raw.h | 6 +++ net/can/raw.c | 97 +++++++++++++++++++++++++++++++----- 2 files changed, 90 insertions(+), 13 deletions(-)include/uapi/linux/can/raw.h +1
-0@@ -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) */ };
Quoted 10 lines
include/uapi/linux/can/raw.h enum { · line 71
Quoted 4 lines
include/uapi/linux/can/raw.h +4
-0+/* 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)/* enable truncated data[] for read() */ +#define CAN_RAW_XL_TX_DYN (1 << 2)/* enable truncated data[] for write() */ This change leaves me perplexed. If I understand correctly, the only purpose is to provide those two APIs is to have:
* one for the beginner users: fixed packet size in order to keep the
approach of CAN(-FD).
* one the advanced users: variable packet size.
I did not think about beginners or advances users. To me it was like:
When I know that I often get 'shorter' CAN XL frames, I can enable this option.
In fact the usage pattern with the truncated data is pretty handy:
https://github.com/hartkopp/can-tests/blob/canxl/raw/canxl-rx.c#L70
Quoted 6 lines
I don’t think that there are any precedent cases where the kernel exposed two differents APIs for the sake of pleasing both beginner and advanced users. Sending packets of variable sizes between the kernel and the userland is a solved problem and I feel uncomfortable introducing custom solutions in order to prevent hypothetical misuses.
Quoted 6 lines
I am not even convinced that this solution is safer than the V2. If you introduce different options, there will always be someone who will mix up things by copy pasting some random code snippets and get an unsafe result. IMHO, it is better to build a simple interface and give a reference implementation rather than trying to add complexity to anticipate misuses.
Yes. Then I would vote for selecting the 'truncated' option only - and therefore remove CAN_RAW_XL_[R|T]X_DYN flags that would be 'always on'.
Best regards,
Oliver