CAN FD support
Subsystems (3)
Oliver Hartkopp
maintainerRFC v4 1/1 CAN FD support
Hi all,
changes since v3:
can: bump version date (simultaneous support)
--> can_raw: allow simultaneous access of legacy apps and CANFD aware apps to CAN(FD) devices
af_can: remove pointless initializer in packet_type definition
can: add notifier handling when changing the CAN device MTU
I generated this diff by 'git diff fb7944b' on the repository
https://gitorious.org/~hartkopp/linux-can/hartkopps-linux-can-next
Regards,
Oliver
drivers/net/can/dev.c | 35 ++++++++++++++ drivers/net/can/vcan.c | 30 +++++++++--- include/linux/can.h | 51 ++++++++++++++++++++- include/linux/can/core.h | 4 - include/linux/can/dev.h | 33 ++++++++++---- include/linux/can/raw.h | 3 - include/linux/if_ether.h | 3 - net/can/af_can.c | 110 +++++++++++++++++++++++++++++++++++------------ net/can/bcm.c | 7 ++ net/can/gw.c | 2 net/can/raw.c | 63 +++++++++++++++++++++++++- 11 files changed, 287 insertions(+), 54 deletions(-)
---
drivers/net/can/dev.c +34 -1
@@ -33,6 +33,39 @@ MODULE_DESCRIPTION(MOD_DESC);MODULE_LICENSE ("GPL v2" );MODULE_AUTHOR ("Wolfgang Grandegger <[email protected]>" ); +/* CAN DLC to real data length conversion helpers */ + +static const u8 dlc2len[] = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 , +8 ,12 ,16 ,20 ,24 ,32 ,48 ,64 }; + +/* get data length from can_dlc with sanitized can_dlc */ +u8 can_dlc2len (u8 can_dlc) +{ +return dlc2len[can_dlc &0x0F ]; +} +EXPORT_SYMBOL_GPL (can_dlc2len); + +static const u8 len2dlc[] = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,/* 0 - 8 */ +9 ,9 ,9 ,9 ,/* 9 - 12 */ +10 ,10 ,10 ,10 ,/* 13 - 16 */ +11 ,11 ,11 ,11 ,/* 17 - 20 */ +12 ,12 ,12 ,12 ,/* 21 - 24 */ +13 ,13 ,13 ,13 ,13 ,13 ,13 ,13 ,/* 25 - 32 */ +14 ,14 ,14 ,14 ,14 ,14 ,14 ,14 ,/* 33 - 40 */ +14 ,14 ,14 ,14 ,14 ,14 ,14 ,14 ,/* 41 - 48 */ +15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 ,/* 49 - 56 */ +15 ,15 ,15 ,15 ,15 ,15 ,15 ,15 };/* 57 - 64 */ + +/* map the sanitized data length to an appropriate data length code */ +u8 can_len2dlc (u8 len) +{ +if (unlikely (len >64 )) +return 0xF ; + +return len2dlc[len]; +} +EXPORT_SYMBOL_GPL (can_len2dlc); +#ifdef CONFIG_CAN_CALC_BITTIMING #define CAN_CALC_MAX_ERROR 50/* in one-tenth of a percent */
@@ -454,7 +487,7 @@ EXPORT_SYMBOL_GPL(can_bus_off);static void can_setup (struct net_device *dev) { dev->type =ARPHRD_CAN ; - dev->mtu =sizeof (struct can_frame ); + dev->mtu =CAN_MTU ; dev->hard_header_len =0 ; dev->addr_len =0 ; dev->tx_queue_len =10 ;
drivers/net/can/vcan.c +23 -7
@@ -70,13 +70,12 @@ 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 can_frame *cf = (struct can_frame *)skb->data; +struct canfd_frame *cfd = (struct canfd_frame *)skb->data;struct net_device_stats *stats = &dev->stats; stats->rx_packets++; - stats->rx_bytes += cf->can_dlc; + stats->rx_bytes += cfd->len; - skb->protocol =htons (ETH_P_CAN ); skb->pkt_type =PACKET_BROADCAST ; skb->dev = dev; skb->ip_summed =CHECKSUM_UNNECESSARY ;
@@ -86,7 +85,7 @@ static void vcan_rx(struct sk_buff *skb, struct net_device *dev)static netdev_tx_t vcan_tx (struct sk_buff *skb,struct net_device *dev) { -struct can_frame *cf = (struct can_frame *)skb->data; +struct canfd_frame *cfd = (struct canfd_frame *)skb->data;struct net_device_stats *stats = &dev->stats;int loop;
@@ -94,7 +93,7 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; stats->tx_packets++; - stats->tx_bytes += cf->can_dlc; + stats->tx_bytes += cfd->len;/* set flag whether this packet has to be looped back */ loop = skb->pkt_type ==PACKET_LOOPBACK ;
@@ -108,7 +107,7 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)* CAN core already did the echo for us */ stats->rx_packets++; - stats->rx_bytes += cf->can_dlc; + stats->rx_bytes += cfd->len; }kfree_skb (skb);return NETDEV_TX_OK ;
@@ -133,14 +132,31 @@ static netdev_tx_t vcan_tx(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; } +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 ) +return -EINVAL ; + + dev->mtu = new_mtu; + +/* notify users (generic networking code notifies only with IFF_UP) */ +call_netdevice_notifiers (NETDEV_CHANGEMTU , dev); +return 0 ; +} +static const struct net_device_ops vcan_netdev_ops = { .ndo_start_xmit = vcan_tx, + .ndo_change_mtu = vcan_change_mtu, };static void vcan_setup (struct net_device *dev) { dev->type =ARPHRD_CAN ; - dev->mtu =sizeof (struct can_frame ); + dev->mtu =CAN_MTU ; dev->hard_header_len =0 ; dev->addr_len =0 ; dev->tx_queue_len =0 ;
include/linux/can.h +49 -2
67 lines not on this page · raw message
include/linux/can/core.h +2 -2
@@ -17,10 +17,10 @@#include <linux/skbuff.h> #include <linux/netdevice.h> -#define CAN_VERSION "20090105" +#define CAN_VERSION "20120515"/* increment this number each time you change some user-space interface */ -#define CAN_ABI_VERSION "8" +#define CAN_ABI_VERSION "9"#define CAN_VERSION_STRING "rev " CAN_VERSION " abi " CAN_ABI_VERSION
include/linux/can/dev.h +25 -8
@@ -61,23 +61,40 @@ struct can_priv {* To be used in the CAN netdriver receive path to ensure conformance with * ISO 11898-1 Chapter 8.4.2.3 (DLC field) */ -#define get_can_dlc (i) (min_t(__u8, (i), 8)) +#define get_can_dlc (i) (min_t(__u8, (i), CAN_MAX_DLC)) +#define get_canfd_dlc (i) (min_t(__u8, (i), CANFD_MAX_DLC))/* Drop a given socketbuffer if it does not contain a valid CAN frame. */ static inline int can_dropped_invalid_skb (struct net_device *dev,struct sk_buff *skb) { -const struct can_frame *cf = (struct can_frame *)skb->data; - -if (unlikely (skb->len !=sizeof (*cf) || cf->can_dlc >8 )) { -kfree_skb (skb); - dev->stats.tx_dropped++; -return 1 ; - } +const struct canfd_frame *cfd = (struct canfd_frame *)skb->data; + +if (skb->protocol ==htons (ETH_P_CAN )) { +if (unlikely (skb->len !=sizeof (struct can_frame ) || + cfd->len >CAN_MAX_DLEN )) + goto inval_skb; + }else if (skb->protocol ==htons (ETH_P_CANFD )) { +if (unlikely (skb->len !=sizeof (struct canfd_frame ) || + cfd->len >CANFD_MAX_DLEN )) + goto inval_skb; + }else + goto inval_skb;return 0 ; + +inval_skb: +kfree_skb (skb); + dev->stats.tx_dropped++; +return 1 ; } +/* get data length from can_dlc with sanitized can_dlc */ +u8 can_dlc2len (u8 can_dlc); + +/* map the sanitized data length to an appropriate data length code */ +u8 can_len2dlc (u8 len); +struct net_device *alloc_candev (int sizeof_priv,unsigned int echo_skb_max);void free_candev (struct net_device *dev);
include/linux/can/raw.h +2 -1
@@ -23,7 +23,8 @@ enum {CAN_RAW_FILTER =1 ,/* set 0 .. n can_filter(s) */ 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_RECV_OWN_MSGS ,/* receive my own msgs (default:off) */ + CAN_RAW_FD_FRAMES, /* use struct canfd_frame (default:off) */ };#endif
include/linux/if_ether.h +2 -1
@@ -105,7 +105,8 @@#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/* Controller Area Network */ +#define ETH_P_CAN 0x000C/* Controller Area Network (CAN)*/ +#define ETH_P_CANFD 0x000D/* CAN FD 64 byte payload frames*/ #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]) */
net/can/af_can.c +83 -27
184 lines not on this page · raw message
net/can/bcm.c +7 -0
@@ -1357,6 +1357,13 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg,switch (msg) { +case NETDEV_CHANGEMTU : +/* +* Changing the MTU breaks the frame size that was formerly +* registered. The change can only be done when the interface +* ist down. Btw. make sure to get no trash from the device and +* drop this device from the open sockets perspective. +*/ case NETDEV_UNREGISTER :lock_sock (sk);
net/can/gw.c +1 -1
@@ -424,7 +424,7 @@ static int cgw_notifier(struct notifier_block *nb,if (dev->type !=ARPHRD_CAN )return NOTIFY_DONE ; -if (msg ==NETDEV_UNREGISTER ) { +if (msg ==NETDEV_UNREGISTER || msg ==NETDEV_CHANGEMTU ) {struct cgw_job *gwj =NULL ;struct hlist_node *n, *nx;
net/can/raw.c +59 -4
125 lines not on this page · raw message
Oliver,
A few nitpicks, and 1 change of behaviour, as announced in my last reply on v3.
Quoted 34 lines · Oliver Hartkopp
Hi all,
changes since v3:
can: bump version date (simultaneous support)
-->can_raw: allow simultaneous access of legacy apps and CANFD aware apps to CAN(FD) devices
af_can: remove pointless initializer inpacket_typedefinition
can: add notifier handling when changing the CAN device MTUI generated this diff by 'git diff fb7944b' on the repository
https://gitorious.org/~hartkopp/linux-can/hartkopps-linux-can-next
Regards,
Oliverdrivers/net/can/dev.c | 35 ++++++++++++++ drivers/net/can/vcan.c | 30 +++++++++--- include/linux/can.h | 51 ++++++++++++++++++++- include/linux/can/core.h | 4 - include/linux/can/dev.h | 33 ++++++++++---- include/linux/can/raw.h | 3 - include/linux/if_ether.h | 3 - net/can/af_can.c | 110 +++++++++++++++++++++++++++++++++++------------ net/can/bcm.c | 7 ++ net/can/gw.c | 2 net/can/raw.c | 63 +++++++++++++++++++++++++- 11 files changed, 287 insertions(+), 54 deletions(-)---
[...]
net/can/bcm.c static int bcm_notifier(struct notifier_block *nb, unsigned long msg, · line 1364
Quoted 13 lines
net/can/bcm.c +5
-0@@ -1357,6 +1357,13 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg,switch (msg) { +case NETDEV_CHANGEMTU : +/* +* Changing the MTU breaks the frame size that was formerly +* registered. The change can only be done when the interface +* ist down. Btw. make sure to get no trash from the device and
'ist' sound german.
You have this in several places :-)
Quoted 9 lines
net/can/bcm.c +2
-0+* drop this device from the open sockets perspective. +*/ case NETDEV_UNREGISTER :lock_sock (sk);net/can/raw.c +0
-0
[...]
net/can/raw.c static void raw_rcv(struct sk_buff *oskb, void *data) · line 127
Quoted 9 lines
net/can/raw.c +5
-0@@ -119,6 +120,14 @@ static void raw_rcv(struct sk_buff *oskb, void *data)if (!ro->recv_own_msgs && oskb->sk == sk)return ; +/* do not pass frames with DLC > 8 to a legacy socket */ +if (!ro->fd_frames) { +struct canfd_frame *cfd = (struct canfd_frame *)oskb->data; + +if (unlikely (cfd->len >CAN_MAX_DLEN ))
idea:
cfd->len = CAN_MAX_LEN;
but this modification must then be done _after_ the skb_clone below.
Quoted 7 lines
net/can/raw.c +3
-0+/* return; */ + } +/* clone the given skb to be able to enqueue it into the rcv queue */ skb =skb_clone (oskb,GFP_ATOMIC );if (!skb)
net/can/raw.c static int raw_notifier(struct notifier_block *nb, · line 264
Quoted 10 lines
net/can/raw.c +7
-0@@ -246,6 +255,13 @@ static int raw_notifier(struct notifier_block *nb,switch (msg) { +case NETDEV_CHANGEMTU : +/* +* Changing the MTU breaks the frame size that was formerly +* registered. The change can only be done when the interface +* ist down. Btw. make sure to get no trash from the device and +* drop this device from the open sockets perspective. +*/
why exactly?
Quoted 3 lines
net/can/raw.c +0
-0case NETDEV_UNREGISTER :lock_sock (sk);/* remove current filters & unregister */
Oliver Hartkopp
maintainerQuoted 15 lines · Kurt Van Dijck
Oliver,
A few nitpicks, and 1 change of behaviour, as announced in my last reply on v3.
[...]
Quoted 8 lines
net/can/bcm.c +0
-0@@ -1357,6 +1357,13 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg,switch (msg) {
Quoted 7 lines
net/can/bcm.c static int bcm_notifier(struct notifier_block *nb, unsigned long msg, · line 1364
Quoted 5 lines
net/can/bcm.c +5
-0+case NETDEV_CHANGEMTU : +/* +* Changing the MTU breaks the frame size that was formerly +* registered. The change can only be done when the interface +* ist down. Btw. make sure to get no trash from the device and 'ist' sound german.
You have this in several places :-)
Oh, snap! German engineering :-)
http://www.youtube.com/watch?v=0I0WfnhVs2s
Fixed the typo in my git repo.
Quoted 20 lines
Quoted 9 lines
net/can/bcm.c +2
-0+* drop this device from the open sockets perspective. +*/ case NETDEV_UNREGISTER :lock_sock (sk);net/can/raw.c +0
-0[...]
net/can/raw.c static void raw_rcv(struct sk_buff *oskb, void *data) · line 127
Quoted 9 lines
net/can/raw.c +5
-0@@ -119,6 +120,14 @@ static void raw_rcv(struct sk_buff *oskb, void *data)if (!ro->recv_own_msgs && oskb->sk == sk)return ; +/* do not pass frames with DLC > 8 to a legacy socket */ +if (!ro->fd_frames) { +struct canfd_frame *cfd = (struct canfd_frame *)oskb->data; + +if (unlikely (cfd->len >CAN_MAX_DLEN ))idea:
Quoted 3 lines
cfd->len = CAN_MAX_LEN;but this modification must then be done _after_ the
skb_clonebelow.
I dislike cutting payload data, because people would not be able to detect that. It's better to provide nothing than to provide manipulated data.
It's hard for me to follow your argument that people will get confused when they don't see a CANFD frame on a legacy RAW socket.
Up to then CANFD capable CAN tools will be surely available too.
Quoted 10 lines
Quoted 10 lines
net/can/raw.c +3
-0+/* return; */ + } +/* clone the given skb to be able to enqueue it into the rcv queue */ skb =skb_clone (oskb,GFP_ATOMIC );if (!skb)@@ -246,6 +255,13 @@ static int raw_notifier(struct notifier_block *nb,switch (msg) {
Quoted 9 lines
net/can/raw.c static int raw_notifier(struct notifier_block *nb, · line 264
Quoted 7 lines
net/can/raw.c +7
-0+case NETDEV_CHANGEMTU : +/* +* Changing the MTU breaks the frame size that was formerly +* registered. The change can only be done when the interface +* ist down. Btw. make sure to get no trash from the device and +* drop this device from the open sockets perspective. +*/ why exactly?
The MTU can only be changed when the interface is down. But the registered CAN device specific filters are not removed on interface DOWN/UP sequence.
Probably removing the registered filters is not so relevant anymore as we have the simultaneous access for new/legacy apps now. Will think about it.
Regards,
Oliver
Quoted 15 lines · Oliver Hartkopp
Quoted 13 lines · Kurt Van Dijck
[...]
net/can/raw.c static void raw_rcv(struct sk_buff *oskb, void *data) · line 127
Quoted 9 lines
net/can/raw.c +5
-0@@ -119,6 +120,14 @@ static void raw_rcv(struct sk_buff *oskb, void *data)if (!ro->recv_own_msgs && oskb->sk == sk)return ; +/* do not pass frames with DLC > 8 to a legacy socket */ +if (!ro->fd_frames) { +struct canfd_frame *cfd = (struct canfd_frame *)oskb->data; + +if (unlikely (cfd->len >CAN_MAX_DLEN ))idea:
cfd->len = CAN_MAX_LEN;
Quoted 5 lines
Quoted 1 line
but this modification must then be done _after_ the
skb_clonebelow.I dislike cutting payload data, because people would not be able to detect that.
Yep, that true as well.
Better leave it the way it is now.
Regards,
Kurt
Oliver Hartkopp
maintainerQuoted 16 lines · Kurt Van Dijck
Quoted 15 lines · Oliver Hartkopp
Quoted 13 lines · Kurt Van Dijck
[...]
net/can/raw.c static void raw_rcv(struct sk_buff *oskb, void *data) · line 127
Quoted 9 lines
net/can/raw.c +5
-0@@ -119,6 +120,14 @@ static void raw_rcv(struct sk_buff *oskb, void *data)if (!ro->recv_own_msgs && oskb->sk == sk)return ; +/* do not pass frames with DLC > 8 to a legacy socket */ +if (!ro->fd_frames) { +struct canfd_frame *cfd = (struct canfd_frame *)oskb->data; + +if (unlikely (cfd->len >CAN_MAX_DLEN ))idea:
cfd->len = CAN_MAX_LEN;
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
but this modification must then be done _after_ the
skb_clonebelow.I dislike cutting payload data, because people would not be able to detect that.
Yep, that true as well.
Better leave it the way it is now.
Yep.
I pushed three commits to my can-utils git clone:
If you look at the (not beautified) changes in cansend ... it looks like an award-winning simple upgrade for CAN FD support, doesn't it?
:-)
So far i can send CAN FD frames after setting my device to CANFD_MTU:
# ifconfig vcan0 mtu 72 SIOCSIFMTU: Device or resource busy # ifconfig vcan0 down # ifconfig vcan0 mtu 72 # ifconfig vcan0 up
# ip -statistics link show vcan0
4: vcan0: <NOARP,UP,LOWER_UP> mtu 72 qdisc noqueue state UNKNOWN mode DEFAULT
link/can
RX: bytes packets errors dropped overrun mcast
46 8 0 0 0 0
TX: bytes packets errors dropped carrier collsns
46 8 0 0 0 0
# ./cansend vcan0 123#001122334455667788
# ip -statistics link show vcan0
4: vcan0: <NOARP,UP,LOWER_UP> mtu 72 qdisc noqueue state UNKNOWN mode DEFAULT
link/can
RX: bytes packets errors dropped overrun mcast
55 9 0 0 0 0
TX: bytes packets errors dropped carrier collsns
55 9 0 0 0 0 length = 9 !!
Let's see how easy candump can be converted ... tomorrow.
Regards,
Oliver
vcan name
Quoted 6 lines
4: vcan0: <NOARP,UP,LOWER_UP> mtu 72 qdisc noqueue state UNKNOWN mode DEFAULT
link/can
RX: bytes packets errors dropped overrun mcast
46 8 0 0 0 0
TX: bytes packets errors dropped carrier collsns
46 8 0 0 0 0
wasn't your vcan interface named 'helga' :-) ?
Kurt
Oliver Hartkopp
maintainerRe: vcan name
Quoted 10 lines · Kurt Van Dijck
Quoted 6 lines
4: vcan0: <NOARP,UP,LOWER_UP> mtu 72 qdisc noqueue state UNKNOWN mode DEFAULT
link/can
RX: bytes packets errors dropped overrun mcast
46 8 0 0 0 0
TX: bytes packets errors dropped carrier collsns
46 8 0 0 0 0wasn't your vcan interface named 'helga' :-) ?
Kurt
Well yes.
On my box @work i usually have vcan0 .. vcan3 and helga - but i did not want to confuse the other readers in the first CAN FD test setups ;-)
Regards,
Oliver