2017-02-28 22:37:15 +00:00
|
|
|
/*
|
|
|
|
* virnwfilterobj.c: network filter object processing
|
|
|
|
* (derived from nwfilter_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 <dirent.h>
|
|
|
|
|
|
|
|
#include "datatypes.h"
|
|
|
|
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virnwfilterobj.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NWFILTER
|
|
|
|
|
|
|
|
VIR_LOG_INIT("conf.virnwfilterobj");
|
|
|
|
|
2017-04-18 16:02:12 +00:00
|
|
|
struct _virNWFilterObj {
|
|
|
|
virMutex lock;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2017-04-18 16:02:12 +00:00
|
|
|
bool wantRemoved;
|
|
|
|
|
|
|
|
virNWFilterDefPtr def;
|
|
|
|
virNWFilterDefPtr newDef;
|
|
|
|
};
|
|
|
|
|
2017-04-18 16:52:10 +00:00
|
|
|
struct _virNWFilterObjList {
|
|
|
|
size_t count;
|
|
|
|
virNWFilterObjPtr *objs;
|
|
|
|
};
|
|
|
|
|
2017-04-18 16:02:12 +00:00
|
|
|
|
2017-04-20 15:15:28 +00:00
|
|
|
static virNWFilterObjPtr
|
|
|
|
virNWFilterObjNew(void)
|
|
|
|
{
|
|
|
|
virNWFilterObjPtr obj;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(obj) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virMutexInitRecursive(&obj->lock) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("cannot initialize mutex"));
|
|
|
|
VIR_FREE(obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNWFilterObjLock(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 16:02:12 +00:00
|
|
|
virNWFilterDefPtr
|
|
|
|
virNWFilterObjGetDef(virNWFilterObjPtr obj)
|
|
|
|
{
|
|
|
|
return obj->def;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNWFilterDefPtr
|
|
|
|
virNWFilterObjGetNewDef(virNWFilterObjPtr obj)
|
|
|
|
{
|
|
|
|
return obj->newDef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
virNWFilterObjWantRemoved(virNWFilterObjPtr obj)
|
|
|
|
{
|
|
|
|
return obj->wantRemoved;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-02-28 22:37:15 +00:00
|
|
|
virNWFilterObjFree(virNWFilterObjPtr obj)
|
|
|
|
{
|
|
|
|
if (!obj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virNWFilterDefFree(obj->def);
|
|
|
|
virNWFilterDefFree(obj->newDef);
|
|
|
|
|
|
|
|
virMutexDestroy(&obj->lock);
|
|
|
|
|
|
|
|
VIR_FREE(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virNWFilterObjListFree(virNWFilterObjListPtr nwfilters)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < nwfilters->count; i++)
|
|
|
|
virNWFilterObjFree(nwfilters->objs[i]);
|
2017-05-04 15:25:09 +00:00
|
|
|
VIR_FREE(nwfilters->objs);
|
2017-04-18 16:52:10 +00:00
|
|
|
VIR_FREE(nwfilters);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNWFilterObjListPtr
|
|
|
|
virNWFilterObjListNew(void)
|
|
|
|
{
|
|
|
|
virNWFilterObjListPtr nwfilters;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(nwfilters) < 0)
|
|
|
|
return NULL;
|
|
|
|
return nwfilters;
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListRemove(virNWFilterObjListPtr nwfilters,
|
|
|
|
virNWFilterObjPtr obj)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count; i++) {
|
|
|
|
virNWFilterObjLock(nwfilters->objs[i]);
|
2017-04-19 02:30:15 +00:00
|
|
|
if (nwfilters->objs[i] == obj) {
|
2017-02-28 22:37:15 +00:00
|
|
|
virNWFilterObjUnlock(nwfilters->objs[i]);
|
|
|
|
virNWFilterObjFree(nwfilters->objs[i]);
|
|
|
|
|
|
|
|
VIR_DELETE_ELEMENT(nwfilters->objs, i, nwfilters->count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
virNWFilterObjUnlock(nwfilters->objs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNWFilterObjPtr
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListFindByUUID(virNWFilterObjListPtr nwfilters,
|
|
|
|
const unsigned char *uuid)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterObjPtr obj;
|
|
|
|
virNWFilterDefPtr def;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count; i++) {
|
2017-04-19 02:32:32 +00:00
|
|
|
obj = nwfilters->objs[i];
|
|
|
|
virNWFilterObjLock(obj);
|
|
|
|
def = obj->def;
|
|
|
|
if (!memcmp(def->uuid, uuid, VIR_UUID_BUFLEN))
|
|
|
|
return obj;
|
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNWFilterObjPtr
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListFindByName(virNWFilterObjListPtr nwfilters,
|
|
|
|
const char *name)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterObjPtr obj;
|
|
|
|
virNWFilterDefPtr def;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count; i++) {
|
2017-04-19 02:32:32 +00:00
|
|
|
obj = nwfilters->objs[i];
|
|
|
|
virNWFilterObjLock(obj);
|
|
|
|
def = obj->def;
|
|
|
|
if (STREQ_NULLABLE(def->name, name))
|
|
|
|
return obj;
|
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-30 21:27:04 +00:00
|
|
|
virNWFilterObjPtr
|
|
|
|
virNWFilterObjListFindInstantiateFilter(virNWFilterObjListPtr nwfilters,
|
|
|
|
const char *filtername)
|
|
|
|
{
|
|
|
|
virNWFilterObjPtr obj;
|
|
|
|
|
|
|
|
if (!(obj = virNWFilterObjListFindByName(nwfilters, filtername))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("referenced filter '%s' is missing"), filtername);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virNWFilterObjWantRemoved(obj)) {
|
|
|
|
virReportError(VIR_ERR_NO_NWFILTER,
|
|
|
|
_("Filter '%s' is in use."), filtername);
|
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-28 22:37:15 +00:00
|
|
|
static int
|
2017-04-18 16:38:50 +00:00
|
|
|
_virNWFilterObjListDefLoopDetect(virNWFilterObjListPtr nwfilters,
|
|
|
|
virNWFilterDefPtr def,
|
|
|
|
const char *filtername)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
size_t i;
|
|
|
|
virNWFilterEntryPtr entry;
|
|
|
|
virNWFilterObjPtr obj;
|
|
|
|
|
|
|
|
if (!def)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < def->nentries; i++) {
|
|
|
|
entry = def->filterEntries[i];
|
|
|
|
if (entry->include) {
|
|
|
|
|
|
|
|
if (STREQ(filtername, entry->include->filterref)) {
|
|
|
|
rc = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:38:50 +00:00
|
|
|
obj = virNWFilterObjListFindByName(nwfilters,
|
|
|
|
entry->include->filterref);
|
2017-02-28 22:37:15 +00:00
|
|
|
if (obj) {
|
2017-04-18 16:38:50 +00:00
|
|
|
rc = _virNWFilterObjListDefLoopDetect(nwfilters, obj->def,
|
|
|
|
filtername);
|
2017-02-28 22:37:15 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
if (rc < 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-04-18 16:38:50 +00:00
|
|
|
* virNWFilterObjListDefLoopDetect:
|
2017-02-28 22:37:15 +00:00
|
|
|
* @nwfilters : the nwfilters to search
|
|
|
|
* @def : the filter definition that may add a loop and is to be tested
|
|
|
|
*
|
|
|
|
* Detect a loop introduced through the filters being able to
|
|
|
|
* reference each other.
|
|
|
|
*
|
|
|
|
* Returns 0 in case no loop was detected, -1 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListDefLoopDetect(virNWFilterObjListPtr nwfilters,
|
|
|
|
virNWFilterDefPtr def)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
2017-04-18 16:38:50 +00:00
|
|
|
return _virNWFilterObjListDefLoopDetect(nwfilters, def, def->name);
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjTestUnassignDef(virNWFilterObjPtr obj)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
2017-04-18 13:11:51 +00:00
|
|
|
obj->wantRemoved = true;
|
2017-02-28 22:37:15 +00:00
|
|
|
/* trigger the update on VMs referencing the filter */
|
2018-04-27 12:25:10 +00:00
|
|
|
if (virNWFilterTriggerRebuild() < 0)
|
2017-02-28 22:37:15 +00:00
|
|
|
rc = -1;
|
|
|
|
|
2017-04-18 13:11:51 +00:00
|
|
|
obj->wantRemoved = false;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-03-02 17:13:02 +00:00
|
|
|
|
2017-02-28 22:37:15 +00:00
|
|
|
static bool
|
2017-03-02 17:13:02 +00:00
|
|
|
virNWFilterDefEqual(const virNWFilterDef *def1,
|
2017-07-16 14:09:10 +00:00
|
|
|
virNWFilterDefPtr def2)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
2017-07-16 14:09:10 +00:00
|
|
|
char *xml1 = NULL;
|
|
|
|
char *xml2 = NULL;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
if (!(xml1 = virNWFilterDefFormat(def1)) ||
|
|
|
|
!(xml2 = virNWFilterDefFormat(def2)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = STREQ(xml1, xml2);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(xml1);
|
|
|
|
VIR_FREE(xml2);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-02 17:13:02 +00:00
|
|
|
|
2017-02-28 22:37:15 +00:00
|
|
|
virNWFilterObjPtr
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListAssignDef(virNWFilterObjListPtr nwfilters,
|
|
|
|
virNWFilterDefPtr def)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjPtr obj;
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterDefPtr objdef;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2017-04-18 16:38:50 +00:00
|
|
|
if ((obj = virNWFilterObjListFindByUUID(nwfilters, def->uuid))) {
|
2017-04-19 02:32:32 +00:00
|
|
|
objdef = obj->def;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2017-04-19 02:32:32 +00:00
|
|
|
if (STRNEQ(def->name, objdef->name)) {
|
2017-02-28 22:37:15 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("filter with same UUID but different name "
|
|
|
|
"('%s') already exists"),
|
2017-04-19 02:32:32 +00:00
|
|
|
objdef->name);
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
} else {
|
2017-04-18 16:38:50 +00:00
|
|
|
if ((obj = virNWFilterObjListFindByName(nwfilters, def->name))) {
|
2017-02-28 22:37:15 +00:00
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
2017-04-19 02:32:32 +00:00
|
|
|
|
|
|
|
objdef = obj->def;
|
|
|
|
virUUIDFormat(objdef->uuid, uuidstr);
|
2017-02-28 22:37:15 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("filter '%s' already exists with uuid %s"),
|
|
|
|
def->name, uuidstr);
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:38:50 +00:00
|
|
|
if (virNWFilterObjListDefLoopDetect(nwfilters, def) < 0) {
|
2017-02-28 22:37:15 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
"%s", _("filter would introduce a loop"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 16:38:50 +00:00
|
|
|
if ((obj = virNWFilterObjListFindByName(nwfilters, def->name))) {
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2017-04-19 02:32:32 +00:00
|
|
|
objdef = obj->def;
|
2017-07-16 14:09:10 +00:00
|
|
|
if (virNWFilterDefEqual(def, objdef)) {
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterDefFree(objdef);
|
2017-04-19 02:30:15 +00:00
|
|
|
obj->def = def;
|
|
|
|
return obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 02:30:15 +00:00
|
|
|
obj->newDef = def;
|
2017-02-28 22:37:15 +00:00
|
|
|
/* trigger the update on VMs referencing the filter */
|
2018-04-27 12:25:10 +00:00
|
|
|
if (virNWFilterTriggerRebuild() < 0) {
|
2017-04-19 02:30:15 +00:00
|
|
|
obj->newDef = NULL;
|
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterDefFree(objdef);
|
2017-04-19 02:30:15 +00:00
|
|
|
obj->def = def;
|
|
|
|
obj->newDef = NULL;
|
|
|
|
return obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 15:15:28 +00:00
|
|
|
if (!(obj = virNWFilterObjNew()))
|
2017-02-28 22:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT_COPY(nwfilters->objs,
|
2017-04-19 02:30:15 +00:00
|
|
|
nwfilters->count, obj) < 0) {
|
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
virNWFilterObjFree(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-19 02:30:15 +00:00
|
|
|
obj->def = def;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2017-04-19 02:30:15 +00:00
|
|
|
return obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-19 12:20:25 +00:00
|
|
|
int
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListNumOfNWFilters(virNWFilterObjListPtr nwfilters,
|
|
|
|
virConnectPtr conn,
|
2017-08-16 13:26:07 +00:00
|
|
|
virNWFilterObjListFilter filter)
|
2017-03-19 12:20:25 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int nfilters = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count; i++) {
|
|
|
|
virNWFilterObjPtr obj = nwfilters->objs[i];
|
|
|
|
virNWFilterObjLock(obj);
|
2017-08-16 13:26:07 +00:00
|
|
|
if (!filter || filter(conn, obj->def))
|
2017-03-19 12:20:25 +00:00
|
|
|
nfilters++;
|
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nfilters;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-19 20:24:59 +00:00
|
|
|
int
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListGetNames(virNWFilterObjListPtr nwfilters,
|
|
|
|
virConnectPtr conn,
|
2017-08-16 13:26:07 +00:00
|
|
|
virNWFilterObjListFilter filter,
|
2017-04-18 16:38:50 +00:00
|
|
|
char **const names,
|
|
|
|
int maxnames)
|
2017-03-19 20:24:59 +00:00
|
|
|
{
|
|
|
|
int nnames = 0;
|
|
|
|
size_t i;
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterDefPtr def;
|
2017-03-19 20:24:59 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count && nnames < maxnames; i++) {
|
|
|
|
virNWFilterObjPtr obj = nwfilters->objs[i];
|
|
|
|
virNWFilterObjLock(obj);
|
2017-04-19 02:32:32 +00:00
|
|
|
def = obj->def;
|
2017-08-16 13:26:07 +00:00
|
|
|
if (!filter || filter(conn, def)) {
|
2019-10-20 11:49:46 +00:00
|
|
|
names[nnames] = g_strdup(def->name);
|
2017-03-19 20:24:59 +00:00
|
|
|
nnames++;
|
|
|
|
}
|
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nnames;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-20 11:08:46 +00:00
|
|
|
int
|
|
|
|
virNWFilterObjListExport(virConnectPtr conn,
|
|
|
|
virNWFilterObjListPtr nwfilters,
|
|
|
|
virNWFilterPtr **filters,
|
2017-08-16 13:26:07 +00:00
|
|
|
virNWFilterObjListFilter filter)
|
2017-03-20 11:08:46 +00:00
|
|
|
{
|
|
|
|
virNWFilterPtr *tmp_filters = NULL;
|
|
|
|
int nfilters = 0;
|
2017-08-16 13:26:07 +00:00
|
|
|
virNWFilterPtr nwfilter = NULL;
|
2017-03-20 11:08:46 +00:00
|
|
|
virNWFilterObjPtr obj = NULL;
|
2017-04-19 02:32:32 +00:00
|
|
|
virNWFilterDefPtr def;
|
2017-03-20 11:08:46 +00:00
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!filters) {
|
|
|
|
ret = nwfilters->count;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(tmp_filters, nwfilters->count + 1) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < nwfilters->count; i++) {
|
|
|
|
obj = nwfilters->objs[i];
|
|
|
|
virNWFilterObjLock(obj);
|
2017-04-19 02:32:32 +00:00
|
|
|
def = obj->def;
|
2017-08-16 13:26:07 +00:00
|
|
|
if (!filter || filter(conn, def)) {
|
|
|
|
if (!(nwfilter = virGetNWFilter(conn, def->name, def->uuid))) {
|
2017-03-20 11:08:46 +00:00
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-08-16 13:26:07 +00:00
|
|
|
tmp_filters[nfilters++] = nwfilter;
|
2017-03-20 11:08:46 +00:00
|
|
|
}
|
|
|
|
virNWFilterObjUnlock(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
*filters = tmp_filters;
|
|
|
|
tmp_filters = NULL;
|
|
|
|
ret = nfilters;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (tmp_filters) {
|
|
|
|
for (i = 0; i < nfilters; i ++)
|
|
|
|
virObjectUnref(tmp_filters[i]);
|
|
|
|
}
|
|
|
|
VIR_FREE(tmp_filters);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-28 22:37:15 +00:00
|
|
|
static virNWFilterObjPtr
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListLoadConfig(virNWFilterObjListPtr nwfilters,
|
|
|
|
const char *configDir,
|
|
|
|
const char *name)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
virNWFilterDefPtr def = NULL;
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjPtr obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
char *configFile = NULL;
|
|
|
|
|
|
|
|
if (!(configFile = virFileBuildPath(configDir, name, ".xml")))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(def = virNWFilterDefParseFile(configFile)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (STRNEQ(name, def->name)) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("network filter config filename '%s' "
|
|
|
|
"does not match name '%s'"),
|
|
|
|
configFile, def->name);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We generated a UUID, make it permanent by saving the config to disk */
|
|
|
|
if (!def->uuid_specified &&
|
|
|
|
virNWFilterSaveConfig(configDir, def) < 0)
|
|
|
|
goto error;
|
|
|
|
|
2017-07-15 12:23:42 +00:00
|
|
|
if (!(obj = virNWFilterObjListAssignDef(nwfilters, def)))
|
|
|
|
goto error;
|
|
|
|
|
2017-02-28 22:37:15 +00:00
|
|
|
VIR_FREE(configFile);
|
2017-04-19 02:30:15 +00:00
|
|
|
return obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
VIR_FREE(configFile);
|
|
|
|
virNWFilterDefFree(def);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-04-18 16:38:50 +00:00
|
|
|
virNWFilterObjListLoadAllConfigs(virNWFilterObjListPtr nwfilters,
|
|
|
|
const char *configDir)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
int ret = -1;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ((rc = virDirOpenIfExists(&dir, configDir)) <= 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
while ((ret = virDirRead(dir, &entry, configDir)) > 0) {
|
2017-04-19 02:30:15 +00:00
|
|
|
virNWFilterObjPtr obj;
|
2017-02-28 22:37:15 +00:00
|
|
|
|
2019-03-06 14:30:04 +00:00
|
|
|
if (!virStringStripSuffix(entry->d_name, ".xml"))
|
2017-02-28 22:37:15 +00:00
|
|
|
continue;
|
|
|
|
|
2017-04-18 16:38:50 +00:00
|
|
|
obj = virNWFilterObjListLoadConfig(nwfilters, configDir, entry->d_name);
|
2017-04-19 02:30:15 +00:00
|
|
|
if (obj)
|
|
|
|
virNWFilterObjUnlock(obj);
|
2017-02-28 22:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DIR_CLOSE(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 17:13:02 +00:00
|
|
|
void
|
|
|
|
virNWFilterObjLock(virNWFilterObjPtr obj)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
virMutexLock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-02 17:13:02 +00:00
|
|
|
void
|
|
|
|
virNWFilterObjUnlock(virNWFilterObjPtr obj)
|
2017-02-28 22:37:15 +00:00
|
|
|
{
|
|
|
|
virMutexUnlock(&obj->lock);
|
|
|
|
}
|