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

Cindy Lu posted 4 patches 8 months ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowang@redhat.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
[PATCH v7 4/4] virtio_net: Add third acceptable configuration for MAC setup.
Posted by Cindy Lu 8 months ago
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.

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

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 808a2fe4d4..2d4ac20ee0 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -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)
-- 
2.45.0
Re: [PATCH v7 4/4] virtio_net: Add third acceptable configuration for MAC setup.
Posted by Jason Wang 8 months ago
On Thu, Apr 17, 2025 at 6:26 PM Cindy Lu <lulu@redhat.com> wrote:
>
> 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.
>
> Signed-off-by: Cindy Lu <lulu@redhat.com>
> ---
>  hw/net/virtio-net.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index 808a2fe4d4..2d4ac20ee0 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -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.

> +     *  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)
> --
> 2.45.0
>

Thanks
Re: [PATCH v7 4/4] virtio_net: Add third acceptable configuration for MAC setup.
Posted by Cindy Lu 7 months, 3 weeks ago
On Mon, Apr 21, 2025 at 11:22 AM Jason Wang <jasowang@redhat.com> wrote:
>
> On Thu, Apr 17, 2025 at 6:26 PM Cindy Lu <lulu@redhat.com> wrote:
> >
> > 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.
> >
> > Signed-off-by: Cindy Lu <lulu@redhat.com>
> > ---
> >  hw/net/virtio-net.c | 11 +++++++++++
> >  1 file changed, 11 insertions(+)
> >
> > diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> > index 808a2fe4d4..2d4ac20ee0 100644
> > --- a/hw/net/virtio-net.c
> > +++ b/hw/net/virtio-net.c
> > @@ -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

> > +     *  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)
> > --
> > 2.45.0
> >
>
> Thanks
>