WIP: chardev: qom-ify
Subsystems (5)
cover WIP: chardev: qom-ify
Hi,
This is a followup of the series "char: fixes and improvements", where I started to refactor a bit the code to allow qom-ification. Paolo quickly reviewed some of those patches already.
qemu-char.c is quite a large file (~130k, 5000 loc) with may chardev and a lot of #ifdef. It doesn't use qemu Object. Using Object hopefully brings cleaner, more consitent code base. It helps to split the various backends in different files. Eventually, we could also allow or switch to -object-add interface.
Note: this series has been tested with Linux and cross-building with mingw. It mostly breaks on other platforms, help welcome for testing. I'd also like to write more chardev backend tests to ensure no regression (test-char.c is quite limited for now)
Comments and testing welcome!
Marc-André Lureau (54):
gtk: avoid oob array access
char: use a const CharDriver
char: use a static array for backends
char: move callbacks in CharDriver
char: fold single-user functions in caller
char: introduce generic qemu_chr_get_kind()
char: use a feature bit for replay
char: allocate CharDriverState as a single object
bt: use qemu_chr_alloc()
char: rename CharDriverState Chardev
char: rename TCPChardev and NetChardev
spice-char: improve error reporting
char: use error_report()
gtk: overwrite the console.c char driver
chardev: qom-ify
spice-qemu-char: convert to finalize
baum: convert to finalize
msmouse: convert to finalize
mux: convert to finalize
char-udp: convert to finalize
char-socket: convert to finalize
char-pty: convert to finalize
char-ringbuf: convert to finalize
char-parallel: convert parallel to finalize
char-stdio: convert to finalize
char-win-stdio: convert to finalize
char-win: do not override chr_free
char-win: convert to finalize
char-fd: convert to finalize
char: remove chr_free
char: get rid of CharDriver
char: remove class kind field
char: move to chardev/
char: create chardev-obj-y
char: make null_chr_write() the default method
char: move null chardev to its own file
char: move mux to its own file
char: move ringbuf/memory to its own file
char: rename and move to header CHR_READ_BUF_LEN
char: remove unused READ_RETRIES
char: move QIOChannel-related in char-io.h
char: move fd chardev in its own file
char: move win chardev base class in its own file
char: move win-stdio into its own file
char: move socket chardev to itw own file
char: move udp chardev in its own file
char: move file chardev in its own file
char: move stdio in its own file
char: move console in its own file
char: move pipe chardev in its own file
char: move pty chardev in its own file
char: move serial chardev to itw own file
char: move parallel chardev in its own file
char: headers clean-up
backends/baum.c | 97 +- backends/msmouse.c | 72 +- backends/rng-egd.c | 4 +- backends/testdev.c | 48 +- chardev/char-console.c | 30 + chardev/char-fd.c | 147 ++ chardev/char-file.c | 115 + chardev/char-io.c | 168 ++ chardev/char-mux.c | 332 +++ chardev/char-null.c | 31 + chardev/char-parallel.c | 292 +++ chardev/char-pipe.c | 167 ++ chardev/char-pty.c | 277 +++ chardev/char-ringbuf.c | 225 ++ chardev/char-serial.c | 294 +++ chardev/char-socket.c | 995 ++++++++ chardev/char-stdio.c | 140 ++ chardev/char-udp.c | 211 ++ chardev/char-win-stdio.c | 243 ++ chardev/char-win.c | 245 ++ chardev/char.c | 1311 ++++++++++ gdbstub.c | 39 +- hw/arm/fsl-imx25.c | 2 +- hw/arm/fsl-imx31.c | 2 +- hw/arm/fsl-imx6.c | 2 +- hw/arm/nseries.c | 2 +- hw/arm/omap2.c | 2 +- hw/arm/pxa2xx.c | 2 +- hw/arm/virt.c | 2 +- hw/bt/hci-csr.c | 63 +- hw/char/escc.c | 2 +- hw/char/exynos4210_uart.c | 2 +- hw/char/imx_serial.c | 2 +- hw/char/mcf_uart.c | 4 +- hw/char/omap_uart.c | 6 +- hw/char/parallel.c | 2 +- hw/char/serial-isa.c | 2 +- hw/char/serial.c | 4 +- hw/char/sh_serial.c | 2 +- hw/char/spapr_vty.c | 2 +- hw/char/virtio-console.c | 2 +- hw/core/qdev-properties-system.c | 4 +- hw/display/milkymist-tmu2.c | 2 +- hw/display/sm501.c | 2 +- hw/isa/isa-bus.c | 2 +- hw/isa/pc87312.c | 2 +- hw/mips/mips_malta.c | 4 +- hw/misc/ivshmem.c | 2 +- hw/misc/milkymist-pfpu.c | 2 +- hw/usb/ccid-card-passthru.c | 2 +- hw/usb/dev-serial.c | 6 +- hw/usb/redirect.c | 4 +- monitor.c | 6 +- net/colo-compare.c | 4 +- net/filter-mirror.c | 4 +- net/slirp.c | 2 +- net/vhost-user.c | 10 +- qemu-char.c | 4956 ------------------------------------- qmp.c | 2 +- qtest.c | 2 +- replay/replay-char.c | 8 +- spice-qemu-char.c | 221 +- stubs/get-next-serial.c | 2 +- stubs/monitor-init.c | 2 +- stubs/replay.c | 4 +- tests/test-char.c | 10 +- tests/vhost-user-test.c | 4 +- ui/console.c | 104 +- ui/gtk.c | 82 +- vl.c | 12 +- xen-common-stub.c | 2 +- xen-common.c | 4 +- Makefile | 3 +- Makefile.objs | 4 +- Makefile.target | 3 + chardev/Makefile.objs | 17 + tests/Makefile.include | 6 +- MAINTAINERS | 2 +- chardev/char-fd.h | 21 + chardev/char-io.h | 24 + chardev/char-mux.h | 40 + chardev/char-parallel.h | 9 + chardev/char-serial.h | 12 + chardev/char-win-stdio.h | 6 + chardev/char-win.h | 30 + hw/lm32/lm32.h | 4 +- hw/lm32/milkymist-hw.h | 2 +- include/hw/arm/exynos4210.h | 2 +- include/hw/arm/omap.h | 6 +- include/hw/bt.h | 4 +- include/hw/char/cadence_uart.h | 2 +- include/hw/char/escc.h | 2 +- include/hw/char/pl011.h | 4 +- include/hw/char/serial.h | 4 +- include/hw/char/xilinx_uartlite.h | 2 +- include/hw/cris/etraxfs.h | 2 +- include/hw/devices.h | 2 +- include/hw/i386/pc.h | 2 +- include/hw/m68k/mcf.h | 4 +- include/hw/ppc/spapr_vio.h | 2 +- include/hw/qdev-properties.h | 2 +- include/hw/sh4/sh.h | 2 +- include/hw/sparc/grlib.h | 2 +- include/hw/xen/xen.h | 2 +- include/monitor/monitor.h | 2 +- include/qemu/typedefs.h | 2 +- include/sysemu/char.h | 153 +- include/sysemu/replay.h | 4 +- include/sysemu/sysemu.h | 4 +- include/ui/console.h | 2 + include/ui/gtk.h | 2 +- include/ui/qemu-spice.h | 2 +- 112 files changed, 6008 insertions(+), 5455 deletions(-) create mode 100644 chardev/char-console.c create mode 100644 chardev/char-fd.c create mode 100644 chardev/char-file.c create mode 100644 chardev/char-io.c create mode 100644 chardev/char-mux.c create mode 100644 chardev/char-null.c create mode 100644 chardev/char-parallel.c create mode 100644 chardev/char-pipe.c create mode 100644 chardev/char-pty.c create mode 100644 chardev/char-ringbuf.c create mode 100644 chardev/char-serial.c create mode 100644 chardev/char-socket.c create mode 100644 chardev/char-stdio.c create mode 100644 chardev/char-udp.c create mode 100644 chardev/char-win-stdio.c create mode 100644 chardev/char-win.c create mode 100644 chardev/char.c delete mode 100644 qemu-char.c create mode 100644 chardev/Makefile.objs create mode 100644 chardev/char-fd.h create mode 100644 chardev/char-io.h create mode 100644 chardev/char-mux.h create mode 100644 chardev/char-parallel.h create mode 100644 chardev/char-serial.h create mode 100644 chardev/char-win-stdio.h create mode 100644 chardev/char-win.h
Signature · 1 line
2.11.0
PATCH 1/54 gtk: avoid oob array access
When too many consoles are created, vcs[] may be write out-of-bounds.
--- ui/gtk.c | 5 +++++ 1 file changed, 5 insertions(+)
ui/gtk.c +5 -0
@@ -1696,6 +1696,11 @@ static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)ChardevCommon *common =qapi_ChardevVC_base (vc);CharDriverState *chr; +if (nb_vcs ==MAX_VCS ) { +error_setg (errp,"Maximum number of consoles reached" ); +return NULL ; + } + chr =qemu_chr_alloc (common, errp);if (!chr) {return NULL ;
Signature · 1 line
2.11.0
PATCH 2/54 char: use a const CharDriver
In reply to Marc-André LureauNo need to allocate & copy fileds, let's use static const struct instead.
--- backends/baum.c | 8 +++- backends/msmouse.c | 7 +++- backends/testdev.c | 7 +++- qemu-char.c | 100 ++++++++++++++++++++++++-------------------------- spice-qemu-char.c | 14 +++++-- ui/console.c | 9 +++-- include/sysemu/char.h | 19 +++++----- 7 files changed, 90 insertions(+), 74 deletions(-)
backends/baum.c +6 -2
@@ -686,8 +686,12 @@ fail_handle:static void register_types (void ) { -register_char_driver ("braille" ,CHARDEV_BACKEND_KIND_BRAILLE ,NULL , - chr_baum_init); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_BRAILLE , + .parse =NULL , .create = chr_baum_init + }; + +register_char_driver (&driver); }type_init (register_types);
backends/msmouse.c +5 -2
@@ -179,8 +179,11 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,static void register_types (void ) { -register_char_driver ("msmouse" ,CHARDEV_BACKEND_KIND_MSMOUSE ,NULL , - qemu_chr_open_msmouse); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_MSMOUSE , + .parse =NULL , .create = qemu_chr_open_msmouse + }; +register_char_driver (&driver); }type_init (register_types);
backends/testdev.c +5 -2
@@ -130,8 +130,11 @@ static CharDriverState *chr_testdev_init(const char *id,static void register_types (void ) { -register_char_driver ("testdev" ,CHARDEV_BACKEND_KIND_TESTDEV ,NULL , - chr_testdev_init); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_TESTDEV , + .parse =NULL , .create = chr_testdev_init + }; +register_char_driver (&driver); }type_init (register_types);
qemu-char.c +48 -52
141 lines not on this page · raw message
spice-qemu-char.c +10 -4
@@ -389,10 +389,16 @@ static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,static void register_types (void ) { -register_char_driver ("spicevmc" ,CHARDEV_BACKEND_KIND_SPICEVMC , - qemu_chr_parse_spice_vmc, qemu_chr_open_spice_vmc); -register_char_driver ("spiceport" ,CHARDEV_BACKEND_KIND_SPICEPORT , - qemu_chr_parse_spice_port, qemu_chr_open_spice_port); +static const CharDriver vmc_driver = { + .kind =CHARDEV_BACKEND_KIND_SPICEVMC , + .parse = qemu_chr_parse_spice_vmc, .create = qemu_chr_open_spice_vmc + }; +static const CharDriver port_driver = { + .kind =CHARDEV_BACKEND_KIND_SPICEPORT , + .parse = qemu_chr_parse_spice_port, .create = qemu_chr_open_spice_port + }; +register_char_driver (&vmc_driver); +register_char_driver (&port_driver); }type_init (register_types);
ui/console.c +6 -3
@@ -2180,12 +2180,15 @@ static const TypeInfo qemu_console_info = { .class_size = sizeof(QemuConsoleClass), }; -static void register_types (void ) { +static const CharDriver vc_driver = { + .kind =CHARDEV_BACKEND_KIND_VC , + .parse = qemu_chr_parse_vc, .create = vc_init + }; +type_register_static (&qemu_console_info); -register_char_driver ("vc" ,CHARDEV_BACKEND_KIND_VC , qemu_chr_parse_vc, - vc_init); +register_char_driver (&vc_driver); }type_init (register_types);
include/sysemu/char.h +10 -9
@@ -473,15 +473,16 @@ void qemu_chr_set_feature(CharDriverState *chr, CharDriverFeature feature);QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); -typedef void CharDriverParse (QemuOpts *opts,ChardevBackend *backend, -Error **errp); -typedef CharDriverState *CharDriverCreate (const char *id, -ChardevBackend *backend, -ChardevReturn *ret,bool *be_opened, -Error **errp); - -void register_char_driver (const char *name,ChardevBackendKind kind, -CharDriverParse *parse,CharDriverCreate *create); +typedef struct CharDriver { +ChardevBackendKind kind; +void (*parse)(QemuOpts *opts,ChardevBackend *backend,Error **errp); +CharDriverState *(*create)(const char *id, +ChardevBackend *backend, +ChardevReturn *ret,bool *be_opened, +Error **errp); +}CharDriver ; + +void register_char_driver (const CharDriver *driver);extern int term_escape_char;
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 02/54] char: use a const CharDriver
Quoted 1 line · Marc-André Lureau
No need to allocate & copy fileds, let's use static const struct
s/fileds/fields/
Quoted 16 lines
instead.
Signed-off-by Marc-André Lureau <[email protected]>--- backends/baum.c | 8 +++- backends/msmouse.c | 7 +++- backends/testdev.c | 7 +++- qemu-char.c | 100 ++++++++++++++++++++++++-------------------------- spice-qemu-char.c | 14 +++++-- ui/console.c | 9 +++-- include/sysemu/char.h | 19 +++++----- 7 files changed, 90 insertions(+), 74 deletions(-)+++
b/backends/baum.c
backends/baum.c fail_handle: · line 691
Quoted 8 lines
backends/baum.c +3
-2@@ -686,8 +686,12 @@ fail_handle:static void register_types (void ) { -register_char_driver ("braille" ,CHARDEV_BACKEND_KIND_BRAILLE ,NULL , - chr_baum_init); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_BRAILLE , + .parse =NULL , .create = chr_baum_init
Why did the "braille" string disappear? Oh, I see... [1]
No need to specify .parse, since C99 initialization guarantees zero-assignment to any field omitted.
I kind of prefer one struct member per line when doing C99 initialization, rather than bunching two in one line.
I also prefer a trailing comma, as then adding a new (non-zero) member initialization in a later patch is a one-line addition, rather than modifying an existing line to add a trailing comma.
backends/msmouse.c static CharDriverState *qemu_chr_open_msmouse(const char *id, · line 184
Quoted 10 lines
backends/msmouse.c +4
-2+++ b/backends/msmouse.c@@ -179,8 +179,11 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,static void register_types (void ) { -register_char_driver ("msmouse" ,CHARDEV_BACKEND_KIND_MSMOUSE ,NULL , - qemu_chr_open_msmouse); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_MSMOUSE , + .parse =NULL , .create = qemu_chr_open_msmouse
Looks like my comments are repeated throughout the patch.
Quoted 4 lines
qemu-char.c +1
-2-void register_char_driver (const char *name,ChardevBackendKind kind, -CharDriverParse *parse,CharDriverCreate *create) +void register_char_driver (const CharDriver *driver) {
qemu-char.c static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, · line 4101
Quoted 10 lines
qemu-char.c +1
-9-CharDriver *s; - - s =g_malloc0 (sizeof (*s)); - s->name =g_strdup (name); - s->kind = kind; - s->parse = parse; - s->create = create; - - backends =g_slist_append (backends, s); + backends =g_slist_append (backends, (void *)driver);
Might be worth a comment that this is casting away const (as my first reaction is "oh, you forgot that C allows automatic conversion of any pointer to void*")
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4126
Quoted 9 lines
}
CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
qemu-char.c +1
-1@@ -4139,7 +4123,7 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,fprintf (stderr,"Available chardev backend types:\n" );for (i = backends; i; i = i->next) { cd = i->data; -fprintf (stderr,"%s\n" , cd->name); +fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]);
...[1] Your series is already long, so don't feel like you have to do this, but: if I were working on it, I might have done the elimination of cd->name, the name parameter, and the use of ChardevBackendKind_lookup[] in one patch (getting rid of JUST the "braille" parameter and friends at the call sites), and then the const'ification of the remaining parameters in the second patch.
Review CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4127
Quoted 1 line
(quoted diff) +1
-0+++ b/include/sysemu/char.h
Laszlo's suggestion of the git order file would have promoted this part of the patch first :)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 02/54] char: use a const CharDriver
----- Original Message -----
Quoted 5 lines · Marc-André Lureau
Quoted 1 line
No need to allocate & copy fileds, let's use static const struct
s/fileds/fields/
ok
Quoted 31 lines
Quoted 17 lines
instead.
Signed-off-by Marc-André Lureau <[email protected]>--- backends/baum.c | 8 +++- backends/msmouse.c | 7 +++- backends/testdev.c | 7 +++- qemu-char.c | 100 ++++++++++++++++++++++++-------------------------- spice-qemu-char.c | 14 +++++-- ui/console.c | 9 +++-- include/sysemu/char.h | 19 +++++----- 7 files changed, 90 insertions(+), 74 deletions(-)+++
b/backends/baum.cbackends/baum.c fail_handle: · line 691
Quoted 8 lines
backends/baum.c +3
-2@@ -686,8 +686,12 @@ fail_handle:static void register_types (void ) { -register_char_driver ("braille" ,CHARDEV_BACKEND_KIND_BRAILLE ,NULL , - chr_baum_init); +static const CharDriver driver = { + .kind =CHARDEV_BACKEND_KIND_BRAILLE , + .parse =NULL , .create = chr_baum_initWhy did the "braille" string disappear? Oh, I see... [1]
No need to specify .parse, since C99 initialization guarantees zero-assignment to any field omitted.
Quoted 6 lines
I kind of prefer one struct member per line when doing C99 initialization, rather than bunching two in one line.
I also prefer a trailing comma, as then adding a new (non-zero) member initialization in a later patch is a one-line addition, rather than modifying an existing line to add a trailing comma.
ok fixed
Quoted 28 lines
backends/msmouse.c static CharDriverState *qemu_chr_open_msmouse(const · line 185
Quoted 13 lines
backends/msmouse.c +4
-2+++ b/backends/msmouse.c@@ -179,8 +179,11 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id, static void register_types(void) { - register_char_driver("msmouse", CHARDEV_BACKEND_KIND_MSMOUSE, NULL, -qemu_chr_open_msmouse ); + static const CharDriver driver = { + .kind = CHARDEV_BACKEND_KIND_MSMOUSE, + .parse = NULL, .create = qemu_chr_open_msmouseLooks like my comments are repeated throughout the patch.
Quoted 11 lines
(quoted diff) +0
-2-void register_char_driver(const char *name, ChardevBackendKind kind, - CharDriverParse *parse, CharDriverCreate*create)
qemu-char.c +1
-6+void register_char_driver (const CharDriver *driver) { -CharDriver *s; - - s =g_malloc0 (sizeof (*s)); - s->name =g_strdup (name); - s->kind = kind; - s->parse = parse;
Quoted 8 lines
qemu-char.c static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, · line 4101
Quoted 4 lines
qemu-char.c +1
-3- s->create = create; - - backends =g_slist_append (backends, s); + backends =g_slist_append (backends, (void *)driver);Might be worth a comment that this is casting away const (as my first reaction is "oh, you forgot that C allows automatic conversion of any pointer to void*")
ok (note: this going away the next patch)
Quoted 18 lines
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts · line 4127
Quoted 11 lines
}
CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
qemu-char.c +1
-1@@ -4139,7 +4123,7 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,fprintf (stderr,"Available chardev backend types:\n" );for (i = backends; i; i = i->next) { cd = i->data; -fprintf (stderr,"%s\n" , cd->name); +fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]);...[1] Your series is already long, so don't feel like you have to do this, but: if I were working on it, I might have done the elimination of
cd->name, the name parameter, and the use of ChardevBackendKind_lookup[] in one patch (getting rid of JUST the "braille" parameter and friends at the call sites), and then the const'ification of the remaining parameters in the second patch.
The series is pretty long already (second version even longer), I don't see much need to split this patch
Quoted 6 lines
Review CharDriverState *qemu_chr_new_from_opts(QemuOpts · line 4128
Quoted 2 lines
(quoted diff) +1
-0+++ b/include/sysemu/char.hLaszlo's suggestion of the git order file would have promoted this part of the patch first :)
done
thanks
PATCH 3/54 char: use a static array for backends
In reply to Marc-André LureauNumber and kinds of backends is known at compile-time, use a fixed-sized static array to simplify iterations & lookups.
--- qemu-char.c | 101 ++++++++++++++++++++++++++++---------------------- include/sysemu/char.h | 1 + 2 files changed, 58 insertions(+), 44 deletions(-)
qemu-char.c +57 -44
@@ -4094,20 +4094,20 @@ static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, } } -static GSList *backends; +static const CharDriver *backends[CHARDEV_BACKEND_KIND__MAX ];void register_char_driver (const CharDriver *driver) { - backends =g_slist_append (backends, (void *)driver); + backends[driver->kind] = driver; }CharDriverState *qemu_chr_new_from_opts (QemuOpts *opts,Error **errp) {Error *local_err =NULL ; -CharDriver *cd; +const CharDriver *cd;CharDriverState *chr; -GSList *i; +int i;ChardevReturn *ret =NULL ;ChardevBackend *backend;const char *id =qemu_opts_id (opts);
@@ -4121,9 +4121,14 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,if (is_help_option (qemu_opt_get (opts,"backend" ))) {fprintf (stderr,"Available chardev backend types:\n" ); -for (i = backends; i; i = i->next) { - cd = i->data; -fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + cd = backends[i]; +if (cd) { +fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +if (cd->alias) { +fprintf (stderr,"%s\n" , cd->alias); + } + } }exit (!is_help_option (qemu_opt_get (opts,"backend" ))); }
@@ -4133,15 +4138,20 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, goto err; } -for (i = backends; i; i = i->next) { - cd = i->data; + cd =NULL ; +for (i =0 ; i <ARRAY_SIZE (backends); i++) { +const char *name =qemu_opt_get (opts,"backend" ); + cd = backends[i]; -if (strcmp (ChardevBackendKind_lookup[cd->kind], -qemu_opt_get (opts,"backend" )) ==0 ) { +if (!cd) { +continue ; + } +if (g_str_equal (ChardevBackendKind_lookup[cd->kind], name) || + (cd->alias &&g_str_equal (cd->alias, name))) {break ; } } -if (i ==NULL ) { +if (cd ==NULL ) {error_setg (errp,"chardev: backend \"%s\" not found" ,qemu_opt_get (opts,"backend" )); goto err;
@@ -4346,20 +4356,35 @@ ChardevInfoList *qmp_query_chardev(Error **errp)return chr_list; } +static ChardevBackendInfoList * +qmp_prepend_backend (ChardevBackendInfoList *list,const CharDriver *c, +const char *name) +{ +ChardevBackendInfoList *info =g_malloc0 (sizeof (*info)); + info->value =g_malloc0 (sizeof (*info->value)); + info->value->name =g_strdup (name); + info->next = list; +return info; + +} +ChardevBackendInfoList *qmp_query_chardev_backends (Error **errp) {ChardevBackendInfoList *backend_list =NULL ; -CharDriver *c =NULL ; -GSList *i =NULL ; +const CharDriver *c; +int i; -for (i = backends; i; i = i->next) { -ChardevBackendInfoList *info =g_malloc0 (sizeof (*info)); - c = i->data; - info->value =g_malloc0 (sizeof (*info->value)); - info->value->name =g_strdup (ChardevBackendKind_lookup[c->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + c = backends[i]; +if (!c) { +continue ; + } - info->next = backend_list; - backend_list = info; + backend_list =qmp_prepend_backend (backend_list, c, + ChardevBackendKind_lookup[c->kind]); +if (c->alias) { + backend_list =qmp_prepend_backend (backend_list, c, c->alias); + } }return backend_list;
@@ -4815,9 +4840,8 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, {ChardevReturn *ret =g_new0 (ChardevReturn,1 );CharDriverState *chr =NULL ; +const CharDriver *cd;Error *local_err =NULL ; -GSList *i; -CharDriver *cd;bool be_opened = true; chr =qemu_chr_find (id);
@@ -4826,22 +4850,16 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, goto out_error; } -for (i = backends; i; i = i->next) { - cd = i->data; - -if (cd->kind == backend->type) { - chr = cd->create (id, backend, ret, &be_opened, &local_err); -if (local_err) { -error_propagate (errp, local_err); - goto out_error; - } -break ; - } + cd = (int )backend->type >=0 && backend->type <ARRAY_SIZE (backends) ? + backends[backend->type] :NULL ; +if (cd ==NULL ) { +error_setg (errp,"chardev backend not available" ); + goto out_error; } -if (chr ==NULL ) { -assert (!i); -error_setg (errp,"chardev backend not available" ); + chr = cd->create (id, backend, ret, &be_opened, &local_err); +if (local_err) { +error_propagate (errp, local_err); goto out_error; }
@@ -4907,16 +4925,11 @@ static void register_types(void) { .kind = CHARDEV_BACKEND_KIND_STDIO, .parse = qemu_chr_parse_stdio, .create = qemu_chr_open_stdio }, #if defined HAVE_CHARDEV_SERIAL - { .kind = CHARDEV_BACKEND_KIND_SERIAL, - .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial }, - { .kind = CHARDEV_BACKEND_KIND_SERIAL, + { .kind = CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty", .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial }, #endif #ifdef HAVE_CHARDEV_PARPORT - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, - .parse = qemu_chr_parse_parallel, - .create = qmp_chardev_open_parallel }, - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, + { .kind = CHARDEV_BACKEND_KIND_PARALLEL, .alias = "parport", .parse = qemu_chr_parse_parallel, .create = qmp_chardev_open_parallel }, #endif
include/sysemu/char.h +1 -0
@@ -474,6 +474,7 @@ void qemu_chr_set_feature(CharDriverState *chr,QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); typedef struct CharDriver { + const char *alias; ChardevBackendKind kind; void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); CharDriverState *(*create)(const char *id,
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 03/54] char: use a static array for backends
Quoted 15 lines · Marc-André Lureau
Number and kinds of backends is known at compile-time, use a fixed-sized static array to simplify iterations & lookups.
Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 101 ++++++++++++++++++++++++++++---------------------- include/sysemu/char.h | 1 + 2 files changed, 58 insertions(+), 44 deletions(-)qemu-char.c +0
-1@@ -4121,9 +4121,14 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,if (is_help_option (qemu_opt_get (opts,"backend" ))) {fprintf (stderr,"Available chardev backend types:\n" ); -for (i = backends; i; i = i->next) {
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4131
Quoted 10 lines
qemu-char.c +8
-2- cd = i->data; -fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + cd = backends[i]; +if (cd) { +fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +if (cd->alias) { +fprintf (stderr,"%s\n" , cd->alias); + } + }
Where did cd->alias come from?
/me reads rest of patch
Oh, you added it in the .h file. All the more reason to use patch ordering to list .h changes first, but may also be worth a mention in the commit message that you add an alias field to cover the cases where we previously registered a driver twice under two names.
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4144
Quoted 6 lines
qemu-char.c +4
-1- cd = i->data; + cd =NULL ; +for (i =0 ; i <ARRAY_SIZE (backends); i++) { +const char *name =qemu_opt_get (opts,"backend" ); + cd = backends[i];
Please hoist the computation of 'name' outside of the loop...
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4150
Quoted 7 lines
qemu-char.c +5
-2-if (strcmp (ChardevBackendKind_lookup[cd->kind], -qemu_opt_get (opts,"backend" )) ==0 ) { +if (!cd) { +continue ; + } +if (g_str_equal (ChardevBackendKind_lookup[cd->kind], name) || + (cd->alias &&g_str_equal (cd->alias, name))) {
Doesn't g_str_equal(NULL, "str") do the right thing without crashing? Therefore, no need to check if cd->alias is non-NULL.
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4156
Quoted 7 lines
break; } }qemu-char.c +1
-1-if (i ==NULL ) { +if (cd ==NULL ) {error_setg (errp,"chardev: backend \"%s\" not found" ,qemu_opt_get (opts,"backend" ));
...and then reuse 'name' here as well, rather than making multiple repeated calls to qemu_opt_get().
Quoted 15 lines
ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
{ ChardevBackendInfoList *backend_list = NULL;qemu-char.c +4
-7-CharDriver *c =NULL ; -GSList *i =NULL ; +const CharDriver *c; +int i; -for (i = backends; i; i = i->next) { -ChardevBackendInfoList *info =g_malloc0 (sizeof (*info)); - c = i->data; - info->value =g_malloc0 (sizeof (*info->value)); - info->value->name =g_strdup (ChardevBackendKind_lookup[c->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + c = backends[i];
qemu-char.c ChardevInfoList *qmp_query_chardev(Error **errp) · line 4386
Quoted 10 lines
qemu-char.c +7
-2+if (!c) { +continue ; + } - info->next = backend_list; - backend_list = info; + backend_list =qmp_prepend_backend (backend_list, c, + ChardevBackendKind_lookup[c->kind]); +if (c->alias) { + backend_list =qmp_prepend_backend (backend_list, c, c->alias);
The help text now lists aliases immediately after the canonical name, while the QMP command now prepends aliases prior to the canonical name. It doesn't affect correctness, but should you try to make the two lists appear in the same order? (Presumably by prepending the alias first, not second.)
qemu-char.c static void register_types(void) · line 4929
Quoted 9 lines
qemu-char.c +1
-3@@ -4907,16 +4925,11 @@ static void register_types(void) { .kind = CHARDEV_BACKEND_KIND_STDIO, .parse = qemu_chr_parse_stdio, .create = qemu_chr_open_stdio }, #if defined HAVE_CHARDEV_SERIAL - { .kind = CHARDEV_BACKEND_KIND_SERIAL, - .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial }, - { .kind = CHARDEV_BACKEND_KIND_SERIAL, + { .kind = CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty", .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial },
Wait. How did patch 2/54 work? Or did you temporarily break the 'tty' alias in that patch, and then fix it here? All the more reason that my review of 2/54 complaining about getting rid of the cd->name field should be a separate patch; now I'm convinced of it. But in that patch, I suggested the order:
remove cd->name field as redundant with enum array lookup convert to struct without cd->name
Whereas with this patch in the mix, the steps should probably be:
convert to struct that still has cd->name
add cd->alias to have multiple names
remove cd->name field as redundant with enum array lookup
qemu-char.c static void register_types(void) · line 4934
Quoted 9 lines
qemu-char.c +1
-4#endif #ifdef HAVE_CHARDEV_PARPORT - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, - .parse = qemu_chr_parse_parallel, - .create = qmp_chardev_open_parallel }, - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, + { .kind = CHARDEV_BACKEND_KIND_PARALLEL, .alias = "parport", .parse = qemu_chr_parse_parallel, .create = qmp_chardev_open_parallel },
Again, another breakage in 2/54.
Quoted 7 lines
(quoted diff) +0
-0#endifinclude/sysemu/char.h +0
-0
include/sysemu/char.h void qemu_chr_set_feature(CharDriverState *chr, · line 480
Quoted 7 lines
include/sysemu/char.h +1
-0@@ -474,6 +474,7 @@ void qemu_chr_set_feature(CharDriverState *chr,QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); typedef struct CharDriver { + const char *alias; ChardevBackendKind kind; void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); CharDriverState *(*create)(const char *id,
Overall, I like the direction this is headed in.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 03/54] char: use a static array for backends
Hi
----- Original Message -----
Quoted 28 lines · Marc-André Lureau
Quoted 27 lines
Number and kinds of backends is known at compile-time, use a fixed-sized static array to simplify iterations & lookups.
Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 101 ++++++++++++++++++++++++++++---------------------- include/sysemu/char.h | 1 + 2 files changed, 58 insertions(+), 44 deletions(-)qemu-char.c +7
-3@@ -4121,9 +4121,14 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, if (is_help_option(qemu_opt_get(opts, "backend"))) {fprintf (stderr,"Available chardev backend types:\n" ); -for (i = backends; i; i = i->next) { - cd = i->data; -fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + cd = backends[i]; +if (cd) { +fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +if (cd->alias) { +fprintf (stderr,"%s\n" , cd->alias); + }
Quoted 8 lines
Review CharDriverState *qemu_chr_new_from_opts(QemuOpts · line 4133
Quoted 1 line
(quoted diff) +1
-0+ }Where did
cd->aliascome from?
/me reads rest of patch
Oh, you added it in the .h file. All the more reason to use patch ordering to list .h changes first, but may also be worth a mention in the commit message that you add an alias field to cover the cases where we previously registered a driver twice under two names.
ok
Quoted 18 lines
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4144
Quoted 7 lines
qemu-char.c +4
-1- cd = i->data; + cd =NULL ; +for (i =0 ; i <ARRAY_SIZE (backends); i++) { +const char *name =qemu_opt_get (opts,"backend" ); + cd = backends[i];Please hoist the computation of 'name' outside of the loop...
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4150
Quoted 7 lines
qemu-char.c +5
-2-if (strcmp (ChardevBackendKind_lookup[cd->kind], -qemu_opt_get (opts,"backend" )) ==0 ) { +if (!cd) { +continue ; + } +if (g_str_equal (ChardevBackendKind_lookup[cd->kind], name) || + (cd->alias &&g_str_equal (cd->alias, name))) {
Quoted 2 lines
Doesn't
g_str_equal(NULL, "str")do the right thing without crashing? Therefore, no need to check ifcd->aliasis non-NULL.
No it doesnt, but g_strcmp0 does, let's switch to it.
Quoted 11 lines
qemu-char.c CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts, · line 4156
Quoted 8 lines
break; } }qemu-char.c +1
-1-if (i ==NULL ) { +if (cd ==NULL ) {error_setg (errp,"chardev: backend \"%s\" not found" ,qemu_opt_get (opts,"backend" ));...and then reuse 'name' here as well, rather than making multiple repeated calls to
qemu_opt_get().
ok
Quoted 28 lines
Quoted 25 lines
ChardevBackendInfoList *qmp_query_chardev_backends(Error **errp)
{ ChardevBackendInfoList *backend_list = NULL;qemu-char.c +9
-9-CharDriver *c =NULL ; -GSList *i =NULL ; +const CharDriver *c; +int i; -for (i = backends; i; i = i->next) { -ChardevBackendInfoList *info =g_malloc0 (sizeof (*info)); - c = i->data; - info->value =g_malloc0 (sizeof (*info->value)); - info->value->name =g_strdup (ChardevBackendKind_lookup[c->kind]); +for (i =0 ; i <ARRAY_SIZE (backends); i++) { + c = backends[i]; +if (!c) { +continue ; + } - info->next = backend_list; - backend_list = info; + backend_list = qmp_prepend_backend(backend_list, c, +ChardevBackendKind_lookup[c->kind]);
qemu-char.c ChardevInfoList *qmp_query_chardev(Error **errp) · line 4386
Quoted 2 lines
qemu-char.c +2
-0+if (c->alias) { + backend_list =qmp_prepend_backend (backend_list, c, c->alias);
Quoted 6 lines
The help text now lists aliases immediately after the canonical name, while the QMP command now prepends aliases prior to the canonical name. It doesn't affect correctness, but should you try to make the two lists appear in the same order? (Presumably by prepending the alias first, not second.)
I don't think it matters. Furthermore, this code is temporary, the "char: get rid of CharDriver" patch will use the same function qmp_query_chardev_backends() to build the list with object_class_get_list() order.
Quoted 6 lines
Quoted 6 lines
qemu-char.c +0
-2@@ -4907,16 +4925,11 @@ static void register_types(void) { .kind = CHARDEV_BACKEND_KIND_STDIO, .parse = qemu_chr_parse_stdio, .create = qemu_chr_open_stdio },#if definedHAVE_CHARDEV_SERIAL - { .kind = CHARDEV_BACKEND_KIND_SERIAL, - .parse = qemu_chr_parse_serial, .create =
Quoted 8 lines
Review static void register_types(void) · line 4931
Quoted 5 lines
(quoted diff) +1
-1qmp_chardev_open_serial }, - { .kind = CHARDEV_BACKEND_KIND_SERIAL, + { .kind = CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty", .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial },Wait. How did patch 2/54 work? Or did you temporarily break the 'tty' alias in that patch, and then fix it here? All the more reason that my
No, it shouldn't break. There were 2 CharDrivers in case of alias chardev. It uses the "kind" field only to lookup the corresponding name.
Quoted 4 lines
review of 2/54 complaining about getting rid of the
cd->namefield should be a separate patch; now I'm convinced of it. But in that patch, I suggested the order:
Quoted 9 lines
remove
cd->namefield as redundant with enum array lookup convert to struct withoutcd->nameWhereas with this patch in the mix, the steps should probably be:
convert to struct that still has
cd->name
addcd->aliasto have multiple names
removecd->namefield as redundant with enum array lookup
Thus I don't see much point in changing the order.
Quoted 11 lines
Quoted 9 lines
qemu-char.c +1
-4#endif #ifdef HAVE_CHARDEV_PARPORT - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, - .parse = qemu_chr_parse_parallel, - .create = qmp_chardev_open_parallel }, - { .kind = CHARDEV_BACKEND_KIND_PARALLEL, + { .kind = CHARDEV_BACKEND_KIND_PARALLEL, .alias = "parport", .parse = qemu_chr_parse_parallel,.create =
qmp_chardev_open_parallel},Again, another breakage in 2/54.
neither
Quoted 23 lines
Quoted 8 lines
#endif
include/sysemu/char.h +0
-0include/sysemu/char.h void qemu_chr_set_feature(CharDriverState *chr, · line 480
Quoted 7 lines
include/sysemu/char.h +1
-0@@ -474,6 +474,7 @@ void qemu_chr_set_feature(CharDriverState *chr,QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); typedef struct CharDriver { + const char *alias; ChardevBackendKind kind; void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); CharDriverState *(*create)(const char *id,Overall, I like the direction this is headed in.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
PATCH 4/54 char: move callbacks in CharDriver
In reply to Marc-André LureauThis makes the code more declarative, and avoids to duplicate the information on all instances.
--- backends/baum.c | 13 +- backends/msmouse.c | 13 +- backends/testdev.c | 10 +- gdbstub.c | 7 +- hw/bt/hci-csr.c | 8 +- qemu-char.c | 427 +++++++++++++++++++++++++++++++------------------- spice-qemu-char.c | 36 +++-- ui/console.c | 26 +-- ui/gtk.c | 11 +- include/sysemu/char.h | 46 +++--- 10 files changed, 370 insertions(+), 227 deletions(-)
backends/baum.c +7 -6
@@ -637,7 +637,8 @@ static void baum_free(struct CharDriverState *chr)g_free (baum); } -static CharDriverState *chr_baum_init(const char *id, +static CharDriverState *chr_baum_init(const CharDriver *driver, + const char *id, ChardevBackend *backend, ChardevReturn *ret, bool *be_opened,
@@ -648,7 +649,7 @@ static CharDriverState *chr_baum_init(const char *id,CharDriverState *chr;brlapi_handle_t *handle; - chr =qemu_chr_alloc (common, errp); + chr =qemu_chr_alloc (driver, common, errp);if (!chr) {return NULL ; }
@@ -656,9 +657,6 @@ static CharDriverState *chr_baum_init(const char *id, baum->chr = chr; chr->opaque = baum; - chr->chr_write = baum_write; - chr->chr_accept_input = baum_accept_input; - chr->chr_free = baum_free; handle =g_malloc0 (brlapi_getHandleSize ()); baum->brlapi = handle;
@@ -688,7 +686,10 @@ static void register_types(void) {static const CharDriver driver = { .kind =CHARDEV_BACKEND_KIND_BRAILLE , - .parse =NULL , .create = chr_baum_init + .parse =NULL , .create = chr_baum_init, + .chr_write = baum_write, + .chr_accept_input = baum_accept_input, + .chr_free = baum_free, };register_char_driver (&driver);
backends/msmouse.c +7 -6
@@ -148,7 +148,8 @@ static QemuInputHandler msmouse_handler = { .sync = msmouse_input_sync, }; -static CharDriverState *qemu_chr_open_msmouse(const char *id, +static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver, + const char *id, ChardevBackend *backend, ChardevReturn *ret, bool *be_opened,
@@ -158,13 +159,10 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,MouseState *mouse;CharDriverState *chr; - chr =qemu_chr_alloc (common, errp); + chr =qemu_chr_alloc (driver, common, errp);if (!chr) {return NULL ; } - chr->chr_write = msmouse_chr_write; - chr->chr_free = msmouse_chr_free; - chr->chr_accept_input = msmouse_chr_accept_input; *be_opened = false; mouse =g_new0 (MouseState,1 );
@@ -181,7 +179,10 @@ static void register_types(void) {static const CharDriver driver = { .kind =CHARDEV_BACKEND_KIND_MSMOUSE , - .parse =NULL , .create = qemu_chr_open_msmouse + .parse =NULL , .create = qemu_chr_open_msmouse, + .chr_write = msmouse_chr_write, + .chr_accept_input = msmouse_chr_accept_input, + .chr_free = msmouse_chr_free, };register_char_driver (&driver); }
backends/testdev.c +6 -4
@@ -109,7 +109,8 @@ static void testdev_free(struct CharDriverState *chr)g_free (testdev); } -static CharDriverState *chr_testdev_init(const char *id, +static CharDriverState *chr_testdev_init(const CharDriver *driver, + const char *id, ChardevBackend *backend, ChardevReturn *ret, bool *be_opened,
@@ -121,9 +122,8 @@ static CharDriverState *chr_testdev_init(const char *id, testdev =g_new0 (TestdevCharState,1 ); testdev->chr = chr =g_new0 (CharDriverState,1 ); + chr->driver = driver; chr->opaque = testdev; - chr->chr_write = testdev_write; - chr->chr_free = testdev_free;return chr; }
@@ -132,7 +132,9 @@ static void register_types(void) {static const CharDriver driver = { .kind =CHARDEV_BACKEND_KIND_TESTDEV , - .parse =NULL , .create = chr_testdev_init + .parse =NULL , .create = chr_testdev_init, + .chr_write = testdev_write, + .chr_free = testdev_free, };register_char_driver (&driver); }
gdbstub.c +5 -2
@@ -1730,6 +1730,10 @@ int gdbserver_start(const char *device)CharDriverState *chr =NULL ;CharDriverState *mon_chr;ChardevCommon common = {0 }; +static const CharDriver driver = { + .kind =-1 , + .chr_write = gdb_monitor_write + };if (!device)return -1 ;
@@ -1762,8 +1766,7 @@ int gdbserver_start(const char *device)qemu_add_vm_change_state_handler (gdb_vm_state_change,NULL );/* Initialize a monitor terminal for gdb */ - mon_chr =qemu_chr_alloc (&common, &error_abort); - mon_chr->chr_write = gdb_monitor_write; + mon_chr =qemu_chr_alloc (&driver, &common, &error_abort);monitor_init (mon_chr,0 ); }else {if (qemu_chr_fe_get_driver (&s->chr)) {
hw/bt/hci-csr.c +6 -2
@@ -462,12 +462,16 @@ qemu_irq *csrhci_pins_get(CharDriverState *chr)CharDriverState *uart_hci_init (void ) { +static const CharDriver hci_driver = { + .kind =-1 , + .chr_write = csrhci_write, + .chr_ioctl = csrhci_ioctl, + };struct csrhci_s *s = (struct csrhci_s *)g_malloc0 (sizeof (struct csrhci_s )); s->chr.opaque = s; - s->chr.chr_write = csrhci_write; - s->chr.chr_ioctl = csrhci_ioctl; + s->chr.driver = &hci_driver; s->hci =qemu_next_hci (); s->hci->opaque = s;
qemu-char.c +269 -158
975 lines not on this page · raw message
spice-qemu-char.c +21 -15
@@ -260,16 +260,15 @@ static void spice_chr_accept_input(struct CharDriverState *chr)spice_server_char_device_wakeup (&s->sin); } -static CharDriverState *chr_open (const char *subtype, -void (*set_fe_open)(struct CharDriverState *, -int ), +static CharDriverState *chr_open (const CharDriver *driver, +const char *subtype,ChardevCommon *backend,Error **errp) {CharDriverState *chr;SpiceCharDriver *s; - chr =qemu_chr_alloc (backend, errp); + chr =qemu_chr_alloc (driver, backend, errp);if (!chr) {return NULL ; }
@@ -278,18 +277,14 @@ static CharDriverState *chr_open(const char *subtype, s->active = false; s->sin.subtype =g_strdup (subtype); chr->opaque = s; - chr->chr_write = spice_chr_write; - chr->chr_add_watch = spice_chr_add_watch; - chr->chr_free = spice_chr_free; - chr->chr_set_fe_open = set_fe_open; - chr->chr_accept_input = spice_chr_accept_input;QLIST_INSERT_HEAD (&spice_chars, s, next);return chr; } -static CharDriverState *qemu_chr_open_spice_vmc(const char *id, +static CharDriverState *qemu_chr_open_spice_vmc(const CharDriver *driver, + const char *id, ChardevBackend *backend, ChardevReturn *ret, bool *be_opened,
@@ -312,11 +307,12 @@ static CharDriverState *qemu_chr_open_spice_vmc(const char *id, } *be_opened = false; -return chr_open (type, spice_vmc_set_fe_open, common, errp); +return chr_open (driver, type, common, errp); }#if SPICE_SERVER_VERSION >=0x000c02 -static CharDriverState *qemu_chr_open_spice_port(const char *id, +static CharDriverState *qemu_chr_open_spice_port(const CharDriver *driver, + const char *id, ChardevBackend *backend, ChardevReturn *ret, bool *be_opened,
@@ -333,7 +329,7 @@ static CharDriverState *qemu_chr_open_spice_port(const char *id,return NULL ; } - chr =chr_open ("port" , spice_port_set_fe_open, common, errp); + chr =chr_open (driver,"port" , common, errp);if (!chr) {return NULL ; }
@@ -391,11 +387,21 @@ static void register_types(void) {static const CharDriver vmc_driver = { .kind =CHARDEV_BACKEND_KIND_SPICEVMC , - .parse = qemu_chr_parse_spice_vmc, .create = qemu_chr_open_spice_vmc + .parse = qemu_chr_parse_spice_vmc, .create = qemu_chr_open_spice_vmc, + .chr_write = spice_chr_write, + .chr_add_watch = spice_chr_add_watch, + .chr_set_fe_open = spice_vmc_set_fe_open, + .chr_accept_input = spice_chr_accept_input, + .chr_free = spice_chr_free, };static const CharDriver port_driver = { .kind =CHARDEV_BACKEND_KIND_SPICEPORT , - .parse = qemu_chr_parse_spice_port, .create = qemu_chr_open_spice_port + .parse = qemu_chr_parse_spice_port, .create = qemu_chr_open_spice_port, + .chr_write = spice_chr_write, + .chr_add_watch = spice_chr_add_watch, + .chr_set_fe_open = spice_port_set_fe_open, + .chr_accept_input = spice_chr_accept_input, + .chr_free = spice_chr_free, };register_char_driver (&vmc_driver);register_char_driver (&port_driver);
ui/console.c +16 -10
@@ -1040,6 +1040,10 @@ static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)QemuConsole *s = chr->opaque;int i; +if (!s->ds) { +return 0 ; + } + s->update_x0 = s->width *FONT_WIDTH ; s->update_y0 = s->height *FONT_HEIGHT ; s->update_x1 =0 ;
@@ -1989,8 +1993,6 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds) s = chr->opaque; - chr->chr_write = console_puts; - s->out_fifo.buf = s->out_fifo_buf; s->out_fifo.buf_size =sizeof (s->out_fifo_buf); s->kbd_timer =timer_new_ms (QEMU_CLOCK_REALTIME , kbd_send_chars, s);
@@ -2037,6 +2039,8 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)qemu_chr_be_generic_open (chr); } +static const CharDriver vc_driver; +static CharDriverState *text_console_init (ChardevVC *vc,Error **errp) {ChardevCommon *common =qapi_ChardevVC_base (vc);
@@ -2045,7 +2049,7 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)unsigned width =0 ;unsigned height =0 ; - chr =qemu_chr_alloc (common, errp); + chr =qemu_chr_alloc (&vc_driver, common, errp);if (!chr) {return NULL ; }
@@ -2078,7 +2082,6 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp) s->chr = chr; chr->opaque = s; - chr->chr_set_echo = text_console_set_echo;if (display_state) {text_console_do_init (chr, display_state);
@@ -2088,7 +2091,8 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)static VcHandler *vc_handler = text_console_init; -static CharDriverState *vc_init (const char *id,ChardevBackend *backend, +static CharDriverState *vc_init (const CharDriver *driver, +const char *id,ChardevBackend *backend,ChardevReturn *ret,bool *be_opened,Error **errp) {
@@ -2180,13 +2184,15 @@ static const TypeInfo qemu_console_info = { .class_size = sizeof(QemuConsoleClass), }; +static const CharDriver vc_driver = { + .kind =CHARDEV_BACKEND_KIND_VC , + .parse = qemu_chr_parse_vc, .create = vc_init, + .chr_write = console_puts, + .chr_set_echo = text_console_set_echo, +}; +static void register_types (void ) { -static const CharDriver vc_driver = { - .kind =CHARDEV_BACKEND_KIND_VC , - .parse = qemu_chr_parse_vc, .create = vc_init - }; -type_register_static (&qemu_console_info);register_char_driver (&vc_driver); }
ui/gtk.c +7 -4
@@ -1693,6 +1693,12 @@ static CharDriverState *vcs[MAX_VCS];static CharDriverState *gd_vc_handler (ChardevVC *vc,Error **errp) { +static const CharDriver gd_vc_driver = { + .kind =CHARDEV_BACKEND_KIND_VC , + .chr_write = gd_vc_chr_write, + .chr_set_echo = gd_vc_chr_set_echo, + }; +ChardevCommon *common =qapi_ChardevVC_base (vc);CharDriverState *chr;
@@ -1701,14 +1707,11 @@ static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)return NULL ; } - chr =qemu_chr_alloc (common, errp); + chr =qemu_chr_alloc (&gd_vc_driver, common, errp);if (!chr) {return NULL ; } - chr->chr_write = gd_vc_chr_write; - chr->chr_set_echo = gd_vc_chr_set_echo; -/* Temporary, until gd_vc_vte_init runs. */ chr->opaque =g_new0 (VirtualConsole,1 );
include/sysemu/char.h +26 -20
72 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 04/54] char: move callbacks in CharDriver
Quoted 1 line · Marc-André Lureau
This makes the code more declarative, and avoids to duplicate the
s/to duplicate/duplicating/
Quoted 14 lines
information on all instances.
Signed-off-by Marc-André Lureau <[email protected]>--- backends/baum.c | 13 +- backends/msmouse.c | 13 +- backends/testdev.c | 10 +- gdbstub.c | 7 +- hw/bt/hci-csr.c | 8 +- qemu-char.c | 427 +++++++++++++++++++++++++++++++------------------- spice-qemu-char.c | 36 +++-- ui/console.c | 26 +-- ui/gtk.c | 11 +- include/sysemu/char.h | 46 +++---
Again, seeing the .h changes first makes a huge difference on code review. I'm manually reformatting:
Quoted 7 lines
10 files changed, 370 insertions(+), 227 deletions(-)include/sysemu/char.h +0
-0
include/sysemu/char.h typedef struct CharBackend { · line 90
Quoted 9 lines
include/sysemu/char.h +3
-1@@ -85,24 +85,11 @@ typedef struct CharBackend {int fe_open; } CharBackend; +typedef struct CharDriver CharDriver ; +struct CharDriverState { +const CharDriver *driver;QemuMutex chr_write_lock; - int (*chr_write)(struct CharDriverState *s, const uint8_t *buf,
int len);
So all the callbacks are moved into the CharDriver struct...
include/sysemu/char.h struct CharDriverState { · line 116
Quoted 7 lines
include/sysemu/char.h +2
-1@@ -125,7 +112,8 @@ struct CharDriverState {* * Returns: a newly allocated CharDriverState, or NULL on error. */ -CharDriverState *qemu_chr_alloc (ChardevCommon *backend,Error **errp); +CharDriverState *qemu_chr_alloc (const CharDriver *driver, +ChardevCommon *backend,Error **errp);
...and all the entry points are now passed that struct as a new parameter.
include/sysemu/char.h void qemu_chr_set_feature(CharDriverState *chr, · line 464
Quoted 9 lines
include/sysemu/char.h +1
-1/** * @qemu_chr_new_from_opts: @@ -473,15 +461,33 @@ void qemu_chr_set_feature(CharDriverState *chr, CharDriverFeature feature);QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); -typedef struct CharDriver { +struct CharDriver {
...the struct already existed, but is now more useful.
Looks worthwhile. I suspect the rest of the patch is mechanical.
include/sysemu/char.h static void register_types(void) · line 689
Quoted 6 lines
include/sysemu/char.h +1
-1@@ -688,7 +686,10 @@ static void register_types(void) {static const CharDriver driver = { .kind =CHARDEV_BACKEND_KIND_BRAILLE , - .parse =NULL , .create = chr_baum_init + .parse =NULL , .create = chr_baum_init,
And now you see why I asked for trailing commas in 2/54 :)
Quoted 8 lines
include/sysemu/char.h +4
-0+ .chr_write = baum_write, + .chr_accept_input = baum_accept_input, + .chr_free = baum_free, }; +++ b/gdbstub.c
include/sysemu/char.h int gdbserver_start(const char *device) · line 1735
Quoted 6 lines
include/sysemu/char.h +3
-0@@ -1730,6 +1730,10 @@ int gdbserver_start(const char *device)CharDriverState *chr =NULL ;CharDriverState *mon_chr;ChardevCommon common = {0 }; +static const CharDriver driver = { + .kind =-1 , + .chr_write = gdb_monitor_write
Trailing comma.
Interesting that this is a new use of CharDriver with an out-of-range .kind. But I think your code in 3/54 was careful to explicitly handle a .kind that does not map to one of the public types, so that you are able to use this as an internal-only driver.
include/sysemu/char.h int gdbserver_start(const char *device) · line 1737
Quoted 3 lines
include/sysemu/char.h +2
-0+static const CharDriver null_driver = { + .kind =CHARDEV_BACKEND_KIND_NULL , .create = qemu_chr_open_null,
One initializer per line is fine.
Quoted 7 lines
include/sysemu/char.h +3
-0+ .chr_write = null_chr_write +}; +@@ -864,14 +880,6 @@ static CharDriverState *qemu_chr_open_mux(const char *id,
include/sysemu/char.h static CharDriverState *qemu_chr_open_mux(const char *id, · line 874
Quoted 10 lines
include/sysemu/char.h +0
-8chr->opaque = d; d->focus =-1 ; - chr->chr_free = mux_chr_free; - chr->chr_write = mux_chr_write; - chr->chr_accept_input = mux_chr_accept_input; -/* Frontend guest-open / -close notification is not support with muxes */ - chr->chr_set_fe_open =NULL ; -if (drv->chr_add_watch) { - chr->chr_add_watch = mux_chr_add_watch; - }
Here, the callback was only conditionally registered...
include/sysemu/char.h static CharDriverState *qemu_chr_open_mux(const char *id,
Quoted 8 lines
include/sysemu/char.h +8
-0+static const CharDriver mux_driver = { + .kind =CHARDEV_BACKEND_KIND_MUX , + .parse = qemu_chr_parse_mux, .create = qemu_chr_open_mux, + .chr_free = mux_chr_free, + .chr_write = mux_chr_write, + .chr_accept_input = mux_chr_accept_input, + .chr_add_watch = mux_chr_add_watch, +};
...but here, it is always registered. Is that an unintentional semantic change?
Quoted 29 lines
include/sysemu/char.h +16
-7+#ifdef HAVE_CHARDEV_SERIAL +static const CharDriver serial_driver = { + .alias ="tty" , .kind =CHARDEV_BACKEND_KIND_SERIAL , + .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial, +#ifdef _WIN32 + .chr_write = win_chr_write, + .chr_free = win_chr_free, +#else + .chr_add_watch = fd_chr_add_watch, + .chr_write = fd_chr_write, + .chr_update_read_handler = fd_chr_update_read_handler, + .chr_ioctl = tty_serial_ioctl, + .chr_free = qemu_chr_free_tty, +#endif +}; +#endif @@ -4910,49 +5037,33 @@ void qemu_chr_cleanup(void)static void register_types (void ) { -int i; -static const CharDriver drivers[] = { - { .kind =CHARDEV_BACKEND_KIND_NULL , .parse =NULL , - .create = qemu_chr_open_null }, - { .kind =CHARDEV_BACKEND_KIND_SOCKET , - .parse = qemu_chr_parse_socket, .create = qmp_chardev_open_socket }, - { .kind =CHARDEV_BACKEND_KIND_UDP , .parse = qemu_chr_parse_udp,
include/sysemu/char.h void qemu_chr_cleanup(void) · line 4929
Quoted 10 lines
include/sysemu/char.h +0
-10- .create = qmp_chardev_open_udp }, - { .kind = CHARDEV_BACKEND_KIND_RINGBUF, - .parse = qemu_chr_parse_ringbuf, .create = qemu_chr_open_ringbuf }, - { .kind = CHARDEV_BACKEND_KIND_FILE, - .parse = qemu_chr_parse_file_out, .create = qmp_chardev_open_file }, - { .kind = CHARDEV_BACKEND_KIND_STDIO, - .parse = qemu_chr_parse_stdio, .create = qemu_chr_open_stdio }, -#if defined HAVE_CHARDEV_SERIAL - { .kind = CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty", - .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial },
It feels like some code motion between 3/54 and 4/54 (you are moving where the CharDriver is declared); is it worth tweaking the series to avoid the code motion by declaring the structs in the right place to begin with? Not necessarily a show-stopper to the series, though.
include/sysemu/char.h void qemu_chr_cleanup(void) · line 5049
Quoted 10 lines
include/sysemu/char.h +9
-0+static const CharDriver *drivers[] = { + &null_driver, + &socket_driver, + &udp_driver, + &ringbuf_driver, + &file_driver, + &stdio_driver, +#ifdef HAVE_CHARDEV_SERIAL + &serial_driver, #endif
Overall impression is that I still like where this is headed.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 04/54] char: move callbacks in CharDriver
hi
----- Original Message -----
Quoted 4 lines · Marc-André Lureau
Quoted 1 line
This makes the code more declarative, and avoids to duplicate the
s/to duplicate/duplicating/
ok
Quoted 90 lines
Quoted 4 lines
information on all instances.
Signed-off-by Marc-André Lureau <[email protected]>Quoted 12 lines
--- backends/baum.c | 13 +- backends/msmouse.c | 13 +- backends/testdev.c | 10 +- gdbstub.c | 7 +- hw/bt/hci-csr.c | 8 +- qemu-char.c | 427 +++++++++++++++++++++++++++++++------------------- spice-qemu-char.c | 36 +++-- ui/console.c | 26 +-- ui/gtk.c | 11 +- include/sysemu/char.h | 46 +++---Again, seeing the .h changes first makes a huge difference on code review. I'm manually reformatting:
Quoted 7 lines
10 files changed, 370 insertions(+), 227 deletions(-)include/sysemu/char.h +0
-0include/sysemu/char.h typedef struct CharBackend { · line 90
Quoted 9 lines
include/sysemu/char.h +3
-1@@ -85,24 +85,11 @@ typedef struct CharBackend {int fe_open; } CharBackend; +typedef struct CharDriver CharDriver ; +struct CharDriverState { +const CharDriver *driver;QemuMutex chr_write_lock; - int (*chr_write)(struct CharDriverState *s, const uint8_t *buf,int len);
So all the callbacks are moved into the CharDriver struct...
include/sysemu/char.h struct CharDriverState { · line 116
Quoted 7 lines
include/sysemu/char.h +2
-1@@ -125,7 +112,8 @@ struct CharDriverState {* * Returns: a newly allocated CharDriverState, or NULL on error. */ -CharDriverState *qemu_chr_alloc (ChardevCommon *backend,Error **errp); +CharDriverState *qemu_chr_alloc (const CharDriver *driver, +ChardevCommon *backend,Error **errp);...and all the entry points are now passed that struct as a new parameter.
include/sysemu/char.h void qemu_chr_set_feature(CharDriverState *chr, · line 464
Quoted 9 lines
include/sysemu/char.h +1
-1/** * @qemu_chr_new_from_opts: @@ -473,15 +461,33 @@ void qemu_chr_set_feature(CharDriverState *chr, CharDriverFeature feature);QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); -typedef struct CharDriver { +struct CharDriver {...the struct already existed, but is now more useful.
Looks worthwhile. I suspect the rest of the patch is mechanical.
include/sysemu/char.h static void register_types(void) · line 689
Quoted 6 lines
include/sysemu/char.h +1
-1@@ -688,7 +686,10 @@ static void register_types(void) {static const CharDriver driver = { .kind =CHARDEV_BACKEND_KIND_BRAILLE , - .parse =NULL , .create = chr_baum_init + .parse =NULL , .create = chr_baum_init,And now you see why I asked for trailing commas in 2/54 :)
Quoted 8 lines
include/sysemu/char.h +4
-0+ .chr_write = baum_write, + .chr_accept_input = baum_accept_input, + .chr_free = baum_free, }; +++ b/gdbstub.cinclude/sysemu/char.h int gdbserver_start(const char *device) · line 1735
Quoted 6 lines
include/sysemu/char.h +3
-0@@ -1730,6 +1730,10 @@ int gdbserver_start(const char *device)CharDriverState *chr =NULL ;CharDriverState *mon_chr;ChardevCommon common = {0 }; +static const CharDriver driver = { + .kind =-1 , + .chr_write = gdb_monitor_writeTrailing comma.
Quoted 4 lines
Interesting that this is a new use of CharDriver with an out-of-range .kind. But I think your code in 3/54 was careful to explicitly handle a .kind that does not map to one of the public types, so that you are able to use this as an internal-only driver.
and kind is removed is later patches "char: remove class kind field"
Quoted 30 lines
include/sysemu/char.h int gdbserver_start(const char *device) · line 1737
Quoted 5 lines
include/sysemu/char.h +2
-0+static const CharDriver null_driver = { + .kind =CHARDEV_BACKEND_KIND_NULL , .create = qemu_chr_open_null,One initializer per line is fine.
Quoted 9 lines
include/sysemu/char.h +3
-0+ .chr_write = null_chr_write +}; +@@ -864,14 +880,6 @@ static CharDriverState *qemu_chr_open_mux(const char *id, chr->opaque = d;include/sysemu/char.h static CharDriverState *qemu_chr_open_mux(const char · line 876
Quoted 10 lines
include/sysemu/char.h +0
-8d->focus = -1; - chr->chr_free = mux_chr_free; - chr->chr_write = mux_chr_write; - chr->chr_accept_input = mux_chr_accept_input; -/* Frontend guest-open / -close notification is not support with muxes */ - chr->chr_set_fe_open =NULL ; -if (drv->chr_add_watch) { - chr->chr_add_watch = mux_chr_add_watch; - }Here, the callback was only conditionally registered...
Quoted 12 lines
include/sysemu/char.h static CharDriverState *qemu_chr_open_mux(const char
Quoted 8 lines
include/sysemu/char.h +8
-0+static const CharDriver mux_driver = { + .kind =CHARDEV_BACKEND_KIND_MUX , + .parse = qemu_chr_parse_mux, .create = qemu_chr_open_mux, + .chr_free = mux_chr_free, + .chr_write = mux_chr_write, + .chr_accept_input = mux_chr_accept_input, + .chr_add_watch = mux_chr_add_watch, +};...but here, it is always registered. Is that an unintentional semantic change?
mux_chr_add_watch() also checks if the underlying driver has chr_add_watch(). qemu_chr_fe_add_watch() returns 0 in both cases then.
Quoted 41 lines
Quoted 41 lines
include/sysemu/char.h +16
-15+#ifdef HAVE_CHARDEV_SERIAL +static const CharDriver serial_driver = { + .alias ="tty" , .kind =CHARDEV_BACKEND_KIND_SERIAL , + .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial, +#ifdef _WIN32 + .chr_write = win_chr_write, + .chr_free = win_chr_free, +#else + .chr_add_watch = fd_chr_add_watch, + .chr_write = fd_chr_write, + .chr_update_read_handler = fd_chr_update_read_handler, + .chr_ioctl = tty_serial_ioctl, + .chr_free = qemu_chr_free_tty, +#endif +}; +#endif @@ -4910,49 +5037,33 @@ void qemu_chr_cleanup(void)static void register_types (void ) { -int i; -static const CharDriver drivers[] = { - { .kind =CHARDEV_BACKEND_KIND_NULL , .parse =NULL , - .create = qemu_chr_open_null }, - { .kind =CHARDEV_BACKEND_KIND_SOCKET , - .parse = qemu_chr_parse_socket, .create = qmp_chardev_open_socket }, - { .kind =CHARDEV_BACKEND_KIND_UDP , .parse = qemu_chr_parse_udp, - .create = qmp_chardev_open_udp }, - { .kind =CHARDEV_BACKEND_KIND_RINGBUF , - .parse = qemu_chr_parse_ringbuf, .create = qemu_chr_open_ringbuf }, - { .kind =CHARDEV_BACKEND_KIND_FILE , - .parse = qemu_chr_parse_file_out, .create = qmp_chardev_open_file }, - { .kind =CHARDEV_BACKEND_KIND_STDIO , - .parse = qemu_chr_parse_stdio, .create = qemu_chr_open_stdio }, -#if defined HAVE_CHARDEV_SERIAL
Quoted 8 lines
include/sysemu/char.h void qemu_chr_cleanup(void) · line 5043
Quoted 3 lines
include/sysemu/char.h +0
-2- { .kind = CHARDEV_BACKEND_KIND_SERIAL, .alias = "tty", - .parse = qemu_chr_parse_serial, .create = qmp_chardev_open_serial },It feels like some code motion between 3/54 and 4/54 (you are moving where the CharDriver is declared); is it worth tweaking the series to avoid the code motion by declaring the structs in the right place to begin with? Not necessarily a show-stopper to the series, though.
I did place it where it felt right at the time I did the change. I don't think we need to care much because all this is going away in the series. No strong feeling though
Quoted 12 lines
include/sysemu/char.h void qemu_chr_cleanup(void) · line 5053
Quoted 11 lines
include/sysemu/char.h +9
-0+static const CharDriver *drivers[] = { + &null_driver, + &socket_driver, + &udp_driver, + &ringbuf_driver, + &file_driver, + &stdio_driver, +#ifdef HAVE_CHARDEV_SERIAL + &serial_driver, #endif
Quoted 1 line
Overall impression is that I still like where this is headed.
thanks
PATCH 5/54 char: fold single-user functions in caller
In reply to Marc-André LureauThis shorten a bit the code.
--- qemu-char.c | 97 ++++++++++++++++++++----------------------------------------- 1 file changed, 31 insertions(+), 66 deletions(-)
qemu-char.c +31 -66
@@ -1884,22 +1884,6 @@ static void qemu_chr_free_tty(CharDriverState *chr) {fd_chr_free (chr); } - -static CharDriverState *qemu_chr_open_tty_fd (const CharDriver *driver, -int fd, -ChardevCommon *backend, -bool *be_opened, -Error **errp) -{ -CharDriverState *chr; - -tty_serial_init (fd,115200 ,'N' ,8 ,1 ); - chr =qemu_chr_open_fd (driver, fd, fd, backend, errp); -if (!chr) { -return NULL ; - } -return chr; -}#endif /* __linux__ || __sun__ */ #if defined(__linux__)
@@ -2320,29 +2304,6 @@ static int win_chr_poll(void *opaque)return 0 ; } -static CharDriverState *qemu_chr_open_win_path (const CharDriver *driver, -const char *filename, -ChardevCommon *backend, -Error **errp) -{ -CharDriverState *chr; -WinCharState *s; - - chr =qemu_chr_alloc (driver, backend, errp); -if (!chr) { -return NULL ; - } - s =g_new0 (WinCharState,1 ); - chr->opaque = s; - -if (win_chr_init (chr, filename, errp) <0 ) { -g_free (s); -qemu_chr_free_common (chr); -return NULL ; - } -return chr; -} -static int win_chr_pipe_poll (void *opaque) {CharDriverState *chr = opaque;
@@ -2798,30 +2759,6 @@ static void udp_chr_free(CharDriverState *chr)qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); } -static CharDriverState *qemu_chr_open_udp (const CharDriver *driver, -QIOChannelSocket *sioc, -ChardevCommon *backend, -bool *be_opened, -Error **errp) -{ -CharDriverState *chr =NULL ; -NetCharDriver *s =NULL ; - - chr =qemu_chr_alloc (driver, backend, errp); -if (!chr) { -return NULL ; - } - s =g_new0 (NetCharDriver,1 ); - - s->ioc =QIO_CHANNEL (sioc); - s->bufcnt =0 ; - s->bufptr =0 ; - chr->opaque = s; -/* be isn't opened until we get a connection */ - *be_opened = false; -return chr; -} -/***********************************************************/ /* TCP Net console */
@@ -4604,8 +4541,23 @@ static CharDriverState *qmp_chardev_open_serial(const CharDriver *driver, {ChardevHostdev *serial = backend->u.serial.data;ChardevCommon *common =qapi_ChardevHostdev_base (serial); +CharDriverState *chr; +WinCharState *s; + + chr =qemu_chr_alloc (driver, common, errp); +if (!chr) { +return NULL ; + } + + s =g_new0 (WinCharState,1 ); + chr->opaque = s; +if (win_chr_init (chr, serial->device, errp) <0 ) { +g_free (s); +qemu_chr_free_common (chr); +return NULL ; + } -return qemu_chr_open_win_path (driver, serial->device, common, errp); +return chr; }#else /* WIN32 */
@@ -4674,8 +4626,9 @@ static CharDriverState *qmp_chardev_open_serial(const CharDriver *driver, return NULL; }qemu_set_nonblock (fd); +tty_serial_init (fd,115200 ,'N' ,8 ,1 ); -return qemu_chr_open_tty_fd (driver, fd, common, be_opened, errp); +return qemu_chr_open_fd (driver, fd, fd, common, errp); }#endif
@@ -4925,6 +4878,7 @@ static CharDriverState *qmp_chardev_open_udp(const CharDriver *driver,QIOChannelSocket *sioc =qio_channel_socket_new ();char *name;CharDriverState *chr; +NetCharDriver *s; if (qio_channel_socket_dgram_sync(sioc, udp->local, udp->remote,
@@ -4933,12 +4887,23 @@ static CharDriverState *qmp_chardev_open_udp(const CharDriver *driver,return NULL ; } - chr =qemu_chr_open_udp (driver, sioc, common, be_opened, errp); + chr =qemu_chr_alloc (driver, common, errp); +if (!chr) { +return NULL ; + } name =g_strdup_printf ("chardev-udp-%s" , chr->label);qio_channel_set_name (QIO_CHANNEL (sioc), name);g_free (name); + s =g_new0 (NetCharDriver,1 ); + s->ioc =QIO_CHANNEL (sioc); + s->bufcnt =0 ; + s->bufptr =0 ; + chr->opaque = s; +/* be isn't opened until we get a connection */ + *be_opened = false; +return chr; }
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 05/54] char: fold single-user functions in caller
Quoted 1 line · Marc-André Lureau
This shorten a bit the code.
Grammar:
This shortens the code a bit.
Quoted 11 lines
Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 97 ++++++++++++++++++++----------------------------------------- 1 file changed, 31 insertions(+), 66 deletions(-)name = g_strdup_printf("chardev-udp-%s", chr->label); qio_channel_set_name(QIO_CHANNEL(sioc), name); g_free(name);
qemu-char.c static CharDriverState *qmp_chardev_open_udp(const CharDriver *driver, · line 4905
Quoted 7 lines
qemu-char.c +7
-0+ s =g_new0 (NetCharDriver,1 ); + s->ioc =QIO_CHANNEL (sioc); + s->bufcnt =0 ; + s->bufptr =0 ; + chr->opaque = s; +/* be isn't opened until we get a connection */ + *be_opened = false;
The assignments to 0 and false are redundant with the g_new0(), if you want to drop them for even more code reduction.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 05/54] char: fold single-user functions in caller
----- Original Message -----
Quoted 26 lines · Marc-André Lureau
Quoted 1 line
This shorten a bit the code.
Grammar:
This shortens the code a bit.Quoted 12 lines
Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 97 ++++++++++++++++++++----------------------------------------- 1 file changed, 31 insertions(+), 66 deletions(-)name = g_strdup_printf("chardev-udp-%s", chr->label); qio_channel_set_name(QIO_CHANNEL(sioc), name); g_free(name);qemu-char.c static CharDriverState *qmp_chardev_open_udp(const CharDriver *driver, · line 4905
Quoted 7 lines
qemu-char.c +7
-0+ s =g_new0 (NetCharDriver,1 ); + s->ioc =QIO_CHANNEL (sioc); + s->bufcnt =0 ; + s->bufptr =0 ; + chr->opaque = s; +/* be isn't opened until we get a connection */ + *be_opened = false;
Quoted 3 lines
The assignments to 0 and false are redundant with the
g_new0(), if you want to drop them for even more code reduction.
thanks, done
Quoted 7 lines
Reviewed-by Eric Blake <[email protected]>--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
PATCH 6/54 char: introduce generic qemu_chr_get_kind()
In reply to Marc-André LureauThis allows to remove the "is_mux" field from CharDriverState.
--- monitor.c | 2 +- qemu-char.c | 21 ++++++++++----------- include/sysemu/char.h | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 14 deletions(-)
monitor.c +1 -1
@@ -3194,7 +3194,7 @@ static void ringbuf_completion(ReadLineState *rs, const char *str)if (!strncmp (chr_info->label, str, len)) {CharDriverState *chr =qemu_chr_find (chr_info->label); -if (chr &&chr_is_ringbuf (chr)) { +if (chr &&qemu_chr_is_ringbuf (chr)) {readline_add_completion (rs, chr_info->label); } }
qemu-char.c +10 -11
@@ -780,7 +780,7 @@ static void muxes_realize_done(Notifier *notifier, void *unused)CharDriverState *chr; QTAILQ_FOREACH(chr, &chardevs, next) { -if (chr->is_mux) { +if (qemu_chr_get_kind (chr) ==CHARDEV_BACKEND_KIND_MUX ) {MuxDriver *d = chr->opaque;int i;
@@ -884,7 +884,6 @@ static CharDriverState *qemu_chr_open_mux(const CharDriver *driver,* set of muxes */ *be_opened = muxes_realized; - chr->is_mux =1 ;if (!qemu_chr_fe_init (&d->chr, drv, errp)) {qemu_chr_free (chr);return NULL ;
@@ -902,7 +901,7 @@ bool qemu_chr_fe_init(CharBackend *b, CharDriverState *s, Error **errp) {int tag =0 ; -if (s->is_mux) { +if (qemu_chr_get_kind (s) ==CHARDEV_BACKEND_KIND_MUX ) {MuxDriver *d = s->opaque;if (d->mux_cnt >=MAX_MUX ) {
@@ -929,7 +928,7 @@ unavailable:static bool qemu_chr_is_busy (CharDriverState *s) { -if (s->is_mux) { +if (qemu_chr_get_kind (s) ==CHARDEV_BACKEND_KIND_MUX ) {MuxDriver *d = s->opaque;return d->mux_cnt >=0 ; }else {
@@ -944,7 +943,7 @@ void qemu_chr_fe_deinit(CharBackend *b)if (b->chr) {qemu_chr_fe_set_handlers (b,NULL ,NULL ,NULL ,NULL ,NULL , true); b->chr->be =NULL ; -if (b->chr->is_mux) { +if (qemu_chr_get_kind (b->chr) ==CHARDEV_BACKEND_KIND_MUX ) {MuxDriver *d = b->chr->opaque; d->backends[b->tag] =NULL ; }
@@ -995,7 +994,7 @@ void qemu_chr_fe_set_handlers(CharBackend *b, } } -if (s->is_mux) { +if (qemu_chr_get_kind (s) ==CHARDEV_BACKEND_KIND_MUX ) {mux_chr_set_handlers (s, context); } }
@@ -1006,7 +1005,7 @@ void qemu_chr_fe_take_focus(CharBackend *b)return ; } -if (b->chr->is_mux) { +if (qemu_chr_get_kind (b->chr) ==CHARDEV_BACKEND_KIND_MUX ) {mux_set_focus (b->chr->opaque, b->tag); } }
@@ -3549,9 +3548,9 @@ fail:return NULL ; } -bool chr_is_ringbuf (const CharDriverState *chr) +ChardevBackendKind qemu_chr_get_kind (const CharDriverState *chr) { -return chr->driver->chr_write == ringbuf_chr_write; +return chr->driver->kind; } void qmp_ringbuf_write(const char *device, const char *data,
@@ -3569,7 +3568,7 @@ void qmp_ringbuf_write(const char *device, const char *data,return ; } -if (!chr_is_ringbuf (chr)) { +if (!qemu_chr_is_ringbuf (chr)) {error_setg (errp,"%s is not a ringbuf device" , device);return ; }
@@ -3613,7 +3612,7 @@ char *qmp_ringbuf_read(const char *device, int64_t size,return NULL ; } -if (!chr_is_ringbuf (chr)) { +if (!qemu_chr_is_ringbuf (chr)) {error_setg (errp,"%s is not a ringbuf device" , device);return NULL ; }
include/sysemu/char.h +13 -2
@@ -96,7 +96,6 @@ struct CharDriverState {char *filename;int logfd;int be_open; -int is_mux;guint fd_in_tag;bool replay;DECLARE_BITMAP (features,QEMU_CHAR_FEATURE_LAST );
@@ -453,7 +452,19 @@ void qemu_chr_be_generic_open(CharDriverState *s);void qemu_chr_fe_accept_input (CharBackend *be);int qemu_chr_add_client (CharDriverState *s,int fd);CharDriverState *qemu_chr_find (const char *name); -bool chr_is_ringbuf (const CharDriverState *chr); + +/** +* @qemu_chr_get_kind: +* +* Returns the kind of char backend, or -1 if unspecified. +*/ +ChardevBackendKind qemu_chr_get_kind (const CharDriverState *chr); + +static inline bool qemu_chr_is_ringbuf (const CharDriverState *chr) +{ +return qemu_chr_get_kind (chr) ==CHARDEV_BACKEND_KIND_RINGBUF || +qemu_chr_get_kind (chr) ==CHARDEV_BACKEND_KIND_MEMORY ; +}bool qemu_chr_has_feature (CharDriverState *chr,CharDriverFeature feature);
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 06/54] char: introduce generic qemu_chr_get_kind()
Quoted 9 lines · Marc-André Lureau
This allows to remove the "
is_mux" field from CharDriverState.Signed-off-by Marc-André Lureau <[email protected]>--- monitor.c | 2 +- qemu-char.c | 21 ++++++++++----------- include/sysemu/char.h | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 14 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 7/54 char: use a feature bit for replay
In reply to Marc-André LureauUse a feature flag rather than a structure field for "replay".
--- qemu-char.c | 33 ++++++++++++++++++++------------- include/sysemu/char.h | 3 ++- 2 files changed, 22 insertions(+), 14 deletions(-)
qemu-char.c +20 -13
@@ -277,6 +277,11 @@ static int qemu_chr_fe_write_buffer(CharDriverState *s, const uint8_t *buf, intreturn res; } +static bool qemu_chr_replay (CharDriverState *chr) +{ +return qemu_chr_has_feature (chr,QEMU_CHAR_FEATURE_REPLAY ); +} +int qemu_chr_fe_write (CharBackend *be,const uint8_t *buf,int len) {CharDriverState *s = be->chr;
@@ -286,7 +291,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)return 0 ; } -if (s->replay && replay_mode ==REPLAY_MODE_PLAY ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_PLAY ) {int offset;replay_char_write_event_load (&ret, &offset);assert (offset <= len);
@@ -303,7 +308,7 @@ int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)qemu_mutex_unlock (&s->chr_write_lock); -if (s->replay && replay_mode ==REPLAY_MODE_RECORD ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_RECORD ) {replay_char_write_event_save (ret, ret <0 ?0 : ret); }
@@ -315,7 +320,7 @@ static int qemu_chr_write_all(CharDriverState *s, const uint8_t *buf, int len)int offset;int res; -if (s->replay && replay_mode ==REPLAY_MODE_PLAY ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_PLAY ) {replay_char_write_event_load (&res, &offset);assert (offset <= len);qemu_chr_fe_write_buffer (s, buf, offset, &offset);
@@ -324,7 +329,7 @@ static int qemu_chr_write_all(CharDriverState *s, const uint8_t *buf, int len) res =qemu_chr_fe_write_buffer (s, buf, len, &offset); -if (s->replay && replay_mode ==REPLAY_MODE_RECORD ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_RECORD ) {replay_char_write_event_save (res, offset); }
@@ -355,7 +360,7 @@ int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)return 0 ; } -if (s->replay && replay_mode ==REPLAY_MODE_PLAY ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_PLAY ) {return replay_char_read_all_load (buf); }
@@ -372,7 +377,7 @@ int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) }if (res <0 ) { -if (s->replay && replay_mode ==REPLAY_MODE_RECORD ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_RECORD ) {replay_char_read_all_save_error (res); }return res;
@@ -385,7 +390,7 @@ int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len) } } -if (s->replay && replay_mode ==REPLAY_MODE_RECORD ) { +if (qemu_chr_replay (s) && replay_mode ==REPLAY_MODE_RECORD ) {replay_char_read_all_save_buf (buf, offset); }return offset;
@@ -396,7 +401,7 @@ int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)CharDriverState *s = be->chr;int res; -if (!s || !s->driver->chr_ioctl || s->replay) { +if (!s || !s->driver->chr_ioctl ||qemu_chr_replay (s)) { res = -ENOTSUP ; }else { res = s->driver->chr_ioctl (s, cmd, arg);
@@ -427,7 +432,7 @@ void qemu_chr_be_write_impl(CharDriverState *s, uint8_t *buf, int len)void qemu_chr_be_write (CharDriverState *s,uint8_t *buf,int len) { -if (s->replay) { +if (qemu_chr_replay (s)) {if (replay_mode ==REPLAY_MODE_PLAY ) {return ; }
@@ -442,7 +447,7 @@ int qemu_chr_fe_get_msgfd(CharBackend *be)CharDriverState *s = be->chr;int fd;int res = (qemu_chr_fe_get_msgfds (be, &fd,1 ) ==1 ) ? fd :-1 ; -if (s && s->replay) { +if (s &&qemu_chr_replay (s)) {fprintf (stderr,"Replay: get msgfd is not supported for serial devices yet\n" );exit (1 );
@@ -4237,8 +4242,10 @@ CharDriverState *qemu_chr_new(const char *label, const char *filename)CharDriverState *chr; chr =qemu_chr_new_noreplay (label, filename);if (chr) { - chr->replay = replay_mode !=REPLAY_MODE_NONE ; -if (chr->replay && chr->driver->chr_ioctl) { +if (replay_mode !=REPLAY_MODE_NONE ) { +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_REPLAY ); + } +if (qemu_chr_replay (chr) && chr->driver->chr_ioctl) {fprintf (stderr,"Replay: ioctl is not supported for serial devices yet\n" ); }
@@ -4982,7 +4989,7 @@ void qmp_chardev_remove(const char *id, Error **errp)error_setg (errp,"Chardev '%s' is busy" , id);return ; } -if (chr->replay) { +if (qemu_chr_replay (chr)) {error_setg (errp,"Chardev '%s' cannot be unplugged in record/replay mode" , id);return ;
include/sysemu/char.h +2 -1
@@ -69,6 +69,8 @@ typedef enum {/* Whether it is possible to send/recv file descriptors * over the data channel */ QEMU_CHAR_FEATURE_FD_PASS , +/* Whether replay or record mode is enabled */ +QEMU_CHAR_FEATURE_REPLAY ,QEMU_CHAR_FEATURE_LAST , } CharDriverFeature;
@@ -97,7 +99,6 @@ struct CharDriverState {int logfd;int be_open;guint fd_in_tag; -bool replay;DECLARE_BITMAP (features,QEMU_CHAR_FEATURE_LAST );QTAILQ_ENTRY (CharDriverState) next; };
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 07/54] char: use a feature bit for replay
Quoted 7 lines · Marc-André Lureau
Use a feature flag rather than a structure field for "replay".
Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 33 ++++++++++++++++++++------------- include/sysemu/char.h | 3 ++- 2 files changed, 22 insertions(+), 14 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 8/54 char: allocate CharDriverState as a single object
In reply to Marc-André LureauUse a single allocation for CharDriverState, this avoids extra allocations & pointers, and is a step towards more object-oriented CharDriver.
Gtk console is a bit peculiar, gd_vc_chr_set_echo
--- backends/baum.c | 23 ++--- backends/msmouse.c | 16 +-- backends/testdev.c | 22 ++-- gdbstub.c | 1 + hw/bt/hci-csr.c | 10 +- qemu-char.c | 280 ++++++++++++++++++++++++++------------------------ spice-qemu-char.c | 39 +++---- ui/console.c | 21 ++-- ui/gtk.c | 30 ++++-- include/sysemu/char.h | 2 +- 10 files changed, 230 insertions(+), 214 deletions(-)
backends/baum.c +10 -13
@@ -87,7 +87,7 @@#define BUF_SIZE 256 typedef struct { - CharDriverState *chr; + CharDriverState parent; brlapi_handle_t *brlapi; int brlapi_fd;
@@ -270,7 +270,7 @@ static int baum_deferred_init(BaumDriverState *baum)/* The serial port can receive more of our data */ static void baum_accept_input (struct CharDriverState *chr) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;int room, first;if (!baum->out_buf_used)
@@ -296,22 +296,23 @@ static void baum_accept_input(struct CharDriverState *chr)/* We want to send a packet */ static void baum_write_packet (BaumDriverState *baum,const uint8_t *buf,int len) { +CharDriverState *chr = (CharDriverState *)baum;uint8_t io_buf[1 +2 * len], *cur = io_buf;int room; *cur++ =ESC ;while (len--)if ((*cur++ = *buf++) ==ESC ) *cur++ =ESC ; - room =qemu_chr_be_can_write (baum->chr); + room =qemu_chr_be_can_write (chr); len = cur - io_buf;if (len <= room) {/* Fits */ -qemu_chr_be_write (baum->chr, io_buf, len); +qemu_chr_be_write (chr, io_buf, len); }else {int first;uint8_t out;/* Can't fit all, send what can be, and store the rest. */ -qemu_chr_be_write (baum->chr, io_buf, room); +qemu_chr_be_write (chr, io_buf, room); len -= room; cur = io_buf + room;if (len >BUF_SIZE - baum->out_buf_used) {
@@ -486,7 +487,7 @@ static int baum_eat_packet(BaumDriverState *baum, const uint8_t *buf, int len)/* The other end is writing some data. Store it and try to interpret */ static int baum_write (CharDriverState *chr,const uint8_t *buf,int len) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;int tocopy, cur, eaten, orig_len = len;if (!len)
@@ -627,14 +628,13 @@ static void baum_chr_read(void *opaque)static void baum_free (struct CharDriverState *chr) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;timer_free (baum->cellCount_timer);if (baum->brlapi) {brlapi__closeConnection (baum->brlapi);g_free (baum->brlapi); } -g_free (baum); } static CharDriverState *chr_baum_init(const CharDriver *driver,
@@ -653,10 +653,7 @@ static CharDriverState *chr_baum_init(const CharDriver *driver,if (!chr) {return NULL ; } - baum =g_malloc0 (sizeof (BaumDriverState)); - baum->chr = chr; - - chr->opaque = baum; + baum = (BaumDriverState *)chr; handle =g_malloc0 (brlapi_getHandleSize ()); baum->brlapi = handle;
@@ -678,13 +675,13 @@ static CharDriverState *chr_baum_init(const CharDriver *driver, fail_handle:g_free (handle);g_free (chr); -g_free (baum);return NULL ; }static void register_types (void ) {static const CharDriver driver = { + .instance_size =sizeof (BaumDriverState), .kind =CHARDEV_BACKEND_KIND_BRAILLE , .parse =NULL , .create = chr_baum_init, .chr_write = baum_write,
backends/msmouse.c +8 -8
@@ -31,7 +31,8 @@#define MSMOUSE_HI2 (n) (((n) & 0xc0) >> 6) typedef struct { - CharDriverState *chr; + CharDriverState parent; + QemuInputHandlerState *hs; int axis[INPUT_AXIS__MAX]; bool btns[INPUT_BUTTON__MAX];
@@ -42,7 +43,7 @@ typedef struct {static void msmouse_chr_accept_input (CharDriverState *chr) { -MouseState *mouse = chr->opaque; +MouseState *mouse = (MouseState *)chr;int len; len =qemu_chr_be_can_write (chr);
@@ -122,9 +123,10 @@ static void msmouse_input_event(DeviceState *dev, QemuConsole *src,static void msmouse_input_sync (DeviceState *dev) {MouseState *mouse = (MouseState *)dev; +CharDriverState *chr = (CharDriverState *)dev;msmouse_queue_event (mouse); -msmouse_chr_accept_input (mouse->chr); +msmouse_chr_accept_input (chr); }static int msmouse_chr_write (struct CharDriverState *s,const uint8_t *buf,int len)
@@ -135,10 +137,9 @@ static int msmouse_chr_write (struct CharDriverState *s, const uint8_t *buf, intstatic void msmouse_chr_free (struct CharDriverState *chr) { -MouseState *mouse = chr->opaque; +MouseState *mouse = (MouseState *)chr;qemu_input_handler_unregister (mouse->hs); -g_free (mouse); }static QemuInputHandler msmouse_handler = {
@@ -165,12 +166,10 @@ static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver, } *be_opened = false; - mouse =g_new0 (MouseState,1 ); + mouse = (MouseState *)chr; mouse->hs =qemu_input_handler_register ((DeviceState *)mouse, &msmouse_handler); - mouse->chr = chr; - chr->opaque = mouse;return chr; }
@@ -178,6 +177,7 @@ static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver,static void register_types (void ) {static const CharDriver driver = { + .instance_size =sizeof (MouseState), .kind =CHARDEV_BACKEND_KIND_MSMOUSE , .parse =NULL , .create = qemu_chr_open_msmouse, .chr_write = msmouse_chr_write,
backends/testdev.c +6 -16
@@ -30,7 +30,8 @@#define BUF_SIZE 32typedef struct { -CharDriverState *chr; +CharDriverState parent; +uint8_t in_buf[32 ];int in_buf_used; }TestdevCharState ;
@@ -79,7 +80,7 @@ static int testdev_eat_packet(TestdevCharState *testdev)/* The other end is writing some data. Store it and try to interpret */ static int testdev_write (CharDriverState *chr,const uint8_t *buf,int len) { -TestdevCharState *testdev = chr->opaque; +TestdevCharState *testdev = (TestdevCharState *)chr;int tocopy, eaten, orig_len = len;while (len) {
@@ -102,13 +103,6 @@ static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)return orig_len; } -static void testdev_free (struct CharDriverState *chr) -{ -TestdevCharState *testdev = chr->opaque; - -g_free (testdev); -} - static CharDriverState *chr_testdev_init(const CharDriver *driver, const char *id, ChardevBackend *backend,
@@ -116,14 +110,10 @@ static CharDriverState *chr_testdev_init(const CharDriver *driver,bool *be_opened, Error **errp) { - TestdevCharState *testdev; -CharDriverState *chr; - - testdev =g_new0 (TestdevCharState,1 ); - testdev->chr = chr =g_new0 (CharDriverState,1 ); + TestdevCharState *testdev = g_new0(TestdevCharState, 1);; +CharDriverState *chr = (CharDriverState *)testdev; chr->driver = driver; - chr->opaque = testdev;return chr; }
@@ -131,10 +121,10 @@ static CharDriverState *chr_testdev_init(const CharDriver *driver,static void register_types (void ) {static const CharDriver driver = { + .instance_size =sizeof (TestdevCharState), .kind =CHARDEV_BACKEND_KIND_TESTDEV , .parse =NULL , .create = chr_testdev_init, .chr_write = testdev_write, - .chr_free = testdev_free, };register_char_driver (&driver); }
gdbstub.c +1 -0
@@ -1731,6 +1731,7 @@ int gdbserver_start(const char *device)CharDriverState *mon_chr;ChardevCommon common = {0 };static const CharDriver driver = { + .instance_size =sizeof (CharDriverState), .kind =-1 , .chr_write = gdb_monitor_write };
hw/bt/hci-csr.c +5 -5
@@ -28,11 +28,11 @@#include "hw/bt.h" struct csrhci_s { +CharDriverState chr;int enable;qemu_irq *pins;int pin_state;int modem_state; -CharDriverState chr;#define FIFO_LEN 4096int out_start;int out_len;
@@ -314,7 +314,7 @@ static void csrhci_ready_for_next_inpkt(struct csrhci_s *s)static int csrhci_write (struct CharDriverState *chr,const uint8_t *buf,int len) { -struct csrhci_s *s = (struct csrhci_s *) chr->opaque; +struct csrhci_s *s = (struct csrhci_s *)chr;int total =0 ;if (!s->enable)
@@ -387,7 +387,7 @@ static void csrhci_out_hci_packet_acl(void *opaque,static int csrhci_ioctl (struct CharDriverState *chr,int cmd,void *arg) {QEMUSerialSetParams *ssp; -struct csrhci_s *s = (struct csrhci_s *) chr->opaque; +struct csrhci_s *s = (struct csrhci_s *) chr;int prev_state = s->modem_state;switch (cmd) {
@@ -455,7 +455,7 @@ static void csrhci_pins(void *opaque, int line, int level)qemu_irq *csrhci_pins_get (CharDriverState *chr) { -struct csrhci_s *s = (struct csrhci_s *) chr->opaque; +struct csrhci_s *s = (struct csrhci_s *) chr;return s->pins; }
@@ -463,6 +463,7 @@ qemu_irq *csrhci_pins_get(CharDriverState *chr)CharDriverState *uart_hci_init (void ) {static const CharDriver hci_driver = { + .instance_size =sizeof (struct csrhci_s ), .kind =-1 , .chr_write = csrhci_write, .chr_ioctl = csrhci_ioctl,
@@ -470,7 +471,6 @@ CharDriverState *uart_hci_init(void)struct csrhci_s *s = (struct csrhci_s *)g_malloc0 (sizeof (struct csrhci_s )); - s->chr.opaque = s; s->chr.driver = &hci_driver; s->hci =qemu_next_hci ();
qemu-char.c +144 -136
1,103 lines not on this page · raw message
spice-qemu-char.c +21 -18
@@ -7,7 +7,8 @@ typedef struct SpiceCharDriver { - CharDriverState* chr; + CharDriverState parent; + SpiceCharDeviceInstance sin; bool active; bool blocked;
@@ -27,17 +28,18 @@ static QLIST_HEAD(, SpiceCharDriver) spice_chars =static int vmc_write (SpiceCharDeviceInstance *sin,const uint8_t *buf,int len) {SpiceCharDriver *scd =container_of (sin, SpiceCharDriver, sin); +CharDriverState *chr = (CharDriverState *)scd;ssize_t out =0 ;ssize_t last_out;uint8_t * p = (uint8_t *)buf;while (len >0 ) { -int can_write =qemu_chr_be_can_write (scd->chr); +int can_write =qemu_chr_be_can_write (chr); last_out =MIN (len, can_write);if (last_out <=0 ) {break ; } -qemu_chr_be_write (scd->chr, p, last_out); +qemu_chr_be_write (chr, p, last_out); out += last_out; len -= last_out; p += last_out;
@@ -70,6 +72,7 @@ static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)static void vmc_event (SpiceCharDeviceInstance *sin,uint8_t event) {SpiceCharDriver *scd =container_of (sin, SpiceCharDriver, sin); +CharDriverState *chr = (CharDriverState *)scd;int chr_event;switch (event) {
@@ -81,20 +84,21 @@ static void vmc_event(SpiceCharDeviceInstance *sin, uint8_t event) }trace_spice_vmc_event (chr_event); -qemu_chr_be_event (scd->chr, chr_event); +qemu_chr_be_event (chr, chr_event); }#endif static void vmc_state (SpiceCharDeviceInstance *sin,int connected) {SpiceCharDriver *scd =container_of (sin, SpiceCharDriver, sin); +CharDriverState *chr = (CharDriverState *)scd; -if ((scd->chr->be_open && connected) || - (!scd->chr->be_open && !connected)) { +if ((chr->be_open && connected) || + (!chr->be_open && !connected)) {return ; } -qemu_chr_be_event (scd->chr, +qemu_chr_be_event (chr, connected ?CHR_EVENT_OPENED :CHR_EVENT_CLOSED ); }
@@ -168,7 +172,7 @@ static GSourceFuncs SpiceCharSourceFuncs = {static GSource *spice_chr_add_watch (CharDriverState *chr,GIOCondition cond) { -SpiceCharDriver *scd = chr->opaque; +SpiceCharDriver *scd = (SpiceCharDriver *)chr;SpiceCharSource *src;assert (cond &G_IO_OUT );
@@ -182,7 +186,7 @@ static GSource *spice_chr_add_watch(CharDriverState *chr, GIOCondition cond)static int spice_chr_write (CharDriverState *chr,const uint8_t *buf,int len) { -SpiceCharDriver *s = chr->opaque; +SpiceCharDriver *s = (SpiceCharDriver *)chr;int read_bytes;assert (s->datalen ==0 );
@@ -201,7 +205,7 @@ static int spice_chr_write(CharDriverState *chr, const uint8_t *buf, int len)static void spice_chr_free (struct CharDriverState *chr) { -SpiceCharDriver *s = chr->opaque; +SpiceCharDriver *s = (SpiceCharDriver *)chr;vmc_unregister_interface (s);QLIST_REMOVE (s, next);
@@ -210,12 +214,11 @@ static void spice_chr_free(struct CharDriverState *chr)#if SPICE_SERVER_VERSION >=0x000c02 g_free ((char *)s->sin.portname);#endif -g_free (s); }static void spice_vmc_set_fe_open (struct CharDriverState *chr,int fe_open) { -SpiceCharDriver *s = chr->opaque; +SpiceCharDriver *s = (SpiceCharDriver *)chr;if (fe_open) {vmc_register_interface (s); }else {
@@ -226,7 +229,7 @@ static void spice_vmc_set_fe_open(struct CharDriverState *chr, int fe_open)static void spice_port_set_fe_open (struct CharDriverState *chr,int fe_open) {#if SPICE_SERVER_VERSION >=0x000c02 -SpiceCharDriver *s = chr->opaque; +SpiceCharDriver *s = (SpiceCharDriver *)chr;if (fe_open) {spice_server_port_event (&s->sin,SPICE_PORT_EVENT_OPENED );
@@ -255,7 +258,7 @@ static void print_allowed_subtypes(void)static void spice_chr_accept_input (struct CharDriverState *chr) { -SpiceCharDriver *s = chr->opaque; +SpiceCharDriver *s = (SpiceCharDriver *)chr;spice_server_char_device_wakeup (&s->sin); }
@@ -272,11 +275,9 @@ static CharDriverState *chr_open(const CharDriver *driver,if (!chr) {return NULL ; } - s =g_malloc0 (sizeof (SpiceCharDriver)); - s->chr = chr; + s = (SpiceCharDriver *)chr; s->active = false; s->sin.subtype =g_strdup (subtype); - chr->opaque = s;QLIST_INSERT_HEAD (&spice_chars, s, next);
@@ -334,7 +335,7 @@ static CharDriverState *qemu_chr_open_spice_port(const CharDriver *driver,return NULL ; } *be_opened = false; - s = chr->opaque; + s = (SpiceCharDriver *)chr; s->sin.portname =g_strdup (name);return chr;
@@ -386,6 +387,7 @@ static void qemu_chr_parse_spice_port(QemuOpts *opts, ChardevBackend *backend,static void register_types (void ) {static const CharDriver vmc_driver = { + .instance_size =sizeof (SpiceCharDriver), .kind =CHARDEV_BACKEND_KIND_SPICEVMC , .parse = qemu_chr_parse_spice_vmc, .create = qemu_chr_open_spice_vmc, .chr_write = spice_chr_write,
@@ -395,6 +397,7 @@ static void register_types(void) .chr_free = spice_chr_free, };static const CharDriver port_driver = { + .instance_size =sizeof (SpiceCharDriver), .kind =CHARDEV_BACKEND_KIND_SPICEPORT , .parse = qemu_chr_parse_spice_port, .create = qemu_chr_open_spice_port, .chr_write = spice_chr_write,
ui/console.c +15 -6
@@ -1035,9 +1035,15 @@ void console_select(unsigned int index) } } +typedef struct VCDriverState { +CharDriverState parent; +QemuConsole *console; +}VCDriverState ; +static int console_puts (CharDriverState *chr,const uint8_t *buf,int len) { -QemuConsole *s = chr->opaque; +VCDriverState *drv = (VCDriverState *)chr; +QemuConsole *s = drv->console;int i;if (!s->ds) {
@@ -1947,7 +1953,8 @@ int qemu_console_get_height(QemuConsole *con, int fallback)static void text_console_set_echo (CharDriverState *chr,bool echo) { -QemuConsole *s = chr->opaque; +VCDriverState *drv = (VCDriverState *)chr; +QemuConsole *s = drv->console; s->echo = echo; }
@@ -1987,12 +1994,11 @@ static const GraphicHwOps text_console_ops = {static void text_console_do_init (CharDriverState *chr,DisplayState *ds) { -QemuConsole *s; +VCDriverState *drv = (VCDriverState *)chr; +QemuConsole *s = drv->console;int g_width =80 *FONT_WIDTH ;int g_height =24 *FONT_HEIGHT ; - s = chr->opaque; - s->out_fifo.buf = s->out_fifo_buf; s->out_fifo.buf_size =sizeof (s->out_fifo_buf); s->kbd_timer =timer_new_ms (QEMU_CLOCK_REALTIME , kbd_send_chars, s);
@@ -2045,6 +2051,7 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp) {ChardevCommon *common =qapi_ChardevVC_base (vc);CharDriverState *chr; +VCDriverState *drv;QemuConsole *s;unsigned width =0 ;unsigned height =0 ;
@@ -2081,7 +2088,8 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp) } s->chr = chr; - chr->opaque = s; + drv = (VCDriverState *)chr; + drv->console = s;if (display_state) {text_console_do_init (chr, display_state);
@@ -2185,6 +2193,7 @@ static const TypeInfo qemu_console_info = { };static const CharDriver vc_driver = { + .instance_size =sizeof (VCDriverState), .kind =CHARDEV_BACKEND_KIND_VC , .parse = qemu_chr_parse_vc, .create = vc_init, .chr_write = console_puts,
ui/gtk.c +19 -11
@@ -178,6 +178,12 @@ struct GtkDisplayState {bool ignore_keys; }; +typedef struct VCDriverState { +CharDriverState parent; +VirtualConsole *console; +bool echo; +}VCDriverState ; +static void gd_grab_pointer (VirtualConsole *vc,const char *reason);static void gd_ungrab_pointer (GtkDisplayState *s);static void gd_grab_keyboard (VirtualConsole *vc,const char *reason);
@@ -1675,7 +1681,8 @@ static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)static int gd_vc_chr_write (CharDriverState *chr,const uint8_t *buf,int len) { -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console;vte_terminal_feed (VTE_TERMINAL (vc->vte.terminal), (const char *)buf, len);return len;
@@ -1683,9 +1690,14 @@ static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)static void gd_vc_chr_set_echo (CharDriverState *chr,bool echo) { -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console; - vc->vte.echo = echo; +if (vc) { + vc->vte.echo = echo; + }else { + vcd->echo = echo; + } }static int nb_vcs;
@@ -1694,6 +1706,7 @@ static CharDriverState *vcs[MAX_VCS];static CharDriverState *gd_vc_handler (ChardevVC *vc,Error **errp) {static const CharDriver gd_vc_driver = { + .instance_size =sizeof (VCDriverState), .kind =CHARDEV_BACKEND_KIND_VC , .chr_write = gd_vc_chr_write, .chr_set_echo = gd_vc_chr_set_echo,
@@ -1712,9 +1725,6 @@ static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)return NULL ; } -/* Temporary, until gd_vc_vte_init runs. */ - chr->opaque =g_new0 (VirtualConsole,1 ); - vcs[nb_vcs++] = chr;return chr;
@@ -1755,14 +1765,12 @@ static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,GtkWidget *box;GtkWidget *scrollbar;GtkAdjustment *vadjustment; -VirtualConsole *tmp_vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; vc->s = s; - vc->vte.echo = tmp_vc->vte.echo; - + vc->vte.echo = vcd->echo; vc->vte.chr = chr; - chr->opaque = vc; -g_free (tmp_vc); + vcd->console = vc;snprintf (buffer,sizeof (buffer),"vc%d" , idx); vc->label = g_strdup_printf("%s", vc->vte.chr->label
include/sysemu/char.h +1 -1
@@ -93,7 +93,6 @@ struct CharDriverState {const CharDriver *driver;QemuMutex chr_write_lock;CharBackend *be; -void *opaque;char *label;char *filename;int logfd;
@@ -482,6 +481,7 @@ struct CharDriver { ChardevBackend *backend, ChardevReturn *ret, bool *be_opened, Error **errp); +size_t instance_size;int (*chr_write)(struct CharDriverState *s,const uint8_t *buf,int len); int (*chr_sync_read)(struct CharDriverState *s,
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 08/54] char: allocate CharDriverState as a single object
Quoted 5 lines · Marc-André Lureau
Use a single allocation for CharDriverState, this avoids extra allocations & pointers, and is a step towards more object-oriented CharDriver.
Gtk console is a bit peculiar,
gd_vc_chr_set_echo
Truncated paragraph?
Quoted 28 lines
Signed-off-by Marc-André Lureau <[email protected]>--- backends/baum.c | 23 ++--- backends/msmouse.c | 16 +-- backends/testdev.c | 22 ++-- gdbstub.c | 1 + hw/bt/hci-csr.c | 10 +- qemu-char.c | 280 ++++++++++++++++++++++++++------------------------ spice-qemu-char.c | 39 +++---- ui/console.c | 21 ++-- ui/gtk.c | 30 ++++-- include/sysemu/char.h | 2 +- 10 files changed, 230 insertions(+), 214 deletions(-)backends/baum.c +1
-1@@ -87,7 +87,7 @@#define BUF_SIZE 256 typedef struct { - CharDriverState *chr; + CharDriverState parent; brlapi_handle_t *brlapi; int brlapi_fd;
backends/baum.c static int baum_deferred_init(BaumDriverState *baum) · line 273
Quoted 5 lines
backends/baum.c +1
-1@@ -270,7 +270,7 @@ static int baum_deferred_init(BaumDriverState *baum)/* The serial port can receive more of our data */ static void baum_accept_input (struct CharDriverState *chr) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;
It might be a little nicer to use:
BaumDriverState *baum = container_of(chr, BaumDriverState, parent);
to avoid a cast and work even if the members are rearranged within the structure. You can even write a one-line helper function to hide the cast behind a more legible semantic (for example, see to_qov() in qobject-output-visitor.c).
(Same comment applies to most other base-to-derived casts in your patch)
Quoted 15 lines
backends/baum.c +2
-1+++ b/hw/bt/hci-csr.c@@ -28,11 +28,11 @@#include "hw/bt.h" struct csrhci_s { +CharDriverState chr;int enable;qemu_irq *pins;int pin_state;int modem_state; -CharDriverState chr;#define FIFO_LEN 4096int out_start;int out_len;
backends/baum.c static void csrhci_ready_for_next_inpkt(struct csrhci_s *s) · line 317
Quoted 5 lines
backends/baum.c +1
-1@@ -314,7 +314,7 @@ static void csrhci_ready_for_next_inpkt(struct csrhci_s *s)static int csrhci_write (struct CharDriverState *chr,const uint8_t *buf,int len) { -struct csrhci_s *s = (struct csrhci_s *) chr->opaque; +struct csrhci_s *s = (struct csrhci_s *)chr;
Wonder if a typedef would make this more legible, but maybe as a separate cleanup.
Quoted 50 lines
backends/baum.c +17
-3+++ b/ui/gtk.c@@ -178,6 +178,12 @@ struct GtkDisplayState {bool ignore_keys; }; +typedef struct VCDriverState { +CharDriverState parent; +VirtualConsole *console; +bool echo; +}VCDriverState ; +static void gd_grab_pointer (VirtualConsole *vc,const char *reason);static void gd_ungrab_pointer (GtkDisplayState *s);static void gd_grab_keyboard (VirtualConsole *vc,const char *reason);@@ -1675,7 +1681,8 @@ static void gd_vc_adjustment_changed(GtkAdjustment *adjustment, void *opaque)static int gd_vc_chr_write (CharDriverState *chr,const uint8_t *buf,int len) { -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console;vte_terminal_feed (VTE_TERMINAL (vc->vte.terminal), (const char *)buf, len);return len;@@ -1683,9 +1690,14 @@ static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf, int len)static void gd_vc_chr_set_echo (CharDriverState *chr,bool echo) { -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console; - vc->vte.echo = echo; +if (vc) { + vc->vte.echo = echo; + }else { + vcd->echo = echo; + } }static int nb_vcs;@@ -1694,6 +1706,7 @@ static CharDriverState *vcs[MAX_VCS];static CharDriverState *gd_vc_handler (ChardevVC *vc,Error **errp) {static const CharDriver gd_vc_driver = { + .instance_size =sizeof (VCDriverState), .kind =CHARDEV_BACKEND_KIND_VC , .chr_write = gd_vc_chr_write, .chr_set_echo = gd_vc_chr_set_echo,
backends/baum.c static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp) · line 1717
Quoted 6 lines
backends/baum.c +0
-3@@ -1712,9 +1725,6 @@ static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)return NULL ; } -/* Temporary, until gd_vc_vte_init runs. */ - chr->opaque =g_new0 (VirtualConsole,1 ); -
Okay, I see the weirdness going on with the 'echo' stuff - you have to simulate the temporary placeholder for whether or not to set echo, such that gd_vc_chr_set_echo() can now be called with a NULL opaque where it used to always have an object to dereference.
Quoted 9 lines
backends/baum.c +1
-1vcs[nb_vcs++] = chr;return chr;@@ -1755,14 +1765,12 @@ static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc,GtkWidget *box;GtkWidget *scrollbar;GtkAdjustment *vadjustment; -VirtualConsole *tmp_vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr;
backends/baum.c static GSList *gd_vc_vte_init(GtkDisplayState *s, VirtualConsole *vc, · line 1774
Quoted 10 lines
backends/baum.c +2
-4vc->s = s; - vc->vte.echo = tmp_vc->vte.echo; - + vc->vte.echo = vcd->echo; vc->vte.chr = chr; - chr->opaque = vc; -g_free (tmp_vc); + vcd->console = vc;
So it is indeed weird, but looks correct in the end.
Quoted 15 lines
backends/baum.c +0
-0snprintf (buffer,sizeof (buffer),"vc%d" , idx); vc->label = g_strdup_printf("%s", vc->vte.chr->labelinclude/sysemu/char.h +0
-1@@ -93,7 +93,6 @@ struct CharDriverState {const CharDriver *driver;QemuMutex chr_write_lock;CharBackend *be; -void *opaque;char *label;char *filename;int logfd;
include/sysemu/char.h struct CharDriver { · line 484
Quoted 4 lines
include/sysemu/char.h +1
-0@@ -482,6 +481,7 @@ struct CharDriver { ChardevBackend *backend, ChardevReturn *ret, bool *be_opened, Error **errp); +size_t instance_size;
My biggest gripe is the number of casts that could be container_of() instead; but otherwise it looks like a sane conversion.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 08/54] char: allocate CharDriverState as a single object
Hi
Quoted 9 lines · Eric Blake
Quoted 5 lines · Marc-André Lureau
Use a single allocation for CharDriverState, this avoids extra allocations & pointers, and is a step towards more object-oriented CharDriver.
Gtk console is a bit peculiar,
gd_vc_chr_set_echoTruncated paragraph?
yes, fixed
Quoted 38 lines
Quoted 9 lines
Signed-off-by Marc-André Lureau <[email protected]>--- backends/baum.c | 23 ++--- backends/msmouse.c | 16 +-- backends/testdev.c | 22 ++-- gdbstub.c | 1 + hw/bt/hci-csr.c | 10 +- qemu-char.c | 280++++++++++++++++++++++++++------------------------
Quoted 20 lines
spice-qemu-char.c | 39 +++---- ui/console.c | 21 ++-- ui/gtk.c | 30 ++++-- include/sysemu/char.h | 2 +- 10 files changed, 230 insertions(+), 214 deletions(-)backends/baum.c +1
-1@@ -87,7 +87,7 @@#define BUF_SIZE 256 typedef struct { - CharDriverState *chr; + CharDriverState parent; brlapi_handle_t *brlapi; int brlapi_fd;backends/baum.c static int baum_deferred_init(BaumDriverState *baum) · line 273
Quoted 5 lines
backends/baum.c +1
-1@@ -270,7 +270,7 @@ static int baum_deferred_init(BaumDriverState *baum)/* The serial port can receive more of our data */ static void baum_accept_input (struct CharDriverState *chr) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;It might be a little nicer to use:
Quoted 3 lines
BaumDriverState *baum =
container_of(chr, BaumDriverState, parent);
The follow-up of the series converts it to the more appropriate
BaumChardev *baum = BAUM_CHARDEV(obj);
So considering that this is temporary commit, do I have to change that?
to avoid a cast and work even if the members are rearranged within the
Quoted 89 lines
structure. You can even write a one-line helper function to hide the cast behind a more legible semantic (for example, see
to_qov()inqobject-output-visitor.c).(Same comment applies to most other base-to-derived casts in your patch)
backends/baum.c static void csrhci_ready_for_next_inpkt(struct
Quoted 15 lines
backends/baum.c +2
-1+++ b/hw/bt/hci-csr.c@@ -28,11 +28,11 @@#include "hw/bt.h" struct csrhci_s { +CharDriverState chr;int enable;qemu_irq *pins;int pin_state;int modem_state; -CharDriverState chr;#define FIFO_LEN 4096int out_start;int out_len;@@ -314,7 +314,7 @@ static void csrhci_ready_for_next_inpkt(struct
csrhci_s*s)backends/baum.c static void csrhci_ready_for_next_inpkt(struct · line 317
Quoted 5 lines
backends/baum.c +1
-1static int csrhci_write (struct CharDriverState *chr,const uint8_t *buf,int len) { -struct csrhci_s *s = (struct csrhci_s *) chr->opaque; +struct csrhci_s *s = (struct csrhci_s *)chr;Wonder if a typedef would make this more legible, but maybe as a separate cleanup.
backends/baum.c static void gd_vc_adjustment_changed(GtkAdjustment
Quoted 15 lines
backends/baum.c +7
-0+++ b/ui/gtk.c@@ -178,6 +178,12 @@ struct GtkDisplayState {bool ignore_keys; }; +typedef struct VCDriverState { +CharDriverState parent; +VirtualConsole *console; +bool echo; +}VCDriverState ; +static void gd_grab_pointer (VirtualConsole *vc,const char *reason);static void gd_ungrab_pointer (GtkDisplayState *s);static void gd_grab_keyboard (VirtualConsole *vc,const char *reason);@@ -1675,7 +1681,8 @@ static void gd_vc_adjustment_changed(GtkAdjustment*adjustment, void *opaque)
backends/baum.c static void gd_vc_adjustment_changed(GtkAdjustment · line 1681
Quoted 2 lines
backends/baum.c +0
-0static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf,int len)
backends/baum.c static void gd_vc_adjustment_changed(GtkAdjustment · line 1686
Quoted 6 lines
backends/baum.c +2
-1{ -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console; vte_terminal_feed(VTE_TERMINAL(vc->vte.terminal), (const char*)buf, len);
backends/baum.c static int gd_vc_chr_write(CharDriverState *chr,
Quoted 2 lines
backends/baum.c +0
-0return len;@@ -1683,9 +1690,14 @@ static int gd_vc_chr_write(CharDriverState *chr,const
uint8_t*buf, int len)backends/baum.c static CharDriverState *gd_vc_handler(ChardevVC
Quoted 25 lines
backends/baum.c +8
-2static void gd_vc_chr_set_echo (CharDriverState *chr,bool echo) { -VirtualConsole *vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr; +VirtualConsole *vc = vcd->console; - vc->vte.echo = echo; +if (vc) { + vc->vte.echo = echo; + }else { + vcd->echo = echo; + } }static int nb_vcs;@@ -1694,6 +1706,7 @@ static CharDriverState *vcs[MAX_VCS];static CharDriverState *gd_vc_handler (ChardevVC *vc,Error **errp) {static const CharDriver gd_vc_driver = { + .instance_size =sizeof (VCDriverState), .kind =CHARDEV_BACKEND_KIND_VC , .chr_write = gd_vc_chr_write, .chr_set_echo = gd_vc_chr_set_echo,@@ -1712,9 +1725,6 @@ static CharDriverState *gd_vc_handler(ChardevVC*vc, Error **errp)
Quoted 3 lines
backends/baum.c +0
-0return NULL ; }
Quoted 10 lines
backends/baum.c static CharDriverState *gd_vc_handler(ChardevVC · line 1717
Quoted 3 lines
backends/baum.c +0
-3-/* Temporary, until gd_vc_vte_init runs. */ - chr->opaque =g_new0 (VirtualConsole,1 ); -Okay, I see the weirdness going on with the 'echo' stuff - you have to simulate the temporary placeholder for whether or not to set echo, such that
gd_vc_chr_set_echo()can now be called with a NULL opaque where it used to always have an object to dereference.
right
Quoted 37 lines
backends/baum.c static GSList *gd_vc_vte_init(GtkDisplayState
Quoted 4 lines
backends/baum.c +0
-0vcs[nb_vcs++] = chr;return chr;@@ -1755,14 +1765,12 @@ static GSList *gd_vc_vte_init(GtkDisplayState*s, VirtualConsole *vc,
Quoted 5 lines
backends/baum.c +1
-1GtkWidget *box;GtkWidget *scrollbar;GtkAdjustment *vadjustment; -VirtualConsole *tmp_vc = chr->opaque; +VCDriverState *vcd = (VCDriverState *)chr;backends/baum.c static GSList *gd_vc_vte_init(GtkDisplayState · line 1774
Quoted 10 lines
backends/baum.c +2
-4vc->s = s; - vc->vte.echo = tmp_vc->vte.echo; - + vc->vte.echo = vcd->echo; vc->vte.chr = chr; - chr->opaque = vc; -g_free (tmp_vc); + vcd->console = vc;So it is indeed weird, but looks correct in the end.
Quoted 14 lines
backends/baum.c +0
-0snprintf (buffer,sizeof (buffer),"vc%d" , idx); vc->label = g_strdup_printf("%s", vc->vte.chr->labelinclude/sysemu/char.h +0
-1@@ -93,7 +93,6 @@ struct CharDriverState {const CharDriver *driver;QemuMutex chr_write_lock;CharBackend *be; -void *opaque;char *label;char *filename;int logfd;
Quoted 10 lines
include/sysemu/char.h struct CharDriver { · line 484
Quoted 5 lines
include/sysemu/char.h +1
-0@@ -482,6 +481,7 @@ struct CharDriver { ChardevBackend *backend, ChardevReturn *ret, bool *be_opened, Error **errp); +size_t instance_size;My biggest gripe is the number of casts that could be
container_of()instead; but otherwise it looks like a sane conversion.
thanks, the goal is to get rid of them, but not in this commit :) See "chardev: qom-ify".
Signature · 1 line
Marc-André Lureau
Eric Blake
Reviewed-byRe: [Qemu-devel] [PATCH 08/54] char: allocate CharDriverState as a single object
Quoted 26 lines · Marc-André Lureau
Hi
Quoted 9 lines · Eric Blake
Quoted 5 lines · Marc-André Lureau
Use a single allocation for CharDriverState, this avoids extra allocations & pointers, and is a step towards more object-oriented CharDriver.
Gtk console is a bit peculiar,
gd_vc_chr_set_echoTruncated paragraph?
yes, fixed
Quoted 9 lines
backends/baum.c static int baum_deferred_init(BaumDriverState *baum) · line 273
Quoted 6 lines
backends/baum.c +1
-1@@ -270,7 +270,7 @@ static int baum_deferred_init(BaumDriverState *baum)/* The serial port can receive more of our data */ static void baum_accept_input (struct CharDriverState *chr) { -BaumDriverState *baum = chr->opaque; +BaumDriverState *baum = (BaumDriverState *)chr;It might be a little nicer to use:
Quoted 7 lines
Quoted 3 lines
BaumDriverState *baum =
container_of(chr, BaumDriverState, parent);The follow-up of the series converts it to the more appropriate
BaumChardev *baum =BAUM_CHARDEV(obj);So considering that this is temporary commit, do I have to change that?
Ah, nice. So BAUM_CHARDEV(obj) will be a nice wrapper around container_of(). I can live with it being temporary (although a note in the commit message can't hurt).
Quoted 5 lines
to avoid a cast and work even if the members are rearranged within the
Quoted 3 lines
structure. You can even write a one-line helper function to hide the cast behind a more legible semantic (for example, see
to_qov()inqobject-output-visitor.c).
My example of to_qov() is matched by your BAUM_CHARDEV() macro.
Quoted 6 lines
Quoted 4 lines
My biggest gripe is the number of casts that could be
container_of()instead; but otherwise it looks like a sane conversion.thanks, the goal is to get rid of them, but not in this commit :) See "chardev: qom-ify".
That's what I get for only reviewing the series one patch at a time. I'm fine with temporary and partial hacks, but calling them out as such makes review easier if we know it's going to improve later.
Since I think you've answered my questions, then with an improved commit message, v2 can add:
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 9/54 bt: use qemu_chr_alloc()
In reply to Marc-André LureauUse common allocator for CharDriverState.
Rename the now untouched parent field.
--- hw/bt/hci-csr.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
hw/bt/hci-csr.c +13 -6
@@ -26,9 +26,10 @@#include "hw/irq.h" #include "sysemu/bt.h" #include "hw/bt.h" +#include "qapi/error.h" struct csrhci_s { -CharDriverState chr; +CharDriverState parent;int enable;qemu_irq *pins;int pin_state;
@@ -78,7 +79,8 @@ enum {static inline void csrhci_fifo_wake (struct csrhci_s *s) { -CharBackend *be = s->chr.be; +CharDriverState *chr = (CharDriverState *)s; +CharBackend *be = chr->be;if (!s->enable || !s->out_len)return ;
@@ -468,10 +470,15 @@ CharDriverState *uart_hci_init(void) .chr_write = csrhci_write, .chr_ioctl = csrhci_ioctl, }; -struct csrhci_s *s = (struct csrhci_s *) -g_malloc0 (sizeof (struct csrhci_s )); +Error *err =NULL ; +ChardevCommon common = {0 , }; +CharDriverState *chr =qemu_chr_alloc (&hci_driver, &common, &err); +struct csrhci_s *s = (struct csrhci_s *)chr; - s->chr.driver = &hci_driver; +if (err) { +error_report_err (err); +return NULL ; + } s->hci =qemu_next_hci (); s->hci->opaque = s;
@@ -482,5 +489,5 @@ CharDriverState *uart_hci_init(void) s->pins =qemu_allocate_irqs (csrhci_pins, s, __csrhci_pins);csrhci_reset (s); -return &s->chr; +return chr; }
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 09/54] bt: use qemu_chr_alloc()
Quoted 13 lines · Marc-André Lureau
Use common allocator for CharDriverState.
Rename the now untouched parent field.
Signed-off-by Marc-André Lureau <[email protected]>--- hw/bt/hci-csr.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)static inline void
csrhci_fifo_wake(struct csrhci_s *s){
hw/bt/hci-csr.c enum { · line 82
Quoted 2 lines
hw/bt/hci-csr.c +1
-1-CharBackend *be = s->chr.be; +CharDriverState *chr = (CharDriverState *)s;
Another one of those temporary casts that got me stuck on 8/54. So with the same idea of the commit message mentioning that it gets better later in the series,
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 10/54 char: rename CharDriverState Chardev
In reply to Marc-André LureauUniformify chardev type name.
--- backends/baum.c | 56 +-- backends/msmouse.c | 42 +- backends/rng-egd.c | 4 +- backends/testdev.c | 28 +- gdbstub.c | 12 +- hw/arm/fsl-imx25.c | 2 +- hw/arm/fsl-imx31.c | 2 +- hw/arm/fsl-imx6.c | 2 +- hw/arm/nseries.c | 2 +- hw/arm/omap2.c | 2 +- hw/arm/pxa2xx.c | 2 +- hw/arm/virt.c | 2 +- hw/bt/hci-csr.c | 14 +- hw/char/escc.c | 2 +- hw/char/exynos4210_uart.c | 2 +- hw/char/imx_serial.c | 2 +- hw/char/mcf_uart.c | 4 +- hw/char/omap_uart.c | 6 +- hw/char/parallel.c | 2 +- hw/char/serial-isa.c | 2 +- hw/char/serial.c | 4 +- hw/char/sh_serial.c | 2 +- hw/char/spapr_vty.c | 2 +- hw/char/virtio-console.c | 2 +- hw/core/qdev-properties-system.c | 4 +- hw/display/milkymist-tmu2.c | 2 +- hw/display/sm501.c | 2 +- hw/isa/isa-bus.c | 2 +- hw/isa/pc87312.c | 2 +- hw/mips/mips_malta.c | 4 +- hw/misc/ivshmem.c | 2 +- hw/misc/milkymist-pfpu.c | 2 +- hw/usb/ccid-card-passthru.c | 2 +- hw/usb/dev-serial.c | 6 +- hw/usb/redirect.c | 4 +- monitor.c | 4 +- net/colo-compare.c | 4 +- net/filter-mirror.c | 4 +- net/slirp.c | 2 +- net/vhost-user.c | 10 +- qemu-char.c | 839 +++++++++++++++++++------------------- qmp.c | 2 +- qtest.c | 2 +- replay/replay-char.c | 8 +- spice-qemu-char.c | 80 ++-- stubs/get-next-serial.c | 2 +- stubs/monitor-init.c | 2 +- stubs/replay.c | 4 +- tests/test-char.c | 10 +- tests/vhost-user-test.c | 4 +- ui/console.c | 40 +- ui/gtk.c | 26 +- vl.c | 10 +- xen-common-stub.c | 2 +- xen-common.c | 4 +- hw/lm32/lm32.h | 4 +- hw/lm32/milkymist-hw.h | 2 +- include/hw/arm/exynos4210.h | 2 +- include/hw/arm/omap.h | 6 +- include/hw/bt.h | 4 +- include/hw/char/cadence_uart.h | 2 +- include/hw/char/escc.h | 2 +- include/hw/char/pl011.h | 4 +- include/hw/char/serial.h | 4 +- include/hw/char/xilinx_uartlite.h | 2 +- include/hw/cris/etraxfs.h | 2 +- include/hw/devices.h | 2 +- include/hw/i386/pc.h | 2 +- include/hw/m68k/mcf.h | 4 +- include/hw/ppc/spapr_vio.h | 2 +- include/hw/qdev-properties.h | 2 +- include/hw/sh4/sh.h | 2 +- include/hw/sparc/grlib.h | 2 +- include/hw/xen/xen.h | 2 +- include/monitor/monitor.h | 2 +- include/qemu/typedefs.h | 2 +- include/sysemu/char.h | 94 ++--- include/sysemu/replay.h | 4 +- include/sysemu/sysemu.h | 4 +- include/ui/gtk.h | 2 +- include/ui/qemu-spice.h | 2 +- 81 files changed, 728 insertions(+), 725 deletions(-)
backends/baum.c +29 -27
@@ -87,7 +87,7 @@#define BUF_SIZE 256 typedef struct { - CharDriverState parent; + Chardev parent; brlapi_handle_t *brlapi; int brlapi_fd;
@@ -100,7 +100,7 @@ typedef struct {uint8_t out_buf_used, out_buf_ptr;QEMUTimer *cellCount_timer; -} BaumDriverState; +} BaumChardev;/* Let's assume NABCC by default */ enum way {
@@ -225,7 +225,7 @@ static const uint8_t nabcc_translation[2][256] = { };/* The guest OS has started discussing with us, finish initializing BrlAPI */ -static int baum_deferred_init (BaumDriverState *baum) +static int baum_deferred_init (BaumChardev *baum) {#if defined(CONFIG_SDL )#if SDL_COMPILEDVERSION <SDL_VERSIONNUM (2 ,0 ,0 )
@@ -268,9 +268,9 @@ static int baum_deferred_init(BaumDriverState *baum) }/* The serial port can receive more of our data */ -static void baum_accept_input (struct CharDriverState *chr) +static void baum_accept_input (struct Chardev *chr) { -BaumDriverState *baum = (BaumDriverState *)chr; +BaumChardev *baum = (BaumChardev *)chr;int room, first;if (!baum->out_buf_used)
@@ -294,9 +294,9 @@ static void baum_accept_input(struct CharDriverState *chr) }/* We want to send a packet */ -static void baum_write_packet (BaumDriverState *baum,const uint8_t *buf,int len) +static void baum_write_packet (BaumChardev *baum,const uint8_t *buf,int len) { -CharDriverState *chr = (CharDriverState *)baum; +Chardev *chr = (Chardev *)baum;uint8_t io_buf[1 +2 * len], *cur = io_buf;int room; *cur++ =ESC ;
@@ -337,14 +337,14 @@ static void baum_write_packet(BaumDriverState *baum, const uint8_t *buf, int len/* Called when the other end seems to have a wrong idea of our display size */ static void baum_cellCount_timer_cb (void *opaque) { -BaumDriverState *baum = opaque; +BaumChardev *baum = opaque;uint8_t cell_count[] = { BAUM_RSP_CellCount, baum->x * baum->y };DPRINTF ("Timeout waiting for DisplayData, sending cell count\n" );baum_write_packet (baum, cell_count,sizeof (cell_count)); }/* Try to interpret a whole incoming packet */ -static int baum_eat_packet (BaumDriverState *baum,const uint8_t *buf,int len) +static int baum_eat_packet (BaumChardev *baum,const uint8_t *buf,int len) {const uint8_t *cur = buf;uint8_t req =0 ;
@@ -485,9 +485,9 @@ static int baum_eat_packet(BaumDriverState *baum, const uint8_t *buf, int len) }/* The other end is writing some data. Store it and try to interpret */ -static int baum_write (CharDriverState *chr,const uint8_t *buf,int len) +static int baum_write (Chardev *chr,const uint8_t *buf,int len) { -BaumDriverState *baum = (BaumDriverState *)chr; +BaumChardev *baum = (BaumChardev *)chr;int tocopy, cur, eaten, orig_len = len;if (!len)
@@ -526,14 +526,16 @@ static int baum_write(CharDriverState *chr, const uint8_t *buf, int len) }/* Send the key code to the other end */ -static void baum_send_key (BaumDriverState *baum,uint8_t type,uint8_t value) { +static void baum_send_key (BaumChardev *baum,uint8_t type,uint8_t value) +{uint8_t packet[] = { type, value };DPRINTF ("writing key %x %x\n" , type, value);baum_write_packet (baum, packet,sizeof (packet)); } -static void baum_send_key2 (BaumDriverState *baum,uint8_t type,uint8_t value, -uint8_t value2) { +static void baum_send_key2 (BaumChardev *baum,uint8_t type,uint8_t value, +uint8_t value2) +{uint8_t packet[] = { type, value, value2 };DPRINTF ("writing key %x %x\n" , type, value);baum_write_packet (baum, packet,sizeof (packet));
@@ -542,7 +544,7 @@ static void baum_send_key2(BaumDriverState *baum, uint8_t type, uint8_t value,/* We got some data on the BrlAPI socket */ static void baum_chr_read (void *opaque) { -BaumDriverState *baum = opaque; +BaumChardev *baum = opaque;brlapi_keyCode_t code;int ret;if (!baum->brlapi)
@@ -626,9 +628,9 @@ static void baum_chr_read(void *opaque) } } -static void baum_free (struct CharDriverState *chr) +static void baum_free (struct Chardev *chr) { -BaumDriverState *baum = (BaumDriverState *)chr; +BaumChardev *baum = (BaumChardev *)chr;timer_free (baum->cellCount_timer);if (baum->brlapi) {
@@ -637,23 +639,23 @@ static void baum_free(struct CharDriverState *chr) } } -static CharDriverState *chr_baum_init (const CharDriver *driver, -const char *id, -ChardevBackend *backend, -ChardevReturn *ret, -bool *be_opened, -Error **errp) +static Chardev *chr_baum_init (const CharDriver *driver, +const char *id, +ChardevBackend *backend, +ChardevReturn *ret, +bool *be_opened, +Error **errp) {ChardevCommon *common = backend->u.braille.data; -BaumDriverState *baum; -CharDriverState *chr; +BaumChardev *baum; +Chardev *chr;brlapi_handle_t *handle; chr =qemu_chr_alloc (driver, common, errp);if (!chr) {return NULL ; } - baum = (BaumDriverState *)chr; + baum = (BaumChardev *)chr; handle =g_malloc0 (brlapi_getHandleSize ()); baum->brlapi = handle;
@@ -681,7 +683,7 @@ fail_handle:static void register_types (void ) {static const CharDriver driver = { - .instance_size =sizeof (BaumDriverState), + .instance_size =sizeof (BaumChardev), .kind =CHARDEV_BACKEND_KIND_BRAILLE , .parse =NULL , .create = chr_baum_init, .chr_write = baum_write,
backends/msmouse.c +21 -21
105 lines not on this page · raw message
backends/rng-egd.c +2 -2
@@ -86,7 +86,7 @@ static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)static void rng_egd_opened (RngBackend *b,Error **errp) {RngEgd *s =RNG_EGD (b); -CharDriverState *chr; +Chardev *chr;if (s->chr_name ==NULL ) { error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
@@ -125,7 +125,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)static char *rng_egd_get_chardev (Object *obj,Error **errp) {RngEgd *s =RNG_EGD (obj); -CharDriverState *chr =qemu_chr_fe_get_driver (&s->chr); +Chardev *chr =qemu_chr_fe_get_driver (&s->chr);if (chr && chr->label) {return g_strdup (chr->label);
backends/testdev.c +14 -14
@@ -30,14 +30,14 @@#define BUF_SIZE 32typedef struct { -CharDriverState parent; +Chardev parent;uint8_t in_buf[32 ];int in_buf_used; -}TestdevCharState ; +}TestdevChardev ;/* Try to interpret a whole incoming packet */ -static int testdev_eat_packet (TestdevCharState *testdev) +static int testdev_eat_packet (TestdevChardev *testdev) {const uint8_t *cur = testdev->in_buf;int len = testdev->in_buf_used;
@@ -78,9 +78,9 @@ static int testdev_eat_packet(TestdevCharState *testdev) }/* The other end is writing some data. Store it and try to interpret */ -static int testdev_write (CharDriverState *chr,const uint8_t *buf,int len) +static int testdev_write (Chardev *chr,const uint8_t *buf,int len) { -TestdevCharState *testdev = (TestdevCharState *)chr; +TestdevChardev *testdev = (TestdevChardev *)chr;int tocopy, eaten, orig_len = len;while (len) {
@@ -103,15 +103,15 @@ static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)return orig_len; } -static CharDriverState *chr_testdev_init (const CharDriver *driver, -const char *id, -ChardevBackend *backend, -ChardevReturn *ret, -bool *be_opened, -Error **errp) +static Chardev *chr_testdev_init (const CharDriver *driver, +const char *id, +ChardevBackend *backend, +ChardevReturn *ret, +bool *be_opened, +Error **errp) { -TestdevCharState *testdev =g_new0 (TestdevCharState,1 );; -CharDriverState *chr = (CharDriverState *)testdev; +TestdevChardev *testdev =g_new0 (TestdevChardev,1 );; +Chardev *chr = (Chardev *)testdev; chr->driver = driver;
@@ -121,7 +121,7 @@ static CharDriverState *chr_testdev_init(const CharDriver *driver,static void register_types (void ) {static const CharDriver driver = { - .instance_size =sizeof (TestdevCharState), + .instance_size =sizeof (TestdevChardev), .kind =CHARDEV_BACKEND_KIND_TESTDEV , .parse =NULL , .create = chr_testdev_init, .chr_write = testdev_write,
gdbstub.c +6 -6
@@ -303,7 +303,7 @@ typedef struct GDBState {int running_state;#else CharBackend chr; -CharDriverState *mon_chr; +Chardev *mon_chr;#endif char syscall_buf[256 ];gdb_syscall_complete_cb current_syscall_cb;
@@ -1471,7 +1471,7 @@ void gdb_exit(CPUArchState *env, int code)GDBState *s;char buf[4 ];#ifndef CONFIG_USER_ONLY -CharDriverState *chr; +Chardev *chr;#endif s = gdbserver_state;
@@ -1696,7 +1696,7 @@ static void gdb_monitor_output(GDBState *s, const char *msg, int len)put_packet (s, buf); } -static int gdb_monitor_write (CharDriverState *chr,const uint8_t *buf,int len) +static int gdb_monitor_write (Chardev *chr,const uint8_t *buf,int len) {const char *p = (const char *)buf;int max_sz;
@@ -1727,11 +1727,11 @@ int gdbserver_start(const char *device) {GDBState *s;char gdbstub_device_name[128 ]; -CharDriverState *chr =NULL ; -CharDriverState *mon_chr; +Chardev *chr =NULL ; +Chardev *mon_chr;ChardevCommon common = {0 };static const CharDriver driver = { - .instance_size =sizeof (CharDriverState), + .instance_size =sizeof (Chardev), .kind =-1 , .chr_write = gdb_monitor_write };
hw/arm/fsl-imx25.c +1 -1
@@ -118,7 +118,7 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp) };if (i <MAX_SERIAL_PORTS ) { -CharDriverState *chr; +Chardev *chr; chr = serial_hds[i];
hw/arm/fsl-imx31.c +1 -1
@@ -107,7 +107,7 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp) };if (i <MAX_SERIAL_PORTS ) { -CharDriverState *chr; +Chardev *chr; chr = serial_hds[i];
hw/arm/fsl-imx6.c +1 -1
@@ -187,7 +187,7 @@ static void fsl_imx6_realize(DeviceState *dev, Error **errp) };if (i <MAX_SERIAL_PORTS ) { -CharDriverState *chr; +Chardev *chr; chr = serial_hds[i];
hw/arm/nseries.c +1 -1
@@ -786,7 +786,7 @@ static void n8x0_cbus_setup(struct n800_s *s)static void n8x0_uart_setup (struct n800_s *s) { -CharDriverState *radio =uart_hci_init (); +Chardev *radio =uart_hci_init ();qdev_connect_gpio_out (s->mpu->gpio,N8X0_BT_RESET_GPIO ,csrhci_pins_get (radio)[csrhci_pin_reset]);
hw/arm/omap2.c +1 -1
@@ -792,7 +792,7 @@ static const MemoryRegionOps omap_sti_fifo_ops = {static struct omap_sti_s *omap_sti_init (struct omap_target_agent_s *ta,MemoryRegion *sysmem,hwaddr channel_base,qemu_irq irq,omap_clk clk, -CharDriverState *chr) +Chardev *chr) { struct omap_sti_s *s = g_new0(struct omap_sti_s, 1);
hw/arm/pxa2xx.c +1 -1
@@ -2029,7 +2029,7 @@ static PXA2xxFIrState *pxa2xx_fir_init(MemoryRegion *sysmem,hwaddr base, qemu_irq irq, qemu_irq rx_dma, qemu_irq tx_dma, - CharDriverState *chr) + Chardev *chr) { DeviceState *dev;SysBusDevice *sbd;
hw/arm/virt.c +1 -1
@@ -626,7 +626,7 @@ static void create_gic(VirtBoardInfo *vbi, qemu_irq *pic, int type, }static void create_uart (const VirtBoardInfo *vbi,qemu_irq *pic,int uart, -MemoryRegion *mem,CharDriverState *chr) +MemoryRegion *mem,Chardev *chr) {char *nodename;hwaddr base = vbi->memmap[uart].base;
hw/bt/hci-csr.c +7 -7
@@ -29,7 +29,7 @@#include "qapi/error.h" struct csrhci_s { -CharDriverState parent; +Chardev parent;int enable;qemu_irq *pins;int pin_state;
@@ -79,7 +79,7 @@ enum {static inline void csrhci_fifo_wake (struct csrhci_s *s) { -CharDriverState *chr = (CharDriverState *)s; +Chardev *chr = (Chardev *)s;CharBackend *be = chr->be;if (!s->enable || !s->out_len)
@@ -313,7 +313,7 @@ static void csrhci_ready_for_next_inpkt(struct csrhci_s *s) s->in_hdr =INT_MAX ; } -static int csrhci_write (struct CharDriverState *chr, +static int csrhci_write (struct Chardev *chr,const uint8_t *buf,int len) {struct csrhci_s *s = (struct csrhci_s *)chr;
@@ -386,7 +386,7 @@ static void csrhci_out_hci_packet_acl(void *opaque,csrhci_fifo_wake (s); } -static int csrhci_ioctl (struct CharDriverState *chr,int cmd,void *arg) +static int csrhci_ioctl (struct Chardev *chr,int cmd,void *arg) {QEMUSerialSetParams *ssp;struct csrhci_s *s = (struct csrhci_s *) chr;
@@ -455,14 +455,14 @@ static void csrhci_pins(void *opaque, int line, int level) } } -qemu_irq *csrhci_pins_get (CharDriverState *chr) +qemu_irq *csrhci_pins_get (Chardev *chr) {struct csrhci_s *s = (struct csrhci_s *) chr;return s->pins; } -CharDriverState *uart_hci_init (void ) +Chardev *uart_hci_init (void ) {static const CharDriver hci_driver = { .instance_size =sizeof (struct csrhci_s ),
@@ -472,7 +472,7 @@ CharDriverState *uart_hci_init(void) };Error *err =NULL ;ChardevCommon common = {0 , }; -CharDriverState *chr =qemu_chr_alloc (&hci_driver, &common, &err); +Chardev *chr =qemu_chr_alloc (&hci_driver, &common, &err);struct csrhci_s *s = (struct csrhci_s *)chr;if (err) {
hw/char/escc.c +1 -1
@@ -689,7 +689,7 @@ static const VMStateDescription vmstate_escc = { };MemoryRegion *escc_init (hwaddr base,qemu_irq irqA,qemu_irq irqB, -CharDriverState *chrA,CharDriverState *chrB, +Chardev *chrA,Chardev *chrB,int clock,int it_shift) {DeviceState *dev;
hw/char/exynos4210_uart.c +1 -1
@@ -586,7 +586,7 @@ static const VMStateDescription vmstate_exynos4210_uart = {DeviceState *exynos4210_uart_create (hwaddr addr,int fifo_size,int channel, -CharDriverState *chr, +Chardev *chr,qemu_irq irq) {DeviceState *dev;
hw/char/imx_serial.c +1 -1
@@ -170,7 +170,7 @@ static void imx_serial_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) {IMXSerialState *s = (IMXSerialState *)opaque; -CharDriverState *chr =qemu_chr_fe_get_driver (&s->chr); +Chardev *chr =qemu_chr_fe_get_driver (&s->chr);unsigned char ch; DPRINTF("write(offset=0x%" HWADDR_PRIx ", value = 0x%x) to %s\n",
hw/char/mcf_uart.c +2 -2
@@ -275,7 +275,7 @@ static void mcf_uart_receive(void *opaque, const uint8_t *buf, int size)mcf_uart_push_byte (s, buf[0 ]); } -void *mcf_uart_init (qemu_irq irq,CharDriverState *chr) +void *mcf_uart_init (qemu_irq irq,Chardev *chr) {mcf_uart_state *s;
@@ -300,7 +300,7 @@ static const MemoryRegionOps mcf_uart_ops = {void mcf_uart_mm_init (MemoryRegion *sysmem,hwaddr base,qemu_irq irq, -CharDriverState *chr) +Chardev *chr) {mcf_uart_state *s;
hw/char/omap_uart.c +3 -3
@@ -54,7 +54,7 @@ void omap_uart_reset(struct omap_uart_s *s)struct omap_uart_s *omap_uart_init (hwaddr base,qemu_irq irq,omap_clk fclk,omap_clk iclk,qemu_irq txdma,qemu_irq rxdma, -const char *label,CharDriverState *chr) +const char *label,Chardev *chr) { struct omap_uart_s *s = g_new0(struct omap_uart_s, 1);
@@ -163,7 +163,7 @@ struct omap_uart_s *omap2_uart_init(MemoryRegion *sysmem,struct omap_target_agent_s *ta, qemu_irq irq, omap_clk fclk, omap_clk iclk, qemu_irq txdma, qemu_irq rxdma, - const char *label, CharDriverState *chr) + const char *label, Chardev *chr) { hwaddr base = omap_l4_attach(ta, 0, NULL); struct omap_uart_s *s = omap_uart_init(base, irq,
@@ -178,7 +178,7 @@ struct omap_uart_s *omap2_uart_init(MemoryRegion *sysmem,return s; } -void omap_uart_attach (struct omap_uart_s *s,CharDriverState *chr) +void omap_uart_attach (struct omap_uart_s *s,Chardev *chr) {/* TODO: Should reuse or destroy current s->serial */ s->serial = serial_mm_init(get_system_memory(), s->base, 2, s->irq,
hw/char/parallel.c +1 -1
@@ -603,7 +603,7 @@ static const MemoryRegionOps parallel_mm_ops = {/* If fd is zero, it means that the parallel device uses the console */ bool parallel_mm_init (MemoryRegion *address_space,hwaddr base,int it_shift,qemu_irq irq, -CharDriverState *chr) +Chardev *chr) {ParallelState *s;
hw/char/serial-isa.c +1 -1
@@ -121,7 +121,7 @@ static void serial_register_types(void)type_init (serial_register_types) -static void serial_isa_init(ISABus *bus, int index, CharDriverState *chr) +static void serial_isa_init(ISABus *bus, int index, Chardev *chr) {DeviceState *dev;ISADevice *isadev;
hw/char/serial.c +2 -2
@@ -927,7 +927,7 @@ const MemoryRegionOps serial_io_ops = { };SerialState *serial_init (int base,qemu_irq irq,int baudbase, -CharDriverState *chr,MemoryRegion *system_io) +Chardev *chr,MemoryRegion *system_io) {SerialState *s;
@@ -983,7 +983,7 @@ static const MemoryRegionOps serial_mm_ops[3] = {SerialState *serial_mm_init (MemoryRegion *address_space,hwaddr base,int it_shift,qemu_irq irq,int baudbase, -CharDriverState *chr,enum device_endian end) +Chardev *chr,enum device_endian end) {SerialState *s;
hw/char/sh_serial.c +1 -1
@@ -356,7 +356,7 @@ static const MemoryRegionOps sh_serial_ops = { void sh_serial_init(MemoryRegion *sysmem, hwaddr base, int feat, - uint32_t freq, CharDriverState *chr, + uint32_t freq, Chardev *chr, qemu_irq eri_source, qemu_irq rxi_source, qemu_irq txi_source,
hw/char/spapr_vty.c +1 -1
@@ -141,7 +141,7 @@ static target_ulong h_get_term_char(PowerPCCPU *cpu, sPAPRMachineState *spapr,return H_SUCCESS ; } -void spapr_vty_create (VIOsPAPRBus *bus,CharDriverState *chardev) +void spapr_vty_create (VIOsPAPRBus *bus,Chardev *chardev) {DeviceState *dev;
hw/char/virtio-console.c +1 -1
@@ -168,7 +168,7 @@ static void virtconsole_realize(DeviceState *dev, Error **errp)VirtIOSerialPort *port =VIRTIO_SERIAL_PORT (dev);VirtConsole *vcon =VIRTIO_CONSOLE (dev);VirtIOSerialPortClass *k =VIRTIO_SERIAL_PORT_GET_CLASS (dev); -CharDriverState *chr =qemu_chr_fe_get_driver (&vcon->chr); +Chardev *chr =qemu_chr_fe_get_driver (&vcon->chr);if (port->id ==0 && !k->is_console) { error_setg(errp, "Port number 0 on virtio-serial devices reserved "
hw/core/qdev-properties-system.c +2 -2
@@ -179,7 +179,7 @@ static void set_chr(Object *obj, Visitor *v, const char *name, void *opaque,Error *local_err =NULL ;Property *prop = opaque;CharBackend *be =qdev_get_prop_ptr (dev, prop); -CharDriverState *s; +Chardev *s;char *str;if (dev->realized) {
@@ -411,7 +411,7 @@ void qdev_prop_set_drive(DeviceState *dev, const char *name, }void qdev_prop_set_chr (DeviceState *dev,const char *name, -CharDriverState *value) +Chardev *value) {assert (!value || value->label); object_property_set_str(OBJECT(dev),
hw/display/milkymist-tmu2.c +1 -1
@@ -85,7 +85,7 @@ struct MilkymistTMU2State {SysBusDevice parent_obj;MemoryRegion regs_region; -CharDriverState *chr; +Chardev *chr;qemu_irq irq;uint32_t regs[R_MAX ];
hw/display/sm501.c +1 -1
@@ -1392,7 +1392,7 @@ static const GraphicHwOps sm501_ops = { };void sm501_init (MemoryRegion *address_space_mem,uint32_t base, -uint32_t local_mem_bytes,qemu_irq irq,CharDriverState *chr) +uint32_t local_mem_bytes,qemu_irq irq,Chardev *chr) {SM501State * s;DeviceState *dev;
hw/isa/isa-bus.c +1 -1
@@ -287,7 +287,7 @@ MemoryRegion *isa_address_space_io(ISADevice *dev)type_init (isabus_register_types) -static void parallel_init(ISABus *bus, int index, CharDriverState *chr) +static void parallel_init(ISABus *bus, int index, Chardev *chr) {DeviceState *dev;ISADevice *isadev;
hw/isa/pc87312.c +1 -1
@@ -268,7 +268,7 @@ static void pc87312_realize(DeviceState *dev, Error **errp)DeviceState *d;ISADevice *isa;ISABus *bus; -CharDriverState *chr; +Chardev *chr;DriveInfo *drive;char name[5 ];int i;
hw/mips/mips_malta.c +2 -2
@@ -551,10 +551,10 @@ static void malta_fgpa_display_event(void *opaque, int event) }static MaltaFPGAState *malta_fpga_init (MemoryRegion *address_space, -hwaddr base,qemu_irq uart_irq,CharDriverState *uart_chr) +hwaddr base,qemu_irq uart_irq,Chardev *uart_chr) {MaltaFPGAState *s; -CharDriverState *chr; +Chardev *chr; s = (MaltaFPGAState *)g_malloc0 (sizeof (MaltaFPGAState));
hw/misc/ivshmem.c +1 -1
@@ -868,7 +868,7 @@ static void ivshmem_common_realize(PCIDevice *dev, Error **errp) s->ivshmem_bar2 =host_memory_backend_get_memory (s->hostmem, &error_abort); }else { -CharDriverState *chr =qemu_chr_fe_get_driver (&s->server_chr); +Chardev *chr =qemu_chr_fe_get_driver (&s->server_chr);assert (chr); IVSHMEM_DPRINTF("using shared memory server (socket = %s)\n",
hw/misc/milkymist-pfpu.c +1 -1
@@ -125,7 +125,7 @@ struct MilkymistPFPUState {SysBusDevice parent_obj;MemoryRegion regs_region; -CharDriverState *chr; +Chardev *chr;qemu_irq irq;uint32_t regs[R_MAX ];
hw/usb/ccid-card-passthru.c +1 -1
@@ -264,7 +264,7 @@ static void ccid_card_vscard_handle_message(PassthruState *card,static void ccid_card_vscard_drop_connection (PassthruState *card) { -CharDriverState *chr =qemu_chr_fe_get_driver (&card->cs); +Chardev *chr =qemu_chr_fe_get_driver (&card->cs);qemu_chr_fe_deinit (&card->cs);qemu_chr_delete (chr);
hw/usb/dev-serial.c +3 -3
@@ -483,7 +483,7 @@ static void usb_serial_realize(USBDevice *dev, Error **errp) {USBSerialState *s =USB_SERIAL_DEV (dev);Error *local_err =NULL ; -CharDriverState *chr =qemu_chr_fe_get_driver (&s->cs); +Chardev *chr =qemu_chr_fe_get_driver (&s->cs);usb_desc_create_serial (dev);usb_desc_init (dev);
@@ -512,7 +512,7 @@ static void usb_serial_realize(USBDevice *dev, Error **errp)static USBDevice *usb_serial_init (USBBus *bus,const char *filename) {USBDevice *dev; -CharDriverState *cdrv; +Chardev *cdrv;uint32_t vendorid =0 , productid =0 ;char label[32 ];static int index;
@@ -564,7 +564,7 @@ static USBDevice *usb_serial_init(USBBus *bus, const char *filename)static USBDevice *usb_braille_init (USBBus *bus,const char *unused) {USBDevice *dev; -CharDriverState *cdrv; +Chardev *cdrv; cdrv =qemu_chr_new ("braille" ,"braille" );if (!cdrv)
hw/usb/redirect.c +2 -2
@@ -284,7 +284,7 @@ static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond,static int usbredir_write (void *priv,uint8_t *data,int count) {USBRedirDevice *dev = priv; -CharDriverState *chr =qemu_chr_fe_get_driver (&dev->cs); +Chardev *chr =qemu_chr_fe_get_driver (&dev->cs);int r;if (!chr->be_open) {
@@ -1430,7 +1430,7 @@ static void usbredir_cleanup_device_queues(USBRedirDevice *dev)static void usbredir_handle_destroy (USBDevice *udev) {USBRedirDevice *dev =USB_REDIRECT (udev); -CharDriverState *chr =qemu_chr_fe_get_driver (&dev->cs); +Chardev *chr =qemu_chr_fe_get_driver (&dev->cs);qemu_chr_fe_deinit (&dev->cs);qemu_chr_delete (chr);
monitor.c +2 -2
@@ -3193,7 +3193,7 @@ static void ringbuf_completion(ReadLineState *rs, const char *str)ChardevInfo *chr_info = list->value;if (!strncmp (chr_info->label, str, len)) { -CharDriverState *chr =qemu_chr_find (chr_info->label); +Chardev *chr =qemu_chr_find (chr_info->label);if (chr &&qemu_chr_is_ringbuf (chr)) {readline_add_completion (rs, chr_info->label); }
@@ -3981,7 +3981,7 @@ static void __attribute__((constructor)) monitor_lock_init(void)qemu_mutex_init (&monitor_lock); } -void monitor_init (CharDriverState *chr,int flags) +void monitor_init (Chardev *chr,int flags) {static int is_first_init =1 ;Monitor *mon;
net/colo-compare.c +2 -2
@@ -564,7 +564,7 @@ static void compare_sec_rs_finalize(SocketReadState *sec_rs)* Return 0 is success. * Return 1 is failed. */ -static int find_and_check_chardev (CharDriverState **chr, +static int find_and_check_chardev (Chardev **chr,char *chr_name,Error **errp) {
@@ -611,7 +611,7 @@ static void check_old_packet_regular(void *opaque)static void colo_compare_complete (UserCreatable *uc,Error **errp) {CompareState *s =COLO_COMPARE (uc); -CharDriverState *chr; +Chardev *chr;char thread_name[64 ];static int compare_id;
net/filter-mirror.c +2 -2
@@ -191,7 +191,7 @@ static void filter_redirector_cleanup(NetFilterState *nf)static void filter_mirror_setup (NetFilterState *nf,Error **errp) {MirrorState *s =FILTER_MIRROR (nf); -CharDriverState *chr; +Chardev *chr;if (!s->outdev) { error_setg(errp, "filter mirror needs 'outdev' "
@@ -220,7 +220,7 @@ static void redirector_rs_finalize(SocketReadState *rs)static void filter_redirector_setup (NetFilterState *nf,Error **errp) {MirrorState *s =FILTER_REDIRECTOR (nf); -CharDriverState *chr; +Chardev *chr;if (!s->indev && !s->outdev) { error_setg(errp, "filter redirector needs 'indev' or "
net/slirp.c +1 -1
@@ -748,7 +748,7 @@ static int slirp_guestfwd(SlirpState *s, const char *config_str, } }else {Error *err =NULL ; -CharDriverState *chr =qemu_chr_new (buf, p); +Chardev *chr =qemu_chr_new (buf, p);if (!chr) {error_report ("could not open guest forwarding device '%s'" , buf);
net/vhost-user.c +5 -5
@@ -195,7 +195,7 @@ static void net_vhost_user_event(void *opaque, int event)const char *name = opaque;NetClientState *ncs[MAX_QUEUE_NUM ];VhostUserState *s; -CharDriverState *chr; +Chardev *chr;Error *err =NULL ;int queues;
@@ -232,7 +232,7 @@ static void net_vhost_user_event(void *opaque, int event) }static int net_vhost_user_init (NetClientState *peer,const char *device, -const char *name,CharDriverState *chr, +const char *name,Chardev *chr,int queues) {Error *err =NULL ;
@@ -274,10 +274,10 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,return 0 ; } -static CharDriverState *net_vhost_claim_chardev ( +static Chardev *net_vhost_claim_chardev (const NetdevVhostUserOptions *opts,Error **errp) { -CharDriverState *chr =qemu_chr_find (opts->chardev); +Chardev *chr =qemu_chr_find (opts->chardev);if (chr ==NULL ) {error_setg (errp,"chardev \"%s\" not found" , opts->chardev);
@@ -324,7 +324,7 @@ int net_init_vhost_user(const Netdev *netdev, const char *name, {int queues;const NetdevVhostUserOptions *vhost_user_opts; -CharDriverState *chr; +Chardev *chr;assert (netdev->type ==NET_CLIENT_DRIVER_VHOST_USER ); vhost_user_opts = &netdev->u.vhost_user;
qemu-char.c +420 -419
2,106 lines not on this page · raw message
qmp.c +1 -1
@@ -616,7 +616,7 @@ void qmp_add_client(const char *protocol, const char *fdname, bool has_skipauth, bool skipauth, bool has_tls, bool tls, Error **errp) { - CharDriverState *s; + Chardev *s;int fd; fd =monitor_get_fd (cur_mon, fdname, errp);
qtest.c +1 -1
@@ -668,7 +668,7 @@ static int qtest_init_accel(MachineState *ms)void qtest_init (const char *qtest_chrdev,const char *qtest_log,Error **errp) { -CharDriverState *chr; +Chardev *chr; chr =qemu_chr_new ("qtest" , qtest_chrdev);
replay/replay-char.c +4 -4
@@ -18,7 +18,7 @@/* Char drivers that generate qemu_chr_be_write events that should be saved into the log. */ -static CharDriverState **char_drivers; +static Chardev **char_drivers;static int drivers_count;/* Char event attributes. */
@@ -28,7 +28,7 @@ typedef struct CharEvent {size_t len; } CharEvent; -static int find_char_driver (CharDriverState *chr) +static int find_char_driver (Chardev *chr) {int i =0 ;for ( ; i < drivers_count ; ++i) {
@@ -39,7 +39,7 @@ static int find_char_driver(CharDriverState *chr)return -1 ; } -void replay_register_char_driver (CharDriverState *chr) +void replay_register_char_driver (Chardev *chr) {if (replay_mode ==REPLAY_MODE_NONE ) {return ;
@@ -49,7 +49,7 @@ void replay_register_char_driver(CharDriverState *chr) char_drivers[drivers_count++] = chr; } -void replay_chr_be_write (CharDriverState *s,uint8_t *buf,int len) +void replay_chr_be_write (Chardev *s,uint8_t *buf,int len) {CharEvent *event =g_malloc0 (sizeof (CharEvent));
spice-qemu-char.c +40 -40
29 lines not on this page · raw message
include/sysemu/char.h +47 -47
94 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.11.0
Part of the diff is not shown here. View the raw message.
Re: [Qemu-devel] [PATCH 10/54] char: rename CharDriverState Chardev
Quoted 1 line · Marc-André Lureau
Uniformify chardev type name.
"Uniformify" - cute :) I might have written:
Pick a uniform chardev type name.
instead of inventing a word, but you're call on whether to keep it.
Quoted 4 lines
Signed-off-by Marc-André Lureau <[email protected]>---
81 files changed, 728 insertions(+), 725 deletions(-)
Big, but mostly mechanical.
backends/baum.c static int baum_deferred_init(BaumDriverState *baum) · line 271
Quoted 6 lines
backends/baum.c +1
-1@@ -268,9 +268,9 @@ static int baum_deferred_init(BaumDriverState *baum) }/* The serial port can receive more of our data */ -static void baum_accept_input (struct CharDriverState *chr) +static void baum_accept_input (struct Chardev *chr)
Do we still need the 'struct' in this parameter list? Separate cleanup may be okay.
A grep on your github chrfe branch shows that it looks like you got them all, so
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 11/54 char: rename TCPChardev and NetChardev
In reply to Marc-André LureauRename the types to follow the name of the chardev kind.
- socket: TCPChardev -> SocketChardev
- udp: NetChardev -> UdpChardev
--- qemu-char.c | 74 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 37 deletions(-)
qemu-char.c +37 -37
@@ -2683,12 +2683,12 @@ typedef struct {int bufcnt;int bufptr;int max_size; -} NetChardev; +} UdpChardev;/* Called with chr_write_lock held. */ static int udp_chr_write (Chardev *chr,const uint8_t *buf,int len) { -NetChardev *s = (NetChardev *)chr; +UdpChardev *s = (UdpChardev *)chr;return qio_channel_write ( s->ioc, (const char *)buf, len,NULL );
@@ -2697,7 +2697,7 @@ static int udp_chr_write(Chardev *chr, const uint8_t *buf, int len)static int udp_chr_read_poll (void *opaque) {Chardev *chr = opaque; -NetChardev *s = opaque; +UdpChardev *s = opaque; s->max_size =qemu_chr_be_can_write (chr);
@@ -2715,7 +2715,7 @@ static int udp_chr_read_poll(void *opaque)static gboolean udp_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) {Chardev *chr = opaque; -NetChardev *s = opaque; +UdpChardev *s = opaque;ssize_t ret;if (s->max_size ==0 ) {
@@ -2742,7 +2742,7 @@ static gboolean udp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)static void udp_chr_update_read_handler (Chardev *chr,GMainContext *context) { -NetChardev *s = (NetChardev *)chr; +UdpChardev *s = (UdpChardev *)chr;remove_fd_in_watch (chr);if (s->ioc) {
@@ -2755,7 +2755,7 @@ static void udp_chr_update_read_handler(Chardev *chr,static void udp_chr_free (Chardev *chr) { -NetChardev *s = (NetChardev *)chr; +UdpChardev *s = (UdpChardev *)chr;remove_fd_in_watch (chr);if (s->ioc) {
@@ -2791,13 +2791,13 @@ typedef struct {guint reconnect_timer;int64_t reconnect_time;bool connect_err_reported; -} TCPChardev; +} SocketChardev;static gboolean socket_reconnect_timeout (gpointer opaque);static void qemu_chr_socket_restart_timer (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;char *name;assert (s->connected ==0 );
@@ -2811,7 +2811,7 @@ static void qemu_chr_socket_restart_timer(Chardev *chr)static void check_report_connect_error (Chardev *chr,Error *err) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (!s->connect_err_reported) { error_report("Unable to connect character device %s: %s",
@@ -2828,7 +2828,7 @@ static gboolean tcp_chr_accept(QIOChannel *chan,/* Called with chr_write_lock held. */ static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (s->connected) { int ret = io_channel_send_full(s->ioc, buf, len,
@@ -2852,7 +2852,7 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)static int tcp_chr_read_poll (void *opaque) {Chardev *chr = opaque; -TCPChardev *s = opaque; +SocketChardev *s = opaque;if (!s->connected)return 0 ; s->max_size =qemu_chr_be_can_write (chr);
@@ -2862,7 +2862,7 @@ static int tcp_chr_read_poll(void *opaque)#define IAC 255#define IAC_BREAK 243static void tcp_chr_process_IAC_bytes (Chardev *chr, -TCPChardev *s, +SocketChardev *s,uint8_t *buf,int *size) {/* Handle any telnet client's basic IAC options to satisfy char by
@@ -2911,7 +2911,7 @@ static void tcp_chr_process_IAC_bytes(Chardev *chr,static int tcp_get_msgfds (Chardev *chr,int *fds,int num) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
@@ -2937,7 +2937,7 @@ static int tcp_get_msgfds(Chardev *chr, int *fds, int num)static int tcp_set_msgfds (Chardev *chr,int *fds,int num) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;/* clear old pending fd array */ g_free (s->write_msgfds);
@@ -2962,7 +2962,7 @@ static int tcp_set_msgfds(Chardev *chr, int *fds, int num)static ssize_t tcp_chr_recv (Chardev *chr,char *buf,size_t len) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;struct iovec iov = { .iov_base = buf, .iov_len = len };int ret;size_t i;
@@ -3019,13 +3019,13 @@ static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)static GSource *tcp_chr_add_watch (Chardev *chr,GIOCondition cond) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;return qio_channel_create_watch (s->ioc, cond); }static void tcp_chr_free_connection (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;int i;if (!s->connected) {
@@ -3054,7 +3054,7 @@ static void tcp_chr_free_connection(Chardev *chr)static void tcp_chr_disconnect (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (!s->connected) {return ;
@@ -3077,7 +3077,7 @@ static void tcp_chr_disconnect(Chardev *chr)static gboolean tcp_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) {Chardev *chr = opaque; -TCPChardev *s = opaque; +SocketChardev *s = opaque;uint8_t buf[READ_BUF_LEN ];int len, size;
@@ -3103,7 +3103,7 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)static int tcp_chr_sync_read (Chardev *chr,const uint8_t *buf,int len) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;int size;if (!s->connected) {
@@ -3122,7 +3122,7 @@ static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)static void tcp_chr_connect (void *opaque) {Chardev *chr = opaque; -TCPChardev *s = opaque; +SocketChardev *s = opaque;g_free (chr->filename); chr->filename = sockaddr_to_str(
@@ -3143,7 +3143,7 @@ static void tcp_chr_connect(void *opaque)static void tcp_chr_update_read_handler (Chardev *chr,GMainContext *context) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (!s->connected) {return ;
@@ -3194,7 +3194,7 @@ static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,static void tcp_chr_telnet_init (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;TCPCharDriverTelnetInit *init =g_new0 (TCPCharDriverTelnetInit,1 );size_t n =0 ;
@@ -3230,7 +3230,7 @@ static void tcp_chr_tls_handshake(Object *source, gpointer user_data) {Chardev *chr = user_data; -TCPChardev *s = user_data; +SocketChardev *s = user_data;if (err) {tcp_chr_disconnect (chr);
@@ -3246,7 +3246,7 @@ static void tcp_chr_tls_handshake(Object *source,static void tcp_chr_tls_init (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;QIOChannelTLS *tioc;Error *err =NULL ;gchar *name;
@@ -3285,7 +3285,7 @@ static void tcp_chr_tls_init(Chardev *chr)static void tcp_chr_set_client_ioc_name (Chardev *chr,QIOChannelSocket *sioc) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;char *name; name = g_strdup_printf("chardev-tcp-%s-%s", s->is_listen ? "server" : "client",
@@ -3297,7 +3297,7 @@ static void tcp_chr_set_client_ioc_name(Chardev *chr,static int tcp_chr_new_client (Chardev *chr,QIOChannelSocket *sioc) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (s->ioc !=NULL ) {return -1 ;
@@ -3369,7 +3369,7 @@ static gboolean tcp_chr_accept(QIOChannel *channel,static int tcp_chr_wait_connected (Chardev *chr,Error **errp) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;QIOChannelSocket *sioc;/* It can't wait on s->connected, since it is set asynchronously
@@ -3417,7 +3417,7 @@ int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)static void tcp_chr_free (Chardev *chr) { -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;tcp_chr_free_connection (chr);
@@ -3445,7 +3445,7 @@ static void qemu_chr_socket_connected(Object *src, Error *err, void *opaque) {QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (src);Chardev *chr = opaque; -TCPChardev *s = (TCPChardev *)chr; +SocketChardev *s = (SocketChardev *)chr;if (err) {check_report_connect_error (chr, err);
@@ -4718,7 +4718,7 @@ static const CharDriver serial_driver = {static gboolean socket_reconnect_timeout (gpointer opaque) {Chardev *chr = opaque; -TCPChardev *s = opaque; +SocketChardev *s = opaque;QIOChannelSocket *sioc; s->reconnect_timer =0 ;
@@ -4744,7 +4744,7 @@ static Chardev *qmp_chardev_open_socket(const CharDriver *driver, Error **errp) {Chardev *chr; -TCPChardev *s; +SocketChardev *s;ChardevSocket *sock = backend->u.socket.data;SocketAddress *addr = sock->addr;bool do_nodelay = sock->has_nodelay ? sock->nodelay : false;
@@ -4759,7 +4759,7 @@ static Chardev *qmp_chardev_open_socket(const CharDriver *driver,if (!chr) {return NULL ; } - s = (TCPChardev *)chr; + s = (SocketChardev *)chr; s->is_unix = addr->type ==SOCKET_ADDRESS_KIND_UNIX ; s->is_listen = is_listen;
@@ -4866,7 +4866,7 @@ static Chardev *qmp_chardev_open_socket(const CharDriver *driver, }static const CharDriver socket_driver = { - .instance_size =sizeof (TCPChardev), + .instance_size =sizeof (SocketChardev), .kind =CHARDEV_BACKEND_KIND_SOCKET , .parse = qemu_chr_parse_socket, .create = qmp_chardev_open_socket, .chr_wait_connected = tcp_chr_wait_connected,
@@ -4893,7 +4893,7 @@ static Chardev *qmp_chardev_open_udp(const CharDriver *driver,QIOChannelSocket *sioc =qio_channel_socket_new ();char *name;Chardev *chr; -NetChardev *s; +UdpChardev *s; if (qio_channel_socket_dgram_sync(sioc, udp->local, udp->remote,
@@ -4911,7 +4911,7 @@ static Chardev *qmp_chardev_open_udp(const CharDriver *driver,qio_channel_set_name (QIO_CHANNEL (sioc), name);g_free (name); - s = (NetChardev *)chr; + s = (UdpChardev *)chr; s->ioc =QIO_CHANNEL (sioc); s->bufcnt =0 ; s->bufptr =0 ;
@@ -4922,7 +4922,7 @@ static Chardev *qmp_chardev_open_udp(const CharDriver *driver, }static const CharDriver udp_driver = { - .instance_size =sizeof (NetChardev), + .instance_size =sizeof (UdpChardev), .kind =CHARDEV_BACKEND_KIND_UDP , .parse = qemu_chr_parse_udp, .create = qmp_chardev_open_udp, .chr_write = udp_chr_write,
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 11/54] char: rename TCPChardev and NetChardev
Quoted 8 lines · Marc-André Lureau
Rename the types to follow the name of the chardev kind.
- socket: TCPChardev -> SocketChardev
- udp: NetChardev -> UdpChardevSigned-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 74 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 37 deletions(-)
Mechanical.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 12/54 spice-char: improve error reporting
In reply to Marc-André LureauSet errp to report errors up.
Use error_report() to give hints about parameters on the right monitor, instead of a direct fprintf() call.
--- spice-qemu-char.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
spice-qemu-char.c +9 -20
@@ -2,6 +2,7 @@#include "trace.h" #include "ui/qemu-spice.h" #include "sysemu/char.h" +#include "qemu/error-report.h" #include <spice.h> #include <spice/protocol.h>
@@ -239,23 +240,6 @@ static void spice_port_set_fe_open(struct Chardev *chr, int fe_open)#endif } -static void print_allowed_subtypes (void ) -{ -const char ** psubtype; -int i; - -fprintf (stderr,"allowed names: " ); -for (i=0 , psubtype =spice_server_char_device_recognized_subtypes (); - *psubtype !=NULL ; ++psubtype, ++i) { -if (i ==0 ) { -fprintf (stderr,"%s" , *psubtype); - }else { -fprintf (stderr,", %s" , *psubtype); - } - } -fprintf (stderr,"\n" ); -} -static void spice_chr_accept_input (struct Chardev *chr) {SpiceChardev *s = (SpiceChardev *)chr;
@@ -302,8 +286,13 @@ static Chardev *qemu_chr_open_spice_vmc(const CharDriver *driver, } }if (*psubtype ==NULL ) { -fprintf (stderr,"spice-qemu-char: unsupported type: %s\n" , type); -print_allowed_subtypes (); +char *subtypes =g_strjoinv (", " , + (gchar **)spice_server_char_device_recognized_subtypes ()); + +error_setg (errp,"unsupported type name: %s" , type); +error_report ("allowed spice char type names: %s" , subtypes); + +g_free (subtypes);return NULL ; }
@@ -326,7 +315,7 @@ static Chardev *qemu_chr_open_spice_port(const CharDriver *driver,SpiceChardev *s;if (name ==NULL ) { -fprintf (stderr,"spice-qemu-char: missing name parameter\n" ); +error_setg (errp,"missing name parameter" );return NULL ; }
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 12/54] spice-char: improve error reporting
Quoted 10 lines · Marc-André Lureau
Set errp to report errors up.
Use
error_report()to give hints about parameters on the right monitor, instead of a directfprintf()call.Signed-off-by Marc-André Lureau <[email protected]>--- spice-qemu-char.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
And it's shorter! I like it.
spice-qemu-char.c static Chardev *qemu_chr_open_spice_vmc(const CharDriver *driver, · line 293
Quoted 11 lines
spice-qemu-char.c +5
-2@@ -302,8 +286,13 @@ static Chardev *qemu_chr_open_spice_vmc(const CharDriver *driver, } }if (*psubtype ==NULL ) { -fprintf (stderr,"spice-qemu-char: unsupported type: %s\n" , type); -print_allowed_subtypes (); +char *subtypes =g_strjoinv (", " , + (gchar **)spice_server_char_device_recognized_subtypes ()); + +error_setg (errp,"unsupported type name: %s" , type); +error_report ("allowed spice char type names: %s" , subtypes);
However, I'm not sure if error_setg() followed by error_report() is correct; should you be using error_append_hint() instead? Markus?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 12/54] spice-char: improve error reporting
Hi
Quoted 28 lines · Eric Blake
Quoted 10 lines · Marc-André Lureau
Set errp to report errors up.
Use
error_report()to give hints about parameters on the right monitor, instead of a directfprintf()call.Signed-off-by Marc-André Lureau <[email protected]>--- spice-qemu-char.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)And it's shorter! I like it.
Review static Chardev *qemu_chr_open_spice_vmc(const
Quoted 1 line
(quoted diff) +0
-0@@ -302,8 +286,13 @@ static Chardev *qemu_chr_open_spice_vmc(constCharDriver *driver,
spice-qemu-char.c static Chardev *qemu_chr_open_spice_vmc(const CharDriver *driver,
Quoted 4 lines
spice-qemu-char.c +0
-1} }if (*psubtype ==NULL ) { - fprintf(stderr, "spice-qemu-char: unsupported type: %s\n",type);
spice-qemu-char.c static Chardev *qemu_chr_open_spice_vmc(const CharDriver *driver, · line 293
Quoted 6 lines
spice-qemu-char.c +5
-1-print_allowed_subtypes (); +char *subtypes =g_strjoinv (", " , + (gchar **)spice_server_char_device_recognized_subtypes ()); + +error_setg (errp,"unsupported type name: %s" , type); +error_report ("allowed spice char type names: %s" , subtypes);
Quoted 4 lines
However, I'm not sure if
error_setg()followed byerror_report()is correct; should you be usingerror_append_hint()instead? Markus?
That doesn't work well with command line errors. error_vprintf_unless_qmp() doesn't print if !cur_mon. I sent a patch on the ML, and I'll follow your suggestion.
Signature · 1 line
Marc-André Lureau
PATCH 13/54 char: use error_report()
In reply to Marc-André LureauPrefer error_report() over fprintf(stderr..)
--- qemu-char.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)
qemu-char.c +15 -12
@@ -451,8 +451,8 @@ int qemu_chr_fe_get_msgfd(CharBackend *be)int fd;int res = (qemu_chr_fe_get_msgfds (be, &fd,1 ) ==1 ) ? fd :-1 ;if (s &&qemu_chr_replay (s)) { -fprintf (stderr, -"Replay: get msgfd is not supported for serial devices yet\n" ); +error_report ("Replay: get msgfd is not supported " +"for serial devices yet" );exit (1 ); }return res;
@@ -1674,8 +1674,8 @@ static Chardev *qemu_chr_open_pty(const CharDriver *driver, ret->pty =g_strdup (pty_name); ret->has_pty = true; -fprintf (stderr,"char device redirected to %s (label %s)\n" , - pty_name, id); +error_report ("char device redirected to %s (label %s)" , + pty_name, id); s = (PtyChardev *)chr; s->ioc =QIO_CHANNEL (qio_channel_file_new_fd (master_fd));
@@ -3376,8 +3376,8 @@ static int tcp_chr_wait_connected(Chardev *chr, Error **errp)* in TLS and telnet cases, only wait for an accepted socket */ while (!s->ioc) {if (s->is_listen) { -fprintf (stderr,"QEMU waiting for connection on: %s\n" , - chr->filename); +error_report ("QEMU waiting for connection on: %s" , + chr->filename);qio_channel_set_blocking (QIO_CHANNEL (s->listen_ioc), true,NULL );tcp_chr_accept (QIO_CHANNEL (s->listen_ioc),G_IO_IN , chr);qio_channel_set_blocking (QIO_CHANNEL (s->listen_ioc), false,NULL );
@@ -4130,17 +4130,20 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, }if (is_help_option (qemu_opt_get (opts,"backend" ))) { -fprintf (stderr,"Available chardev backend types:\n" ); +GString *str =g_string_new ("" );for (i =0 ; i <ARRAY_SIZE (backends); i++) { cd = backends[i];if (cd) { -fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +g_string_append_printf (str,"\n%s" , ChardevBackendKind_lookup[cd->kind]);if (cd->alias) { -fprintf (stderr,"%s\n" , cd->alias); +g_string_append_printf (str,"\n%s" , cd->alias); } } } -exit (!is_help_option (qemu_opt_get (opts,"backend" ))); + +error_report ("Available chardev backend types: %s" , str->str); +g_string_free (str, true); +exit (0 ); }if (id ==NULL ) {
@@ -4255,8 +4258,8 @@ Chardev *qemu_chr_new(const char *label, const char *filename)qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_REPLAY ); }if (qemu_chr_replay (chr) && chr->driver->chr_ioctl) { -fprintf (stderr, -"Replay: ioctl is not supported for serial devices yet\n" ); +error_report ("Replay: ioctl is not supported " +"for serial devices yet" ); }replay_register_char_driver (chr); }
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 13/54] char: use error_report()
Quoted 19 lines · Marc-André Lureau
Prefer
error_report()over fprintf(stderr..)Signed-off-by Marc-André Lureau <[email protected]>--- qemu-char.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-)qemu-char.c +2
-2@@ -4130,17 +4130,20 @@ Chardev *qemu_chr_new_from_opts(QemuOpts *opts, }if (is_help_option (qemu_opt_get (opts,"backend" ))) { -fprintf (stderr,"Available chardev backend types:\n" ); +GString *str =g_string_new ("" );for (i =0 ; i <ARRAY_SIZE (backends); i++) { cd = backends[i];if (cd) { -fprintf (stderr,"%s\n" , ChardevBackendKind_lookup[cd->kind]); +g_string_append_printf (str,"\n%s" , ChardevBackendKind_lookup[cd->kind]);if (cd->alias) {
qemu-char.c Chardev *qemu_chr_new_from_opts(QemuOpts *opts, · line 4146
Quoted 10 lines
qemu-char.c +5
-2-fprintf (stderr,"%s\n" , cd->alias); +g_string_append_printf (str,"\n%s" , cd->alias); } } } -exit (!is_help_option (qemu_opt_get (opts,"backend" ))); + +error_report ("Available chardev backend types: %s" , str->str); +g_string_free (str, true); +exit (0 );
I suppose the g_string_free() could be elided due to the pending exit(), but it's fine to keep it for lint purposes. Nice simplification of the error status.
qemu-char.c Chardev *qemu_chr_new(const char *label, const char *filename) · line 4264
Quoted 13 lines
qemu-char.c +2
-2}if (id ==NULL ) {@@ -4255,8 +4258,8 @@ Chardev *qemu_chr_new(const char *label, const char *filename)qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_REPLAY ); }if (qemu_chr_replay (chr) && chr->driver->chr_ioctl) { -fprintf (stderr, -"Replay: ioctl is not supported for serial devices yet\n" ); +error_report ("Replay: ioctl is not supported " +"for serial devices yet" ); }replay_register_char_driver (chr);
Rather gross that we print the message but plow on anyways, but that's not this patch's problem.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 14/54 gtk: overwrite the console.c char driver
In reply to Marc-André LureauInstead of registering a vc handler to allocate the Gtk VC Chardev, overwrite the console.c char driver.
A later patch, when switching to QOM, will register a default console vc QOM class if none has been registered before.
--- ui/console.c | 24 +++++++----------------- ui/gtk.c | 30 +++++++++++++++++++++--------- include/sysemu/char.h | 4 +--- 3 files changed, 29 insertions(+), 29 deletions(-)
ui/console.c +7 -17
@@ -2047,8 +2047,12 @@ static void text_console_do_init(Chardev *chr, DisplayState *ds)static const CharDriver vc_driver; -static Chardev *text_console_init (ChardevVC *vc,Error **errp) +static Chardev *vc_init (const CharDriver *driver, +const char *id,ChardevBackend *backend, +ChardevReturn *ret,bool *be_opened, +Error **errp) { +ChardevVC *vc = backend->u.vc.data;ChardevCommon *common =qapi_ChardevVC_base (vc);Chardev *chr;VCChardev *drv;
@@ -2094,26 +2098,13 @@ static Chardev *text_console_init(ChardevVC *vc, Error **errp)if (display_state) {text_console_do_init (chr, display_state); } -return chr; -} -static VcHandler *vc_handler = text_console_init; - -static Chardev *vc_init (const CharDriver *driver, -const char *id,ChardevBackend *backend, -ChardevReturn *ret,bool *be_opened, -Error **errp) -{/* console/chardev init sometimes completes elsewhere in a 2nd * stage, so defer OPENED events until they are fully initialized */ *be_opened = false; -return vc_handler (backend->u.vc.data, errp); -} -void register_vc_handler (VcHandler *handler) -{ - vc_handler = handler; +return chr; }void qemu_console_resize (QemuConsole *s,int width,int height)
@@ -2151,8 +2142,7 @@ PixelFormat qemu_default_pixelformat(int bpp)return pf; } -static void qemu_chr_parse_vc (QemuOpts *opts,ChardevBackend *backend, -Error **errp) +void qemu_chr_parse_vc (QemuOpts *opts,ChardevBackend *backend,Error **errp) {int val;ChardevVC *vc;
ui/gtk.c +21 -9
@@ -1702,16 +1702,14 @@ static void gd_vc_chr_set_echo(Chardev *chr, bool echo)static int nb_vcs;static Chardev *vcs[MAX_VCS ]; +static const CharDriver gd_vc_driver; -static Chardev *gd_vc_handler (ChardevVC *vc,Error **errp) +static Chardev *vc_init (const CharDriver *driver, +const char *id,ChardevBackend *backend, +ChardevReturn *ret,bool *be_opened, +Error **errp) { -static const CharDriver gd_vc_driver = { - .instance_size =sizeof (VCChardev), - .kind =CHARDEV_BACKEND_KIND_VC , - .chr_write = gd_vc_chr_write, - .chr_set_echo = gd_vc_chr_set_echo, - }; - +ChardevVC *vc = backend->u.vc.data;ChardevCommon *common =qapi_ChardevVC_base (vc);Chardev *chr;
@@ -1727,9 +1725,22 @@ static Chardev *gd_vc_handler(ChardevVC *vc, Error **errp) vcs[nb_vcs++] = chr; +/* console/chardev init sometimes completes elsewhere in a 2nd +* stage, so defer OPENED events until they are fully initialized +*/ + *be_opened = false; +return chr; } +static const CharDriver gd_vc_driver = { + .instance_size =sizeof (VCChardev), + .kind =CHARDEV_BACKEND_KIND_VC , + .parse = qemu_chr_parse_vc, .create = vc_init, + .chr_write = gd_vc_chr_write, + .chr_set_echo = gd_vc_chr_set_echo, +}; +static gboolean gd_vc_in (VteTerminal *terminal,gchar *text,guint size,gpointer user_data) {
@@ -2317,6 +2328,7 @@ void early_gtk_display_init(int opengl) }#if defined(CONFIG_VTE ) -register_vc_handler (gd_vc_handler); +/* overwrite the console.c vc driver */ +register_char_driver (&gd_vc_driver);#endif }
include/sysemu/char.h +1 -3
@@ -505,9 +505,7 @@ void register_char_driver(const CharDriver *driver);extern int term_escape_char; -/* console.c */ -typedef Chardev *(VcHandler )(ChardevVC *vc,Error **errp); -void register_vc_handler (VcHandler *handler); +void qemu_chr_parse_vc (QemuOpts *opts,ChardevBackend *backend,Error **errp);#endif
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 14/54] gtk: overwrite the console.c char driver
Quoted 6 lines · Marc-André Lureau
Instead of registering a vc handler to allocate the Gtk VC Chardev, overwrite the
console.cchar driver.A later patch, when switching to QOM, will register a default console vc QOM class if none has been registered before.
Quoted 7 lines
Signed-off-by Marc-André Lureau <[email protected]>--- ui/console.c | 24 +++++++----------------- ui/gtk.c | 30 +++++++++++++++++++++--------- include/sysemu/char.h | 4 +--- 3 files changed, 29 insertions(+), 29 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 15/54 chardev: qom-ify
In reply to Marc-André LureauTurn Chardev into Object.
qemu_chr_alloc() is replaced by the qemu_chardev_new() constructor. It will call qemu_char_open() to open/intialize the chardev with the ChardevCommon *backend settings.
The CharDriver::create() callback is turned into a ChardevClass::open() which is called from the newly introduced qemu_chardev_open().
"chardev-gdb" and "chardev-hci" are internal chardev and aren't creatable directly with -chardev. Use a new internal flag to disable them. We may want to use TYPE_USER_CREATABLE interface instead, or perhaps allow -chardev usage.
--- backends/baum.c | 70 +-- backends/msmouse.c | 57 ++- backends/testdev.c | 34 +- gdbstub.c | 33 +- hw/bt/hci-csr.c | 54 +- monitor.c | 2 +- qemu-char.c | 1334 ++++++++++++++++++++++++++----------------------- spice-qemu-char.c | 144 +++--- ui/console.c | 73 +-- ui/gtk.c | 51 +- vl.c | 2 + include/sysemu/char.h | 107 ++-- include/ui/console.h | 2 + 13 files changed, 1084 insertions(+), 879 deletions(-)
backends/baum.c +35 -35
@@ -102,6 +102,9 @@ typedef struct {QEMUTimer *cellCount_timer; } BaumChardev; +#define TYPE_CHARDEV_BRAILLE "chardev-braille" +#define BAUM_CHARDEV (obj) OBJECT_CHECK(BaumChardev, (obj), TYPE_CHARDEV_BRAILLE) +/* Let's assume NABCC by default */ enum way {DOTS2ASCII ,
@@ -268,9 +271,9 @@ static int baum_deferred_init(BaumChardev *baum) }/* The serial port can receive more of our data */ -static void baum_accept_input (struct Chardev *chr) +static void baum_chr_accept_input (struct Chardev *chr) { -BaumChardev *baum = (BaumChardev *)chr; +BaumChardev *baum =BAUM_CHARDEV (chr);int room, first;if (!baum->out_buf_used)
@@ -296,7 +299,7 @@ static void baum_accept_input(struct Chardev *chr)/* We want to send a packet */ static void baum_write_packet (BaumChardev *baum,const uint8_t *buf,int len) { -Chardev *chr = (Chardev *)baum; +Chardev *chr =CHARDEV (baum);uint8_t io_buf[1 +2 * len], *cur = io_buf;int room; *cur++ =ESC ;
@@ -337,7 +340,7 @@ static void baum_write_packet(BaumChardev *baum, const uint8_t *buf, int len)/* Called when the other end seems to have a wrong idea of our display size */ static void baum_cellCount_timer_cb (void *opaque) { -BaumChardev *baum = opaque; +BaumChardev *baum =BAUM_CHARDEV (opaque);uint8_t cell_count[] = { BAUM_RSP_CellCount, baum->x * baum->y };DPRINTF ("Timeout waiting for DisplayData, sending cell count\n" );baum_write_packet (baum, cell_count,sizeof (cell_count));
@@ -485,9 +488,9 @@ static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len) }/* The other end is writing some data. Store it and try to interpret */ -static int baum_write (Chardev *chr,const uint8_t *buf,int len) +static int baum_chr_write (Chardev *chr,const uint8_t *buf,int len) { -BaumChardev *baum = (BaumChardev *)chr; +BaumChardev *baum =BAUM_CHARDEV (chr);int tocopy, cur, eaten, orig_len = len;if (!len)
@@ -544,7 +547,7 @@ static void baum_send_key2(BaumChardev *baum, uint8_t type, uint8_t value,/* We got some data on the BrlAPI socket */ static void baum_chr_read (void *opaque) { -BaumChardev *baum = opaque; +BaumChardev *baum =BAUM_CHARDEV (opaque);brlapi_keyCode_t code;int ret;if (!baum->brlapi)
@@ -628,9 +631,9 @@ static void baum_chr_read(void *opaque) } } -static void baum_free (struct Chardev *chr) +static void baum_chr_free (Chardev *chr) { -BaumChardev *baum = (BaumChardev *)chr; +BaumChardev *baum =BAUM_CHARDEV (chr);timer_free (baum->cellCount_timer);if (baum->brlapi) {
@@ -639,24 +642,14 @@ static void baum_free(struct Chardev *chr) } } -static Chardev *chr_baum_init (const CharDriver *driver, -const char *id, -ChardevBackend *backend, -ChardevReturn *ret, -bool *be_opened, -Error **errp) +static void baum_chr_open (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) { -ChardevCommon *common = backend->u.braille.data; -BaumChardev *baum; -Chardev *chr; +BaumChardev *baum =BAUM_CHARDEV (chr);brlapi_handle_t *handle; - chr =qemu_chr_alloc (driver, common, errp); -if (!chr) { -return NULL ; - } - baum = (BaumChardev *)chr; - handle =g_malloc0 (brlapi_getHandleSize ()); baum->brlapi = handle;
@@ -664,34 +657,41 @@ static Chardev *chr_baum_init(const CharDriver *driver,if (baum->brlapi_fd ==-1 ) {error_setg (errp,"brlapi__openConnection: %s" ,brlapi_strerror (brlapi_error_location ())); - goto fail_handle; +g_free (handle); +return ; } baum->deferred_init =0 ; baum->cellCount_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , baum_cellCount_timer_cb, baum);qemu_set_fd_handler (baum->brlapi_fd, baum_chr_read,NULL , baum); +} -return chr; +static void char_braille_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); -fail_handle: -g_free (handle); -g_free (chr); -return NULL ; + cc->open = baum_chr_open; + cc->chr_write = baum_chr_write; + cc->chr_accept_input = baum_chr_accept_input; + cc->chr_free = baum_chr_free; } +static const TypeInfo char_braille_type_info = { + .name =TYPE_CHARDEV_BRAILLE , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (BaumChardev), + .class_init = char_braille_class_init, +}; +static void register_types (void ) {static const CharDriver driver = { - .instance_size =sizeof (BaumChardev), .kind =CHARDEV_BACKEND_KIND_BRAILLE , - .parse =NULL , .create = chr_baum_init, - .chr_write = baum_write, - .chr_accept_input = baum_accept_input, - .chr_free = baum_free, };register_char_driver (&driver); +type_register_static (&char_braille_type_info); }type_init (register_types);
backends/msmouse.c +31 -26
104 lines not on this page · raw message
backends/testdev.c +18 -16
@@ -36,6 +36,10 @@ typedef struct {int in_buf_used; } TestdevChardev; +#define TYPE_CHARDEV_TESTDEV "chardev-testdev" +#define TESTDEV_CHARDEV (obj) \ + OBJECT_CHECK(TestdevChardev, (obj), TYPE_CHARDEV_TESTDEV) +/* Try to interpret a whole incoming packet */ static int testdev_eat_packet (TestdevChardev *testdev) {
@@ -78,9 +82,9 @@ static int testdev_eat_packet(TestdevChardev *testdev) }/* The other end is writing some data. Store it and try to interpret */ -static int testdev_write (Chardev *chr,const uint8_t *buf,int len) +static int testdev_chr_write (Chardev *chr,const uint8_t *buf,int len) { -TestdevChardev *testdev = (TestdevChardev *)chr; +TestdevChardev *testdev =TESTDEV_CHARDEV (chr);int tocopy, eaten, orig_len = len;while (len) {
@@ -103,30 +107,28 @@ static int testdev_write(Chardev *chr, const uint8_t *buf, int len)return orig_len; } -static Chardev *chr_testdev_init (const CharDriver *driver, -const char *id, -ChardevBackend *backend, -ChardevReturn *ret, -bool *be_opened, -Error **errp) +static void char_testdev_class_init (ObjectClass *oc,void *data) { -TestdevChardev *testdev =g_new0 (TestdevChardev,1 );; -Chardev *chr = (Chardev *)testdev; - - chr->driver = driver; +ChardevClass *cc =CHARDEV_CLASS (oc); -return chr; + cc->chr_write = testdev_chr_write; } +static const TypeInfo char_testdev_type_info = { + .name =TYPE_CHARDEV_TESTDEV , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (TestdevChardev), + .class_init = char_testdev_class_init, +}; +static void register_types (void ) {static const CharDriver driver = { - .instance_size =sizeof (TestdevChardev), .kind =CHARDEV_BACKEND_KIND_TESTDEV , - .parse =NULL , .create = chr_testdev_init, - .chr_write = testdev_write, }; +register_char_driver (&driver); +type_register_static (&char_testdev_type_info); }type_init (register_types);
gdbstub.c +26 -7
@@ -1723,18 +1723,29 @@ static void gdb_sigterm_handler(int signal) }#endif + +static void char_gdb_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->internal = true; + cc->chr_write = gdb_monitor_write; +} + +#define TYPE_CHARDEV_GDB "chardev-gdb" + +static const TypeInfo char_gdb_type_info = { + .name =TYPE_CHARDEV_GDB , + .parent =TYPE_CHARDEV , + .class_init = char_gdb_class_init, +}; +int gdbserver_start (const char *device) {GDBState *s;char gdbstub_device_name[128 ];Chardev *chr =NULL ;Chardev *mon_chr; -ChardevCommon common = {0 }; -static const CharDriver driver = { - .instance_size =sizeof (Chardev), - .kind =-1 , - .chr_write = gdb_monitor_write - };if (!device)return -1 ;
@@ -1767,7 +1778,8 @@ int gdbserver_start(const char *device)qemu_add_vm_change_state_handler (gdb_vm_state_change,NULL );/* Initialize a monitor terminal for gdb */ - mon_chr =qemu_chr_alloc (&driver, &common, &error_abort); + mon_chr =qemu_chardev_new ("gdb" ,TYPE_CHARDEV_GDB , +NULL , &error_abort);monitor_init (mon_chr,0 ); }else {if (qemu_chr_fe_get_driver (&s->chr)) {
@@ -1790,4 +1802,11 @@ int gdbserver_start(const char *device) return 0; } + +static void register_types (void ) +{ +type_register_static (&char_gdb_type_info); +} + +type_init (register_types);#endif
hw/bt/hci-csr.c +37 -17
72 lines not on this page · raw message
monitor.c +1 -1
@@ -3194,7 +3194,7 @@ static void ringbuf_completion(ReadLineState *rs, const char *str)if (!strncmp (chr_info->label, str, len)) {Chardev *chr =qemu_chr_find (chr_info->label); -if (chr &&qemu_chr_is_ringbuf (chr)) { +if (chr &&CHARDEV_IS_RINGBUF (chr)) {readline_add_completion (rs, chr_info->label); } }
qemu-char.c +720 -614
2,463 lines not on this page · raw message
spice-qemu-char.c +83 -61
88 lines not on this page · raw message
ui/console.c +43 -30
73 lines not on this page · raw message
ui/gtk.c +28 -23
51 lines not on this page · raw message
include/sysemu/char.h +58 -49
107 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.11.0
Part of the diff is not shown here. View the raw message.
Re: [Qemu-devel] [PATCH 15/54] chardev: qom-ify
Quoted 15 lines · Marc-André Lureau
Turn Chardev into Object.
qemu_chr_alloc()is replaced by theqemu_chardev_new()constructor. It will callqemu_char_open()to open/intialize the chardev with the ChardevCommon *backend settings.The
CharDriver::create()callback is turned into aChardevClass::open()which is called from the newly introducedqemu_chardev_open()."chardev-gdb" and "chardev-hci" are internal chardev and aren't creatable directly with -chardev. Use a new internal flag to disable them. We may want to use
TYPE_USER_CREATABLEinterface instead, or perhaps allow -chardev usage.Signed-off-by Marc-André Lureau <[email protected]>
Quoted 15 lines
--- backends/baum.c | 70 +-- backends/msmouse.c | 57 ++- backends/testdev.c | 34 +- gdbstub.c | 33 +- hw/bt/hci-csr.c | 54 +- monitor.c | 2 +- qemu-char.c | 1334 ++++++++++++++++++++++++++----------------------- spice-qemu-char.c | 144 +++--- ui/console.c | 73 +-- ui/gtk.c | 51 +- vl.c | 2 + include/sysemu/char.h | 107 ++-- include/ui/console.h | 2 + 13 files changed, 1084 insertions(+), 879 deletions(-)
Big, but probably not possible to break it into many more chunks.
Rearranging the reply, to put the .h first:
Quoted 69 lines
include/sysemu/char.h +44
-1@@ -10,6 +10,7 @@#include "qapi/qmp/qstring.h" #include "qemu/main-loop.h" #include "qemu/bitmap.h" +#include "qom/object.h" /* character device */ @@ -90,7 +91,8 @@ typedef struct CharBackend {typedef struct CharDriver CharDriver ;struct Chardev { -const CharDriver *driver; +Object parent_obj; +QemuMutex chr_write_lock;CharBackend *be;char *label;@@ -102,18 +104,6 @@ struct Chardev {QTAILQ_ENTRY (Chardev) next; }; +#define TYPE_CHARDEV "chardev" +#define CHARDEV (obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV) +#define CHARDEV_CLASS (klass) \ + OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV) +#define CHARDEV_GET_CLASS (obj) \ + OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV) + +#define TYPE_CHARDEV_NULL "chardev-null" +#define TYPE_CHARDEV_MUX "chardev-mux" +#define TYPE_CHARDEV_RINGBUF "chardev-ringbuf" +#define TYPE_CHARDEV_PTY "chardev-pty" +#define TYPE_CHARDEV_CONSOLE "chardev-console" +#define TYPE_CHARDEV_STDIO "chardev-stdio" +#define TYPE_CHARDEV_PIPE "chardev-pipe" +#define TYPE_CHARDEV_MEMORY "chardev-memory" +#define TYPE_CHARDEV_PARALLEL "chardev-parallel" +#define TYPE_CHARDEV_FILE "chardev-file" +#define TYPE_CHARDEV_SERIAL "chardev-serial" +#define TYPE_CHARDEV_SOCKET "chardev-socket" +#define TYPE_CHARDEV_UDP "chardev-udp" + +#define CHARDEV_IS_MUX (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX) +#define CHARDEV_IS_RINGBUF (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF) +#define CHARDEV_IS_PTY (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY) + +typedef struct ChardevClass { + ObjectClass parent_class; + + bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */ + + void (*open)(Chardev *chr, ChardevBackend *backend, + bool *be_opened, Error **errp); + + int (*chr_write)(Chardev *s, const uint8_t *buf, int len); + int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len); + GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond); + void (*chr_update_read_handler)(Chardev *s, GMainContext *context); + int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
include/sysemu/char.h struct Chardev { · line 502
Quoted 10 lines
include/sysemu/char.h +10
-0+int (*get_msgfds)(Chardev *s,int * fds,int num); +int (*set_msgfds)(Chardev *s,int *fds,int num); +int (*chr_add_client)(Chardev *chr,int fd); +int (*chr_wait_connected)(Chardev *chr,Error **errp); +void (*chr_free)(Chardev *chr); +void (*chr_disconnect)(Chardev *chr); +void (*chr_accept_input)(Chardev *chr); +void (*chr_set_echo)(Chardev *chr,bool echo); +void (*chr_set_fe_open)(Chardev *chr,int fe_open); +} ChardevClass;
Looks nice.
On to the .c:
backends/baum.c typedef struct { · line 106
Quoted 11 lines
backends/baum.c +2
-0@@ -102,6 +102,9 @@ typedef struct {QEMUTimer *cellCount_timer; } BaumChardev; +#define TYPE_CHARDEV_BRAILLE "chardev-braille" +#define BAUM_CHARDEV (obj) OBJECT_CHECK(BaumChardev, (obj), TYPE_CHARDEV_BRAILLE)
As mentioned earlier, OBJECT_CHECK() is a nice wrapper around container_of(), so this indeed resolves my earlier concerns on casts.
backends/baum.c static int baum_deferred_init(BaumChardev *baum) · line 274
Quoted 10 lines
backends/baum.c +2
-1+/* Let's assume NABCC by default */ enum way {DOTS2ASCII ,@@ -268,9 +271,9 @@ static int baum_deferred_init(BaumChardev *baum) }/* The serial port can receive more of our data */ -static void baum_accept_input (struct Chardev *chr) +static void baum_chr_accept_input (struct Chardev *chr)
Why the rename?
backends/baum.c static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len) · line 491
Quoted 6 lines
backends/baum.c +1
-1@@ -485,9 +488,9 @@ static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len) }/* The other end is writing some data. Store it and try to interpret */ -static int baum_write (Chardev *chr,const uint8_t *buf,int len) +static int baum_chr_write (Chardev *chr,const uint8_t *buf,int len)
and again
backends/baum.c static void baum_send_key2(BaumChardev *baum, uint8_t type, uint8_t value, · line 549
Quoted 4 lines
backends/baum.c +0
-0@@ -544,7 +547,7 @@ static void baum_send_key2(BaumChardev *baum, uint8_t type, uint8_t value,/* We got some data on the BrlAPI socket */ static void baum_chr_read (void *opaque) {
If it is for consistency in the baum callback names, maybe a separate patch is warranted for that part.
Quoted 24 lines
backends/baum.c +17
-2@@ -515,33 +485,98 @@ static void remove_fd_in_watch(Chardev *chr);static void mux_chr_set_handlers (Chardev *chr,GMainContext *context);static void mux_set_focus (MuxChardev *d,int focus); -static int null_chr_write (Chardev *chr,const uint8_t *buf,int len) +static void qemu_char_open (Chardev *chr,ChardevBackend *backend, +bool *be_opened,Error **errp) { -return len; +ChardevClass *cc =CHARDEV_GET_CLASS (chr); +/* Any ChardevCommon member would work */ +ChardevCommon *common = backend ? backend->u.null.data :NULL ; + +if (common && common->has_logfile) { +int flags =O_WRONLY |O_CREAT ; +if (common->has_logappend && + common->logappend) { + flags |=O_APPEND ; + }else { + flags |=O_TRUNC ; + } + chr->logfd =qemu_open (common->logfile, flags,0666 ); +if (chr->logfd <0 ) { + error_setg_errno(errp, errno,
backends/baum.c static void remove_fd_in_watch(Chardev *chr); · line 515
Quoted 10 lines
backends/baum.c +9
-0+"Unable to open logfile %s" , + common->logfile); +return ; + } + } + +if (cc->open) { + cc->open (chr, backend, be_opened, errp); + } }
Looking good.
Quoted 11 lines
backends/baum.c +2
-4@@ -1421,19 +1451,15 @@ static Chardev *qemu_chr_open_stdio(const CharDriver *driver, act.sa_handler = term_stdio_handler;sigaction (SIGCONT , &act,NULL ); - chr =qemu_chr_open_fd (driver,0 ,1 , common, errp); -if (!chr) { -return NULL ; - } +qemu_chr_open_fd (chr,0 ,1 ); +if (opts->has_signal) {
backends/baum.c static Chardev *qemu_chr_open_stdio(const CharDriver *driver, · line 1463
Quoted 10 lines
backends/baum.c +1
-3stdio_allow_signal = opts->signal; }qemu_chr_set_echo_stdio (chr, false); - -return chr; } -#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ +#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
The change in this #if looks spurious.
backends/baum.c static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, · line 3929
Quoted 7 lines
backends/baum.c +2
-1@@ -3905,18 +3925,24 @@ static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend,static const CharDriver pipe_driver = { .kind =CHARDEV_BACKEND_KIND_PIPE , - .parse = qemu_chr_parse_pipe, .create = qemu_chr_open_pipe, + .parse = qemu_chr_parse_pipe +};
nit: As pointed out earlier in the series, I like trailing comma in struct initializations.
backends/baum.c static void qemu_chr_parse_pipe(QemuOpts *opts, ChardevBackend *backend, · line 3933
Quoted 10 lines
backends/baum.c +2
-5static const CharDriver udp_driver = { - .instance_size =sizeof (UdpChardev), .kind =CHARDEV_BACKEND_KIND_UDP , - .parse = qemu_chr_parse_udp, .create = qmp_chardev_open_udp, - .chr_write = udp_chr_write, - .chr_update_read_handler = udp_chr_update_read_handler, - .chr_free = udp_chr_free, + .parse = qemu_chr_parse_udp +};
Multiple places
spice-qemu-char.c
Quoted 62 lines
backends/baum.c +27
-16@@ -5020,33 +5115,44 @@ void qemu_chr_cleanup(void)static void register_types (void ) { -static const CharDriver *drivers[] = { - &null_driver, - &socket_driver, - &udp_driver, - &ringbuf_driver, - &file_driver, - &stdio_driver, +static const struct { +const CharDriver *driver; +const TypeInfo *type; + } chardevs[] = { + { &null_driver, &char_null_type_info }, + { &socket_driver, &char_socket_type_info }, + { &udp_driver, &char_udp_type_info }, + { &ringbuf_driver, &char_ringbuf_type_info }, + { &file_driver, &char_file_type_info }, + { &stdio_driver, &char_stdio_type_info }, #ifdef HAVE_CHARDEV_SERIAL - &serial_driver, + { &serial_driver, &char_serial_type_info }, #endif #ifdef HAVE_CHARDEV_PARPORT - ¶llel_driver, + { ¶llel_driver, &char_parallel_type_info }, #endif #ifdef HAVE_CHARDEV_PTY - &pty_driver, + { &pty_driver, &char_pty_type_info }, #endif #ifdef _WIN32 - &console_driver, + { &console_driver, &char_console_type_info }, #endif - &pipe_driver, - &mux_driver, - &memory_driver + { &pipe_driver, &char_pipe_type_info }, + { &mux_driver, &char_mux_type_info }, + { &memory_driver, &char_memory_type_info } };int i; -for (i =0 ; i <ARRAY_SIZE (drivers); i++) { -register_char_driver (drivers[i]); +type_register_static (&char_type_info); +#ifndef _WIN32 +type_register_static (&char_fd_type_info); +#else +type_register_static (&char_win_type_info); +type_register_static (&char_win_stdio_type_info); +#endif +for (i =0 ; i <ARRAY_SIZE (chardevs); i++) { +type_register_static (chardevs[i].type); +register_char_driver (chardevs[i].driver); }/* this must be done after machine init, since we register FEs with muxes spice-qemu-char.c +0
-0
So far, looks like a sane conversion. I've run out of review time today; I'll have to pick up from this file tomorrow.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 15/54] chardev: qom-ify
In reply to Marc-André LureauQuoted 5 lines · Marc-André Lureau
Turn Chardev into Object.
qemu_chr_alloc()is replaced by theqemu_chardev_new()constructor. It will callqemu_char_open()to open/intialize the chardev with the ChardevCommon *backend settings.
Review part 2:
spice-qemu-char.c typedef struct SpiceChardev { · line 23
Quoted 13 lines
spice-qemu-char.c | 144 +++--- ui/console.c | 73 +-- ui/gtk.c | 51 +- vl.c | 2 ++++
b/spice-qemu-char.cspice-qemu-char.c +3
-0@@ -18,6 +18,12 @@ typedef struct SpiceChardev {QLIST_ENTRY (SpiceChardev) next; } SpiceChardev; +#define TYPE_CHARDEV_SPICE "chardev-spice" +#define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc" +#define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"
Why do we have a lot of TYPE_CHARDEV_* in sysemu/char.h, but not these? Should they all live in the same place?
Quoted 10 lines
ui/console.c +5
-1+++ b/ui/console.c@@ -1040,9 +1040,12 @@ typedef struct VCChardev {QemuConsole *console; } VCChardev; -static int console_puts (Chardev *chr,const uint8_t *buf,int len) +#define TYPE_CHARDEV_VC "chardev-vc" +#define VC_CHARDEV (obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC) + +static int vc_chr_write (Chardev *chr,const uint8_t *buf,int len)
...
ui/console.c int qemu_console_get_height(QemuConsole *con, int fallback) · line 1957
Quoted 6 lines
ui/console.c +1
-1@@ -1951,9 +1954,9 @@ int qemu_console_get_height(QemuConsole *con, int fallback)return con ?surface_height (con->surface) : fallback; } -static void text_console_set_echo (Chardev *chr,bool echo) +static void vc_chr_set_echo (Chardev *chr,bool echo)
Should these function renames be split to a separate patch?
ui/gtk.c typedef struct VCChardev { · line 188
Quoted 7 lines
ui/gtk.c +3
-0+++ b/ui/gtk.c@@ -184,6 +184,9 @@ typedef struct VCChardev {bool echo; } VCChardev; +#define TYPE_CHARDEV_VC "chardev-vc" +#define VC_CHARDEV (obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
Why do we have TYPE_CHARDEV_VC and VC_CHARDEV() defined in two different .c files?
Overall the conversion looks good; as said elsewhere in the thread, I think you can post a v2 of 1-15 and get that merged first, while still hammering out the details of the rest of the series.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH 15/54] chardev: qom-ify
Hi
----- Original Message -----
Quoted 19 lines · Marc-André Lureau
Quoted 5 lines
Turn Chardev into Object.
qemu_chr_alloc()is replaced by theqemu_chardev_new()constructor. It will callqemu_char_open()to open/intialize the chardev with the ChardevCommon *backend settings.Review part 2:
Quoted 10 lines
spice-qemu-char.c | 144 +++--- ui/console.c | 73 +-- ui/gtk.c | 51 +- vl.c | 2 ++++
b/spice-qemu-char.cspice-qemu-char.c +0
-0@@ -18,6 +18,12 @@ typedef struct SpiceChardev {QLIST_ENTRY (SpiceChardev) next; } SpiceChardev;
Quoted 6 lines
spice-qemu-char.c typedef struct SpiceChardev { · line 23
Quoted 3 lines
spice-qemu-char.c +3
-0+#define TYPE_CHARDEV_SPICE "chardev-spice" +#define TYPE_CHARDEV_SPICEVMC "chardev-spicevmc" +#define TYPE_CHARDEV_SPICEPORT "chardev-spiceport"Why do we have a lot of TYPE_CHARDEV_* in
sysemu/char.h, but not these? Should they all live in the same place?
I think in general, we keep typename private, unless the type is being used by some other file.
In this refactoring series, it's a bit tricky, because there are types such as mux, memory and pty that are shared with several units (when splitting).
Moving them back and forth in char.h introduces yet again code churning in my tree ;) It would be easier to clean that later in the series. So for this patch, all types and common helper macros for qemu-char.c are in char.h. I'll update commit message with this detail.
Quoted 18 lines
Quoted 11 lines
ui/console.c +5
-1+++ b/ui/console.c@@ -1040,9 +1040,12 @@ typedef struct VCChardev {QemuConsole *console; } VCChardev; -static int console_puts (Chardev *chr,const uint8_t *buf,int len) +#define TYPE_CHARDEV_VC "chardev-vc" +#define VC_CHARDEV (obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC) + +static int vc_chr_write (Chardev *chr,const uint8_t *buf,int len)...
Quoted 5 lines
ui/console.c +0
-0@@ -1951,9 +1954,9 @@ int qemu_console_get_height(QemuConsole *con, int fallback)return con ?surface_height (con->surface) : fallback; }
Quoted 4 lines
ui/console.c int qemu_console_get_height(QemuConsole *con, int fallback) · line 1957
Quoted 2 lines
ui/console.c +1
-1-static void text_console_set_echo (Chardev *chr,bool echo) +static void vc_chr_set_echo (Chardev *chr,bool echo)Should these function renames be split to a separate patch?
ok
Quoted 12 lines
ui/gtk.c typedef struct VCChardev { · line 188
Quoted 8 lines
ui/gtk.c +3
-0+++ b/ui/gtk.c@@ -184,6 +184,9 @@ typedef struct VCChardev {bool echo; } VCChardev; +#define TYPE_CHARDEV_VC "chardev-vc" +#define VC_CHARDEV (obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)Why do we have
TYPE_CHARDEV_VCandVC_CHARDEV()defined in two different .c files?
Quoted 3 lines
Overall the conversion looks good; as said elsewhere in the thread, I think you can post a v2 of 1-15 and get that merged first, while still hammering out the details of the rest of the series.
ok
PATCH 16/54 spice-qemu-char: convert to finalize
In reply to Marc-André Lureau--- spice-qemu-char.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
spice-qemu-char.c +3 -3
@@ -210,9 +210,9 @@ static int spice_chr_write(Chardev *chr, const uint8_t *buf, int len)return read_bytes; } -static void spice_chr_free (struct Chardev *chr) +static void char_spice_finalize (Object *obj) { -SpiceChardev *s =SPICE_CHARDEV (chr); +SpiceChardev *s =SPICE_CHARDEV (obj);vmc_unregister_interface (s);QLIST_REMOVE (s, next);
@@ -364,13 +364,13 @@ static void char_spice_class_init(ObjectClass *oc, void *data) cc->chr_write = spice_chr_write; cc->chr_add_watch = spice_chr_add_watch; cc->chr_accept_input = spice_chr_accept_input; - cc->chr_free = spice_chr_free; }static const TypeInfo char_spice_type_info = { .name =TYPE_CHARDEV_SPICE , .parent =TYPE_CHARDEV , .instance_size =sizeof (SpiceChardev), + .instance_finalize = char_spice_finalize, .class_init = char_spice_class_init, .abstract = true, };
Signature · 1 line
2.11.0
PATCH 17/54 baum: convert to finalize
In reply to Marc-André Lureau--- backends/baum.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
backends/baum.c +3 -3
@@ -631,9 +631,9 @@ static void baum_chr_read(void *opaque) } } -static void baum_chr_free (Chardev *chr) +static void char_braille_finalize (Object *obj) { -BaumChardev *baum =BAUM_CHARDEV (chr); +BaumChardev *baum =BAUM_CHARDEV (obj);timer_free (baum->cellCount_timer);if (baum->brlapi) {
@@ -674,13 +674,13 @@ static void char_braille_class_init(ObjectClass *oc, void *data) cc->open = baum_chr_open; cc->chr_write = baum_chr_write; cc->chr_accept_input = baum_chr_accept_input; - cc->chr_free = baum_chr_free; }static const TypeInfo char_braille_type_info = { .name =TYPE_CHARDEV_BRAILLE , .parent =TYPE_CHARDEV , .instance_size =sizeof (BaumChardev), + .instance_finalize = char_braille_finalize, .class_init = char_braille_class_init, };
Signature · 1 line
2.11.0
PATCH 18/54 msmouse: convert to finalize
In reply to Marc-André Lureau--- backends/msmouse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
backends/msmouse.c +3 -3
@@ -139,9 +139,9 @@ static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)return len; } -static void msmouse_chr_free (struct Chardev *chr) +static void char_msmouse_finalize (Object *obj) { -MouseChardev *mouse =MOUSE_CHARDEV (chr); +MouseChardev *mouse =MOUSE_CHARDEV (obj);qemu_input_handler_unregister (mouse->hs); }
@@ -172,13 +172,13 @@ static void char_msmouse_class_init(ObjectClass *oc, void *data) cc->open = msmouse_chr_open; cc->chr_write = msmouse_chr_write; cc->chr_accept_input = msmouse_chr_accept_input; - cc->chr_free = msmouse_chr_free; }static const TypeInfo char_msmouse_type_info = { .name =TYPE_CHARDEV_MSMOUSE , .parent =TYPE_CHARDEV , .instance_size =sizeof (MouseChardev), + .instance_finalize = char_msmouse_finalize, .class_init = char_msmouse_class_init, };
Signature · 1 line
2.11.0
PATCH 19/54 mux: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-char.c +3 -3
@@ -859,9 +859,9 @@ static GSource *mux_chr_add_watch(Chardev *s, GIOCondition cond)return cc->chr_add_watch (chr, cond); } -static void mux_chr_free (struct Chardev *chr) +static void char_mux_finalize (Object *obj) { -MuxChardev *d =MUX_CHARDEV (chr); +MuxChardev *d =MUX_CHARDEV (obj);int i;for (i =0 ; i < d->mux_cnt; i++) {
@@ -4018,7 +4018,6 @@ static void char_mux_class_init(ObjectClass *oc, void *data)ChardevClass *cc =CHARDEV_CLASS (oc); cc->open = qemu_chr_open_mux; - cc->chr_free = mux_chr_free; cc->chr_write = mux_chr_write; cc->chr_accept_input = mux_chr_accept_input; cc->chr_add_watch = mux_chr_add_watch;
@@ -4029,6 +4028,7 @@ static const TypeInfo char_mux_type_info = { .parent = TYPE_CHARDEV, .class_init = char_mux_class_init, .instance_size = sizeof(MuxChardev), + .instance_finalize = char_mux_finalize, }; static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
Signature · 1 line
2.11.0
PATCH 20/54 char-udp: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
qemu-char.c +4 -3
@@ -2774,9 +2774,10 @@ static void udp_chr_update_read_handler(Chardev *chr, } } -static void udp_chr_free (Chardev *chr) +static void char_udp_finalize (Object *obj) { -UdpChardev *s =UDP_CHARDEV (chr); +Chardev *chr =CHARDEV (obj); +UdpChardev *s =UDP_CHARDEV (obj);remove_fd_in_watch (chr);if (s->ioc) {
@@ -4973,13 +4974,13 @@ static void char_udp_class_init(ObjectClass *oc, void *data) cc->open = qmp_chardev_open_udp; cc->chr_write = udp_chr_write; cc->chr_update_read_handler = udp_chr_update_read_handler; - cc->chr_free = udp_chr_free; }static const TypeInfo char_udp_type_info = { .name =TYPE_CHARDEV_UDP , .parent =TYPE_CHARDEV , .instance_size =sizeof (UdpChardev), + .instance_finalize = char_udp_finalize, .class_init = char_udp_class_init, };
Signature · 1 line
2.11.0
PATCH 21/54 char-socket: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
qemu-char.c +4 -3
@@ -3442,9 +3442,10 @@ int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)return qemu_chr_wait_connected (be->chr, errp); } -static void tcp_chr_free (Chardev *chr) +static void char_socket_finalize (Object *obj) { -SocketChardev *s =SOCKET_CHARDEV (chr); +Chardev *chr =CHARDEV (obj); +SocketChardev *s =SOCKET_CHARDEV (obj);tcp_chr_free_connection (chr);
@@ -4924,13 +4925,13 @@ static void char_socket_class_init(ObjectClass *oc, void *data) cc->chr_add_client = tcp_chr_add_client; cc->chr_add_watch = tcp_chr_add_watch; cc->chr_update_read_handler = tcp_chr_update_read_handler; - cc->chr_free = tcp_chr_free; }static const TypeInfo char_socket_type_info = { .name =TYPE_CHARDEV_SOCKET , .parent =TYPE_CHARDEV , .instance_size =sizeof (SocketChardev), + .instance_finalize = char_socket_finalize, .class_init = char_socket_class_init, };
Signature · 1 line
2.11.0
PATCH 22/54 char-pty: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
qemu-char.c +4 -3
@@ -1654,9 +1654,10 @@ static void pty_chr_state(Chardev *chr, int connected) } } -static void pty_chr_free (struct Chardev *chr) +static void char_pty_finalize (Object *obj) { -PtyChardev *s =PTY_CHARDEV (chr); +Chardev *chr =CHARDEV (obj); +PtyChardev *s =PTY_CHARDEV (obj);qemu_mutex_lock (&chr->chr_write_lock);pty_chr_state (chr,0 );
@@ -1713,13 +1714,13 @@ static void char_pty_class_init(ObjectClass *oc, void *data) cc->chr_write = char_pty_chr_write; cc->chr_update_read_handler = pty_chr_update_read_handler; cc->chr_add_watch = pty_chr_add_watch; - cc->chr_free = pty_chr_free; }static const TypeInfo char_pty_type_info = { .name =TYPE_CHARDEV_PTY , .parent =TYPE_CHARDEV , .instance_size =sizeof (PtyChardev), + .instance_finalize = char_pty_finalize, .class_init = char_pty_class_init, };
Signature · 1 line
2.11.0
PATCH 23/54 char-ringbuf: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-char.c +3 -3
@@ -3543,9 +3543,9 @@ static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)return i; } -static void ringbuf_chr_free (struct Chardev *chr) +static void char_ringbuf_finalize (Object *obj) { -RingBufChardev *d =RINGBUF_CHARDEV (chr); +RingBufChardev *d =RINGBUF_CHARDEV (obj);g_free (d->cbuf); }
@@ -3975,7 +3975,6 @@ static void char_ringbuf_class_init(ObjectClass *oc, void *data) cc->open = qemu_chr_open_ringbuf; cc->chr_write = ringbuf_chr_write; - cc->chr_free = ringbuf_chr_free; }static const TypeInfo char_ringbuf_type_info = {
@@ -3983,6 +3982,7 @@ static const TypeInfo char_ringbuf_type_info = { .parent = TYPE_CHARDEV, .class_init = char_ringbuf_class_init, .instance_size = sizeof(RingBufChardev), + .instance_finalize = char_ringbuf_finalize, };/* Bug-compatibility: */
Signature · 1 line
2.11.0
PATCH 24/54 char-parallel: convert parallel to finalize
In reply to Marc-André Lureau--- qemu-char.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-)
qemu-char.c +17 -13
@@ -2020,17 +2020,6 @@ static int pp_ioctl(Chardev *chr, int cmd, void *arg)return 0 ; } -static void pp_free (Chardev *chr) -{ -ParallelChardev *drv =PARALLEL_CHARDEV (chr); -int fd = drv->fd; - -pp_hw_mode (drv,IEEE1284_MODE_COMPAT ); -ioctl (fd,PPRELEASE ); -close (fd); -qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); -} - static void qemu_chr_open_pp_fd(Chardev *chr, int fd, bool *be_opened,
@@ -4696,18 +4685,33 @@ static void char_parallel_class_init(ObjectClass *oc, void *data)#if defined(__linux__) cc->chr_write = null_chr_write; cc->chr_ioctl = pp_ioctl; - cc->chr_free = pp_free;#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -/* FIXME: no chr_free */ cc->chr_write = null_chr_write; cc->chr_ioctl = pp_ioctl; #endif } +static void char_parallel_finalize (Object *obj) +{ +#if defined(__linux__) +Chardev *chr =CHARDEV (obj); +ParallelChardev *drv =PARALLEL_CHARDEV (chr); +int fd = drv->fd; + +pp_hw_mode (drv,IEEE1284_MODE_COMPAT ); +ioctl (fd,PPRELEASE ); +close (fd); +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) +/* FIXME: close fd? */ +#endif +} +static const TypeInfo char_parallel_type_info = { .name =TYPE_CHARDEV_PARALLEL , .parent =TYPE_CHARDEV , .instance_size =sizeof (ParallelChardev), + .instance_finalize = char_parallel_finalize, .class_init = char_parallel_class_init, };#endif
Signature · 1 line
2.11.0
PATCH 25/54 char-stdio: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-char.c +3 -3
@@ -1417,10 +1417,10 @@ static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)tcsetattr (0 ,TCSANOW , &tty); } -static void qemu_chr_free_stdio (struct Chardev *chr) +static void char_stdio_finalize (Object *obj) {term_exit (); -fd_chr_free (chr); +fd_chr_free (CHARDEV (chr)); } static void qemu_chr_open_stdio(Chardev *chr,
@@ -3852,7 +3852,6 @@ static void char_stdio_class_init(ObjectClass *oc, void *data) cc->chr_free = win_stdio_free;#else cc->chr_set_echo = qemu_chr_set_echo_stdio; - cc->chr_free = qemu_chr_free_stdio;#endif }
@@ -3862,6 +3861,7 @@ static const TypeInfo char_stdio_type_info = { .parent = TYPE_CHARDEV_WIN_STDIO, #else .parent = TYPE_CHARDEV_FD, + .instance_finalize = char_stdio_finalize, #endif .class_init = char_stdio_class_init, };
Signature · 1 line
2.11.0
PATCH 26/54 char-win-stdio: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-char.c +3 -3
@@ -2589,9 +2589,9 @@ static void qemu_chr_set_echo_win_stdio(Chardev *chr, bool echo) } } -static void win_stdio_free (Chardev *chr) +static void char_win_stdio_finalize (Object *obj) { -WinStdioChardev *stdio =WIN_STDIO_CHARDEV (chr); +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (obj);if (stdio->hInputReadyEvent !=INVALID_HANDLE_VALUE ) {CloseHandle (stdio->hInputReadyEvent);
@@ -2608,6 +2608,7 @@ static const TypeInfo char_win_stdio_type_info = { .name = TYPE_CHARDEV_WIN_STDIO, .parent = TYPE_CHARDEV, .instance_size = sizeof(WinStdioChardev), + .instance_finalize = char_win_stdio_finalize, .abstract = true, };
@@ -3849,7 +3850,6 @@ static void char_stdio_class_init(ObjectClass *oc, void *data)#ifdef _WIN32 cc->chr_write = win_stdio_write; cc->chr_set_echo = qemu_chr_set_echo_win_stdio; - cc->chr_free = win_stdio_free;#else cc->chr_set_echo = qemu_chr_set_echo_stdio;#endif
Signature · 1 line
2.11.0
PATCH 27/54 char-win: do not override chr_free
In reply to Marc-André LureauFor some unclear reason to me, char-file does not have chr_free on win32. Since we want to switch to instance finalizer instead of class chr_free, we should be able to run the base WinChardev class finalizer in any case. Use a boolean to skip free to ease the transition to instance finalizer.
--- qemu-char.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
qemu-char.c +7 -5
@@ -2117,6 +2117,8 @@ typedef struct {/* Protected by the Chardev chr_write_lock. */ OVERLAPPED osend; +/* FIXME: file/console do not finalize */ +BOOL skip_free; } WinChardev;#define TYPE_CHARDEV_WIN "chardev-win"
@@ -2147,6 +2149,10 @@ static void win_chr_free(Chardev *chr) {WinChardev *s =WIN_CHARDEV (chr); +if (s->skip_free) { +return ; + } +if (s->hsend) {CloseHandle (s->hsend); s->hsend =NULL ;
@@ -2427,6 +2433,7 @@ static void qemu_chr_open_win_file(Chardev *chr, HANDLE fd_out) {WinChardev *s =WIN_CHARDEV (chr); + s->skip_free = true; s->hcom = fd_out; }
@@ -2463,7 +2470,6 @@ static void char_console_class_init(ObjectClass *oc, void *data)ChardevClass *cc =CHARDEV_CLASS (oc); cc->open = qemu_chr_open_win_con; - cc->chr_free =NULL ; }static const TypeInfo char_console_type_info = {
@@ -4728,10 +4734,6 @@ static void char_file_class_init(ObjectClass *oc, void *data)ChardevClass *cc =CHARDEV_CLASS (oc); cc->open = qmp_chardev_open_file; -#ifdef _WIN32 -/* FIXME: no chr_free */ - cc->chr_free =NULL ; -#endif }static const TypeInfo char_file_type_info = {
Signature · 1 line
2.11.0
PATCH 28/54 char-win: convert to finalize
In reply to Marc-André Lureau--- qemu-char.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
qemu-char.c +3 -4
@@ -2145,8 +2145,9 @@ typedef struct {static int win_chr_poll (void *opaque);static int win_chr_pipe_poll (void *opaque); -static void win_chr_free (Chardev *chr) +static void char_win_finalize (Object *obj) { +Chardev *chr =CHARDEV (obj);WinChardev *s =WIN_CHARDEV (chr);if (s->skip_free) {
@@ -2236,7 +2237,6 @@ static int win_chr_init(Chardev *chr, const char *filename, Error **errp)return 0 ; fail: -win_chr_free (chr);return -1 ; }
@@ -2411,7 +2411,6 @@ static int win_chr_pipe_init(Chardev *chr, const char *filename,return 0 ; fail: -win_chr_free (chr);return -1 ; }
@@ -2442,13 +2441,13 @@ static void char_win_class_init(ObjectClass *oc, void *data)ChardevClass *cc =CHARDEV_CLASS (oc); cc->chr_write = win_chr_write; - cc->chr_free = win_chr_free; }static const TypeInfo char_win_type_info = { .name =TYPE_CHARDEV_WIN , .parent =TYPE_CHARDEV , .instance_size =sizeof (WinChardev), + .instance_finalize = char_win_finalize, .class_init = char_win_class_init, .abstract = true, };
Signature · 1 line
2.11.0
PATCH 29/54 char-fd: convert to finalize
In reply to Marc-André Lureauchar-serial inherits from char-fd finalizer.
--- qemu-char.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
qemu-char.c +4 -10
@@ -1291,9 +1291,10 @@ static void fd_chr_update_read_handler(Chardev *chr, } } -static void fd_chr_free (struct Chardev *chr) +static void char_fd_finalize (Object *obj) { -FDChardev *s =FD_CHARDEV (chr); +Chardev *chr =CHARDEV (obj); +FDChardev *s =FD_CHARDEV (obj);remove_fd_in_watch (chr);if (s->ioc_in) {
@@ -1332,13 +1333,13 @@ static void char_fd_class_init(ObjectClass *oc, void *data) cc->chr_add_watch = fd_chr_add_watch; cc->chr_write = fd_chr_write; cc->chr_update_read_handler = fd_chr_update_read_handler; - cc->chr_free = fd_chr_free; }static const TypeInfo char_fd_type_info = { .name =TYPE_CHARDEV_FD , .parent =TYPE_CHARDEV , .instance_size =sizeof (FDChardev), + .instance_finalize = char_fd_finalize, .class_init = char_fd_class_init, .abstract = true, };
@@ -1420,7 +1421,6 @@ static void qemu_chr_set_echo_stdio(Chardev *chr, bool echo)static void char_stdio_finalize (Object *obj) {term_exit (); -fd_chr_free (CHARDEV (chr)); } static void qemu_chr_open_stdio(Chardev *chr,
@@ -1908,11 +1908,6 @@ static int tty_serial_ioctl(Chardev *chr, int cmd, void *arg) }return 0 ; } - -static void qemu_chr_free_tty (Chardev *chr) -{ -fd_chr_free (chr); -}#endif /* __linux__ || __sun__ */ #if defined(__linux__)
@@ -4759,7 +4754,6 @@ static void char_serial_class_init(ObjectClass *oc, void *data) cc->open = qmp_chardev_open_serial;#ifndef _WIN32 cc->chr_ioctl = tty_serial_ioctl; - cc->chr_free = qemu_chr_free_tty;#endif }
Signature · 1 line
2.11.0
PATCH 30/54 char: remove chr_free
In reply to Marc-André LureauNow it uses Object instance_finalize instead.
--- qemu-char.c | 10 +--------- include/sysemu/char.h | 8 -------- 2 files changed, 1 insertion(+), 17 deletions(-)
qemu-char.c +1 -9
@@ -4370,18 +4370,10 @@ void qemu_chr_fe_disconnect(CharBackend *be) } } -void qemu_chr_free (Chardev *chr) -{ -if (CHARDEV_GET_CLASS (chr)->chr_free) { -CHARDEV_GET_CLASS (chr)->chr_free (chr); - } -object_unref (OBJECT (chr)); -} -void qemu_chr_delete (Chardev *chr) {QTAILQ_REMOVE (&chardevs, chr, next); -qemu_chr_free (chr); +object_unref (OBJECT (chr)); }ChardevInfoList *qmp_query_chardev (Error **errp)
include/sysemu/char.h +0 -8
@@ -184,13 +184,6 @@ Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);*/ void qemu_chr_delete (Chardev *chr); -/** -* @qemu_chr_free: -* -* Destroy a character backend. -*/ -void qemu_chr_free (Chardev *chr); -/** * @qemu_chr_fe_set_echo: *
@@ -494,7 +487,6 @@ typedef struct ChardevClass {int (*set_msgfds)(Chardev *s,int *fds,int num);int (*chr_add_client)(Chardev *chr,int fd);int (*chr_wait_connected)(Chardev *chr,Error **errp); -void (*chr_free)(Chardev *chr);void (*chr_disconnect)(Chardev *chr);void (*chr_accept_input)(Chardev *chr);void (*chr_set_echo)(Chardev *chr,bool echo);
Signature · 1 line
2.11.0
PATCH 31/54 char: get rid of CharDriver
In reply to Marc-André Lureau--- backends/baum.c | 6 +- backends/msmouse.c | 6 +- backends/testdev.c | 6 +- qemu-char.c | 281 +++++++++++++++++++++----------------------------- spice-qemu-char.c | 15 +-- ui/console.c | 10 +- ui/gtk.c | 10 +- include/sysemu/char.h | 12 +-- 8 files changed, 133 insertions(+), 213 deletions(-)
backends/baum.c +1 -5
@@ -671,6 +671,7 @@ static void char_braille_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_BRAILLE ; cc->open = baum_chr_open; cc->chr_write = baum_chr_write; cc->chr_accept_input = baum_chr_accept_input;
@@ -686,11 +687,6 @@ static const TypeInfo char_braille_type_info = {static void register_types (void ) { -static const CharDriver driver = { - .kind =CHARDEV_BACKEND_KIND_BRAILLE , - }; - -register_char_driver (&driver);type_register_static (&char_braille_type_info); }
backends/msmouse.c +1 -5
@@ -169,6 +169,7 @@ static void char_msmouse_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_MSMOUSE ; cc->open = msmouse_chr_open; cc->chr_write = msmouse_chr_write; cc->chr_accept_input = msmouse_chr_accept_input;
@@ -184,11 +185,6 @@ static const TypeInfo char_msmouse_type_info = {static void register_types (void ) { -static const CharDriver driver = { - .kind =CHARDEV_BACKEND_KIND_MSMOUSE , - }; - -register_char_driver (&driver);type_register_static (&char_msmouse_type_info); }
backends/testdev.c +1 -5
@@ -111,6 +111,7 @@ static void char_testdev_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_TESTDEV ; cc->chr_write = testdev_chr_write; }
@@ -123,11 +124,6 @@ static const TypeInfo char_testdev_type_info = {static void register_types (void ) { -static const CharDriver driver = { - .kind =CHARDEV_BACKEND_KIND_TESTDEV , - }; - -register_char_driver (&driver);type_register_static (&char_testdev_type_info); }
qemu-char.c +120 -161
499 lines not on this page · raw message
spice-qemu-char.c +4 -11
@@ -379,6 +379,8 @@ static void char_spicevmc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_SPICEVMC ; + cc->parse = qemu_chr_parse_spice_vmc; cc->open = qemu_chr_open_spice_vmc; cc->chr_set_fe_open = spice_vmc_set_fe_open; }
@@ -393,6 +395,8 @@ static void char_spiceport_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_SPICEPORT ; + cc->parse = qemu_chr_parse_spice_port; cc->open = qemu_chr_open_spice_port; cc->chr_set_fe_open = spice_port_set_fe_open; }
@@ -405,17 +409,6 @@ static const TypeInfo char_spiceport_type_info = {static void register_types (void ) { -static const CharDriver vmc_driver = { - .kind =CHARDEV_BACKEND_KIND_SPICEVMC , - .parse = qemu_chr_parse_spice_vmc - }; -static const CharDriver port_driver = { - .kind =CHARDEV_BACKEND_KIND_SPICEPORT , - .parse = qemu_chr_parse_spice_port - }; -register_char_driver (&vmc_driver); -register_char_driver (&port_driver); -type_register_static (&char_spice_type_info);type_register_static (&char_spicevmc_type_info);type_register_static (&char_spiceport_type_info);
ui/console.c +2 -8
@@ -2048,8 +2048,6 @@ static void text_console_do_init(Chardev *chr, DisplayState *ds)qemu_chr_be_generic_open (chr); } -static const CharDriver vc_driver; - static void vc_open(Chardev *chr, ChardevBackend *backend, bool *be_opened,
@@ -2178,6 +2176,8 @@ static void char_vc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_VC ; + cc->parse = qemu_chr_parse_vc; cc->open = vc_open; cc->chr_write = vc_chr_write; cc->chr_set_echo = vc_chr_set_echo;
@@ -2195,15 +2195,9 @@ void qemu_console_early_init(void)/* set the default vc driver */ if (!object_class_by_name (TYPE_CHARDEV_VC )) {type_register (&char_vc_type_info); -register_char_driver (&vc_driver); } } -static const CharDriver vc_driver = { - .kind =CHARDEV_BACKEND_KIND_VC , - .parse = qemu_chr_parse_vc -}; -static void register_types (void ) {type_register_static (&qemu_console_info);
ui/gtk.c +2 -8
@@ -1705,8 +1705,6 @@ static void gd_vc_chr_set_echo(Chardev *chr, bool echo)static int nb_vcs;static Chardev *vcs[MAX_VCS ]; -static const CharDriver gd_vc_driver; - static void gd_vc_open(Chardev *chr, ChardevBackend *backend, bool *be_opened,
@@ -1729,6 +1727,8 @@ static void char_gd_vc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); + cc->kind =CHARDEV_BACKEND_KIND_VC ; + cc->parse = qemu_chr_parse_vc; cc->open = gd_vc_open; cc->chr_write = gd_vc_chr_write; cc->chr_set_echo = gd_vc_chr_set_echo;
@@ -1741,11 +1741,6 @@ static const TypeInfo char_gd_vc_type_info = { .class_init = char_gd_vc_class_init, }; -static const CharDriver gd_vc_driver = { - .kind =CHARDEV_BACKEND_KIND_VC , - .parse = qemu_chr_parse_vc -}; -static gboolean gd_vc_in (VteTerminal *terminal,gchar *text,guint size,gpointer user_data) {
@@ -2334,6 +2329,5 @@ void early_gtk_display_init(int opengl)#if defined(CONFIG_VTE )type_register (&char_gd_vc_type_info); -register_char_driver (&gd_vc_driver);#endif }
include/sysemu/char.h +2 -10
@@ -88,8 +88,6 @@ typedef struct CharBackend {int fe_open; } CharBackend; -typedef struct CharDriver CharDriver ; -struct Chardev {Object parent_obj;
@@ -474,6 +472,8 @@ typedef struct ChardevClass {ObjectClass parent_class;bool internal;/* TODO: eventually use TYPE_USER_CREATABLE */ +ChardevBackendKind kind; +void (*parse)(QemuOpts *opts,ChardevBackend *backend,Error **errp);void (*open)(Chardev *chr,ChardevBackend *backend,bool *be_opened,Error **errp);
@@ -493,17 +493,9 @@ typedef struct ChardevClass {void (*chr_set_fe_open)(Chardev *chr,int fe_open); } ChardevClass; -struct CharDriver { -const char *alias; -ChardevBackendKind kind; -void (*parse)(QemuOpts *opts,ChardevBackend *backend,Error **errp); -}; -Chardev *qemu_chardev_new (const char *id,const char *typename,ChardevBackend *backend,Error **errp); -void register_char_driver (const CharDriver *driver); -extern int term_escape_char;/* console.c */
Signature · 1 line
2.11.0
PATCH 32/54 char: remove class kind field
In reply to Marc-André LureauThe class kind is only necessary to lookup the chardev name in qmp_chardev_add() after calling qemu_chr_new_from_opts(). However, qemu_chr_new_from_opts() can be changed to use a non-qmp function using the chardev class typename. Introduce qemu_chardev_add() to be called from qemu_chr_new_from_opts() and remove the class chardev kind field.
--- backends/baum.c | 1 - backends/msmouse.c | 1 - backends/testdev.c | 1 - qemu-char.c | 96 ++++++++++++++++++++++----------------------------- spice-qemu-char.c | 2 -- ui/console.c | 1 - ui/gtk.c | 1 - include/sysemu/char.h | 1 - 8 files changed, 42 insertions(+), 62 deletions(-)
backends/baum.c +0 -1
@@ -671,7 +671,6 @@ static void char_braille_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_BRAILLE ; cc->open = baum_chr_open; cc->chr_write = baum_chr_write; cc->chr_accept_input = baum_chr_accept_input;
backends/msmouse.c +0 -1
@@ -169,7 +169,6 @@ static void char_msmouse_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_MSMOUSE ; cc->open = msmouse_chr_open; cc->chr_write = msmouse_chr_write; cc->chr_accept_input = msmouse_chr_accept_input;
backends/testdev.c +0 -1
@@ -111,7 +111,6 @@ static void char_testdev_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_TESTDEV ; cc->chr_write = testdev_chr_write; }
qemu-char.c +42 -54
261 lines not on this page · raw message
spice-qemu-char.c +0 -2
@@ -379,7 +379,6 @@ static void char_spicevmc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_SPICEVMC ; cc->parse = qemu_chr_parse_spice_vmc; cc->open = qemu_chr_open_spice_vmc; cc->chr_set_fe_open = spice_vmc_set_fe_open;
@@ -395,7 +394,6 @@ static void char_spiceport_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_SPICEPORT ; cc->parse = qemu_chr_parse_spice_port; cc->open = qemu_chr_open_spice_port; cc->chr_set_fe_open = spice_port_set_fe_open;
ui/console.c +0 -1
@@ -2176,7 +2176,6 @@ static void char_vc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_VC ; cc->parse = qemu_chr_parse_vc; cc->open = vc_open; cc->chr_write = vc_chr_write;
ui/gtk.c +0 -1
@@ -1727,7 +1727,6 @@ static void char_gd_vc_class_init(ObjectClass *oc, void *data) {ChardevClass *cc =CHARDEV_CLASS (oc); - cc->kind =CHARDEV_BACKEND_KIND_VC ; cc->parse = qemu_chr_parse_vc; cc->open = gd_vc_open; cc->chr_write = gd_vc_chr_write;
include/sysemu/char.h +0 -1
@@ -472,7 +472,6 @@ typedef struct ChardevClass {ObjectClass parent_class;bool internal;/* TODO: eventually use TYPE_USER_CREATABLE */ -ChardevBackendKind kind;void (*parse)(QemuOpts *opts,ChardevBackend *backend,Error **errp); void (*open)(Chardev *chr, ChardevBackend *backend,
Signature · 1 line
2.11.0
PATCH 33/54 char: move to chardev/
In reply to Marc-André LureauThe following commits will split char.c in several files. Let's put them in a subdirectory.
--- qemu-char.c => chardev/char.c | 0 Makefile.objs | 2 +- chardev/Makefile.objs | 1 + tests/Makefile.include | 6 +++--- MAINTAINERS | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) rename qemu-char.c => chardev/char.c (100%) create mode 100644 chardev/Makefile.objs
chardev/char.c renamed from qemu-char.c +0 -0
Makefile.objs +1 -1
@@ -51,7 +51,7 @@ common-obj-$(CONFIG_POSIX) += os-posix.o common-obj-$(CONFIG_LINUX) += fsdev/ common-obj-y += migration/ -common-obj-y += qemu-char.o #aio.o +common-obj-y += chardev/ #aio.o common-obj-y += page_cache.o common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
chardev/Makefile.objs new file +1 -0
@@ -0,0 +1 @@ +common-obj-y += char.o
tests/Makefile.include +3 -3
@@ -9,7 +9,7 @@ SYSEMU_TARGET_LIST := $(subst -softmmu.mak,,$(notdir \ check-unit-y = tests/check-qdict$(EXESUF) gcov-files-check-qdict-y = qobject/qdict.c check-unit-y += tests/test-char$(EXESUF) -gcov-files-check-qdict-y = qemu-char.c +gcov-files-check-qdict-y = chardev/char.c check-unit-y += tests/check-qfloat$(EXESUF) gcov-files-check-qfloat-y = qobject/qfloat.c check-unit-y += tests/check-qint$(EXESUF)
@@ -488,7 +488,7 @@ tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y) tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y) tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) -tests/test-char$(EXESUF): tests/test-char.o qemu-char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-io-obj-y) +tests/test-char$(EXESUF): tests/test-char.o chardev/char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-io-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y) tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y)
@@ -678,7 +678,7 @@ tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y) tests/pc-cpu-test$(EXESUF): tests/pc-cpu-test.o tests/postcopy-test$(EXESUF): tests/postcopy-test.o -tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-char.o qemu-timer.o $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o chardev/char.o qemu-timer.o $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y) tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
MAINTAINERS +1 -1
@@ -1175,7 +1175,7 @@ T: git git://github.com/jnsnow/qemu.git bitmaps Character device backends M: Paolo Bonzini <[email protected]> S: Maintained -F: qemu-char.c +F: chardev/ F: backends/msmouse.c F: backends/testdev.c
Signature · 1 line
2.11.0
PATCH 34/54 char: create chardev-obj-y
In reply to Marc-André LureauThis will help to split char.c in several units without having to reference them all everywhere.
--- Makefile | 3 ++- Makefile.objs | 4 +++- Makefile.target | 3 +++ chardev/Makefile.objs | 2 +- tests/Makefile.include | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-)
Makefile +2 -1
@@ -145,6 +145,7 @@ endif dummy := $(call unnest-vars,, \ stub-obj-y \ + chardev-obj-y \ util-obj-y \ qga-obj-y \ ivshmem-client-obj-y \
@@ -220,7 +221,7 @@ subdir-dtc:dtc/libfdt dtc/tests dtc/%: mkdir -p $@ -$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY)) +$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(chardev-obj-y) $(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY)) ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS)) # Only keep -O and -g cflags
Makefile.objs +3 -1
@@ -51,7 +51,7 @@ common-obj-$(CONFIG_POSIX) += os-posix.o common-obj-$(CONFIG_LINUX) += fsdev/ common-obj-y += migration/ -common-obj-y += chardev/ #aio.o +common-obj-y += #aio.o common-obj-y += page_cache.o common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
@@ -79,6 +79,8 @@ common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o common-obj-$(CONFIG_FDT) += device_tree.o +chardev-obj-y = chardev/ + ###################################################################### # qapi
Makefile.target +3 -0
@@ -170,12 +170,14 @@ all-obj-y := $(obj-y) target-obj-y := block-obj-y := common-obj-y := +chardev-obj-y := include $(SRC_PATH)/Makefile.objs dummy := $(call unnest-vars,,target-obj-y) target-obj-y-save := $(target-obj-y) dummy := $(call unnest-vars,.., \ block-obj-y \ block-obj-m \ + chardev-obj-y \ crypto-obj-y \ crypto-aes-obj-y \ qom-obj-y \
@@ -184,6 +186,7 @@ dummy := $(call unnest-vars,.., \ common-obj-m) target-obj-y := $(target-obj-y-save) all-obj-y += $(common-obj-y) +all-obj-y += $(chardev-obj-y) all-obj-y += $(target-obj-y) all-obj-y += $(qom-obj-y) all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y)
chardev/Makefile.objs +1 -1
@@ -1 +1 @@ -common-obj-y += char.o +chardev-obj-y += char.o
tests/Makefile.include +2 -2
@@ -488,7 +488,7 @@ tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y) tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y) tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y) -tests/test-char$(EXESUF): tests/test-char.o chardev/char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-io-obj-y) +tests/test-char$(EXESUF): tests/test-char.o qemu-timer.o $(test-util-obj-y) $(qtest-obj-y) $(test-io-obj-y) $(chardev-obj-y) tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(test-block-obj-y) tests/test-aio$(EXESUF): tests/test-aio.o $(test-block-obj-y) tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y)
@@ -678,7 +678,7 @@ tests/usb-hcd-ehci-test$(EXESUF): tests/usb-hcd-ehci-test.o $(libqos-usb-obj-y) tests/usb-hcd-xhci-test$(EXESUF): tests/usb-hcd-xhci-test.o $(libqos-usb-obj-y) tests/pc-cpu-test$(EXESUF): tests/pc-cpu-test.o tests/postcopy-test$(EXESUF): tests/postcopy-test.o -tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o chardev/char.o qemu-timer.o $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) +tests/vhost-user-test$(EXESUF): tests/vhost-user-test.o qemu-timer.o $(qtest-obj-y) $(test-io-obj-y) $(libqos-virtio-obj-y) $(libqos-pc-obj-y) $(chardev-obj-y) tests/qemu-iotests/socket_scm_helper$(EXESUF): tests/qemu-iotests/socket_scm_helper.o tests/test-qemu-opts$(EXESUF): tests/test-qemu-opts.o $(test-util-obj-y) tests/test-write-threshold$(EXESUF): tests/test-write-threshold.o $(test-block-obj-y)
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH 34/54] char: create chardev-obj-y
Quoted 11 lines · Marc-André Lureau
This will help to split
char.cin several units without having to reference them all everywhere.Signed-off-by Marc-André Lureau <[email protected]>--- Makefile | 3 ++- Makefile.objs | 4 +++- Makefile.target | 3 +++ chardev/Makefile.objs | 2 +- tests/Makefile.include | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-)
Any reason to do this instead of keeping common-obj-y?
Thanks,
Paolo
Re: [Qemu-devel] [PATCH 34/54] char: create chardev-obj-y
Hi
Quoted 15 lines · Paolo Bonzini
Quoted 11 lines · Marc-André Lureau
This will help to split
char.cin several units without having to reference them all everywhere.Signed-off-by Marc-André Lureau <[email protected]>--- Makefile | 3 ++- Makefile.objs | 4 +++- Makefile.target | 3 +++ chardev/Makefile.objs | 2 +- tests/Makefile.include | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-)
Quoted 3 lines
Any reason to do this instead of keeping common-obj-y?
common-obj-y has a lot of other units, including vl.o, making it not possible to link with tests/test-char for ex. However, if we changed it to link with everything except vl.c (or other changes), we may be able to link with and test spice-qemu-char. The resulting binaries will likely explode though.
Thanks,
Quoted 4 lines
Paolo
--
Marc-André Lureau
Re: [Qemu-devel] [PATCH 34/54] char: create chardev-obj-y
Quoted 15 lines · Marc-André Lureau
On 12/12/2016 23:43, Marc-André Lureau wrote:
> This will help to splitchar.cin several units without having to > reference them all everywhere.
>
> Signed-off-by: Marc-André Lureau <[email protected] <mailto:[email protected]>>
> ---
>Makefile| 3 ++-
> Makefile.objs | 4 +++-
> Makefile.target | 3 +++
> chardev/Makefile.objs | 2 +-
> tests/Makefile.include | 4 ++--
> 5 files changed, 11 insertions(+), 5 deletions(-)
Quoted 8 lines
Any reason to do this instead of keeping common-obj-y?
common-obj-y has a lot of other units, including vl.o, making it not possible to link with tests/test-char for ex. However, if we changed it to link with everything except
vl.c(or other changes), we may be able to link with and test spice-qemu-char. The resulting binaries will likely explode though.
Ok, so it's for tests. Fair enough!
Paolo
PATCH 35/54 char: make null_chr_write() the default method
In reply to Marc-André LureauAll chardev must implement chr_write(), but parallel and null chardev both use null_chr_write(). Move it to the base class, so we don't need to export the function when splitting the chardev in respective files.
--- chardev/char.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
chardev/char.c +13 -8
@@ -522,6 +522,18 @@ static void char_init(Object *obj)qemu_mutex_init (&chr->chr_write_lock); } +static int null_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +return len; +} + +static void char_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->chr_write = null_chr_write; +} +static void char_finalize (Object *obj) {Chardev *chr =CHARDEV (obj);
@@ -545,13 +557,9 @@ static const TypeInfo char_type_info = { .instance_finalize = char_finalize, .abstract = true, .class_size = sizeof(ChardevClass), + .class_init = char_class_init, }; -static int null_chr_write (Chardev *chr,const uint8_t *buf,int len) -{ -return len; -} - static void null_chr_open(Chardev *chr, ChardevBackend *backend, bool *be_opened,
@@ -565,7 +573,6 @@ static void char_null_class_init(ObjectClass *oc, void *data)ChardevClass *cc =CHARDEV_CLASS (oc); cc->open = null_chr_open; - cc->chr_write = null_chr_write; }static const TypeInfo char_null_type_info = {
@@ -4681,10 +4688,8 @@ static void char_parallel_class_init(ObjectClass *oc, void *data) cc->parse = qemu_chr_parse_parallel; cc->open = qmp_chardev_open_parallel;#if defined(__linux__) - cc->chr_write = null_chr_write; cc->chr_ioctl = pp_ioctl;#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) - cc->chr_write = null_chr_write; cc->chr_ioctl = pp_ioctl;#endif }
Signature · 1 line
2.11.0
PATCH 36/54 char: move null chardev to its own file
In reply to Marc-André Lureau--- chardev/char-null.c | 31 +++++++++++++++++++++++++++++++ chardev/char.c | 23 ----------------------- chardev/Makefile.objs | 1 + 3 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 chardev/char-null.c
chardev/char-null.c new file +31 -0
@@ -0,0 +1,31 @@ +#include "qemu/osdep.h" +#include "sysemu/char.h" + +static void null_chr_open (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ + *be_opened = false; +} + +static void char_null_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->open = null_chr_open; +} + +static const TypeInfo char_null_type_info = { + .name =TYPE_CHARDEV_NULL , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (Chardev), + .class_init = char_null_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_null_type_info); +} + +type_init (register_types);
chardev/char.c +0 -23
@@ -560,28 +560,6 @@ static const TypeInfo char_type_info = { .class_init = char_class_init, }; -static void null_chr_open (Chardev *chr, -ChardevBackend *backend, -bool *be_opened, -Error **errp) -{ - *be_opened = false; -} - -static void char_null_class_init (ObjectClass *oc,void *data) -{ -ChardevClass *cc =CHARDEV_CLASS (oc); - - cc->open = null_chr_open; -} - -static const TypeInfo char_null_type_info = { - .name =TYPE_CHARDEV_NULL , - .parent =TYPE_CHARDEV , - .instance_size =sizeof (Chardev), - .class_init = char_null_class_init, -}; -/* MUX driver for serial I/O splitting */ #define MAX_MUX 4#define MUX_BUFFER_SIZE 32/* Must be a power of 2. */
@@ -5079,7 +5057,6 @@ static void register_types(void)type_register_static (&char_win_type_info);type_register_static (&char_win_stdio_type_info);#endif -type_register_static (&char_null_type_info);type_register_static (&char_socket_type_info);type_register_static (&char_udp_type_info);type_register_static (&char_ringbuf_type_info);
chardev/Makefile.objs +1 -0
@@ -1 +1,2 @@ chardev-obj-y += char.o +chardev-obj-y += char-null.o
Signature · 1 line
2.11.0
PATCH 37/54 char: move mux to its own file
In reply to Marc-André LureauA mechanical move, except that qemu_chr_write_all() needs to be declared in char.h header to be used from chardev unit files.
--- chardev/char-mux.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 348 +------------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-mux.h | 40 ++++++ include/sysemu/char.h | 3 +- 5 files changed, 377 insertions(+), 347 deletions(-) create mode 100644 chardev/char-mux.c create mode 100644 chardev/char-mux.h
chardev/char-mux.c new file +332 -0
@@ -0,0 +1,332 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "sysemu/block-backend.h" +#include "char-mux.h" + +/* MUX driver for serial I/O splitting */ + +/* Called with chr_write_lock held. */ +static int mux_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +MuxChardev *d =MUX_CHARDEV (chr); +int ret; +if (!d->timestamps) { + ret =qemu_chr_fe_write (&d->chr, buf, len); + }else { +int i; + + ret =0 ; +for (i =0 ; i < len; i++) { +if (d->linestart) { +char buf1[64 ]; +int64_t ti; +int secs; + + ti =qemu_clock_get_ms (QEMU_CLOCK_REALTIME ); +if (d->timestamps_start ==-1 ) { + d->timestamps_start = ti; + } + ti -= d->timestamps_start; + secs = ti /1000 ; +snprintf (buf1,sizeof (buf1), +"[%02d:%02d:%02d.%03d] " , + secs /3600 , + (secs /60 ) %60 , + secs %60 , + (int )(ti %1000 )); +/* XXX this blocks entire thread. Rewrite to use +* qemu_chr_fe_write and background I/O callbacks */ +qemu_chr_fe_write_all (&d->chr, + (uint8_t *)buf1,strlen (buf1)); + d->linestart =0 ; + } + ret +=qemu_chr_fe_write (&d->chr, buf + i,1 ); +if (buf[i] =='\n' ) { + d->linestart =1 ; + } + } + } +return ret; +} + +static const char *const mux_help[] = { +"% h print this help\n\r" , +"% x exit emulator\n\r" , +"% s save disk data back to file (if -snapshot)\n\r" , +"% t toggle console timestamps\n\r" , +"% b send break (magic sysrq)\n\r" , +"% c switch between console and monitor\n\r" , +"% % sends %\n\r" , +NULL +}; + +int term_escape_char =0x01 ;/* ctrl-a is used for escape */ +static void mux_print_help (Chardev *chr) +{ +int i, j; +char ebuf[15 ] ="Escape-Char" ; +char cbuf[50 ] ="\n\r" ; + +if (term_escape_char >0 && term_escape_char <26 ) { +snprintf (cbuf,sizeof (cbuf),"\n\r" ); +snprintf (ebuf,sizeof (ebuf),"C-%c" , term_escape_char -1 +'a' ); + }else { +snprintf (cbuf,sizeof (cbuf), +"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r" , + term_escape_char); + } +/* XXX this blocks entire thread. Rewrite to use +* qemu_chr_fe_write and background I/O callbacks */ +qemu_chr_write_all (chr, (uint8_t *)cbuf,strlen (cbuf)); +for (i =0 ; mux_help[i] !=NULL ; i++) { +for (j =0 ; mux_help[i][j] !='\0' ; j++) { +if (mux_help[i][j] =='%' ) { +qemu_chr_write_all (chr, (uint8_t *)ebuf,strlen (ebuf)); + }else { +qemu_chr_write_all (chr, (uint8_t *)&mux_help[i][j],1 ); + } + } + } +} + +void mux_chr_send_event (MuxChardev *d,int mux_nr,int event) +{ +CharBackend *be = d->backends[mux_nr]; + +if (be && be->chr_event) { + be->chr_event (be->opaque, event); + } +} + +static int mux_proc_byte (Chardev *chr,MuxChardev *d,int ch) +{ +if (d->term_got_escape) { + d->term_got_escape =0 ; +if (ch == term_escape_char) { + goto send_char; + } +switch (ch) { +case '?' : +case 'h' : +mux_print_help (chr); +break ; +case 'x' : + { +const char *term ="QEMU: Terminated\n\r" ; +qemu_chr_write_all (chr, (uint8_t *)term,strlen (term)); +exit (0 ); +break ; + } +case 's' : +blk_commit_all (); +break ; +case 'b' : +qemu_chr_be_event (chr,CHR_EVENT_BREAK ); +break ; +case 'c' : +assert (d->mux_cnt >0 );/* handler registered with first fe */ +/* Switch to the next registered device */ +mux_set_focus (d, (d->focus +1 ) % d->mux_cnt); +break ; +case 't' : + d->timestamps = !d->timestamps; + d->timestamps_start =-1 ; + d->linestart =0 ; +break ; + } + }else if (ch == term_escape_char) { + d->term_got_escape =1 ; + }else { + send_char: +return 1 ; + } +return 0 ; +} + +static void mux_chr_accept_input (Chardev *chr) +{ +MuxChardev *d =MUX_CHARDEV (chr); +int m = d->focus; +CharBackend *be = d->backends[m]; + +while (be && d->prod[m] != d->cons[m] && + be->chr_can_read && be->chr_can_read (be->opaque)) { + be->chr_read (be->opaque, + &d->buffer[m][d->cons[m]++ &MUX_BUFFER_MASK ],1 ); + } +} + +static int mux_chr_can_read (void *opaque) +{ +MuxChardev *d =MUX_CHARDEV (opaque); +int m = d->focus; +CharBackend *be = d->backends[m]; + +if ((d->prod[m] - d->cons[m]) <MUX_BUFFER_SIZE ) { +return 1 ; + } + +if (be && be->chr_can_read) { +return be->chr_can_read (be->opaque); + } + +return 0 ; +} + +static void mux_chr_read (void *opaque,const uint8_t *buf,int size) +{ +Chardev *chr =CHARDEV (opaque); +MuxChardev *d =MUX_CHARDEV (opaque); +int m = d->focus; +CharBackend *be = d->backends[m]; +int i; + +mux_chr_accept_input (opaque); + +for (i =0 ; i < size; i++) +if (mux_proc_byte (chr, d, buf[i])) { +if (d->prod[m] == d->cons[m] && + be && be->chr_can_read && + be->chr_can_read (be->opaque)) { + be->chr_read (be->opaque, &buf[i],1 ); + }else { + d->buffer[m][d->prod[m]++ &MUX_BUFFER_MASK ] = buf[i]; + } + } +} + +bool muxes_realized; + +static void mux_chr_event (void *opaque,int event) +{ +MuxChardev *d =MUX_CHARDEV (opaque); +int i; + +if (!muxes_realized) { +return ; + } + +/* Send the event to all registered listeners */ +for (i =0 ; i < d->mux_cnt; i++) { +mux_chr_send_event (d, i, event); + } +} + +static GSource *mux_chr_add_watch (Chardev *s,GIOCondition cond) +{ +MuxChardev *d =MUX_CHARDEV (s); +Chardev *chr =qemu_chr_fe_get_driver (&d->chr); +ChardevClass *cc =CHARDEV_GET_CLASS (chr); + +if (!cc->chr_add_watch) { +return NULL ; + } + +return cc->chr_add_watch (chr, cond); +} + +static void char_mux_finalize (Object *obj) +{ +MuxChardev *d =MUX_CHARDEV (obj); +int i; + +for (i =0 ; i < d->mux_cnt; i++) { +CharBackend *be = d->backends[i]; +if (be) { + be->chr =NULL ; + } + } +qemu_chr_fe_deinit (&d->chr); +} + +void mux_chr_set_handlers (Chardev *chr,GMainContext *context) +{ +MuxChardev *d =MUX_CHARDEV (chr); + +/* Fix up the real driver with mux routines */ +qemu_chr_fe_set_handlers (&d->chr, + mux_chr_can_read, + mux_chr_read, + mux_chr_event, + chr, + context, true); +} + +void mux_set_focus (MuxChardev *d,int focus) +{ +assert (focus >=0 ); +assert (focus < d->mux_cnt); + +if (d->focus !=-1 ) { +mux_chr_send_event (d, d->focus,CHR_EVENT_MUX_OUT ); + } + + d->focus = focus; +mux_chr_send_event (d, d->focus,CHR_EVENT_MUX_IN ); +} + +static void qemu_chr_open_mux (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevMux *mux = backend->u.mux.data; +Chardev *drv; +MuxChardev *d =MUX_CHARDEV (chr); + + drv =qemu_chr_find (mux->chardev); +if (drv ==NULL ) { +error_setg (errp,"mux: base chardev %s not found" , mux->chardev); +return ; + } + + d->focus =-1 ; +/* only default to opened state if we've realized the initial +* set of muxes +*/ + *be_opened = muxes_realized; +qemu_chr_fe_init (&d->chr, drv, errp); +} + +static void qemu_chr_parse_mux (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *chardev =qemu_opt_get (opts,"chardev" ); +ChardevMux *mux; + +if (chardev ==NULL ) { +error_setg (errp,"chardev: mux: no chardev given" ); +return ; + } + mux = backend->u.mux.data =g_new0 (ChardevMux,1 ); +qemu_chr_parse_common (opts,qapi_ChardevMux_base (mux)); + mux->chardev =g_strdup (chardev); +} + +static void char_mux_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_mux; + cc->open = qemu_chr_open_mux; + cc->chr_write = mux_chr_write; + cc->chr_accept_input = mux_chr_accept_input; + cc->chr_add_watch = mux_chr_add_watch; +} + +static const TypeInfo char_mux_type_info = { + .name =TYPE_CHARDEV_MUX , + .parent =TYPE_CHARDEV , + .class_init = char_mux_class_init, + .instance_size =sizeof (MuxChardev), + .instance_finalize = char_mux_finalize, +}; + +static void register_types (void ) +{ +type_register_static (&char_mux_type_info); +} + +type_init (register_types);
chardev/char.c +3 -345
394 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -1,2 +1,3 @@ chardev-obj-y += char.o +chardev-obj-y += char-mux.o chardev-obj-y += char-null.o
chardev/char-mux.h new file +40 -0
@@ -0,0 +1,40 @@ +#ifndef CHAR_MUX_H +#define CHAR_MUX_H + +#include "sysemu/char.h" + +extern bool muxes_realized; + +#define MAX_MUX 4 +#define MUX_BUFFER_SIZE 32/* Must be a power of 2. */ +#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) +typedef struct MuxChardev { +Chardev parent; +CharBackend *backends[MAX_MUX ]; +CharBackend chr; +int focus; +int mux_cnt; +int term_got_escape; +int max_size; +/* Intermediate input buffer allows to catch escape sequences even if the +currently active device is not accepting any input - but only until it +is full as well. */ +unsigned char buffer[MAX_MUX ][MUX_BUFFER_SIZE ]; +int prod[MAX_MUX ]; +int cons[MAX_MUX ]; +int timestamps; + +/* Protected by the Chardev chr_write_lock. */ +int linestart; +int64_t timestamps_start; +}MuxChardev ; + +#define MUX_CHARDEV (obj) OBJECT_CHECK(MuxChardev, (obj), TYPE_CHARDEV_MUX) +#define CHARDEV_IS_MUX (chr) \ + object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX) + +void mux_chr_set_handlers (Chardev *chr,GMainContext *context); +void mux_set_focus (MuxChardev *d,int focus); +void mux_chr_send_event (MuxChardev *d,int mux_nr,int event); + +#endif /* CHAR_MUX_H */
include/sysemu/char.h +1 -2
@@ -439,6 +439,7 @@ bool qemu_chr_has_feature(Chardev *chr,void qemu_chr_set_feature (Chardev *chr,CharDriverFeature feature);QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename); +int qemu_chr_write_all (Chardev *s,const uint8_t *buf,int len);#define TYPE_CHARDEV "chardev"#define CHARDEV (obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
@@ -461,8 +462,6 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);#define TYPE_CHARDEV_SOCKET "chardev-socket"#define TYPE_CHARDEV_UDP "chardev-udp" -#define CHARDEV_IS_MUX (chr) \ - object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX)#define CHARDEV_IS_RINGBUF (chr) \ object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)#define CHARDEV_IS_PTY (chr) \
Signature · 1 line
2.11.0
PATCH 38/54 char: move ringbuf/memory to its own file
In reply to Marc-André Lureau--- chardev/char-ringbuf.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 217 ----------------------------------------------- chardev/Makefile.objs | 1 + 3 files changed, 226 insertions(+), 217 deletions(-) create mode 100644 chardev/char-ringbuf.c
chardev/char-ringbuf.c new file +225 -0
@@ -0,0 +1,225 @@ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "qmp-commands.h" +#include "qemu/base64.h" + +/* Ring buffer chardev */ + +typedef struct { +Chardev parent; +size_t size; +size_t prod; +size_t cons; +uint8_t *cbuf; +}RingBufChardev ; + +#define RINGBUF_CHARDEV (obj) \ + OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF) + +static size_t ringbuf_count (const Chardev *chr) +{ +const RingBufChardev *d =RINGBUF_CHARDEV (chr); + +return d->prod - d->cons; +} + +static int ringbuf_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +RingBufChardev *d =RINGBUF_CHARDEV (chr); +int i; + +if (!buf || (len <0 )) { +return -1 ; + } + +for (i =0 ; i < len; i++) { + d->cbuf[d->prod++ & (d->size -1 )] = buf[i]; +if (d->prod - d->cons > d->size) { + d->cons = d->prod - d->size; + } + } + +return len; +} + +static int ringbuf_chr_read (Chardev *chr,uint8_t *buf,int len) +{ +RingBufChardev *d =RINGBUF_CHARDEV (chr); +int i; + +qemu_mutex_lock (&chr->chr_write_lock); +for (i =0 ; i < len && d->cons != d->prod; i++) { + buf[i] = d->cbuf[d->cons++ & (d->size -1 )]; + } +qemu_mutex_unlock (&chr->chr_write_lock); + +return i; +} + +static void char_ringbuf_finalize (Object *obj) +{ +RingBufChardev *d =RINGBUF_CHARDEV (obj); + +g_free (d->cbuf); +} + +static void qemu_chr_open_ringbuf (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevRingbuf *opts = backend->u.ringbuf.data; +RingBufChardev *d =RINGBUF_CHARDEV (chr); + + d->size = opts->has_size ? opts->size :65536 ; + +/* The size must be power of 2 */ +if (d->size & (d->size -1 )) { +error_setg (errp,"size of ringbuf chardev must be power of two" ); +return ; + } + + d->prod =0 ; + d->cons =0 ; + d->cbuf =g_malloc0 (d->size); +} + +void qmp_ringbuf_write (const char *device,const char *data, +bool has_format,enum DataFormat format, +Error **errp) +{ +Chardev *chr; +const uint8_t *write_data; +int ret; +gsize write_count; + + chr =qemu_chr_find (device); +if (!chr) { +error_setg (errp,"Device '%s' not found" , device); +return ; + } + +if (!CHARDEV_IS_RINGBUF (chr)) { +error_setg (errp,"%s is not a ringbuf device" , device); +return ; + } + +if (has_format && (format ==DATA_FORMAT_BASE64 )) { + write_data =qbase64_decode (data,-1 , + &write_count, + errp); +if (!write_data) { +return ; + } + }else { + write_data = (uint8_t *)data; + write_count =strlen (data); + } + + ret =ringbuf_chr_write (chr, write_data, write_count); + +if (write_data != (uint8_t *)data) { +g_free ((void *)write_data); + } + +if (ret <0 ) { +error_setg (errp,"Failed to write to device %s" , device); +return ; + } +} + +char *qmp_ringbuf_read (const char *device,int64_t size, +bool has_format,enum DataFormat format, +Error **errp) +{ +Chardev *chr; +uint8_t *read_data; +size_t count; +char *data; + + chr =qemu_chr_find (device); +if (!chr) { +error_setg (errp,"Device '%s' not found" , device); +return NULL ; + } + +if (!CHARDEV_IS_RINGBUF (chr)) { +error_setg (errp,"%s is not a ringbuf device" , device); +return NULL ; + } + +if (size <=0 ) { +error_setg (errp,"size must be greater than zero" ); +return NULL ; + } + + count =ringbuf_count (chr); + size = size > count ? count : size; + read_data =g_malloc (size +1 ); + +ringbuf_chr_read (chr, read_data, size); + +if (has_format && (format ==DATA_FORMAT_BASE64 )) { + data =g_base64_encode (read_data, size); +g_free (read_data); + }else { +/* +* FIXME should read only complete, valid UTF-8 characters up +* to @size bytes. Invalid sequences should be replaced by a +* suitable replacement character. Except when (and only +* when) ring buffer lost characters since last read, initial +* continuation characters should be dropped. +*/ + read_data[size] =0 ; + data = (char *)read_data; + } + +return data; +} + +static void qemu_chr_parse_ringbuf (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +int val; +ChardevRingbuf *ringbuf; + + ringbuf = backend->u.ringbuf.data =g_new0 (ChardevRingbuf,1 ); +qemu_chr_parse_common (opts,qapi_ChardevRingbuf_base (ringbuf)); + + val =qemu_opt_get_size (opts,"size" ,0 ); +if (val !=0 ) { + ringbuf->has_size = true; + ringbuf->size = val; + } +} + +static void char_ringbuf_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_ringbuf; + cc->open = qemu_chr_open_ringbuf; + cc->chr_write = ringbuf_chr_write; +} + +static const TypeInfo char_ringbuf_type_info = { + .name =TYPE_CHARDEV_RINGBUF , + .parent =TYPE_CHARDEV , + .class_init = char_ringbuf_class_init, + .instance_size =sizeof (RingBufChardev), + .instance_finalize = char_ringbuf_finalize, +}; + +/* Bug-compatibility: */ +static const TypeInfo char_memory_type_info = { + .name =TYPE_CHARDEV_MEMORY , + .parent =TYPE_CHARDEV_RINGBUF , +}; + +static void register_types (void ) +{ +type_register_static (&char_ringbuf_type_info); +type_register_static (&char_memory_type_info); +} + +type_init (register_types);
chardev/char.c +0 -217
241 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -1,3 +1,4 @@ chardev-obj-y += char.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o +chardev-obj-y += char-ringbuf.o
Signature · 1 line
2.11.0
PATCH 39/54 char: rename and move to header CHR_READ_BUF_LEN
In reply to Marc-André LureauThis define is used by several character devices, place it in char common header.
--- chardev/char.c | 13 ++++++------- include/sysemu/char.h | 1 + 2 files changed, 7 insertions(+), 7 deletions(-)
chardev/char.c +6 -7
@@ -87,7 +87,6 @@#include "char-mux.h" -#define READ_BUF_LEN 4096#define READ_RETRIES 10#define TCP_MAX_FDS 16
@@ -471,7 +470,7 @@ void qemu_chr_fe_accept_input(CharBackend *be)void qemu_chr_fe_printf (CharBackend *be,const char *fmt, ...) { -char buf[READ_BUF_LEN ]; +char buf[CHR_READ_BUF_LEN ];va_list ap;va_start (ap, fmt);vsnprintf (buf,sizeof (buf), fmt, ap);
@@ -911,7 +910,7 @@ static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)Chardev *chr =CHARDEV (opaque);FDChardev *s =FD_CHARDEV (opaque);int len; -uint8_t buf[READ_BUF_LEN ]; +uint8_t buf[CHR_READ_BUF_LEN ];ssize_t ret; len =sizeof (buf);
@@ -1263,7 +1262,7 @@ static gboolean pty_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)Chardev *chr =CHARDEV (opaque);PtyChardev *s =PTY_CHARDEV (opaque);gsize len; -uint8_t buf[READ_BUF_LEN ]; +uint8_t buf[CHR_READ_BUF_LEN ];ssize_t ret; len =sizeof (buf);
@@ -1953,7 +1952,7 @@ static void win_chr_readfile(Chardev *chr)WinChardev *s =WIN_CHARDEV (chr);int ret, err; -uint8_t buf[READ_BUF_LEN ]; +uint8_t buf[CHR_READ_BUF_LEN ];DWORD size;ZeroMemory (&s->orecv,sizeof (s->orecv));
@@ -2355,7 +2354,7 @@ err1: typedef struct { Chardev parent; QIOChannel *ioc; - uint8_t buf[READ_BUF_LEN]; + uint8_t buf[CHR_READ_BUF_LEN]; int bufcnt; int bufptr; int max_size;
@@ -2760,7 +2759,7 @@ static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque) {Chardev *chr =CHARDEV (opaque);SocketChardev *s =SOCKET_CHARDEV (opaque); -uint8_t buf[READ_BUF_LEN ]; +uint8_t buf[CHR_READ_BUF_LEN ];int len, size;if (!s->connected || s->max_size <=0 ) {
include/sysemu/char.h +1 -0
@@ -22,6 +22,7 @@ typedef enum {CHR_EVENT_CLOSED /* connection closed */ } QEMUChrEvent; +#define CHR_READ_BUF_LEN 4096#define CHR_IOCTL_SERIAL_SET_PARAMS 1 typedef struct {
Signature · 1 line
2.11.0
PATCH 40/54 char: remove unused READ_RETRIES
In reply to Marc-André LureauCuriously unused since its introduction in commit 7b0bfdf52d69.
--- chardev/char.c | 1 - 1 file changed, 1 deletion(-)
chardev/char.c +0 -1
@@ -87,7 +87,6 @@#include "char-mux.h" -#define READ_RETRIES 10#define TCP_MAX_FDS 16/***********************************************************/
Signature · 1 line
2.11.0
PATCH 41/54 char: move QIOChannel-related in char-io.h
In reply to Marc-André Lureau--- chardev/char-io.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 174 +------------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-io.h | 24 +++++++ 4 files changed, 194 insertions(+), 173 deletions(-) create mode 100644 chardev/char-io.c create mode 100644 chardev/char-io.h
chardev/char-io.c new file +168 -0
@@ -0,0 +1,168 @@ +#include "char-io.h" + +typedef struct IOWatchPoll { +GSource parent; + +QIOChannel *ioc; +GSource *src; + +IOCanReadHandler *fd_can_read; +GSourceFunc fd_read; +void *opaque; +GMainContext *context; +}IOWatchPoll ; + +static IOWatchPoll *io_watch_poll_from_source (GSource *source) +{ +return container_of (source, IOWatchPoll, parent); +} + +static gboolean io_watch_poll_prepare (GSource *source, +gint *timeout_) +{ +IOWatchPoll *iwp =io_watch_poll_from_source (source); +bool now_active = iwp->fd_can_read (iwp->opaque) >0 ; +bool was_active = iwp->src !=NULL ; +if (was_active == now_active) { +return FALSE; + } + +if (now_active) { + iwp->src =qio_channel_create_watch ( + iwp->ioc,G_IO_IN |G_IO_ERR |G_IO_HUP |G_IO_NVAL ); +g_source_set_callback (iwp->src, iwp->fd_read, iwp->opaque,NULL ); +g_source_attach (iwp->src, iwp->context); + }else { +g_source_destroy (iwp->src); +g_source_unref (iwp->src); + iwp->src =NULL ; + } +return FALSE; +} + +static gboolean io_watch_poll_check (GSource *source) +{ +return FALSE; +} + +static gboolean io_watch_poll_dispatch (GSource *source,GSourceFunc callback, +gpointer user_data) +{ +abort (); +} + +static void io_watch_poll_finalize (GSource *source) +{ +/* Due to a glib bug, removing the last reference to a source +* inside a finalize callback causes recursive locking (and a +* deadlock). This is not a problem inside other callbacks, +* including dispatch callbacks, so we call io_remove_watch_poll +* to remove this source. At this point, iwp->src must +* be NULL, or we would leak it. +* +* This would be solved much more elegantly by child sources, +* but we support older glib versions that do not have them. +*/ +IOWatchPoll *iwp =io_watch_poll_from_source (source); +assert (iwp->src ==NULL ); +} + +static GSourceFuncs io_watch_poll_funcs = { + .prepare = io_watch_poll_prepare, + .check = io_watch_poll_check, + .dispatch = io_watch_poll_dispatch, + .finalize = io_watch_poll_finalize, +}; + +guint io_add_watch_poll (Chardev *chr, +QIOChannel *ioc, +IOCanReadHandler *fd_can_read, +QIOChannelFunc fd_read, +gpointer user_data, +GMainContext *context) +{ +IOWatchPoll *iwp; +int tag; +char *name; + + iwp = (IOWatchPoll *)g_source_new (&io_watch_poll_funcs, +sizeof (IOWatchPoll)); + iwp->fd_can_read = fd_can_read; + iwp->opaque = user_data; + iwp->ioc = ioc; + iwp->fd_read = (GSourceFunc ) fd_read; + iwp->src =NULL ; + iwp->context = context; + + name =g_strdup_printf ("chardev-iowatch-%s" , chr->label); +g_source_set_name ((GSource *)iwp, name); +g_free (name); + + tag =g_source_attach (&iwp->parent, context); +g_source_unref (&iwp->parent); +return tag; +} + +static void io_remove_watch_poll (guint tag) +{ +GSource *source; +IOWatchPoll *iwp; + +g_return_if_fail (tag >0 ); + + source =g_main_context_find_source_by_id (NULL , tag); +g_return_if_fail (source !=NULL ); + + iwp =io_watch_poll_from_source (source); +if (iwp->src) { +g_source_destroy (iwp->src); +g_source_unref (iwp->src); + iwp->src =NULL ; + } +g_source_destroy (&iwp->parent); +} + +void remove_fd_in_watch (Chardev *chr) +{ +if (chr->fd_in_tag) { +io_remove_watch_poll (chr->fd_in_tag); + chr->fd_in_tag =0 ; + } +} + +int io_channel_send_full (QIOChannel *ioc, +const void *buf,size_t len, +int *fds,size_t nfds) +{ +size_t offset =0 ; + +while (offset < len) { +ssize_t ret =0 ; +struct iovec iov = { .iov_base = (char *)buf + offset, + .iov_len = len - offset }; + + ret =qio_channel_writev_full ( + ioc, &iov,1 , + fds, nfds,NULL ); +if (ret ==QIO_CHANNEL_ERR_BLOCK ) { +if (offset) { +return offset; + } + + errno =EAGAIN ; +return -1 ; + }else if (ret <0 ) { + errno =EINVAL ; +return -1 ; + } + + offset += ret; + } + +return offset; +} + +int io_channel_send (QIOChannel *ioc,const void *buf,size_t len) +{ +return io_channel_send_full (ioc, buf, len,NULL ,0 ); +}
chardev/char.c +1 -173
193 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ chardev-obj-y += char.o +chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o
chardev/char-io.h new file +24 -0
@@ -0,0 +1,24 @@ +#ifndef CHAR_IO_H +#define CHAR_IO_H + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "io/channel.h" +#include "sysemu/char.h" + +/* Can only be used for read */ +guint io_add_watch_poll (Chardev *chr, +QIOChannel *ioc, +IOCanReadHandler *fd_can_read, +QIOChannelFunc fd_read, +gpointer user_data, +GMainContext *context); + +void remove_fd_in_watch (Chardev *chr); + +int io_channel_send (QIOChannel *ioc,const void *buf,size_t len); + +int io_channel_send_full (QIOChannel *ioc,const void *buf,size_t len, +int *fds,size_t nfds); + +#endif /* CHAR_IO_H */
Signature · 1 line
2.11.0
PATCH 42/54 char: move fd chardev in its own file
In reply to Marc-André Lureau--- chardev/char-fd.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 147 +------------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-fd.h | 21 ++++++++ 4 files changed, 171 insertions(+), 145 deletions(-) create mode 100644 chardev/char-fd.c create mode 100644 chardev/char-fd.h
chardev/char-fd.c new file +147 -0
@@ -0,0 +1,147 @@ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "io/channel-file.h" + +#include "char-fd.h" +#include "char-io.h" + +/* Called with chr_write_lock held. */ +static int fd_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +FDChardev *s =FD_CHARDEV (chr); + +return io_channel_send (s->ioc_out, buf, len); +} + +static gboolean fd_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +FDChardev *s =FD_CHARDEV (opaque); +int len; +uint8_t buf[CHR_READ_BUF_LEN ]; +ssize_t ret; + + len =sizeof (buf); +if (len > s->max_size) { + len = s->max_size; + } +if (len ==0 ) { +return TRUE; + } + + ret =qio_channel_read ( + chan, (gchar *)buf, len,NULL ); +if (ret ==0 ) { +remove_fd_in_watch (chr); +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +return FALSE; + } +if (ret >0 ) { +qemu_chr_be_write (chr, buf, ret); + } + +return TRUE; +} + +static int fd_chr_read_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +FDChardev *s =FD_CHARDEV (opaque); + + s->max_size =qemu_chr_be_can_write (chr); +return s->max_size; +} + +static GSource *fd_chr_add_watch (Chardev *chr,GIOCondition cond) +{ +FDChardev *s =FD_CHARDEV (chr); +return qio_channel_create_watch (s->ioc_out, cond); +} + +static void fd_chr_update_read_handler (Chardev *chr, +GMainContext *context) +{ +FDChardev *s =FD_CHARDEV (chr); + +remove_fd_in_watch (chr); +if (s->ioc_in) { + chr->fd_in_tag =io_add_watch_poll (chr, s->ioc_in, + fd_chr_read_poll, + fd_chr_read, chr, + context); + } +} + +static void char_fd_finalize (Object *obj) +{ +Chardev *chr =CHARDEV (obj); +FDChardev *s =FD_CHARDEV (obj); + +remove_fd_in_watch (chr); +if (s->ioc_in) { +object_unref (OBJECT (s->ioc_in)); + } +if (s->ioc_out) { +object_unref (OBJECT (s->ioc_out)); + } + +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +} + +int qmp_chardev_open_file_source (char *src,int flags,Error **errp) +{ +int fd =-1 ; + +TFR (fd =qemu_open (src, flags,0666 )); +if (fd ==-1 ) { +error_setg_file_open (errp, errno, src); + } +return fd; +} + +/* open a character device to a unix fd */ +void qemu_chr_open_fd (Chardev *chr, +int fd_in,int fd_out) +{ +FDChardev *s =FD_CHARDEV (chr); +char *name; + + s->ioc_in =QIO_CHANNEL (qio_channel_file_new_fd (fd_in)); + name =g_strdup_printf ("chardev-file-in-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc_in), name); +g_free (name); + s->ioc_out =QIO_CHANNEL (qio_channel_file_new_fd (fd_out)); + name =g_strdup_printf ("chardev-file-out-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc_out), name); +g_free (name); +qemu_set_nonblock (fd_out); + s->chr = chr; +} + +static void char_fd_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->chr_add_watch = fd_chr_add_watch; + cc->chr_write = fd_chr_write; + cc->chr_update_read_handler = fd_chr_update_read_handler; +} + +static const TypeInfo char_fd_type_info = { + .name =TYPE_CHARDEV_FD , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (FDChardev), + .instance_finalize = char_fd_finalize, + .class_init = char_fd_class_init, + .abstract = true, +}; + +static void register_types (void ) +{ +type_register_static (&char_fd_type_info); +} + +type_init (register_types);
chardev/char.c +2 -145
171 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ chardev-obj-y += char.o +chardev-obj-$(CONFIG_POSIX) += char-fd.o chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o
chardev/char-fd.h new file +21 -0
@@ -0,0 +1,21 @@ +#ifndef CHAR_FD_H +#define CHAR_FD_H + +#include "io/channel.h" +#include "sysemu/char.h" + +typedef struct FDChardev { +Chardev parent; +Chardev *chr; +QIOChannel *ioc_in, *ioc_out; +int max_size; +}FDChardev ; + +#define TYPE_CHARDEV_FD "chardev-fd" + +#define FD_CHARDEV (obj) OBJECT_CHECK(FDChardev, (obj), TYPE_CHARDEV_FD) + +void qemu_chr_open_fd (Chardev *chr,int fd_in,int fd_out); +int qmp_chardev_open_file_source (char *src,int flags,Error **errp); + +#endif /* CHAR_FD_H */
Signature · 1 line
2.11.0
PATCH 43/54 char: move win chardev base class in its own file
In reply to Marc-André Lureau--- chardev/char-win.c | 245 +++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 256 +------------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-win.h | 30 ++++++ 4 files changed, 279 insertions(+), 253 deletions(-) create mode 100644 chardev/char-win.c create mode 100644 chardev/char-win.h
chardev/char-win.c new file +245 -0
@@ -0,0 +1,245 @@ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qapi/error.h" +#include "char-win.h" + +static void win_chr_readfile (Chardev *chr) +{ +WinChardev *s =WIN_CHARDEV (chr); + +int ret, err; +uint8_t buf[CHR_READ_BUF_LEN ]; +DWORD size; + +ZeroMemory (&s->orecv,sizeof (s->orecv)); + s->orecv.hEvent = s->hrecv; + ret =ReadFile (s->hcom, buf, s->len, &size, &s->orecv); +if (!ret) { + err =GetLastError (); +if (err ==ERROR_IO_PENDING ) { + ret =GetOverlappedResult (s->hcom, &s->orecv, &size, TRUE); + } + } + +if (size >0 ) { +qemu_chr_be_write (chr, buf, size); + } +} + +static void win_chr_read (Chardev *chr) +{ +WinChardev *s =WIN_CHARDEV (chr); + +if (s->len > s->max_size) { + s->len = s->max_size; + } +if (s->len ==0 ) { +return ; + } + +win_chr_readfile (chr); +} + +static int win_chr_read_poll (Chardev *chr) +{ +WinChardev *s =WIN_CHARDEV (chr); + + s->max_size =qemu_chr_be_can_write (chr); +return s->max_size; +} + +static int win_chr_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +WinChardev *s =WIN_CHARDEV (opaque); +COMSTAT status; +DWORD comerr; + +ClearCommError (s->hcom, &comerr, &status); +if (status.cbInQue >0 ) { + s->len = status.cbInQue; +win_chr_read_poll (chr); +win_chr_read (chr); +return 1 ; + } +return 0 ; +} + +int win_chr_init (Chardev *chr,const char *filename,Error **errp) +{ +WinChardev *s =WIN_CHARDEV (chr); +COMMCONFIG comcfg; +COMMTIMEOUTS cto = {0 ,0 ,0 ,0 ,0 }; +COMSTAT comstat; +DWORD size; +DWORD err; + + s->hsend =CreateEvent (NULL , TRUE, FALSE,NULL ); +if (!s->hsend) { +error_setg (errp,"Failed CreateEvent" ); + goto fail; + } + s->hrecv =CreateEvent (NULL , TRUE, FALSE,NULL ); +if (!s->hrecv) { +error_setg (errp,"Failed CreateEvent" ); + goto fail; + } + + s->hcom =CreateFile (filename,GENERIC_READ |GENERIC_WRITE ,0 ,NULL , +OPEN_EXISTING ,FILE_FLAG_OVERLAPPED ,0 ); +if (s->hcom ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"Failed CreateFile (%lu)" ,GetLastError ()); + s->hcom =NULL ; + goto fail; + } + +if (!SetupComm (s->hcom,NRECVBUF ,NSENDBUF )) { +error_setg (errp,"Failed SetupComm" ); + goto fail; + } + +ZeroMemory (&comcfg,sizeof (COMMCONFIG )); + size =sizeof (COMMCONFIG ); +GetDefaultCommConfig (filename, &comcfg, &size); + comcfg.dcb.DCBlength =sizeof (DCB ); +CommConfigDialog (filename,NULL , &comcfg); + +if (!SetCommState (s->hcom, &comcfg.dcb)) { +error_setg (errp,"Failed SetCommState" ); + goto fail; + } + +if (!SetCommMask (s->hcom,EV_ERR )) { +error_setg (errp,"Failed SetCommMask" ); + goto fail; + } + + cto.ReadIntervalTimeout =MAXDWORD ; +if (!SetCommTimeouts (s->hcom, &cto)) { +error_setg (errp,"Failed SetCommTimeouts" ); + goto fail; + } + +if (!ClearCommError (s->hcom, &err, &comstat)) { +error_setg (errp,"Failed ClearCommError" ); + goto fail; + } +qemu_add_polling_cb (win_chr_poll, chr); +return 0 ; + + fail: +return -1 ; +} + +int win_chr_pipe_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +WinChardev *s =WIN_CHARDEV (opaque); +DWORD size; + +PeekNamedPipe (s->hcom,NULL ,0 ,NULL , &size,NULL ); +if (size >0 ) { + s->len = size; +win_chr_read_poll (chr); +win_chr_read (chr); +return 1 ; + } +return 0 ; +} + +/* Called with chr_write_lock held. */ +static int win_chr_write (Chardev *chr,const uint8_t *buf,int len1) +{ +WinChardev *s =WIN_CHARDEV (chr); +DWORD len, ret, size, err; + + len = len1; +ZeroMemory (&s->osend,sizeof (s->osend)); + s->osend.hEvent = s->hsend; +while (len >0 ) { +if (s->hsend) { + ret =WriteFile (s->hcom, buf, len, &size, &s->osend); + }else { + ret =WriteFile (s->hcom, buf, len, &size,NULL ); + } +if (!ret) { + err =GetLastError (); +if (err ==ERROR_IO_PENDING ) { + ret =GetOverlappedResult (s->hcom, &s->osend, &size, TRUE); +if (ret) { + buf += size; + len -= size; + }else { +break ; + } + }else { +break ; + } + }else { + buf += size; + len -= size; + } + } +return len1 - len; +} + +static void char_win_finalize (Object *obj) +{ +Chardev *chr =CHARDEV (obj); +WinChardev *s =WIN_CHARDEV (chr); + +if (s->skip_free) { +return ; + } + +if (s->hsend) { +CloseHandle (s->hsend); + s->hsend =NULL ; + } +if (s->hrecv) { +CloseHandle (s->hrecv); + s->hrecv =NULL ; + } +if (s->hcom) { +CloseHandle (s->hcom); + s->hcom =NULL ; + } +if (s->fpipe) { +qemu_del_polling_cb (win_chr_pipe_poll, chr); + }else { +qemu_del_polling_cb (win_chr_poll, chr); + } + +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +} + +void qemu_chr_open_win_file (Chardev *chr,HANDLE fd_out) +{ +WinChardev *s =WIN_CHARDEV (chr); + + s->skip_free = true; + s->hcom = fd_out; +} + +static void char_win_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->chr_write = win_chr_write; +} + +static const TypeInfo char_win_type_info = { + .name =TYPE_CHARDEV_WIN , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (WinChardev), + .instance_finalize = char_win_finalize, + .class_init = char_win_class_init, + .abstract = true, +}; + +static void register_types (void ) +{ +type_register_static (&char_win_type_info); +} + +type_init (register_types);
chardev/char.c +3 -253
289 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -4,3 +4,4 @@ chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o +chardev-obj-$(CONFIG_WIN32) += char-win.o
chardev/char-win.h new file +30 -0
@@ -0,0 +1,30 @@ +#ifndef CHAR_WIN_H +#define CHAR_WIN_H + +#include "sysemu/char.h" + +typedef struct { +Chardev parent; +int max_size; +HANDLE hcom, hrecv, hsend; +OVERLAPPED orecv; +BOOL fpipe; +DWORD len; + +/* Protected by the Chardev chr_write_lock. */ +OVERLAPPED osend; +/* FIXME: file/console do not finalize */ +BOOL skip_free; +}WinChardev ; + +#define NSENDBUF 2048 +#define NRECVBUF 2048 + +#define TYPE_CHARDEV_WIN "chardev-win" +#define WIN_CHARDEV (obj) OBJECT_CHECK(WinChardev, (obj), TYPE_CHARDEV_WIN) + +void qemu_chr_open_win_file (Chardev *chr,HANDLE fd_out); +int win_chr_init (Chardev *chr,const char *filename,Error **errp); +int win_chr_pipe_poll (void *opaque); + +#endif /* CHAR_WIN_H */
Signature · 1 line
2.11.0
PATCH 44/54 char: move win-stdio into its own file
In reply to Marc-André Lureau--- chardev/char-win-stdio.c | 243 +++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 231 +------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-win-stdio.h | 6 ++ 4 files changed, 252 insertions(+), 229 deletions(-) create mode 100644 chardev/char-win-stdio.c create mode 100644 chardev/char-win-stdio.h
chardev/char-win-stdio.c new file +243 -0
@@ -0,0 +1,243 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "char-win.h" +#include "char-win-stdio.h" + +typedef struct { +Chardev parent; +HANDLE hStdIn; +HANDLE hInputReadyEvent; +HANDLE hInputDoneEvent; +HANDLE hInputThread; +uint8_t win_stdio_buf; +}WinStdioChardev ; + +#define WIN_STDIO_CHARDEV (obj) \ + OBJECT_CHECK(WinStdioChardev, (obj), TYPE_CHARDEV_WIN_STDIO) + +static void win_stdio_wait_func (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (opaque); +INPUT_RECORD buf[4 ]; +int ret; +DWORD dwSize; +int i; + + ret =ReadConsoleInput (stdio->hStdIn, buf,ARRAY_SIZE (buf), &dwSize); + +if (!ret) { +/* Avoid error storm */ +qemu_del_wait_object (stdio->hStdIn,NULL ,NULL ); +return ; + } + +for (i =0 ; i < dwSize; i++) { +KEY_EVENT_RECORD *kev = &buf[i].Event.KeyEvent; + +if (buf[i].EventType ==KEY_EVENT && kev->bKeyDown) { +int j; +if (kev->uChar.AsciiChar !=0 ) { +for (j =0 ; j < kev->wRepeatCount; j++) { +if (qemu_chr_be_can_write (chr)) { +uint8_t c = kev->uChar.AsciiChar; +qemu_chr_be_write (chr, &c,1 ); + } + } + } + } + } +} + +static DWORD WINAPI win_stdio_thread(LPVOID param) +{ +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (param); +int ret; +DWORD dwSize; + +while (1 ) { + +/* Wait for one byte */ + ret =ReadFile (stdio->hStdIn, &stdio->win_stdio_buf,1 , &dwSize,NULL ); + +/* Exit in case of error, continue if nothing read */ +if (!ret) { +break ; + } +if (!dwSize) { +continue ; + } + +/* Some terminal emulator returns \r\n for Enter, just pass \n */ +if (stdio->win_stdio_buf =='\r' ) { +continue ; + } + +/* Signal the main thread and wait until the byte was eaten */ +if (!SetEvent (stdio->hInputReadyEvent)) { +break ; + } +if (WaitForSingleObject (stdio->hInputDoneEvent,INFINITE ) + !=WAIT_OBJECT_0 ) { +break ; + } + } + +qemu_del_wait_object (stdio->hInputReadyEvent,NULL ,NULL ); +return 0 ; +} + +static void win_stdio_thread_wait_func (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (opaque); + +if (qemu_chr_be_can_write (chr)) { +qemu_chr_be_write (chr, &stdio->win_stdio_buf,1 ); + } + +SetEvent (stdio->hInputDoneEvent); +} + +static void qemu_chr_set_echo_win_stdio (Chardev *chr,bool echo) +{ +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (chr); +DWORD dwMode =0 ; + +GetConsoleMode (stdio->hStdIn, &dwMode); + +if (echo) { +SetConsoleMode (stdio->hStdIn, dwMode |ENABLE_ECHO_INPUT ); + }else { +SetConsoleMode (stdio->hStdIn, dwMode & ~ENABLE_ECHO_INPUT ); + } +} + +static void qemu_chr_open_stdio (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (chr); +DWORD dwMode; +int is_console =0 ; + + stdio->hStdIn =GetStdHandle (STD_INPUT_HANDLE ); +if (stdio->hStdIn ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"cannot open stdio: invalid handle" ); +return ; + } + + is_console =GetConsoleMode (stdio->hStdIn, &dwMode) !=0 ; + +if (is_console) { +if (qemu_add_wait_object (stdio->hStdIn, + win_stdio_wait_func, chr)) { +error_setg (errp,"qemu_add_wait_object: failed" ); + goto err1; + } + }else { +DWORD dwId; + + stdio->hInputReadyEvent =CreateEvent (NULL , FALSE, FALSE,NULL ); + stdio->hInputDoneEvent =CreateEvent (NULL , FALSE, FALSE,NULL ); +if (stdio->hInputReadyEvent ==INVALID_HANDLE_VALUE + || stdio->hInputDoneEvent ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"cannot create event" ); + goto err2; + } +if (qemu_add_wait_object (stdio->hInputReadyEvent, + win_stdio_thread_wait_func, chr)) { +error_setg (errp,"qemu_add_wait_object: failed" ); + goto err2; + } + stdio->hInputThread =CreateThread (NULL ,0 , win_stdio_thread, + chr,0 , &dwId); + +if (stdio->hInputThread ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"cannot create stdio thread" ); + goto err3; + } + } + + dwMode |=ENABLE_LINE_INPUT ; + +if (is_console) { +/* set the terminal in raw mode */ +/* ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS */ + dwMode |=ENABLE_PROCESSED_INPUT ; + } + +SetConsoleMode (stdio->hStdIn, dwMode); + +qemu_chr_set_echo_win_stdio (chr, false); + +return ; + +err3: +qemu_del_wait_object (stdio->hInputReadyEvent,NULL ,NULL ); +err2: +CloseHandle (stdio->hInputReadyEvent); +CloseHandle (stdio->hInputDoneEvent); +err1: +qemu_del_wait_object (stdio->hStdIn,NULL ,NULL ); +} + +static void char_win_stdio_finalize (Object *obj) +{ +WinStdioChardev *stdio =WIN_STDIO_CHARDEV (obj); + +if (stdio->hInputReadyEvent !=INVALID_HANDLE_VALUE ) { +CloseHandle (stdio->hInputReadyEvent); + } +if (stdio->hInputDoneEvent !=INVALID_HANDLE_VALUE ) { +CloseHandle (stdio->hInputDoneEvent); + } +if (stdio->hInputThread !=INVALID_HANDLE_VALUE ) { +TerminateThread (stdio->hInputThread,0 ); + } +} + +static int win_stdio_write (Chardev *chr,const uint8_t *buf,int len) +{ +HANDLE hStdOut =GetStdHandle (STD_OUTPUT_HANDLE ); +DWORD dwSize; +int len1; + + len1 = len; + +while (len1 >0 ) { +if (!WriteFile (hStdOut, buf, len1, &dwSize,NULL )) { +break ; + } + buf += dwSize; + len1 -= dwSize; + } + +return len - len1; +} + +static void char_win_stdio_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->open = qemu_chr_open_stdio; + cc->chr_write = win_stdio_write; + cc->chr_set_echo = qemu_chr_set_echo_win_stdio; +} + +static const TypeInfo char_win_stdio_type_info = { + .name =TYPE_CHARDEV_WIN_STDIO , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (WinStdioChardev), + .instance_finalize = char_win_stdio_finalize, + .class_init = char_win_stdio_class_init, + .abstract = true, +}; + +static void register_types (void ) +{ +type_register_static (&char_win_stdio_type_info); +} + +type_init (register_types);
chardev/char.c +2 -229
262 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -5,3 +5,4 @@ chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o chardev-obj-$(CONFIG_WIN32) += char-win.o +chardev-obj-$(CONFIG_WIN32) += char-win-stdio.o
chardev/char-win-stdio.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef CHAR_WIN_STDIO_H +#define CHAR_WIN_STDIO_H + +#define TYPE_CHARDEV_WIN_STDIO "chardev-win-stdio" + +#endif /* CHAR_WIN_STDIO_H */
Signature · 1 line
2.11.0
PATCH 45/54 char: move socket chardev to itw own file
In reply to Marc-André Lureau--- chardev/char-socket.c | 995 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 980 +------------------------------------------------ chardev/Makefile.objs | 1 + include/sysemu/char.h | 1 + 4 files changed, 998 insertions(+), 979 deletions(-) create mode 100644 chardev/char-socket.c
chardev/char-socket.c new file +995 -0
@@ -0,0 +1,995 @@ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "io/channel-socket.h" +#include "io/channel-tls.h" +#include "qemu/error-report.h" +#include "qapi/error.h" +#include "qapi/clone-visitor.h" + +#include "char-io.h" + +/***********************************************************/ +/* TCP Net console */ + +#define TCP_MAX_FDS 16 + +typedef struct { +Chardev parent; +QIOChannel *ioc;/* Client I/O channel */ +QIOChannelSocket *sioc;/* Client master channel */ +QIOChannelSocket *listen_ioc; +guint listen_tag; +QCryptoTLSCreds *tls_creds; +int connected; +int max_size; +int do_telnetopt; +int do_nodelay; +int is_unix; +int *read_msgfds; +size_t read_msgfds_num; +int *write_msgfds; +size_t write_msgfds_num; + +SocketAddress *addr; +bool is_listen; +bool is_telnet; + +guint reconnect_timer; +int64_t reconnect_time; +bool connect_err_reported; +}SocketChardev ; + +#define SOCKET_CHARDEV (obj) \ + OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET) + +static gboolean socket_reconnect_timeout (gpointer opaque); + +static void qemu_chr_socket_restart_timer (Chardev *chr) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +char *name; + +assert (s->connected ==0 ); + s->reconnect_timer =g_timeout_add_seconds (s->reconnect_time, + socket_reconnect_timeout, chr); + name =g_strdup_printf ("chardev-socket-reconnect-%s" , chr->label); +g_source_set_name_by_id (s->reconnect_timer, name); +g_free (name); +} + +static void check_report_connect_error (Chardev *chr, +Error *err) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (!s->connect_err_reported) { +error_report ("Unable to connect character device %s: %s" , + chr->label,error_get_pretty (err)); + s->connect_err_reported = true; + } +qemu_chr_socket_restart_timer (chr); +} + +static gboolean tcp_chr_accept (QIOChannel *chan, +GIOCondition cond, +void *opaque); + +/* Called with chr_write_lock held. */ +static int tcp_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (s->connected) { +int ret =io_channel_send_full (s->ioc, buf, len, + s->write_msgfds, + s->write_msgfds_num); + +/* free the written msgfds, no matter what */ +if (s->write_msgfds_num) { +g_free (s->write_msgfds); + s->write_msgfds =0 ; + s->write_msgfds_num =0 ; + } + +return ret; + }else { +/* XXX: indicate an error ? */ +return len; + } +} + +static int tcp_chr_read_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (opaque); +if (!s->connected) { +return 0 ; + } + s->max_size =qemu_chr_be_can_write (chr); +return s->max_size; +} + +#define IAC 255 +#define IAC_BREAK 243 +static void tcp_chr_process_IAC_bytes (Chardev *chr, +SocketChardev *s, +uint8_t *buf,int *size) +{ +/* Handle any telnet client's basic IAC options to satisfy char by +* char mode with no echo. All IAC options will be removed from +* the buf and the do_telnetopt variable will be used to track the +* state of the width of the IAC information. +* +* IAC commands come in sets of 3 bytes with the exception of the +* "IAC BREAK" command and the double IAC. +*/ + +int i; +int j =0 ; + +for (i =0 ; i < *size; i++) { +if (s->do_telnetopt >1 ) { +if ((unsigned char )buf[i] ==IAC && s->do_telnetopt ==2 ) { +/* Double IAC means send an IAC */ +if (j != i) { + buf[j] = buf[i]; + } + j++; + s->do_telnetopt =1 ; + }else { +if ((unsigned char )buf[i] ==IAC_BREAK + && s->do_telnetopt ==2 ) { +/* Handle IAC break commands by sending a serial break */ +qemu_chr_be_event (chr,CHR_EVENT_BREAK ); + s->do_telnetopt++; + } + s->do_telnetopt++; + } +if (s->do_telnetopt >=4 ) { + s->do_telnetopt =1 ; + } + }else { +if ((unsigned char )buf[i] ==IAC ) { + s->do_telnetopt =2 ; + }else { +if (j != i) { + buf[j] = buf[i]; + } + j++; + } + } + } + *size = j; +} + +static int tcp_get_msgfds (Chardev *chr,int *fds,int num) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num; + +assert (num <=TCP_MAX_FDS ); + +if (to_copy) { +int i; + +memcpy (fds, s->read_msgfds, to_copy *sizeof (int )); + +/* Close unused fds */ +for (i = to_copy; i < s->read_msgfds_num; i++) { +close (s->read_msgfds[i]); + } + +g_free (s->read_msgfds); + s->read_msgfds =0 ; + s->read_msgfds_num =0 ; + } + +return to_copy; +} + +static int tcp_set_msgfds (Chardev *chr,int *fds,int num) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +/* clear old pending fd array */ +g_free (s->write_msgfds); + s->write_msgfds =NULL ; + s->write_msgfds_num =0 ; + +if (!s->connected || + !qio_channel_has_feature (s->ioc, +QIO_CHANNEL_FEATURE_FD_PASS )) { +return -1 ; + } + +if (num) { + s->write_msgfds =g_new (int, num); +memcpy (s->write_msgfds, fds, num *sizeof (int )); + } + + s->write_msgfds_num = num; + +return 0 ; +} + +static ssize_t tcp_chr_recv (Chardev *chr,char *buf,size_t len) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +struct iovec iov = { .iov_base = buf, .iov_len = len }; +int ret; +size_t i; +int *msgfds =NULL ; +size_t msgfds_num =0 ; + +if (qio_channel_has_feature (s->ioc,QIO_CHANNEL_FEATURE_FD_PASS )) { + ret =qio_channel_readv_full (s->ioc, &iov,1 , + &msgfds, &msgfds_num, +NULL ); + }else { + ret =qio_channel_readv_full (s->ioc, &iov,1 , +NULL ,NULL , +NULL ); + } + +if (ret ==QIO_CHANNEL_ERR_BLOCK ) { + errno =EAGAIN ; + ret =-1 ; + }else if (ret ==-1 ) { + errno =EIO ; + } + +if (msgfds_num) { +/* close and clean read_msgfds */ +for (i =0 ; i < s->read_msgfds_num; i++) { +close (s->read_msgfds[i]); + } + +if (s->read_msgfds_num) { +g_free (s->read_msgfds); + } + + s->read_msgfds = msgfds; + s->read_msgfds_num = msgfds_num; + } + +for (i =0 ; i < s->read_msgfds_num; i++) { +int fd = s->read_msgfds[i]; +if (fd <0 ) { +continue ; + } + +/* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */ +qemu_set_block (fd); + +#ifndef MSG_CMSG_CLOEXEC +qemu_set_cloexec (fd); +#endif + } + +return ret; +} + +static GSource *tcp_chr_add_watch (Chardev *chr,GIOCondition cond) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +return qio_channel_create_watch (s->ioc, cond); +} + +static void tcp_chr_free_connection (Chardev *chr) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +int i; + +if (!s->connected) { +return ; + } + +if (s->read_msgfds_num) { +for (i =0 ; i < s->read_msgfds_num; i++) { +close (s->read_msgfds[i]); + } +g_free (s->read_msgfds); + s->read_msgfds =NULL ; + s->read_msgfds_num =0 ; + } + +tcp_set_msgfds (chr,NULL ,0 ); +remove_fd_in_watch (chr); +object_unref (OBJECT (s->sioc)); + s->sioc =NULL ; +object_unref (OBJECT (s->ioc)); + s->ioc =NULL ; +g_free (chr->filename); + chr->filename =NULL ; + s->connected =0 ; +} + +static char *SocketAddress_to_str (const char *prefix,SocketAddress *addr, +bool is_listen,bool is_telnet) +{ +switch (addr->type) { +case SOCKET_ADDRESS_KIND_INET : +return g_strdup_printf ("%s%s:%s:%s%s" , prefix, + is_telnet ?"telnet" :"tcp" , + addr->u.inet.data->host, + addr->u.inet.data->port, + is_listen ?",server" :"" ); +break ; +case SOCKET_ADDRESS_KIND_UNIX : +return g_strdup_printf ("%sunix:%s%s" , prefix, + addr->u.q_unix.data->path, + is_listen ?",server" :"" ); +break ; +case SOCKET_ADDRESS_KIND_FD : +return g_strdup_printf ("%sfd:%s%s" , prefix, addr->u.fd.data->str, + is_listen ?",server" :"" ); +break ; +default : +abort (); + } +} + +static void tcp_chr_disconnect (Chardev *chr) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (!s->connected) { +return ; + } + +tcp_chr_free_connection (chr); + +if (s->listen_ioc) { + s->listen_tag =qio_channel_add_watch ( +QIO_CHANNEL (s->listen_ioc),G_IO_IN , tcp_chr_accept, chr,NULL ); + } + chr->filename =SocketAddress_to_str ("disconnected:" , s->addr, + s->is_listen, s->is_telnet); +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +if (s->reconnect_time) { +qemu_chr_socket_restart_timer (chr); + } +} + +static gboolean tcp_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (opaque); +uint8_t buf[CHR_READ_BUF_LEN ]; +int len, size; + +if (!s->connected || s->max_size <=0 ) { +return TRUE; + } + len =sizeof (buf); +if (len > s->max_size) { + len = s->max_size; + } + size =tcp_chr_recv (chr, (void *)buf, len); +if (size ==0 || size ==-1 ) { +/* connection closed */ +tcp_chr_disconnect (chr); + }else if (size >0 ) { +if (s->do_telnetopt) { +tcp_chr_process_IAC_bytes (chr, s, buf, &size); + } +if (size >0 ) { +qemu_chr_be_write (chr, buf, size); + } + } + +return TRUE; +} + +static int tcp_chr_sync_read (Chardev *chr,const uint8_t *buf,int len) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +int size; + +if (!s->connected) { +return 0 ; + } + + size =tcp_chr_recv (chr, (void *) buf, len); +if (size ==0 ) { +/* connection closed */ +tcp_chr_disconnect (chr); + } + +return size; +} + +static char *sockaddr_to_str (struct sockaddr_storage *ss,socklen_t ss_len, +struct sockaddr_storage *ps,socklen_t ps_len, +bool is_listen,bool is_telnet) +{ +char shost[NI_MAXHOST ], sserv[NI_MAXSERV ]; +char phost[NI_MAXHOST ], pserv[NI_MAXSERV ]; +const char *left ="" , *right ="" ; + +switch (ss->ss_family) { +#ifndef _WIN32 +case AF_UNIX : +return g_strdup_printf ("unix:%s%s" , + ((struct sockaddr_un *)(ss))->sun_path, + is_listen ?",server" :"" ); +#endif +case AF_INET6 : + left ="[" ; + right ="]" ; +/* fall through */ +case AF_INET : +getnameinfo ((struct sockaddr *) ss, ss_len, shost,sizeof (shost), + sserv,sizeof (sserv),NI_NUMERICHOST |NI_NUMERICSERV ); +getnameinfo ((struct sockaddr *) ps, ps_len, phost,sizeof (phost), + pserv,sizeof (pserv),NI_NUMERICHOST |NI_NUMERICSERV ); +return g_strdup_printf ("%s:%s%s%s:%s%s <-> %s%s%s:%s" , + is_telnet ?"telnet" :"tcp" , + left, shost, right, sserv, + is_listen ?",server" :"" , + left, phost, right, pserv); + +default : +return g_strdup_printf ("unknown" ); + } +} + +static void tcp_chr_connect (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (opaque); + +g_free (chr->filename); + chr->filename =sockaddr_to_str ( + &s->sioc->localAddr, s->sioc->localAddrLen, + &s->sioc->remoteAddr, s->sioc->remoteAddrLen, + s->is_listen, s->is_telnet); + + s->connected =1 ; +if (s->ioc) { + chr->fd_in_tag =io_add_watch_poll (chr, s->ioc, + tcp_chr_read_poll, + tcp_chr_read, + chr,NULL ); + } +qemu_chr_be_generic_open (chr); +} + +static void tcp_chr_update_read_handler (Chardev *chr, +GMainContext *context) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (!s->connected) { +return ; + } + +remove_fd_in_watch (chr); +if (s->ioc) { + chr->fd_in_tag =io_add_watch_poll (chr, s->ioc, + tcp_chr_read_poll, + tcp_chr_read, chr, + context); + } +} + +typedef struct { +Chardev *chr; +char buf[12 ]; +size_t buflen; +}TCPCharDriverTelnetInit ; + +static gboolean tcp_chr_telnet_init_io (QIOChannel *ioc, + GIOCondition cond G_GNUC_UNUSED, +gpointer user_data) +{ +TCPCharDriverTelnetInit *init = user_data; +ssize_t ret; + + ret =qio_channel_write (ioc, init->buf, init->buflen,NULL ); +if (ret <0 ) { +if (ret ==QIO_CHANNEL_ERR_BLOCK ) { + ret =0 ; + }else { +tcp_chr_disconnect (init->chr); +return FALSE; + } + } + init->buflen -= ret; + +if (init->buflen ==0 ) { +tcp_chr_connect (init->chr); +return FALSE; + } + +memmove (init->buf, init->buf + ret, init->buflen); + +return TRUE; +} + +static void tcp_chr_telnet_init (Chardev *chr) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +TCPCharDriverTelnetInit *init = +g_new0 (TCPCharDriverTelnetInit,1 ); +size_t n =0 ; + + init->chr = chr; + init->buflen =12 ; + +#define IACSET (x, a, b, c) \ + do { \ + x[n++] = a; \ + x[n++] = b; \ + x[n++] = c; \ + } while (0) + +/* Prep the telnet negotion to put telnet in binary, +* no echo, single char mode */ +IACSET (init->buf,0xff ,0xfb ,0x01 );/* IAC WILL ECHO */ +IACSET (init->buf,0xff ,0xfb ,0x03 );/* IAC WILL Suppress go ahead */ +IACSET (init->buf,0xff ,0xfb ,0x00 );/* IAC WILL Binary */ +IACSET (init->buf,0xff ,0xfd ,0x00 );/* IAC DO Binary */ + +#undef IACSET + +qio_channel_add_watch ( + s->ioc,G_IO_OUT , + tcp_chr_telnet_init_io, + init,NULL ); +} + + +static void tcp_chr_tls_handshake (Object *source, +Error *err, +gpointer user_data) +{ +Chardev *chr = user_data; +SocketChardev *s = user_data; + +if (err) { +tcp_chr_disconnect (chr); + }else { +if (s->do_telnetopt) { +tcp_chr_telnet_init (chr); + }else { +tcp_chr_connect (chr); + } + } +} + + +static void tcp_chr_tls_init (Chardev *chr) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +QIOChannelTLS *tioc; +Error *err =NULL ; +gchar *name; + +if (s->is_listen) { + tioc =qio_channel_tls_new_server ( + s->ioc, s->tls_creds, +NULL ,/* XXX Use an ACL */ + &err); + }else { + tioc =qio_channel_tls_new_client ( + s->ioc, s->tls_creds, + s->addr->u.inet.data->host, + &err); + } +if (tioc ==NULL ) { +error_free (err); +tcp_chr_disconnect (chr); +return ; + } + name =g_strdup_printf ("chardev-tls-%s-%s" , + s->is_listen ?"server" :"client" , + chr->label); +qio_channel_set_name (QIO_CHANNEL (tioc), name); +g_free (name); +object_unref (OBJECT (s->ioc)); + s->ioc =QIO_CHANNEL (tioc); + +qio_channel_tls_handshake (tioc, + tcp_chr_tls_handshake, + chr, +NULL ); +} + + +static void tcp_chr_set_client_ioc_name (Chardev *chr, +QIOChannelSocket *sioc) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +char *name; + name =g_strdup_printf ("chardev-tcp-%s-%s" , + s->is_listen ?"server" :"client" , + chr->label); +qio_channel_set_name (QIO_CHANNEL (sioc), name); +g_free (name); + +} + +static int tcp_chr_new_client (Chardev *chr,QIOChannelSocket *sioc) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (s->ioc !=NULL ) { +return -1 ; + } + + s->ioc =QIO_CHANNEL (sioc); +object_ref (OBJECT (sioc)); + s->sioc = sioc; +object_ref (OBJECT (sioc)); + +qio_channel_set_blocking (s->ioc, false,NULL ); + +if (s->do_nodelay) { +qio_channel_set_delay (s->ioc, false); + } +if (s->listen_tag) { +g_source_remove (s->listen_tag); + s->listen_tag =0 ; + } + +if (s->tls_creds) { +tcp_chr_tls_init (chr); + }else { +if (s->do_telnetopt) { +tcp_chr_telnet_init (chr); + }else { +tcp_chr_connect (chr); + } + } + +return 0 ; +} + + +static int tcp_chr_add_client (Chardev *chr,int fd) +{ +int ret; +QIOChannelSocket *sioc; + + sioc =qio_channel_socket_new_fd (fd,NULL ); +if (!sioc) { +return -1 ; + } +tcp_chr_set_client_ioc_name (chr, sioc); + ret =tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +return ret; +} + +static gboolean tcp_chr_accept (QIOChannel *channel, +GIOCondition cond, +void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +QIOChannelSocket *sioc; + + sioc =qio_channel_socket_accept (QIO_CHANNEL_SOCKET (channel), +NULL ); +if (!sioc) { +return TRUE; + } + +tcp_chr_new_client (chr, sioc); + +object_unref (OBJECT (sioc)); + +return TRUE; +} + +static int tcp_chr_wait_connected (Chardev *chr,Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +QIOChannelSocket *sioc; + +/* It can't wait on s->connected, since it is set asynchronously +* in TLS and telnet cases, only wait for an accepted socket */ +while (!s->ioc) { +if (s->is_listen) { +error_report ("QEMU waiting for connection on: %s" , + chr->filename); +qio_channel_set_blocking (QIO_CHANNEL (s->listen_ioc), true,NULL ); +tcp_chr_accept (QIO_CHANNEL (s->listen_ioc),G_IO_IN , chr); +qio_channel_set_blocking (QIO_CHANNEL (s->listen_ioc), false,NULL ); + }else { + sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +if (qio_channel_socket_connect_sync (sioc, s->addr, errp) <0 ) { +object_unref (OBJECT (sioc)); +return -1 ; + } +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); + } + } + +return 0 ; +} + +static void char_socket_finalize (Object *obj) +{ +Chardev *chr =CHARDEV (obj); +SocketChardev *s =SOCKET_CHARDEV (obj); + +tcp_chr_free_connection (chr); + +if (s->reconnect_timer) { +g_source_remove (s->reconnect_timer); + s->reconnect_timer =0 ; + } +qapi_free_SocketAddress (s->addr); +if (s->listen_tag) { +g_source_remove (s->listen_tag); + s->listen_tag =0 ; + } +if (s->listen_ioc) { +object_unref (OBJECT (s->listen_ioc)); + } +if (s->tls_creds) { +object_unref (OBJECT (s->tls_creds)); + } + +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +} + + +static void qemu_chr_socket_connected (Object *src,Error *err,void *opaque) +{ +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (src); +Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (chr); + +if (err) { +check_report_connect_error (chr, err); +object_unref (src); +return ; + } + + s->connect_err_reported = false; +tcp_chr_new_client (chr, sioc); +object_unref (OBJECT (sioc)); +} +static gboolean socket_reconnect_timeout (gpointer opaque) +{ +Chardev *chr =CHARDEV (opaque); +SocketChardev *s =SOCKET_CHARDEV (opaque); +QIOChannelSocket *sioc; + + s->reconnect_timer =0 ; + +if (chr->be_open) { +return false; + } + + sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +qio_channel_socket_connect_async (sioc, s->addr, + qemu_chr_socket_connected, + chr,NULL ); + +return false; +} + +static void qmp_chardev_open_socket (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +SocketChardev *s =SOCKET_CHARDEV (chr); +ChardevSocket *sock = backend->u.socket.data; +SocketAddress *addr = sock->addr; +bool do_nodelay = sock->has_nodelay ? sock->nodelay : false; +bool is_listen = sock->has_server ? sock->server : true; +bool is_telnet = sock->has_telnet ? sock->telnet : false; +bool is_waitconnect = sock->has_wait ? sock->wait : false; +int64_t reconnect = sock->has_reconnect ? sock->reconnect :0 ; +QIOChannelSocket *sioc =NULL ; + + s->is_unix = addr->type ==SOCKET_ADDRESS_KIND_UNIX ; + s->is_listen = is_listen; + s->is_telnet = is_telnet; + s->do_nodelay = do_nodelay; +if (sock->tls_creds) { +Object *creds; + creds =object_resolve_path_component ( +object_get_objects_root (), sock->tls_creds); +if (!creds) { +error_setg (errp,"No TLS credentials with id '%s'" , + sock->tls_creds); + goto error; + } + s->tls_creds = (QCryptoTLSCreds *) +object_dynamic_cast (creds, +TYPE_QCRYPTO_TLS_CREDS ); +if (!s->tls_creds) { +error_setg (errp,"Object with id '%s' is not TLS credentials" , + sock->tls_creds); + goto error; + } +object_ref (OBJECT (s->tls_creds)); +if (is_listen) { +if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_SERVER ) { +error_setg (errp,"%s" , +"Expected TLS credentials for server endpoint" ); + goto error; + } + }else { +if (s->tls_creds->endpoint !=QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT ) { +error_setg (errp,"%s" , +"Expected TLS credentials for client endpoint" ); + goto error; + } + } + } + + s->addr =QAPI_CLONE (SocketAddress, sock->addr); + +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_RECONNECTABLE ); +if (s->is_unix) { +qemu_chr_set_feature (chr,QEMU_CHAR_FEATURE_FD_PASS ); + } + +/* be isn't opened until we get a connection */ + *be_opened = false; + + chr->filename =SocketAddress_to_str ("disconnected:" , + addr, is_listen, is_telnet); + +if (is_listen) { +if (is_telnet) { + s->do_telnetopt =1 ; + } + }else if (reconnect >0 ) { + s->reconnect_time = reconnect; + } + +if (s->reconnect_time) { + sioc =qio_channel_socket_new (); +tcp_chr_set_client_ioc_name (chr, sioc); +qio_channel_socket_connect_async (sioc, s->addr, + qemu_chr_socket_connected, + chr,NULL ); + }else { +if (s->is_listen) { +char *name; + sioc =qio_channel_socket_new (); + + name =g_strdup_printf ("chardev-tcp-listener-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (sioc), name); +g_free (name); + +if (qio_channel_socket_listen_sync (sioc, s->addr, errp) <0 ) { + goto error; + } + s->listen_ioc = sioc; +if (is_waitconnect && +qemu_chr_wait_connected (chr, errp) <0 ) { + goto error; + } +if (!s->ioc) { + s->listen_tag =qio_channel_add_watch ( +QIO_CHANNEL (s->listen_ioc),G_IO_IN , + tcp_chr_accept, chr,NULL ); + } + }else if (qemu_chr_wait_connected (chr, errp) <0 ) { + goto error; + } + } + +return ; + +error: +if (sioc) { +object_unref (OBJECT (sioc)); + } +if (s->tls_creds) { +object_unref (OBJECT (s->tls_creds)); + } +} + +static void qemu_chr_parse_socket (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +bool is_listen =qemu_opt_get_bool (opts,"server" , false); +bool is_waitconnect = is_listen &&qemu_opt_get_bool (opts,"wait" , true); +bool is_telnet =qemu_opt_get_bool (opts,"telnet" , false); +bool do_nodelay = !qemu_opt_get_bool (opts,"delay" , true); +int64_t reconnect =qemu_opt_get_number (opts,"reconnect" ,0 ); +const char *path =qemu_opt_get (opts,"path" ); +const char *host =qemu_opt_get (opts,"host" ); +const char *port =qemu_opt_get (opts,"port" ); +const char *tls_creds =qemu_opt_get (opts,"tls-creds" ); +SocketAddress *addr; +ChardevSocket *sock; + +if (!path) { +if (!host) { +error_setg (errp,"chardev: socket: no host given" ); +return ; + } +if (!port) { +error_setg (errp,"chardev: socket: no port given" ); +return ; + } + }else { +if (tls_creds) { +error_setg (errp,"TLS can only be used over TCP socket" ); +return ; + } + } + + sock = backend->u.socket.data =g_new0 (ChardevSocket,1 ); +qemu_chr_parse_common (opts,qapi_ChardevSocket_base (sock)); + + sock->has_nodelay = true; + sock->nodelay = do_nodelay; + sock->has_server = true; + sock->server = is_listen; + sock->has_telnet = true; + sock->telnet = is_telnet; + sock->has_wait = true; + sock->wait = is_waitconnect; + sock->has_reconnect = true; + sock->reconnect = reconnect; + sock->tls_creds =g_strdup (tls_creds); + + addr =g_new0 (SocketAddress,1 ); +if (path) { +UnixSocketAddress *q_unix; + addr->type =SOCKET_ADDRESS_KIND_UNIX ; + q_unix = addr->u.q_unix.data =g_new0 (UnixSocketAddress,1 ); + q_unix->path =g_strdup (path); + }else { + addr->type =SOCKET_ADDRESS_KIND_INET ; + addr->u.inet.data =g_new (InetSocketAddress,1 ); + *addr->u.inet.data = (InetSocketAddress ) { + .host =g_strdup (host), + .port =g_strdup (port), + .has_to =qemu_opt_get (opts,"to" ), + .to =qemu_opt_get_number (opts,"to" ,0 ), + .has_ipv4 =qemu_opt_get (opts,"ipv4" ), + .ipv4 =qemu_opt_get_bool (opts,"ipv4" ,0 ), + .has_ipv6 =qemu_opt_get (opts,"ipv6" ), + .ipv6 =qemu_opt_get_bool (opts,"ipv6" ,0 ), + }; + } + sock->addr = addr; +} + +static void char_socket_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_socket; + cc->open = qmp_chardev_open_socket; + cc->chr_wait_connected = tcp_chr_wait_connected; + cc->chr_write = tcp_chr_write; + cc->chr_sync_read = tcp_chr_sync_read; + cc->chr_disconnect = tcp_chr_disconnect; + cc->get_msgfds = tcp_get_msgfds; + cc->set_msgfds = tcp_set_msgfds; + cc->chr_add_client = tcp_chr_add_client; + cc->chr_add_watch = tcp_chr_add_watch; + cc->chr_update_read_handler = tcp_chr_update_read_handler; +} + +static const TypeInfo char_socket_type_info = { + .name =TYPE_CHARDEV_SOCKET , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (SocketChardev), + .instance_finalize = char_socket_finalize, + .class_init = char_socket_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_socket_type_info); +} + +type_init (register_types);
chardev/char.c +1 -979
1,016 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -4,5 +4,6 @@ chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o +chardev-obj-y += char-socket.o chardev-obj-$(CONFIG_WIN32) += char-win.o chardev-obj-$(CONFIG_WIN32) += char-win-stdio.o
include/sysemu/char.h +1 -0
@@ -441,6 +441,7 @@ void qemu_chr_set_feature(Chardev *chr, CharDriverFeature feature);QemuOpts *qemu_chr_parse_compat (const char *label,const char *filename);int qemu_chr_write_all (Chardev *s,const uint8_t *buf,int len); +int qemu_chr_wait_connected (Chardev *chr,Error **errp);#define TYPE_CHARDEV "chardev"#define CHARDEV (obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
Signature · 1 line
2.11.0
PATCH 46/54 char: move udp chardev in its own file
In reply to Marc-André Lureau--- chardev/char-udp.c | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 199 ----------------------------------------------- chardev/Makefile.objs | 1 + 3 files changed, 212 insertions(+), 199 deletions(-) create mode 100644 chardev/char-udp.c
chardev/char-udp.c new file +211 -0
@@ -0,0 +1,211 @@ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "io/channel-socket.h" +#include "qapi/error.h" + +#include "char-io.h" + +/***********************************************************/ +/* UDP Net console */ + +typedef struct { +Chardev parent; +QIOChannel *ioc; +uint8_t buf[CHR_READ_BUF_LEN ]; +int bufcnt; +int bufptr; +int max_size; +}UdpChardev ; + +#define UDP_CHARDEV (obj) OBJECT_CHECK(UdpChardev, (obj), TYPE_CHARDEV_UDP) + +/* Called with chr_write_lock held. */ +static int udp_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +UdpChardev *s =UDP_CHARDEV (chr); + +return qio_channel_write ( + s->ioc, (const char *)buf, len,NULL ); +} + +static int udp_chr_read_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +UdpChardev *s =UDP_CHARDEV (opaque); + + s->max_size =qemu_chr_be_can_write (chr); + +/* If there were any stray characters in the queue process them +* first +*/ +while (s->max_size >0 && s->bufptr < s->bufcnt) { +qemu_chr_be_write (chr, &s->buf[s->bufptr],1 ); + s->bufptr++; + s->max_size =qemu_chr_be_can_write (chr); + } +return s->max_size; +} + +static gboolean udp_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +UdpChardev *s =UDP_CHARDEV (opaque); +ssize_t ret; + +if (s->max_size ==0 ) { +return TRUE; + } + ret =qio_channel_read ( + s->ioc, (char *)s->buf,sizeof (s->buf),NULL ); +if (ret <=0 ) { +remove_fd_in_watch (chr); +return FALSE; + } + s->bufcnt = ret; + + s->bufptr =0 ; +while (s->max_size >0 && s->bufptr < s->bufcnt) { +qemu_chr_be_write (chr, &s->buf[s->bufptr],1 ); + s->bufptr++; + s->max_size =qemu_chr_be_can_write (chr); + } + +return TRUE; +} + +static void udp_chr_update_read_handler (Chardev *chr, +GMainContext *context) +{ +UdpChardev *s =UDP_CHARDEV (chr); + +remove_fd_in_watch (chr); +if (s->ioc) { + chr->fd_in_tag =io_add_watch_poll (chr, s->ioc, + udp_chr_read_poll, + udp_chr_read, chr, + context); + } +} + +static void char_udp_finalize (Object *obj) +{ +Chardev *chr =CHARDEV (obj); +UdpChardev *s =UDP_CHARDEV (obj); + +remove_fd_in_watch (chr); +if (s->ioc) { +object_unref (OBJECT (s->ioc)); + } +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +} + +static void qemu_chr_parse_udp (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *host =qemu_opt_get (opts,"host" ); +const char *port =qemu_opt_get (opts,"port" ); +const char *localaddr =qemu_opt_get (opts,"localaddr" ); +const char *localport =qemu_opt_get (opts,"localport" ); +bool has_local = false; +SocketAddress *addr; +ChardevUdp *udp; + +if (host ==NULL ||strlen (host) ==0 ) { + host ="localhost" ; + } +if (port ==NULL ||strlen (port) ==0 ) { +error_setg (errp,"chardev: udp: remote port not specified" ); +return ; + } +if (localport ==NULL ||strlen (localport) ==0 ) { + localport ="0" ; + }else { + has_local = true; + } +if (localaddr ==NULL ||strlen (localaddr) ==0 ) { + localaddr ="" ; + }else { + has_local = true; + } + + udp = backend->u.udp.data =g_new0 (ChardevUdp,1 ); +qemu_chr_parse_common (opts,qapi_ChardevUdp_base (udp)); + + addr =g_new0 (SocketAddress,1 ); + addr->type =SOCKET_ADDRESS_KIND_INET ; + addr->u.inet.data =g_new (InetSocketAddress,1 ); + *addr->u.inet.data = (InetSocketAddress ) { + .host =g_strdup (host), + .port =g_strdup (port), + .has_ipv4 =qemu_opt_get (opts,"ipv4" ), + .ipv4 =qemu_opt_get_bool (opts,"ipv4" ,0 ), + .has_ipv6 =qemu_opt_get (opts,"ipv6" ), + .ipv6 =qemu_opt_get_bool (opts,"ipv6" ,0 ), + }; + udp->remote = addr; + +if (has_local) { + udp->has_local = true; + addr =g_new0 (SocketAddress,1 ); + addr->type =SOCKET_ADDRESS_KIND_INET ; + addr->u.inet.data =g_new (InetSocketAddress,1 ); + *addr->u.inet.data = (InetSocketAddress ) { + .host =g_strdup (localaddr), + .port =g_strdup (localport), + }; + udp->local = addr; + } +} + +static void qmp_chardev_open_udp (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevUdp *udp = backend->u.udp.data; +QIOChannelSocket *sioc =qio_channel_socket_new (); +char *name; +UdpChardev *s =UDP_CHARDEV (chr); + +if (qio_channel_socket_dgram_sync (sioc, + udp->local, udp->remote, + errp) <0 ) { +object_unref (OBJECT (sioc)); +return ; + } + + name =g_strdup_printf ("chardev-udp-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (sioc), name); +g_free (name); + + s->ioc =QIO_CHANNEL (sioc); + s->bufcnt =0 ; + s->bufptr =0 ; +/* be isn't opened until we get a connection */ + *be_opened = false; +} + +static void char_udp_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_udp; + cc->open = qmp_chardev_open_udp; + cc->chr_write = udp_chr_write; + cc->chr_update_read_handler = udp_chr_update_read_handler; +} + +static const TypeInfo char_udp_type_info = { + .name =TYPE_CHARDEV_UDP , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (UdpChardev), + .instance_finalize = char_udp_finalize, + .class_init = char_udp_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_udp_type_info); +} + +type_init (register_types);
chardev/char.c +0 -199
223 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -5,5 +5,6 @@ chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o chardev-obj-y += char-socket.o +chardev-obj-y += char-udp.o chardev-obj-$(CONFIG_WIN32) += char-win.o chardev-obj-$(CONFIG_WIN32) += char-win-stdio.o
Signature · 1 line
2.11.0
PATCH 47/54 char: move file chardev in its own file
In reply to Marc-André Lureau--- chardev/char-file.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 104 --------------------------------------------- chardev/Makefile.objs | 1 + 3 files changed, 116 insertions(+), 104 deletions(-) create mode 100644 chardev/char-file.c
chardev/char-file.c new file +115 -0
@@ -0,0 +1,115 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include "char-fd.h" +#endif + +static void qmp_chardev_open_file (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevFile *file = backend->u.file.data; +#ifdef _WIN32 +HANDLE out; +DWORD accessmode; +DWORD flags; + +if (file->has_in) { +error_setg (errp,"input file not supported" ); +return ; + } + +if (file->has_append && file->append) { +/* Append to file if it already exists. */ + accessmode =FILE_GENERIC_WRITE & ~FILE_WRITE_DATA ; + flags =OPEN_ALWAYS ; + }else { +/* Truncate file if it already exists. */ + accessmode =GENERIC_WRITE ; + flags =CREATE_ALWAYS ; + } + + out =CreateFile (file->out, accessmode,FILE_SHARE_READ ,NULL , flags, +FILE_ATTRIBUTE_NORMAL ,NULL ); +if (out ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"open %s failed" , file->out); +return ; + } + +qemu_chr_open_win_file (chr, out); +#else +int flags, in =-1 , out; + + flags =O_WRONLY |O_CREAT |O_BINARY ; +if (file->has_append && file->append) { + flags |=O_APPEND ; + }else { + flags |=O_TRUNC ; + } + + out =qmp_chardev_open_file_source (file->out, flags, errp); +if (out <0 ) { +return ; + } + +if (file->has_in) { + flags =O_RDONLY ; + in =qmp_chardev_open_file_source (file->in, flags, errp); +if (in <0 ) { +qemu_close (out); +return ; + } + } + +qemu_chr_open_fd (chr, in, out); +#endif +} + +static void qemu_chr_parse_file_out (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *path =qemu_opt_get (opts,"path" ); +ChardevFile *file; + +if (path ==NULL ) { +error_setg (errp,"chardev: file: no filename given" ); +return ; + } + file = backend->u.file.data =g_new0 (ChardevFile,1 ); +qemu_chr_parse_common (opts,qapi_ChardevFile_base (file)); + file->out =g_strdup (path); + + file->has_append = true; + file->append =qemu_opt_get_bool (opts,"append" , false); +} + +static void char_file_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_file_out; + cc->open = qmp_chardev_open_file; +} + +static const TypeInfo char_file_type_info = { + .name =TYPE_CHARDEV_FILE , +#ifdef _WIN32 + .parent =TYPE_CHARDEV_WIN , +#else + .parent =TYPE_CHARDEV_FD , +#endif + .class_init = char_file_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_file_type_info); +} + +type_init (register_types);
chardev/char.c +0 -104
134 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -1,5 +1,6 @@ chardev-obj-y += char.o chardev-obj-$(CONFIG_POSIX) += char-fd.o +chardev-obj-y += char-file.o chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o
Signature · 1 line
2.11.0
PATCH 48/54 char: move stdio in its own file
In reply to Marc-André Lureau--- chardev/char-stdio.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 122 +------------------------------------------ chardev/Makefile.objs | 1 + 3 files changed, 142 insertions(+), 121 deletions(-) create mode 100644 chardev/char-stdio.c
chardev/char-stdio.c new file +140 -0
@@ -0,0 +1,140 @@ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#include "char-win-stdio.h" +#else +#include <termios.h> +#include "char-fd.h" +#endif + +#ifndef _WIN32 +/* init terminal so that we can grab keys */ +static struct termios oldtty; +static int old_fd0_flags; +static bool stdio_in_use; +static bool stdio_allow_signal; +static bool stdio_echo_state; + +static void term_exit (void ) +{ +tcsetattr (0 ,TCSANOW , &oldtty); +fcntl (0 ,F_SETFL , old_fd0_flags); +} + +static void qemu_chr_set_echo_stdio (Chardev *chr,bool echo) +{ +struct termios tty; + + stdio_echo_state = echo; + tty = oldtty; +if (!echo) { + tty.c_iflag &= ~(IGNBRK |BRKINT |PARMRK |ISTRIP + |INLCR |IGNCR |ICRNL |IXON ); + tty.c_oflag |=OPOST ; + tty.c_lflag &= ~(ECHO |ECHONL |ICANON |IEXTEN ); + tty.c_cflag &= ~(CSIZE |PARENB ); + tty.c_cflag |=CS8 ; + tty.c_cc[VMIN ] =1 ; + tty.c_cc[VTIME ] =0 ; + } +if (!stdio_allow_signal) { + tty.c_lflag &= ~ISIG ; + } + +tcsetattr (0 ,TCSANOW , &tty); +} + +static void term_stdio_handler (int sig) +{ +/* restore echo after resume from suspend. */ +qemu_chr_set_echo_stdio (NULL , stdio_echo_state); +} + +static void qemu_chr_open_stdio (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevStdio *opts = backend->u.stdio.data; +struct sigaction act; + +if (is_daemonized ()) { +error_setg (errp,"cannot use stdio with -daemonize" ); +return ; + } + +if (stdio_in_use) { +error_setg (errp,"cannot use stdio by multiple character devices" ); +return ; + } + + stdio_in_use = true; + old_fd0_flags =fcntl (0 ,F_GETFL ); +tcgetattr (0 , &oldtty); +qemu_set_nonblock (0 ); +atexit (term_exit); + +memset (&act,0 ,sizeof (act)); + act.sa_handler = term_stdio_handler; +sigaction (SIGCONT , &act,NULL ); + +qemu_chr_open_fd (chr,0 ,1 ); + +if (opts->has_signal) { + stdio_allow_signal = opts->signal; + } +qemu_chr_set_echo_stdio (chr, false); +} +#endif + +static void qemu_chr_parse_stdio (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +ChardevStdio *stdio; + + stdio = backend->u.stdio.data =g_new0 (ChardevStdio,1 ); +qemu_chr_parse_common (opts,qapi_ChardevStdio_base (stdio)); + stdio->has_signal = true; + stdio->signal =qemu_opt_get_bool (opts,"signal" , true); +} + +static void char_stdio_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_stdio; +#ifndef _WIN32 + cc->open = qemu_chr_open_stdio; + cc->chr_set_echo = qemu_chr_set_echo_stdio; +#endif +} + +static void char_stdio_finalize (Object *obj) +{ +#ifndef _WIN32 +term_exit (); +#endif +} + +static const TypeInfo char_stdio_type_info = { + .name =TYPE_CHARDEV_STDIO , +#ifdef _WIN32 + .parent =TYPE_CHARDEV_WIN_STDIO , +#else + .parent =TYPE_CHARDEV_FD , +#endif + .instance_finalize = char_stdio_finalize, + .class_init = char_stdio_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_stdio_type_info); +} + +type_init (register_types);
chardev/char.c +1 -121
146 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -6,6 +6,7 @@ chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-ringbuf.o chardev-obj-y += char-socket.o +chardev-obj-y += char-stdio.o chardev-obj-y += char-udp.o chardev-obj-$(CONFIG_WIN32) += char-win.o chardev-obj-$(CONFIG_WIN32) += char-win-stdio.o
Signature · 1 line
2.11.0
PATCH 49/54 char: move console in its own file
In reply to Marc-André Lureau--- chardev/char-console.c | 30 ++++++++++++++++++++++++++++++ chardev/char.c | 24 ------------------------ chardev/Makefile.objs | 1 + 3 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 chardev/char-console.c
chardev/char-console.c new file +30 -0
@@ -0,0 +1,30 @@ +#include "qemu/osdep.h" +#include "char-win.h" + +static void qemu_chr_open_win_con (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +qemu_chr_open_win_file (chr,GetStdHandle (STD_OUTPUT_HANDLE )); +} + +static void char_console_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->open = qemu_chr_open_win_con; +} + +static const TypeInfo char_console_type_info = { + .name =TYPE_CHARDEV_CONSOLE , + .parent =TYPE_CHARDEV_WIN , + .class_init = char_console_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_console_type_info); +} + +type_init (register_types);
chardev/char.c +0 -24
@@ -1403,27 +1403,6 @@ static void qemu_chr_open_pipe(Chardev *chr, } } -static void qemu_chr_open_win_con (Chardev *chr, -ChardevBackend *backend, -bool *be_opened, -Error **errp) -{ -qemu_chr_open_win_file (chr,GetStdHandle (STD_OUTPUT_HANDLE )); -} - -static void char_console_class_init (ObjectClass *oc,void *data) -{ -ChardevClass *cc =CHARDEV_CLASS (oc); - - cc->open = qemu_chr_open_win_con; -} - -static const TypeInfo char_console_type_info = { - .name =TYPE_CHARDEV_CONSOLE , - .parent =TYPE_CHARDEV_WIN , - .class_init = char_console_class_init, -}; -#endif /* !_WIN32 */ int qemu_chr_wait_connected (Chardev *chr,Error **errp)
@@ -2319,9 +2298,6 @@ static void register_types(void)#ifdef HAVE_CHARDEV_PTY type_register_static (&char_pty_type_info);#endif -#ifdef _WIN32 -type_register_static (&char_console_type_info); -#endif type_register_static (&char_pipe_type_info);/* this must be done after machine init, since we register FEs with muxes
chardev/Makefile.objs +1 -0
@@ -1,4 +1,5 @@ chardev-obj-y += char.o +chardev-obj-$(CONFIG_WIN32) += char-console.o chardev-obj-$(CONFIG_POSIX) += char-fd.o chardev-obj-y += char-file.o chardev-obj-y += char-io.o
Signature · 1 line
2.11.0
PATCH 50/54 char: move pipe chardev in its own file
In reply to Marc-André Lureau--- chardev/char-pipe.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 141 ------------------------------------------ chardev/Makefile.objs | 1 + 3 files changed, 168 insertions(+), 141 deletions(-) create mode 100644 chardev/char-pipe.c
chardev/char-pipe.c new file +167 -0
@@ -0,0 +1,167 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "sysemu/char.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include "char-fd.h" +#endif + +#ifdef _WIN32 +#define MAXCONNECT 1 +#define NTIMEOUT 5000 + +static int win_chr_pipe_init (Chardev *chr,const char *filename, +Error **errp) +{ +WinChardev *s =WIN_CHARDEV (chr); +OVERLAPPED ov; +int ret; +DWORD size; +char *openname; + + s->fpipe = TRUE; + + s->hsend =CreateEvent (NULL , TRUE, FALSE,NULL ); +if (!s->hsend) { +error_setg (errp,"Failed CreateEvent" ); + goto fail; + } + s->hrecv =CreateEvent (NULL , TRUE, FALSE,NULL ); +if (!s->hrecv) { +error_setg (errp,"Failed CreateEvent" ); + goto fail; + } + + openname =g_strdup_printf ("\\\\.\\pipe\\%s" , filename); + s->hcom =CreateNamedPipe (openname, +PIPE_ACCESS_DUPLEX |FILE_FLAG_OVERLAPPED , +PIPE_TYPE_BYTE |PIPE_READMODE_BYTE | +PIPE_WAIT , +MAXCONNECT ,NSENDBUF ,NRECVBUF ,NTIMEOUT ,NULL ); +g_free (openname); +if (s->hcom ==INVALID_HANDLE_VALUE ) { +error_setg (errp,"Failed CreateNamedPipe (%lu)" ,GetLastError ()); + s->hcom =NULL ; + goto fail; + } + +ZeroMemory (&ov,sizeof (ov)); + ov.hEvent =CreateEvent (NULL , TRUE, FALSE,NULL ); + ret =ConnectNamedPipe (s->hcom, &ov); +if (ret) { +error_setg (errp,"Failed ConnectNamedPipe" ); + goto fail; + } + + ret =GetOverlappedResult (s->hcom, &ov, &size, TRUE); +if (!ret) { +error_setg (errp,"Failed GetOverlappedResult" ); +if (ov.hEvent) { +CloseHandle (ov.hEvent); + ov.hEvent =NULL ; + } + goto fail; + } + +if (ov.hEvent) { +CloseHandle (ov.hEvent); + ov.hEvent =NULL ; + } +qemu_add_polling_cb (win_chr_pipe_poll, chr); +return 0 ; + + fail: +return -1 ; +} + +static void qemu_chr_open_pipe (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevHostdev *opts = backend->u.pipe.data; +const char *filename = opts->device; + +if (win_chr_pipe_init (chr, filename, errp) <0 ) { +return ; + } +} + +#else + +static void qemu_chr_open_pipe (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevHostdev *opts = backend->u.pipe.data; +int fd_in, fd_out; +char *filename_in; +char *filename_out; +const char *filename = opts->device; + + filename_in =g_strdup_printf ("%s.in" , filename); + filename_out =g_strdup_printf ("%s.out" , filename); +TFR (fd_in =qemu_open (filename_in,O_RDWR |O_BINARY )); +TFR (fd_out =qemu_open (filename_out,O_RDWR |O_BINARY )); +g_free (filename_in); +g_free (filename_out); +if (fd_in <0 || fd_out <0 ) { +if (fd_in >=0 ) { +close (fd_in); + } +if (fd_out >=0 ) { +close (fd_out); + } +TFR (fd_in = fd_out =qemu_open (filename,O_RDWR |O_BINARY )); +if (fd_in <0 ) { +error_setg_file_open (errp, errno, filename); +return ; + } + } +qemu_chr_open_fd (chr, fd_in, fd_out); +} + +#endif /* !_WIN32 */ + +static void qemu_chr_parse_pipe (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *device =qemu_opt_get (opts,"path" ); +ChardevHostdev *dev; + +if (device ==NULL ) { +error_setg (errp,"chardev: pipe: no device path given" ); +return ; + } + dev = backend->u.pipe.data =g_new0 (ChardevHostdev,1 ); +qemu_chr_parse_common (opts,qapi_ChardevHostdev_base (dev)); + dev->device =g_strdup (device); +} + +static void char_pipe_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_pipe; + cc->open = qemu_chr_open_pipe; +} + +static const TypeInfo char_pipe_type_info = { + .name =TYPE_CHARDEV_PIPE , +#ifdef _WIN32 + .parent =TYPE_CHARDEV_WIN , +#else + .parent =TYPE_CHARDEV_FD , +#endif + .class_init = char_pipe_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_pipe_type_info); +} + +type_init (register_types);
chardev/char.c +0 -141
165 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -5,6 +5,7 @@ chardev-obj-y += char-file.o chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o +chardev-obj-y += char-pipe.o chardev-obj-y += char-ringbuf.o chardev-obj-y += char-socket.o chardev-obj-y += char-stdio.o
Signature · 1 line
2.11.0
PATCH 51/54 char: move pty chardev in its own file
In reply to Marc-André Lureau--- chardev/char-pty.c | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 258 ---------------------------------------------- chardev/Makefile.objs | 1 + 3 files changed, 278 insertions(+), 258 deletions(-) create mode 100644 chardev/char-pty.c
chardev/char-pty.c new file +277 -0
@@ -0,0 +1,277 @@ +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "sysemu/char.h" +#include "io/channel-file.h" +#include "qemu/sockets.h" +#include "qemu/error-report.h" + +#include "char-io.h" + +#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) + +typedef struct { +Chardev parent; +QIOChannel *ioc; +int read_bytes; + +/* Protected by the Chardev chr_write_lock. */ +int connected; +guint timer_tag; +guint open_tag; +}PtyChardev ; + +#define PTY_CHARDEV (obj) OBJECT_CHECK(PtyChardev, (obj), TYPE_CHARDEV_PTY) + +static void pty_chr_update_read_handler_locked (Chardev *chr); +static void pty_chr_state (Chardev *chr,int connected); + +static gboolean pty_chr_timer (gpointer opaque) +{ +struct Chardev *chr =CHARDEV (opaque); +PtyChardev *s =PTY_CHARDEV (opaque); + +qemu_mutex_lock (&chr->chr_write_lock); + s->timer_tag =0 ; + s->open_tag =0 ; +if (!s->connected) { +/* Next poll ... */ +pty_chr_update_read_handler_locked (chr); + } +qemu_mutex_unlock (&chr->chr_write_lock); +return FALSE; +} + +/* Called with chr_write_lock held. */ +static void pty_chr_rearm_timer (Chardev *chr,int ms) +{ +PtyChardev *s =PTY_CHARDEV (chr); +char *name; + +if (s->timer_tag) { +g_source_remove (s->timer_tag); + s->timer_tag =0 ; + } + +if (ms ==1000 ) { + name =g_strdup_printf ("pty-timer-secs-%s" , chr->label); + s->timer_tag =g_timeout_add_seconds (1 , pty_chr_timer, chr); + }else { + name =g_strdup_printf ("pty-timer-ms-%s" , chr->label); + s->timer_tag =g_timeout_add (ms, pty_chr_timer, chr); + } +g_source_set_name_by_id (s->timer_tag, name); +g_free (name); +} + +/* Called with chr_write_lock held. */ +static void pty_chr_update_read_handler_locked (Chardev *chr) +{ +PtyChardev *s =PTY_CHARDEV (chr); +GPollFD pfd; +int rc; +QIOChannelFile *fioc =QIO_CHANNEL_FILE (s->ioc); + + pfd.fd = fioc->fd; + pfd.events =G_IO_OUT ; + pfd.revents =0 ; +do { + rc =g_poll (&pfd,1 ,0 ); + }while (rc ==-1 && errno ==EINTR ); +assert (rc >=0 ); + +if (pfd.revents &G_IO_HUP ) { +pty_chr_state (chr,0 ); + }else { +pty_chr_state (chr,1 ); + } +} + +static void pty_chr_update_read_handler (Chardev *chr, +GMainContext *context) +{ +qemu_mutex_lock (&chr->chr_write_lock); +pty_chr_update_read_handler_locked (chr); +qemu_mutex_unlock (&chr->chr_write_lock); +} + +/* Called with chr_write_lock held. */ +static int char_pty_chr_write (Chardev *chr,const uint8_t *buf,int len) +{ +PtyChardev *s =PTY_CHARDEV (chr); + +if (!s->connected) { +/* guest sends data, check for (re-)connect */ +pty_chr_update_read_handler_locked (chr); +if (!s->connected) { +return 0 ; + } + } +return io_channel_send (s->ioc, buf, len); +} + +static GSource *pty_chr_add_watch (Chardev *chr,GIOCondition cond) +{ +PtyChardev *s =PTY_CHARDEV (chr); +if (!s->connected) { +return NULL ; + } +return qio_channel_create_watch (s->ioc, cond); +} + +static int pty_chr_read_poll (void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +PtyChardev *s =PTY_CHARDEV (opaque); + + s->read_bytes =qemu_chr_be_can_write (chr); +return s->read_bytes; +} + +static gboolean pty_chr_read (QIOChannel *chan,GIOCondition cond,void *opaque) +{ +Chardev *chr =CHARDEV (opaque); +PtyChardev *s =PTY_CHARDEV (opaque); +gsize len; +uint8_t buf[CHR_READ_BUF_LEN ]; +ssize_t ret; + + len =sizeof (buf); +if (len > s->read_bytes) { + len = s->read_bytes; + } +if (len ==0 ) { +return TRUE; + } + ret =qio_channel_read (s->ioc, (char *)buf, len,NULL ); +if (ret <=0 ) { +pty_chr_state (chr,0 ); +return FALSE; + }else { +pty_chr_state (chr,1 ); +qemu_chr_be_write (chr, buf, ret); + } +return TRUE; +} + +static gboolean qemu_chr_be_generic_open_func (gpointer opaque) +{ +Chardev *chr =CHARDEV (opaque); +PtyChardev *s =PTY_CHARDEV (opaque); + + s->open_tag =0 ; +qemu_chr_be_generic_open (chr); +return FALSE; +} + +/* Called with chr_write_lock held. */ +static void pty_chr_state (Chardev *chr,int connected) +{ +PtyChardev *s =PTY_CHARDEV (chr); + +if (!connected) { +if (s->open_tag) { +g_source_remove (s->open_tag); + s->open_tag =0 ; + } +remove_fd_in_watch (chr); + s->connected =0 ; +/* (re-)connect poll interval for idle guests: once per second. +* We check more frequently in case the guests sends data to +* the virtual device linked to our pty. */ +pty_chr_rearm_timer (chr,1000 ); + }else { +if (s->timer_tag) { +g_source_remove (s->timer_tag); + s->timer_tag =0 ; + } +if (!s->connected) { +g_assert (s->open_tag ==0 ); + s->connected =1 ; + s->open_tag =g_idle_add (qemu_chr_be_generic_open_func, chr); + } +if (!chr->fd_in_tag) { + chr->fd_in_tag =io_add_watch_poll (chr, s->ioc, + pty_chr_read_poll, + pty_chr_read, + chr,NULL ); + } + } +} + +static void char_pty_finalize (Object *obj) +{ +Chardev *chr =CHARDEV (obj); +PtyChardev *s =PTY_CHARDEV (obj); + +qemu_mutex_lock (&chr->chr_write_lock); +pty_chr_state (chr,0 ); +object_unref (OBJECT (s->ioc)); +if (s->timer_tag) { +g_source_remove (s->timer_tag); + s->timer_tag =0 ; + } +qemu_mutex_unlock (&chr->chr_write_lock); +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +} + +static void char_pty_open (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +PtyChardev *s; +int master_fd, slave_fd; +char pty_name[PATH_MAX ]; +char *name; + + master_fd =qemu_openpty_raw (&slave_fd, pty_name); +if (master_fd <0 ) { +error_setg_errno (errp, errno,"Failed to create PTY" ); +return ; + } + +close (slave_fd); +qemu_set_nonblock (master_fd); + + chr->filename =g_strdup_printf ("pty:%s" , pty_name); +error_report ("char device redirected to %s (label %s)" , + pty_name, chr->label); + + s =PTY_CHARDEV (chr); + s->ioc =QIO_CHANNEL (qio_channel_file_new_fd (master_fd)); + name =g_strdup_printf ("chardev-pty-%s" , chr->label); +qio_channel_set_name (QIO_CHANNEL (s->ioc), name); +g_free (name); + s->timer_tag =0 ; + *be_opened = false; +} + +static void char_pty_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->open = char_pty_open; + cc->chr_write = char_pty_chr_write; + cc->chr_update_read_handler = pty_chr_update_read_handler; + cc->chr_add_watch = pty_chr_add_watch; +} + +static const TypeInfo char_pty_type_info = { + .name =TYPE_CHARDEV_PTY , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (PtyChardev), + .instance_finalize = char_pty_finalize, + .class_init = char_pty_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_pty_type_info); +} + +type_init (register_types); + +#endif
chardev/char.c +0 -258
270 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -6,6 +6,7 @@ chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o chardev-obj-y += char-pipe.o +chardev-obj-$(CONFIG_POSIX) += char-pty.o chardev-obj-y += char-ringbuf.o chardev-obj-y += char-socket.o chardev-obj-y += char-stdio.o
Signature · 1 line
2.11.0
PATCH 52/54 char: move serial chardev to itw own file
In reply to Marc-André Lureau--- chardev/char-serial.c | 294 ++++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 280 +---------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-serial.h | 12 +++ 4 files changed, 309 insertions(+), 278 deletions(-) create mode 100644 chardev/char-serial.c create mode 100644 chardev/char-serial.h
chardev/char-serial.c new file +294 -0
@@ -0,0 +1,294 @@ +#include "qemu/osdep.h" +#include "qemu/sockets.h" +#include "io/channel-file.h" +#include "qapi/error.h" + +#ifdef _WIN32 +#include "char-win.h" +#else +#include <sys/ioctl.h> +#include <termios.h> +#include "char-fd.h" +#endif + +#include "char-serial.h" + +#ifdef _WIN32 + +static void qmp_chardev_open_serial (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevHostdev *serial = backend->u.serial.data; + +win_chr_init (chr, serial->device, errp); +} + +#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) + +static void tty_serial_init (int fd,int speed, +int parity,int data_bits,int stop_bits) +{ +struct termios tty; +speed_t spd; + +#if 0 +printf ("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n" , + speed, parity, data_bits, stop_bits); +#endif +tcgetattr (fd, &tty); + +#define check_speed (val) if (speed <= val) { spd = B##val; break; } + speed = speed *10 /11 ; +do { +check_speed (50 ); +check_speed (75 ); +check_speed (110 ); +check_speed (134 ); +check_speed (150 ); +check_speed (200 ); +check_speed (300 ); +check_speed (600 ); +check_speed (1200 ); +check_speed (1800 ); +check_speed (2400 ); +check_speed (4800 ); +check_speed (9600 ); +check_speed (19200 ); +check_speed (38400 ); +/* Non-Posix values follow. They may be unsupported on some systems. */ +check_speed (57600 ); +check_speed (115200 ); +#ifdef B230400 +check_speed (230400 ); +#endif +#ifdef B460800 +check_speed (460800 ); +#endif +#ifdef B500000 +check_speed (500000 ); +#endif +#ifdef B576000 +check_speed (576000 ); +#endif +#ifdef B921600 +check_speed (921600 ); +#endif +#ifdef B1000000 +check_speed (1000000 ); +#endif +#ifdef B1152000 +check_speed (1152000 ); +#endif +#ifdef B1500000 +check_speed (1500000 ); +#endif +#ifdef B2000000 +check_speed (2000000 ); +#endif +#ifdef B2500000 +check_speed (2500000 ); +#endif +#ifdef B3000000 +check_speed (3000000 ); +#endif +#ifdef B3500000 +check_speed (3500000 ); +#endif +#ifdef B4000000 +check_speed (4000000 ); +#endif + spd =B115200 ; + }while (0 ); + +cfsetispeed (&tty, spd); +cfsetospeed (&tty, spd); + + tty.c_iflag &= ~(IGNBRK |BRKINT |PARMRK |ISTRIP + |INLCR |IGNCR |ICRNL |IXON ); + tty.c_oflag |=OPOST ; + tty.c_lflag &= ~(ECHO |ECHONL |ICANON |IEXTEN |ISIG ); + tty.c_cflag &= ~(CSIZE |PARENB |PARODD |CRTSCTS |CSTOPB ); +switch (data_bits) { +default : +case 8 : + tty.c_cflag |=CS8 ; +break ; +case 7 : + tty.c_cflag |=CS7 ; +break ; +case 6 : + tty.c_cflag |=CS6 ; +break ; +case 5 : + tty.c_cflag |=CS5 ; +break ; + } +switch (parity) { +default : +case 'N' : +break ; +case 'E' : + tty.c_cflag |=PARENB ; +break ; +case 'O' : + tty.c_cflag |=PARENB |PARODD ; +break ; + } +if (stop_bits ==2 ) { + tty.c_cflag |=CSTOPB ; + } + +tcsetattr (fd,TCSANOW , &tty); +} + +static int tty_serial_ioctl (Chardev *chr,int cmd,void *arg) +{ +FDChardev *s =FD_CHARDEV (chr); +QIOChannelFile *fioc =QIO_CHANNEL_FILE (s->ioc_in); + +switch (cmd) { +case CHR_IOCTL_SERIAL_SET_PARAMS : + { +QEMUSerialSetParams *ssp = arg; +tty_serial_init (fioc->fd, + ssp->speed, ssp->parity, + ssp->data_bits, ssp->stop_bits); + } +break ; +case CHR_IOCTL_SERIAL_SET_BREAK : + { +int enable = *(int *)arg; +if (enable) { +tcsendbreak (fioc->fd,1 ); + } + } +break ; +case CHR_IOCTL_SERIAL_GET_TIOCM : + { +int sarg =0 ; +int *targ = (int *)arg; +ioctl (fioc->fd,TIOCMGET , &sarg); + *targ =0 ; +if (sarg &TIOCM_CTS ) { + *targ |=CHR_TIOCM_CTS ; + } +if (sarg &TIOCM_CAR ) { + *targ |=CHR_TIOCM_CAR ; + } +if (sarg &TIOCM_DSR ) { + *targ |=CHR_TIOCM_DSR ; + } +if (sarg &TIOCM_RI ) { + *targ |=CHR_TIOCM_RI ; + } +if (sarg &TIOCM_DTR ) { + *targ |=CHR_TIOCM_DTR ; + } +if (sarg &TIOCM_RTS ) { + *targ |=CHR_TIOCM_RTS ; + } + } +break ; +case CHR_IOCTL_SERIAL_SET_TIOCM : + { +int sarg = *(int *)arg; +int targ =0 ; +ioctl (fioc->fd,TIOCMGET , &targ); + targ &= ~(CHR_TIOCM_CTS |CHR_TIOCM_CAR |CHR_TIOCM_DSR + |CHR_TIOCM_RI |CHR_TIOCM_DTR |CHR_TIOCM_RTS ); +if (sarg &CHR_TIOCM_CTS ) { + targ |=TIOCM_CTS ; + } +if (sarg &CHR_TIOCM_CAR ) { + targ |=TIOCM_CAR ; + } +if (sarg &CHR_TIOCM_DSR ) { + targ |=TIOCM_DSR ; + } +if (sarg &CHR_TIOCM_RI ) { + targ |=TIOCM_RI ; + } +if (sarg &CHR_TIOCM_DTR ) { + targ |=TIOCM_DTR ; + } +if (sarg &CHR_TIOCM_RTS ) { + targ |=TIOCM_RTS ; + } +ioctl (fioc->fd,TIOCMSET , &targ); + } +break ; +default : +return -ENOTSUP ; + } +return 0 ; +} + +static void qmp_chardev_open_serial (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevHostdev *serial = backend->u.serial.data; +int fd; + + fd =qmp_chardev_open_file_source (serial->device,O_RDWR , errp); +if (fd <0 ) { +return ; + } +qemu_set_nonblock (fd); +tty_serial_init (fd,115200 ,'N' ,8 ,1 ); + +qemu_chr_open_fd (chr, fd, fd); +} +#endif /* __linux__ || __sun__ */ + +#ifdef HAVE_CHARDEV_SERIAL +static void qemu_chr_parse_serial (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *device =qemu_opt_get (opts,"path" ); +ChardevHostdev *serial; + +if (device ==NULL ) { +error_setg (errp,"chardev: serial/tty: no device path given" ); +return ; + } + serial = backend->u.serial.data =g_new0 (ChardevHostdev,1 ); +qemu_chr_parse_common (opts,qapi_ChardevHostdev_base (serial)); + serial->device =g_strdup (device); +} + +static void char_serial_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_serial; + cc->open = qmp_chardev_open_serial; +#ifndef _WIN32 + cc->chr_ioctl = tty_serial_ioctl; +#endif +} + + +static const TypeInfo char_serial_type_info = { + .name =TYPE_CHARDEV_SERIAL , +#ifdef _WIN32 + .parent =TYPE_CHARDEV_WIN , +#else + .parent =TYPE_CHARDEV_FD , +#endif + .class_init = char_serial_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_serial_type_info); +} + +type_init (register_types); + +#endif
chardev/char.c +2 -278
334 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -8,6 +8,7 @@ chardev-obj-y += char-null.o chardev-obj-y += char-pipe.o chardev-obj-$(CONFIG_POSIX) += char-pty.o chardev-obj-y += char-ringbuf.o +chardev-obj-y += char-serial.o chardev-obj-y += char-socket.o chardev-obj-y += char-stdio.o chardev-obj-y += char-udp.o
chardev/char-serial.h new file +12 -0
@@ -0,0 +1,12 @@ +#ifndef CHAR_SERIAL_H +#define CHAR_SERIAL_H + +#ifdef _WIN32 +#define HAVE_CHARDEV_SERIAL 1 +#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \ + || defined(__GLIBC__) +#define HAVE_CHARDEV_SERIAL 1 +#endif + +#endif
Signature · 1 line
2.11.0
PATCH 53/54 char: move parallel chardev in its own file
In reply to Marc-André Lureau--- chardev/char-parallel.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++ chardev/char.c | 287 +---------------------------------------------- chardev/Makefile.objs | 1 + chardev/char-parallel.h | 9 ++ 4 files changed, 303 insertions(+), 286 deletions(-) create mode 100644 chardev/char-parallel.c create mode 100644 chardev/char-parallel.h
chardev/char-parallel.c new file +292 -0
@@ -0,0 +1,292 @@ +#include "qemu/osdep.h" +#include "sysemu/char.h" +#include "qapi/error.h" +#include <sys/ioctl.h> + +#ifdef CONFIG_BSD +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#include <dev/ppbus/ppi.h> +#include <dev/ppbus/ppbconf.h> +#elif defined(__DragonFly__) +#include <dev/misc/ppi/ppi.h> +#include <bus/ppbus/ppbconf.h> +#endif +#else +#ifdef __linux__ +#include <linux/ppdev.h> +#include <linux/parport.h> +#endif +#endif + +#include "char-fd.h" +#include "char-parallel.h" + +#if defined(__linux__) + +typedef struct { +Chardev parent; +int fd; +int mode; +}ParallelChardev ; + +#define PARALLEL_CHARDEV (obj) \ + OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) + +static int pp_hw_mode (ParallelChardev *s,uint16_t mode) +{ +if (s->mode != mode) { +int m = mode; +if (ioctl (s->fd,PPSETMODE , &m) <0 ) { +return 0 ; + } + s->mode = mode; + } +return 1 ; +} + +static int pp_ioctl (Chardev *chr,int cmd,void *arg) +{ +ParallelChardev *drv =PARALLEL_CHARDEV (chr); +int fd = drv->fd; +uint8_t b; + +switch (cmd) { +case CHR_IOCTL_PP_READ_DATA : +if (ioctl (fd,PPRDATA , &b) <0 ) { +return -ENOTSUP ; + } + *(uint8_t *)arg = b; +break ; +case CHR_IOCTL_PP_WRITE_DATA : + b = *(uint8_t *)arg; +if (ioctl (fd,PPWDATA , &b) <0 ) { +return -ENOTSUP ; + } +break ; +case CHR_IOCTL_PP_READ_CONTROL : +if (ioctl (fd,PPRCONTROL , &b) <0 ) { +return -ENOTSUP ; + } +/* Linux gives only the lowest bits, and no way to know data +direction! For better compatibility set the fixed upper +bits. */ + *(uint8_t *)arg = b |0xc0 ; +break ; +case CHR_IOCTL_PP_WRITE_CONTROL : + b = *(uint8_t *)arg; +if (ioctl (fd,PPWCONTROL , &b) <0 ) { +return -ENOTSUP ; + } +break ; +case CHR_IOCTL_PP_READ_STATUS : +if (ioctl (fd,PPRSTATUS , &b) <0 ) { +return -ENOTSUP ; + } + *(uint8_t *)arg = b; +break ; +case CHR_IOCTL_PP_DATA_DIR : +if (ioctl (fd,PPDATADIR , (int *)arg) <0 ) { +return -ENOTSUP ; + } +break ; +case CHR_IOCTL_PP_EPP_READ_ADDR : +if (pp_hw_mode (drv,IEEE1284_MODE_EPP |IEEE1284_ADDR )) { +struct ParallelIOArg *parg = arg; +int n =read (fd, parg->buffer, parg->count); +if (n != parg->count) { +return -EIO ; + } + } +break ; +case CHR_IOCTL_PP_EPP_READ : +if (pp_hw_mode (drv,IEEE1284_MODE_EPP )) { +struct ParallelIOArg *parg = arg; +int n =read (fd, parg->buffer, parg->count); +if (n != parg->count) { +return -EIO ; + } + } +break ; +case CHR_IOCTL_PP_EPP_WRITE_ADDR : +if (pp_hw_mode (drv,IEEE1284_MODE_EPP |IEEE1284_ADDR )) { +struct ParallelIOArg *parg = arg; +int n =write (fd, parg->buffer, parg->count); +if (n != parg->count) { +return -EIO ; + } + } +break ; +case CHR_IOCTL_PP_EPP_WRITE : +if (pp_hw_mode (drv,IEEE1284_MODE_EPP )) { +struct ParallelIOArg *parg = arg; +int n =write (fd, parg->buffer, parg->count); +if (n != parg->count) { +return -EIO ; + } + } +break ; +default : +return -ENOTSUP ; + } +return 0 ; +} + +static void qemu_chr_open_pp_fd (Chardev *chr, +int fd, +bool *be_opened, +Error **errp) +{ +ParallelChardev *drv =PARALLEL_CHARDEV (chr); + +if (ioctl (fd,PPCLAIM ) <0 ) { +error_setg_errno (errp, errno,"not a parallel port" ); +close (fd); +return ; + } + + drv->fd = fd; + drv->mode =IEEE1284_MODE_COMPAT ; +} +#endif /* __linux__ */ + +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + +typedef struct { +Chardev parent; +int fd; +}ParallelChardev ; + +#define PARALLEL_CHARDEV (obj) \ + OBJECT_CHECK(ParallelChardev, (obj), TYPE_CHARDEV_PARALLEL) + +static int pp_ioctl (Chardev *chr,int cmd,void *arg) +{ +ParallelChardev *drv =PARALLEL_CHARDEV (chr); +uint8_t b; + +switch (cmd) { +case CHR_IOCTL_PP_READ_DATA : +if (ioctl (drv->fd,PPIGDATA , &b) <0 ) { +return -ENOTSUP ; + } + *(uint8_t *)arg = b; +break ; +case CHR_IOCTL_PP_WRITE_DATA : + b = *(uint8_t *)arg; +if (ioctl (drv->fd,PPISDATA , &b) <0 ) { +return -ENOTSUP ; + } +break ; +case CHR_IOCTL_PP_READ_CONTROL : +if (ioctl (drv->fd,PPIGCTRL , &b) <0 ) { +return -ENOTSUP ; + } + *(uint8_t *)arg = b; +break ; +case CHR_IOCTL_PP_WRITE_CONTROL : + b = *(uint8_t *)arg; +if (ioctl (drv->fd,PPISCTRL , &b) <0 ) { +return -ENOTSUP ; + } +break ; +case CHR_IOCTL_PP_READ_STATUS : +if (ioctl (drv->fd,PPIGSTATUS , &b) <0 ) { +return -ENOTSUP ; + } + *(uint8_t *)arg = b; +break ; +default : +return -ENOTSUP ; + } +return 0 ; +} + +static void qemu_chr_open_pp_fd (Chardev *chr, +int fd, +bool *be_opened, +Error **errp) +{ +ParallelChardev *drv =PARALLEL_CHARDEV (chr); + drv->fd = fd; + *be_opened = false; +} +#endif + +#ifdef HAVE_CHARDEV_PARPORT +static void qmp_chardev_open_parallel (Chardev *chr, +ChardevBackend *backend, +bool *be_opened, +Error **errp) +{ +ChardevHostdev *parallel = backend->u.parallel.data; +int fd; + + fd =qmp_chardev_open_file_source (parallel->device,O_RDWR , errp); +if (fd <0 ) { +return ; + } +qemu_chr_open_pp_fd (chr, fd, be_opened, errp); +} + +static void qemu_chr_parse_parallel (QemuOpts *opts,ChardevBackend *backend, +Error **errp) +{ +const char *device =qemu_opt_get (opts,"path" ); +ChardevHostdev *parallel; + +if (device ==NULL ) { +error_setg (errp,"chardev: parallel: no device path given" ); +return ; + } + parallel = backend->u.parallel.data =g_new0 (ChardevHostdev,1 ); +qemu_chr_parse_common (opts,qapi_ChardevHostdev_base (parallel)); + parallel->device =g_strdup (device); +} + +static void char_parallel_class_init (ObjectClass *oc,void *data) +{ +ChardevClass *cc =CHARDEV_CLASS (oc); + + cc->parse = qemu_chr_parse_parallel; + cc->open = qmp_chardev_open_parallel; +#if defined(__linux__) + cc->chr_ioctl = pp_ioctl; +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__DragonFly__) + cc->chr_ioctl = pp_ioctl; +#endif +} + +static void char_parallel_finalize (Object *obj) +{ +#if defined(__linux__) +Chardev *chr =CHARDEV (obj); +ParallelChardev *drv =PARALLEL_CHARDEV (chr); +int fd = drv->fd; + +pp_hw_mode (drv,IEEE1284_MODE_COMPAT ); +ioctl (fd,PPRELEASE ); +close (fd); +qemu_chr_be_event (chr,CHR_EVENT_CLOSED ); +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__DragonFly__) +/* FIXME: close fd? */ +#endif +} + +static const TypeInfo char_parallel_type_info = { + .name =TYPE_CHARDEV_PARALLEL , + .parent =TYPE_CHARDEV , + .instance_size =sizeof (ParallelChardev), + .instance_finalize = char_parallel_finalize, + .class_init = char_parallel_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&char_parallel_type_info); +} + +type_init (register_types); + +#endif
chardev/char.c +1 -286
338 lines not on this page · raw message
chardev/Makefile.objs +1 -0
@@ -5,6 +5,7 @@ chardev-obj-y += char-file.o chardev-obj-y += char-io.o chardev-obj-y += char-mux.o chardev-obj-y += char-null.o +chardev-obj-$(CONFIG_POSIX) += char-parallel.o chardev-obj-y += char-pipe.o chardev-obj-$(CONFIG_POSIX) += char-pty.o chardev-obj-y += char-ringbuf.o
chardev/char-parallel.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef CHAR_PARALLEL_H +#define CHAR_PARALLEL_H + +#if defined(__linux__) || defined(__FreeBSD__) || \ + defined(__FreeBSD_kernel__) || defined(__DragonFly__) +#define HAVE_CHARDEV_PARPORT 1 +#endif + +#endif /* CHAR_PARALLEL_H */
Signature · 1 line
2.11.0
PATCH 54/54 char: headers clean-up
In reply to Marc-André LureauThose could probably be squashed with earlier patches, however I couldn't easily identify them, test them or check if there are still necessary on various platforms.
--- chardev/char.c | 39 --------------------------------------- 1 file changed, 39 deletions(-)
chardev/char.c +0 -39
@@ -26,54 +26,15 @@#include "qemu/cutils.h" #include "monitor/monitor.h" #include "sysemu/sysemu.h" -#include "sysemu/block-backend.h" #include "qemu/error-report.h" -#include "qemu/timer.h" #include "sysemu/char.h" -#include "hw/usb.h" #include "qmp-commands.h" -#include "qapi/clone-visitor.h" #include "qapi-visit.h" -#include "qemu/base64.h" -#include "io/channel-socket.h" -#include "io/channel-tls.h" #include "sysemu/replay.h" #include "qemu/help_option.h" -#include <zlib.h> - -#ifndef _WIN32 -#include <sys/times.h> -#include <sys/wait.h> -#include <sys/ioctl.h> -#include <sys/resource.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <net/if.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <sys/select.h> -#ifdef __sun__ -#include <sys/ethernet.h> -#include <sys/sockio.h> -#include <netinet/arp.h> -#include <netinet/in.h> -#include <netinet/in_systm.h> -#include <netinet/ip.h> -#include <netinet/ip_icmp.h> // must come after ip.h -#include <netinet/udp.h> -#include <netinet/tcp.h> -#endif -#endif - -#include "qemu/sockets.h" -#include "ui/qemu-spice.h" -#include "char-mux.h" #include "char-io.h" -#ifdef _WIN32 -#include "char-win.h" -#endif #include "char-parallel.h" #include "char-serial.h"
Signature · 1 line
2.11.0
Your series seems to have some coding style problems. See output below for more information:
Hi,
Your series seems to have some coding style problems. See output below for more information:
Subject: [Qemu-devel] [PATCH 00/54] WIP: chardev: qom-ify
Type: series
Message-id: [email protected]
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 Switched to a new branch 'test'
6dd47da char: headers clean-up
eae63eb char: move parallel chardev in its own file 513bbbd char: move serial chardev to itw own file 2aad2df char: move pty chardev in its own file ca281a9 char: move pipe chardev in its own file 7badddb char: move console in its own file 647d879 char: move stdio in its own file
55a7839 char: move file chardev in its own file c1d5963 char: move udp chardev in its own file ca11e13 char: move socket chardev to itw own file ff7313b char: move win-stdio into its own file 4c6498d char: move win chardev base class in its own file 33aaee6 char: move fd chardev in its own file 4de906b char: move QIOChannel-related in char-io.h 31c1f1a char: remove unused READ_RETRIES
4efc6d4 char: rename and move to header CHR_READ_BUF_LEN 36c17aa char: move ringbuf/memory to its own file 027ca5a char: move mux to its own file
c6d8321 char: move null chardev to its own file d65b167 char: make null_chr_write() the default method 609664c char: create chardev-obj-y
75fcc6e char: move to chardev/
844de46 char: remove class kind field
ad2221e char: get rid of CharDriver
90b9713 char: remove chr_free
7c4855f char-fd: convert to finalize
43a0815 char-win: convert to finalize
da085e4 char-win: do not override chr_free 41c23fa char-win-stdio: convert to finalize 4fd4b2f char-stdio: convert to finalize
aa5ce7d char-parallel: convert parallel to finalize 1c19acc char-ringbuf: convert to finalize c57180d char-pty: convert to finalize
2105b5a char-socket: convert to finalize
070b2bc char-udp: convert to finalize
40786d6 mux: convert to finalize
a14b7cc msmouse: convert to finalize
1d27cb9 baum: convert to finalize
4a16040 spice-qemu-char: convert to finalize 1b0eeaa chardev: qom-ify
1b252d2 gtk: overwrite the console.c char driver 465bb1b char: use error_report()
2f15b5a spice-char: improve error reporting 96b38bf char: rename TCPChardev and NetChardev c4738ef char: rename CharDriverState Chardev 39e99a6 bt: use qemu_chr_alloc()
3bdbe90 char: allocate CharDriverState as a single object 495e3c5 char: use a feature bit for replay 37280da char: introduce generic qemu_chr_get_kind() 0011e50 char: fold single-user functions in caller 72464ef char: move callbacks in CharDriver 6dc33a0 char: use a static array for backends a3e1bca char: use a const CharDriver
2ac8265 gtk: avoid oob array access
=== OUTPUT BEGIN ===
Checking PATCH 1/54: gtk: avoid oob array access... Checking PATCH 2/54: char: use a const CharDriver... Checking PATCH 3/54: char: use a static array for backends... Checking PATCH 4/54: char: move callbacks in CharDriver...
WARNING: architecture specific defines should be avoided #1106: FILE: qemu-char.c:4704:
+#if defined(__linux__)
WARNING: line over 80 characters
#1110: FILE: qemu-char.c:4708:
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
total: 0 errors, 2 warnings, 1365 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 5/54: char: fold single-user functions in caller... Checking PATCH 6/54: char: introduce generic qemu_chr_get_kind()... Checking PATCH 7/54: char: use a feature bit for replay... Checking PATCH 8/54: char: allocate CharDriverState as a single object... Checking PATCH 9/54: bt: use qemu_chr_alloc()...
Checking PATCH 10/54: char: rename CharDriverState Chardev... Checking PATCH 11/54: char: rename TCPChardev and NetChardev... Checking PATCH 12/54: spice-char: improve error reporting... Checking PATCH 13/54: char: use error_report()...
WARNING: line over 80 characters
#61: FILE: qemu-char.c:4137:
+ g_string_append_printf(str, "\n%s", ChardevBackendKind_lookup[cd->kind]);
total: 0 errors, 1 warnings, 64 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 14/54: gtk: overwrite the console.c char driver... Checking PATCH 15/54: chardev: qom-ify...
WARNING: architecture specific defines should be avoided #1385: FILE: qemu-char.c:1462:
+#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
total: 0 errors, 1 warnings, 3582 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 16/54: spice-qemu-char: convert to finalize... Checking PATCH 17/54: baum: convert to finalize... Checking PATCH 18/54: msmouse: convert to finalize... Checking PATCH 19/54: mux: convert to finalize...
Checking PATCH 20/54: char-udp: convert to finalize... Checking PATCH 21/54: char-socket: convert to finalize... Checking PATCH 22/54: char-pty: convert to finalize... Checking PATCH 23/54: char-ringbuf: convert to finalize... Checking PATCH 24/54: char-parallel: convert parallel to finalize...
WARNING: architecture specific defines should be avoided #48: FILE: qemu-char.c:4696:
+#if defined(__linux__)
WARNING: line over 80 characters
#57: FILE: qemu-char.c:4705:
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
total: 0 errors, 2 warnings, 52 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 25/54: char-stdio: convert to finalize... Checking PATCH 26/54: char-win-stdio: convert to finalize... Checking PATCH 27/54: char-win: do not override chr_free... Checking PATCH 28/54: char-win: convert to finalize... Checking PATCH 29/54: char-fd: convert to finalize...
Checking PATCH 30/54: char: remove chr_free...
Checking PATCH 31/54: char: get rid of CharDriver...
Checking PATCH 32/54: char: remove class kind field... Checking PATCH 33/54: char: move to chardev/...
Checking PATCH 34/54: char: create chardev-obj-y...
Checking PATCH 35/54: char: make null_chr_write() the default method... Checking PATCH 36/54: char: move null chardev to its own file... Checking PATCH 37/54: char: move mux to its own file... Checking PATCH 38/54: char: move ringbuf/memory to its own file... Checking PATCH 39/54: char: rename and move to header CHR_READ_BUF_LEN... Checking PATCH 40/54: char: remove unused READ_RETRIES... Checking PATCH 41/54: char: move QIOChannel-related in char-io.h... Checking PATCH 42/54: char: move fd chardev in its own file... Checking PATCH 43/54: char: move win chardev base class in its own file... Checking PATCH 44/54: char: move win-stdio into its own file... Checking PATCH 45/54: char: move socket chardev to itw own file... Checking PATCH 46/54: char: move udp chardev in its own file... Checking PATCH 47/54: char: move file chardev in its own file... Checking PATCH 48/54: char: move stdio in its own file...
WARNING: architecture specific defines should be avoided
#271: FILE: chardev/char.c:687:
+#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
total: 0 errors, 1 warnings, 293 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 49/54: char: move console in its own file... Checking PATCH 50/54: char: move pipe chardev in its own file... Checking PATCH 51/54: char: move pty chardev in its own file...
WARNING: architecture specific defines should be avoided #40: FILE: chardev/char-pty.c:11:
+#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
total: 0 errors, 1 warnings, 554 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 52/54: char: move serial chardev to itw own file...
ERROR: if this code is redundant consider removing it
#67: FILE: chardev/char-serial.c:38:
+#if 0
ERROR: braces {} are necessary for all arms of this statement #73: FILE: chardev/char-serial.c:44:
+#define check_speed(val) if (speed <= val) { spd = B##val; break; } [...]
total: 2 errors, 0 warnings, 647 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 53/54: char: move parallel chardev in its own file...
WARNING: architecture specific defines should be avoided #36: FILE: chardev/char-parallel.c:7:
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
WARNING: architecture specific defines should be avoided #44: FILE: chardev/char-parallel.c:15:
+#ifdef __linux__
WARNING: architecture specific defines should be avoided #53: FILE: chardev/char-parallel.c:24:
+#if defined(__linux__)
WARNING: line over 80 characters
#181: FILE: chardev/char-parallel.c:152:
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
WARNING: architecture specific defines should be avoided
#181: FILE: chardev/char-parallel.c:152:
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
WARNING: architecture specific defines should be avoided #281: FILE: chardev/char-parallel.c:252:
+#if defined(__linux__)
WARNING: architecture specific defines should be avoided #291: FILE: chardev/char-parallel.c:262:
+#if defined(__linux__)
WARNING: architecture specific defines should be avoided #331: FILE: chardev/char-parallel.h:4:
+#if defined(__linux__) || defined(__FreeBSD__) || \
total: 0 errors, 8 warnings, 646 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
Checking PATCH 54/54: char: headers clean-up...
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]
Quoted 3 lines · Marc-André Lureau
Note: this series has been tested with Linux and cross-building with mingw. It mostly breaks on other platforms,
What breaks exactly?
Paolo
Quoted 3 lines
help welcome for
testing. I'd also like to write more chardev backend tests to ensure no regression (test-char.cis quite limited for now)
Hi
Quoted 10 lines · Paolo Bonzini
Quoted 3 lines · Marc-André Lureau
Note: this series has been tested with Linux and cross-building with mingw. It mostly breaks on other platforms,
What breaks exactly?
Not much that I know. I was afraid it would break on BSD (in particular the move to own file and last patch), so I tested on FreeBSD. The last version of the series works on win32, linux and freebsd. It can be found on
https://github.com/elmarco/qemu/commits/chrfe It's slightly over 80 patches now, and needs some win32 fixes I sent earlier (subprocess tests, and main-loop busy loop fix)
Since Erik started reviewing the series, I suggest to resend the first part until "qom-ify" patch. Erik, feel free to review the remaining part of the WIP series, but more changes happened after iirc. What do you think?
Paolo
Quoted 6 lines
Quoted 4 lines
help welcome for
testing. I'd also like to write more chardev backend tests to ensure no regression (test-char.cis quite limited for now)--
Marc-André Lureau
Quoted 21 lines · Marc-André Lureau
Hi
Quoted 10 lines · Paolo Bonzini
Quoted 3 lines · Marc-André Lureau
Note: this series has been tested with Linux and cross-building with mingw. It mostly breaks on other platforms,
What breaks exactly?
Not much that I know. I was afraid it would break on BSD (in particular the move to own file and last patch), so I tested on FreeBSD. The last version of the series works on win32, linux and freebsd. It can be found on
https://github.com/elmarco/qemu/commits/chrfe It's slightly over 80 patches now, and needs some win32 fixes I sent earlier (subprocess tests, and main-loop busy loop fix)
Quoted 3 lines
Since Erik started reviewing the series, I suggest to resend the first part until "qom-ify" patch. Erik, feel free to review the remaining part of the WIP series, but more changes happened after iirc. What do you think?
It's Eric (but you're not the first, and probably not the last). The qom-ify patch is 15/54, right? Okay, I'll complete my review up to that point, and then you can split the series into two: a v2 through qom-ify (which should probably be ready to go, or only very little to change), and the second half which is still the WIP.
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org