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"
|
|
|
|
|
|
|
|
#include "node_device_conf.h"
|
|
|
|
#include "node_device.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < driver->devs.count && ndevs < maxnames; i++)
|
|
|
|
if (cap == NULL ||
|
|
|
|
dev_has_cap(driver->devs.objs[i], cap))
|
|
|
|
if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
return ndevs;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
--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:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, name);
|
2008-11-21 12:27:11 +00:00
|
|
|
if (!obj) {
|
|
|
|
virNodeDeviceReportError(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 = virGetNodeDevice(conn, name);
|
2008-11-21 12:27:11 +00:00
|
|
|
|
2008-12-04 21:46:34 +00:00
|
|
|
cleanup:
|
|
|
|
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:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
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:
|
|
|
|
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:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
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 = strdup(obj->def->parent);
|
|
|
|
if (!ret)
|
|
|
|
virNodeDeviceReportError(dev->conn, VIR_ERR_NO_MEMORY, NULL);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
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:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
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:
|
|
|
|
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:46:34 +00:00
|
|
|
obj = virNodeDeviceFindByName(&driver->devs, dev->name);
|
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:
|
|
|
|
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
|
|
|
|
}
|