Skip to contentSkip to conversation

virtio_net: Add the check for vdpa's mac address

PATCH Superseded Cindy Lu 4 participants 14 messages Aug 6, 2024 – Aug 9, 2024

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

PATCH 1/3 virtio_net: Add the check for vdpa's mac address

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +29 -4
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+				      Error **errp)
+{
+	struct virtio_net_config hwcfg = {};
+	static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+	vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.
+     */
+
+	if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+		if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+			return true;
+		}
+	}
+	error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+			 "Please check the the vdpa device's setting.");
 
+	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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+	   if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+		   virtio_cleanup(vdev);
+		   return;
+	   }
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH 2/3 virtio_net: Add the check for vdpa mac address

When using a VDPA device, this is another acceptable situations
The hardware MAC address is not 0, and the MAC address in the QEMU command line is 0. This is also acceptable

---
 hw/net/virtio-net.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +12 -1
index 7f51bd0dd3..c144ae2e78 100644
@@ -3592,11 +3592,22 @@ static bool virtio_net_check_vdpa_mac(NetClientState *nc, VirtIONet *n, MACAddr
      * 1.The hardware MAC address is the same as the QEMU command line MAC
      *   address, and both of them are not 0.
      */
-
+	/*
+     * 2.The hardware MAC address is not 0,
+     *  and the MAC address in the QEMU command line is 0.
+     *  In this situation, the hardware MAC address will overwrite
+     *  the QEMU command line address.
+     */
 	if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
 		if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
 			return true;
 		}
+		if (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0) {
+			/* overwrite the mac address with hardware address*/
+			memcpy(&n->mac[0], &hwcfg.mac, sizeof(n->mac));
+			memcpy(&n->nic_conf.macaddr, &hwcfg.mac, sizeof(n->mac));
+			return true;
+		}
 	}
 	error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
 			 "Please check the the vdpa device's setting.");
Signature · 1 line
2.45.0
#2 lore raw

Re: [PATCH 2/3] virtio_net: Add the check for vdpa mac address

Quoted 15 lines · Cindy Lu

When using a VDPA device, this is another acceptable situations
The hardware MAC address is not 0, and the MAC address in the QEMU command line is 0. This is also acceptable

---
 hw/net/virtio-net.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +0 -0
index 7f51bd0dd3..c144ae2e78 100644

hw/net/virtio-net.c static bool virtio_net_check_vdpa_mac(NetClientState *nc, VirtIONet *n, MACAddr · line 3599

Quoted 9 lines
hw/net/virtio-net.c +5 -1
@@ -3592,11 +3592,22 @@ static bool virtio_net_check_vdpa_mac(NetClientState *nc, VirtIONet *n, MACAddr
      * 1.The hardware MAC address is the same as the QEMU command line MAC
      *   address, and both of them are not 0.
      */
-
+       /*
+     * 2.The hardware MAC address is not 0,
+     *  and the MAC address in the QEMU command line is 0.
+     *  In this situation, the hardware MAC address will overwrite
+     *  the QEMU command line address.

This seems to break libvirt assumption?

Adding Jonathon.

Quoted 15 lines
hw/net/virtio-net.c +7 -1
+     */
        if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
                if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
                        return true;
                }
+               if (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0) {
+                       /* overwrite the mac address with hardware address*/
+                       memcpy(&n->mac[0], &hwcfg.mac, sizeof(n->mac));
+                       memcpy(&n->nic_conf.macaddr, &hwcfg.mac, sizeof(n->mac));
+                       return true;
+               }
        }
        error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
                         "Please check the the vdpa device's setting.");
--
Quoted 2 lines

2.45.0

Thanks

author
#3 lore raw

PATCH 3/3 virtio_net: remove the unnecessary check in get_config

In reply to Cindy Lu

The vdpa device with MAC address 0 should not boot. So remove the check here

---
 hw/net/virtio-net.c | 13 -------------
 1 file changed, 13 deletions(-)
hw/net/virtio-net.c +0 -13
index c144ae2e78..8a7c743ad3 100644
@@ -142,7 +142,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
     NetClientState *nc = qemu_get_queue(n->nic);
