[PATCH 6/8] domain_conf: Move MAC generation to post parse callback

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/domain_conf.c     | 33 ++++++++++++++++++++-------------
 src/conf/domain_conf.h     |  4 +++-
 src/openvz/openvz_conf.c   |  1 +
 src/openvz/openvz_driver.c | 10 +++++++---
 src/qemu/qemu_command.c    |  7 +++----
 src/vbox/vbox_tmpl.c       |  1 +
 src/vmx/vmx.c              |  2 ++
 src/xenapi/xenapi_driver.c |  1 +
 src/xenxs/xen_sxpr.c       |  1 +
 src/xenxs/xen_xm.c         |  1 +
 tests/openvzutilstest.c    |  1 -
 11 files changed, 40 insertions(+), 22 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 7c92e51..f2a96e9 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -2802,7 +2802,8 @@ virDomainDefPostParseInternal(virDomainDefPtr def,
 static int
 virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
                                     virDomainDefPtr def,
-                                    virCapsPtr caps ATTRIBUTE_UNUSED)
+                                    virCapsPtr caps ATTRIBUTE_UNUSED,
+                                    virDomainXMLOptionPtr xmlopt)
 {
     if (dev->type == VIR_DOMAIN_DEVICE_CHR) {
         virDomainChrDefPtr chr = dev->data.chr;
@@ -2845,6 +2846,13 @@ virDomainDeviceDefPostParseInternal(virDomainDeviceDefPtr dev,
         }
     }
 
+    if (dev->type == VIR_DOMAIN_DEVICE_NET) {
+        virDomainNetDefPtr net = dev->data.net;
+
+        if (!net->macUsable)
+            virDomainNetGenerateMAC(xmlopt, net);
+    }
+
     return 0;
 }
 
@@ -2864,7 +2872,7 @@ virDomainDeviceDefPostParse(virDomainDeviceDefPtr dev,
             return ret;
     }
 
-    if ((ret = virDomainDeviceDefPostParseInternal(dev, def, caps)) < 0)
+    if ((ret = virDomainDeviceDefPostParseInternal(dev, def, caps, xmlopt)) < 0)
         return ret;
 
     return 0;
@@ -5892,9 +5900,10 @@ cleanup:
 
 void
 virDomainNetGenerateMAC(virDomainXMLOptionPtr xmlopt,
-                        virMacAddrPtr mac)
+                        virDomainNetDefPtr net)
 {
-    virMacAddrGenerate(xmlopt->config.macPrefix, mac);
+    virMacAddrGenerate(xmlopt->config.macPrefix, &net->mac);
+    net->macUsable = true;
 }
 
 
@@ -6218,8 +6227,7 @@ error:
  * @return 0 on success, -1 on failure
  */
 static virDomainNetDefPtr
-virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
-                        xmlNodePtr node,
+virDomainNetDefParseXML(xmlNodePtr node,
                         xmlXPathContextPtr ctxt,
                         virHashTablePtr bootHash,
                         unsigned int flags)
@@ -6402,8 +6410,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
                            (const char *)macaddr);
             goto error;
         }
