can: isotp: add local echo tx processing for consecutive frames
Subsystems (2)
Oliver Hartkopp
maintainerPATCH v2 1/5 can: isotp: add local echo tx processing for consecutive frames
Instead of dumping the CAN frames into the netdevice queue the process to transmit consecutive frames (CF) now waits for the frame to be transmitted and therefore echo'ed from the CAN interface.
--- net/can/isotp.c | 219 ++++++++++++++++++++++++++++++------------------ 1 file changed, 139 insertions(+), 80 deletions(-)
net/can/isotp.c +139 -80
@@ -12,11 +12,10 @@* - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG * - when a transfer (tx) is on the run the next write() blocks until it's done * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent * - as we have static buffers the check whether the PDU fits into the buffer * is done at FF reception time (no support for sending 'wait frames') -* - take care of the tx-queue-len as traffic shaping is still on the TODO list * * Copyright (c) 2020 Volkswagen Group Electronic Research * All rights reserved. * * Redistribution and use in source and binary forms, with or without
@@ -141,10 +140,11 @@ struct isotp_sock {struct can_isotp_options opt;struct can_isotp_fc_options rxfc, txfc;struct can_isotp_ll_options ll;u32 force_tx_stmin;u32 force_rx_stmin; +u32 cfecho;/* consecutive frame echo tag */ struct tpcon rx, tx;struct list_head notifier;wait_queue_head_t wait;spinlock_t rx_lock;/* protect single thread state machine */ };
@@ -710,10 +710,67 @@ static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,if (ae) cf->data[0 ] = so->opt.ext_address; } +static void isotp_send_cframe (struct isotp_sock *so) +{ +struct sock *sk = &so->sk; +struct sk_buff *skb; +struct net_device *dev; +struct canfd_frame *cf; +int can_send_ret; +int ae = (so->opt.flags &CAN_ISOTP_EXTEND_ADDR ) ?1 :0 ; + + dev =dev_get_by_index (sock_net (sk), so->ifindex); +if (!dev) +return ; + + skb =alloc_skb (so->ll.mtu +sizeof (struct can_skb_priv ),GFP_ATOMIC ); +if (!skb) { +dev_put (dev); +return ; + } + +can_skb_reserve (skb); +can_skb_prv (skb)->ifindex = dev->ifindex; +can_skb_prv (skb)->skbcnt =0 ; + + cf = (struct canfd_frame *)skb->data; +skb_put_zero (skb, so->ll.mtu); + +/* create consecutive frame */ +isotp_fill_dataframe (cf, so, ae,0 ); + +/* place consecutive frame N_PCI in appropriate index */ + cf->data[ae] =N_PCI_CF | so->tx.sn++; + so->tx.sn %=16 ; + so->tx.bs++; + + cf->flags = so->ll.tx_flags; + + skb->dev = dev; +can_skb_set_owner (skb, sk); + +/* cfecho should have been zero'ed by init/isotp_rcv_echo() */ +if (so->cfecho) +pr_notice_once ("can-isotp: cfecho is %08X != 0\n" , so->cfecho); + +/* set consecutive frame echo tag */ + so->cfecho = *(u32 *)cf->data; + +/* send frame with local echo enabled */ + can_send_ret =can_send (skb,1 ); +if (can_send_ret) { +pr_notice_once ("can-isotp: %s: can_send_ret %pe\n" , + __func__,ERR_PTR (can_send_ret)); +if (can_send_ret == -ENOBUFS ) +pr_notice_once ("can-isotp: tx queue is full\n" ); + } +dev_put (dev); +} +static void isotp_create_fframe (struct canfd_frame *cf,struct isotp_sock *so,int ae) {int i;int ff_pci_sz;
@@ -746,23 +803,78 @@ static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so, so->tx.sn =1 ; so->tx.state =ISOTP_WAIT_FIRST_FC ; } +static void isotp_rcv_echo (struct sk_buff *skb,void *data) +{ +struct sock *sk = (struct sock *)data; +struct isotp_sock *so =isotp_sk (sk); +struct canfd_frame *cf = (struct canfd_frame *)skb->data; + +/* only handle my own local echo skb's */ +if (skb->sk != sk || so->cfecho != *(u32 *)cf->data) +return ; + +/* cancel local echo timeout */ +hrtimer_cancel (&so->txtimer); + +/* local echo skb with consecutive frame has been consumed */ + so->cfecho =0 ; + +if (so->tx.idx >= so->tx.len) { +/* we are done */ + so->tx.state =ISOTP_IDLE ; +wake_up_interruptible (&so->wait); +return ; + } + +if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { +/* stop and wait for FC with timeout */ + so->tx.state =ISOTP_WAIT_FC ; +hrtimer_start (&so->txtimer,ktime_set (1 ,0 ), +HRTIMER_MODE_REL_SOFT ); +return ; + } + +/* no gap between data frames needed => use burst mode */ +if (!so->tx_gap) { +isotp_send_cframe (so); +return ; + } + +/* start timer to send next consecutive frame with correct delay */ +hrtimer_start (&so->txtimer, so->tx_gap,HRTIMER_MODE_REL_SOFT ); +} +static enum hrtimer_restart isotp_tx_timer_handler (struct hrtimer *hrtimer) { struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, txtimer);struct sock *sk = &so->sk; -struct sk_buff *skb; -struct net_device *dev; -struct canfd_frame *cf;enum hrtimer_restart restart =HRTIMER_NORESTART ; -int can_send_ret; -int ae = (so->opt.flags &CAN_ISOTP_EXTEND_ADDR ) ?1 :0 ;switch (so->tx.state) { +case ISOTP_SENDING : + +/* cfecho should be consumed by isotp_rcv_echo() here */ +if (!so->cfecho) { +/* start timeout for unlikely lost echo skb */ +hrtimer_set_expires (&so->txtimer, +ktime_add (ktime_get (), +ktime_set (2 ,0 ))); + restart =HRTIMER_RESTART ; + +/* push out the next consecutive frame */ +isotp_send_cframe (so); +break ; + } + +/* cfecho has not been cleared in isotp_rcv_echo() */ +pr_notice_once ("can-isotp: cfecho %08X timeout\n" , so->cfecho); + fallthrough; +case ISOTP_WAIT_FC :case ISOTP_WAIT_FIRST_FC :/* we did not get any flow control frame in time */
@@ -774,82 +886,10 @@ static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer)/* reset tx state */ so->tx.state =ISOTP_IDLE ;wake_up_interruptible (&so->wait);break ; -case ISOTP_SENDING : - -/* push out the next segmented pdu */ - dev =dev_get_by_index (sock_net (sk), so->ifindex); -if (!dev) -break ; - -isotp_tx_burst: - skb =alloc_skb (so->ll.mtu +sizeof (struct can_skb_priv ), -GFP_ATOMIC ); -if (!skb) { -dev_put (dev); -break ; - } - -can_skb_reserve (skb); -can_skb_prv (skb)->ifindex = dev->ifindex; -can_skb_prv (skb)->skbcnt =0 ; - - cf = (struct canfd_frame *)skb->data; -skb_put_zero (skb, so->ll.mtu); - -/* create consecutive frame */ -isotp_fill_dataframe (cf, so, ae,0 ); - -/* place consecutive frame N_PCI in appropriate index */ - cf->data[ae] =N_PCI_CF | so->tx.sn++; - so->tx.sn %=16 ; - so->tx.bs++; - - cf->flags = so->ll.tx_flags; - - skb->dev = dev; -can_skb_set_owner (skb, sk); - - can_send_ret =can_send (skb,1 ); -if (can_send_ret) { -pr_notice_once ("can-isotp: %s: can_send_ret %pe\n" , - __func__,ERR_PTR (can_send_ret)); -if (can_send_ret == -ENOBUFS ) -pr_notice_once ("can-isotp: tx queue is full, increasing txqueuelen may prevent this error\n" ); - } -if (so->tx.idx >= so->tx.len) { -/* we are done */ - so->tx.state =ISOTP_IDLE ; -dev_put (dev); -wake_up_interruptible (&so->wait); -break ; - } - -if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { -/* stop and wait for FC */ - so->tx.state =ISOTP_WAIT_FC ; -dev_put (dev); -hrtimer_set_expires (&so->txtimer, -ktime_add (ktime_get (), -ktime_set (1 ,0 ))); - restart =HRTIMER_RESTART ; -break ; - } - -/* no gap between data frames needed => use burst mode */ -if (!so->tx_gap) - goto isotp_tx_burst; - -/* start timer to send next data frame with correct delay */ -dev_put (dev); -hrtimer_set_expires (&so->txtimer, -ktime_add (ktime_get (), so->tx_gap)); - restart =HRTIMER_RESTART ; -break ; -default :WARN_ON_ONCE (1 ); }return restart;
@@ -1073,10 +1113,13 @@ static int isotp_release(struct socket *sock) dev =dev_get_by_index (net, so->ifindex);if (dev) {can_rx_unregister (net, dev, so->rxid,SINGLE_MASK (so->rxid), isotp_rcv, sk); +can_rx_unregister (net, dev, so->txid, +SINGLE_MASK (so->txid), + isotp_rcv_echo, sk);dev_put (dev);synchronize_rcu (); } } }
@@ -1159,25 +1202,37 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)if (!(dev->flags &IFF_UP )) notify_enetdown =1 ; ifindex = dev->ifindex; -if (do_rx_reg) +if (do_rx_reg) {can_rx_register (net, dev, addr->can_addr.tp.rx_id,SINGLE_MASK (addr->can_addr.tp.rx_id), isotp_rcv, sk,"isotp" , sk); +/* no consecutive frame echo skb in flight */ + so->cfecho =0 ; + +/* register for echo skb's */ +can_rx_register (net, dev, addr->can_addr.tp.tx_id, +SINGLE_MASK (addr->can_addr.tp.tx_id), + isotp_rcv_echo, sk,"isotpe" , sk); + } +dev_put (dev);if (so->bound && do_rx_reg) {/* unregister old filter */ if (so->ifindex) { dev =dev_get_by_index (net, so->ifindex);if (dev) {can_rx_unregister (net, dev, so->rxid,SINGLE_MASK (so->rxid), isotp_rcv, sk); +can_rx_unregister (net, dev, so->txid, +SINGLE_MASK (so->txid), + isotp_rcv_echo, sk);dev_put (dev); } } }
@@ -1379,14 +1434,18 @@ static void isotp_notify(struct isotp_sock *so, unsigned long msg,switch (msg) {case NETDEV_UNREGISTER :lock_sock (sk);/* remove current filters & unregister */ -if (so->bound && (!(so->opt.flags &CAN_ISOTP_SF_BROADCAST ))) +if (so->bound && (!(so->opt.flags &CAN_ISOTP_SF_BROADCAST ))) {can_rx_unregister (dev_net (dev), dev, so->rxid,SINGLE_MASK (so->rxid), isotp_rcv, sk); +can_rx_unregister (dev_net (dev), dev, so->txid, +SINGLE_MASK (so->txid), + isotp_rcv_echo, sk); + } so->ifindex =0 ; so->bound =0 ;release_sock (sk);
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerPATCH v2 2/5 can: isotp: set default value for N_As to 50 micro seconds
The N_As value describes the time a CAN frame needs on the wire when transmitted by the CAN controller. Even very short CAN FD frames need arround 100 usecs (bitrate 1Mbit/s, data bitrate 8Mbit/s).
Having N_As to be zero (the former default) leads to 'no CAN frame separation' when STmin is set to zero by the receiving node. This 'burst mode' should not be enabled by default as it could potentially dump a high number of CAN frames into the netdev queue from the soft hrtimer context. This does not affect the system stability but is just not nice and cooperative.
With this N_As/frame_txtime value the 'burst mode' is disabled by default.
As user space applications usually do not set the frame_txtime element of struct can_isotp_options the new in-kernel default is very likely overwritten with zero when the sockopt() CAN_ISOTP_OPTS is invoked. To make sure that a N_As value of zero is only set intentional the value '0' is now interpreted as 'do not change the current value'. When a frame_txtime of zero is required for testing purposes this CAN_ISOTP_FRAME_TXTIME_ZERO u32 value has to be set in frame_txtime.
--- include/uapi/linux/can/isotp.h | 28 ++++++++++++++++++++++------ net/can/isotp.c | 12 +++++++++++- 2 files changed, 33 insertions(+), 7 deletions(-)
include/uapi/linux/can/isotp.h +22 -6
@@ -135,24 +135,20 @@ struct can_isotp_ll_options {#define CAN_ISOTP_FORCE_RXSTMIN 0x100/* ignore CFs depending on rx stmin */ #define CAN_ISOTP_RX_EXT_ADDR 0x200/* different rx extended addressing */ #define CAN_ISOTP_WAIT_TX_DONE 0x400/* wait for tx completion */ #define CAN_ISOTP_SF_BROADCAST 0x800/* 1-to-N functional addressing */ -/* default values */ +/* protocol machine default values */ #define CAN_ISOTP_DEFAULT_FLAGS 0#define CAN_ISOTP_DEFAULT_EXT_ADDRESS 0x00#define CAN_ISOTP_DEFAULT_PAD_CONTENT 0xCC/* prevent bit-stuffing */ -#define CAN_ISOTP_DEFAULT_FRAME_TXTIME 0 +#define CAN_ISOTP_DEFAULT_FRAME_TXTIME 50000/* 50 micro seconds */ #define CAN_ISOTP_DEFAULT_RECV_BS 0#define CAN_ISOTP_DEFAULT_RECV_STMIN 0x00#define CAN_ISOTP_DEFAULT_RECV_WFTMAX 0 -#define CAN_ISOTP_DEFAULT_LL_MTU CAN_MTU -#define CAN_ISOTP_DEFAULT_LL_TX_DL CAN_MAX_DLEN -#define CAN_ISOTP_DEFAULT_LL_TX_FLAGS 0 -/* * Remark on CAN_ISOTP_DEFAULT_RECV_* values: * * We can strongly assume, that the Linux Kernel implementation of * CAN_ISOTP is capable to run with BS=0, STmin=0 and WFTmax=0.
@@ -160,6 +156,26 @@ struct can_isotp_ll_options {* these default settings can be changed via sockopts. * For that reason the STmin value is intentionally _not_ checked for * consistency and copied directly into the flow control (FC) frame. */ +/* link layer default values => make use of Classical CAN frames */ + +#define CAN_ISOTP_DEFAULT_LL_MTU CAN_MTU +#define CAN_ISOTP_DEFAULT_LL_TX_DL CAN_MAX_DLEN +#define CAN_ISOTP_DEFAULT_LL_TX_FLAGS 0 + +/* +* The CAN_ISOTP_DEFAULT_FRAME_TXTIME has become a non-zero value as +* it only makes sense for isotp implementation tests to run without +* a N_As value. As user space applications usually do not set the +* frame_txtime element of struct can_isotp_options the new in-kernel +* default is very likely overwritten with zero when the sockopt() +* CAN_ISOTP_OPTS is invoked. +* To make sure that a N_As value of zero is only set intentional the +* value '0' is now interpreted as 'do not change the current value'. +* When a frame_txtime of zero is required for testing purposes this +* CAN_ISOTP_FRAME_TXTIME_ZERO u32 value has to be set in frame_txtime. +*/ +#define CAN_ISOTP_FRAME_TXTIME_ZERO 0xFFFFFFFF +#endif /* !_UAPI_CAN_ISOTP_H */
net/can/isotp.c +11 -1
@@ -138,10 +138,11 @@ struct isotp_sock {ktime_t lastrxcf_tstamp;struct hrtimer rxtimer, txtimer;struct can_isotp_options opt;struct can_isotp_fc_options rxfc, txfc;struct can_isotp_ll_options ll; +u32 frame_txtime;u32 force_tx_stmin;u32 force_rx_stmin;u32 cfecho;/* consecutive frame echo tag */ struct tpcon rx, tx;struct list_head notifier;
@@ -358,11 +359,11 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae) (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9)) so->txfc.stmin = 0x7F; so->tx_gap =ktime_set (0 ,0 );/* add transmission time for CAN frame N_As */ - so->tx_gap =ktime_add_ns (so->tx_gap, so->opt.frame_txtime); + so->tx_gap =ktime_add_ns (so->tx_gap, so->frame_txtime);/* add waiting time for consecutive frames N_Cs */ if (so->opt.flags &CAN_ISOTP_FORCE_TXSTMIN ) so->tx_gap =ktime_add_ns (so->tx_gap, so->force_tx_stmin);else if (so->txfc.stmin <0x80 )
@@ -1291,10 +1292,18 @@ static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,return -EFAULT ;/* no separate rx_ext_address is given => use ext_address */ if (!(so->opt.flags &CAN_ISOTP_RX_EXT_ADDR )) so->opt.rx_ext_address = so->opt.ext_address; + +/* check for frame_txtime changes (0 => no changes) */ +if (so->opt.frame_txtime) { +if (so->opt.frame_txtime ==CAN_ISOTP_FRAME_TXTIME_ZERO ) + so->frame_txtime =0 ; +else + so->frame_txtime = so->opt.frame_txtime; + }break ;case CAN_ISOTP_RECV_FC :if (optlen !=sizeof (struct can_isotp_fc_options ))return -EINVAL ;
@@ -1496,10 +1505,11 @@ static int isotp_init(struct sock *sk) so->opt.ext_address =CAN_ISOTP_DEFAULT_EXT_ADDRESS ; so->opt.rx_ext_address =CAN_ISOTP_DEFAULT_EXT_ADDRESS ; so->opt.rxpad_content =CAN_ISOTP_DEFAULT_PAD_CONTENT ; so->opt.txpad_content =CAN_ISOTP_DEFAULT_PAD_CONTENT ; so->opt.frame_txtime =CAN_ISOTP_DEFAULT_FRAME_TXTIME ; + so->frame_txtime =CAN_ISOTP_DEFAULT_FRAME_TXTIME ; so->rxfc.bs =CAN_ISOTP_DEFAULT_RECV_BS ; so->rxfc.stmin =CAN_ISOTP_DEFAULT_RECV_STMIN ; so->rxfc.wftmax =CAN_ISOTP_DEFAULT_RECV_WFTMAX ; so->ll.mtu =CAN_ISOTP_DEFAULT_LL_MTU ; so->ll.tx_dl =CAN_ISOTP_DEFAULT_LL_TX_DL ;
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerPATCH v2 3/5 can: isotp: set max PDU size to 64 kByte
In reply to Oliver HartkoppThe reason to extend the max PDU size from 4095 Byte (12 bit length value) to a 32 bit value (up to 4 GByte) was to be able to flash 64 kByte bootloaders with a single ISO-TP PDU. The max PDU size in the Linux kernel implementation was set to 8200 Bytes to be able to test the length information escape sequence.
It turns out that the demand for 64 kByte PDUs is real so the value for MAX_MSG_LENGTH is set to 66000 to be able to potentially add some checksums to the 65.536 Byte block.
--- net/can/isotp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/can/isotp.c +2 -2
@@ -84,13 +84,13 @@ MODULE_ALIAS("can-proto-6"); (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))/* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can * take full 32 bit values (4 Gbyte). We would need some good concept to handle * this between user space and kernel space. For now increase the static buffer -* to something about 8 kbyte to be able to test this new functionality. +* to something about 64 kbyte to be able to test this new functionality. */ -#define MAX_MSG_LENGTH 8200 +#define MAX_MSG_LENGTH 66000/* N_PCI type values in bits 7-4 of N_PCI bytes */ #define N_PCI_SF 0x00/* single frame */ #define N_PCI_FF 0x10/* first frame */ #define N_PCI_CF 0x20/* consecutive frame */
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerPATCH v2 4/5 vxcan: remove sk reference in peer skb
In reply to Oliver HartkoppWith can_create_echo_skb() the skb which is forwarded to the peer CAN interface shares the sk pointer from the originating socket. This makes the CAN frame show up in the peer namespace as a TX packet.
With the use of skb_clone() analogue to the handling in gw.c the peer skb gets a new start in the peer namespace and correctly appears as a RX packet.
--- drivers/net/can/vxcan.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/net/can/vxcan.c +11 -6
@@ -31,32 +31,37 @@ MODULE_ALIAS_RTNL_LINK(DRV_NAME);struct vxcan_priv {struct net_device __rcu *peer; }; -static netdev_tx_t vxcan_xmit (struct sk_buff *skb,struct net_device *dev) +static netdev_tx_t vxcan_xmit (struct sk_buff *oskb,struct net_device *dev) {struct vxcan_priv *priv =netdev_priv (dev);struct net_device *peer; -struct canfd_frame *cfd = (struct canfd_frame *)skb->data; +struct canfd_frame *cfd = (struct canfd_frame *)oskb->data;struct net_device_stats *peerstats, *srcstats = &dev->stats; +struct sk_buff *skb;u8 len; -if (can_dropped_invalid_skb (dev, skb)) +if (can_dropped_invalid_skb (dev, oskb))return NETDEV_TX_OK ;rcu_read_lock (); peer =rcu_dereference (priv->peer);if (unlikely (!peer)) { -kfree_skb (skb); +kfree_skb (oskb); dev->stats.tx_dropped++; goto out_unlock; } - skb =can_create_echo_skb (skb); -if (!skb) + skb =skb_clone (oskb,GFP_ATOMIC ); +if (skb) { +consume_skb (oskb); + }else { +kfree (oskb); goto out_unlock; + }/* reset CAN GW hop counter */ skb->csum_start =0 ; skb->pkt_type =PACKET_BROADCAST ; skb->dev = peer;
Signature · 1 line
2.30.2
Oliver Hartkopp
maintainerPATCH v2 5/5 vxcan: enable local echo for sent CAN frames
In reply to Oliver HartkoppThe vxcan driver provides a pair of virtual CAN interfaces to exchange CAN traffic between different namespaces - analogue to veth.
In opposite to the vcan driver the local sent CAN traffic on this interface is not echo'ed back but only sent to the remote peer. This is unusual and can be easily fixed by removing IFF_ECHO from the netdevice flags that are set for vxcan interfaces by default at startup.
Without IFF_ECHO set on driver level, the local sent CAN frames are echo'ed in af_can.c in can_send(). This patch makes vxcan interfaces adopt the same local echo behavior and procedures as known from the vcan interfaces.
--- drivers/net/can/vxcan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/can/vxcan.c +1 -1
@@ -151,11 +151,11 @@ static void vxcan_setup(struct net_device *dev) dev->type =ARPHRD_CAN ; dev->mtu =CANFD_MTU ; dev->hard_header_len =0 ; dev->addr_len =0 ; dev->tx_queue_len =0 ; - dev->flags = (IFF_NOARP |IFF_ECHO ); + dev->flags =IFF_NOARP ; dev->netdev_ops = &vxcan_netdev_ops; dev->needs_free_netdev = true; can_ml =netdev_priv (dev) +ALIGN (sizeof (struct vxcan_priv ),NETDEV_ALIGN );can_set_ml_priv (dev, can_ml);
Signature · 1 line
2.30.2