Skip to contentSkip to conversation

libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_*

PATCH Superseded Anthony PERARD 2 participants 24 messages Jan 4, 2019 – Jan 11, 2019

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v8 cover libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_*

Patch series available in this git branch:
https://xenbits.xen.org/git-http/people/aperard/xen-unstable.git br.libxl-ev-qmp-v8

Changes in v8:
libxl_qmp: Implementation of libxl__ev_qmp_*:
- few changes

Then 3 patches have been added but are not necessary, they are follow-up patches:
- libxl: Remove unused arg from libxl__sendmsg_fds
- libxl_json: Remove libxl__json_object_append_to from header
- libxl: Add comments to libxl__json_*get* functions

All other patches are acked.

Changes in v7:
plenty, with new patches

Changes in v6:
Implementation of libxl__ev_qmp_* functions have been squashed to a single patch. And with that, a lot of changes in order to make it simpler to read the implementation, have better error reporting and a few bug fix.

Checkout more detail changelog in the notes of each patch, as there is many.

Changes in v5:
Plenty of patch have been applied.
Other changes mostly are coding style and other typos. Some bug fixes.
Details can be found in patch notes.

I have left aside the change to cdrom_insert until I can found what to do with the userdata lock.

In order for libxl to be able to manage QEMU while it is restricted, a few changes are needed. We need a new way to get a startup notification from QEMU as xenstore may not be accessible when QEMU is ready. We also need to a different way to have QEMU save it's state and to insert cdrom as a restricted QEMU doesn't have access to the file system.

For both, we can use QMP, we can use it to query QEMU's status, and we can use it to send a file descriptor through which QEMU can save its state, or it can be a cdrom.

We take this opportunity to rewrite the QMP client, and this time been asynchronous, the result is libxl__ev_qmp_*.

The plat de résistance in this patch series start with patch "libxl: Design of an async API to issue QMP commands to QEMU" which implement libxl__ev_qmp_* functions to turn the QMP client into asynchronous mode.

This comes with changes that uses the new interface.
* "libxl: QEMU startup sync based on QMP"
which can use QMP to find out when QEMU as started.
this requires: "libxl_dm: Pre-open QMP socket for QEMU"
But that only works with dm_restrict=1 as explain in the patch.
* "libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp" Which rewrite libxl__qmp_save(), and adds the ability to have QEMU save its state to a file descriptor which libxl will have openned.

Patch series available in this git branch:
https://xenbits.xen.org/git-http/people/aperard/xen-unstable.git br.libxl-ev-qmp-v8

Cheers,

Anthony PERARD (17):
libxl: Enhance libxl__sendmsg_fds to deal with EINTR and EWOULDBLOCK
libxl_qmp: Separate QMP message generation from qmp_send_prepare
libxl_qmp: Change qmp_qemu_check_version to compare version
libxl: Add wrapper around libxl__json_object_to_json JSON
libxl: Design of an async API to issue QMP commands to QEMU
libxl_qmp: Implementation of libxl__ev_qmp_*
libxl_exec: Add libxl__spawn_initiate_failure
libxl: Add init/dispose of for libxl__domain_build_state
libxl_dm: Pre-open QMP socket for QEMU
libxl: Add dmss_init/dispose for libxl__dm_spawn_state
libxl: QEMU startup sync based on QMP
libxl_qmp: Store advertised QEMU version in libxl__ev_qmp
libxl: Change libxl__domain_suspend_device_model() to be async
libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp
libxl: Remove unused arg from libxl__sendmsg_fds
libxl_json: Remove libxl__json_object_append_to from header
libxl: Add comments to libxl__json_*get* functions

 tools/libxl/libxl_aoutils.c     |   2 +-
 tools/libxl/libxl_create.c      |  54 +-
 tools/libxl/libxl_dm.c          | 143 ++++-
 tools/libxl/libxl_dom_suspend.c |  37 +-
 tools/libxl/libxl_exec.c        |  11 +-
 tools/libxl/libxl_internal.h    | 197 ++++++-
 tools/libxl/libxl_json.c        |   9 +-
 tools/libxl/libxl_qmp.c         | 991 ++++++++++++++++++++++++++++++--
 tools/libxl/libxl_types.idl     |   7 +
 tools/libxl/libxl_utils.c       |  28 +-
 10 files changed, 1356 insertions(+), 123 deletions(-)
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH v8 1/17 libxl: Enhance libxl__sendmsg_fds to deal with EINTR and EWOULDBLOCK

This patch change the behavior of libxl__sendmsg_fds to retry sendmsg on EINTR error and return an error on short writes.

This patch allow a caller of libxl__sendmsg_fds to deal with EWOULDBLOCK and short writes. The function now requires to send only 1 byte of data so that when dealing with non-blocking fds a EWOULDBLOCK error would mean that the fds haven't been sent yet. Current caller already send only 1 byte.

---

Notes:
v8:
Acked, but:
- there is a follow-up patch to get rid of datalen param.
- commit message have been modified a bit to talk about short write in general.

v7:
always assert datalen == 1, but only fail when sendmsg haven't send everything (r != datalen)
check sendmsg return value on success as well (check for short write)

 tools/libxl/libxl_internal.h |  5 ++++-
 tools/libxl/libxl_utils.c    | 25 ++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 6 deletions(-)
tools/libxl/libxl_internal.h +4 -1
index 67f3f6ac76..aa9059ffec 100644
@@ -1876,7 +1876,10 @@ _hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
 _hidden int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
                                        const libxl_domain_config *guest_config);
 
-/* on failure, logs */
+/* `datalen` should be 1 byte
+ * When dealing with a non-blocking fd, it returns
+ *   ERROR_NOT_READY on EWOULDBLOCK
+ * logs on other failures. */
 int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
                        const void *data, size_t datalen,
                        int nfds, const int fds[], const char *what);
tools/libxl/libxl_utils.c +20 -5
index 5854717b11..d53db8c37d 100644
@@ -1070,6 +1070,8 @@ int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
     struct iovec iov;
     int r;
 
+    assert(datalen == 1);
+
     iov.iov_base = (void*)data;
     iov.iov_len  = datalen;
 
@@ -1088,11 +1090,24 @@ int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
 
     msg.msg_controllen = cmsg->cmsg_len;
 
-    r = sendmsg(carrier, &msg, 0);
-    if (r < 0) {
-        LOGE(ERROR, "failed to send fd-carrying message (%s)", what);
-        return ERROR_FAIL;
-    }
+    while (1) {
+        r = sendmsg(carrier, &msg, 0);
+        if (r < 0) {
+            if (errno == EINTR)
+                continue;
+            if (errno == EWOULDBLOCK) {
+                return ERROR_NOT_READY;
+            }
+            LOGE(ERROR, "failed to send fd-carrying message (%s)", what);
+            return ERROR_FAIL;
+        }
+        if (r != datalen) {
+            LOG(ERROR, "sendmsg have written %d instead of %ld",
+                r, datalen);
+            return ERROR_FAIL;
+        }
+        break;
+    };
 
     return 0;
 }
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v8 2/17 libxl_qmp: Separate QMP message generation from qmp_send_prepare

In reply to Anthony PERARD

.. to be able to re-use qmp_prepare_cmd with libxl__ev_qmp.

This patch also add the QMP end of command '\r\n' into the generated string as every caller will needs this.

There should be no functional change.

---

Notes:
v8:
Acked

v7:
got rid of len_r in qmp_prepare_cmd parameters
callers will need to call strlen.
The returned value (ret) is now allocated within gc (instead of NOGC)

v6:
comment about ownership of buf
use lib__sprintf instead of two strncpy
v5:
rename qmp_prepare_qmp_cmd to qmp_prepare_cmd fix coding style

 tools/libxl/libxl_qmp.c | 49 ++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 18 deletions(-)
tools/libxl/libxl_qmp.c +31 -18
index 6a5c997546..45a2cc423b 100644
@@ -571,17 +571,16 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
     return rc;
 }
 
