qemu_char: socket backend: disconnect on EPIPE
PATCH 1/1 qemu_char: socket backend: disconnect on EPIPE
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
qemu-char.c +11 -1
@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;return -1 ; }
@@ -2854,6 +2854,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) {
@@ -2871,6 +2874,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; } +if (ret <0 && errno ==EPIPE ) { +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 ? */
Signature · 1 line
2.7.4
Quoted 32 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)qemu-char.c +0
-0
qemu-char.c static int io_channel_send_full(QIOChannel *ioc, · line 1205
Quoted 5 lines
qemu-char.c +1
-1@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;
You can't rely on 'errno' being valid after qio_channel_writev_full().
The 'Error **errp' arg to that function is the only error reporting mechanism that's supported. In particular since the TCP channel can be wrapped in TLS, the errno can easily have been clobbered by time you get here.
Quoted 11 lines
qemu-char.c +3
-0@@ -2854,6 +2854,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) {
qemu-char.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) · line 2877
Quoted 4 lines
qemu-char.c +1
-0@@ -2871,6 +2874,13 @@ static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) s->write_msgfds_num =0 ; } +if (ret <0 && errno ==EPIPE ) {
IMHO you should just remove "&& errno == EPIPE" and it'd be fine.
qemu-char.c static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len) · line 2886
Quoted 9 lines
qemu-char.c +6
-0+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 ? */
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 3 lines · Daniel P. Berrange
Quoted 2 lines
+ if (ret < 0 && errno == EPIPE) {
IMHO you should just remove "&& errno == EPIPE" and it'd be fine.
It should be errno != EAGAIN.
Paolo
Quoted 5 lines
Quoted 5 lines
qemu-char.c +5
-0+if (tcp_chr_read_poll (chr) <=0 ) { +tcp_chr_disconnect (chr); +return len; + }/* else let the read handler finish it properly */ + }
Quoted 40 lines · Daniel P. Berrange
Quoted 32 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)qemu-char.c +0
-0qemu-char.c static int io_channel_send_full(QIOChannel *ioc, · line 1205
Quoted 5 lines
qemu-char.c +1
-1@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;You can't rely on 'errno' being valid after
qio_channel_writev_full().
Quoted 4 lines
The 'Error **errp' arg to that function is the only error reporting mechanism that's supported. In particular since the TCP channel can be wrapped in TLS, the errno can easily have been clobbered by time you get here.
can we return errno directly from this call and pass exit code to the upper layer? This will be fair and much more straightforward.
In this case we will have to check ret directly below.
Den
Quoted 41 lines · Denis V. Lunev
Quoted 40 lines · Daniel P. Berrange
Quoted 32 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)qemu-char.c +0
-0qemu-char.c static int io_channel_send_full(QIOChannel *ioc, · line 1205
Quoted 5 lines
qemu-char.c +1
-1@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;You can't rely on 'errno' being valid after
qio_channel_writev_full().
Quoted 6 lines
Quoted 4 lines
The 'Error **errp' arg to that function is the only error reporting mechanism that's supported. In particular since the TCP channel can be wrapped in TLS, the errno can easily have been clobbered by time you get here.
can we return errno directly from this call and pass exit code to the upper layer? This will be fair and much more straightforward.
No, the qio APIs explicitly do *not* use errno because their implementations may be calling APIs which in turn do not provide errnos. errno is only a useful concept if your always calling into system calls. As soon as you need to deal with higher level libraries, errno is woefully inadequate as a concept, hence using Error ** instead.
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 8 lines · Paolo Bonzini
Quoted 3 lines · Daniel P. Berrange
Quoted 2 lines
+ if (ret < 0 && errno == EPIPE) {
IMHO you should just remove "&& errno == EPIPE" and it'd be fine.
It should be errno != EAGAIN.
Oh, yes, thats right.
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|
Quoted 48 lines · Daniel P. Berrange
Quoted 41 lines · Denis V. Lunev
Quoted 40 lines · Daniel P. Berrange
Quoted 32 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)qemu-char.c +0
-0qemu-char.c static int io_channel_send_full(QIOChannel *ioc, · line 1205
Quoted 5 lines
qemu-char.c +1
-1@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;You can't rely on 'errno' being valid after
qio_channel_writev_full().Quoted 6 lines
Quoted 4 lines
The 'Error **errp' arg to that function is the only error reporting mechanism that's supported. In particular since the TCP channel can be wrapped in TLS, the errno can easily have been clobbered by time you get here.
can we return errno directly from this call and pass exit code to the upper layer? This will be fair and much more straightforward.
Quoted 8 lines
No, the qio APIs explicitly do *not* use errno because their implementations may be calling APIs which in turn do not provide errnos. errno is only a useful concept if your always calling into system calls. As soon as you need to deal with higher level libraries, errno is woefully inadequate as a concept, hence using Error ** instead.
Regards,
Daniel
But the problem is that Error does not have error code field inside. Should we play with ErrorClass enum?
Den
Quoted 55 lines · Denis V. Lunev
Quoted 54 lines · Daniel P. Berrange
Quoted 41 lines · Denis V. Lunev
Quoted 40 lines · Daniel P. Berrange
Quoted 32 lines · Denis V. Lunev
From: Anton Nefedov <[email protected]>
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 EPIPE in the socket write handler. We must not disconnect right away though, there still may be data to read (see 4bf1cb0).
Signed-off-by Anton Nefedov <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Daniel P. Berrange <[email protected]>--- qemu-char.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)qemu-char.c +0
-0qemu-char.c static int io_channel_send_full(QIOChannel *ioc, · line 1205
Quoted 5 lines
qemu-char.c +1
-1@@ -1202,7 +1202,7 @@ static int io_channel_send_full(QIOChannel *ioc, errno =EAGAIN ;return -1 ; }else if (ret <0 ) { - errno =EINVAL ; + errno = errno ==EPIPE ?EPIPE :EINVAL ;You can't rely on 'errno' being valid after
qio_channel_writev_full().Quoted 6 lines
Quoted 4 lines
The 'Error **errp' arg to that function is the only error reporting mechanism that's supported. In particular since the TCP channel can be wrapped in TLS, the errno can easily have been clobbered by time you get here.
can we return errno directly from this call and pass exit code to the upper layer? This will be fair and much more straightforward.
No, the qio APIs explicitly do *not* use errno because their implementations may be calling APIs which in turn do not provide errnos. errno is only a useful concept if your always calling into system calls. As soon as you need to deal with higher level libraries, errno is woefully inadequate as a concept, hence using Error ** instead.
Quoted 4 lines
Quoted 2 lines
Regards,
DanielBut the problem is that Error does not have error code field inside. Should we play with ErrorClass enum?
No, don't try to distinguish different types of errors. As mentioned elsewhere just use the same logic for all errors, except when seeing QIO_ERR_WOULD_BLOCK
Regards,
Daniel
Signature · 3 lines
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://entangle-photo.org -o- http://search.cpan.org/~danberr/ :|