On a Wednesday in 2024, Peter Krempa wrote:
Similarly to how we approach the generators for
-device/-object/-blockdev/-netdev rewrite the generator of -chardev to
be unified with the generator for the monitor.
Unfortunately with -chardev it will be a bit more quirky when compared
to the others as the generator itself will need to know whether it
generates command line output or not as few field names change and data
s/few/a few/
is nested differently.
This first step adds the generator and uses it only for command line
generation. This was possible to achieve without changing any of the
output in tests.
In further patches the same generator will then be used also in the
monitor code replacing the both.
s/the//
As basis for the generator I took the monitor code but modified it to
have the same field order as the commandline code and extended it
further to support all backend types, even those which are not
hotpluggable.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/meson.build | 1 +
src/qemu/qemu_chardev.c | 488 ++++++++++++++++++++++++++++++++++++++++
src/qemu/qemu_chardev.h | 22 ++
src/qemu/qemu_command.c | 202 +----------------
4 files changed, 513 insertions(+), 200 deletions(-)
create mode 100644 src/qemu/qemu_chardev.c
create mode 100644 src/qemu/qemu_chardev.h
diff --git a/src/qemu/meson.build b/src/qemu/meson.build
index 57356451e4..1d904bbc68 100644
--- a/src/qemu/meson.build
+++ b/src/qemu/meson.build
@@ -6,6 +6,7 @@ qemu_driver_sources = [
'qemu_blockjob.c',
'qemu_capabilities.c',
'qemu_cgroup.c',
+ 'qemu_chardev.c',
'qemu_checkpoint.c',
'qemu_command.c',
'qemu_conf.c',
diff --git a/src/qemu/qemu_chardev.c b/src/qemu/qemu_chardev.c
new file mode 100644
index 0000000000..cc6ba33f51
--- /dev/null
+++ b/src/qemu/qemu_chardev.c
@@ -0,0 +1,488 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <config.h>
+
+#include "qemu_capabilities.h"
+#include "qemu_domain.h"
+#include "qemu_fd.h"
+
+#include "vircommand.h"
+#include "virlog.h"
+#include "virqemu.h"
+
+#include "domain_conf.h"
+
+#include "qemu_chardev.h"
+
+#define VIR_FROM_THIS VIR_FROM_QEMU
+
+VIR_LOG_INIT("qemu.qemu_command");
+
+static int
+qemuChardevBackendAddSocketAddressInet(virJSONValue **backendData,
+ const char *backendFieldName,
+ bool commandline,
+ const char *commandlinePrefix,
+ const char *host,
+ const char *port)
+{
+ if (commandline) {
+ g_autofree char *hostField = NULL;
+ g_autofree char *portField = NULL;
+
+ if (!commandlinePrefix) {
+ hostField = g_strdup("s:host");
+ portField = g_strdup("s:port");
+ } else {
+ hostField = g_strdup_printf("s:%saddr", commandlinePrefix);
+ portField = g_strdup_printf("s:%sport", commandlinePrefix);
+ }
+
+ if (virJSONValueObjectAdd(backendData,
+ hostField, host,
+ portField, port,
+ NULL) < 0)
+ return -1;
+ } else {
+ g_autoptr(virJSONValue) addr = NULL;
+ g_autoptr(virJSONValue) data = NULL;
+ g_autofree char *datafiled = g_strdup_printf("a:%s", backendFieldName);
s/filed/field/
+
+ if (virJSONValueObjectAdd(&data,
+ "s:host", host,
+ "s:port", port,
+ NULL) < 0)
+ return -1;
+
+ if (virJSONValueObjectAdd(&addr,
+ "s:type", "inet",
+ "a:data", &data,
+ NULL) < 0)
+ return -1;
+
+ if (virJSONValueObjectAdd(backendData,
+ datafiled, &addr,
+ NULL) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+
+static int
+qemuChardevBackendAddSocketAddressFD(virJSONValue **backendData,
+ const char *backendFieldName,
+ bool commandline,
+ const char *fdname)
+{
+ if (commandline) {
+ if (virJSONValueObjectAdd(backendData,
+ "s:fd", fdname,
+ NULL) < 0)
+ return -1;
+ } else {
+ g_autoptr(virJSONValue) addr = NULL;
+ g_autoptr(virJSONValue) data = NULL;
+ g_autofree char *datafiled = g_strdup_printf("a:%s", backendFieldName);
s/filed/field/
+
+ if (virJSONValueObjectAdd(&data, "s:str", fdname, NULL) < 0)
+ return -1;
+
+ if (virJSONValueObjectAdd(&addr,
+ "s:type", "fd",
+ "a:data", &data, NULL) < 0)
+ return -1;
+
+ if (virJSONValueObjectAdd(backendData,
+ datafiled, &addr,
+ NULL) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
Reviewed-by: Ján Tomko <jtomko(a)redhat.com>
Jano