xen-devel.lists.xenproject.org archive mirror
 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>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	Wei Liu <wei.liu2@citrix.com>
Subject: [PATCH v4 14/32] libxl_qmp: Implement fd callback and read data
Date: Fri, 27 Jul 2018 15:05:56 +0100	[thread overview]
Message-ID: <20180727140614.13256-15-anthony.perard@citrix.com> (raw)
In-Reply-To: <20180727140614.13256-1-anthony.perard@citrix.com>

First step into taking care of the input from QEMU's QMP socket. For
now, we read data and store them in a buffer.

Parsing of the data will be done in the following patches.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---

Notes:
    v4:
        remove use of a linked list of receive buffer, and use realloc instead.

 tools/libxl/libxl_internal.h |   9 ++++
 tools/libxl/libxl_qmp.c      | 101 +++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 90ac48a659..8c3625a243 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -438,6 +438,15 @@ struct libxl__ev_qmp {
     libxl__carefd *qmp_cfd;
     libxl__ev_fd qmp_efd;
     libxl__qmp_state qmp_state;
+
+    /* receive buffer, with:
+     * buf_size: current allocated size,
+     * buf_used: actual data in the buffer,
+     * buf_consumed: data already parsed.  */
+    char *rx_buf;
+    size_t buf_size;
+    size_t buf_used;
+    size_t buf_consumed;
 };
 
 
diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index 96a347dd3b..b0554df843 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -75,6 +75,12 @@
 #  define DEBUG_REPORT_RECEIVED(dom, buf, len) ((void)0)
 #endif
 
+#ifdef DEBUG_QMP_CLIENT
+#  define LOG_QMP(f, ...) LOGD(DEBUG, ev->domid, f, ##__VA_ARGS__)
+#else
+#  define LOG_QMP(f, ...)
+#endif
+
 /*
  * QMP types & constant
  */
@@ -1278,9 +1284,99 @@ int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
 
 /* ------------ Implementation of libxl__ev_qmp ---------------- */
 
+/*
+ * QMP FD callbacks
+ */
+
+static int qmp_ev_callback_readable(libxl__egc *egc, libxl__ev_qmp *ev, int fd)
+{
+    EGC_GC;
+    ssize_t r;
+
+    if (!ev->rx_buf) {
+        ev->rx_buf = libxl__malloc(NOGC, QMP_RECEIVE_BUFFER_SIZE);
+        ev->buf_size = QMP_RECEIVE_BUFFER_SIZE;
+        ev->buf_used = 0;
+        ev->buf_consumed = 0;
+    }
+
+    /* Check if last buffer still have space, or increase size */
+    /* The -1 is because there is always space for a NUL character */
+    if (ev->buf_used == ev->buf_size - 1) {
+        ev->buf_size += QMP_RECEIVE_BUFFER_SIZE;
+        ev->rx_buf = libxl__realloc(NOGC, ev->rx_buf, ev->buf_size);
+    }
+
+    for (;;) {
+        /* The -1 is because there is always space for a NUL character */
+        r = read(fd, ev->rx_buf + ev->buf_used,
+                 ev->buf_size - ev->buf_used - 1);
+        if (r < 0) {
+            if (errno == EINTR) continue;
+            assert(errno);
+            if (errno == EWOULDBLOCK) {
+                return 0;
+            }
+            LOGED(ERROR, ev->domid, "error reading QMP socket");
+            return ERROR_FAIL;
+        }
+        break;
+    }
+
+    if (r == 0) {
+        LOGD(ERROR, ev->domid, "No data read on QMP socket");
+        return 0;
+    }
+
+    LOG_QMP("received %ldB: '%.*s'", r, (int)r, ev->rx_buf + ev->buf_used);
+
+    ev->buf_used += r;
+    assert(ev->buf_used < ev->buf_size);
+
+    return 0;
+}
+
+static void qmp_ev_callback_error(libxl__egc *egc, libxl__ev_qmp *ev)
+{
+    EGC_GC;
+
+    LOGD(ERROR, ev->domid, "Error happend with the QMP connection to QEMU");
+
+    /* On error, deallocate all private ressources */
+    libxl__ev_qmp_dispose(gc, ev);
+}
+
 static void qmp_ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev_fd,
                                int fd, short events, short revents)
 {
+    EGC_GC;
+    int rc;
+
+    libxl__ev_qmp *ev = CONTAINER_OF(ev_fd, *ev, qmp_efd);
+
+    if (revents & (POLLHUP)) {
+        LOGD(DEBUG, ev->domid, "received POLLHUP from QMP socket");
+        qmp_ev_callback_error(egc, ev);
+        return;
+    }
+    if (revents & ~(POLLIN|POLLOUT)) {
+        LOGD(ERROR, ev->domid,
+             "unexpected poll event 0x%x on QMP socket (expected POLLIN "
+             "and/or POLLOUT)",
+            revents);
+        qmp_ev_callback_error(egc, ev);
+        return;
+    }
+
+    if (revents & POLLIN) {
+        rc = qmp_ev_callback_readable(egc, ev, fd);
+        if (rc)
+            goto out;
+    }
+out:
+    if (rc) {
+        qmp_ev_callback_error(egc, ev);
+    }
 }
 
 static int qmp_ev_connect(libxl__gc *gc, libxl__ev_qmp *ev)