-    static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
 
     int ret = 0;
     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -170,18 +169,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
         if (ret == -1) {
             return;
         }
-
-        /*
-         * Some NIC/kernel combinations present 0 as the mac address.  As that
-         * is not a legal address, try to proceed with the address from the
-         * QEMU command line in the hope that the address has been configured
-         * correctly elsewhere - just not reported by the device.
-         */
-        if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
-            info_report("Zero hardware mac address detected. Ignoring.");
-            memcpy(netcfg.mac, n->mac, ETH_ALEN);
-        }
-
         netcfg.status |= virtio_tswap16(vdev,
                                         n->status & VIRTIO_NET_S_ANNOUNCE);
         memcpy(config, &netcfg, n->config_size);
Signature · 1 line
2.45.0
#4 lore raw

Re: [PATCH 3/3] virtio_net: remove the unnecessary check in get_config

Quoted 5 lines · Cindy Lu

The vdpa device with MAC address 0 should not boot. So remove the check here

Please describe what issues you've seen, and how it is fixed by this commit.

Thanks

Quoted 38 lines
---
 hw/net/virtio-net.c | 13 -------------
 1 file changed, 13 deletions(-)
hw/net/virtio-net.c +0 -14
index c144ae2e78..8a7c743ad3 100644
@@ -142,7 +142,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
     NetClientState *nc = qemu_get_queue(n->nic);
-    static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
 
     int ret = 0;
     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -170,18 +169,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
         if (ret == -1) {
             return;
         }
-
-        /*
-         * Some NIC/kernel combinations present 0 as the mac address.  As that
-         * is not a legal address, try to proceed with the address from the
-         * QEMU command line in the hope that the address has been configured
-         * correctly elsewhere - just not reported by the device.
-         */
-        if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
-            info_report("Zero hardware mac address detected. Ignoring.");
-            memcpy(netcfg.mac, n->mac, ETH_ALEN);
-        }
-
         netcfg.status |= virtio_tswap16(vdev,
                                         n->status & VIRTIO_NET_S_ANNOUNCE);
         memcpy(config, &netcfg, n->config_size);
--

2.45.0

author
#5 lore raw

Re: [PATCH 3/3] virtio_net: remove the unnecessary check in get_config

Quoted 12 lines · Jason Wang
Quoted 5 lines · Cindy Lu

The vdpa device with MAC address 0 should not boot. So remove the check here

Please describe what issues you've seen, and how it is fixed by this commit.

Thanks

this is a fix after the previous one. In the previous fix, the device with MAC address 0 would not allow booting, so there is no need to check if the MAC address is 0 here.
I will re-organize the whole patch and make it more clear Thanks
Cindy

Quoted 39 lines
Quoted 38 lines
---
 hw/net/virtio-net.c | 13 -------------
 1 file changed, 13 deletions(-)
hw/net/virtio-net.c +0 -14
index c144ae2e78..8a7c743ad3 100644
@@ -142,7 +142,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
     VirtIONet *n = VIRTIO_NET(vdev);
     struct virtio_net_config netcfg;
     NetClientState *nc = qemu_get_queue(n->nic);
-    static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
 
     int ret = 0;
     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
@@ -170,18 +169,6 @@ static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
         if (ret == -1) {
             return;
         }
-
-        /*
-         * Some NIC/kernel combinations present 0 as the mac address.  As that
-         * is not a legal address, try to proceed with the address from the
-         * QEMU command line in the hope that the address has been configured
-         * correctly elsewhere - just not reported by the device.
-         */
-        if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
-            info_report("Zero hardware mac address detected. Ignoring.");
-            memcpy(netcfg.mac, n->mac, ETH_ALEN);
-        }
-
         netcfg.status |= virtio_tswap16(vdev,
                                         n->status & VIRTIO_NET_S_ANNOUNCE);
         memcpy(config, &netcfg, n->config_size);
--

2.45.0

#6 lore raw
In reply to Cindy Lu
Quoted 21 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +2 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+                                     Error **errp)

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3602

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+                        "Please check the the vdpa device's setting.");

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 22 lines
hw/net/virtio-net.c +4 -0
+       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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 10 lines
hw/net/virtio-net.c +4 -4
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Thanks

Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

