[PATCH 0/5] Introduce 'virDomainQemuMonitorCommandWithFiles'

I needed to do some tests where I wanted to pass FDs to qemu to see how 'add-fd' and 'getfd' behave. It may be useful for others who wish for any reason to pass FDs to QMP commands. Peter Krempa (5): lib: Introduce 'virDomainQemuMonitorCommandWithFiles' cmdStartGetFDs: Modernize virsh-domain: Move and rename cmdStartGetFDs to virshFetchPassFdsList virsh: Implement support for virDomainQemuMonitorCommandWithFiles qemu: Implement qemuDomainQemuMonitorCommandWithFiles docs/manpages/virsh.rst | 6 +- include/libvirt/libvirt-qemu.h | 6 ++ src/driver-hypervisor.h | 8 +++ src/libvirt-qemu.c | 71 ++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu/qemu_driver.c | 34 +++++++-- src/qemu/qemu_monitor.c | 7 +- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 +- src/qemu/qemu_monitor_json.h | 2 + src/qemu/qemu_monitor_text.c | 8 +-- src/qemu_protocol-structs | 9 +++ src/remote/qemu_protocol.x | 20 +++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++ src/remote/remote_driver.c | 40 +++++++++++ tools/virsh-domain.c | 108 ++++++++++++++++------------ 16 files changed, 313 insertions(+), 60 deletions(-) -- 2.34.1

