Commit c9a641 (first appearred in 1.2.12) added support for setting
the guest-side IP address of veth devices in lxc domains, and commit
6e244c (not yet in a release) used that as a model to allow setting
the IP address of tap devices in qemu domains.  Unfortunately, the
original hardcoded the assumption that the proper prefix for any IP
address with no explicit prefix in the config should be "24"; that is
only correct for class C IPv4 addresses, but not for any other IPv4
address, nor for any IPv6 address.

The good news is that there is already a function in libvirt that will
determine the proper default prefix for any IP address. This patch
replaces the two uses of the ill-fated VIR_SOCKET_ADDR_DEFAULT_PREFIX
with calls to virSocketAddrGetIpPrefix().
---
 src/lxc/lxc_container.c   | 15 +++++++++++----
 src/qemu/qemu_interface.c | 13 ++++++++++---
 src/util/virsocketaddr.h  |  3 +--
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index a1deb0c..15dacf1 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2015 Red Hat, Inc.
+ * Copyright (C) 2008-2016 Red Hat, Inc.
  * Copyright (C) 2008 IBM Corp.
  * Copyright (c) 2015 SUSE LINUX Products GmbH, Nuernberg, Germany.
  *
@@ -514,11 +514,18 @@ static int 
lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
 
         for (j = 0; j < netDef->nips; j++) {
             virDomainNetIpDefPtr ip = netDef->ips[j];
-            unsigned int prefix = (ip->prefix > 0) ? ip->prefix :
-                                  VIR_SOCKET_ADDR_DEFAULT_PREFIX;
+            int prefix;
             char *ipStr = virSocketAddrFormat(&ip->address);
 
-            VIR_DEBUG("Adding IP address '%s/%u' to '%s'",
+            if ((prefix = virSocketAddrGetIpPrefix(&ip->address,
+                                                   NULL, ip->prefix)) < 0) {
+                virReportError(VIR_ERR_INTERNAL_ERROR,
+                               _("Failed to determine prefix for IP address 
'%s'"),
+                               ipStr);
+                goto error_out;
+            }
+
+            VIR_DEBUG("Adding IP address '%s/%d' to '%s'",
                       ipStr, ip->prefix, newname);
             if (virNetDevSetIPAddress(newname, &ip->address, &ip->peer, 
prefix) < 0) {
                 virReportError(VIR_ERR_SYSTEM_ERROR,
diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c
index a4e9d86..34fe30e 100644
--- a/src/qemu/qemu_interface.c
+++ b/src/qemu/qemu_interface.c
@@ -450,11 +450,18 @@ qemuInterfaceEthernetConnect(virDomainDefPtr def,
 
     for (i = 0; i < net->nips; i++) {
         virDomainNetIpDefPtr ip = net->ips[i];
-        unsigned int prefix = (ip->prefix > 0) ? ip->prefix :
-            VIR_SOCKET_ADDR_DEFAULT_PREFIX;
+        int prefix;
         char *ipStr = virSocketAddrFormat(&ip->address);
 
-        VIR_DEBUG("Adding IP address '%s/%u' to '%s'",
+        if ((prefix = virSocketAddrGetIpPrefix(&ip->address,
+                                               NULL, ip->prefix)) < 0) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           _("Failed to determine prefix for IP address '%s'"),
+                           ipStr);
+            goto cleanup;
+        }
+
+        VIR_DEBUG("Adding IP address '%s/%d' to '%s'",
                   ipStr, ip->prefix, net->ifname);
 
         if (virNetDevSetIPAddress(net->ifname, &ip->address, &ip->peer, 
prefix) < 0) {
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index c7aaa61..a7bec5d 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2013, 2015 Red Hat, Inc.
+ * Copyright (C) 2009-2013, 2015-2016 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -54,7 +54,6 @@ typedef struct {
 # define VIR_SOCKET_ADDR_FAMILY(s)              \
     ((s)->data.sa.sa_family)
 
-# define VIR_SOCKET_ADDR_DEFAULT_PREFIX 24
 # define VIR_SOCKET_ADDR_IPV4_ALL "0.0.0.0"
 # define VIR_SOCKET_ADDR_IPV6_ALL "::"
 
-- 
2.5.5

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

Reply via email to