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>
This commit is contained in:
Vincent Mailhol
2025-09-23 15:37:10 +09:00
committed by Marc Kleine-Budde
parent 7c7da8aa3f
commit 2304993860
3 changed files with 23 additions and 7 deletions

View File

@@ -240,6 +240,8 @@ void can_setup(struct net_device *dev)
{ {
dev->type = ARPHRD_CAN; dev->type = ARPHRD_CAN;
dev->mtu = CAN_MTU; dev->mtu = CAN_MTU;
dev->min_mtu = CAN_MTU;
dev->max_mtu = CAN_MTU;
dev->hard_header_len = 0; dev->hard_header_len = 0;
dev->addr_len = 0; dev->addr_len = 0;
dev->tx_queue_len = 10; dev->tx_queue_len = 10;
@@ -309,6 +311,21 @@ void free_candev(struct net_device *dev)
} }
EXPORT_SYMBOL_GPL(free_candev); EXPORT_SYMBOL_GPL(free_candev);
void can_set_default_mtu(struct net_device *dev)
{
struct can_priv *priv = netdev_priv(dev);
if (priv->ctrlmode & CAN_CTRLMODE_FD) {
dev->mtu = CANFD_MTU;
dev->min_mtu = CANFD_MTU;
dev->max_mtu = CANFD_MTU;
} else {
dev->mtu = CAN_MTU;
dev->min_mtu = CAN_MTU;
dev->max_mtu = CAN_MTU;
}
}
/* changing MTU and control mode for CAN/CANFD devices */ /* changing MTU and control mode for CAN/CANFD devices */
int can_change_mtu(struct net_device *dev, int new_mtu) int can_change_mtu(struct net_device *dev, int new_mtu)
{ {
@@ -361,8 +378,7 @@ int can_set_static_ctrlmode(struct net_device *dev, u32 static_mode)
priv->ctrlmode = static_mode; priv->ctrlmode = static_mode;
/* override MTU which was set by default in can_setup()? */ /* override MTU which was set by default in can_setup()? */
if (static_mode & CAN_CTRLMODE_FD) can_set_default_mtu(dev);
dev->mtu = CANFD_MTU;
return 0; return 0;
} }

View File

@@ -223,17 +223,16 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
priv->ctrlmode &= ~cm->mask; priv->ctrlmode &= ~cm->mask;
priv->ctrlmode |= maskedflags; priv->ctrlmode |= maskedflags;
/* CAN_CTRLMODE_FD can only be set when driver supports FD */ /* Wipe potential leftovers from previous CAN FD config */
if (priv->ctrlmode & CAN_CTRLMODE_FD) { if (!(priv->ctrlmode & CAN_CTRLMODE_FD)) {
dev->mtu = CANFD_MTU;
} else {
dev->mtu = CAN_MTU;
memset(&priv->fd.data_bittiming, 0, memset(&priv->fd.data_bittiming, 0,
sizeof(priv->fd.data_bittiming)); sizeof(priv->fd.data_bittiming));
priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK; priv->ctrlmode &= ~CAN_CTRLMODE_FD_TDC_MASK;
memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc)); memset(&priv->fd.tdc, 0, sizeof(priv->fd.tdc));
} }
can_set_default_mtu(dev);
fd_tdc_flag_provided = cm->mask & CAN_CTRLMODE_FD_TDC_MASK; fd_tdc_flag_provided = cm->mask & CAN_CTRLMODE_FD_TDC_MASK;
/* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually /* CAN_CTRLMODE_TDC_{AUTO,MANUAL} are mutually
* exclusive: make sure to turn the other one off * exclusive: make sure to turn the other one off

View File

@@ -166,6 +166,7 @@ struct can_priv *safe_candev_priv(struct net_device *dev);
int open_candev(struct net_device *dev); int open_candev(struct net_device *dev);
void close_candev(struct net_device *dev); void close_candev(struct net_device *dev);
void can_set_default_mtu(struct net_device *dev);
int can_change_mtu(struct net_device *dev, int new_mtu); int can_change_mtu(struct net_device *dev, int new_mtu);
int __must_check can_set_static_ctrlmode(struct net_device *dev, int __must_check can_set_static_ctrlmode(struct net_device *dev,
u32 static_mode); u32 static_mode);