From: "Daniel P. Berrange" <berrange@redhat.com>
To: "Gonglei (Arei)" <arei.gonglei@huawei.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
"Lilijun (Jerry)" <jerry.lilijun@huawei.com>,
"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Subject: Re: [Qemu-devel] A question about 9894dc0cdcc397ee5b26370bc53da6d360a363c2
Date: Mon, 18 Jul 2016 10:07:22 +0100 [thread overview]
Message-ID: <20160718090722.GK1670@redhat.com> (raw)
In-Reply-To: <33183CC9F5247A488A2544077AF19020B0376528@SZXEMA503-MBS.china.huawei.com>
On Mon, Jul 18, 2016 at 08:41:32AM +0000, Gonglei (Arei) wrote:
> Hi Daniel & Paolo,
>
> Commit 9894dc0c "char: convert from GIOChannel to QIOChannel",
> about the below code segment:
>
> static bool qemu_chr_open_socket_fd(CharDriverState *chr, Error **errp)
> {
> TCPCharDriver *s = chr->opaque;
> - int fd;
> + QIOChannelSocket *sioc = qio_channel_socket_new();
>
> if (s->is_listen) {
> - fd = socket_listen(s->addr, errp);
> + if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) {
> + goto fail;
> + }
> + qemu_chr_finish_socket_connection(chr, sioc);
> } else if (s->reconnect_time) {
> - fd = socket_connect(s->addr, errp, qemu_chr_socket_connected, chr);
> - return fd >= 0;
> + qio_channel_socket_connect_async(sioc, s->addr,
> + qemu_chr_socket_connected,
> + chr, NULL);
> } else {
> - fd = socket_connect(s->addr, errp, NULL, NULL);
> - }
> - if (fd < 0) {
> - return false;
> + if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
> + goto fail;
> + }
> + qemu_chr_finish_socket_connection(chr, sioc);
> }
>
> - qemu_chr_finish_socket_connection(chr, fd);
> return true;
> +
> + fail:
> + object_unref(OBJECT(sioc));
> + return false;
> }
>
> Why did you change socket_connect() to qio_channel_socket_connect_async
> but not qio_channel_socket_connect_sync when s->reconnect_time is ture? Thanks.
> I can't get the reason from the commit message.
When the socket_connect() function is called with a non-null value for
the NonBlockingConnectHandler parameter, it'll put the socket into
non-blocking mode before doing the connection. Therefore socket_connect()
method is no longer guaranteed to actually connect to the socket - it
may simply start the connection and leave an event handler to finish
it. This avoids blocking the caller waiting for the connectionb to
finish which may take non-negligible time with TCP sockets.
The qio_channel_socket_connect_sync() method will always block until
completion of the connect, so we use qio_channel_socket_connect_async
which always does the connection attempt in the background.
The old code which may or may not finish the connect() in the background
was a recipe for bugs because it forced callers to have 2 completely
separate codepaths for the same API call and chances are only one of
those code paths would be tested by the developers. With the code code
we're guaranteed to always complete in the background, so the callers
only have 1 codepath to worry about and so they'll be forced to ensure
that codepath works.
> PS: We encountered a problem that when config vhost-user-net reconneticon function,
> the virtio_net_get_features() didn't get a correct value after this changed, and it was
> fixed when we change the async to sync.
It seems that you were mostly lucky with the old code in that presumably
socket_connect() would complete the connection fully. There was never any
guarantee of this though - it was perfectly feasible that socket_connect()
would finish the connect in the background. So IMHO the vhost user code
was already buggy if it doesn't cope with the connect finishing in the
background.
Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
next prev parent reply other threads:[~2016-07-18 9:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-18 8:41 [Qemu-devel] A question about 9894dc0cdcc397ee5b26370bc53da6d360a363c2 Gonglei (Arei)
2016-07-18 9:07 ` Daniel P. Berrange [this message]
2016-07-19 8:51 ` Gonglei (Arei)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160718090722.GK1670@redhat.com \
--to=berrange@redhat.com \
--cc=arei.gonglei@huawei.com \
--cc=jerry.lilijun@huawei.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.