qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>, Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PATCH 16/20] chardev: add udp support to qapi
Date: Thu, 14 Mar 2013 09:57:37 +0100	[thread overview]
Message-ID: <1363251462-31498-17-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1363251462-31498-1-git-send-email-kraxel@redhat.com>

This patch adds 'udp' support to qapi.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 include/qemu/sockets.h |    1 +
 qapi-schema.json       |   16 +++++++++++++++-
 qemu-char.c            |   44 ++++++++++++++++++++++++++------------------
 util/qemu-sockets.c    |   25 +++++++++++++++++++++++++
 4 files changed, 67 insertions(+), 19 deletions(-)

diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h
index c5cee4b..ae5c21c 100644
--- a/include/qemu/sockets.h
+++ b/include/qemu/sockets.h
@@ -71,6 +71,7 @@ SocketAddress *socket_parse(const char *str, Error **errp);
 int socket_connect(SocketAddress *addr, Error **errp,
                    NonBlockingConnectHandler *callback, void *opaque);
 int socket_listen(SocketAddress *addr, Error **errp);
+int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
 
 /* Old, ipv4 only bits.  Don't use for new code. */
 int parse_host_port(struct sockaddr_in *saddr, const char *str);
diff --git a/qapi-schema.json b/qapi-schema.json
index 8d371f1..fdaa9da 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -3166,7 +3166,7 @@
 ##
 # @ChardevSocket:
 #
-# Configuration info for socket chardevs.
+# Configuration info for (stream) socket chardevs.
 #
 # @addr: socket address to listen on (server=true)
 #        or connect to (server=false)
@@ -3185,6 +3185,19 @@
                                      '*telnet'  : 'bool' } }
 
 ##
+# @ChardevDgram:
+#
+# Configuration info for datagram socket chardevs.
+#
+# @remote: remote address
+# @local: #optional local address
+#
+# Since: 1.5
+##
+{ 'type': 'ChardevDgram', 'data': { 'remote' : 'SocketAddress',
+                                    '*local' : 'SocketAddress' } }
+
+##
 # @ChardevMux:
 #
 # Configuration info for mux chardevs.
@@ -3272,6 +3285,7 @@
                                        'parallel': 'ChardevHostdev',
                                        'pipe'   : 'ChardevHostdev',
                                        'socket' : 'ChardevSocket',
+                                       'dgram'  : 'ChardevDgram',
                                        'pty'    : 'ChardevDummy',
                                        'null'   : 'ChardevDummy',
                                        'mux'    : 'ChardevMux',
diff --git a/qemu-char.c b/qemu-char.c
index eb2045a..7e13757 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2261,21 +2261,14 @@ static void udp_chr_close(CharDriverState *chr)
     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
 }
 
-static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
+static CharDriverState *qemu_chr_open_udp_fd(int fd)
 {
     CharDriverState *chr = NULL;
     NetCharDriver *s = NULL;
-    Error *local_err = NULL;
-    int fd = -1;
 
     chr = g_malloc0(sizeof(CharDriverState));
     s = g_malloc0(sizeof(NetCharDriver));
 
-    fd = inet_dgram_opts(opts, &local_err);
-    if (fd < 0) {
-        goto return_err;
-    }
-
     s->fd = fd;
     s->chan = io_channel_from_socket(s->fd);
     s->bufcnt = 0;
@@ -2285,18 +2278,18 @@ static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
     chr->chr_update_read_handler = udp_chr_update_read_handler;
     chr->chr_close = udp_chr_close;
     return chr;
+}
 
-return_err:
-    if (local_err) {
-        qerror_report_err(local_err);
-        error_free(local_err);
-    }
-    g_free(chr);
-    g_free(s);
-    if (fd >= 0) {
-        closesocket(fd);
+static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
+{
+    Error *local_err = NULL;
+    int fd = -1;
+
+    fd = inet_dgram_opts(opts, &local_err);
+    if (fd < 0) {
+        return NULL;
     }
-    return NULL;
+    return qemu_chr_open_udp_fd(fd);
 }
 
 /***********************************************************/
@@ -3679,6 +3672,18 @@ static CharDriverState *qmp_chardev_open_socket(ChardevSocket *sock,
                                    is_telnet, is_waitconnect, errp);
 }
 
