Skip to contentSkip to conversation

libxl: QMP client improvement + pci passthrougth insert through QMP

PATCH Superseded Anthony PERARD 3 participants 27 messages Oct 7, 2011 – Oct 10, 2011

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover libxl: QMP client improvement + pci passthrougth insert through QMP

This patch series improve the QMP client in lib XenLight to be able to insert a PCI passthrough device with the upstream QEMU. This require to apply the patch (RFC) series post earlier this week for QEMU.

The first patch create a key in xenstore with the version of the running device model, here: /local/domain/$domid/dm-version

The patch series require one patch from Ian:
"libxl: IDL: autogenerate functions to produce JSON from libxl data structures"

Anthony PERARD (7):
libxl, Introduce dm-version xenstore key.
libxl_qmp, Introduce an opaque argument to the callbacks. libxl_qmp, Introduce list of arguments to qmp_send libxl_qmp, Always insert a command id in the callback_list. libxl_qmp, Return the callback return code in qmp_next. libxl_qmp, Introduce libxl__qmp_pci_add.
libxl, Use QMP to insert a passthrough device when using upstream
QEMU

 tools/libxl/libxl_create.c   |    5 ++
 tools/libxl/libxl_internal.c |   19 +++++
 tools/libxl/libxl_internal.h |    7 ++
 tools/libxl/libxl_pci.c      |   58 ++++++++++------
 tools/libxl/libxl_qmp.c      |  164 ++++++++++++++++++++++++++++++++++-------
 5 files changed, 204 insertions(+), 49 deletions(-)
Signature · 1 line
Anthony PERARD
#1 lore raw

PATCH 1/7 libxl, Introduce dm-version xenstore key.

The key is /local/domain/$domid/dm-version.
This come with libxl__device_model_version_running helper function.

---
 tools/libxl/libxl_create.c   |    5 +++++
 tools/libxl/libxl_internal.c |   19 +++++++++++++++++++
 tools/libxl/libxl_internal.h |    5 +++++
 3 files changed, 29 insertions(+), 0 deletions(-)
tools/libxl/libxl_create.c +5 -0
index e1e3258..3f33d90 100644
@@ -175,6 +175,11 @@ int libxl__domain_build(libxl__gc *gc,
 
     gettimeofday(&start_time, NULL);
 
+    localents = libxl__calloc(gc, 3, sizeof (char *));
+    i = 0;
+    localents[i++] = "dm-version";
+    localents[i++] = libxl__strdup(gc, libxl_device_model_version_to_string(dm_info->device_model_version));
+
     switch (info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
         ret = libxl__build_hvm(gc, domid, info, dm_info, state);
tools/libxl/libxl_internal.c +19 -0
index 3b8a41f..e535c0c 100644
@@ -318,3 +318,22 @@ int libxl__fd_set_cloexec(int fd)
     }
     return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
 }