-    } else {
-        virDomainNetGenerateMAC(xmlopt, &def->mac);
+        def->macUsable = true;
     }
 
     if (devaddr) {
@@ -9522,7 +9529,7 @@ virDomainDeviceDefParse(const char *xmlStr,
             goto error;
         break;
     case VIR_DOMAIN_DEVICE_NET:
-        if (!(dev->data.net = virDomainNetDefParseXML(xmlopt, node, ctxt,
+        if (!(dev->data.net = virDomainNetDefParseXML(node, ctxt,
                                                       NULL, flags)))
             goto error;
         break;
@@ -11851,8 +11858,7 @@ virDomainDefParseXML(xmlDocPtr xml,
     if (n && VIR_ALLOC_N(def->nets, n) < 0)
         goto error;
     for (i = 0; i < n; i++) {
-        virDomainNetDefPtr net = virDomainNetDefParseXML(xmlopt,
-                                                         nodes[i],
+        virDomainNetDefPtr net = virDomainNetDefParseXML(nodes[i],
                                                          ctxt,
                                                          bootHash,
                                                          flags);
@@ -14945,8 +14951,9 @@ virDomainNetDefFormat(virBufferPtr buf,
     virBufferAddLit(buf, ">\n");
 
     virBufferAdjustIndent(buf, 6);
-    virBufferAsprintf(buf, "<mac address='%s'/>\n",
-                      virMacAddrFormat(&def->mac, macstr));
+    if (def->macUsable)
+        virBufferAsprintf(buf, "<mac address='%s'/>\n",
+                          virMacAddrFormat(&def->mac, macstr));
 
     switch (def->type) {
     case VIR_DOMAIN_NET_TYPE_NETWORK:
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index a65ee3e..0ac3478 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -974,6 +974,7 @@ struct _virDomainActualNetDef {
 struct _virDomainNetDef {
     enum virDomainNetType type;
     virMacAddr mac;
+    bool macUsable; /* @mac has been parsed and is set */
     char *model;
     union {
         struct {
@@ -2166,7 +2167,8 @@ virDomainXMLOptionPtr virDomainXMLOptionNew(virDomainDefParserConfigPtr config,
                                             virDomainXMLPrivateDataCallbacksPtr priv,
                                             virDomainXMLNamespacePtr xmlns);
 
-void virDomainNetGenerateMAC(virDomainXMLOptionPtr xmlopt, virMacAddrPtr mac);
+void virDomainNetGenerateMAC(virDomainXMLOptionPtr xmlopt,
+                             virDomainNetDefPtr net);
 
 virDomainXMLNamespacePtr
 virDomainXMLOptionGetNamespace(virDomainXMLOptionPtr xmlopt)
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index 0dbaa4a..a95da19 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -321,6 +321,7 @@ openvzReadNetworkConf(virDomainDefPtr def,
                                        _("Wrong MAC address"));
                         goto error;
                     }
+                    net->macUsable = true;
                 }
                 p = ++next;
             } while (p < token + strlen(token));
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
index d268647..993e037 100644
--- a/src/openvz/openvz_driver.c
+++ b/src/openvz/openvz_driver.c
@@ -809,7 +809,7 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
 {
     int rc = -1;
     char macaddr[VIR_MAC_STRING_BUFLEN];
-    virMacAddr host_mac;
+    virDomainNetDefPtr hostDev = NULL;
     char host_macaddr[VIR_MAC_STRING_BUFLEN];
     struct openvz_driver *driver =  conn->privateData;
     virCommandPtr cmd = NULL;
@@ -827,11 +827,14 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
         net->type != VIR_DOMAIN_NET_TYPE_ETHERNET)
         return 0;
 
+    if (VIR_ALLOC(hostDev) < 0)
+        return rc;
+
     cmd = virCommandNewArgList(VZCTL, "--quiet", "set", vpsid, NULL);
 
     virMacAddrFormat(&net->mac, macaddr);
-    virDomainNetGenerateMAC(driver->xmlopt, &host_mac);
-    virMacAddrFormat(&host_mac, host_macaddr);
+    virDomainNetGenerateMAC(driver->xmlopt, hostDev);
+    virMacAddrFormat(&hostDev->mac, host_macaddr);
 
     if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE ||
         (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
@@ -899,6 +902,7 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
  cleanup:
     virCommandFree(cmd);
     VIR_FREE(guest_ifname);
+    virDomainNetDefFree(hostDev);
     return rc;
 }
 
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 4628dac..d83d015 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -10244,7 +10244,6 @@ qemuParseCommandLineNet(virDomainXMLOptionPtr xmlopt,
     const char *nic;
     int wantvlan = 0;
     const char *tmp;
-    bool genmac = true;
     size_t i;
 
     tmp = strchr(val, ',');
@@ -10333,7 +10332,6 @@ qemuParseCommandLineNet(virDomainXMLOptionPtr xmlopt,
 
     for (i = 0; i < nkeywords; i++) {
         if (STREQ(keywords[i], "macaddr")) {
-            genmac = false;
             if (virMacAddrParse(values[i], &def->mac) < 0) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("unable to parse mac address '%s'"),
@@ -10342,6 +10340,7 @@ qemuParseCommandLineNet(virDomainXMLOptionPtr xmlopt,
                 def = NULL;
                 goto cleanup;
             }
+            def->macUsable = true;
         } else if (STREQ(keywords[i], "model")) {
             def->model = values[i];
             values[i] = NULL;
@@ -10363,8 +10362,8 @@ qemuParseCommandLineNet(virDomainXMLOptionPtr xmlopt,
         }
     }
 
-    if (genmac)
-        virDomainNetGenerateMAC(xmlopt, &def->mac);
+    if (!def->macUsable)
+        virDomainNetGenerateMAC(xmlopt, def);
 
 cleanup:
     for (i = 0; i < nkeywords; i++) {
diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c
index 5e5ea85..66e6cbc 100644
--- a/src/vbox/vbox_tmpl.c
+++ b/src/vbox/vbox_tmpl.c
@@ -3031,6 +3031,7 @@ sharedFoldersCleanup:
                         /* XXX some real error handling here some day ... */
                         if (virMacAddrParse(macaddr, &def->nets[netAdpIncCnt]->mac) < 0)
                         {}
+                        def->nets[netAdpIncCnt]->macUsable = true;
 
                         netAdpIncCnt++;
 
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index 40416a0..8410473 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -2510,6 +2510,7 @@ virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
                                generatedAddress);
                 goto cleanup;
             }
+            (*def)->macUsable = true;
         }
     } else if (STRCASEEQ(addressType, "static")) {
         if (address != NULL) {
@@ -2519,6 +2520,7 @@ virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
                                  "found '%s'"), address_name, address);
                 goto cleanup;
             }
+            (*def)->macUsable = true;
         }
     } else {
         virReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/xenapi/xenapi_driver.c b/src/xenapi/xenapi_driver.c
index bca19af..d13e712 100644
--- a/src/xenapi/xenapi_driver.c
+++ b/src/xenapi/xenapi_driver.c
@@ -1519,6 +1519,7 @@ xenapiDomainGetXMLDesc(virDomainPtr dom, unsigned int flags)
                                     &defPtr->nets[i]->mac) < 0)
                     xenapiSessionErrorHandler(dom->conn, VIR_ERR_INTERNAL_ERROR,
                                               _("Unable to parse given mac address"));
+                defPtr->nets[i]->macUsable = true;
                 xen_vif_record_free(vif_rec);
             }
         }
diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c
index 6209c68..9d1d374 100644
--- a/src/xenxs/xen_sxpr.c
+++ b/src/xenxs/xen_sxpr.c
@@ -589,6 +589,7 @@ xenParseSxprNets(virDomainDefPtr def,
                                    _("malformed mac address '%s'"), tmp);
                     goto cleanup;
                 }
+                net->macUsable = true;
             }
 
             if (VIR_STRDUP(net->model, model) < 0)
diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
index 1ffea84..79bd952 100644
--- a/src/xenxs/xen_xm.c
+++ b/src/xenxs/xen_xm.c
@@ -743,6 +743,7 @@ xenParseXM(virConfPtr conf, int xendConfigVersion,
                                    _("malformed mac address '%s'"), mac);
                     goto cleanup;
                 }
+                net->macUsable = true;
             }
 
             if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") ||
diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c
index ee68c06..f418e50 100644
--- a/tests/openvzutilstest.c
+++ b/tests/openvzutilstest.c
@@ -94,7 +94,6 @@ testReadNetworkConf(const void *data ATTRIBUTE_UNUSED)
         "  <on_crash>destroy</on_crash>\n"
         "  <devices>\n"
         "    <interface type='ethernet'>\n"
-        "      <mac address='00:00:00:00:00:00'/>\n"
         "      <ip address='194.44.18.88'/>\n"
         "    </interface>\n"
         "    <interface type='bridge'>\n"
-- 
1.8.1.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]