qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 11/15] qemu-nbd: use common main loop


From: Paolo Bonzini
Subject: [Qemu-devel] [PATCH 11/15] qemu-nbd: use common main loop
Date: Mon, 10 Oct 2011 11:37:53 +0200

Using a single main loop for sockets will help yielding from the socket
coroutine back to the main loop, and later reentering it.

Signed-off-by: Paolo Bonzini <address@hidden>
---
 qemu-nbd.c |  101 ++++++++++++++++++++++++++++--------------------------------
 1 files changed, 47 insertions(+), 54 deletions(-)

diff --git a/qemu-nbd.c b/qemu-nbd.c
index 9fa3979..7896e9b 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -36,6 +36,8 @@
 
 static NBDExport *exp;
 static int verbose;
+static int shared = 1;
+static int nb_fds;
 
 static void usage(const char *name)
 {
@@ -179,6 +181,35 @@ static void show_parts(const char *device)
     }
 }
 
+static int nbd_can_accept(void *opaque)
+{
+    return nb_fds < shared;
+}
+
+static void nbd_read(void *opaque)
+{
+    int fd = (uintptr_t) opaque;
+
+    if (nbd_trip(&exp, fd) != 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, exp.size, exp.nbdflags) != -1) {
+        qemu_set_fd_handler2(fd, NULL, nbd_read, NULL, (void *) (intptr_t) fd);
+        nb_fds++;
+    }
+}
+
 int main(int argc, char **argv)
 {
     BlockDriverState *bs;
@@ -187,8 +218,6 @@ int main(int argc, char **argv)
     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 = -1;
     char *device = NULL;
     char *socket = NULL;
@@ -219,13 +248,8 @@ int main(int argc, char **argv)
     int flags = BDRV_O_RDWR;
     int partition = -1;
     int ret;
-    int shared = 1;
     fd_set fds;
-    int *sharing_fds;
     int fd;
-    int i;
-    int nb_fds = 0;
-    int max_fd;
     int persistent = 0;
 
     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
@@ -427,61 +451,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++;
-
-    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(exp, sharing_fds[i]) != 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, nbdflags) != 
-1) {
-                        if (sharing_fds[nb_fds] > max_fd)
-                            max_fd = sharing_fds[nb_fds];
-                        nb_fds++;
-                }
-            }
-        }
-    } while (persistent || nb_fds > 1);
+    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]);
     nbd_export_close(exp);
-    g_free(sharing_fds);
     if (socket)
         unlink(socket);
 
-- 
1.7.6





reply via email to

[Prev in Thread] Current Thread [Next in Thread]