[libvirt] [PATCH 6/7] conf: add hypervisor agnostic, domain start-time, validation function for NetDef

Laine Stump posted 7 patches 5 years, 1 month ago
[libvirt] [PATCH 6/7] conf: add hypervisor agnostic, domain start-time, validation function for NetDef
Posted by Laine Stump 5 years, 1 month ago
<interface> devices (virDomainNetDef) are a bit different from other
types of devices in that their actual type may come from a network (in
the form of a port connection), and that doesn't happen until the
domain is started. This means that any validation of an <interface> at
parse time needs to be a bit liberal in what it accepts - when
type='network', you could think that something is/isn't allowed, but
once the domain is started and a port is created by the configured
network, the opposite might be true.

To solve this problem hypervisor drivers need to do an extra
validation step when the domain is being started. I recently (commit
3cff23f7, libvirt 5.7.0) added a function to peform such validation
for all interfaces to the QEMU driver -
qemuDomainValidateActualNetDef() - but while that function is a good
single point to call for the multiple places that need to "start" an
interface (domain startup, device hotplug, device update), it can't be
called by the other hypervisor drivers, since 1) it's in the QEMU
driver, and 2) it contains some checks specific to QEMU. For
validation that applies to network devices on *all* hypervisors, we
need yet another interface validation function that can be called by
any hypervisor driver (not just QEMU) right after its network port has
been created during domain startup or hotplug. This patch adds that
function - virDomainNetDefRuntimeValidate(), in the conf directory,
and calls it in appropriate places in the QEMU, lxc, and libxl
drivers.

The new function, virDomainNetDefRuntimeValidate(), should contain
network device validation that 1) is hypervisor agnostic, and 2) can't
be done until we know the "actual type" of an interface.

There is no framework for validation at domain startup as there is for
post-parse validation, but I don't want to create a whole elaborate
system that will only be used by one type of device. For that reason,
I just made a single function that should be called directly from the
hypervisors, when they are initializing interfaces to start a domain,
right after conditionally allocating the network port (and regardless
of whether or not that was actually needed). In the case of the QEMU
driver, qemuDomainValidateActualNetDef() is already called in all the
appropriate places, so we can just call the new function from
there. In the case of the other hypervisors, we search for
virDomainNetAllocateActualDevice() (which is the hypervisor-agnostic
function that calls virNetworkPortCreateXML()), and add the call to our
new function right after that.

The new function itself could be plunked down into many places in the
code, but we already have 3 validation functions for network devices
in 2 different places (not counting any basic validation done in
virDomainNetDefParseXML() itself):

1) post-parse hypervisor-agnostic
   (virDomainNetDefValidate() - domain_conf.c:6145)
2) post-parse hypervisor-specific
   (qemuDomainDeviceDefValidateNetwork() - qemu_domain.c:5498)
3) domain-start hypervisor-specific
   (qemuDomainValidateActualNetDef() - qemu_domain.c:5390)

