All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 05/25] nbd: allow multiple in-flight requests
Date: Tue,  6 Dec 2011 16:27:32 +0100	[thread overview]
Message-ID: <1323185272-2610-6-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1323185272-2610-1-git-send-email-pbonzini@redhat.com>

Allow sending up to 16 requests, and drive the replies to the coroutine
that did the request.  The code is written to be exactly the same as
before this patch when MAX_NBD_REQUESTS == 1 (modulo the extra mutex
and state).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 block/nbd.c |   69 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 56 insertions(+), 13 deletions(-)

diff --git a/block/nbd.c b/block/nbd.c
index 7e6bf87..93f5d16 100644
--- a/block/nbd.c
+++ b/block/nbd.c
@@ -46,6 +46,10 @@
 #define logout(fmt, ...) ((void)0)
 #endif
 
+#define MAX_NBD_REQUESTS	16
+#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
+#define INDEX_TO_HANDLE(bs, index)  ((index)  ^ ((uint64_t)(intptr_t)bs))
+
 typedef struct BDRVNBDState {
     int sock;
     uint32_t nbdflags;
@@ -53,9 +57,12 @@ typedef struct BDRVNBDState {
     size_t blocksize;
     char *export_name; /* An NBD server may export several devices */
 
-    CoMutex mutex;
-    Coroutine *coroutine;
+    CoMutex send_mutex;
+    CoMutex free_sema;
+    Coroutine *send_coroutine;
+    int in_flight;
 
+    Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
     struct nbd_reply reply;
 
     /* If it begins with  '/', this is a UNIX domain socket. Otherwise,
@@ -112,41 +119,68 @@ out:
 
 static void nbd_coroutine_start(BDRVNBDState *s, struct nbd_request *request)
 {
-    qemu_co_mutex_lock(&s->mutex);
-    s->coroutine = qemu_coroutine_self();
-    request->handle = (uint64_t)(intptr_t)s;
+    int i;
+
+    /* Poor man semaphore.  The free_sema is locked when no other request
+     * can be accepted, and unlocked after receiving one reply.  */
+    if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
+        qemu_co_mutex_lock(&s->free_sema);
+        assert(s->in_flight < MAX_NBD_REQUESTS);
+    }
+    s->in_flight++;
+
+    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
+        if (s->recv_coroutine[i] == NULL) {
+            s->recv_coroutine[i] = qemu_coroutine_self();
+            break;
+        }
+    }
+
+    assert(i < MAX_NBD_REQUESTS);
+    request->handle = INDEX_TO_HANDLE(s, i);
 }
 
 static int nbd_have_request(void *opaque)
 {
     BDRVNBDState *s = opaque;
 
-    return !!s->coroutine;
+    return s->in_flight > 0;
 }
 
 static void nbd_reply_ready(void *opaque)
 {
     BDRVNBDState *s = opaque;
+    int i;
 
     if (s->reply.handle == 0) {
         /* No reply already in flight.  Fetch a header.  */
         if (nbd_receive_reply(s->sock, &s->reply) < 0) {
             s->reply.handle = 0;
+            goto fail;
         }
     }
 
     /* There's no need for a mutex on the receive side, because the
      * handler acts as a synchronization point and ensures that only
      * one coroutine is called until the reply finishes.  */
-    if (s->coroutine) {
-        qemu_coroutine_enter(s->coroutine, NULL);
+    i = HANDLE_TO_INDEX(s, s->reply.handle);
+    if (s->recv_coroutine[i]) {
+        qemu_coroutine_enter(s->recv_coroutine[i], NULL);
+        return;
+    }
+
+fail:
+    for (i = 0; i < MAX_NBD_REQUESTS; i++) {
+        if (s->recv_coroutine[i]) {
+            qemu_coroutine_enter(s->recv_coroutine[i], NULL);
+        }
     }
 }
 
 static void nbd_restart_write(void *opaque)
 {
     BDRVNBDState *s = opaque;
-    qemu_coroutine_enter(s->coroutine, NULL);
+    qemu_coroutine_enter(s->send_coroutine, NULL);
 }
 
 static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
@@ -154,6 +188,8 @@ static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
 {
     int rc, ret;
 
+    qemu_co_mutex_lock(&s->send_mutex);
+    s->send_coroutine = qemu_coroutine_self();
     qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write,
                             nbd_have_request, NULL, s);
     rc = nbd_send_request(s->sock, request);
@@ -166,6 +202,8 @@ static int nbd_co_send_request(BDRVNBDState *s, struct nbd_request *request,
     }
     qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL,
                             nbd_have_request, NULL, s);
