Skip to content

Commit 16032be

Browse files
Nikolay Aleksandrovdavem330
authored andcommitted
virtio_net: add ethtool support for set and get of settings
This patch allows the user to set and retrieve speed and duplex of the virtio_net device via ethtool. Having this functionality is very helpful for simulating different environments and also enables the virtio_net device to participate in operations where proper speed and duplex are required (e.g. currently bonding lacp mode requires full duplex). Custom speed and duplex are not allowed, the user-supplied settings are validated before applying. Example: $ ethtool eth1 Settings for eth1: ... Speed: Unknown! Duplex: Unknown! (255) $ ethtool -s eth1 speed 1000 duplex full $ ethtool eth1 Settings for eth1: ... Speed: 1000Mb/s Duplex: Full Based on a patch by Roopa Prabhu. Signed-off-by: Nikolay Aleksandrov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 103a8ad commit 16032be

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

drivers/net/virtio_net.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ struct virtnet_info {
146146
virtio_net_ctrl_ack ctrl_status;
147147
u8 ctrl_promisc;
148148
u8 ctrl_allmulti;
149+
150+
/* Ethtool settings */
151+
u8 duplex;
152+
u32 speed;
149153
};
150154

151155
struct padded_vnet_hdr {
@@ -1376,13 +1380,67 @@ static void virtnet_get_channels(struct net_device *dev,
13761380
channels->other_count = 0;
13771381
}
13781382

1383+
/* Check if the user is trying to change anything besides speed/duplex */
1384+
static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd)
1385+
{
1386+
struct ethtool_cmd diff1 = *cmd;
1387+
struct ethtool_cmd diff2 = {};
1388+
1389+
/* advertising and cmd are usually set, ignore port because we set it */
1390+
ethtool_cmd_speed_set(&diff1, 0);
1391+
diff1.advertising = 0;
1392+
diff1.duplex = 0;
1393+
diff1.port = 0;
1394+
diff1.cmd = 0;
1395+
1396+
return !memcmp(&diff1, &diff2, sizeof(diff1));
1397+
}
1398+
1399+
static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1400+
{
1401+
struct virtnet_info *vi = netdev_priv(dev);
1402+
u32 speed;
1403+
1404+
speed = ethtool_cmd_speed(cmd);
1405+
/* don't allow custom speed and duplex */
1406+
if (!ethtool_validate_speed(speed) ||
1407+
!ethtool_validate_duplex(cmd->duplex) ||
1408+
!virtnet_validate_ethtool_cmd(cmd))
1409+
return -EINVAL;
1410+
vi->speed = speed;
1411+
vi->duplex = cmd->duplex;
1412+
1413+
return 0;
1414+
}
1415+
1416+
static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1417+
{
1418+
struct virtnet_info *vi = netdev_priv(dev);
1419+
1420+
ethtool_cmd_speed_set(cmd, vi->speed);
1421+
cmd->duplex = vi->duplex;
1422+
cmd->port = PORT_OTHER;
1423+
1424+
return 0;
1425+
}
1426+
1427+
static void virtnet_init_settings(struct net_device *dev)
1428+
{
1429+
struct virtnet_info *vi = netdev_priv(dev);
1430+
1431+
vi->speed = SPEED_UNKNOWN;
1432+
vi->duplex = DUPLEX_UNKNOWN;
1433+
}
1434+
13791435
static const struct ethtool_ops virtnet_ethtool_ops = {
13801436
.get_drvinfo = virtnet_get_drvinfo,
13811437
.get_link = ethtool_op_get_link,
13821438
.get_ringparam = virtnet_get_ringparam,
13831439
.set_channels = virtnet_set_channels,
13841440
.get_channels = virtnet_get_channels,
13851441
.get_ts_info = ethtool_op_get_ts_info,
1442+
.get_settings = virtnet_get_settings,
1443+
.set_settings = virtnet_set_settings,
13861444
};
13871445

13881446
#define MIN_MTU 68
@@ -1855,6 +1913,8 @@ static int virtnet_probe(struct virtio_device *vdev)
18551913
netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
18561914
netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
18571915

1916+
virtnet_init_settings(dev);
1917+
18581918
err = register_netdev(dev);
18591919
if (err) {
18601920
pr_debug("virtio_net: registering device failed\n");

0 commit comments

Comments
 (0)