All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony PERARD <anthony.perard@citrix.com>
To: xen-devel@lists.xenproject.org
Cc: Anthony PERARD <anthony.perard@citrix.com>,
	Wei Liu <wei.liu2@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH v3 24/31] libxl_qmp_ev: Respond to QMP greeting
Date: Fri, 1 Jun 2018 15:37:13 +0100	[thread overview]
Message-ID: <20180601143720.24637-25-anthony.perard@citrix.com> (raw)
In-Reply-To: <20180601143720.24637-1-anthony.perard@citrix.com>

Slight change in the infrastructure to allow to send a buffer before any
command that would already been prepared.

qmp_capabilities needs to be the first command sent.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---
 tools/libxl/libxl_qmp.c | 87 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 82 insertions(+), 5 deletions(-)

diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index db07c1822a..adf466e4c4 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -1387,6 +1387,8 @@ struct libxl__ev_qmp_state {
     unsigned int last_id_used;
     /* Indicate that QEMU is ready to respond to command. */
     bool ready;
+    /* Allow to commands while !ready, to respond to QMP greeting. */
+    int priority_send;
     LIBXL_TAILQ_HEAD(libxl__qmp_tx_bufs, libxl__qmp_tx_buf) tx_buf;
 
     LIBXL_TAILQ_HEAD(libxl__ev_qmps, libxl__ev_qmp) qmp_events;
@@ -1398,7 +1400,8 @@ static int ev_qmp_queue_command(libxl__gc *gc,
                                 libxl__ev_qmp_state *qmp,
                                 const char *cmd,
                                 const libxl__json_object *args,
-                                libxl__carefd *efd)
+                                libxl__carefd *efd,
+                                bool high_priority)
 {
     char *buf = NULL;
     size_t len;
@@ -1414,12 +1417,78 @@ static int ev_qmp_queue_command(libxl__gc *gc,
     out->buf = buf;
     out->len = len;
     out->efd = efd;
-    LIBXL_TAILQ_INSERT_TAIL(&qmp->tx_buf, out, entry);
+    if (high_priority) {
+        /* qmp_capabilities cmd need to be send out first */
+        LIBXL_TAILQ_INSERT_HEAD(&qmp->tx_buf, out, entry);
+        qmp->priority_send += 1;
+    } else {
+        LIBXL_TAILQ_INSERT_TAIL(&qmp->tx_buf, out, entry);
+    }
     libxl__ev_fd_modify(gc, &qmp->efd, qmp->efd.events | POLLOUT);
 
     return 0;
 }
 
+/* QMP greeting response */
+typedef struct {
+    libxl__ev_qmp ev;
+    libxl__ev_qmp_state *qmp;
+} ev_qmp_greeting_state;
+static void ev_qmp_callback_capabilities(libxl__egc *egc,
+                                        libxl__ev_qmp *ev,
+                                        const libxl__json_object *o,
+                                        libxl__qmp_error_class error)
+{
+    EGC_GC;
+    ev_qmp_greeting_state *qegs = CONTAINER_OF(ev, *qegs, ev);
+    libxl__ev_qmp_state *qmp = qegs->qmp;
+
+    CTX_LOCK;
+    qmp->ready = true;
+
+    /* Allow potential command to be sent */
+    libxl__ev_fd_modify(gc, &qmp->efd, qmp->efd.events | POLLOUT);
+
+    CTX_UNLOCK;
+
+    libxl__ev_qmp_deregister(gc, ev);
+
+    free(qegs);
+}
+
+static int ev_qmp_callback_greeting(libxl__egc *egc,
+                                    libxl__ev_qmp_state *qmp)
+{
+    EGC_GC;
+    ev_qmp_greeting_state *qegs;
+    int rc;
+
+    /* Will be freed in callback */
+    qegs = libxl__malloc(NOGC, sizeof(*qegs));
+
+    libxl__ev_qmp_init(&qegs->ev);
+
+    /* Do part of libxl__ev_qmp_register as this is the only time where the
+     * argument high_priority is true. */
+
+    qegs->qmp = qmp;
+    qegs->ev.domid = qmp->domid;
+    qegs->ev.callback = ev_qmp_callback_capabilities;
+    rc = ev_qmp_queue_command(gc, qmp, "qmp_capabilities", NULL, NULL, true);
+    if (rc)
+        goto out;
+    qegs->ev.id = qmp->last_id_used;
+
+    CTX_LOCK;
+    LIBXL_TAILQ_INSERT_TAIL(&qmp->qmp_events, &qegs->ev, entry);
+    CTX_UNLOCK;
+
+out:
+    if (rc)
+        free(qegs);
+    return rc;
+}
+
 /*
  * Handle messages received from QMP server
  */
@@ -1501,7 +1570,8 @@ static void ev_qmp_handle_message(libxl__egc *egc,
 
     switch (type) {
     case LIBXL__QMP_MESSAGE_TYPE_QMP:
-        /* greeting message */
+        /* On the greeting message from the server, call qmp_capabilities */
+        ev_qmp_callback_greeting(egc, qmp);
         return;
     case LIBXL__QMP_MESSAGE_TYPE_RETURN:
     case LIBXL__QMP_MESSAGE_TYPE_ERROR:
@@ -1673,7 +1743,9 @@ static int ev_qmp_callback_writable(libxl__egc *egc,
     libxl__qmp_tx_buf *buf;
     int rc;
 
-    if (!qmp->ready) {
+    /* Don't send anything until connected, unless there is the response to the
+     * greeting message */
+    if (!qmp->ready && !qmp->priority_send) {
         return 0;
     }
 
@@ -1701,6 +1773,9 @@ static int ev_qmp_callback_writable(libxl__egc *egc,
     free(buf->buf);
     free(buf);
 
+    if (!qmp->ready && qmp->priority_send)
+        qmp->priority_send -= 1;
+
 out:
     return 1;
 }
@@ -1771,6 +1846,7 @@ static void libxl__ev_qmp_state_init(libxl__ev_qmp_state *qmp)
     LIBXL_TAILQ_INIT(&qmp->bufs);
     qmp->last_id_used = 0;
     qmp->ready = false;
+    qmp->priority_send = 0;
     LIBXL_TAILQ_INIT(&qmp->tx_buf);
     LIBXL_TAILQ_INIT(&qmp->qmp_events);
 }
@@ -1878,6 +1954,7 @@ void libxl__ev_qmp_stop(libxl__gc *gc, libxl__ev_qmp_state *qmp)
     LIBXL_TAILQ_INIT(&qmp->qmp_events);
 
     qmp->ready = false;
+    qmp->priority_send = 0;
 
     libxl__ev_fd_deregister(gc, &qmp->efd);
     libxl__carefd_close(qmp->cfd);
@@ -1917,7 +1994,7 @@ int libxl__ev_qmp_register(libxl__gc *gc, libxl__ev_qmp *ev,
         goto out;
     }
 
-    rc = ev_qmp_queue_command(gc, qmp, cmd, args, ev->efd);
+    rc = ev_qmp_queue_command(gc, qmp, cmd, args, ev->efd, false);
     if (rc)
         goto out;
 
-- 
Anthony PERARD


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-06-01 14:58 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01 14:36 [PATCH v3 00/31] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 01/31] libxl_event: Fix DEBUG prints Anthony PERARD
2018-06-27 14:19   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 02/31] libxl_qmp: Documentation of the logic of the QMP client Anthony PERARD
2018-06-27 14:20   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 03/31] libxl_qmp: Fix use of DEBUG_RECEIVED Anthony PERARD
2018-06-27 14:20   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 04/31] libxl_json: fix build with DEBUG_ANSWER Anthony PERARD
2018-06-27 14:22   ` Ian Jackson
2018-07-17 14:35     ` Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 05/31] libxl_qmp: Move the buffer realloc to the same scope level as read Anthony PERARD
2018-06-27 14:25   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 06/31] libxl_qmp: Add a warning to not trust QEMU Anthony PERARD
2018-06-27 14:25   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 07/31] libxl_qmp: Learned to send FD through QMP to QEMU Anthony PERARD
2018-06-27 14:26   ` Ian Jackson
2018-06-27 16:50     ` Anthony PERARD
2018-06-28  9:56       ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 08/31] libxl_qmp: Have QEMU save its state to a file descriptor Anthony PERARD
2018-06-27 14:29   ` Ian Jackson
2018-06-01 14:36 ` [PATCH v3 09/31] libxl_qmp: Move struct sockaddr_un variable to qmp_open() Anthony PERARD
2018-06-27 14:31   ` Ian Jackson
2018-06-27 16:54     ` Anthony PERARD
2018-06-01 14:36 ` [PATCH v3 10/31] libxl_qmp: Move buffers to the stack of qmp_next Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-27 16:58     ` Anthony PERARD
2018-06-28  9:57       ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 11/31] libxl_qmp: Remove unused yajl_ctx form handler Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 12/31] libxl_json: constify libxl__json_object_to_yajl_gen arguments Anthony PERARD
2018-06-27 14:32   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 13/31] libxl_qmp: Separate QMP message generation from qmp_send_prepare Anthony PERARD
2018-06-27 14:45   ` Ian Jackson
2018-06-27 17:12     ` Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 14/31] libxl_qmp_ev: Introduce libxl__ev_qmp_start() to connect to QMP Anthony PERARD
2018-06-27 15:07   ` Ian Jackson
2018-06-28 11:28     ` Anthony PERARD
2018-06-28 11:44       ` Ian Jackson
2018-06-28 13:01         ` Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 15/31] libxl_qmp_ev: Implement fd callback and read data Anthony PERARD
2018-06-27 15:10   ` Ian Jackson
2018-06-28 11:33     ` Anthony PERARD
2018-06-28 11:37       ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 16/31] libxl_json: Allow partial parsing Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 17/31] libxl_json: Enable yajl_allow_trailing_garbage Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 18/31] libxl_json: libxl__json_object_to_json Anthony PERARD
2018-06-27 15:51   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 19/31] libxl_qmp_ev: Parse JSON input from QMP Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 20/31] libxl_qmp: Introduce libxl__ev_qmp functions Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 21/31] libxl_qmp_ev: Handle write to socket Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 22/31] libxl_qmp: Simplify qmp_response_type() prototype Anthony PERARD
2018-06-27 16:03   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 23/31] libxl_qmp_ev: Handle messages from QEMU Anthony PERARD
2018-06-01 14:37 ` Anthony PERARD [this message]
2018-06-27 16:07   ` [PATCH v3 24/31] libxl_qmp_ev: Respond to QMP greeting Ian Jackson
2018-06-01 14:37 ` [PATCH v3 25/31] libxl_qmp_ev: Disconnect QMP when no more events Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 26/31] libxl_qmp: Disable beautify for QMP generated cmd Anthony PERARD
2018-06-27 16:07   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 27/31] libxl_qmp: Implement libxl__qmp_insert_cdrom_ev Anthony PERARD
2018-06-27 16:10   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 28/31] libxl_disk: Cut libxl_cdrom_insert into step Anthony PERARD
2018-06-01 14:37 ` [PATCH v3 29/31] libxl_disk: Have libxl_cdrom_insert use libxl__ev_qmp Anthony PERARD
2018-06-27 16:12   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 30/31] libxl_dm: Pre-open QMP socket for QEMU Anthony PERARD
2018-06-27 16:14   ` Ian Jackson
2018-06-01 14:37 ` [PATCH v3 31/31] libxl: QEMU startup sync based on QMP Anthony PERARD
2018-06-27 16:16   ` Ian Jackson
2018-07-26 14:59     ` Anthony PERARD
2018-06-01 14:47 ` [PATCH v3 00/31] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-07-03  9:47 ` [PATCH v3.1] libxl: Design of an async API to issue QMP commands to QEMU Anthony PERARD
2018-07-03 14:48   ` Ian Jackson
2018-07-04 11:11     ` Anthony PERARD
2018-07-12 16:36       ` Ian Jackson
2018-07-16 15:27         ` Anthony PERARD
2018-07-16 15:28           ` [PATCH v3.2] " Anthony PERARD
2018-07-16 16:33             ` Anthony PERARD
2018-07-16 17:04               ` [PATCH v3.2] libxl: Design of an async API to issue QMP commands to QEMU [and 1 more messages] Ian Jackson
2018-07-18 10:30                 ` Anthony PERARD

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=20180601143720.24637-25-anthony.perard@citrix.com \
    --to=anthony.perard@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.