author
#7 lore raw
Quoted 27 lines · Jason Wang
Quoted 25 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +6 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+                                     Error **errp)
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
Quoted 9 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

yes, there is a section 2, will change this code here Thanks
cindy

Quoted 49 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3602

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+                        "Please check the the vdpa device's setting.");

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 22 lines
hw/net/virtio-net.c +4 -0
+       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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 10 lines
hw/net/virtio-net.c +4 -4
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Quoted 2 lines

Thanks

as we discussed before, the MAC address in hardware should have a "higher priority"
than the MAC address in qemu cmdline. So I remove the set_config there, the MAC address from the hardware will overwrite the MAC in qemu cmdline. so don't need to set_config to hardware now
Thanks,
cindy

Quoted 7 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

#8 lore raw

Do we check that the MAC from the command line or HW was formed correctly and doesn't include multicast bit?

Best regards,
Yan.

Quoted 108 lines · Cindy Lu
Quoted 27 lines · Jason Wang
Quoted 25 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +6 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+                                     Error **errp)
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
Quoted 9 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

yes, there is a section 2, will change this code here Thanks
cindy

Quoted 49 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3602

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+                        "Please check the the vdpa device's setting.");

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 22 lines
hw/net/virtio-net.c +4 -0
+       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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 10 lines
hw/net/virtio-net.c +4 -4
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Quoted 2 lines

Thanks

as we discussed before, the MAC address in hardware should have a "higher priority"
than the MAC address in qemu cmdline. So I remove the set_config there, the MAC address from the hardware will overwrite the MAC in qemu cmdline. so don't need to set_config to hardware now
Thanks,
cindy

Quoted 7 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

author
#9 lore raw
Quoted 7 lines · Yan Vugenfirer

Do we check that the MAC from the command line or HW was formed correctly and doesn't include multicast bit?

Best regards,
Yan.

I didn't include a check for this, but it seems the vhost also doesn't

have this kind of verification. I will double check this Thanks
Cindy

Quoted 125 lines · Cindy Lu
Quoted 15 lines · Jason Wang
Quoted 13 lines · Cindy Lu
Quoted 11 lines

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/net/virtio-net.c static bool

Quoted 5 lines
hw/net/virtio-net.c +0 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ static bool

failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +1 -0
     /* failover_primary_hidden is set during feature negotiation */
     return qatomic_read(&n->failover_primary_hidden);
 }
+static bool virtio_net_check_vdpa_mac(NetClientState *nc, VirtIONet

*n, MACAddr *cmdline_mac,

Quoted 6 lines
Quoted 6 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+                                     Error **errp)
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t

*)&hwcfg, ETH_ALEN);

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +4 -0
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command

line MAC

Quoted 11 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 1 line
hw/net/virtio-net.c +1 -0
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

yes, there is a section 2, will change this code here Thanks
cindy

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +4 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac,

sizeof(MACAddr)) == 0)) {

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +4 -0
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from

qemu cmdline"

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static bool · line 3602

Quoted 1 line
hw/net/virtio-net.c +1 -0
+                        "Please check the the vdpa device's

setting.");

Quoted 8 lines
Quoted 8 lines

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 4 lines
hw/net/virtio-net.c +2 -0
+       return false;
+}
 static void virtio_net_device_realize(DeviceState *dev, Error

**errp)

Quoted 9 lines
Quoted 9 lines

hw/net/virtio-net.c static void