@@ -1346,6 +1442,8 @@ void libxl__ev_qmp_init(libxl__ev_qmp *ev)
     ev->qmp_cfd = NULL;
     libxl__ev_fd_init(&ev->qmp_efd);
     ev->qmp_state = qmp_state_disconnected;
+
+    ev->rx_buf = NULL;
 }
 
 int libxl__ev_qmp_send(libxl__gc *gc, libxl__ev_qmp *ev,
@@ -1365,6 +1463,9 @@ void libxl__ev_qmp_dispose(libxl__gc *gc, libxl__ev_qmp *ev)
 {
     LOGD(DEBUG, ev->domid, " ev %p", ev);
 
+    free(ev->rx_buf);
+    ev->rx_buf = NULL;
+
     libxl__ev_fd_deregister(gc, &ev->qmp_efd);
     libxl__carefd_close(ev->qmp_cfd);
     ev->qmp_cfd = NULL;
-- 
Anthony PERARD


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

  parent reply	other threads:[~2018-07-27 14:28 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-27 14:05 [PATCH v4 00/32] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 01/32] libxl_event: Fix DEBUG prints Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 02/32] libxl_qmp: Documentation of the logic of the QMP client Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 03/32] libxl_qmp: Fix use of DEBUG_RECEIVED Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 04/32] libxl_json: fix build with DEBUG_ANSWER Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 05/32] libxl_qmp: Move the buffer realloc to the same scope level as read Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 06/32] libxl_qmp: Add a warning to not trust QEMU Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 07/32] libxl_qmp: Move struct sockaddr_un variable to qmp_open() Anthony PERARD
