qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com
Subject: [Qemu-devel] [RFC PATCH 14/14] qemu-nbd: use common main loop
Date: Fri, 16 Sep 2011 17:09:10 +0200	[thread overview]
Message-ID: <1316185750-9187-15-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1316185750-9187-1-git-send-email-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 qemu-nbd.c |  127 +++++++++++++++++++++++++++++-------------------------------
 1 files changed, 61 insertions(+), 66 deletions(-)

diff --git a/qemu-nbd.c b/qemu-nbd.c
index 3a39145..a45dd4b 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -37,6 +37,13 @@
 #define NBD_BUFFER_SIZE (1024*1024)
 
 static int verbose;
+static int shared = 1;
+static int nb_fds;
+static BlockDriverState *bs;
+static off_t fd_size;
+static off_t dev_offset;
+static off_t offset;
+static bool readonly;
 
 static void usage(const char *name)
 {
@@ -180,18 +187,49 @@ static void show_parts(const char *device)
     }
 }
 
+static int nbd_can_accept(void *opaque)
+{
+    return nb_fds < shared;
+}
+
+static void nbd_read(void *opaque)
+{
+    static uint8_t *data;
+    int fd = (uintptr_t) opaque;
+
+    if (data == NULL) {
+        data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
+        if (data == NULL) {
+            errx(EXIT_FAILURE, "Cannot allocate data buffer");
+        }
+    }
+
+    if (nbd_trip(bs, fd, fd_size, dev_offset,
+        &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
+        qemu_set_fd_handler2(fd, NULL, NULL, NULL, NULL);
+        close(fd);
+        nb_fds--;
+    }
+}
+
+static void nbd_accept(void *opaque)
+{
+    int server_fd = (uintptr_t) opaque;
+    struct sockaddr_in addr;
+    socklen_t addr_len = sizeof(addr);
+
+    int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
+    if (fd != -1 && nbd_negotiate(fd, fd_size) != -1) {
+        qemu_set_fd_handler2(fd, NULL, nbd_read, NULL, (void *) (intptr_t) fd);
+        nb_fds++;
+    }
+}
+
 int main(int argc, char **argv)
 {
-    BlockDriverState *bs;
-    off_t dev_offset = 0;
-    off_t offset = 0;
-    bool readonly = false;
     bool disconnect = false;
     const char *bindto = "0.0.0.0";
     int port = NBD_DEFAULT_PORT;
-    struct sockaddr_in addr;
-    socklen_t addr_len = sizeof(addr);
-    off_t fd_size;
     char *device = NULL;
     char *socket = NULL;
     char sockpath[128];
@@ -221,14 +259,8 @@ int main(int argc, char **argv)
     int flags = BDRV_O_RDWR;
     int partition = -1;
     int ret;
-    int shared = 1;
-    uint8_t *data;
     fd_set fds;
-    int *sharing_fds;
     int fd;
-    int i;
-    int nb_fds = 0;
-    int max_fd;
     int persistent = 0;
     uint32_t nbdflags;
 
@@ -431,67 +463,30 @@ int main(int argc, char **argv)
         /* children */
     }
 
-    sharing_fds = g_malloc((shared + 1) * sizeof(int));
-
     if (socket) {
-        sharing_fds[0] = unix_socket_incoming(socket);
+        fd = unix_socket_incoming(socket);
     } else {
-        sharing_fds[0] = tcp_socket_incoming(bindto, port);
+        fd = tcp_socket_incoming(bindto, port);
     }
 
-    if (sharing_fds[0] == -1)
+    if (fd == -1) {
         return 1;
-    max_fd = sharing_fds[0];
-    nb_fds++;
-
-    data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
-    if (data == NULL)
-        errx(EXIT_FAILURE, "Cannot allocate data buffer");
-
-    do {
-
-        FD_ZERO(&fds);
-        for (i = 0; i < nb_fds; i++)
-            FD_SET(sharing_fds[i], &fds);
-
-        ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
-        if (ret == -1)
-            break;
+    }
 
-        if (FD_ISSET(sharing_fds[0], &fds))
-            ret--;
-        for (i = 1; i < nb_fds && ret; i++) {
-            if (FD_ISSET(sharing_fds[i], &fds)) {
-                if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
-                    &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
-                    close(sharing_fds[i]);
-                    nb_fds--;
-                    sharing_fds[i] = sharing_fds[nb_fds];
-                    i--;
-                }
-                ret--;
-            }
-        }
-        /* new connection ? */
-        if (FD_ISSET(sharing_fds[0], &fds)) {
-            if (nb_fds < shared + 1) {
-                sharing_fds[nb_fds] = accept(sharing_fds[0],
-                                             (struct sockaddr *)&addr,
-                                             &addr_len);
-                if (sharing_fds[nb_fds] != -1 &&
-                    nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
-                        if (sharing_fds[nb_fds] > max_fd)
-                            max_fd = sharing_fds[nb_fds];
-                        nb_fds++;
-                }
-            }
-        }
-    } while (persistent || nb_fds > 1);
-    qemu_vfree(data);
+    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
+                         (void *)(uintptr_t)fd);
+
+    /* Wait for the first incoming connection.  */
+    FD_ZERO(&fds);
+    FD_SET(fd, &fds);
+    ret = select(fd + 1, &fds, NULL, NULL, NULL);
+    if (ret != -1) {
+        do {
+            main_loop_wait(false);
+        } while (persistent || nb_fds > 0);
+    }
 
-    close(sharing_fds[0]);
     bdrv_close(bs);
-    g_free(sharing_fds);
     if (socket)
         unlink(socket);
 
-- 
1.7.6

      parent reply	other threads:[~2011-09-16 15:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-16 15:08 [Qemu-devel] [RFC PATCH 00/14] allow tools to use the QEMU main loop Paolo Bonzini
2011-09-16 15:08 ` [Qemu-devel] [RFC PATCH 01/14] remove unused function Paolo Bonzini
2011-09-16 15:08 ` [Qemu-devel] [RFC PATCH 02/14] qemu-timer: remove active_timers array Paolo Bonzini
2011-09-16 15:08 ` [Qemu-devel] [RFC PATCH 03/14] qemu-timer: move common code to qemu_rearm_alarm_timer Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 04/14] qemu-timer: more clock functions Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 05/14] qemu-timer: move icount to cpus.c Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 06/14] qemu-timer: do not refer to vm_running Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 07/14] qemu-timer: move more stuff out of qemu-timer.c Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 08/14] create main-loop.h Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 09/14] create main-loop.c Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 10/14] Revert to a hand-made select loop Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 11/14] simplify main loop functions Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 12/14] makefile: extract tools-obj-y Paolo Bonzini
2011-09-16 15:09 ` [Qemu-devel] [RFC PATCH 13/14] link the main loop and its dependencies into the tools Paolo Bonzini
2011-09-16 15:09 ` Paolo Bonzini [this message]

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=1316185750-9187-15-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=aliguori@us.ibm.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 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).