[PATCH 14/28] util: move IP route & address object-related functions to virnetdevip.c

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

 



These functions all need to be called from a utility function that
must be located in the util directory, so we move them all into
util/virnetdevip.[ch] now that it exists.

Function and struct names were appropriately changed for the new
location, but all code is unchanged aside from motion and renaming.
---
 src/conf/domain_conf.c            |  36 ++++++-------
 src/conf/domain_conf.h            |  16 ++----
 src/conf/network_conf.c           |  16 +++---
 src/conf/network_conf.h           |   4 +-
 src/conf/networkcommon_conf.c     | 107 ++++----------------------------------
 src/conf/networkcommon_conf.h     |  55 +++++++-------------
 src/libvirt_private.syms          |  16 +++---
 src/lxc/lxc_container.c           |  12 ++---
 src/lxc/lxc_native.c              |  12 ++---
 src/network/bridge_driver.c       |  14 ++---
 src/network/bridge_driver_linux.c |   6 +--
 src/util/virnetdevip.c            |  69 ++++++++++++++++++++++++
 src/util/virnetdevip.h            |  29 +++++++++++
 13 files changed, 191 insertions(+), 201 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 4802e03..f380271 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1806,7 +1806,7 @@ virDomainNetDefClear(virDomainNetDefPtr def)
     VIR_FREE(def->ips);
 
     for (i = 0; i < def->nroutes; i++)
-        virNetworkRouteDefFree(def->routes[i]);
+        virNetDevIPRouteFree(def->routes[i]);
     VIR_FREE(def->routes);
 
     virDomainDeviceInfoClear(&def->info);
@@ -2214,7 +2214,7 @@ void virDomainHostdevDefClear(virDomainHostdevDefPtr def)
                 VIR_FREE(def->source.caps.u.net.ips[i]);
             VIR_FREE(def->source.caps.u.net.ips);
             for (i = 0; i < def->source.caps.u.net.nroutes; i++)
-                virNetworkRouteDefFree(def->source.caps.u.net.routes[i]);
+                virNetDevIPRouteFree(def->source.caps.u.net.routes[i]);
             VIR_FREE(def->source.caps.u.net.routes);
             break;
         }
@@ -6115,11 +6115,11 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
     return ret;
 }
 
