From patchwork Tue Sep 6 10:51:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 9316367 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5BE57601C0 for ; Tue, 6 Sep 2016 10:53:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E88B28C78 for ; Tue, 6 Sep 2016 10:53:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 434CD28C7B; Tue, 6 Sep 2016 10:53:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6D80E28C78 for ; Tue, 6 Sep 2016 10:53:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bhDyD-0002st-It; Tue, 06 Sep 2016 10:51:13 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bhDyC-0002sk-DJ for xen-devel@lists.xen.org; Tue, 06 Sep 2016 10:51:12 +0000 Received: from [85.158.137.68] by server-4.bemta-3.messagelabs.com id E8/D2-15788-F9F9EC75; Tue, 06 Sep 2016 10:51:11 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrHLMWRWlGSWpSXmKPExsVyuP0Ov+68+ef CDRY+MrRY8nExiwOjx9Hdv5kCGKNYM/OS8isSWDOm7F3AXrDXueLgjR1MDYx3TbsYOTkkBIwk 3k78x9TFyMUhJLCQUWJx23smkASbgKrEhuunWEFsEQFpiWufLzOC2MwCoRIn391jBrGFBSwkv j9ZAxZnAapfeHwRO4jNK2Av8XhxJxvEAjmJ6zOnM01g5FzAyLCKUaM4tagstUjX0FgvqSgzPa MkNzEzR9fQwFgvN7W4ODE9NScxqVgvOT93EyPQXwxAsINx23bPQ4ySHExKorxqgefChfiS8lM qMxKLM+KLSnNSiw8xynBwKEnwTpoHlBMsSk1PrUjLzAEGDkxagoNHSYTXbS5Qmre4IDG3ODMd InWKUVFKnDcKpE8AJJFRmgfXBgvWS4yyUsK8jECHCPEUpBblZpagyr9iFOdgVBLmrQSZwpOZV wI3/RXQYiagxet2nwZZXJKIkJJqYIyd//FCw/+fn+oesXSqXV7Jnv/h2/f0FStXte4qD/x1nv 8zI9cez4BHZ5TrRSbcWHxPJNMttOG1r6bt0fSMd78qZR6f3a+R/7XAZcITT92QdIfoD4oeUQq XhfOF7krG//I7/rbk8cxFsZ2Fe6UjnO5FffOuX7aS73bY4/JLXz/+TTmmbB8w+ZQSS3FGoqEW c1FxIgBkXztbUQIAAA== X-Env-Sender: jgross@suse.com X-Msg-Ref: server-3.tower-31.messagelabs.com!1473159070!59177541!1 X-Originating-IP: [195.135.220.15] X-SpamReason: No, hits=0.5 required=7.0 tests=BODY_RANDOM_LONG X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 39458 invoked from network); 6 Sep 2016 10:51:10 -0000 Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by server-3.tower-31.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 6 Sep 2016 10:51:10 -0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 59128ABE8; Tue, 6 Sep 2016 10:51:09 +0000 (UTC) From: Juergen Gross To: xen-devel@lists.xen.org Date: Tue, 6 Sep 2016 12:51:06 +0200 Message-Id: <1473159066-26227-1-git-send-email-jgross@suse.com> X-Mailer: git-send-email 2.6.6 Cc: Juergen Gross , wei.liu2@citrix.com, ian.jackson@eu.citrix.com Subject: [Xen-devel] [PATCH v3] libxl: add "xl qemu-monitor-command" X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a new xl command "qemu-monitor-command" to issue arbitrary commands to a domain's device model. Syntax is: xl qemu-monitor-command The command is issued via qmp human-monitor-command command. Any information returned by the command is printed to stdout. Signed-off-by: Juergen Gross Reviewed-by: Dario Faggioli Acked-by: Wei Liu --- V3: - add GC_FREE as requested by Dario Faggioli - return with EXIT_FAILURE/SUCCESS as requested by Dario Faggioli - don't initialize rc statically as requested by Dario Faggioli V2: - add warning to man page as requested by Ian Jackson --- docs/man/xl.pod.1.in | 36 ++++++++++++++++++++++++++++++++++ tools/libxl/libxl.h | 14 ++++++++++++++ tools/libxl/libxl_colo_qdisk.c | 2 +- tools/libxl/libxl_internal.h | 3 ++- tools/libxl/libxl_qmp.c | 44 ++++++++++++++++++++++++++++++++++++++++-- tools/libxl/xl.h | 1 + tools/libxl/xl_cmdimpl.c | 29 ++++++++++++++++++++++++++++ tools/libxl/xl_cmdtable.c | 5 +++++ 8 files changed, 130 insertions(+), 4 deletions(-) diff --git a/docs/man/xl.pod.1.in b/docs/man/xl.pod.1.in index 1adf322..a2be541 100644 --- a/docs/man/xl.pod.1.in +++ b/docs/man/xl.pod.1.in @@ -1516,6 +1516,42 @@ List pass-through usb devices for a domain. =back +=head1 DEVICE-MODEL CONTROL + +=over 4 + +=item B I I + +Issue a monitor command to the device model of the domain specified by +I. I can be any valid command qemu understands. This +can be e.g. used to add non-standard devices or devices with non-standard +parameters to a domain. The output of the command is printed to stdout. + +B This qemu monitor access is provided for convenience when +debugging, troubleshooting, and experimenting. Its use is not +supported by the Xen Project. + +Specifically, not all information printed by the qemu monitor will +necessarily be accurate or complete, because in a Xen system qemu +does not have a complete view of the guest. + +Furthermore, modifying the guest's setup via the qemu monitor may +conflict with the Xen toolstack's assumptions. Resulting problems +may include, but are not limited to: guest crashes; toolstack error +messages; inability to migrate the guest; and security +vulnerabilities which are not covered by the Xen Project security +response policy. + +B + +Obtain information of USB devices connected as such via the device model +(only!) to a domain: + + xl qemu-monitor-command vm1 'info usb' + Device 0.2, Port 5, Speed 480 Mb/s, Product Mass Storage + +=back + =head1 TMEM =over 4 diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index e4c25c4..7cfa540 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -275,6 +275,12 @@ #define LIBXL_HAVE_BUILD_ID 1 /* + * LIBXL_HAVE_QEMU_MONITOR_COMMAND indiactes the availability of the + * libxl_qemu_monitor_command() function. + */ +#define LIBXL_HAVE_QEMU_MONITOR_COMMAND 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -2152,6 +2158,14 @@ void libxl_psr_cat_info_list_free(libxl_psr_cat_info *list, int nr); int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec); int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock); +/* + * Issue a qmp monitor command to the device model of the specified domain. + * The function returns the output of the command in a new allocated buffer + * via output. + */ +int libxl_qemu_monitor_command(libxl_ctx *ctx, uint32_t domid, + const char *command_line, char **output); + #include #endif /* LIBXL_H */ diff --git a/tools/libxl/libxl_colo_qdisk.c b/tools/libxl/libxl_colo_qdisk.c index c23b81b..d271d1f 100644 --- a/tools/libxl/libxl_colo_qdisk.c +++ b/tools/libxl/libxl_colo_qdisk.c @@ -173,7 +173,7 @@ static void colo_qdisk_save_preresume(libxl__egc *egc, "file.driver=nbd,file.host=%s,file.port=%d," "file.export=%s,node-name=%s,if=none", host, port, export_name, node); - ret = libxl__qmp_hmp(gc, domid, cmd); + ret = libxl__qmp_hmp(gc, domid, cmd, NULL); if (ret) rc = ERROR_FAIL; diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index ce8e17a..9f534c4 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -1818,7 +1818,8 @@ _hidden int libxl__qmp_x_blockdev_change(libxl__gc *gc, int domid, const char *parant, const char *child, const char *node); /* run a hmp command in qmp mode */ -_hidden int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line); +_hidden int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line, + char **out); /* close and free the QMP handler */ _hidden void libxl__qmp_close(libxl__qmp_handler *qmp); /* remove the socket file, if the file has already been removed, diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c index 63c49c5..0d8d5f4 100644 --- a/tools/libxl/libxl_qmp.c +++ b/tools/libxl/libxl_qmp.c @@ -1103,14 +1103,54 @@ int libxl__qmp_x_blockdev_change(libxl__gc *gc, int domid, const char *parent, return qmp_run_command(gc, domid, "x-blockdev-change", args, NULL, NULL); } -int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line) +static int hmp_callback(libxl__qmp_handler *qmp, + const libxl__json_object *response, + void *opaque) +{ + char **output = opaque; + GC_INIT(qmp->ctx); + int rc; + + rc = 0; + if (!output) + goto out; + + *output = NULL; + + if (libxl__json_object_is_string(response)) { + *output = libxl__strdup(NOGC, libxl__json_object_get_string(response)); + goto out; + } + + LOG(ERROR, "Response has unexpected format"); + rc = ERROR_FAIL; + +out: + GC_FREE; + return rc; +} + +int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line, + char **output) { libxl__json_object *args = NULL; qmp_parameters_add_string(gc, &args, "command-line", command_line); return qmp_run_command(gc, domid, "human-monitor-command", args, - NULL, NULL); + hmp_callback, output); +} + +int libxl_qemu_monitor_command(libxl_ctx *ctx, uint32_t domid, + const char *command_line, char **output) +{ + GC_INIT(ctx); + int rc; + + rc = libxl__qmp_hmp(gc, domid, command_line, output); + + GC_FREE; + return rc; } int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid, diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h index e601ca1..0a8c813 100644 --- a/tools/libxl/xl.h +++ b/tools/libxl/xl.h @@ -133,6 +133,7 @@ int main_psr_cmt_show(int argc, char **argv); int main_psr_cat_cbm_set(int argc, char **argv); int main_psr_cat_show(int argc, char **argv); #endif +int main_qemu_monitor_command(int argc, char **argv); void help(const char *command); diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 7540fb1..d1a2a14 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -9536,6 +9536,35 @@ int main_psr_hwinfo(int argc, char **argv) #endif +int main_qemu_monitor_command(int argc, char **argv) +{ + int opt; + uint32_t domid; + char *cmd; + char *output; + int ret; + + SWITCH_FOREACH_OPT(opt, "", NULL, "qemu-monitor-command", 2) { + /* No options */ + } + + domid = find_domain(argv[optind]); + cmd = argv[optind + 1]; + + if (argc - optind > 2) { + fprintf(stderr, "Invalid arguments.\n"); + return 1; + } + + ret = libxl_qemu_monitor_command(ctx, domid, cmd, &output); + if (!ret && output) { + printf("%s\n", output); + free(output); + } + + return ret ? EXIT_FAILURE : EXIT_SUCCESS; +} + /* * Local variables: * mode: C diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c index 85c1e0f..78786fe 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -584,6 +584,11 @@ struct cmd_spec cmd_table[] = { "List information about all USB controllers and devices for a domain", "", }, + { "qemu-monitor-command", + &main_qemu_monitor_command, 0, 1, + "Issue a qemu monitor command to the device model of a domain", + " ", + }, }; int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);