Skip to contentSkip to conversation

virtio_net: Add the check for vdpa's mac address

PATCH In review Cindy Lu 3 participants 13 messages Apr 17, 2025 – Apr 28, 2025 Networking

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v7 cover virtio_net: Add the check for vdpa's mac address

When using a VDPA device, it is important to ensure that the MAC address is correctly set. We add a new parameter to enable this check in this patch series.
Only three MAC configurations are acceptable; any others will fail to boot

The usage is:
....
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=true\
-device virtio-net-pci,netdev=vhost-vdpa0\
....

tested by ConnectX-6 Dx/vdpa_sim device

change in v3
1. Add a new parameter to enable the check and keep the old behavior
2. Adjust the comment and make it clearer

change in v4
1. Change the new parameter's name to check-mac
2. Change the comment and make it clearer

change in v5
1. These patches haven't been merged for a while, so I rebased them with the latest code and resubmitted

change in v6
1. Address comments
2. Rebase with the latest QEMU

change in v7
1. Enable this check by default
2. Address comments and rebase with the latest QEMU

Cindy Lu (4):
vhost_vdpa : Add a new parameter to enable check mac address
virtio_net: Add the check for vdpa's mac address
virtio_net: Add second acceptable configuration for MAC setup
virtio_net: Add third acceptable configuration for MAC setup.

 hw/net/virtio-net.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-
 include/net/net.h   |  1 +
 net/vhost-vdpa.c    |  5 ++++
 qapi/net.json       |  5 ++++
 4 files changed, 75 insertions(+), 1 deletion(-)
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH v7 1/4 vhost_vdpa : Add a new parameter to enable check mac address

When using a VDPA device, it's important to ensure that the MAC address is correctly set.
This patch adds a new QEMU command line parameter to enable MAC address verification, which is enabled by default.

Usage example:
....
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=true\
-device virtio-net-pci,netdev=vhost-vdpa0\
....
To disable this check:
....
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=false\
-device virtio-net-pci,netdev=vhost-vdpa0\
....

---
 include/net/net.h | 1 +
 net/vhost-vdpa.c  | 5 +++++
 qapi/net.json     | 5 +++++
 3 files changed, 11 insertions(+)
include/net/net.h +1 -0
index cdd5b109b0..fac1951b6e 100644
@@ -112,6 +112,7 @@ struct NetClientState {
     bool is_netdev;
     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
     bool is_datapath;
+    bool check_mac;
     QTAILQ_HEAD(, NetFilterState) filters;
 };
 
net/vhost-vdpa.c +5 -0
index 7ca8b46eee..7dbe6cf65c 100644
@@ -1871,6 +1871,11 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
         if (!ncs[i])
             goto err;
     }
+    /* Enable the mac check by default */
+    if (opts->has_check_mac)
+        ncs[0]->check_mac = opts->check_mac;
+    else
+        ncs[0]->check_mac = true;
 
     if (has_cvq) {
         VhostVDPAState *s0 = DO_UPCAST(VhostVDPAState, nc, ncs[0]);
qapi/net.json +5 -0
index 310cc4fd19..0607c83833 100644
@@ -510,6 +510,10 @@
 # @queues: number of queues to be created for multiqueue vhost-vdpa
 #     (default: 1)
 #
+# @check-mac: Enable the check for whether the device's MAC address
+#     and the MAC in QEMU command line are acceptable for booting.
+#     (default: true)
+#
 # @x-svq: Start device with (experimental) shadow virtqueue.  (Since
 #     7.1) (default: false)
 #
@@ -524,6 +528,7 @@
     '*vhostdev':     'str',
     '*vhostfd':      'str',
     '*queues':       'int',
+    '*check-mac':    'bool',
     '*x-svq':        {'type': 'bool', 'features' : [ 'unstable'] } } }
 
 ##
Signature · 1 line
2.45.0
Tested-by on v7 1/4
#2 lore raw

Re: [PATCH v7 1/4] vhost_vdpa : Add a new parameter to enable check mac address

I tested this series of patches v7 with virtio-net regression tests, everything works fine.

Quoted 79 lines · Cindy Lu

When using a VDPA device, it's important to ensure that the MAC address is correctly set.
This patch adds a new QEMU command line parameter to enable MAC address verification, which is enabled by default.

Usage example:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=true\
-device virtio-net-pci,netdev=vhost-vdpa0\
....

To disable this check:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=false\
-device virtio-net-pci,netdev=vhost-vdpa0\
....
---
 include/net/net.h | 1 +
 net/vhost-vdpa.c  | 5 +++++
 qapi/net.json     | 5 +++++
 3 files changed, 11 insertions(+)
include/net/net.h +1 -0
index cdd5b109b0..fac1951b6e 100644
@@ -112,6 +112,7 @@ struct NetClientState {
     bool is_netdev;
     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
     bool is_datapath;
+    bool check_mac;
     QTAILQ_HEAD(, NetFilterState) filters;
 };
 
net/vhost-vdpa.c +5 -0
index 7ca8b46eee..7dbe6cf65c 100644
@@ -1871,6 +1871,11 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
         if (!ncs[i])
             goto err;
     }
+    /* Enable the mac check by default */
+    if (opts->has_check_mac)
+        ncs[0]->check_mac = opts->check_mac;
+    else
+        ncs[0]->check_mac = true;
 
     if (has_cvq) {
         VhostVDPAState *s0 = DO_UPCAST(VhostVDPAState, nc, ncs[0]);
qapi/net.json +5 -1
index 310cc4fd19..0607c83833 100644
@@ -510,6 +510,10 @@
 # @queues: number of queues to be created for multiqueue vhost-vdpa
 #     (default: 1)
 #
+# @check-mac: Enable the check for whether the device's MAC address
+#     and the MAC in QEMU command line are acceptable for booting.
+#     (default: true)
+#
 # @x-svq: Start device with (experimental) shadow virtqueue.  (Since
 #     7.1) (default: false)
 #
@@ -524,6 +528,7 @@
     '*vhostdev':     'str',
     '*vhostfd':      'str',
     '*queues':       'int',
+    '*check-mac':    'bool',
     '*x-svq':        {'type': 'bool', 'features' : [ 'unstable'] } } }
 
 ##
--

2.45.0

#3 lore raw

Re: [PATCH v7 1/4] vhost_vdpa : Add a new parameter to enable check mac address

In reply to Cindy Lu
Quoted 17 lines · Cindy Lu

When using a VDPA device, it's important to ensure that the MAC address is correctly set.
This patch adds a new QEMU command line parameter to enable MAC address verification, which is enabled by default.

Usage example:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=true\
-device virtio-net-pci,netdev=vhost-vdpa0\
....

To disable this check:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=false\
-device virtio-net-pci,netdev=vhost-vdpa0\
....
Quoted 1 line

This patch needs to be squashed to the patch that implements the real logic of the checking.

Quoted 23 lines
---
 include/net/net.h | 1 +
 net/vhost-vdpa.c  | 5 +++++
 qapi/net.json     | 5 +++++
 3 files changed, 11 insertions(+)
include/net/net.h +1 -0
index cdd5b109b0..fac1951b6e 100644
@@ -112,6 +112,7 @@ struct NetClientState {
     bool is_netdev;
     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
     bool is_datapath;
+    bool check_mac;
     QTAILQ_HEAD(, NetFilterState) filters;
 };
 
net/vhost-vdpa.c +0 -0
index 7ca8b46eee..7dbe6cf65c 100644

net/vhost-vdpa.c int net_init_vhost_vdpa(const Netdev *netdev, const char *name, · line 1878

Quoted 8 lines
net/vhost-vdpa.c +5 -0
@@ -1871,6 +1871,11 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
         if (!ncs[i])
             goto err;
     }
+    /* Enable the mac check by default */
+    if (opts->has_check_mac)
+        ncs[0]->check_mac = opts->check_mac;
+    else
+        ncs[0]->check_mac = true;

How could we maintain the migration compatibility here?

Quoted 27 lines
net/vhost-vdpa.c +0 -0
     if (has_cvq) {
         VhostVDPAState *s0 = DO_UPCAST(VhostVDPAState, nc, ncs[0]);
qapi/net.json +5 -1
index 310cc4fd19..0607c83833 100644
@@ -510,6 +510,10 @@
 # @queues: number of queues to be created for multiqueue vhost-vdpa
 #     (default: 1)
 #
+# @check-mac: Enable the check for whether the device's MAC address
+#     and the MAC in QEMU command line are acceptable for booting.
+#     (default: true)
+#
 # @x-svq: Start device with (experimental) shadow virtqueue.  (Since
 #     7.1) (default: false)
 #
@@ -524,6 +528,7 @@
     '*vhostdev':     'str',
     '*vhostfd':      'str',
     '*queues':       'int',
+    '*check-mac':    'bool',
     '*x-svq':        {'type': 'bool', 'features' : [ 'unstable'] } } }
 
 ##
--
Quoted 2 lines

2.45.0

Thanks

#4 lore raw

Re: [PATCH v7 1/4] vhost_vdpa : Add a new parameter to enable check mac address

Quoted 30 lines · Jason Wang
Quoted 17 lines · Cindy Lu

When using a VDPA device, it's important to ensure that the MAC address is correctly set.
This patch adds a new QEMU command line parameter to enable MAC address verification, which is enabled by default.

Usage example:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=true\
-device virtio-net-pci,netdev=vhost-vdpa0\
....

To disable this check:
....

(quoted diff) +0 -2
-netdev type=vhost-vdpa,vhostdev=/dev/vhost-vdpa-0,id=vhost-vdpa0,check-mac=false\
-device virtio-net-pci,netdev=vhost-vdpa0\
....
Quoted 1 line

This patch needs to be squashed to the patch that implements the real logic of the checking.

Quoted 6 lines
---
 include/net/net.h | 1 +
 net/vhost-vdpa.c  | 5 +++++
 qapi/net.json     | 5 +++++
 3 files changed, 11 insertions(+)
Quoted 9 lines

include/net/net.h struct NetClientState { · line 115

Quoted 9 lines
include/net/net.h +1 -0
index cdd5b109b0..fac1951b6e 100644
@@ -112,6 +112,7 @@ struct NetClientState {
     bool is_netdev;
     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
     bool is_datapath;
+    bool check_mac;

Rethink of this, it is vDPA specific, we probably need to move it to VhostVDPAState instead.

Thanks

author
#5 lore raw

PATCH v7 2/4 virtio_net: Add the check for vdpa's mac address

In reply to Cindy Lu

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.

---
 hw/net/virtio-net.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +39 -1
index bd37651dab..0b6a4e5634 100644
@@ -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);
Signature · 1 line
2.45.0
#6 lore raw
Quoted 73 lines · Cindy Lu

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.

---
 hw/net/virtio-net.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +32 -1
index bd37651dab..0b6a4e5634 100644
@@ -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],

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

Quoted 10 lines
hw/net/virtio-net.c +7 -0
+                           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);

If the mac check passes, do we still need to go here to override the config space which is tricky as some parents doesn't even support set config.

Thanks

Quoted 3 lines

--
2.45.0

author
#7 lore raw

PATCH v7 3/4 virtio_net: Add second acceptable configuration for MAC setup

In reply to Cindy Lu

For VDPA devices, Allow configurations where the hardware MAC address is non-zero while the MAC address in the QEMU command line is zero. In this case, QEMU will use the hardware MAC address from the device.

---
 hw/net/virtio-net.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/net/virtio-net.c +14 -0
index 0b6a4e5634..808a2fe4d4 100644
@@ -3757,6 +3757,20 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
         if ((memcmp(hwmac, cmdline_mac, sizeof(MACAddr)) == 0)) {
             return true;
         }
+        /*
+         * 2. The hardware MAC address is NOT 0 and the MAC address in
+         *  the QEMU command line is 0.
+         *  In this case,qemu will use the hardware MAC address overwrite
+         *  the QEMU command line MAC address saved in VirtIONet->mac[0].
+         *  In the following process, QEMU will use this MAC address
+         *  to complete the initialization.
+         */
+        if (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0) {
+            /* overwrite the mac address with hardware address */
+            memcpy(&n->mac[0], hwmac, sizeof(n->mac));
+            memcpy(&n->nic_conf.macaddr, hwmac, sizeof(n->mac));
+            return true;
+        }
     }
 
     return false;
Signature · 1 line
2.45.0
#8 lore raw

Re: [PATCH v7 3/4] virtio_net: Add second acceptable configuration for MAC setup

Quoted 20 lines · Cindy Lu

For VDPA devices, Allow configurations where the hardware MAC address is non-zero while the MAC address in the QEMU command line is zero. In this case, QEMU will use the hardware MAC address from the device.

---
 hw/net/virtio-net.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/net/virtio-net.c +2 -0
index 0b6a4e5634..808a2fe4d4 100644
@@ -3757,6 +3757,20 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
         if ((memcmp(hwmac, cmdline_mac, sizeof(MACAddr)) == 0)) {
             return true;
         }
+        /*
+         * 2. The hardware MAC address is NOT 0 and the MAC address in

hw/net/virtio-net.c static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac, · line 3771

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+         *  the QEMU command line is 0.
+         *  In this case,qemu will use the hardware MAC address overwrite
+         *  the QEMU command line MAC address saved in VirtIONet->mac[0].
+         *  In the following process, QEMU will use this MAC address
+         *  to complete the initialization.
+         */
+        if (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0) {
+            /* overwrite the mac address with hardware address */
+            memcpy(&n->mac[0], hwmac, sizeof(n->mac));
+            memcpy(&n->nic_conf.macaddr, hwmac, sizeof(n->mac));

I'm not sure I get here, shouldn't we need to call get_config() here?

Thanks

Quoted 8 lines
hw/net/virtio-net.c +2 -1
+            return true;
+        }
     }
 
     return false;
--

2.45.0

author
#9 lore raw

Re: [PATCH v7 3/4] virtio_net: Add second acceptable configuration for MAC setup

Quoted 35 lines · Jason Wang
Quoted 20 lines · Cindy Lu

For VDPA devices, Allow configurations where the hardware MAC address is non-zero while the MAC address in the QEMU command line is zero. In this case, QEMU will use the hardware MAC address from the device.

---
 hw/net/virtio-net.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/net/virtio-net.c +2 -0
index 0b6a4e5634..808a2fe4d4 100644
@@ -3757,6 +3757,20 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
         if ((memcmp(hwmac, cmdline_mac, sizeof(MACAddr)) == 0)) {
             return true;
         }
+        /*
+         * 2. The hardware MAC address is NOT 0 and the MAC address in

hw/net/virtio-net.c static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac, · line 3771

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+         *  the QEMU command line is 0.
+         *  In this case,qemu will use the hardware MAC address overwrite
+         *  the QEMU command line MAC address saved in VirtIONet->mac[0].
+         *  In the following process, QEMU will use this MAC address
+         *  to complete the initialization.
+         */
+        if (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0) {
+            /* overwrite the mac address with hardware address */
+            memcpy(&n->mac[0], hwmac, sizeof(n->mac));
+            memcpy(&n->nic_conf.macaddr, hwmac, sizeof(n->mac));

I'm not sure I get here, shouldn't we need to call get_config() here?

Quoted 2 lines

Thanks

I made a small change to this function: get_config() is called in virtio_net_device_realize() before check_mac(), so we don't need to call it again.
Thanks
Cindy

Quoted 9 lines
Quoted 8 lines
hw/net/virtio-net.c +2 -1
+            return true;
+        }
     }
 
     return false;
--

2.45.0

author
#10 lore raw

PATCH v7 4/4 virtio_net: Add third acceptable configuration for MAC setup.

In reply to Cindy Lu

For VDPA devices, Allow configurations where both the hardware MAC address and QEMU command line MAC address are zero.
In this case, QEMU will automatically generate a random MAC address and assign it to the hardware, and the VM will use this random MAC address as its MAC address.

---
 hw/net/virtio-net.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/net/virtio-net.c +11 -0
index 808a2fe4d4..2d4ac20ee0 100644
@@ -3772,7 +3772,18 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
             return true;
         }
     }
+    /*
+     * 3. The hardware MAC address is 0,
+     *  and the MAC address in the QEMU command line is also 0.
+     *  In this situation, QEMU generates a random MAC address and
+     *  uses CVQ/set_config to assign it to the device.
+     */
+    if ((memcmp(hwmac, &zero, sizeof(MACAddr)) == 0) &&
+        (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0)) {
+        memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
 
+        return true;
+    }
     return false;
 }
 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
Signature · 1 line
2.45.0
#11 lore raw

Re: [PATCH v7 4/4] virtio_net: Add third acceptable configuration for MAC setup.

Quoted 17 lines · Cindy Lu

For VDPA devices, Allow configurations where both the hardware MAC address and QEMU command line MAC address are zero.
In this case, QEMU will automatically generate a random MAC address and assign it to the hardware, and the VM will use this random MAC address as its MAC address.

---
 hw/net/virtio-net.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/net/virtio-net.c +0 -0
index 808a2fe4d4..2d4ac20ee0 100644

hw/net/virtio-net.c static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac, · line 3777

Quoted 6 lines
hw/net/virtio-net.c +3 -0
@@ -3772,7 +3772,18 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
             return true;
         }
     }
+    /*
+     * 3. The hardware MAC address is 0,
+     *  and the MAC address in the QEMU command line is also 0.

Can this happen? I'd expect qemu should check for invalid mac addresses.

Quoted 13 lines
hw/net/virtio-net.c +8 -1
+     *  In this situation, QEMU generates a random MAC address and
+     *  uses CVQ/set_config to assign it to the device.
+     */
+    if ((memcmp(hwmac, &zero, sizeof(MACAddr)) == 0) &&
+        (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0)) {
+        memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
 
+        return true;
+    }
     return false;
 }
 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
--
Quoted 2 lines

2.45.0

Thanks

author
#12 lore raw

Re: [PATCH v7 4/4] virtio_net: Add third acceptable configuration for MAC setup.

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

For VDPA devices, Allow configurations where both the hardware MAC address and QEMU command line MAC address are zero.
In this case, QEMU will automatically generate a random MAC address and assign it to the hardware, and the VM will use this random MAC address as its MAC address.

---
 hw/net/virtio-net.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
Quoted 14 lines

hw/net/virtio-net.c static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac, · line 3777

Quoted 11 lines
hw/net/virtio-net.c +3 -0
index 808a2fe4d4..2d4ac20ee0 100644
@@ -3772,7 +3772,18 @@ static bool virtio_net_check_vdpa_mac(VirtIONet *n, uint8_t *hwmac,
             return true;
         }
     }
+    /*
+     * 3. The hardware MAC address is 0,
+     *  and the MAC address in the QEMU command line is also 0.

Can this happen? I'd expect qemu should check for invalid mac addresses.

If the QEMU command line does not specify the MAC address, it passes a value of 0 to this function. Therefore, we may not need to add a check here, as this value will be replaced with a random MAC address in the subsequent process.
thanks
Cindy

Quoted 18 lines
Quoted 13 lines
hw/net/virtio-net.c +8 -1
+     *  In this situation, QEMU generates a random MAC address and
+     *  uses CVQ/set_config to assign it to the device.
+     */
+    if ((memcmp(hwmac, &zero, sizeof(MACAddr)) == 0) &&
+        (memcmp(cmdline_mac, &zero, sizeof(MACAddr)) == 0)) {
+        memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
 
+        return true;
+    }
     return false;
 }
 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
--
Quoted 2 lines

2.45.0

Thanks