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

[Xen-devel] Re: [PATCH V3] libxl, Introduce a QMP client


  • To: Ian Campbell <Ian.Campbell@xxxxxxxxxxxxx>
  • From: Anthony PERARD <anthony.perard@xxxxxxxxxx>
  • Date: Wed, 15 Jun 2011 19:44:30 +0100
  • Cc: Xen Devel <xen-devel@xxxxxxxxxxxxxxxxxxx>, Stefano Stabellini <Stefano.Stabellini@xxxxxxxxxxxxx>
  • Delivery-date: Wed, 15 Jun 2011 11:45:54 -0700
  • Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; b=QncSJfw8JCImScJ4N0k8Usy0/5vjAJkvfOBw2GDhj14iJ3svi+lVnmmNbRTH+kR8iv PEreYRSGpXEkqb0F98XwVdP/0SFTuSr65TD+ILaioHFzlLhxsUDBzXhRtiihpUA4ZSow 3SsUIm5kDo2xiuNumSVF0KKJ0K3dfC66fpw34=
  • List-id: Xen developer discussion <xen-devel.lists.xensource.com>

On Wed, Jun 15, 2011 at 11:04, Ian Campbell <Ian.Campbell@xxxxxxxxxxxxx> wrote:
> On Tue, 2011-06-14 at 18:15 +0100, Anthony Perard wrote:
>> From: Anthony PERARD <anthony.perard@xxxxxxxxxx>
>>
>> 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).
>>
>> Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
>> ---
>>
>> 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.)
>
> I would have sworn that initiali[zs]ations wasn't the plural of
> initiali[zs]e (it sounds wrong to my ear) and that it was one of those
> words with the same plural and singular form, but the Internet tells me
> I'm wrong...
>
> On the other hand libxl__qmp_domain_create works, so would something
> like libxl__qmp_gather_initial_info, the later conveys what's actually
> going on too...

Actually, we could also set up the VNC password by this way. So in
this case, it will not be anymore only "gather initial info".

>> 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.h    Â|  Â1 +
>> Âtools/libxl/libxl_create.c | Â Â4 +
>> Âtools/libxl/libxl_dm.c   |  Â6 +
>> Âtools/libxl/libxl_paths.c Â| Â Â4 +
>> Âtools/libxl/libxl_qmp.c  Â| Â980 
>> ++++++++++++++++++++++++++++++++++++++++++++
>> Âtools/libxl/libxl_qmp.h  Â|  35 ++
>> Â9 files changed, 1037 insertions(+), 0 deletions(-)
>> Âcreate mode 100644 tools/libxl/libxl_qmp.c
>> Âcreate mode 100644 tools/libxl/libxl_qmp.h
>>
>> diff --git a/Config.mk b/Config.mk
>> index aa681ae..8b11cd8 100644
>> --- a/Config.mk
>> +++ b/Config.mk
>> @@ -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
>>
>> diff --git a/config/StdGNU.mk b/config/StdGNU.mk
>> index 25aeb4d..68fa226 100644
>> --- a/config/StdGNU.mk
>> +++ b/config/StdGNU.mk
>> @@ -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)
>> diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
>> index 84ab76f..be5445d 100644
>> --- a/tools/libxl/Makefile
>> +++ b/tools/libxl/Makefile
>> @@ -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)
>> diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
>> index b0471c0..c3bbe87 100644
>> --- a/tools/libxl/libxl.h
>> +++ b/tools/libxl/libxl.h
>> @@ -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 */
>>
>> diff --git a/tools/libxl/libxl_create.c b/tools/libxl/libxl_create.c
>> index 91e2414..e818faf 100644
>> --- a/tools/libxl/libxl_create.c
>> +++ b/tools/libxl/libxl_create.c
>> @@ -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,
>> diff --git a/tools/libxl/libxl_dm.c b/tools/libxl/libxl_dm.c
>> index 5e80bc8..094226d 100644
>> --- a/tools/libxl/libxl_dm.c
>> +++ b/tools/libxl/libxl_dm.c
>> @@ -245,6 +245,12 @@ 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, "-qmp");
>> + Â Âflexarray_append(dm_args,
>> + Â Â Â Â Â Â Â Â Â Â libxl__sprintf(gc, "unix:%s/qmp-%d,server,nowait",
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âlibxl_run_dir_path(),
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âinfo->domid));
>
> Presumably qemu will clean this socket up in the normal shutdown case.
> Do we need to do so as well to handle crashes and the like?

