libvirt/src/conf/virnwfilterobj.h

120 lines
3.4 KiB
C
Raw Normal View History

/*
* virnwfilterobj.h: network filter object processing
* (derived from nwfilter_conf.h)
*
* 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/>.
*/
#pragma once
#include "internal.h"
#include "nwfilter_conf.h"
#include "virnwfilterbindingobjlist.h"
typedef struct _virNWFilterObj virNWFilterObj;
typedef struct _virNWFilterObjList virNWFilterObjList;
typedef struct _virNWFilterDriverState virNWFilterDriverState;
struct _virNWFilterDriverState {
bool privileged;
/* pid file FD, ensures two copies of the driver can't use the same root */
int lockFD;
virNWFilterObjList *nwfilters;
virNWFilterBindingObjList *bindings;
char *stateDir;
char *configDir;
char *bindingDir;
nwfilter: merge updateMutex and updateLock The updateLock is a R/W lock held by anything which needs to read or modify the rules associated with an NWFilter. APIs for defining/undefining NW filters rules hold a write lock on updateLock. APIs for creating/deleting NW filter bindings hold a read lock on updateLock, which prevents define/undefine taking place concurrently. The problems arise when we attempt to creating two NW filter bindings in parallel. Thread 1 can acquire the mutex for filter A Thread 2 can acquire the mutex for filter B Consider if filters A and B both reference filters C and D, but in different orders: Filter A -> filter C -> filter D Filter B -> filter D -> filter C Thread 1 will try to acquire locks in order A, C, D while thread 1 will try to acquire in order A, D, C. Deadlock can still occur. Think we can sort the list of filters before acquiring locks on all of them ? Nope, we allow arbitrary recursion: Filter A -> filter C -> filter E -> filter F -> filter H -> filter K -> filter D -> filter G -> filter I So we can't tell from looking at 'A' which filters we're going to need to lock. We can only see the first level of filters references and we need to lock those before we can see the second level of filters, etc. We could probably come up with some cleverness to address this but it isn't worth the time investment. It is simpler to just keep the process of creating NW filter bindings totally serialized. Using two separate locks for this serialization though is pointless. Every code path which gets a read(updateLock) will go on to hold updateMutex. It is simpler to just hold write(updateLock) and get rid of updateMutex. At that point we don't need updateLock to be a R/W lock, it can be a plain mutex. Thus this patch gets rid of the current updateLock and updateMutex and introduces a new top level updateMutex. This has a secondary benefit of introducing fairness into the locking. With a POSIX R/W lock, you get writer starvation if you have lots of readers. IOW, if we call virNWFilterBIndingCreate and virNWFilterBindingDelete in a tight loop from a couple of threads, we can prevent virNWFilterDefine from ever acquiring a write lock. Getting rid of the R/W lock gives us FIFO lock acquisition preventing starvation of any API call servicing. Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2022-03-07 11:11:48 +00:00
/* Recursive. Hold for filter changes, instantiation or deletion */
virMutex updateLock;
bool updateLockInitialized;
};
virNWFilterDef *
virNWFilterObjGetDef(virNWFilterObj *obj);
virNWFilterDef *
virNWFilterObjGetNewDef(virNWFilterObj *obj);
bool
virNWFilterObjWantRemoved(virNWFilterObj *obj);
virNWFilterObjList *
virNWFilterObjListNew(void);
void
virNWFilterObjListFree(virNWFilterObjList *nwfilters);
void
virNWFilterObjListRemove(virNWFilterObjList *nwfilters,
virNWFilterObj *obj);
virNWFilterObj *
virNWFilterObjListFindByUUID(virNWFilterObjList *nwfilters,
const unsigned char *uuid);
virNWFilterObj *
virNWFilterObjListFindByName(virNWFilterObjList *nwfilters,
const char *name);
virNWFilterObj *
virNWFilterObjListFindInstantiateFilter(virNWFilterObjList *nwfilters,
const char *filtername);
virNWFilterObj *
virNWFilterObjListAssignDef(virNWFilterObjList *nwfilters,
virNWFilterDef *def);
int
virNWFilterObjTestUnassignDef(virNWFilterObj *obj);
typedef bool
(*virNWFilterObjListFilter)(virConnectPtr conn,
virNWFilterDef *def);
int
virNWFilterObjListNumOfNWFilters(virNWFilterObjList *nwfilters,
virConnectPtr conn,
virNWFilterObjListFilter filter);
int
virNWFilterObjListGetNames(virNWFilterObjList *nwfilters,
virConnectPtr conn,
virNWFilterObjListFilter filter,
char **const names,
int maxnames);
int
virNWFilterObjListExport(virConnectPtr conn,
virNWFilterObjList *nwfilters,
virNWFilterPtr **filters,
virNWFilterObjListFilter filter);
int
virNWFilterObjListLoadAllConfigs(virNWFilterObjList *nwfilters,
const char *configDir);
void
virNWFilterObjLock(virNWFilterObj *obj);
void
virNWFilterObjUnlock(virNWFilterObj *obj);