nwfilter_learnipaddr: Use automatic mutex management

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2022-03-08 14:11:02 +01:00
parent 2d7682dd3b
commit dd8150c48d

View File

@ -143,11 +143,9 @@ static bool threadsTerminate;
int
virNWFilterLockIface(const char *ifname)
{
virNWFilterIfaceLock *ifaceLock;
VIR_LOCK_GUARD lock = virLockGuardLock(&ifaceMapLock);
virNWFilterIfaceLock *ifaceLock = virHashLookup(ifaceLockMap, ifname);
virMutexLock(&ifaceMapLock);
ifaceLock = virHashLookup(ifaceLockMap, ifname);
if (!ifaceLock) {
ifaceLock = g_new0(virNWFilterIfaceLock, 1);
@ -155,21 +153,20 @@ virNWFilterLockIface(const char *ifname)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("mutex initialization failed"));
g_free(ifaceLock);
goto error;
return -1;
}
if (virStrcpyStatic(ifaceLock->ifname, ifname) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("interface name %s does not fit into "
"buffer "),
_("interface name %s does not fit into buffer"),
ifaceLock->ifname);
g_free(ifaceLock);
goto error;
return -1;
}
while (virHashAddEntry(ifaceLockMap, ifname, ifaceLock)) {
g_free(ifaceLock);
goto error;
return -1;
}
ifaceLock->refctr = 0;
@ -177,27 +174,17 @@ virNWFilterLockIface(const char *ifname)
ifaceLock->refctr++;
virMutexUnlock(&ifaceMapLock);
virMutexLock(&ifaceLock->lock);
return 0;
error:
virMutexUnlock(&ifaceMapLock);
return -1;
}
void
virNWFilterUnlockIface(const char *ifname)
{
virNWFilterIfaceLock *ifaceLock;
virMutexLock(&ifaceMapLock);
ifaceLock = virHashLookup(ifaceLockMap, ifname);
VIR_LOCK_GUARD lock = virLockGuardLock(&ifaceMapLock);
virNWFilterIfaceLock *ifaceLock = virHashLookup(ifaceLockMap, ifname);
if (ifaceLock) {
virMutexUnlock(&ifaceLock->lock);
@ -206,8 +193,6 @@ virNWFilterUnlockIface(const char *ifname)
if (ifaceLock->refctr == 0)
virHashRemoveEntry(ifaceLockMap, ifname);
}
virMutexUnlock(&ifaceMapLock);
}
@ -228,17 +213,13 @@ virNWFilterIPAddrLearnReqFree(virNWFilterIPAddrLearnReq *req)
static int
virNWFilterRegisterLearnReq(virNWFilterIPAddrLearnReq *req)
{
int res = -1;
g_autofree char *ifindex_str = g_strdup_printf("%d", req->ifindex);
VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
virMutexLock(&pendingLearnReqLock);
if (virHashLookup(pendingLearnReq, ifindex_str))
return -1;
if (!virHashLookup(pendingLearnReq, ifindex_str))
res = virHashAddEntry(pendingLearnReq, ifindex_str, req);
virMutexUnlock(&pendingLearnReqLock);
return res;
return virHashAddEntry(pendingLearnReq, ifindex_str, req);
}
@ -247,9 +228,7 @@ virNWFilterRegisterLearnReq(virNWFilterIPAddrLearnReq *req)
int
virNWFilterTerminateLearnReq(const char *ifname)
{
int rc = -1;
int ifindex;
virNWFilterIPAddrLearnReq *req;
g_autofree char *ifindex_str = NULL;
/* It's possible that it's already been removed as a result of
@ -262,38 +241,30 @@ virNWFilterTerminateLearnReq(const char *ifname)
if (virNetDevGetIndex(ifname, &ifindex) < 0) {
virResetLastError();
return rc;
return -1;
}
ifindex_str = g_strdup_printf("%d", ifindex);
virMutexLock(&pendingLearnReqLock);
req = virHashLookup(pendingLearnReq, ifindex_str);
if (req) {
rc = 0;
req->terminate = true;
VIR_WITH_MUTEX_LOCK_GUARD(&pendingLearnReqLock) {
virNWFilterIPAddrLearnReq *req;
if ((req = virHashLookup(pendingLearnReq, ifindex_str))) {
req->terminate = true;
return 0;
}
}
virMutexUnlock(&pendingLearnReqLock);
return rc;
return -1;
}
bool
virNWFilterHasLearnReq(int ifindex)
{
void *res;
g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex);
VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
virMutexLock(&pendingLearnReqLock);
res = virHashLookup(pendingLearnReq, ifindex_str);
virMutexUnlock(&pendingLearnReqLock);
return res != NULL;
return virHashLookup(pendingLearnReq, ifindex_str) != NULL;
}
@ -309,16 +280,10 @@ freeLearnReqEntry(void *payload)
static virNWFilterIPAddrLearnReq *
virNWFilterDeregisterLearnReq(int ifindex)
{
virNWFilterIPAddrLearnReq *res;
g_autofree char *ifindex_str = g_strdup_printf("%d", ifindex);
VIR_LOCK_GUARD lock = virLockGuardLock(&pendingLearnReqLock);
virMutexLock(&pendingLearnReqLock);
res = virHashSteal(pendingLearnReq, ifindex_str);
virMutexUnlock(&pendingLearnReqLock);
return res;
return virHashSteal(pendingLearnReq, ifindex_str);
}
#endif