From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411127; cv=none; d=zohomail.com; s=zohoarc; b=PmVP3d/dvR7RoyymSqov3BvsWu4L7nOA7TUbTH8ZDcynVfKsE8Uk+vY6Bz+DnufNZ8tgwn2tROx0NMq9fZipTggeS2hGKFNVy0hDFRSnUGCyu/Ifl6Fifvln4TtazJb3Nv9n0V5VIpusgQdflwzRHJ46x+KNUrgrVntx1LCyBxM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411127; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KVKDOWFdrwGJNR46YeE3iOgMzM7afZeL0i/xV90sWQg=; b=lZIFCTAiJx6hBR1doH0ZxYEoCz5L+w34Zdu9WvspJK64Lz6HRCVuJMGAmVDd01scdzI68xgpAZIlpj+as6G1rM6jwgc9oZK26DgyigjvYegRvZ17Dq62UlJfF0DzlgzVfBa6yLFMHaDedX+c+I5HeaSkuM8CT7YkaDaSOapN05k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174741112791820.987959318254525; Fri, 16 May 2025 08:58:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRN-0008Q1-PW; Fri, 16 May 2025 11:57:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRM-0008Pt-CP for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRK-0002cX-Um for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:40 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-224-25EWm5fxMXOSn-MkQ4KmWg-1; Fri, 16 May 2025 11:57:33 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B40861800366; Fri, 16 May 2025 15:57:31 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1CB6018003FC; Fri, 16 May 2025 15:57:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411056; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KVKDOWFdrwGJNR46YeE3iOgMzM7afZeL0i/xV90sWQg=; b=BaarPSiRYPcxfpGa4pSVEV7ZVDFub9SJpbTrLWMfoSk1ck5lKqSnmjVnxy3//rY6mDGYej mU6JoXlg+HamtsQXBQzfhr4Nm294R4/9x5OUF+wtGc0DRVqvkfQGETcQ3fpQ2yf8jFzhVR ix6joixF76aHZtIX8C3M+TpTaJYakZI= X-MC-Unique: 25EWm5fxMXOSn-MkQ4KmWg-1 X-Mimecast-MFC-AGG-ID: 25EWm5fxMXOSn-MkQ4KmWg_1747411052 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 1/7] io: Fix partial struct copy in qio_dns_resolver_lookup_sync_inet() Date: Fri, 16 May 2025 17:56:53 +0200 Message-ID: <20250516155710.2246148-2-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411130155116600 From: Juraj Marcin Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, but this flag is not copied together with other options in qio_dns_resolver_lookup_sync_inet(). This patch fixes this issue and also prevents future ones by copying the entire structure first and only then overriding a few attributes that need to be different. Fixes: aec21d31756c (qapi: Add InetSocketAddress member keep-alive) Signed-off-by: Juraj Marcin Reviewed-by: Daniel P. Berrang=C3=A9 --- io/dns-resolver.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/io/dns-resolver.c b/io/dns-resolver.c index 53b0e8407a..3712438f82 100644 --- a/io/dns-resolver.c +++ b/io/dns-resolver.c @@ -111,22 +111,11 @@ static int qio_dns_resolver_lookup_sync_inet(QIODNSRe= solver *resolver, uaddr, INET6_ADDRSTRLEN, uport, 32, NI_NUMERICHOST | NI_NUMERICSERV); =20 - newaddr->u.inet =3D (InetSocketAddress){ - .host =3D g_strdup(uaddr), - .port =3D g_strdup(uport), - .has_numeric =3D true, - .numeric =3D true, - .has_to =3D iaddr->has_to, - .to =3D iaddr->to, - .has_ipv4 =3D iaddr->has_ipv4, - .ipv4 =3D iaddr->ipv4, - .has_ipv6 =3D iaddr->has_ipv6, - .ipv6 =3D iaddr->ipv6, -#ifdef HAVE_IPPROTO_MPTCP - .has_mptcp =3D iaddr->has_mptcp, - .mptcp =3D iaddr->mptcp, -#endif - }; + newaddr->u.inet =3D *iaddr; + newaddr->u.inet.host =3D g_strdup(uaddr), + newaddr->u.inet.port =3D g_strdup(uport), + newaddr->u.inet.has_numeric =3D true, + newaddr->u.inet.numeric =3D true, =20 (*addrs)[i] =3D newaddr; } --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411121; cv=none; d=zohomail.com; s=zohoarc; b=H7a58uDbQxh/ydGYXj64SK6t+kf/aK/xsdl7qltclYqXkw5LNA51PrFNFthhUwuf2eKljVwxUJfWHieUrBocG2pO8BeUlbIltEXeedNUlo/PmQmE74w26zDGuFk3+/nXj2SgkPHe2Wz1k9BujaAjL6EzqAYd+nkScqtVPm4FowQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411121; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Zx/5finWrW3BE7soKnFMX10p0Eu9Wv9Ivwwng/VowTQ=; b=m9iVdogND5ZkmaLEIwGfEk6iAyGbWxvxaca64xr+oR+sqGbTn7kl+S24mAi379GdFQoU6mZPo88+ekJllQOeGMnZM+7MsGX9aIPkGLSYPhQtiAzCsyHQhxl8kqsNCDnPqIKC+r8Bzv+P/dlHthcbbllyB3SNuMxJ5Mwff0sIkZ4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411121626644.3904528015083; Fri, 16 May 2025 08:58:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRY-0008RH-CL; Fri, 16 May 2025 11:57:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRW-0008Qm-ON for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:50 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRV-0002cn-9M for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:50 -0400 Received: from mx-prod-mc-01.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-460-SCBtfdGeNWmL4vl8qqWkFw-1; Fri, 16 May 2025 11:57:45 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 07AC619560A5; Fri, 16 May 2025 15:57:40 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6722018003FC; Fri, 16 May 2025 15:57:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Zx/5finWrW3BE7soKnFMX10p0Eu9Wv9Ivwwng/VowTQ=; b=UY3VhwqbP1dcLODC9j2L2prciMs1kG9hp+gZWSj7jeCT+XFsh9hs1zNKaPWMI2iXfi3iNt HrgJiDhB5D2VrxaCEaiqP3T9eoJ//mp0PNnMiFYvLgsQb4mtBZiZnVicoAi8I2rvmu1iKn 44wBmNcTf/bwZvJWjFDiX1U3oJswCqQ= X-MC-Unique: SCBtfdGeNWmL4vl8qqWkFw-1 X-Mimecast-MFC-AGG-ID: SCBtfdGeNWmL4vl8qqWkFw_1747411064 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 2/7] util/qemu-sockets: Refactor setting client sockopts into a separate function Date: Fri, 16 May 2025 17:56:54 +0200 Message-ID: <20250516155710.2246148-3-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411123704116600 Content-Type: text/plain; charset="utf-8" From: Juraj Marcin This is done in preparation for enabling the SO_KEEPALIVE support for server sockets and adding settings for more TCP keep-alive socket options. Signed-off-by: Juraj Marcin Reviewed-by: Daniel P. Berrang=C3=A9 --- util/qemu-sockets.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 77477c1cd5..4a878e0527 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -205,6 +205,22 @@ static int try_bind(int socket, InetSocketAddress *sad= dr, struct addrinfo *e) #endif } =20 +static int inet_set_sockopts(int sock, InetSocketAddress *saddr, Error **e= rrp) +{ + if (saddr->keep_alive) { + int keep_alive =3D 1; + int ret =3D setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, + &keep_alive, sizeof(keep_alive)); + + if (ret < 0) { + error_setg_errno(errp, errno, + "Unable to set keep-alive option on socket"); + return -1; + } + } + return 0; +} + static int inet_listen_saddr(InetSocketAddress *saddr, int port_offset, int num, @@ -475,16 +491,9 @@ int inet_connect_saddr(InetSocketAddress *saddr, Error= **errp) return sock; } =20 - if (saddr->keep_alive) { - int val =3D 1; - int ret =3D setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, - &val, sizeof(val)); - - if (ret < 0) { - error_setg_errno(errp, errno, "Unable to set KEEPALIVE"); - close(sock); - return -1; - } + if (inet_set_sockopts(sock, saddr, errp) < 0) { + close(sock); + return -1; } =20 return sock; --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411132; cv=none; d=zohomail.com; s=zohoarc; b=esdspkAZ+IgrcPo1zEovWfFkYCD8FSQgaWrGwrEdPRD6ekSbiz6P4SVmB6m0Zl8gXy0V1p9ag6ghWbJNBt6x86UQezsXy7fHWFjwpeeNbCKvYqN6NXCFx6548HPAQ0b9knUyRLS2HSEPRG8vvznpPFuUHHjx/Bfy0c96MKta2Ic= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411132; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=py+TZoPFweDXa675c5l/OIWhVyVWlz3r43R1tVyvdH0=; b=c4mK7hr6GvtFWn6o6dJAoqjVrEPBHmy3QxUKMHVB/ZzA1khYM7MWRh/kq3vyROMvX+hQLbt+Lfy7n6SsfifBAeDZha11gZsSjWbt5dvAHQlSiRmjV0w1vn2wL8hNRDnuZSpvjfXmQ75KG/angr83VV2J6LJrJJNFAuF9QmbjnDY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411132127788.1129267378841; Fri, 16 May 2025 08:58:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRZ-0008RJ-3y; Fri, 16 May 2025 11:57:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRX-0008Qu-EE for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:51 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRV-0002cr-P6 for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:51 -0400 Received: from mx-prod-mc-01.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-318-YOvTP7OTM0SaAr4U16CjxA-1; Fri, 16 May 2025 11:57:47 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 48CD01956096; Fri, 16 May 2025 15:57:46 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AB6CE180045B; Fri, 16 May 2025 15:57:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411069; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=py+TZoPFweDXa675c5l/OIWhVyVWlz3r43R1tVyvdH0=; b=AVajkAQ8l/1MlILyTXIvxZRkw1D3jmtiOOVOr5Md9MTQDg+1YnZ80XldZhZyaCCb1swlHl Hfz6ASE9rwqTki6DlQriXs0srwoj/La+W8NwnynyaJrUIgX+m6dCFZTqAmqgEvpZI8YNpB D7SVugFEXjAYYqQcO7dgOVw9gv4//SM= X-MC-Unique: YOvTP7OTM0SaAr4U16CjxA-1 X-Mimecast-MFC-AGG-ID: YOvTP7OTM0SaAr4U16CjxA_1747411066 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 3/7] util/qemu-sockets: Refactor success and failure paths in inet_listen_saddr() Date: Fri, 16 May 2025 17:56:55 +0200 Message-ID: <20250516155710.2246148-4-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411133810116600 From: Juraj Marcin To get a listening socket, we need to first create a socket, try binding it to a certain port, and lastly starting listening to it. Each of these operations can fail due to various reasons, one of them being that the requested address/port is already in use. In such case, the function tries the same process with a new port number. This patch refactors the port number loop, so the success path is no longer buried inside the 'if' statements in the middle of the loop. Now, the success path is not nested and ends at the end of the iteration after successful socket creation, binding, and listening. In case any of the operations fails, it either continues to the next iteration (and the next port) or jumps out of the loop to handle the error and exits the function. Signed-off-by: Juraj Marcin Reviewed-by: Daniel P. Berrang=C3=A9 --- util/qemu-sockets.c | 51 ++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 4a878e0527..329fdbfd97 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -303,11 +303,20 @@ static int inet_listen_saddr(InetSocketAddress *saddr, port_min =3D inet_getport(e); port_max =3D saddr->has_to ? saddr->to + port_offset : port_min; for (p =3D port_min; p <=3D port_max; p++) { + if (slisten >=3D 0) { + /* + * We have a socket we tried with the previous port. It ca= nnot + * be rebound, we need to close it and create a new one. + */ + close(slisten); + slisten =3D -1; + } inet_setport(e, p); =20 slisten =3D create_fast_reuse_socket(e); if (slisten < 0) { - /* First time we expect we might fail to create the socket + /* + * First time we expect we might fail to create the socket * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded. * Later iterations should always succeed if first iterati= on * worked though, so treat that as fatal. @@ -317,40 +326,38 @@ static int inet_listen_saddr(InetSocketAddress *saddr, } else { error_setg_errno(errp, errno, "Failed to recreate failed listening = socket"); - goto listen_failed; + goto fail; } } socket_created =3D true; =20 rc =3D try_bind(slisten, saddr, e); if (rc < 0) { - if (errno !=3D EADDRINUSE) { - error_setg_errno(errp, errno, "Failed to bind socket"); - goto listen_failed; + if (errno =3D=3D EADDRINUSE) { + /* This port is already used, try the next one */ + continue; } - } else { - if (!listen(slisten, num)) { - goto listen_ok; - } - if (errno !=3D EADDRINUSE) { - error_setg_errno(errp, errno, "Failed to listen on soc= ket"); - goto listen_failed; + error_setg_errno(errp, errno, "Failed to bind socket"); + goto fail; + } + if (listen(slisten, num)) { + if (errno =3D=3D EADDRINUSE) { + /* This port is already used, try the next one */ + continue; } + error_setg_errno(errp, errno, "Failed to listen on socket"= ); + goto fail; } - /* Someone else managed to bind to the same port and beat us - * to listen on it! Socket semantics does not allow us to - * recover from this situation, so we need to recreate the - * socket to allow bind attempts for subsequent ports: - */ - close(slisten); - slisten =3D -1; + /* We have a listening socket */ + freeaddrinfo(res); + return slisten; } } error_setg_errno(errp, errno, socket_created ? "Failed to find an available port" : "Failed to create a socket"); -listen_failed: +fail: saved_errno =3D errno; if (slisten >=3D 0) { close(slisten); @@ -358,10 +365,6 @@ listen_failed: freeaddrinfo(res); errno =3D saved_errno; return -1; - -listen_ok: - freeaddrinfo(res); - return slisten; } =20 #ifdef _WIN32 --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411123; cv=none; d=zohomail.com; s=zohoarc; b=Ro6y4C1dLd/XolRQGIM/XNLDMxfmakOYMyfWh0QTp9GLY5xsZOSzGaf0aiVl7u4gHHG3E229wFHby3g3EFAN91cMg5wNKY6dOEQMl1WpigOEJN1FhD5ACecrqFeTqMc7V60ECsa+DIxlZYGeZZhBJK013aNxiUCLESWCS5EkmRA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411123; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=B3+EyUCTIK9FqId1wl+BA9m2aXuQYDWfJ5AScKo9HVI=; b=I7CEJuubHzeCr0B3WtW1LxtQrc0BFOX7Vf/rE17zScRd4TFPELAM/ppclMC3kAO0u9HyNP1xRvWtCPUjufngSBKbIE5bOJP/m+WnSToA70DG86wnBHlUxDVvyVVsWaAP0fJDBumfNtHtVKibDrgarz+Vg0J2ZH+sZuWIK46q+Qw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411123805379.3509826270322; Fri, 16 May 2025 08:58:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRh-0008SI-Tf; Fri, 16 May 2025 11:58:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRf-0008S7-Nn for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRe-0002do-4n for qemu-devel@nongnu.org; Fri, 16 May 2025 11:57:59 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-85-11W-h8PGOtObeu180J_Dow-1; Fri, 16 May 2025 11:57:53 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id ED7B51800ECB; Fri, 16 May 2025 15:57:52 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 680BC180045B; Fri, 16 May 2025 15:57:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B3+EyUCTIK9FqId1wl+BA9m2aXuQYDWfJ5AScKo9HVI=; b=h9toQRrBI+rK+9ss5/xt8j4vTAs8w4dfg/MhPW53WBefPjNhwM0ryComtf5bGWVYhElZWo nrjeTspN/VzKYDzQ2dONxBRNbJgtOu984fBpfk1HUIooqSY9TzVeUNk/06lUlKVOOUPS6j 5vfG1PCBvfVw4bcgdjkHoG5sb+jJSVo= X-MC-Unique: 11W-h8PGOtObeu180J_Dow-1 X-Mimecast-MFC-AGG-ID: 11W-h8PGOtObeu180J_Dow_1747411073 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 4/7] util/qemu-sockets: Add support for keep-alive flag to passive sockets Date: Fri, 16 May 2025 17:56:56 +0200 Message-ID: <20250516155710.2246148-5-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411125978116600 From: Juraj Marcin Commit aec21d3175 (qapi: Add InetSocketAddress member keep-alive) introduces the keep-alive flag, which enables the SO_KEEPALIVE socket option, but only on client-side sockets. However, this option is also useful for server-side sockets, so they can check if a client is still reachable or drop the connection otherwise. This patch enables the SO_KEEPALIVE socket option on passive server-side sockets if the keep-alive flag is enabled. This socket option is then inherited by active server-side sockets communicating with connected clients. Signed-off-by: Juraj Marcin Reviewed-by: Daniel P. Berrang=C3=A9 --- qapi/sockets.json | 4 ++-- util/qemu-sockets.c | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/qapi/sockets.json b/qapi/sockets.json index 6a95023315..62797cd027 100644 --- a/qapi/sockets.json +++ b/qapi/sockets.json @@ -56,8 +56,8 @@ # @ipv6: whether to accept IPv6 addresses, default try both IPv4 and # IPv6 # -# @keep-alive: enable keep-alive when connecting to this socket. Not -# supported for passive sockets. (Since 4.2) +# @keep-alive: enable keep-alive when connecting to/listening on this sock= et. +# (Since 4.2, not supported for listening sockets until 10.1) # # @mptcp: enable multi-path TCP. (Since 6.1) # diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 329fdbfd97..4fbf1ed5bf 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -236,12 +236,6 @@ static int inet_listen_saddr(InetSocketAddress *saddr, int saved_errno =3D 0; bool socket_created =3D false; =20 - if (saddr->keep_alive) { - error_setg(errp, "keep-alive option is not supported for passive " - "sockets"); - return -1; - } - memset(&ai,0, sizeof(ai)); ai.ai_flags =3D AI_PASSIVE; if (saddr->has_numeric && saddr->numeric) { @@ -349,6 +343,9 @@ static int inet_listen_saddr(InetSocketAddress *saddr, goto fail; } /* We have a listening socket */ + if (inet_set_sockopts(slisten, saddr, errp) < 0) { + goto fail; + } freeaddrinfo(res); return slisten; } --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411119; cv=none; d=zohomail.com; s=zohoarc; b=cn+WgAlubNzwtclnqkPMIdGfe6fJXoap/bU2GhqtqBNassHvklml5cBWzG3jQxx8N/Szuy2N6M4Ko2YfhKeiXV/+2RBwnKvRl4s0p3CHoiaIQF9aE3LDfzffHbX1SrHyZYg6vYuzBTF7Mx2S5qkgXRSQXSo4DHlbbfm4GX0es+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411119; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=x6WuEoBBxDetQlDtLp0lnM1Dtbhxi8WuhpnDOYhag6M=; b=LZLGgIgcH4VSzj6yfRJcuWXxHjNUfBSEzdSnYr+Epfk1nOnNyj22TJVM4Iho+fdRPstrZrnpohviR19cDF+baYg8E0kjE6IYe+W/Nv8HAW3B6DX587r3u4EmXA6zPsHVTHdsmj0ObBnIgk6+Ccp17bDxmQX5vW/A41zHiz8sJDo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411119667849.5623928251108; Fri, 16 May 2025 08:58:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRu-0008TQ-I7; Fri, 16 May 2025 11:58:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRt-0008T5-8u for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:13 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRr-0002eN-Lv for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:13 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-654-2n8xUhnuOqeoFGeSlqQqrw-1; Fri, 16 May 2025 11:58:00 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B6FFC18004AD; Fri, 16 May 2025 15:57:59 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F1FB818003FC; Fri, 16 May 2025 15:57:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411091; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x6WuEoBBxDetQlDtLp0lnM1Dtbhxi8WuhpnDOYhag6M=; b=hziFCar5lWjSdjE9poW1zjVii4/3+8jHIkkgqD0rV+PoVziOAKL2iMG/0vyPa+/bmTwWKO rwyM40hrBB11YIWadlEiBll7BxeVANwq5Y2dVOZ3DviLWJPd6HIirrroVSAV07VbSJabyI /vrhcB54akVTfFF4aynP66jP7vY0iNE= X-MC-Unique: 2n8xUhnuOqeoFGeSlqQqrw-1 X-Mimecast-MFC-AGG-ID: 2n8xUhnuOqeoFGeSlqQqrw_1747411079 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 5/7] tests/unit/test-util-sockets: Add tests for inet_parse() Date: Fri, 16 May 2025 17:56:57 +0200 Message-ID: <20250516155710.2246148-6-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411122181116600 Content-Type: text/plain; charset="utf-8" From: Juraj Marcin Before refactoring the inet_parse() function, this patch adds tests verifying the inet address parsing works correctly. Signed-off-by: Juraj Marcin --- tests/unit/test-util-sockets.c | 161 +++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/tests/unit/test-util-sockets.c b/tests/unit/test-util-sockets.c index 4c9dd0b271..60f15a4ddf 100644 --- a/tests/unit/test-util-sockets.c +++ b/tests/unit/test-util-sockets.c @@ -332,6 +332,144 @@ static void test_socket_unix_abstract(void) =20 #endif /* CONFIG_LINUX */ =20 +static void inet_parse_test_helper(const char *str, InetSocketAddress *exp= _addr, bool success) +{ + InetSocketAddress addr; + Error *error =3D NULL; + + int rc =3D inet_parse(&addr, str, &error); + + if (success) { + g_assert_cmpint(rc, =3D=3D, 0); + } else { + g_assert_cmpint(rc, <, 0); + } + if (exp_addr !=3D NULL) { + g_assert_cmpstr(addr.host, =3D=3D, exp_addr->host); + g_assert_cmpstr(addr.port, =3D=3D, exp_addr->port); + /* Own members: */ + g_assert_cmpint(addr.has_numeric, =3D=3D, exp_addr->has_numeric); + g_assert_cmpint(addr.numeric, =3D=3D, exp_addr->numeric); + g_assert_cmpint(addr.has_to, =3D=3D, exp_addr->has_to); + g_assert_cmpint(addr.to, =3D=3D, exp_addr->to); + g_assert_cmpint(addr.has_ipv4, =3D=3D, exp_addr->has_ipv4); + g_assert_cmpint(addr.ipv4, =3D=3D, exp_addr->ipv4); + g_assert_cmpint(addr.has_ipv6, =3D=3D, exp_addr->has_ipv6); + g_assert_cmpint(addr.ipv6, =3D=3D, exp_addr->ipv6); + g_assert_cmpint(addr.has_keep_alive, =3D=3D, exp_addr->has_keep_al= ive); + g_assert_cmpint(addr.keep_alive, =3D=3D, exp_addr->keep_alive); +#ifdef HAVE_IPPROTO_MPTCP + g_assert_cmpint(addr.has_mptcp, =3D=3D, exp_addr->has_mptcp); + g_assert_cmpint(addr.mptcp, =3D=3D, exp_addr->mptcp); +#endif + } + + g_free(addr.host); + g_free(addr.port); +} + +static void test_inet_parse_nohost_good(void) +{ + char host[] =3D ""; + char port[] =3D "5000"; + InetSocketAddress exp_addr =3D { + .host =3D host, + .port =3D port, + }; + inet_parse_test_helper(":5000", &exp_addr, true); +} + +static void test_inet_parse_empty_bad(void) +{ + inet_parse_test_helper("", NULL, false); +} + +static void test_inet_parse_only_colon_bad(void) +{ + inet_parse_test_helper(":", NULL, false); +} + +static void test_inet_parse_ipv4_good(void) +{ + char host[] =3D "127.0.0.1"; + char port[] =3D "5000"; + InetSocketAddress exp_addr =3D { + .host =3D host, + .port =3D port, + }; + inet_parse_test_helper("127.0.0.1:5000", &exp_addr, true); +} + +static void test_inet_parse_ipv4_noport_bad(void) +{ + inet_parse_test_helper("127.0.0.1", NULL, false); +} + +static void test_inet_parse_ipv6_good(void) +{ + char host[] =3D "::1"; + char port[] =3D "5000"; + InetSocketAddress exp_addr =3D { + .host =3D host, + .port =3D port, + }; + inet_parse_test_helper("[::1]:5000", &exp_addr, true); +} + +static void test_inet_parse_ipv6_noend_bad(void) +{ + inet_parse_test_helper("[::1", NULL, false); +} + +static void test_inet_parse_ipv6_noport_bad(void) +{ + inet_parse_test_helper("[::1]:", NULL, false); +} + +static void test_inet_parse_ipv6_empty_bad(void) +{ + inet_parse_test_helper("[]:5000", NULL, false); +} + +static void test_inet_parse_hostname_good(void) +{ + char host[] =3D "localhost"; + char port[] =3D "5000"; + InetSocketAddress exp_addr =3D { + .host =3D host, + .port =3D port, + }; + inet_parse_test_helper("localhost:5000", &exp_addr, true); +} + +static void test_inet_parse_all_options_good(void) +{ + char host[] =3D "::1"; + char port[] =3D "5000"; + InetSocketAddress exp_addr =3D { + .host =3D host, + .port =3D port, + .has_to =3D true, + .to =3D 5006, + .has_ipv4 =3D true, + .ipv4 =3D false, + .has_ipv6 =3D true, + .ipv6 =3D true, + .has_keep_alive =3D true, + .keep_alive =3D true, +#ifdef HAVE_IPPROTO_MPTCP + .has_mptcp =3D true, + .mptcp =3D true, +#endif + }; + inet_parse_test_helper( + "[::1]:5000,to=3D5006,ipv4=3Doff,ipv6=3Don,keep-alive" +#ifdef HAVE_IPPROTO_MPTCP + ",mptcp" +#endif + , &exp_addr, true); +} + int main(int argc, char **argv) { bool has_ipv4, has_ipv6; @@ -377,6 +515,29 @@ int main(int argc, char **argv) test_socket_unix_abstract); #endif =20 + g_test_add_func("/util/socket/inet-parse/nohost-good", + test_inet_parse_nohost_good); + g_test_add_func("/util/socket/inet-parse/empty-bad", + test_inet_parse_empty_bad); + g_test_add_func("/util/socket/inet-parse/only-colon-bad", + test_inet_parse_only_colon_bad); + g_test_add_func("/util/socket/inet-parse/ipv4-good", + test_inet_parse_ipv4_good); + g_test_add_func("/util/socket/inet-parse/ipv4-noport-bad", + test_inet_parse_ipv4_noport_bad); + g_test_add_func("/util/socket/inet-parse/ipv6-good", + test_inet_parse_ipv6_good); + g_test_add_func("/util/socket/inet-parse/ipv6-noend-bad", + test_inet_parse_ipv6_noend_bad); + g_test_add_func("/util/socket/inet-parse/ipv6-noport-bad", + test_inet_parse_ipv6_noport_bad); + g_test_add_func("/util/socket/inet-parse/ipv6-empty-bad", + test_inet_parse_ipv6_empty_bad); + g_test_add_func("/util/socket/inet-parse/hostname-good", + test_inet_parse_hostname_good); + g_test_add_func("/util/socket/inet-parse/all-options-good", + test_inet_parse_all_options_good); + end: return g_test_run(); } --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411184; cv=none; d=zohomail.com; s=zohoarc; b=Zyo4bjfbyraNOlyoirXZK0XavTQUf5k04PDcuOBmMA+zd9+/RCrR3OeXp5BdIE6LRoK+J4UpLABSlFpcUTQab/ohEJ6nOOxwSed5siaHqt/k2cpv6Ij7iNh9dZQZTh+3O0pLvfRbuEemk+RuvOtQHDSySMlszRFbEeBZR7MfTg4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411184; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=63fxX36W1DeMEqkQnncf19Px4ERiPQlpA84N7Wfc39A=; b=iE1UG4nQ747mNr4ZHZ6pZ9QslxUHFdn/l66C+eoV4gp7nJsj2o50x2DocciP0mVNqhNCE9dko5w7iJff4YVo6J0rvzbYGAsav04i1mOAJEu+sIzqBWfOQNtTTO5B4/FuE/L3V0TEzlRveS5GE+epWNlYu6hogB4B3x000GYBQeU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411184758231.1193485268201; Fri, 16 May 2025 08:59:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxRt-0008TK-EJ; Fri, 16 May 2025 11:58:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRr-0008Sr-Tl for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRq-0002eJ-21 for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:11 -0400 Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-460-zGmYJnFHP8ygtXyzssrZtw-1; Fri, 16 May 2025 11:58:08 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 367B71800EC8; Fri, 16 May 2025 15:58:07 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9A68218003FC; Fri, 16 May 2025 15:58:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=63fxX36W1DeMEqkQnncf19Px4ERiPQlpA84N7Wfc39A=; b=FGG+saCEhSc3GUnSk5q4PeA9FXTN0UIEXwsDQxL/CBYa+P7/X9NzeED7ymljRcsAhfKFbm SqeTLZa5oz6N1SLJOKNtq72+vCebq9Gsg8UqdYpbWDkk/teaM/ZGGBlTRhI7ptZPxytFgD igftn67rkYWbY+I0KSz77Ug8HE7kwBA= X-MC-Unique: zGmYJnFHP8ygtXyzssrZtw-1 X-Mimecast-MFC-AGG-ID: zGmYJnFHP8ygtXyzssrZtw_1747411087 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 6/7] util/qemu-sockets: Refactor inet_parse() to use QemuOpts Date: Fri, 16 May 2025 17:56:58 +0200 Message-ID: <20250516155710.2246148-7-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411186780116600 Content-Type: text/plain; charset="utf-8" From: Juraj Marcin Currently, the inet address parser cannot handle multiple options where one is prefixed with the name of the other. For example, with the 'keep-alive-idle' option added, the current parser cannot parse '127.0.0.1:5000,keep-alive-idle=3D60,keep-alive' correctly. Instead, it fails with "error parsing 'keep-alive' flag '-idle=3D60,keep-alive'". To resolve these issues, this patch rewrites the inet address parsing using the QemuOpts parser, which the inet_parse_flag() function tries to mimic. This new parser supports all previously supported options and on top of that the 'numeric' flag is now also supported. The only difference is, the new parser produces an error if an unknown option is passed, instead of silently ignoring it. Signed-off-by: Juraj Marcin --- tests/unit/test-util-sockets.c | 4 +- util/qemu-sockets.c | 158 +++++++++++++++------------------ 2 files changed, 77 insertions(+), 85 deletions(-) diff --git a/tests/unit/test-util-sockets.c b/tests/unit/test-util-sockets.c index 60f15a4ddf..77d0767b9b 100644 --- a/tests/unit/test-util-sockets.c +++ b/tests/unit/test-util-sockets.c @@ -449,6 +449,8 @@ static void test_inet_parse_all_options_good(void) InetSocketAddress exp_addr =3D { .host =3D host, .port =3D port, + .has_numeric =3D true, + .numeric =3D true, .has_to =3D true, .to =3D 5006, .has_ipv4 =3D true, @@ -463,7 +465,7 @@ static void test_inet_parse_all_options_good(void) #endif }; inet_parse_test_helper( - "[::1]:5000,to=3D5006,ipv4=3Doff,ipv6=3Don,keep-alive" + "[::1]:5000,numeric,to=3D5006,ipv4=3Doff,ipv6=3Don,keep-alive" #ifdef HAVE_IPPROTO_MPTCP ",mptcp" #endif diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 4fbf1ed5bf..403dc26b36 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -30,6 +30,7 @@ #include "qapi/qobject-input-visitor.h" #include "qapi/qobject-output-visitor.h" #include "qemu/cutils.h" +#include "qemu/option.h" #include "trace.h" =20 #ifndef AI_ADDRCONFIG @@ -600,115 +601,104 @@ err: return -1; } =20 -/* compatibility wrapper */ -static int inet_parse_flag(const char *flagname, const char *optstr, bool = *val, - Error **errp) -{ - char *end; - size_t len; - - end =3D strstr(optstr, ","); - if (end) { - if (end[1] =3D=3D ',') { /* Reject 'ipv6=3Don,,foo' */ - error_setg(errp, "error parsing '%s' flag '%s'", flagname, opt= str); - return -1; - } - len =3D end - optstr; - } else { - len =3D strlen(optstr); - } - if (len =3D=3D 0 || (len =3D=3D 3 && strncmp(optstr, "=3Don", len) =3D= =3D 0)) { - *val =3D true; - } else if (len =3D=3D 4 && strncmp(optstr, "=3Doff", len) =3D=3D 0) { - *val =3D false; - } else { - error_setg(errp, "error parsing '%s' flag '%s'", flagname, optstr); - return -1; - } - return 0; -} +static QemuOptsList inet_opts =3D { + .name =3D "InetSocketAddress", + .head =3D QTAILQ_HEAD_INITIALIZER(inet_opts.head), + .implied_opt_name =3D "addr", + .desc =3D { + { + .name =3D "addr", + .type =3D QEMU_OPT_STRING, + }, + { + .name =3D "numeric", + .type =3D QEMU_OPT_BOOL, + }, + { + .name =3D "to", + .type =3D QEMU_OPT_NUMBER, + }, + { + .name =3D "ipv4", + .type =3D QEMU_OPT_BOOL, + }, + { + .name =3D "ipv6", + .type =3D QEMU_OPT_BOOL, + }, + { + .name =3D "keep-alive", + .type =3D QEMU_OPT_BOOL, + }, +#ifdef HAVE_IPPROTO_MPTCP + { + .name =3D "mptcp", + .type =3D QEMU_OPT_BOOL, + }, +#endif + { /* end of list */ } + }, +}; =20 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) { - const char *optstr, *h; - char host[65]; - char port[33]; - int to; - int pos; - char *begin; - + QemuOpts *opts =3D qemu_opts_parse(&inet_opts, str, true, errp); + if (!opts) { + return -1; + } memset(addr, 0, sizeof(*addr)); =20 /* parse address */ - if (str[0] =3D=3D ':') { - /* no host given */ - host[0] =3D '\0'; - if (sscanf(str, ":%32[^,]%n", port, &pos) !=3D 1) { - error_setg(errp, "error parsing port in address '%s'", str); - return -1; - } - } else if (str[0] =3D=3D '[') { + const char *addr_str =3D qemu_opt_get(opts, "addr"); + if (!addr_str) { + error_setg(errp, "error parsing address ''"); + return -1; + } + if (str[0] =3D=3D '[') { /* IPv6 addr */ - if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) !=3D 2) { - error_setg(errp, "error parsing IPv6 address '%s'", str); + const char *ip_end =3D strstr(addr_str, "]:"); + if (!ip_end || ip_end - addr_str < 2 || strlen(ip_end) < 3) { + error_setg(errp, "error parsing IPv6 address '%s'", addr_str); return -1; } + addr->host =3D g_strndup(addr_str + 1, ip_end - addr_str - 1); + addr->port =3D g_strdup(ip_end + 2); } else { - /* hostname or IPv4 addr */ - if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) !=3D 2) { - error_setg(errp, "error parsing address '%s'", str); + /* no host, hostname or IPv4 addr */ + const char *port =3D strchr(addr_str, ':'); + if (!port || strlen(port) < 2) { + error_setg(errp, "error parsing address '%s'", addr_str); return -1; } + addr->host =3D g_strndup(addr_str, port - addr_str); + addr->port =3D g_strdup(port + 1); } =20 - addr->host =3D g_strdup(host); - addr->port =3D g_strdup(port); - /* parse options */ - optstr =3D str + pos; - h =3D strstr(optstr, ",to=3D"); - if (h) { - h +=3D 4; - if (sscanf(h, "%d%n", &to, &pos) !=3D 1 || - (h[pos] !=3D '\0' && h[pos] !=3D ',')) { - error_setg(errp, "error parsing to=3D argument"); - return -1; - } + if (qemu_opt_find(opts, "numeric")) { + addr->has_numeric =3D true, + addr->numeric =3D qemu_opt_get_bool(opts, "numeric", false); + } + if (qemu_opt_find(opts, "to")) { addr->has_to =3D true; - addr->to =3D to; + addr->to =3D qemu_opt_get_number(opts, "to", 0); } - begin =3D strstr(optstr, ",ipv4"); - if (begin) { - if (inet_parse_flag("ipv4", begin + 5, &addr->ipv4, errp) < 0) { - return -1; - } + if (qemu_opt_find(opts, "ipv4")) { addr->has_ipv4 =3D true; + addr->ipv4 =3D qemu_opt_get_bool(opts, "ipv4", false); } - begin =3D strstr(optstr, ",ipv6"); - if (begin) { - if (inet_parse_flag("ipv6", begin + 5, &addr->ipv6, errp) < 0) { - return -1; - } + if (qemu_opt_find(opts, "ipv6")) { addr->has_ipv6 =3D true; + addr->ipv6 =3D qemu_opt_get_bool(opts, "ipv6", false); } - begin =3D strstr(optstr, ",keep-alive"); - if (begin) { - if (inet_parse_flag("keep-alive", begin + strlen(",keep-alive"), - &addr->keep_alive, errp) < 0) - { - return -1; - } + if (qemu_opt_find(opts, "keep-alive")) { addr->has_keep_alive =3D true; + addr->keep_alive =3D qemu_opt_get_bool(opts, "keep-alive", false); } #ifdef HAVE_IPPROTO_MPTCP - begin =3D strstr(optstr, ",mptcp"); - if (begin) { - if (inet_parse_flag("mptcp", begin + strlen(",mptcp"), - &addr->mptcp, errp) < 0) - { - return -1; - } + if (qemu_opt_find(opts, "mptcp")) { addr->has_mptcp =3D true; + addr->mptcp =3D qemu_opt_get_bool(opts, "mptcp", 0); } #endif return 0; --=20 2.49.0 From nobody Thu Dec 18 00:47:01 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1747411166; cv=none; d=zohomail.com; s=zohoarc; b=gKuJczlTYNyGEi61hWWMNNyvmEKjGAxlEKxzEbYit0Y3qvFvsmm0N0neYhPaOQAscZkcp/Gbjn3DaK32Rtash1ej4/DiNhTq51HQVeq6SEIJZAOPhRXM9hMw7bfR7FumZ9GFoqaCrjoAeZqerr0eQmh8qUAbOq4mD5GeUk+lDpE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1747411166; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=aFMICsjYm62/z6PLh4qeoP8TmO0u9dpjXLy2gFguuw8=; b=YUzJdkMsP3GuERa4+f7XRtnzbTlsFjMZBUZ/NfHu0/JEJs0whY22YaRbMpjVf1fOblG/gh/MQ8QMTwQRqNPT+O67w7JZ66Eqhygq1V+eWTMC6KTu7tzMoeLNy1Y6zCxHRSeqxGArwR+wvrlLOGuukcPMqQ6eDizyKQeXZq5v6TY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1747411166755430.6574668144998; Fri, 16 May 2025 08:59:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uFxS0-0000Tf-Ik; Fri, 16 May 2025 11:58:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRy-0000KW-Pe for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uFxRw-0002ed-EE for qemu-devel@nongnu.org; Fri, 16 May 2025 11:58:18 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-81-921KARWCNXmJibd9IuEkuA-1; Fri, 16 May 2025 11:58:13 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 199A11800772; Fri, 16 May 2025 15:58:13 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.2.64]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D02518003FC; Fri, 16 May 2025 15:58:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747411095; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aFMICsjYm62/z6PLh4qeoP8TmO0u9dpjXLy2gFguuw8=; b=XQibT9B9qOypPGkh6pmDdDfElqwa1+EX5NLYdDXcQuP0fP9V0pcj+eo/58dC4Ue0OOURD5 B0sqsOQcWr6yWiAhOXjpfZK3Fgq628n16qmkAIr++YKwV4GyoYqKBU/hCgqlpaOs0TLFml /piGmVSLr1FQLTIl0PQ8YG857o0on+0= X-MC-Unique: 921KARWCNXmJibd9IuEkuA-1 X-Mimecast-MFC-AGG-ID: 921KARWCNXmJibd9IuEkuA_1747411093 From: Juraj Marcin To: qemu-devel@nongnu.org Cc: Juraj Marcin , vsementsov@yandex-team.ru, Paolo Bonzini , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v4 7/7] util/qemu-sockets: Introduce inet socket options controlling TCP keep-alive Date: Fri, 16 May 2025 17:56:59 +0200 Message-ID: <20250516155710.2246148-8-jmarcin@redhat.com> In-Reply-To: <20250516155710.2246148-1-jmarcin@redhat.com> References: <20250516155710.2246148-1-jmarcin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=jmarcin@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.686, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1747411168576116600 Content-Type: text/plain; charset="utf-8" From: Juraj Marcin With the default TCP stack configuration, it could be even 2 hours before the connection times out due to the other side not being reachable. However, in some cases, the application needs to be aware of a connection issue much sooner. This is the case, for example, for postcopy live migration. If there is no traffic from the migration destination guest (server-side) to the migration source guest (client-side), the destination keeps waiting for pages indefinitely and does not switch to the postcopy-paused state. This can happen, for example, if the destination QEMU instance is started with the '-S' command line option and the machine is not started yet, or if the machine is idle and produces no new page faults for not-yet-migrated pages. This patch introduces new inet socket parameters that control count, idle period, and interval of TCP keep-alive packets before the connection is considered broken. These parameters are available on systems where the respective TCP socket options are defined, that includes Linux, Windows, macOS, but not OpenBSD. Additionally, macOS defines TCP_KEEPIDLE as TCP_KEEPALIVE instead, so the patch supplies its own definition. The default value for all is 0, which means the system configuration is used. Signed-off-by: Juraj Marcin Reviewed-by: Daniel P. Berrang=C3=A9 --- meson.build | 30 ++++++++++++ qapi/sockets.json | 19 ++++++++ tests/unit/test-util-sockets.c | 33 ++++++++++++++ util/qemu-sockets.c | 83 ++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) diff --git a/meson.build b/meson.build index ad2053f968..fdad3fb528 100644 --- a/meson.build +++ b/meson.build @@ -2760,6 +2760,36 @@ if linux_io_uring.found() config_host_data.set('HAVE_IO_URING_PREP_WRITEV2', cc.has_header_symbol('liburing.h', 'io_uring_prep_w= ritev2')) endif +config_host_data.set('HAVE_TCP_KEEPCNT', + cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPCNT') = or + cc.compiles(''' + #include + #ifndef TCP_KEEPCNT + #error + #endif + int main(void) { return 0; }''', + name: 'Win32 TCP_KEEPCNT')) +# On Darwin TCP_KEEPIDLE is available under different name, TCP_KEEPALIVE. +# https://github.com/apple/darwin-xnu/blob/xnu-4570.1.46/bsd/man/man4/tcp.= 4#L172 +config_host_data.set('HAVE_TCP_KEEPIDLE', + cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPIDLE')= or + cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPALIVE'= ) or + cc.compiles(''' + #include + #ifndef TCP_KEEPIDLE + #error + #endif + int main(void) { return 0; }''', + name: 'Win32 TCP_KEEPIDLE')) +config_host_data.set('HAVE_TCP_KEEPINTVL', + cc.has_header_symbol('netinet/tcp.h', 'TCP_KEEPINTVL'= ) or + cc.compiles(''' + #include + #ifndef TCP_KEEPINTVL + #error + #endif + int main(void) { return 0; }''', + name: 'Win32 TCP_KEEPINTVL')) =20 # has_member config_host_data.set('HAVE_SIGEV_NOTIFY_THREAD_ID', diff --git a/qapi/sockets.json b/qapi/sockets.json index 62797cd027..f9f559daba 100644 --- a/qapi/sockets.json +++ b/qapi/sockets.json @@ -59,6 +59,22 @@ # @keep-alive: enable keep-alive when connecting to/listening on this sock= et. # (Since 4.2, not supported for listening sockets until 10.1) # +# @keep-alive-count: number of keep-alive packets sent before the connecti= on is +# closed. Only supported for TCP sockets on systems where TCP_KEEPCNT +# socket option is defined (this includes Linux, Windows, macOS, FreeB= SD, +# but not OpenBSD). When set to 0, system setting is used. (Since 10= .1) +# +# @keep-alive-idle: time in seconds the connection needs to be idle before +# sending a keepalive packet. Only supported for TCP sockets on syste= ms +# where TCP_KEEPIDLE socket option is defined (this includes Linux, +# Windows, macOS, FreeBSD, but not OpenBSD). When set to 0, system se= tting +# is used. (Since 10.1) +# +# @keep-alive-interval: time in seconds between keep-alive packets. Only +# supported for TCP sockets on systems where TCP_KEEPINTVL is defined = (this +# includes Linux, Windows, macOS, FreeBSD, but not OpenBSD). When set= to +# 0, system setting is used. (Since 10.1) +# # @mptcp: enable multi-path TCP. (Since 6.1) # # Since: 1.3 @@ -71,6 +87,9 @@ '*ipv4': 'bool', '*ipv6': 'bool', '*keep-alive': 'bool', + '*keep-alive-count': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPCNT' }, + '*keep-alive-idle': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPIDLE' }, + '*keep-alive-interval': { 'type': 'uint32', 'if': 'HAVE_TCP_KEEPINTVL'= }, '*mptcp': { 'type': 'bool', 'if': 'HAVE_IPPROTO_MPTCP' } } } =20 ## diff --git a/tests/unit/test-util-sockets.c b/tests/unit/test-util-sockets.c index 77d0767b9b..e4fb5b702a 100644 --- a/tests/unit/test-util-sockets.c +++ b/tests/unit/test-util-sockets.c @@ -358,6 +358,18 @@ static void inet_parse_test_helper(const char *str, In= etSocketAddress *exp_addr, g_assert_cmpint(addr.ipv6, =3D=3D, exp_addr->ipv6); g_assert_cmpint(addr.has_keep_alive, =3D=3D, exp_addr->has_keep_al= ive); g_assert_cmpint(addr.keep_alive, =3D=3D, exp_addr->keep_alive); +#ifdef HAVE_TCP_KEEPCNT + g_assert_cmpint(addr.has_keep_alive_count, =3D=3D, exp_addr->has_k= eep_alive_count); + g_assert_cmpint(addr.keep_alive_count, =3D=3D, exp_addr->keep_aliv= e_count); +#endif +#ifdef HAVE_TCP_KEEPIDLE + g_assert_cmpint(addr.has_keep_alive_idle, =3D=3D, exp_addr->has_ke= ep_alive_idle); + g_assert_cmpint(addr.keep_alive_idle, =3D=3D, exp_addr->keep_alive= _idle); +#endif +#ifdef HAVE_TCP_KEEPINTVL + g_assert_cmpint(addr.has_keep_alive_interval, =3D=3D, exp_addr->ha= s_keep_alive_interval); + g_assert_cmpint(addr.keep_alive_interval, =3D=3D, exp_addr->keep_a= live_interval); +#endif #ifdef HAVE_IPPROTO_MPTCP g_assert_cmpint(addr.has_mptcp, =3D=3D, exp_addr->has_mptcp); g_assert_cmpint(addr.mptcp, =3D=3D, exp_addr->mptcp); @@ -459,6 +471,18 @@ static void test_inet_parse_all_options_good(void) .ipv6 =3D true, .has_keep_alive =3D true, .keep_alive =3D true, +#ifdef HAVE_TCP_KEEPCNT + .has_keep_alive_count =3D true, + .keep_alive_count =3D 10, +#endif +#ifdef HAVE_TCP_KEEPIDLE + .has_keep_alive_idle =3D true, + .keep_alive_idle =3D 60, +#endif +#ifdef HAVE_TCP_KEEPINTVL + .has_keep_alive_interval =3D true, + .keep_alive_interval =3D 30, +#endif #ifdef HAVE_IPPROTO_MPTCP .has_mptcp =3D true, .mptcp =3D true, @@ -466,6 +490,15 @@ static void test_inet_parse_all_options_good(void) }; inet_parse_test_helper( "[::1]:5000,numeric,to=3D5006,ipv4=3Doff,ipv6=3Don,keep-alive" +#ifdef HAVE_TCP_KEEPCNT + ",keep-alive-count=3D10" +#endif +#ifdef HAVE_TCP_KEEPIDLE + ",keep-alive-idle=3D60" +#endif +#ifdef HAVE_TCP_KEEPINTVL + ",keep-alive-interval=3D30" +#endif #ifdef HAVE_IPPROTO_MPTCP ",mptcp" #endif diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 403dc26b36..3da5c18870 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -45,6 +45,14 @@ # define AI_NUMERICSERV 0 #endif =20 +/* + * On macOS TCP_KEEPIDLE is available under a different name, TCP_KEEPALIV= E. + * https://github.com/apple/darwin-xnu/blob/xnu-4570.1.46/bsd/man/man4/tcp= .4#L172 + */ +#if defined(TCP_KEEPALIVE) && !defined(TCP_KEEPIDLE) +# define TCP_KEEPIDLE TCP_KEEPALIVE +#endif + =20 static int inet_getport(struct addrinfo *e) { @@ -218,6 +226,45 @@ static int inet_set_sockopts(int sock, InetSocketAddre= ss *saddr, Error **errp) "Unable to set keep-alive option on socket"); return -1; } +#ifdef HAVE_TCP_KEEPCNT + if (saddr->has_keep_alive_count && + saddr->keep_alive_count) { + int keep_count =3D saddr->has_keep_alive_count; + ret =3D setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keep_count, + sizeof(keep_count)); + if (ret < 0) { + error_setg_errno(errp, errno, + "Unable to set TCP keep-alive count optio= n on socket"); + return -1; + } + } +#endif +#ifdef HAVE_TCP_KEEPIDLE + if (saddr->has_keep_alive_idle && + saddr->keep_alive_idle) { + int keep_idle =3D saddr->has_keep_alive_idle; + ret =3D setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keep_idle, + sizeof(keep_idle)); + if (ret < 0) { + error_setg_errno(errp, errno, + "Unable to set TCP keep-alive idle option= on socket"); + return -1; + } + } +#endif +#ifdef HAVE_TCP_KEEPINTVL + if (saddr->has_keep_alive_interval && + saddr->keep_alive_interval) { + int keep_interval =3D saddr->has_keep_alive_interval; + ret =3D setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keep_int= erval, + sizeof(keep_interval)); + if (ret < 0) { + error_setg_errno(errp, errno, + "Unable to set TCP keep-alive interval op= tion on socket"); + return -1; + } + } +#endif } return 0; } @@ -630,6 +677,24 @@ static QemuOptsList inet_opts =3D { .name =3D "keep-alive", .type =3D QEMU_OPT_BOOL, }, +#ifdef HAVE_TCP_KEEPCNT + { + .name =3D "keep-alive-count", + .type =3D QEMU_OPT_NUMBER, + }, +#endif +#ifdef HAVE_TCP_KEEPIDLE + { + .name =3D "keep-alive-idle", + .type =3D QEMU_OPT_NUMBER, + }, +#endif +#ifdef HAVE_TCP_KEEPINTVL + { + .name =3D "keep-alive-interval", + .type =3D QEMU_OPT_NUMBER, + }, +#endif #ifdef HAVE_IPPROTO_MPTCP { .name =3D "mptcp", @@ -695,6 +760,24 @@ int inet_parse(InetSocketAddress *addr, const char *st= r, Error **errp) addr->has_keep_alive =3D true; addr->keep_alive =3D qemu_opt_get_bool(opts, "keep-alive", false); } +#ifdef HAVE_TCP_KEEPCNT + if (qemu_opt_find(opts, "keep-alive-count")) { + addr->has_keep_alive_count =3D true; + addr->keep_alive_count =3D qemu_opt_get_number(opts, "keep-alive-c= ount", 0); + } +#endif +#ifdef HAVE_TCP_KEEPIDLE + if (qemu_opt_find(opts, "keep-alive-idle")) { + addr->has_keep_alive_idle =3D true; + addr->keep_alive_idle =3D qemu_opt_get_number(opts, "keep-alive-id= le", 0); + } +#endif +#ifdef HAVE_TCP_KEEPINTVL + if (qemu_opt_find(opts, "keep-alive-interval")) { + addr->has_keep_alive_interval =3D true; + addr->keep_alive_interval =3D qemu_opt_get_number(opts, "keep-aliv= e-interval", 0); + } +#endif #ifdef HAVE_IPPROTO_MPTCP if (qemu_opt_find(opts, "mptcp")) { addr->has_mptcp =3D true; --=20 2.49.0