+
+libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
+                                                               uint32_t domid)
+{
+    char *path = NULL;
+    char *dm_version = NULL;
+    libxl_device_model_version value;
+
+    path = libxl__sprintf(gc, "/local/domain/%d/dm-version", domid);
+    dm_version = libxl__xs_read(gc, XBT_NULL, path);
+    if (!dm_version) {
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+
+    if (libxl_device_model_version_from_string(dm_version, &value) < 0) {
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+    return value;
+}
tools/libxl/libxl_internal.h +5 -0
index 935c899..4dd0f91 100644
@@ -554,4 +554,9 @@ _hidden void libxl__json_object_free(libxl__gc *gc, libxl__json_object *obj);
 
 _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s);
 
+  /* Based on /local/domain/$domid/dm-version xenstore key
+   * default is qemu xen traditional */
+_hidden libxl_device_model_version
+libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
+
 #endif
Signature · 1 line
Anthony PERARD
#2 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

Quoted 1 line · Anthony PERARD

The key is /local/domain/$domid/dm-version.

I've been wondering if we should introduce /libxl/$domid/ as a place for keeping tooltack internal droppings like this. The danger with putting stuff in /local/domain is that domains come to rely on them.

Quoted 14 lines

This come with libxl__device_model_version_running helper function.

---
 tools/libxl/libxl_create.c   |    5 +++++
 tools/libxl/libxl_internal.c |   19 +++++++++++++++++++
 tools/libxl/libxl_internal.h |    5 +++++
 3 files changed, 29 insertions(+), 0 deletions(-)
tools/libxl/libxl_create.c +0 -0
index e1e3258..3f33d90 100644

tools/libxl/libxl_create.c int libxl__domain_build(libxl__gc *gc, · line 182

Quoted 8 lines
tools/libxl/libxl_create.c +5 -0
@@ -175,6 +175,11 @@ int libxl__domain_build(libxl__gc *gc,
 
     gettimeofday(&start_time, NULL);
 
+    localents = libxl__calloc(gc, 3, sizeof (char *));
+    i = 0;
+    localents[i++] = "dm-version";
+    localents[i++] = libxl__strdup(gc, libxl_device_model_version_to_string(dm_info->device_model_version));
+

You don't seem to use this anywhere?

Quoted 12 lines
tools/libxl/libxl_create.c +0 -0
     switch (info->type) {
     case LIBXL_DOMAIN_TYPE_HVM:
         ret = libxl__build_hvm(gc, domid, info, dm_info, state);
tools/libxl/libxl_internal.c +1 -0
index 3b8a41f..e535c0c 100644
@@ -318,3 +318,22 @@ int libxl__fd_set_cloexec(int fd)
     }
     return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
 }
+

tools/libxl/libxl_internal.c int libxl__fd_set_cloexec(int fd) · line 331

Quoted 10 lines
tools/libxl/libxl_internal.c +10 -0
+libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
+                                                               uint32_t domid)
+{
+    char *path = NULL;
+    char *dm_version = NULL;
+    libxl_device_model_version value;
+
+    path = libxl__sprintf(gc, "/local/domain/%d/dm-version", domid);
+    dm_version = libxl__xs_read(gc, XBT_NULL, path);
+    if (!dm_version) {

This would be a bug, since it would imply an inconsistent version of libxl was used to create the domain? (not sure what our policy around this actually is / should be).

Quoted 22 lines
tools/libxl/libxl_internal.c +8 -0
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+
+    if (libxl_device_model_version_from_string(dm_version, &value) < 0) {
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+    return value;
+}
tools/libxl/libxl_internal.h +5 -0
index 935c899..4dd0f91 100644
@@ -554,4 +554,9 @@ _hidden void libxl__json_object_free(libxl__gc *gc, libxl__json_object *obj);
 
 _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s);
 
+  /* Based on /local/domain/$domid/dm-version xenstore key
+   * default is qemu xen traditional */
+_hidden libxl_device_model_version
+libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
+
 #endif
#3 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

Quoted 6 lines · Ian Campbell
Quoted 1 line · Anthony PERARD

The key is /local/domain/$domid/dm-version.

I've been wondering if we should introduce /libxl/$domid/ as a place for keeping tooltack internal droppings like this. The danger with putting stuff in /local/domain is that domains come to rely on them.

I was not sure about where to put the value. So
/libxl/$domid/dm-version is good to me.

Quoted 17 lines
Quoted 17 lines

This come with libxl__device_model_version_running helper function.

---
 tools/libxl/libxl_create.c   |    5 +++++ tools/libxl/libxl_internal.c |   19 +++++++++++++++++++ tools/libxl/libxl_internal.h |    5 +++++

 3 files changed, 29 insertions(+), 0 deletions(-)
tools/libxl/libxl_create.c +0 -0
index e1e3258..3f33d90 100644
@@ -175,6 +175,11 @@ int libxl__domain_build(libxl__gc *gc,
 
      gettimeofday(&start_time, NULL);
 
Quoted 7 lines

tools/libxl/libxl_create.c int libxl__domain_build(libxl__gc *gc, · line 182

Quoted 5 lines
tools/libxl/libxl_create.c +5 -0
+    localents = libxl__calloc(gc, 3, sizeof (char *));
+    i = 0;
+    localents[i++] = "dm-version";
+    localents[i++] = libxl__strdup(gc, libxl_device_model_version_to_string(dm_info->device_model_version));
+

You don't seem to use this anywhere?

This is magic ! :-)

This pointer was not used in this functions, but still given to libxl__build_post() (and written in xenstore to /local/domain). My other option from this function was to write the value in /vm/ which does not seams to be a good place.

Quoted 18 lines
Quoted 18 lines
tools/libxl/libxl_create.c +0 -0
      switch (info->type) {

     case LIBXL_DOMAIN_TYPE_HVM:
         ret = libxl__build_hvm(gc, domid, info, dm_info, state);

tools/libxl/libxl_internal.c +7 -0
index 3b8a41f..e535c0c 100644
@@ -318,3 +318,22 @@ int libxl__fd_set_cloexec(int fd)
      }
      return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  }
+
+libxl_device_model_version libxl__device_model_version_running(libxl__gc *gc,
+                                                               uint32_t domid)
+{
+    char *path = NULL;
+    char *dm_version = NULL;
+    libxl_device_model_version value;
Quoted 8 lines

tools/libxl/libxl_internal.c int libxl__fd_set_cloexec(int fd) · line 331

Quoted 4 lines
tools/libxl/libxl_internal.c +4 -0
+
+    path = libxl__sprintf(gc, "/local/domain/%d/dm-version", domid);
+    dm_version = libxl__xs_read(gc, XBT_NULL, path);
+    if (!dm_version) {

This would be a bug, since it would imply an inconsistent version of libxl was used to create the domain? (not sure what our policy around this actually is / should be).

I just want to give libxl the ability to run after an update. But it's maybe better to print and return an error, so that will give a chance to the user to update is xenstore :). (or use the tool that created the domain).

Quoted 22 lines
Quoted 22 lines
tools/libxl/libxl_internal.c +8 -0
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+
+    if (libxl_device_model_version_from_string(dm_version, &value) < 0) {
+        return LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL;
+    }
+    return value;
+}
tools/libxl/libxl_internal.h +5 -0
index 935c899..4dd0f91 100644
@@ -554,4 +554,9 @@ _hidden void libxl__json_object_free(libxl__gc *gc, libxl__json_object *obj);
 
  _hidden libxl__json_object *libxl__json_parse(libxl__gc *gc, const char *s);
 
+  /* Based on /local/domain/$domid/dm-version xenstore key
+   * default is qemu xen traditional */
+_hidden libxl_device_model_version
+libxl__device_model_version_running(libxl__gc *gc, uint32_t domid);
+
  #endif
Signature · 1 line
Anthony PERARD
#4 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

In reply to Ian Campbell

Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/7] libxl, Introduce dm-version xenstore key."):

Quoted 6 lines · Anthony PERARD
Quoted 1 line

The key is /local/domain/$domid/dm-version.

I've been wondering if we should introduce /libxl/$domid/ as a place for keeping tooltack internal droppings like this. The danger with putting stuff in /local/domain is that domains come to rely on them.

Also, can't the domain write to /local/domain/$domid ? Which we don't want, in this case.

Quoted 4 lines

tools/libxl/libxl_create.c int libxl__domain_build(libxl__gc *gc, · line 182

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+    localents[i++] = libxl__strdup(gc, libxl_device_model_version_to_string(dm_info->device_model_version));
+

You don't seem to use this anywhere?

Also, like several other lines here, it needs to be wrapped (preferably, to 75 columns or so).

Quoted 3 lines

This would be a bug, since it would imply an inconsistent version of libxl was used to create the domain? (not sure what our policy around this actually is / should be).

No, because the same toolstack can create domains with either version of qemu.

Ian.

#5 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

Quoted 7 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/7] libxl, Introduce dm-version xenstore key."):

Quoted 3 lines

This would be a bug, since it would imply an inconsistent version of libxl was used to create the domain? (not sure what our policy around this actually is / should be).

No, because the same toolstack can create domains with either version of qemu.

Such a toolstack would always write the node to one value or the other.

The case I'm referring to is when the domain was created by one version of libxl (which doesn't write the node) but is now being acted upon by a new one (which does write & check it), perhaps due to an intervening upgrade.

There is no way to know which version of qemu was used in this scenario but I question whether it is something we should support in any case (at least outside of stable branches).

Ian.

Quoted 2 lines

Ian.

#6 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

In reply to Ian Jackson
Quoted 10 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/7] libxl, Introduce dm-version xenstore key."):

Quoted 6 lines · Anthony PERARD
Quoted 1 line

The key is /local/domain/$domid/dm-version.

I've been wondering if we should introduce /libxl/$domid/ as a place for keeping tooltack internal droppings like this. The danger with putting stuff in /local/domain is that domains come to rely on them.

Also, can't the domain write to /local/domain/$domid ? Which we don't want, in this case.

Do you think /libxl/$domid is a good idea then or do you have somewhere else in mind?

Ian.

#7 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/7] libxl, Introduce dm-version xenstore key."):

Quoted 2 lines

Do you think /libxl/$domid is a good idea then or do you have somewhere else in mind?

/libxl/$domid is fine I think. We need a clear rule that nothing other than libxl will ever be expected to look there, so it's entirely a private path and does not ever form part of any API.

Ian.

#8 lore raw

Re: [PATCH 1/7] libxl, Introduce dm-version xenstore key.

Quoted 7 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] [PATCH 1/7] libxl, Introduce dm-version xenstore key."):

Quoted 2 lines

Do you think /libxl/$domid is a good idea then or do you have somewhere else in mind?

/libxl/$domid is fine I think. We need a clear rule that nothing other than libxl will ever be expected to look there, so it's entirely a private path and does not ever form part of any API.

Agreed, this includes toolstacks using libxl accessing directly also.

If we come up with a valid reason for toolstacks to store stuff in xenstore then libxl can provide a user-data style interface to mediate this use.

The permissions should be such that only the toolstack domain can access it.

Ian.

#9 lore raw

PATCH 2/7 libxl_qmp, Introduce an opaque argument to the callbacks.

In reply to Anthony PERARD
---
 tools/libxl/libxl_qmp.c |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 deletions(-)
tools/libxl/libxl_qmp.c +19 -10
index 34db29f..5ab9acc 100644
@@ -43,11 +43,13 @@
 #define QMP_RECEIVE_BUFFER_SIZE 4096
 
 typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp,
-                              const libxl__json_object *tree);
+                              const libxl__json_object *tree,
+                              void *opaque);
 
 typedef struct callback_id_pair {
     int id;
     qmp_callback_t callback;
+    void *opaque;
     SIMPLEQ_ENTRY(callback_id_pair) next;
 } callback_id_pair;
 
