[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH v5 02/15] libxl_qmp: Connect to QMP socket
This is a first patch to implement libxl__ev_qmp, it only connects to the QMP socket of QEMU and registers a fd callback that does nothing. Callback functions will be implemented in following patches. Signed-off-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> --- Notes: v5: nits use a define instead of a static int for QMP_CAPABILITY_NEGOCIATION_MSGID use a switch in qmp_ev_callback_writable to check qmp_state Add a description of the different value of libxl__qmp_state enum. tools/libxl/libxl_internal.h | 16 +++++++ tools/libxl/libxl_qmp.c | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 979a9947f0..9bcab39f00 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -414,6 +414,19 @@ _hidden int libxl__ev_qmp_send(libxl__gc *gc, libxl__ev_qmp *ev, const char *cmd, libxl__json_object *args); _hidden void libxl__ev_qmp_dispose(libxl__gc *gc, libxl__ev_qmp *ev); +typedef enum { + /* initial state */ + qmp_state_disconnected = 1, + /* connected to QMP socket, waiting for greeting message */ + qmp_state_connecting, + /* greeting message received */ + qmp_state_greeting, + /* qmp_capabilities command sent, waiting for reply */ + qmp_state_capability_negotiation, + /* ready to send commands */ + qmp_state_connected, +} libxl__qmp_state; + struct libxl__ev_qmp { /* caller should include this in their own struct */ /* caller must fill these in, and they must all remain valid */ @@ -423,6 +436,9 @@ struct libxl__ev_qmp { /* remaining fields are private to libxl_ev_qmp_* */ int id; + libxl__carefd *qmp_cfd; + libxl__ev_fd qmp_efd; + libxl__qmp_state qmp_state; }; diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c index bdf1778cf1..167829a5ec 100644 --- a/tools/libxl/libxl_qmp.c +++ b/tools/libxl/libxl_qmp.c @@ -1280,6 +1280,97 @@ int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid, return ret; } +/* ------------ Implementation of libxl__ev_qmp ---------------- */ + +static void qmp_ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev_fd, + int fd, short events, short revents) +{ +} + +static int qmp_ev_connect(libxl__gc *gc, libxl__ev_qmp *ev) +{ + int rc, r; + struct sockaddr_un un; + const char *qmp_socket_path; + + if (ev->qmp_state != qmp_state_disconnected) + return 0; + + qmp_socket_path = libxl__qemu_qmp_path(gc, ev->domid); + + LOGD(DEBUG, ev->domid, "Connecting to %s", qmp_socket_path); + + libxl__carefd_begin(); + ev->qmp_cfd = libxl__carefd_opened(CTX, socket(AF_UNIX, SOCK_STREAM, 0)); + if (!ev->qmp_cfd) { + LOGED(ERROR, ev->domid, "socket() failed"); + rc = ERROR_FAIL; + goto out; + } + rc = libxl_fd_set_nonblock(CTX, libxl__carefd_fd(ev->qmp_cfd), 1); + if (rc) + goto out; + + rc = libxl__prepare_sockaddr_un(gc, &un, qmp_socket_path, "QMP socket"); + if (rc) + goto out; + + r = connect(libxl__carefd_fd(ev->qmp_cfd), + (struct sockaddr *) &un, sizeof(un)); + if (r) { + LOGED(ERROR, ev->domid, "Failed to connect to QMP socket %s", + qmp_socket_path); + rc = ERROR_FAIL; + goto out; + } + + rc = libxl__ev_fd_register(gc, &ev->qmp_efd, qmp_ev_fd_callback, + libxl__carefd_fd(ev->qmp_cfd), POLLIN); + if (rc) + goto out; + + ev->qmp_state = qmp_state_connecting; + return 0; + +out: + libxl__carefd_close(ev->qmp_cfd); + ev->qmp_cfd = NULL; + return rc; +} + + +/* + * libxl__ev_qmp_* + */ + +void libxl__ev_qmp_init(libxl__ev_qmp *ev) +{ + ev->id = -1; + + ev->qmp_cfd = NULL; + libxl__ev_fd_init(&ev->qmp_efd); + ev->qmp_state = qmp_state_disconnected; +} + +int libxl__ev_qmp_send(libxl__gc *gc, libxl__ev_qmp *ev, + const char *cmd, libxl__json_object *args) +{ + LOGD(DEBUG, ev->domid, " ev %p, cmd '%s'", ev, cmd); + + /* Connect to QEMU if not already connected */ + return qmp_ev_connect(gc, ev); +} + +void libxl__ev_qmp_dispose(libxl__gc *gc, libxl__ev_qmp *ev) +{ + LOGD(DEBUG, ev->domid, " ev %p", ev); + + libxl__ev_fd_deregister(gc, &ev->qmp_efd); + libxl__carefd_close(ev->qmp_cfd); + + libxl__ev_qmp_init(ev); +} + /* * Local variables: * mode: C -- Anthony PERARD _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxxx https://lists.xenproject.org/mailman/listinfo/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |