qemu-char: add logfile facility to all chardev backends
PATCH v2 1/1 qemu-char: add logfile facility to all chardev backends
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path.
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\
server=on,nowait,id-charserial0,\
logfile=/var/log/libvirt/qemu/test-serial0.log
-device isa-serial,chardev=charserial0,id=serial0This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
---
Changed in v2:
- Push logfile logic out of backend into common code so it is available for all backend types
- Add docs to qemu -help text
include/sysemu/char.h | 1 + qapi-schema.json | 49 +++++++--- qemu-char.c | 263 +++++++++++++++++++++++++++++++++++++++++++------- qemu-options.hx | 41 ++++---- 4 files changed, 290 insertions(+), 64 deletions(-)
include/sysemu/char.h +1 -0
@@ -77,6 +77,7 @@ struct CharDriverState {void *opaque;char *label;char *filename; +int logfd;int be_open;int fe_open;int explicit_fe_open;
qapi-schema.json +38 -11
121 lines not on this page · raw message
qemu-char.c +229 -34
649 lines not on this page · raw message
qemu-options.hx +22 -19
62 lines not on this page · raw message
Signature · 1 line
2.5.0
Quoted 6 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[meta-review of JUST qapi decisions; code review in a separate message]
Quoted 22 lines
Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path.
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0
Quoted 6 lines
This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union.
It is possible to convert ChardevBackend into a discriminated union while still keeping the same QMP semantics.
But where it gets interesting is what the QMP semantics should be.
Right now, we have (simplifying to just two branches, for less typing):
{ 'union': 'ChardevBackend',
'data': { 'file': 'ChardevFile',
'serial': 'ChardevHostdev' } }
which means we support:
{ "execute": "chardev-add", "arguments": {
"id": "foo", "backend": { "type": "file",
"data": { "out": "filename" } } } }With your addition, ChardevFile now inherits from ChardevCommon, so we gain:
{ "execute": "chardev-add", "arguments": {
"id": "foo", "backend": { "type": "file",
"data": { "logfile": "logname", "out": "filename" } } } }Re-writing the existing ChardevBackend to a semantically-identical flat union would be (using my shorthand syntax for anonymous base [1] and anonymous branch wrappers [2]):
{ 'enum': 'ChardevType', 'data': [ 'file', 'serial' ] } { 'union': 'ChardevBackend',
'base': { 'type': 'ChardevType' }, 'discriminator': 'type', 'data': { 'file': { 'data': 'ChardevFile' },
'serial': { 'data': 'ChardevHostdev' } } }[1] http://repo.or.cz/qemu/ericb.git/commitdiff/dbb8680b1
[2] not yet posted to list or my git repo
Note that in my conversion, 'file' is no longer directly a 'ChardevFile', but an anonymous type with one field named 'data' where _that_ field is a ChardevFile; necessary to keep the existing QMP nesting the same.
Your proposal, then, is that the new 'logging' fields in your ChardevCommon should be made part of the base type of the 'ChardevBackend' union; which would be spelled as:
{ 'enum': 'ChardevType', 'data': [ 'file', 'serial' ] } { 'struct': 'ChardevCommon', 'data': {
'type': 'ChardevType', '*logfile': 'str', '*logappend': bool } }
{ 'union': 'ChardevBackend',
'base': 'ChardevCommon', 'discriminator': 'type', 'data': { 'file': { 'data': 'ChardevFile' },
'serial': { 'data': 'ChardevHostdev' } } }But done that way, the QMP wire form would be:
{ "execute": "chardev-add", "arguments": {
"id": "foo", "backend": { "type": "file",
"logfile": "logname", "data": { "out": "filename" } } } }Note the difference: "logfile" changes from being a child of "data" to being a sibling.
Hmm - now that I've typed all that, I wonder if it would make more sense to instead just make these parameters be siblings of "backend", by instead modifying:
{ 'command': 'chardev-add', 'data': {
'id': 'str', 'backend': 'ChardevBackend',
'*logfile': 'str', '*logappend': bool } }where the QMP command would be:
{ "execute": "chardev-add", "arguments": {
"id": "foo", "logfile": "logname", "backend": { "type": "file",
"data": { "out": "filename" } } } }But while that would certainly be less invasive to the qapi, it may make life harder for the C implementation (it's no longer associated with the ChardevBackend struct, but has to be tracked separately).
So, depending on which of those three places we want to stick the new parameters determines which approach we should use for exposing them in qapi.
Quoted 8 lines
The
ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).Signed-off-by Daniel P. Berrange <[email protected]>---
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 7 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[code review, if we go with this design; see my other message for 2 possible alternative qapi designs that may supersede this code review]
Quoted 27 lines
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
Signed-off-by Daniel P. Berrange <[email protected]>---
+++
b/qapi-schema.jsonqapi-schema.json +0
-0@@ -3093,6 +3093,21 @@ ## { 'command': 'screendump', 'data': {'filename': 'str'} }
qapi-schema.json line 3104
Quoted 10 lines
qapi-schema.json +9
-0+ +## +# @ChardevCommon: +# +# Configuration shared across all chardev backends +# +# @logfile: #optional The name of a logfile to save output +# @logappend: #optional true to append instead of truncate +# (default to false to truncate)
Could shorten to:
# @logappend: #optional true to append to @logfile (default false to truncate)
qapi-schema.json line 3273
Quoted 9 lines
qapi-schema.json +2
-1## # @ChardevBackend:@@ -3243,7 +3269,8 @@ # # Since: 1.4 (testdev since 2.2) ## -{ 'struct': 'ChardevDummy', 'data': { } } +{ 'struct': 'ChardevDummy', 'data': { }, + 'base': 'ChardevCommon' }
Instead of changing ChardevDummy, you could have deleted this type and done:
Quoted 3 lines
qapi-schema.json +0
-0{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile', 'serial' : 'ChardevHostdev',
...
'pty': 'ChardevCommon',
'null': 'ChardevCommon',and so on. I don't know which is better.
Quoted 8 lines
qemu-char.c +8
-0+/* Not reporting errors from writing to logfile, as logs are +* defined to be "best effort" only */ +static void qemu_chr_fe_write_log (CharDriverState *s, +const uint8_t *buf,size_t len) +{ +size_t done =0 ; +ssize_t ret; +
qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s) · line 212
Quoted 10 lines
qemu-char.c +10
-0+if (s->logfd <0 ) { +return ; + } + +while (done < len) { + do { + ret = write(s->logfd, buf + done, len - done); + if (ret == -1 && errno == EAGAIN) { + g_usleep(100); + }
Do we really want to be sleeping here?
qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s) · line 215
Quoted 3 lines
qemu-char.c +3
-0+ }while (ret ==-1 && errno ==EAGAIN ); + +if (ret <=0 ) {
Why '<='? Shouldn't this be '<'?
Quoted 4 lines
qemu-char.c +4
-0+return ; + } + done += ret; + }
qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s)
Quoted 10 lines
qemu-char.c +8
-0+} + + +static int qemu_chr_open_common (CharDriverState *chr, +ChardevBackend *backend, +Error **errp) +{ +ChardevCommon *common = backend->u.data;
Phooey. This conflicts with my pending patches to get rid of 'data':
http://repo.or.cz/qemu/ericb.git/commitdiff/84aaab99
I mentioned above that you could do things like 'null':'ChardevCommon' instead of 'null':'ChardevDummy'; and we also know that qapi guarantees a layout where all base types occur at the front of the rest of the type. So you could write this as:
ChardevCommon *common = backend->u.null;
and things will work even when 'data' is gone. But maybe that argues more strongly that we should hoist the common members into the base fields of ChardevBackend struct, or even as separate parameters to the 'chardev-add' command (the two alternate qapi representations I described in my other message).
qemu-char.c static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) · line 414
Quoted 7 lines
qemu-char.c +7
-0+ +if (common->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (!common->has_logappend || + !common->logappend) { + flags |=O_TRUNC ; + }
Umm, don't you need to set O_APPEND when common->logappend is true?
Quoted 1 line
+
chr->logfd=qemu_open(common->logfile, flags, 0666);
...
qemu-char.c static CharDriverState *qemu_chr_open_null(const char *id, · line 437
Quoted 7 lines
qemu-char.c +3
-0@@ -367,9 +432,16 @@ static CharDriverState *qemu_chr_open_null(const char *id,CharDriverState *chr; chr =qemu_chr_alloc (); +if (qemu_chr_open_common (chr, backend, errp) <0 ) { + goto error; + }
The other thing we could do here is have qemu_chr_open_common() take a ChardevCommon* instead of a ChardevBackend*. Then each caller would do an appropriate upcast before calling the common code:
ChardevCommon *common = qapi_ChardevDummy_base(backend->u.null); if (qemu_chr_open_common(chr, common, errp) {
qemu-char.c static CharDriverState *qemu_chr_open_mux(const char *id, · line 748
Quoted 7 lines
qemu-char.c +1
-0/* MUX driver for serial I/O splitting */ @@ -673,6 +745,9 @@ static CharDriverState *qemu_chr_open_mux(const char *id, } chr =qemu_chr_alloc (); +if (qemu_chr_open_common (chr, backend, errp) <0 ) {
ChardevCommon *common = qapi_ChardevDummy_base(backend->u.mux); if (qemu_chr_open_common(chr, common, errp) {
and so forth. That feels a bit more type-safe (and less reliant on qapi layout guarantees) than trying to rely on the backend->u.data that I'm trying to get rid of.
qemu-char.c static void fd_chr_close(struct CharDriverState *chr) · line 1129
Quoted 7 lines
qemu-char.c +2
-1@@ -1046,12 +1125,16 @@ static void fd_chr_close(struct CharDriverState *chr) }/* open a character device to a unix fd */ -static CharDriverState *qemu_chr_open_fd (int fd_in,int fd_out) +static CharDriverState *qemu_chr_open_fd (int fd_in,int fd_out, +ChardevBackend *backend,Error **errp)
Might be better as ChardevCommon *common here as well.
qemu-options.hx The general form of a character device option is: · line 2037
Quoted 7 lines
qemu-options.hx +2
-1+++ b/qemu-options.hx@@ -2034,40 +2034,43 @@ The general form of a character device option is: ETEXI DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, - "-chardev null,id=id[,mux=on|off]\n" + "-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"
Do we want to allow logappend=on even when logfile is unspecified, or should we make that an error?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 55 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[meta-review of JUST qapi decisions; code review in a separate message]
Quoted 22 lines
Currently providing such a feature forces the mgmt app to either provide 2 separate serial ports, one for logging boot messages and one for interactive console login, or to proxy all output via a separate service that can multiplex the two needs onto one serial port. While both are valid approaches, they each have their own downsides. The former causes confusion and extra setup work for VM admins creating disk images. The latter places an extra burden to re-implement much of the QEMU chardev backends logic in libvirt or even higher level mgmt apps and adds extra hops in the data transfer path.
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0Quoted 6 lines
This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union.
It is possible to convert ChardevBackend into a discriminated union while still keeping the same QMP semantics.
But where it gets interesting is what the QMP semantics should be.
Right now, we have (simplifying to just two branches, for less typing):
{ 'union': 'ChardevBackend',
'data': { 'file': 'ChardevFile','serial': 'ChardevHostdev' } }which means we support:
{ "execute": "chardev-add", "arguments": { "id": "foo", "backend": { "type": "file", "data": { "out": "filename" } } } }
Quoted 5 lines
With your addition, ChardevFile now inherits from ChardevCommon, so we gain:
{ "execute": "chardev-add", "arguments": { "id": "foo", "backend": { "type": "file", "data": { "logfile": "logname", "out": "filename" } } } }
Ok good that matches what I intended - namely that 'logfile' should appear at the same dict as the rest of the backend fields, to mirror the the fact that the C struct had all the common fields in the same struct too.
Quoted 30 lines
Re-writing the existing ChardevBackend to a semantically-identical flat union would be (using my shorthand syntax for anonymous base [1] and anonymous branch wrappers [2]):
{ 'enum': 'ChardevType', 'data': [ 'file', 'serial' ] } { 'union': 'ChardevBackend',
'base': { 'type': 'ChardevType' }, 'discriminator': 'type', 'data': { 'file': { 'data': 'ChardevFile' },'serial': { 'data': 'ChardevHostdev' } } }[1] http://repo.or.cz/qemu/ericb.git/commitdiff/dbb8680b1
[2] not yet posted to list or my git repoNote that in my conversion, 'file' is no longer directly a 'ChardevFile', but an anonymous type with one field named 'data' where _that_ field is a ChardevFile; necessary to keep the existing QMP nesting the same.
Your proposal, then, is that the new 'logging' fields in your ChardevCommon should be made part of the base type of the 'ChardevBackend' union; which would be spelled as:
{ 'enum': 'ChardevType', 'data': [ 'file', 'serial' ] } { 'struct': 'ChardevCommon', 'data': {
'type': 'ChardevType', '*logfile': 'str', '*logappend': bool } }
{ 'union': 'ChardevBackend',
'base': 'ChardevCommon', 'discriminator': 'type', 'data': { 'file': { 'data': 'ChardevFile' },'serial': { 'data': 'ChardevHostdev' } } }
Quoted 8 lines
But done that way, the QMP wire form would be:
{ "execute": "chardev-add", "arguments": { "id": "foo", "backend": { "type": "file", "logfile": "logname", "data": { "out": "filename" } } } }Note the difference: "logfile" changes from being a child of "data" to being a sibling.
Ok, so that's still backwards compatible, but the 'logfile' is appearing in an expected place IMHO.
Quoted 10 lines
Hmm - now that I've typed all that, I wonder if it would make more sense to instead just make these parameters be siblings of "backend", by instead modifying:
{ 'command': 'chardev-add', 'data': { 'id': 'str', 'backend': 'ChardevBackend', '*logfile': 'str', '*logappend': bool } }where the QMP command would be:
Quoted 7 lines
{ "execute": "chardev-add", "arguments": { "id": "foo", "logfile": "logname", "backend": { "type": "file", "data": { "out": "filename" } } } }But while that would certainly be less invasive to the qapi, it may make life harder for the C implementation (it's no longer associated with the ChardevBackend struct, but has to be tracked separately).
Yeah, that would require a bit of refactoring, since many of the codepaths I'm changing only get passed in the 'ChardevBackend' struct, not its parent owner.
Quoted 5 lines
So, depending on which of those three places we want to stick the new parameters determines which approach we should use for exposing them in qapi.
From the QMP representation POV, my preference is for the current
design since I think the 'logfile' attribute is really just another one of the backend config attributes. The choice to have a ChardevCommon struct was just a mechanism to avoid having to repeat the 'logfile' parameter in every single Chardev* backend type. This naturally matches the C-struct, where the ChardevCommon fields get directly added to the ChardevFile, ChardevSocket, etc structs.
So from that POV, I'd be against, pushing the 'logfile' field up either 1 or 2 levels.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 68 lines · Eric Blake
Quoted 7 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[code review, if we go with this design; see my other message for 2 possible alternative qapi designs that may supersede this code review]
Quoted 27 lines
A simpler approach that is satisfactory for many use cases is to allow the QEMU chardev backends to have a "logfile" property associated with them.
$QEMU -chardev socket,host=localhost,port=9000,\ server=on,nowait,id-charserial0,\ logfile=/var/log/libvirt/qemu/test-serial0.log -device isa-serial,chardev=charserial0,id=serial0This patch introduces a 'ChardevCommon' struct which is setup as a base for all the ChardevBackend types. Ideally this would be registered directly as a base against ChardevBackend, rather than each type, but the QAPI generator doesn't allow that since the ChardevBackend is a non-discriminated union. The ChardevCommon struct provides the optional 'logfile' parameter, as well as 'logappend' which controls whether QEMU truncates or appends (default truncate).
Signed-off-by Daniel P. Berrange <[email protected]>---
+++
b/qapi-schema.jsonqapi-schema.json +0
-0@@ -3093,6 +3093,21 @@ ## { 'command': 'screendump', 'data': {'filename': 'str'} }qapi-schema.json line 3104
Quoted 10 lines
qapi-schema.json +9
-0+ +## +# @ChardevCommon: +# +# Configuration shared across all chardev backends +# +# @logfile: #optional The name of a logfile to save output +# @logappend: #optional true to append instead of truncate +# (default to false to truncate)Could shorten to:
# @logappend: #optional true to append to @logfile (default false to truncate)
qapi-schema.json line 3273
Quoted 9 lines
qapi-schema.json +2
-1## # @ChardevBackend:@@ -3243,7 +3269,8 @@ # # Since: 1.4 (testdev since 2.2) ## -{ 'struct': 'ChardevDummy', 'data': { } } +{ 'struct': 'ChardevDummy', 'data': { }, + 'base': 'ChardevCommon' }Instead of changing ChardevDummy, you could have deleted this type and done:
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile',
'serial' : 'ChardevHostdev',...
'pty': 'ChardevCommon', 'null': 'ChardevCommon',and so on. I don't know which is better.
Yep, me neither. Given the name it felt like some kind of placeholder for future work, so I left it alone.
Quoted 12 lines
Quoted 12 lines
qemu-char.c +12
-0+/* Not reporting errors from writing to logfile, as logs are +* defined to be "best effort" only */ +static void qemu_chr_fe_write_log (CharDriverState *s, +const uint8_t *buf,size_t len) +{ +size_t done =0 ; +ssize_t ret; + +if (s->logfd <0 ) { +return ; + } +
Quoted 8 lines
qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s) · line 212
Quoted 6 lines
qemu-char.c +6
-0+while (done < len) { + do { + ret = write(s->logfd, buf + done, len - done); + if (ret == -1 && errno == EAGAIN) { + g_usleep(100); + }Do we really want to be sleeping here?
If logfile points to a plain file, you'll never get EAGAIN. For that matter even if it doesn't point to a plain file I realize that we've not called qemu_nonblock() on logfd, so it'll never get EAGAIN for that reason either.
The qemu_chr_fe_write_all() currently has the same usleep which is what I followed. The qemu_chr_fe_write() just returns on EAGAIN. In the write_log() method we want to try to write all the data the qemu_chr_fe_write/write_all just sent. If we ignore EGAIN, we'll potentially loose data from the log, but if we honour it, we have to sleep a little or not set O_NONBLOCK.
Quoted 6 lines
qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s) · line 215
Quoted 4 lines
qemu-char.c +3
-0+ }while (ret ==-1 && errno ==EAGAIN ); + +if (ret <=0 ) {Why '<='? Shouldn't this be '<'?
Well there's no difference really since write() will never return 0.
Quoted 57 lines
Quoted 5 lines
qemu-char.c +4
-0+return ; + } + done += ret; + }qemu-char.c void qemu_chr_be_generic_open(CharDriverState *s)
Quoted 10 lines
qemu-char.c +8
-0+} + + +static int qemu_chr_open_common (CharDriverState *chr, +ChardevBackend *backend, +Error **errp) +{ +ChardevCommon *common = backend->u.data;Phooey. This conflicts with my pending patches to get rid of 'data':
http://repo.or.cz/qemu/ericb.git/commitdiff/84aaab99
I mentioned above that you could do things like 'null':'ChardevCommon' instead of 'null':'ChardevDummy'; and we also know that qapi guarantees a layout where all base types occur at the front of the rest of the type. So you could write this as:
ChardevCommon *common =
backend->u.null;and things will work even when 'data' is gone. But maybe that argues more strongly that we should hoist the common members into the base fields of ChardevBackend struct, or even as separate parameters to the 'chardev-add' command (the two alternate qapi representations I described in my other message).
qemu-char.c static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) · line 414
Quoted 7 lines
qemu-char.c +7
-0+ +if (common->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (!common->has_logappend || + !common->logappend) { + flags |=O_TRUNC ; + }Umm, don't you need to set
O_APPENDwhencommon->logappendis true?Quoted 1 line
+
chr->logfd=qemu_open(common->logfile, flags, 0666);...
qemu-char.c static CharDriverState *qemu_chr_open_null(const char *id, · line 437
Quoted 7 lines
qemu-char.c +3
-0@@ -367,9 +432,16 @@ static CharDriverState *qemu_chr_open_null(const char *id,CharDriverState *chr; chr =qemu_chr_alloc (); +if (qemu_chr_open_common (chr, backend, errp) <0 ) { + goto error; + }The other thing we could do here is have
qemu_chr_open_common()take a ChardevCommon* instead of a ChardevBackend*. Then each caller would do an appropriate upcast before calling the common code:
Quoted 2 lines
ChardevCommon *common =
qapi_ChardevDummy_base(backend->u.null); if (qemu_chr_open_common(chr, common, errp){
Yep, I think this is the easiest thing todo, to avoid use of backend->u.data.
Quoted 3 lines
and so forth. That feels a bit more type-safe (and less reliant on qapi layout guarantees) than trying to rely on the
backend->u.datathat I'm trying to get rid of.
Agreed
Quoted 10 lines
qemu-options.hx The general form of a character device option is: · line 2037
Quoted 7 lines
qemu-options.hx +2
-1+++ b/qemu-options.hx@@ -2034,40 +2034,43 @@ The general form of a character device option is: ETEXI DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, - "-chardev null,id=id[,mux=on|off]\n" + "-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"Do we want to allow logappend=on even when logfile is unspecified, or should we make that an error?
I figured it was harmless to just ignore it.
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
qemu-char.c static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) · line 414
Quoted 6 lines · Daniel P. Berrange
qemu-char.c +6
-0+if (common->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (!common->has_logappend || + !common->logappend) { + flags |=O_TRUNC ; + }
Should it use O_APPEND if logappend is absent or true, or perhaps always? I can take care of the change myself.
You are also missing a few qemu_chr_alloc calls outside qemu-char.c, which makes me wonder if it's better to add the new logic in qemu_chr_alloc itself.
Paolo
Quoted 11 lines
qemu-char.c +11
-0+ chr->logfd =qemu_open (common->logfile, flags,0666 ); +if (chr->logfd <0 ) { +error_setg_errno (errp, errno, +"Unable to open logfile %s" , + common->logfile); +return -1 ; + } + } + +return 0 ; +}
Quoted 10 lines · Paolo Bonzini
qemu-char.c static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) · line 414
Quoted 6 lines · Daniel P. Berrange
qemu-char.c +6
-0+if (common->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (!common->has_logappend || + !common->logappend) { + flags |=O_TRUNC ; + }
Quoted 2 lines
Should it use
O_APPENDif logappend is absent or true, or perhaps always? I can take care of the change myself.
Yes it should use O_APPEND in the other branch of the if, that's a bug eric pointed out too. Basically same logic as the recently added 'append' flag in qmp_chardev_open_file
Quoted 3 lines
You are also missing a few
qemu_chr_alloccalls outsideqemu-char.c, which makes me wonder if it's better to add the new logic inqemu_chr_allocitself.
Oh, yes, perhaps I should just pass ChardevCommon straight into qemu_chr_alloc(). I'll look at that when doing the other changes Eric suggested wrt to removing use of backend.u.data
Regards,
Daniel
Signature · 4 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
Quoted 15 lines · Daniel P. Berrange
Quoted 14 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[meta-review of JUST qapi decisions; code review in a separate message]
With your addition, ChardevFile now inherits from ChardevCommon, so we gain:
Quoted 8 lines
Quoted 3 lines
{ "execute": "chardev-add", "arguments": { "id": "foo", "backend": { "type": "file", "data": { "logfile": "logname", "out": "filename" } } } }Ok good that matches what I intended - namely that 'logfile' should appear at the same dict as the rest of the backend fields, to mirror the the fact that the C struct had all the common fields in the same struct too.
Or in C terms, your proposal is:
struct ChardevCommon {
char *logname; ...
};
struct ChardevFile {
/* inherited fields from ChardevCommon */
char *logname; ...
/* own fields */
char *out; ...
};
struct ChardevBackend {
ChardevBackendKind type;
union {
ChardevFile *file; ...
} u;
};Each branch of ChardevBackend.u then has an upcast function qapi_ChardevFile_base() that gets you to a ChardevCommon pointer.
Quoted 11 lines
Quoted 11 lines
Re-writing the existing ChardevBackend to a semantically-identical flat union would be (using my shorthand syntax for anonymous base [1] and anonymous branch wrappers [2]):
Your proposal, then, is that the new 'logging' fields in your ChardevCommon should be made part of the base type of the 'ChardevBackend' union; which would be spelled as:
Quoted 7 lines
Quoted 7 lines
{ 'enum': 'ChardevType', 'data': [ 'file', 'serial' ] } { 'struct': 'ChardevCommon', 'data': {
'type': 'ChardevType', '*logfile': 'str', '*logappend': bool } }
{ 'union': 'ChardevBackend',
'base': 'ChardevCommon', 'discriminator': 'type', 'data': { 'file': { 'data': 'ChardevFile' },'serial': { 'data': 'ChardevHostdev' } } }
In C terms, this one would be:
struct ChardevCommon {
char *logname; ...
};
struct ChardevFile {
char *out; ...
};
struct ChardevBackend {
/* inherited fields from ChardevCommon */
char *logname; ...
/* own fields */
ChardevBackendKind type;
union {
ChardevFile *file; ...
} u;
};Here, you can pass ChardevBackend* directly (and access backend->logname, regardless of which union branch is in use).
Quoted 11 lines
Quoted 5 lines
So, depending on which of those three places we want to stick the new parameters determines which approach we should use for exposing them in qapi.
From the QMP representation POV, my preference is for the current
design since I think the 'logfile' attribute is really just another one of the backend config attributes. The choice to have a ChardevCommon struct was just a mechanism to avoid having to repeat the 'logfile' parameter in every single Chardev* backend type. This naturally matches the C-struct, where the ChardevCommon fields get directly added to the ChardevFile, ChardevSocket, etc structs.
Yep - the decision is up to you whether to add it to each struct used as a branch of ChardevBackend (and each caller then upcasts and passes a ChardevCommon* to the common code), or whether to add it directly to ChardevBackend (and each caller merely passes a ChardevBackend*).
Quoted 7 lines
So from that POV, I'd be against, pushing the 'logfile' field up either 1 or 2 levels.
Regards,
Daniel
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Quoted 24 lines · Daniel P. Berrange
Quoted 23 lines · Eric Blake
Quoted 7 lines · Daniel P. Berrange
Typically a UNIX guest OS will log boot messages to a serial port in addition to any graphical console. An admin user may also wish to use the serial port for an interactive console. A virtualization management system may wish to collect system boot messages by logging the serial port, but also wish to allow admins interactive access.
[code review, if we go with this design; see my other message for 2 possible alternative qapi designs that may supersede this code review]
qapi-schema.json line 3273
Quoted 4 lines
##
qapi-schema.json +2
-1-{ 'struct': 'ChardevDummy', 'data': { } } +{ 'struct': 'ChardevDummy', 'data': { }, + 'base': 'ChardevCommon' }Instead of changing ChardevDummy, you could have deleted this type and done:
Quoted 3 lines
{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile',
'serial' : 'ChardevHostdev',
Quoted 8 lines
Quoted 5 lines
...
'pty': 'ChardevCommon', 'null': 'ChardevCommon',and so on. I don't know which is better.
Yep, me neither. Given the name it felt like some kind of placeholder for future work, so I left it alone.
ChardevDummy exists because we have no way (yet) to represent an empty type as a union branch. That is, since you can't do:
'pty': {},
we had to instead create a named empty type. But now that we have a non-empty type, I see no real reason to keep the name, and no reason to have a subclass that adds no additional fields; so dropping ChardevDummy is my recommendation.
Quoted 8 lines
Quoted 7 lines
The other thing we could do here is have
qemu_chr_open_common()take a ChardevCommon* instead of a ChardevBackend*. Then each caller would do an appropriate upcast before calling the common code:ChardevCommon *common =
qapi_ChardevDummy_base(backend->u.null); if (qemu_chr_open_common(chr, common, errp){
Quoted 9 lines
Yep, I think this is the easiest thing todo, to avoid use of
backend->u.data.Quoted 3 lines
and so forth. That feels a bit more type-safe (and less reliant on qapi layout guarantees) than trying to rely on the
backend->u.datathat I'm trying to get rid of.Agreed
Okay, I think having each branch be a subclass of ChardevCommon (and not ChardevBackend using ChardevCommon as a base) sounds like the way to go, and now it's up to v3 to rework the clients to be a bit more typesafe.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+++ b/qemu-options.hx
(quoted diff) +0
-0@@ -2034,40 +2034,43 @@ The general form of a character device option is: ETEXI
Quoted 8 lines
Quoted 6 lines
qemu-options.hx The general form of a character device option is: · line 2037
Quoted 3 lines
qemu-options.hx +1
-1DEF("chardev", HAS_ARG, QEMU_OPTION_chardev, - "-chardev null,id=id[,mux=on|off]\n" + "-chardev null,id=id[,mux=on|off][,logfile=PATH][,logappend=on|off]\n"Do we want to allow logappend=on even when logfile is unspecified, or should we make that an error?
I figured it was harmless to just ignore it.
Works for me.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org