Actually, qemu doesn't remove the socket. So we will have do clean it
when the domain is cleaned. Is libxl_domain_destroy() a good function
to do that?

> The handling of -qmp via monitor_parse() seems to suggest that this is a
> compat_monitor option of some sort. Is the modern way to use both a
> -chardev and a -qmp? e.g.
> Â Â Â Â-chardev socket,id=libxl-qmp,path="....",server,nowait
> Â Â Â Â-qmp chardev=libxl-qmp
> (or is it -mon not -qmp?)

-qmp is the easy way to enable QMP. But to have more complexe
combinations they suggest to use -chardev with -mon:
-chardev socket,id=mon1,path=./qmp,server,nowait  -mon chardev=mon1,mode=control

>> diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
>> new file mode 100644
>> index 0000000..061eea6
>> --- /dev/null
>> +++ b/tools/libxl/libxl_qmp.c
>> @@ -0,0 +1,980 @@
>> +/*
>> + * Copyright (C) 2011 Â Â ÂCitrix Ltd.
>> + * Author Anthony PERARD <anthony.perard@xxxxxxxxxx>
>> + *
>> + * 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 <stdio.h>
>> +#include <stdlib.h>
>> +#include <string.h>
>> +#include <unistd.h>
>> +
>> +#include <sys/un.h>
>> +#include <sys/queue.h>
>> +
>> +#include <yajl/yajl_parse.h>
>> +#include <yajl/yajl_gen.h>
>> +
>> +#include "libxl_internal.h"
>> +#include "flexarray.h"
>> +#include "libxl_qmp.h"
>> +
>> +/* #define DEBUG_ANSWER */
>> +/* #define DEBUG_RECEIVE */
>> +
>> +/*
>> + * json_object types
>> + */
>> +
>> +typedef struct json_object json_object;
>> +typedef struct json_map_node json_map_node;
>> +typedef enum node_type node_type_e;
>> +
>> +enum node_type {
>> + Â ÂJSON_ERROR,
>> + Â ÂJSON_NULL,
>> + Â ÂJSON_BOOL,
>> + Â ÂJSON_INTEGER,
>> + Â ÂJSON_DOUBLE,
>> + Â ÂJSON_STRING,
>> + Â ÂJSON_MAP,
>> + Â ÂJSON_ARRAY
>> +};
>
> We typically use
> Â Â Â Âtypedef enum NAME {
> Â Â Â Â Â Â Â Â....
> Â Â Â Â} NAME;
> in libxl so far. NAME is the same in both places, since this is an
> internal type you could omit the first (since it's really just for
> backward compat in the public interface).
>
>> +struct json_object {
>> + Â Ânode_type_e type;
>> + Â Âunion {
>> + Â Â Â Âbool boolean;
>> + Â Â Â Âlong integer;
>> + Â Â Â Âdouble floating;
>
> floating is a funny name in the context of the other members (it's an
> adjective the others are not) also the type is double not float.
>
> I guess float and double themselves are out since they are C keywords.

As suggested by george, I could use "fp" or "floating_point", but the
second is too long so I will just rename to "fp".

>> + Â Â Â Âconst char *string;
>> + Â Â Â Â/* List of json_object */
>> + Â Â Â Âflexarray_t *array;
>> + Â Â Â Â/* List of json_map_node */
>> + Â Â Â Âflexarray_t *map;
>> + Â Â} u;
>> + Â Âjson_object *parent;
>> +};
>
> Similarly we tend to just do
> Â Â Â Âtypedef struct {
> Â Â Â Â Â Â Â Â....
> Â Â Â Â} NAME;
> but not in this case due to the *parent member.
>
>> +struct json_map_node {
>> + Â Âconst char *map_key;
>> + Â Âjson_object *obj;
>> +};
>
> But you could follow the convention here (and a bunch of other places).

