char: Add CirMemCharDriver and provide QMP interface
v7 cover char: Add CirMemCharDriver and provide QMP interface
This patch series attempts to add new char backend CirMemCharDriver with a circular buffer and expose it to users by introducing QMP interface memchar-write and memchar-read and via the command line like the other CharDriverStates.
Serial ports in qemu always use CharDriverStates as there backends, Right now, all of our backends always try to write the data from the guest to a socket or file. The concern from OpenStack is that this could lead to unbounded disk space usage since they log the serial output. For more detail of the background info:
https://bugs.launchpad.net/nova/+bug/832507
So we want to use a circular buffer in QEMU instead, and then OpenStack can periodically read the buffer in QEMU and log it.
The QMP commands introduced like:
{ 'command': 'memchar-write',
'data': {'chardev': 'str', 'size': 'int', 'data': 'str',
'format': 'str' } }
{ 'command': 'memchar-read',
'data': {'chardev': 'str', 'size': 'int', 'format': 'str' }, 'returns': 'str' }
Expose CirMemCharDriver via the command line like:
qemu -chardev memory,id=foo,maxcapacity=65536 -serial chardev:foo
Introduce HMP command 'console' like:
(qemu) console foo
foo: Input data
Note:
Now all of the feature were implemented, and the pervious comments are fixed up too. Please comment and let me know if there is anything else need to be improved. Your suggestion would be very appreciated!
Changes since v6:
- Improve the document based on Luiz's comments.
- Keep pointing to the right position in cbuf for the case producer and consumer might overflow for long running VMs pointed by Luiz.
- Limit the size of read_data to the amount of bytes available in the circular buffer.
- Other fixups from Luiz.
Changes since v5:
- Avoid writing the IAC information to the queue.
- Grammar of the doc for command line options improved from Eric.
Changes since v4:
- Get rid of all CongestionControl bits, and assume a dropping behavior based on Luiz's suggestion for now. Will add it when we add async support to QMP.
- Squashed the patches about CirMemCharDriver in one.
- Other fixups from Luiz.
Changes since v3:
- Improve the algorithm of circular buffer based on Anthony's suggestion.
- Some changes suggested by Luiz and Blue.
- And other fixups.
Changes since v2:
- Add congestion mechanism. For the 'block' option as sync command, will support it later when we gain the necessary infrastructure enhancement.
- Add HMP 'console' command so that can interact with multiple chardevs via a single monitor socket.
- Make the circular buffer backend and the current MemCharDriver live in parallel, expose a new char backend with circular buffer CirMemCharDriver suggested by Luiz.
- Other fixs from Eric and Markus.
Changes since v1:
- Exposing the MemCharDriver via command line.
- Support base64 data format suggested by Anthony and Eric.
- Follow the new rule for the name of qmp command from Eric.
Lei Li (4):
qemu-char: Add new char backend CirMemCharDriver
QAPI: Introduce memchar-write QMP command
QAPI: Introduce memchar-read QMP command
HMP: Introduce console command
hmp-commands.hx | 72 +++++++++++++++++++ hmp.c | 99 +++++++++++++++++++++++ hmp.h | 3 + monitor.c | 15 ++++ monitor.h | 3 + qapi-schema.json | 96 +++++++++++++++++++++++++ qemu-char.c | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-config.c | 3 + qemu-options.hx | 10 +++ qmp-commands.hx | 89 +++++++++++++++++++++ 10 files changed, 607 insertions(+), 0 deletions(-)
PATCH v7 2/4 QAPI: Introduce memchar-write QMP command
--- hmp-commands.hx | 15 +++++++++++++++ hmp.c | 13 +++++++++++++ hmp.h | 1 + qapi-schema.json | 41 +++++++++++++++++++++++++++++++++++++++++ qemu-char.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+), 0 deletions(-)
hmp-commands.hx +15 -0
@@ -840,6 +840,21 @@ Inject an NMI on the given CPU (x86 only). ETEXI { + .name = "memchar_write", + .args_type = "chardev:s,data:s", + .params = "chardev data", + .mhandler.cmd = hmp_memchar_write, + }, + +STEXI +@item memchar_write @var{chardev} @var{data} +@findex memchar_write +Provide writing interface for CirMemCharDriver. Write @var{data} +to char device 'memory'. + +ETEXI + + { .name = "migrate", .args_type = "detach:-d,blk:-b,inc:-i,uri:s", .params = "[-d] [-b] [-i] uri",
hmp.c +13 -0
@@ -682,6 +682,19 @@ void hmp_pmemsave(Monitor *mon, const QDict *qdict)hmp_handle_error (mon, &errp); } +void hmp_memchar_write (Monitor *mon,const QDict *qdict) +{ +uint32_t size; +const char *chardev =qdict_get_str (qdict,"chardev" ); +const char *data =qdict_get_str (qdict,"data" ); +Error *errp =NULL ; + + size =strlen (data); +qmp_memchar_write (chardev, size, data, false,0 , &errp); + +hmp_handle_error (mon, &errp); +} +static void hmp_cont_cb (void *opaque,int err) {if (!err) {
hmp.h +1 -0
@@ -43,6 +43,7 @@ void hmp_system_powerdown(Monitor *mon, const QDict *qdict);void hmp_cpu (Monitor *mon,const QDict *qdict);void hmp_memsave (Monitor *mon,const QDict *qdict);void hmp_pmemsave (Monitor *mon,const QDict *qdict); +void hmp_memchar_write (Monitor *mon,const QDict *qdict);void hmp_cont (Monitor *mon,const QDict *qdict);void hmp_system_wakeup (Monitor *mon,const QDict *qdict);void hmp_inject_nmi (Monitor *mon,const QDict *qdict);
qapi-schema.json +41 -0
47 lines not on this page · raw message
qemu-char.c +48 -0
54 lines not on this page · raw message
qmp-commands.hx +34 -0
@@ -466,6 +466,40 @@ Note: inject-nmi fails when the guest doesn't support injecting. EQMP { + .name = "memchar-write", + .args_type = "chardev:s,size:i,data:s,format:s?", + .help = "write size of data to memory chardev", + .mhandler.cmd_new = qmp_marshal_input_memchar_write, + }, + +SQMP +memchar-write +------------- + +Provide writing interface for CirMemCharDriver. Write data to memory +char device. + +Arguments: + +- "chardev": the name of the char device, must be unique (json-string) +- "size": the memory size, in bytes, should be power of 2 (json-int) +- "data": the source data write to memory (json-string) +- "format": the data format write to memory, default is + utf8. (json-string, optional) + - Possible values: "utf8", "base64" + +Example: + +-> { "execute": "memchar-write", + "arguments": { "chardev": foo, + "size": 8, + "data": "abcdefgh", + "format": "utf8" } } +<- { "return": {} } + +EQMP + + { .name = "xen-save-devices-state", .args_type = "filename:F", .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,
Signature · 1 line
1.7.7.6
PATCH v7 4/4 HMP: Introduce console command
In reply to Lei Li--- hmp-commands.hx | 21 +++++++++++++++++++++ hmp.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ hmp.h | 1 + monitor.c | 15 +++++++++++++++ monitor.h | 3 +++ 5 files changed, 91 insertions(+), 0 deletions(-)
hmp-commands.hx +21 -0
@@ -874,6 +874,27 @@ if the requested size is larger than it. ETEXI { + .name = "console", + .args_type = "chardev:s", + .params = "chardev", + .mhandler.cmd = hmp_console, + }, + +STEXI +@item console @var{device} +@findex console +Connect to the serial console from within the monitor, allow to write data +to memchardev @var{chardev}. Exit from the console and return back to +monitor by 'ctrl-]' or enter. + +@example +(qemu) console foo +foo: data string... +@end example + +ETEXI + + { .name = "migrate", .args_type = "detach:-d,blk:-b,inc:-i,uri:s", .params = "[-d] [-b] [-i] uri",
hmp.c +51 -0
54 lines not on this page · raw message
hmp.h +1 -0
@@ -79,5 +79,6 @@ void hmp_getfd(Monitor *mon, const QDict *qdict);void hmp_closefd (Monitor *mon,const QDict *qdict);void hmp_send_key (Monitor *mon,const QDict *qdict);void hmp_screen_dump (Monitor *mon,const QDict *qdict); +void hmp_console (Monitor *mon,const QDict *qdict);#endif
monitor.c +15 -0
@@ -256,6 +256,21 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, } } +int monitor_read_console (Monitor *mon,const char *device, +ReadLineFunc *readline_func,void *opaque) +{ +char prompt[60 ]; + +if (!mon->rs) { +return -1 ; + } + +snprintf (prompt,sizeof (prompt),"%s: " , device); +readline_start (mon->rs, prompt,0 , readline_func, opaque); + +return 0 ; +} +void monitor_flush (Monitor *mon) {if (mon && mon->outbuf_index !=0 && !mon->mux_out) {
monitor.h +3 -0
@@ -87,6 +87,9 @@ ReadLineState *monitor_get_rs(Monitor *mon);int monitor_read_password (Monitor *mon,ReadLineFunc *readline_func,void *opaque); +int monitor_read_console (Monitor *mon,const char *device, +ReadLineFunc *readline_func,void *opaque); +int qmp_qom_set (Monitor *mon,const QDict *qdict,QObject **ret);int qmp_qom_get (Monitor *mon,const QDict *qdict,QObject **ret);
Signature · 1 line
1.7.7.6
PATCH v7 3/4 QAPI: Introduce memchar-read QMP command
In reply to Lei Li--- hmp-commands.hx | 19 ++++++++++++++++++ hmp.c | 17 ++++++++++++++++ hmp.h | 1 + qapi-schema.json | 27 ++++++++++++++++++++++++- qemu-char.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qmp-commands.hx | 34 ++++++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 1 deletions(-)
hmp-commands.hx +19 -0
@@ -855,6 +855,25 @@ to char device 'memory'. ETEXI { + .name = "memchar_read", + .args_type = "chardev:s,size:i", + .params = "chardev size", + .mhandler.cmd = hmp_memchar_read, + }, + +STEXI +@item memchar_read @var{chardev} +@findex memchar_read +Provide read interface for CirMemCharDriver. Read from char device +'memory' and return @var{size} of the data. + +@var{size} is the size of data want to read from. Refer to unencoded +size of the raw data, would adjust to the init size of the memchar +if the requested size is larger than it. + +ETEXI + + { .name = "migrate", .args_type = "detach:-d,blk:-b,inc:-i,uri:s", .params = "[-d] [-b] [-i] uri",
hmp.c +17 -0
@@ -695,6 +695,23 @@ void hmp_memchar_write(Monitor *mon, const QDict *qdict)hmp_handle_error (mon, &errp); } +void hmp_memchar_read (Monitor *mon,const QDict *qdict) +{ +uint32_t size =qdict_get_int (qdict,"size" ); +const char *chardev =qdict_get_str (qdict,"chardev" ); +char *data; +Error *errp =NULL ; + + data =qmp_memchar_read (chardev, size, false,0 , &errp); +if (errp) { +monitor_printf (mon,"%s\n" ,error_get_pretty (errp)); +error_free (errp); +return ; + } + +monitor_printf (mon,"%s\n" , data); +} +static void hmp_cont_cb (void *opaque,int err) {if (!err) {
hmp.h +1 -0
@@ -44,6 +44,7 @@ void hmp_cpu(Monitor *mon, const QDict *qdict);void hmp_memsave (Monitor *mon,const QDict *qdict);void hmp_pmemsave (Monitor *mon,const QDict *qdict);void hmp_memchar_write (Monitor *mon,const QDict *qdict); +void hmp_memchar_read (Monitor *mon,const QDict *qdict);void hmp_cont (Monitor *mon,const QDict *qdict);void hmp_system_wakeup (Monitor *mon,const QDict *qdict);void hmp_inject_nmi (Monitor *mon,const QDict *qdict);
qapi-schema.json +26 -1
@@ -346,7 +346,7 @@ # # @chardev: the name of the memory char device. # -# @size: the size to write in bytes. Should be power of 2. +# @size: the size to write in bytes. # # @data: the source data write to memchar. #
@@ -366,6 +366,31 @@ '*format': 'DataFormat'} } ## +# @memchar-read: +# +# Provide read interface for memchardev. Read from the char +# device 'memory' and return the data. +# +# @chardev: the name of the memory char device. +# +# @size: the size to read in bytes. +# +# @format: #optional the format of the data want to read from +# memchardev, by default is 'utf8'. +# +# Returns: The data read from memchar as string +# If @chardev is not a valid memchr device, DeviceNotFound +# +# Notes: For now assume 'drop' behaver, which would result in reads +# returning empty strings. +# +# Since: 1.3 +## +{ 'command': 'memchar-read', + 'data': {'chardev': 'str', 'size': 'int', '*format': 'DataFormat'}, + 'returns': 'str' } + +## # @CommandInfo: # # Information about a QMP command
qemu-char.c +57 -0
63 lines not on this page · raw message
qmp-commands.hx +34 -0
@@ -500,6 +500,40 @@ Example: EQMP { + .name = "memchar-read", + .args_type = "chardev:s,size:i,format:s?", + .help = "return the size of data from memchar chardev", + .mhandler.cmd_new = qmp_marshal_input_memchar_read, + }, + +SQMP +memchar-read +------------- + +Provide read interface for CirMemCharDriver. Read from the char +device memory and return the data. + +Arguments: + +- "chardev": the name of the char device, must be unique (json-string) +- "size": the memory size wanted to read in bytes(refer to unencoded + size of the raw data), would adjust to the init size of the + memchar if the requested size is larger than it. (json-int) +- "format": the data format write to memchardev, default is + utf8. (json-string, optional) + - Possible values: "utf8", "base64" + +Example: + +-> { "execute": "memchar-read", + "arguments": { "chardev": foo, + "size": 1000, + "format": "utf8" } } +<- { "return": "data string..." } + +EQMP + + { .name = "xen-save-devices-state", .args_type = "filename:F", .mhandler.cmd_new = qmp_marshal_input_xen_save_devices_state,
Signature · 1 line
1.7.7.6
PATCH v7 1/4 qemu-char: Add new char backend CirMemCharDriver
In reply to Lei Li--- qemu-char.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-config.c | 3 + qemu-options.hx | 10 ++++ 3 files changed, 144 insertions(+), 0 deletions(-)
qemu-char.c +131 -0
@@ -99,6 +99,7 @@#include "ui/qemu-spice.h" #define READ_BUF_LEN 4096 +#define CBUFF_SIZE 65536/***********************************************************/ /* character device */
@@ -2598,6 +2599,130 @@ size_t qemu_chr_mem_osize(const CharDriverState *chr)return d->outbuf_size; } +/*********************************************************/ +/*CircularMemory chardev*/ + +typedef struct { +size_t size; +size_t head; +size_t count; +uint8_t *cbuf; +}CirMemCharDriver ; + +static bool cirmem_chr_is_empty (const CharDriverState *chr) +{ +const CirMemCharDriver *d = chr->opaque; + +return d->count ==0 ; +} + +static bool cirmem_chr_is_full (const CharDriverState *chr) +{ +const CirMemCharDriver *d = chr->opaque; + +return d->count == d->size; +} + +static size_t qemu_chr_cirmem_count (const CharDriverState *chr) +{ +const CirMemCharDriver *d = chr->opaque; + +return d->count; +} + +static int cirmem_chr_write (CharDriverState *chr,const uint8_t *buf,int len) +{ +CirMemCharDriver *d = chr->opaque; +int i; +int tail; + +if (!buf || (len <0 )) { +return -1 ; + } + +for (i =0 ; i < len; i++ ) { +/* Avoid writing the IAC information to the queue. */ +if ((unsigned char )buf[i] ==IAC ) { +continue ; + } + + tail = (d->head + d->count) % d->size; + d->cbuf[tail] = buf[i]; +if (d->count == d->size) { + d->head = (d->head +1 ) % d->size; + }else { + ++d->count; + } + } + +return 0 ; +} + +static int cirmem_chr_read (CharDriverState *chr,uint8_t *buf,int len) +{ +CirMemCharDriver *d = chr->opaque; +int i; + +if (cirmem_chr_is_empty (chr) || len <0 ) { +return -1 ; + } + +for (i =0 ; i < len; i++) { + buf[i] = d->cbuf[d->head]; + d->head = (d->head +1 ) % d->size; + d->count--; + +if (cirmem_chr_is_empty (chr)) { +break ; + } + } + +return 0 ; +} + +static void cirmem_chr_close (struct CharDriverState *chr) +{ +CirMemCharDriver *d = chr->opaque; + +g_free (d->cbuf); +g_free (d); + chr->opaque =NULL ; +} + +static CharDriverState *qemu_chr_open_cirmemchr (QemuOpts *opts) +{ +CharDriverState *chr; +CirMemCharDriver *d; + + chr =g_malloc0 (sizeof (CharDriverState)); + d =g_malloc (sizeof (*d)); + + d->size =qemu_opt_get_number (opts,"maxcapacity" ,0 ); +if (d->size ==0 ) { + d->size =CBUFF_SIZE ; + } + +/* The size must be power of 2 */ +if (d->size & (d->size -1 )) { + goto fail; + } + + d->head =0 ; + d->count =0 ; + d->cbuf =g_malloc0 (d->size); + + chr->opaque = d; + chr->chr_write = cirmem_chr_write; + chr->chr_close = cirmem_chr_close; + +return chr; + +fail: +g_free (d); +g_free (chr); +return NULL ; +} +QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename) {char host[65 ], port[33 ], width[8 ], height[8 ];
@@ -2662,6 +2787,11 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)qemu_opt_set (opts,"path" , p);return opts; } +if (strstart (filename,"memory" , &p)) { +qemu_opt_set (opts,"backend" ,"memory" ); +qemu_opt_set (opts,"maxcapacity" , p); +return opts; + }if (strstart (filename,"tcp:" , &p) ||strstart (filename,"telnet:" , &p)) {if (sscanf (p,"%64[^:]:%32[^,]%n" , host, port, &pos) <2 ) {
@@ -2735,6 +2865,7 @@ static const struct { { .name = "udp", .open = qemu_chr_open_udp }, { .name = "msmouse", .open = qemu_chr_open_msmouse }, { .name = "vc", .open = text_console_init }, + { .name = "memory", .open = qemu_chr_open_cirmemchr }, #ifdef _WIN32 { .name = "file", .open = qemu_chr_open_win_file_out }, { .name = "pipe", .open = qemu_chr_open_win_pipe },
qemu-config.c +3 -0
@@ -213,6 +213,9 @@ static QemuOptsList qemu_chardev_opts = { },{ .name = "debug", .type = QEMU_OPT_NUMBER, + },{ + .name = "maxcapacity", + .type = QEMU_OPT_NUMBER, }, {/* end of list */ } },
qemu-options.hx +10 -0
@@ -1716,6 +1716,7 @@ DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, "-chardev msmouse,id=id[,mux=on|off]\n" "-chardev vc,id=id[[,width=width][,height=height]][[,cols=cols][,rows=rows]]\n" " [,mux=on|off]\n" + "-chardev memory,id=id,maxcapacity=maxcapacity\n" "-chardev file,id=id,path=path[,mux=on|off]\n" "-chardev pipe,id=id,path=path[,mux=on|off]\n" #ifdef _WIN32
@@ -1754,6 +1755,7 @@ Backend is one of: @option{udp}, @option{msmouse}, @option{vc}, +@option{memory}, @option{file}, @option{pipe}, @option{console},
@@ -1860,6 +1862,14 @@ the console, in pixels. @option{cols} and @option{rows} specify that the console be sized to fit a text console with the given dimensions. +@item -chardev memory ,id=@var{id} ,maxcapacity=@var{maxcapacity} + +Create a circular buffer with fixed size indicated by optionally @option{maxcapacity} +which will be default 64K if it is not given. + +@option{maxcapacity} specifies the max capacity of the size of circular buffer +to create. Should be power of 2. + @item -chardev file ,id=@var{id} ,path=@var{path} Log all traffic received from the guest to a file.
Signature · 1 line
1.7.7.6
Re: [Qemu-devel] [PATCH 3/4] QAPI: Introduce memchar-read QMP command
In reply to Lei Liqapi-schema.json line 352
Quoted 12 lines · Lei Li
Signed-off-by Lei Li <[email protected]>---
+++b/qapi-schema.jsonqapi-schema.json +1
-1@@ -346,7 +346,7 @@ # # @chardev: the name of the memory char device. # -# @size: the size to write in bytes. Should be power of 2. +# @size: the size to write in bytes. # # @data: the source data write to memchar. #
Spurious deletion?
qmp-commands.hx Example: · line 519
Quoted 11 lines
qmp-commands.hx +11
-0+SQMP +memchar-read +------------- + +Provide read interface for CirMemCharDriver. Read from the char +device memory and return the data. + +Arguments: + +- "chardev": the name of the char device, must be unique (json-string) +- "size": the memory size wanted to read in bytes(refer to unencoded
s/bytes(refer/bytes (refers/
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org