libxl, Introduce a QMP client
PATCH v4 1/1 libxl, Introduce a QMP client
From: Anthony PERARD <[email protected]>
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).
---
Change v3->v4:
- In case of a timeout, there is no more infinit loop
- include select bit in the qmp_next function, to avoid have it twice in the code.
- introduce libxl__qmp_cleanup to remove the socket file. It's called from libxl_domain_destroy when a dm is present.
- libxl__qmp_get_fd is removed as is not yet used.
- qmp_connect renamed to qmp_open
- I'm not skip anymore the CRLF a the end of a command. So there will always be another turn in the loop to eat them.
- fix coding style issue related to struct definitions.
- the receive buffer is now include in the qmp_handler struct to avoid an extra alloc.
- in struct json_object, rename floating to fp.
- introduce yajl_gen_asciiz that take a string nul-terminated and give it to yajl_gen_string.
- use "-chardev ... -mon ..." qemu option instead of -qmp "..."
Change v2->v3:
- Use of a timeout when wait for a reply from the server.
- Use of a command list, a list of pair "command" + callback. It's associated with an enum.
- Introduce libxl__qmp_initializations that will ask of all informations need through QMP. (It's just a rename of libxl__qmp_get_serial_console_path from the previous patch.)
Change v1->v2:
- Introduction of libxl_run_dir_path(), should maybe be in another patch.
- Add a new static function qmp_synchronous_send that wait the answer from the server.
- QMP is know use only inside libxl, so only one command is send through the socket and after, the connection is closed.
Config.mk | 1 + config/StdGNU.mk | 2 + tools/libxl/Makefile | 4 + tools/libxl/libxl.c | 4 + tools/libxl/libxl.h | 1 + tools/libxl/libxl_create.c | 4 + tools/libxl/libxl_dm.c | 9 + tools/libxl/libxl_paths.c | 4 + tools/libxl/libxl_qmp.c | 974 ++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_qmp.h | 35 ++ 10 files changed, 1038 insertions(+), 0 deletions(-) create mode 100644 tools/libxl/libxl_qmp.c create mode 100644 tools/libxl/libxl_qmp.h
Config.mk +1 -0
@@ -133,6 +133,7 @@ define buildmakevars2file-closure echo "XEN_CONFIG_DIR=\"$(XEN_CONFIG_DIR)\"" >> $(1).tmp; \ echo "XEN_SCRIPT_DIR=\"$(XEN_SCRIPT_DIR)\"" >> $(1).tmp; \ echo "XEN_LOCK_DIR=\"$(XEN_LOCK_DIR)\"" >> $(1).tmp; \ + echo "XEN_RUN_DIR=\"$(XEN_RUN_DIR)\"" >> $(1).tmp; \ if ! cmp $(1).tmp $(1); then mv -f $(1).tmp $(1); fi endef
config/StdGNU.mk +2 -0
@@ -52,9 +52,11 @@ PRIVATE_BINDIR = $(PRIVATE_PREFIX)/bin ifeq ($(PREFIX),/usr) CONFIG_DIR = /etc XEN_LOCK_DIR = /var/lock +XEN_RUN_DIR = /var/run/xen else CONFIG_DIR = $(PREFIX)/etc XEN_LOCK_DIR = $(PREFIX)/var/lock +XEN_RUN_DIR = $(PREFIX)/var/run/xen endif SYSCONFIG_DIR = $(CONFIG_DIR)/$(CONFIG_LEAF_DIR)
tools/libxl/Makefile +4 -0
@@ -32,6 +32,9 @@ endif LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o LIBXL_OBJS-$(CONFIG_IA64) += libxl_nocpuid.o +LIBXL_OBJS-y += libxl_qmp.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)
@@ -115,6 +118,7 @@ install: all $(INSTALL_DIR) $(DESTDIR)$(LIBDIR) $(INSTALL_DIR) $(DESTDIR)$(INCLUDEDIR) $(INSTALL_DIR) $(DESTDIR)$(BASH_COMPLETION_DIR) + $(INSTALL_DIR) $(DESTDIR)$(XEN_RUN_DIR) $(INSTALL_PROG) xl $(DESTDIR)$(SBINDIR) $(INSTALL_PROG) libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR) ln -sf libxenlight.so.$(MAJOR).$(MINOR) $(DESTDIR)$(LIBDIR)/libxenlight.so.$(MAJOR)
tools/libxl/libxl.c +4 -0
@@ -35,6 +35,7 @@#include "libxl_utils.h" #include "libxl_internal.h" #include "flexarray.h" +#include "libxl_qmp.h" #define PAGE_TO_MEMKB (pages) ((pages) * 4)#define BACKEND_STRING_SIZE 5
@@ -757,6 +758,9 @@ 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); + +/* TODO only call this with qemu-version == qemu-xen */ +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.h +1 -0
@@ -518,6 +518,7 @@ const char *libxl_xenfirmwaredir_path(void);const char *libxl_xen_config_dir_path (void );const char *libxl_xen_script_dir_path (void );const char *libxl_lock_dir_path (void ); +const char *libxl_run_dir_path (void );#endif /* LIBXL_H */
tools/libxl/libxl_create.c +4 -0
@@ -30,6 +30,7 @@#include "libxl_utils.h" #include "libxl_internal.h" #include "flexarray.h" +#include "libxl_qmp.h" void libxl_domain_config_destroy (libxl_domain_config *d_config) {
@@ -514,6 +515,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
@@ -245,6 +245,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_paths.c +4 -0
@@ -64,3 +64,7 @@ const char *libxl_lock_dir_path(void) {return XEN_LOCK_DIR ; } +const char *libxl_run_dir_path (void ) +{ +return XEN_RUN_DIR ; +}
tools/libxl/libxl_qmp.c new file +974 -0
974 lines not on this page · raw message
tools/libxl/libxl_qmp.h new file +35 -0
@@ -0,0 +1,35 @@ +/* +* 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. +*/ + +#include "libxl.h" + +typedef struct libxl__qmp_handler libxl__qmp_handler ; +typedef enum libxl__qmp_command_e libxl__qmp_command_e ; + +/* QMP Command that can be send */ +enum libxl__qmp_command_e { +QMP_COMMAND_QUERY_CHARDEV , +QMP_COMMAND_NUMBER , +}; + + +_hidden libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx, +uint32_t domid); +_hidden int libxl__qmp_send_command(libxl__qmp_handler *qmp, libxl__qmp_command_e command); +_hidden int libxl__qmp_do_next(libxl__qmp_handler *qmp); +_hidden void libxl__qmp_close(libxl__qmp_handler *qmp); +_hidden void libxl__qmp_cleanup(libxl__gc *gc, uint32_t domid); + +_hidden int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid);
Signature · 1 line
1.7.2.5
[email protected] writes ("[Xen-devel] [PATCH V4] libxl, Introduce a QMP client"):
Quoted 2 lines
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Thanks for this; it's coming along. I have some comments....
Firstly, can you make sure all your lines are less than around 75 columns ? Otherwise they wrap badly on 80-column terminals (when viewing the code, and when replying to emails containing diffs).
Config.mk define buildmakevars2file-closure · line 136
Quoted 1 line
Config.mk +1
-0+ echo "XEN_RUN_DIR=\"$(XEN_RUN_DIR)\"" >> $(1).tmp; \
Can we have the introduction of XEN_RUN_DIR in a separate patch ?
tools/libxl/libxl_qmp.h
Quoted 5 lines
tools/libxl/libxl_qmp.h new file +0
-0
Is this supposed to be an internal-only header file ? If so it's not clear to me why it shouldn't all be in libxl_internal.h.
Quoted 5 lines
tools/libxl/libxl_qmp.h new file +5
-0+/* QMP Command that can be send */ +enum libxl__qmp_command_e { +QMP_COMMAND_QUERY_CHARDEV , +QMP_COMMAND_NUMBER , +};
...
Quoted 1 line
+_hidden int
libxl__qmp_send_command(libxl__qmp_handler *qmp, libxl__qmp_command_e command);
I'm not entirely convinced by this interface. Wouldn't it be better to have a specific function for each command ? After all commands may have arguments. Something like:
_hidden int libxl__qmp_send_query_chardev(libxl__qmp_handler *qmp);
It's not clear from your header file what the intended calling order of these is. I think there should be a comment in the private header file explaining what to do in what order.
And if we are keeping the enums:
tools/libxl/libxl_qmp.h line 19
Quoted 2 lines
tools/libxl/libxl_qmp.h new file +2
-0+typedef struct libxl__qmp_handler libxl__qmp_handler ; +typedef enum libxl__qmp_command_e libxl__qmp_command_e ;
I don't think this _e suffix is very helpful. Just call it libxl__qmp_command.
And our practice is not normally to declare the tag as well as the typedef for an enum. So I think you want just
typedef enum { .... } libxl__qmp_command.
Quoted 4 lines
tools/libxl/libxl.c +0
-0
...
tools/libxl/libxl.c int libxl_domain_destroy(libxl_ctx *ctx, uint32_t domid, int force) · line 763
Quoted 3 lines
tools/libxl/libxl.c +3
-0+ +/* TODO only call this with qemu-version == qemu-xen */ +libxl__qmp_cleanup (&gc, domid);
I don't think we want to add more TODOs. (And if we did, the word we should use is FIXME.)
The best solution is to make this function idempotent, in which case calling it is harmless.
tools/libxl/libxl.c
Quoted 8 lines
tools/libxl/libxl.c +8
-0+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" );
Yuck, what a horrible command line interface is presented here by qemu! But this is not your fault :-).
Quoted 6 lines
tools/libxl/libxl_qmp.c new file +0
-0@@ -0,0 +1,974 @@
...
tools/libxl/libxl_qmp.c line 54
Quoted 12 lines
tools/libxl/libxl_qmp.c new file +12
-0+typedef enum { +JSON_ERROR , +JSON_NULL , +JSON_BOOL , +JSON_INTEGER , +JSON_DOUBLE , +JSON_STRING , +JSON_MAP , +JSON_ARRAY +}node_type_e ; + +typedef struct json_object {
It's odd to see this here in our (libxl) code. Is this kind of thing not provided by the json library ?
Quoted 8 lines
tools/libxl/libxl_qmp.c new file +8
-0+typedef enum { +QMP_ANY , +QMP_QMP , +QMP_RETURN , +QMP_ERROR , +QMP_EVENT +}message_type_e ; +
tools/libxl/libxl_qmp.c line 99
Quoted 10 lines
tools/libxl/libxl_qmp.c new file +10
-0+static struct { +const char *name; +message_type_e type; +} member_name_to_message_type[] = { + {"QMP" ,QMP_QMP }, + {"return" ,QMP_RETURN }, + {"error" ,QMP_ERROR }, + {"event" ,QMP_EVENT }, + {"" ,QMP_ANY }, +};
If we do need to define these, we should use a libxl idl enum (with Ian Campbell's string-mapping-generation patch which I will apply shortly.
Quoted 3 lines
+/*
+ *json_objectfunctions
+ */
I don't understand what these are for. They look disturbingly like boilerplate or at least rather verbose plumbing.
Quoted 1 line
+static int
json_object_append_to(libxl__qmp_handler *qmp, json_object *obj, json_object *dst)
Does our json library not provide a pile of generic facilities for managing json objects ? Why do we need all of this code ?
Quoted 1 line
+static
yajl_callbackscallbacks = {
Oh wait, I just looked up yajl. Why are we using a SAX-style (callback-based) json parser ?
Surely it would be better to use a json library which simply provides an in-memory data structure for each json object. We don't anticipate dealing with any huge json objects (which is the motivation for the SAX-style approach).
I will stop with comments now because I don't want to get too bogged down in details if we're going to change the approach.
Ian.
Quoted 7 lines · I wrote:
Oh wait, I just looked up yajl. Why are we using a SAX-style (callback-based) json parser ?
Surely it would be better to use a json library which simply provides an in-memory data structure for each json object. We don't anticipate dealing with any huge json objects (which is the motivation for the SAX-style approach).
Well, I went and did some searching and I have discovered that:
There does not appear to be any widely-used "atomic-style" json library for C, other than the one which comes with glib (which is unsuitable because we don't want to import glib into our project).
YAJL 2 does have a simple "atomic" ("tree" they call it) interface. It's not in squeeze or afaict recent fedora.
YAJL 1 is even lacking in Debian lenny. :-/.
So probably what we have is the best approach. But perhaps it might be worth considering whether we should make our interface to json objects the similar to yajl 1's tree interface ?
Ian.
Quoted 15 lines · Ian Jackson
tools/libxl/libxl_qmp.c line 99
Quoted 11 lines
tools/libxl/libxl_qmp.c new file +10
-0+static struct { +const char *name; +message_type_e type; +} member_name_to_message_type[] = { + {"QMP" ,QMP_QMP }, + {"return" ,QMP_RETURN }, + {"error" ,QMP_ERROR }, + {"event" ,QMP_EVENT }, + {"" ,QMP_ANY }, +};If we do need to define these, we should use a libxl idl enum (with Ian Campbell's string-mapping-generation patch which I will apply shortly.
Just to note that this is enum is only used internally, using the IDL as is would make it public... It'd be relatively easy to allow for internal types in the IDL but I'm not sure if we want to? I think if we did I'd be tempted to add a separate libxl_internal.idl generating (using the same generator) _libxl_types_internal.[ch] etc.
Ian.
Ian Campbell writes ("Re: [Xen-devel] [PATCH V4] libxl, Introduce a QMP client"):
Quoted 10 lines · Ian Jackson
Quoted 3 lines
If we do need to define these, we should use a libxl idl enum (with Ian Campbell's string-mapping-generation patch which I will apply shortly.
Just to note that this is enum is only used internally, using the IDL as is would make it public... It'd be relatively easy to allow for internal types in the IDL but I'm not sure if we want to? I think if we did I'd be tempted to add a separate libxl_internal.idl generating (using the same generator) _libxl_types_internal.[ch] etc.
Yes.
My point was that we shouldn't have handwritten tables of enum-vs-strings when we have a generator ...
Ian.
[email protected] writes ("[Xen-devel] [PATCH V4] libxl, Introduce a QMP client"):
Quoted 2 lines
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Thanks. You still haven't fixed the long lines :-/. Can you do so please as it makes the review a lot easier if you do ?
Ian.
Quoted 6 lines · I wrote:
[email protected] writes ("[Xen-devel] [PATCH V4] libxl, Introduce a QMP client"):
Quoted 2 lines
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Thanks. You still haven't fixed the long lines :-/. Can you do so please as it makes the review a lot easier if you do ?
Oh, wait, this wasn't v5. I said that for v4 already. Sorry.
Ian.
Quoted 6 lines · Ian Jackson
[email protected] writes ("[Xen-devel] [PATCH V4] libxl, Introduce a QMP client"):
Quoted 2 lines
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Thanks for this; it's coming along. I have some comments....
[...]
Quoted 6 lines
Quoted 5 lines
tools/libxl/libxl_qmp.h new file +5
-0+/* QMP Command that can be send */ +enum libxl__qmp_command_e { + QMP_COMMAND_QUERY_CHARDEV, + QMP_COMMAND_NUMBER, +};...
Quoted 7 lines
Quoted 1 line
+_hidden int
libxl__qmp_send_command(libxl__qmp_handler *qmp, libxl__qmp_command_e command);I'm not entirely convinced by this interface. Wouldn't it be better to have a specific function for each command ? After all commands may have arguments. Something like:
_hiddenintlibxl__qmp_send_query_chardev(libxl__qmp_handler *qmp);
Indeed, it should be better to have specific functions. Some commands will have arguments. And have a generic function for all of them will maybe not be easy and not clear.
Signature · 1 line
Anthony PERARD