When using a VDPA device, it is important to ensure that the MAC
address is correctly set. The MAC address in the hardware should
match the MAC address from the QEMU command line. This is a recommended
configuration and will allow the system to boot.

Signed-off-by: Cindy Lu <l...@redhat.com>
---
 hw/net/virtio-net.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index bd37651dab..0b6a4e5634 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -3744,12 +3744,29 @@ static bool failover_hide_primary_device(DeviceListener 
*listener,
     /* failover_primary_hidden is set during feature negotiation */
     return qatomic_read(&n->failover_primary_hidden);
 }
+static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
+                                      MACAddr *cmdline_mac, Error **errp)
+{
+    static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+    if (memcmp(hwmac, &zero, sizeof(MACAddr)) != 0) {
+        /*
+         * 1. The hardware MAC address is the same as the QEMU command line MAC
+         *   address, and both of them are not 0.
+         */
+        if ((memcmp(hwmac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+            return true;
+        }
+    }
 
+    return false;
+}
 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *nc;
+    MACAddr macaddr_cmdline;
     int i;
 
     if (n->net_conf.mtu) {
@@ -3857,6 +3874,7 @@ static void virtio_net_device_realize(DeviceState *dev, 
Error **errp)
     virtio_net_add_queue(n, 0);
 
     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
+    memcpy(&macaddr_cmdline, &n->nic_conf.macaddr, sizeof(n->mac));
     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
     n->status = VIRTIO_NET_S_LINK_UP;
@@ -3903,8 +3921,28 @@ static void virtio_net_device_realize(DeviceState *dev, 
Error **errp)
     nc = qemu_get_queue(n->nic);
     nc->rxfilter_notify_enabled = 1;
 
-   if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
+    if (nc->peer && (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA)) {
+
         struct virtio_net_config netcfg = {};
+        if (nc->peer->check_mac) {
+            vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
+                                 ETH_ALEN);
+            if (!virtio_net_check_vdpa_mac(n, (uint8_t *)&netcfg.mac,
+                                           &macaddr_cmdline, errp)) {
+                error_setg(errp, "vDPA device's MAC address "
+                           "%02x:%02x:%02x:%02x:%02x:%02x "
+                           "is not same as the QEMU command line MAC address"
+                           "%02x:%02x:%02x:%02x:%02x:%02x,"
+                           "Initialization failed.",
+                           netcfg.mac[0], netcfg.mac[1], netcfg.mac[2],
+                           netcfg.mac[3], netcfg.mac[4], netcfg.mac[5],
+                           macaddr_cmdline.a[0], macaddr_cmdline.a[1],
+                           macaddr_cmdline.a[2], macaddr_cmdline.a[3],
+                           macaddr_cmdline.a[4], macaddr_cmdline.a[5]);
+                virtio_cleanup(vdev);
+                return;
+            }
+        }
         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
         vhost_net_set_config(get_vhost_net(nc->peer),
             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND);
-- 
2.45.0


Reply via email to