From: "Daniel P. Berrange" <berrange@redhat.com>
To: Fam Zheng <famz@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
qemu-devel@nongnu.org, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 1/2] nbd: Interface tweak of nbd_client_new
Date: Fri, 8 Jan 2016 16:24:28 +0000 [thread overview]
Message-ID: <20160108162428.GC24031@redhat.com> (raw)
In-Reply-To: <1451454566-15005-2-git-send-email-famz@redhat.com>
On Wed, Dec 30, 2015 at 01:49:25PM +0800, Fam Zheng wrote:
> In preparation for an async implementation, introduce a callback and
> move the shutdown/close to the function.
>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
> blockdev-nbd.c | 5 ++---
> include/block/nbd.h | 6 ++++--
> nbd.c | 20 +++++++++++++++-----
> qemu-nbd.c | 16 +++++++++-------
> 4 files changed, 30 insertions(+), 17 deletions(-)
>
> diff --git a/blockdev-nbd.c b/blockdev-nbd.c
> index bcdd18b..f708e0f 100644
> --- a/blockdev-nbd.c
> +++ b/blockdev-nbd.c
> @@ -27,9 +27,8 @@ static void nbd_accept(void *opaque)
> socklen_t addr_len = sizeof(addr);
>
> int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
> - if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) {
> - shutdown(fd, 2);
> - close(fd);
> + if (fd >= 0) {
> + nbd_client_new(NULL, fd, nbd_client_put, NULL);
> }
> }
>
> diff --git a/include/block/nbd.h b/include/block/nbd.h
> index 65f409d..11194e0 100644
> --- a/include/block/nbd.h
> +++ b/include/block/nbd.h
> @@ -98,8 +98,10 @@ NBDExport *nbd_export_find(const char *name);
> void nbd_export_set_name(NBDExport *exp, const char *name);
> void nbd_export_close_all(void);
>
> -NBDClient *nbd_client_new(NBDExport *exp, int csock,
> - void (*close)(NBDClient *));
> +typedef void (*NBDClientNewCB)(NBDExport *exp, int csock, int ret);
> +void nbd_client_new(NBDExport *exp, int csock,
> + void (*close_fn)(NBDClient *),
> + NBDClientNewCB cb);
> void nbd_client_get(NBDClient *client);
> void nbd_client_put(NBDClient *client);
>
> diff --git a/nbd.c b/nbd.c
> index b3d9654..bcb79d4 100644
> --- a/nbd.c
> +++ b/nbd.c
> @@ -1475,9 +1475,13 @@ static void nbd_update_can_read(NBDClient *client)
> }
> }
>
> -NBDClient *nbd_client_new(NBDExport *exp, int csock,
> - void (*close)(NBDClient *))
> +/* Create and initialize a new client. If it fails, @csock is closed.
> + * cb will be called with the status code when done. */
> +void nbd_client_new(NBDExport *exp, int csock,
> + void (*close_fn)(NBDClient *),
> + NBDClientNewCB cb)
> {
> + int ret = 0;
> NBDClient *client;
> client = g_malloc0(sizeof(NBDClient));
> client->refcount = 1;
> @@ -1485,10 +1489,13 @@ NBDClient *nbd_client_new(NBDExport *exp, int csock,
> client->sock = csock;
> client->can_read = true;
> if (nbd_send_negotiate(client)) {
> + shutdown(csock, 2);
> + close(csock);
> g_free(client);
> - return NULL;
> + ret = -1;
> + goto out;
If you simply make this failure code branch call close_fn() then I
think you can adding needing the new NBDClientNewCB entirely if....
> diff --git a/qemu-nbd.c b/qemu-nbd.c
> index 65dc30c..bdec228 100644
> --- a/qemu-nbd.c
> +++ b/qemu-nbd.c
> @@ -319,6 +319,14 @@ static void nbd_client_closed(NBDClient *client)
> nbd_client_put(client);
> }
>
> +static void nbd_client_new_cb(NBDExport *exp, int fd, int ret)
> +{
> + if (ret == 0) {
> + nb_fds++;
> + nbd_update_server_fd_handler(server_fd);
> + }
> +}
> +
> static void nbd_accept(void *opaque)
> {
> struct sockaddr_in addr;
> @@ -335,13 +343,7 @@ static void nbd_accept(void *opaque)
> return;
> }
>
> - if (nbd_client_new(exp, fd, nbd_client_closed)) {
> - nb_fds++;
> - nbd_update_server_fd_handler(server_fd);
> - } else {
> - shutdown(fd, 2);
> - close(fd);
> - }
> + nbd_client_new(exp, fd, nbd_client_closed, nbd_client_new_cb);
...you make this do
nb_fds++;
nbd_update_server_fd_handler(server_fd);
nbd_client_new(exp, fd, nbd_client_closed, nbd_client_new_cb);
ie, you once guarantee that *every* invocation of nbd_client_new()
will eventually lead to a call to 'nbd_client_closed', you can
unconditionally increment nb_fds before calling nbd_client_new.
This has the added benefit in that the 'nb_fds' count now takes
account of client connections that are in the negotiate phase,
whereas your approach allows for an unlimited number of clients
to be in the negotiate phase, only limiting them post-negotiate
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-01-08 16:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-30 5:49 [Qemu-devel] [PATCH 0/2] nbd: Async built-in server negotiation Fam Zheng
2015-12-30 5:49 ` [Qemu-devel] [PATCH 1/2] nbd: Interface tweak of nbd_client_new Fam Zheng
2016-01-08 16:24 ` Daniel P. Berrange [this message]
2016-01-08 18:24 ` Paolo Bonzini
2016-01-11 3:20 ` Fam Zheng
2015-12-30 5:49 ` [Qemu-devel] [PATCH 2/2] nbd: Coroutine based nbd_send_negotiate Fam Zheng
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=20160108162428.GC24031@redhat.com \
--to=berrange@redhat.com \
--cc=famz@redhat.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--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 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).