Move error reporting into virDomainNetFindIdx

Every caller checked the return value and logged an error
- one if no device with the specified MAC was found,
other if there were multiple devices matching the MAC address
(except for qemuDomainUpdateDeviceConfig which logged the same
 message in both cases).

Move the error reporting into virDomainNetFindIdx, since in both cases,
we couldn't find one single match - it's just the error messages that
differ.
This commit is contained in:
Ján Tomko 2014-04-01 15:56:55 +02:00
parent 8feec44a09
commit 2fbae1b2a9
4 changed files with 21 additions and 76 deletions

View File

@ -10236,14 +10236,14 @@ int virDomainNetInsert(virDomainDefPtr def, virDomainNetDefPtr net)
* PCI address (if specified) * PCI address (if specified)
* *
* Return: index of match if unique match found * Return: index of match if unique match found
* -1 if not found * -1 otherwise and an error is logged
* -2 if multiple matches
*/ */
int int
virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net) virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net)
{ {
size_t i; size_t i;
int matchidx = -1; int matchidx = -1;
char mac[VIR_MAC_STRING_BUFLEN];
bool PCIAddrSpecified = virDomainDeviceAddressIsValid(&net->info, bool PCIAddrSpecified = virDomainDeviceAddressIsValid(&net->info,
VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI); 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 * specify only vendor and product ID, and there may be
* multiples of those. * multiples of those.
*/ */
matchidx = -2; /* indicates "multiple matches" to caller */ virReportError(VIR_ERR_OPERATION_FAILED,
break; _("multiple devices matching mac address %s found"),
virMacAddrFormat(&net->mac, mac));
return -1;
} }
if (PCIAddrSpecified) { if (PCIAddrSpecified) {
if (virDevicePCIAddressEqual(&def->nets[i]->info.addr.pci, if (virDevicePCIAddressEqual(&def->nets[i]->info.addr.pci,
@ -10275,6 +10277,11 @@ virDomainNetFindIdx(virDomainDefPtr def, virDomainNetDefPtr net)
matchidx = i; matchidx = i;
} }
} }
if (matchidx < 0) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("no device matching mac address %s found"),
virMacAddrFormat(&net->mac, mac));
}
return matchidx; return matchidx;
} }

View File