Quoted 9 lines
hw/net/virtio-net.c +1 -0
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *nc;
+    MACAddr macaddr_cmdline;
     int i;
 
     if (n->net_conf.mtu) {
@@ -3692,6 +3716,7 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 8 lines
Quoted 8 lines

hw/net/virtio-net.c static void

Quoted 8 lines
hw/net/virtio-net.c +1 -0
     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;
@@ -3739,10 +3764,10 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 3 lines
Quoted 3 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 3 lines
hw/net/virtio-net.c +0 -0
     nc->rxfilter_notify_enabled = 1;
 
    if (nc->peer && nc->peer->info->type ==

NET_CLIENT_DRIVER_VHOST_VDPA) {

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/net/virtio-net.c +0 -4
-        struct virtio_net_config netcfg = {};
-        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);

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static void · line 3767

Quoted 1 line
hw/net/virtio-net.c +1 -0
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline,

errp)) {

Quoted 25 lines
Quoted 9 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Thanks

as we discussed before, the MAC address in hardware should have a "higher priority"
than the MAC address in qemu cmdline. So I remove the set_config there, the MAC address from the hardware will overwrite the MAC in qemu cmdline. so don't need to set_config to hardware now
Thanks,
cindy

Quoted 7 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

#10 lore raw
In reply to Cindy Lu
Quoted 90 lines · Cindy Lu
Quoted 27 lines · Jason Wang
Quoted 25 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +6 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+                                     Error **errp)
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
Quoted 9 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

yes, there is a section 2, will change this code here Thanks
cindy

Quoted 49 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3602

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+                        "Please check the the vdpa device's setting.");

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 22 lines
hw/net/virtio-net.c +4 -0
+       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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 10 lines
hw/net/virtio-net.c +4 -4
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Quoted 7 lines
Quoted 2 lines

Thanks

as we discussed before, the MAC address in hardware should have a "higher priority"
than the MAC address in qemu cmdline. So I remove the set_config there, the MAC address from the hardware will overwrite the MAC in qemu cmdline. so don't need to set_config to hardware now

Probably, but I meant it needs to be a separate patch.

For example the title said "add ...." but here it's a removal of something.

Thanks

Quoted 10 lines

Thanks,
cindy

Quoted 7 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

author
#11 lore raw
Quoted 112 lines · Jason Wang
Quoted 15 lines · Cindy Lu
Quoted 13 lines · Jason Wang
Quoted 11 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
Quoted 5 lines
Quoted 5 lines

hw/net/virtio-net.c static bool

Quoted 5 lines
hw/net/virtio-net.c +0 -0
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ static bool

failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +1 -0
     /* failover_primary_hidden is set during feature negotiation */
     return qatomic_read(&n->failover_primary_hidden);
 }
+static bool virtio_net_check_vdpa_mac(NetClientState *nc, VirtIONet

*n, MACAddr *cmdline_mac,

Quoted 6 lines
Quoted 6 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+                                     Error **errp)
+{
+       struct virtio_net_config hwcfg = {};
+       static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+       vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t

*)&hwcfg, ETH_ALEN);

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +4 -0
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command

line MAC

Quoted 11 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener, · line 3593

Quoted 1 line
hw/net/virtio-net.c +1 -0
+     *   address, and both of them are not 0.

I guess there should be a bullet 2?

yes, there is a section 2, will change this code here Thanks
cindy

Quoted 4 lines
Quoted 4 lines
hw/net/virtio-net.c +4 -0
+     */
+
+       if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+               if ((memcmp(&hwcfg.mac, cmdline_mac,

sizeof(MACAddr)) == 0)) {

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static bool failover_hide_primary_device(DeviceListener *listener,

Quoted 4 lines
hw/net/virtio-net.c +4 -0
+                       return true;
+               }
+       }
+       error_setg(errp, "vDPA device's mac != the mac address from

qemu cmdline"

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static bool · line 3602

Quoted 1 line
hw/net/virtio-net.c +1 -0
+                        "Please check the the vdpa device's

setting.");

Quoted 8 lines
Quoted 8 lines

For error messages I think it's better to use english instead of "!=" to describe the issue.

Quoted 4 lines
hw/net/virtio-net.c +2 -0
+       return false;
+}
 static void virtio_net_device_realize(DeviceState *dev, Error

**errp)

Quoted 9 lines
Quoted 9 lines

hw/net/virtio-net.c static void

Quoted 9 lines
hw/net/virtio-net.c +1 -0
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIONet *n = VIRTIO_NET(dev);
     NetClientState *nc;
