Skip to content

Commit 9f62d22

Browse files
vivierkuba-moo
authored andcommitted
virtio_net: notify MAC address change on device initialization
In virtnet_probe(), if the device doesn't provide a MAC address the driver assigns a random one. As we modify the MAC address we need to notify the device to allow it to update all the related information. The problem can be seen with vDPA and mlx5_vdpa driver as it doesn't assign a MAC address by default. The virtio_net device uses a random MAC address (we can see it with "ip link"), but we can't ping a net namespace from another one using the virtio-vdpa device because the new MAC address has not been provided to the hardware: RX packets are dropped since they don't go through the receive filters, TX packets go through unaffected. Signed-off-by: Laurent Vivier <lvivier@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 7c06458 commit 9f62d22

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

drivers/net/virtio_net.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,6 +3931,8 @@ static int virtnet_probe(struct virtio_device *vdev)
39313931
eth_hw_addr_set(dev, addr);
39323932
} else {
39333933
eth_hw_addr_random(dev);
3934+
dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
3935+
dev->dev_addr);
39343936
}
39353937

39363938
/* Set up our device-specific information */
@@ -4058,6 +4060,24 @@ static int virtnet_probe(struct virtio_device *vdev)
40584060

40594061
virtio_device_ready(vdev);
40604062

4063+
/* a random MAC address has been assigned, notify the device.
4064+
* We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
4065+
* because many devices work fine without getting MAC explicitly
4066+
*/
4067+
if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
4068+
virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
4069+
struct scatterlist sg;
4070+
4071+
sg_init_one(&sg, dev->dev_addr, dev->addr_len);
4072+
if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
4073+
VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
4074+
pr_debug("virtio_net: setting MAC address failed\n");
4075+
rtnl_unlock();
4076+
err = -EINVAL;
4077+
goto free_unregister_netdev;
4078+
}
4079+
}
4080+
40614081
rtnl_unlock();
40624082

40634083
err = virtnet_cpu_notif_add(vi);

0 commit comments

Comments
 (0)