diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6e71885488..534c9e74e2 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -10236,14 +10236,14 @@ int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net) * PCI address (if specified) * * Return: index of match if unique match found - * -1 if not found - * -2 if multiple matches + * -1 otherwise and an error is logged */ int virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net) { size_t i; int matchidx = -1; + char mac[VIR_MAC_STRING_BUFLEN]; bool PCIAddrSpecified = virDomainDeviceAddressIsValid(&net->info, VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI); @@ -10258,8 +10258,10 @@ virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net) * specify only vendor and product ID, and there may be * multiples of those. */ - matchidx = -2; /* indicates "multiple matches" to caller */ - break; + virReportError(VIR_ERR_OPERATION_FAILED, + _("multiple devices matching mac address %s found"), + virMacAddrFormat(&net->mac, mac)); + return -1; } if (PCIAddrSpecified) { if (virDevicePCIAddressEqual(&def->nets[i]->info.addr.pci, @@ -10275,6 +10277,11 @@ virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net) matchidx = i; } } + if (matchidx < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("no device matching mac address %s found"), + virMacAddrFormat(&net->mac, mac)); + } return matchidx; } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 05464cbf20..b900bc6e09 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -3768,22 +3768,12 @@ lxcDomainUpdateDeviceConfig(virDomainDefPtr vmdef, int ret = -1; virDomainNetDefPtr net; int idx; - char mac[VIR_MAC_STRING_BUFLEN]; switch (dev->type) { case VIR_DOMAIN_DEVICE_NET: net = dev->data.net; - idx = virDomainNetFindIdx(vmdef, net); - if (idx == -2) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching mac address %s found"), - virMacAddrFormat(&net->mac, mac)); + if ((idx = virDomainNetFindIdx(vmdef, net)) < 0) goto cleanup; - } else if (idx < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("no matching network device was found")); - goto cleanup; - } virDomainNetDefFree(vmdef->nets[idx]); @@ -3813,7 +3803,6 @@ lxcDomainDetachDeviceConfig(virDomainDefPtr vmdef, virDomainNetDefPtr net; virDomainHostdevDefPtr hostdev, det_hostdev; int idx; - char mac[VIR_MAC_STRING_BUFLEN]; switch (dev->type) { case VIR_DOMAIN_DEVICE_DISK: @@ -3829,17 +3818,9 @@ lxcDomainDetachDeviceConfig(virDomainDefPtr vmdef, case VIR_DOMAIN_DEVICE_NET: net = dev->data.net; - idx = virDomainNetFindIdx(vmdef, net); - if (idx == -2) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching mac address %s found"), - virMacAddrFormat(&net->mac, mac)); + if ((idx = virDomainNetFindIdx(vmdef, net)) < 0) goto cleanup; - } else if (idx < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("no matching network device was found")); - goto cleanup; - } + /* this is guaranteed to succeed */ virDomainNetDefFree(virDomainNetRemove(vmdef, idx)); ret = 0; @@ -4650,21 +4631,11 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm, { int detachidx, ret = -1; virDomainNetDefPtr detach = NULL; - char mac[VIR_MAC_STRING_BUFLEN]; virNetDevVPortProfilePtr vport = NULL; - detachidx = virDomainNetFindIdx(vm->def, dev->data.net); - if (detachidx == -2) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching mac address %s found"), - virMacAddrFormat(&dev->data.net->mac, mac)); + if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0) goto cleanup; - } else if (detachidx < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("network device %s not found"), - virMacAddrFormat(&dev->data.net->mac, mac)); - goto cleanup; - } + detach = vm->def->nets[detachidx]; switch (virDomainNetGetActualType(detach)) { diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 9a2de120ad..1d08951ce5 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6763,7 +6763,6 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef, virDomainChrDefPtr chr; virDomainFSDefPtr fs; int idx; - char mac[VIR_MAC_STRING_BUFLEN]; switch (dev->type) { case VIR_DOMAIN_DEVICE_DISK: @@ -6778,17 +6777,9 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef, case VIR_DOMAIN_DEVICE_NET: net = dev->data.net; - idx = virDomainNetFindIdx(vmdef, net); - if (idx == -2) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching mac address %s found"), - virMacAddrFormat(&net->mac, mac)); + if ((idx = virDomainNetFindIdx(vmdef, net)) < 0) return -1; - } else if (idx < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("no matching network device was found")); - return -1; - } + /* this is guaranteed to succeed */ virDomainNetDefFree(virDomainNetRemove(vmdef, idx)); break; @@ -6868,7 +6859,6 @@ qemuDomainUpdateDeviceConfig(virQEMUCapsPtr qemuCaps, virDomainDiskDefPtr orig, disk; virDomainNetDefPtr net; int pos; - char mac[VIR_MAC_STRING_BUFLEN]; switch (dev->type) { @@ -6907,20 +6897,8 @@ qemuDomainUpdateDeviceConfig(virQEMUCapsPtr qemuCaps, case VIR_DOMAIN_DEVICE_NET: net = dev->data.net; - pos = virDomainNetFindIdx(vmdef, net); - if (pos == -2) { - virMacAddrFormat(&net->mac, mac); - virReportError(VIR_ERR_OPERATION_FAILED, - _("couldn't find matching device " - "with mac address %s"), mac); + if ((pos = virDomainNetFindIdx(vmdef, net)) < 0) return -1; - } else if (pos < 0) { - virMacAddrFormat(&net->mac, mac); - virReportError(VIR_ERR_OPERATION_FAILED, - _("couldn't find matching device " - "with mac address %s"), mac); - return -1; - } virDomainNetDefFree(vmdef->nets[pos]); diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index e81bc7a301..bf92aec542 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -3362,21 +3362,10 @@ qemuDomainDetachNetDevice(virQEMUDriverPtr driver, qemuDomainObjPrivatePtr priv = vm->privateData; int vlan; char *hostnet_name = NULL; - char mac[VIR_MAC_STRING_BUFLEN]; - detachidx = virDomainNetFindIdx(vm->def, dev->data.net); - if (detachidx == -2) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("multiple devices matching mac address %s found"), - virMacAddrFormat(&dev->data.net->mac, mac)); + if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0) goto cleanup; - } - else if (detachidx < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("network device %s not found"), - virMacAddrFormat(&dev->data.net->mac, mac)); - goto cleanup; - } + detach = vm->def->nets[detachidx]; if (virDomainNetGetActualType(detach) == VIR_DOMAIN_NET_TYPE_HOSTDEV) {