Patchwork [RFC,3/4] qmp: Introduce memchar_write QMP command

login
register
mail settings
Submitter Lei Li
Date Aug. 1, 2012, 9:48 a.m.
Message ID <1343814538-27591-4-git-send-email-lilei@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/174425/
State New
Headers show

Comments

Lei Li - Aug. 1, 2012, 9:48 a.m.
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 qapi-schema.json |   20 ++++++++++++++++++++
 qemu-char.c      |   19 +++++++++++++++++++
 qmp-commands.hx  |   29 +++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 0 deletions(-)
Eric Blake - Aug. 1, 2012, 3:50 p.m.
On 08/01/2012 03:48 AM, Lei Li wrote:
> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qapi-schema.json |   20 ++++++++++++++++++++
>  qemu-char.c      |   19 +++++++++++++++++++
>  qmp-commands.hx  |   29 +++++++++++++++++++++++++++++
>  3 files changed, 68 insertions(+), 0 deletions(-)
> 
> diff --git a/qapi-schema.json b/qapi-schema.json
> index bc55ed2..3c8530f 100644
> --- a/qapi-schema.json
> +++ b/qapi-schema.json
> @@ -206,6 +206,26 @@
>  { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>  
>  ##
> +# @memchar_write:

s/memchar_write/memchar-write/

New QMP commands should use '-' not '_'.

> +#
> +# Provide writing interface for memchardev. Write data to memchar
> +# char device.
> +#
> +# @chardev: the name of the memchar char device.
> +#
> +# @size: the size to write in bytes.
> +#
> +# @data: the source data write to memchar.

Does this allow full binary data processing, or does the data need
something like base64 encoding?

> +#
> +# Returns: Nothing on success
> +#          If an I/O error occurs while writing, IOError
> +#
> +# Since: 1.2
> +##
> +{ 'command': 'memchar_write',

s/_/-/

> +++ b/qmp-commands.hx
> @@ -441,6 +441,35 @@ Note: inject-nmi is only supported for x86 guest currently, it will
>  EQMP
>  
>      {
> +        .name       = "memchar_write",

s/_/-/

> +        .args_type  = "chardev:s,size:i,data:s",
> +        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
> +    },
> +
> +SQMP
> +memchar_write
> +-------------
> +
> +Provide writing interface for memchardev. Write data to memchar
> +char device.
> +
> +Arguments:
> +
> +- "chardev": the name of the char device, must be unique (json-string)
> +- "size": the memory size, in bytes (json-int)
> +- "data": the source data writed to memchar (json-string)

s/writed/written/

> +
> +Example:
> +
> +-> { "execute": "memchar_write",
> +                "arguments": { "chardev": foo,
> +                               "size": 1000,
> +                               "data": "data string" } }

1000 is longer than strlen("data string").  Is there supposed to be a
correlation?  Is there a start offset to worry about, or do memchardevs
always behave like they are in append mode?
Anthony Liguori - Aug. 1, 2012, 9:33 p.m.
Lei Li <lilei@linux.vnet.ibm.com> writes:

> Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
> ---
>  qapi-schema.json |   20 ++++++++++++++++++++
>  qemu-char.c      |   19 +++++++++++++++++++
>  qmp-commands.hx  |   29 +++++++++++++++++++++++++++++
>  3 files changed, 68 insertions(+), 0 deletions(-)
>
> diff --git a/qapi-schema.json b/qapi-schema.json
> index bc55ed2..3c8530f 100644
> --- a/qapi-schema.json
> +++ b/qapi-schema.json
> @@ -206,6 +206,26 @@
>  { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
>  
>  ##
> +# @memchar_write:

memchar-write as Eric suggested.

> +#
> +# Provide writing interface for memchardev. Write data to memchar
> +# char device.
> +#
> +# @chardev: the name of the memchar char device.
> +#
> +# @size: the size to write in bytes.
> +#
> +# @data: the source data write to memchar.
> +#
> +# Returns: Nothing on success
> +#          If an I/O error occurs while writing, IOError
> +#
> +# Since: 1.2
> +##
> +{ 'command': 'memchar_write',
> +  'data': {'chardev': 'str', 'size': 'int', 'data': 'str'} }

You should add another option which is '*format': 'DataFormat'.

DataFormat would be an enum and would include: "utf8", "base64" to start
with.  The default value would be utf8.  You'll need to be careful to
scrub the output of memchar-read of any non-UTF8 character to make sure
what you are returning is UTF8 clean.

> +
> +##
>  # @CommandInfo:
>  #
>  # Information about a QMP command
> diff --git a/qemu-char.c b/qemu-char.c
> index 087c92d..1012e65 100644
> --- a/qemu-char.c
> +++ b/qemu-char.c
> @@ -2636,6 +2636,25 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr)
>      return d->cbuf_count;
>  }
>  
> +void qmp_memchar_write(const char *chardev, int64_t size,
> +                       uint8_t *data, Error **errp)
> +{
> +    CharDriverState *chr;
> +    int ret;
> +
> +    chr = qemu_chr_find(chardev);
> +
> +    if(!chr) {
> +        qemu_chr_init_mem(chr, size);
> +    }

Indenting is off here (need a space after the if).  Plus chr is still
NULL after this returns.  I don't think you should do lazy init here.
If qemu_chr_find() returns NULL, you should throw an error.

Regards,

Anthony Liguori

> +
> +    ret = mem_chr_write(chr, data, size);
> +    if (ret <= 0) {
> +        error_set(errp, QERR_IO_ERROR);
> +        return;
> +    }
> +}
> +
>  QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
>  {
>      char host[65], port[33], width[8], height[8];
> diff --git a/qmp-commands.hx b/qmp-commands.hx
> index e3cf3c5..182f1e6 100644
> --- a/qmp-commands.hx
> +++ b/qmp-commands.hx
> @@ -441,6 +441,35 @@ Note: inject-nmi is only supported for x86 guest currently, it will
>  EQMP
>  
>      {
> +        .name       = "memchar_write",
> +        .args_type  = "chardev:s,size:i,data:s",
> +        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
> +    },
> +
> +SQMP
> +memchar_write
> +-------------
> +
> +Provide writing interface for memchardev. Write data to memchar
> +char device.
> +
> +Arguments:
> +
> +- "chardev": the name of the char device, must be unique (json-string)
> +- "size": the memory size, in bytes (json-int)
> +- "data": the source data writed to memchar (json-string)
> +
> +Example:
> +
> +-> { "execute": "memchar_write",
> +                "arguments": { "chardev": foo,
> +                               "size": 1000,
> +                               "data": "data string" } }
> +<- { "return": {} }
> +
> +EQMP
> +
> +    {
>          .name       = "xen-save-devices-state",
>          .args_type  = "filename:F",
>      .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,
> -- 
> 1.7.7.6

Patch

diff --git a/qapi-schema.json b/qapi-schema.json
index bc55ed2..3c8530f 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -206,6 +206,26 @@ 
 { 'command': 'query-chardev', 'returns': ['ChardevInfo'] }
 
 ##
+# @memchar_write:
+#
+# Provide writing interface for memchardev. Write data to memchar
+# char device.
+#
+# @chardev: the name of the memchar char device.
+#
+# @size: the size to write in bytes.
+#
+# @data: the source data write to memchar.
+#
+# Returns: Nothing on success
+#          If an I/O error occurs while writing, IOError
+#
+# Since: 1.2
+##
+{ 'command': 'memchar_write',
+  'data': {'chardev': 'str', 'size': 'int', 'data': 'str'} }
+
+##
 # @CommandInfo:
 #
 # Information about a QMP command
diff --git a/qemu-char.c b/qemu-char.c
index 087c92d..1012e65 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2636,6 +2636,25 @@  size_t qemu_chr_mem_osize(const CharDriverState *chr)
     return d->cbuf_count;
 }
 
+void qmp_memchar_write(const char *chardev, int64_t size,
+                       uint8_t *data, Error **errp)
+{
+    CharDriverState *chr;
+    int ret;
+
+    chr = qemu_chr_find(chardev);
+
+    if(!chr) {
+        qemu_chr_init_mem(chr, size);
+    }
+
+    ret = mem_chr_write(chr, data, size);
+    if (ret <= 0) {
+        error_set(errp, QERR_IO_ERROR);
+        return;
+    }
+}
+
 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
 {
     char host[65], port[33], width[8], height[8];
diff --git a/qmp-commands.hx b/qmp-commands.hx
index e3cf3c5..182f1e6 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -441,6 +441,35 @@  Note: inject-nmi is only supported for x86 guest currently, it will
 EQMP
 
     {
+        .name       = "memchar_write",
+        .args_type  = "chardev:s,size:i,data:s",
+        .mhandler.cmd_new = qmp_marshal_input_memchar_write,
+    },
+
+SQMP
+memchar_write
+-------------
+
+Provide writing interface for memchardev. Write data to memchar
+char device.
+
+Arguments:
+
+- "chardev": the name of the char device, must be unique (json-string)
+- "size": the memory size, in bytes (json-int)
+- "data": the source data writed to memchar (json-string)
+
+Example:
+
+-> { "execute": "memchar_write",
+                "arguments": { "chardev": foo,
+                               "size": 1000,
+                               "data": "data string" } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "xen-save-devices-state",
         .args_type  = "filename:F",
     .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,