From patchwork Fri Jan 22 19:03:51 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adam Litke X-Patchwork-Id: 43501 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4D971B7CB9 for ; Sat, 23 Jan 2010 06:08:53 +1100 (EST) Received: from localhost ([127.0.0.1]:54338 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NYOqW-0001zm-2f for incoming@patchwork.ozlabs.org; Fri, 22 Jan 2010 14:07:04 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NYOne-0001FE-7o for qemu-devel@nongnu.org; Fri, 22 Jan 2010 14:04:06 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NYOnZ-0001D2-IT for qemu-devel@nongnu.org; Fri, 22 Jan 2010 14:04:05 -0500 Received: from [199.232.76.173] (port=55562 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NYOnZ-0001Cy-9W for qemu-devel@nongnu.org; Fri, 22 Jan 2010 14:04:01 -0500 Received: from mx20.gnu.org ([199.232.41.8]:26603) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1NYOnY-0000wE-Ml for qemu-devel@nongnu.org; Fri, 22 Jan 2010 14:04:00 -0500 Received: from e2.ny.us.ibm.com ([32.97.182.142]) by mx20.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NYOnX-0000AJ-Dj for qemu-devel@nongnu.org; Fri, 22 Jan 2010 14:03:59 -0500 Received: from d01relay03.pok.ibm.com (d01relay03.pok.ibm.com [9.56.227.235]) by e2.ny.us.ibm.com (8.14.3/8.13.1) with ESMTP id o0MIsTpY023130 for ; Fri, 22 Jan 2010 13:54:29 -0500 Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d01relay03.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o0MJ3uLE122784 for ; Fri, 22 Jan 2010 14:03:57 -0500 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVout) with ESMTP id o0MJ3rWa010492 for ; Fri, 22 Jan 2010 12:03:53 -0700 Received: from [9.10.86.208] (aglitke.rchland.ibm.com [9.10.86.208]) by d03av01.boulder.ibm.com (8.14.3/8.13.1/NCO v10.0 AVin) with ESMTP id o0MJ3q9i010453; Fri, 22 Jan 2010 12:03:52 -0700 From: Adam Litke To: Anthony Liguori Organization: IBM Date: Fri, 22 Jan 2010 13:03:51 -0600 Message-ID: <1264187031.2861.13.camel@aglitke> Mime-Version: 1.0 X-Mailer: Evolution 2.28.1 X-detected-operating-system: by mx20.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: qemu-devel@nongnu.org, Luiz Capitulino Subject: [Qemu-devel] [RFC] New API for asynchronous monitor commands X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Qemu has a number of commands that can operate asynchronously (savevm, migrate, etc) and it will be getting more. For these commands, the user monitor needs to be suspended, but QMP monitors could continue to to accept other commands. This patch introduces a new command API that isolates the details of handling different monitor types from the actual command execution. A monitor command can use this API by implementing the mhandler.cmd_async handler (or info_async if appropriate). This function is responsible for submitting the command and does not return any data although it may raise errors. When the command completes, the QMPCompletion callback should be invoked with its opaque data and the command result. The process for submitting and completing an asynchronous command is different for QMP and user monitors. A user monitor must be suspended at submit time and resumed at completion time. The user_print() function must be passed to the QMPCompletion callback so the result can be displayed properly. QMP monitors are simpler. No submit time setup is required. When the command completes, monitor_protocol_emitter() writes the result in JSON format. This API can also be used to implement synchronous commands. In this case, the cmd_async handler should immediately call the QMPCompletion callback. It is my hope that this new interface will work for all commands, leading to a drastically simplified monitor.c once all commands are ported. Thanks to Anthony for helping me out with the initial design. Signed-off-by: Adam Litke To: Anthony Liguori cc: Luiz Capitulino Cc: qemu-devel@nongnu.org diff --git a/monitor.c b/monitor.c index cadf422..c0d0fa9 100644 --- a/monitor.c +++ b/monitor.c @@ -76,6 +76,12 @@ * */ +typedef struct UserQMPCompletionData UserQMPCompletionData; +struct UserQMPCompletionData { + Monitor *mon; + void (*user_print)(Monitor *mon, const QObject *data); +}; + typedef struct mon_cmd_t { const char *name; const char *args_type; @@ -85,11 +91,19 @@ typedef struct mon_cmd_t { union { void (*info)(Monitor *mon); void (*info_new)(Monitor *mon, QObject **ret_data); + int (*info_async)(Monitor *mon, QMPCompletion *cb, void *opaque); void (*cmd)(Monitor *mon, const QDict *qdict); void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data); + int (*cmd_async)(Monitor *mon, const QDict *params, + QMPCompletion *cb, void *opaque); } mhandler; + int async; } mon_cmd_t; +static void do_async_info_handler(Monitor *mon, const mon_cmd_t *cmd); +static void do_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, + const QDict *params); + /* file descriptors passed via SCM_RIGHTS */ typedef struct mon_fd_t mon_fd_t; struct mon_fd_t { @@ -255,6 +269,11 @@ static inline int monitor_handler_ported(const mon_cmd_t *cmd) return cmd->user_print != NULL; } +static inline bool monitor_handler_is_async(const mon_cmd_t *cmd) +{ + return cmd->async != 0; +} + static inline int monitor_has_error(const Monitor *mon) { return mon->error != NULL; @@ -453,6 +472,23 @@ static void do_commit(Monitor *mon, const QDict *qdict) } } +static void user_monitor_complete(void *opaque, QObject *ret_data) +{ + UserQMPCompletionData *data = (UserQMPCompletionData *)opaque; + + if (ret_data) { + data->user_print(data->mon, ret_data); + } + monitor_resume(data->mon); + qemu_free(data); +} + +static void qmp_monitor_complete(void *opaque, QObject *ret_data) +{ + Monitor *mon = (Monitor *)opaque; + monitor_protocol_emitter(mon, ret_data); +} + static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data) { const mon_cmd_t *cmd; @@ -476,7 +512,15 @@ static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data) goto help; } - if (monitor_handler_ported(cmd)) { + if (monitor_handler_is_async(cmd)) { + do_async_info_handler(mon, cmd); + /* + * Indicate that this command is asynchronous and will not return any + * date (not even empty). Instead, the data will be returned via a + * completion callback. + */ + *ret_data = qobject_from_jsonf("{ 'async': 'return' }"); + } else if (monitor_handler_ported(cmd)) { cmd->mhandler.info_new(mon, ret_data); if (!monitor_ctrl_mode(mon)) { @@ -3612,6 +3656,11 @@ static void monitor_print_error(Monitor *mon) mon->error = NULL; } +static int is_async_return(const QObject *data) +{ + return data && qdict_haskey(qobject_to_qdict(data), "async"); +} + static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd, const QDict *params) { @@ -3619,7 +3668,15 @@ static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd, cmd->mhandler.cmd_new(mon, params, &data); - if (monitor_ctrl_mode(mon)) { + if (is_async_return(data)) { + /* + * Asynchronous commands have no initial return data but they can + * generate errors. Data is returned via the async completion handler. + */ + if (monitor_ctrl_mode(mon) && monitor_has_error(mon)) { + monitor_protocol_emitter(mon, NULL); + } + } else if (monitor_ctrl_mode(mon)) { /* Monitor Protocol */ monitor_protocol_emitter(mon, data); } else { @@ -3631,6 +3688,46 @@ static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd, qobject_decref(data); } +static void do_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd, + const QDict *params) +{ + if (monitor_ctrl_mode(mon)) { + cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon); + } else { + int ret; + + UserQMPCompletionData *cb_data = qemu_malloc(sizeof(*cb_data)); + cb_data->mon = mon; + cb_data->user_print = cmd->user_print; + monitor_suspend(mon); + ret = cmd->mhandler.cmd_async(mon, params, + user_monitor_complete, cb_data); + if (ret < 0) { + monitor_resume(mon); + qemu_free(cb_data); + } + } +} + +static void do_async_info_handler(Monitor *mon, const mon_cmd_t *cmd) +{ + if (monitor_ctrl_mode(mon)) { + cmd->mhandler.info_async(mon, qmp_monitor_complete, mon); + } else { + int ret; + + UserQMPCompletionData *cb_data = qemu_malloc(sizeof(*cb_data)); + cb_data->mon = mon; + cb_data->user_print = cmd->user_print; + monitor_suspend(mon); + ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data); + if (ret < 0) { + monitor_resume(mon); + qemu_free(cb_data); + } + } +} + static void handle_user_command(Monitor *mon, const char *cmdline) { QDict *qdict; @@ -3644,7 +3741,9 @@ static void handle_user_command(Monitor *mon, const char *cmdline) qemu_errors_to_mon(mon); - if (monitor_handler_ported(cmd)) { + if (monitor_handler_is_async(cmd)) { + do_async_cmd_handler(mon, cmd, qdict); + } else if (monitor_handler_ported(cmd)) { monitor_call_handler(mon, cmd, qdict); } else { cmd->mhandler.cmd(mon, qdict); @@ -4106,7 +4205,11 @@ static void handle_qmp_command(JSONMessageParser *parser, QList *tokens) goto err_out; } - monitor_call_handler(mon, cmd, args); + if (monitor_handler_is_async(cmd)) { + do_async_cmd_handler(mon, cmd, args); + } else { + monitor_call_handler(mon, cmd, args); + } goto out; err_input: diff --git a/monitor.h b/monitor.h index 2da30e8..366b423 100644 --- a/monitor.h +++ b/monitor.h @@ -44,4 +44,6 @@ void monitor_printf(Monitor *mon, const char *fmt, ...) void monitor_print_filename(Monitor *mon, const char *filename); void monitor_flush(Monitor *mon); +typedef void (QMPCompletion)(void *opaque, QObject *ret_data); + #endif /* !MONITOR_H */