2008-11-21 12:27:11 +00:00
|
|
|
/*
|
|
|
|
* node_device.c: node device enumeration
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: David F. Lively <dlively@virtualiron.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "memory.h"
|
2008-12-04 21:48:31 +00:00
|
|
|
#include "logging.h"
|
2008-11-21 12:27:11 +00:00
|
|
|
#include "node_device_conf.h"
|
|
|
|
#include "node_device.h"
|
|
|
|
|
2009-01-29 12:10:32 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NODEDEV
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
static int dev_has_cap(const virNodeDeviceObjPtr dev, const char *cap)
|
|
|
|
{
|
|
|
|
virNodeDevCapsDefPtr caps = dev->def->caps;
|
|
|
|
while (caps) {
|
|
|
|
if (STREQ(cap, virNodeDevCapTypeToString(caps->type)))
|
|
|
|
return 1;
|
|
|
|
caps = caps->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
void nodeDeviceLock(virDeviceMonitorStatePtr driver)
|
|
|
|
{
|
|
|
|
DEBUG("LOCK node %p", driver);
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexLock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
void nodeDeviceUnlock(virDeviceMonitorStatePtr driver)
|
|
|
|
{
|
|
|
|
DEBUG("UNLOCK node %p", driver);
|
2009-01-15 19:56:05 +00:00
|
|
|
virMutexUnlock(&driver->lock);
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
static int nodeNumOfDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
int ndevs = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < driver->devs.count; i++)
|
|
|
|
if ((cap == NULL) ||
|
|
|
|
dev_has_cap(driver->devs.objs[i], cap))
|
|
|
|
++ndevs;
|
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nodeListDevices(virConnectPtr conn,
|
|
|
|
const char *cap,
|
|
|
|
char **const names, int maxnames,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
|
|
|
int ndevs = 0;
|
|
|
|
unsigned int i;
|
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
|
|
|
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
|
|
|
|
virNodeDeviceObjLock(driver->devs.objs[i]);
|
2008-11-21 12:27:11 +00:00
|
|
|
if (cap == NULL ||
|
2008-12-04 21:48:31 +00:00
|
|
|
dev_has_cap(driver->devs.objs[i], cap)) {
|
|
|
|
if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) {
|
|
|
|
virNodeDeviceObjUnlock(driver->devs.objs[i]);
|
2008-11-21 12:27:11 +00:00
|
|
|
goto failure;
|
2008-12-04 21:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
virNodeDeviceObjUnlock(driver->devs.objs[i]);
|
|
|
|
}
|
|
|
|
nodeDeviceUnlock(driver);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
|
|
|
|
failure:
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
2008-11-21 12:27:11 +00:00
|
|
|
--ndevs;
|
|
|
|
while (--ndevs >= 0)
|
|
|
|
VIR_FREE(names[ndevs]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virNodeDevicePtr nodeDeviceLookupByName(virConnectPtr conn,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
virNodeDevicePtr ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
2009-04-01 09:55:32 +00:00
|
|
|
virNodeDeviceReportError(conn, VIR_ERR_NO_NODE_DEVICE, NULL);
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = virGetNodeDevice(conn, name);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00: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
|
|
|
}
|
|
|
|
|
|
|
|
static char *nodeDeviceDumpXML(virNodeDevicePtr dev,
|
|
|
|
unsigned int flags ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_INVALID_NODE_DEVICE,
|
|
|
|
"%s", _("no node device with matching name"));
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = virNodeDeviceDefFormat(dev->conn, obj->def);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *nodeDeviceGetParent(virNodeDevicePtr dev)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
2008-12-04 21:46:34 +00:00
|
|
|
virNodeDeviceObjPtr obj;
|
|
|
|
char *ret = NULL;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_INVALID_NODE_DEVICE,
|
|
|
|
"%s", _("no node device with matching name"));
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 10:21:34 +00:00
|
|
|
if (obj->def->parent) {
|
|
|
|
ret = strdup(obj->def->parent);
|
|
|
|
if (!ret)
|
|
|
|
virReportOOMError(dev->conn);
|
|
|
|
} else {
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("no parent for this device"));
|
|
|
|
}
|
2008-12-04 21:46:34 +00: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int nodeDeviceNumOfCaps(virNodeDevicePtr dev)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
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
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_INVALID_NODE_DEVICE,
|
|
|
|
"%s", _("no node device with matching name"));
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (caps = obj->def->caps; caps; caps = caps->next)
|
|
|
|
++ncaps;
|
2008-12-04 21:46:34 +00:00
|
|
|
ret = ncaps;
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
|
|
|
|
{
|
|
|
|
virDeviceMonitorStatePtr driver = dev->conn->devMonPrivateData;
|
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
|
|
|
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceLock(driver);
|
2008-12-04 21:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
2008-12-04 21:48:31 +00:00
|
|
|
nodeDeviceUnlock(driver);
|
|
|
|
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_INVALID_NODE_DEVICE,
|
|
|
|
"%s", _("no node device with matching name"));
|
2008-12-04 21:46:34 +00:00
|
|
|
goto cleanup;
|
2008-11-21 12:27:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
|
|
|
|
names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type));
|
|
|
|
if (names[ncaps++] == NULL)
|
2008-12-04 21:46:34 +00:00
|
|
|
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
|
|
|
|
2008-12-04 21:46:34 +00: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerCommonNodeFuncs(virDeviceMonitorPtr driver)
|
|
|
|
{
|
|
|
|
driver->numOfDevices = nodeNumOfDevices;
|
|
|
|
driver->listDevices = nodeListDevices;
|
|
|
|
driver->deviceLookupByName = nodeDeviceLookupByName;
|
|
|
|
driver->deviceDumpXML = nodeDeviceDumpXML;
|
|
|
|
driver->deviceGetParent = nodeDeviceGetParent;
|
|
|
|
driver->deviceNumOfCaps = nodeDeviceNumOfCaps;
|
|
|
|
driver->deviceListCaps = nodeDeviceListCaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int nodedevRegister(void) {
|
|
|
|
#if defined(HAVE_HAL) && defined(HAVE_DEVKIT)
|
|
|
|
/* Register only one of these two - they conflict */
|
|
|
|
if (halNodeRegister() == -1)
|
|
|
|
return devkitNodeRegister();
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
#ifdef HAVE_HAL
|
|
|
|
return halNodeRegister();
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DEVKIT
|
|
|
|
return devkitNodeRegister();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|