This API has the same semantics as 'virDomainQemuMonitorCommand' but accepts file descriptors which are then forwarded to qemu. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- include/libvirt/libvirt-qemu.h | 6 +++ src/driver-hypervisor.h | 8 ++++ src/libvirt-qemu.c | 71 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu_protocol-structs | 9 ++++ src/remote/qemu_protocol.x | 20 +++++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++++++++ src/remote/remote_driver.c | 40 ++++++++++++++++ 8 files changed, 200 insertions(+), 1 deletion(-) diff --git a/include/libvirt/libvirt-qemu.h b/include/libvirt/libvirt-qemu.h index 0cc2872821..eed691ec91 100644 --- a/include/libvirt/libvirt-qemu.h +++ b/include/libvirt/libvirt-qemu.h @@ -37,6 +37,12 @@ typedef enum { int virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags); +int virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags); virDomainPtr virDomainQemuAttach(virConnectPtr domain, unsigned int pid_value, diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index c83fb648a2..b3e55cf4ac 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -874,6 +874,13 @@ typedef int const char *cmd, char **result, unsigned int flags); +typedef int +(*virDrvDomainQemuMonitorCommandWithFiles)(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags); typedef char * (*virDrvDomainQemuAgentCommand)(virDomainPtr domain, @@ -1597,6 +1604,7 @@ struct _virHypervisorDriver { virDrvDomainRevertToSnapshot domainRevertToSnapshot; virDrvDomainSnapshotDelete domainSnapshotDelete; virDrvDomainQemuMonitorCommand domainQemuMonitorCommand; + virDrvDomainQemuMonitorCommandWithFiles domainQemuMonitorCommandWithFiles; virDrvDomainQemuAttach domainQemuAttach; virDrvDomainQemuAgentCommand domainQemuAgentCommand; virDrvConnectDomainQemuMonitorEventRegister connectDomainQemuMonitorEventRegister; diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c index 1afb5fe529..1dbe0cba54 100644 --- a/src/libvirt-qemu.c +++ b/src/libvirt-qemu.c @@ -96,6 +96,77 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +/** + * virDomainQemuMonitorCommandWithFiles: + * @domain: a domain object + * @cmd: the qemu monitor command string + * @nfiles: number of filedescriptors passed in @files + * @files: filedescriptors to be passed to qemu with the command + * @result: a string returned by @cmd + * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags + * + * This API is QEMU specific, so it will only work with hypervisor + * connections to the QEMU driver with local connections using the unix socket. + * + * Send an arbitrary monitor command @cmd with file descriptors @files to + * @domain through the qemu monitor. There are several requirements to safely + * and successfully use this API: + * + * - A @cmd that queries state without making any modifications is safe + * - A @cmd that alters state that is also tracked by libvirt is unsafe, + * and may cause libvirtd to crash + * - A @cmd that alters state not tracked by the current version of + * libvirt is possible as a means to test new qemu features before + * they have support in libvirt, but no guarantees are made to safety + * + * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is + * considered to be a human monitor command and libvirt will automatically + * convert it into QMP if needed. In that case the @result will also + * be converted back from QMP. + * + * If successful, @result will be filled with the string output of the + * @cmd, and the caller must free this string. + * + * Returns 0 in case of success, -1 in case of failure + */ +int +virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "cmd=%s, nfiles=%u, files=%p, result=%p, flags=0x%x", + cmd, nfiles, files, result, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckNonNullArgGoto(result, error); + virCheckReadOnlyGoto(conn->flags, error); + + if (conn->driver->domainQemuMonitorCommandWithFiles) { + int ret; + ret = conn->driver->domainQemuMonitorCommandWithFiles(domain, cmd, + nfiles, files, + result, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(conn); + return -1; +} + /** * virDomainQemuAttach: * @conn: pointer to a hypervisor connection diff --git a/src/libvirt_qemu.syms b/src/libvirt_qemu.syms index 3a297e3a2b..019e545101 100644 --- a/src/libvirt_qemu.syms +++ b/src/libvirt_qemu.syms @@ -30,3 +30,8 @@ LIBVIRT_QEMU_1.2.3 { virConnectDomainQemuMonitorEventDeregister; virConnectDomainQemuMonitorEventRegister; } LIBVIRT_QEMU_0.10.0; + +LIBVIRT_QEMU_8.1.0 { + global: + virDomainQemuMonitorCommandWithFiles; +} LIBVIRT_QEMU_1.2.3; diff --git a/src/qemu_protocol-structs b/src/qemu_protocol-structs index 8501543cd9..ea0854385f 100644 --- a/src/qemu_protocol-structs +++ b/src/qemu_protocol-structs @@ -47,6 +47,14 @@ struct qemu_domain_monitor_event_msg { u_int micros; remote_string details; }; +struct qemu_domain_monitor_command_with_files_args { + remote_nonnull_domain dom; + remote_nonnull_string cmd; + u_int flags; +}; +struct qemu_domain_monitor_command_with_files_ret { + remote_nonnull_string result; +}; enum qemu_procedure { QEMU_PROC_DOMAIN_MONITOR_COMMAND = 1, QEMU_PROC_DOMAIN_ATTACH = 2, @@ -54,4 +62,5 @@ enum qemu_procedure { QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_REGISTER = 4, QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_DEREGISTER = 5, QEMU_PROC_DOMAIN_MONITOR_EVENT = 6, + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES = 7, }; diff --git a/src/remote/qemu_protocol.x b/src/remote/qemu_protocol.x index 8ff5dc8568..c7f3abfcbf 100644 --- a/src/remote/qemu_protocol.x +++ b/src/remote/qemu_protocol.x @@ -79,6 +79,17 @@ struct qemu_domain_monitor_event_msg { remote_string details; }; +struct qemu_domain_monitor_command_with_files_args { + remote_nonnull_domain dom; + remote_nonnull_string cmd; + unsigned int flags; +}; + +struct qemu_domain_monitor_command_with_files_ret { + remote_nonnull_string result; +}; + + /* Define the program number, protocol version and procedure numbers here. */ const QEMU_PROGRAM = 0x20008087; const QEMU_PROTOCOL_VERSION = 1; @@ -151,5 +162,12 @@ enum qemu_procedure { * @generate: both * @acl: none */ - QEMU_PROC_DOMAIN_MONITOR_EVENT = 6 + QEMU_PROC_DOMAIN_MONITOR_EVENT = 6, + + /** + * @generate: none + * @priority: low + * @acl: domain:write + */ + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES = 7 }; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c index 689001889e..5f967abed0 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -4689,6 +4689,48 @@ qemuDispatchDomainMonitorCommand(virNetServer *server G_GNUC_UNUSED, } +static int +qemuDispatchDomainMonitorCommandWithFiles(virNetServer *server G_GNUC_UNUSED, + virNetServerClient *client, + virNetMessage *msg, + struct virNetMessageError *rerr, + qemu_domain_monitor_command_with_files_args *args, + qemu_domain_monitor_command_with_files_ret *ret) +{ + virDomainPtr dom = NULL; + int *files = NULL; + unsigned int nfiles = 0; + int rv = -1; + virConnectPtr conn = remoteGetHypervisorConn(client); + size_t i; + + if (!conn) + goto cleanup; + + if (!(dom = get_nonnull_domain(conn, args->dom))) + goto cleanup; + + files = g_new0(int, msg->nfds); + for (i = 0; i < msg->nfds; i++) { + if ((files[i] = virNetMessageDupFD(msg, i)) < 0) + goto cleanup; + nfiles++; + } + + if (virDomainQemuMonitorCommandWithFiles(dom, args->cmd, nfiles, files, + &ret->result, args->flags) < 0) + goto cleanup; + + rv = 0; + + cleanup: + if (rv < 0) + virNetMessageSaveError(rerr); + virObjectUnref(dom); + return rv; +} + + static int remoteDispatchDomainMigrateBegin3(virNetServer *server G_GNUC_UNUSED, virNetServerClient *client, diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 5b7ccfaebd..ee3f4e4e63 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -5938,6 +5938,45 @@ remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +static int +remoteDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags) +{ + int rv = -1; + qemu_domain_monitor_command_with_files_args args; + qemu_domain_monitor_command_with_files_ret ret; + struct private_data *priv = domain->conn->privateData; + + remoteDriverLock(priv); + + make_nonnull_domain(&args.dom, domain); + args.cmd = (char *)cmd; + args.flags = flags; + + memset(&ret, 0, sizeof(ret)); + if (callFull(domain->conn, priv, REMOTE_CALL_QEMU, + files, nfiles, NULL, NULL, + QEMU_PROC_DOMAIN_MONITOR_COMMAND_WITH_FILES, + (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_args, (char *) &args, + (xdrproc_t) xdr_qemu_domain_monitor_command_with_files_ret, (char *) &ret) == -1) + goto done; + + *result = g_strdup(ret.result); + + rv = 0; + + xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret); + + done: + remoteDriverUnlock(priv); + return rv; +} + + static char * remoteDomainMigrateBegin3(virDomainPtr domain, const char *xmlin, @@ -8513,6 +8552,7 @@ static virHypervisorDriver hypervisor_driver = { .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */ .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */ .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */ + .domainQemuMonitorCommandWithFiles = remoteDomainQemuMonitorCommandWithFiles, /* 8.1.0 */ .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */ .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */ .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */ -- 2.34.1

On a Thursday in 2022, Peter Krempa wrote:
This API has the same semantics as 'virDomainQemuMonitorCommand' but accepts file descriptors which are then forwarded to qemu.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- include/libvirt/libvirt-qemu.h | 6 +++ src/driver-hypervisor.h | 8 ++++ src/libvirt-qemu.c | 71 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu_protocol-structs | 9 ++++ src/remote/qemu_protocol.x | 20 +++++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++++++++ src/remote/remote_driver.c | 40 ++++++++++++++++ 8 files changed, 200 insertions(+), 1 deletion(-)
diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c index 1afb5fe529..1dbe0cba54 100644 --- a/src/libvirt-qemu.c +++ b/src/libvirt-qemu.c @@ -96,6 +96,77 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, }
+/** + * virDomainQemuMonitorCommandWithFiles: + * @domain: a domain object + * @cmd: the qemu monitor command string + * @nfiles: number of filedescriptors passed in @files + * @files: filedescriptors to be passed to qemu with the command + * @result: a string returned by @cmd + * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags + * + * This API is QEMU specific, so it will only work with hypervisor + * connections to the QEMU driver with local connections using the unix socket. + * + * Send an arbitrary monitor command @cmd with file descriptors @files to + * @domain through the qemu monitor. There are several requirements to safely + * and successfully use this API: + * + * - A @cmd that queries state without making any modifications is safe + * - A @cmd that alters state that is also tracked by libvirt is unsafe, + * and may cause libvirtd to crash + * - A @cmd that alters state not tracked by the current version of + * libvirt is possible as a means to test new qemu features before + * they have support in libvirt, but no guarantees are made to safety + * + * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is + * considered to be a human monitor command and libvirt will automatically + * convert it into QMP if needed. In that case the @result will also
'if needed' is not really needed. Jano
+ * be converted back from QMP. + * + * If successful, @result will be filled with the string output of the + * @cmd, and the caller must free this string. + * + * Returns 0 in case of success, -1 in case of failure + */

On 2/3/22 15:51, Peter Krempa wrote:
This API has the same semantics as 'virDomainQemuMonitorCommand' but accepts file descriptors which are then forwarded to qemu.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- include/libvirt/libvirt-qemu.h | 6 +++ src/driver-hypervisor.h | 8 ++++ src/libvirt-qemu.c | 71 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu_protocol-structs | 9 ++++ src/remote/qemu_protocol.x | 20 +++++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++++++++ src/remote/remote_driver.c | 40 ++++++++++++++++ 8 files changed, 200 insertions(+), 1 deletion(-)
diff --git a/include/libvirt/libvirt-qemu.h b/include/libvirt/libvirt-qemu.h index 0cc2872821..eed691ec91 100644 --- a/include/libvirt/libvirt-qemu.h +++ b/include/libvirt/libvirt-qemu.h @@ -37,6 +37,12 @@ typedef enum {
int virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags); +int virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files,
Do we perhaps want to have another argument for fdOut (e.g. when QEMU would want to return an FD)? I don't think there is a command that would do that now, but who knows, maybe there will be someday.
+ char **result, + unsigned int flags);
virDomainPtr virDomainQemuAttach(virConnectPtr domain, unsigned int pid_value, diff --git a/src/driver-hypervisor.h b/src/driver-hypervisor.h index c83fb648a2..b3e55cf4ac 100644 --- a/src/driver-hypervisor.h +++ b/src/driver-hypervisor.h @@ -874,6 +874,13 @@ typedef int const char *cmd, char **result, unsigned int flags); +typedef int +(*virDrvDomainQemuMonitorCommandWithFiles)(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags);
typedef char * (*virDrvDomainQemuAgentCommand)(virDomainPtr domain, @@ -1597,6 +1604,7 @@ struct _virHypervisorDriver { virDrvDomainRevertToSnapshot domainRevertToSnapshot; virDrvDomainSnapshotDelete domainSnapshotDelete; virDrvDomainQemuMonitorCommand domainQemuMonitorCommand; + virDrvDomainQemuMonitorCommandWithFiles domainQemuMonitorCommandWithFiles; virDrvDomainQemuAttach domainQemuAttach; virDrvDomainQemuAgentCommand domainQemuAgentCommand; virDrvConnectDomainQemuMonitorEventRegister connectDomainQemuMonitorEventRegister; diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c index 1afb5fe529..1dbe0cba54 100644 --- a/src/libvirt-qemu.c +++ b/src/libvirt-qemu.c @@ -96,6 +96,77 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, }
+/** + * virDomainQemuMonitorCommandWithFiles: + * @domain: a domain object + * @cmd: the qemu monitor command string + * @nfiles: number of filedescriptors passed in @files + * @files: filedescriptors to be passed to qemu with the command + * @result: a string returned by @cmd + * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags + * + * This API is QEMU specific, so it will only work with hypervisor + * connections to the QEMU driver with local connections using the unix socket. + * + * Send an arbitrary monitor command @cmd with file descriptors @files to + * @domain through the qemu monitor. There are several requirements to safely + * and successfully use this API: + * + * - A @cmd that queries state without making any modifications is safe + * - A @cmd that alters state that is also tracked by libvirt is unsafe, + * and may cause libvirtd to crash + * - A @cmd that alters state not tracked by the current version of + * libvirt is possible as a means to test new qemu features before + * they have support in libvirt, but no guarantees are made to safety + * + * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is + * considered to be a human monitor command and libvirt will automatically + * convert it into QMP if needed. In that case the @result will also + * be converted back from QMP. + * + * If successful, @result will be filled with the string output of the + * @cmd, and the caller must free this string. + * + * Returns 0 in case of success, -1 in case of failure + */ +int +virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "cmd=%s, nfiles=%u, files=%p, result=%p, flags=0x%x", + cmd, nfiles, files, result, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckNonNullArgGoto(result, error); + virCheckReadOnlyGoto(conn->flags, error);
Missing VIR_DRV_SUPPORTS_FEATURE(conn->driver, conn, VIR_DRV_FEATURE_FD_PASSING); check.
+ + if (conn->driver->domainQemuMonitorCommandWithFiles) { + int ret; + ret = conn->driver->domainQemuMonitorCommandWithFiles(domain, cmd, + nfiles, files, + result, flags); + if (ret < 0) + goto error; + return ret; + } + + virReportUnsupportedError(); + + error: + virDispatchError(conn); + return -1; +}
Michal

On Thu, Feb 03, 2022 at 18:23:25 +0100, Michal Prívozník wrote:
On 2/3/22 15:51, Peter Krempa wrote:
This API has the same semantics as 'virDomainQemuMonitorCommand' but accepts file descriptors which are then forwarded to qemu.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- include/libvirt/libvirt-qemu.h | 6 +++ src/driver-hypervisor.h | 8 ++++ src/libvirt-qemu.c | 71 +++++++++++++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu_protocol-structs | 9 ++++ src/remote/qemu_protocol.x | 20 +++++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++++++++ src/remote/remote_driver.c | 40 ++++++++++++++++ 8 files changed, 200 insertions(+), 1 deletion(-)
diff --git a/include/libvirt/libvirt-qemu.h b/include/libvirt/libvirt-qemu.h index 0cc2872821..eed691ec91 100644 --- a/include/libvirt/libvirt-qemu.h +++ b/include/libvirt/libvirt-qemu.h @@ -37,6 +37,12 @@ typedef enum {
int virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, char **result, unsigned int flags); +int virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files,
Do we perhaps want to have another argument for fdOut (e.g. when QEMU would want to return an FD)? I don't think there is a command that would do that now, but who knows, maybe there will be someday.
I can wire up the arguments into the public API, that should be easy enough.
+ char **result, + unsigned int flags);
virDomainPtr virDomainQemuAttach(virConnectPtr domain, unsigned int pid_value,
[...]
diff --git a/src/libvirt-qemu.c b/src/libvirt-qemu.c index 1afb5fe529..1dbe0cba54 100644 --- a/src/libvirt-qemu.c +++ b/src/libvirt-qemu.c @@ -96,6 +96,77 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, }
+/** + * virDomainQemuMonitorCommandWithFiles: + * @domain: a domain object + * @cmd: the qemu monitor command string + * @nfiles: number of filedescriptors passed in @files + * @files: filedescriptors to be passed to qemu with the command + * @result: a string returned by @cmd + * @flags: bitwise-or of supported virDomainQemuMonitorCommandFlags + * + * This API is QEMU specific, so it will only work with hypervisor + * connections to the QEMU driver with local connections using the unix socket. + * + * Send an arbitrary monitor command @cmd with file descriptors @files to + * @domain through the qemu monitor. There are several requirements to safely + * and successfully use this API: + * + * - A @cmd that queries state without making any modifications is safe + * - A @cmd that alters state that is also tracked by libvirt is unsafe, + * and may cause libvirtd to crash + * - A @cmd that alters state not tracked by the current version of + * libvirt is possible as a means to test new qemu features before + * they have support in libvirt, but no guarantees are made to safety + * + * If VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP is set, the command is + * considered to be a human monitor command and libvirt will automatically + * convert it into QMP if needed. In that case the @result will also + * be converted back from QMP. + * + * If successful, @result will be filled with the string output of the + * @cmd, and the caller must free this string. + * + * Returns 0 in case of success, -1 in case of failure + */ +int +virDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfiles, + int *files, + char **result, + unsigned int flags) +{ + virConnectPtr conn; + + VIR_DOMAIN_DEBUG(domain, "cmd=%s, nfiles=%u, files=%p, result=%p, flags=0x%x", + cmd, nfiles, files, result, flags); + + virResetLastError(); + + virCheckDomainReturn(domain, -1); + conn = domain->conn; + + virCheckNonNullArgGoto(result, error); + virCheckReadOnlyGoto(conn->flags, error);
Missing VIR_DRV_SUPPORTS_FEATURE(conn->driver, conn, VIR_DRV_FEATURE_FD_PASSING); check.
Ah, so this is how that's supposed to work :D.

Calculate the length of the FD list beforehand to avoid multiple expansions and mainly simplify the code and use automatic freeing to remove the error code path. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/virsh-domain.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 43d310f2af..b4cb7193a7 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4021,7 +4021,7 @@ cmdStartGetFDs(vshControl *ctl, { const char *fdopt; g_auto(GStrv) fdlist = NULL; - int *fds = NULL; + g_autofree int *fds = NULL; size_t nfds = 0; size_t i; @@ -4036,23 +4036,19 @@ cmdStartGetFDs(vshControl *ctl, return -1; } + nfds = g_strv_length(fdlist); + fds = g_new0(int, nfds); + for (i = 0; fdlist[i] != NULL; i++) { - int fd; - if (virStrToLong_i(fdlist[i], NULL, 10, &fd) < 0) { + if (virStrToLong_i(fdlist[i], NULL, 10, fds + i) < 0) { vshError(ctl, _("Unable to parse FD number '%s'"), fdlist[i]); - goto error; + return -1; } - VIR_EXPAND_N(fds, nfds, 1); - fds[nfds - 1] = fd; } - *fdsret = fds; + *fdsret = g_steal_pointer(&fds); *nfdsret = nfds; return 0; - - error: - VIR_FREE(fds); - return -1; } static bool -- 2.34.1

On a Thursday in 2022, Peter Krempa wrote:
Calculate the length of the FD list beforehand to avoid multiple expansions and mainly simplify the code and use automatic freeing to remove the error code path.
Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/virsh-domain.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 43d310f2af..b4cb7193a7 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4021,7 +4021,7 @@ cmdStartGetFDs(vshControl *ctl, { const char *fdopt; g_auto(GStrv) fdlist = NULL; - int *fds = NULL; + g_autofree int *fds = NULL; size_t nfds = 0; size_t i;
@@ -4036,23 +4036,19 @@ cmdStartGetFDs(vshControl *ctl, return -1; }
+ nfds = g_strv_length(fdlist); + fds = g_new0(int, nfds); + for (i = 0; fdlist[i] != NULL; i++) {
Should nfds be used in the for loop condition too? Jano

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tools/virsh-domain.c | 86 +++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index b4cb7193a7..0bf4bb8dad 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -225,6 +225,50 @@ virshAddressFormat(virBuffer *buf, } +/** + * virshFetchPassFdsList + * + * Helper to process the 'pass-fds' argument. + */ +static int +virshFetchPassFdsList(vshControl *ctl, + const vshCmd *cmd, + size_t *nfdsret, + int **fdsret) +{ + const char *fdopt; + g_auto(GStrv) fdlist = NULL; + g_autofree int *fds = NULL; + size_t nfds = 0; + size_t i; + + *nfdsret = 0; + *fdsret = NULL; + + if (vshCommandOptStringQuiet(ctl, cmd, "pass-fds", &fdopt) <= 0) + return 0; + + if (!(fdlist = g_strsplit(fdopt, ",", -1))) { + vshError(ctl, _("Unable to split FD list '%s'"), fdopt); + return -1; + } + + nfds = g_strv_length(fdlist); + fds = g_new0(int, nfds); + + for (i = 0; fdlist[i] != NULL; i++) { + if (virStrToLong_i(fdlist[i], NULL, 10, fds + i) < 0) { + vshError(ctl, _("Unable to parse FD number '%s'"), fdlist[i]); + return -1; + } + } + + *fdsret = g_steal_pointer(&fds); + *nfdsret = nfds; + return 0; +} + + #define VIRSH_COMMON_OPT_DOMAIN_PERSISTENT \ {.name = "persistent", \ .type = VSH_OT_BOOL, \ @@ -4013,44 +4057,6 @@ static const vshCmdOptDef opts_start[] = { {.name = NULL} }; -static int -cmdStartGetFDs(vshControl *ctl, - const vshCmd *cmd, - size_t *nfdsret, - int **fdsret) -{ - const char *fdopt; - g_auto(GStrv) fdlist = NULL; - g_autofree int *fds = NULL; - size_t nfds = 0; - size_t i; - - *nfdsret = 0; - *fdsret = NULL; - - if (vshCommandOptStringQuiet(ctl, cmd, "pass-fds", &fdopt) <= 0) - return 0; - - if (!(fdlist = g_strsplit(fdopt, ",", -1))) { - vshError(ctl, _("Unable to split FD list '%s'"), fdopt); - return -1; - } - - nfds = g_strv_length(fdlist); - fds = g_new0(int, nfds); - - for (i = 0; fdlist[i] != NULL; i++) { - if (virStrToLong_i(fdlist[i], NULL, 10, fds + i) < 0) { - vshError(ctl, _("Unable to parse FD number '%s'"), fdlist[i]); - return -1; - } - } - - *fdsret = g_steal_pointer(&fds); - *nfdsret = nfds; - return 0; -} - static bool cmdStart(vshControl *ctl, const vshCmd *cmd) { @@ -4072,7 +4078,7 @@ cmdStart(vshControl *ctl, const vshCmd *cmd) return false; } - if (cmdStartGetFDs(ctl, cmd, &nfds, &fds) < 0) + if (virshFetchPassFdsList(ctl, cmd, &nfds, &fds) < 0) return false; if (vshCommandOptBool(cmd, "paused")) @@ -8112,7 +8118,7 @@ cmdCreate(vshControl *ctl, const vshCmd *cmd) if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; - if (cmdStartGetFDs(ctl, cmd, &nfds, &fds) < 0) + if (virshFetchPassFdsList(ctl, cmd, &nfds, &fds) < 0) return false; if (vshCommandOptBool(cmd, "paused")) -- 2.34.1

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- docs/manpages/virsh.rst | 6 +++++- tools/virsh-domain.c | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index e28927ed6c..82a759a636 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -7881,7 +7881,8 @@ qemu-monitor-command :: - qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } command... + qemu-monitor-command domain { [--hmp] | [--pretty] [--return-value] } + [--pass-fds N,M,...] command... Send an arbitrary monitor command *command* to domain *domain* through the QEMU monitor. The results of the command will be printed on stdout. @@ -7914,6 +7915,9 @@ extracted rather than passing through the full reply from QEMU. If *--hmp* is passed, the command is considered to be a human monitor command and libvirt will automatically convert it into QMP and convert the result back. +If *--pass-fds* is specified, the argument is a comma separated list +of open file descriptors which should be passed on to qemu along with the +command. qemu-agent-command ------------------ diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 0bf4bb8dad..6cd031f8c3 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -9703,6 +9703,11 @@ static const vshCmdOptDef opts_qemu_monitor_command[] = { .type = VSH_OT_BOOL, .help = N_("extract the value of the 'return' key from the returned string") }, + {.name = "pass-fds", + .type = VSH_OT_STRING, + .completer = virshCompleteEmpty, + .help = N_("pass file descriptors N,M,... along with the command") + }, {.name = "cmd", .type = VSH_OT_ARGV, .flags = VSH_OFLAG_REQ, @@ -9801,6 +9806,8 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) bool returnval = vshCommandOptBool(cmd, "return-value"); virJSONValue *formatjson; g_autofree char *jsonstr = NULL; + g_autofree int *fds = NULL; + size_t nfds = 0; VSH_EXCLUSIVE_OPTIONS("hmp", "pretty"); VSH_EXCLUSIVE_OPTIONS("hmp", "return-value"); @@ -9820,9 +9827,18 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *cmd) return NULL; } - if (virDomainQemuMonitorCommand(dom, monitor_cmd, &result, flags) < 0) + if (virshFetchPassFdsList(ctl, cmd, &nfds, &fds) < 0) return false; + if (fds) { + if (virDomainQemuMonitorCommandWithFiles(dom, monitor_cmd, nfds, fds, + &result, flags) < 0) + return false; + } else { + if (virDomainQemuMonitorCommand(dom, monitor_cmd, &result, flags) < 0) + return false; + } + if (returnval || pretty) { resultjson = virJSONValueFromString(result); -- 2.34.1

Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- src/qemu/qemu_driver.c | 34 ++++++++++++++++++++++++++++++---- src/qemu/qemu_monitor.c | 7 ++++--- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 ++++-- src/qemu/qemu_monitor_json.h | 2 ++ src/qemu/qemu_monitor_text.c | 8 ++++---- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 698f57f00e..d10d1556a7 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -13897,21 +13897,36 @@ qemuDomainBackupGetXMLDesc(virDomainPtr domain, } -static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, - char **result, unsigned int flags) +static int +qemuDomainQemuMonitorCommandWithFiles(virDomainPtr domain, + const char *cmd, + unsigned int nfds, + int *fds, + char **result, + unsigned int flags) { virQEMUDriver *driver = domain->conn->privateData; virDomainObj *vm = NULL; int ret = -1; qemuDomainObjPrivate *priv; bool hmp; + int fd = -1; virCheckFlags(VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP, -1); + if (nfds > 1) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("at most 1 fd can be passed to qemu along with a command")); + return -1; + } + + if (nfds == 1) + fd = fds[0]; + if (!(vm = qemuDomainObjFromDomain(domain))) goto cleanup; - if (virDomainQemuMonitorCommandEnsureACL(domain->conn, vm->def) < 0) + if (virDomainQemuMonitorCommandWithFilesEnsureACL(domain->conn, vm->def) < 0) goto cleanup; if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0) @@ -13927,7 +13942,7 @@ static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, hmp = !!(flags & VIR_DOMAIN_QEMU_MONITOR_COMMAND_HMP); qemuDomainObjEnterMonitor(driver, vm); - ret = qemuMonitorArbitraryCommand(priv->mon, cmd, result, hmp); + ret = qemuMonitorArbitraryCommand(priv->mon, cmd, fd, result, hmp); qemuDomainObjExitMonitor(driver, vm); endjob: @@ -13939,6 +13954,16 @@ static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd, } +static int +qemuDomainQemuMonitorCommand(virDomainPtr domain, + const char *cmd, + char **result, + unsigned int flags) +{ + return qemuDomainQemuMonitorCommandWithFiles(domain, cmd, 0, NULL, result, flags); +} + + static int qemuDomainOpenConsole(virDomainPtr dom, const char *dev_name, @@ -20849,6 +20874,7 @@ static virHypervisorDriver qemuHypervisorDriver = { .domainRevertToSnapshot = qemuDomainRevertToSnapshot, /* 0.8.0 */ .domainSnapshotDelete = qemuDomainSnapshotDelete, /* 0.8.0 */ .domainQemuMonitorCommand = qemuDomainQemuMonitorCommand, /* 0.8.3 */ + .domainQemuMonitorCommandWithFiles = qemuDomainQemuMonitorCommandWithFiles, /* 8.1.0 */ .domainQemuAttach = NULL, /* 0.9.4 - 5.5.0 */ .domainQemuAgentCommand = qemuDomainQemuAgentCommand, /* 0.10.0 */ .connectDomainQemuMonitorEventRegister = qemuConnectDomainQemuMonitorEventRegister, /* 1.2.3 */ diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index babf9e62fb..6359a05155 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -3104,17 +3104,18 @@ qemuMonitorDrivePivot(qemuMonitor *mon, int qemuMonitorArbitraryCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply, bool hmp) { - VIR_DEBUG("cmd=%s, reply=%p, hmp=%d", cmd, reply, hmp); + VIR_DEBUG("cmd=%s, fd=%d, reply=%p, hmp=%d", cmd, fd, reply, hmp); QEMU_CHECK_MONITOR(mon); if (hmp) - return qemuMonitorJSONHumanCommand(mon, cmd, reply); + return qemuMonitorJSONHumanCommand(mon, cmd, fd, reply); else - return qemuMonitorJSONArbitraryCommand(mon, cmd, reply); + return qemuMonitorJSONArbitraryCommand(mon, cmd, fd, reply); } diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 9b2e4e1421..b7ed8d9bae 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -1108,6 +1108,7 @@ char *qemuMonitorDiskNameLookup(qemuMonitor *mon, int qemuMonitorArbitraryCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply, bool hmp); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index b0b513683b..9ab07bb29a 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -1461,6 +1461,7 @@ qemuMonitorJSONHandleGuestCrashloaded(qemuMonitor *mon, int qemuMonitorJSONHumanCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str) { g_autoptr(virJSONValue) cmd = NULL; @@ -1472,7 +1473,7 @@ qemuMonitorJSONHumanCommand(qemuMonitor *mon, "s:command-line", cmd_str, NULL); - if (!cmd || qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + if (!cmd || qemuMonitorJSONCommandWithFd(mon, cmd, fd, &reply) < 0) return -1; if (qemuMonitorJSONHasError(reply, "CommandNotFound")) { @@ -4497,6 +4498,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitor *mon, int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str) { g_autoptr(virJSONValue) cmd = NULL; @@ -4505,7 +4507,7 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, if (!(cmd = virJSONValueFromString(cmd_str))) return -1; - if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONCommandWithFd(mon, cmd, fd, &reply) < 0) return -1; if (!(*reply_str = virJSONValueToString(reply, false))) diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index 64d9ebdaa3..c57f54e040 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -43,6 +43,7 @@ qemuMonitorJSONIOProcess(qemuMonitor *mon, int qemuMonitorJSONHumanCommand(qemuMonitor *mon, const char *cmd, + int fd, char **reply); int @@ -369,6 +370,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitor *mon, int qemuMonitorJSONArbitraryCommand(qemuMonitor *mon, const char *cmd_str, + int fd, char **reply_str); int diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c index 0ca7f5a470..86af1e4975 100644 --- a/src/qemu/qemu_monitor_text.c +++ b/src/qemu/qemu_monitor_text.c @@ -43,7 +43,7 @@ int qemuMonitorTextAddDrive(qemuMonitor *mon, * address required when attaching drives to a controller */ cmd = g_strdup_printf("drive_add dummy %s", drivestr); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply) < 0) return -1; if (strstr(reply, "unknown command:")) { @@ -93,7 +93,7 @@ int qemuMonitorTextDriveDel(qemuMonitor *mon, cmd = g_strdup_printf("drive_del %s", drivestr); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply) < 0) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply) < 0) return -1; if (strstr(reply, "unknown command:")) { @@ -124,7 +124,7 @@ qemuMonitorTextCreateSnapshot(qemuMonitor *mon, cmd = g_strdup_printf("savevm \"%s\"", name); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply)) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply)) return -1; if (strstr(reply, "Error while creating snapshot") || @@ -150,7 +150,7 @@ int qemuMonitorTextDeleteSnapshot(qemuMonitor *mon, const char *name) g_autofree char *reply = NULL; cmd = g_strdup_printf("delvm \"%s\"", name); - if (qemuMonitorJSONHumanCommand(mon, cmd, &reply)) + if (qemuMonitorJSONHumanCommand(mon, cmd, -1, &reply)) return -1; if (strstr(reply, "No block device supports snapshots")) { -- 2.34.1

On a Thursday in 2022, Peter Krempa wrote:
I needed to do some tests where I wanted to pass FDs to qemu to see how 'add-fd' and 'getfd' behave.
It may be useful for others who wish for any reason to pass FDs to QMP commands.
Peter Krempa (5): lib: Introduce 'virDomainQemuMonitorCommandWithFiles' cmdStartGetFDs: Modernize virsh-domain: Move and rename cmdStartGetFDs to virshFetchPassFdsList virsh: Implement support for virDomainQemuMonitorCommandWithFiles qemu: Implement qemuDomainQemuMonitorCommandWithFiles
docs/manpages/virsh.rst | 6 +- include/libvirt/libvirt-qemu.h | 6 ++ src/driver-hypervisor.h | 8 +++ src/libvirt-qemu.c | 71 ++++++++++++++++++ src/libvirt_qemu.syms | 5 ++ src/qemu/qemu_driver.c | 34 +++++++-- src/qemu/qemu_monitor.c | 7 +- src/qemu/qemu_monitor.h | 1 + src/qemu/qemu_monitor_json.c | 6 +- src/qemu/qemu_monitor_json.h | 2 + src/qemu/qemu_monitor_text.c | 8 +-- src/qemu_protocol-structs | 9 +++ src/remote/qemu_protocol.x | 20 +++++- src/remote/remote_daemon_dispatch.c | 42 +++++++++++ src/remote/remote_driver.c | 40 +++++++++++ tools/virsh-domain.c | 108 ++++++++++++++++------------ 16 files changed, 313 insertions(+), 60 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (3)
-
Ján Tomko
-
Michal Prívozník
-
Peter Krempa