Skip to contentSkip to conversation

slirp: iohandler: Rebase onto aio

PATCH In review Fam Zheng 2 participants 17 messages Jun 10, 2015 – Jun 19, 2015

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover slirp: iohandler: Rebase onto aio

Currently iohandler.c and asyn.c has a lot in common, except that iohandler is hooked into main_loop_wait(), with qemu_iohandler_fill(). Likewise for slirp.

It is much cleaner to drive slirp fds with iohandler, and implement iohandler with an AioContext. This series does these two things. It applies to Stefan's net-next tree which has the prerequisite that removes the obstacle "qemu_set_fd_handler2" for this convergence:

http://comments.gmane.org/gmane.comp.emulators.qemu/341526

Upon that this series makes a more consistent main loop structure, and will also benefits future work on event loop, such as introducing of epoll and possibly in the long run moving things to separate iothread (whose event loop is based on AioContext too).

Fam Zheng (9):
aio: Introduce aio_set_fd_handler_pri
iohandler: Use aio code
main-loop: Move include of "qemu/sockets.h" to libslirp.h
slirp: Remove dead code for "PROBE_CONN"
slirp: Add "poll_events" to struct socket
slirp: Move icmp socket to iohandler
slirp: Move udb socket to iohandler
slirp: Move tcb socket to iohandler
slirp: Remove unused pollfds from the parameter list

 aio-posix.c              |  19 ++
 aio-win32.c              |  10 +
 include/block/aio.h      |   8 +
 include/qemu/main-loop.h |  10 +-
 iohandler.c              | 124 +++--------
 main-loop.c              |  10 +-
 slirp/libslirp.h         |   5 +-
 slirp/slirp.c            | 528 +++++++++++++++++++----------------------------
 slirp/slirp_config.h     |   4 -
 slirp/socket.h           |   1 +
 stubs/slirp.c            |   4 +-
 11 files changed, 300 insertions(+), 423 deletions(-)
Signature · 1 line
2.4.2
author
#1 lore raw

PATCH 1/9 aio: Introduce aio_set_fd_handler_pri

G_IO_PRI event is watched by slirp, add support of that to make future refactoring possible.

---
 aio-posix.c         | 19 +++++++++++++++++++
 aio-win32.c         | 10 ++++++++++
 include/block/aio.h |  8 ++++++++
 3 files changed, 37 insertions(+)
aio-posix.c +19 -0
index 4abec38..a633c6e 100644
@@ -23,6 +23,7 @@ struct AioHandler
     GPollFD pfd;
     IOHandler *io_read;
     IOHandler *io_write;
+    IOHandler *io_read_pri;
     int deleted;
     void *opaque;
     QLIST_ENTRY(AioHandler) node;
@@ -47,6 +48,16 @@ void aio_set_fd_handler(AioContext *ctx,
                         IOHandler *io_write,
                         void *opaque)
 {
+    return aio_set_fd_handler_pri(ctx, fd, io_read, io_write, NULL, opaque);
+}
+
+void aio_set_fd_handler_pri(AioContext *ctx,
+                            int fd,
+                            IOHandler *io_read,
+                            IOHandler *io_write,
+                            IOHandler *io_read_pri,
+                            void *opaque)
+{
     AioHandler *node;
 
     node = find_aio_handler(ctx, fd);
@@ -81,10 +92,12 @@ void aio_set_fd_handler(AioContext *ctx,
         /* Update handler with latest information */
         node->io_read = io_read;
         node->io_write = io_write;
+        node->io_read_pri = io_read_pri;
         node->opaque = opaque;
 
         node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
         node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
+        node->pfd.events |= (io_read_pri ? G_IO_PRI | G_IO_HUP | G_IO_ERR : 0);
     }
 
     aio_notify(ctx);
@@ -151,6 +164,12 @@ bool aio_dispatch(AioContext *ctx)
         node->pfd.revents = 0;
 
         if (!node->deleted &&
+            (revents & (G_IO_PRI | G_IO_HUP | G_IO_ERR)) &&
+            node->io_read_pri) {
+            node->io_read_pri(node->opaque);
+            progress = true;
+        }
+        if (!node->deleted &&
             (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
             node->io_read) {
             node->io_read(node->opaque);
aio-win32.c +10 -0
index 233d8f5..016cc19 100644
@@ -37,6 +37,16 @@ void aio_set_fd_handler(AioContext *ctx,
                         IOHandler *io_write,
                         void *opaque)
 {
+    return aio_set_fd_handler_pri(ctx, fd, io_read, io_write, NULL, opaque);
+}
+
+void aio_set_fd_handler_pri(AioContext *ctx,
+                            int fd,
+                            IOHandler *io_read,
+                            IOHandler *io_write,
+                            IOHandler *io_read_pri,
+                            void *opaque)
+{
     /* fd is a SOCKET in our case */
     AioHandler *node;
 
include/block/aio.h +8 -0
index b46103e..82502e1 100644
@@ -253,6 +253,14 @@ void aio_set_fd_handler(AioContext *ctx,
                         IOHandler *io_write,
                         void *opaque);
 
+/* Like aio_set_fd_handler with an additional G_IO_PRI event handler. */
+void aio_set_fd_handler_pri(AioContext *ctx,
+                            int fd,
+                            IOHandler *io_read,
+                            IOHandler *io_write,
+                            IOHandler *io_read_pri,
+                            void *opaque);
+
 /* Register an event notifier and associated callbacks.  Behaves very similarly
  * to event_notifier_set_handler.  Unlike event_notifier_set_handler, these callbacks
  * will be invoked when using aio_poll().
Signature · 1 line
2.4.2
author
#2 lore raw

PATCH 2/9 iohandler: Use aio code

In reply to Fam Zheng

Create a global AioContext instance and let its GSource to handle the events.

---
 include/qemu/main-loop.h |  10 +++-
 iohandler.c              | 124 ++++++++++-------------------------------------
 main-loop.c              |   5 +-
 3 files changed, 37 insertions(+), 102 deletions(-)
include/qemu/main-loop.h +8 -2
index 0f4a0fd..05f8b34 100644
@@ -203,6 +203,14 @@ void qemu_set_fd_handler(int fd,
                          IOHandler *fd_write,
                          void *opaque);
 
+/* Like qemu_set_fd_handler, while also watch for G_IO_PRI event. */
+void qemu_set_fd_handler_pri(int fd,
+                             IOHandler *fd_read,
+                             IOHandler *fd_write,
+                             IOHandler *fd_read_pri,
+                             void *opaque);
+
+GSource *iohandler_get_g_source(void);
 #ifdef CONFIG_POSIX
 /**
  * qemu_add_child_watch: Register a child process for reaping.
@@ -255,8 +263,6 @@ void qemu_mutex_unlock_iothread(void);
 /* internal interfaces */
 
 void qemu_fd_register(int fd);
-void qemu_iohandler_fill(GArray *pollfds);
-void qemu_iohandler_poll(GArray *pollfds, int rc);
 
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
 void qemu_bh_schedule_idle(QEMUBH *bh);
iohandler.c +26 -98

137 lines not on this page · raw message

main-loop.c +3 -2
index 82875a4..631caa0 100644
@@ -152,6 +152,9 @@ int qemu_init_main_loop(Error **errp)
     src = aio_get_g_source(qemu_aio_context);
     g_source_attach(src, NULL);
     g_source_unref(src);
+    src = iohandler_get_g_source();
+    g_source_attach(src, NULL);
+    g_source_unref(src);
     return 0;
 }
 
@@ -478,7 +481,6 @@ int main_loop_wait(int nonblocking)
 #ifdef CONFIG_SLIRP
     slirp_pollfds_fill(gpollfds, &timeout);
 #endif
-    qemu_iohandler_fill(gpollfds);
 
     if (timeout == UINT32_MAX) {
         timeout_ns = -1;
@@ -491,7 +493,6 @@ int main_loop_wait(int nonblocking)
                                           &main_loop_tlg));
 
     ret = os_host_main_loop_wait(timeout_ns);
-    qemu_iohandler_poll(gpollfds, ret);
 #ifdef CONFIG_SLIRP
     slirp_pollfds_poll(gpollfds, (ret < 0));
 #endif
Signature · 1 line
2.4.2
#3 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 2/9] iohandler: Use aio code

Quoted 8 lines · Fam Zheng
iohandler.c +0 -0
index 826f713..f5d7704 100644
@@ -32,111 +32,39 @@
 #include <sys/wait.h>
 #endif
 

iohandler.c line 35

Quoted 10 lines
iohandler.c +1 -9
-typedef struct IOHandlerRecord {
-    IOHandler *fd_read;
-    IOHandler *fd_write;
-    void *opaque;
-    QLIST_ENTRY(IOHandlerRecord) next;
-    int fd;
-    int pollfds_idx;
-    bool deleted;
-} IOHandlerRecord;
+static AioContext *iohandler_ctx;

Please include a comment explaining why this AioContext is needed in addition to the global qemu_aio_context.

author
#4 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 2/9] iohandler: Use aio code

Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines · Fam Zheng
iohandler.c +0 -0
index 826f713..f5d7704 100644
@@ -32,111 +32,39 @@
 #include <sys/wait.h>
 #endif
 
Quoted 13 lines

iohandler.c line 35

Quoted 10 lines
iohandler.c +1 -9
-typedef struct IOHandlerRecord {
-    IOHandler *fd_read;
-    IOHandler *fd_write;
-    void *opaque;
-    QLIST_ENTRY(IOHandlerRecord) next;
-    int fd;
-    int pollfds_idx;
-    bool deleted;
-} IOHandlerRecord;
+static AioContext *iohandler_ctx;

Please include a comment explaining why this AioContext is needed in addition to the global qemu_aio_context.

OK!

It's needed because iohandlers shouldn't be polled by aio_poll().

Fam

author
#5 lore raw

PATCH 3/9 main-loop: Move include of "qemu/sockets.h" to libslirp.h

In reply to Fam Zheng
---
 main-loop.c      | 1 -
 slirp/libslirp.h | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
main-loop.c +0 -1
index 631caa0..18b7508 100644
@@ -24,7 +24,6 @@
 
 #include "qemu-common.h"
 #include "qemu/timer.h"
-#include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
slirp/libslirp.h +1 -0
index 5bdcbd5..7f329d9 100644
@@ -2,6 +2,7 @@
 #define _LIBSLIRP_H
 
 #include "qemu-common.h"
+#include "qemu/sockets.h"
 
 struct Slirp;
 typedef struct Slirp Slirp;
Signature · 1 line
2.4.2
author
#7 lore raw

PATCH 4/9 slirp: Remove dead code for "PROBE_CONN"

In reply to Fam Zheng

It has always been compiled out. Remove it before we refactor the rest part of slirp poll.

---
 slirp/slirp.c        | 40 ----------------------------------------
 slirp/slirp_config.h |  4 ----
 2 files changed, 44 deletions(-)
slirp/slirp.c +0 -40
index 35f819a..25cdca6 100644
@@ -578,46 +578,6 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                      * a window probe to get things going again
                      */
                 }
-
-                /*
-                 * Probe a still-connecting, non-blocking socket
-                 * to check if it's still alive
-                 */
-#ifdef PROBE_CONN
-                if (so->so_state & SS_ISFCONNECTING) {
-                    ret = qemu_recv(so->s, &ret, 0, 0);
-
-                    if (ret < 0) {
-                        /* XXX */
-                        if (errno == EAGAIN || errno == EWOULDBLOCK ||
-                            errno == EINPROGRESS || errno == ENOTCONN) {
-                            continue; /* Still connecting, continue */
-                        }
-
-                        /* else failed */
-                        so->so_state &= SS_PERSISTENT_MASK;
-                        so->so_state |= SS_NOFDREF;
-
-                        /* tcp_input will take care of it */
-                    } else {
-                        ret = send(so->s, &ret, 0, 0);
-                        if (ret < 0) {
-                            /* XXX */
-                            if (errno == EAGAIN || errno == EWOULDBLOCK ||
-                                errno == EINPROGRESS || errno == ENOTCONN) {
-                                continue;
-                            }
-                            /* else failed */
-                            so->so_state &= SS_PERSISTENT_MASK;
-                            so->so_state |= SS_NOFDREF;
-                        } else {
-                            so->so_state &= ~SS_ISFCONNECTING;
-                        }
-
-                    }
-                    tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
-                } /* SS_ISFCONNECTING */
-#endif
             }
 
             /*
slirp/slirp_config.h +0 -4
index 896d802..70563ae 100644
@@ -2,10 +2,6 @@
  * User definable configuration options
  */
 
