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

[libvirt] [PATCHv3 5/5] network: add connections counter to networks



Just as each physical device used by a network has a connections
counter, now each network has a connections counter which is
incremented once for each guest interface that connects using this
network.

The count is output in the live network XML, like this:

   <network connections='20'>
   ...
   </network>

It is read-only, and for informational purposes only - it isn't used
internally anywhere by libvirt.
---
Changes from v2:

Eric pointed out that even though connections isn't configurable, it
should still be documented, and might show up in someone's XML that
they input to the API. So in this revision, I added connections to the
rng for both <network> and to the <forward> element's <interface>
subelement, and also added a short bit describing it in the
appropriate places in formatnetwork.html.



 docs/formatnetwork.html.in  | 32 ++++++++++++++++++++++++--------
 docs/schemas/network.rng    | 10 ++++++++++
 src/conf/network_conf.c     |  6 +++++-
 src/conf/network_conf.h     |  1 +
 src/network/bridge_driver.c | 10 ++++++++++
 5 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/docs/formatnetwork.html.in b/docs/formatnetwork.html.in
index daa6524..a010cbd 100644
--- a/docs/formatnetwork.html.in
+++ b/docs/formatnetwork.html.in
@@ -15,8 +15,14 @@
 
     <p>
       The root element required for all virtual networks is
-      named <code>network</code> and has no attributes.
-      The network XML format is available <span class="since">since 0.3.0</span>
+      named <code>network</code> and has no configurable attributes
+      (although <span class="since">since 0.10.0</span> there is one
+      optional read-only attribute - when examining the live
+      configuration of a network, the
+      attribute <code>connections</code>, if present, specifies the
+      number of guest interfaces currently connected via this
+      network).  The network XML format is
+      available <span class="since">since 0.3.0</span>
     </p>
 
     <h3><a name="elementsMetadata">General metadata</a></h3>
@@ -233,12 +239,22 @@
   &lt;/forward&gt;
 ...
         </pre>
-        Additionally, <span class="since">since 0.9.10</span>, libvirt
-        allows a shorthand for specifying all virtual interfaces
-        associated with a single physical function, by using
-        the <code>&lt;pf&gt;</code> subelement to call out the
-        corresponding physical interface associated with multiple
-        virtual interfaces:
+        <p>
+          <span class="since">since 0.10.0</span>,
+          <code>&lt;interface&gt;</code> also has an optional read-only
+          attribute - when examining the live configuration of a
+          network, the attribute <code>connections</code>, if present,
+          specifies the number of guest interfaces currently connected
+          via this physical interface.
+        </p>
+        <p>
+          Additionally, <span class="since">since 0.9.10</span>, libvirt
+          allows a shorthand for specifying all virtual interfaces
+          associated with a single physical function, by using
+          the <code>&lt;pf&gt;</code> subelement to call out the
+          corresponding physical interface associated with multiple
+          virtual interfaces:
+        </p>
         <pre>
 ...
   &lt;forward mode='passthrough'&gt;
diff --git a/docs/schemas/network.rng b/docs/schemas/network.rng
index 2ae879e..25df266 100644
--- a/docs/schemas/network.rng
+++ b/docs/schemas/network.rng
@@ -12,6 +12,11 @@
   <define name="network">
 
     <element name="network">
+      <optional>
+        <attribute name="connections">
+          <data type="unsignedInt"/>
+        </attribute>
+      </optional>
       <interleave>
 
         <!-- The name of the network, used to refer to it through the API
@@ -91,6 +96,11 @@
                   <attribute name='dev'>
                     <ref name='deviceName'/>
                   </attribute>
+                  <optional>
+                    <attribute name="connections">
+                      <data type="unsignedInt"/>
+                    </attribute>
+                  </optional>
                 </element>
               </zeroOrMore>
               <optional>
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index ca5b759..45f8e69 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1463,7 +1463,11 @@ char *virNetworkDefFormat(const virNetworkDefPtr def, unsigned int flags)
     char uuidstr[VIR_UUID_STRING_BUFLEN];
     int ii;
 
-    virBufferAddLit(&buf, "<network>\n");
+    virBufferAddLit(&buf, "<network");
+    if (!(flags & VIR_NETWORK_XML_INACTIVE) && (def->connections > 0)) {
+        virBufferAsprintf(&buf, " connections='%d'", def->connections);
+    }
+    virBufferAddLit(&buf, ">\n");
     virBufferEscapeString(&buf, "  <name>%s</name>\n", def->name);
 
     uuid = def->uuid;
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 23f1632..32fc765 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -156,6 +156,7 @@ struct _virNetworkDef {
     unsigned char uuid[VIR_UUID_BUFLEN];
     bool uuid_specified;
     char *name;
+    int   connections; /* # of guest interfaces connected to this network */
 
     char *bridge;       /* Name of bridge device */
     char *domain;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index c86a157..efc79c1 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -3005,6 +3005,10 @@ networkAllocateActualDevice(virDomainNetDefPtr iface)
         VIR_DEBUG("Using physical device %s, %d connections",
                   dev->dev, dev->connections);
     }
+
+    netdef->connections++;
+    VIR_DEBUG("Using network %s, %d connections",
+              netdef->name, netdef->connections);
     ret = 0;
 cleanup:
     for (ii = 0; ii < num_virt_fns; ii++)
@@ -3117,6 +3121,9 @@ networkNotifyActualDevice(virDomainNetDefPtr iface)
     }
 
 success:
+    netdef->connections++;
+    VIR_DEBUG("Using network %s, %d connections",
+              netdef->name, netdef->connections);
     ret = 0;
 cleanup:
     if (network)
@@ -3205,6 +3212,9 @@ networkReleaseActualDevice(virDomainNetDefPtr iface)
     }
 
 success:
+    netdef->connections--;
+    VIR_DEBUG("Releasing network %s, %d connections",
+              netdef->name, netdef->connections);
     ret = 0;
 cleanup:
     if (network)
-- 
1.7.11.2


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