qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, armbru@redhat.com, dgilbert@redhat.com,
	pbonzini@redhat.com, marcandre.lureau@redhat.com
Subject: [PATCH 05/13] char-socket: Implement compat code for CLI QAPIfication
Date: Thu, 12 Nov 2020 18:58:57 +0100	[thread overview]
Message-ID: <20201112175905.404472-6-kwolf@redhat.com> (raw)
In-Reply-To: <20201112175905.404472-1-kwolf@redhat.com>

Socket backends have a few differences between CLI and QMP. This adds
QAPI aliases and a .translate_legacy_options() implementation that
converts CLI inputs to a form that's usable for a QAPIfied --chardev.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qapi/char.json         |  3 ++-
 qapi/sockets.json      |  6 ++++-
 include/chardev/char.h |  1 +
 chardev/char-socket.c  | 53 ++++++++++++++++++++++++++++++++++++++++++
 chardev/char.c         | 10 +++++++-
 5 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/qapi/char.json b/qapi/char.json
index 91c0dbfa1e..1930e90e95 100644
--- a/qapi/char.json
+++ b/qapi/char.json
@@ -287,7 +287,8 @@
             '*tn3270': 'bool',
             '*websocket': 'bool',
             '*reconnect': 'int' },
-  'base': 'ChardevCommon' }
+  'base': 'ChardevCommon',
+  'aliases': [ { 'source': ['addr'] } ] }
 
 ##
 # @ChardevUdp:
diff --git a/qapi/sockets.json b/qapi/sockets.json
index 2e83452797..8c61787311 100644
--- a/qapi/sockets.json
+++ b/qapi/sockets.json
@@ -125,7 +125,11 @@
     'inet': 'InetSocketAddress',
     'unix': 'UnixSocketAddress',
     'vsock': 'VsockSocketAddress',
-    'fd': 'String' } }
+    'fd': 'String' },
+  'aliases': [
+    {'source': ['data']},
+    {'alias': 'fd', 'source': ['data', 'str']}
+  ]}
 
 ##
 # @SocketAddressType:
diff --git a/include/chardev/char.h b/include/chardev/char.h
index 7795e17ca5..c0944f5828 100644
--- a/include/chardev/char.h
+++ b/include/chardev/char.h
@@ -275,6 +275,7 @@ struct ChardevClass {
 
     bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
     void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
+    void (*translate_legacy_options)(QDict *args);
 
     void (*open)(Chardev *chr, ChardevBackend *backend,
                  bool *be_opened, Error **errp);
diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 213a4c8dd0..6bf916a3e4 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -34,6 +34,7 @@
 #include "qapi/error.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/qapi-visit-sockets.h"
+#include "qapi/qmp/qdict.h"
 
 #include "chardev/char-io.h"
 #include "qom/object.h"
@@ -1484,6 +1485,57 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
     sock->addr = addr;
 }
 
+static void qemu_chr_translate_socket(QDict *args)
+{
+    const char *path = qdict_get_try_str(args, "path");
+    const char *host = qdict_get_try_str(args, "host");
+    const char *fd = qdict_get_try_str(args, "fd");
+    const char *delay = qdict_get_try_str(args, "delay");
+    const char *server = qdict_get_try_str(args, "server");
+    const char *wait = qdict_get_try_str(args, "wait");
+    QDict *addr;
+
+    if ((!!path + !!fd + !!host) != 1) {
+        return;
+    }
+
+    /* If "addr" is not present, automatically set the type */
+    if (!qdict_haskey(args, "addr")) {
+        addr = qdict_new();
+        qdict_put(args, "addr", addr);
+
+        if (path) {
+            qdict_put_str(addr, "type", "unix");
+        } else if (host) {
+            qdict_put_str(addr, "type", "inet");
+        } else if (fd) {
+            qdict_put_str(addr, "type", "fd");
+        }
+    }
+
+    /* "delay" is translated into "nodelay" */
+    if (delay && !qdict_haskey(args, "nodelay")) {
+        if (!strcmp(delay, "on")) {
+            qdict_put_str(args, "nodelay", "off");
+            qdict_del(args, "delay");
+        } else if (!strcmp(delay, "off")) {
+            qdict_put_str(args, "nodelay", "on");
+            qdict_del(args, "delay");
+        }
+    }
+
+    /* "server=off" is the CLI default */
+    if (!server) {
+        server = "off";
+        qdict_put_str(args, "server", server);
+    }
+
+    /* "wait=on" is the default if "server=on" */
+    if (!wait && !strcmp(server, "on")) {
+        qdict_put_str(args, "wait", "on");
+    }
+}
+
 static void
 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
                      void *opaque, Error **errp)