-/* Define if you want the connection to be probed */
-/* XXX Not working yet, so ignore this for now */
-#undef PROBE_CONN
-
 /* Define to 1 if you want KEEPALIVE timers */
 #define DO_KEEPALIVE 0
 
Signature · 1 line
2.4.2
Reviewed-by on v1 4/9
#8 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 4/9] slirp: Remove dead code for "PROBE_CONN"

Quoted 8 lines · Fam Zheng

It has always been compiled out. Remove it before we refactor the rest part of slirp poll.

---
 slirp/slirp.c        | 40 ----------------------------------------
 slirp/slirp_config.h |  4 ----
 2 files changed, 44 deletions(-)
author
#9 lore raw

PATCH 5/9 slirp: Add "poll_events" to struct socket

In reply to Fam Zheng

The field stores the current interesting events.

---
 slirp/socket.h | 1 +
 1 file changed, 1 insertion(+)
slirp/socket.h +1 -0
index 57e0407..0c49770 100644
@@ -53,6 +53,7 @@ struct socket {
   struct sbuf so_rcv;		/* Receive buffer */
   struct sbuf so_snd;		/* Send buffer */
   void * extra;			/* Extra pointer */
+  int poll_events;
 };
 
 
Signature · 1 line
2.4.2
author
#10 lore raw

PATCH 6/9 slirp: Move icmp socket to iohandler

In reply to Fam Zheng

Where the fd was added, compare the events with so->poll_events and call qemu_set_fd_handler() to update if it's changed. Where the fd was not added, call qemu_set_fd_handler() to remove it. Update slirp->do_slowtimo with a return value. For simplicity, G_IO_HUP and G_IO_ERR are unnecessary to be explicitly stated here, because they're implied in qemu_set_fd_handler.

For poll part, iohandler will call the read handler when there is an event, so the check in slirp_pollfds_poll() is removed.

---
 slirp/slirp.c | 83 ++++++++++++++++++++++++++++-------------------------------
 1 file changed, 39 insertions(+), 44 deletions(-)
slirp/slirp.c +39 -44
index 25cdca6..f648e6c 100644
@@ -286,6 +286,44 @@ static void slirp_update_timeout(uint32_t *timeout)
     *timeout = t;
 }
 
+static void slirp_icmp_read(void *opaque)
+{
+    struct socket *so = opaque;
+    icmp_receive(so);
+}
+
+static bool slirp_poll_update_icmp(struct socket *so)
+{
+    bool ret = false;
+    bool old, new;
+    /*
+     * See if it's timed out
+     */
+    if (so->so_expire) {
+        if (so->so_expire <= curtime) {
+            icmp_detach(so);
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
+            return ret;
+        } else {
+            ret = true; /* Let socket expire */
+        }
+    }
+
+    old = so->poll_events == G_IO_IN;
+    new = !!(so->so_state & SS_ISFCONNECTED);
+    if (old != new) {
+        /* Need update */
+        if (new) {
+            so->poll_events = G_IO_IN;
+            qemu_set_fd_handler(so->s, slirp_icmp_read, NULL, so);
+        } else {
+            so->poll_events = 0;
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
+        }
+    }
+    return ret;
+}
+
 void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 {
     Slirp *slirp;
@@ -431,29 +469,7 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
         for (so = slirp->icmp.so_next; so != &slirp->icmp;
                 so = so_next) {
             so_next = so->so_next;
-
-            so->pollfds_idx = -1;
-
-            /*
-             * See if it's timed out
-             */
-            if (so->so_expire) {
-                if (so->so_expire <= curtime) {
-                    icmp_detach(so);
-                    continue;
-                } else {
-                    slirp->do_slowtimo = true; /* Let socket expire */
-                }
-            }
-
-            if (so->so_state & SS_ISFCONNECTED) {
-                GPollFD pfd = {
-                    .fd = so->s,
-                    .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
-                };
-                so->pollfds_idx = pollfds->len;
-                g_array_append_val(pollfds, pfd);
-            }
+            slirp->do_slowtimo |= slirp_poll_update_icmp(so);
         }
     }
     slirp_update_timeout(timeout);
@@ -602,27 +618,6 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                     sorecvfrom(so);
                 }
             }
-
-            /*
-             * Check incoming ICMP relies.
-             */
-            for (so = slirp->icmp.so_next; so != &slirp->icmp;
-                    so = so_next) {
-                    int revents;
-
-                    so_next = so->so_next;
-
-                    revents = 0;
-                    if (so->pollfds_idx != -1) {
-                        revents = g_array_index(pollfds, GPollFD,
-                                                so->pollfds_idx).revents;
-                    }
-
-                    if (so->s != -1 &&
-                        (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
-                    icmp_receive(so);
-                }
-            }
         }
 
         if_start(slirp);
Signature · 1 line
2.4.2
author
#11 lore raw

PATCH 7/9 slirp: Move udb socket to iohandler

In reply to Fam Zheng

This change is a replicate of the previous one.

---
 slirp/slirp.c | 95 ++++++++++++++++++++++++-----------------------------------
 1 file changed, 39 insertions(+), 56 deletions(-)
slirp/slirp.c +39 -56
index f648e6c..a031240 100644
@@ -286,6 +286,44 @@ static void slirp_update_timeout(uint32_t *timeout)
     *timeout = t;
 }
 
+static void slirp_udb_read(void *opaque)
+{
+    struct socket *so = opaque;
+    sorecvfrom(so);
+}
+
+static bool slirp_poll_update_udb(struct socket *so)
+{
+    bool ret = false;
+    bool old, new;
+    /*
+     * See if it's timed out
+     */
+    if (so->so_expire) {
+        if (so->so_expire <= curtime) {
+            udp_detach(so);
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
+            return ret;
+        } else {
+            ret = true;
+        }
+    }
+
+    new = so->so_state & SS_ISFCONNECTED && so->so_queued <= 4;
+    old = so->poll_events == G_IO_IN;
+    if (old != new) {
+        /* Need update */
+        if (new) {
+            so->poll_events = G_IO_IN;
+            qemu_set_fd_handler(so->s, slirp_udb_read, NULL, so);
+        } else {
+            so->poll_events = 0;
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
+        }
+    }
+    return ret;
+}
+
 static void slirp_icmp_read(void *opaque)
 {
     struct socket *so = opaque;
@@ -428,39 +466,7 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
         for (so = slirp->udb.so_next; so != &slirp->udb;
                 so = so_next) {
             so_next = so->so_next;
-
-            so->pollfds_idx = -1;
-
-            /*
-             * See if it's timed out
-             */
-            if (so->so_expire) {
-                if (so->so_expire <= curtime) {
-                    udp_detach(so);
-                    continue;
-                } else {
-                    slirp->do_slowtimo = true; /* Let socket expire */
-                }
-            }
-
-            /*
-             * When UDP packets are received from over the
-             * link, they're sendto()'d straight away, so
-             * no need for setting for writing
-             * Limit the number of packets queued by this session
-             * to 4.  Note that even though we try and limit this
-             * to 4 packets, the session could have more queued
-             * if the packets needed to be fragmented
-             * (XXX <= 4 ?)
-             */
-            if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
-                GPollFD pfd = {
-                    .fd = so->s,
-                    .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
-                };
-                so->pollfds_idx = pollfds->len;
-                g_array_append_val(pollfds, pfd);
-            }
+            slirp->do_slowtimo |= slirp_poll_update_udb(so);
         }
 
         /*
@@ -595,29 +601,6 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
                      */
                 }
             }
-
-            /*
-             * Now UDP sockets.
-             * Incoming packets are sent straight away, they're not buffered.
-             * Incoming UDP data isn't buffered either.
-             */
-            for (so = slirp->udb.so_next; so != &slirp->udb;
-                    so = so_next) {
-                int revents;
-
-                so_next = so->so_next;
-
-                revents = 0;
-                if (so->pollfds_idx != -1) {
-                    revents = g_array_index(pollfds, GPollFD,
-                            so->pollfds_idx).revents;
-                }
-
-                if (so->s != -1 &&
-                    (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
-                    sorecvfrom(so);
-                }
-            }
         }
 
         if_start(slirp);
Signature · 1 line
2.4.2
author
#12 lore raw

PATCH 8/9 slirp: Move tcb socket to iohandler

In reply to Fam Zheng

This change is a replicate of the previous two, just the calculation of polled events is more delicate, and both read and write are polled.

---
 slirp/slirp.c | 310 ++++++++++++++++++++++++++--------------------------------
 1 file changed, 140 insertions(+), 170 deletions(-)
slirp/slirp.c +140 -170
index a031240..4cfa190 100644
@@ -286,6 +286,143 @@ static void slirp_update_timeout(uint32_t *timeout)
     *timeout = t;
 }
 
+static void slirp_tcb_read_oob(void *opaque)
+{
+    struct socket *so = opaque;
+    sorecvoob(so);
+}
+
+static void slirp_tcb_read(void *opaque)
+{
+    int ret;
+    struct socket *so = opaque;
+
+    if (so->so_state & SS_NOFDREF) {
+        return;
+    }
+    /*
+     * Check for incoming connections
+     */
+    if (so->so_state & SS_FACCEPTCONN) {
+        tcp_connect(so);
+    } else {
+        ret = soread(so);
+
+        /* Output it if we read something */
+        if (ret > 0) {
+            tcp_output(sototcpcb(so));
+        }
+    }
+}
+
+static void slirp_tcb_write(void *opaque)
+{
+    int ret;
+    struct socket *so = opaque;
+
+    if (so->so_state & SS_NOFDREF) {
+        return;
+    }
+    /*
+     * Check for non-blocking, still-connecting sockets
+     */
+    if (so->so_state & SS_ISFCONNECTING) {
+        /* Connected */
+        so->so_state &= ~SS_ISFCONNECTING;
+
+        ret = send(so->s, (const void *) &ret, 0, 0);
+        if (ret < 0) {
+            /* XXXXX Must fix, zero bytes is a NOP */
+            if (errno == EAGAIN || errno == EWOULDBLOCK ||
+                errno == EINPROGRESS || errno == ENOTCONN) {
+                return;
+            }
+
+            /* else failed */
+            so->so_state &= SS_PERSISTENT_MASK;
+            so->so_state |= SS_NOFDREF;
+        }
+        /* else so->so_state &= ~SS_ISFCONNECTING; */
+
+        /*
+         * Continue tcp_input
+         */
+        tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
+        /* continue; */
+    } else {
+        sowrite(so);
+    }
+    /*
+     * XXXXX If we wrote something (a lot), there
+     * could be a need for a window update.
+     * In the worst case, the remote will send
+     * a window probe to get things going again
+     */
+}
+
+static bool slirp_poll_update_tcb(struct socket *so)
+{
+    int events = 0;
+    Slirp *slirp = so->slirp;
+
+    /*
+     * See if we need a tcp_fasttimo
+     */
+    if (slirp->time_fasttimo == 0 &&
+        so->so_tcpcb->t_flags & TF_DELACK) {
+        slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
+    }
+
+    /*
+     * NOFDREF can include still connecting to local-host,
+     * newly socreated() sockets etc. Don't want to select these.
+     */
+    if (so->so_state & SS_NOFDREF || so->s == -1) {
+        so->poll_events = 0;
+        qemu_set_fd_handler(so->s, NULL, NULL, NULL);
+        return false;
+    }
+
+    if (so->so_state & SS_FACCEPTCONN) {
+        /*
+         * Set for reading sockets which are accepting
+         */
+        events = G_IO_IN;
+    } else if (so->so_state & SS_ISFCONNECTING) {
+        /*
+         * Set for writing sockets which are connecting
+         */
+        events = G_IO_OUT;
+    }
+
+    /*
+     * Set for writing if we are connected, can send more, and
+     * we have something to send
+     */
+    if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
+        events |= G_IO_OUT;
+    }
+
+    /*
+     * Set for reading (and urgent data) if we are connected, can
+     * receive more, and we have room for it XXX /2 ?
+     */
+    if (CONN_CANFRCV(so) &&
+        (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
+        events |= G_IO_IN | G_IO_PRI;
+    }
+
+    if (events != so->poll_events) {
+        so->poll_events = events;
+        qemu_set_fd_handler_pri(so->s,
+                                events & G_IO_IN ? slirp_tcb_read : NULL,
+                                events & G_IO_OUT ? slirp_tcb_write : NULL,
+                                events & G_IO_PRI ? slirp_tcb_read_oob : NULL,
+                                so);
+    }
+    return false;
+}
+
 static void slirp_udb_read(void *opaque)
 {
     struct socket *so = opaque;
@@ -301,8 +438,8 @@ static bool slirp_poll_update_udb(struct socket *so)
      */
     if (so->so_expire) {
         if (so->so_expire <= curtime) {
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
             udp_detach(so);
-            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
             return ret;
         } else {
             ret = true;
@@ -339,8 +476,8 @@ static bool slirp_poll_update_icmp(struct socket *so)
      */
     if (so->so_expire) {
         if (so->so_expire <= curtime) {
+            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
             icmp_detach(so);
-            qemu_set_fd_handler(so->s, NULL, NULL, NULL);
             return ret;
         } else {
             ret = true; /* Let socket expire */
@@ -385,79 +522,8 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 
         for (so = slirp->tcb.so_next; so != &slirp->tcb;
                 so = so_next) {
-            int events = 0;
-
             so_next = so->so_next;
-
-            so->pollfds_idx = -1;
-
-            /*
-             * See if we need a tcp_fasttimo
-             */
-            if (slirp->time_fasttimo == 0 &&
-                so->so_tcpcb->t_flags & TF_DELACK) {
-                slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
-            }
-
-            /*
-             * NOFDREF can include still connecting to local-host,
-             * newly socreated() sockets etc. Don't want to select these.
-             */
-            if (so->so_state & SS_NOFDREF || so->s == -1) {
-                continue;
-            }
-
-            /*
-             * Set for reading sockets which are accepting
-             */
-            if (so->so_state & SS_FACCEPTCONN) {
-                GPollFD pfd = {
-                    .fd = so->s,
-                    .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
-                };
-                so->pollfds_idx = pollfds->len;
-                g_array_append_val(pollfds, pfd);
-                continue;
-            }
-
-            /*
-             * Set for writing sockets which are connecting
-             */
-            if (so->so_state & SS_ISFCONNECTING) {
-                GPollFD pfd = {
-                    .fd = so->s,
-                    .events = G_IO_OUT | G_IO_ERR,
-                };
-                so->pollfds_idx = pollfds->len;
-                g_array_append_val(pollfds, pfd);
-                continue;
-            }
-
-            /*
-             * Set for writing if we are connected, can send more, and
-             * we have something to send
-             */
-            if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
-                events |= G_IO_OUT | G_IO_ERR;
-            }
-
-            /*
-             * Set for reading (and urgent data) if we are connected, can
-             * receive more, and we have room for it XXX /2 ?
-             */
-            if (CONN_CANFRCV(so) &&
-                (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
-                events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
-            }
-
-            if (events) {
-                GPollFD pfd = {
-                    .fd = so->s,
-                    .events = events,
-                };
-                so->pollfds_idx = pollfds->len;
-                g_array_append_val(pollfds, pfd);
-            }
+            slirp->do_slowtimo |= slirp_poll_update_tcb(so);
         }
 
         /*
@@ -484,8 +550,6 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
 void slirp_pollfds_poll(GArray *pollfds, int select_error)
 {
     Slirp *slirp;
-    struct socket *so, *so_next;
-    int ret;
 
     if (QTAILQ_EMPTY(&slirp_instances)) {
         return;
@@ -509,100 +573,6 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
             slirp->last_slowtimo = curtime;
         }
 
-        /*
-         * Check sockets
-         */
-        if (!select_error) {
-            /*
-             * Check TCP sockets
-             */
-            for (so = slirp->tcb.so_next; so != &slirp->tcb;
-                    so = so_next) {
-                int revents;
-
-                so_next = so->so_next;
-
-                revents = 0;
-                if (so->pollfds_idx != -1) {
-                    revents = g_array_index(pollfds, GPollFD,
-                                            so->pollfds_idx).revents;
-                }
-
-                if (so->so_state & SS_NOFDREF || so->s == -1) {
-                    continue;
-                }
-
-                /*
-                 * Check for URG data
-                 * This will soread as well, so no need to
-                 * test for G_IO_IN below if this succeeds
-                 */
-                if (revents & G_IO_PRI) {
-                    sorecvoob(so);
-                }
-                /*
-                 * Check sockets for reading
-                 */
-                else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
-                    /*
-                     * Check for incoming connections
-                     */
-                    if (so->so_state & SS_FACCEPTCONN) {
-                        tcp_connect(so);
-                        continue;
-                    } /* else */
-                    ret = soread(so);
-
-                    /* Output it if we read something */
-                    if (ret > 0) {
-                        tcp_output(sototcpcb(so));
-                    }
-                }
-
-                /*
-                 * Check sockets for writing
-                 */
-                if (!(so->so_state & SS_NOFDREF) &&
-                        (revents & (G_IO_OUT | G_IO_ERR))) {
-                    /*
-                     * Check for non-blocking, still-connecting sockets
-                     */
-                    if (so->so_state & SS_ISFCONNECTING) {
-                        /* Connected */
-                        so->so_state &= ~SS_ISFCONNECTING;
-
-                        ret = send(so->s, (const void *) &ret, 0, 0);
-                        if (ret < 0) {
-                            /* XXXXX Must fix, zero bytes is a NOP */
-                            if (errno == EAGAIN || errno == EWOULDBLOCK ||
-                                errno == EINPROGRESS || errno == ENOTCONN) {
-                                continue;
-                            }
-
-                            /* else failed */
-                            so->so_state &= SS_PERSISTENT_MASK;
-                            so->so_state |= SS_NOFDREF;
-                        }
-                        /* else so->so_state &= ~SS_ISFCONNECTING; */
-
-                        /*
-                         * Continue tcp_input
-                         */
-                        tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
-                        /* continue; */
-                    } else {
-                        ret = sowrite(so);
-                    }
-                    /*
-                     * XXXXX If we wrote something (a lot), there
-                     * could be a need for a window update.
-                     * In the worst case, the remote will send
-                     * a window probe to get things going again
-                     */
-                }
-            }
-        }
-
         if_start(slirp);
     }
 }
Signature · 1 line
2.4.2
author
#13 lore raw

PATCH 9/9 slirp: Remove unused pollfds from the parameter list

In reply to Fam Zheng
---
 main-loop.c      | 4 ++--
 slirp/libslirp.h | 4 ++--
 slirp/slirp.c    | 4 ++--
 stubs/slirp.c    | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
main-loop.c +2 -2
index 18b7508..b3808ae 100644
@@ -478,7 +478,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_fill(gpollfds, &timeout);
+    slirp_poll_prepare(&timeout);
 #endif
 
     if (timeout == UINT32_MAX) {
@@ -493,7 +493,7 @@ int main_loop_wait(int nonblocking)
 
     ret = os_host_main_loop_wait(timeout_ns);
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_poll(gpollfds, (ret < 0));
+    slirp_poll();
 #endif
 
     qemu_clock_run_all_timers();
slirp/libslirp.h +2 -2
index 7f329d9..94032ba 100644
@@ -17,9 +17,9 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
                   void *opaque);
 void slirp_cleanup(Slirp *slirp);
 
-void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout);
+void slirp_poll_prepare(uint32_t *timeout);
 
-void slirp_pollfds_poll(GArray *pollfds, int select_error);
+void slirp_poll(void);
 
 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
 
slirp/slirp.c +2 -2
index 4cfa190..bd80708 100644
@@ -499,7 +499,7 @@ static bool slirp_poll_update_icmp(struct socket *so)
     return ret;
 }
 
-void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
+void slirp_poll_prepare(uint32_t *timeout)
 {
     Slirp *slirp;
     struct socket *so, *so_next;
@@ -547,7 +547,7 @@ void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
     slirp_update_timeout(timeout);
 }
 
-void slirp_pollfds_poll(GArray *pollfds, int select_error)
+void slirp_poll(void)
 {
     Slirp *slirp;
 
stubs/slirp.c +2 -2
index bd0ac7f..6b6d211 100644
@@ -1,11 +1,11 @@
 #include "qemu-common.h"
 #include "slirp/slirp.h"
 
-void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
+void slirp_poll_prepare(uint32_t *timeout)
 {
 }
 
-void slirp_pollfds_poll(GArray *pollfds, int select_error)
+void slirp_poll(void)
 {
 }
 
Signature · 1 line
2.4.2
#14 lore raw

Re: [Qemu-devel] [PATCH 9/9] slirp: Remove unused pollfds from the parameter list

Quoted 22 lines · Fam Zheng
---
 main-loop.c      | 4 ++--
 slirp/libslirp.h | 4 ++--
 slirp/slirp.c    | 4 ++--
 stubs/slirp.c    | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
main-loop.c +1 -1
index 18b7508..b3808ae 100644
@@ -478,7 +478,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_fill(gpollfds, &timeout);
+    slirp_poll_prepare(&timeout);
 #endif
 
     if (timeout == UINT32_MAX) {

main-loop.c int main_loop_wait(int nonblocking) · line 496

Quoted 5 lines
main-loop.c +1 -1
@@ -493,7 +493,7 @@ int main_loop_wait(int nonblocking)
 
     ret = os_host_main_loop_wait(timeout_ns);
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_poll(gpollfds, (ret < 0));
+    slirp_poll();

The final step is to remove slirp calls completely from main_loop_wait(). slirp should use the qemu_set_fd_handler() and timer APIs directly instead of requiring callbacks around ppoll().

That would probably even make the slirp code simpler since it currently splits evented I/O into two phases:
1. Set slirp-internal state
2. Reflect slirp-internal state into iohandler and check timeouts after ppoll()

If slirp calls the appropriate qemu_set_fd_handler() and timer APIs *during* state transitions it won't have to use the two-step approach.

Stefan

author
#15 lore raw

Re: [Qemu-devel] [PATCH 9/9] slirp: Remove unused pollfds from the parameter list

Quoted 33 lines · Stefan Hajnoczi
Quoted 22 lines · Fam Zheng
---
 main-loop.c      | 4 ++--
 slirp/libslirp.h | 4 ++--
 slirp/slirp.c    | 4 ++--
 stubs/slirp.c    | 4 ++--
 4 files changed, 8 insertions(+), 8 deletions(-)
main-loop.c +1 -1
index 18b7508..b3808ae 100644
@@ -478,7 +478,7 @@ int main_loop_wait(int nonblocking)
     g_array_set_size(gpollfds, 0); /* reset for new iteration */
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_fill(gpollfds, &timeout);
+    slirp_poll_prepare(&timeout);
 #endif
 
     if (timeout == UINT32_MAX) {

main-loop.c int main_loop_wait(int nonblocking) · line 496

Quoted 5 lines
main-loop.c +1 -1
@@ -493,7 +493,7 @@ int main_loop_wait(int nonblocking)
 
     ret = os_host_main_loop_wait(timeout_ns);
 #ifdef CONFIG_SLIRP
-    slirp_pollfds_poll(gpollfds, (ret < 0));
+    slirp_poll();

The final step is to remove slirp calls completely from main_loop_wait(). slirp should use the qemu_set_fd_handler() and timer APIs directly instead of requiring callbacks around ppoll().

Quoted 8 lines

That would probably even make the slirp code simpler since it currently splits evented I/O into two phases:
1. Set slirp-internal state
2. Reflect slirp-internal state into iohandler and check timeouts after ppoll()

If slirp calls the appropriate qemu_set_fd_handler() and timer APIs *during* state transitions it won't have to use the two-step approach.

Yes, that's right, but it is a harder change that I want to leave for a separate series.

Fam

#16 lore raw
In reply to Fam Zheng
Quoted 27 lines · Fam Zheng

Currently iohandler.c and asyn.c has a lot in common, except that iohandler is hooked into main_loop_wait(), with qemu_iohandler_fill(). Likewise for slirp.

It is much cleaner to drive slirp fds with iohandler, and implement iohandler with an AioContext. This series does these two things. It applies to Stefan's net-next tree which has the prerequisite that removes the obstacle "qemu_set_fd_handler2" for this convergence:

http://comments.gmane.org/gmane.comp.emulators.qemu/341526

Upon that this series makes a more consistent main loop structure, and will also benefits future work on event loop, such as introducing of epoll and possibly in the long run moving things to separate iothread (whose event loop is based on AioContext too).

Fam Zheng (9):
aio: Introduce aio_set_fd_handler_pri
iohandler: Use aio code
main-loop: Move include of "qemu/sockets.h" to libslirp.h
slirp: Remove dead code for "PROBE_CONN"
slirp: Add "poll_events" to struct socket
slirp: Move icmp socket to iohandler
slirp: Move udb socket to iohandler
slirp: Move tcb socket to iohandler
slirp: Remove unused pollfds from the parameter list

Quoted 12 lines
 aio-posix.c              |  19 ++
 aio-win32.c              |  10 +
 include/block/aio.h      |   8 +
 include/qemu/main-loop.h |  10 +-
 iohandler.c              | 124 +++--------
 main-loop.c              |  10 +-
 slirp/libslirp.h         |   5 +-
 slirp/slirp.c            | 528 +++++++++++++++++++----------------------------
 slirp/slirp_config.h     |   4 -
 slirp/socket.h           |   1 +
 stubs/slirp.c            |   4 +-
 11 files changed, 300 insertions(+), 423 deletions(-)

Pinging Jan Kiszka for slirp changes.