Patchwork [2/3] QMP: Introduce Human Monitor passthrough command

login
register
mail settings
Submitter Luiz Capitulino
Date Nov. 10, 2010, 6:59 p.m.
Message ID <1289415546-19105-3-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/70678/
State New
Headers show

Comments

Luiz Capitulino - Nov. 10, 2010, 6:59 p.m.
This command allows QMP clients to execute HMP commands.

Please, check the documentation added to the qmp-commands.hx file
for additional details about the interface and its limitations.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c       |   39 +++++++++++++++++++++++++++++++++++++++
 qmp-commands.hx |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+), 0 deletions(-)
Markus Armbruster - Nov. 11, 2010, 3:47 p.m.
Luiz Capitulino <lcapitulino@redhat.com> writes:

> This command allows QMP clients to execute HMP commands.
>
> Please, check the documentation added to the qmp-commands.hx file
> for additional details about the interface and its limitations.
>
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  monitor.c       |   39 +++++++++++++++++++++++++++++++++++++++
>  qmp-commands.hx |   45 +++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 84 insertions(+), 0 deletions(-)
>
> diff --git a/monitor.c b/monitor.c
> index 8cee35d..89513be 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -491,6 +491,45 @@ static int do_qmp_capabilities(Monitor *mon, const QDict *params,
>      return 0;
>  }
>  
> +static int mon_set_cpu(int cpu_index);
> +static void handle_user_command(Monitor *mon, const char *cmdline);
> +
> +static int do_hmp_passthrough(Monitor *mon, const QDict *params,
> +                              QObject **ret_data)
> +{
> +    int ret = 0;
> +    QString *qs;
> +    Monitor *old_mon, hmp;
> +    CharDriverState memchr;

Uh, let's not shadow memchr() from string.h.

> +
> +    memset(&hmp, 0, sizeof(hmp));
> +    hmp.chr = &memchr;
> +    qemu_chr_init_mem(hmp.chr);
> +
> +    old_mon = cur_mon;
> +    cur_mon = &hmp;
> +
> +    if (qdict_haskey(params, "cpu-index")) {
> +        ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
> +        if (ret < 0) {
> +            qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
> +            goto out;
> +        }
> +    }
> +
> +    handle_user_command(&hmp, qdict_get_str(params, "command-line"));

It would sure be nice if handle_user_command() returned status...  But
fixing that is out of this patch series' scope.

> +
> +    qs = qemu_chr_mem_to_qs(hmp.chr);
> +    if (qs) {
> +        *ret_data = QOBJECT(qs);
> +    }

Conditional goes away when qemu_chr_mem_to_qs() is changed not to return
NULL for empty character device.

> +
> +out:
> +    cur_mon = old_mon;
> +    qemu_chr_close_mem(hmp.chr);
> +    return ret;
> +}
> +
>  static int compare_cmd(const char *name, const char *list)
>  {
>      const char *p, *pstart;
> diff --git a/qmp-commands.hx b/qmp-commands.hx
> index 793cf1c..b344096 100644
> --- a/qmp-commands.hx
> +++ b/qmp-commands.hx
> @@ -761,6 +761,51 @@ Example:
>  
>  Note: This command must be issued before issuing any other command.
>  
> +EQMP
> +
> +    {
> +        .name       = "hmp_passthrough",
> +        .args_type  = "command-line:s,cpu-index:i?",
> +        .params     = "",
> +        .help       = "",
> +        .user_print = monitor_user_noop,
> +        .mhandler.cmd_new = do_hmp_passthrough,
> +    },
> +
> +SQMP
> +hmp_passthrough
> +---------------
> +
> +Execute a Human Monitor command.
> +
> +Arguments: 
> +
> +- command-line: the command name and its arguments, just like the
> +                Human Monitor's shell (json-string)
> +- cpu-index: select the CPU number to be used by commands which access CPU
> +             data, like 'info registers'. The Monitor selects CPU 0 if this
> +             argument is not provided (json-int, optional)
> +
> +Example:
> +
> +-> { "execute": "hmp_passthrough", "arguments": { "command-line": "info kvm" } }
> +<- { "return": "kvm support: enabled\r\n" }
> +
> +Notes:
> +
> +(1) The Human Monitor is NOT an stable interface, this means that command
> +    names, arguments and responses can change or be removed at ANY time.
> +    Applications that rely on long term stability guarantees should NOT
> +    use this command
> +
> +(2) Limitations:
> +
> +    o This command is stateless, this means that commands that depend
> +      on state information (such as getfd) might not work
> +
> +    o Commands that prompt the user for data (eg. 'cont' when the block
> +      device is encrypted) don't currently work
> +
>  3. Query Commands
>  =================
Luiz Capitulino - Nov. 11, 2010, 5:11 p.m.
On Thu, 11 Nov 2010 16:47:52 +0100
Markus Armbruster <armbru@redhat.com> wrote:

> Luiz Capitulino <lcapitulino@redhat.com> writes:
> 
> > This command allows QMP clients to execute HMP commands.
> >
> > Please, check the documentation added to the qmp-commands.hx file
> > for additional details about the interface and its limitations.
> >
> > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > ---
> >  monitor.c       |   39 +++++++++++++++++++++++++++++++++++++++
> >  qmp-commands.hx |   45 +++++++++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 84 insertions(+), 0 deletions(-)
> >
> > diff --git a/monitor.c b/monitor.c
> > index 8cee35d..89513be 100644
> > --- a/monitor.c
> > +++ b/monitor.c
> > @@ -491,6 +491,45 @@ static int do_qmp_capabilities(Monitor *mon, const QDict *params,
> >      return 0;
> >  }
> >  
> > +static int mon_set_cpu(int cpu_index);
> > +static void handle_user_command(Monitor *mon, const char *cmdline);
> > +
> > +static int do_hmp_passthrough(Monitor *mon, const QDict *params,
> > +                              QObject **ret_data)
> > +{
> > +    int ret = 0;
> > +    QString *qs;
> > +    Monitor *old_mon, hmp;
> > +    CharDriverState memchr;
> 
> Uh, let's not shadow memchr() from string.h.

Ah, good catch.

> 
> > +
> > +    memset(&hmp, 0, sizeof(hmp));
> > +    hmp.chr = &memchr;
> > +    qemu_chr_init_mem(hmp.chr);
> > +
> > +    old_mon = cur_mon;
> > +    cur_mon = &hmp;
> > +
> > +    if (qdict_haskey(params, "cpu-index")) {
> > +        ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
> > +        if (ret < 0) {
> > +            qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
> > +            goto out;
> > +        }
> > +    }
> > +
> > +    handle_user_command(&hmp, qdict_get_str(params, "command-line"));
> 
> It would sure be nice if handle_user_command() returned status...  But
> fixing that is out of this patch series' scope.
> 
> > +
> > +    qs = qemu_chr_mem_to_qs(hmp.chr);
> > +    if (qs) {
> > +        *ret_data = QOBJECT(qs);
> > +    }
> 
> Conditional goes away when qemu_chr_mem_to_qs() is changed not to return
> NULL for empty character device.

If we do this, then our success response is going to be "", like:

{ "execute": "execute": "hmp_passthrough", "arguments": { "command-line": "stop" } }
{ "return": "" }

Today, it's:

{ "execute": "execute": "hmp_passthrough", "arguments": { "command-line": "stop" } }
{ "return": {} }

Which is what we should expect.

In any case, it's not a big deal to fix that.

> > +
> > +out:
> > +    cur_mon = old_mon;
> > +    qemu_chr_close_mem(hmp.chr);
> > +    return ret;
> > +}
> > +
> >  static int compare_cmd(const char *name, const char *list)
> >  {
> >      const char *p, *pstart;
> > diff --git a/qmp-commands.hx b/qmp-commands.hx
> > index 793cf1c..b344096 100644
> > --- a/qmp-commands.hx
> > +++ b/qmp-commands.hx
> > @@ -761,6 +761,51 @@ Example:
> >  
> >  Note: This command must be issued before issuing any other command.
> >  
> > +EQMP
> > +
> > +    {
> > +        .name       = "hmp_passthrough",
> > +        .args_type  = "command-line:s,cpu-index:i?",
> > +        .params     = "",
> > +        .help       = "",
> > +        .user_print = monitor_user_noop,
> > +        .mhandler.cmd_new = do_hmp_passthrough,
> > +    },
> > +
> > +SQMP
> > +hmp_passthrough
> > +---------------
> > +
> > +Execute a Human Monitor command.
> > +
> > +Arguments: 
> > +
> > +- command-line: the command name and its arguments, just like the
> > +                Human Monitor's shell (json-string)
> > +- cpu-index: select the CPU number to be used by commands which access CPU
> > +             data, like 'info registers'. The Monitor selects CPU 0 if this
> > +             argument is not provided (json-int, optional)
> > +
> > +Example:
> > +
> > +-> { "execute": "hmp_passthrough", "arguments": { "command-line": "info kvm" } }
> > +<- { "return": "kvm support: enabled\r\n" }
> > +
> > +Notes:
> > +
> > +(1) The Human Monitor is NOT an stable interface, this means that command
> > +    names, arguments and responses can change or be removed at ANY time.
> > +    Applications that rely on long term stability guarantees should NOT
> > +    use this command
> > +
> > +(2) Limitations:
> > +
> > +    o This command is stateless, this means that commands that depend
> > +      on state information (such as getfd) might not work
> > +
> > +    o Commands that prompt the user for data (eg. 'cont' when the block
> > +      device is encrypted) don't currently work
> > +
> >  3. Query Commands
> >  =================
>

Patch

diff --git a/monitor.c b/monitor.c
index 8cee35d..89513be 100644
--- a/monitor.c
+++ b/monitor.c
@@ -491,6 +491,45 @@  static int do_qmp_capabilities(Monitor *mon, const QDict *params,
     return 0;
 }
 
+static int mon_set_cpu(int cpu_index);
+static void handle_user_command(Monitor *mon, const char *cmdline);
+
+static int do_hmp_passthrough(Monitor *mon, const QDict *params,
+                              QObject **ret_data)
+{
+    int ret = 0;
+    QString *qs;
+    Monitor *old_mon, hmp;
+    CharDriverState memchr;
+
+    memset(&hmp, 0, sizeof(hmp));
+    hmp.chr = &memchr;
+    qemu_chr_init_mem(hmp.chr);
+
+    old_mon = cur_mon;
+    cur_mon = &hmp;
+
+    if (qdict_haskey(params, "cpu-index")) {
+        ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
+        if (ret < 0) {
+            qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
+            goto out;
+        }
+    }
+
+    handle_user_command(&hmp, qdict_get_str(params, "command-line"));
+
+    qs = qemu_chr_mem_to_qs(hmp.chr);
+    if (qs) {
+        *ret_data = QOBJECT(qs);
+    }
+
+out:
+    cur_mon = old_mon;
+    qemu_chr_close_mem(hmp.chr);
+    return ret;
+}
+
 static int compare_cmd(const char *name, const char *list)
 {
     const char *p, *pstart;
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 793cf1c..b344096 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -761,6 +761,51 @@  Example:
 
 Note: This command must be issued before issuing any other command.
 
+EQMP
+
+    {
+        .name       = "hmp_passthrough",
+        .args_type  = "command-line:s,cpu-index:i?",
+        .params     = "",
+        .help       = "",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_hmp_passthrough,
+    },
+
+SQMP
+hmp_passthrough
+---------------
+
+Execute a Human Monitor command.
+
+Arguments: 
+
+- command-line: the command name and its arguments, just like the
+                Human Monitor's shell (json-string)
+- cpu-index: select the CPU number to be used by commands which access CPU
+             data, like 'info registers'. The Monitor selects CPU 0 if this
+             argument is not provided (json-int, optional)
+
+Example:
+
+-> { "execute": "hmp_passthrough", "arguments": { "command-line": "info kvm" } }
+<- { "return": "kvm support: enabled\r\n" }
+
+Notes:
+
+(1) The Human Monitor is NOT an stable interface, this means that command
+    names, arguments and responses can change or be removed at ANY time.
+    Applications that rely on long term stability guarantees should NOT
+    use this command
+
+(2) Limitations:
+
+    o This command is stateless, this means that commands that depend
+      on state information (such as getfd) might not work
+
+    o Commands that prompt the user for data (eg. 'cont' when the block
+      device is encrypted) don't currently work
+
 3. Query Commands
 =================