404507
From 1747bfd95ad80d2453de11e99a7bf9fa53c9aa55 Mon Sep 17 00:00:00 2001
404507
Message-Id: <1747bfd95ad80d2453de11e99a7bf9fa53c9aa55@dist-git>
404507
From: Peter Krempa <pkrempa@redhat.com>
404507
Date: Thu, 23 Nov 2017 19:02:14 +0100
404507
Subject: [PATCH] qemu: domain: Despaghettify qemuDomainDeviceDefValidate
404507
404507
Move network device validation into a separate function.
404507
404507
(cherry picked from commit 577ccd07c35a8e9b9d682778cc7d6c1d34d891f9)
404507
404507
https://bugzilla.redhat.com/show_bug.cgi?id=1511480
404507
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
404507
---
404507
 src/qemu/qemu_domain.c | 184 ++++++++++++++++++++++++++-----------------------
404507
 1 file changed, 97 insertions(+), 87 deletions(-)
404507
404507
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
404507
index 72d67adfe3..30a602a1e9 100644
404507
--- a/src/qemu/qemu_domain.c
404507
+++ b/src/qemu/qemu_domain.c
404507
@@ -3553,102 +3553,112 @@ qemuDomainWatchdogDefValidate(const virDomainWatchdogDef *dev,
404507
 }
404507
 
404507
 
404507
+static int
404507
+qemuDomainDeviceDefValidateNetwork(const virDomainNetDef *net)
404507
+{
404507
+    bool hasIPv4 = false;
404507
+    bool hasIPv6 = false;
404507
+    size_t i;
404507
+
404507
+    if (net->type == VIR_DOMAIN_NET_TYPE_USER) {
404507
+        if (net->guestIP.nroutes) {
404507
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                           _("Invalid attempt to set network interface "
404507
+                             "guest-side IP route, not supported by QEMU"));
404507
+            return -1;
404507
+        }
404507
+
404507
+        for (i = 0; i < net->guestIP.nips; i++) {
404507
+            const virNetDevIPAddr *ip = net->guestIP.ips[i];
404507
+
404507
+            if (VIR_SOCKET_ADDR_VALID(&net->guestIP.ips[i]->peer)) {
404507
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                               _("Invalid attempt to set peer IP for guest"));
404507
+                return -1;
404507
+            }
404507
+
404507
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET)) {
404507
+                if (hasIPv4) {
404507
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                                   _("Only one IPv4 address per "
404507
+                                     "interface is allowed"));
404507
+                    return -1;
404507
+                }
404507
+                hasIPv4 = true;
404507
+
404507
+                if (ip->prefix > 27) {
404507
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                                   _("prefix too long"));
404507
+                    return -1;
404507
+                }
404507
+            }
404507
+
404507
+            if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET6)) {
404507
+                if (hasIPv6) {
404507
+                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                                   _("Only one IPv6 address per "
404507
+                                     "interface is allowed"));
404507
+                    return -1;
404507
+                }
404507
+                hasIPv6 = true;
404507
+
404507
+                if (ip->prefix > 120) {
404507
+                    virReportError(VIR_ERR_XML_ERROR, "%s",
404507
+                                   _("prefix too long"));
404507
+                    return -1;
404507
+                }
404507
+            }
404507
+        }
404507
+    } else if (net->guestIP.nroutes || net->guestIP.nips) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                       _("Invalid attempt to set network interface "
404507
+                         "guest-side IP route and/or address info, "
404507
+                         "not supported by QEMU"));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (STREQ_NULLABLE(net->model, "virtio")) {
404507
+        if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
404507
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                           _("rx_queue_size has to be a power of two"));
404507
+            return -1;
404507
+        }
404507
+        if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
404507
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
+                           _("tx_queue_size has to be a power of two"));
404507
+            return -1;
404507
+        }
404507
+    }
404507
+
404507
+    if (net->mtu &&
404507
+        !qemuDomainNetSupportsMTU(net->type)) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("setting MTU on interface type %s is not supported yet"),
404507
+                       virDomainNetTypeToString(net->type));
404507
+        return -1;
404507
+    }
404507
+
404507
+    if (net->coalesce && !qemuDomainNetSupportsCoalesce(net->type)) {
404507
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
+                       _("coalesce settings on interface type %s are not supported"),
404507
+                       virDomainNetTypeToString(net->type));
404507
+        return -1;
404507
+    }
404507
+
404507
+    return 0;
404507
+}
404507
+
404507
+
404507
 static int
