From nobody Tue Sep 22 07:43:27 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) client-ip=38.145.34.151; 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 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1783604792; cv=none; d=zohomail.com; s=zohoarc; b=dUDJTUGVFfAoRj3GeomFE/HNk9Sm6xz2LHLJT1mZTbNSaQOcrWLe68XzMMC2ou40Cut//JMo29WNtuHs9iAWyjEfF8mRCCTB2DCda1HUT4j56Fdaigb3y8p/qS9lM9KH0Z4YMUvanVo37mdo4Ay+qbuRLMXrm8E+jaQT9+nfge0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783604792; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=tv+N6u4ljpvkD+8L6TQNjs0Y2uqEoAoQbWwhb97xi0Y=; b=kd/VMigei6G0q5H5ztq5yT0yE12IT+3/eUxr6Jvrl2bK5EBzADem+oGQVYX4KcyJpVRNdgsQ+Sv1kQabRwT7cSKQF3tpIB2ZcJbsPhtrhmB1fzuhJyI2RI1DIVzZN+F6npW5v/x1jsATASTxm9b67esrRO09Ok7AIQ16sUK6iuU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 38.145.34.151 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [38.145.34.151]) by mx.zohomail.com with SMTPS id 1783604792041302.8250252094185; Thu, 9 Jul 2026 06:46:32 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 070BC41CB4; Thu, 9 Jul 2026 09:46:30 -0400 (EDT) Received: from [172.19.199.10] (unknown [10.16.107.18]) by lists.libvirt.org (Postfix) with ESMTP id 9885B41D85; Thu, 9 Jul 2026 09:41:12 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 993) id 760F841983; Thu, 9 Jul 2026 09:38:27 -0400 (EDT) Received: from relay.virtuozzo.com (relay.virtuozzo.com [130.117.225.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id ADED441AA9 for ; Thu, 9 Jul 2026 09:38:26 -0400 (EDT) Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=mami) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1whogt-006MF9-1d for devel@lists.libvirt.org; Thu, 09 Jul 2026 15:22:27 +0200 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=tv+N6u4ljpvkD+8L6TQNjs0Y2uqEoAoQbWwhb97xi0Y=; b=fdtSrlNrbiDg XgtATjR0XGDo94+eVzFdQmuMoGdOlB3fJ0zH+8ISlwNPDeH3qONg0N2UUNOLcF29IjCm/ZkVlXd2F FW1fjleWU6HioYw3KeBqjHuSxad50eEh6AN6lwNInuiJpUPeLyd5vksvwsNJl8hk+uBg7EhUeXMiR mcyL8N2zkgYDt7Z3pWBeqQnkfcoEDp8azJ7lx9kPc7Kc8GXHDqGev+mmcYnbEUd5skw0SMzMqyO8p Fd9pNSj9fRNAPoXqBU/nTeu9o3FxB8TxnPyUd9q/md1Q1QGGY5tjtsupVmWaQPrbrFUDelwGEECex DUU8H43DnxfQITKqm+8B8Q==; To: devel@lists.libvirt.org Subject: [PATCH 1/6] util: Add virVsockConnectQuiet helper Date: Thu, 9 Jul 2026 15:15:14 +0200 Message-ID: <20260709132213.2392145-2-polina.vishneva@virtuozzo.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260709132213.2392145-1-polina.vishneva@virtuozzo.com> References: <20260709132213.2392145-1-polina.vishneva@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MailFrom: polina.vishneva@virtuozzo.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation Message-ID-Hash: 44IGYBATNQOQ64Y66ZK6IURZBW4J44YQ X-Message-ID-Hash: 44IGYBATNQOQ64Y66ZK6IURZBW4J44YQ X-Mailman-Approved-At: Thu, 09 Jul 2026 13:40:43 +0000 X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Polina Vishneva via Devel Reply-To: Polina Vishneva X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1783604792913158500 Content-Type: text/plain; charset="utf-8" Add a common helper to connect to a vsock endpoint by CID and port. This can be used by both the daemon (guest agent over vsock) and tools (e.g. SSH-over-vsock command execution). It raises no libvirt errors and preserves errno, making it suitable for probing service availability without spamming the log. An error-reporting counterpart can be added alongside once a caller needs one. We use SO_VM_SOCKETS_CONNECT_TIMEOUT to cap the worst-case wait to 200ms. Best case is still immediate (when the service is present or when the kernel early-returns in case the guest isn't ready). Signed-off-by: Polina Vishneva --- src/libvirt_private.syms | 1 + src/util/virvsock.c | 69 ++++++++++++++++++++++++++++++++++++++++ src/util/virvsock.h | 6 ++++ 3 files changed, 76 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index c76e5cb08a..6a9cd34134 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3842,6 +3842,7 @@ virPCIVPDResourceUpdateKeyword; =20 # util/virvsock.h virVsockAcquireGuestCid; +virVsockConnectQuiet; virVsockSetGuestCid; =20 =20 diff --git a/src/util/virvsock.c b/src/util/virvsock.c index c6f8b362b8..10ca1d44eb 100644 --- a/src/util/virvsock.c +++ b/src/util/virvsock.c @@ -23,11 +23,15 @@ =20 #ifdef __linux__ # include +# include +# include +# include #endif =20 #include "virvsock.h" =20 #include "virerror.h" +#include "virfile.h" #include "virlog.h" =20 =20 @@ -105,3 +109,68 @@ virVsockAcquireGuestCid(int fd, =20 return 0; } + + +#ifdef __linux__ + +/* Use the _OLD option with a __kernel_old_timeval: _NEW takes a 64-bit + * __kernel_sock_timeval which is more awkward to build, and glibc's struct + * timeval is 64-bit under _TIME_BITS=3D64 on 32-bit, which the _OLD option + * misreads. */ +#ifndef SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD +# define SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD SO_VM_SOCKETS_CONNECT_TIMEOUT +#endif + +/** + * virVsockConnectQuiet: + * @cid: guest context ID + * @port: vsock port number + * + * Connect to a vsock endpoint identified by @cid and @port. + * Doesn't raise any errors. Preserves the actual errno on error. + * + * Returns: connected fd on success, -1 on error. + */ +int +virVsockConnectQuiet(unsigned int cid, + unsigned int port) +{ + struct sockaddr_vm addr =3D { + .svm_family =3D AF_VSOCK, + .svm_cid =3D cid, + .svm_port =3D port, + }; + struct __kernel_old_timeval tv =3D { + .tv_sec =3D VIR_VSOCK_CONNECT_TIMEOUT_MS / 1000, + .tv_usec =3D (VIR_VSOCK_CONNECT_TIMEOUT_MS % 1000) * 1000, + }; + int fd; + + if ((fd =3D socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) + return -1; + + if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_CONNECT_TIMEOUT_OLD, + &tv, sizeof(tv)) < 0) + goto error; + + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) + goto error; + + return fd; + + error: + VIR_FORCE_CLOSE(fd); + return -1; +} + +#else /* !__linux__ */ + +int +virVsockConnectQuiet(unsigned int cid G_GNUC_UNUSED, + unsigned int port G_GNUC_UNUSED) +{ + errno =3D ENOSYS; + return -1; +} + +#endif /* !__linux__ */ diff --git a/src/util/virvsock.h b/src/util/virvsock.h index d6ba2faabf..06c251b594 100644 --- a/src/util/virvsock.h +++ b/src/util/virvsock.h @@ -18,6 +18,8 @@ =20 #pragma once =20 +#define VIR_VSOCK_CONNECT_TIMEOUT_MS 200 + int virVsockSetGuestCid(int fd, unsigned int guest_cid); @@ -25,3 +27,7 @@ virVsockSetGuestCid(int fd, int virVsockAcquireGuestCid(int fd, unsigned int *guest_cid); + +int +virVsockConnectQuiet(unsigned int cid, + unsigned int port); --=20 2.54.0