@@ -1506,6 +1558,7 @@ static void char_socket_class_init(ObjectClass *oc, void *data)
     ChardevClass *cc = CHARDEV_CLASS(oc);
 
     cc->parse = qemu_chr_parse_socket;
+    cc->translate_legacy_options = qemu_chr_translate_socket;
     cc->open = qmp_chardev_open_socket;
     cc->chr_wait_connected = tcp_chr_wait_connected;
     cc->chr_write = tcp_chr_write;
diff --git a/chardev/char.c b/chardev/char.c
index 40c3f02ec9..91b44e53b6 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -720,6 +720,7 @@ out:
 
 void qemu_chr_translate_legacy_options(QDict *args)
 {
+    const ChardevClass *cc;
     const char *name;
 
     /* "backend" instead of "type" enables legacy CLI compatibility */
@@ -730,12 +731,19 @@ void qemu_chr_translate_legacy_options(QDict *args)
 
     name = chardev_alias_translate(name);
     qdict_put_str(args, "type", name);
+
+    cc = char_get_class(name, NULL);
+    if (cc != NULL && cc->translate_legacy_options) {
+        QDict *backend_data = qdict_get_qdict(args, "data") ?: args;
+        cc->translate_legacy_options(backend_data);
+    }
+
+    /* name may refer to a QDict entry, so delete it only now */
     qdict_del(args, "backend");
 
     /*
      * TODO:
      * All backend types: "mux"
-     * socket: "addr.type", "delay", "server", "wait", "fd"
      * udp: defaults for "host"/"localaddr"/"localport"
      */
 }
-- 
2.28.0



  parent reply	other threads:[~2020-11-12 18:05 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-12 17:58 [PATCH 00/13] char: QAPIfy the command line parsing Kevin Wolf
2020-11-12 17:58 ` [PATCH 01/13] char: Factor out qemu_chr_print_types() Kevin Wolf
2020-11-12 17:58 ` [PATCH 02/13] char: Add ChardevOptions and qemu_chr_new_cli() Kevin Wolf
2020-11-12 17:58 ` [PATCH 03/13] char: Some QAPI aliases for CLI compatibility Kevin Wolf
2020-11-12 17:58 ` [PATCH 04/13] char: Add qemu_chr_translate_legacy_options() Kevin Wolf
2020-11-12 17:58 ` Kevin Wolf [this message]
2020-11-12 17:58 ` [PATCH 06/13] char-udp: Implement compat code for CLI QAPIfication Kevin Wolf
2020-11-12 17:58 ` [PATCH 07/13] char: Add qemu_chr_parse_cli_dict/str() Kevin Wolf
2020-11-12 17:59 ` [PATCH 08/13] char: Add mux option to ChardevOptions Kevin Wolf
2020-11-13 11:50   ` Paolo Bonzini
2020-11-13 13:20     ` Kevin Wolf
2020-11-13 14:16       ` Paolo Bonzini
2020-11-12 17:59 ` [PATCH 09/13] qemu-storage-daemon: QAPIfy --chardev Kevin Wolf
2020-11-12 17:59 ` [PATCH 10/13] char: Implement qemu_chr_new_from_opts() in terms of QAPI Kevin Wolf
2020-11-12 17:59 ` [PATCH 11/13] hmp/char: Use qemu_chr_parse_cli_str() for chardev-change Kevin Wolf
2020-11-13 18:44   ` Dr. David Alan Gilbert
2020-11-12 17:59 ` [PATCH 12/13] char: Remove qemu_chr_parse_opts() Kevin Wolf
2020-11-12 17:59 ` [PATCH 13/13] char: Remove ChardevClass.parse Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201112175905.404472-6-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).