@@ -70,7 +72,8 @@ struct libxl__qmp_handler {
 };
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd, qmp_callback_t callback);
+                    const char *cmd,
+                    qmp_callback_t callback, void *opaque);
 
 static const int QMP_SOCKET_CONNECT_TIMEOUT = 5;
 
@@ -100,7 +103,8 @@ static int store_serial_port_info(libxl__qmp_handler *qmp,
 }
 
 static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
-                                             const libxl__json_object *o)
+                                             const libxl__json_object *o,
+                                             void *unused)
 {
     const libxl__json_object *obj = NULL;
     const libxl__json_object *label = NULL;
@@ -144,7 +148,7 @@ static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
 }
 
 static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
-                                     const libxl__json_object *o)
+                                     const libxl__json_object *o, void *unused)
 {
     qmp->connected = true;
 
@@ -157,7 +161,7 @@ static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
 
 static int enable_qmp_capabilities(libxl__qmp_handler *qmp)
 {
-    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback);
+    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback, NULL);
 }
 
 /*
@@ -208,7 +212,7 @@ static void qmp_handle_error_response(libxl__qmp_handler *qmp,
     resp = libxl__json_map_get("desc", resp, JSON_STRING);
 
     if (pp) {
-        pp->callback(qmp, NULL);
+        pp->callback(qmp, NULL, pp->opaque);
         if (pp->id == qmp->wait_for_id) {
             /* tell that the id have been processed */
             qmp->wait_for_id = 0;
@@ -241,7 +245,8 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
 
         if (pp) {
             pp->callback(qmp,
-                         libxl__json_map_get("return", resp, JSON_ANY));
+                         libxl__json_map_get("return", resp, JSON_ANY),
+                         pp->opaque);
             if (pp->id == qmp->wait_for_id) {
                 /* tell that the id have been processed */
                 qmp->wait_for_id = 0;
@@ -423,7 +428,8 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd, qmp_callback_t callback)
+                    const char *cmd,
+                    qmp_callback_t callback, void *opaque)
 {
     yajl_gen_config conf = { 0, NULL };
     const unsigned char *buf;
@@ -461,6 +467,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
         }
         elm->id = qmp->last_id_used;
         elm->callback = callback;
+        elm->opaque = opaque;
         SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
     }
 
@@ -483,13 +490,14 @@ error:
 }
 
 static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
-                                qmp_callback_t callback, int ask_timeout)
+                                qmp_callback_t callback, void *opaque,
+                                int ask_timeout)
 {
     int id = 0;
     int ret = 0;
     libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
 
-    id = qmp_send(qmp, cmd, callback);
+    id = qmp_send(qmp, cmd, callback, opaque);
     if (id <= 0) {
         return -1;
     }
@@ -579,6 +587,7 @@ int libxl__qmp_query_serial(libxl__qmp_handler *qmp)
 {
     return qmp_synchronous_send(qmp, "query-chardev",
                                 register_serials_chardev_callback,
+                                NULL,
                                 qmp->timeout);
 }
 
Signature · 1 line
Anthony PERARD
Acked-by on v1 2/7
#10 lore raw

Re: [PATCH 2/7] libxl_qmp, Introduce an opaque argument to the callbacks.

Quoted 1 line · Anthony PERARD
Quoted 123 lines
---
 tools/libxl/libxl_qmp.c |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 deletions(-)
tools/libxl/libxl_qmp.c +19 -10
index 34db29f..5ab9acc 100644
@@ -43,11 +43,13 @@
 #define QMP_RECEIVE_BUFFER_SIZE 4096
 
 typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp,
-                              const libxl__json_object *tree);
+                              const libxl__json_object *tree,
+                              void *opaque);
 
 typedef struct callback_id_pair {
     int id;
     qmp_callback_t callback;
+    void *opaque;
     SIMPLEQ_ENTRY(callback_id_pair) next;
 } callback_id_pair;
 
@@ -70,7 +72,8 @@ struct libxl__qmp_handler {
 };
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd, qmp_callback_t callback);
+                    const char *cmd,
+                    qmp_callback_t callback, void *opaque);
 
 static const int QMP_SOCKET_CONNECT_TIMEOUT = 5;
 
@@ -100,7 +103,8 @@ static int store_serial_port_info(libxl__qmp_handler *qmp,
 }
 
 static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
-                                             const libxl__json_object *o)
+                                             const libxl__json_object *o,
+                                             void *unused)
 {
     const libxl__json_object *obj = NULL;
     const libxl__json_object *label = NULL;
@@ -144,7 +148,7 @@ static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
 }
 
 static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
-                                     const libxl__json_object *o)
+                                     const libxl__json_object *o, void *unused)
 {
     qmp->connected = true;
 
@@ -157,7 +161,7 @@ static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
 
 static int enable_qmp_capabilities(libxl__qmp_handler *qmp)
 {
-    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback);
+    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback, NULL);
 }
 
 /*
@@ -208,7 +212,7 @@ static void qmp_handle_error_response(libxl__qmp_handler *qmp,
     resp = libxl__json_map_get("desc", resp, JSON_STRING);
 
     if (pp) {
-        pp->callback(qmp, NULL);
+        pp->callback(qmp, NULL, pp->opaque);
         if (pp->id == qmp->wait_for_id) {
             /* tell that the id have been processed */
             qmp->wait_for_id = 0;
@@ -241,7 +245,8 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
 
         if (pp) {
             pp->callback(qmp,
-                         libxl__json_map_get("return", resp, JSON_ANY));
+                         libxl__json_map_get("return", resp, JSON_ANY),
+                         pp->opaque);
             if (pp->id == qmp->wait_for_id) {
                 /* tell that the id have been processed */
                 qmp->wait_for_id = 0;
@@ -423,7 +428,8 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd, qmp_callback_t callback)
+                    const char *cmd,
+                    qmp_callback_t callback, void *opaque)
 {
     yajl_gen_config conf = { 0, NULL };
     const unsigned char *buf;
@@ -461,6 +467,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
         }
         elm->id = qmp->last_id_used;
         elm->callback = callback;
+        elm->opaque = opaque;
         SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
     }
 
@@ -483,13 +490,14 @@ error:
 }
 
 static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
-                                qmp_callback_t callback, int ask_timeout)
+                                qmp_callback_t callback, void *opaque,
+                                int ask_timeout)
 {
     int id = 0;
     int ret = 0;
     libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
 
-    id = qmp_send(qmp, cmd, callback);
+    id = qmp_send(qmp, cmd, callback, opaque);
     if (id <= 0) {
         return -1;
     }
@@ -579,6 +587,7 @@ int libxl__qmp_query_serial(libxl__qmp_handler *qmp)
 {
     return qmp_synchronous_send(qmp, "query-chardev",
                                 register_serials_chardev_callback,
+                                NULL,
                                 qmp->timeout);
 }
 
#11 lore raw

PATCH 3/7 libxl_qmp, Introduce list of arguments to qmp_send

In reply to Anthony PERARD
---
 tools/libxl/libxl_qmp.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
tools/libxl/libxl_qmp.c +11 -5
index 5ab9acc..002fb13 100644
@@ -72,7 +72,7 @@ struct libxl__qmp_handler {
 };
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd,
+                    const char *cmd, libxl_key_value_list *args,
                     qmp_callback_t callback, void *opaque);
 
 static const int QMP_SOCKET_CONNECT_TIMEOUT = 5;