OK, I will do that.

>> [...]
>> +} message_type_e;
>> +
>> +struct {
>
> static... ?
>
> [...]
>> +/*
>> + * JSON callbacks
>> + */
>> +
>> +static int json_callback_null(void *opaque)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_null(qmp->g);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âobj->type = JSON_NULL;
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_boolean(void *opaque, int boolean)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_bool(qmp->g, boolean);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âobj->type = JSON_BOOL;
>> + Â Âobj->u.boolean = boolean;
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_integer(void *opaque, long value)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_integer(qmp->g, value);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âobj->type = JSON_INTEGER;
>> + Â Âobj->u.integer = value;
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_double(void *opaque, double value)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_double(qmp->g, value);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âobj->type = JSON_DOUBLE;
>> + Â Âobj->u.floating = value;
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_string(void *opaque, const unsigned char *str,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int len)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âchar *t = malloc(len + 1);
>> + Â Âjson_object *obj = NULL;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_string(qmp->g, str, len);
>> +#endif
>> +
>> + Â Âstrncpy(t, (const char *) str, len);
>> + Â Ât[len] = 0;
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âobj->type = JSON_STRING;
>> + Â Âobj->u.string = t;
>> +
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_map_key(void *opaque, const unsigned char *str,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Âunsigned int len)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âchar *t = malloc(len + 1);
>> + Â Âjson_object *obj = qmp->current;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_string(qmp->g, str, len);
>> +#endif
>> +
>> + Â Âstrncpy(t, (const char *) str, len);
>> + Â Ât[len] = 0;
>> +
>> + Â Âif (obj->type == JSON_MAP) {
>> + Â Â Â Âjson_map_node *node = malloc(sizeof (json_map_node));
>> +
>> + Â Â Â Ânode->map_key = t;
>> + Â Â Â Ânode->obj = NULL;
>> +
>> + Â Â Â Âflexarray_append(obj->u.map, node);
>> + Â Â} else {
>> + Â Â Â Âreturn 0;
>> + Â Â}
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_start_map(void *opaque)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj = NULL;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_map_open(qmp->g);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âif (qmp->head == NULL) {
>> + Â Â Â Âqmp->head = obj;
>> + Â Â}
>> +
>> + Â Âobj->type = JSON_MAP;
>> + Â Âobj->u.map = flexarray_make(1, 1);
>> +
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> +
>> + Â Âobj->parent = qmp->current;
>> + Â Âqmp->current = obj;
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_end_map(void *opaque)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_map_close(qmp->g);
>> +#endif
>> +
>> + Â Âif (qmp->current) {
>> + Â Â Â Âqmp->current = qmp->current->parent;
>> + Â Â} else {
>> + Â Â Â ÂLIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR, "no parents for a 
>> json_object");
>> + Â Â Â Âreturn 0;
>> + Â Â}
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_start_array(void *opaque)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> + Â Âjson_object *obj = NULL;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_array_open(qmp->g);
>> +#endif
>> +
>> + Â Âobj = calloc(1, sizeof (json_object));
>> + Â Âif (qmp->head == NULL) {
>> + Â Â Â Âqmp->head = obj;
>> + Â Â}
>> + Â Âobj->type = JSON_ARRAY;
>> + Â Âobj->u.array = flexarray_make(1, 1);
>> + Â Âjson_object_append_to(qmp, obj, qmp->current);
>> + Â Âqmp->current = obj;
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static int json_callback_end_array(void *opaque)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = opaque;
>> +
>> +#ifdef DEBUG_ANSWER
>> + Â Âyajl_gen_array_close(qmp->g);
>> +#endif
>> +
>> + Â Âif (qmp->current) {
>> + Â Â Â Âqmp->current = qmp->current->parent;
>> + Â Â} else {
>> + Â Â Â ÂLIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR, "no parents for a 
>> json_object");
>> + Â Â Â Âreturn 0;
>> + Â Â}
>> +
>> + Â Âreturn 1;
>> +}
>> +
>> +static yajl_callbacks callbacks = {
>> + Â Âjson_callback_null,
>> + Â Âjson_callback_boolean,
>> + Â Âjson_callback_integer,
>> + Â Âjson_callback_double,
>> + Â ÂNULL,
>
> This is the "number" callback? Would be useful to do
> Â Â Â NULL /* Number */,
>
> Also, how come we don't need this one? (nevermind, I see in the docs
> that interger+double and number are mutually exclusive)
>
>> + Â Âjson_callback_string,
>> + Â Âjson_callback_start_map,
>> + Â Âjson_callback_map_key,
>> + Â Âjson_callback_end_map,
>> + Â Âjson_callback_start_array,
>> + Â Âjson_callback_end_array
>> +};
>> +
>> +/*
>> + * QMP callbacks functions
>> + */
>> +
>> +static const char *get_serial0_chardev(libxl__qmp_handler *qmp, const 
>> json_object *tree)
>> +{
>> + Â Âconst json_object *ret = NULL;
>> + Â Âconst json_object *obj = NULL;
>> + Â Âconst json_object *label = NULL;
>> + Â Âconst char *s = NULL;
>> + Â Âflexarray_t *array = NULL;
>> + Â Âint index = 0;
>> +
>> + Â Âret = json_object_map_get("return", tree);
>> +
>> + Â Âif (!ret || ret->type != JSON_ARRAY) {
>
> Do these conditions represent some sort of protocol error or is it
> acceptable?

It's more a wrong answer to this specific command ("query-chardev")
than a protocol error. As the doc says that is an array.

>> + Â Â Â Âreturn NULL;
>> + Â Â}
>> + Â Âarray = ret->u.array;
>> + Â Âfor (index = 0; index < array->count; index++) {
>> + Â Â Â Âif (flexarray_get(array, index, (void**)&obj) != 0)
>> + Â Â Â Â Â Âbreak;
>> + Â Â Â Âlabel = json_object_map_get("label", obj);
>> + Â Â Â Âs = json_object_get_string(label);
>> +
>> + Â Â Â Â/* TODO Could replace serial0 by serial and get all serial ttys, if 
>> sevral */
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 
> several
> [...]
>
>> +static void qmp_handle_error_response(libxl__qmp_handler *qmp, json_object 
>> *resp)
>> +{
>> + Â Âcallback_id_pair *pp = qmp_get_callback_from_id(qmp, resp);
>> + Â Âconst json_object *error = json_object_map_get("error", resp);
>> + Â Âconst char *msg = json_object_get_string(json_object_map_get("desc", 
>> error));
>> +
>> + Â Â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,
>> + Â Â Â Â Â Â Â "receive an error message from QMP server: %s",
> Â Â Â Â Â Â Â Â Â received
>
>> + Â Â Â Â Â Â Â msg);
>> +}
> [...]
>> +/*
>> + * Handler functions
>> + */
>> +
>> +static int qmp_connect(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 */
>> + Â Â Â Â Â Âcontinue;
>> + Â Â Â Â}
>> + Â Â Â Âreturn -1;
>> + Â Â} while ((++i <= timeout * 5) && (usleep(.2 * 1000000) <= 0));
>
> usleep (effectively) takes an unsigned int, what typedoes .2 * x become?