+    s->send_coroutine = NULL;
+    qemu_co_mutex_unlock(&s->send_mutex);
     return rc;
 }
 
@@ -175,7 +213,8 @@ static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
 {
     int ret;
 
-    /* Wait until we're woken up by the read handler.  */
+    /* Wait until we're woken up by the read handler.  TODO: perhaps
+     * peek at the next reply and avoid yielding if it's ours?  */
     qemu_coroutine_yield();
     *reply = s->reply;
     if (reply->handle != request->handle) {
@@ -195,8 +234,11 @@ static void nbd_co_receive_reply(BDRVNBDState *s, struct nbd_request *request,
 
 static void nbd_coroutine_end(BDRVNBDState *s, struct nbd_request *request)
 {
-    s->coroutine = NULL;
-    qemu_co_mutex_unlock(&s->mutex);
+    int i = HANDLE_TO_INDEX(s, request->handle);
+    s->recv_coroutine[i] = NULL;
+    if (s->in_flight-- == MAX_NBD_REQUESTS) {
+        qemu_co_mutex_unlock(&s->free_sema);
+    }
 }
 
 static int nbd_establish_connection(BlockDriverState *bs)
@@ -261,7 +303,8 @@ static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
     BDRVNBDState *s = bs->opaque;
     int result;
 
-    qemu_co_mutex_init(&s->mutex);
+    qemu_co_mutex_init(&s->send_mutex);
+    qemu_co_mutex_init(&s->free_sema);
 
     /* Pop the config into our state object. Exit if invalid. */
     result = nbd_config(s, filename, flags);
-- 
1.7.7.1

  parent reply	other threads:[~2011-12-06 15:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-06 15:27 [Qemu-devel] [PATCH 00/25] nbd asynchronous operation Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 01/25] add qemu_send_full and qemu_recv_full Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 02/25] sheepdog: move coroutine send/recv function to generic code Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 03/25] nbd: switch to asynchronous operation Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 04/25] nbd: split requests Paolo Bonzini
2011-12-06 15:27 ` Paolo Bonzini [this message]
2011-12-06 15:27 ` [Qemu-devel] [PATCH 06/25] nbd: fix error handling in the server Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 07/25] nbd: add support for NBD_CMD_FLAG_FUA Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 08/25] nbd: add support for NBD_CMD_FLUSH Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 09/25] nbd: add support for NBD_CMD_TRIM Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 10/25] Update ioctl order in nbd_init() to detect EBUSY Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 11/25] qemu-nbd: remove offset argument to nbd_trip Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 12/25] qemu-nbd: remove data_size " Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 13/25] move corking functions to osdep.c Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 14/25] qemu-nbd: simplify nbd_trip Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 15/25] qemu-nbd: introduce nbd_do_send_reply Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 16/25] qemu-nbd: more robust handling of invalid requests Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 17/25] qemu-nbd: introduce nbd_do_receive_request Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 18/25] qemu-nbd: introduce NBDExport Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 19/25] qemu-nbd: introduce NBDRequest Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 20/25] link the main loop and its dependencies into the tools Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 21/25] qemu-nbd: use common main loop Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 22/25] qemu-nbd: move client handling to nbd.c Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 23/25] qemu-nbd: add client pointer to NBDRequest Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 24/25] qemu-nbd: asynchronous operation Paolo Bonzini
2011-12-06 15:27 ` [Qemu-devel] [PATCH 25/25] qemu-nbd: throttle requests Paolo Bonzini
2011-12-15 10:21 ` [Qemu-devel] [PATCH 00/25] nbd asynchronous operation Paolo Bonzini
2011-12-15 11:09   ` Kevin Wolf
2011-12-21 18:11     ` Paolo Bonzini
2011-12-21 19:37       ` Anthony Liguori

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=1323185272-2610-6-git-send-email-pbonzini@redhat.com \
    --to=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.