Skip to content

Commit db9a140

Browse files
hartkoppgregkh
authored andcommitted
can: isotp: set default value for N_As to 50 micro seconds
[ Upstream commit 530e0d4 ] 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. Link: https://lore.kernel.org/all/20220309120416.83514-2-socketcan@hartkopp.net Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f581df4 commit db9a140

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

include/uapi/linux/can/isotp.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,16 @@ struct can_isotp_ll_options {
137137
#define CAN_ISOTP_WAIT_TX_DONE 0x400 /* wait for tx completion */
138138
#define CAN_ISOTP_SF_BROADCAST 0x800 /* 1-to-N functional addressing */
139139

140-
/* default values */
140+
/* protocol machine default values */
141141

142142
#define CAN_ISOTP_DEFAULT_FLAGS 0
143143
#define CAN_ISOTP_DEFAULT_EXT_ADDRESS 0x00
144144
#define CAN_ISOTP_DEFAULT_PAD_CONTENT 0xCC /* prevent bit-stuffing */
145-
#define CAN_ISOTP_DEFAULT_FRAME_TXTIME 0
145+
#define CAN_ISOTP_DEFAULT_FRAME_TXTIME 50000 /* 50 micro seconds */
146146
#define CAN_ISOTP_DEFAULT_RECV_BS 0
147147
#define CAN_ISOTP_DEFAULT_RECV_STMIN 0x00
148148
#define CAN_ISOTP_DEFAULT_RECV_WFTMAX 0
149149

150-
#define CAN_ISOTP_DEFAULT_LL_MTU CAN_MTU
151-
#define CAN_ISOTP_DEFAULT_LL_TX_DL CAN_MAX_DLEN
152-
#define CAN_ISOTP_DEFAULT_LL_TX_FLAGS 0
153-
154150
/*
155151
* Remark on CAN_ISOTP_DEFAULT_RECV_* values:
156152
*
@@ -162,4 +158,24 @@ struct can_isotp_ll_options {
162158
* consistency and copied directly into the flow control (FC) frame.
163159
*/
164160

161+
/* link layer default values => make use of Classical CAN frames */
162+
163+
#define CAN_ISOTP_DEFAULT_LL_MTU CAN_MTU
164+
#define CAN_ISOTP_DEFAULT_LL_TX_DL CAN_MAX_DLEN
165+
#define CAN_ISOTP_DEFAULT_LL_TX_FLAGS 0
166+
167+
/*
168+
* The CAN_ISOTP_DEFAULT_FRAME_TXTIME has become a non-zero value as
169+
* it only makes sense for isotp implementation tests to run without
170+
* a N_As value. As user space applications usually do not set the
171+
* frame_txtime element of struct can_isotp_options the new in-kernel
172+
* default is very likely overwritten with zero when the sockopt()
173+
* CAN_ISOTP_OPTS is invoked.
174+
* To make sure that a N_As value of zero is only set intentional the
175+
* value '0' is now interpreted as 'do not change the current value'.
176+
* When a frame_txtime of zero is required for testing purposes this
177+
* CAN_ISOTP_FRAME_TXTIME_ZERO u32 value has to be set in frame_txtime.
178+
*/
179+
#define CAN_ISOTP_FRAME_TXTIME_ZERO 0xFFFFFFFF
180+
165181
#endif /* !_UAPI_CAN_ISOTP_H */

net/can/isotp.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ struct isotp_sock {
141141
struct can_isotp_options opt;
142142
struct can_isotp_fc_options rxfc, txfc;
143143
struct can_isotp_ll_options ll;
144+
u32 frame_txtime;
144145
u32 force_tx_stmin;
145146
u32 force_rx_stmin;
146147
struct tpcon rx, tx;
@@ -360,7 +361,7 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
360361

361362
so->tx_gap = ktime_set(0, 0);
362363
/* add transmission time for CAN frame N_As */
363-
so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
364+
so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
364365
/* add waiting time for consecutive frames N_Cs */
365366
if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
366367
so->tx_gap = ktime_add_ns(so->tx_gap,
@@ -1247,6 +1248,14 @@ static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,
12471248
/* no separate rx_ext_address is given => use ext_address */
12481249
if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
12491250
so->opt.rx_ext_address = so->opt.ext_address;
1251+
1252+
/* check for frame_txtime changes (0 => no changes) */
1253+
if (so->opt.frame_txtime) {
1254+
if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
1255+
so->frame_txtime = 0;
1256+
else
1257+
so->frame_txtime = so->opt.frame_txtime;
1258+
}
12501259
break;
12511260

12521261
case CAN_ISOTP_RECV_FC:
@@ -1448,6 +1457,7 @@ static int isotp_init(struct sock *sk)
14481457
so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
14491458
so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
14501459
so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1460+
so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
14511461
so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
14521462
so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
14531463
so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;

0 commit comments

Comments
 (0)