+    MACAddr macaddr_cmdline;
     int i;
 
     if (n->net_conf.mtu) {
@@ -3692,6 +3716,7 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 8 lines
Quoted 8 lines

hw/net/virtio-net.c static void

Quoted 8 lines
hw/net/virtio-net.c +1 -0
     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;
@@ -3739,10 +3764,10 @@ static void

virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 3 lines
Quoted 3 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 3 lines
hw/net/virtio-net.c +0 -0
     nc->rxfilter_notify_enabled = 1;
 
    if (nc->peer && nc->peer->info->type ==

NET_CLIENT_DRIVER_VHOST_VDPA) {

Quoted 4 lines
Quoted 4 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp)

Quoted 4 lines
hw/net/virtio-net.c +0 -4
-        struct virtio_net_config netcfg = {};
-        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);

Quoted 1 line
Quoted 1 line

hw/net/virtio-net.c static void · line 3767

Quoted 1 line
hw/net/virtio-net.c +1 -0
+          if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline,

errp)) {

Quoted 7 lines
Quoted 7 lines

hw/net/virtio-net.c static void virtio_net_device_realize(DeviceState *dev, Error **errp) · line 3770

Quoted 3 lines
hw/net/virtio-net.c +3 -0
+                  virtio_cleanup(vdev);
+                  return;
+          }

Any reason we remove vhost_net_set_config() here? It is not described in the commit or does it belong to another patch?

Quoted 7 lines
Quoted 2 lines

Thanks

as we discussed before, the MAC address in hardware should have a "higher priority"
than the MAC address in qemu cmdline. So I remove the set_config there, the MAC address from the hardware will overwrite the MAC in qemu cmdline. so don't need to set_config to hardware now

Quoted 7 lines

Probably, but I meant it needs to be a separate patch.

For example the title said "add ...." but here it's a removal of something.

Thanks

sure will fix this

Thanks
cindy

Quoted 12 lines
Quoted 10 lines

Thanks,
cindy

Quoted 7 lines
Quoted 6 lines
hw/net/virtio-net.c +0 -1
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0

#12 lore raw
In reply to Cindy Lu
Quoted 6 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

Always post threads with a cover letter please.

Quoted 70 lines
---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +29 -4
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+				      Error **errp)
+{
+	struct virtio_net_config hwcfg = {};
+	static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+	vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.
+     */
+
+	if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+		if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+			return true;
+		}
+	}
+	error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+			 "Please check the the vdpa device's setting.");
 
+	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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+	   if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+		   virtio_cleanup(vdev);
+		   return;
+	   }
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
Signature · 1 line
2.45.0
author
#13 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Cindy Lu

When using a VDPA device, it is important to ensure that the MAC address in the hardware matches the MAC address from the QEMU command line.
This will allow the device to boot.

Always post threads with a cover letter please.

Sure,will fix this
thanks
cindy

Quoted 72 lines
Quoted 71 lines
---
 hw/net/virtio-net.c | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
hw/net/virtio-net.c +29 -5
index 9c7e85caea..7f51bd0dd3 100644
@@ -3579,12 +3579,36 @@ 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(NetClientState *nc, VirtIONet *n, MACAddr *cmdline_mac,
+                                   Error **errp)
+{
+     struct virtio_net_config hwcfg = {};
+     static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
+
+     vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&hwcfg, ETH_ALEN);
+
+    /*
+     * For VDPA device: Only two situations are acceptable:
+     * 1.The hardware MAC address is the same as the QEMU command line MAC
+     *   address, and both of them are not 0.
+     */
+
+     if (memcmp(&hwcfg.mac, &zero, sizeof(MACAddr)) != 0) {
+             if ((memcmp(&hwcfg.mac, cmdline_mac, sizeof(MACAddr)) == 0)) {
+                     return true;
+             }
+     }
+     error_setg(errp, "vDPA device's mac != the mac address from qemu cmdline"
+                      "Please check the the vdpa device's setting.");
 
+     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) {
@@ -3692,6 +3716,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;
@@ -3739,10 +3764,10 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc->rxfilter_notify_enabled = 1;
 
    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
-        struct virtio_net_config netcfg = {};
-        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);
+        if (!virtio_net_check_vdpa_mac(nc, n, &macaddr_cmdline, errp)) {
+                virtio_cleanup(vdev);
+                return;
+        }
     }
     QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
--

2.45.0