Skip to content

Commit faa9b39

Browse files
almostivandavem330
authored andcommitted
virtio_net: propagate linkspeed/duplex settings from the hypervisor
The ability to set speed and duplex for virtio_net is useful in various scenarios as described here: 16032be virtio_net: add ethtool support for set and get of settings However, it would be nice to be able to set this from the hypervisor, such that virtio_net doesn't require custom guest ethtool commands. Introduce a new feature flag, VIRTIO_NET_F_SPEED_DUPLEX, which allows the hypervisor to export a linkspeed and duplex setting. The user can subsequently overwrite it later if desired via: 'ethtool -s'. Note that VIRTIO_NET_F_SPEED_DUPLEX is defined as bit 63, the intention is that device feature bits are to grow down from bit 63, since the transports are starting from bit 24 and growing up. Signed-off-by: Jason Baron <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Jason Wang <[email protected]> Cc: [email protected] Signed-off-by: David S. Miller <[email protected]>
1 parent ccfdec9 commit faa9b39

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

drivers/net/virtio_net.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,24 @@ static void virtnet_init_settings(struct net_device *dev)
19061906
vi->duplex = DUPLEX_UNKNOWN;
19071907
}
19081908

1909+
static void virtnet_update_settings(struct virtnet_info *vi)
1910+
{
1911+
u32 speed;
1912+
u8 duplex;
1913+
1914+
if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
1915+
return;
1916+
1917+
speed = virtio_cread32(vi->vdev, offsetof(struct virtio_net_config,
1918+
speed));
1919+
if (ethtool_validate_speed(speed))
1920+
vi->speed = speed;
1921+
duplex = virtio_cread8(vi->vdev, offsetof(struct virtio_net_config,
1922+
duplex));
1923+
if (ethtool_validate_duplex(duplex))
1924+
vi->duplex = duplex;
1925+
}
1926+
19091927
static const struct ethtool_ops virtnet_ethtool_ops = {
19101928
.get_drvinfo = virtnet_get_drvinfo,
19111929
.get_link = ethtool_op_get_link,
@@ -2159,6 +2177,7 @@ static void virtnet_config_changed_work(struct work_struct *work)
21592177
vi->status = v;
21602178

21612179
if (vi->status & VIRTIO_NET_S_LINK_UP) {
2180+
virtnet_update_settings(vi);
21622181
netif_carrier_on(vi->dev);
21632182
netif_tx_wake_all_queues(vi->dev);
21642183
} else {
@@ -2707,6 +2726,7 @@ static int virtnet_probe(struct virtio_device *vdev)
27072726
schedule_work(&vi->config_work);
27082727
} else {
27092728
vi->status = VIRTIO_NET_S_LINK_UP;
2729+
virtnet_update_settings(vi);
27102730
netif_carrier_on(dev);
27112731
}
27122732

@@ -2808,7 +2828,8 @@ static struct virtio_device_id id_table[] = {
28082828
VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
28092829
VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
28102830
VIRTIO_NET_F_CTRL_MAC_ADDR, \
2811-
VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
2831+
VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
2832+
VIRTIO_NET_F_SPEED_DUPLEX
28122833

28132834
static unsigned int features[] = {
28142835
VIRTNET_FEATURES,

include/uapi/linux/virtio_net.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
* Steering */
5858
#define VIRTIO_NET_F_CTRL_MAC_ADDR 23 /* Set MAC address */
5959

60+
#define VIRTIO_NET_F_SPEED_DUPLEX 63 /* Device set linkspeed and duplex */
61+
6062
#ifndef VIRTIO_NET_NO_LEGACY
6163
#define VIRTIO_NET_F_GSO 6 /* Host handles pkts w/ any GSO type */
6264
#endif /* VIRTIO_NET_NO_LEGACY */
@@ -76,6 +78,17 @@ struct virtio_net_config {
7678
__u16 max_virtqueue_pairs;
7779
/* Default maximum transmit unit advice */
7880
__u16 mtu;
81+
/*
82+
* speed, in units of 1Mb. All values 0 to INT_MAX are legal.
83+
* Any other value stands for unknown.
84+
*/
85+
__u32 speed;
86+
/*
87+
* 0x00 - half duplex
88+
* 0x01 - full duplex
89+
* Any other value stands for unknown.
90+
*/
91+
__u8 duplex;
7992
} __attribute__((packed));
8093

8194
/*

0 commit comments

Comments
 (0)