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 v5 08/15] libxl_qmp: Handle messages from QEMU
Date: Fri, 7 Sep 2018 16:10:57 +0100	[thread overview]
Message-ID: <20180907151104.32306-9-anthony.perard@citrix.com> (raw)
In-Reply-To: <20180907151104.32306-1-anthony.perard@citrix.com>

This patch will handle messages received, and calls callbacks associated
with the libxl__ev_qmp when the expected response is received.

This also print error messages from QMP on behalf of the callback.

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

Notes:
    v5:
        Adding default:abort() in qmp_ev_handle_message.

 tools/libxl/libxl_qmp.c              | 119 +++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl          |   4 +
 tools/libxl/libxl_types_internal.idl |   8 ++
 3 files changed, 131 insertions(+)

diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index 351fb1a292..ffbc055110 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -1331,6 +1331,121 @@ static int qmp_ev_prepare_cmd(libxl__gc *gc,
     return 0;
 }
 
+/*
+ * Handle messages received from QMP server
+ */
+
+static int qmp_error_class_to_libxl_error_code(const libxl__qmp_error_class c)
+{
+    switch (c) {
+    case LIBXL__QMP_ERROR_CLASS_GENERICERROR:
+        return ERROR_QMP_GENERIC_ERROR;
+    case LIBXL__QMP_ERROR_CLASS_COMMANDNOTFOUND:
+        return ERROR_QMP_COMMAND_NOT_FOUND;
+    case LIBXL__QMP_ERROR_CLASS_DEVICENOTACTIVE:
+        return ERROR_QMP_DEVICE_NOT_ACTIVE;
+    case LIBXL__QMP_ERROR_CLASS_DEVICENOTFOUND:
+        return ERROR_QMP_DEVICE_NOT_FOUND;
+    default:
+        abort();
+    }
+}
+
+/* return 1 when a user callback as been called */
+static int qmp_ev_handle_response(libxl__egc *egc,
+                                  libxl__ev_qmp *ev,
+                                  const libxl__json_object *resp,
+                                  libxl__qmp_message_type type)
+{
+    EGC_GC;
+    const libxl__json_object *response;
+    const libxl__json_object *o;
+    int rc;
+    int id;
+
+    o = libxl__json_map_get("id", resp, JSON_INTEGER);
+    if (!o) {
+        const char *error_desc;
+
+        /* unexpected message, attempt to find an error description */
+        o = libxl__json_map_get("error", resp, JSON_MAP);
+        o = libxl__json_map_get("desc", o, JSON_STRING);
+        error_desc = libxl__json_object_get_string(o);
+        if (error_desc)
+            LOGD(ERROR, ev->domid, "%s", error_desc);
+        else
+            LOGD(ERROR, ev->domid, "Received unexpected message: %s",
+                 libxl__json_object_to_json(gc, resp));
+        return 0;
+    }
+
+    id = libxl__json_object_get_integer(o);
+    if (id != ev->id)
+        return 0;
+
+    switch (type) {
+    case LIBXL__QMP_MESSAGE_TYPE_RETURN: {
+        response = libxl__json_map_get("return", resp, JSON_ANY);
+        rc = 0;
+        break;
+    }
+    case LIBXL__QMP_MESSAGE_TYPE_ERROR: {
+        const char *s;
+        const libxl__json_object *err;
+        libxl__qmp_error_class error_class;
+
+        rc = ERROR_FAIL;
+        response = NULL;
+
+        err = libxl__json_map_get("error", resp, JSON_MAP);
+        o = libxl__json_map_get("class", err, JSON_STRING);
+        s = libxl__json_object_get_string(o);
+        if (s && !libxl__qmp_error_class_from_string(s, &error_class))
+            rc = qmp_error_class_to_libxl_error_code(error_class);
+
+        o = libxl__json_map_get("desc", err, JSON_STRING);
+        s = libxl__json_object_get_string(o);
+        if (s)
+            LOGD(ERROR, ev->domid, "%s", s);
+
+        break;
+    }
+    default:
+        abort();
+    }
+
+    ev->id = -1;
+    ev->callback(egc, ev, response, rc); /* must be last */
+    return 1;
+}
+
+/* return 1 when a user callback as been called */
+static int qmp_ev_handle_message(libxl__egc *egc,
+                                 libxl__ev_qmp *ev,
+                                 const libxl__json_object *resp)
+{
+    EGC_GC;
+    libxl__qmp_message_type type = qmp_response_type(resp);
+
+    switch (type) {
+    case LIBXL__QMP_MESSAGE_TYPE_QMP:
+        /* greeting message */
+        return 0;
+    case LIBXL__QMP_MESSAGE_TYPE_RETURN:
+    case LIBXL__QMP_MESSAGE_TYPE_ERROR:
+        return qmp_ev_handle_response(egc, ev, resp, type);
+    case LIBXL__QMP_MESSAGE_TYPE_EVENT:
+        /* Event are ignored */
+        return 0;
+    case LIBXL__QMP_MESSAGE_TYPE_INVALID:
+        return 0;
+    default:
+        abort();
+    }
+
+    return 0;
+}
+
 /*
  * QMP FD callbacks
  */
@@ -1426,6 +1541,10 @@ static int qmp_ev_callback_readable(libxl__egc *egc, libxl__ev_qmp *ev, int fd)
             end = NULL;
 
         LOG_QMP("JSON object received: %s", libxl__json_object_to_json(gc, o));