2018-08-02  8:26   ` Roger Pau Monné
2018-08-20 14:52   ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 08/32] libxl: Add libxl__prepare_sockaddr_un() helper Anthony PERARD
2018-08-02  8:36   ` Roger Pau Monné
2018-08-20 14:56   ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 09/32] libxl_qmp: Remove unused yajl_ctx from handler Anthony PERARD
2018-08-20 14:56   ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 10/32] libxl_json: constify libxl__json_object_to_yajl_gen arguments Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 11/32] libxl_dm: Add libxl__qemu_qmp_path() Anthony PERARD
2018-08-02  8:41   ` Roger Pau Monné
2018-08-20 14:57   ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 12/32] libxl: Design of an async API to issue QMP commands to QEMU Anthony PERARD
2018-08-02  9:01   ` [PATCH v4 12/32] libxl: Design of an async API to issue QMP commands to QEMUç Roger Pau Monné
2018-08-03 11:18     ` Anthony PERARD
2018-08-03 13:55       ` Roger Pau Monné
2018-08-03 14:45         ` Anthony PERARD
2018-08-03 15:30           ` Roger Pau Monné
2018-07-27 14:05 ` [PATCH v4 13/32] libxl_qmp: Connect to QMP socket Anthony PERARD
2018-08-02  9:35   ` Roger Pau Monné
2018-08-03 13:54     ` Anthony PERARD
2018-08-03 14:03       ` Roger Pau Monné
2018-08-21  8:22         ` Wei Liu
2018-07-27 14:05 ` Anthony PERARD [this message]
2018-08-02  9:56   ` [PATCH v4 14/32] libxl_qmp: Implement fd callback and read data Roger Pau Monné
2018-08-03 14:32     ` Anthony PERARD
2018-08-03 15:24       ` Roger Pau Monné
2018-08-06 15:01         ` Anthony PERARD
2018-07-27 14:05 ` [PATCH v4 15/32] libxl_json: Enable yajl_allow_trailing_garbage Anthony PERARD
2018-08-02 10:01   ` Roger Pau Monné
2018-08-21  8:26     ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 16/32] libxl_json: libxl__json_object_to_json Anthony PERARD
2018-08-02 10:10   ` Roger Pau Monné
2018-08-21  8:26   ` Wei Liu
2018-07-27 14:05 ` [PATCH v4 17/32] libxl_qmp: Parse JSON input from QMP Anthony PERARD
2018-08-02 10:25   ` Roger Pau Monné
2018-08-03 15:33     ` Anthony PERARD
2018-08-06 11:17       ` Roger Pau Monné
2018-07-27 14:06 ` [PATCH v4 18/32] libxl_qmp: Separate QMP message generation from qmp_send_prepare Anthony PERARD
2018-08-02 10:34   ` Roger Pau Monné
2018-08-03 15:43     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 19/32] libxl_qmp: Prepare the command to be sent Anthony PERARD
2018-08-02 10:41   ` Roger Pau Monné
2018-08-03 16:35     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 20/32] libxl_qmp: Handle write to QMP socket Anthony PERARD
2018-08-02 11:02   ` Roger Pau Monné
2018-08-03 16:50     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 21/32] libxl_qmp: Simplify qmp_response_type() prototype Anthony PERARD
2018-08-02 11:03   ` Roger Pau Monné
2018-08-21  8:53   ` Wei Liu
2018-07-27 14:06 ` [PATCH v4 22/32] libxl_qmp: Handle messages from QEMU Anthony PERARD
2018-08-02 11:17   ` Roger Pau Monné
2018-08-03 17:25     ` Anthony PERARD
2018-08-06 11:25       ` Roger Pau Monné
2018-08-06 13:14         ` Anthony PERARD
2018-08-06 15:03           ` Roger Pau Monné
2018-08-21  8:58       ` Wei Liu
2018-08-21 12:50         ` Anthony PERARD
2018-08-21 14:56           ` Ian Jackson
2018-07-27 14:06 ` [PATCH v4 23/32] libxl_qmp: Respond to QMP greeting Anthony PERARD
2018-08-02 11:26   ` Roger Pau Monné
2018-08-06 17:41     ` Anthony PERARD
2018-08-21  9:00   ` Wei Liu
2018-08-21 10:51     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 24/32] libxl_qmp: Disable beautify for QMP generated cmd Anthony PERARD
2018-08-21  9:00   ` Wei Liu
2018-07-27 14:06 ` [PATCH v4 25/32] libxl_exec: Add libxl__spawn_initiate_failure Anthony PERARD
2018-08-02 11:34   ` Roger Pau Monné
2018-08-06 15:58     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 26/32] libxl_dm: Pre-open QMP socket for QEMU Anthony PERARD
2018-08-02 15:00   ` Roger Pau Monné
2018-07-27 14:06 ` [PATCH v4 27/32] libxl: QEMU startup sync based on QMP Anthony PERARD
2018-08-02 15:06   ` Roger Pau Monné
2018-07-27 14:06 ` [PATCH v4 28/32] libxl_qmp: Store advertised QEMU version in libxl__ev_qmp Anthony PERARD
2018-08-02 15:08   ` Roger Pau Monné
2018-08-06 16:52     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 29/32] libxl: Change libxl__domain_suspend_device_model() to be async Anthony PERARD
2018-08-02 15:15   ` Roger Pau Monné
2018-07-27 14:06 ` [PATCH v4 30/32] libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp Anthony PERARD
2018-08-02 15:38   ` Roger Pau Monné
2018-08-06 17:07     ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 31/32] libxl_disk: Cut libxl_cdrom_insert into step Anthony PERARD
2018-08-02 15:50   ` Roger Pau Monné
2018-08-06 17:20     ` Anthony PERARD
2018-08-07 14:18       ` Roger Pau Monné
2018-08-07 14:40         ` Anthony PERARD
2018-08-21  9:08           ` Wei Liu
2018-08-21 12:58             ` Anthony PERARD
2018-07-27 14:06 ` [PATCH v4 32/32] libxl_disk: Have libxl_cdrom_insert use libxl__ev_qmp 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=20180727140614.13256-15-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 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).