@ -3768,22 +3768,12 @@ lxcDomainUpdateDeviceConfig(virDomainDefPtr vmdef,
int ret = -1; int ret = -1;
virDomainNetDefPtr net; virDomainNetDefPtr net;
int idx; int idx;
char mac[VIR_MAC_STRING_BUFLEN];
switch (dev->type) { switch (dev->type) {
case VIR_DOMAIN_DEVICE_NET: case VIR_DOMAIN_DEVICE_NET:
net = dev->data.net; net = dev->data.net;
idx = virDomainNetFindIdx(vmdef, net); if ((idx = virDomainNetFindIdx(vmdef, net)) < 0)
if (idx == -2) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("multiple devices matching mac address %s found"),
virMacAddrFormat(&net->mac, mac));
goto cleanup; goto cleanup;
} else if (idx < 0) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("no matching network device was found"));
goto cleanup;
}
virDomainNetDefFree(vmdef->nets[idx]); virDomainNetDefFree(vmdef->nets[idx]);
@ -3813,7 +3803,6 @@ lxcDomainDetachDeviceConfig(virDomainDefPtr vmdef,
virDomainNetDefPtr net; virDomainNetDefPtr net;
virDomainHostdevDefPtr hostdev, det_hostdev; virDomainHostdevDefPtr hostdev, det_hostdev;
int idx; int idx;
char mac[VIR_MAC_STRING_BUFLEN];
switch (dev->type) { switch (dev->type) {
case VIR_DOMAIN_DEVICE_DISK: case VIR_DOMAIN_DEVICE_DISK:
@ -3829,17 +3818,9 @@ lxcDomainDetachDeviceConfig(virDomainDefPtr vmdef,
case VIR_DOMAIN_DEVICE_NET: case VIR_DOMAIN_DEVICE_NET:
net = dev->data.net; net = dev->data.net;
idx = virDomainNetFindIdx(vmdef, net); if ((idx = virDomainNetFindIdx(vmdef, net)) < 0)
if (idx == -2) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("multiple devices matching mac address %s found"),
virMacAddrFormat(&net->mac, mac));
goto cleanup; 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 */ /* this is guaranteed to succeed */
virDomainNetDefFree(virDomainNetRemove(vmdef, idx)); virDomainNetDefFree(virDomainNetRemove(vmdef, idx));
ret = 0; ret = 0;
@ -4650,21 +4631,11 @@ lxcDomainDetachDeviceNetLive(virDomainObjPtr vm,
{ {
int detachidx, ret = -1; int detachidx, ret = -1;
virDomainNetDefPtr detach = NULL; virDomainNetDefPtr detach = NULL;
char mac[VIR_MAC_STRING_BUFLEN];
virNetDevVPortProfilePtr vport = NULL; virNetDevVPortProfilePtr vport = NULL;
detachidx = virDomainNetFindIdx(vm->def, dev->data.net); if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0)
if (detachidx == -2) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("multiple devices matching mac address %s found"),
virMacAddrFormat(&dev->data.net->mac, mac));
goto cleanup; 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]; detach = vm->def->nets[detachidx];
switch (virDomainNetGetActualType(detach)) { switch (virDomainNetGetActualType(detach)) {

View File

@ -6763,7 +6763,6 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
virDomainChrDefPtr chr; virDomainChrDefPtr chr;
virDomainFSDefPtr fs; virDomainFSDefPtr fs;
int idx; int idx;
char mac[VIR_MAC_STRING_BUFLEN];
switch (dev->type) { switch (dev->type) {
case VIR_DOMAIN_DEVICE_DISK: case VIR_DOMAIN_DEVICE_DISK:
@ -6778,17 +6777,9 @@ qemuDomainDetachDeviceConfig(virDomainDefPtr vmdef,
case VIR_DOMAIN_DEVICE_NET: case VIR_DOMAIN_DEVICE_NET:
net = dev->data.net; net = dev->data.net;
idx = virDomainNetFindIdx(vmdef, net); if ((idx = virDomainNetFindIdx(vmdef, net)) < 0)
if (idx == -2) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("multiple devices matching mac address %s found"),
virMacAddrFormat(&net->mac, mac));
return -1; 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 */ /* this is guaranteed to succeed */
virDomainNetDefFree(virDomainNetRemove(vmdef, idx)); virDomainNetDefFree(virDomainNetRemove(vmdef, idx));
break; break;
@ -6868,7 +6859,6 @@ qemuDomainUpdateDeviceConfig(virQEMUCapsPtr qemuCaps,
virDomainDiskDefPtr orig, disk; virDomainDiskDefPtr orig, disk;
virDomainNetDefPtr net; virDomainNetDefPtr net;
int pos; int pos;
char mac[VIR_MAC_STRING_BUFLEN];
switch (dev->type) { switch (dev->type) {
@ -6907,20 +6897,8 @@ qemuDomainUpdateDeviceConfig(virQEMUCapsPtr qemuCaps,
case VIR_DOMAIN_DEVICE_NET: case VIR_DOMAIN_DEVICE_NET:
net = dev->data.net; net = dev->data.net;
pos = virDomainNetFindIdx(vmdef, net); if ((pos = virDomainNetFindIdx(vmdef, net)) < 0)
if (pos == -2) {
virMacAddrFormat(&net->mac, mac);
virReportError(VIR_ERR_OPERATION_FAILED,
_("couldn't find matching device "
"with mac address %s"), mac);
return -1; 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]); virDomainNetDefFree(vmdef->nets[pos]);

View File

@ -3362,21 +3362,10 @@ qemuDomainDetachNetDevice(virQEMUDriverPtr driver,
qemuDomainObjPrivatePtr priv = vm->privateData; qemuDomainObjPrivatePtr priv = vm->privateData;
int vlan; int vlan;
char *hostnet_name = NULL; char *hostnet_name = NULL;
char mac[VIR_MAC_STRING_BUFLEN];
detachidx = virDomainNetFindIdx(vm->def, dev->data.net); if ((detachidx = virDomainNetFindIdx(vm->def, dev->data.net)) < 0)
if (detachidx == -2) {
virReportError(VIR_ERR_OPERATION_FAILED,
_("multiple devices matching mac address %s found"),
virMacAddrFormat(&dev->data.net->mac, mac));
goto cleanup; 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]; detach = vm->def->nets[detachidx];
if (virDomainNetGetActualType(detach) == VIR_DOMAIN_NET_TYPE_HOSTDEV) { if (virDomainNetGetActualType(detach) == VIR_DOMAIN_NET_TYPE_HOSTDEV) {