-static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
-                              const char *cmd, libxl__json_object *args,
-                              qmp_callback_t callback, void *opaque,
-                              qmp_request_context *context)
+static char *qmp_prepare_cmd(libxl__gc *gc, const char *cmd,
+                             const libxl__json_object *args,
+                             int id)
 {
-    const unsigned char *buf = NULL;
-    char *ret = NULL;
-    libxl_yajl_length len = 0;
+    yajl_gen hand = NULL;
+    /* memory for 'buf' is owned by 'hand' */
+    const unsigned char *buf;
+    libxl_yajl_length len;
     yajl_gen_status s;
-    yajl_gen hand;
-    callback_id_pair *elm = NULL;
+    char *ret = NULL;
 
     hand = libxl_yajl_gen_alloc(NULL);
 
@@ -598,7 +597,7 @@ static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
     libxl__yajl_gen_asciiz(hand, "execute");
     libxl__yajl_gen_asciiz(hand, cmd);
     libxl__yajl_gen_asciiz(hand, "id");
-    yajl_gen_integer(hand, ++qmp->last_id_used);
+    yajl_gen_integer(hand, id);
     if (args) {
         libxl__yajl_gen_asciiz(hand, "arguments");
         libxl__json_object_to_yajl_gen(gc, hand, args);
@@ -607,7 +606,27 @@ static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
 
     s = yajl_gen_get_buf(hand, &buf, &len);
 
-    if (s) {
+    if (s != yajl_gen_status_ok)
+        goto out;
+
+    ret = libxl__sprintf(gc, "%*.*s\r\n", (int)len, (int)len, buf);
+
+out:
+    yajl_gen_free(hand);
+    return ret;
+}
+
+static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
+                              const char *cmd, libxl__json_object *args,
+                              qmp_callback_t callback, void *opaque,
+                              qmp_request_context *context)
+{
+    char *buf;
+    callback_id_pair *elm;
+
+    buf = qmp_prepare_cmd(gc, cmd, args, ++qmp->last_id_used);
+
+    if (!buf) {
         LOGD(ERROR, qmp->domid, "Failed to generate a qmp command");
         goto out;
     }
@@ -623,13 +642,10 @@ static char *qmp_send_prepare(libxl__gc *gc, libxl__qmp_handler *qmp,
     elm->context = context;
     LIBXL_STAILQ_INSERT_TAIL(&qmp->callback_list, elm, next);
 
-    ret = libxl__strndup(gc, (const char*)buf, len);
-
     LOGD(DEBUG, qmp->domid, "next qmp command: '%s'", buf);
 
 out:
-    yajl_gen_free(hand);
-    return ret;
+    return buf;
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
@@ -650,9 +666,6 @@ static int qmp_send(libxl__qmp_handler *qmp,
     if (libxl_write_exactly(qmp->ctx, qmp->qmp_fd, buf, strlen(buf),
                             "QMP command", "QMP socket"))
         goto out;
-    if (libxl_write_exactly(qmp->ctx, qmp->qmp_fd, "\r\n", 2,
-                            "CRLF", "QMP socket"))
-        goto out;
 
     rc = qmp->last_id_used;
 out:
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH v8 3/17 libxl_qmp: Change qmp_qemu_check_version to compare version

In reply to Anthony PERARD

This patch makes the function simpler to read. It also add the ability for a caller to tell if QEMU is newer or have the exact version.

---

Notes:

    v6:
        new patch
 tools/libxl/libxl_qmp.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/libxl/libxl_qmp.c +21 -7
index 45a2cc423b..73f2202b4f 100644
@@ -392,13 +392,27 @@ static int qmp_handle_response(libxl__gc *gc, libxl__qmp_handler *qmp,
     return 0;
 }
 
-static bool qmp_qemu_check_version(libxl__qmp_handler *qmp, int major,
-                                   int minor, int micro)
+/*
+ * return values:
+ *   < 0  if qemu's version <  asked version
+ *   = 0  if qemu's version == asked version
+ *   > 0  if qemu's version >  asked version
+ */
+static int qmp_qemu_compare_version(libxl__qmp_handler *qmp, int major,
+                                    int minor, int micro)
 {
-    return qmp->version.major > major ||
-        (qmp->version.major == major &&
-            (qmp->version.minor > minor ||
-             (qmp->version.minor == minor && qmp->version.micro >= micro)));
+#define CHECK_VERSION(level) do { \
+    if (qmp->version.level > (level)) return +1; \
+    if (qmp->version.level < (level)) return -1; \
+} while (0)
+
+    CHECK_VERSION(major);
+    CHECK_VERSION(minor);
+    CHECK_VERSION(micro);
+
+#undef CHECK_VERSION
+
+    return 0;
 }
 
 /*
@@ -1012,7 +1026,7 @@ int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename, bool live)
 
     /* live parameter was added to QEMU 2.11. It signal QEMU that the save
      * operation is for a live migration rather that for taking a snapshot. */
-    if (qmp_qemu_check_version(qmp, 2, 11, 0))
+    if (qmp_qemu_compare_version(qmp, 2, 11, 0) >= 0)
         qmp_parameters_add_bool(gc, &args, "live", live);
 
     rc = qmp_synchronous_send(qmp, "xen-save-devices-state", args,
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH v8 4/17 libxl: Add wrapper around libxl__json_object_to_json JSON

In reply to Anthony PERARD

That wrapper is going to be used to safely log a json_object, as libxl__json_object_to_json return NULL on error. In the error case, JSON() will return an invalid json string.

---

Notes:
v8:
Acked
v7:
new patch
There are no user yet because the first users is going to be in "libxl_qmp: Implementation of libxl__ev_qmp_*" which is already a huge patch, that 3 lines might get lost.

 tools/libxl/libxl_internal.h | 3 +++
 1 file changed, 3 insertions(+)
tools/libxl/libxl_internal.h +3 -0
index aa9059ffec..16e6eb6c5e 100644
@@ -2046,6 +2046,9 @@ _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc_opt, const char *s);
 
 _hidden char *libxl__json_object_to_json(libxl__gc *gc,
                                          const libxl__json_object *args);
+/* Always return a valid string, but invalid json on error. */
+#define JSON(o) \
+    (libxl__json_object_to_json(gc, (o)) ? : "<invalid-json-object>")
 
   /* Based on /local/domain/$domid/dm-version xenstore key
    * default is qemu xen traditional */
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v8 5/17 libxl: Design of an async API to issue QMP commands to QEMU

In reply to Anthony PERARD

All the functions will be implemented in later patches.

This patch includes the API that libxl can use to send QMP commands to QEMU.

---

Notes:
v7:
acked, but with:
fd field renamed to payload_fd
libxl__ao ao field added
added to _send that callback isn't called synchronously.

v6:
use libxl_domid type for domid instead of plain uin32_t avoid the work "chained", rewrite the paragraph about sending one
cmd after another
Rewrite the comment about the callback, and explain that on error,
the ev may be Idle or may still be Connected
Change the carefd to a simple int (field cfd -> fd)

v5:
some changes in the comment

 tools/libxl/libxl_internal.h | 76 +++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 2 deletions(-)
tools/libxl/libxl_internal.h +74 -2
index 16e6eb6c5e..fadafa5f11 100644
@@ -192,6 +192,8 @@ typedef struct libxl__ao libxl__ao;
 typedef struct libxl__aop_occurred libxl__aop_occurred;
 typedef struct libxl__osevent_hook_nexus libxl__osevent_hook_nexus;
 typedef struct libxl__osevent_hook_nexi libxl__osevent_hook_nexi;
+typedef struct libxl__json_object libxl__json_object;
+typedef struct libxl__carefd libxl__carefd;
 
 typedef struct libxl__domain_create_state libxl__domain_create_state;
 typedef void libxl__domain_create_cb(struct libxl__egc *egc,
@@ -355,6 +357,76 @@ struct libxl__ev_child {
     LIBXL_LIST_ENTRY(struct libxl__ev_child) entry;
 };
 
+/*
+ * QMP asynchronous calls
+ *
+ * This facility allows a command to be sent to QEMU, and the response
+ * to be handed to a callback function.
+ *
+ * Commands can be submited one after an other with the same
+ * connection (e.g. the result from the "add-fd" command need to be
+ * use in a follow-up command before disconnecting from QMP). A
+ * libxl__ev_qmp can be reused when the callback is been called in
+ * order to use the same connection.
+ *
+ * Only one connection at a time can be made to one QEMU, so avoid
+ * keeping a libxl__ev_qmp Connected for to long and call
+ * libxl__ev_qmp_dispose as soon as it is not needed anymore.
+ *
+ * Possible states of a libxl__ev_qmp:
+ *  Undefined
+ *    Might contain anything.
+ *  Idle
+ *    Struct contents are defined enough to pass to any
+ *    libxl__ev_qmp_* function.
+ *    The struct does not contain references to any allocated private
+ *    resources so can be thrown away.
+ *  Active
+ *    Currently waiting for the callback to be called.
+ *    _dispose must be called to reclaim resources.
+ *  Connected
+ *    Struct contain allocated ressources.
+ *    Calling _send() with this same ev will use the same QMP connection.
+ *    _dispose() must be called to reclaim resources.
+ *
+ * libxl__ev_qmp_init: Undefined/Idle -> Idle
+ *
+ * libxl__ev_qmp_send: Idle/Connected -> Active (on error: Idle)
+ *    Sends a command to QEMU.
+ *    callback will be called when a response is received or when an
+ *    error as occured.
+ *    callback isn't called synchronously.
+ *
+ * libxl__ev_qmp_dispose: Connected/Active/Idle -> Idle
+ *
+ * callback: When called: Active -> Connected (on error: Idle/Connected)
+ *    When called, ev is Connected and can be reused or disposed of.
+ *    On error, the callback is called with response == NULL and the
+ *    error code in rc. The new state of ev depending on the value of rc:
+ *    - rc == ERROR_QMP_*: This is an error associated with the cmd to
+ *      run, ev is Connected.
+ *    - otherwise: An other error happend, ev is now Idle.
+ *    The callback is only called once.
+ */
+typedef struct libxl__ev_qmp libxl__ev_qmp;
+typedef void libxl__ev_qmp_callback(libxl__egc *egc, libxl__ev_qmp *ev,
+                                    const libxl__json_object *response,
+                                    int rc);
+
+_hidden void libxl__ev_qmp_init(libxl__ev_qmp *ev);
+_hidden int libxl__ev_qmp_send(libxl__gc *gc, libxl__ev_qmp *ev,
+                               const char *cmd, libxl__json_object *args);
+_hidden void libxl__ev_qmp_dispose(libxl__gc *gc, libxl__ev_qmp *ev);
+
+struct libxl__ev_qmp {
+    /* caller should include this in their own struct */
+    /* caller must fill these in, and they must all remain valid */
+    libxl__ao *ao;
+    libxl_domid domid;
+    libxl__ev_qmp_callback *callback;
+    int payload_fd; /* set to send a fd with the command, -1 otherwise */
+};
+
 
 /*
  * evgen structures, which are the state we use for generating
@@ -1909,7 +1981,7 @@ typedef enum {
     JSON_ANY     = 255 /* this is a mask of all values above, adjust as needed */
 } libxl__json_node_type;
 
-typedef struct libxl__json_object {
+struct libxl__json_object {
     libxl__json_node_type type;
     union {
         bool b;
@@ -1922,7 +1994,7 @@ typedef struct libxl__json_object {
         flexarray_t *map;
     } u;
     struct libxl__json_object *parent;
-} libxl__json_object;
+};
 
 typedef int (*libxl__json_parse_callback)(libxl__gc *gc,
                                           libxl__json_object *o,
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

PATCH v8 6/17 libxl_qmp: Implementation of libxl__ev_qmp_*

In reply to Anthony PERARD

This patch implement the API libxl__ev_qmp documented in the previous patch, "libxl: Design of an async API to issue QMP commands to QEMU".

Since this API is to interact with QEMU via the QMP protocol, it also implement a QMP client. The specification for the QEMU Machine Protocol (QMP) can be found in the QEMU repository at:
https://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/interop/qmp-spec.txt

---

Notes:
v8:
- using STATE_AO_GC everywhere
- call qmp_ev_ensure_reading_writing from qmp_ev_set_state instead of other places. But add a call to qmp_ev_ensure_reading_writing when the tx_buf is updated (data been transmitted to QEMU)
- assert return value of write().

- fix implementation doc of rx_buf (can be free when used)
- fix doc of qmp_ev_ensure_reading_writing

- reorder qmp_ev_parse_error_messages and qmp_ev_handle_message

v7:
- Make use of the new ao field filled by the caller
(no more free(tx_buf))
- Have reworked the state table to be more accurate, easier to read and with better desc of the waiting_reply state with sub-states as well as better description of the state of the ev_fd efd.
- There is state transition changes, now we have cap.neg -> waiting_reply (before it was cap.neg -> connected -> waiting_reply), that makes the internal connected state the same as the external Connected state.
- there are now 3 different id variable instead of a single one, the currently sent one id, the next one next_id, and the one associated with the caller's command msg_id, id will always be the for the next expected message from QEMU.
- qmp_ev_fd_callback now check for POLLERR, and read async error with getsockopt(SO_ERROR).
- qmp_ev_callback_readable takes care once again to also parse messages. But it now first attempt to read messages that would be in the rx_buf before reading from the socket.
- Some functions state changes have been updated, qmp_ev_ensure_reading_writing, qmp_ev_set_state, qmp_ev_callback_writable, qmp_ev_callback_readable
- Have cleaned up qmp_ev_ensure_reading_writing
- Have cleaned up qmp_error_class_to_libxl_error_code, and log unknown error classes.
- In qmp_ev_callback_readable, the size increase of the buffer have been reworked to update both rx_buf_size and rx_buf at the same time, and remove the use of max()
- Return value of qmp_prepare_cmd calles is checked and logged on error.
- comments in RHS of struct
- New qmp_ev_tx_buf_clear func
- Add a link to the QMP spec.
- update callers of qmp_prepare_cmd, which doesn't provide the string lenght anymore also remove ev->msg_len
- rename internal fields qmp_cfd, qmp_efd, qmp_state to cfd, efd and state.
- change qmp_ev_connect to only allow disconnect on entry
- squash qmp_ev_prepare_cmd into libxl__ev_qmp_send
- reduce max rx buffer size to 1M
query-vcpus with 71 cpus active yield 14484 bytes.
- use JSON to print libxl__json_object

v6.2:
Add definition of the internal broken state updated comments about states
v6.1:
Adding some comment about possible internal state changes

v6:
This is a squash of 7 commits on the previous version:
- libxl_qmp: Connect to QMP socket
- libxl_qmp: Implement fd callback and read data
- libxl_qmp: Parse JSON input from QMP
- libxl_qmp: Prepare the command to be sent
- libxl_qmp: Handle write to QMP socket
- libxl_qmp: Handle messages from QEMU
- libxl_qmp: Respond to QMP greeting

General rework of the implementation.
Added more comment, with a description of allowed internal states. Check for EINPROGRESS after connect().
Read until EWOULDBLOCK.
Handle EWOULDBLOCK on write and sendmsg.
Using memmem instead of strstr.
Using memmove instead of having an offset in rx_buf. Rework buffer allocation
Don't feed \r into json parser anymore
Add a check for a maximum RX buffer size
Added more error messages
New error code ERROR_PROTOCOL_ERROR_QMP
Rewrite conversion of QMP ErrorClass to libxl_error code Added helpers: qmp_ev_ensure_reading_writing, qmp_ev_set_state Split some functions, squash others
Added ev->msg* to store generated user command as tx_buf is used during
connection (for qmp_capabilities)
Remove qmp_state_greeting
Added qmp_state_waiting_reply

v5:
nits
use a define instead of a static int for QMP_CAPABILITY_NEGOCIATION_MSGID use a switch in qmp_ev_callback_writable to check qmp_state Add a description of the different value of libxl__qmp_state enum. some cleanup
remove read loop that only handled EINTR, simply return initialize len and s at declaration time
remove old comment
rename buf_fd to send_fd
Adding default:abort() in qmp_ev_handle_message.

v4:
remove use of a linked list of receive buffer, and use realloc instead. simplification of the patch due to use of a single allocated space for the receive buffer.

 tools/libxl/libxl_internal.h |  34 ++
 tools/libxl/libxl_qmp.c      | 739 +++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl  |   6 +
 3 files changed, 779 insertions(+)
tools/libxl/libxl_internal.h +34 -0
index fadafa5f11..be18d4f341 100644
@@ -418,6 +418,19 @@ _hidden int libxl__ev_qmp_send(libxl__gc *gc, libxl__ev_qmp *ev,
                                const char *cmd, libxl__json_object *args);
 _hidden void libxl__ev_qmp_dispose(libxl__gc *gc, libxl__ev_qmp *ev);
 
+typedef enum {
+    /* initial state */
+    qmp_state_disconnected = 1,
+    /* connected to QMP socket, waiting for greeting message */
+    qmp_state_connecting,
+    /* qmp_capabilities command sent, waiting for reply */
+    qmp_state_capability_negotiation,
+    /* sending user's cmd and waiting for reply */
+    qmp_state_waiting_reply,
+    /* ready to send commands */
+    qmp_state_connected,
+} libxl__qmp_state;
+
 struct libxl__ev_qmp {
     /* caller should include this in their own struct */
     /* caller must fill these in, and they must all remain valid */
@@ -425,6 +438,27 @@ struct libxl__ev_qmp {
     libxl_domid domid;
     libxl__ev_qmp_callback *callback;
     int payload_fd; /* set to send a fd with the command, -1 otherwise */
+
+    /*
+     * remaining fields are private to libxl_ev_qmp_*
+     */
+
+    libxl__carefd *cfd;
+    libxl__ev_fd efd;
+    libxl__qmp_state state;
+    int id;
+    int next_id;        /* next id to use */
+    /* receive buffer */
+    char *rx_buf;
+    size_t rx_buf_size; /* current allocated size */
+    size_t rx_buf_used; /* actual data in the buffer */
+    /* sending buffer */
+    char *tx_buf;
+    size_t tx_buf_len;  /* tx_buf size */
+    size_t tx_buf_off;  /* already sent */
+    /* The message to send when ready */
+    char *msg;
+    int msg_id;
 };
 
 
tools/libxl/libxl_qmp.c +739 -0

756 lines not on this page · raw message

tools/libxl/libxl_types.idl +6 -0
index 141c46e42a..212b00a677 100644
@@ -69,6 +69,12 @@ libxl_error = Enumeration("error", [
     (-23, "NOTFOUND"),
     (-24, "DOMAIN_DESTROYED"), # Target domain ceased to exist during op
     (-25, "FEATURE_REMOVED"), # For functionality that has been removed
+    (-26, "PROTOCOL_ERROR_QMP"),
+    (-27, "UNKNOWN_QMP_ERROR"),
+    (-28, "QMP_GENERIC_ERROR"), # unspecified qmp error
+    (-29, "QMP_COMMAND_NOT_FOUND"), # the requested command has not been found
+    (-30, "QMP_DEVICE_NOT_ACTIVE"), # a device has failed to be become active
+    (-31, "QMP_DEVICE_NOT_FOUND"), # the requested device has not been found
     ], value_namespace = "")
 
 libxl_domain_type = Enumeration("domain_type", [
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*

Anthony PERARD writes ("[PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*"):

Quoted 7 lines

This patch implement the API libxl__ev_qmp documented in the previous patch, "libxl: Design of an async API to issue QMP commands to QEMU".

Since this API is to interact with QEMU via the QMP protocol, it also implement a QMP client. The specification for the QEMU Machine Protocol (QMP) can be found in the QEMU repository at:
https://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/interop/qmp-spec.txt

Thanks. I have re-reviewed this and I have only this one question:

tools/libxl/libxl_qmp.c int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid, · line 1653

Quoted 12 lines
tools/libxl/libxl_qmp.c +12 -0
+    while (ev->tx_buf_off < ev->tx_buf_len) {
+        ssize_t max_write = ev->tx_buf_len - ev->tx_buf_off;
+        r = write(fd, ev->tx_buf + ev->tx_buf_off, max_write);
+        if (r < 0) {
+            if (errno == EINTR)
+                continue;
+            if (errno == EWOULDBLOCK)
+                break;
+            LOGED(ERROR, ev->domid, "failed to write to QMP socket");
+            return ERROR_FAIL;
+        }
+        assert(r >= 0 && r <= max_write);

Doesn't this want to be
+ assert(r > 0 && r <= max_write);
?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*

Quoted 24 lines · Ian Jackson

Anthony PERARD writes ("[PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*"):

Quoted 7 lines

This patch implement the API libxl__ev_qmp documented in the previous patch, "libxl: Design of an async API to issue QMP commands to QEMU".

Since this API is to interact with QEMU via the QMP protocol, it also implement a QMP client. The specification for the QEMU Machine Protocol (QMP) can be found in the QEMU repository at:
https://git.qemu.org/?p=qemu.git;a=blob_plain;f=docs/interop/qmp-spec.txt

Thanks. I have re-reviewed this and I have only this one question:

tools/libxl/libxl_qmp.c int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid, · line 1653

Quoted 12 lines
tools/libxl/libxl_qmp.c +12 -0
+    while (ev->tx_buf_off < ev->tx_buf_len) {
+        ssize_t max_write = ev->tx_buf_len - ev->tx_buf_off;
+        r = write(fd, ev->tx_buf + ev->tx_buf_off, max_write);
+        if (r < 0) {
+            if (errno == EINTR)
+                continue;
+            if (errno == EWOULDBLOCK)
+                break;
+            LOGED(ERROR, ev->domid, "failed to write to QMP socket");
+            return ERROR_FAIL;
+        }
+        assert(r >= 0 && r <= max_write);
Quoted 2 lines

Doesn't this want to be
+ assert(r > 0 && r <= max_write);

Yes, I think that would be fine. I guess that would catch issue where max_write is 0 (so trying to write 0 bytes).

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#9 lore raw

Re: [PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*

Anthony PERARD writes ("Re: [PATCH v8 06/17] libxl_qmp: Implementation of libxl__ev_qmp_*"):

Quoted 6 lines · Ian Jackson
Quoted 2 lines

Doesn't this want to be
+ assert(r > 0 && r <= max_write);

Yes, I think that would be fine. I guess that would catch issue where max_write is 0 (so trying to write 0 bytes).

I was thinking of situations where the open-file referred to by fd has been put into a funny state which leads to write() returning 0.

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

PATCH v8 7/17 libxl_exec: Add libxl__spawn_initiate_failure

In reply to Anthony PERARD

This function can be used by user of libxl__spawn_* when they setup a notification other than xenstore. The parent can already report success via libxl__spawn_initiate_detach(), this new function can be used for failure instead of waiting for the timeout.

---

Notes:
v8:
Acked

v7:
call spawn_fail from libxl__spawn_initiate_failure. modify spawn_fail to set ss->rc only once.
Fix description to tell caller of libxl__spawn_initiate_failure that
they have to log.
use an egc instead of gc with libxl__spawn_initiate_failure.

v6:
long line fix
typos fixed
fix leak of internal state into external doc
if the function is call multiple times, set rc only the first time.

v5:
fix typos

 tools/libxl/libxl_exec.c     | 11 ++++++++++-
 tools/libxl/libxl_internal.h | 23 ++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
tools/libxl/libxl_exec.c +10 -1
index 02e6c917f0..47c9c8f1ba 100644
@@ -373,13 +373,22 @@ void libxl__spawn_initiate_detach(libxl__gc *gc, libxl__spawn_state *ss)
     spawn_detach(gc, ss);
 }
 
+void libxl__spawn_initiate_failure(libxl__egc *egc, libxl__spawn_state *ss,
+                                   int rc)
+/* The spawn state must be Attached on entry and will be Attached Failed
+ * on return.  */
+{
+    spawn_fail(egc, ss, rc);
+}
+
 static void spawn_fail(libxl__egc *egc, libxl__spawn_state *ss, int rc)
 /* Caller must have logged.  Must be last thing in calling function,
  * as it may make the callback.  Precondition: Attached or Detaching. */
 {
     EGC_GC;
     assert(rc);
-    ss->rc = rc;
+    if (!ss->rc)
+        ss->rc = rc;
     spawn_detach(gc, ss);
 }
 
tools/libxl/libxl_internal.h +22 -1
index be18d4f341..9e7e4dcae9 100644
@@ -1567,7 +1567,8 @@ _hidden void libxl__spawn_init(libxl__spawn_state*);
  *
  * The inner child must soon exit or exec.  It must also soon exit or
  * notify the parent of its successful startup by writing to the
- * xenstore path xspath.
+ * xenstore path xspath OR via other means that the parent will have
+ * to set up.
  *
  * The user (in the parent) will be called back (confirm_cb) every
  * time that xenstore path is modified.
@@ -1623,6 +1624,26 @@ _hidden int libxl__spawn_spawn(libxl__egc *egc, libxl__spawn_state *spawn);
  */
 _hidden void libxl__spawn_initiate_detach(libxl__gc *gc, libxl__spawn_state*);
 
+/*
+ * libxl__spawn_initiate_failure - Propagate failure from the caller to the
+ * callee.
+ *
+ * Works by killing the intermediate process from spawn_spawn.
+ * After this function returns, a failure will be reported.
+ *
+ * This is not synchronous: there will be a further callback when
+ * the detach is complete.
+ *
+ * Caller must have logged a failure reason.
+ *
+ * The spawn state must be Attached on entry and will remain Attached. It
+ * is possible for a spawn to fail for multiple reasons, for example
+ * call(s) to libxl__spawn_initiate_failure and also for some other reason.
+ * In that case the first rc value from any source will take precedence.
+ */
+_hidden void libxl__spawn_initiate_failure(libxl__egc *egc,
+                                           libxl__spawn_state *ss, int rc);
+
 /*
  * If successful, this should return 0.
  *
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

PATCH v8 8/17 libxl: Add init/dispose of for libxl__domain_build_state

In reply to Anthony PERARD

These two new functions libxl__domain_build_state_{init,dispose} should be called every time a new libxl__domain_build_state comes to existance.

There seems to be two of them, one with the domain creation machinery, and one in the stub_dm_spawn.

---

Notes:

    v8:
        Acked
    
    v7:
        new patch
 tools/libxl/libxl_create.c   | 14 ++++++++++++--
 tools/libxl/libxl_dm.c       |  3 +++
 tools/libxl/libxl_internal.h |  3 +++
 3 files changed, 18 insertions(+), 2 deletions(-)
tools/libxl/libxl_create.c +12 -2
index 891175b15b..4ca25f56a6 100644
@@ -761,6 +761,16 @@ static int store_libxl_entry(libxl__gc *gc, uint32_t domid,
                             libxl_device_model_version_to_string(b_info->device_model_version));
 }
 
+void libxl__domain_build_state_init(libxl__domain_build_state *state)
+{
+}
+
+void libxl__domain_build_state_dispose(libxl__domain_build_state *state)
+{
+    libxl__file_reference_unmap(&state->pv_kernel);
+    libxl__file_reference_unmap(&state->pv_ramdisk);
+}
+
 /*----- main domain creation -----*/
 
 /* We have a linear control flow; only one event callback is
@@ -823,6 +833,7 @@ static void initiate_domain_create(libxl__egc *egc,
     const int restore_fd = dcs->restore_fd;
 
     domid = dcs->domid_soft_reset;
+    libxl__domain_build_state_init(&dcs->build_state);
 
     if (d_config->c_info.ssid_label) {
         char *s = d_config->c_info.ssid_label;
@@ -1595,8 +1606,7 @@ static void domcreate_complete(libxl__egc *egc,
     libxl_domain_config *const d_config = dcs->guest_config;
     libxl_domain_config *d_config_saved = &dcs->guest_config_saved;
 
-    libxl__file_reference_unmap(&dcs->build_state.pv_kernel);
-    libxl__file_reference_unmap(&dcs->build_state.pv_ramdisk);
+    libxl__domain_build_state_dispose(&dcs->build_state);
 
     if (!rc && d_config->b_info.exec_ssidref)
         rc = xc_flask_relabel_domain(CTX->xch, dcs->guest_domid, d_config->b_info.exec_ssidref);
tools/libxl/libxl_dm.c +3 -0
index be493cf9f2..200310083f 100644
@@ -2045,6 +2045,8 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     libxl__domain_build_state *const d_state = sdss->dm.build_state;
     libxl__domain_build_state *const stubdom_state = &sdss->dm_state;
 
+    libxl__domain_build_state_init(stubdom_state);
+
     if (guest_config->b_info.device_model_version !=
         LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL) {
         ret = ERROR_INVAL;
@@ -2373,6 +2375,7 @@ static void stubdom_xswait_cb(libxl__egc *egc, libxl__xswait_state *xswait,
     if (strcmp(p, "running"))
         return;
  out:
+    libxl__domain_build_state_dispose(&sdss->dm_state);
     libxl__xswait_stop(gc, xswait);
     sdss->callback(egc, &sdss->dm, rc);
 }
tools/libxl/libxl_internal.h +3 -0
index 9e7e4dcae9..88c01ff118 100644
@@ -1265,6 +1265,9 @@ typedef struct {
     uint32_t clock_frequency;
 } libxl__domain_build_state;
 
+_hidden void libxl__domain_build_state_init(libxl__domain_build_state *s);
+_hidden void libxl__domain_build_state_dispose(libxl__domain_build_state *s);
+
 _hidden int libxl__build_pre(libxl__gc *gc, uint32_t domid,
               libxl_domain_config * const d_config,
               libxl__domain_build_state *state);
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#12 lore raw

PATCH v8 9/17 libxl_dm: Pre-open QMP socket for QEMU

In reply to Anthony PERARD

This patch moves the creation of the QMP unix socket from QEMU to libxl. But libxl doesn't rely on this yet.

When starting QEMU with dm_restrict=1, pre-open the QMP socket before exec QEMU. That socket will be useful to find out if QEMU is ready, and pre-opening it means that libxl can connect to it without waiting for QEMU to create it.

The pre-opening is conditional, based on the use of dm_restrict because it is using a new command line option of QEMU, and dm_restrict support in QEMU is newer.

-chardev socket,fd=X is available with QEMU 2.12, since commit:

Quoted 2 lines

char: allow passing pre-opened socket file descriptor at startup 0935700f8544033ebbd41e1f13cd528f8a58d24d

dm_restrict is available in QEMU 3.0.

---

Notes:
v8:
acked

v7:
move domain_build_state init/dispose to a different patch
and close dm_monitor_fd in that new dispose function
assert that dm_monitor_fd should be -1 when using old qemu

v6:
move dm_monitor_fd into libxl_domain_build_info (or d_state) -> move the creation of the socket into libxl__spawn_local_dm
instead of libxl__build_device_model_args
Use libxl_domid type instead of int for libxl__pre_open_qmp_socket() Check function calls (bind and listen) return value in a separate statement. typo and other coding style issue fixes

v5:
use libxl__remove_file
few changes in coding style
remove stale includes (sys/socket, sys/un) which are now in libxl_internal.h

v4:
separate the logic to open a socket into a function. Use libxl__prepare_sockaddr_un() to check path size

 tools/libxl/libxl_create.c   |  5 +++
 tools/libxl/libxl_dm.c       | 71 ++++++++++++++++++++++++++++++++++--
 tools/libxl/libxl_internal.h |  1 +
 3 files changed, 73 insertions(+), 4 deletions(-)
tools/libxl/libxl_create.c +5 -0
index 4ca25f56a6..89f4766a27 100644
@@ -763,12 +763,17 @@ static int store_libxl_entry(libxl__gc *gc, uint32_t domid,
 
 void libxl__domain_build_state_init(libxl__domain_build_state *state)
 {
+    state->dm_monitor_fd = -1;
 }
 
 void libxl__domain_build_state_dispose(libxl__domain_build_state *state)
 {
     libxl__file_reference_unmap(&state->pv_kernel);
     libxl__file_reference_unmap(&state->pv_ramdisk);
+    if (state->dm_monitor_fd >= 0) {
+        close(state->dm_monitor_fd);
+        state->dm_monitor_fd = -1;
+    }
 }
 
 /*----- main domain creation -----*/
tools/libxl/libxl_dm.c +67 -4

95 lines not on this page · raw message

tools/libxl/libxl_internal.h +1 -0
index 88c01ff118..83bc384d3e 100644
@@ -1240,6 +1240,7 @@ typedef struct {
     char *console_tty;
 
     char *saved_state;
+    int dm_monitor_fd;
 
     libxl__file_reference pv_kernel;
     libxl__file_reference pv_ramdisk;
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw

PATCH v8 10/17 libxl: Add dmss_init/dispose for libxl__dm_spawn_state

In reply to Anthony PERARD

These two functions, dmss_init and dmss_dispose, need to be called to initialise the private parts of a libxl__dm_spawn_state (dmss) as well as dispose of them before giving back control to a caller.

There are 3 functions that can start using a dmss, the classic libxl__spawn_local_dm, the one for stubdom libxl__spawn_stub_dm and libxl__spawn_qdisk_backend. But there are only 2 exit path as libxl__spawn_qdisk_backend is using libxl__spawn_local_dm functions.

These two new functions are empty but will be used shortly.

---

Notes:

    v8:
        Acked
    
    v7:
        new patch
 tools/libxl/libxl_dm.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tools/libxl/libxl_dm.c +16 -0
index b9921eb885..4000f47545 100644
@@ -2061,6 +2061,14 @@ static int libxl__write_stub_dmargs(libxl__gc *gc,
     return 0;
 }
 
+static void dmss_init(libxl__dm_spawn_state *dmss)
+{
+}
+
+static void dmss_dispose(libxl__gc *gc, libxl__dm_spawn_state *dmss)
+{
+}
+
 static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
                                 libxl__dm_spawn_state *stubdom_dmss,
                                 int rc);
@@ -2099,6 +2107,7 @@ void libxl__spawn_stub_dm(libxl__egc *egc, libxl__stub_dm_spawn_state *sdss)
     libxl__domain_build_state *const stubdom_state = &sdss->dm_state;
 
     libxl__domain_build_state_init(stubdom_state);
+    dmss_init(&sdss->dm);
 
     if (guest_config->b_info.device_model_version !=
         LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL) {
@@ -2430,6 +2439,7 @@ static void stubdom_xswait_cb(libxl__egc *egc, libxl__xswait_state *xswait,
  out:
     libxl__domain_build_state_dispose(&sdss->dm_state);
     libxl__xswait_stop(gc, xswait);
+    dmss_dispose(gc, &sdss->dm);
     sdss->callback(egc, &sdss->dm, rc);
 }
 
@@ -2471,6 +2481,8 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     const char *dm;
     int dm_state_fd = -1;
 
+    dmss_init(dmss);
+
     if (libxl_defbool_val(b_info->device_model_stubdomain)) {
         abort();
     }
@@ -2690,6 +2702,7 @@ static void device_model_spawn_outcome(libxl__egc *egc,
     }
 
  out:
+    dmss_dispose(gc, dmss);
     dmss->callback(egc, dmss, rc);
 }
 
@@ -2702,6 +2715,8 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     int logfile_w, null = -1, rc;
     uint32_t domid = dmss->guest_domid;
 
+    dmss_init(dmss);
+
     /* Always use qemu-xen as device model */
     dm = qemu_xen_path(gc);
 
@@ -2766,6 +2781,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
 
     rc = 0;
 out:
+    dmss_dispose(gc, dmss);
     if (logfile_w >= 0) close(logfile_w);
     if (null >= 0) close(null);
     /* callback on error only, success goes via dmss->spawn.*_cb */
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#14 lore raw

PATCH v8 11/17 libxl: QEMU startup sync based on QMP

In reply to Anthony PERARD

This is only activated when dm_restrict=1, as explained in a previous patch "libxl_dm: Pre-open QMP socket for QEMU"

---

Notes:
v8:
Acked

v7:
fixed _dispose call in device_model_spawn_outcome move qmp field in libxl__dm_spawn_state into the private section added description about the new QEMU_API error value

v6:
invent ERROR_QEMU_API
return better rc: ERROR_QEMU_API or ERROR_NOT_READY enhance log messages (debug and error)

v5:
removed empty success branch in device_model_qmp_cb() call libxl__ev_qmp_init() earlier in libxl__spawn_local_dm.
otherwise the error path would use an uninitialised libxl__ev_qmp.

v4:
moved to libxl__dm_spawn_* from libxl__spawn_*

 tools/libxl/libxl_dm.c       | 53 ++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |  1 +
 tools/libxl/libxl_types.idl  |  1 +
 3 files changed, 55 insertions(+)
tools/libxl/libxl_dm.c +53 -0
index 4000f47545..20d811be03 100644
@@ -2063,10 +2063,12 @@ static int libxl__write_stub_dmargs(libxl__gc *gc,
 
 static void dmss_init(libxl__dm_spawn_state *dmss)
 {
+    libxl__ev_qmp_init(&dmss->qmp);
 }
 
 static void dmss_dispose(libxl__gc *gc, libxl__dm_spawn_state *dmss)
 {
+    libxl__ev_qmp_dispose(gc, &dmss->qmp);
 }
 
 static void spawn_stubdom_pvqemu_cb(libxl__egc *egc,
@@ -2451,6 +2453,9 @@ static void device_model_startup_failed(libxl__egc *egc,
                                         int rc);
 static void device_model_detached(libxl__egc *egc,
                                   libxl__spawn_state *spawn);
+static void device_model_qmp_cb(libxl__egc *egc, libxl__ev_qmp *ev,
+                                const libxl__json_object *response,
+                                int rc);
 
 /* our "next step" function, called from those callbacks and elsewhere */
 static void device_model_spawn_outcome(libxl__egc *egc,
@@ -2605,6 +2610,17 @@ void libxl__spawn_local_dm(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     spawn->failure_cb = device_model_startup_failed;
     spawn->detached_cb = device_model_detached;
 
+    if (state->dm_monitor_fd >= 0) {
+        /* There is a valid QMP socket available now,
+         * use it to find out when QEMU is ready */
+        dmss->qmp.ao = ao;
+        dmss->qmp.callback = device_model_qmp_cb;
+        dmss->qmp.domid = domid;
+        dmss->qmp.payload_fd = -1;
+        rc = libxl__ev_qmp_send(gc, &dmss->qmp, "query-status", NULL);
+        if (rc) goto out_close;
+    }
+
     rc = libxl__spawn_spawn(egc, spawn);
     if (rc < 0)
         goto out_close;
@@ -2678,6 +2694,43 @@ static void device_model_detached(libxl__egc *egc,
     device_model_spawn_outcome(egc, dmss, 0);
 }
 
+static void device_model_qmp_cb(libxl__egc *egc, libxl__ev_qmp *ev,
+                                const libxl__json_object *response,
+                                int rc)
+{
+    EGC_GC;
+    libxl__dm_spawn_state *dmss = CONTAINER_OF(ev, *dmss, qmp);
+    const libxl__json_object *o;
+    const char *status;
+
+    libxl__ev_qmp_dispose(gc, ev);
+
+    if (rc)
+        goto failed;
+
+    o = libxl__json_map_get("status", response, JSON_STRING);
+    if (!o) {
+        LOGD(ERROR, ev->domid,
+             "Missing 'status' in response to 'query-status'");
+        LOGD(DEBUG, ev->domid, ".. instead, got: %s", JSON(response));
+        rc = ERROR_QEMU_API;
+        goto failed;
+    }
+    status = libxl__json_object_get_string(o);
+    if (strcmp(status, "running")) {
+        LOGD(ERROR, ev->domid, "Unexpected QEMU status: %s", status);
+        rc = ERROR_NOT_READY;
+        goto failed;
+    }
+
+    libxl__spawn_initiate_detach(gc, &dmss->spawn);
+    return;
+
+failed:
+    LOGD(ERROR, ev->domid, "QEMU did not start properly, rc=%d", rc);
+    libxl__spawn_initiate_failure(egc, &dmss->spawn, rc);
+}
+
 static void device_model_spawn_outcome(libxl__egc *egc,
                                        libxl__dm_spawn_state *dmss,
                                        int rc)
tools/libxl/libxl_internal.h +1 -0
index 83bc384d3e..87a58b3ac3 100644
@@ -3934,6 +3934,7 @@ typedef void libxl__dm_spawn_cb(libxl__egc *egc, libxl__dm_spawn_state*,
 struct libxl__dm_spawn_state {
     /* mixed - spawn.ao must be initialised by user; rest is private: */
     libxl__spawn_state spawn;
+    libxl__ev_qmp qmp;
     /* filled in by user, must remain valid: */
     uint32_t guest_domid; /* domain being served */
     libxl_domain_config *guest_config;
tools/libxl/libxl_types.idl +1 -0
index 212b00a677..b685ac47ac 100644
@@ -75,6 +75,7 @@ libxl_error = Enumeration("error", [
     (-29, "QMP_COMMAND_NOT_FOUND"), # the requested command has not been found
     (-30, "QMP_DEVICE_NOT_ACTIVE"), # a device has failed to be become active
     (-31, "QMP_DEVICE_NOT_FOUND"), # the requested device has not been found
+    (-32, "QEMU_API"), # QEMU's replies don't contains expected members
     ], value_namespace = "")
 
 libxl_domain_type = Enumeration("domain_type", [
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw

PATCH v8 12/17 libxl_qmp: Store advertised QEMU version in libxl__ev_qmp

In reply to Anthony PERARD

This will be used in a later patch.

---

Notes:
v8:
Acked

v7:
Add do{}while around the MACRO.
formating nits changes.

v6:
new local macro GRAB_VERSION
better definition of qemu_version field in libxl_internal.h

v5:
initialise qemu_version struct in libxl__ev_qmp_init

 tools/libxl/libxl_internal.h |  8 ++++++++
 tools/libxl/libxl_qmp.c      | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
tools/libxl/libxl_internal.h +8 -0
index 87a58b3ac3..62492e13d7 100644
@@ -439,6 +439,14 @@ struct libxl__ev_qmp {
     libxl__ev_qmp_callback *callback;
     int payload_fd; /* set to send a fd with the command, -1 otherwise */
 
+    /* read-only when Connected
+     * and not to be accessed by the caller otherwise */
+    struct {
+        int major;
+        int minor;
+        int micro;
+    } qemu_version;
+
     /*
      * remaining fields are private to libxl_ev_qmp_*
      */
tools/libxl/libxl_qmp.c +25 -0
index f2ab08620a..9b4a07d622 100644
@@ -1829,6 +1829,27 @@ static int qmp_ev_handle_message(libxl__egc *egc,
             return ERROR_PROTOCOL_ERROR_QMP;
         }
 
+        /*
+         * Store advertised QEMU version
+         * { "QMP": { "version": {
+         *     "qemu": { "major": int, "minor": int, "micro": int } } } }
+         */
+        o = libxl__json_map_get("QMP", resp, JSON_MAP);
+        o = libxl__json_map_get("version", o, JSON_MAP);
+        o = libxl__json_map_get("qemu", o, JSON_MAP);
+#define GRAB_VERSION(level) do { \
+        ev->qemu_version.level = libxl__json_object_get_integer( \
+            libxl__json_map_get(#level, o, JSON_INTEGER)); \
+        } while (0)
+        GRAB_VERSION(major);
+        GRAB_VERSION(minor);
+        GRAB_VERSION(micro);
+#undef GRAB_VERSION
+        LOGD(DEBUG, ev->domid, "QEMU version: %d.%d.%d",
+             ev->qemu_version.major,
+             ev->qemu_version.minor,
+             ev->qemu_version.micro);
+
         /* Prepare next message to send */
         assert(!ev->tx_buf);
         ev->id = ev->next_id++;
@@ -1991,6 +2012,10 @@ void libxl__ev_qmp_init(libxl__ev_qmp *ev)
 
     ev->msg = NULL;
     ev->msg_id = 0;
+
+    ev->qemu_version.major = -1;
+    ev->qemu_version.minor = -1;
+    ev->qemu_version.micro = -1;
 }
 
 int libxl__ev_qmp_send(libxl__gc *unused_gc, libxl__ev_qmp *ev,
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#16 lore raw

PATCH v8 13/17 libxl: Change libxl__domain_suspend_device_model() to be async

In reply to Anthony PERARD

This create an extra step for the two call sites of the function.

libxl__domain_suspend_device_model() in this patch gets an extra error variable (there is ret and rc), but ret goes away in the next patch.

---
libxl_domain_soft_reset() haven't been tested, as it doesn't appear to possible to call the function from xl.
---

Notes:
v8:
Acked

v6:
fix multiple way to report errors, libxl__domain_suspend_device_model will now only report via callbacks, and return void
add rc in libxl__domain_suspend_device_model (ret isn't a proper rc as libxl__qmp_save don't return one)

 tools/libxl/libxl_create.c      | 35 +++++++++++++++++++++-----------
 tools/libxl/libxl_dom_suspend.c | 36 ++++++++++++++++++++-------------
 tools/libxl/libxl_internal.h    |  7 +++++--
 3 files changed, 50 insertions(+), 28 deletions(-)
tools/libxl/libxl_create.c +23 -12
index 89f4766a27..a4e74a5cd2 100644
@@ -1774,6 +1774,9 @@ static void domain_soft_reset_cb(libxl__egc *egc,
     domcreate_complete(egc, &cdcs->dcs, rc);
 }
 
+static void soft_reset_dm_suspended(libxl__egc *egc,
+                                    libxl__domain_suspend_state *dsps,
+                                    int rc);
 static int do_domain_soft_reset(libxl_ctx *ctx,
                                 libxl_domain_config *d_config,
                                 uint32_t domid_soft_reset,
@@ -1856,11 +1859,24 @@ static int do_domain_soft_reset(libxl_ctx *ctx,
         goto out;
     }
 
-    rc = libxl__domain_suspend_device_model(gc, &dss->dsps);
-    if (rc) {
-        LOGD(ERROR, domid_soft_reset, "failed to suspend device model.");
-        goto out;
-    }
+    dss->dsps.ao = ao;
+    dss->dsps.callback_device_model_done = soft_reset_dm_suspended;
+    libxl__domain_suspend_device_model(egc, &dss->dsps); /* must be last */
+
+    return AO_INPROGRESS;
+
+ out:
+    return AO_CREATE_FAIL(rc);
+}
+
+static void soft_reset_dm_suspended(libxl__egc *egc,
+                                    libxl__domain_suspend_state *dsps,
+                                    int rc)
+{
+    STATE_AO_GC(dsps->ao);
+    libxl__domain_soft_reset_state *srs =
+        CONTAINER_OF(dsps, *srs, dss.dsps);
+    libxl__app_domain_create_state *cdcs = &srs->cdcs;
 
     /*
      * Ask all backends to disconnect by removing the domain from
@@ -1868,18 +1884,13 @@ static int do_domain_soft_reset(libxl_ctx *ctx,
      * xenstore again with probably different store/console/...
      * channels.
      */
-    xs_release_domain(ctx->xsh, cdcs->dcs.domid_soft_reset);
+    xs_release_domain(CTX->xsh, cdcs->dcs.domid_soft_reset);
 
     srs->dds.ao = ao;
-    srs->dds.domid = domid_soft_reset;
+    srs->dds.domid = cdcs->dcs.domid_soft_reset;
     srs->dds.callback = domain_soft_reset_cb;
     srs->dds.soft_reset = true;
     libxl__domain_destroy(egc, &srs->dds);
-
-    return AO_INPROGRESS;
-
- out:
-    return AO_CREATE_FAIL(rc);
 }
 
 static void domain_create_cb(libxl__egc *egc,
tools/libxl/libxl_dom_suspend.c +22 -14
index 1e904bae8a..f8ff5cf0c5 100644
@@ -68,10 +68,12 @@ int libxl__domain_suspend_init(libxl__egc *egc,
 
 /*----- callbacks, called by xc_domain_save -----*/
 
-int libxl__domain_suspend_device_model(libxl__gc *gc,
+void libxl__domain_suspend_device_model(libxl__egc *egc,
                                        libxl__domain_suspend_state *dsps)
 {
+    STATE_AO_GC(dsps->ao);
     int ret = 0;
+    int rc = 0;
     uint32_t const domid = dsps->domid;
     const char *const filename = dsps->dm_savefile;
 
@@ -83,18 +85,29 @@ int libxl__domain_suspend_device_model(libxl__gc *gc,
         break;
     }
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
-        if (libxl__qmp_stop(gc, domid))
-            return ERROR_FAIL;
+        ret = libxl__qmp_stop(gc, domid);
+        if (ret) {
+            rc = ERROR_FAIL;
+            goto out;
+        }
         /* Save DM state into filename */
         ret = libxl__qmp_save(gc, domid, filename, dsps->live);
-        if (ret)
+        if (ret) {
+            rc = ERROR_FAIL;
             unlink(filename);
+            goto out;
+        }
         break;
     default:
-        return ERROR_INVAL;
+        rc = ERROR_INVAL;
+        goto out;
     }
 
-    return ret;
+out:
+    if (rc)
+        LOGD(ERROR, dsps->domid,
+             "failed to suspend device model, rc=%d", rc);
+    dsps->callback_device_model_done(egc, dsps, rc); /* must be last */
 }
 
 static void domain_suspend_common_wait_guest(libxl__egc *egc,
@@ -371,20 +384,15 @@ static void domain_suspend_common_guest_suspended(libxl__egc *egc,
                                          libxl__domain_suspend_state *dsps)
 {
     STATE_AO_GC(dsps->ao);
-    int rc;
 
     libxl__ev_evtchn_cancel(gc, &dsps->guest_evtchn);
     libxl__ev_xswatch_deregister(gc, &dsps->guest_watch);
     libxl__ev_time_deregister(gc, &dsps->guest_timeout);
 
     if (dsps->type == LIBXL_DOMAIN_TYPE_HVM) {
-        rc = libxl__domain_suspend_device_model(gc, dsps);
-        if (rc) {
-            LOGD(ERROR, dsps->domid,
-                 "libxl__domain_suspend_device_model failed ret=%d", rc);
-            domain_suspend_common_done(egc, dsps, rc);
-            return;
-        }
+        dsps->callback_device_model_done = domain_suspend_common_done;
+        libxl__domain_suspend_device_model(egc, dsps); /* must be last */
+        return;
     }
     domain_suspend_common_done(egc, dsps, 0);
 }
tools/libxl/libxl_internal.h +5 -2
index 62492e13d7..5a18937562 100644
@@ -3435,6 +3435,8 @@ struct libxl__domain_suspend_state {
     libxl__ev_time guest_timeout;
 
     const char *dm_savefile;
+    void (*callback_device_model_done)(libxl__egc*,
+                              struct libxl__domain_suspend_state*, int rc);
     void (*callback_common_done)(libxl__egc*,
                                  struct libxl__domain_suspend_state*, int ok);
 };
@@ -4076,8 +4078,9 @@ static inline bool libxl__save_helper_inuse(const libxl__save_helper_state *shs)
     return libxl__ev_child_inuse(&shs->child);
 }
 
-/* Each time the dm needs to be saved, we must call suspend and then save */
-_hidden int libxl__domain_suspend_device_model(libxl__gc *gc,
+/* Each time the dm needs to be saved, we must call suspend and then save
+ * calls dsps->callback_device_model_done when done */
+_hidden void libxl__domain_suspend_device_model(libxl__egc *egc,
                                            libxl__domain_suspend_state *dsps);
 
 _hidden const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid);
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#17 lore raw

PATCH v8 14/17 libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp

In reply to Anthony PERARD

The re-implementation is done because we want to be able to send the file description that QEMU can use to save its state. When QEMU is restricted, it would not be able to write to a path.

This replace both libxl__qmp_stop() and libxl__qmp_save().

qmp_qemu_check_version() was only used by libxl__qmp_save(), so it is replace by a version using libxl__ev_qmp instead.

Coding style fixed in libxl__domain_suspend_device_model() for the return value.

---

Notes:
v8:
Acked

v7:
use libxl__remove_file instead of unlink.
comment that libxl__qmp_suspend_save can callback synchronously

v6:
extract open call from libxl__carefd_opened to respect coding style libxl__qmp_suspend_save now always report success/error
via dsps->callback_device_model_done

v5:
rename goto 'out' label to 'error', as it is use only for errors. re-add/keep the comment about the "live" parameter in dm_state_fd_ready use libxl__remove_file instead of plain unlink

v4:
This patch replace the patch "libxl_qmp: Have QEMU save its state to a file descriptor" from previous version of the serie.
It uses libxl__ev_qmp instead.

 tools/libxl/libxl_dom_suspend.c |  19 +---
 tools/libxl/libxl_internal.h    |  11 ++-
 tools/libxl/libxl_qmp.c         | 160 +++++++++++++++++++++++++-------
 3 files changed, 139 insertions(+), 51 deletions(-)
tools/libxl/libxl_dom_suspend.c +5 -14
index f8ff5cf0c5..d1af3a6573 100644
@@ -34,6 +34,7 @@ int libxl__domain_suspend_init(libxl__egc *egc,
     libxl__ev_evtchn_init(&dsps->guest_evtchn);
     libxl__ev_xswatch_init(&dsps->guest_watch);
     libxl__ev_time_init(&dsps->guest_timeout);
+    libxl__ev_qmp_init(&dsps->qmp);
 
     if (type == LIBXL_DOMAIN_TYPE_INVALID) goto out;
     dsps->type = type;
@@ -72,7 +73,6 @@ void libxl__domain_suspend_device_model(libxl__egc *egc,
                                        libxl__domain_suspend_state *dsps)
 {
     STATE_AO_GC(dsps->ao);
-    int ret = 0;
     int rc = 0;
     uint32_t const domid = dsps->domid;
     const char *const filename = dsps->dm_savefile;
@@ -85,19 +85,9 @@ void libxl__domain_suspend_device_model(libxl__egc *egc,
         break;
     }
     case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN:
-        ret = libxl__qmp_stop(gc, domid);
-        if (ret) {
-            rc = ERROR_FAIL;
-            goto out;
-        }
-        /* Save DM state into filename */
-        ret = libxl__qmp_save(gc, domid, filename, dsps->live);
-        if (ret) {
-            rc = ERROR_FAIL;
-            unlink(filename);
-            goto out;
-        }
-        break;
+        /* calls dsps->callback_device_model_done when done */
+        libxl__qmp_suspend_save(egc, dsps); /* must be last */
+        return;
     default:
         rc = ERROR_INVAL;
         goto out;
@@ -406,6 +396,7 @@ static void domain_suspend_common_done(libxl__egc *egc,
     libxl__ev_evtchn_cancel(gc, &dsps->guest_evtchn);
     libxl__ev_xswatch_deregister(gc, &dsps->guest_watch);
     libxl__ev_time_deregister(gc, &dsps->guest_timeout);
+    libxl__ev_qmp_dispose(gc, &dsps->qmp);
     dsps->callback_common_done(egc, dsps, rc);
 }
 
tools/libxl/libxl_internal.h +6 -5
index 5a18937562..77a8cd5aa5 100644
@@ -1963,13 +1963,8 @@ _hidden int libxl__qmp_pci_del(libxl__gc *gc, int domid,
                                libxl_device_pci *pcidev);
 /* Resume hvm domain */
 _hidden int libxl__qmp_system_wakeup(libxl__gc *gc, int domid);
-/* Suspend QEMU. */
-_hidden int libxl__qmp_stop(libxl__gc *gc, int domid);
 /* Resume QEMU. */
 _hidden int libxl__qmp_resume(libxl__gc *gc, int domid);
-/* Save current QEMU state into fd. */
-_hidden int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename,
-                            bool live);
 /* Load current QEMU state from file. */
 _hidden int libxl__qmp_restore(libxl__gc *gc, int domid, const char *filename);
 /* Set dirty bitmap logging status */
@@ -3433,6 +3428,7 @@ struct libxl__domain_suspend_state {
     libxl__xswait_state pvcontrol;
     libxl__ev_xswatch guest_watch;
     libxl__ev_time guest_timeout;
+    libxl__ev_qmp qmp;
 
     const char *dm_savefile;
     void (*callback_device_model_done)(libxl__egc*,
@@ -3444,6 +3440,11 @@ int libxl__domain_suspend_init(libxl__egc *egc,
                                libxl__domain_suspend_state *dsps,
                                libxl_domain_type type);
 
+/* calls dsps->callback_device_model_done when done
+ * may synchronously calls this callback */
+_hidden void libxl__qmp_suspend_save(libxl__egc *egc,
+                                     libxl__domain_suspend_state *dsps);
+
 struct libxl__domain_save_state {
     /* set by caller of libxl__domain_save */
     libxl__ao *ao;
tools/libxl/libxl_qmp.c +128 -32

186 lines not on this page · raw message

Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#18 lore raw

PATCH v8 15/17 libxl: Remove unused arg from libxl__sendmsg_fds

In reply to Anthony PERARD

From: Anthony PERARD <[email protected]>

Now that datalen' needs to be 1, we can remove it. Also change data' parameter to be a singe byte.

---

Notes:
v8:
new patch, just a follow-up

 tools/libxl/libxl_aoutils.c  | 2 +-
 tools/libxl/libxl_internal.h | 4 ++--
 tools/libxl/libxl_qmp.c      | 2 +-
 tools/libxl/libxl_utils.c    | 7 +++----
 4 files changed, 7 insertions(+), 8 deletions(-)
tools/libxl/libxl_aoutils.c +1 -1
index 9e493cd487..e24e4eed53 100644
@@ -477,7 +477,7 @@ int libxl__openptys(libxl__openpty_state *op,
             r = openpty(&ptyfds[i][0], &ptyfds[i][1], NULL, termp, winp);
             if (r) { LOGE(ERROR,"openpty failed"); _exit(-1); }
         }
-        rc = libxl__sendmsg_fds(gc, sockets[1], "",1,
+        rc = libxl__sendmsg_fds(gc, sockets[1], '\0',
                                 2*count, &ptyfds[0][0], "ptys");
         if (rc) { LOGE(ERROR,"sendmsg to parent failed"); _exit(-1); }
         _exit(0);
tools/libxl/libxl_internal.h +2 -2
index 77a8cd5aa5..c4968ca135 100644
@@ -2010,12 +2010,12 @@ _hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
 _hidden int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
                                        const libxl_domain_config *guest_config);
 
-/* `datalen` should be 1 byte
+/* `data' should contain a byte to send.
  * When dealing with a non-blocking fd, it returns
  *   ERROR_NOT_READY on EWOULDBLOCK
  * logs on other failures. */
 int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
-                       const void *data, size_t datalen,
+                       const char data,
                        int nfds, const int fds[], const char *what);
 
 /* Insists on receiving exactly nfds and datalen.  On failure, logs
tools/libxl/libxl_qmp.c +1 -1
index 02ba156df9..131d94fb29 100644
@@ -1725,7 +1725,7 @@ static int qmp_ev_callback_writable(libxl__gc *gc,
         ev->payload_fd >= 0 &&
         ev->tx_buf_off == 0) {
 
-        rc = libxl__sendmsg_fds(gc, fd, ev->tx_buf, 1,
+        rc = libxl__sendmsg_fds(gc, fd, ev->tx_buf[ev->tx_buf_off],
                                 1, &ev->payload_fd, "QMP socket");
         /* Check for EWOULDBLOCK, and return to try again later */
         if (rc == ERROR_NOT_READY)
tools/libxl/libxl_utils.c +3 -4
index d53db8c37d..e123baa632 100644
@@ -1061,18 +1061,17 @@ void libxl_vcpuinfo_list_free(libxl_vcpuinfo *list, int nr)
 }
 
 int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
-                       const void *data, size_t datalen,
+                       const char data,
                        int nfds, const int fds[], const char *what) {
     struct msghdr msg = { 0 };
     struct cmsghdr *cmsg;
     size_t spaceneeded = nfds * sizeof(fds[0]);
     char control[CMSG_SPACE(spaceneeded)];
+    const size_t datalen = 1;
     struct iovec iov;
     int r;
 
-    assert(datalen == 1);
-
-    iov.iov_base = (void*)data;
+    iov.iov_base = (void*)&data;
     iov.iov_len  = datalen;
 
     /* compose the message */
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v8 15/17
#19 lore raw

Re: [PATCH v8 15/17] libxl: Remove unused arg from libxl__sendmsg_fds

Anthony PERARD writes ("[PATCH v8 15/17] libxl: Remove unused arg from libxl__sendmsg_fds"):

Quoted 4 lines

From: Anthony PERARD <[email protected]>

Now that datalen' needs to be 1, we can remove it. Also change data' parameter to be a singe byte.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw

PATCH v8 16/17 libxl_json: Remove libxl__json_object_append_to from header

In reply to Anthony PERARD

It isn't possible to use libxl__json_object_append_to() outside of libxl_json.c as there is no way to allocate a struct libxl__yajl_ctx. So also remove libxl__yajl_ctx typedef from the internal header.

---

Notes:
v8:
New follow-up patch

 tools/libxl/libxl_internal.h | 5 -----
 tools/libxl/libxl_json.c     | 9 +++++----
 2 files changed, 5 insertions(+), 9 deletions(-)
tools/libxl/libxl_internal.h +0 -5
index c4968ca135..f1e5c2c980 100644
@@ -2071,8 +2071,6 @@ typedef struct {
     libxl__json_object *obj;
 } libxl__json_map_node;
 
-typedef struct libxl__yajl_ctx libxl__yajl_ctx;
-
 static inline bool libxl__json_object_is_null(const libxl__json_object *o)
 {
     return o != NULL && o->type == JSON_NULL;
@@ -2159,9 +2157,6 @@ static inline long long libxl__json_object_get_integer(const libxl__json_object
  */
 _hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt,
                                                      libxl__json_node_type type);
-_hidden int libxl__json_object_append_to(libxl__gc *gc_opt,
-                                         libxl__json_object *obj,
-                                         libxl__yajl_ctx *ctx);
 _hidden libxl__json_object *libxl__json_array_get(const libxl__json_object *o,
                                                   int i);
 _hidden
tools/libxl/libxl_json.c +5 -4
index cd78313df2..9b8ef2cab9 100644
@@ -23,7 +23,7 @@
 
 /* #define DEBUG_ANSWER */
 
-struct libxl__yajl_ctx {
+typedef struct libxl__yajl_ctx {
     libxl__gc *gc;
     yajl_handle hand;
     libxl__json_object *head;
@@ -31,7 +31,7 @@ struct libxl__yajl_ctx {
 #ifdef DEBUG_ANSWER
     yajl_gen g;
 #endif
-};
+} libxl__yajl_ctx;
 
 #ifdef DEBUG_ANSWER
 #if YAJL_VERSION < 20000
@@ -463,8 +463,9 @@ libxl__json_object *libxl__json_object_alloc(libxl__gc *gc,
     return obj;
 }
 
-int libxl__json_object_append_to(libxl__gc *gc, libxl__json_object *obj,
-                                 libxl__yajl_ctx *ctx)
+static int libxl__json_object_append_to(libxl__gc *gc,
+                                        libxl__json_object *obj,
+                                        libxl__yajl_ctx *ctx)
 {
     libxl__json_object *dst = ctx->current;
 
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v8 16/17
#21 lore raw

Re: [PATCH v8 16/17] libxl_json: Remove libxl__json_object_append_to from header

Anthony PERARD writes ("[PATCH v8 16/17] libxl_json: Remove libxl__json_object_append_to from header"):

Quoted 5 lines

It isn't possible to use libxl__json_object_append_to() outside of libxl_json.c as there is no way to allocate a struct libxl__yajl_ctx. So also remove libxl__yajl_ctx typedef from the internal header.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

PATCH v8 17/17 libxl: Add comments to libxl__json_*get* functions

In reply to Anthony PERARD

This comments that libxl__json_object_get_* and libxl__json_*_get functions accept the libxl__json_object parameter to be NULL.

libxl__json_object_to_json also works with NULL.

This also move libxl__json_object_alloc declaration closer to similar functions, and closer to libxl__json_object_free.

---

Notes:
v8:
New follow-up patch

 tools/libxl/libxl_internal.h | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
tools/libxl/libxl_internal.h +14 -4
index f1e5c2c980..8674b3ed47 100644
@@ -2104,6 +2104,10 @@ static inline bool libxl__json_object_is_array(const libxl__json_object *o)
     return o != NULL && o->type == JSON_ARRAY;
 }
 
+/*
+ * `o` may be NULL for all libxl__json_object_get_* functions.
+ */
+
 static inline bool libxl__json_object_get_bool(const libxl__json_object *o)
 {
     if (libxl__json_object_is_bool(o))
@@ -2152,11 +2156,9 @@ static inline long long libxl__json_object_get_integer(const libxl__json_object
 }
 
 /*
- * NOGC can be used with those json_object functions, but the
- * libxl__json_object* will need to be freed with libxl__json_object_free.
+ * `o` may be NULL for the following libxl__json_*_get functions.
  */
-_hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt,
-                                                     libxl__json_node_type type);
+
 _hidden libxl__json_object *libxl__json_array_get(const libxl__json_object *o,
                                                   int i);
 _hidden
@@ -2165,6 +2167,13 @@ libxl__json_map_node *libxl__json_map_node_get(const libxl__json_object *o,
 _hidden const libxl__json_object *libxl__json_map_get(const char *key,
                                           const libxl__json_object *o,
                                           libxl__json_node_type expected_type);
+
+/*
+ * NOGC can be used with those json_object functions, but the
+ * libxl__json_object* will need to be freed with libxl__json_object_free.
+ */
+_hidden libxl__json_object *libxl__json_object_alloc(libxl__gc *gc_opt,
+                                                     libxl__json_node_type type);
 _hidden yajl_status libxl__json_object_to_yajl_gen(libxl__gc *gc_opt,
                                                    yajl_gen hand,
                                                    const libxl__json_object *param);
@@ -2173,6 +2182,7 @@ _hidden void libxl__json_object_free(libxl__gc *gc_opt,
 
 _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc_opt, const char *s);
 
+/* `args` may be NULL */
 _hidden char *libxl__json_object_to_json(libxl__gc *gc,
                                          const libxl__json_object *args);
 /* Always return a valid string, but invalid json on error. */
Signature · 7 lines
Anthony PERARD


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v8 17/17
#23 lore raw

Re: [PATCH v8 17/17] libxl: Add comments to libxl__json_*get* functions

Anthony PERARD writes ("[PATCH v8 17/17] libxl: Add comments to libxl__json_*get* functions"):

Quoted 7 lines

This comments that libxl__json_object_get_* and libxl__json_*_get functions accept the libxl__json_object parameter to be NULL.

libxl__json_object_to_json also works with NULL.

This also move libxl__json_object_alloc declaration closer to similar functions, and closer to libxl__json_object_free.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel