From nobody Sun Feb 8 14:09:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1728399503896970.5051307767982; Tue, 8 Oct 2024 07:58:23 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 86D891560; Tue, 8 Oct 2024 10:58:22 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id CFACB14B3; Tue, 8 Oct 2024 10:58:02 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 8D0FE143B; Tue, 8 Oct 2024 10:57:59 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 153571470 for ; Tue, 8 Oct 2024 10:57:59 -0400 (EDT) Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-397-P8D44ILcPL2aYFEOdTtWbw-1; Tue, 08 Oct 2024 10:57:56 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3FA861955EAD for ; Tue, 8 Oct 2024 14:57:55 +0000 (UTC) Received: from vhost3.router.laine.org (unknown [10.22.32.80]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9EB3719560A2 for ; Tue, 8 Oct 2024 14:57:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728399478; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=aWJ5DM4l++ILbVDRUfPGtlGUQ3+2FiYl21u7bD0sD0E=; b=XXXtXo6brRb5VQj5WwkrAZK+pxbXlrS3RGokQ9FSmkugOyL9yIJvr8GfMsNKW6fThSoIL2 Aykeg63hpeFiuG1K3PjjVc3pJiMHkVRg51LhK6j6AmakmIwTGmukdgOUjEFOFyUg2yLS+h /4T2gyUInMRcKXMQLm/XuYmKRr7Nu1A= X-MC-Unique: P8D44ILcPL2aYFEOdTtWbw-1 From: Laine Stump To: devel@lists.libvirt.org Subject: [PATCH] network: inhibit idle timeout of daemon if there are any active networks Date: Tue, 8 Oct 2024 10:57:53 -0400 Message-ID: <20241008145753.2047342-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: TQEHEYI456P3UBYBFAX6KK7VVOA3EHRL X-Message-ID-Hash: TQEHEYI456P3UBYBFAX6KK7VVOA3EHRL X-MailFrom: laine@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1728399504617116600 Content-Type: text/plain; charset="utf-8"; x-default="true" 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 Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: Martin Kletzander --- I had made this patch as a part of a larger series that will require it, but haven't sent that yet and keep being annoyed when virtnetworkd exits out from under a gdb session, so I decided it has enough general usefulness to send by itself. src/network/bridge_driver.c | 20 +++++++++++++++++--- src/network/bridge_driver_conf.h | 9 ++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 74ba59b4e9..6a48516fdf 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -501,9 +501,13 @@ networkUpdateState(virNetworkObj *obj, return -1; } =20 - if (virNetworkObjIsActive(obj)) + if (virNetworkObjIsActive(obj)) { virNetworkObjPortForEach(obj, networkUpdatePort, obj); =20 + if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhi= bitCallback) + driver->inhibitCallback(true, driver->inhibitOpaque); + } + /* Try and read dnsmasq pids of both active and inactive networks, jus= t in * case a network became inactive and we need to clean up. */ if (def->ips && (def->nips > 0)) { @@ -617,8 +621,8 @@ static virDrvStateInitResult networkStateInitialize(bool privileged, const char *root, bool monolithic G_GNUC_UNUSED, - virStateInhibitCallback callback G_GNUC_UNUSED, - void *opaque G_GNUC_UNUSED) + virStateInhibitCallback callback, + void *opaque) { virNetworkDriverConfig *cfg; bool autostart =3D true; @@ -640,6 +644,9 @@ networkStateInitialize(bool privileged, goto error; } =20 + network_driver->inhibitCallback =3D callback; + network_driver->inhibitOpaque =3D opaque; + network_driver->privileged =3D privileged; =20 if (!(network_driver->xmlopt =3D networkDnsmasqCreateXMLConf())) @@ -2427,6 +2434,9 @@ networkStartNetwork(virNetworkDriverState *driver, obj, network_driver->xmlopt) < 0) goto cleanup; =20 + if (g_atomic_int_add(&driver->nactive, 1) =3D=3D 0 && driver->inhibitC= allback) + driver->inhibitCallback(true, driver->inhibitOpaque); + virNetworkObjSetActive(obj, true); VIR_INFO("Network '%s' started up", def->name); ret =3D 0; @@ -2500,6 +2510,10 @@ networkShutdownNetwork(virNetworkDriverState *driver, VIR_HOOK_SUBOP_END); =20 virNetworkObjSetActive(obj, false); + + if (!!g_atomic_int_dec_and_test(&driver->nactive) && driver->inhibitCa= llback) + driver->inhibitCallback(false, driver->inhibitOpaque); + virNetworkObjUnsetDefTransient(obj); return ret; } diff --git a/src/network/bridge_driver_conf.h b/src/network/bridge_driver_c= onf.h index 8f221f391e..1beed01efb 100644 --- a/src/network/bridge_driver_conf.h +++ b/src/network/bridge_driver_conf.h @@ -21,7 +21,7 @@ =20 #pragma once =20 -#include "internal.h" +#include "libvirt_internal.h" #include "virthread.h" #include "virdnsmasq.h" #include "virnetworkobj.h" @@ -49,6 +49,13 @@ typedef struct _virNetworkDriverState virNetworkDriverSt= ate; struct _virNetworkDriverState { virMutex lock; =20 + /* Atomic inc/dec only */ + unsigned int nactive; + + /* Immutable pointers. Caller must provide locking */ + virStateInhibitCallback inhibitCallback; + void *inhibitOpaque; + /* Read-only */ bool privileged; =20 --=20 2.46.1