2008-11-21 12:27:11 +00:00
|
|
|
/*
|
2014-03-07 14:38:51 +01:00
|
|
|
* node_device_driver.c: node device enumeration
|
2008-11-21 12:27:11 +00:00
|
|
|
*
|
2015-05-08 12:55:00 -04: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 16:30:55 -06:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 18:06:23 +08: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 14:59:04 -04:00
|
|
|
#include "virfile.h"
|
2013-05-02 15:54:22 -06:00
|
|
|
#include "virstring.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
#include "node_device_conf.h"
|
2009-09-15 18:30:17 +01:00
|
|
|
#include "node_device_driver.h"
|
2015-05-06 16:40:39 -04:00
|
|
|
#include "node_device_hal.h"
|
|
|
|
#include "node_device_linux_sysfs.h"
|
2013-04-23 11:56:22 +01:00
|
|
|
#include "virutil.h"
|
|
|
|
#include "viraccessapicheck.h"
|
2014-06-05 17:36:31 +02: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;
|
|
|
|
|
2009-06-26 14:09:01 +00:00
|
|
|
static int update_caps(virNodeDeviceObjPtr dev)
|
|
|
|
{
|
|
|
|
virNodeDevCapsDefPtr cap = dev->def->caps;
|
|
|
|
|
|
|
|
while (cap) {
|
2015-05-06 13:51:47 -04:00
|
|
|
switch (cap->data.type) {
|
|
|
|
case VIR_NODE_DEV_CAP_SCSI_HOST:
|
2015-05-06 16:40:39 -04:00
|
|
|
nodeDeviceSysfsGetSCSIHostCaps(&dev->def->caps->data);
|
2015-05-06 13:51:47 -04:00
|
|
|
break;
|
|
|
|
case VIR_NODE_DEV_CAP_NET:
|
|
|
|
if (virNetDevGetLinkInfo(cap->data.net.ifname, &cap->data.net.lnk) < 0)
|
|
|
|
return -1;
|
2015-06-16 11:57:11 -04:00
|
|
|
virBitmapFree(cap->data.net.features);
|
|
|
|
if (virNetDevGetFeatures(cap->data.net.ifname, &cap->data.net.features) < 0)
|
|
|
|
return -1;
|
2015-05-06 13:51:47 -04:00
|
|
|
break;
|
2015-05-11 13:40:21 -04:00
|
|
|
case VIR_NODE_DEV_CAP_PCI_DEV:
|
|
|
|
if (nodeDeviceSysfsGetPCIRelatedDevCaps(dev->def->sysfs_path,
|
|
|
|
&dev->def->caps->data) < 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
2014-06-05 17:36:31 +02:00
|
|
|
|
2015-05-06 13:51:47 -04:00
|
|
|
/* all types that (supposedly) don't require any updates
|
|
|
|
* relative to what's in the cache.
|
|
|
|
*/
|
|
|
|
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_TARGET:
|
|
|
|
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:
|
|
|
|
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 14:49:21 -04: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.
|
|
|
|
*/
|
2010-02-04 21:02:58 +01:00
|
|
|
static int update_driver_name(virNodeDeviceObjPtr dev)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
char *driver_link = NULL;
|
2009-12-23 22:18:04 +01:00
|
|
|
char *devpath = NULL;
|
2009-06-12 13:12:55 +00:00
|
|
|
char *p;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
VIR_FREE(dev->def->driver);
|
|
|
|
|
2013-07-04 12:11:59 +02:00
|
|
|
if (virAsprintf(&driver_link, "%s/driver", dev->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 12:05:38 +01:00
|
|
|
if (virFileResolveLink(driver_link, &devpath) < 0) {
|
2010-02-04 21:02:58 +01:00
|
|
|
virReportSystemError(errno,
|
2009-06-12 13:12:55 +00:00
|
|
|
_("cannot resolve driver link %s"), driver_link);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strrchr(devpath, '/');
|
2013-05-03 14:44:20 +02:00
|
|
|
if (p && VIR_STRDUP(dev->def->driver, p + 1) < 0)
|
|
|
|
goto cleanup;
|
2009-06-12 13:12:55 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 07:57:22 +01:00
|
|
|
cleanup:
|
2009-06-12 13:12:55 +00:00
|
|
|
VIR_FREE(driver_link);
|
2009-12-23 22:18:04 +01:00
|
|
|
VIR_FREE(devpath);
|
2009-06-12 13:12:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* XXX: Implement me for non-linux */
|
2010-02-04 21:02:58 +01:00
|
|
|
static int update_driver_name(virNodeDeviceObjPtr dev ATTRIBUTE_UNUSED)
|
2009-06-12 13:12:55 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-11-17 16:30:27 +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
|
|
|
}
|
2014-11-17 16:30:27 +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
|
|
|
}
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
int
|
|
|
|
nodeNumOfDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
2011-07-06 16:21:23 -06:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
int ndevs = 0;
|
Convert 'int i' to 'size_t i' in src/node_device/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 15:09:33 +01:00
|
|
|
size_t i;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeNumOfDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-06 16:21:23 -06:00
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-10-01 14:51:39 -04:00
|
|
|
for (i = 0; i < driver->devs.count; i++) {
|
2013-06-26 17:50:54 +01:00
|
|
|
virNodeDeviceObjPtr obj = driver->devs.objs[i];
|
|
|
|
virNodeDeviceObjLock(obj);
|
|
|
|
if (virNodeNumOfDevicesCheckACL(conn, obj->def) &&
|
|
|
|
((cap == NULL) ||
|
|
|
|
virNodeDeviceHasCap(obj, cap)))
|
2008-11-21 12:27:11 +00:00
|
|
|
++ndevs;
|
2013-06-26 17:50:54 +01:00
|
|
|
virNodeDeviceObjUnlock(obj);
|
2009-10-01 14:51:39 -04:00
|
|
|
}
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
}
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
int
|
2008-11-21 12:27:11 +00:00
|
|
|
nodeListDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
|
|
|
char **const names, int maxnames,
|
2011-07-06 16:21:23 -06:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
|
|
|
int ndevs = 0;
|
Convert 'int i' to 'size_t i' in src/node_device/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 15:09:33 +01:00
|
|
|
size_t i;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeListDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-07-06 16:21:23 -06:00
|
|
|
virCheckFlags(0, -1);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:48:31 +00:00
|
|
|
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
|
2013-06-26 17:50:54 +01:00
|
|
|
virNodeDeviceObjPtr obj = driver->devs.objs[i];
|
|
|
|
virNodeDeviceObjLock(obj);
|
|
|
|
if (virNodeListDevicesCheckACL(conn, obj->def) &&
|
|
|
|
(cap == NULL ||
|
|
|
|
virNodeDeviceHasCap(obj, cap))) {
|
|
|
|
if (VIR_STRDUP(names[ndevs++], obj->def->name) < 0) {
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-11-21 12:27:11 +00:00
|
|
|
goto failure;
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-26 17:50:54 +01:00
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
|
|
|
|
failure:
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2008-11-21 12:27:11 +00:00
|
|
|
--ndevs;
|
|
|
|
while (--ndevs >= 0)
|
|
|
|
VIR_FREE(names[ndevs]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-05 13:34:10 +08:00
|
|
|
int
|
2013-04-29 10:20:17 +01:00
|
|
|
nodeConnectListAllNodeDevices(virConnectPtr conn,
|
|
|
|
virNodeDevicePtr **devices,
|
|
|
|
unsigned int flags)
|
2012-09-05 13:34:10 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virCheckFlags(VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP, -1);
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virConnectListAllNodeDevicesEnsureACL(conn) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2013-06-26 17:50:54 +01:00
|
|
|
ret = virNodeDeviceObjListExport(conn, driver->devs, devices,
|
|
|
|
virConnectListAllNodeDevicesCheckACL,
|
|
|
|
flags);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2012-09-05 13:34:10 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
virNodeDevicePtr
|
|
|
|
nodeDeviceLookupByName(virConnectPtr conn, const char *name)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
virNodeDevicePtr ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&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 13:59:57 -04:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceLookupByNameEnsureACL(conn, obj->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = virGetNodeDevice(conn, name);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-03-25 07:57:22 +01:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2013-02-04 21:03:11 +08:00
|
|
|
virNodeDevicePtr
|
|
|
|
nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
|
|
|
|
const char *wwnn,
|
|
|
|
const char *wwpn,
|
|
|
|
unsigned int flags)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/node_device/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 15:09:33 +01:00
|
|
|
size_t i;
|
2009-06-02 15:12:53 +00:00
|
|
|
virNodeDeviceObjListPtr devs = &driver->devs;
|
|
|
|
virNodeDevCapsDefPtr cap = NULL;
|
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
|
|
|
virNodeDevicePtr dev = NULL;
|
|
|
|
|
2013-02-04 21:03:11 +08:00
|
|
|
virCheckFlags(0, NULL);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
for (i = 0; i < devs->count; i++) {
|
|
|
|
obj = devs->objs[i];
|
|
|
|
virNodeDeviceObjLock(obj);
|
|
|
|
cap = obj->def->caps;
|
|
|
|
|
|
|
|
while (cap) {
|
2015-05-08 12:55:00 -04:00
|
|
|
if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2015-05-06 16:40:39 -04:00
|
|
|
nodeDeviceSysfsGetSCSIHostCaps(&cap->data);
|
2009-06-02 15:12:53 +00:00
|
|
|
if (cap->data.scsi_host.flags &
|
|
|
|
VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
|
|
|
|
if (STREQ(cap->data.scsi_host.wwnn, wwnn) &&
|
|
|
|
STREQ(cap->data.scsi_host.wwpn, wwpn)) {
|
2013-04-23 11:56:22 +01:00
|
|
|
|
|
|
|
if (virNodeDeviceLookupSCSIHostByWWNEnsureACL(conn, obj->def) < 0)
|
|
|
|
goto out;
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
dev = virGetNodeDevice(conn, obj->def->name);
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cap = cap->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
2014-03-25 07:57:22 +01:00
|
|
|
out:
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2009-06-02 15:12:53 +00:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
char *
|
|
|
|
nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
|
2011-07-06 16:21:23 -06:00
|
|
|
unsigned int flags)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2011-07-06 16:21:23 -06:00
|
|
|
virCheckFlags(0, NULL);
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->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) {
|
2012-07-18 12:42:38 +01:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceGetXMLDescEnsureACL(dev->conn, obj->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2010-02-04 21:02:58 +01:00
|
|
|
update_driver_name(obj);
|
2014-06-05 17:36:31 +02:00
|
|
|
if (update_caps(obj) < 0)
|
|
|
|
goto cleanup;
|
2009-06-26 14:09:01 +00:00
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
ret = virNodeDeviceDefFormat(obj->def);
|
2008-12-04 21:46:34 +00:00
|
|
|
|
2014-03-25 07:57:22 +01:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
char *
|
|
|
|
nodeDeviceGetParent(virNodeDevicePtr dev)
|
2008-11-21 12:27:11 +00:00
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->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) {
|
2012-07-18 12:42:38 +01:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceGetParentEnsureACL(dev->conn, obj->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2009-04-01 10:21:34 +00:00
|
|
|
if (obj->def->parent) {
|
2013-05-03 14:44:20 +02:00
|
|
|
if (VIR_STRDUP(ret, obj->def->parent) < 0)
|
|
|
|
goto cleanup;
|
2009-04-01 10:21:34 +00:00
|
|
|
} else {
|
2012-07-18 12:42:38 +01: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 07:57:22 +01:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
int
|
|
|
|
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
|
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
|
|
|
virNodeDevCapsDefPtr caps;
|
|
|
|
int ncaps = 0;
|
2008-12-04 21:46:34 +00:00
|
|
|
int ret = -1;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->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) {
|
2012-07-18 12:42:38 +01:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceNumOfCapsEnsureACL(dev->conn, obj->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-02-04 08:10:52 -05:00
|
|
|
for (caps = obj->def->caps; caps; caps = caps->next) {
|
2008-11-21 12:27:11 +00:00
|
|
|
++ncaps;
|
2015-02-04 08:10:52 -05:00
|
|
|
|
2015-05-08 12:55:00 -04:00
|
|
|
if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2015-02-04 08:10:52 -05: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 07:57:22 +01:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2008-12-04 21:46:34 +00:00
|
|
|
return ret;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
int
|
2008-11-21 12:27:11 +00:00
|
|
|
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
|
|
|
|
{
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
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
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->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) {
|
2012-07-18 12:42:38 +01:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
dev->name);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceListCapsEnsureACL(dev->conn, obj->def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
|
2015-05-08 12:55:00 -04:00
|
|
|
if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2015-02-04 08:10:52 -05:00
|
|
|
|
2015-05-08 12:55:00 -04:00
|
|
|
if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
|
2015-02-04 08:10:52 -05: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 07:57:22 +01:00
|
|
|
cleanup:
|
2008-12-04 21:48:31 +00:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(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
|
|
|
}
|
|
|
|
|
2009-06-02 15:12:53 +00:00
|
|
|
static int
|
2010-02-10 10:40:18 +00:00
|
|
|
get_time(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 12:42:38 +01: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
|
|
|
|
find_new_device(virConnectPtr conn, const char *wwnn, const char *wwpn)
|
|
|
|
{
|
|
|
|
virNodeDevicePtr dev = NULL;
|
|
|
|
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
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
get_time(&start);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {
|
|
|
|
|
2010-02-04 23:41:52 +01:00
|
|
|
virFileWaitForDevices();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2013-02-04 21:03:11 +08:00
|
|
|
dev = nodeDeviceLookupSCSIHostByWWN(conn, wwnn, wwpn, 0);
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2014-11-13 15:24:17 +01:00
|
|
|
if (dev != NULL)
|
2009-06-02 15:12:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
sleep(5);
|
2014-11-13 15:24:17 +01:00
|
|
|
if (get_time(&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
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
virNodeDevicePtr
|
2009-06-02 15:12:53 +00:00
|
|
|
nodeDeviceCreateXML(virConnectPtr conn,
|
|
|
|
const char *xmlDesc,
|
2011-07-06 16:21:23 -06:00
|
|
|
unsigned int flags)
|
2009-06-02 15:12:53 +00:00
|
|
|
{
|
|
|
|
virNodeDeviceDefPtr def = NULL;
|
|
|
|
char *wwnn = NULL, *wwpn = NULL;
|
|
|
|
int parent_host = -1;
|
|
|
|
virNodeDevicePtr dev = NULL;
|
2012-02-10 12:51:47 +08:00
|
|
|
const char *virt_type = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2011-07-06 16:21:23 -06:00
|
|
|
virCheckFlags(0, NULL);
|
2012-02-10 12:51:47 +08:00
|
|
|
virt_type = virConnectGetType(conn);
|
2011-07-06 16:21:23 -06:00
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2012-02-10 12:51:47 +08:00
|
|
|
def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type);
|
2014-11-13 15:24:17 +01:00
|
|
|
if (def == NULL)
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceCreateXMLEnsureACL(conn, def) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-11-13 15:24:17 +01:00
|
|
|
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetParentHost(&driver->devs,
|
2009-10-14 15:47:10 -04:00
|
|
|
def->name,
|
|
|
|
def->parent,
|
|
|
|
&parent_host) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-01-08 01:05:34 +08:00
|
|
|
if (virManageVport(parent_host,
|
|
|
|
wwpn,
|
|
|
|
wwnn,
|
|
|
|
VPORT_CREATE) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = find_new_device(conn, wwnn, wwpn);
|
|
|
|
/* We don't check the return value, because one way or another,
|
|
|
|
* we're returning what we get... */
|
|
|
|
|
2014-11-13 15:24:17 +01:00
|
|
|
if (dev == NULL)
|
2015-05-27 13:59:57 -04: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 07:57:22 +01: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);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-04 23:24:54 +02:00
|
|
|
int
|
2009-06-02 15:12:53 +00:00
|
|
|
nodeDeviceDestroy(virNodeDevicePtr dev)
|
|
|
|
{
|
2010-06-02 21:59:16 -04:00
|
|
|
int ret = -1;
|
2009-06-02 15:12:53 +00:00
|
|
|
virNodeDeviceObjPtr obj = NULL;
|
|
|
|
char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
|
|
|
|
int parent_host = -1;
|
|
|
|
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceLock();
|
2009-06-02 15:12:53 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2014-11-17 16:30:27 +00:00
|
|
|
nodeDeviceUnlock();
|
2009-06-02 15:12:53 +00:00
|
|
|
|
|
|
|
if (!obj) {
|
2015-05-27 13:59:57 -04:00
|
|
|
virReportError(VIR_ERR_NO_NODE_DEVICE,
|
|
|
|
_("no node device with matching name '%s'"),
|
|
|
|
dev->name);
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:56:22 +01:00
|
|
|
if (virNodeDeviceDestroyEnsureACL(dev->conn, obj->def) < 0)
|
|
|
|
goto out;
|
|
|
|
|
2014-11-13 15:24:17 +01:00
|
|
|
if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
2009-10-14 15:47:10 -04:00
|
|
|
/* virNodeDeviceGetParentHost will cause the device object's lock to be
|
2009-06-02 15:12:53 +00:00
|
|
|
* taken, so we have to dup the parent's name and drop the lock
|
|
|
|
* before calling it. We don't need the reference to the object
|
|
|
|
* any more once we have the parent's name. */
|
2013-05-03 14:44:20 +02:00
|
|
|
if (VIR_STRDUP(parent_name, obj->def->parent) < 0) {
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
obj = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2013-05-03 14:44:20 +02:00
|
|
|
virNodeDeviceObjUnlock(obj);
|
|
|
|
obj = NULL;
|
2009-06-02 15:12:53 +00:00
|
|
|
|
2010-02-10 10:40:18 +00:00
|
|
|
if (virNodeDeviceGetParentHost(&driver->devs,
|
2009-10-14 15:47:10 -04:00
|
|
|
dev->name,
|
|
|
|
parent_name,
|
|
|
|
&parent_host) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-01-08 01:05:34 +08:00
|
|
|
if (virManageVport(parent_host,
|
|
|
|
wwpn,
|
|
|
|
wwnn,
|
|
|
|
VPORT_DELETE) == -1) {
|
2009-06-02 15:12:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-06-02 21:59:16 -04:00
|
|
|
ret = 0;
|
2014-03-25 07:57:22 +01:00
|
|
|
out:
|
2009-10-14 16:15:13 -04:00
|
|
|
if (obj)
|
|
|
|
virNodeDeviceObjUnlock(obj);
|
2009-06-02 15:12:53 +00:00
|
|
|
VIR_FREE(parent_name);
|
|
|
|
VIR_FREE(wwnn);
|
|
|
|
VIR_FREE(wwpn);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-18 09:19:33 +01:00
|
|
|
int nodedevRegister(void)
|
|
|
|
{
|
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 15:38:35 +01:00
|
|
|
# ifdef WITH_HAL
|
2008-11-21 12:27:11 +00:00
|
|
|
return halNodeRegister();
|
2010-03-09 19:22:22 +01:00
|
|
|
# endif
|
2008-11-21 12:27:11 +00:00
|
|
|
#endif
|
|
|
|
}
|