2008-11-21 12:27:11 +00:00
|
|
|
/*
|
2014-03-07 13:38:51 +00:00
|
|
|
* node_device_driver.c: node device enumeration
|
2008-11-21 12:27:11 +00:00
|
|
|
*
|
2015-05-08 16:55:00 +00:00
|
|
|
* Copyright (C) 2010-2015 Red Hat, Inc.
|
2008-11-21 12:27:11 +00:00
|
|
|
* Copyright (C) 2008 Virtual Iron Software, Inc.
|
|
|
|
* Copyright (C) 2008 David F. Lively
|
|
|
|
*
|
|
|
|
* 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2008-11-21 12:27:11 +00:00
|
|
|
*
|
|
|
|
* Author: David F. Lively <dlively@virtualiron.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2009-06-02 15:12:53 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
#include "datatypes.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2013-05-02 21:54:22 +00:00
|
|
|
#include "virstring.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
#include "node_device_conf.h"
|
2016-07-28 12:02:54 +00:00
|
|
|
#include "node_device_event.h"
|
2009-09-15 17:30:17 +00:00
|
|
|
#include "node_device_driver.h"
|
2015-05-06 20:40:39 +00:00
|
|
|
#include "node_device_hal.h"
|
|
|
|
#include "node_device_linux_sysfs.h"
|
2017-01-23 17:54:42 +00:00
|
|
|
#include "virvhba.h"
|
2013-04-23 10:56:22 +00:00
|
|
|
#include "viraccessapicheck.h"
|
2014-06-05 15:36:31 +00:00
|
|
|
#include "virnetdev.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2009-01-29 12:10:32 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NODEDEV
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
virNodeDeviceDriverStatePtr driver;
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
|
|
|
static int
|
2017-05-12 20:06:47 +00:00
|
|
|
nodeDeviceUpdateCaps(virNodeDeviceDefPtr def)
|
2009-06-26 14:09:01 +00:00
|
|
|
{
|
2017-05-12 20:06:47 +00:00
|
|
|
virNodeDevCapsDefPtr cap = def->caps;
|
2009-06-26 14:09:01 +00:00
|
|
|
|
|
|
|
while (cap) {
|
2015-05-06 17:51:47 +00:00
|
|
|
switch (cap->data.type) {
|
|
|
|
case VIR_NODE_DEV_CAP_SCSI_HOST:
|
2017-05-22 06:38:20 +00:00
|
|
|
nodeDeviceSysfsGetSCSIHostCaps(&cap->data.scsi_host);
|
2015-05-06 17:51:47 +00:00
|
|
|
break;
|
2017-05-22 06:38:25 +00:00
|
|
|
case VIR_NODE_DEV_CAP_SCSI_TARGET:
|
2017-05-12 20:06:47 +00:00
|
|
|
nodeDeviceSysfsGetSCSITargetCaps(def->sysfs_path,
|
2017-05-22 06:38:25 +00:00
|
|
|
&cap->data.scsi_target);
|
|
|
|
break;
|
2015-05-06 17:51:47 +00:00
|
|
|
case VIR_NODE_DEV_CAP_NET:
|
|
|
|
if (virNetDevGetLinkInfo(cap->data.net.ifname, &cap->data.net.lnk) < 0)
|
|
|
|
return -1;
|
2015-06-16 15:57:11 +00:00
|
|
|
virBitmapFree(cap->data.net.features);
|
|
|
|
if (virNetDevGetFeatures(cap->data.net.ifname, &cap->data.net.features) < 0)
|
|
|
|
return -1;
|
2015-05-06 17:51:47 +00:00
|
|
|
break;
|
2015-05-11 17:40:21 +00:00
|
|
|
case VIR_NODE_DEV_CAP_PCI_DEV:
|
2017-05-12 20:06:47 +00:00
|
|
|
if (nodeDeviceSysfsGetPCIRelatedDevCaps(def->sysfs_path,
|
2017-05-22 06:38:20 +00:00
|
|
|
&cap->data.pci_dev) < 0)
|
2015-05-11 17:40:21 +00:00
|
|
|
return -1;
|
|
|
|
break;
|
2014-06-05 15:36:31 +00:00
|
|
|
|
2015-05-06 17:51:47 +00:00
|
|
|
/* all types that (supposedly) don't require any updates
|
|
|
|
* relative to what's in the cache.
|
|
|
|
*/
|
2017-02-14 21:04:12 +00:00
|
|
|
case VIR_NODE_DEV_CAP_DRM:
|
2015-05-06 17:51:47 +00:00
|
|
|
case VIR_NODE_DEV_CAP_SYSTEM:
|
|
|
|
case VIR_NODE_DEV_CAP_USB_DEV:
|
|
|
|
case VIR_NODE_DEV_CAP_USB_INTERFACE:
|
|
|
|
case VIR_NODE_DEV_CAP_SCSI:
|
|
|
|
case VIR_NODE_DEV_CAP_STORAGE:
|
|
|
|
case VIR_NODE_DEV_CAP_FC_HOST:
|
|
|
|
case VIR_NODE_DEV_CAP_VPORTS:
|
|
|
|
case VIR_NODE_DEV_CAP_SCSI_GENERIC:
|
2017-03-06 16:20:00 +00:00
|
|
|
case VIR_NODE_DEV_CAP_MDEV_TYPES:
|
|
|
|
case VIR_NODE_DEV_CAP_MDEV:
|
2017-05-22 06:38:22 +00:00
|
|
|
case VIR_NODE_DEV_CAP_CCW_DEV:
|
2015-05-06 17:51:47 +00:00
|
|
|
case VIR_NODE_DEV_CAP_LAST:
|
|
|
|
break;
|
|
|
|
}
|
2009-07-01 21:30:41 +00:00
|
|
|
cap = cap->next;
|
2009-06-26 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-30 18:49:21 +00:00
|
|
|
#if defined (__linux__) && ( defined (WITH_HAL) || defined(WITH_UDEV))
|
|
|
|
/* NB: It was previously believed that changes in driver name were
|
|
|
|
* relayed to libvirt as "change" events by udev, and the udev event
|
|
|
|
* notification is setup to recognize such events and effectively
|
|
|
|
* recreate the device entry in the cache. However, neither the kernel
|
|
|
|
* nor udev sends such an event, so it is necessary to manually update
|
|
|
|
* the driver name for a device each time its entry is used, both for
|
|
|
|
* udev *and* HAL backends.
|
|
|
|
*/
|
2017-05-12 15:49:18 +00:00
|
|
|
static int
|
2017-05-12 20:04:19 +00:00
|
|
|
nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
char *driver_link = NULL;
|
2009-12-23 21:18:04 +00:00
|
|
|
char *devpath = NULL;
|
2009-06-12 13:12:55 +00:00
|
|
|
char *p;
|
|
|
|
int ret = -1;
|
|
|
|
|
2017-05-12 20:04:19 +00:00
|
|
|
VIR_FREE(def->driver);
|
2009-06-12 13:12:55 +00:00
|
|
|
|
2017-05-12 20:04:19 +00:00
|
|
|
if (virAsprintf(&driver_link, "%s/driver", def->sysfs_path) < 0)
|
2009-06-12 13:12:55 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Some devices don't have an explicit driver, so just return
|
|
|
|
without a name */
|
|
|
|
if (access(driver_link, R_OK) < 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2009-12-14 11:05:38 +00:00
|
|
|
if (virFileResolveLink(driver_link, &devpath) < 0) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2009-06-12 13:12:55 +00:00
|
|
|
_("cannot resolve driver link %s"), driver_link);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strrchr(devpath, '/');
|
2017-05-12 20:04:19 +00:00
|
|
|
if (p && VIR_STRDUP(def->driver, p + 1) < 0)
|
2013-05-03 12:44:20 +00:00
|
|
|
goto cleanup;
|
2009-06-12 13:12:55 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2009-06-12 13:12:55 +00:00
|
|
|
VIR_FREE(driver_link);
|
2009-12-23 21:18:04 +00:00
|
|
|
VIR_FREE(devpath);
|
2009-06-12 13:12:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* XXX: Implement me for non-linux */
|
2017-05-12 15:49:18 +00:00
|
|
|
static int
|
2017-05-12 20:04:19 +00:00
|
|
|
nodeDeviceUpdateDriverName(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
void
|
|
|
|
nodeDeviceLock(void)
|
2008-12-04 21:48:31 +00:00
|
|
|
{
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexLock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
2017-05-12 15:49:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nodeDeviceUnlock(void)
|
2008-12-04 21:48:31 +00:00
|
|
|
{
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexUnlock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
|
|
|
nodeNumOfDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
2011-07-06 22:21:23 +00:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
int ndevs = 0;
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virNodeNumOfDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-06 22:21:23 +00:00
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2017-05-12 18:47:17 +00:00
|
|
|
ndevs = virNodeDeviceObjListNumOfDevices(driver->devs, conn, cap,
|
|
|
|
virNodeNumOfDevicesCheckACL);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
}
|
|
|
|
|
2017-03-19 19:51:03 +00:00
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2008-11-21 12:27:11 +00:00
|
|
|
nodeListDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
2017-03-19 19:51:03 +00:00
|
|
|
char **const names,
|
|
|
|
int maxnames,
|
2011-07-06 22:21:23 +00:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2017-03-19 19:51:03 +00:00
|
|
|
int nnames;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virNodeListDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-06 22:21:23 +00:00
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2017-05-12 18:47:17 +00:00
|
|
|
nnames = virNodeDeviceObjListGetNames(driver->devs, conn,
|
|
|
|
virNodeListDevicesCheckACL,
|
|
|
|
cap, names, maxnames);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-03-19 19:51:03 +00:00
|
|
|
return nnames;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2012-09-05 05:34:10 +00:00
|
|
|
int
|
2013-04-29 09:20:17 +00:00
|
|
|
nodeConnectListAllNodeDevices(virConnectPtr conn,
|
|
|
|
virNodeDevicePtr **devices,
|
|
|
|
unsigned int flags)
|
2012-09-05 05:34:10 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virCheckFlags(VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP, -1);
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virConnectListAllNodeDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2017-05-12 17:51:25 +00:00
|
|
|
ret = virNodeDeviceObjListExport(conn, driver->devs, devices,
|
2013-06-26 16:50:54 +00:00
|
|
|
virConnectListAllNodeDevicesCheckACL,
|
|
|
|
flags);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2012-09-05 05:34:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2017-05-12 16:24:56 +00:00
|
|
|
static virNodeDeviceObjPtr
|
|
|
|
nodeDeviceObjFindByName(const char *name)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2017-05-12 18:47:17 +00:00
|
|
|
obj = virNodeDeviceObjListFindByName(driver->devs, name);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-12-04 21:48:31 +00:00
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2015-05-27 17:59:57 +00:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
name);
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 16:24:56 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNodeDevicePtr
|
|
|
|
nodeDeviceLookupByName(virConnectPtr conn,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
virNodeDeviceObjPtr obj;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2017-05-12 16:45:16 +00:00
|
|
|
virNodeDevicePtr device = NULL;
|
2017-05-12 16:24:56 +00:00
|
|
|
|
|
|
|
if (!(obj = nodeDeviceObjFindByName(name)))
|
|
|
|
return NULL;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2017-05-12 16:24:56 +00:00
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
if (virNodeDeviceLookupByNameEnsureACL(conn, def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if ((device = virGetNodeDevice(conn, name))) {
|
|
|
|
if (VIR_STRDUP(device->parent, def->parent) < 0) {
|
|
|
|
virObjectUnref(device);
|
|
|
|
device = NULL;
|
2017-05-26 10:57:43 +00:00
|
|
|
}
|
2017-02-07 18:04:08 +00:00
|
|
|
}
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2017-05-12 16:45:16 +00:00
|
|
|
return device;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2013-02-04 13:03:11 +00:00
|
|
|
virNodeDevicePtr
|
|
|
|
nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
|
|
|
|
const char *wwnn,
|
|
|
|
const char *wwpn,
|
|
|
|
unsigned int flags)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2017-05-12 16:45:16 +00:00
|
|
|
virNodeDevicePtr device = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2013-02-04 13:03:11 +00:00
|
|
|
virCheckFlags(0, NULL);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2017-05-12 19:31:10 +00:00
|
|
|
obj = virNodeDeviceObjListFindSCSIHostByWWNs(driver->devs, wwnn, wwpn);
|
|
|
|
nodeDeviceUnlock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 19:31:10 +00:00
|
|
|
if (!obj)
|
|
|
|
return NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 19:31:10 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 19:31:10 +00:00
|
|
|
if (virNodeDeviceLookupSCSIHostByWWNEnsureACL(conn, def) < 0)
|
|
|
|
goto cleanup;
|
2017-05-22 06:38:21 +00:00
|
|
|
|
2017-05-12 19:31:10 +00:00
|
|
|
if ((device = virGetNodeDevice(conn, def->name))) {
|
|
|
|
if (VIR_STRDUP(device->parent, def->parent) < 0) {
|
|
|
|
virObjectUnref(device);
|
|
|
|
device = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2017-05-12 19:31:10 +00:00
|
|
|
return device;
|
2009-06-02 15:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
char *
|
2017-05-12 16:45:16 +00:00
|
|
|
nodeDeviceGetXMLDesc(virNodeDevicePtr device,
|
2011-07-06 22:21:23 +00:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2008-12-04 21:46:34 +00:00
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2011-07-06 22:21:23 +00:00
|
|
|
virCheckFlags(0, NULL);
|
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
2017-05-12 16:24:56 +00:00
|
|
|
return NULL;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (virNodeDeviceGetXMLDescEnsureACL(device->conn, def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
if (nodeDeviceUpdateDriverName(def) < 0)
|
2017-05-12 20:04:19 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
if (nodeDeviceUpdateCaps(def) < 0)
|
2014-06-05 15:36:31 +00:00
|
|
|
goto cleanup;
|
2009-06-26 14:09:01 +00:00
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
ret = virNodeDeviceDefFormat(def);
|
2008-12-04 21:46:34 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
char *
|
2017-05-12 16:45:16 +00:00
|
|
|
nodeDeviceGetParent(virNodeDevicePtr device)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2008-12-04 21:46:34 +00:00
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
2017-05-12 16:24:56 +00:00
|
|
|
return NULL;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (virNodeDeviceGetParentEnsureACL(device->conn, def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
if (def->parent) {
|
|
|
|
if (VIR_STRDUP(ret, def->parent) < 0)
|
2013-05-03 12:44:20 +00:00
|
|
|
goto cleanup;
|
2009-04-01 10:21:34 +00:00
|
|
|
} else {
|
2012-07-18 11:42:38 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("no parent for this device"));
|
2009-04-01 10:21:34 +00:00
|
|
|
}
|
2008-12-04 21:46:34 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2017-05-12 16:45:16 +00:00
|
|
|
nodeDeviceNumOfCaps(virNodeDevicePtr device)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2008-11-21 12:27:11 +00:00
|
|
|
virNodeDevCapsDefPtr caps;
|
|
|
|
int ncaps = 0;
|
2008-12-04 21:46:34 +00:00
|
|
|
int ret = -1;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
2017-05-12 16:24:56 +00:00
|
|
|
return -1;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (virNodeDeviceNumOfCapsEnsureACL(device->conn, def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
for (caps = def->caps; caps; caps = caps->next) {
|
2008-11-21 12:27:11 +00:00
|
|
|
++ncaps;
|
2015-02-04 13:10:52 +00:00
|
|
|
|
2015-05-08 16:55:00 +00:00
|
|
|
if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2015-02-04 13:10:52 +00:00
|
|
|
if (caps->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST)
|
|
|
|
ncaps++;
|
|
|
|
|
|
|
|
if (caps->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS)
|
|
|
|
ncaps++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = ncaps;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2017-05-12 16:45:16 +00:00
|
|
|
nodeDeviceListCaps(virNodeDevicePtr device,
|
2017-05-12 15:49:18 +00:00
|
|
|
char **const names,
|
|
|
|
int maxnames)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
2017-06-03 11:36:01 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
2008-11-21 12:27:11 +00:00
|
|
|
virNodeDevCapsDefPtr caps;
|
|
|
|
int ncaps = 0;
|
2008-12-04 21:46:34 +00:00
|
|
|
int ret = -1;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
2017-05-12 16:24:56 +00:00
|
|
|
return -1;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (virNodeDeviceListCapsEnsureACL(device->conn, def) < 0)
|
2013-04-23 10:56:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
|
2015-05-08 16:55:00 +00:00
|
|
|
if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2015-02-04 13:10:52 +00:00
|
|
|
|
2015-05-08 16:55:00 +00:00
|
|
|
if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2015-02-04 13:10:52 +00:00
|
|
|
if (ncaps < maxnames &&
|
|
|
|
caps->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
|
|
|
|
if (VIR_STRDUP(names[ncaps++],
|
|
|
|
virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_FC_HOST)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ncaps < maxnames &&
|
|
|
|
caps->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
|
|
|
|
if (VIR_STRDUP(names[ncaps++],
|
|
|
|
virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_VPORTS)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = ncaps;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
--ncaps;
|
|
|
|
while (--ncaps >= 0)
|
|
|
|
VIR_FREE(names[ncaps]);
|
|
|
|
}
|
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
static int
|
2017-05-12 15:49:18 +00:00
|
|
|
nodeDeviceGetTime(time_t *t)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
*t = time(NULL);
|
|
|
|
if (*t == (time_t)-1) {
|
2012-07-18 11:42:38 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("Could not get current time"));
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
*t = 0;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* When large numbers of devices are present on the host, it's
|
|
|
|
* possible for udev not to realize that it has work to do before we
|
|
|
|
* get here. We thus keep trying to find the new device we just
|
|
|
|
* created for up to LINUX_NEW_DEVICE_WAIT_TIME. Note that udev's
|
|
|
|
* default settle time is 180 seconds, so once udev realizes that it
|
|
|
|
* has work to do, it might take that long for the udev wait to
|
|
|
|
* return. Thus the total maximum time for this function to return is
|
|
|
|
* the udev settle time plus LINUX_NEW_DEVICE_WAIT_TIME.
|
|
|
|
*
|
|
|
|
* This whole area is a race, but if we retry the udev wait for
|
|
|
|
* LINUX_NEW_DEVICE_WAIT_TIME seconds and there's still no device,
|
|
|
|
* it's probably safe to assume it's not going to appear.
|
|
|
|
*/
|
|
|
|
static virNodeDevicePtr
|
2017-05-12 15:49:18 +00:00
|
|
|
nodeDeviceFindNewDevice(virConnectPtr conn,
|
|
|
|
const char *wwnn,
|
|
|
|
const char *wwpn)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
2017-05-12 16:45:16 +00:00
|
|
|
virNodeDevicePtr device = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
time_t start = 0, now = 0;
|
|
|
|
|
|
|
|
/* The thread that creates the device takes the driver lock, so we
|
|
|
|
* must release it in order to allow the device to be created.
|
|
|
|
* We're not doing anything with the driver pointer at this point,
|
|
|
|
* so it's safe to release it, assuming that the pointer itself
|
|
|
|
* doesn't become invalid. */
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
nodeDeviceGetTime(&start);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {
|
|
|
|
|
2017-02-20 12:00:51 +00:00
|
|
|
virWaitForDevices();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
device = nodeDeviceLookupSCSIHostByWWN(conn, wwnn, wwpn, 0);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (device != NULL)
|
2009-06-02 15:12:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
sleep(5);
|
2017-05-12 15:49:18 +00:00
|
|
|
if (nodeDeviceGetTime(&now) == -1)
|
2009-06-02 15:12:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
return device;
|
2009-06-02 15:12:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
virNodeDevicePtr
|
2009-06-02 15:12:53 +00:00
|
|
|
nodeDeviceCreateXML(virConnectPtr conn,
|
|
|
|
const char *xmlDesc,
|
2011-07-06 22:21:23 +00:00
|
|
|
unsigned int flags)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
|
|
|
virNodeDeviceDefPtr def = NULL;
|
|
|
|
char *wwnn = NULL, *wwpn = NULL;
|
|
|
|
int parent_host = -1;
|
2017-05-12 16:45:16 +00:00
|
|
|
virNodeDevicePtr device = NULL;
|
2012-02-10 04:51:47 +00:00
|
|
|
const char *virt_type = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2011-07-06 22:21:23 +00:00
|
|
|
virCheckFlags(0, NULL);
|
2012-02-10 04:51:47 +00:00
|
|
|
virt_type = virConnectGetType(conn);
|
2011-07-06 22:21:23 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-01-24 17:21:26 +00:00
|
|
|
if (!(def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type)))
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-04-23 10:56:22 +00:00
|
|
|
if (virNodeDeviceCreateXMLEnsureACL(conn, def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-11-13 14:24:17 +00:00
|
|
|
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-05-12 18:47:17 +00:00
|
|
|
if ((parent_host = virNodeDeviceObjListGetParentHost(driver->devs, def,
|
|
|
|
CREATE_DEVICE)) < 0)
|
2017-01-24 17:21:26 +00:00
|
|
|
goto cleanup;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-01-23 17:54:42 +00:00
|
|
|
if (virVHBAManageVport(parent_host, wwpn, wwnn, VPORT_CREATE) < 0)
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
device = nodeDeviceFindNewDevice(conn, wwnn, wwpn);
|
2009-06-02 15:12:53 +00:00
|
|
|
/* We don't check the return value, because one way or another,
|
|
|
|
* we're returning what we get... */
|
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (device == NULL)
|
2015-05-27 17:59:57 +00:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device for '%s' with matching "
|
|
|
|
"wwnn '%s' and wwpn '%s'"),
|
|
|
|
def->name, wwnn, wwpn);
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2009-06-02 15:12:53 +00:00
|
|
|
virNodeDeviceDefFree(def);
|
|
|
|
VIR_FREE(wwnn);
|
|
|
|
VIR_FREE(wwpn);
|
2017-05-12 16:45:16 +00:00
|
|
|
return device;
|
2009-06-02 15:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 21:24:54 +00:00
|
|
|
int
|
2017-05-12 16:45:16 +00:00
|
|
|
nodeDeviceDestroy(virNodeDevicePtr device)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
2010-06-03 01:59:16 +00:00
|
|
|
int ret = -1;
|
2009-06-02 15:12:53 +00:00
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
2017-01-24 17:21:26 +00:00
|
|
|
virNodeDeviceDefPtr def;
|
|
|
|
char *wwnn = NULL, *wwpn = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
int parent_host = -1;
|
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (!(obj = nodeDeviceObjFindByName(device->name)))
|
2017-05-12 16:24:56 +00:00
|
|
|
return -1;
|
2017-06-03 11:36:01 +00:00
|
|
|
def = virNodeDeviceObjGetDef(obj);
|
2017-05-12 16:24:56 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-05-12 16:45:16 +00:00
|
|
|
if (virNodeDeviceDestroyEnsureACL(device->conn, def) < 0)
|
2017-01-24 17:26:25 +00:00
|
|
|
goto cleanup;
|
2013-04-23 10:56:22 +00:00
|
|
|
|
2017-06-03 11:36:01 +00:00
|
|
|
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
|
2017-01-24 17:26:25 +00:00
|
|
|
goto cleanup;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-01-24 17:21:26 +00:00
|
|
|
/* virNodeDeviceGetParentHost will cause the device object's lock
|
|
|
|
* to be taken, so grab the object def which will have the various
|
|
|
|
* fields used to search (name, parent, parent_wwnn, parent_wwpn,
|
|
|
|
* or parent_fabric_wwn) and drop the object lock. */
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2017-05-12 18:47:17 +00:00
|
|
|
if ((parent_host = virNodeDeviceObjListGetParentHost(driver->devs, def,
|
|
|
|
EXISTING_DEVICE)) < 0)
|
2017-01-24 17:26:25 +00:00
|
|
|
goto cleanup;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2017-01-23 17:54:42 +00:00
|
|
|
if (virVHBAManageVport(parent_host, wwpn, wwnn, VPORT_DELETE) < 0)
|
2017-01-24 17:26:25 +00:00
|
|
|
goto cleanup;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2010-06-03 01:59:16 +00:00
|
|
|
ret = 0;
|
2017-01-24 17:21:26 +00:00
|
|
|
|
2017-01-24 17:26:25 +00:00
|
|
|
cleanup:
|
|
|
|
nodeDeviceUnlock();
|
2017-05-15 15:00:59 +00:00
|
|
|
virNodeDeviceObjEndAPI(&obj);
|
2009-06-02 15:12:53 +00:00
|
|
|
VIR_FREE(wwnn);
|
|
|
|
VIR_FREE(wwpn);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2016-07-28 12:02:54 +00:00
|
|
|
int
|
|
|
|
nodeConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
|
2017-05-12 16:45:16 +00:00
|
|
|
virNodeDevicePtr device,
|
2016-07-28 12:02:54 +00:00
|
|
|
int eventID,
|
|
|
|
virConnectNodeDeviceEventGenericCallback callback,
|
|
|
|
void *opaque,
|
|
|
|
virFreeCallback freecb)
|
|
|
|
{
|
|
|
|
int callbackID = -1;
|
|
|
|
|
|
|
|
if (virConnectNodeDeviceEventRegisterAnyEnsureACL(conn) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virNodeDeviceEventStateRegisterID(conn, driver->nodeDeviceEventState,
|
2017-05-12 16:45:16 +00:00
|
|
|
device, eventID, callback,
|
2016-07-28 12:02:54 +00:00
|
|
|
opaque, freecb, &callbackID) < 0)
|
|
|
|
callbackID = -1;
|
|
|
|
cleanup:
|
|
|
|
return callbackID;
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
2016-07-28 12:02:54 +00:00
|
|
|
int
|
|
|
|
nodeConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
|
|
|
|
int callbackID)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (virConnectNodeDeviceEventDeregisterAnyEnsureACL(conn) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virObjectEventStateDeregisterID(conn,
|
|
|
|
driver->nodeDeviceEventState,
|
2017-06-14 11:32:15 +00:00
|
|
|
callbackID, true) < 0)
|
2016-07-28 12:02:54 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-12 15:49:18 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
nodedevRegister(void)
|
2014-03-18 08:19:33 +00:00
|
|
|
{
|
2015-01-20 16:16:26 +00:00
|
|
|
#ifdef WITH_UDEV
|
|
|
|
return udevNodeRegister();
|
2008-11-21 12:27:11 +00:00
|
|
|
#else
|
2012-09-20 14:38:35 +00:00
|
|
|
# ifdef WITH_HAL
|
2008-11-21 12:27:11 +00:00
|
|
|
return halNodeRegister();
|
2010-03-09 18:22:22 +00:00
|
|
|
# endif
|
2008-11-21 12:27:11 +00:00
|
|
|
#endif
|
|
|
|
}
|