From nobody Mon Feb 9 00:30:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1652778483; cv=none; d=zohomail.com; s=zohoarc; b=hoYdi+V/vS3OhTgWP3u68sYKv12g3fxPKSgNIe1bghRxUneAdgYf4JYEo7G9dINfkxzUK0wxDFUvbtuuUV1PpWMrlm8syEDN+9y8m64istIHkZGfwUdlxEq9VhCsVuxUCWwYSpsSlxSxrtCZ45EQKL2l7UOvJnJ4Ox8etYevvE0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1652778483; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=isyXYEQeG62P65VEDgNN6wjzeADNx7IVqCFlv5xsrig=; b=iQvjykZ2QXuY8dl3NZAmPdLZ/QBekjxyD9VfAKStXEmDF09GY8PH3htpjGphpDt13N/YykbcwXpx5DWag8L0BgQT/7OvNkyGvcnT1ytVtXMsjCcJ6F66SPQkGgm0j6hGLX7dXI22yZOpaUW26+lJbSXCNnBYziyBPu0B00KVDbY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1652778483979369.3144471814197; Tue, 17 May 2022 02:08:03 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-438-HMogIRmyMaGme8q83EZyMw-1; Tue, 17 May 2022 05:07:33 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 392E13C10256; Tue, 17 May 2022 09:07:31 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1E3E040CF8F0; Tue, 17 May 2022 09:07:31 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 62B891947079; Tue, 17 May 2022 09:07:30 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 502A21947056 for ; Tue, 17 May 2022 09:07:29 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 45369156D12A; Tue, 17 May 2022 09:07:29 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id C7720156D120 for ; Tue, 17 May 2022 09:07:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1652778483; h=from:from:sender:sender: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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=isyXYEQeG62P65VEDgNN6wjzeADNx7IVqCFlv5xsrig=; b=cfTFqjgm5hMXh819HSMoRffSXjVxUoECpUeaKHpSV+t/axQGe3m7vg5Gfysf7qJvB+fJJ1 ObutdYS1N7lIzDxFDMVSPIVAi+T1kKKdFv4XA7bp4P6bwbMLB9Ge+aDRqX1rycZYXhx15y zNF4FEVn3NT49Vo7QPyny/gPv0XSVzQ= X-MC-Unique: HMogIRmyMaGme8q83EZyMw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/9] qemu: fd: Add a distinct set of APIs for 'direct' fd passing mode Date: Tue, 17 May 2022 11:07:18 +0200 Message-Id: <6650bdbb9fd5f5bdab28112b0082d1ad3fbbc2b1.1652778351.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1652778484495100001 Content-Type: text/plain; charset="utf-8" Originally I envisioned a common set of APIs for both FD passing approaches but it turns out they are not really compatible enough for it to make sense to use one set of APIs. As of such introduce a distinct set of APIs for the 'direct' mode, which will later be used to convert all places that currently use 'qemuFDPassNewDirect' and later clean up the existing APIs. Signed-off-by: Peter Krempa --- src/qemu/qemu_fd.c | 128 +++++++++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_fd.h | 22 ++++++++ 2 files changed, 150 insertions(+) diff --git a/src/qemu/qemu_fd.c b/src/qemu/qemu_fd.c index 4641122d1f..442f92df2f 100644 --- a/src/qemu/qemu_fd.c +++ b/src/qemu/qemu_fd.c @@ -324,3 +324,131 @@ qemuFDPassGetPath(qemuFDPass *fdpass) return fdpass->path; } + + +struct _qemuFDPassDirect { + int fd; + char *path; + char *name; + + bool passed; /* passed to qemu via monitor */ +}; + + +void +qemuFDPassDirectFree(qemuFDPassDirect *fdpass) +{ + + if (!fdpass) + return; + + VIR_FORCE_CLOSE(fdpass->fd); + g_free(fdpass->path); + g_free(fdpass); +} + + +/** + * qemuFDPassDirectNew: + * @name: Name of the fd (for monitor passing use-case) + * @fd: The FD, cleared when passed. + * + * The qemuFDPassDirect helper returned by this helper is used to hold a FD + * passed to qemu either direcly via FD number when used on commandline or= the + * 'getfd' QMP command. + */ +qemuFDPassDirect * +qemuFDPassDirectNew(const char *name, + int *fd) +{ + qemuFDPassDirect *fdpass =3D g_new0(qemuFDPassDirect, 1); + + fdpass->name =3D g_strdup(name); + fdpass->fd =3D *fd; + *fd =3D -1; + + return fdpass; +} + + +/** + * qemuFDPassDirectTransferCommand: + * @fdpass: The fd passing helper struct + * @cmd: Command to pass the filedescriptors to + * + * Pass the fds in @fdpass to a commandline object @cmd. @fdpass may be NU= LL + * in which case this is a no-op. + */ +void +qemuFDPassDirectTransferCommand(qemuFDPassDirect *fdpass, + virCommand *cmd) +{ + if (!fdpass) + return; + + virCommandPassFD(cmd, fdpass->fd, VIR_COMMAND_PASS_FD_CLOSE_PARENT); + fdpass->path =3D g_strdup_printf("%d", fdpass->fd); + fdpass->fd =3D -1; +} + + +/** + * qemuFDPassDirectTransferMonitor: + * @fdpass: The fd passing helper struct + * @mon: monitor object + * + * Pass the fds in @fdpass to qemu via the monitor. @fdpass may be NULL + * in which case this is a no-op. Caller needs to enter the monitor contex= t. + */ +int +qemuFDPassDirectTransferMonitor(qemuFDPassDirect *fdpass, + qemuMonitor *mon) +{ + if (!fdpass) + return 0; + + if (qemuMonitorSendFileHandle(mon, fdpass->name, fdpass->fd) < 0) + return -1; + + fdpass->path =3D g_strdup(fdpass->name); + VIR_FORCE_CLOSE(fdpass->fd); + fdpass->passed =3D true; + + return 0; +} + + +/** + * qemuFDPassDirectTransferMonitorRollback: + * @fdpass: The fd passing helper struct + * @mon: monitor object + * + * Rolls back the addition of @fdpass to @mon if it was added originally. + */ +void +qemuFDPassDirectTransferMonitorRollback(qemuFDPassDirect *fdpass, + qemuMonitor *mon) +{ + if (!fdpass || !fdpass->passed) + return; + + ignore_value(qemuMonitorCloseFileHandle(mon, fdpass->name)); +} + + +/** + * qemuFDPassDirectGetPath: + * @fdpass: The fd passing helper struct + * + * Returns the path/fd name that is used in qemu to refer to the passed FD. + * Note that it's only valid to call this function after @fdpass was alrea= dy + * transferred to the command or monitor. + */ +const char * +qemuFDPassDirectGetPath(qemuFDPassDirect *fdpass) +{ + if (!fdpass) + return NULL; + + return fdpass->path; +} diff --git a/src/qemu/qemu_fd.h b/src/qemu/qemu_fd.h index d078d4ce5d..dbb4ab0aa5 100644 --- a/src/qemu/qemu_fd.h +++ b/src/qemu/qemu_fd.h @@ -53,3 +53,25 @@ qemuFDPassTransferMonitorRollback(qemuFDPass *fdpass, const char * qemuFDPassGetPath(qemuFDPass *fdpass); + + +typedef struct _qemuFDPassDirect qemuFDPassDirect; + +void +qemuFDPassDirectFree(qemuFDPassDirect *fdpass); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuFDPassDirect, qemuFDPassDirectFree); + +qemuFDPassDirect * +qemuFDPassDirectNew(const char *name, + int *fd); +void +qemuFDPassDirectTransferCommand(qemuFDPassDirect *fdpass, + virCommand *cmd); +int +qemuFDPassDirectTransferMonitor(qemuFDPassDirect *fdpass, + qemuMonitor *mon); +void +qemuFDPassDirectTransferMonitorRollback(qemuFDPassDirect *fdpass, + qemuMonitor *mon); +const char * +qemuFDPassDirectGetPath(qemuFDPassDirect *fdpass); --=20 2.35.3