+
+        /* Must be last and return when the user callback is called */
+        if (qmp_ev_handle_message(egc, ev, o) == 1)
+            return 0;
     }
 
     return 0;
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index 4a385801ba..e104fea941 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -69,6 +69,10 @@ 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, "QMP_GENERIC_ERROR"), # unspecified qmp error
+    (-27, "QMP_COMMAND_NOT_FOUND"), # the requested command has not been found
+    (-28, "QMP_DEVICE_NOT_ACTIVE"), # a device has failed to be become active
+    (-29, "QMP_DEVICE_NOT_FOUND"), # the requested device has not been found
     ], value_namespace = "")
 
 libxl_domain_type = Enumeration("domain_type", [
diff --git a/tools/libxl/libxl_types_internal.idl b/tools/libxl/libxl_types_internal.idl
index 37402e49cb..1536d02215 100644
--- a/tools/libxl/libxl_types_internal.idl
+++ b/tools/libxl/libxl_types_internal.idl
@@ -13,6 +13,14 @@ libxl__qmp_message_type = Enumeration("qmp_message_type", [
     (5, "invalid"),
     ])
 
+libxl__qmp_error_class = Enumeration("qmp_error_class", [
+    # QMP error classes described in QEMU sources code (QapiErrorClass)
+    (1, "GenericError"),
+    (2, "CommandNotFound"),
+    (3, "DeviceNotActive"),
+    (4, "DeviceNotFound"),
+    ])
+
 libxl__device_kind = Enumeration("device_kind", [
     (0, "NONE"),
     (1, "VIF"),
-- 
Anthony PERARD


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

  parent reply	other threads:[~2018-09-07 15:11 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20181015151630.3887-1-ian.jackson@eu.citrix.com>
2018-09-07 15:10 ` [PATCH v5 00/15] libxl: Enable save/restore/migration of a restricted QEMU + libxl__ev_qmp_* Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 01/15] libxl: Design of an async API to issue QMP commands to QEMU Anthony PERARD
2018-10-10 15:18     ` Ian Jackson
2018-10-11 11:17       ` Anthony PERARD
2018-10-11 11:21         ` Ian Jackson
2018-10-10 23:49     ` Marek Marczykowski-Górecki
2018-10-11 14:29       ` Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 02/15] libxl_qmp: Connect to QMP socket Anthony PERARD
2018-10-10 15:29     ` Ian Jackson
2018-10-11 11:27       ` Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 03/15] libxl_qmp: Implement fd callback and read data Anthony PERARD
2018-10-10 15:47     ` Ian Jackson
2018-10-11 14:06       ` Anthony PERARD
2018-10-15 14:04         ` Ian Jackson
2018-10-15 16:35         ` [PATCH v5 03/15] libxl_qmp: Implement fd callback and read data [and 1 more messages] Ian Jackson
2018-10-29 15:52           ` Anthony PERARD
2018-10-29 17:31             ` Ian Jackson
2018-10-30 18:03               ` Anthony PERARD
2018-10-30 18:25                 ` Ian Jackson
2018-09-07 15:10   ` [PATCH v5 04/15] libxl_qmp: Parse JSON input from QMP Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 05/15] libxl_qmp: Separate QMP message generation from qmp_send_prepare Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 06/15] libxl_qmp: Prepare the command to be sent Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 07/15] libxl_qmp: Handle write to QMP socket Anthony PERARD
2018-09-07 15:10   ` Anthony PERARD [this message]
2018-09-07 15:10   ` [PATCH v5 09/15] libxl_qmp: Respond to QMP greeting Anthony PERARD
2018-09-07 15:10   ` [PATCH v5 10/15] libxl_exec: Add libxl__spawn_initiate_failure Anthony PERARD
2018-10-16 14:02     ` Ian Jackson
2018-11-09 12:26       ` Anthony PERARD
2018-09-07 15:11   ` [PATCH v5 11/15] libxl_dm: Pre-open QMP socket for QEMU Anthony PERARD
2018-10-16 14:11     ` Ian Jackson
2018-11-12 14:52       ` Anthony PERARD
2018-11-12 15:14         ` Ian Jackson
2018-11-12 15:22           ` Anthony PERARD
2018-11-12 15:54             ` Ian Jackson
2018-09-07 15:11   ` [PATCH v5 12/15] libxl: QEMU startup sync based on QMP Anthony PERARD
2018-10-16 14:23     ` Ian Jackson
2018-11-12 15:00       ` Anthony PERARD
2018-11-12 15:17         ` Ian Jackson
2018-09-07 15:11   ` [PATCH v5 13/15] libxl_qmp: Store advertised QEMU version in libxl__ev_qmp Anthony PERARD
2018-10-16 14:25     ` Ian Jackson
2018-09-07 15:11   ` [PATCH v5 14/15] libxl: Change libxl__domain_suspend_device_model() to be async Anthony PERARD
2018-10-16 15:14     ` Ian Jackson
2018-09-07 15:11   ` [PATCH v5 15/15] libxl: Re-implement domain_suspend_device_model using libxl__ev_qmp Anthony PERARD
2018-10-16 15:28     ` Ian Jackson
2018-11-09 16:59       ` Anthony PERARD
2018-11-09 17:11         ` Ian Jackson
2018-11-09 17: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=20180907151104.32306-9-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).