Skip to contentSkip to conversation

Introduce a QMP client

PATCH Superseded Anthony PERARD 3 participants 14 messages Jun 30, 2011 – Jul 8, 2011

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover Introduce a QMP client

New:
- Only one makefile rules for all user of gentypes.py.
- No more #ifdef DEBUG_ANSWER in the code flow, there is now macros DEBUG_GEN_*
- Introduce libxl_json.c for the JSON parsing stuff.

Change v4->v5:
- Add a separate patch for XEN_RUN_DIR path.
- Add a new patch to generate an some internal type with gentypes.py. It's

    used only to have a ENUM <-> String convertion with the QMP client.

- libxl_qmp.h content have been moved to libxl_internal.h
- return value of every single alloc are now check
- introduce some DEBUG_GEN_* macro to avoid some #ifdefs
- remove unused exported function: libxL__qmp_next libxl__qmp_send_command
- introduce libxl__qmp_query_serial and store all serial port chardev in xenstore.
- introduce json_object_{get,is}_* inline functions.
- coding style fixed

Anthony PERARD (3):
libxl: Introduce libxl_internal_types.idl.
libxl: Introduce JSON parsing stuff.
libxl, Introduce a QMP client

 tools/libxl/Makefile                       |   21 +-
 tools/libxl/gentypes.py                    |   15 +-
 tools/libxl/libxl.c                        |    2 +
 tools/libxl/libxl_create.c                 |    3 +
 tools/libxl/libxl_dm.c                     |    9 +
 tools/libxl/libxl_internal.h               |  117 ++++++
 tools/libxl/libxl_json.c                   |  521 +++++++++++++++++++++++++
 tools/libxl/libxl_qmp.c                    |  570 ++++++++++++++++++++++++++++
 tools/libxl/{libxl.idl => libxl_types.idl} |    0
 tools/libxl/libxl_types_internal.idl       |   10 +
 10 files changed, 1253 insertions(+), 15 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
 create mode 100644 tools/libxl/libxl_qmp.c
 rename tools/libxl/{libxl.idl => libxl_types.idl} (100%)
 create mode 100644 tools/libxl/libxl_types_internal.idl
Signature · 1 line
1.7.2.5
#1 lore raw

PATCH v6 1/3 libxl: Introduce libxl_internal_types.idl.

---
 tools/libxl/Makefile                       |   16 +++++++++-------
 tools/libxl/gentypes.py                    |   15 ++++++++-------
 tools/libxl/libxl_internal.h               |    1 +
 tools/libxl/{libxl.idl => libxl_types.idl} |    0
 tools/libxl/libxl_types_internal.idl       |   10 ++++++++++
 5 files changed, 28 insertions(+), 14 deletions(-)
 rename tools/libxl/{libxl.idl => libxl_types.idl} (100%)
 create mode 100644 tools/libxl/libxl_types_internal.idl
tools/libxl/Makefile +9 -7
index bfe9c58..a95cd5d 100644
@@ -35,7 +35,7 @@ LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
 			libxl_internal.o libxl_utils.o libxl_uuid.o $(LIBXL_OBJS-y)
-LIBXL_OBJS += _libxl_types.o libxl_flask.o
+LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
 
@@ -51,8 +51,8 @@ $(XL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) # For xentoollog.h
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxenlight)
 
 testenum.o: CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenlight)
-testenum.c: libxl.idl gentest.py
-	$(PYTHON) gentest.py libxl.idl testenum.c.new
+testenum.c: libxl_types.idl gentest.py
+	$(PYTHON) gentest.py libxl_types.idl testenum.c.new
 	mv testenum.c.new testenum.c
 
 .PHONY: all
@@ -79,13 +79,15 @@ _libxl_paths.h: genpath
 libxl_paths.c: _libxl_paths.h
 
 libxl.h: _libxl_types.h
+libxl_internal.h: _libxl_types_internal.h
 
 $(LIBXL_OBJS) $(LIBXLU_OBJS) $(XL_OBJS): libxl.h
+$(LIBXL_OBJS): libxl_internal.h
 
-_libxl_%.h _libxl_%.c: libxl.idl gen%.py libxl%.py
-	$(PYTHON) gen$*.py libxl.idl __libxl_$*.h __libxl_$*.c
-	mv __libxl_$*.h _libxl_$*.h
-	mv __libxl_$*.c _libxl_$*.c
+_libxl_type%.h _libxl_type%.c: libxl_type%.idl gentypes.py libxltypes.py
+	$(PYTHON) gentypes.py libxl_type$*.idl __libxl_type$*.h __libxl_type$*.c
+	mv __libxl_type$*.h _libxl_type$*.h
+	mv __libxl_type$*.c _libxl_type$*.c
 
 libxenlight.so: libxenlight.so.$(MAJOR)
 	ln -sf $< $@
tools/libxl/gentypes.py +8 -7
index c9a3d9c..5188b66 100644
@@ -162,9 +162,10 @@ if __name__ == '__main__':
     print "outputting libxl type definitions to %s" % header
 
     f = open(header, "w")
-    
-    f.write("""#ifndef __LIBXL_TYPES_H
-#define __LIBXL_TYPES_H
+
+    header_define = header.upper().replace('.','_')
+    f.write("""#ifndef %s
+#define %s
 
 /*
  * DO NOT EDIT.
@@ -172,9 +173,9 @@ if __name__ == '__main__':
  * This file is autogenerated by
  * "%s"
  */
- 
-""" % " ".join(sys.argv))
-        
+
+""" % (header_define, header_define, " ".join(sys.argv)))
+
     for ty in types:
         f.write(libxl_C_type_define(ty) + ";\n")
         if ty.destructor_fn is not None:
@@ -185,7 +186,7 @@ if __name__ == '__main__':
             f.write("extern libxl_enum_string_table %s_string_table[];\n" % (ty.typename))
         f.write("\n")
 
-    f.write("""#endif /* __LIBXL_TYPES_H */\n""")
+    f.write("""#endif /* %s */\n""" % (header_define))
     f.close()
     
     impl = sys.argv[3]
tools/libxl/libxl_internal.h +1 -0
index 47752e5..71eb189 100644
@@ -35,6 +35,7 @@
 
 #include "flexarray.h"
 #include "libxl_utils.h"
+#include "_libxl_types_internal.h"
 
 #define LIBXL_DESTROY_TIMEOUT 10
 #define LIBXL_DEVICE_MODEL_START_TIMEOUT 10
