nodedev: handle mdevs from multiple parents

Due to a rather unfortunate misunderstanding, we were parsing the list
of defined devices from mdevctl incorrectly. Since my primary
development machine only has a single device capable of mdevs, I
apparently neglected to test multiple parent devices and made some
assumptions based on reading the mdevctl code. These assumptions turned
out to be incorrect, so the parsing failed when devices from more than
one parent device were returned.

The details: mdevctl returns an array of objects representing the
defined devices. But instead of an array of multiple objects (with each
object representing a parent device), the array always contains only a
single object. That object has a separate property for each parent
device.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Jonathon Jongsma 2021-06-10 13:15:37 -05:00 committed by Michal Privoznik
parent 9c3b6b7a82
commit e9b534905f
2 changed files with 23 additions and 22 deletions

View File

@ -1056,6 +1056,7 @@ nodeDeviceParseMdevctlJSON(const char *jsonstring,
size_t noutdevs = 0; size_t noutdevs = 0;
size_t i; size_t i;
size_t j; size_t j;
virJSONValue *obj;
json_devicelist = virJSONValueFromString(jsonstring); json_devicelist = virJSONValueFromString(jsonstring);
@ -1065,31 +1066,33 @@ nodeDeviceParseMdevctlJSON(const char *jsonstring,
goto error; goto error;
} }
n = virJSONValueArraySize(json_devicelist); /* mdevctl list --dumpjson produces an output that is an array that
* contains only a single object which contains a property for each parent
* device */
if (virJSONValueArraySize(json_devicelist) != 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unexpected format for mdevctl response"));
goto error;
}
obj = virJSONValueArrayGet(json_devicelist, 0);
if (!virJSONValueIsObject(obj)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("device list is not an object"));
goto error;
}
n = virJSONValueObjectKeysNumber(obj);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
virJSONValue *obj = virJSONValueArrayGet(json_devicelist, i);
const char *parent; const char *parent;
virJSONValue *child_array; virJSONValue *child_array;
int nchildren; int nchildren;
if (!virJSONValueIsObject(obj)) { /* The key of each object property is the name of a parent device
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", * which maps to an array of child devices */
_("Parent device is not an object")); parent = virJSONValueObjectGetKey(obj, i);
goto error; child_array = virJSONValueObjectGetValue(obj, i);
}
/* mdevctl returns an array of objects. Each object is a parent device
* object containing a single key-value pair which maps from the name
* of the parent device to an array of child devices */
if (virJSONValueObjectKeysNumber(obj) != 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Unexpected format for parent device object"));
goto error;
}
parent = virJSONValueObjectGetKey(obj, 0);
child_array = virJSONValueObjectGetValue(obj, 0);
if (!virJSONValueIsArray(child_array)) { if (!virJSONValueIsArray(child_array)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",

View File

@ -24,9 +24,7 @@
] ]
} }
} }
] ],
},
{
"matrix": [ "matrix": [
{ "783e6dbb-ea0e-411f-94e2-717eaad438bf": { { "783e6dbb-ea0e-411f-94e2-717eaad438bf": {
"mdev_type": "vfio_ap-passthrough", "mdev_type": "vfio_ap-passthrough",