2017-03-22 14:50:33 +00:00
|
|
|
/*
|
|
|
|
* virmdev.c: helper APIs for managing host mediated devices
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "dirname.h"
|
|
|
|
#include "virmdev.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2017-03-27 15:38:33 +00:00
|
|
|
#define MDEV_SYSFS_DEVICES "/sys/bus/mdev/devices/"
|
|
|
|
|
2017-03-22 14:50:33 +00:00
|
|
|
VIR_LOG_INIT("util.mdev");
|
|
|
|
|
|
|
|
struct _virMediatedDevice {
|
|
|
|
char *path; /* sysfs path */
|
|
|
|
virMediatedDeviceModelType model;
|
|
|
|
|
|
|
|
char *used_by_drvname;
|
|
|
|
char *used_by_domname;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virMediatedDeviceList {
|
|
|
|
virObjectLockable parent;
|
|
|
|
|
|
|
|
size_t count;
|
|
|
|
virMediatedDevicePtr *devs;
|
|
|
|
};
|
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virMediatedDeviceModel,
|
|
|
|
VIR_MDEV_MODEL_TYPE_LAST,
|
2018-05-07 14:41:14 +00:00
|
|
|
"vfio-pci",
|
2018-10-18 14:54:07 +00:00
|
|
|
"vfio-ccw",
|
2019-01-20 16:30:15 +00:00
|
|
|
"vfio-ap",
|
|
|
|
);
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
static virClassPtr virMediatedDeviceListClass;
|
|
|
|
|
|
|
|
static void
|
|
|
|
virMediatedDeviceListDispose(void *obj);
|
|
|
|
|
|
|
|
static int
|
|
|
|
virMediatedOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virMediatedDeviceList, virClassForObjectLockable()))
|
2017-03-22 14:50:33 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virMediated);
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
static int
|
|
|
|
virMediatedDeviceGetSysfsDeviceAPI(virMediatedDevicePtr dev,
|
|
|
|
char **device_api)
|
|
|
|
{
|
2018-07-24 15:52:13 +00:00
|
|
|
VIR_AUTOFREE(char *) buf = NULL;
|
|
|
|
VIR_AUTOFREE(char *) file = NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&file, "%s/mdev_type/device_api", dev->path) < 0)
|
2018-07-24 15:52:13 +00:00
|
|
|
return -1;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
/* TODO - make this a generic method to access sysfs files for various
|
|
|
|
* kinds of devices
|
|
|
|
*/
|
|
|
|
if (!virFileExists(file)) {
|
|
|
|
virReportSystemError(errno, _("failed to read '%s'"), file);
|
2018-07-24 15:52:13 +00:00
|
|
|
return -1;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileReadAll(file, 1024, &buf) < 0)
|
2018-07-24 15:52:13 +00:00
|
|
|
return -1;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
if ((tmp = strchr(buf, '\n')))
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
*device_api = buf;
|
|
|
|
buf = NULL;
|
|
|
|
|
2018-07-24 15:52:13 +00:00
|
|
|
return 0;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virMediatedDeviceCheckModel(virMediatedDevicePtr dev,
|
|
|
|
virMediatedDeviceModelType model)
|
|
|
|
{
|
2018-07-24 15:52:13 +00:00
|
|
|
VIR_AUTOFREE(char *) dev_api = NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
int actual_model;
|
|
|
|
|
|
|
|
if (virMediatedDeviceGetSysfsDeviceAPI(dev, &dev_api) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* safeguard in case we've got an older libvirt which doesn't know newer
|
|
|
|
* device_api models yet
|
|
|
|
*/
|
|
|
|
if ((actual_model = virMediatedDeviceModelTypeFromString(dev_api)) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("device API '%s' not supported yet"),
|
|
|
|
dev_api);
|
2018-07-24 15:52:13 +00:00
|
|
|
return -1;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (actual_model != model) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("invalid device API '%s' for device %s: "
|
|
|
|
"device only supports '%s'"),
|
|
|
|
virMediatedDeviceModelTypeToString(model),
|
|
|
|
dev->path, dev_api);
|
2018-07-24 15:52:13 +00:00
|
|
|
return -1;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:13 +00:00
|
|
|
return 0;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
|
|
|
virMediatedDeviceNew(const char *uuidstr, virMediatedDeviceModelType model)
|
|
|
|
{
|
|
|
|
virMediatedDevicePtr ret = NULL;
|
2018-07-24 15:52:14 +00:00
|
|
|
VIR_AUTOPTR(virMediatedDevice) dev = NULL;
|
2018-07-24 15:52:13 +00:00
|
|
|
VIR_AUTOFREE(char *) sysfspath = NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
2018-03-16 09:06:04 +00:00
|
|
|
if (!(sysfspath = virMediatedDeviceGetSysfsPath(uuidstr)))
|
2018-07-24 15:52:14 +00:00
|
|
|
return NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
2018-03-16 09:06:04 +00:00
|
|
|
if (!virFileExists(sysfspath)) {
|
|
|
|
virReportError(VIR_ERR_DEVICE_MISSING,
|
|
|
|
_("mediated device '%s' not found"), uuidstr);
|
2018-07-24 15:52:14 +00:00
|
|
|
return NULL;
|
2018-03-16 09:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_ALLOC(dev) < 0)
|
2018-07-24 15:52:14 +00:00
|
|
|
return NULL;
|
2018-03-16 09:06:04 +00:00
|
|
|
|
|
|
|
VIR_STEAL_PTR(dev->path, sysfspath);
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
/* Check whether the user-provided model corresponds with the actually
|
|
|
|
* supported mediated device's API.
|
|
|
|
*/
|
|
|
|
if (virMediatedDeviceCheckModel(dev, model))
|
2018-07-24 15:52:14 +00:00
|
|
|
return NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
dev->model = model;
|
|
|
|
VIR_STEAL_PTR(ret, dev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
2017-03-27 15:38:33 +00:00
|
|
|
virMediatedDeviceNew(const char *uuidstr ATTRIBUTE_UNUSED,
|
|
|
|
virMediatedDeviceModelType model ATTRIBUTE_UNUSED)
|
2017-03-22 14:50:33 +00:00
|
|
|
{
|
2017-03-27 15:38:33 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("mediated devices are not supported on non-linux "
|
|
|
|
"platforms"));
|
2017-03-22 14:50:33 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
|
|
|
void
|
|
|
|
virMediatedDeviceFree(virMediatedDevicePtr dev)
|
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
VIR_FREE(dev->path);
|
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
virMediatedDeviceGetPath(virMediatedDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Returns an absolute canonicalized path to the device used to control the
|
|
|
|
* mediated device's IOMMU group (e.g. "/dev/vfio/15"). Caller is responsible
|
|
|
|
* for freeing the result.
|
|
|
|
*/
|
|
|
|
char *
|
2017-04-26 12:47:15 +00:00
|
|
|
virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
|
2017-03-22 14:50:33 +00:00
|
|
|
{
|
2018-07-24 15:52:13 +00:00
|
|
|
VIR_AUTOFREE(char *) result_path = NULL;
|
|
|
|
VIR_AUTOFREE(char *) iommu_path = NULL;
|
|
|
|
VIR_AUTOFREE(char *) dev_path = virMediatedDeviceGetSysfsPath(uuidstr);
|
2017-03-22 14:50:33 +00:00
|
|
|
char *vfio_path = NULL;
|
|
|
|
|
2017-04-26 12:47:15 +00:00
|
|
|
if (!dev_path)
|
2017-03-22 14:50:33 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-04-26 12:47:15 +00:00
|
|
|
if (virAsprintf(&iommu_path, "%s/iommu_group", dev_path) < 0)
|
2018-07-24 15:52:13 +00:00
|
|
|
return NULL;
|
2017-04-26 12:47:15 +00:00
|
|
|
|
2017-03-22 14:50:33 +00:00
|
|
|
if (!virFileExists(iommu_path)) {
|
|
|
|
virReportSystemError(errno, _("failed to access '%s'"), iommu_path);
|
2018-07-24 15:52:13 +00:00
|
|
|
return NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 12:47:15 +00:00
|
|
|
if (virFileResolveLink(iommu_path, &result_path) < 0) {
|
2017-03-22 14:50:33 +00:00
|
|
|
virReportSystemError(errno, _("failed to resolve '%s'"), iommu_path);
|
2018-07-24 15:52:13 +00:00
|
|
|
return NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 12:47:15 +00:00
|
|
|
if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(result_path)) < 0)
|
2018-07-24 15:52:13 +00:00
|
|
|
return NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
|
|
|
|
return vfio_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-26 12:47:15 +00:00
|
|
|
virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr)
|
2017-03-22 14:50:33 +00:00
|
|
|
{
|
2018-07-24 15:52:13 +00:00
|
|
|
VIR_AUTOFREE(char *) vfio_path = NULL;
|
2017-03-22 14:50:33 +00:00
|
|
|
char *group_num_str = NULL;
|
|
|
|
unsigned int group_num = -1;
|
|
|
|
|
2017-04-26 12:47:15 +00:00
|
|
|
if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(uuidstr)))
|
2017-03-22 14:50:33 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
group_num_str = last_component(vfio_path);
|
|
|
|
ignore_value(virStrToLong_ui(group_num_str, NULL, 10, &group_num));
|
|
|
|
|
|
|
|
return group_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virMediatedDeviceGetUsedBy(virMediatedDevicePtr dev,
|
|
|
|
const char **drvname, const char **domname)
|
|
|
|
{
|
|
|
|
*drvname = dev->used_by_drvname;
|
|
|
|
*domname = dev->used_by_domname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev,
|
|
|
|
const char *drvname,
|
|
|
|
const char *domname)
|
|
|
|
{
|
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
|
|
|
if (VIR_STRDUP(dev->used_by_drvname, drvname) < 0)
|
|
|
|
return -1;
|
|
|
|
if (VIR_STRDUP(dev->used_by_domname, domname) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDeviceListPtr
|
|
|
|
virMediatedDeviceListNew(void)
|
|
|
|
{
|
|
|
|
virMediatedDeviceListPtr list;
|
|
|
|
|
|
|
|
if (virMediatedInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(list = virObjectLockableNew(virMediatedDeviceListClass)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virMediatedDeviceListDispose(void *obj)
|
|
|
|
{
|
|
|
|
virMediatedDeviceListPtr list = obj;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
virMediatedDeviceFree(list->devs[i]);
|
|
|
|
list->devs[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->count = 0;
|
|
|
|
VIR_FREE(list->devs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 07:24:31 +00:00
|
|
|
/* The reason for @dev to be double pointer is that VIR_APPEND_ELEMENT clears
|
|
|
|
* the pointer and we need to clear the original not a copy on the stack
|
|
|
|
*/
|
2017-03-22 14:50:33 +00:00
|
|
|
int
|
|
|
|
virMediatedDeviceListAdd(virMediatedDeviceListPtr list,
|
2017-04-28 07:24:31 +00:00
|
|
|
virMediatedDevicePtr *dev)
|
2017-03-22 14:50:33 +00:00
|
|
|
{
|
2017-04-28 07:24:31 +00:00
|
|
|
if (virMediatedDeviceListFind(list, *dev)) {
|
2017-03-22 14:50:33 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2017-04-28 07:24:31 +00:00
|
|
|
_("device %s is already in use"), (*dev)->path);
|
2017-03-22 14:50:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-04-28 07:24:31 +00:00
|
|
|
return VIR_APPEND_ELEMENT(list->devs, list->count, *dev);
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
|
|
|
virMediatedDeviceListGet(virMediatedDeviceListPtr list,
|
|
|
|
ssize_t idx)
|
|
|
|
{
|
|
|
|
if (idx < 0 || idx >= list->count)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t
|
|
|
|
virMediatedDeviceListCount(virMediatedDeviceListPtr list)
|
|
|
|
{
|
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
|
|
|
virMediatedDeviceListStealIndex(virMediatedDeviceListPtr list,
|
|
|
|
ssize_t idx)
|
|
|
|
{
|
|
|
|
virMediatedDevicePtr ret;
|
|
|
|
|
|
|
|
if (idx < 0 || idx >= list->count)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = list->devs[idx];
|
|
|
|
VIR_DELETE_ELEMENT(list->devs, idx, list->count);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
|
|
|
virMediatedDeviceListSteal(virMediatedDeviceListPtr list,
|
|
|
|
virMediatedDevicePtr dev)
|
|
|
|
{
|
|
|
|
int idx = virMediatedDeviceListFindIndex(list, dev);
|
|
|
|
|
|
|
|
return virMediatedDeviceListStealIndex(list, idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virMediatedDeviceListDel(virMediatedDeviceListPtr list,
|
|
|
|
virMediatedDevicePtr dev)
|
|
|
|
{
|
2018-07-27 21:17:34 +00:00
|
|
|
virMediatedDeviceFree(virMediatedDeviceListSteal(list, dev));
|
2017-03-22 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virMediatedDeviceListFindIndex(virMediatedDeviceListPtr list,
|
|
|
|
virMediatedDevicePtr dev)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
virMediatedDevicePtr other = list->devs[i];
|
|
|
|
if (STREQ(other->path, dev->path))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virMediatedDevicePtr
|
|
|
|
virMediatedDeviceListFind(virMediatedDeviceListPtr list,
|
|
|
|
virMediatedDevicePtr dev)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if ((idx = virMediatedDeviceListFindIndex(list, dev)) >= 0)
|
|
|
|
return list->devs[idx];
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
virMediatedDeviceIsUsed(virMediatedDevicePtr dev,
|
|
|
|
virMediatedDeviceListPtr list)
|
|
|
|
{
|
|
|
|
const char *drvname, *domname;
|
|
|
|
virMediatedDevicePtr tmp = NULL;
|
|
|
|
|
|
|
|
if ((tmp = virMediatedDeviceListFind(list, dev))) {
|
|
|
|
virMediatedDeviceGetUsedBy(tmp, &drvname, &domname);
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("mediated device %s is in use by "
|
|
|
|
"driver %s, domain %s"),
|
|
|
|
tmp->path, drvname, domname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
virMediatedDeviceGetSysfsPath(const char *uuidstr)
|
|
|
|
{
|
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
ignore_value(virAsprintf(&ret, MDEV_SYSFS_DEVICES "%s", uuidstr));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virMediatedDeviceListMarkDevices(virMediatedDeviceListPtr dst,
|
|
|
|
virMediatedDeviceListPtr src,
|
|
|
|
const char *drvname,
|
|
|
|
const char *domname)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
size_t count = virMediatedDeviceListCount(src);
|
|
|
|
size_t i, j;
|
|
|
|
|
|
|
|
virObjectLock(dst);
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
virMediatedDevicePtr mdev = virMediatedDeviceListGet(src, i);
|
|
|
|
|
|
|
|
if (virMediatedDeviceIsUsed(mdev, dst) ||
|
|
|
|
virMediatedDeviceSetUsedBy(mdev, drvname, domname) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Copy mdev references to the driver list:
|
2017-04-28 05:52:52 +00:00
|
|
|
* - caller is responsible for NOT freeing devices in @src on success
|
2017-03-22 14:50:33 +00:00
|
|
|
* - we're responsible for performing a rollback on failure
|
|
|
|
*/
|
2017-05-04 13:36:37 +00:00
|
|
|
VIR_DEBUG("Add '%s' to list of active mediated devices used by '%s'",
|
|
|
|
mdev->path, domname);
|
2017-04-28 07:24:31 +00:00
|
|
|
if (virMediatedDeviceListAdd(dst, &mdev) < 0)
|
2017-03-22 14:50:33 +00:00
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(dst);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
rollback:
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
virMediatedDevicePtr tmp = virMediatedDeviceListGet(src, j);
|
|
|
|
virMediatedDeviceListSteal(dst, tmp);
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-23 08:47:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virMediatedDeviceTypeFree(virMediatedDeviceTypePtr type)
|
|
|
|
{
|
|
|
|
if (!type)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_FREE(type->id);
|
|
|
|
VIR_FREE(type->name);
|
|
|
|
VIR_FREE(type->device_api);
|
|
|
|
VIR_FREE(type);
|
|
|
|
}
|
2018-01-23 08:55:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virMediatedDeviceTypeReadAttrs(const char *sysfspath,
|
|
|
|
virMediatedDeviceTypePtr *type)
|
|
|
|
{
|
2018-07-24 15:52:14 +00:00
|
|
|
VIR_AUTOPTR(virMediatedDeviceType) tmp = NULL;
|
2018-01-23 08:55:52 +00:00
|
|
|
|
2018-03-05 14:43:45 +00:00
|
|
|
#define MDEV_GET_SYSFS_ATTR(attr, dst, cb, optional) \
|
2018-01-23 08:55:52 +00:00
|
|
|
do { \
|
2018-03-05 14:43:45 +00:00
|
|
|
int rc; \
|
|
|
|
if ((rc = cb(dst, "%s/%s", sysfspath, attr)) < 0) { \
|
|
|
|
if (rc != -2 || !optional) \
|
2018-07-24 15:52:14 +00:00
|
|
|
return -1; \
|
2018-03-05 14:43:45 +00:00
|
|
|
} \
|
2018-01-23 08:55:52 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
if (VIR_ALLOC(tmp) < 0)
|
2018-07-24 15:52:14 +00:00
|
|
|
return -1;
|
2018-01-23 08:55:52 +00:00
|
|
|
|
|
|
|
if (VIR_STRDUP(tmp->id, last_component(sysfspath)) < 0)
|
2018-07-24 15:52:14 +00:00
|
|
|
return -1;
|
2018-01-23 08:55:52 +00:00
|
|
|
|
2018-03-05 14:43:45 +00:00
|
|
|
/* @name sysfs attribute is optional, so getting ENOENT is fine */
|
|
|
|
MDEV_GET_SYSFS_ATTR("name", &tmp->name, virFileReadValueString, true);
|
|
|
|
MDEV_GET_SYSFS_ATTR("device_api", &tmp->device_api,
|
|
|
|
virFileReadValueString, false);
|
2018-01-23 08:55:52 +00:00
|
|
|
MDEV_GET_SYSFS_ATTR("available_instances", &tmp->available_instances,
|
2018-03-05 14:43:45 +00:00
|
|
|
virFileReadValueUint, false);
|
2018-01-23 08:55:52 +00:00
|
|
|
|
|
|
|
#undef MDEV_GET_SYSFS_ATTR
|
|
|
|
|
|
|
|
VIR_STEAL_PTR(*type, tmp);
|
2018-07-24 15:52:14 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-01-23 08:55:52 +00:00
|
|
|
}
|