2017-02-28 21:20:44 +00:00
|
|
|
/*
|
|
|
|
* virinterfaceobj.c: interface object handling
|
|
|
|
* (derived from interface_conf.c)
|
|
|
|
*
|
|
|
|
* 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, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "datatypes.h"
|
|
|
|
#include "interface_conf.h"
|
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virinterfaceobj.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_INTERFACE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("conf.virinterfaceobj");
|
|
|
|
|
2017-04-25 21:45:02 +00:00
|
|
|
struct _virInterfaceObj {
|
|
|
|
virMutex lock;
|
|
|
|
|
|
|
|
bool active; /* true if interface is active (up) */
|
|
|
|
virInterfaceDefPtr def; /* The interface definition */
|
|
|
|
};
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
struct _virInterfaceObjList {
|
|
|
|
size_t count;
|
|
|
|
virInterfaceObjPtr *objs;
|
|
|
|
};
|
2017-02-28 21:20:44 +00:00
|
|
|
|
|
|
|
/* virInterfaceObj manipulation */
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
void
|
|
|
|
virInterfaceObjLock(virInterfaceObjPtr obj)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
virMutexLock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virInterfaceObjUnlock(virInterfaceObjPtr obj)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
virMutexUnlock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
void
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceObjFree(virInterfaceObjPtr obj)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
2017-04-19 02:29:44 +00:00
|
|
|
if (!obj)
|
2017-02-28 21:20:44 +00:00
|
|
|
return;
|
|
|
|
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceDefFree(obj->def);
|
|
|
|
virMutexDestroy(&obj->lock);
|
|
|
|
VIR_FREE(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 21:45:02 +00:00
|
|
|
virInterfaceDefPtr
|
|
|
|
virInterfaceObjGetDef(virInterfaceObjPtr obj)
|
|
|
|
{
|
|
|
|
return obj->def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
virInterfaceObjIsActive(virInterfaceObjPtr obj)
|
|
|
|
{
|
|
|
|
return obj->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virInterfaceObjSetActive(virInterfaceObjPtr obj,
|
|
|
|
bool active)
|
|
|
|
{
|
|
|
|
obj->active = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
/* virInterfaceObjList manipulation */
|
2017-04-14 19:45:27 +00:00
|
|
|
virInterfaceObjListPtr
|
|
|
|
virInterfaceObjListNew(void)
|
|
|
|
{
|
|
|
|
virInterfaceObjListPtr interfaces;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(interfaces) < 0)
|
|
|
|
return NULL;
|
|
|
|
return interfaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
int
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListFindByMACString(virInterfaceObjListPtr interfaces,
|
|
|
|
const char *mac,
|
|
|
|
virInterfaceObjPtr *matches,
|
|
|
|
int maxmatches)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned int matchct = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < interfaces->count; i++) {
|
2017-04-19 02:32:17 +00:00
|
|
|
virInterfaceObjPtr obj = interfaces->objs[i];
|
|
|
|
virInterfaceDefPtr def;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-19 02:32:17 +00:00
|
|
|
virInterfaceObjLock(obj);
|
|
|
|
def = obj->def;
|
|
|
|
if (STRCASEEQ(def->mac, mac)) {
|
2017-02-28 21:20:44 +00:00
|
|
|
matchct++;
|
|
|
|
if (matchct <= maxmatches) {
|
2017-04-19 02:32:17 +00:00
|
|
|
matches[matchct - 1] = obj;
|
2017-02-28 21:20:44 +00:00
|
|
|
/* keep the lock if we're returning object to caller */
|
|
|
|
/* it is the caller's responsibility to unlock *all* matches */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 02:32:17 +00:00
|
|
|
virInterfaceObjUnlock(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return matchct;
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
virInterfaceObjPtr
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListFindByName(virInterfaceObjListPtr interfaces,
|
|
|
|
const char *name)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < interfaces->count; i++) {
|
2017-04-19 02:32:17 +00:00
|
|
|
virInterfaceObjPtr obj = interfaces->objs[i];
|
|
|
|
virInterfaceDefPtr def;
|
|
|
|
|
|
|
|
virInterfaceObjLock(obj);
|
|
|
|
def = obj->def;
|
|
|
|
if (STREQ(def->name, name))
|
|
|
|
return obj;
|
|
|
|
virInterfaceObjUnlock(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virInterfaceObjListFree(virInterfaceObjListPtr interfaces)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < interfaces->count; i++)
|
|
|
|
virInterfaceObjFree(interfaces->objs[i]);
|
|
|
|
VIR_FREE(interfaces->objs);
|
2017-04-14 19:45:27 +00:00
|
|
|
VIR_FREE(interfaces);
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
virInterfaceObjListPtr
|
|
|
|
virInterfaceObjListClone(virInterfaceObjListPtr interfaces)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
unsigned int cnt;
|
2017-04-14 19:45:27 +00:00
|
|
|
virInterfaceObjListPtr dest;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
if (!interfaces)
|
|
|
|
return NULL;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
if (!(dest = virInterfaceObjListNew()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cnt = interfaces->count;
|
2017-02-28 21:20:44 +00:00
|
|
|
for (i = 0; i < cnt; i++) {
|
2017-04-14 19:45:27 +00:00
|
|
|
virInterfaceObjPtr srcobj = interfaces->objs[i];
|
2017-02-28 21:20:44 +00:00
|
|
|
virInterfaceDefPtr backup;
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceObjPtr obj;
|
2017-04-19 02:32:17 +00:00
|
|
|
char *xml = virInterfaceDefFormat(srcobj->def);
|
2017-02-28 21:20:44 +00:00
|
|
|
|
|
|
|
if (!xml)
|
2017-04-14 19:45:27 +00:00
|
|
|
goto error;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
if (!(backup = virInterfaceDefParseString(xml))) {
|
2017-02-28 21:20:44 +00:00
|
|
|
VIR_FREE(xml);
|
2017-04-14 19:45:27 +00:00
|
|
|
goto error;
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(xml);
|
2017-04-14 20:49:48 +00:00
|
|
|
if (!(obj = virInterfaceObjListAssignDef(dest, backup)))
|
2017-04-14 19:45:27 +00:00
|
|
|
goto error;
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjUnlock(obj); /* locked by virInterfaceObjListAssignDef */
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-14 19:45:27 +00:00
|
|
|
return dest;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virInterfaceObjListFree(dest);
|
|
|
|
return NULL;
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
virInterfaceObjPtr
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListAssignDef(virInterfaceObjListPtr interfaces,
|
|
|
|
virInterfaceDefPtr def)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceObjPtr obj;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-14 20:49:48 +00:00
|
|
|
if ((obj = virInterfaceObjListFindByName(interfaces, def->name))) {
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceDefFree(obj->def);
|
|
|
|
obj->def = def;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
2017-04-19 02:29:44 +00:00
|
|
|
return obj;
|
2017-02-28 21:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 02:29:44 +00:00
|
|
|
if (VIR_ALLOC(obj) < 0)
|
2017-02-28 21:20:44 +00:00
|
|
|
return NULL;
|
2017-04-19 02:29:44 +00:00
|
|
|
if (virMutexInit(&obj->lock) < 0) {
|
2017-02-28 21:20:44 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("cannot initialize mutex"));
|
2017-04-19 02:29:44 +00:00
|
|
|
VIR_FREE(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceObjLock(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT_COPY(interfaces->objs,
|
2017-04-19 02:29:44 +00:00
|
|
|
interfaces->count, obj) < 0) {
|
|
|
|
virInterfaceObjFree(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-19 02:29:44 +00:00
|
|
|
obj->def = def;
|
|
|
|
return obj;
|
2017-02-28 21:20:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:44:11 +00:00
|
|
|
|
|
|
|
void
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListRemove(virInterfaceObjListPtr interfaces,
|
|
|
|
virInterfaceObjPtr obj)
|
2017-02-28 21:20:44 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2017-04-19 02:29:44 +00:00
|
|
|
virInterfaceObjUnlock(obj);
|
2017-02-28 21:20:44 +00:00
|
|
|
for (i = 0; i < interfaces->count; i++) {
|
|
|
|
virInterfaceObjLock(interfaces->objs[i]);
|
2017-04-19 02:29:44 +00:00
|
|
|
if (interfaces->objs[i] == obj) {
|
2017-02-28 21:20:44 +00:00
|
|
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
|
|
|
virInterfaceObjFree(interfaces->objs[i]);
|
|
|
|
|
|
|
|
VIR_DELETE_ELEMENT(interfaces->objs, i, interfaces->count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
virInterfaceObjUnlock(interfaces->objs[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 13:23:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListNumOfInterfaces(virInterfaceObjListPtr interfaces,
|
|
|
|
bool wantActive)
|
2017-04-06 13:23:17 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ninterfaces = 0;
|
|
|
|
|
|
|
|
for (i = 0; (i < interfaces->count); i++) {
|
|
|
|
virInterfaceObjPtr obj = interfaces->objs[i];
|
|
|
|
virInterfaceObjLock(obj);
|
|
|
|
if (wantActive == virInterfaceObjIsActive(obj))
|
|
|
|
ninterfaces++;
|
|
|
|
virInterfaceObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ninterfaces;
|
|
|
|
}
|
2017-04-06 13:37:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-14 20:49:48 +00:00
|
|
|
virInterfaceObjListGetNames(virInterfaceObjListPtr interfaces,
|
|
|
|
bool wantActive,
|
|
|
|
char **const names,
|
|
|
|
int maxnames)
|
2017-04-06 13:37:57 +00:00
|
|
|
{
|
|
|
|
int nnames = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < interfaces->count && nnames < maxnames; i++) {
|
|
|
|
virInterfaceObjPtr obj = interfaces->objs[i];
|
2017-04-19 02:32:17 +00:00
|
|
|
virInterfaceDefPtr def;
|
|
|
|
|
2017-04-06 13:37:57 +00:00
|
|
|
virInterfaceObjLock(obj);
|
2017-04-19 02:32:17 +00:00
|
|
|
def = obj->def;
|
2017-04-06 13:37:57 +00:00
|
|
|
if (wantActive == virInterfaceObjIsActive(obj)) {
|
2017-04-19 02:32:17 +00:00
|
|
|
if (VIR_STRDUP(names[nnames], def->name) < 0) {
|
2017-04-06 13:37:57 +00:00
|
|
|
virInterfaceObjUnlock(obj);
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
nnames++;
|
|
|
|
}
|
|
|
|
virInterfaceObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nnames;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
while (--nnames >= 0)
|
|
|
|
VIR_FREE(names[nnames]);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|