@@ -161,7 +161,8 @@ static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
 
 static int enable_qmp_capabilities(libxl__qmp_handler *qmp)
 {
-    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback, NULL);
+    return qmp_send(qmp, "qmp_capabilities", NULL,
+                    qmp_capabilities_callback, NULL);
 }
 
 /*
@@ -428,7 +429,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd,
+                    const char *cmd, libxl_key_value_list *args,
                     qmp_callback_t callback, void *opaque)
 {
     yajl_gen_config conf = { 0, NULL };
@@ -447,6 +448,10 @@ static int qmp_send(libxl__qmp_handler *qmp,
     libxl__yajl_gen_asciiz(hand, cmd);
     libxl__yajl_gen_asciiz(hand, "id");
     yajl_gen_integer(hand, ++qmp->last_id_used);
+    if (args) {
+        libxl__yajl_gen_asciiz(hand, "arguments");
+        libxl_key_value_list_gen_json(hand, args);
+    }
     yajl_gen_map_close(hand);
 
     s = yajl_gen_get_buf(hand, &buf, &len);
@@ -490,6 +495,7 @@ error:
 }
 
 static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
+                                libxl_key_value_list *args,
                                 qmp_callback_t callback, void *opaque,
                                 int ask_timeout)
 {
@@ -497,7 +503,7 @@ static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
     int ret = 0;
     libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
 
-    id = qmp_send(qmp, cmd, callback, opaque);
+    id = qmp_send(qmp, cmd, args, callback, opaque);
     if (id <= 0) {
         return -1;
     }
@@ -585,7 +591,7 @@ void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid)
 
 int libxl__qmp_query_serial(libxl__qmp_handler *qmp)
 {
-    return qmp_synchronous_send(qmp, "query-chardev",
+    return qmp_synchronous_send(qmp, "query-chardev", NULL,
                                 register_serials_chardev_callback,
                                 NULL,
                                 qmp->timeout);
Signature · 1 line
Anthony PERARD
Acked-by on v1 3/7
#12 lore raw

Re: [PATCH 3/7] libxl_qmp, Introduce list of arguments to qmp_send

Quoted 1 line · Anthony PERARD
Quoted 73 lines
---
 tools/libxl/libxl_qmp.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
tools/libxl/libxl_qmp.c +11 -5
index 5ab9acc..002fb13 100644
@@ -72,7 +72,7 @@ struct libxl__qmp_handler {
 };
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd,
+                    const char *cmd, libxl_key_value_list *args,
                     qmp_callback_t callback, void *opaque);
 
 static const int QMP_SOCKET_CONNECT_TIMEOUT = 5;
@@ -161,7 +161,8 @@ static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
 
 static int enable_qmp_capabilities(libxl__qmp_handler *qmp)
 {
-    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback, NULL);
+    return qmp_send(qmp, "qmp_capabilities", NULL,
+                    qmp_capabilities_callback, NULL);
 }
 
 /*
@@ -428,7 +429,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
-                    const char *cmd,
+                    const char *cmd, libxl_key_value_list *args,
                     qmp_callback_t callback, void *opaque)
 {
     yajl_gen_config conf = { 0, NULL };
@@ -447,6 +448,10 @@ static int qmp_send(libxl__qmp_handler *qmp,
     libxl__yajl_gen_asciiz(hand, cmd);
     libxl__yajl_gen_asciiz(hand, "id");
     yajl_gen_integer(hand, ++qmp->last_id_used);
+    if (args) {
+        libxl__yajl_gen_asciiz(hand, "arguments");
+        libxl_key_value_list_gen_json(hand, args);
+    }
     yajl_gen_map_close(hand);
 
     s = yajl_gen_get_buf(hand, &buf, &len);
@@ -490,6 +495,7 @@ error:
 }
 
 static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
+                                libxl_key_value_list *args,
                                 qmp_callback_t callback, void *opaque,
                                 int ask_timeout)
 {
@@ -497,7 +503,7 @@ static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd,
     int ret = 0;
     libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
 
-    id = qmp_send(qmp, cmd, callback, opaque);
+    id = qmp_send(qmp, cmd, args, callback, opaque);
     if (id <= 0) {
         return -1;
     }
@@ -585,7 +591,7 @@ void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid)
 
 int libxl__qmp_query_serial(libxl__qmp_handler *qmp)
 {
-    return qmp_synchronous_send(qmp, "query-chardev",
+    return qmp_synchronous_send(qmp, "query-chardev", NULL,
                                 register_serials_chardev_callback,
                                 NULL,
                                 qmp->timeout);
#13 lore raw

PATCH 4/7 libxl_qmp, Always insert a command id in the callback_list.

In reply to Anthony PERARD

Because the function qmp_synchronous_send rely on the presence of the id in the callback_list.

---
 tools/libxl/libxl_qmp.c |   34 ++++++++++++++++++----------------
 1 files changed, 18 insertions(+), 16 deletions(-)
tools/libxl/libxl_qmp.c +18 -16
index 002fb13..1594a4f 100644
@@ -213,7 +213,9 @@ static void qmp_handle_error_response(libxl__qmp_handler *qmp,
     resp = libxl__json_map_get("desc", resp, JSON_STRING);
 
     if (pp) {
-        pp->callback(qmp, NULL, pp->opaque);
+        if (pp->callback) {
+            pp->callback(qmp, NULL, pp->opaque);
+        }
         if (pp->id == qmp->wait_for_id) {
             /* tell that the id have been processed */
             qmp->wait_for_id = 0;
@@ -245,9 +247,11 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
         callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
 
         if (pp) {
-            pp->callback(qmp,
-                         libxl__json_map_get("return", resp, JSON_ANY),
-                         pp->opaque);
+            if (pp->callback) {
+                pp->callback(qmp,
+                             libxl__json_map_get("return", resp, JSON_ANY),
+                             pp->opaque);
+            }
             if (pp->id == qmp->wait_for_id) {
                 /* tell that the id have been processed */
                 qmp->wait_for_id = 0;
@@ -437,6 +441,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
     unsigned int len = 0;
     yajl_gen_status s;
     yajl_gen hand;
+    callback_id_pair *elm = NULL;
 
     hand = yajl_gen_alloc(&conf, NULL);
     if (!hand) {
@@ -462,19 +467,16 @@ static int qmp_send(libxl__qmp_handler *qmp,
         return -1;
     }
 
-    if (callback) {
-        callback_id_pair *elm = malloc(sizeof (callback_id_pair));
-        if (elm == NULL) {
-            LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
-                             "Failed to allocate a QMP callback");
-            yajl_gen_free(hand);
-            return -1;
-        }
-        elm->id = qmp->last_id_used;
-        elm->callback = callback;
-        elm->opaque = opaque;
-        SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
+    elm = malloc(sizeof (callback_id_pair));
+    if (elm == NULL) {
+        LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
+                         "Failed to allocate a QMP callback");
+        goto error;
     }
+    elm->id = qmp->last_id_used;
+    elm->callback = callback;
+    elm->opaque = opaque;
+    SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
 
     LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "next qmp command: '%s'", buf);
 
Signature · 1 line
Anthony PERARD
Acked-by on v1 4/7
#14 lore raw

Re: [PATCH 4/7] libxl_qmp, Always insert a command id in the callback_list.

Quoted 4 lines · Anthony PERARD

Because the function qmp_synchronous_send rely on the presence of the id in the callback_list.

Quoted 71 lines
---
 tools/libxl/libxl_qmp.c |   34 ++++++++++++++++++----------------
 1 files changed, 18 insertions(+), 16 deletions(-)
tools/libxl/libxl_qmp.c +18 -16
index 002fb13..1594a4f 100644
@@ -213,7 +213,9 @@ static void qmp_handle_error_response(libxl__qmp_handler *qmp,
     resp = libxl__json_map_get("desc", resp, JSON_STRING);
 
     if (pp) {
-        pp->callback(qmp, NULL, pp->opaque);
+        if (pp->callback) {
+            pp->callback(qmp, NULL, pp->opaque);
+        }
         if (pp->id == qmp->wait_for_id) {
             /* tell that the id have been processed */
             qmp->wait_for_id = 0;
@@ -245,9 +247,11 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
         callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
 
         if (pp) {
-            pp->callback(qmp,
-                         libxl__json_map_get("return", resp, JSON_ANY),
-                         pp->opaque);
+            if (pp->callback) {
+                pp->callback(qmp,
+                             libxl__json_map_get("return", resp, JSON_ANY),
+                             pp->opaque);
+            }
             if (pp->id == qmp->wait_for_id) {
                 /* tell that the id have been processed */
                 qmp->wait_for_id = 0;
@@ -437,6 +441,7 @@ static int qmp_send(libxl__qmp_handler *qmp,
     unsigned int len = 0;
     yajl_gen_status s;
     yajl_gen hand;
+    callback_id_pair *elm = NULL;
 
     hand = yajl_gen_alloc(&conf, NULL);
     if (!hand) {
@@ -462,19 +467,16 @@ static int qmp_send(libxl__qmp_handler *qmp,
         return -1;
     }
 
-    if (callback) {
-        callback_id_pair *elm = malloc(sizeof (callback_id_pair));
-        if (elm == NULL) {
-            LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
-                             "Failed to allocate a QMP callback");
-            yajl_gen_free(hand);
-            return -1;
-        }
-        elm->id = qmp->last_id_used;
-        elm->callback = callback;
-        elm->opaque = opaque;
-        SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
+    elm = malloc(sizeof (callback_id_pair));
+    if (elm == NULL) {
+        LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
+                         "Failed to allocate a QMP callback");
+        goto error;
     }
+    elm->id = qmp->last_id_used;
+    elm->callback = callback;
+    elm->opaque = opaque;
+    SIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
 
     LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "next qmp command: '%s'", buf);
 
#15 lore raw

PATCH 5/7 libxl_qmp, Return the callback return code in qmp_next.

In reply to Anthony PERARD

So, if there is an error in the answer given by QEMU, the function qmp_synchronous_send while know.

---
 tools/libxl/libxl_qmp.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)
tools/libxl/libxl_qmp.c +8 -6
index 1594a4f..cd3e4e4 100644
@@ -233,6 +233,7 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
                                const libxl__json_object *resp)
 {
     libxl__qmp_message_type type = LIBXL__QMP_MESSAGE_TYPE_INVALID;
+    int rc = 0;
 
     type = qmp_response_type(qmp, resp);
     LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG,
@@ -241,14 +242,14 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
     switch (type) {
     case LIBXL__QMP_MESSAGE_TYPE_QMP:
         /* On the greeting message from the server, enable QMP capabilities */
-        enable_qmp_capabilities(qmp);
+        rc = enable_qmp_capabilities(qmp);
         break;
     case LIBXL__QMP_MESSAGE_TYPE_RETURN: {
         callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
 
         if (pp) {
             if (pp->callback) {
-                pp->callback(qmp,
+                rc = pp->callback(qmp,
                              libxl__json_map_get("return", resp, JSON_ANY),
                              pp->opaque);
             }
@@ -263,13 +264,13 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
     }
     case LIBXL__QMP_MESSAGE_TYPE_ERROR:
         qmp_handle_error_response(qmp, resp);
-        break;
+        return -1;
     case LIBXL__QMP_MESSAGE_TYPE_EVENT:
         break;
     case LIBXL__QMP_MESSAGE_TYPE_INVALID:
         return -1;
     }
-    return 0;
+    return rc;
 }
 
 /*
@@ -358,6 +359,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 
     char *incomplete = NULL;
     size_t incomplete_size = 0;
+    int rc = 0;
 
     do {
         fd_set rfds;
@@ -416,7 +418,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
                 s = end + 2;
 
                 if (o) {
-                    qmp_handle_response(qmp, o);
+                    rc = qmp_handle_response(qmp, o);
                     libxl__json_object_free(gc, o);
                 } else {
                     LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
@@ -429,7 +431,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
         } while (s < s_end);
    } while (s < s_end);
 
-    return 1;
+    return rc;
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
Signature · 1 line
Anthony PERARD
#16 lore raw

Re: [PATCH 5/7] libxl_qmp, Return the callback return code in qmp_next.

Quoted 2 lines · Anthony PERARD

So, if there is an error in the answer given by QEMU, the function qmp_synchronous_send while know.

                       "will"
Quoted 36 lines
---
 tools/libxl/libxl_qmp.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)
tools/libxl/libxl_qmp.c +3 -2
index 1594a4f..cd3e4e4 100644
@@ -233,6 +233,7 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
                                const libxl__json_object *resp)
 {
     libxl__qmp_message_type type = LIBXL__QMP_MESSAGE_TYPE_INVALID;
+    int rc = 0;
 
     type = qmp_response_type(qmp, resp);
     LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG,
@@ -241,14 +242,14 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
     switch (type) {
     case LIBXL__QMP_MESSAGE_TYPE_QMP:
         /* On the greeting message from the server, enable QMP capabilities */
-        enable_qmp_capabilities(qmp);
+        rc = enable_qmp_capabilities(qmp);
         break;
     case LIBXL__QMP_MESSAGE_TYPE_RETURN: {
         callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
 
         if (pp) {
             if (pp->callback) {
-                pp->callback(qmp,
+                rc = pp->callback(qmp,
                              libxl__json_map_get("return", resp, JSON_ANY),
                              pp->opaque);
             }

tools/libxl/libxl_qmp.c static int qmp_handle_response(libxl__qmp_handler *qmp, · line 267

Quoted 5 lines
tools/libxl/libxl_qmp.c +1 -1
@@ -263,13 +264,13 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
     }
     case LIBXL__QMP_MESSAGE_TYPE_ERROR:
         qmp_handle_error_response(qmp, resp);
-        break;
+        return -1;

A mixture or "return -1" and a rc variable returned at the outermost level is a bit odd. You should either always set rc and fall through or always return at the end of each case.

Quoted 19 lines
tools/libxl/libxl_qmp.c +2 -1
     case LIBXL__QMP_MESSAGE_TYPE_EVENT:
         break;
     case LIBXL__QMP_MESSAGE_TYPE_INVALID:
         return -1;
     }
-    return 0;
+    return rc;
 }
 
 /*
@@ -358,6 +359,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 
     char *incomplete = NULL;
     size_t incomplete_size = 0;
+    int rc = 0;
 
     do {
         fd_set rfds;

tools/libxl/libxl_qmp.c static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp) · line 421

Quoted 5 lines
tools/libxl/libxl_qmp.c +1 -1
@@ -416,7 +418,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
                 s = end + 2;
 
                 if (o) {
-                    qmp_handle_response(qmp, o);
+                    rc = qmp_handle_response(qmp, o);

If rc now indicates error do we need to bail straight away or need to keep going around this loop? (Or is it certain we will immediately fall out of the loop after this?)

Quoted 12 lines
tools/libxl/libxl_qmp.c +1 -1
                     libxl__json_object_free(gc, o);
                 } else {
                     LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
@@ -429,7 +431,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
         } while (s < s_end);
    } while (s < s_end);
 
-    return 1;
+    return rc;
 }
 
 static int qmp_send(libxl__qmp_handler *qmp,
#17 lore raw

Re: [PATCH 5/7] libxl_qmp, Return the callback return code in qmp_next.

Quoted 42 lines · Ian Campbell
Quoted 2 lines · Anthony PERARD

So, if there is an error in the answer given by QEMU, the function qmp_synchronous_send while know.

                      "will"

Quoted 37 lines

---
 tools/libxl/libxl_qmp.c |   14 ++++++++------

 1 files changed, 8 insertions(+), 6 deletions(-)
tools/libxl/libxl_qmp.c +1 -0
index 1594a4f..cd3e4e4 100644
@@ -233,6 +233,7 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
                                 const libxl__json_object *resp)
  {
      libxl__qmp_message_type type = LIBXL__QMP_MESSAGE_TYPE_INVALID;
+    int rc = 0;
 
      type = qmp_response_type(qmp, resp);
      LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG,
@@ -241,14 +242,14 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
      switch (type) {

     case LIBXL__QMP_MESSAGE_TYPE_QMP:
         /* On the greeting message from the server, enable QMP capabilities */
-        enable_qmp_capabilities(qmp);
+        rc = enable_qmp_capabilities(qmp);
         break;
     case LIBXL__QMP_MESSAGE_TYPE_RETURN: {
         callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);

         if (pp) {
             if (pp->callback) {
-                pp->callback(qmp,
+                rc = pp->callback(qmp, libxl__json_map_get("return", resp, JSON_ANY), pp->opaque);
             }

tools/libxl/libxl_qmp.c +0 -0
@@ -263,13 +264,13 @@ static int qmp_handle_response(libxl__qmp_handler *qmp,
      }
Quoted 8 lines
Quoted 4 lines

     case LIBXL__QMP_MESSAGE_TYPE_ERROR:
         qmp_handle_error_response(qmp, resp);
-        break;
+        return -1;

A mixture or "return -1" and a rc variable returned at the outermost level is a bit odd. You should either always set rc and fall through or always return at the end of each case.

OK, I will change that.

Quoted 21 lines
Quoted 21 lines

     case LIBXL__QMP_MESSAGE_TYPE_EVENT:
         break;
     case LIBXL__QMP_MESSAGE_TYPE_INVALID:
         return -1;
     }
-    return 0;
+    return rc;
 }

 /*

tools/libxl/libxl_qmp.c +1 -0
@@ -358,6 +359,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
 
      char *incomplete = NULL;
      size_t incomplete_size = 0;
+    int rc = 0;
 
      do {
          fd_set rfds;
@@ -416,7 +418,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
                  s = end + 2;
 
Quoted 7 lines

tools/libxl/libxl_qmp.c static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp) · line 421

Quoted 3 lines
tools/libxl/libxl_qmp.c +1 -1
                  if (o) {
-                    qmp_handle_response(qmp, o);
+                    rc = qmp_handle_response(qmp, o);

If rc now indicates error do we need to bail straight away or need to keep going around this loop? (Or is it certain we will immediately fall out of the loop after this?)

We can not be sure that we will return, because it could be another message in the butffer. So I should return if there is a protocol error. But I think that I should keep seperate the return code of a callback, so only the interested function (qmp_synchronous_send) will read it (and return to the caller).

Quoted 12 lines
Quoted 12 lines
tools/libxl/libxl_qmp.c +1 -1
                      libxl__json_object_free(gc, o);
                  } else {
                      LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
@@ -429,7 +431,7 @@ static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp)
          } while (s < s_end);
     } while (s < s_end);
 
-    return 1;
+    return rc;
  }
 
  static int qmp_send(libxl__qmp_handler *qmp,
Signature · 1 line
Anthony PERARD
#18 lore raw

Re: [PATCH 5/7] libxl_qmp, Return the callback return code in qmp_next.

Quoted 11 lines · Anthony PERARD
Quoted 1 line

Review static int qmp_next(libxl__gc *gc,

Quoted 1 line
(quoted diff) +0 -0
@@ -416,7 +418,7 @@ static int qmp_next(libxl__gc *gc,

libxl__qmp_handler *qmp)

Quoted 7 lines

tools/libxl/libxl_qmp.c static int qmp_next(libxl__gc *gc, libxl__qmp_handler *qmp) · line 421

Quoted 5 lines
tools/libxl/libxl_qmp.c +1 -1
                 s = end + 2;
 
                 if (o) {
-                    qmp_handle_response(qmp, o);
+                    rc = qmp_handle_response(qmp, o);

If rc now indicates error do we need to bail straight away or need

to

Quoted 1 line

keep going around this loop? (Or is it certain we will immediately

Quoted 8 lines

fall

Quoted 1 line

out of the loop after this?)

We can not be sure that we will return, because it could be another message in the butffer. So I should return if there is a protocol error. But I think that I should keep seperate the return code of a callback, so only the interested function (qmp_synchronous_send) will read it (and return to the caller).

I think this means the caller cannot tell which request or how many errors have occurred in total.

I think that the callbacks should take a success/error code. If that particular function is interested in propagating the error then they can save it in their opaque data pointer to be retrieved by the caller of the sync_send.

Another alternative could be to add an rc member to callback_id_pair (making it more of a qmp request handle than an callback/id pair) and push the allocation/free of that datastructure back up into the caller of qmp_send (perhaps many callers of sync_send would just use an on stack variable). Callers could then check the result of a specific request by looking at that new member.

Ian.

#19 lore raw

PATCH 6/7 libxl_qmp, Introduce libxl__qmp_pci_add.

In reply to Anthony PERARD

This function insert a PCI passthrough device in qemu.

---
 tools/libxl/libxl_internal.h |    2 +
 tools/libxl/libxl_qmp.c      |   89 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +2 -0
index 4dd0f91..78e1be2 100644
@@ -119,6 +119,7 @@ typedef struct {
 } libxl__device;
 
 #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
+#define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
 #define AUTO_PHP_SLOT          0x100
 #define SYSFS_PCI_DEV          "/sys/bus/pci/devices"
 #define SYSFS_PCIBACK_DRIVER   "/sys/bus/pci/drivers/pciback"
@@ -444,6 +445,7 @@ _hidden libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx,
                                                   uint32_t domid);
 /* ask to QEMU the serial port information and store it in xenstore. */
 _hidden int libxl__qmp_query_serial(libxl__qmp_handler *qmp);
+_hidden int libxl__qmp_pci_add(libxl__gc *gc, int d, libxl_device_pci *pcidev);
 /* close and free the QMP handler */
 _hidden void libxl__qmp_close(libxl__qmp_handler *qmp);
 /* remove the socket file, if the file has already been removed,
tools/libxl/libxl_qmp.c +89 -0

101 lines not on this page · raw message

Signature · 1 line
Anthony PERARD
#20 lore raw

Re: [PATCH 6/7] libxl_qmp, Introduce libxl__qmp_pci_add.

Quoted 13 lines · Anthony PERARD

This function insert a PCI passthrough device in qemu.

---
 tools/libxl/libxl_internal.h |    2 +
 tools/libxl/libxl_qmp.c      |   89 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +0 -0
index 4dd0f91..78e1be2 100644

tools/libxl/libxl_internal.h typedef struct { · line 122

Quoted 4 lines
tools/libxl/libxl_internal.h +1 -0
@@ -119,6 +119,7 @@ typedef struct {
 } libxl__device;
 
 #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
+#define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))

No need to include the domain or dev in this? The name is pretty weird if you don't include the dev portion. Looks like this is actually a vdevfn? xl has an open coded instance of this pattern for that -- worth adding a public macro?

FWIW pcidev_encode_bdf() exists which encodes the whole lot from a libxl_device_pci, if that what you end up needing.

Quoted 118 lines
tools/libxl/libxl_internal.h +1 -0
 #define AUTO_PHP_SLOT          0x100
 #define SYSFS_PCI_DEV          "/sys/bus/pci/devices"
 #define SYSFS_PCIBACK_DRIVER   "/sys/bus/pci/drivers/pciback"
@@ -444,6 +445,7 @@ _hidden libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx,
                                                   uint32_t domid);
 /* ask to QEMU the serial port information and store it in xenstore. */
 _hidden int libxl__qmp_query_serial(libxl__qmp_handler *qmp);
+_hidden int libxl__qmp_pci_add(libxl__gc *gc, int d, libxl_device_pci *pcidev);
 /* close and free the QMP handler */
 _hidden void libxl__qmp_close(libxl__qmp_handler *qmp);
 /* remove the socket file, if the file has already been removed,
tools/libxl/libxl_qmp.c +89 -0
index cd3e4e4..c39a1ac 100644
@@ -41,6 +41,7 @@
  */
 
 #define QMP_RECEIVE_BUFFER_SIZE 4096
+#define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x"
 
 typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp,
                               const libxl__json_object *tree,
@@ -601,6 +602,94 @@ int libxl__qmp_query_serial(libxl__qmp_handler *qmp)
                                 qmp->timeout);
 }
 
+static int pci_add_callback(libxl__qmp_handler *qmp,
+                            const libxl__json_object *response, void *opaque)
+{
+    libxl_device_pci *pcidev = opaque;
+    const libxl__json_object *bus = NULL;
+    libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
+    int i, j, rc = -1;
+    char *asked_id = libxl__sprintf(&gc, PCI_PT_QDEV_ID,
+                                    pcidev->bus, pcidev->dev, pcidev->func);
+
+    for (i = 0; (bus = libxl__json_array_get(response, i)); i++) {
+        const libxl__json_object *devices = NULL;
+        const libxl__json_object *device = NULL;
+        const libxl__json_object *o = NULL;
+        const char *id = NULL;
+
+        devices = libxl__json_map_get("devices", bus, JSON_ARRAY);
+
+        for (j = 0; (device = libxl__json_array_get(devices, j)); j++) {
+             o = libxl__json_map_get("qdev_id", device, JSON_STRING);
+             id = libxl__json_object_get_string(o);
+
+             if (id && strcmp(asked_id, id) == 0) {
+                 int dev_slot, dev_func;
+
+                 o = libxl__json_map_get("slot", device, JSON_INTEGER);
+                 if (!o)
+                     goto out;
+                 dev_slot = libxl__json_object_get_integer(o);
+                 o = libxl__json_map_get("function", device, JSON_INTEGER);
+                 if (!o)
+                     goto out;
+                 dev_func = libxl__json_object_get_integer(o);
+
+                 pcidev->vdevfn = PCI_DEVFN(dev_slot, dev_func);
+
+                 rc = 0;
+                 goto out;
+             }
+        }
+    }
+
+
+out:
+    libxl__free_all(&gc);
+    return rc;
+}
+
+int libxl__qmp_pci_add(libxl__gc *gc, int domid, libxl_device_pci *pcidev)
+{
+    libxl__qmp_handler *qmp = NULL;
+    flexarray_t *parameters = NULL;
+    libxl_key_value_list args = NULL;
+    char *hostaddr = NULL;
+    int rc = 0;
+
+    qmp = libxl__qmp_initialize(libxl__gc_owner(gc), domid);
+    if (!qmp)
+        return -1;
+
+    hostaddr = libxl__sprintf(gc, "%04x:%02x:%02x.%01x", pcidev->domain,
+                              pcidev->bus, pcidev->dev, pcidev->func);
+    if (!hostaddr)
+        return -1;
+
+    parameters = flexarray_make(6, 1);
+    flexarray_append_pair(parameters, "driver", "xen-pci-passthrough");
+    flexarray_append_pair(parameters, "id",
+                          libxl__sprintf(gc, PCI_PT_QDEV_ID,
+                                         pcidev->bus, pcidev->dev,
+                                         pcidev->func));
+    flexarray_append_pair(parameters, "hostaddr", hostaddr);
+    args = libxl__xs_kvs_of_flexarray(gc, parameters, 6);
+    if (!args)
+        return -1;
+
+    rc = qmp_synchronous_send(qmp, "device_add", &args,
+                              NULL, NULL, qmp->timeout);
+    if (rc == 0) {
+        rc = qmp_synchronous_send(qmp, "query-pci", NULL,
+                                  pci_add_callback, pcidev, qmp->timeout);
+    }
+
+    flexarray_free(parameters);
+    libxl__qmp_close(qmp);
+    return rc;
+}
+
 int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid)
 {
     libxl__qmp_handler *qmp = NULL;
#21 lore raw

Re: [PATCH 6/7] libxl_qmp, Introduce libxl__qmp_pci_add.

Quoted 16 lines · Ian Campbell
Quoted 15 lines · Anthony PERARD

This function insert a PCI passthrough device in qemu.

---
 tools/libxl/libxl_internal.h |    2 +
 tools/libxl/libxl_qmp.c      |   89 ++++++++++++++++++++++++++++++++++++++++++

 2 files changed, 91 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +0 -0
index 4dd0f91..78e1be2 100644
@@ -119,6 +119,7 @@ typedef struct {
  } libxl__device;
 
Quoted 7 lines

tools/libxl/libxl_internal.h typedef struct { · line 122

Quoted 2 lines
tools/libxl/libxl_internal.h +1 -0
  #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
+#define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))

No need to include the domain or dev in this? The name is pretty weird if you don't include the dev portion.  Looks like this is actually a vdevfn? xl has an open coded instance of this pattern for that -- worth adding a public macro?

I have actual copy the macro from qemu, and I think "slot === dev". I think the macro come from linux/pci.h.
But yes, I use it to store the vdevfn.

Quoted 2 lines

FWIW pcidev_encode_bdf() exists which encodes the whole lot from a libxl_device_pci, if that what you end up needing.

If the vdefvn should contain the virtual bus and domain number, so yes, that will be the function I need.

Signature · 1 line
Anthony PERARD
#22 lore raw

Re: [PATCH 6/7] libxl_qmp, Introduce libxl__qmp_pci_add.

Quoted 25 lines · Anthony PERARD
Quoted 16 lines · Ian Campbell
Quoted 15 lines · Anthony PERARD

This function insert a PCI passthrough device in qemu.

---
 tools/libxl/libxl_internal.h |    2 +
 tools/libxl/libxl_qmp.c      |   89 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 0 deletions(-)
tools/libxl/libxl_internal.h +0 -0
index 4dd0f91..78e1be2 100644
@@ -119,6 +119,7 @@ typedef struct {
 } libxl__device;
 
Quoted 7 lines

tools/libxl/libxl_internal.h typedef struct { · line 122

Quoted 2 lines
tools/libxl/libxl_internal.h +1 -0
 #define XC_PCI_BDF             "0x%x, 0x%x, 0x%x, 0x%x"
+#define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))

No need to include the domain or dev in this? The name is pretty weird if you don't include the dev portion. Looks like this is actually a vdevfn? xl has an open coded instance of this pattern for that -- worth adding a public macro?

Quoted 1 line

I have actual copy the macro from qemu, and I think "slot === dev".

It appears to yes.

Quoted 9 lines

I
think the macro come from linux/pci.h.
But yes, I use it to store the vdevfn.

Quoted 2 lines

FWIW pcidev_encode_bdf() exists which encodes the whole lot from a libxl_device_pci, if that what you end up needing.

If the vdefvn should contain the virtual bus and domain number, so yes, that will be the function I need.

I don't think it does in the old qemu stuff, but I don't know about upstream, I suspect not.

Quoted 1 line
#23 lore raw

PATCH 7/7 libxl, Use QMP to insert a passthrough device when using upstream QEMU

In reply to Anthony PERARD
---
 tools/libxl/libxl_pci.c |   58 ++++++++++++++++++++++++++++++-----------------
 1 files changed, 37 insertions(+), 21 deletions(-)
tools/libxl/libxl_pci.c +37 -21
index 1523cf0..b6cb4a3 100644
@@ -613,27 +613,43 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
                                          NULL, NULL, NULL) < 0) {
             return ERROR_FAIL;
         }
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
-        state = libxl__xs_read(gc, XBT_NULL, path);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/parameter", domid);
-        if (pcidev->vdevfn)
-            libxl__xs_write(gc, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
-                           pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
-        else
-            libxl__xs_write(gc, XBT_NULL, path, PCI_BDF, pcidev->domain,
-                           pcidev->bus, pcidev->dev, pcidev->func);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/command", domid);
-        xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
-        rc = libxl__wait_for_device_model(gc, domid, NULL, NULL,
-                                          pci_ins_check, state);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/parameter", domid);
-        vdevfn = libxl__xs_read(gc, XBT_NULL, path);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
-        if ( rc < 0 )
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "qemu refused to add device: %s", vdevfn);
-        else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 )
-            rc = -1;
-        xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
+        if (libxl__device_model_version_running(gc, domid)
+            == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
+            rc = libxl__qmp_pci_add(gc, domid, pcidev);
+        } else {
+            path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state",
+                                  domid);
+            state = libxl__xs_read(gc, XBT_NULL, path);
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/parameter",
+                                  domid);
+            if (pcidev->vdevfn) {
+                libxl__xs_write(gc, XBT_NULL, path, PCI_BDF_VDEVFN,
+                                pcidev->domain, pcidev->bus, pcidev->dev,
+                                pcidev->func, pcidev->vdevfn);
+            } else {
+                libxl__xs_write(gc, XBT_NULL, path, PCI_BDF, pcidev->domain,
+                                pcidev->bus, pcidev->dev, pcidev->func);
+            }
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/command",
+                                  domid);
+            xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
+            rc = libxl__wait_for_device_model(gc, domid, NULL, NULL,
+                                              pci_ins_check, state);
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/parameter",
+                                  domid);
+            vdevfn = libxl__xs_read(gc, XBT_NULL, path);
+            path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state",
+                                  domid);
+            if ( rc < 0 )
+                LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                           "qemu refused to add device: %s", vdevfn);
+            else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 )
+                rc = -1;
+            xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
+        }
         if ( rc )
             return ERROR_FAIL;
         break;
Signature · 1 line
Anthony PERARD
#24 lore raw

Re: [PATCH 7/7] libxl, Use QMP to insert a passthrough device when using upstream QEMU

Quoted 1 line · Anthony PERARD

Sticking within the 80-column limit is getting pretty hard, perhaps pulling out the interactions with old-qemu into a new function would help?

Quoted 73 lines
---
 tools/libxl/libxl_pci.c |   58 ++++++++++++++++++++++++++++++-----------------
 1 files changed, 37 insertions(+), 21 deletions(-)
tools/libxl/libxl_pci.c +37 -21
index 1523cf0..b6cb4a3 100644
@@ -613,27 +613,43 @@ static int do_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev, i
                                          NULL, NULL, NULL) < 0) {
             return ERROR_FAIL;
         }
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
-        state = libxl__xs_read(gc, XBT_NULL, path);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/parameter", domid);
-        if (pcidev->vdevfn)
-            libxl__xs_write(gc, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
-                           pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
-        else
-            libxl__xs_write(gc, XBT_NULL, path, PCI_BDF, pcidev->domain,
-                           pcidev->bus, pcidev->dev, pcidev->func);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/command", domid);
-        xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
-        rc = libxl__wait_for_device_model(gc, domid, NULL, NULL,
-                                          pci_ins_check, state);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/parameter", domid);
-        vdevfn = libxl__xs_read(gc, XBT_NULL, path);
-        path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
-        if ( rc < 0 )
-            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "qemu refused to add device: %s", vdevfn);
-        else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 )
-            rc = -1;
-        xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
+        if (libxl__device_model_version_running(gc, domid)
+            == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
+            rc = libxl__qmp_pci_add(gc, domid, pcidev);
+        } else {
+            path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state",
+                                  domid);
+            state = libxl__xs_read(gc, XBT_NULL, path);
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/parameter",
+                                  domid);
+            if (pcidev->vdevfn) {
+                libxl__xs_write(gc, XBT_NULL, path, PCI_BDF_VDEVFN,
+                                pcidev->domain, pcidev->bus, pcidev->dev,
+                                pcidev->func, pcidev->vdevfn);
+            } else {
+                libxl__xs_write(gc, XBT_NULL, path, PCI_BDF, pcidev->domain,
+                                pcidev->bus, pcidev->dev, pcidev->func);
+            }
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/command",
+                                  domid);
+            xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
+            rc = libxl__wait_for_device_model(gc, domid, NULL, NULL,
+                                              pci_ins_check, state);
+            path = libxl__sprintf(gc,
+                                  "/local/domain/0/device-model/%d/parameter",
+                                  domid);
+            vdevfn = libxl__xs_read(gc, XBT_NULL, path);
+            path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state",
+                                  domid);
+            if ( rc < 0 )
+                LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                           "qemu refused to add device: %s", vdevfn);
+            else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 )
+                rc = -1;
+            xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
+        }
         if ( rc )
             return ERROR_FAIL;
         break;
#25 lore raw

Re: [PATCH 7/7] libxl, Use QMP to insert a passthrough device when using upstream QEMU

Quoted 6 lines · Ian Campbell
Quoted 1 line · Anthony PERARD

Sticking within the 80-column limit is getting pretty hard, perhaps pulling out the interactions with old-qemu into a new function would help?

I think it does, so I will probably create this new function.

Signature · 1 line
Anthony PERARD
#26 lore raw

Re: [PATCH 7/7] libxl, Use QMP to insert a passthrough device when using upstream QEMU

In reply to Anthony PERARD

Anthony PERARD writes ("[Xen-devel] [PATCH 7/7] libxl, Use QMP to insert a passt> + else if ( sscanf(vdevfn, "0x%x", &pcidev->vdevfn) != 1 )

Quoted 1 line

+ rc = -1;

If this ever happens, it would be very mysterious. Perhaps add a logging call ?

Ian.