]> xenbits.xensource.com Git - libvirt.git/commitdiff
network: new function networkGetActualType
authorLaine Stump <laine@laine.org>
Fri, 1 Apr 2016 13:45:51 +0000 (09:45 -0400)
committerLaine Stump <laine@laine.org>
Mon, 4 Apr 2016 11:03:12 +0000 (07:03 -0400)
There are times when it's necessary to learn the actual type of a
network connection before any resources have been allocated
(e.g. during qemuProcessPrepareDomain()), but in the past it was
necessary to call networkAllocateActualDevice() in order to have the
actual type filled in.

This new function returns the type of network that *will be* setup
once it actually happens, but without making any changes on the host.

src/network/bridge_driver.c
src/network/bridge_driver.h

index c673cc1515e24e138144d7fac8209577f29c834e..0d999914e3a30684a6779316caf17cc2aa47ca3b 100644 (file)
@@ -4736,6 +4736,78 @@ networkGetNetworkAddress(const char *netname, char **netaddr)
     return ret;
 }
 
+/* networkGetActualType:
+ * @dom: domain definition that @iface belongs to
+ * @iface: the original NetDef from the domain
+ *
+ * Looks up the network reference by iface, and returns the actual
+ * type of the connection without allocating any resources.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int
+networkGetActualType(virDomainNetDefPtr iface)
+{
+    virNetworkDriverStatePtr driver = networkGetDriver();
+    virNetworkObjPtr network = NULL;
+    virNetworkDefPtr netdef = NULL;
+    int ret = -1;
+
+    if (!driver || iface->type != VIR_DOMAIN_NET_TYPE_NETWORK)
+        return iface->type;
+
+    if (iface->data.network.actual)
+        return iface->data.network.actual->type;
+
+    network = virNetworkObjFindByName(driver->networks, iface->data.network.name);
+    if (!network) {
+        virReportError(VIR_ERR_NO_NETWORK,
+                       _("no network with matching name '%s'"),
+                       iface->data.network.name);
+        return -1;
+    }
+    netdef = network->def;
+
+    if ((netdef->forward.type == VIR_NETWORK_FORWARD_NONE) ||
+        (netdef->forward.type == VIR_NETWORK_FORWARD_NAT) ||
+        (netdef->forward.type == VIR_NETWORK_FORWARD_ROUTE)) {
+        /* for these forward types, the actual net type really *is*
+         * NETWORK; we just keep the info from the portgroup in
+         * iface->data.network.actual
+         */
+        ret = VIR_DOMAIN_NET_TYPE_NETWORK;
+
+    } else if ((netdef->forward.type == VIR_NETWORK_FORWARD_BRIDGE) &&
+               netdef->bridge) {
+
+        /* <forward type='bridge'/> <bridge name='xxx'/>
+         * is VIR_DOMAIN_NET_TYPE_BRIDGE
+         */
+
+        ret = VIR_DOMAIN_NET_TYPE_BRIDGE;
+
+    } else if (netdef->forward.type == VIR_NETWORK_FORWARD_HOSTDEV) {
+
+        ret = VIR_DOMAIN_NET_TYPE_HOSTDEV;
+
+    } else if ((netdef->forward.type == VIR_NETWORK_FORWARD_BRIDGE) ||
+               (netdef->forward.type == VIR_NETWORK_FORWARD_PRIVATE) ||
+               (netdef->forward.type == VIR_NETWORK_FORWARD_VEPA) ||
+               (netdef->forward.type == VIR_NETWORK_FORWARD_PASSTHROUGH)) {
+
+        /* <forward type='bridge|private|vepa|passthrough'> are all
+         * VIR_DOMAIN_NET_TYPE_DIRECT.
+         */
+
+        ret = VIR_DOMAIN_NET_TYPE_DIRECT;
+
+    }
+
+    virNetworkObjEndAPI(&network);
+    return ret;
+}
+
+
 /**
  * networkCheckBandwidth:
  * @net: network QoS
index 7db2c9011058560e32a22444567e7dfa461c31c5..f0cac5d52835ec99721ad1532e40292974c93657 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * bridge_driver.h: core driver methods for managing networks
  *
- * Copyright (C) 2006-2015 Red Hat, Inc.
+ * Copyright (C) 2006-2016 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -47,6 +47,9 @@ int networkReleaseActualDevice(virDomainDefPtr dom,
 int networkGetNetworkAddress(const char *netname, char **netaddr)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
+int networkGetActualType(virDomainNetDefPtr iface)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 int networkDnsmasqConfContents(virNetworkObjPtr network,
                         const char *pidfile,
                         char **configstr,
@@ -64,6 +67,7 @@ int networkBandwidthUpdate(virDomainNetDefPtr iface,
 # else
 /* Define no-op replacements that don't drag in any link dependencies.  */
 #  define networkAllocateActualDevice(dom, iface) 0
+#  define networkGetActualType(iface) (iface->type)
 #  define networkGetNetworkAddress(netname, netaddr) (-2)
 #  define networkDnsmasqConfContents(network, pidfile, configstr, \
                     dctx, caps) 0