I placed (3) right next to (2) when I added it, specifically to avoid
spreading validation all over the code. For the same reason, I decided
to put this new function right next to (1) - this way if someone needs
to add validation specific to qemu, they go to one location, and if
they need to add validation applying to everyone, they go to the
other. It looks a bit strange to have a public function in between a
bunch of statics, but I think it's better than the alternative of
further fragmentation. (I'm open to other ideas though, of course.)

Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/conf/domain_conf.c   | 22 ++++++++++++++++++++++
 src/conf/domain_conf.h   |  3 +++
 src/libvirt_private.syms |  1 +
 src/libxl/libxl_domain.c |  4 ++++
 src/libxl/libxl_driver.c |  4 ++++
 src/lxc/lxc_driver.c     |  4 ++++
 src/lxc/lxc_process.c    |  4 ++++
 src/qemu/qemu_domain.c   |  6 ++++++
 8 files changed, 48 insertions(+)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 01a1a74946..c063f40a4c 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6094,6 +6094,28 @@ virDomainRedirdevDefValidate(const virDomainDef *def,
 }
 
 
+int
+virDomainNetDefRuntimeValidate(const virDomainNetDef *net G_GNUC_UNUSED)
+{
+    /* Unlike virDomainNetDefValidate(), which is a static function
+     * called internally to this file, virDomainActualNetDefValidate()
+     * is a public function that can be called from a hypervisor after
+     * it has completely setup the NetDef for use by a domain,
+     * including possibly allocating a port from the network driver
+     * (which could change the effective/"actual" type of the NetDef,
+     * thus changing what should/shouldn't be allowed by validation).
+     *
+     * This function should contain validations not specific to a
+     * particular hypervisor (e.g. whether or not specifying bandwidth
+     * is allowed for a type of interface), but *not*
+     * hypervisor-specific things.
+     */
+
+    return 0;
+
+}
+
+
 static int
 virDomainNetDefValidate(const virDomainNetDef *net)
 {
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 4a7010e8a5..dc0226e6da 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2802,6 +2802,9 @@ int virDomainDefValidate(virDomainDefPtr def,
                          unsigned int parseFlags,
                          virDomainXMLOptionPtr xmlopt);
 
+int
+virDomainNetDefRuntimeValidate(const virDomainNetDef *net);
+
 static inline bool
 virDomainObjIsActive(virDomainObjPtr dom)
 {
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 12cb3b5bf7..811d01c4f5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -479,6 +479,7 @@ virDomainNetDefClear;
 virDomainNetDefFormat;
 virDomainNetDefFree;
 virDomainNetDefNew;
+virDomainNetDefRuntimeValidate;
 virDomainNetDefToNetworkPort;
 virDomainNetFind;
 virDomainNetFindByName;
diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c
index d79c3c1ed7..57d2a4aeb5 100644
--- a/src/libxl/libxl_domain.c
+++ b/src/libxl/libxl_domain.c
@@ -1065,6 +1065,10 @@ libxlNetworkPrepareDevices(virDomainDefPtr def)
                 goto cleanup;
         }
 
+        /* final validation now that actual type is known */
+        if (virDomainNetDefRuntimeValidate(net) < 0)
+            return -1;
+
         actualType = virDomainNetGetActualType(net);
         if (actualType == VIR_DOMAIN_NET_TYPE_HOSTDEV &&
             net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 5cc6b87b8c..323cb92c04 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -3425,6 +3425,10 @@ libxlDomainAttachNetDevice(libxlDriverPrivatePtr driver,
             goto cleanup;
     }
 
+    /* final validation now that actual type is known */
+    if (virDomainNetDefRuntimeValidate(net) < 0)
+        return -1;
+
     actualType = virDomainNetGetActualType(net);
 
     if (virDomainHasNet(vm->def, net)) {
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 1a1239b1bd..8d519d282a 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -3860,6 +3860,10 @@ lxcDomainAttachDeviceNetLive(virConnectPtr conn,
         virObjectUnref(netconn);
     }
 
+    /* final validation now that actual type is known */
+    if (virDomainNetDefRuntimeValidate(net) < 0)
+        return -1;
+
     actualType = virDomainNetGetActualType(net);
 
     switch (actualType) {
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 1a565b6358..13547abec4 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -574,6 +574,10 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
                 goto cleanup;
         }
 
+        /* final validation now that actual type is known */
+        if (virDomainNetDefRuntimeValidate(net) < 0)
+            return -1;
+
         type = virDomainNetGetActualType(net);
         switch (type) {
         case VIR_DOMAIN_NET_TYPE_NETWORK:
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 0181b62602..a8a7907546 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -5389,6 +5389,12 @@ qemuDomainValidateActualNetDef(const virDomainNetDef *net,
 
     virMacAddrFormat(&net->mac, macstr);
 
+    /* hypervisor-agnostic validation */
+    if (virDomainNetDefRuntimeValidate(net) < 0)
+        return -1;
+
+    /* QEMU-specific validation */
+
     /* Only tap/macvtap devices support multiqueue. */
     if (net->driver.virtio.queues > 0) {
 
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 6/7] conf: add hypervisor agnostic, domain start-time, validation function for NetDef
Posted by Cole Robinson 5 years ago
On 10/22/19 9:24 PM, Laine Stump wrote:
> <interface> devices (virDomainNetDef) are a bit different from other
> types of devices in that their actual type may come from a network (in
> the form of a port connection), and that doesn't happen until the
> domain is started. This means that any validation of an <interface> at
> parse time needs to be a bit liberal in what it accepts - when
> type='network', you could think that something is/isn't allowed, but
> once the domain is started and a port is created by the configured
> network, the opposite might be true.
> 
> To solve this problem hypervisor drivers need to do an extra
> validation step when the domain is being started. I recently (commit
> 3cff23f7, libvirt 5.7.0) added a function to peform such validation
> for all interfaces to the QEMU driver -
> qemuDomainValidateActualNetDef() - but while that function is a good
> single point to call for the multiple places that need to "start" an
> interface (domain startup, device hotplug, device update), it can't be
> called by the other hypervisor drivers, since 1) it's in the QEMU
> driver, and 2) it contains some checks specific to QEMU. For
> validation that applies to network devices on *all* hypervisors, we
> need yet another interface validation function that can be called by
> any hypervisor driver (not just QEMU) right after its network port has
> been created during domain startup or hotplug. This patch adds that
> function - virDomainNetDefRuntimeValidate(), in the conf directory,
> and calls it in appropriate places in the QEMU, lxc, and libxl
> drivers.
> 
> The new function, virDomainNetDefRuntimeValidate(), should contain
> network device validation that 1) is hypervisor agnostic, and 2) can't
> be done until we know the "actual type" of an interface.
> 
> There is no framework for validation at domain startup as there is for
> post-parse validation, but I don't want to create a whole elaborate
> system that will only be used by one type of device. For that reason,
> I just made a single function that should be called directly from the
> hypervisors, when they are initializing interfaces to start a domain,
> right after conditionally allocating the network port (and regardless
> of whether or not that was actually needed). In the case of the QEMU
> driver, qemuDomainValidateActualNetDef() is already called in all the
> appropriate places, so we can just call the new function from
> there. In the case of the other hypervisors, we search for
> virDomainNetAllocateActualDevice() (which is the hypervisor-agnostic
> function that calls virNetworkPortCreateXML()), and add the call to our
> new function right after that.
> 
> The new function itself could be plunked down into many places in the
> code, but we already have 3 validation functions for network devices
> in 2 different places (not counting any basic validation done in
> virDomainNetDefParseXML() itself):
> 
> 1) post-parse hypervisor-agnostic
>    (virDomainNetDefValidate() - domain_conf.c:6145)
> 2) post-parse hypervisor-specific
>    (qemuDomainDeviceDefValidateNetwork() - qemu_domain.c:5498)
> 3) domain-start hypervisor-specific
>    (qemuDomainValidateActualNetDef() - qemu_domain.c:5390)
> 
> I placed (3) right next to (2) when I added it, specifically to avoid
> spreading validation all over the code. For the same reason, I decided
> to put this new function right next to (1) - this way if someone needs
> to add validation specific to qemu, they go to one location, and if
> they need to add validation applying to everyone, they go to the
> other. It looks a bit strange to have a public function in between a
> bunch of statics, but I think it's better than the alternative of
> further fragmentation. (I'm open to other ideas though, of course.)
> 
> Signed-off-by: Laine Stump <laine@redhat.com>
> ---
>  src/conf/domain_conf.c   | 22 ++++++++++++++++++++++
>  src/conf/domain_conf.h   |  3 +++
>  src/libvirt_private.syms |  1 +
>  src/libxl/libxl_domain.c |  4 ++++
>  src/libxl/libxl_driver.c |  4 ++++
>  src/lxc/lxc_driver.c     |  4 ++++
>  src/lxc/lxc_process.c    |  4 ++++
>  src/qemu/qemu_domain.c   |  6 ++++++
>  8 files changed, 48 insertions(+)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 01a1a74946..c063f40a4c 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -6094,6 +6094,28 @@ virDomainRedirdevDefValidate(const virDomainDef *def,
>  }
>  
>  
> +int
> +virDomainNetDefRuntimeValidate(const virDomainNetDef *net G_GNUC_UNUSED)
> +{
> +    /* Unlike virDomainNetDefValidate(), which is a static function
> +     * called internally to this file, virDomainActualNetDefValidate()
> +     * is a public function that can be called from a hypervisor after
> +     * it has completely setup the NetDef for use by a domain,
> +     * including possibly allocating a port from the network driver
> +     * (which could change the effective/"actual" type of the NetDef,
> +     * thus changing what should/shouldn't be allowed by validation).
> +     *

The comment here claims the function is named
virDomainActualNetDefValidate. I think that naming is more consistent
rather than invent a new 'runtime' naming scheme, event though runtime
is more semanticly descriptive. (though TBH I wouldn't mind
s/actual/runtime/ in the codebase anyways, the 'actual' naming always
confuses me)

Comment should be fixed either way, I'll leave it up to you whether to
actually rename the function. Series:

Reviewed-by: Cole Robinson <crobinso@redhat.com>

I think there's a conflict in patch 7, git am gave me some confusing
results, so make sure the code you are moving is incorporating and
possible new changes that landed there since this was posted.

- Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 6/7] conf: add hypervisor agnostic, domain start-time, validation function for NetDef
Posted by Laine Stump 5 years ago
On 11/14/19 5:20 PM, Cole Robinson wrote:
> On 10/22/19 9:24 PM, Laine Stump wrote:
>> +int
>> +virDomainNetDefRuntimeValidate(const virDomainNetDef *net G_GNUC_UNUSED)
>> +{
>> +    /* Unlike virDomainNetDefValidate(), which is a static function
>> +     * called internally to this file, virDomainActualNetDefValidate()
>> +     * is a public function that can be called from a hypervisor after
>> +     * it has completely setup the NetDef for use by a domain,
>> +     * including possibly allocating a port from the network driver
>> +     * (which could change the effective/"actual" type of the NetDef,
>> +     * thus changing what should/shouldn't be allowed by validation).
>> +     *
> 
> The comment here claims the function is named
> virDomainActualNetDefValidate. I think that naming is more consistent
> rather than invent a new 'runtime' naming scheme, event though runtime
> is more semanticly descriptive. (though TBH I wouldn't mind
> s/actual/runtime/ in the codebase anyways, the 'actual' naming always
> confuses me)

Well, "actual" is describing *what* it is, and "runtime" is describing 
*when* it is. In most cases I've been fine with the "actual" name, but 
this time it didn't seem to adequately convey the usefulness of the 
function, so I decided to try something else out, and missed the 
function name in the comment.

> 
> Comment should be fixed either way, I'll leave it up to you whether to
> actually rename the function.

Thinking about it more, I guess it's better to remain consistent 
everywhere. Maybe someday someone will find some other adjective that 
works better than actual in all circumstances (it might even be 
"runtime", my personal mental jury is still out), and will switch all 
occurrences at once.

> Series:
> 
> Reviewed-by: Cole Robinson <crobinso@redhat.com>
> 
> I think there's a conflict in patch 7, git am gave me some confusing
> results, so make sure the code you are moving is incorporating and
> possible new changes that landed there since this was posted.

Yeah, I had rebased and was going to resend, but you reviewed the 
original before I got around to it.

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list