Skip to content

Commit 2304993

Browse files
vincent-mailholmarckleinebudde
authored andcommitted
can: populate the minimum and maximum MTU values
By populating: net_device->min_mtu and net_device->max_mtu the net core infrastructure will automatically: 1. validate that the user's inputs are in range. 2. report those min and max MTU values through the netlink interface. Add can_set_default_mtu() which sets the default mtu value as well as the minimum and maximum values. The logic for the default mtu value remains unchanged: - CANFD_MTU if the device has a static CAN_CTRLMODE_FD. - CAN_MTU otherwise. Call can_set_default_mtu() each time the CAN_CTRLMODE_FD is modified. This will guarantee that the MTU value is always consistent with the control mode flags. With this, the checks done in can_change_mtu() become fully redundant and will be removed in an upcoming change and it is now possible to confirm the minimum and maximum MTU values on a physical CAN interface by doing: $ ip --details link show can0 The virtual interfaces (vcan and vxcan) are not impacted by this change. Signed-off-by: Vincent Mailhol <mailhol@kernel.org> Link: https://patch.msgid.link/20250923-can-fix-mtu-v3-3-581bde113f52@kernel.org [mkl: squashed https://patch.msgid.link/20250924143644.17622-2-mailhol@kernel.org] Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
1 parent 7c7da8a commit 2304993

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

drivers/net/can/dev/dev.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ void can_setup(struct net_device *dev)
240240
{
241241
dev->type = ARPHRD_CAN;
242242
dev->mtu = CAN_MTU;
243+
dev->min_mtu = CAN_MTU;
244+
dev->max_mtu = CAN_MTU;
243245
dev->hard_header_len = 0;
244246
dev->addr_len = 0;
245247
dev->tx_queue_len = 10;
@@ -309,6 +311,21 @@ void free_candev(struct net_device *dev)
309311
}
310312
EXPORT_SYMBOL_GPL(free_candev);
311313

314+
void can_set_default_mtu(struct net_device *dev)
315+
{
316+
struct can_priv *priv = netdev_priv(dev);
317+
318+
if (priv->ctrlmode & CAN_CTRLMODE_FD) {
319+
dev->mtu = CANFD_MTU;
320+
dev->min_mtu = CANFD_MTU;
321+
dev->max_mtu = CANFD_MTU;
322+
} else {
323+
dev->mtu = CAN_MTU;
324+
dev->min_mtu = CAN_MTU;
325+
dev->max_mtu = CAN_MTU;
326+
}
327+
}
328+
312329
/* changing MTU and control mode for CAN/CANFD devices */
313330
int can_change_mtu(struct net_device *dev, int new_mtu)
314331
{
@@ -361,8 +378,7 @@ int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
361378
priv->ctrlmode = static_mode;
362379

363380
/* override MTU which was set by default in can_setup()? */
364-
if (static_mode & CAN_CTRLMODE_FD)
365-
dev->mtu = CANFD_MTU;
381+
can_set_default_mtu(dev);
366382

367383
return 0;
368384
}

drivers/net/can/dev/netlink.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,16 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
223223
priv->ctrlmode &= ~cm->mask;
224224
priv->ctrlmode |= maskedflags;
225225

226-
/* CAN_CTRLMODE_FD can only be set when driver supports FD */
227-
if (priv->ctrlmode & CAN_CTRLMODE_FD) {
228-
dev->mtu = CANFD_MTU;
229-
} else {
230-
dev->mtu = CAN_MTU;
226+
/* Wipe potential leftovers from previous CAN FD config */
227+
if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {
231228
memset(&priv->fd.data_bittiming, 0,
232229
sizeof(priv->fd.data_bittiming));
233230
priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;
234231
memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc));
235232
}
236233

234+
can_set_default_mtu(dev);
235+
237236
fd_tdc_flag_provided = cm->mask & CAN_CTRLMODE_FD_TDC_MASK;
238237
/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
239238
* exclusive: make sure to turn the other one off

include/linux/can/dev.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
166166

167167
int open_candev(struct net_device *dev);
168168
void close_candev(struct net_device *dev);
169+
void can_set_default_mtu(struct net_device *dev);
169170
int can_change_mtu(struct net_device *dev, int new_mtu);
170171
int __must_check can_set_static_ctrlmode(struct net_device *dev,
171172
u32 static_mode);

0 commit comments

Comments
 (0)