libxl, Introduce a QMP client
PATCH 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).
--- tools/libxl/Makefile | 4 + tools/libxl/libxl_dm.c | 5 + tools/libxl/libxl_qmp.c | 864 ++++++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_qmp.h | 25 ++ tools/libxl/xl_cmdimpl.c | 29 ++- 5 files changed, 926 insertions(+), 1 deletions(-) create mode 100644 tools/libxl/libxl_qmp.c create mode 100644 tools/libxl/libxl_qmp.h
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)/var/run/xen $(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_dm.c +5 -0
@@ -245,6 +245,11 @@ 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:/var/run/xen/qmp-%d,server,nowait" , + info->domid)); +if (info->type ==LIBXL_DOMAIN_TYPE_PV ) {flexarray_append (dm_args,"-xen-attach" ); }
tools/libxl/libxl_qmp.c new file +864 -0
864 lines not on this page · raw message
tools/libxl/libxl_qmp.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* 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 ; + +libxl_qmp_handler *libxl_qmp_initialize (libxl_ctx *ctx, +const char *qmp_socket, +uint32_t domid); +int libxl_qmp_get_fd (libxl_qmp_handler *qmp); +int libxl_qmp_do_next (libxl_qmp_handler *qmp); +void libxl_qmp_close (libxl_qmp_handler *qmp);
tools/libxl/xl_cmdimpl.c +28 -1
@@ -39,6 +39,7 @@#include "libxl.h" #include "libxl_utils.h" #include "libxlutil.h" +#include "libxl_qmp.h" #include "xl.h" #define CHK_ERRNO ( call ) ({ \
@@ -1367,6 +1368,9 @@ static int create_domain(struct domain_create *dom_info)pid_t child_console_pid =-1 ;struct save_file_header hdr; +libxl_qmp_handler *qmp_handler =NULL ; +int qmp_fd =-1 ; +memset (&d_config,0x00 ,sizeof (d_config));if (restore_file) {
@@ -1606,6 +1610,19 @@ start:libxl_wait_for_disk_ejects (ctx, domid, d_config.disks, d_config.num_disks, w1);libxl_wait_for_domain_death (ctx, domid, w2);libxl_get_wait_fd (ctx, &fd); +if (d_config.dm_info.device_model_version ==LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN ) { +char socket[40 ]; +snprintf (socket,sizeof (socket),"/var/run/xen/qmp-%d" , domid); +LOG ("Try initialize QMP connection, path: %s" , socket); + qmp_handler =libxl_qmp_initialize (ctx, socket, domid); +if (qmp_handler) { + qmp_fd =libxl_qmp_get_fd (qmp_handler); + }else { +LOG ("Could not connect to the QMP socket." ); + qmp_fd =-1 ; + } + } +while (1 ) {int ret;fd_set rfds;
@@ -1615,10 +1632,12 @@ start:FD_ZERO (&rfds);FD_SET (fd, &rfds); +FD_SET (qmp_fd, &rfds); - ret =select (fd +1 , &rfds,NULL ,NULL ,NULL ); + ret =select (fd > qmp_fd ? fd +1 : qmp_fd +1 , &rfds,NULL ,NULL ,NULL );if (!ret)continue ; +if (FD_ISSET (fd, &rfds)) {libxl_get_event (ctx, &event);switch (event.type) {case LIBXL_EVENT_TYPE_DOMAIN_DEATH :
@@ -1687,6 +1706,10 @@ start:break ; }libxl_free_event (&event); + } +if (FD_ISSET (qmp_fd, &rfds)) { +libxl_qmp_do_next (qmp_handler); + } } error_out:
@@ -1697,6 +1720,10 @@ error_out: out:if (logfile !=2 )close (logfile); +if (qmp_handler) { +libxl_qmp_close (qmp_handler); + qmp_handler =NULL ; + }libxl_domain_config_destroy (&d_config);
Signature · 1 line
1.7.2.5
Quoted 5 lines · Anthony Perard
From: Anthony PERARD <[email protected]>
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Quoted 8 lines
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 <[email protected]>
I didn't yet review this in detail but I have a few initial questions/thoughts.
Am I correct that QMP is intended to provide a stable interface going forwards? Or are we tying ourselves to a particular qemu version and/or will we need version specific bits (and associated configuration stuff) in the future?
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW libxl_qmp_initialize should not take a socket option, it should construct the path, do the open internally and return the fd.
Quoted 9 lines
tools/libxl/xl_cmdimpl.c +2
-1- ret =select (fd +1 , &rfds,NULL ,NULL ,NULL ); + ret =select (fd > qmp_fd ? fd +1 : qmp_fd +1 , &rfds,NULL ,NULL ,NULL );if (!ret)continue ; +if (FD_ISSET (fd, &rfds)) {libxl_get_event (ctx, &event);switch (event.type) {case LIBXL_EVENT_TYPE_DOMAIN_DEATH :
tools/libxl/xl_cmdimpl.c start: · line 1712
Quoted 7 lines
tools/libxl/xl_cmdimpl.c +4
-0@@ -1687,6 +1706,10 @@ start:break ; }libxl_free_event (&event); + } +if (FD_ISSET (qmp_fd, &rfds)) { +libxl_qmp_do_next (qmp_handler); + }
Looks like some re-indentation is needed in these two hunks?
tools/libxl/xl_cmdimpl.c error_out: · line 1726
Quoted 11 lines
tools/libxl/xl_cmdimpl.c +4
-0} error_out:@@ -1697,6 +1720,10 @@ error_out: out:if (logfile !=2 )close (logfile); +if (qmp_handler) { +libxl_qmp_close (qmp_handler); + qmp_handler =NULL ; + }
Ian.
Quoted 15 lines · Ian Campbell
Quoted 5 lines · Anthony Perard
From: Anthony PERARD <[email protected]>
QMP stands for QEMU Monitor Protocol and it is used to query information from QEMU or to control QEMU.
Quoted 8 lines
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 <[email protected]>
Quoted 7 lines
I didn't yet review this in detail but I have a few initial questions/thoughts.
Am I correct that QMP is intended to provide a stable interface going forwards? Or are we tying ourselves to a particular qemu version and/or will we need version specific bits (and associated configuration stuff) in the future?
QMP is intended to provide a stable interface. Once a command have been shipped, it should not been modified.
Quoted 9 lines
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.
OK, I will do that, with a #define of the path somewhere.
Quoted 10 lines
Quoted 10 lines
- ret =
select(fd + 1, &rfds, NULL, NULL, NULL);
+ ret =select(fd > qmp_fd ? fd + 1 : qmp_fd + 1, &rfds, NULL, NULL, NULL); if (!ret)
continue;
+ if (FD_ISSET(fd, &rfds)) {
libxl_get_event(ctx, &event);
switch (event.type) {
caseLIBXL_EVENT_TYPE_DOMAIN_DEATH:(quoted diff) +0
-0@@ -1687,6 +1706,10 @@ start: break;
Quoted 8 lines
tools/libxl/xl_cmdimpl.c start: · line 1712
Quoted 6 lines
tools/libxl/xl_cmdimpl.c +4
-0} libxl_free_event(&event); + } + if (FD_ISSET(qmp_fd, &rfds)) { + libxl_qmp_do_next(qmp_handler); + }Looks like some re-indentation is needed in these two hunks?
Oops, indeed, I forget to change that.
Thanks,
Signature · 1 line
Anthony PERARD
Quoted 9 lines · Ian Campbell
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.
I agree on this.
Libxl needs to use QMP internally for things like the serial. Libxl cannot rely on the caller (xl) to select on the fd and call libxl_qmp_do_next later for libxl to put the appropriate serial device on xenstore.
Ideally QMP should be completely hidden inside libxl.
I think all the initialization details should be handled internally by libxl_domain_create_new, including opening the QMP connection and reading back the serial device.
After that libxl should probably expose a single event driven mechanism, both for xenstore and QMP, with some opaque callbacks in the QMP case. However this could be done together with the libxl events refactoring that Ian wants to do.
Quoted 19 lines · Stefano Stabellini
Quoted 9 lines · Ian Campbell
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.I agree on this.
Libxl needs to use QMP internally for things like the serial. Libxl cannot rely on the caller (xl) to select on the fd and call
libxl_qmp_do_nextlater for libxl to put the appropriate serial device on xenstore.
Ideally QMP should be completely hidden inside libxl.
Quoted 3 lines
I think all the initialization details should be handled internally by
libxl_domain_create_new, including opening the QMP connection and reading back the serial device.
Yes I think it would be better to have libxl open a short lived QMP channel for specific operations entirely internally (including closing it again).
If we don't do this then we need to be mindful of multithreaded users of the library multiplexing over a single channel and all the inherent complexity of matching replies to requests, blocking the caller threads, handling async notifications while a request is in progress etc etc.
Probably we will also need a long-running channel dedicated to feeding out into the user's event loop to handle async notifications etc.
How does QMP handle the async notifications with multiple connected clients? I suppose they must all see them (or else writing a client would be virtually impossible), in which case the function-specific connections can simply discard them.
Quoted 4 lines
After that libxl should probably expose a single event driven mechanism, both for xenstore and QMP, with some opaque callbacks in the QMP case. However this could be done together with the libxl events refactoring that Ian wants to do.
Quoted 20 lines · Ian Campbell
Quoted 19 lines · Stefano Stabellini
Quoted 9 lines · Ian Campbell
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.I agree on this.
Libxl needs to use QMP internally for things like the serial. Libxl cannot rely on the caller (xl) to select on the fd and call
libxl_qmp_do_nextlater for libxl to put the appropriate serial device on xenstore.
Ideally QMP should be completely hidden inside libxl.
Quoted 7 lines
Quoted 3 lines
I think all the initialization details should be handled internally by
libxl_domain_create_new, including opening the QMP connection and reading back the serial device.Yes I think it would be better to have libxl open a short lived QMP channel for specific operations entirely internally (including closing it again).
Ok, I will change that.
Quoted 5 lines
If we don't do this then we need to be mindful of multithreaded users of the library multiplexing over a single channel and all the inherent complexity of matching replies to requests, blocking the caller threads, handling async notifications while a request is in progress etc etc.
Quoted 7 lines
Probably we will also need a long-running channel dedicated to feeding out into the user's event loop to handle async notifications etc.
How does QMP handle the async notifications with multiple connected clients? I suppose they must all see them (or else writing a client would be virtually impossible), in which case the function-specific connections can simply discard them.
Actually, QEMU doesn't seem to handle more than one client at a time with a single socket. For more client, we can always open more than one QMP server with different path/port. In this case, they will be handle separately by QEMU.
To handle the async request, it's relatively easy. When we send a command, we can add an "id" to this request and the id will be part of the answer. I use that to handle the replies.
For the QEMU event/async notification, indeed, all clients receive them.
Signature · 1 line
Anthony PERARD
Quoted 49 lines · Anthony PERARD
Quoted 20 lines · Ian Campbell
Quoted 19 lines · Stefano Stabellini
Quoted 9 lines · Ian Campbell
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.I agree on this.
Libxl needs to use QMP internally for things like the serial. Libxl cannot rely on the caller (xl) to select on the fd and call
libxl_qmp_do_nextlater for libxl to put the appropriate serial device on xenstore.
Ideally QMP should be completely hidden inside libxl.Quoted 7 lines
Quoted 3 lines
I think all the initialization details should be handled internally by
libxl_domain_create_new, including opening the QMP connection and reading back the serial device.Yes I think it would be better to have libxl open a short lived QMP channel for specific operations entirely internally (including closing it again).
Ok, I will change that.
Quoted 5 lines
If we don't do this then we need to be mindful of multithreaded users of the library multiplexing over a single channel and all the inherent complexity of matching replies to requests, blocking the caller threads, handling async notifications while a request is in progress etc etc.
Quoted 7 lines
Probably we will also need a long-running channel dedicated to feeding out into the user's event loop to handle async notifications etc.
How does QMP handle the async notifications with multiple connected clients? I suppose they must all see them (or else writing a client would be virtually impossible), in which case the function-specific connections can simply discard them.
Actually, QEMU doesn't seem to handle more than one client at a time with a single socket. For more client, we can always open more than one QMP server with different path/port. In this case, they will be handle separately by QEMU.
Quoted 5 lines
To handle the async request, it's relatively easy. When we send a command, we can add an "id" to this request and the id will be part of the answer. I use that to handle the replies.
For the QEMU event/async notification, indeed, all clients receive them.
That means that if we provide a single QMP server socket we cannot expose QMP to xl at all, because we have to keep the server socket free for libxl to use whenever the library sees fit.
Quoted 39 lines · Anthony PERARD
Quoted 20 lines · Ian Campbell
Quoted 19 lines · Stefano Stabellini
Quoted 9 lines · Ian Campbell
I think we should try where possible to keep this stuff entirely within libxl. The existing libxl event API is a bit of a mess but I think if it were cleaned up (IanJ has a plan I think) then it would be the right place to integrate the libxl and caller event loops.
For the time being though I think libxl should provide the fd and not expect the caller to construct the path and open it etc. IOW
libxl_qmp_initializeshould not take a socket option, it should construct the path, do the open internally and return the fd.I agree on this.
Libxl needs to use QMP internally for things like the serial. Libxl cannot rely on the caller (xl) to select on the fd and call
libxl_qmp_do_nextlater for libxl to put the appropriate serial device on xenstore.
Ideally QMP should be completely hidden inside libxl.Quoted 7 lines
Quoted 3 lines
I think all the initialization details should be handled internally by
libxl_domain_create_new, including opening the QMP connection and reading back the serial device.Yes I think it would be better to have libxl open a short lived QMP channel for specific operations entirely internally (including closing it again).
Ok, I will change that.
Quoted 8 lines
If we don't do this then we need to be mindful of multithreaded users of the library multiplexing over a single channel and all the inherent complexity of matching replies to requests, blocking the caller threads, handling async notifications while a request is in progress etc etc.
Probably we will also need a long-running channel dedicated to feeding out into the user's event loop to handle async notifications etc.
Quoted 7 lines
Quoted 4 lines
How does QMP handle the async notifications with multiple connected clients? I suppose they must all see them (or else writing a client would be virtually impossible), in which case the function-specific connections can simply discard them.
Actually, QEMU doesn't seem to handle more than one client at a time with a single socket.
That seems like a pretty obvious short coming, do you know if it is a deliberate policy or just a case of not implemented yet?
Quoted 3 lines
For more client, we can always open more than
one QMP server with different path/port. In this case, they will be handle separately by QEMU.
Problem is determining the correct number to create when we start qemu.
Quoted 3 lines
To handle the async request, it's relatively easy. When we send a command, we can add an "id" to this request and the id will be part of the answer. I use that to handle the replies.
Most of the uses of QMP (at least within libxl) are likely to want to be synchronous though, e.g. libxl functions usually do not return to the user in a semi completed state and require a callback for completion.
This means that we need a means to arrange to wait for a specific response, which in a multithreaded user may mean blocking threads until their response comes in and waking them etc, ensuring that there is one (and only one) thread actually handling the incoming requests to do the dispatch/handle async notifications etc. It's not the end of the world but it does add complexity within the library and a dependency on a threading library which IIRC we currently manage to avoid...
Quoted 2 lines
For the QEMU event/async notification, indeed, all clients receive them.
Quoted 5 lines · Ian Campbell
Quoted 2 lines
Actually, QEMU doesn't seem to handle more than one client at a time with a single socket.
That seems like a pretty obvious short coming, do you know if it is a deliberate policy or just a case of not implemented yet?
I think it because they use the same "char device" code to handle by example the serial port or the monitor (including QMP). So when a client is connected, there just stop to handle the listenning fd until the client disconnects itself.
Quoted 5 lines
Quoted 3 lines
For more client, we can always open more than
one QMP server with different path/port. In this case, they will be handle separately by QEMU.Problem is determining the correct number to create when we start qemu.
Well, a simple answer would be two, one to listen the event from QEMU, if we need to do that, and the second one would be for the commands, one at a time, and libxl close the socket. If a command socket is already in use, then another libxl client whose trying to connect to the same socket will wait until QEMU accept the connection.
Signature · 1 line
Anthony PERARD
Quoted 16 lines · Anthony PERARD
Quoted 5 lines · Ian Campbell
Quoted 2 lines
Actually, QEMU doesn't seem to handle more than one client at a time with a single socket.
That seems like a pretty obvious short coming, do you know if it is a deliberate policy or just a case of not implemented yet?
I think it because they use the same "char device" code to handle by example the serial port or the monitor (including QMP). So when a client is connected, there just stop to handle the listenning fd until the client disconnects itself.
Quoted 4 lines
Quoted 3 lines
For more client, we can always open more than
one QMP server with different path/port. In this case, they will be handle separately by QEMU.
Quoted 7 lines
Quoted 1 line
Problem is determining the correct number to create when we start qemu.
Well, a simple answer would be two, one to listen the event from QEMU, if we need to do that, and the second one would be for the commands, one at a time, and libxl close the socket. If a command socket is already in use, then another libxl client whose trying to connect to the same socket will wait until QEMU accept the connection.
Yeah, we could start by implementing only the "libxl commands" socket, leaving out the event socket for later.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel