File ec07aad8-libxl-normalize-mac-addr.patch of Package libvirt.16761

commit ec07aad8f8a6ead8e9363c8deec4845edb5bf112
Author: Jim Fehlig <jfehlig@suse.com>
Date:   Wed May 27 17:29:33 2020 -0600

    libxl: Normalize MAC address in device conf when hotplugging a netdev
    
    Similar to commits 55ce6564634 and 6c17606b7cc in the qemu driver, make
    separate copies of persistent and live device config and normalize the MAC
    address between the two. This avoids having different MAC address for the
    persistent and live config, ensuring the device has the same address when
    the persistent config takes affect after a VM restart.
    
    Signed-off-by: Jim Fehlig <jfehlig@suse.com>
    Reviewed-by: Laine Stump <laine@redhat.com>

Index: libvirt-5.1.0/src/libxl/libxl_driver.c
===================================================================
--- libvirt-5.1.0.orig/src/libxl/libxl_driver.c
+++ libvirt-5.1.0/src/libxl/libxl_driver.c
@@ -4066,6 +4066,31 @@ libxlDomainUpdateDeviceConfig(virDomainD
 }
 
 
+static void
+libxlDomainAttachDeviceNormalize(const virDomainDeviceDef *devConf,
+                                 virDomainDeviceDefPtr devLive)
+{
+    /*
+     * Fixup anything that needs to be identical in the live and
+     * config versions of DeviceDef, but might not be. Do this by
+     * changing the contents of devLive. This is done after all
+     * post-parse tweaks and validation, so be very careful about what
+     * changes are made.
+     */
+
+    /* MAC address should be identical in both DeviceDefs, but if it
+     * wasn't specified in the XML, and was instead autogenerated, it
+     * will be different for the two since they are each the result of
+     * a separate parser call. If it *was* specified, it will already
+     * be the same, so copying does no harm.
+     */
+
+    if (devConf->type == VIR_DOMAIN_DEVICE_NET)
+        virMacAddrSet(&devLive->data.net->mac, &devConf->data.net->mac);
+
+}
+
+
 static int
 libxlDomainAttachDeviceFlags(virDomainPtr dom, const char *xml,
                              unsigned int flags)
@@ -4074,7 +4099,9 @@ libxlDomainAttachDeviceFlags(virDomainPt
     libxlDriverConfigPtr cfg = libxlDriverConfigGet(driver);
     virDomainObjPtr vm = NULL;
     virDomainDefPtr vmdef = NULL;
-    virDomainDeviceDefPtr dev = NULL;
+    virDomainDeviceDefPtr devConf = NULL;
+    virDomainDeviceDef devConfSave = { 0 };
+    virDomainDeviceDefPtr devLive = NULL;
     int ret = -1;
 
     virCheckFlags(VIR_DOMAIN_DEVICE_MODIFY_LIVE |
@@ -4093,9 +4120,9 @@ libxlDomainAttachDeviceFlags(virDomainPt
         goto endjob;
 
     if (flags & VIR_DOMAIN_DEVICE_MODIFY_CONFIG) {
-        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
-                                            cfg->caps, driver->xmlopt,
-                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
+        if (!(devConf = virDomainDeviceDefParse(xml, vm->def,
+                                                cfg->caps, driver->xmlopt,
+                                                VIR_DOMAIN_DEF_PARSE_INACTIVE)))
             goto endjob;
 
         /* Make a copy for updated domain. */
@@ -4103,19 +4130,27 @@ libxlDomainAttachDeviceFlags(virDomainPt
                                                     driver->xmlopt)))
             goto endjob;
 
-        if (libxlDomainAttachDeviceConfig(vmdef, dev) < 0)
+        /*
+         * devConf will be NULLed out by
+         * libxlDomainAttachDeviceConfig(), so save it for later use by
+         * libxlDomainAttachDeviceNormalize()
+         */
+        devConfSave = *devConf;
+
+        if (libxlDomainAttachDeviceConfig(vmdef, devConf) < 0)
             goto endjob;
     }
 
     if (flags & VIR_DOMAIN_DEVICE_MODIFY_LIVE) {
-        /* If dev exists it was created to modify the domain config. Free it. */
-        virDomainDeviceDefFree(dev);
-        if (!(dev = virDomainDeviceDefParse(xml, vm->def,
-                                            cfg->caps, driver->xmlopt,
-                                            VIR_DOMAIN_DEF_PARSE_INACTIVE)))
+        if (!(devLive = virDomainDeviceDefParse(xml, vm->def,
+                                                cfg->caps, driver->xmlopt,
+                                                VIR_DOMAIN_DEF_PARSE_INACTIVE)))
             goto endjob;
 
-        if (libxlDomainAttachDeviceLive(driver, vm, dev) < 0)
+        if (flags & VIR_DOMAIN_AFFECT_CONFIG)
+            libxlDomainAttachDeviceNormalize(&devConfSave, devLive);
+
+        if (libxlDomainAttachDeviceLive(driver, vm, devLive) < 0)
             goto endjob;
 
         /*
@@ -4142,7 +4177,8 @@ libxlDomainAttachDeviceFlags(virDomainPt
 
  cleanup:
     virDomainDefFree(vmdef);
-    virDomainDeviceDefFree(dev);
+    virDomainDeviceDefFree(devConf);
+    virDomainDeviceDefFree(devLive);
     virDomainObjEndAPI(&vm);
     virObjectUnref(cfg);
     return ret;
openSUSE Build Service is sponsored by