network: inhibit idle timeout of daemon if there are any active networks

When the daemons were split out from the monolithic libvirtd, the
network driver didn't implement "inhibit idle timeout if there are any
active objects" as was done for other drivers, so virtnetworkd would
always exit after 120 seconds of no incoming connections. This didn't
every cause any visible problem, although it did mean that anytime a
network API was called after an idle time > 120 seconds, that the
restarting virtnetworkd would flush and reload all the
iptables/nftables rules for any active networks.

This patch replicates what is done in the QEMU driver - an nactive is
added to the network driver object, along with an inhibitCallback; the
latter is passed into networkStateInitialize when the driver is
loaded, and the former is incremented for each already-active network,
then incremented/decremented each time a network is started or
stopped. If nactive transitions from 0 to 1 or 1 to 0, inhibitCallback
is called, and it "does the right stuff" to prevent/enable the idle
timeout.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Laine Stump 2024-08-30 12:37:05 -04:00
parent f677665ce9
commit 37800af9a4
2 changed files with 25 additions and 4 deletions

View File

@ -501,9 +501,13 @@ networkUpdateState(virNetworkObj *obj,
return -1; return -1;
} }
if (virNetworkObjIsActive(obj)) if (virNetworkObjIsActive(obj)) {
virNetworkObjPortForEach(obj, networkUpdatePort, obj); virNetworkObjPortForEach(obj, networkUpdatePort, obj);
if (g_atomic_int_add(&driver->nactive, 1) == 0 && driver->inhibitCallback)
driver->inhibitCallback(true, driver->inhibitOpaque);
}
/* Try and read dnsmasq pids of both active and inactive networks, just in /* Try and read dnsmasq pids of both active and inactive networks, just in
* case a network became inactive and we need to clean up. */ * case a network became inactive and we need to clean up. */
if (def->ips && (def->nips > 0)) { if (def->ips && (def->nips > 0)) {
@ -617,8 +621,8 @@ static virDrvStateInitResult
networkStateInitialize(bool privileged, networkStateInitialize(bool privileged,
const char *root, const char *root,
bool monolithic G_GNUC_UNUSED, bool monolithic G_GNUC_UNUSED,
virStateInhibitCallback callback G_GNUC_UNUSED, virStateInhibitCallback callback,
void *opaque G_GNUC_UNUSED) void *opaque)
{ {
virNetworkDriverConfig *cfg; virNetworkDriverConfig *cfg;
bool autostart = true; bool autostart = true;
@ -640,6 +644,9 @@ networkStateInitialize(bool privileged,
goto error; goto error;
} }
network_driver->inhibitCallback = callback;
network_driver->inhibitOpaque = opaque;
network_driver->privileged = privileged; network_driver->privileged = privileged;
if (!(network_driver->xmlopt = networkDnsmasqCreateXMLConf())) if (!(network_driver->xmlopt = networkDnsmasqCreateXMLConf()))
@ -2419,6 +2426,9 @@ networkStartNetwork(virNetworkDriverState *driver,
obj, network_driver->xmlopt) < 0) obj, network_driver->xmlopt) < 0)
goto cleanup; goto cleanup;
if (g_atomic_int_add(&driver->nactive, 1) == 0 && driver->inhibitCallback)
driver->inhibitCallback(true, driver->inhibitOpaque);
virNetworkObjSetActive(obj, true); virNetworkObjSetActive(obj, true);
VIR_INFO("Network '%s' started up", def->name); VIR_INFO("Network '%s' started up", def->name);
ret = 0; ret = 0;
@ -2492,6 +2502,10 @@ networkShutdownNetwork(virNetworkDriverState *driver,
VIR_HOOK_SUBOP_END); VIR_HOOK_SUBOP_END);
virNetworkObjSetActive(obj, false); virNetworkObjSetActive(obj, false);
if (g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCallback)
driver->inhibitCallback(false, driver->inhibitOpaque);
virNetworkObjUnsetDefTransient(obj); virNetworkObjUnsetDefTransient(obj);
return ret; return ret;
} }

View File

@ -21,7 +21,7 @@
#pragma once #pragma once
#include "internal.h" #include "libvirt_internal.h"
#include "virthread.h" #include "virthread.h"
#include "virdnsmasq.h" #include "virdnsmasq.h"
#include "virnetworkobj.h" #include "virnetworkobj.h"
@ -49,6 +49,13 @@ typedef struct _virNetworkDriverState virNetworkDriverState;
struct _virNetworkDriverState { struct _virNetworkDriverState {
virMutex lock; virMutex lock;
/* Atomic inc/dec only */
unsigned int nactive;
/* Immutable pointers. Caller must provide locking */
virStateInhibitCallback inhibitCallback;
void *inhibitOpaque;
/* Read-only */ /* Read-only */
bool privileged; bool privileged;