* [Qemu-devel] [PATCH v3 1/1] qemu-char: socket backend: disconnect on write error
@ 2017-02-02 14:26 Denis V. Lunev
2017-02-03 18:59 ` Paolo Bonzini
0 siblings, 1 reply; 2+ messages in thread
From: Denis V. Lunev @ 2017-02-02 14:26 UTC (permalink / raw)
To: qemu-devel
Cc: Anton Nefedov, Denis V . Lunev, Paolo Bonzini,
Daniel P . Berrange, Marc-André Lureau
From: Anton Nefedov <anton.nefedov@virtuozzo.com>
Socket backend read handler should normally perform a disconnect, however
the read handler may not get a chance to run if the frontend is not ready
(qemu_chr_be_can_write() == 0).
This means that in virtio-serial frontend case if
- the host has disconnected (giving EPIPE on socket write)
- and the guest has disconnected (-> frontend not ready -> backend
will not read)
- and there is still data (frontend->backend) to flush (has to be a really
tricky timing but nevertheless, we have observed the case in production)
This results in virtio-serial trying to flush this data continiously forming
a busy loop.
Solution: react on write error in the socket write handler.
errno is not reliable after qio_channel_writev_full(), so we may not get
the exact EPIPE, so disconnect on any error but QIO_CHANNEL_ERR_BLOCK which
io_channel_send_full() converts to errno EAGAIN.
We must not disconnect right away though, there still may be data to read
(see 4bf1cb0).
Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Daniel P. Berrange <berrange@redhat.com>
CC: Marc-André Lureau <marcandre.lureau@redhat.com>
---
Changes from v2:
- fixed prototype of tcp_chr_disconnect
Changes from v1:
- we do not rely on EPIPE anynore. Socket should be closed on all errors
except EAGAIN
chardev/char-socket.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 4068dc5..865c527 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,
GIOCondition cond,
void *opaque);
+static int tcp_chr_read_poll(void *opaque);
+static void tcp_chr_disconnect(Chardev *chr);
+
/* Called with chr_write_lock held. */
static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
{
@@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
s->write_msgfds_num = 0;
}
+ if (ret < 0 && errno != EAGAIN) {
+ if (tcp_chr_read_poll(chr) <= 0) {
+ tcp_chr_disconnect(chr);
+ return len;
+ } /* else let the read handler finish it properly */
+ }
+
return ret;
} else {
/* XXX: indicate an error ? */
--
2.7.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH v3 1/1] qemu-char: socket backend: disconnect on write error
2017-02-02 14:26 [Qemu-devel] [PATCH v3 1/1] qemu-char: socket backend: disconnect on write error Denis V. Lunev
@ 2017-02-03 18:59 ` Paolo Bonzini
0 siblings, 0 replies; 2+ messages in thread
From: Paolo Bonzini @ 2017-02-03 18:59 UTC (permalink / raw)
To: Denis V. Lunev, qemu-devel
Cc: Anton Nefedov, Daniel P . Berrange, Marc-André Lureau
On 02/02/2017 06:26, Denis V. Lunev wrote:
> From: Anton Nefedov <anton.nefedov@virtuozzo.com>
>
> Socket backend read handler should normally perform a disconnect, however
> the read handler may not get a chance to run if the frontend is not ready
> (qemu_chr_be_can_write() == 0).
>
> This means that in virtio-serial frontend case if
> - the host has disconnected (giving EPIPE on socket write)
> - and the guest has disconnected (-> frontend not ready -> backend
> will not read)
> - and there is still data (frontend->backend) to flush (has to be a really
> tricky timing but nevertheless, we have observed the case in production)
>
> This results in virtio-serial trying to flush this data continiously forming
> a busy loop.
>
> Solution: react on write error in the socket write handler.
> errno is not reliable after qio_channel_writev_full(), so we may not get
> the exact EPIPE, so disconnect on any error but QIO_CHANNEL_ERR_BLOCK which
> io_channel_send_full() converts to errno EAGAIN.
> We must not disconnect right away though, there still may be data to read
> (see 4bf1cb0).
>
> Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> CC: Paolo Bonzini <pbonzini@redhat.com>
> CC: Daniel P. Berrange <berrange@redhat.com>
> CC: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> Changes from v2:
> - fixed prototype of tcp_chr_disconnect
>
> Changes from v1:
> - we do not rely on EPIPE anynore. Socket should be closed on all errors
> except EAGAIN
>
> chardev/char-socket.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
>
> diff --git a/chardev/char-socket.c b/chardev/char-socket.c
> index 4068dc5..865c527 100644
> --- a/chardev/char-socket.c
> +++ b/chardev/char-socket.c
> @@ -97,6 +97,9 @@ static gboolean tcp_chr_accept(QIOChannel *chan,
> GIOCondition cond,
> void *opaque);
>
> +static int tcp_chr_read_poll(void *opaque);
> +static void tcp_chr_disconnect(Chardev *chr);
> +
> /* Called with chr_write_lock held. */
> static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
> {
> @@ -114,6 +117,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
> s->write_msgfds_num = 0;
> }
>
> + if (ret < 0 && errno != EAGAIN) {
> + if (tcp_chr_read_poll(chr) <= 0) {
> + tcp_chr_disconnect(chr);
> + return len;
> + } /* else let the read handler finish it properly */
> + }
> +
> return ret;
> } else {
> /* XXX: indicate an error ? */
>
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-02-03 18:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-02-02 14:26 [Qemu-devel] [PATCH v3 1/1] qemu-char: socket backend: disconnect on write error Denis V. Lunev
2017-02-03 18:59 ` Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).