libvirt was unconditionally calling virNetDevBandwidthClear() for
every interface (and network bridge) of a type that supported
bandwidth, whether it actually had anything set or not. This doesn't
hurt anything (unless ifname == NULL!), but is wasteful.
This patch makes sure that all calls to virNetDevBandwidthClear() are
qualified by checking that ifname != NULL and that it really had some
bandwidth setup done.
(NB: an sa_assert(detach->ifname) was added in
lxcDomainDetachDeviceNetLive() because the preceding new check of
detach->ifname before calling virNetDevBandwidthClear() caused
Coverity to complain about a possible null dereference.)
---
src/conf/netdev_bandwidth_conf.c | 6 ++++--
src/lxc/lxc_driver.c | 7 ++++++-
src/network/bridge_driver.c | 6 ++++--
src/qemu/qemu_hotplug.c | 4 +++-
4 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/conf/netdev_bandwidth_conf.c b/src/conf/netdev_bandwidth_conf.c
index c3e0f9f..42d42d6 100644
--- a/src/conf/netdev_bandwidth_conf.c
+++ b/src/conf/netdev_bandwidth_conf.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009-2014 Red Hat, Inc.
+ * Copyright (C) 2009-2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -285,7 +285,9 @@ virDomainClearNetBandwidth(virDomainObjPtr vm)
for (i = 0; i < vm->def->nnets; i++) {
type = virDomainNetGetActualType(vm->def->nets[i]);
- if (virNetDevSupportBandwidth(type))
+ if (vm->def->nets[i]->ifname &&
+ virDomainNetGetActualBandwidth(vm->def->nets[i]) &&
+ virNetDevSupportBandwidth(type))
virNetDevBandwidthClear(vm->def->nets[i]->ifname);
}
}
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 3adb21d..87fcd98 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -4650,13 +4650,18 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm,
actualType = virDomainNetGetActualType(detach);
/* clear network bandwidth */
- if (virNetDevSupportBandwidth(actualType) &&
+ if (detach->ifname &&
+ virDomainNetGetActualBandwidth(detach) &&
+ virNetDevSupportBandwidth(actualType) &&
virNetDevBandwidthClear(detach->ifname))
goto cleanup;
switch (actualType) {
case VIR_DOMAIN_NET_TYPE_BRIDGE:
case VIR_DOMAIN_NET_TYPE_NETWORK:
+ /* make sure Coverity knows that detach->ifname is valid */
+ sa_assert(detach->ifname);
+
if (virNetDevVethDelete(detach->ifname) < 0) {
virDomainAuditNet(vm, detach, NULL, "detach", false);
goto cleanup;
diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index f240d3b..1209609 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -2096,7 +2096,8 @@ networkStartNetworkVirtual(virNetworkObjPtr network)
return 0;
err5:
- virNetDevBandwidthClear(network->def->bridge);
+ if (network->def->bandwidth)
+ virNetDevBandwidthClear(network->def->bridge);
err4:
if (!save_err)
@@ -2142,7 +2143,8 @@ networkStartNetworkVirtual(virNetworkObjPtr network)
static int networkShutdownNetworkVirtual(virNetworkObjPtr network)
{
- virNetDevBandwidthClear(network->def->bridge);
+ if (network->def->bandwidth)
+ virNetDevBandwidthClear(network->def->bridge);
if (network->radvdPid > 0) {
char *radvdpidbase;
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 8691c7e..9518abd 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3750,7 +3750,9 @@ qemuDomainDetachNetDevice(virQEMUDriverPtr driver,
goto cleanup;
}
- if (virNetDevSupportBandwidth(virDomainNetGetActualType(detach)) &&
+ if (detach->ifname &&
+ virDomainNetGetActualBandwidth(detach) &&
+ virNetDevSupportBandwidth(virDomainNetGetActualType(detach)) &&
virNetDevBandwidthClear(detach->ifname) < 0)
VIR_WARN("cannot clear bandwidth setting for device : %s",
detach->ifname);
--
2.1.0