+static CharDriverState *qmp_chardev_open_dgram(ChardevDgram *dgram,
+                                               Error **errp)
+{
+    int fd;
+
+    fd = socket_dgram(dgram->remote, dgram->local, errp);
+    if (error_is_set(errp)) {
+        return NULL;
+    }
+    return qemu_chr_open_udp_fd(fd);
+}
+
 ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
                                Error **errp)
 {
@@ -3708,6 +3713,9 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     case CHARDEV_BACKEND_KIND_SOCKET:
         chr = qmp_chardev_open_socket(backend->socket, errp);
         break;
+    case CHARDEV_BACKEND_KIND_DGRAM:
+        chr = qmp_chardev_open_dgram(backend->dgram, errp);
+        break;
 #ifdef HAVE_CHARDEV_TTY
     case CHARDEV_BACKEND_KIND_PTY:
         chr = qemu_chr_open_pty(id, ret);
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 3f12296..83e4e08 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -949,6 +949,31 @@ int socket_listen(SocketAddress *addr, Error **errp)
     return fd;
 }
 
+int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
+{
+    QemuOpts *opts;
+    int fd;
+
+    opts = qemu_opts_create_nofail(&dummy_opts);
+    switch (remote->kind) {
+    case SOCKET_ADDRESS_KIND_INET:
+        qemu_opt_set(opts, "host", remote->inet->host);
+        qemu_opt_set(opts, "port", remote->inet->port);
+        if (local) {
+            qemu_opt_set(opts, "localaddr", local->inet->host);
+            qemu_opt_set(opts, "localport", local->inet->port);
+        }
+        fd = inet_dgram_opts(opts, errp);
+        break;
+
+    default:
+        error_setg(errp, "socket type unsupported for datagram");
+        return -1;
+    }
+    qemu_opts_del(opts);
+    return fd;
+}
+
 #ifdef _WIN32
 static void socket_cleanup(void)
 {
-- 
1.7.9.7

  parent reply	other threads:[~2013-03-14  8:58 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-14  8:57 [Qemu-devel] [PULL v4 00/20] chardev: qapi conversion continued Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 01/20] chardev: add support for qapi-based chardev initialization Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 02/20] chardev: add mux chardev support to qapi Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 03/20] chardev: switch null init " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 04/20] chardev: add msmouse support " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 05/20] chardev: add braille " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 06/20] chardev: switch file init " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 07/20] chardev: add stdio support " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 08/20] chardev: switch serial/tty init " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 09/20] chardev: switch parallel " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 10/20] chardev: switch pty " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 11/20] chardev: add console support " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 12/20] chardev: add pipe " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 13/20] chardev: add spice " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 14/20] chardev: add vc " Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 15/20] chardev: add memory (ringbuf) " Gerd Hoffmann
2013-03-14  8:57 ` Gerd Hoffmann [this message]
2013-03-14  8:57 ` [Qemu-devel] [PATCH 17/20] Revert "hmp: Disable chardev-add and chardev-remove" Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 18/20] qemu-char.c: fix waiting for telnet connection message Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 19/20] spice-qemu-char: Fix name parameter issues after qapi-ifying Gerd Hoffmann
2013-03-14  8:57 ` [Qemu-devel] [PATCH 20/20] spice-qemu-char: Remove dead debugging code Gerd Hoffmann
2013-03-14 14:27 ` [Qemu-devel] [PULL v4 00/20] chardev: qapi conversion continued Eric Blake
2013-03-14 19:49   ` Anthony Liguori

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=1363251462-31498-17-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=aliguori@us.ibm.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).