mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-25 15:15:25 +00:00
Add interface object list manipulation functions
* interface_conf.c interface_conf.h: utilities function usful for interface driver like the test interface driver
This commit is contained in:
parent
9734c81f3a
commit
846f694908
@ -1128,6 +1128,8 @@ cleanup:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* virInterfaceObj manipulation */
|
||||||
|
|
||||||
void virInterfaceObjLock(virInterfaceObjPtr obj)
|
void virInterfaceObjLock(virInterfaceObjPtr obj)
|
||||||
{
|
{
|
||||||
virMutexLock(&obj->lock);
|
virMutexLock(&obj->lock);
|
||||||
@ -1137,3 +1139,136 @@ void virInterfaceObjUnlock(virInterfaceObjPtr obj)
|
|||||||
{
|
{
|
||||||
virMutexUnlock(&obj->lock);
|
virMutexUnlock(&obj->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void virInterfaceObjFree(virInterfaceObjPtr iface)
|
||||||
|
{
|
||||||
|
if (!iface)
|
||||||
|
return;
|
||||||
|
|
||||||
|
virInterfaceDefFree(iface->def);
|
||||||
|
virMutexDestroy(&iface->lock);
|
||||||
|
VIR_FREE(iface);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* virInterfaceObjList manipulation */
|
||||||
|
|
||||||
|
int virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
|
||||||
|
const char *mac,
|
||||||
|
virInterfaceObjPtr *matches, int maxmatches)
|
||||||
|
{
|
||||||
|
unsigned int i, matchct = 0;
|
||||||
|
|
||||||
|
for (i = 0 ; i < interfaces->count ; i++) {
|
||||||
|
|
||||||
|
virInterfaceObjLock(interfaces->objs[i]);
|
||||||
|
if (STRCASEEQ(interfaces->objs[i]->def->mac, mac)) {
|
||||||
|
matchct++;
|
||||||
|
if (matchct <= maxmatches) {
|
||||||
|
matches[matchct - 1] = interfaces->objs[i];
|
||||||
|
/* keep the lock if we're returning object to caller */
|
||||||
|
/* it is the caller's responsibility to unlock *all* matches */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
||||||
|
|
||||||
|
}
|
||||||
|
return matchct;
|
||||||
|
}
|
||||||
|
|
||||||
|
virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr
|
||||||
|
interfaces,
|
||||||
|
const char *name)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
for (i = 0 ; i < interfaces->count ; i++) {
|
||||||
|
virInterfaceObjLock(interfaces->objs[i]);
|
||||||
|
if (STREQ(interfaces->objs[i]->def->name, name))
|
||||||
|
return interfaces->objs[i];
|
||||||
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void virInterfaceObjListFree(virInterfaceObjListPtr interfaces)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
for (i = 0 ; i < interfaces->count ; i++)
|
||||||
|
virInterfaceObjFree(interfaces->objs[i]);
|
||||||
|
|
||||||
|
VIR_FREE(interfaces->objs);
|
||||||
|
interfaces->count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virInterfaceObjPtr virInterfaceAssignDef(virConnectPtr conn,
|
||||||
|
virInterfaceObjListPtr interfaces,
|
||||||
|
const virInterfaceDefPtr def)
|
||||||
|
{
|
||||||
|
virInterfaceObjPtr interface;
|
||||||
|
|
||||||
|
if ((interface = virInterfaceFindByName(interfaces, def->name))) {
|
||||||
|
if (interface->def)
|
||||||
|
virInterfaceDefFree(interface->def);
|
||||||
|
interface->def = def;
|
||||||
|
|
||||||
|
return interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (VIR_ALLOC(interface) < 0) {
|
||||||
|
virReportOOMError(conn);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (virMutexInit(&interface->lock) < 0) {
|
||||||
|
virInterfaceReportError(conn, VIR_ERR_INTERNAL_ERROR,
|
||||||
|
"%s", _("cannot initialize mutex"));
|
||||||
|
VIR_FREE(interface);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
virInterfaceObjLock(interface);
|
||||||
|
interface->def = def;
|
||||||
|
|
||||||
|
if (VIR_REALLOC_N(interfaces->objs, interfaces->count + 1) < 0) {
|
||||||
|
virReportOOMError(conn);
|
||||||
|
VIR_FREE(interface);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
interfaces->objs[interfaces->count] = interface;
|
||||||
|
interfaces->count++;
|
||||||
|
|
||||||
|
return interface;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void virInterfaceRemove(virInterfaceObjListPtr interfaces,
|
||||||
|
const virInterfaceObjPtr interface)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
virInterfaceObjUnlock(interface);
|
||||||
|
for (i = 0 ; i < interfaces->count ; i++) {
|
||||||
|
virInterfaceObjLock(interfaces->objs[i]);
|
||||||
|
if (interfaces->objs[i] == interface) {
|
||||||
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
||||||
|
virInterfaceObjFree(interfaces->objs[i]);
|
||||||
|
|
||||||
|
if (i < (interfaces->count - 1))
|
||||||
|
memmove(interfaces->objs + i, interfaces->objs + i + 1,
|
||||||
|
sizeof(*(interfaces->objs)) * (interfaces->count - (i + 1)));
|
||||||
|
|
||||||
|
if (VIR_REALLOC_N(interfaces->objs, interfaces->count - 1) < 0) {
|
||||||
|
; /* Failure to reduce memory allocation isn't fatal */
|
||||||
|
}
|
||||||
|
interfaces->count--;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,6 +160,7 @@ typedef virInterfaceObj *virInterfaceObjPtr;
|
|||||||
struct _virInterfaceObj {
|
struct _virInterfaceObj {
|
||||||
virMutex lock;
|
virMutex lock;
|
||||||
|
|
||||||
|
int active:1; /* 1 if interface is active (up) */
|
||||||
virInterfaceDefPtr def; /* The interface definition */
|
virInterfaceDefPtr def; /* The interface definition */
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -170,9 +171,17 @@ struct _virInterfaceObjList {
|
|||||||
virInterfaceObjPtr *objs;
|
virInterfaceObjPtr *objs;
|
||||||
};
|
};
|
||||||
|
|
||||||
virInterfaceObjPtr virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
|
static inline int
|
||||||
const char *mac);
|
virInterfaceIsActive(const virInterfaceObjPtr iface)
|
||||||
virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr interfaces,
|
{
|
||||||
|
return iface->active;
|
||||||
|
}
|
||||||
|
|
||||||
|
int virInterfaceFindByMACString(const virInterfaceObjListPtr interfaces,
|
||||||
|
const char *mac,
|
||||||
|
virInterfaceObjPtr *matches, int maxmatches);
|
||||||
|
virInterfaceObjPtr virInterfaceFindByName(const virInterfaceObjListPtr
|
||||||
|
interfaces,
|
||||||
const char *name);
|
const char *name);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user