[PATCH 1/3] network: new function networkGetActualType

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

 



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 | 72 +++++++++++++++++++++++++++++++++++++++++++++
 src/network/bridge_driver.h |  6 +++-
 2 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index c673cc1..0d99991 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -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
diff --git a/src/network/bridge_driver.h b/src/network/bridge_driver.h
index 7db2c90..f0cac5d 100644
--- a/src/network/bridge_driver.h
+++ b/src/network/bridge_driver.h
@@ -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
-- 
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]