[PATCH 4/9] Allow network capabilities hostdev to configure IP addresses

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

 



---
 docs/formatdomain.html.in            | 12 ++++++++--
 docs/schemas/domaincommon.rng        | 23 ++++++++++++++----
 src/conf/domain_conf.c               | 46 ++++++++++++++++++++++++++++++++++++
 src/conf/domain_conf.h               |  2 ++
 tests/lxcxml2xmldata/lxc-hostdev.xml |  2 ++
 5 files changed, 79 insertions(+), 6 deletions(-)

diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in
index 0baf961..7fe5976 100644
--- a/docs/formatdomain.html.in
+++ b/docs/formatdomain.html.in
@@ -3991,13 +3991,21 @@ qemu-kvm -net nic,model=? /dev/null
       <target dev='vnet0'/>
       <b>&lt;ip address='192.168.122.5' prefix='24'/&gt;</b>
     &lt;/interface&gt;
+    ...
+    &lt;hostdev mode='capabilities' type='net'&gt;
+      &lt;source&gt;
+        &lt;interface&gt;eth0&lt;/interface&gt;
+      &lt;/source&gt;
+      <b>&lt;ip address='192.168.122.6' prefix='24'/&gt;</b>
+    &lt;/hostdev&gt;
+
   &lt;/devices&gt;
   ...
 </pre>
 
     <p>
-    <span class="since">Since 1.2.8</span> the network devices can be provided
-    zero or more IP addresses to set
+    <span class="since">Since 1.2.8</span> the network devices and host devices
+    with network capabilities can be provided zero or more IP addresses to set
     on the target device. Note that some hypervisors or network device types
     will simply ignore them or only use the first one. The <code>address</code>
     attribute can hold either an IPv4 or IPv6 address. The <code>prefix</code>
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng
index cbfe1ef..74fba3d 100644
--- a/docs/schemas/domaincommon.rng
+++ b/docs/schemas/domaincommon.rng
@@ -3604,11 +3604,26 @@
     <attribute name="type">
       <value>net</value>
     </attribute>
-    <element name="source">
-      <element name="interface">
-        <ref name="deviceName"/>
+    <interleave>
+      <element name="source">
+        <element name="interface">
+          <ref name="deviceName"/>
+        </element>
       </element>
-    </element>
+      <zeroOrMore>
+        <element name="ip">
+          <attribute name="address">
+            <ref name="ipAddr"/>
+          </attribute>
+          <optional>
+            <attribute name="prefix">
+              <ref name="ipPrefix"/>
+            </attribute>
+          </optional>
+          <empty/>
+        </element>
+      </zeroOrMore>
+    </interleave>
   </define>
 
   <define name="usbproduct">
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 4cdb435..8307428 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1707,6 +1707,8 @@ virDomainHostdevDefPtr virDomainHostdevDefAlloc(void)
 
 void virDomainHostdevDefClear(virDomainHostdevDefPtr def)
 {
+    size_t i;
+
     if (!def)
         return;
 
@@ -1731,6 +1733,9 @@ void virDomainHostdevDefClear(virDomainHostdevDefPtr def)
             break;
         case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET:
             VIR_FREE(def->source.caps.u.net.iface);
+            for (i = 0; i < def->source.caps.u.net.nips; i++)
+                virDomainNetIpDefFree(def->source.caps.u.net.ips[i]);
+            VIR_FREE(def->source.caps.u.net.ips);
             break;
         }
         break;
@@ -4373,6 +4378,8 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED,
                                 virDomainHostdevDefPtr def)
 {
     xmlNodePtr sourcenode;
+    xmlNodePtr *ipnodes = NULL;
+    int nipnodes;
     int ret = -1;
 
     /* @type is passed in from the caller rather than read from the
@@ -4427,6 +4434,40 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node ATTRIBUTE_UNUSED,
                            _("Missing <interface> element in hostdev net device"));
             goto error;
         }
+
+        /* Parse possible IP addresses */
+        if ((nipnodes = virXPathNodeSet("./ip", ctxt, &ipnodes)) < 0)
+            goto error;
+
+        if (nipnodes) {
+            size_t i;
+            for (i = 0; i < nipnodes; i++) {
+                char *prefixStr = NULL;
+                virDomainNetIpDefPtr ip = NULL;
+
+                if (VIR_ALLOC(ip) < 0)
+                    goto error;
+
+                ip->address = virXMLPropString(ipnodes[i], "address");
+
+                if ((prefixStr = virXMLPropString(ipnodes[i], "prefix")) &&
+                    (virStrToLong_ui(prefixStr, NULL, 10, &ip->prefix) < 0)) {
+
+                    virReportError(VIR_ERR_INVALID_ARG,
+                                   _("Invalid network prefix: '%s'"),
+                                   prefixStr);
+                    VIR_FREE(prefixStr);
+                    goto error;
+                }
+                VIR_FREE(prefixStr);
+
+                if (ip->address != NULL &&
+                    VIR_APPEND_ELEMENT(def->source.caps.u.net.ips,
+                                       def->source.caps.u.net.nips, ip) < 0)
+                    goto error;
+            }
+            VIR_FREE(ipnodes);
+        }
         break;
     default:
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -15716,6 +15757,11 @@ virDomainHostdevDefFormatCaps(virBufferPtr buf,
 
     virBufferAdjustIndent(buf, -2);
     virBufferAddLit(buf, "</source>\n");
+
+    if (def->source.caps.type == VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET) {
+        virDomainNetIpsFormat(buf, def->source.caps.u.net.ips,
+                              def->source.caps.u.net.nips);
+    }
     return 0;
 }
 
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index d322295..8ef69b4 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -447,6 +447,8 @@ struct _virDomainHostdevCaps {
         } misc;
         struct {
             char *iface;
+            size_t nips;
+            virDomainNetIpDefPtr *ips;
         } net;
     } u;
 };
diff --git a/tests/lxcxml2xmldata/lxc-hostdev.xml b/tests/lxcxml2xmldata/lxc-hostdev.xml
index befe0db..23bf04d 100644
--- a/tests/lxcxml2xmldata/lxc-hostdev.xml
+++ b/tests/lxcxml2xmldata/lxc-hostdev.xml
@@ -35,6 +35,8 @@
       <source>
         <interface>eth0</interface>
       </source>
+      <ip address='192.168.122.2'/>
+      <ip address='2003:db8:1:0:214:1234:fe0b:3596' prefix='24'/>
     </hostdev>
   </devices>
 </domain>
-- 
1.8.4.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]