404507
 qemuDomainDeviceDefValidate(const virDomainDeviceDef *dev,
404507
                             const virDomainDef *def,
404507
                             void *opaque ATTRIBUTE_UNUSED)
404507
 {
404507
     int ret = -1;
404507
-    size_t i;
404507
 
404507
     if (dev->type == VIR_DOMAIN_DEVICE_NET) {
404507
-        const virDomainNetDef *net = dev->data.net;
404507
-        bool hasIPv4 = false, hasIPv6 = false;
404507
-
404507
-        if (net->type == VIR_DOMAIN_NET_TYPE_USER) {
404507
-            if (net->guestIP.nroutes) {
404507
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                               _("Invalid attempt to set network interface "
404507
-                                 "guest-side IP route, not supported by QEMU"));
404507
-                goto cleanup;
404507
-            }
404507
-
404507
-            for (i = 0; i < net->guestIP.nips; i++) {
404507
-                const virNetDevIPAddr *ip = net->guestIP.ips[i];
404507
-
404507
-                if (VIR_SOCKET_ADDR_VALID(&net->guestIP.ips[i]->peer)) {
404507
-                    virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                                   _("Invalid attempt to set peer IP for guest"));
404507
-                    goto cleanup;
404507
-                }
404507
-
404507
-                if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET)) {
404507
-                    if (hasIPv4) {
404507
-                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                                       _("Only one IPv4 address per "
404507
-                                         "interface is allowed"));
404507
-                        goto cleanup;
404507
-                    }
404507
-                    hasIPv4 = true;
404507
-
404507
-                    if (ip->prefix > 27) {
404507
-                        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
-                                       _("prefix too long"));
404507
-                        goto cleanup;
404507
-                    }
404507
-                }
404507
-
404507
-                if (VIR_SOCKET_ADDR_IS_FAMILY(&ip->address, AF_INET6)) {
404507
-                    if (hasIPv6) {
404507
-                        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                                       _("Only one IPv6 address per "
404507
-                                         "interface is allowed"));
404507
-                        goto cleanup;
404507
-                    }
404507
-                    hasIPv6 = true;
404507
-
404507
-                    if (ip->prefix > 120) {
404507
-                        virReportError(VIR_ERR_XML_ERROR, "%s",
404507
-                                       _("prefix too long"));
404507
-                        goto cleanup;
404507
-                    }
404507
-                }
404507
-            }
404507
-        } else if (net->guestIP.nroutes || net->guestIP.nips) {
404507
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                           _("Invalid attempt to set network interface "
404507
-                             "guest-side IP route and/or address info, "
404507
-                             "not supported by QEMU"));
404507
+        if (qemuDomainDeviceDefValidateNetwork(dev->data.net) < 0)
404507
             goto cleanup;
404507
-        }
404507
-
404507
-        if (STREQ_NULLABLE(net->model, "virtio")) {
404507
-            if (net->driver.virtio.rx_queue_size & (net->driver.virtio.rx_queue_size - 1)) {
404507
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                               _("rx_queue_size has to be a power of two"));
404507
-                goto cleanup;
404507
-            }
404507
-            if (net->driver.virtio.tx_queue_size & (net->driver.virtio.tx_queue_size - 1)) {
404507
-                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
404507
-                               _("tx_queue_size has to be a power of two"));
404507
-                goto cleanup;
404507
-            }
404507
-        }
404507
-
404507
-        if (net->mtu &&
404507
-            !qemuDomainNetSupportsMTU(net->type)) {
404507
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
-                           _("setting MTU on interface type %s is not supported yet"),
404507
-                           virDomainNetTypeToString(net->type));
404507
-            goto cleanup;
404507
-        }
404507
-
404507
-        if (net->coalesce && !qemuDomainNetSupportsCoalesce(net->type)) {
404507
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
404507
-                           _("coalesce settings on interface type %s are not supported"),
404507
-                           virDomainNetTypeToString(net->type));
404507
-            goto cleanup;
404507
-        }
404507
     } else if (dev->type == VIR_DOMAIN_DEVICE_CHR) {
404507
         if (qemuDomainChrDefValidate(dev->data.chr, def) < 0)
404507
             goto cleanup;
404507
-- 
404507
2.15.0
404507