-static virDomainNetIPDefPtr
+static virNetDevIPAddrPtr
 virDomainNetIPParseXML(xmlNodePtr node)
 {
     /* Parse the prefix in every case */
-    virDomainNetIPDefPtr ip = NULL, ret = NULL;
+    virNetDevIPAddrPtr ip = NULL, ret = NULL;
     char *prefixStr = NULL;
     unsigned int prefixValue = 0;
     char *familyStr = NULL;
@@ -6246,7 +6246,7 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED,
         if (nipnodes) {
             size_t i;
             for (i = 0; i < nipnodes; i++) {
-                virDomainNetIPDefPtr ip = virDomainNetIPParseXML(ipnodes[i]);
+                virNetDevIPAddrPtr ip = virDomainNetIPParseXML(ipnodes[i]);
 
                 if (!ip)
                     goto error;
@@ -6266,9 +6266,9 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED,
         if (nroutenodes) {
             size_t i;
             for (i = 0; i < nroutenodes; i++) {
-                virNetworkRouteDefPtr route = NULL;
+                virNetDevIPRoutePtr route = NULL;
 
-                if (!(route = virNetworkRouteDefParseXML(_("Domain hostdev device"),
+                if (!(route = virNetDevIPRouteParseXML(_("Domain hostdev device"),
                                                          routenodes[i],
                                                          ctxt)))
                     goto error;
@@ -6276,7 +6276,7 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED,
 
                 if (VIR_APPEND_ELEMENT(def->source.caps.u.net.routes,
                                        def->source.caps.u.net.nroutes, route) < 0) {
-                    virNetworkRouteDefFree(route);
+                    virNetDevIPRouteFree(route);
                     goto error;
                 }
             }
@@ -8883,7 +8883,7 @@ virDomainNetAppendIPAddress(virDomainNetDefPtr def,
                             int family,
                             unsigned int prefix)
 {
-    virDomainNetIPDefPtr ipDef = NULL;
+    virNetDevIPAddrPtr ipDef = NULL;
     if (VIR_ALLOC(ipDef) < 0)
         return -1;
 
@@ -8955,9 +8955,9 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
     int ret, val;
     size_t i;
     size_t nips = 0;
-    virDomainNetIPDefPtr *ips = NULL;
+    virNetDevIPAddrPtr *ips = NULL;
     size_t nroutes = 0;
-    virNetworkRouteDefPtr *routes = NULL;
+    virNetDevIPRoutePtr *routes = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
@@ -9074,7 +9074,7 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
                     ctxt->node = tmpnode;
                 }
             } else if (xmlStrEqual(cur->name, BAD_CAST "ip")) {
-                virDomainNetIPDefPtr ip = NULL;
+                virNetDevIPAddrPtr ip = NULL;
 
                 if (!(ip = virDomainNetIPParseXML(cur)))
                     goto error;
@@ -9082,13 +9082,13 @@ virDomainNetDefParseXML(virDomainXMLOptionPtr xmlopt,
                 if (VIR_APPEND_ELEMENT(ips, nips, ip) < 0)
                     goto error;
             } else if (xmlStrEqual(cur->name, BAD_CAST "route")) {
-                virNetworkRouteDefPtr route = NULL;
-                if (!(route = virNetworkRouteDefParseXML(_("Domain interface"),
+                virNetDevIPRoutePtr route = NULL;
+                if (!(route = virNetDevIPRouteParseXML(_("Domain interface"),
                                                          cur, ctxt)))
                     goto error;
 
                 if (VIR_APPEND_ELEMENT(routes, nroutes, route) < 0) {
-                    virNetworkRouteDefFree(route);
+                    virNetDevIPRouteFree(route);
                     goto error;
                 }
             } else if (!ifname &&
@@ -20270,7 +20270,7 @@ virDomainFSDefFormat(virBufferPtr buf,
 }
 
 static int
-virDomainNetIPsFormat(virBufferPtr buf, virDomainNetIPDefPtr *ips, size_t nips)
+virDomainNetIPsFormat(virBufferPtr buf, virNetDevIPAddrPtr *ips, size_t nips)
 {
     size_t i;
 
@@ -20300,13 +20300,13 @@ virDomainNetIPsFormat(virBufferPtr buf, virDomainNetIPDefPtr *ips, size_t nips)
 
 static int
 virDomainNetRoutesFormat(virBufferPtr buf,
-                         virNetworkRouteDefPtr *routes,
+                         virNetDevIPRoutePtr *routes,
                          size_t nroutes)
 {
     size_t i;
 
     for (i = 0; i < nroutes; i++)
-        if (virNetworkRouteDefFormat(buf, routes[i]) < 0)
+        if (virNetDevIPRouteFormat(buf, routes[i]) < 0)
             return -1;
     return 0;
 }
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index e93bd5c..c529b09 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -41,6 +41,7 @@
 # include "numa_conf.h"
 # include "virnetdevmacvlan.h"
 # include "virsysinfo.h"
+# include "virnetdevip.h"
 # include "virnetdevvportprofile.h"
 # include "virnetdevbandwidth.h"
 # include "virnetdevvlan.h"
@@ -382,13 +383,6 @@ typedef enum {
     VIR_DOMAIN_HOSTDEV_CAPS_TYPE_LAST
 } virDomainHostdevCapsType;
 
-typedef struct _virDomainNetIPDef virDomainNetIPDef;
-typedef virDomainNetIPDef *virDomainNetIPDefPtr;
-struct _virDomainNetIPDef {
-    virSocketAddr address;       /* ipv4 or ipv6 address */
-    unsigned int prefix; /* number of 1 bits in the net mask */
-};
-
 typedef struct _virDomainHostdevCaps virDomainHostdevCaps;
 typedef virDomainHostdevCaps *virDomainHostdevCapsPtr;
 struct _virDomainHostdevCaps {
@@ -403,9 +397,9 @@ struct _virDomainHostdevCaps {
         struct {
             char *iface;
             size_t nips;
-            virDomainNetIPDefPtr *ips;
+            virNetDevIPAddrPtr *ips;
             size_t nroutes;
-            virNetworkRouteDefPtr *routes;
+            virNetDevIPRoutePtr *routes;
         } net;
     } u;
 };
@@ -985,9 +979,9 @@ struct _virDomainNetDef {
     int trustGuestRxFilters; /* enum virTristateBool */
     int linkstate;
     size_t nips;
-    virDomainNetIPDefPtr *ips;
+    virNetDevIPAddrPtr *ips;
     size_t nroutes;
-    virNetworkRouteDefPtr *routes;
+    virNetDevIPRoutePtr *routes;
 };
 
 /* Used for prefix of ifname of any network name generated dynamically
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 5ae2bdf..fb2a48d 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -406,7 +406,7 @@ virNetworkDefFree(virNetworkDefPtr def)
     VIR_FREE(def->ips);
 
     for (i = 0; i < def->nroutes && def->routes; i++)
-        virNetworkRouteDefFree(def->routes[i]);
+        virNetDevIPRouteFree(def->routes[i]);
     VIR_FREE(def->routes);
 
     for (i = 0; i < def->nPortGroups && def->portGroups; i++)
@@ -804,7 +804,7 @@ virNetworkDefGetIPByIndex(const virNetworkDef *def,
 }
 
 /* return routes[index], or NULL if there aren't enough routes */
-virNetworkRouteDefPtr
+virNetDevIPRoutePtr
 virNetworkDefGetRouteByIndex(const virNetworkDef *def,
                              int family, size_t n)
 {
@@ -818,7 +818,7 @@ virNetworkDefGetRouteByIndex(const virNetworkDef *def,
 
     /* find the nth route of type "family" */
     for (i = 0; i < def->nroutes; i++) {
-        virSocketAddrPtr addr = virNetworkRouteDefGetAddress(def->routes[i]);
+        virSocketAddrPtr addr = virNetDevIPRouteGetAddress(def->routes[i]);
         if (VIR_SOCKET_ADDR_IS_FAMILY(addr, family)
             && (n-- <= 0)) {
             return def->routes[i];
@@ -2261,9 +2261,9 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
             goto error;
         /* parse each definition */
         for (i = 0; i < nRoutes; i++) {
-            virNetworkRouteDefPtr route = NULL;
+            virNetDevIPRoutePtr route = NULL;
 
-            if (!(route = virNetworkRouteDefParseXML(def->name,
+            if (!(route = virNetDevIPRouteParseXML(def->name,
                                                      routeNodes[i],
                                                      ctxt)))
                 goto error;
@@ -2283,8 +2283,8 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt)
             size_t j;
             virSocketAddr testAddr, testGw;
             bool addrMatch;
-            virNetworkRouteDefPtr gwdef = def->routes[i];
-            virSocketAddrPtr gateway = virNetworkRouteDefGetGateway(gwdef);
+            virNetDevIPRoutePtr gwdef = def->routes[i];
+            virSocketAddrPtr gateway = virNetDevIPRouteGetGateway(gwdef);
             addrMatch = false;
             for (j = 0; j < nips; j++) {
                 virNetworkIPDefPtr def2 = &def->ips[j];
@@ -2876,7 +2876,7 @@ virNetworkDefFormatBuf(virBufferPtr buf,
     }
 
     for (i = 0; i < def->nroutes; i++) {
-        if (virNetworkRouteDefFormat(buf, def->routes[i]) < 0)
+        if (virNetDevIPRouteFormat(buf, def->routes[i]) < 0)
             goto error;
     }
 
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 18f4d1e..e7ce674 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -243,7 +243,7 @@ struct _virNetworkDef {
     virNetworkIPDefPtr ips; /* ptr to array of IP addresses on this network */
 
     size_t nroutes;
-    virNetworkRouteDefPtr *routes; /* ptr to array of static routes on this interface */
+    virNetDevIPRoutePtr *routes; /* ptr to array of static routes on this interface */
 
     virNetworkDNSDef dns;   /* dns related configuration */
     virNetDevVPortProfilePtr virtPortProfile;
@@ -354,7 +354,7 @@ virPortGroupDefPtr virPortGroupFindByName(virNetworkDefPtr net,
 virNetworkIPDefPtr
 virNetworkDefGetIPByIndex(const virNetworkDef *def,
                           int family, size_t n);
-virNetworkRouteDefPtr
+virNetDevIPRoutePtr
 virNetworkDefGetRouteByIndex(const virNetworkDef *def,
                              int family, size_t n);
 int virNetworkIPDefPrefix(const virNetworkIPDef *def);
diff --git a/src/conf/networkcommon_conf.c b/src/conf/networkcommon_conf.c
index 8f9d4b8..12be819 100644
--- a/src/conf/networkcommon_conf.c
+++ b/src/conf/networkcommon_conf.c
@@ -32,34 +32,8 @@
 
 #define VIR_FROM_THIS VIR_FROM_NETWORK
 
-struct _virNetworkRouteDef {
-    char *family;               /* ipv4 or ipv6 - default is ipv4 */
-    virSocketAddr address;      /* Routed Network IP address */
-
-    /* One or the other of the following two will be used for a given
-     * Network address, but never both. The parser guarantees this.
-     * The virSocketAddrGetIPPrefix() can be used to get a
-     * valid prefix.
-     */
-    virSocketAddr netmask;      /* ipv4 - either netmask or prefix specified */
-    unsigned int prefix;        /* ipv6 - only prefix allowed */
-    bool has_prefix;            /* prefix= was specified */
-    unsigned int metric;        /* value for metric (defaults to 1) */
-    bool has_metric;            /* metric= was specified */
-    virSocketAddr gateway;      /* gateway IP address for ip-route */
-};
-
-void
-virNetworkRouteDefFree(virNetworkRouteDefPtr def)
-{
-    if (!def)
-        return;
-    VIR_FREE(def->family);
-    VIR_FREE(def);
-}
-
-virNetworkRouteDefPtr
-virNetworkRouteDefCreate(const char *errorDetail,
+virNetDevIPRoutePtr
+virNetDevIPRouteCreate(const char *errorDetail,
                          char *family,
                          const char *address,
                          const char *netmask,
@@ -69,7 +43,7 @@ virNetworkRouteDefCreate(const char *errorDetail,
                          unsigned int metric,
                          bool hasMetric)
 {
-    virNetworkRouteDefPtr def = NULL;
+    virNetDevIPRoutePtr def = NULL;
     virSocketAddr testAddr;
 
     if (VIR_ALLOC(def) < 0)
@@ -242,21 +216,21 @@ virNetworkRouteDefCreate(const char *errorDetail,
     return def;
 
  error:
-    virNetworkRouteDefFree(def);
+    virNetDevIPRouteFree(def);
     return NULL;
 }
 
-virNetworkRouteDefPtr
-virNetworkRouteDefParseXML(const char *errorDetail,
+virNetDevIPRoutePtr
+virNetDevIPRouteParseXML(const char *errorDetail,
                            xmlNodePtr node,
                            xmlXPathContextPtr ctxt)
 {
     /*
-     * virNetworkRouteDef object is already allocated as part
+     * virNetDevIPRoute object is already allocated as part
      * of an array.  On failure clear: it out, but don't free it.
      */
 
-    virNetworkRouteDefPtr def = NULL;
+    virNetDevIPRoutePtr def = NULL;
     xmlNodePtr save;
     char *family = NULL;
     char *address = NULL, *netmask = NULL;
@@ -302,7 +276,7 @@ virNetworkRouteDefParseXML(const char *errorDetail,
         }
     }
 
-    def = virNetworkRouteDefCreate(errorDetail, family, address, netmask,
+    def = virNetDevIPRouteCreate(errorDetail, family, address, netmask,
                                    gateway, prefix, hasPrefix, metric,
                                    hasMetric);
 
@@ -316,8 +290,8 @@ virNetworkRouteDefParseXML(const char *errorDetail,
 }
 
 int
-virNetworkRouteDefFormat(virBufferPtr buf,
-                         const virNetworkRouteDef *def)
+virNetDevIPRouteFormat(virBufferPtr buf,
+                         const virNetDevIPRoute *def)
 {
     int result = -1;
     char *addr = NULL;
@@ -354,62 +328,3 @@ virNetworkRouteDefFormat(virBufferPtr buf,
  cleanup:
     return result;
 }
-
-virSocketAddrPtr
-virNetworkRouteDefGetAddress(virNetworkRouteDefPtr def)
-{
-    if (def)
-        return &def->address;
-
-    return NULL;
-}
-
-int
-virNetworkRouteDefGetPrefix(virNetworkRouteDefPtr def)
-{
-    int prefix = 0;
-    virSocketAddr zero;
-
-    if (!def)
-        return -1;
-
-    /* this creates an all-0 address of the appropriate family */
-    ignore_value(virSocketAddrParse(&zero,
-                                    (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET)
-                                     ? VIR_SOCKET_ADDR_IPV4_ALL
-                                     : VIR_SOCKET_ADDR_IPV6_ALL),
-                                    VIR_SOCKET_ADDR_FAMILY(&def->address)));
-
-    if (virSocketAddrEqual(&def->address, &zero)) {
-        if (def->has_prefix && def->prefix == 0)
-            prefix = 0;
-        else if ((VIR_SOCKET_ADDR_IS_FAMILY(&def->netmask, AF_INET) &&
-                  virSocketAddrEqual(&def->netmask, &zero)))
-            prefix = 0;
-        else
-            prefix = virSocketAddrGetIPPrefix(&def->address, &def->netmask,
-                                              def->prefix);
-    } else {
-        prefix = virSocketAddrGetIPPrefix(&def->address, &def->netmask,
-                                          def->prefix);
-    }
-
-    return prefix;
-}
-
-unsigned int
-virNetworkRouteDefGetMetric(virNetworkRouteDefPtr def)
-{
-    if (def && def->has_metric && def->metric > 0)
-        return def->metric;
-
-    return 1;
-}
-
-virSocketAddrPtr
-virNetworkRouteDefGetGateway(virNetworkRouteDefPtr def)
-{
-    if (def)
-        return &def->gateway;
-    return NULL;
-}
diff --git a/src/conf/networkcommon_conf.h b/src/conf/networkcommon_conf.h
index 1500d0f..160f80b 100644
--- a/src/conf/networkcommon_conf.h
+++ b/src/conf/networkcommon_conf.h
@@ -31,42 +31,25 @@
 # include "internal.h"
 # include "virbuffer.h"
 # include "virsocketaddr.h"
-
-typedef struct _virNetworkRouteDef virNetworkRouteDef;
-typedef virNetworkRouteDef *virNetworkRouteDefPtr;
-
-void
-virNetworkRouteDefFree(virNetworkRouteDefPtr def);
-
-virNetworkRouteDefPtr
-virNetworkRouteDefCreate(const char *networkName,
-                         char *family,
-                         const char *address,
-                         const char *netmask,
-                         const char *gateway,
-                         unsigned int prefix,
-                         bool hasPrefix,
-                         unsigned int metric,
-                         bool hasMetric);
-
-virNetworkRouteDefPtr
-virNetworkRouteDefParseXML(const char *networkName,
-                           xmlNodePtr node,
-                           xmlXPathContextPtr ctxt);
+# include "virnetdevip.h"
+
+virNetDevIPRoutePtr
+virNetDevIPRouteCreate(const char *networkName,
+                       char *family,
+                       const char *address,
+                       const char *netmask,
+                       const char *gateway,
+                       unsigned int prefix,
+                       bool hasPrefix,
+                       unsigned int metric,
+                       bool hasMetric);
+
+virNetDevIPRoutePtr
+virNetDevIPRouteParseXML(const char *networkName,
+                         xmlNodePtr node,
+                         xmlXPathContextPtr ctxt);
 int
-virNetworkRouteDefFormat(virBufferPtr buf,
-                         const virNetworkRouteDef *def);
-
-virSocketAddrPtr
-virNetworkRouteDefGetAddress(virNetworkRouteDefPtr def);
-
-int
-virNetworkRouteDefGetPrefix(virNetworkRouteDefPtr def);
-
-unsigned int
-virNetworkRouteDefGetMetric(virNetworkRouteDefPtr def);
-
-virSocketAddrPtr
-virNetworkRouteDefGetGateway(virNetworkRouteDefPtr def);
+virNetDevIPRouteFormat(virBufferPtr buf,
+                       const virNetDevIPRoute *def);
 
 #endif /* __NETWORKCOMMON_CONF_H__ */
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bd7b730..151cf9f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -644,14 +644,9 @@ virNetworkEventStateRegisterID;
 
 
 # conf/networkcommon_conf.h
-virNetworkRouteDefCreate;
-virNetworkRouteDefFormat;
-virNetworkRouteDefFree;
-virNetworkRouteDefGetAddress;
-virNetworkRouteDefGetGateway;
-virNetworkRouteDefGetMetric;
-virNetworkRouteDefGetPrefix;
-virNetworkRouteDefParseXML;
+virNetDevIPRouteCreate;
+virNetDevIPRouteFormat;
+virNetDevIPRouteParseXML;
 
 
 # conf/node_device_conf.h
@@ -1931,6 +1926,11 @@ virNetDevIPAddrAdd;
 virNetDevIPAddrDel;
 virNetDevIPAddrGet;
 virNetDevIPRouteAdd;
+virNetDevIPRouteFree;
+virNetDevIPRouteGetAddress;
+virNetDevIPRouteGetGateway;
+virNetDevIPRouteGetMetric;
+virNetDevIPRouteGetPrefix;
 virNetDevIPWaitDadFinish;
 
 
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index a95472c..1000d88 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -513,7 +513,7 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
             goto error_out;
 
         for (j = 0; j < netDef->nips; j++) {
-            virDomainNetIPDefPtr ip = netDef->ips[j];
+            virNetDevIPAddrPtr ip = netDef->ips[j];
             int prefix;
             char *ipStr = virSocketAddrFormat(&ip->address);
 
@@ -546,13 +546,13 @@ static int lxcContainerRenameAndEnableInterfaces(virDomainDefPtr vmDef,
 
             /* Set the routes */
             for (j = 0; j < netDef->nroutes; j++) {
-                virNetworkRouteDefPtr route = netDef->routes[j];
+                virNetDevIPRoutePtr route = netDef->routes[j];
 
                 if (virNetDevIPRouteAdd(newname,
-                                        virNetworkRouteDefGetAddress(route),
-                                        virNetworkRouteDefGetPrefix(route),
-                                        virNetworkRouteDefGetGateway(route),
-                                        virNetworkRouteDefGetMetric(route)) < 0) {
+                                        virNetDevIPRouteGetAddress(route),
+                                        virNetDevIPRouteGetPrefix(route),
+                                        virNetDevIPRouteGetGateway(route),
+                                        virNetDevIPRouteGetMetric(route)) < 0) {
                     goto error_out;
                 }
                 VIR_FREE(toStr);
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 9ad1b08..8294d29 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -419,7 +419,7 @@ typedef struct {
     char *macvlanmode;
     char *vlanid;
     char *name;
-    virDomainNetIPDefPtr *ips;
+    virNetDevIPAddrPtr *ips;
     size_t nips;
     char *gateway_ipv4;
     char *gateway_ipv6;
@@ -430,10 +430,10 @@ typedef struct {
 static int
 lxcAddNetworkRouteDefinition(const char *address,
                              int family,
-                             virNetworkRouteDefPtr **routes,
+                             virNetDevIPRoutePtr **routes,
                              size_t *nroutes)
 {
-    virNetworkRouteDefPtr route = NULL;
+    virNetDevIPRoutePtr route = NULL;
     char *familyStr = NULL;
     char *zero = NULL;
 
@@ -444,7 +444,7 @@ lxcAddNetworkRouteDefinition(const char *address,
     if (VIR_STRDUP(familyStr, family == AF_INET ? "ipv4" : "ipv6") < 0)
         goto error;
 
-    if (!(route = virNetworkRouteDefCreate(_("Domain interface"), familyStr,
+    if (!(route = virNetDevIPRouteCreate(_("Domain interface"), familyStr,
                                           zero, NULL, address, 0, false,
                                           0, false)))
         goto error;
@@ -460,7 +460,7 @@ lxcAddNetworkRouteDefinition(const char *address,
  error:
     VIR_FREE(familyStr);
     VIR_FREE(zero);
-    virNetworkRouteDefFree(route);
+    virNetDevIPRouteFree(route);
     return -1;
 }
 
@@ -601,7 +601,7 @@ lxcNetworkWalkCallback(const char *name, virConfValuePtr value, void *data)
              STREQ(name, "lxc.network.ipv6")) {
         int family = AF_INET;
         char **ipparts = NULL;
-        virDomainNetIPDefPtr ip = NULL;
+        virNetDevIPAddrPtr ip = NULL;
 
         if (VIR_ALLOC(ip) < 0)
             return -1;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 135f7bd..63c6e76 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -2020,12 +2020,12 @@ networkStartHandleMACTableManagerMode(virNetworkObjPtr network,
 /* add an IP (static) route to a bridge */
 static int
 networkAddRouteToBridge(virNetworkObjPtr network,
-                        virNetworkRouteDefPtr routedef)
+                        virNetDevIPRoutePtr routedef)
 {
-    int prefix = virNetworkRouteDefGetPrefix(routedef);
-    unsigned int metric = virNetworkRouteDefGetMetric(routedef);
-    virSocketAddrPtr addr = virNetworkRouteDefGetAddress(routedef);
-    virSocketAddrPtr gateway = virNetworkRouteDefGetGateway(routedef);
+    int prefix = virNetDevIPRouteGetPrefix(routedef);
+    unsigned int metric = virNetDevIPRouteGetMetric(routedef);
+    virSocketAddrPtr addr = virNetDevIPRouteGetAddress(routedef);
+    virSocketAddrPtr gateway = virNetDevIPRouteGetGateway(routedef);
 
     if (prefix < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -2076,7 +2076,7 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
     bool v4present = false, v6present = false;
     virErrorPtr save_err = NULL;
     virNetworkIPDefPtr ipdef;
-    virNetworkRouteDefPtr routedef;
+    virNetDevIPRoutePtr routedef;
     char *macTapIfName = NULL;
     int tapfd = -1;
 
@@ -2171,7 +2171,7 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
         virSocketAddrPtr gateway = NULL;
 
         routedef = network->def->routes[i];
-        gateway = virNetworkRouteDefGetGateway(routedef);
+        gateway = virNetDevIPRouteGetGateway(routedef);
 
         /* Add the IP route to the bridge */
         /* ignore errors, error msg will be generated */
diff --git a/src/network/bridge_driver_linux.c b/src/network/bridge_driver_linux.c
index b41a1ba..3effcdc 100644
--- a/src/network/bridge_driver_linux.c
+++ b/src/network/bridge_driver_linux.c
@@ -69,7 +69,7 @@ int networkCheckRouteCollision(virNetworkDefPtr def)
         char iface[17], dest[128], mask[128];
         unsigned int addr_val, mask_val;
         virNetworkIPDefPtr ipdef;
-        virNetworkRouteDefPtr routedef;
+        virNetDevIPRoutePtr routedef;
         int num;
         size_t i;
 
@@ -130,8 +130,8 @@ int networkCheckRouteCollision(virNetworkDefPtr def)
              i++) {
 
             virSocketAddr r_mask, r_addr;
-            virSocketAddrPtr tmp_addr = virNetworkRouteDefGetAddress(routedef);
-            int r_prefix = virNetworkRouteDefGetPrefix(routedef);
+            virSocketAddrPtr tmp_addr = virNetDevIPRouteGetAddress(routedef);
+            int r_prefix = virNetDevIPRouteGetPrefix(routedef);
 
             if (!tmp_addr ||
                 virSocketAddrMaskByPrefix(tmp_addr, r_prefix, &r_addr) < 0 ||
diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 044f2bc..619f926 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -776,3 +776,72 @@ virNetDevIPAddrGet(const char *ifname,
                          _("Unable to get IP address on this platform"));
     return -1;
 }
+
+/* manipulating the virNetDevIPRoute object */
+void
+virNetDevIPRouteFree(virNetDevIPRoutePtr def)
+{
+    if (!def)
+        return;
+    VIR_FREE(def->family);
+    VIR_FREE(def);
+}
+
+virSocketAddrPtr
+virNetDevIPRouteGetAddress(virNetDevIPRoutePtr def)
+{
+    if (def)
+        return &def->address;
+
+    return NULL;
+}
+
+int
+virNetDevIPRouteGetPrefix(virNetDevIPRoutePtr def)
+{
+    int prefix = 0;
+    virSocketAddr zero;
+
+    if (!def)
+        return -1;
+
+    /* this creates an all-0 address of the appropriate family */
+    ignore_value(virSocketAddrParse(&zero,
+                                    (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET)
+                                     ? VIR_SOCKET_ADDR_IPV4_ALL
+                                     : VIR_SOCKET_ADDR_IPV6_ALL),
+                                    VIR_SOCKET_ADDR_FAMILY(&def->address)));
+
+    if (virSocketAddrEqual(&def->address, &zero)) {
+        if (def->has_prefix && def->prefix == 0)
+            prefix = 0;
+        else if ((VIR_SOCKET_ADDR_IS_FAMILY(&def->netmask, AF_INET) &&
+                  virSocketAddrEqual(&def->netmask, &zero)))
+            prefix = 0;
+        else
+            prefix = virSocketAddrGetIPPrefix(&def->address, &def->netmask,
+                                              def->prefix);
+    } else {
+        prefix = virSocketAddrGetIPPrefix(&def->address, &def->netmask,
+                                          def->prefix);
+    }
+
+    return prefix;
+}
+
+unsigned int
+virNetDevIPRouteGetMetric(virNetDevIPRoutePtr def)
+{
+    if (def && def->has_metric && def->metric > 0)
+        return def->metric;
+
+    return 1;
+}
+
+virSocketAddrPtr
+virNetDevIPRouteGetGateway(virNetDevIPRoutePtr def)
+{
+    if (def)
+        return &def->gateway;
+    return NULL;
+}
diff --git a/src/util/virnetdevip.h b/src/util/virnetdevip.h
index f60465d..7a07b73 100644
--- a/src/util/virnetdevip.h
+++ b/src/util/virnetdevip.h
@@ -25,6 +25,28 @@
 
 # include "virsocketaddr.h"
 
+typedef struct {
+    virSocketAddr address;       /* ipv4 or ipv6 address */
+    unsigned int prefix; /* number of 1 bits in the net mask */
+} virNetDevIPAddr, *virNetDevIPAddrPtr;
+
+typedef struct {
+    char *family;               /* ipv4 or ipv6 - default is ipv4 */
+    virSocketAddr address;      /* Routed Network IP address */
+
+    /* One or the other of the following two will be used for a given
+     * Network address, but never both. The parser guarantees this.
+     * The virSocketAddrGetIPPrefix() can be used to get a
+     * valid prefix.
+     */
+    virSocketAddr netmask;      /* ipv4 - either netmask or prefix specified */
+    unsigned int prefix;        /* ipv6 - only prefix allowed */
+    bool has_prefix;            /* prefix= was specified */
+    unsigned int metric;        /* value for metric (defaults to 1) */
+    bool has_metric;            /* metric= was specified */
+    virSocketAddr gateway;      /* gateway IP address for ip-route */
+} virNetDevIPRoute, *virNetDevIPRoutePtr;
+
 /* manipulating/querying the netdev */
 int virNetDevIPAddrAdd(const char *ifname,
                        virSocketAddr *addr,
@@ -47,4 +69,11 @@ int virNetDevIPAddrGet(const char *ifname, virSocketAddrPtr addr)
 int virNetDevIPWaitDadFinish(virSocketAddrPtr *addrs, size_t count)
     ATTRIBUTE_NONNULL(1);
 
+/* virNetDevIPRoute object */
+void virNetDevIPRouteFree(virNetDevIPRoutePtr def);
+virSocketAddrPtr virNetDevIPRouteGetAddress(virNetDevIPRoutePtr def);
+int virNetDevIPRouteGetPrefix(virNetDevIPRoutePtr def);
+unsigned int virNetDevIPRouteGetMetric(virNetDevIPRoutePtr def);
+virSocketAddrPtr virNetDevIPRouteGetGateway(virNetDevIPRoutePtr def);
+
 #endif /* __VIR_NETDEVIP_H__ */
-- 
2.5.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]