tools/libxl/libxl_types.idl renamed from tools/libxl/libxl.idl +0 -0
similarity index 100%
rename from tools/libxl/libxl.idl
rename to tools/libxl/libxl_types.idl
tools/libxl/libxl_types_internal.idl new file +10 -0
new file mode 100644
index 0000000..d993298
@@ -0,0 +1,10 @@
+
+libxl__qmp_message_type  = Enumeration("qmp_message_type", [
+    (1, "QMP"),
+    (2, "return"),
+    (3, "error"),
+    (4, "event"),
+    (5, "invalid"),
+    ],
+    namespace = "libxl__")
+
Signature · 1 line
1.7.2.5
#2 lore raw

Re: [PATCH V6 1/3] libxl: Introduce libxl_internal_types.idl.

Quoted 6 lines · Anthony PERARD
---
 tools/libxl/Makefile                       |   16 +++++++++-------
 tools/libxl/gentypes.py                    |   15 ++++++++-------
 tools/libxl/libxl_internal.h               |    1 +
 tools/libxl/{libxl.idl => libxl_types.idl} |    0

This is going to need changes to at least tools/python/Makefile and tools/ocaml/libs/xl/Makefile as well. Probably best to split the rename into another patch.

[...]

Quoted 6 lines
tools/libxl/libxl_types_internal.idl new file +0 -0
new file mode 100644
index 0000000..d993298

tools/libxl/libxl_types_internal.idl line 9

Quoted 9 lines
tools/libxl/libxl_types_internal.idl new file +9 -0
@@ -0,0 +1,10 @@
+
+libxl__qmp_message_type  = Enumeration("qmp_message_type", [
+    (1, "QMP"),
+    (2, "return"),
+    (3, "error"),
+    (4, "event"),
+    (5, "invalid"),
+    ],
+    namespace = "libxl__")

I wonder if the IDL infrastructure should export a way to set the default namespace? So we'd end up with:
default_namespace = "libxl__"

libxl__qmp_message_type = Enumeration("...", [....]) libxl__another_internal_type = Struct(...)

We could use the same in libxl_types.idl and change the default to be no namespace, making the whole thing a tiny bit more generic.

I expect that scoping will require the actual syntax to use the module e.g.
libxltypes.default_namespace = "libxl__"
or maybe a function call is cleaner
libxltypes.default_namespace("libxl__")

Ian.

#3 lore raw

Re: Re: [PATCH V6 1/3] libxl: Introduce libxl_internal_types.idl.

Quoted 11 lines · Ian Campbell
Quoted 6 lines · Anthony PERARD

---
 tools/libxl/Makefile                       |   16 +++++++++------- tools/libxl/gentypes.py                    |   15 ++++++++------- tools/libxl/libxl_internal.h               |    1 + tools/libxl/{libxl.idl => libxl_types.idl} |    0

This is going to need changes to at least tools/python/Makefile and tools/ocaml/libs/xl/Makefile as well. Probably best to split the rename into another patch.

:(, this file is use everywhere. So yes, I will split the patch.

Quoted 24 lines

[...]

Quoted 6 lines
tools/libxl/libxl_types_internal.idl new file +0 -0
new file mode 100644
index 0000000..d993298

tools/libxl/libxl_types_internal.idl line 9

Quoted 9 lines
tools/libxl/libxl_types_internal.idl new file +9 -0
@@ -0,0 +1,10 @@
+
+libxl__qmp_message_type  = Enumeration("qmp_message_type", [
+    (1, "QMP"),
+    (2, "return"),
+    (3, "error"),
+    (4, "event"),
+    (5, "invalid"),
+    ],
+    namespace = "libxl__")

I wonder if the IDL infrastructure should export a way to set the default namespace? So we'd end up with:
       default_namespace = "libxl__"

       libxl__qmp_message_type = Enumeration("...", [....]) libxl__another_internal_type = Struct(...)

Quoted 8 lines

We could use the same in libxl_types.idl and change the default to be no namespace, making the whole thing a tiny bit more generic.

I expect that scoping will require the actual syntax to use the module e.g.
       libxltypes.default_namespace = "libxl__"
or maybe a function call is cleaner libxltypes.default_namespace("libxl__")

I will try to do that. The .idl files will be much more cleanner after that.

Signature · 1 line
Anthony PERARD
#4 lore raw

PATCH v6 2/3 libxl: Introduce JSON parsing stuff.

In reply to Anthony PERARD

We use the yajl parser, but we need to make a tree from the parse result to use it outside the parser.

So this patch include json_object struct that is used to hold the JSON data.

---
 tools/libxl/Makefile         |    5 +-
 tools/libxl/libxl_internal.h |   97 ++++++++
 tools/libxl/libxl_json.c     |  521 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 622 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
tools/libxl/Makefile +4 -1
index a95cd5d..0306cb0 100644
@@ -32,9 +32,12 @@ endif
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o
 LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 
+LIBXL_LIBS += -lyajl
+
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
-			libxl_internal.o libxl_utils.o libxl_uuid.o $(LIBXL_OBJS-y)
+			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+			$(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl_internal.h +97 -0

101 lines not on this page · raw message

tools/libxl/libxl_json.c new file +521 -0

521 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#5 lore raw

Re: [PATCH V6 2/3] libxl: Introduce JSON parsing stuff.

Quoted 43 lines · Anthony PERARD

We use the yajl parser, but we need to make a tree from the parse result to use it outside the parser.

So this patch include json_object struct that is used to hold the JSON data.

---
 tools/libxl/Makefile         |    5 +-
 tools/libxl/libxl_internal.h |   97 ++++++++
 tools/libxl/libxl_json.c     |  521 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 622 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_json.c
tools/libxl/Makefile +4 -1
index a95cd5d..0306cb0 100644
@@ -32,9 +32,12 @@ endif
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o
 LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o
 
+LIBXL_LIBS += -lyajl
+
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
                        libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
-                       libxl_internal.o libxl_utils.o libxl_uuid.o $(LIBXL_OBJS-y)
+                       libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
+                       $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl_internal.h +3 -0
index 71eb189..555d9f3 100644
@@ -381,4 +381,101 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
 
+/* from libxl_json */
+typedef enum {
+    JSON_ERROR,

tools/libxl/libxl_internal.h _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi · line 396

Quoted 10 lines
tools/libxl/libxl_internal.h +10 -0
+    JSON_NULL,
+    JSON_TRUE,
+    JSON_FALSE,
+    JSON_INTEGER,
+    JSON_DOUBLE,
+    JSON_STRING,
+    JSON_MAP,
+    JSON_ARRAY,
+    JSON_ANY
+} libxl__json_node_type;

Could be internal IDL? Maybe no much point if you aren't using the to_string functions etc?

tools/libxl/libxl_internal.h _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi · line 403

Quoted 7 lines
tools/libxl/libxl_internal.h +7 -0
+
+typedef struct libxl__json_object {
+    libxl__json_node_type type;
+    union {
+        long i;
+        double d;
+        const char *string;

Is it really const? Seems to be malloc/freed?

tools/libxl/libxl_internal.h

Quoted 6 lines

[...]+#ifdef DEBUG_ANSWER

tools/libxl/libxl_internal.h +5 -0
+#  define DEBUG_GEN_ALLOC(h) \
+    if (h == NULL) { \
+        yajl_gen_config conf = { 1, "  " }; \
+        h = yajl_gen_alloc(&conf, NULL); \
+    }

All the callers of these macros use ctx->g. I think you could push the ->g down into the macros.

[...]

Ian

#6 lore raw

Re: Re: [PATCH V6 2/3] libxl: Introduce JSON parsing stuff.

Quoted 8 lines · Ian Campbell
Quoted 8 lines
tools/libxl/libxl_internal.h +8 -0
+/* from libxl_json */
+typedef enum {
+    JSON_ERROR,
+    JSON_NULL,
+    JSON_TRUE,
+    JSON_FALSE,
+    JSON_INTEGER,
+    JSON_DOUBLE,
Quoted 8 lines
Quoted 5 lines

+    JSON_STRING,
+    JSON_MAP,
+    JSON_ARRAY,
+    JSON_ANY
+} libxl__json_node_type;

Could be internal IDL? Maybe no much point if you aren't using the to_string functions etc?

The to_string function could be use for debug messages. But that all.

Quoted 9 lines

tools/libxl/libxl_internal.h _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi · line 403

Quoted 7 lines
tools/libxl/libxl_internal.h +7 -0
+
+typedef struct libxl__json_object {
+    libxl__json_node_type type;
+    union {
+        long i;
+        double d;
+        const char *string;

Is it really const? Seems to be malloc/freed?

Yes, the string belong only to this structure. In that case, there is no const ?

Quoted 9 lines
Quoted 6 lines

[...]+#ifdef DEBUG_ANSWER
+#  define DEBUG_GEN_ALLOC(h) \
+    if (h == NULL) { \
+        yajl_gen_config conf = { 1, "  " }; \
+        h = yajl_gen_alloc(&conf, NULL); \
+    }

All the callers of these macros use ctx->g. I think you could push the ->g down into the macros.

Yes, maybe better, specially for the case of the _alloc where the parameter is modified.

Thanks,

Signature · 1 line
Anthony PERARD
#7 lore raw

Re: [PATCH V6 2/3] libxl: Introduce JSON parsing stuff.

In reply to Anthony PERARD

Anthony PERARD writes ("[Xen-devel] [PATCH V6 2/3] libxl: Introduce JSON parsing stuff."):

tools/libxl/libxl_json.c line 105

Quoted 9 lines
tools/libxl/libxl_json.c new file +9 -0
+static int json_object_append_to(libxl_ctx *ctx,
+                                 libxl__json_object *obj,
+                                 libxl__json_object *dst)
+{
+    if (!dst) {
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
+                   "No parent json object to fill");
+        return -1;
+    }

Surely it is fair enough for this function to dereference NULL (and segfault) if dst==0 ? Returning -1 (ie, operational error similar to memory allocation failure) for a programming mistake like that is not a good idea IMO.

tools/libxl/libxl_json.c line 298

Quoted 11 lines
tools/libxl/libxl_json.c new file +11 -0
+static int json_callback_string(void *opaque, const unsigned char *str,
+                                unsigned int len)
+{
+    libxl__yajl_ctx *ctx = opaque;
+    char *t = malloc(len + 1);
+    libxl__json_object *obj = NULL;
+
+    if (t == NULL) {
+        LIBXL__LOG_ERRNO(ctx->ctx, LIBXL__LOG_ERROR, "Failed to allocate");
+        return 0;
+    }

This separation of the allocation from the test is pretty nasty.

Thanks,
Ian.

#8 lore raw

PATCH v6 3/3 libxl, Introduce a QMP client

In reply to Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

---
 tools/libxl/Makefile         |    2 +-
 tools/libxl/libxl.c          |    2 +
 tools/libxl/libxl_create.c   |    3 +
 tools/libxl/libxl_dm.c       |    9 +
 tools/libxl/libxl_internal.h |   19 ++
 tools/libxl/libxl_qmp.c      |  570 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 604 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_qmp.c
tools/libxl/Makefile +1 -1
index 0306cb0..aea0e93 100644
@@ -37,7 +37,7 @@ LIBXL_LIBS += -lyajl
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
 			libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
-			$(LIBXL_OBJS-y)
+			libxl_qmp.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +2 -0
index abba45e..9d5e547 100644
@@ -757,6 +757,8 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
     if (dm_present) {
         if (libxl__destroy_device_model(&gc, domid) < 0)
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
+
+        libxl__qmp_cleanup(&gc, domid);
     }
     if (libxl__devices_destroy(&gc, domid, force) < 0)
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
tools/libxl/libxl_create.c +3 -0
index 5612aeb..28e38fa 100644
@@ -522,6 +522,9 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     }
 
     if (dm_starting) {
+        if (dm_info->device_model_version == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
+            libxl__qmp_initializations(ctx, domid);
+        }
         ret = libxl__confirm_device_model_startup(gc, dm_starting);
         if (ret < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
tools/libxl/libxl_dm.c +9 -0
index 76d72a5..fa11370 100644
@@ -249,6 +249,15 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     flexarray_vappend(dm_args, dm,
                       "-xen-domid", libxl__sprintf(gc, "%d", info->domid), NULL);
 
+    flexarray_append(dm_args, "-chardev");
+    flexarray_append(dm_args,
+                     libxl__sprintf(gc, "socket,id=libxl-cmd,path=%s/qmp-%d,server,nowait",
+                                    libxl_run_dir_path(),
+                                    info->domid));
+
+    flexarray_append(dm_args, "-mon");
+    flexarray_append(dm_args, "chardev=libxl-cmd,mode=control");
+
     if (info->type == LIBXL_DOMAIN_TYPE_PV) {
         flexarray_append(dm_args, "-xen-attach");
     }
tools/libxl/libxl_internal.h +19 -0
index 555d9f3..338f8a6 100644
@@ -381,6 +381,25 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
 
+/* from libxl_qmp */
+typedef struct libxl__qmp_handler libxl__qmp_handler;
+
+/* Initialise and connect to the QMP socket.
+ *   Return an handler or NULL if there is an error
+ */
+_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);
+/* 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,
+ * nothing happen */
+_hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
+
+/* this helper calls qmp_initialize, query_serial and qmp_close */
+_hidden int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid);
+
 /* from libxl_json */
 typedef enum {
     JSON_ERROR,
tools/libxl/libxl_qmp.c new file +570 -0

570 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#9 lore raw

Re: [PATCH V6 3/3] libxl, Introduce a QMP client

Quoted 9 lines · Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

Should include "libxl" in the name somewhere to differentiate from other potential connections in the future?

Quoted 381 lines
---
 tools/libxl/Makefile         |    2 +-
 tools/libxl/libxl.c          |    2 +
 tools/libxl/libxl_create.c   |    3 +
 tools/libxl/libxl_dm.c       |    9 +
 tools/libxl/libxl_internal.h |   19 ++
 tools/libxl/libxl_qmp.c      |  570 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 604 insertions(+), 1 deletions(-)
 create mode 100644 tools/libxl/libxl_qmp.c
tools/libxl/Makefile +1 -1
index 0306cb0..aea0e93 100644
@@ -37,7 +37,7 @@ LIBXL_LIBS += -lyajl
 LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
                        libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \
                        libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \
-                       $(LIBXL_OBJS-y)
+                       libxl_qmp.o $(LIBXL_OBJS-y)
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
 $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +2 -0
index abba45e..9d5e547 100644
@@ -757,6 +757,8 @@ int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force)
     if (dm_present) {
         if (libxl__destroy_device_model(&gc, domid) < 0)
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model failed for %d", domid);
+
+        libxl__qmp_cleanup(&gc, domid);
     }
     if (libxl__devices_destroy(&gc, domid, force) < 0)
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
tools/libxl/libxl_create.c +3 -0
index 5612aeb..28e38fa 100644
@@ -522,6 +522,9 @@ static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
     }
 
     if (dm_starting) {
+        if (dm_info->device_model_version == LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN) {
+            libxl__qmp_initializations(ctx, domid);
+        }
         ret = libxl__confirm_device_model_startup(gc, dm_starting);
         if (ret < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR,
tools/libxl/libxl_dm.c +9 -0
index 76d72a5..fa11370 100644
@@ -249,6 +249,15 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     flexarray_vappend(dm_args, dm,
                       "-xen-domid", libxl__sprintf(gc, "%d", info->domid), NULL);
 
+    flexarray_append(dm_args, "-chardev");
+    flexarray_append(dm_args,
+                     libxl__sprintf(gc, "socket,id=libxl-cmd,path=%s/qmp-%d,server,nowait",
+                                    libxl_run_dir_path(),
+                                    info->domid));
+
+    flexarray_append(dm_args, "-mon");
+    flexarray_append(dm_args, "chardev=libxl-cmd,mode=control");
+
     if (info->type == LIBXL_DOMAIN_TYPE_PV) {
         flexarray_append(dm_args, "-xen-attach");
     }
tools/libxl/libxl_internal.h +19 -0
index 555d9f3..338f8a6 100644
@@ -381,6 +381,25 @@ _hidden int libxl__e820_alloc(libxl_ctx *ctx, uint32_t domid, libxl_domain_confi
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
 
+/* from libxl_qmp */
+typedef struct libxl__qmp_handler libxl__qmp_handler;
+
+/* Initialise and connect to the QMP socket.
+ *   Return an handler or NULL if there is an error
+ */
+_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);
+/* 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,
+ * nothing happen */
+_hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid);
+
+/* this helper calls qmp_initialize, query_serial and qmp_close */
+_hidden int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid);
+
 /* from libxl_json */
 typedef enum {
     JSON_ERROR,
tools/libxl/libxl_qmp.c new file +273 -0
new file mode 100644
index 0000000..6b03c08
@@ -0,0 +1,570 @@
+/*
+ * Copyright (C) 2011      Citrix Ltd.
+ * Author Anthony PERARD <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+/*
+ * This file implement a client for QMP (QEMU Monitor Protocol). For the
+ * Specification, see in the QEMU repository.
+ */
+
+#include <unistd.h>
+#include <sys/un.h>
+#include <sys/queue.h>
+
+#include <yajl/yajl_gen.h>
+
+#include "libxl_internal.h"
+
+/* #define DEBUG_RECEIVED */
+
+#ifdef DEBUG_RECEIVED
+#  define DEBUG_REPORT_RECEIVED(len, buf) \
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "received: '%.*s'", len, buf)
+#else
+#  define DEBUG_REPORT_RECEIVED(len, buf) ((void)0)
+#endif
+
+/*
+ * QMP types & constant
+ */
+
+#define QMP_RECEIVE_BUFFER_SIZE 4096
+
+typedef int (*qmp_callback_t)(libxl__qmp_handler *qmp,
+                              const libxl__json_object *tree);
+
+typedef struct callback_id_pair {
+    int id;
+    qmp_callback_t callback;
+    SIMPLEQ_ENTRY(callback_id_pair) next;
+} callback_id_pair;
+
+struct libxl__qmp_handler {
+    struct sockaddr_un addr;
+    int qmp_fd;
+    bool connected;
+    time_t timeout;
+    /* wait_for_id will be used by the synchronous send function */
+    int wait_for_id;
+
+    unsigned char buffer[QMP_RECEIVE_BUFFER_SIZE];
+    libxl__yajl_ctx *yajl_ctx;
+
+    libxl_ctx *ctx;
+    uint32_t domid;
+
+    int last_id_used;
+    SIMPLEQ_HEAD(callback_list, callback_id_pair) callback_list;
+};
+
+static int qmp_send(libxl__qmp_handler *qmp,
+                    const char *cmd, qmp_callback_t callback);
+
+static const int QMP_SOCKET_CONNECT_TIMEOUT = 5;
+
+/*
+ * QMP callbacks functions
+ */
+
+static int store_serial_port_info(libxl__qmp_handler *qmp,
+                                  const char *chardev,
+                                  int port)
+{
+    libxl__gc gc = LIBXL_INIT_GC(qmp->ctx);
+    char *path = NULL;
+    int ret = 0;
+
+    if (!(chardev && strncmp("pty:", chardev, 4) == 0)) {
+        return -1;
+    }
+
+    path = libxl__xs_get_dompath(&gc, qmp->domid);
+    path = libxl__sprintf(&gc, "%s/serial/%d/tty", path, port);
+
+    ret = libxl__xs_write(&gc, XBT_NULL, path, "%s", chardev + 4);
+
+    libxl__free_all(&gc);
+    return ret;
+}
+
+static int register_serials_chardev_callback(libxl__qmp_handler *qmp,
+                                             const libxl__json_object *o)
+{
+    const libxl__json_object *obj = NULL;
+    const libxl__json_object *label = NULL;
+    const char *s = NULL;
+    flexarray_t *array = NULL;
+    int index = 0;
+    const char *chardev = NULL;
+    int ret = 0;
+
+    if ((array = json_object_get_array(o)) == NULL) {
+        return -1;
+    }
+
+    for (index = 0; index < array->count; index++) {
+        if (flexarray_get(array, index, (void**)&obj) != 0)
+            break;
+        label = json_object_get("label", obj, JSON_STRING);
+        s = json_object_get_string(label);
+
+        if (s && strncmp("serial", s, strlen("serial")) == 0) {
+            const libxl__json_object *filename = NULL;
+            char *endptr = NULL;
+            int port_number;
+
+            filename = json_object_get("filename", obj, JSON_STRING);
+            chardev = json_object_get_string(filename);
+
+            s += strlen("serial");
+            port_number = strtol(s, &endptr, 10);
+            if (*s == 0 || *endptr != 0) {
+                LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
+                           "Invalid serial port number: %s", s);
+                return -1;
+            }
+            ret = store_serial_port_info(qmp, chardev, port_number);
+            if (ret) {
+                LIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR,
+                                 "Failed to store serial port information"
+                                 " in xenstore");
+                return ret;
+            }
+        }
+    };
+
+    return ret;
+}
+
+static int qmp_capabilities_callback(libxl__qmp_handler *qmp,
+                                     const libxl__json_object *o)
+{
+    qmp->connected = true;
+
+    return 0;
+}
+
+/*
+ * QMP commands
+ */
+
+static int enable_qmp_capabilities(libxl__qmp_handler *qmp)
+{
+    return qmp_send(qmp, "qmp_capabilities", qmp_capabilities_callback);
+}
+
+/*
+ * Helpers
+ */
+
+static libxl__qmp_message_type qmp_response_type(libxl__qmp_handler *qmp,
+                                                 const libxl__json_object *o)
+{
+    flexarray_t *maps = json_object_get_map(o);
+    libxl__qmp_message_type type;
+
+    if (maps) {
+        libxl__json_map_node *node = NULL;
+        int index = 0;
+
+        for (index = 0; index < maps->count; index++) {
+            if (flexarray_get(maps, index, (void**)&node) != 0)
+                break;
+            if (libxl__qmp_message_type_from_string(node->map_key, &type) == 0)
+                return type;
+        }
+    }
+
+    return LIBXL__QMP_MESSAGE_TYPE_INVALID;
+}
+
+static callback_id_pair *qmp_get_callback_from_id(libxl__qmp_handler *qmp,
+                                                  const libxl__json_object *o)
+{
+    const libxl__json_object *id_object = json_object_get("id", o,
+                                                          JSON_INTEGER);
+    int id = -1;
+    callback_id_pair *pp = NULL;
+
+    if (id_object) {
+        id = json_object_get_integer(id_object);
+
+        SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+            if (pp->id == id) {
+                return pp;
+            }
+        }
+    }
+    return NULL;
+}
+
+static void qmp_handle_error_response(libxl__qmp_handler *qmp,
+                                      const libxl__json_object *resp)
+{
+    callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
+
+    resp = json_object_get("error", resp, JSON_MAP);
+    resp = json_object_get("desc", resp, JSON_STRING);
+
+    if (pp) {
+        if (pp->id == qmp->wait_for_id) {
+            /* tell that the id have been processed */
+            qmp->wait_for_id = 0;
+        }
+        SIMPLEQ_REMOVE(&qmp->callback_list, pp, callback_id_pair, next);
+        free(pp);
+    }
+
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
+               "received an error message from QMP server: %s",
+               json_object_get_string(resp));
+}
+
+static int qmp_handle_response(libxl__qmp_handler *qmp,
+                               const libxl__json_object *resp)
+{
+    libxl__qmp_message_type type = LIBXL__QMP_MESSAGE_TYPE_INVALID;
+
+    type = qmp_response_type(qmp, resp);
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG,
+               "message type: %s", libxl__qmp_message_type_to_string(type));
+
+    switch (type) {
+    case LIBXL__QMP_MESSAGE_TYPE_QMP:
+        /* On the greeting message from the server, enable QMP capabilities */
+        enable_qmp_capabilities(qmp);
+        break;
+    case LIBXL__QMP_MESSAGE_TYPE_RETURN: {
+        callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
+
+        if (pp) {
+            pp->callback(qmp, json_object_get("return", resp, JSON_ANY));
+            if (pp->id == qmp->wait_for_id) {
+                /* tell that the id have been processed */
+                qmp->wait_for_id = 0;
+            }
+            SIMPLEQ_REMOVE(&qmp->callback_list, pp, callback_id_pair, next);
+            free(pp);
+        }
+        break;
+    }
+    case LIBXL__QMP_MESSAGE_TYPE_ERROR:
+        qmp_handle_error_response(qmp, resp);
+        break;
+    case LIBXL__QMP_MESSAGE_TYPE_EVENT:
+        break;
+    case LIBXL__QMP_MESSAGE_TYPE_INVALID:
+        return -1;
+    }
+    return 0;
+}
+
+static inline yajl_gen_status libxl__yajl_gen_asciiz(yajl_gen hand,

tools/libxl/libxl_qmp.c line 283

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+                                                     const char *str)
+{
+    return yajl_gen_string(hand, (const unsigned char *)str, strlen(str));
+}
+
+/*
+ * Handler functions
+ */
+
+static libxl__qmp_handler *qmp_init_handler(libxl_ctx *ctx, uint32_t domid)