It should be a float. But have 0.2 * 10^6 will be better to understand
that we have 0.2 second of sleep, but not power in C. If you prefere,
I will just wrote 200000, should be OK.

> In any case given you have "* 5" wouldn't "/ 5" be a bit clearer?

Yes, a little bit.

>> +
>> + Â Âif (ret == -1)
>> + Â Â Â Âreturn -1;
>> +
>> + Â Âreturn 0;
>
> Are values of ret other than 0 or -1 possible here? connect only returns
> 0 or -1, I think you can just return ret?

I ask myself this question when I wrote this to put "return ret;" or
these two lines. I though it was a bit clearer that the function
return only -1 or 0. But "return ret;" should be OK.

> [...]
>> +static int qmp_next(libxl__qmp_handler *qmp)
>> +{
>> + Â Âyajl_status status;
>> + Â Âssize_t rd;
>> + Â Âssize_t bytes_parsed = 0;
>> +
>> + Â Â/* read the socket */
>> + Â Ârd = read(qmp->qmp_fd, qmp->buffer, QMP_RECEIVE_BUFFER_SIZE);
>> + Â Âif (rd <= 0) {
>> + Â Â Â Â/* either an error, or nothing */
>> + Â Â Â Âreturn rd;
>> + Â Â}
>
> Does this (and the following while loop) handle reads which return only
> a partial json datastructure? I'd expect to see some sort of loop around
> the whole thing and some buffer shuffling and/or offsets if so.

If yajl needs more data, we have to call qmp_next again. If I put the
whole thing in a loop, I will also move the select. Actually, this
should be a bit better, thus they will have only one select instead of
two, currently.

>> +#ifdef DEBUG_RECEIVE
>> + Â Âqmp->buffer[rd] = 0;
>> + Â ÂLIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "received: '%s'", qmp->buffer);
>> +#endif
>> +
>> + Â Âwhile (bytes_parsed < rd) {
> [...]
>> + Â Â Â Â/* skip the CRLF of the end of a command */
>> + Â Â Â Âwhile (bytes_parsed < rd && (qmp->buffer[bytes_parsed] ==
>> '\r'
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â || qmp->buffer[bytes_parsed] ==
>> '\n')) {
>> + Â Â Â Â Â Â Â bytes_parsed++;
>> + Â Â Â Â}
>
> The parser doesn't just eat \r & \n?

It does not eat it when it finished to parse a response but it will
eat these after. So the loop do another round and end up with a
partiel json data and an allocated parser.

This little loop is just here to avoid doing a not necessary turn but
is not necessary

> [...]
>> +static int qmp_send(libxl__qmp_handler *qmp, const char *cmd, 
>> qmp_callback_t callback)
>> +{
>> + Â Âyajl_gen_config conf = { 0, NULL };
>> + Â Âconst unsigned char *buf;
>> + Â Âconst char *ex = "execute";
>> + Â Âunsigned int len = 0;
>> + Â Âyajl_gen_status s;
>> + Â Âyajl_gen hand;
>> +
>> + Â Âhand = yajl_gen_alloc(&conf, NULL);
>> + Â Âif (!hand) {
>> + Â Â Â Âreturn -1;
>> + Â Â}
>> +
>> + Â Âyajl_gen_map_open(hand);
>> + Â Âyajl_gen_string(hand, (const unsigned char *)ex, strlen(ex));
>> + Â Âyajl_gen_string(hand, (const unsigned char *)cmd, strlen(cmd));
>> + Â Âyajl_gen_string(hand, (const unsigned char *)"id", 2);
>
> ex is a variable and "id" is not?

Well, it was easier to cound the char in "id" than in "execute", that
why there is a ex variable. But I think the next step would to eithier
use a #define QMP_{EXECUTE,ID} or a static const char* global to the
file.

> You'd think yajl would have yajl_gen_asciiz or something.

Unfortunatly, there is no such thing, at least in the debian version
(1.?). I did not check the 2.? version of libyajl.

> (am I the only one who thinks the choice of unsigned char in the library
> is odd?)

No, me too, it just really annoying.

>> + Â Âyajl_gen_integer(hand, ++qmp->last_id_used);
>> + Â Âyajl_gen_map_close(hand);
>> +
>> + Â Âs = yajl_gen_get_buf(hand, &buf, &len);
>> +
>> + Â Âif (s) {
>> + Â Â Â ÂLIBXL__LOG(qmp->ctx, LIBXL__LOG_ERROR,
>> + Â Â Â Â Â Â Â Â Â "could not generate a qmp command");
>> + Â Â Â Âreturn -1;
>> + Â Â}
>> +
>> + Â Âif (callback) {
>> + Â Â Â Âcallback_id_pair *elm = malloc(sizeof (callback_id_pair));
>> + Â Â Â Âelm->id = qmp->last_id_used;
>> + Â Â Â Âelm->callback = callback;
>> + Â Â Â ÂSIMPLEQ_INSERT_TAIL(&qmp->callback_list, elm, next);
>> + Â Â}
>> +
>> + Â ÂLIBXL__LOG(qmp->ctx, LIBXL__LOG_DEBUG, "next qmp command: '%s'", buf);
>> +
>> + Â Âwrite(qmp->qmp_fd, buf, len);
>> + Â Âwrite(qmp->qmp_fd, "\r\n", 2);
>
> These need to handle partial writes?

What did you mean ?
Call write twice was easier here than call a snprintf or other.

>> + Â Âyajl_gen_free(hand);
>> +
>> + Â Âreturn qmp->last_id_used;
>> +}
>> +
>> +static int qmp_synchronous_send(libxl__qmp_handler *qmp, const char *cmd, 
>> qmp_callback_t callback, int ask_timeout)
>> +{
>> + Â Âint id = 0;
>> + Â Âint ret = 0;
>> + Â Âfd_set rfds;
>> + Â Âstruct timeval timeout;
>> +
>> + Â Âid = qmp_send(qmp, cmd, callback);
>> + Â Âif (id <= 0) {
>> + Â Â Â Âreturn -1;
>> + Â Â}
>> + Â Âqmp->wait_for_id = id;
>> +
>> + Â Âtimeout.tv_sec = ask_timeout;
>> + Â Âtimeout.tv_usec = 0;
>> +
>> + Â Âwhile (qmp->wait_for_id == id) {
>> + Â Â Â ÂFD_ZERO(&rfds);
>> + Â Â Â ÂFD_SET(qmp->qmp_fd, &rfds);
>> + Â Â Â Âret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
>
> Linux modifies timeout to reflect the amount of time left, which will
> mean that the timeout shrinks as answers which aren't for us come in.
> You need to init timeout inside the loop. select(2) recommends treating
> timeout as undefined after a select().
>
> Do we want an overall timeout in case qemu never responds?

I did want an overall timeout. But I should just have a timeout for a
single "read" and do as recommended by the man. Also, I just see that
I just try again in case of timeout (with ret == 0) so I will fix
that.

>> + Â Â Â Âif (ret > 0) {
>> + Â Â Â Â Â Âif ((ret = qmp_next(qmp)) < 0) {
>> + Â Â Â Â Â Â Â Âreturn ret;
>> + Â Â Â Â Â Â}
>> + Â Â Â Â} else if (ret < 0) {
>> + Â Â Â Â Â ÂLIBXL__LOG_ERRNO(qmp->ctx, LIBXL__LOG_ERROR, "Select error");
>> + Â Â Â Â Â Âreturn -1;
>> + Â Â Â Â}
>> + Â Â}
>> + Â Âreturn 0;
>> +}
>> +
>> +static libxl__qmp_handler *qmp_init_handler(libxl_ctx *ctx, uint32_t domid)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = NULL;
>> +
>> + Â Âqmp = calloc(1, sizeof (libxl__qmp_handler));
>> + Â Âqmp->ctx = ctx;
>> + Â Âqmp->domid = domid;
>> + Â Âif ((qmp->buffer = malloc(QMP_RECEIVE_BUFFER_SIZE)) == NULL) {
>> + Â Â Â ÂLIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Can not allocate the 
>> reception buffer");
>> + Â Â Â Âfree(qmp);
>> + Â Â Â Âreturn NULL;
>> + Â Â}
>
> Any reason not to include buffer[QMP_RECEIVE_BUFFER_SIZE] directly in
> libxl__qmp_handler and avoid handling multiple allocations?

No reason, indeed. I will put it in the structure.

> [...]
>> +
>> +/*
>> + * API
>> + */
>> +
>> +libxl__qmp_handler *libxl__qmp_initialize(libxl_ctx *ctx, uint32_t domid)
>> +{
>> + Â Âint ret = 0;
>> + Â Âlibxl__qmp_handler *qmp = NULL;
>> + Â Âchar qmp_socket[40];
>> + Â Âfd_set rfds;
>> + Â Âstruct timeval timeout;
>> +
>> + Â Âqmp = qmp_init_handler(ctx, domid);
>> +
>> + Â Âsnprintf(qmp_socket, sizeof (qmp_socket), "%s/qmp-%d",
>> + Â Â Â Â Â Â libxl_run_dir_path(), domid);
>
> libxl__sprintf?

will change that.

>> + Â Âif ((ret = qmp_connect(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);
>> +
>> + Â Âtimeout.tv_sec = 5;
>> + Â Âtimeout.tv_usec = 0;
>> +
>> + Â Â/* Wait for the response to qmp_capabilities */
>> + Â Âwhile (!qmp->connected) {
>> + Â Â Â ÂFD_ZERO(&rfds);
>> + Â Â Â ÂFD_SET(qmp->qmp_fd, &rfds);
>> + Â Â Â Âret = select(qmp->qmp_fd + 1, &rfds, NULL, NULL, &timeout);
>
> Same comment as before regarding timeout's value after select.
>
> [...]
>> +}
>> +
>> +int libxl__qmp_get_fd(libxl__qmp_handler *qmp)
>> +{
>> + Â Âif (qmp)
>> + Â Â Â Âreturn qmp->qmp_fd;
>> + Â Âelse
>> + Â Â Â Âreturn -1;
>> +}
>
> Unused?

This can be use if we want in the future to know the qemu event. So
know the fd outside this file can be use. But indeed, not yet. So we
will just readd we the time will come.

>> +
>> +int libxl__qmp_send_command(libxl__qmp_handler *qmp, libxl__qmp_command_e 
>> command)
>> +{
> [...]
>> +}
>> +
>> +int libxl__qmp_do_next(libxl__qmp_handler *qmp)
>> +{
> [...]
>> +}
>> +
>> +void libxl__qmp_close(libxl__qmp_handler *qmp)
>> +{
> [...]
>> +}
>
> These could all be static at the moment (are some of them also unused?),
> but I assume you are making them useful for future external uses which
> is ok.

Yes, they should be usefull in future external use. And apart from the
get_fd one, are all used.

>> +
>> +int libxl__qmp_initializations(libxl_ctx *ctx, uint32_t domid)
>> +{
>> + Â Âlibxl__qmp_handler *qmp = NULL;
>> + Â Âint ret = 0;
>> +
>> + Â Âqmp = libxl__qmp_initialize(ctx, domid);
>
> Might ..._open() be a better name since it returns a handle and its
> counterpart is ..._close()?

Yes, will change that.

>> + Â Âif (!qmp)
>> + Â Â Â Âreturn -1;
>> + Â Âif (qmp->connected) {
>> + Â Â Â Âret = libxl__qmp_send_command(qmp, QMP_COMMAND_QUERY_CHARDEV);
>> + Â Â}
>> + Â Âlibxl__qmp_close(qmp);
>> + Â Âreturn ret;
>> +}

Thanks for the review,

-- 
Anthony PERARD

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

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