network: make networkCreateInterfacePool more robust

networkCreateInterfacePool was a bit loose in its error cleanup, which
could result in a network definition with interfaces in the pool that
were NULL. This would in turn lead to a libvirtd crash when a guest
tried to attach an interface using the network with that pool.

In particular this would happen when creating a pool to be used for
macvtap connections. macvtap needs the netdev name of the virtual
function in order to use it, and each VF only has a netdev name if it
is currently bound to a network driver. If one of the VFs of a PF
happened to be bound to the pci-stub or vfio-pci driver (indicating
it's already in use for PCI passthrough), or no driver at all, it
would have no name. In this case networkCreateInterfacePool would
return an error, but would leave the netdef->forward.nifs set to the
total number of VFs in the PF. The interface attach that triggered
calling of networkCreateInterfacePool (it uses a "lazy fill" strategy)
would simply fail, but the very next attempt to attach an interface
using the same network pool would result in a crash.

This patch refactors networkCreateInterfacePool to bring it more in
line with current coding practices (label name, use of a switch with
no default case) as well as providing the following two changes to
behavior:

1) If a VF with no netdev name is encountered, just log a warning and
continue; only fail if exactly 0 devices are found to put in the pool.

2) If the function fails, clean up any partial interface pool and set
netdef->forward.nifs to 0.

This resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1111455
This commit is contained in:
Laine Stump 2014-08-05 16:40:52 -04:00
parent 7dc11d6be4
commit cd7759cb96

View File

@ -3608,65 +3608,98 @@ int networkRegister(void)
static int static int
networkCreateInterfacePool(virNetworkDefPtr netdef) networkCreateInterfacePool(virNetworkDefPtr netdef)
{ {
size_t num_virt_fns = 0; size_t numVirtFns = 0;
char **vfname = NULL; char **vfNames = NULL;
virPCIDeviceAddressPtr *virt_fns; virPCIDeviceAddressPtr *virtFns;
int ret = -1; int ret = -1;
size_t i; size_t i;
if ((virNetDevGetVirtualFunctions(netdef->forward.pfs->dev, if ((virNetDevGetVirtualFunctions(netdef->forward.pfs->dev,
&vfname, &virt_fns, &num_virt_fns)) < 0) { &vfNames, &virtFns, &numVirtFns)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Could not get Virtual functions on %s"), _("Could not get Virtual functions on %s"),
netdef->forward.pfs->dev); netdef->forward.pfs->dev);
goto finish; goto cleanup;
} }
if (num_virt_fns == 0) { netdef->forward.nifs = 0;
virReportError(VIR_ERR_INTERNAL_ERROR, if (VIR_ALLOC_N(netdef->forward.ifs, numVirtFns) < 0)
_("No Vf's present on SRIOV PF %s"), goto cleanup;
netdef->forward.pfs->dev);
goto finish;
}
if (VIR_ALLOC_N(netdef->forward.ifs, num_virt_fns) < 0) for (i = 0; i < numVirtFns; i++) {
goto finish; virPCIDeviceAddressPtr thisVirtFn = virtFns[i];
const char *thisName = vfNames[i];
virNetworkForwardIfDefPtr thisIf
= &netdef->forward.ifs[netdef->forward.nifs];
netdef->forward.nifs = num_virt_fns; switch (netdef->forward.type) {
case VIR_NETWORK_FORWARD_BRIDGE:
for (i = 0; i < netdef->forward.nifs; i++) { case VIR_NETWORK_FORWARD_PRIVATE:
if ((netdef->forward.type == VIR_NETWORK_FORWARD_BRIDGE) || case VIR_NETWORK_FORWARD_VEPA:
(netdef->forward.type == VIR_NETWORK_FORWARD_PRIVATE) || case VIR_NETWORK_FORWARD_PASSTHROUGH:
(netdef->forward.type == VIR_NETWORK_FORWARD_VEPA) || if (thisName) {
(netdef->forward.type == VIR_NETWORK_FORWARD_PASSTHROUGH)) { if (VIR_STRDUP(thisIf->device.dev, thisName) < 0)
netdef->forward.ifs[i].type = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV; goto cleanup;
if (vfname[i]) { thisIf->type = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV;
if (VIR_STRDUP(netdef->forward.ifs[i].device.dev, vfname[i]) < 0) netdef->forward.nifs++;
goto finish;
} else { } else {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", VIR_WARN("VF %zu of SRIOV PF %s couldn't be added to the "
_("Direct mode types require interface names")); "interface pool because it isn't bound "
goto finish; "to a network driver - possibly in use elsewhere",
i, netdef->forward.pfs->dev);
} }
} break;
else if (netdef->forward.type == VIR_NETWORK_FORWARD_HOSTDEV) {
case VIR_NETWORK_FORWARD_HOSTDEV:
/* VF's are always PCI devices */ /* VF's are always PCI devices */
netdef->forward.ifs[i].type = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_PCI; thisIf->type = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_PCI;
netdef->forward.ifs[i].device.pci.domain = virt_fns[i]->domain; thisIf->device.pci.domain = thisVirtFn->domain;
netdef->forward.ifs[i].device.pci.bus = virt_fns[i]->bus; thisIf->device.pci.bus = thisVirtFn->bus;
netdef->forward.ifs[i].device.pci.slot = virt_fns[i]->slot; thisIf->device.pci.slot = thisVirtFn->slot;
netdef->forward.ifs[i].device.pci.function = virt_fns[i]->function; thisIf->device.pci.function = thisVirtFn->function;
netdef->forward.nifs++;
break;
case VIR_NETWORK_FORWARD_NONE:
case VIR_NETWORK_FORWARD_NAT:
case VIR_NETWORK_FORWARD_ROUTE:
case VIR_NETWORK_FORWARD_LAST:
/* by definition these will never be encountered here */
break;
} }
} }
if (netdef->forward.nifs == 0) {
/* If we don't get at least one interface in the pool, declare
* failure
*/
virReportError(VIR_ERR_INTERNAL_ERROR,
_("No usable Vf's present on SRIOV PF %s"),
netdef->forward.pfs->dev);
goto cleanup;
}
ret = 0; ret = 0;
finish: cleanup:
for (i = 0; i < num_virt_fns; i++) { if (ret < 0) {
VIR_FREE(vfname[i]); /* free all the entries made before error */
VIR_FREE(virt_fns[i]); for (i = 0; i < netdef->forward.nifs; i++) {
if (netdef->forward.ifs[i].type
== VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV)
VIR_FREE(netdef->forward.ifs[i].device.dev);
}
netdef->forward.nifs = 0;
} }
VIR_FREE(vfname); if (netdef->forward.nifs == 0)
VIR_FREE(virt_fns); VIR_FREE(netdef->forward.ifs);
for (i = 0; i < numVirtFns; i++) {
VIR_FREE(vfNames[i]);
VIR_FREE(virtFns[i]);
}
VIR_FREE(vfNames);
VIR_FREE(virtFns);
return ret; return ret;
} }