libxl__qmp_init_handler?

Generally internal functions should take a libxl__gc *gc rather than a ctx (applies to a bunch of functions).

tools/libxl/libxl_qmp.c line 287

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+{
+    libxl__qmp_handler *qmp = NULL;
+
+    qmp = calloc(1, sizeof (libxl__qmp_handler));

Could be attached to the libxl__gc infrastructure instead of using an explicit free?

BTW, I was wondering the same thing as I read the parser stuff in the previous patch but that would involve plumbing a *gc through and careful thought about whether or not a given data field could ever reach the libxl user (e.g. JSON_STRING's could ultimately get returned to the caller).

Quoted 18 lines
tools/libxl/libxl_qmp.c new file +18 -0
+static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
+                    int timeout)
+{
+    int ret;
+    int i = 0;
+
+    qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+    if (qmp->qmp_fd < 0) {
+        return -1;
+    }
+
+    memset(&qmp->addr, 0, sizeof (&qmp->addr));
+    qmp->addr.sun_family = AF_UNIX;
+    strncpy(qmp->addr.sun_path, qmp_socket_path,
+            sizeof (qmp->addr.sun_path));
+
+    do {
+        ret = connect(qmp->qmp_fd, (struct sockaddr *) &qmp->addr,

tools/libxl/libxl_qmp.c line 329

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+                      sizeof (qmp->addr));
+        if (ret == 0)
+            break;
+        if (errno == ENOENT || errno == ECONNREFUSED) {
+            /* ENOENT       : Socket may not have shown up yet
+             * ECONNREFUSED : Leftover socket hasn't been removed yet */
+            continue;
+        }
+        return -1;
+    } while ((++i / 5 <= timeout) && (usleep(200 * 1000) <= 0));

If we exit this loop because we timed out do we need to set errno to ETIMEDOUT or something similar to indicate this? Or is the existing err errno (one of ENOENT or ECONNREFUSED?) a sufficient indicator?

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+
+    return ret;
+}
+

tools/libxl/libxl_qmp.c line 343

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+static void qmp_close(libxl__qmp_handler *qmp)
+{
+    callback_id_pair *pp = NULL;
+    callback_id_pair *tmp = NULL;
+
+    close(qmp->qmp_fd);
+    SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+        if (tmp)
+            free(tmp);
+        tmp = pp;

That seems like an odd construct, but I suppose it is necessary to work around the lack of a SIMPLEQ_FOREACH variant which is safe against removing the iterator from the list.

Quoted 13 lines
tools/libxl/libxl_qmp.c new file +13 -0
+libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx, uint32_t domid)
+{
+    int ret = 0;
+    libxl__qmp_handler *qmp = NULL;
+    char *qmp_socket;
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+
+    qmp = qmp_init_handler(ctx, domid);
+
+    qmp_socket = libxl__sprintf(&gc, "%s/qmp-%d",
+                                libxl_run_dir_path(), domid);
+    if ((ret = qmp_open(qmp, qmp_socket, QMP_SOCKET_CONNECT_TIMEOUT)) < 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Connection error");

tools/libxl/libxl_qmp.c line 518

Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+        qmp_free_handler(qmp);
+        return NULL;
+    }
+
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "connected to %s", qmp_socket);
+
+    /* Wait for the response to qmp_capabilities */
+    while (!qmp->connected) {
+        if ((ret = qmp_next(qmp)) < 0) {
+            break;

Is it an error condition not to get in to the connected state? Should we therefore qmp_free_handler and return NULL? That would save callers checking qmp->connected since they can just assume it is true...

[...]

tools/libxl/libxl_qmp.c line 565

Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid)
+{
+    libxl__qmp_handler *qmp = NULL;
+    int ret = 0;
+
+    qmp = libxl__qmp_initialize(ctx, domid);
+    if (!qmp)
+        return -1;
+    if (qmp->connected) {

... like here.

Quoted 8 lines
tools/libxl/libxl_qmp.c new file +5 -1
+        ret = libxl__qmp_query_serial(qmp);
+    }
+    libxl__qmp_close(qmp);
+    return ret;
+}
--

1.7.2.5

#10 lore raw

Re: Re: [PATCH V6 3/3] libxl, Introduce a QMP client

Quoted 8 lines · Ian Campbell
Quoted 7 lines · Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

Quoted 5 lines
Quoted 2 lines

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

Should include "libxl" in the name somewhere to differentiate from other potential connections in the future?

Well, this socket belong to QEMU (the creator). So I'm not sure that "libxl" in the name is appropriate.

But did you prefer to see "qmp-libxl-$(domid)" as a socket file name ?

[...]

Quoted 7 lines

tools/libxl/libxl_qmp.c line 283

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+/*
+ * Handler functions
+ */
+
+static libxl__qmp_handler *qmp_init_handler(libxl_ctx *ctx, uint32_t domid)

libxl__qmp_init_handler?

"libxl__" event if it's intern to this file?

Quoted 10 lines

Generally internal functions should take a libxl__gc *gc rather than a ctx (applies to a bunch of functions).

tools/libxl/libxl_qmp.c line 287

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+{
+    libxl__qmp_handler *qmp = NULL;
+
+    qmp = calloc(1, sizeof (libxl__qmp_handler));

Could be attached to the libxl__gc infrastructure instead of using an explicit free?

So, I should use the garbage collector for the handler, but also for the callback ? For the callback, I know when I can free them because they will not be used anymore. But if I use the gc, the callbacks will not be freed before the dustman is called by the caller.

Quoted 5 lines

BTW, I was wondering the same thing as I read the parser stuff in the previous patch but that would involve plumbing a *gc through and careful thought about whether or not a given data field could ever reach the libxl user (e.g. JSON_STRING's could ultimately get returned to the caller).

Well, a json_string should be strdup because, with or without gc, everything malloc for a json_object will be free with this object.

I will try to use the gc with the json parsing stuff.

Quoted 24 lines
Quoted 24 lines
tools/libxl/libxl_qmp.c new file +24 -0
+static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
+                    int timeout)
+{
+    int ret;
+    int i = 0;
+
+    qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+    if (qmp->qmp_fd < 0) {
+        return -1;
+    }
+
+    memset(&qmp->addr, 0, sizeof (&qmp->addr));
+    qmp->addr.sun_family = AF_UNIX;
+    strncpy(qmp->addr.sun_path, qmp_socket_path,
+            sizeof (qmp->addr.sun_path));
+
+    do {
+        ret = connect(qmp->qmp_fd, (struct sockaddr *) &qmp->addr,
+                      sizeof (qmp->addr));
+        if (ret == 0)
+            break;
+        if (errno == ENOENT || errno == ECONNREFUSED) {
+            /* ENOENT       : Socket may not have shown up yet
+             * ECONNREFUSED : Leftover socket hasn't been removed yet */
Quoted 8 lines
Quoted 4 lines

+            continue;
+        }
+        return -1;
+    } while ((++i / 5 <= timeout) && (usleep(200 * 1000) <= 0));

If we exit this loop because we timed out do we need to set errno to ETIMEDOUT or something similar to indicate this? Or is the existing err errno (one of ENOENT or ECONNREFUSED?) a sufficient indicator?

I think it's should be a ETIMEDOUT. ENOENT or ECONNREFUSED explain just why we timeout.

Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+
+    return ret;
+}
+
+static void qmp_close(libxl__qmp_handler *qmp)
+{
+    callback_id_pair *pp = NULL;
+    callback_id_pair *tmp = NULL;
+
+    close(qmp->qmp_fd);
Quoted 8 lines
Quoted 4 lines

+    SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+        if (tmp)
+            free(tmp);
+        tmp = pp;

That seems like an odd construct, but I suppose it is necessary to work around the lack of a SIMPLEQ_FOREACH variant which is safe against removing the iterator from the list.

Yes.

Quoted 19 lines
Quoted 19 lines
tools/libxl/libxl_qmp.c new file +19 -0
+libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx, uint32_t domid)
+{
+    int ret = 0;
+    libxl__qmp_handler *qmp = NULL;
+    char *qmp_socket;
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+
+    qmp = qmp_init_handler(ctx, domid);
+
+    qmp_socket = libxl__sprintf(&gc, "%s/qmp-%d",
+                                libxl_run_dir_path(), domid);
+    if ((ret = qmp_open(qmp, qmp_socket, QMP_SOCKET_CONNECT_TIMEOUT)) < 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Connection error");
+        qmp_free_handler(qmp);
+        return NULL;
+    }
+
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "connected to %s", qmp_socket);
+
Quoted 8 lines
Quoted 4 lines

+    /* Wait for the response to qmp_capabilities */
+    while (!qmp->connected) {
+        if ((ret = qmp_next(qmp)) < 0) {
+            break;

Is it an error condition not to get in to the connected state? Should we therefore qmp_free_handler and return NULL? That would save callers checking qmp->connected since they can just assume it is true...

That meen that QEMU did not respond to the "qmp_capabilities" command. And it's needed in order send other commands. So yes, it's an error. I will return NULL.

Quoted 18 lines

[...]

tools/libxl/libxl_qmp.c line 565

Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid)
+{
+    libxl__qmp_handler *qmp = NULL;
+    int ret = 0;
+
+    qmp = libxl__qmp_initialize(ctx, domid);
+    if (!qmp)
+        return -1;
+    if (qmp->connected) {

... like here.

Quoted 5 lines

+        ret = libxl__qmp_query_serial(qmp);
+    }
+    libxl__qmp_close(qmp);
+    return ret;
+}

Signature · 1 line
Anthony PERARD
#11 lore raw

Re: Re: [PATCH V6 3/3] libxl, Introduce a QMP client

Quoted 9 lines · Anthony PERARD
Quoted 8 lines · Ian Campbell
Quoted 7 lines · Anthony PERARD

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

This implementation will ask QEMU the list of chardevice and store the path to serial0 in xenstored. So we will be able to use xl console with QEMU upstream.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

In order to connect to the QMP server, a socket file is created in /var/run/xen/qmp-$(domid).

Should include "libxl" in the name somewhere to differentiate from other potential connections in the future?

Well, this socket belong to QEMU (the creator). So I'm not sure that "libxl" in the name is appropriate.

It's created due to libxl asking it too was how I was thinking about it.

Quoted 1 line

But did you prefer to see "qmp-libxl-$(domid)" as a socket file name ?

Something like that, yeah.

Quoted 11 lines

[...]

Quoted 7 lines

tools/libxl/libxl_qmp.c line 283

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+/*
+ * Handler functions
+ */
+
+static libxl__qmp_handler *qmp_init_handler(libxl_ctx *ctx, uint32_t domid)

libxl__qmp_init_handler?

"libxl__" event if it's intern to this file?

I guess it's not required, I'm just used to seeing it. The use of libxl__gc for internal functions is more important.

Quoted 11 lines
Quoted 10 lines

Generally internal functions should take a libxl__gc *gc rather than a ctx (applies to a bunch of functions).

tools/libxl/libxl_qmp.c line 287

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+{
+    libxl__qmp_handler *qmp = NULL;
+
+    qmp = calloc(1, sizeof (libxl__qmp_handler));

Could be attached to the libxl__gc infrastructure instead of using an explicit free?

Quoted 4 lines

So, I should use the garbage collector for the handler, but also for the callback ? For the callback, I know when I can free them because they will not be used anymore. But if I use the gc, the callbacks will not be freed before the dustman is called by the caller.

I don't think using the gc infrastructure should be mandatory, just do what leads to the cleanest code IMHO.

Quoted 119 lines
Quoted 5 lines

BTW, I was wondering the same thing as I read the parser stuff in the previous patch but that would involve plumbing a *gc through and careful thought about whether or not a given data field could ever reach the libxl user (e.g. JSON_STRING's could ultimately get returned to the caller).

Well, a json_string should be strdup because, with or without gc, everything malloc for a json_object will be free with this object.

I will try to use the gc with the json parsing stuff.

Quoted 24 lines
Quoted 24 lines
tools/libxl/libxl_qmp.c new file +24 -0
+static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
+                    int timeout)
+{
+    int ret;
+    int i = 0;
+
+    qmp->qmp_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+    if (qmp->qmp_fd < 0) {
+        return -1;
+    }
+
+    memset(&qmp->addr, 0, sizeof (&qmp->addr));
+    qmp->addr.sun_family = AF_UNIX;
+    strncpy(qmp->addr.sun_path, qmp_socket_path,
+            sizeof (qmp->addr.sun_path));
+
+    do {
+        ret = connect(qmp->qmp_fd, (struct sockaddr *) &qmp->addr,
+                      sizeof (qmp->addr));
+        if (ret == 0)
+            break;
+        if (errno == ENOENT || errno == ECONNREFUSED) {
+            /* ENOENT       : Socket may not have shown up yet
+             * ECONNREFUSED : Leftover socket hasn't been removed yet */
Quoted 8 lines

tools/libxl/libxl_qmp.c line 329

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+            continue;
+        }
+        return -1;
+    } while ((++i / 5 <= timeout) && (usleep(200 * 1000) <= 0));

If we exit this loop because we timed out do we need to set errno to ETIMEDOUT or something similar to indicate this? Or is the existing err errno (one of ENOENT or ECONNREFUSED?) a sufficient indicator?

I think it's should be a ETIMEDOUT. ENOENT or ECONNREFUSED explain just why we timeout.

Quoted 10 lines
Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10 -0
+
+    return ret;
+}
+
+static void qmp_close(libxl__qmp_handler *qmp)
+{
+    callback_id_pair *pp = NULL;
+    callback_id_pair *tmp = NULL;
+
+    close(qmp->qmp_fd);
Quoted 8 lines

tools/libxl/libxl_qmp.c line 343

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+    SIMPLEQ_FOREACH(pp, &qmp->callback_list, next) {
+        if (tmp)
+            free(tmp);
+        tmp = pp;

That seems like an odd construct, but I suppose it is necessary to work around the lack of a SIMPLEQ_FOREACH variant which is safe against removing the iterator from the list.

Yes.

Quoted 19 lines
Quoted 19 lines
tools/libxl/libxl_qmp.c new file +19 -0
+libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx, uint32_t domid)
+{
+    int ret = 0;
+    libxl__qmp_handler *qmp = NULL;
+    char *qmp_socket;
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+
+    qmp = qmp_init_handler(ctx, domid);
+
+    qmp_socket = libxl__sprintf(&gc, "%s/qmp-%d",
+                                libxl_run_dir_path(), domid);
+    if ((ret = qmp_open(qmp, qmp_socket, QMP_SOCKET_CONNECT_TIMEOUT)) < 0) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Connection error");
+        qmp_free_handler(qmp);
+        return NULL;
+    }
+
+    LIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "connected to %s", qmp_socket);
+
Quoted 8 lines

tools/libxl/libxl_qmp.c line 518

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+    /* Wait for the response to qmp_capabilities */
+    while (!qmp->connected) {
+        if ((ret = qmp_next(qmp)) < 0) {
+            break;

Is it an error condition not to get in to the connected state? Should we therefore qmp_free_handler and return NULL? That would save callers checking qmp->connected since they can just assume it is true...

That meen that QEMU did not respond to the "qmp_capabilities" command. And it's needed in order send other commands. So yes, it's an error. I will return NULL.

Quoted 18 lines

[...]

tools/libxl/libxl_qmp.c line 565

Quoted 9 lines
tools/libxl/libxl_qmp.c new file +9 -0
+int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid)
+{
+    libxl__qmp_handler *qmp = NULL;
+    int ret = 0;
+
+    qmp = libxl__qmp_initialize(ctx, domid);
+    if (!qmp)
+        return -1;
+    if (qmp->connected) {

... like here.

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+        ret = libxl__qmp_query_serial(qmp);
+    }
+    libxl__qmp_close(qmp);
+    return ret;
+}
#12 lore raw

Re: [PATCH V6 3/3] libxl, Introduce a QMP client

In reply to Anthony PERARD

Anthony PERARD writes ("[Xen-devel] [PATCH V6 3/3] libxl, Introduce a QMP client"):

Quoted 5 lines

QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.

     if (dm_starting) {

+ if (dm_info->device_model_version == LIBXL_DEVICE_MODEL_VERSION_QEMU

_XEN) {

Quoted 2 lines
tools/libxl/libxl_create.c +2 -0
+            libxl__qmp_initializations(ctx, domid);
+        }

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+    flexarray_append(dm_args,
+                     libxl__sprintf(gc, "socket,id=libxl-cmd,path=%s/qmp-%d,

server,nowait",

Quoted 1 line

+ libxl_run_dir_path(),

Lines too long, please wrap

Quoted 4 lines
tools/libxl/libxl_qmp.c new file +4 -0
+static void qmp_handle_error_response(libxl__qmp_handler *qmp,
+                                      const libxl__json_object *resp)
+{
+    callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);

...

tools/libxl/libxl_qmp.c line 226

Quoted 7 lines
tools/libxl/libxl_qmp.c new file +7 -0
+    if (pp) {
+        if (pp->id == qmp->wait_for_id) {
+            /* tell that the id have been processed */
+            qmp->wait_for_id = 0;
+        }
+        SIMPLEQ_REMOVE(&qmp->callback_list, pp, callback_id_pair, next);
+        free(pp);

I think this needs to call the callback, or the code which set up the callback will surely just continue to wait forever.

...

Later: I see that you have a single wait_for_id. What if multiple callers want to use a single qmp for multiple things ? Or is "wait_for_id" simply the one that you're waiting on synchronously ?

Quoted 2 lines
(quoted diff) +2 -0
+static int qmp_next(libxl__qmp_handler *qmp)
+{

...

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+        ret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
+        if (ret > 0) {
+            rd = read(qmp->qmp_fd, qmp->buffer, QMP_RECEIVE_BUFFER_SIZE);
+            if (rd > 0) {
+                break;

...

tools/libxl/libxl_qmp.c line 391

Quoted 5 lines
tools/libxl/libxl_qmp.c new file +5 -0
+    s = qmp->buffer;
+    s_end = qmp->buffer + rd;
+    while (s < s_end) {
+        libxl__json_object *o = libxl__json_parse(qmp->ctx, &qmp->yajl_ctx,
+                                                  &s, s_end - s);

This assumes that the response will be received in a single read(). This is not correct. read() may return partial results; it may also aggregate multiple writes from the sending qemu into a single read() result.

You need to pull data into the buffer and then test the buffer for completeness (eg by looking for the cr-lf), and split the buffer up into packets yourself, and if there are partial packets left over go round and read again.

Thanks,
Ian.

#13 lore raw

Re: [PATCH V6 3/3] libxl, Introduce a QMP client

Quoted 14 lines · Ian Jackson
Quoted 5 lines
tools/libxl/libxl_qmp.c new file +4 -0
+static void qmp_handle_error_response(libxl__qmp_handler *qmp,
+                                      const libxl__json_object *resp)
+{
+    callback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);

...

Quoted 7 lines

+    if (pp) {
+        if (pp->id == qmp->wait_for_id) {
+            /* tell that the id have been processed */
+            qmp->wait_for_id = 0;
+        }
+        SIMPLEQ_REMOVE(&qmp->callback_list, pp, callback_id_pair, next);
+        free(pp);

Quoted 2 lines

I think this needs to call the callback, or the code which set up the callback will surely just continue to wait forever.

Ok, I will just call the callback with NULL, so, the callback will fail explicitly. That will not change anything for the only callback I have.

Quoted 3 lines

Later: I see that you have a single wait_for_id.  What if multiple callers want to use a single qmp for multiple things ?  Or is "wait_for_id" simply the one that you're waiting on synchronously ?

The second.
Multiple callers can call qmp_send_synchronous multiple time.

Quoted 15 lines
Quoted 2 lines
(quoted diff) +2 -0
+static int qmp_next(libxl__qmp_handler *qmp)
+{

...

Quoted 5 lines

+        ret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
+        if (ret > 0) {
+            rd = read(qmp->qmp_fd, qmp->buffer, QMP_RECEIVE_BUFFER_SIZE);
+            if (rd > 0) {
+                break;

...

Quoted 5 lines

+    s = qmp->buffer;
+    s_end = qmp->buffer + rd;
+    while (s < s_end) {
+        libxl__json_object *o = libxl__json_parse(qmp->ctx, &qmp->yajl_ctx,
+                                                  &s, s_end - s);

Quoted 4 lines

This assumes that the response will be received in a single read(). This is not correct.  read() may return partial results; it may also aggregate multiple writes from the sending qemu into a single read() result.

Actually, json_parse takes care of these. It tells if it's a partiel result and how much of the buffer have been read. So the function assume that the caller will call qmp_next again if it's needed (like in case a callback have not been called).
I tried the function with a small buffer (not enough for an entire response) and it works fine.

Quoted 4 lines

You need to pull data into the buffer and then test the buffer for completeness (eg by looking for the cr-lf), and split the buffer up into packets yourself, and if there are partial packets left over go round and read again.

At least, I will check if the protocole is respected, and I will not relie anymore on json_parse to tell me the end of a packet.

Signature · 1 line
Anthony PERARD