Blob Blame History Raw
From 1747bfd95ad80d2453de11e99a7bf9fa53c9aa55 Mon Sep 17 00:00:00 2001
Message-Id: <1747bfd95ad80d2453de11e99a7bf9fa53c9aa55@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Thu, 23 Nov 2017 19:02:14 +0100
Subject: [PATCH] qemu: domain: Despaghettify qemuDomainDeviceDefValidate

Move network device validation into a separate function.

(cherry picked from commit 577ccd07c35a8e9b9d682778cc7d6c1d34d891f9)

https://bugzilla.redhat.com/show_bug.cgi?id=1511480
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_domain.c | 184 ++++++++++++++++++++++++++-----------------------
 1 file changed, 97 insertions(+), 87 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 72d67adfe3..30a602a1e9 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -3553,102 +3553,112 @@ qemuDomainWatchdogDefValidate(const virDomainWatchdogDef *dev,
 }
 
 
+static int
+qemuDomainDeviceDefValidateNetwork(const virDomainNetDef *net)
+{
+    bool hasIPv4 = false;
+    bool hasIPv6 = false;
+    size_t i;
+
+    if (net->type == VIR_DOMAIN_NET_TYPE_USER) {
+        if (net->guestIP.nroutes) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("Invalid attempt to set network interface "
+                             "guest-side IP route, not supported by QEMU"));
+            return -1;
+        }
+
+        for (i = 0; i < net->guestIP.nips; i++) {
+            const virNetDevIPAddr *ip = net->guestIP.ips[i];
+
+            if (VIR_SOCKET_ADDR_VALID(&net->guestIP.ips[i]->peer)) {
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                               _("Invalid attempt to set peer IP for guest"));
+                return -1;
+            }
+
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET)) {
+                if (hasIPv4) {
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                                   _("Only one IPv4 address per "
+                                     "interface is allowed"));
+                    return -1;
+                }
+                hasIPv4 = true;
+
+                if (ip->prefix > 27) {
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
+                                   _("prefix too long"));
+                    return -1;
+                }
+            }
+
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET6)) {
+                if (hasIPv6) {
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                                   _("Only one IPv6 address per "
+                                     "interface is allowed"));
+                    return -1;
+                }
+                hasIPv6 = true;
+
+                if (ip->prefix > 120) {
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
+                                   _("prefix too long"));
+                    return -1;
+                }
+            }
+        }
+    } else if (net->guestIP.nroutes || net->guestIP.nips) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Invalid attempt to set network interface "
+                         "guest-side IP route and/or address info, "
+                         "not supported by QEMU"));
+        return -1;
+    }
+
+    if (STREQ_NULLABLE(net->model, "virtio")) {
+        if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("rx_queue_size has to be a power of two"));
+            return -1;
+        }
+        if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                           _("tx_queue_size has to be a power of two"));
+            return -1;
+        }
+    }
+
+    if (net->mtu &&
+        !qemuDomainNetSupportsMTU(net->type)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("setting MTU on interface type %s is not supported yet"),
+                       virDomainNetTypeToString(net->type));
+        return -1;
+    }
+
+    if (net->coalesce && !qemuDomainNetSupportsCoalesce(net->type)) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("coalesce settings on interface type %s are not supported"),
+                       virDomainNetTypeToString(net->type));
+        return -1;
+    }
+
+    return 0;
+}
+
+
 static int
 qemuDomainDeviceDefValidate(const virDomainDeviceDef *dev,
                             const virDomainDef *def,
                             void *opaque ATTRIBUTE_UNUSED)
 {
     int ret = -1;
-    size_t i;
 
     if (dev->type == VIR_DOMAIN_DEVICE_NET) {
-        const virDomainNetDef *net = dev->data.net;
-        bool hasIPv4 = false, hasIPv6 = false;
-
-        if (net->type == VIR_DOMAIN_NET_TYPE_USER) {
-            if (net->guestIP.nroutes) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                               _("Invalid attempt to set network interface "
-                                 "guest-side IP route, not supported by QEMU"));
-                goto cleanup;
-            }
-
-            for (i = 0; i < net->guestIP.nips; i++) {
-                const virNetDevIPAddr *ip = net->guestIP.ips[i];
-
-                if (VIR_SOCKET_ADDR_VALID(&net->guestIP.ips[i]->peer)) {
-                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                                   _("Invalid attempt to set peer IP for guest"));
-                    goto cleanup;
-                }
-
-                if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET)) {
-                    if (hasIPv4) {
-                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                                       _("Only one IPv4 address per "
-                                         "interface is allowed"));
-                        goto cleanup;
-                    }
-                    hasIPv4 = true;
-
-                    if (ip->prefix > 27) {
-                        virReportError(VIR_ERR_XML_ERROR, "%s",
-                                       _("prefix too long"));
-                        goto cleanup;
-                    }
-                }
-
-                if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET6)) {
-                    if (hasIPv6) {
-                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                                       _("Only one IPv6 address per "
-                                         "interface is allowed"));
-                        goto cleanup;
-                    }
-                    hasIPv6 = true;
-
-                    if (ip->prefix > 120) {
-                        virReportError(VIR_ERR_XML_ERROR, "%s",
-                                       _("prefix too long"));
-                        goto cleanup;
-                    }
-                }
-            }
-        } else if (net->guestIP.nroutes || net->guestIP.nips) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                           _("Invalid attempt to set network interface "
-                             "guest-side IP route and/or address info, "
-                             "not supported by QEMU"));
+        if (qemuDomainDeviceDefValidateNetwork(dev->data.net) < 0)
             goto cleanup;
-        }
-
-        if (STREQ_NULLABLE(net->model, "virtio")) {
-            if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                               _("rx_queue_size has to be a power of two"));
-                goto cleanup;
-            }
-            if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
-                               _("tx_queue_size has to be a power of two"));
-                goto cleanup;
-            }
-        }
-
-        if (net->mtu &&
-            !qemuDomainNetSupportsMTU(net->type)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("setting MTU on interface type %s is not supported yet"),
-                           virDomainNetTypeToString(net->type));
-            goto cleanup;
-        }
-
-        if (net->coalesce && !qemuDomainNetSupportsCoalesce(net->type)) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("coalesce settings on interface type %s are not supported"),
-                           virDomainNetTypeToString(net->type));
-            goto cleanup;
-        }
     } else if (dev->type == VIR_DOMAIN_DEVICE_CHR) {
         if (qemuDomainChrDefValidate(dev->data.chr, def) < 0)
             goto cleanup;
-- 
2.15.0