[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH v5 09/15] libxl_qmp: Respond to QMP greeting



Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
---
 tools/libxl/libxl_qmp.c | 41 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index ffbc055110..90308b1598 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -1310,6 +1310,9 @@ int libxl__qmp_initializations(libxl__gc *gc, uint32_t 
domid,
 
 /* ------------ Implementation of libxl__ev_qmp ---------------- */
 
+/* hard coded message ID used for capability negotiation */
+#define QMP_CAPABILITY_NEGOTIATION_MSGID 1
+
 static int qmp_ev_prepare_cmd(libxl__gc *gc,
                               libxl__ev_qmp *ev,
                               const char *cmd,
@@ -1380,7 +1383,7 @@ static int qmp_ev_handle_response(libxl__egc *egc,
     }
 
     id = libxl__json_object_get_integer(o);
-    if (id != ev->id)
+    if (id != ev->id && id != QMP_CAPABILITY_NEGOTIATION_MSGID)
         return 0;
 
     switch (type) {
@@ -1414,9 +1417,21 @@ static int qmp_ev_handle_response(libxl__egc *egc,
         abort();
     }
 
-    ev->id = -1;
-    ev->callback(egc, ev, response, rc); /* must be last */
-    return 1;
+    /*
+     * Even if the current state is capability_negotiation and the correct ID
+     * as been received, call the callback on error.
+     */
+    if (ev->qmp_state == qmp_state_capability_negotiation &&
+        id == QMP_CAPABILITY_NEGOTIATION_MSGID &&
+        rc == 0) {
+        ev->qmp_state = qmp_state_connected;
+        libxl__ev_fd_modify(gc, &ev->qmp_efd, ev->qmp_efd.events | POLLOUT);
+        return 0;
+    } else {
+        ev->id = -1;
+        ev->callback(egc, ev, response, rc); /* must be last */
+        return 1;
+    }
 }
 
 /* return 1 when a user callback as been called */
@@ -1430,6 +1445,11 @@ static int qmp_ev_handle_message(libxl__egc *egc,
     switch (type) {
     case LIBXL__QMP_MESSAGE_TYPE_QMP:
         /* greeting message */
+        assert(ev->qmp_state == qmp_state_connecting);
+        ev->qmp_state = qmp_state_greeting;
+        /* Allow qmp_ev_callback_writable to be called in order to send
+         * qmp_capabilities */
+        libxl__ev_fd_modify(gc, &ev->qmp_efd, ev->qmp_efd.events | POLLOUT);
         return 0;
     case LIBXL__QMP_MESSAGE_TYPE_RETURN:
     case LIBXL__QMP_MESSAGE_TYPE_ERROR:
@@ -1561,7 +1581,13 @@ static int qmp_ev_callback_writable(libxl__gc *gc, 
libxl__ev_qmp *ev, int fd)
      * be send for now will be in this call. */
     libxl__ev_fd_modify(gc, &ev->qmp_efd, ev->qmp_efd.events & ~POLLOUT);
 
-    if (ev->qmp_state == qmp_state_connected) {
+    switch (ev->qmp_state) {
+    case qmp_state_greeting:
+        buf = qmp_prepare_cmd(gc, "qmp_capabilities", NULL,
+                              QMP_CAPABILITY_NEGOTIATION_MSGID, &len);
+        ev->qmp_state = qmp_state_capability_negotiation;
+        break;
+    case qmp_state_connected:
         if (!ev->tx_buf)
             return 0;
 
@@ -1570,7 +1596,8 @@ static int qmp_ev_callback_writable(libxl__gc *gc, 
libxl__ev_qmp *ev, int fd)
         send_fd = libxl__carefd_fd(ev->cfd);
 
         ev->tx_buf = NULL;
-    } else {
+        break;
+    default:
         return 0;
     }
 
@@ -1700,7 +1727,7 @@ 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->last_id_used = 0;
+    ev->last_id_used = QMP_CAPABILITY_NEGOTIATION_MSGID + 1;
 
     ev->rx_buf = NULL;
     ev->tx_buf = NULL;
-- 
Anthony PERARD


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

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.