Received: from eggs.gnu.org ([140.186.70.92]:46437) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qf8I9-0007GZ-8U for qemu-devel@nongnu.org; Fri, 08 Jul 2011 06:28:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qf8I4-0002cI-Ge for qemu-devel@nongnu.org; Fri, 08 Jul 2011 06:28:12 -0400 Received: from cantor2.suse.de ([195.135.220.15]:57927 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qf8I3-0002c7-TK for qemu-devel@nongnu.org; Fri, 08 Jul 2011 06:28:08 -0400 Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: text/plain; charset=us-ascii From: Alexander Graf In-Reply-To: <201107081817.38187.cyliu@novell.com> Date: Fri, 8 Jul 2011 12:28:05 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: <3DDF82F0-28B4-4D52-BA6A-84B2EC504ED5@suse.de> References: <1310027075-11462-1-git-send-email-cyliu@novell.com> <4E15B9F1.9070200@suse.de> <201107081817.38187.cyliu@novell.com> Subject: Re: [Qemu-devel] [PATCH] Add "tee" option to qemu char device List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Chun Yan Liu Cc: qemu-devel@nongnu.org, stefano.stabellini@eu.citrix.com On 08.07.2011, at 12:17, Chun Yan Liu wrote: > On Thursday, July 07, 2011 09:51:45 PM you wrote: >> On 07/07/2011 10:24 AM, Chunyan Liu wrote: >>> In previous thread "Support logging xen-guest console", it's = considered >>> that adding a "tee" option to char layer is a more generic way and = makes >>> more sense. >>> = http://lists.nongnu.org/archive/html/qemu-devel/2011-06/msg03011.html >>>=20 >>> Following is an implementation of "tee" option to char device. It = could >>> be used >>>=20 >>> as follows: >>> -chardev pty,id=3Did,path=3Dpath,[mux=3Don|off],[tee=3Dfilepath] >>> -serial tee:filepath,pty >>>=20 >>> With "tee" option, "pty" output would be duplicated to filepath. >>>=20 >>> I've ported this patch to qemu-xen and tested with xen guests = already. >>> But I'm not very clear how to test the qemu binary directly. Any = info? >>>=20 >>> Please share your comments. Thanks! >>>=20 >>> Signed-off-by: Chunyan Liu >>> --- >>>=20 >>> qemu-char.c | 159 >>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >>> qemu-config.c | 3 + >>=20 >> This is missing documentation in *.hx >>=20 >>> 2 files changed, 162 insertions(+), 0 deletions(-) >>>=20 >>> diff --git a/qemu-char.c b/qemu-char.c >>> index fb13b28..7281ab4 100644 >>> --- a/qemu-char.c >>> +++ b/qemu-char.c >>> @@ -228,6 +228,135 @@ static CharDriverState = *qemu_chr_open_null(QemuOpts >>> *opts) >>>=20 >>> return chr; >>>=20 >>> } >>>=20 >>> +/* Tee driver */ >>> +typedef struct { >>> + CharDriverState *basechr; /* base io*/ >>> + CharDriverState *filechr; /* duplicate output to file */ >>> +} TeeDriver; >>> + >>> +static void tee_init(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->init) { >>> + s->basechr->init(s->basechr); >>> + } >>> + if (s->filechr->init) { >>> + s->filechr->init(s->filechr); >>> + } >>> +} >>> + >>> +static void tee_chr_update_read_handler(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + qemu_chr_add_handlers(s->basechr, chr->chr_can_read, = chr->chr_read, >>> + chr->chr_event, chr->handler_opaque); >>> +} >>> + >>> +static int tee_chr_write(CharDriverState *chr, const uint8_t *buf, = int >>> len) +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->filechr->chr_write) { >>> + s->filechr->chr_write(s->filechr, buf, len); >>=20 >> What would we do if the file write didn't finish? > Result of file write is ignored, we cann't do anything except printing = some=20 > error log, and we don't want char device output is affacted by that = result.=20 > So, still write to char device. How do you think?=20 Well, we could repeat the write at a later point or escalate up that the = write failed. We could even go as far as failing the write altogether if = the file write failed. The return value is "number of bytes written" = btw, so there could be a case where we send a write of 10 bytes in, the = file backend writes 5 bytes and the other backend writes 7. But I guess we should just ignore these corner cases and put a comment = in the code saying that we're aware of the potential issues :). >>=20 >>> + } >>> + if (s->basechr->chr_write) { >>> + return s->basechr->chr_write(s->basechr, buf, len); >>> + } >>> + return 0; >>> +} >>> + >>> +static void tee_chr_close(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_close) { >>> + s->basechr->chr_close(s->basechr); >>> + } >>> + if (s->filechr->chr_close) { >>> + s->filechr->chr_close(s->filechr); >>> + } >>> + qemu_free(s); >>> +} >>> + >>> +static int tee_chr_ioctl(CharDriverState *chr, int cmd, void *arg) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_ioctl) { >>> + return s->basechr->chr_ioctl(s->basechr, cmd, arg); >>> + } >>> + return 0; >>> +} >>> + >>> +static int tee_get_msgfd(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->get_msgfd) { >>> + return s->basechr->get_msgfd(s->basechr); >>> + } >>> + return -1; >>> +} >>> + >>> +static void tee_chr_send_event(CharDriverState *chr, int event) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_send_event) { >>> + s->basechr->chr_send_event(s->basechr, event); >>> + } >>> +} >>> + >>> +static void tee_chr_accept_input(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_accept_input) { >>> + s->basechr->chr_accept_input(s->basechr); >>> + } >>> +} >>> +static void tee_chr_set_echo(CharDriverState *chr, bool echo) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_set_echo) { >>> + s->basechr->chr_set_echo(s->basechr, echo); >>> + } >>> +} >>> +static void tee_chr_guest_open(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_guest_open) { >>> + s->basechr->chr_guest_open(s->basechr); >>> + } >>> +} >>> +static void tee_chr_guest_close(CharDriverState *chr) >>> +{ >>> + TeeDriver *s =3D chr->opaque; >>> + if (s->basechr->chr_guest_close) { >>> + s->basechr->chr_guest_close(s->basechr); >>> + } >>> +} >>> + >>> +static CharDriverState *qemu_chr_open_tee(CharDriverState *basechr, >>> + CharDriverState *filechr) >>> +{ >>> + CharDriverState *chr; >>> + TeeDriver *d; >>> + >>> + chr =3D qemu_mallocz(sizeof(CharDriverState)); >>> + d =3D qemu_mallocz(sizeof(TeeDriver)); >>=20 >> Instead of having 2 allocated regions, could you please fold them >> together and access each other through DO_UPCAST? >>=20 >> typedef struct { >> CharDriverState chr; /* our own driver state */ >> CharDriverState *basechr; /* base io*/ >> CharDriverState *filechr; /* duplicate output to file */ >> } TeeDriver; >>=20 >>=20 >> [...] >>=20 >> void foo(CharDriverState *chr) >> { >> TeeDriver *d =3D DO_UPCAST(TeeDriver, chr, chr); >> [...] >>=20 >> } >>=20 >>> + >>> + d->basechr =3D basechr; >>> + d->filechr =3D filechr; >>> + chr->opaque =3D d; >>> + chr->init =3D tee_init; >>> + chr->chr_write =3D tee_chr_write; >>> + chr->chr_close =3D tee_chr_close; >>> + chr->chr_update_read_handler =3D tee_chr_update_read_handler; >>> + chr->chr_ioctl =3D tee_chr_ioctl; >>> + chr->get_msgfd =3D tee_get_msgfd; >>> + chr->chr_send_event =3D tee_chr_send_event; >>> + chr->chr_accept_input =3D tee_chr_accept_input; >>> + chr->chr_set_echo =3D tee_chr_set_echo; >>> + chr->chr_guest_open =3D tee_chr_guest_open; >>> + chr->chr_guest_close =3D tee_chr_guest_close; >>> + >>> + return chr; >>> +} >>>=20 >>> /* MUX driver for serial I/O splitting */ >>> #define MAX_MUX 4 >>> #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ >>>=20 >>> @@ -2356,6 +2485,13 @@ QemuOpts *qemu_chr_parse_compat(const char = *label, >>> const char *filename) >>>=20 >>> qemu_opt_set(opts, "mux", "on"); >>>=20 >>> } >>>=20 >>> + if (strstart(filename, "tee:",&p)) { >>> + char tee_fpath[1024]; >>> + p =3D get_opt_value(tee_fpath, sizeof(tee_fpath), p); >>> + filename =3D p+1; >>> + qemu_opt_set(opts, "tee", tee_fpath); >>=20 >> This should set an function local variable to remember that it's >> actually the "tee" backend we want to use and then set "backend" to >> "tee" at the end after saving the determined "backend" option to the >> "tee_backend"(?) option. >>=20 >> The tee backend really shouldn't be too different from the other >> backends :). >=20 > I have thought of implementing it as a new backend. But I reviewed = "mux" code,=20 > it's implemented as an option. So, .. Ugh, I figured you might say that. Please ignore the mux target. It's = ugly and hacky and been a constant source of pain :). It's very useful = in practice, but you don't need all those hacks for a tee target. Alex