Skip to contentSkip to conversation

port network layer onto glib

RFC PATCH Superseded Liu Ping Fan 5 participants 35 messages Apr 17, 2013 – Apr 23, 2013

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover port network layer onto glib

From: Liu Ping Fan <[email protected]>

summary:
patch1: GSource event abstraction
patch2~7: port network backend to glib
patch8~11: make network core re-entrant
patch12~15: port the slirp backend onto glib

For the patch --slirp: make slirp event dispatch based on slirp instance, not global The slirp_pollfds_fill/poll logic are untouch, but owning the format issue, it change much. Will fix in next verion.

v3->v4:
1.separate GSource event to dedicated file
2.integrated with net core re-entrant
3.make slirp/ re-entrant

v2->v3:
1.drop hub and the frontend(virtio net)
2.split the patch for NetClientSource

v1->v2:
1.NetClientState can associate with up to 2 GSource, for virtio net, one for tx, one for rx,
so vq can run on different threads.
2.make network front-end onto glib, currently virtio net dataplane

Liu Ping Fan (15):
util: introduce gsource event abstration
net: introduce bind_ctx to NetClientInfo
net: port tap onto GSource
net: resolve race of tap backend and its peer
net: port vde onto GSource
net: port socket to GSource
net: port tap-win32 onto GSource
net: hub use lock to protect ports list
net: introduce lock to protect NetQueue
net: introduce lock to protect NetClientState's peer's access
net: make netclient re-entrant with refcnt
slirp: make timeout local
slirp: make slirp event dispatch based on slirp instance, not global
slirp: handle race condition
slirp: use lock to protect the slirp_instances

 hw/qdev-properties-system.c |   14 +
 include/net/net.h           |   12 +
 include/qemu/module.h       |    2 +
 main-loop.c                 |    4 -
 net/hub.c                   |   28 ++-
 net/net.c                   |  123 ++++++++-
 net/queue.c                 |   15 +-
 net/slirp.c                 |   47 ++++-
 net/socket.c                |  158 +++++++++---
 net/tap-win32.c             |   28 ++-
 net/tap.c                   |   67 ++++-
 net/vde.c                   |   28 ++-
 slirp/libslirp.h            |    7 +-
 slirp/slirp.c               |  625 +++++++++++++++++++++----------------------
 slirp/slirp.h               |    6 +
 slirp/socket.c              |    2 +
 slirp/socket.h              |    1 +
 stubs/slirp.c               |    8 -
 util/Makefile.objs          |    1 +
 util/event_gsource.c        |  169 ++++++++++++
 util/event_gsource.h        |   54 ++++
 21 files changed, 1003 insertions(+), 396 deletions(-)
 create mode 100644 util/event_gsource.c
 create mode 100644 util/event_gsource.h
Signature · 1 line
1.7.4.4
#1 lore raw

RFC PATCH v4 1/15 util: introduce gsource event abstration

From: Liu Ping Fan <[email protected]>

Introduce two structs EventGSource, EventsGSource EventGSource is used to abstract the event with single backend file. EventsGSource is used to abstract the event with dynamicly changed backend file, ex, slirp.

---
 util/Makefile.objs   |    1 +
 util/event_gsource.c |  169 ++++++++++++++++++++++++++++++++++++++++++++++++++
 util/event_gsource.h |   54 ++++++++++++++++
 3 files changed, 224 insertions(+), 0 deletions(-)
 create mode 100644 util/event_gsource.c
 create mode 100644 util/event_gsource.h
util/Makefile.objs +1 -0
index 495a178..a676d7d 100644
@@ -8,3 +8,4 @@ util-obj-y += error.o qemu-error.o
 util-obj-$(CONFIG_POSIX) += compatfd.o
 util-obj-y += iov.o aes.o qemu-config.o qemu-sockets.o uri.o notify.o
 util-obj-y += qemu-option.o qemu-progress.o
+util-obj-y += event_gsource.o
util/event_gsource.c new file +169 -0

169 lines not on this page · raw message

util/event_gsource.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#6 lore raw

RFC PATCH v4 2/15 net: introduce bind_ctx to NetClientInfo

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Introduce bind_ctx interface for NetClientState. It will help to bind NetClientState with a GSource. Currently, these GSource attached with default context, but in future, after resolving all the race condition in network layer, NetClientStates can run on different threads

---
 include/net/net.h |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
include/net/net.h +2 -0
index cb049a1..88332d2 100644
@@ -44,6 +44,7 @@ typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
 typedef void (NetCleanup) (NetClientState *);
 typedef void (LinkStatusChanged)(NetClientState *);
 typedef void (NetClientDestructor)(NetClientState *);
+typedef void (NetClientBindCtx)(NetClientState *, GMainContext *);
 
 typedef struct NetClientInfo {
     NetClientOptionsKind type;
@@ -55,6 +56,7 @@ typedef struct NetClientInfo {
     NetCleanup *cleanup;
     LinkStatusChanged *link_status_changed;
     NetPoll *poll;
+    NetClientBindCtx *bind_ctx;
 } NetClientInfo;
 
 struct NetClientState {
Signature · 1 line
1.7.4.4
#7 lore raw

RFC PATCH v4 3/15 net: port tap onto GSource

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 net/tap.c |   62 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 52 insertions(+), 10 deletions(-)
net/tap.c +52 -10
index daab350..35cbb6e 100644
@@ -62,6 +62,7 @@ typedef struct TAPState {
     bool enabled;
     VHostNetState *vhost_net;
     unsigned host_vnet_hdr_len;
+    EventGSource *nsrc;
 } TAPState;
 
 static int launch_script(const char *setup_script, const char *ifname, int fd);
@@ -70,25 +71,48 @@ static int tap_can_send(void *opaque);
 static void tap_send(void *opaque);
 static void tap_writable(void *opaque);
 
-static void tap_update_fd_handler(TAPState *s)
+static bool readable(void *opaque)
 {
-    qemu_set_fd_handler2(s->fd,
-                         s->read_poll && s->enabled ? tap_can_send : NULL,
-                         s->read_poll && s->enabled ? tap_send     : NULL,
-                         s->write_poll && s->enabled ? tap_writable : NULL,
-                         s);
+    TAPState *s = opaque;
+
+    if (s->enabled && s->read_poll &&
+        tap_can_send(s)) {
+        return true;
+    }
+    return false;
+}
+
+static bool writable(void *opaque)
+{
+    TAPState *s = opaque;
+
+    if (s->enabled && s->write_poll) {
+        return true;
+    }
+    return false;
+}
+
+static gboolean tap_handler(gpointer data)
+{
+    EventGSource *nsrc = data;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        tap_send(nsrc->opaque);
+    }
+    if (nsrc->gfd.revents & G_IO_OUT) {
+        tap_writable(nsrc->opaque);
+    }
+    return true;
 }
 
 static void tap_read_poll(TAPState *s, bool enable)
 {
     s->read_poll = enable;
-    tap_update_fd_handler(s);
 }
 
 static void tap_write_poll(TAPState *s, bool enable)
 {
     s->write_poll = enable;
-    tap_update_fd_handler(s);
 }
 
 static void tap_writable(void *opaque)
@@ -291,6 +315,7 @@ static void tap_cleanup(NetClientState *nc)
 
     tap_read_poll(s, false);
     tap_write_poll(s, false);
+    event_source_release(s->nsrc);
     close(s->fd);
     s->fd = -1;
 }
@@ -298,8 +323,10 @@ static void tap_cleanup(NetClientState *nc)
 static void tap_poll(NetClientState *nc, bool enable)
 {
     TAPState *s = DO_UPCAST(TAPState, nc, nc);
+    /* fixme, when tap backend on another thread, the disable should be sync */
     tap_read_poll(s, enable);
     tap_write_poll(s, enable);
+
 }
 
 int tap_get_fd(NetClientState *nc)
@@ -309,6 +336,13 @@ int tap_get_fd(NetClientState *nc)
     return s->fd;
 }
 
+static void tap_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    TAPState *s = DO_UPCAST(TAPState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
+}
+
 /* fd support */
 
 static NetClientInfo net_tap_info = {
@@ -319,6 +353,7 @@ static NetClientInfo net_tap_info = {
     .receive_iov = tap_receive_iov,
     .poll = tap_poll,
     .cleanup = tap_cleanup,
+    .bind_ctx = tap_bind_ctx,
 };
 
 static TAPState *net_tap_fd_init(NetClientState *peer,
@@ -596,6 +631,7 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
                             int vnet_hdr, int fd)
 {
     TAPState *s;
+    EventGSource *nsrc;
 
     s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
     if (!s) {
@@ -606,6 +642,12 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
     if (tap_set_sndbuf(s->fd, tap) < 0) {
         return -1;
     }
+    nsrc = event_source_new(s->fd, tap_handler, s);
+    nsrc->gfd.events = G_IO_IN|G_IO_OUT;
+    nsrc->readable = readable;
+    nsrc->writable = writable;
+    s->nsrc = nsrc;
+    s->nc.info->bind_ctx(&s->nc, NULL);
 
     if (tap->has_fd || tap->has_fds) {
         snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
@@ -843,8 +885,8 @@ int tap_enable(NetClientState *nc)
     } else {
         ret = tap_fd_enable(s->fd);
         if (ret == 0) {
+            /*fixme, will be sync to ensure handler not be called */
             s->enabled = true;
-            tap_update_fd_handler(s);
         }
         return ret;
     }
@@ -861,8 +903,8 @@ int tap_disable(NetClientState *nc)
         ret = tap_fd_disable(s->fd);
         if (ret == 0) {
             qemu_purge_queued_packets(nc);
+            /*fixme, will be sync to ensure handler not be called */
             s->enabled = false;
-            tap_update_fd_handler(s);
         }
         return ret;
     }
Signature · 1 line
1.7.4.4
#8 lore raw

RFC PATCH v4 4/15 net: resolve race of tap backend and its peer

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

When vhost net enabled, we should be sure that the user space fd handler is not in flight

---
 net/tap.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
net/tap.c +5 -0
index 35cbb6e..b5629e3 100644
@@ -41,6 +41,7 @@
 #include "qemu/error-report.h"
 
 #include "net/tap.h"
+#include "util/event_gsource.h"
 
 #include "hw/vhost_net.h"
 
@@ -327,6 +328,10 @@ static void tap_poll(NetClientState *nc, bool enable)
     tap_read_poll(s, enable);
     tap_write_poll(s, enable);
 
+    if (!enable) {
+        /* need sync so vhost can take over polling */
+        g_source_remove_poll(&s->nsrc->source, &s->nsrc->gfd);
+    }
 }
 
 int tap_get_fd(NetClientState *nc)
Signature · 1 line
1.7.4.4
#11 lore raw

RFC PATCH v4 5/15 net: port vde onto GSource

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 net/vde.c |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)
net/vde.c +26 -2
index 4dea32d..bb7016b 100644
@@ -30,10 +30,12 @@
 #include "qemu-common.h"
 #include "qemu/option.h"
 #include "qemu/main-loop.h"
+#include "util/event_gsource.h"
 
 typedef struct VDEState {
     NetClientState nc;
     VDECONN *vde;
+    EventGSource *nsrc;
 } VDEState;
 
 static void vde_to_qemu(void *opaque)
@@ -60,18 +62,36 @@ static ssize_t vde_receive(NetClientState *nc, const uint8_t *buf, size_t size)
     return ret;
 }
 
+static gboolean vde_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        vde_to_qemu(nsrc->opaque);
+    }
+    return true;
+}
+
 static void vde_cleanup(NetClientState *nc)
 {
     VDEState *s = DO_UPCAST(VDEState, nc, nc);
-    qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
+    event_source_release(s->nsrc);
     vde_close(s->vde);
 }
 
+static void vde_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    VDEState *s = DO_UPCAST(VDEState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
+}
+
 static NetClientInfo net_vde_info = {
     .type = NET_CLIENT_OPTIONS_KIND_VDE,
     .size = sizeof(VDEState),
     .receive = vde_receive,
     .cleanup = vde_cleanup,
+    .bind_ctx = vde_bind_ctx,
 };
 
 static int net_vde_init(NetClientState *peer, const char *model,
@@ -83,6 +103,7 @@ static int net_vde_init(NetClientState *peer, const char *model,
     VDECONN *vde;
     char *init_group = (char *)group;
     char *init_sock = (char *)sock;
+    EventGSource *nsrc;
 
     struct vde_open_args args = {
         .port = port,
@@ -104,7 +125,10 @@ static int net_vde_init(NetClientState *peer, const char *model,
 
     s->vde = vde;
 
-    qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
+    nsrc = event_source_new(vde_datafd(vde), vde_handler, s);
+    nsrc->gfd.events = G_IO_IN;
+    s->nsrc = nsrc;
+    nc->info->bind_ctx(nc, NULL);
 
     return 0;
 }
Signature · 1 line
1.7.4.4
#12 lore raw

RFC PATCH v4 6/15 net: port socket to GSource

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Port NetSocketState onto NetClientSource. The only thing specail is that owning to the socket's state machine changes, we need to change the handler. We implement that by destroy the old NetClientSource and attach a new one with NetSocketState.

---
 net/socket.c |  158 ++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 122 insertions(+), 36 deletions(-)
net/socket.c +122 -36
index 396dc8c..bdd5dc0 100644
@@ -31,6 +31,8 @@
 #include "qemu/option.h"
 #include "qemu/sockets.h"
 #include "qemu/iov.h"
+#include "util/event_gsource.h"
+
 
 typedef struct NetSocketState {
     NetClientState nc;
@@ -42,13 +44,15 @@ typedef struct NetSocketState {
     unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
     uint8_t buf[4096];
     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
-    IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
     bool read_poll;               /* waiting to receive data? */
     bool write_poll;              /* waiting to transmit data? */
+    EventGSource *nsrc;
 } NetSocketState;
 
-static void net_socket_accept(void *opaque);
 static void net_socket_writable(void *opaque);
+static gboolean net_socket_listen_handler(gpointer data);
+static gboolean net_socket_establish_handler(gpointer data);
+
 
 /* Only read packets from socket when peer can receive them */
 static int net_socket_can_send(void *opaque)
@@ -58,25 +62,14 @@ static int net_socket_can_send(void *opaque)
     return qemu_can_send_packet(&s->nc);
 }
 
-static void net_socket_update_fd_handler(NetSocketState *s)
-{
-    qemu_set_fd_handler2(s->fd,
-                         s->read_poll  ? net_socket_can_send : NULL,
-                         s->read_poll  ? s->send_fn : NULL,
-                         s->write_poll ? net_socket_writable : NULL,
-                         s);
-}
-
 static void net_socket_read_poll(NetSocketState *s, bool enable)
 {
     s->read_poll = enable;
-    net_socket_update_fd_handler(s);
 }
 
 static void net_socket_write_poll(NetSocketState *s, bool enable)
 {
     s->write_poll = enable;
-    net_socket_update_fd_handler(s);
 }
 
 static void net_socket_writable(void *opaque)
@@ -148,6 +141,7 @@ static void net_socket_send(void *opaque)
     unsigned l;
     uint8_t buf1[4096];
     const uint8_t *buf;
+    EventGSource *new_nsrc, *nsrc;
 
     size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
     if (size < 0) {
@@ -160,7 +154,13 @@ static void net_socket_send(void *opaque)
         net_socket_read_poll(s, false);
         net_socket_write_poll(s, false);
         if (s->listen_fd != -1) {
-            qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+            nsrc = s->nsrc;
+            new_nsrc = event_source_new(s->listen_fd, net_socket_listen_handler,
+                                s);
+            s->nsrc = new_nsrc;
+            new_nsrc->gfd.events = G_IO_IN;
+            g_source_destroy(&nsrc->source);
+            s->nc.info->bind_ctx(&s->nc, NULL);
         }
         closesocket(s->fd);
 
@@ -331,6 +331,14 @@ static void net_socket_cleanup(NetClientState *nc)
         closesocket(s->listen_fd);
         s->listen_fd = -1;
     }
+    event_source_release(s->nsrc);
+}
+
+static void net_socket_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -338,8 +346,22 @@ static NetClientInfo net_dgram_socket_info = {
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive_dgram,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,
 };
 
+static gboolean net_socket_dgram_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send_dgram(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
                                                 const char *model,
                                                 const char *name,
@@ -350,6 +372,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     socklen_t saddr_len;
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
      * Because this may be "shared" socket from a "master" process, datagrams would be recv()
@@ -393,7 +416,10 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
 
     s->fd = fd;
     s->listen_fd = -1;
-    s->send_fn = net_socket_send_dgram;
+    nsrc = event_source_new(fd, net_socket_dgram_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN|G_IO_OUT;
+    nc->info->bind_ctx(nc, NULL);
     net_socket_read_poll(s, true);
 
     /* mcast: save bound address as dst */
@@ -408,20 +434,28 @@ err:
     return NULL;
 }
 
-static void net_socket_connect(void *opaque)
-{
-    NetSocketState *s = opaque;
-    s->send_fn = net_socket_send;
-    net_socket_read_poll(s, true);
-}
-
 static NetClientInfo net_socket_info = {
     .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,
 };
 
+static gboolean net_socket_connect_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
+
+    new_nsrc = event_source_new(s->fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
                                                  const char *model,
                                                  const char *name,
@@ -429,6 +463,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
 
@@ -440,9 +475,16 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
     s->listen_fd = -1;
 
     if (is_connected) {
-        net_socket_connect(s);
+        nsrc = event_source_new(fd, net_socket_establish_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN|G_IO_OUT;
+        nc->info->bind_ctx(nc, NULL);
     } else {
-        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
+        nsrc = event_source_new(fd, net_socket_connect_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN;
+        nc->info->bind_ctx(nc, NULL);
+
     }
     return s;
 }
@@ -473,30 +515,69 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
     return NULL;
 }
 
-static void net_socket_accept(void *opaque)
+static gboolean net_socket_establish_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
+static bool readable(void *opaque)
 {
     NetSocketState *s = opaque;
+
+    if (s->read_poll && net_socket_can_send(s)) {
+        return true;
+    }
+    return false;
+}
+
+static bool writable(void *opaque)
+{
+    NetSocketState *s = opaque;
+
+    if (s->write_poll) {
+        return true;
+    }
+    return false;
+}
+
+static gboolean net_socket_listen_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
     struct sockaddr_in saddr;
     socklen_t len;
     int fd;
 
-    for(;;) {
-        len = sizeof(saddr);
-        fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
-        if (fd < 0 && errno != EINTR) {
-            return;
-        } else if (fd >= 0) {
-            qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
-            break;
-        }
+    len = sizeof(saddr);
+    fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
+    if (fd < 0 && errno != EINTR) {
+        return false;
     }
 
     s->fd = fd;
     s->nc.link_down = false;
-    net_socket_connect(s);
+    new_nsrc = event_source_new(fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;
+    new_nsrc->readable = readable;
+    new_nsrc->writable = writable;
+    /* prevent more than one connect req */
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+    net_socket_read_poll(s, true);
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
              "socket: connection from %s:%d",
              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+
+    return true;
 }
 
 static int net_socket_listen_init(NetClientState *peer,
@@ -508,6 +589,7 @@ static int net_socket_listen_init(NetClientState *peer,
     NetSocketState *s;
     struct sockaddr_in saddr;
     int fd, val, ret;
+    EventGSource *nsrc;
 
     if (parse_host_port(&saddr, host_str) < 0)
         return -1;
@@ -542,7 +624,11 @@ static int net_socket_listen_init(NetClientState *peer,
     s->listen_fd = fd;
     s->nc.link_down = true;
 
-    qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    nsrc = event_source_new(fd, net_socket_listen_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN;
+    nc->info->bind_ctx(nc, NULL);
+
     return 0;
 }
 
Signature · 1 line
1.7.4.4
#17 lore raw

RFC PATCH v4 7/15 net: port tap-win32 onto GSource

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

---
 net/tap-win32.c |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)
net/tap-win32.c +26 -2
index 91e9e84..e66edf1 100644
@@ -635,13 +635,14 @@ static int tap_win32_open(tap_win32_overlapped_t **phandle,
  typedef struct TAPState {
      NetClientState nc;
      tap_win32_overlapped_t *handle;
+     EventGSource *nsrc;
  } TAPState;
 
 static void tap_cleanup(NetClientState *nc)
 {
     TAPState *s = DO_UPCAST(TAPState, nc, nc);
 
-    qemu_del_wait_object(s->handle->tap_semaphore, NULL, NULL);
+    event_source_release(s->nsrc);
 
     /* FIXME: need to kill thread and close file handle:
        tap_win32_close(s);
@@ -669,19 +670,39 @@ static void tap_win32_send(void *opaque)
     }
 }
 
+static void tap_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    TAPState *s = DO_UPCAST(TAPState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
+}
+
 static NetClientInfo net_tap_win32_info = {
     .type = NET_CLIENT_OPTIONS_KIND_TAP,
     .size = sizeof(TAPState),
     .receive = tap_receive,
     .cleanup = tap_cleanup,
+    .bind_ctx = tap_bind_ctx,
 };
 
+static gboolean tap_win32_handler(gpointer data)
+{
+    EventGSource *nsrc = data;
+    TAPState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        tap_win32_send(s);
+    }
+    return true;
+}
+
 static int tap_win32_init(NetClientState *peer, const char *model,
                           const char *name, const char *ifname)
 {
     NetClientState *nc;
     TAPState *s;
     tap_win32_overlapped_t *handle;
+    EventGSource *nsrc;
 
     if (tap_win32_open(&handle, ifname) < 0) {
         printf("tap: Could not open '%s'\n", ifname);
@@ -697,7 +718,10 @@ static int tap_win32_init(NetClientState *peer, const char *model,
 
     s->handle = handle;
 
-    qemu_add_wait_object(s->handle->tap_semaphore, tap_win32_send, s);
+    nsrc = event_source_new(s->handle->tap_semaphore, tap_win32_handler, s);
+    nsrc->gfd.events = G_IO_IN;
+    s->nsrc = nsrc;
+    nc->info->bind_ctx(&s->nc, NULL);
 
     return 0;
 }
Signature · 1 line
1.7.4.4
#18 lore raw

RFC PATCH v4 8/15 net: hub use lock to protect ports list

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Hub ports will run on multi-threads, so use lock to protect them.

---
 net/hub.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
net/hub.c +24 -1
index df32074..812a6dc 100644
@@ -37,6 +37,7 @@ struct NetHub {
     int id;
     QLIST_ENTRY(NetHub) next;
     int num_ports;
+    QemuMutex ports_lock;
     QLIST_HEAD(, NetHubPort) ports;
 };
 
@@ -47,6 +48,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
 {
     NetHubPort *port;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == source_port) {
             continue;
@@ -54,6 +56,7 @@ static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
 
         qemu_send_packet(&port->nc, buf, len);
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return len;
 }
 
@@ -63,6 +66,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
     NetHubPort *port;
     ssize_t len = iov_size(iov, iovcnt);
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == source_port) {
             continue;
@@ -70,6 +74,7 @@ static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
 
         qemu_sendv_packet(&port->nc, iov, iovcnt);
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return len;
 }
 
@@ -80,6 +85,7 @@ static NetHub *net_hub_new(int id)
     hub = g_malloc(sizeof(*hub));
     hub->id = id;
     hub->num_ports = 0;
+    qemu_mutex_init(&hub->ports_lock);
     QLIST_INIT(&hub->ports);
 
     QLIST_INSERT_HEAD(&hubs, hub, next);
@@ -93,16 +99,19 @@ static int net_hub_port_can_receive(NetClientState *nc)
     NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
     NetHub *hub = src_port->hub;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &hub->ports, next) {
         if (port == src_port) {
             continue;
         }
 
         if (qemu_can_send_packet(&port->nc)) {
+            qemu_mutex_unlock(&hub->ports_lock);
             return 1;
         }
     }
 
+    qemu_mutex_unlock(&hub->ports_lock);
     return 0;
 }
 
@@ -155,8 +164,9 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
     port = DO_UPCAST(NetHubPort, nc, nc);
     port->id = id;
     port->hub = hub;
-
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_INSERT_HEAD(&hub->ports, port, next);
+    qemu_mutex_unlock(&hub->ports_lock);
 
     return port;
 }
@@ -197,13 +207,16 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
 
     QLIST_FOREACH(hub, &hubs, next) {
         if (hub->id == hub_id) {
+            qemu_mutex_lock(&hub->ports_lock);
             QLIST_FOREACH(port, &hub->ports, next) {
                 peer = port->nc.peer;
 
                 if (peer && strcmp(peer->name, name) == 0) {
+                    qemu_mutex_unlock(&hub->ports_lock);
                     return peer;
                 }
             }
+            qemu_mutex_unlock(&hub->ports_lock);
         }
     }
     return NULL;
@@ -220,12 +233,15 @@ NetClientState *net_hub_port_find(int hub_id)
 
     QLIST_FOREACH(hub, &hubs, next) {
         if (hub->id == hub_id) {
+            qemu_mutex_lock(&hub->ports_lock);
             QLIST_FOREACH(port, &hub->ports, next) {
                 nc = port->nc.peer;
                 if (!nc) {
+                    qemu_mutex_unlock(&hub->ports_lock);
                     return &(port->nc);
                 }
             }
+            qemu_mutex_unlock(&hub->ports_lock);
             break;
         }
     }
@@ -244,12 +260,14 @@ void net_hub_info(Monitor *mon)
 
     QLIST_FOREACH(hub, &hubs, next) {
         monitor_printf(mon, "hub %d\n", hub->id);
+        qemu_mutex_lock(&hub->ports_lock);
         QLIST_FOREACH(port, &hub->ports, next) {
             if (port->nc.peer) {
                 monitor_printf(mon, " \\ ");
                 print_net_client(mon, port->nc.peer);
             }
         }
+        qemu_mutex_unlock(&hub->ports_lock);
     }
 }
 
@@ -306,6 +324,7 @@ void net_hub_check_clients(void)
     QLIST_FOREACH(hub, &hubs, next) {
         int has_nic = 0, has_host_dev = 0;
 
+        qemu_mutex_lock(&hub->ports_lock);
         QLIST_FOREACH(port, &hub->ports, next) {
             peer = port->nc.peer;
             if (!peer) {
@@ -328,6 +347,7 @@ void net_hub_check_clients(void)
                 break;
             }
         }
+        qemu_mutex_unlock(&hub->ports_lock);
         if (has_host_dev && !has_nic) {
             fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
         }
@@ -343,12 +363,15 @@ bool net_hub_flush(NetClientState *nc)
 {
     NetHubPort *port;
     NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
+    NetHub *hub = source_port->hub;
     int ret = 0;
 
+    qemu_mutex_lock(&hub->ports_lock);
     QLIST_FOREACH(port, &source_port->hub->ports, next) {
         if (port != source_port) {
             ret += qemu_net_queue_flush(port->nc.send_queue);
         }
     }
+    qemu_mutex_unlock(&hub->ports_lock);
     return ret ? true : false;
 }
Signature · 1 line
1.7.4.4
#19 lore raw

RFC PATCH v4 9/15 net: introduce lock to protect NetQueue

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

NetQueue will be accessed by nc and its peers at the same time, need lock to protect it.

---
 net/queue.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
net/queue.c +11 -0
index 859d02a..2856c1d 100644
@@ -53,6 +53,7 @@ struct NetQueue {
     uint32_t nq_maxlen;
     uint32_t nq_count;
 
+    QemuMutex lock;
     QTAILQ_HEAD(packets, NetPacket) packets;
 
     unsigned delivering : 1;
@@ -68,6 +69,7 @@ NetQueue *qemu_new_net_queue(void *opaque)
     queue->nq_maxlen = 10000;
     queue->nq_count = 0;
 
+    qemu_mutex_init(&queue->lock);
     QTAILQ_INIT(&queue->packets);
 
     queue->delivering = 0;
@@ -107,7 +109,9 @@ static void qemu_net_queue_append(NetQueue *queue,
     memcpy(packet->data, buf, size);
 
     queue->nq_count++;
+    qemu_mutex_lock(&queue->lock);
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
+    qemu_mutex_unlock(&queue->lock);
 }
 
 static void qemu_net_queue_append_iov(NetQueue *queue,
@@ -142,7 +146,9 @@ static void qemu_net_queue_append_iov(NetQueue *queue,
     }
 
     queue->nq_count++;
+    qemu_mutex_lock(&queue->lock);
     QTAILQ_INSERT_TAIL(&queue->packets, packet, entry);
+    qemu_mutex_unlock(&queue->lock);
 }
 
 static ssize_t qemu_net_queue_deliver(NetQueue *queue,
@@ -229,6 +235,7 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
 {
     NetPacket *packet, *next;
 
+    qemu_mutex_lock(&queue->lock);
     QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) {
         if (packet->sender == from) {
             QTAILQ_REMOVE(&queue->packets, packet, entry);
@@ -236,10 +243,12 @@ void qemu_net_queue_purge(NetQueue *queue, NetClientState *from)
             g_free(packet);
         }
     }
+    qemu_mutex_unlock(&queue->lock);
 }
 
 bool qemu_net_queue_flush(NetQueue *queue)
 {
+    qemu_mutex_lock(&queue->lock);
     while (!QTAILQ_EMPTY(&queue->packets)) {
         NetPacket *packet;
         int ret;
@@ -256,6 +265,7 @@ bool qemu_net_queue_flush(NetQueue *queue)
         if (ret == 0) {
             queue->nq_count++;
             QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
+            qemu_mutex_unlock(&queue->lock);
             return false;
         }
 
@@ -265,5 +275,6 @@ bool qemu_net_queue_flush(NetQueue *queue)
 
         g_free(packet);
     }
+    qemu_mutex_unlock(&queue->lock);
     return true;
 }
Signature · 1 line
1.7.4.4
#20 lore raw

RFC PATCH v4 10/15 net: introduce lock to protect NetClientState's peer's access

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Introduce nc->peer_lock to shield off the race of nc->peer's reader and deleter. With it, after deleter finish, no new qemu_send_packet_xx() will append packet to peer->send_queue, therefore no new reference from packet->sender to nc will exist in nc->peer->send_queue.

---
 include/net/net.h |    7 +++++
 net/net.c         |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 net/queue.c       |    4 +-
 3 files changed, 84 insertions(+), 6 deletions(-)
include/net/net.h +7 -0
index 88332d2..54f91ea 100644
@@ -5,6 +5,7 @@
 #include "qemu-common.h"
 #include "qapi/qmp/qdict.h"
 #include "qemu/option.h"
+#include "qemu/thread.h"
 #include "net/queue.h"
 #include "migration/vmstate.h"
 #include "qapi-types.h"
@@ -63,6 +64,10 @@ struct NetClientState {
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
+    /* protect the race access of peer only between reader and writer.
+         * to resolve the writer's race condition, resort on biglock.
+         */
+    QemuMutex peer_lock;
     NetClientState *peer;
     NetQueue *send_queue;
     char *model;
@@ -75,6 +80,7 @@ struct NetClientState {
 
 typedef struct NICState {
     NetClientState *ncs;
+    NetClientState **pending_peer;
     NICConf *conf;
     void *opaque;
     bool peer_deleted;
@@ -102,6 +108,7 @@ NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
                                               const char *client_str);
 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
+int qemu_can_send_packet_nolock(NetClientState *sender);
 int qemu_can_send_packet(NetClientState *nc);
 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
                           int iovcnt);
net/net.c +75 -4

171 lines not on this page · raw message

net/queue.c +2 -2
index 2856c1d..123c338 100644
@@ -190,7 +190,7 @@ ssize_t qemu_net_queue_send(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
         qemu_net_queue_append(queue, sender, flags, data, size, sent_cb);
         return 0;
     }
@@ -215,7 +215,7 @@ ssize_t qemu_net_queue_send_iov(NetQueue *queue,
 {
     ssize_t ret;
 
-    if (queue->delivering || !qemu_can_send_packet(sender)) {
+    if (queue->delivering || !qemu_can_send_packet_nolock(sender)) {
         qemu_net_queue_append_iov(queue, sender, flags, iov, iovcnt, sent_cb);
         return 0;
     }
Signature · 1 line
1.7.4.4
#21 lore raw

RFC PATCH v4 11/15 net: make netclient re-entrant with refcnt

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

With refcnt, NetClientState's user can run agaist deleter.

---
 hw/qdev-properties-system.c |   14 +++++++++++++
 include/net/net.h           |    3 ++
 net/hub.c                   |    3 ++
 net/net.c                   |   46 ++++++++++++++++++++++++++++++++++++++++--
 net/slirp.c                 |    3 +-
 5 files changed, 65 insertions(+), 4 deletions(-)
hw/qdev-properties-system.c +14 -0
index ce3af22..14c6d49 100644
@@ -301,6 +301,7 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
         return;
     }
 
+    /* inc ref, released when unset property */
     hubport = net_hub_port_find(id);
     if (!hubport) {
         error_set(errp, QERR_INVALID_PARAMETER_VALUE,
@@ -310,11 +311,24 @@ static void set_vlan(Object *obj, Visitor *v, void *opaque,
     *ptr = hubport;
 }
 
+static void release_vlan(Object *obj, const char *name, void *opaque)
+{
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    NICPeers *peers_ptr = qdev_get_prop_ptr(dev, prop);
+    NetClientState **ptr = &peers_ptr->ncs[0];
+
+    if (*ptr) {
+        netclient_unref(*ptr);
+    }
+}
+
 PropertyInfo qdev_prop_vlan = {
     .name  = "vlan",
     .print = print_vlan,
     .get   = get_vlan,
     .set   = set_vlan,
+    .release = release_vlan,
 };
 
 int qdev_prop_set_drive(DeviceState *dev, const char *name,
include/net/net.h +3 -0
index 54f91ea..ef4137d 100644
@@ -61,6 +61,7 @@ typedef struct NetClientInfo {
 } NetClientInfo;
 
 struct NetClientState {
+    int ref;
     NetClientInfo *info;
     int link_down;
     QTAILQ_ENTRY(NetClientState) next;
@@ -89,6 +90,8 @@ typedef struct NICState {
 NetClientState *qemu_find_netdev(const char *id);
 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
                                  NetClientOptionsKind type, int max);
+void netclient_ref(NetClientState *nc);
+void netclient_unref(NetClientState *nc);
 NetClientState *qemu_new_net_client(NetClientInfo *info,
                                     NetClientState *peer,
                                     const char *model,
net/hub.c +3 -0
index 812a6dc..2970f8e 100644
@@ -212,6 +212,7 @@ NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
                 peer = port->nc.peer;
 
                 if (peer && strcmp(peer->name, name) == 0) {
+                    netclient_ref(peer);
                     qemu_mutex_unlock(&hub->ports_lock);
                     return peer;
                 }
@@ -237,6 +238,7 @@ NetClientState *net_hub_port_find(int hub_id)
             QLIST_FOREACH(port, &hub->ports, next) {
                 nc = port->nc.peer;
                 if (!nc) {
+                    netclient_ref(&port->nc);
                     qemu_mutex_unlock(&hub->ports_lock);
                     return &(port->nc);
                 }
@@ -247,6 +249,7 @@ NetClientState *net_hub_port_find(int hub_id)
     }
 
     nc = net_hub_add_port(hub_id, NULL);
+    netclient_ref(nc);
     return nc;
 }
 
net/net.c +43 -3

203 lines not on this page · raw message

net/slirp.c +2 -1
index 4df550f..a6116d5 100644
@@ -346,7 +346,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict)
 
     err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
                                host_addr, host_port);
-
+    netclient_unref(&s->nc);
     monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
                    err ? "not found" : "removed");
     return;
@@ -437,6 +437,7 @@ void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict)
     }
     if (s) {
         slirp_hostfwd(s, redir_str, 0);
+        netclient_unref(&s->nc);
     }
 
 }
Signature · 1 line
1.7.4.4
#22 lore raw

RFC PATCH v4 12/15 slirp: make timeout local

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Each slirp has its own time to caculate timeout.

---
 slirp/slirp.c |   22 ++++++++++------------
 slirp/slirp.h |    3 +++
 2 files changed, 13 insertions(+), 12 deletions(-)
slirp/slirp.c +10 -12
index bd9b7cb..5f1c5e8 100644
@@ -40,8 +40,6 @@ static const uint8_t special_ethaddr[ETH_ALEN] = {
 static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
 
 u_int curtime;
-static u_int time_fasttimo, last_slowtimo;
-static int do_slowtimo;
 
 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
@@ -278,14 +276,14 @@ void slirp_pollfds_fill(GArray *pollfds)
     /*
      * First, TCP sockets
      */
-    do_slowtimo = 0;
 
     QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
         /*
          * *_slowtimo needs calling if there are IP fragments
          * in the fragment queue, or there are TCP connections active
          */
-        do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
+        slirp->do_slowtimo = 0;
+        slirp->do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
                 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
 
         for (so = slirp->tcb.so_next; so != &slirp->tcb;
@@ -299,8 +297,8 @@ void slirp_pollfds_fill(GArray *pollfds)
             /*
              * See if we need a tcp_fasttimo
              */
-            if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) {
-                time_fasttimo = curtime; /* Flag when we want a fasttimo */
+            if (slirp->time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) {
+                slirp->time_fasttimo = curtime; /* Flag when we want a fasttimo */
             }
 
             /*
@@ -381,7 +379,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     udp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = 1; /* Let socket expire */
                 }
             }
 
@@ -422,7 +420,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     icmp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = 1; /* Let socket expire */
                 }
             }
 
@@ -454,14 +452,14 @@ void slirp_pollfds_poll(GArray *pollfds, int select_error)
         /*
          * See if anything has timed out
          */
-        if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
+        if (slirp->time_fasttimo && ((curtime - slirp->time_fasttimo) >= 2)) {
             tcp_fasttimo(slirp);
-            time_fasttimo = 0;
+            slirp->time_fasttimo = 0;
         }
-        if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
+        if (slirp->do_slowtimo && ((curtime - slirp->last_slowtimo) >= 499)) {
             ip_slowtimo(slirp);
             tcp_slowtimo(slirp);
-            last_slowtimo = curtime;
+            slirp->last_slowtimo = curtime;
         }
 
         /*
slirp/slirp.h +3 -0
index fe0e65d..008360e 100644
@@ -203,6 +203,9 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
 
 struct Slirp {
     QTAILQ_ENTRY(Slirp) entry;
+    u_int time_fasttimo;
+    u_int last_slowtimo;
+    int do_slowtimo;
 
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
Signature · 1 line
1.7.4.4
#24 lore raw

RFC PATCH v4 13/15 slirp: make slirp event dispatch based on slirp instance, not global

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Split slirp_pollfds_fill/poll actions into each slirp, so that SlirpState can run on dedicated context.
Each slirp socket will corresponds to a GPollFD, and its SlirpState stands for a GSource(EventsGSource). Finally different SlirpState can run on different context.

net: port slirp to glib

---
 main-loop.c      |    4 -
 net/slirp.c      |   44 ++++
 slirp/libslirp.h |    7 +-
 slirp/slirp.c    |  587 +++++++++++++++++++++++++-----------------------------
 slirp/socket.c   |    2 +
 slirp/socket.h   |    1 +
 stubs/slirp.c    |    8 -
 7 files changed, 321 insertions(+), 332 deletions(-)
main-loop.c +0 -4
index 8c9b58c..970f25d 100644
@@ -432,14 +432,10 @@ int main_loop_wait(int nonblocking)
     /* XXX: separate device handlers from system ones */
 #ifdef CONFIG_SLIRP
     slirp_update_timeout(&timeout);
-    slirp_pollfds_fill(gpollfds);
 #endif
     qemu_iohandler_fill(gpollfds);
     ret = os_host_main_loop_wait(timeout);
     qemu_iohandler_poll(gpollfds, ret);
-#ifdef CONFIG_SLIRP
-    slirp_pollfds_poll(gpollfds, (ret < 0));
-#endif
 
     qemu_run_all_timers();
 
net/slirp.c +44 -0

85 lines not on this page · raw message

slirp/libslirp.h +4 -3
index ceabff8..1aad5a4 100644
@@ -17,11 +17,10 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 void slirp_cleanup(Slirp *slirp);
 
 void slirp_update_timeout(uint32_t *timeout);
-void slirp_pollfds_fill(GArray *pollfds);
-
-void slirp_pollfds_poll(GArray *pollfds, int select_error);
 
 void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len);
+gboolean slirp_prepare(GSource *source, gint *time);
+gboolean slirp_handler(gpointer data);
 
 /* you must provide the following functions: */
 void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len);
@@ -40,5 +39,7 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr,
                        int guest_port, const uint8_t *buf, int size);
 size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
                              int guest_port);
+GPollFD *slirp_gsource_get_gfd(void *opaque, int fd);
+void slirp_gsource_close_gfd(void *opaque, GPollFD *pollfd);
 
 #endif
slirp/slirp.c +270 -317

662 lines not on this page · raw message

slirp/socket.c +2 -0
index bb639ae..058d2e3 100644
@@ -52,6 +52,7 @@ socreate(Slirp *slirp)
     so->s = -1;
     so->slirp = slirp;
     so->pollfds_idx = -1;
+    so->pollfd = slirp_gsource_get_gfd(slirp->opaque, so->s);
   }
   return(so);
 }
@@ -64,6 +65,7 @@ sofree(struct socket *so)
 {
   Slirp *slirp = so->slirp;
 
+  slirp_gsource_close_gfd(slirp->opaque, so->pollfd);
   if (so->so_emu==EMU_RSH && so->extra) {
 	sofree(so->extra);
 	so->extra=NULL;
slirp/socket.h +1 -0
index 57e0407..522c5f0 100644
@@ -21,6 +21,7 @@ struct socket {
   int s;                           /* The actual socket */
 
   int pollfds_idx;                 /* GPollFD GArray index */
+  GPollFD *pollfd;
 
   Slirp *slirp;			   /* managing slirp instance */
 
stubs/slirp.c +0 -8
index f1fc833..c343364 100644
@@ -5,11 +5,3 @@ void slirp_update_timeout(uint32_t *timeout)
 {
 }
 
-void slirp_pollfds_fill(GArray *pollfds)
-{
-}
-
-void slirp_pollfds_poll(GArray *pollfds, int select_error)
-{
-}
-
Signature · 1 line
1.7.4.4
#25 lore raw

RFC PATCH v4 14/15 slirp: handle race condition

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

---
 slirp/slirp.c |   16 ++++++++++++++--
 slirp/slirp.h |    3 +++
 2 files changed, 17 insertions(+), 2 deletions(-)
slirp/slirp.c +14 -2
index 883b7bd..6bfcc67 100644
@@ -206,6 +206,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     slirp_init_once();
 
+    qemu_mutex_init(&slirp->lock);
     slirp->restricted = restricted;
 
     if_init(slirp);
@@ -248,6 +249,7 @@ void slirp_cleanup(Slirp *slirp)
 
     ip_cleanup(slirp);
     m_cleanup(slirp);
+    qemu_mutex_destroy(&slirp->lock);
 
     g_free(slirp->vdnssearch);
     g_free(slirp->tftp_prefix);
@@ -411,6 +413,7 @@ gboolean slirp_handler(gpointer data)
     struct socket *so, *so_next;
     int ret;
 
+    qemu_mutex_lock(&slirp->lock);
     /*
      * See if anything has timed out
      */
@@ -594,6 +597,7 @@ gboolean slirp_handler(gpointer data)
     }
 
     if_start(slirp);
+    qemu_mutex_unlock(&slirp->lock);
     return true;
 }
 
@@ -665,6 +669,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
         return;
 
     proto = ntohs(*(uint16_t *)(pkt + 12));
+    qemu_mutex_lock(&slirp->lock);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);
@@ -688,6 +693,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     default:
         break;
     }
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
@@ -860,15 +866,21 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size)
 {
     int ret;
-    struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
+    struct socket *so;
+
+    qemu_mutex_lock(&slirp->lock);
+    so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
 
-    if (!so)
+    if (!so) {
+        qemu_mutex_unlock(&slirp->lock);
         return;
+    }
 
     ret = soreadbuf(so, (const char *)buf, size);
 
     if (ret > 0)
         tcp_output(sototcpcb(so));
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
slirp/slirp.h +3 -0
index 008360e..7ab0c70 100644
@@ -135,6 +135,7 @@ void free(void *ptr);
 
 #include "qemu/queue.h"
 #include "qemu/sockets.h"
+#include "qemu/thread.h"
 
 #include "libslirp.h"
 #include "ip.h"
@@ -207,6 +208,8 @@ struct Slirp {
     u_int last_slowtimo;
     int do_slowtimo;
 
+    /* lock to protect slirp running both on frontend or SlirpState context */
+    QemuMutex lock;
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
     struct in_addr vnetwork_mask;
Signature · 1 line
1.7.4.4
#31 lore raw

RFC PATCH v4 15/15 slirp: use lock to protect the slirp_instances

In reply to Liu Ping Fan

From: Liu Ping Fan <[email protected]>

slirps will run on dedicated thread, so need to protect the global list.

---
 include/qemu/module.h |    2 ++
 slirp/slirp.c         |   20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)
include/qemu/module.h +2 -0
index c4ccd57..2720943 100644
@@ -22,6 +22,7 @@ static void __attribute__((constructor)) do_qemu_init_ ## function(void) {  \
 
 typedef enum {
     MODULE_INIT_BLOCK,
+    MODULE_INIT_SLIRP,
     MODULE_INIT_MACHINE,
     MODULE_INIT_QAPI,
     MODULE_INIT_QOM,
@@ -29,6 +30,7 @@ typedef enum {
 } module_init_type;
 
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
+#define slirplayer_init(function) module_init(function, MODULE_INIT_SLIRP)
 #define machine_init(function) module_init(function, MODULE_INIT_MACHINE)
 #define qapi_init(function) module_init(function, MODULE_INIT_QAPI)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
slirp/slirp.c +20 -0
index 6bfcc67..4cbf04d 100644
@@ -42,6 +42,7 @@ static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
 
 u_int curtime;
 
+static QemuMutex slirp_instances_lock;
 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
 
@@ -236,14 +237,18 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
     register_savevm(NULL, "slirp", 0, 3,
                     slirp_state_save, slirp_state_load, slirp);
 
+    qemu_mutex_lock(&slirp_instances_lock);
     QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
+    qemu_mutex_unlock(&slirp_instances_lock);
 
     return slirp;
 }
 
 void slirp_cleanup(Slirp *slirp)
 {
+    qemu_mutex_lock(&slirp_instances_lock);
     QTAILQ_REMOVE(&slirp_instances, slirp, entry);
+    qemu_mutex_unlock(&slirp_instances_lock);
 
     unregister_savevm(NULL, "slirp", slirp);
 
@@ -262,9 +267,12 @@ void slirp_cleanup(Slirp *slirp)
 
 void slirp_update_timeout(uint32_t *timeout)
 {
+    qemu_mutex_lock(&slirp_instances_lock);
     if (!QTAILQ_EMPTY(&slirp_instances)) {
         *timeout = MIN(1000, *timeout);
     }
+    qemu_mutex_unlock(&slirp_instances_lock);
+
     curtime = qemu_get_clock_ms(rt_clock);
 }
 
@@ -1140,3 +1148,15 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
 
     return 0;
 }
+
+static void slirplayer_cleanup(void)
+{
+    qemu_mutex_destroy(&slirp_instances_lock);
+}
+
+static void slirplayer_bootup(void)
+{
+    qemu_mutex_init(&slirp_instances_lock);
+    atexit(&slirplayer_cleanup);
+}
+slirplayer_init(slirplayer_bootup)
Signature · 1 line
1.7.4.4
#26 lore raw

Re: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition

In reply to Liu Ping Fan
Quoted 4 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

What are the usage rules for this lock, what precisely is it protecting? Is it ensured that we do not take the BQL while holding this one?

Jan

Quoted 105 lines
---
 slirp/slirp.c |   16 ++++++++++++++--
 slirp/slirp.h |    3 +++
 2 files changed, 17 insertions(+), 2 deletions(-)
slirp/slirp.c +14 -2
index 883b7bd..6bfcc67 100644
@@ -206,6 +206,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     slirp_init_once();
 
+    qemu_mutex_init(&slirp->lock);
     slirp->restricted = restricted;
 
     if_init(slirp);
@@ -248,6 +249,7 @@ void slirp_cleanup(Slirp *slirp)
 
     ip_cleanup(slirp);
     m_cleanup(slirp);
+    qemu_mutex_destroy(&slirp->lock);
 
     g_free(slirp->vdnssearch);
     g_free(slirp->tftp_prefix);
@@ -411,6 +413,7 @@ gboolean slirp_handler(gpointer data)
     struct socket *so, *so_next;
     int ret;
 
+    qemu_mutex_lock(&slirp->lock);
     /*
      * See if anything has timed out
      */
@@ -594,6 +597,7 @@ gboolean slirp_handler(gpointer data)
     }
 
     if_start(slirp);
+    qemu_mutex_unlock(&slirp->lock);
     return true;
 }
 
@@ -665,6 +669,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
         return;
 
     proto = ntohs(*(uint16_t *)(pkt + 12));
+    qemu_mutex_lock(&slirp->lock);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);
@@ -688,6 +693,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     default:
         break;
     }
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
@@ -860,15 +866,21 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size)
 {
     int ret;
-    struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
+    struct socket *so;
+
+    qemu_mutex_lock(&slirp->lock);
+    so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
 
-    if (!so)
+    if (!so) {
+        qemu_mutex_unlock(&slirp->lock);
         return;
+    }
 
     ret = soreadbuf(so, (const char *)buf, size);
 
     if (ret > 0)
         tcp_output(sototcpcb(so));
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
slirp/slirp.h +3 -0
index 008360e..7ab0c70 100644
@@ -135,6 +135,7 @@ void free(void *ptr);
 
 #include "qemu/queue.h"
 #include "qemu/sockets.h"
+#include "qemu/thread.h"
 
 #include "libslirp.h"
 #include "ip.h"
@@ -207,6 +208,8 @@ struct Slirp {
     u_int last_slowtimo;
     int do_slowtimo;
 
+    /* lock to protect slirp running both on frontend or SlirpState context */
+    QemuMutex lock;
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
     struct in_addr vnetwork_mask;
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#32 lore raw

Re: [Qemu-devel] [RFC PATCH v4 15/15] slirp: use lock to protect the slirp_instances

In reply to Liu Ping Fan
Quoted 82 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

slirps will run on dedicated thread, so need to protect the global list.

---
 include/qemu/module.h |    2 ++
 slirp/slirp.c         |   20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)
include/qemu/module.h +2 -0
index c4ccd57..2720943 100644
@@ -22,6 +22,7 @@ static void __attribute__((constructor)) do_qemu_init_ ## function(void) {  \
 
 typedef enum {
     MODULE_INIT_BLOCK,
+    MODULE_INIT_SLIRP,
     MODULE_INIT_MACHINE,
     MODULE_INIT_QAPI,
     MODULE_INIT_QOM,
@@ -29,6 +30,7 @@ typedef enum {
 } module_init_type;
 
 #define block_init(function) module_init(function, MODULE_INIT_BLOCK)
+#define slirplayer_init(function) module_init(function, MODULE_INIT_SLIRP)
 #define machine_init(function) module_init(function, MODULE_INIT_MACHINE)
 #define qapi_init(function) module_init(function, MODULE_INIT_QAPI)
 #define type_init(function) module_init(function, MODULE_INIT_QOM)
slirp/slirp.c +10 -0
index 6bfcc67..4cbf04d 100644
@@ -42,6 +42,7 @@ static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
 
 u_int curtime;
 
+static QemuMutex slirp_instances_lock;
 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
 
@@ -236,14 +237,18 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
     register_savevm(NULL, "slirp", 0, 3,
                     slirp_state_save, slirp_state_load, slirp);
 
+    qemu_mutex_lock(&slirp_instances_lock);
     QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
+    qemu_mutex_unlock(&slirp_instances_lock);
 
     return slirp;
 }
 
 void slirp_cleanup(Slirp *slirp)
 {
+    qemu_mutex_lock(&slirp_instances_lock);
     QTAILQ_REMOVE(&slirp_instances, slirp, entry);
+    qemu_mutex_unlock(&slirp_instances_lock);
 
     unregister_savevm(NULL, "slirp", slirp);
 
@@ -262,9 +267,12 @@ void slirp_cleanup(Slirp *slirp)
 
 void slirp_update_timeout(uint32_t *timeout)
 {
+    qemu_mutex_lock(&slirp_instances_lock);
     if (!QTAILQ_EMPTY(&slirp_instances)) {
         *timeout = MIN(1000, *timeout);
     }
+    qemu_mutex_unlock(&slirp_instances_lock);
+
     curtime = qemu_get_clock_ms(rt_clock);
 }
 
@@ -1140,3 +1148,15 @@ static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
 
     return 0;
 }
+
+static void slirplayer_cleanup(void)

slirp/slirp.c static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) · line 1162

Quoted 10 lines
slirp/slirp.c +10 -0
+{
+    qemu_mutex_destroy(&slirp_instances_lock);
+}
+
+static void slirplayer_bootup(void)
+{
+    qemu_mutex_init(&slirp_instances_lock);
+    atexit(&slirplayer_cleanup);
+}
+slirplayer_init(slirplayer_bootup)

grep'ing for slirp_instances points to more spots that work with that list (QTAILQ_FOREACH, QTAILQ_EMPTY, ...). So the same question here:
What are the usage rules? When do I _not_ need it when touching the list of instances, and why?

Well, I started reading at the top, but there are more lock-adding patches in this series. And the more locks we have, the higher the probability of ABBA gets. Therefore, please document from the beginning the lock order rules that shall prevent it (which may also be "never take other locks while holding this one" or "never hold other locks when taking this one").

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v4 01/15] util: introduce gsource event abstration

In reply to Liu Ping Fan
Quoted 4 lines · Liu Ping Fan
util/event_gsource.c new file +4 -0
+static gboolean prepare(GSource *src, gint *time)
+{
+    EventGSource *nsrc = (EventGSource *)src;
+    int events = 0;

util/event_gsource.c line 33

Quoted 10 lines
util/event_gsource.c new file +10 -0
+
+    if (!nsrc->readable && !nsrc->writable) {
+        return false;
+    }
+    if (nsrc->readable && nsrc->readable(nsrc->opaque)) {
+        events |= G_IO_IN;
+    }
+    if ((nsrc->writable) && nsrc->writable(nsrc->opaque)) {
+        events |= G_IO_OUT;
+    }

G_IO_ERR, G_IO_HUP, G_IO_PRI?

Here is the select(2) to GCondition mapping:
rfds -> G_IO_IN | G_IO_HUP | G_IO_ERR
wfds -> G_IO_OUT | G_IO_ERR
xfds -> G_IO_PRI

In other words, we're missing events by just using G_IO_IN and G_IO_OUT. Whether that matters depends on EventGSource users. For sockets it can matter.

util/event_gsource.c line 80

Quoted 3 lines
util/event_gsource.c new file +3 -0
+void event_source_release(EventGSource *src)
+{
+    g_source_destroy(&src->source);

Leaks src.

util/event_gsource.c line 83

Quoted 3 lines
util/event_gsource.c new file +3 -0
+}
+
+GPollFD *events_source_get_gfd(EventsGSource *src, int fd)

events_source_add_fd() seems like a better name since this function always allocates a new GPollFD, it never "gets" an existing one.

util/event_gsource.c line 90

Quoted 7 lines
util/event_gsource.c new file +7 -0
+{
+    GPollFD *retfd;
+    unsigned long idx;
+
+    idx = find_first_zero_bit(src->alloc_bmp, src->bmp_sz);
+    if (idx == src->bmp_sz) {
+        //idx = src->bmp_sz;

Commented out line.

Quoted 1 line

+void events_source_close_gfd(EventsGSource *src, GPollFD *pollfd)

"close" usually means close(2). I suggest "remove" instead.

util/event_gsource.c line 154

Quoted 7 lines
util/event_gsource.c new file +7 -0
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque)
+{
+    EventsGSource *src = (EventsGSource *)g_source_new(funcs, sizeof(EventsGSource));
+
+    /* 8bits size at initial */
+    src->bmp_sz = 8;
+    src->alloc_bmp = g_malloc0(src->bmp_sz >> 3);

This is unportable. alloc_bmp is unsigned long, you are allocating just one byte!

Please drop the bitmap approach and use a doubly-linked list or another glib container type of your choice. It needs 3 operations: add, remove, and iterate.

Quoted 6 lines
util/event_gsource.h new file +6 -0
+/* multi fd drive gsource*/
+typedef struct EventsGSource {
+    GSource source;
+    /* 8 for initial, stand for 8 pollfds */
+    unsigned int bmp_sz;
+    unsigned long *alloc_bmp;

util/event_gsource.h line 50

Quoted 10 lines
util/event_gsource.h new file +10 -0
+    GPollFD *pollfds;
+    void *opaque;
+} EventsGSource;
+
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque);
+void events_source_release(EventsGSource *src);
+gboolean events_source_check(GSource *src);
+gboolean events_source_dispatch(GSource *src, GSourceFunc cb, gpointer data);
+GPollFD *events_source_get_gfd(EventsGSource *src, int fd);
+void events_source_close_gfd(EventsGSource *src, GPollFD *pollfd);

Why are check/dispatch public? Perhaps events_source_new() just needs a prepare() argument instead of exposing GSourceFuncs.

#9 lore raw

Re: [Qemu-devel] [RFC PATCH v4 04/15] net: resolve race of tap backend and its peer

In reply to Liu Ping Fan
Quoted 23 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

When vhost net enabled, we should be sure that the user space fd handler is not in flight

---
 net/tap.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
net/tap.c +1 -0
index 35cbb6e..b5629e3 100644
@@ -41,6 +41,7 @@
 #include "qemu/error-report.h"
 
 #include "net/tap.h"
+#include "util/event_gsource.h"
 
 #include "hw/vhost_net.h"
 

net/tap.c static void tap_poll(NetClientState *nc, bool enable) · line 334

Quoted 7 lines
net/tap.c +4 -0
@@ -327,6 +328,10 @@ static void tap_poll(NetClientState *nc, bool enable)
     tap_read_poll(s, enable);
     tap_write_poll(s, enable);
 
+    if (!enable) {
+        /* need sync so vhost can take over polling */
+        g_source_remove_poll(&s->nsrc->source, &s->nsrc->gfd);
+    }

We must also re-enable it when .poll(nc, true) is called.

Please drop the comments the previous patch added. In fact, this patch can be squashed.

I think there was another vhost sync comment which you haven't addressed here. See the previous patch.

Stefan

#33 lore raw

Re: [Qemu-devel] [RFC PATCH v4 15/15] slirp: use lock to protect the slirp_instances

In reply to Jan Kiszka
Quoted 6 lines

grep'ing for slirp_instances points to more spots that work with that list (QTAILQ_FOREACH, QTAILQ_EMPTY, ...). So the same question here:
What are the usage rules? When do I _not_ need it when touching the list of instances, and why?

Quoted 6 lines

Well, I started reading at the top, but there are more lock-adding patches in this series. And the more locks we have, the higher the probability of ABBA gets. Therefore, please document from the beginning the lock order rules that shall prevent it (which may also be "never take other locks while holding this one" or "never hold other locks when taking this one").

Yeah, the only sane ordering rules should be "hold nothing or just the BQL when taking this one". Everything else needs a very good justification...

Paolo

#23 lore raw

Re: [Qemu-devel] [RFC PATCH v4 12/15] slirp: make timeout local

In reply to Liu Ping Fan
Quoted 4 lines

From: Liu Ping Fan <[email protected]>

Each slirp has its own time to caculate timeout.

Nice cleanup to have anyway upstream.

Quoted 24 lines
---
 slirp/slirp.c |   22 ++++++++++------------
 slirp/slirp.h |    3 +++
 2 files changed, 13 insertions(+), 12 deletions(-)
slirp/slirp.c +0 -3
index bd9b7cb..5f1c5e8 100644
@@ -40,8 +40,6 @@ static const uint8_t special_ethaddr[ETH_ALEN] = {
 static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
 
 u_int curtime;
-static u_int time_fasttimo, last_slowtimo;
-static int do_slowtimo;
 
 static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
@@ -278,14 +276,14 @@ void slirp_pollfds_fill(GArray *pollfds)
     /*
      * First, TCP sockets
      */
-    do_slowtimo = 0;

slirp/slirp.c void slirp_pollfds_fill(GArray *pollfds) · line 287

Quoted 10 lines
slirp/slirp.c +2 -1
 
     QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
         /*
          * *_slowtimo needs calling if there are IP fragments
          * in the fragment queue, or there are TCP connections active
          */
-        do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
+        slirp->do_slowtimo = 0;
+        slirp->do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
                 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));

No need to do = 0 here.

Paolo

Quoted 71 lines
slirp/slirp.c +8 -8
         for (so = slirp->tcb.so_next; so != &slirp->tcb;
@@ -299,8 +297,8 @@ void slirp_pollfds_fill(GArray *pollfds)
             /*
              * See if we need a tcp_fasttimo
              */
-            if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) {
-                time_fasttimo = curtime; /* Flag when we want a fasttimo */
+            if (slirp->time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) {
+                slirp->time_fasttimo = curtime; /* Flag when we want a fasttimo */
             }
 
             /*
@@ -381,7 +379,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     udp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = 1; /* Let socket expire */
                 }
             }
 
@@ -422,7 +420,7 @@ void slirp_pollfds_fill(GArray *pollfds)
                     icmp_detach(so);
                     continue;
                 } else {
-                    do_slowtimo = 1; /* Let socket expire */
+                    slirp->do_slowtimo = 1; /* Let socket expire */
                 }
             }
 
@@ -454,14 +452,14 @@ void slirp_pollfds_poll(GArray *pollfds, int
 select_error)
         /*
          * See if anything has timed out
          */
-        if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
+        if (slirp->time_fasttimo && ((curtime - slirp->time_fasttimo) >= 2))
 {
             tcp_fasttimo(slirp);
-            time_fasttimo = 0;
+            slirp->time_fasttimo = 0;
         }
-        if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
+        if (slirp->do_slowtimo && ((curtime - slirp->last_slowtimo) >= 499))
 {
             ip_slowtimo(slirp);
             tcp_slowtimo(slirp);
-            last_slowtimo = curtime;
+            slirp->last_slowtimo = curtime;
         }
 
         /*
slirp/slirp.h +3 -1
index fe0e65d..008360e 100644
@@ -203,6 +203,9 @@ bool arp_table_search(Slirp *slirp, uint32_t ip_addr,
 
 struct Slirp {
     QTAILQ_ENTRY(Slirp) entry;
+    u_int time_fasttimo;
+    u_int last_slowtimo;
+    int do_slowtimo;
 
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
--

1.7.4.4

#13 lore raw

Re: [Qemu-devel] [RFC PATCH v4 06/15] net: port socket to GSource

In reply to Liu Ping Fan

net/socket.c static void net_socket_send(void *opaque) · line 163

Quoted 12 lines · Liu Ping Fan
net/socket.c +7 -1
@@ -160,7 +154,13 @@ static void net_socket_send(void *opaque)
         net_socket_read_poll(s, false);
         net_socket_write_poll(s, false);
         if (s->listen_fd != -1) {
-            qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+            nsrc = s->nsrc;
+            new_nsrc = event_source_new(s->listen_fd, net_socket_listen_handler,
+                                s);
+            s->nsrc = new_nsrc;
+            new_nsrc->gfd.events = G_IO_IN;
+            g_source_destroy(&nsrc->source);
+            s->nc.info->bind_ctx(&s->nc, NULL);

The following is equivalent:

event_source_release(s->nsrc);
s->nsrc = event_source_new(s->listen_fd, net_socket_listen_handler, s); s->nc.info->bind_ctx(&s->nc, NULL);

Then new_nsrc/nsrc can be dropped and the nsrc memory leak is avoided.

Note that gfd.events = G_IO_IN does not get used since prepare() overwrites gfd.events. Please drop and make sure read_poll == true.

I'm a little worried that we're lacking G_IO_HUP | G_IO_ERR. Perhaps disconnect and network errors will be ignored.

Quoted 50 lines
net/socket.c +23 -0
         }
         closesocket(s->fd);
 
@@ -331,6 +331,14 @@ static void net_socket_cleanup(NetClientState *nc)
         closesocket(s->listen_fd);
         s->listen_fd = -1;
     }
+    event_source_release(s->nsrc);
+}
+
+static void net_socket_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -338,8 +346,22 @@ static NetClientInfo net_dgram_socket_info = {
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive_dgram,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,
 };
 
+static gboolean net_socket_dgram_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send_dgram(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
                                                 const char *model,
                                                 const char *name,
@@ -350,6 +372,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     socklen_t saddr_len;
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
      * Because this may be "shared" socket from a "master" process, datagrams would be recv()

net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 421

Quoted 7 lines
net/socket.c +3 -1
@@ -393,7 +416,10 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
 
     s->fd = fd;
     s->listen_fd = -1;
-    s->send_fn = net_socket_send_dgram;
+    nsrc = event_source_new(fd, net_socket_dgram_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 21 lines
net/socket.c +2 -7
+    nc->info->bind_ctx(nc, NULL);
     net_socket_read_poll(s, true);
 
     /* mcast: save bound address as dst */
@@ -408,20 +434,28 @@ err:
     return NULL;
 }
 
-static void net_socket_connect(void *opaque)
-{
-    NetSocketState *s = opaque;
-    s->send_fn = net_socket_send;
-    net_socket_read_poll(s, true);
-}
-
 static NetClientInfo net_socket_info = {
     .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,

net/socket.c err: · line 452

Quoted 10 lines
net/socket.c +8 -0
 };
 
+static gboolean net_socket_connect_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
+
+    new_nsrc = event_source_new(s->fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 18 lines
net/socket.c +7 -0
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
                                                  const char *model,
                                                  const char *name,
@@ -429,6 +463,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
 

net/socket.c static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, · line 480

Quoted 7 lines
net/socket.c +3 -1
@@ -440,9 +475,16 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
     s->listen_fd = -1;
 
     if (is_connected) {
-        net_socket_connect(s);
+        nsrc = event_source_new(fd, net_socket_establish_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

net/socket.c static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, · line 485

Quoted 6 lines
net/socket.c +4 -1
+        nc->info->bind_ctx(nc, NULL);
     } else {
-        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
+        nsrc = event_source_new(fd, net_socket_connect_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN;

Please drop.

Quoted 62 lines
net/socket.c +39 -10
+        nc->info->bind_ctx(nc, NULL);
+
     }
     return s;
 }
@@ -473,30 +515,69 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
     return NULL;
 }
 
-static void net_socket_accept(void *opaque)
+static gboolean net_socket_establish_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
+static bool readable(void *opaque)
 {
     NetSocketState *s = opaque;
+
+    if (s->read_poll && net_socket_can_send(s)) {
+        return true;
+    }
+    return false;
+}
+
+static bool writable(void *opaque)
+{
+    NetSocketState *s = opaque;
+
+    if (s->write_poll) {
+        return true;
+    }
+    return false;
+}
+
+static gboolean net_socket_listen_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
     struct sockaddr_in saddr;
     socklen_t len;
     int fd;
 
-    for(;;) {
-        len = sizeof(saddr);
-        fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
-        if (fd < 0 && errno != EINTR) {
-            return;
-        } else if (fd >= 0) {
-            qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
-            break;
-        }
+    len = sizeof(saddr);
+    fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);

net/socket.c static NetSocketState *net_socket_fd_init(NetClientState *peer, · line 569

Quoted 10 lines
net/socket.c +5 -1
+    if (fd < 0 && errno != EINTR) {
+        return false;
     }
 
     s->fd = fd;
     s->nc.link_down = false;
-    net_socket_connect(s);
+    new_nsrc = event_source_new(fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 23 lines
net/socket.c +9 -0
+    new_nsrc->readable = readable;
+    new_nsrc->writable = writable;
+    /* prevent more than one connect req */
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+    net_socket_read_poll(s, true);
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
              "socket: connection from %s:%d",
              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+
+    return true;
 }
 
 static int net_socket_listen_init(NetClientState *peer,
@@ -508,6 +589,7 @@ static int net_socket_listen_init(NetClientState *peer,
     NetSocketState *s;
     struct sockaddr_in saddr;
     int fd, val, ret;
+    EventGSource *nsrc;
 
     if (parse_host_port(&saddr, host_str) < 0)
         return -1;

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 629

Quoted 7 lines
net/socket.c +3 -1
@@ -542,7 +624,11 @@ static int net_socket_listen_init(NetClientState *peer,
     s->listen_fd = fd;
     s->nc.link_down = true;
 
-    qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    nsrc = event_source_new(fd, net_socket_listen_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN;

Please drop.

#27 lore raw

Re: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition

In reply to Jan Kiszka
Quoted 9 lines · Jan Kiszka
Quoted 4 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

What are the usage rules for this lock, what precisely is it protecting? Is it ensured that we do not take the BQL while holding this one?

It protect the slirp state, since slirp can be touched by slrip_input called by frontend(ex, e1000), also it can be touched by its event handler. With this lock, we do not need BQL

Regards,
Pingfan

Quoted 111 lines

Jan

Quoted 97 lines
---
 slirp/slirp.c |   16 ++++++++++++++--
 slirp/slirp.h |    3 +++
 2 files changed, 17 insertions(+), 2 deletions(-)
slirp/slirp.c +14 -2
index 883b7bd..6bfcc67 100644
@@ -206,6 +206,7 @@ Slirp *slirp_init(int restricted, struct in_addr vnetwork,
 
     slirp_init_once();
 
+    qemu_mutex_init(&slirp->lock);
     slirp->restricted = restricted;
 
     if_init(slirp);
@@ -248,6 +249,7 @@ void slirp_cleanup(Slirp *slirp)
 
     ip_cleanup(slirp);
     m_cleanup(slirp);
+    qemu_mutex_destroy(&slirp->lock);
 
     g_free(slirp->vdnssearch);
     g_free(slirp->tftp_prefix);
@@ -411,6 +413,7 @@ gboolean slirp_handler(gpointer data)
     struct socket *so, *so_next;
     int ret;
 
+    qemu_mutex_lock(&slirp->lock);
     /*
      * See if anything has timed out
      */
@@ -594,6 +597,7 @@ gboolean slirp_handler(gpointer data)
     }
 
     if_start(slirp);
+    qemu_mutex_unlock(&slirp->lock);
     return true;
 }
 
@@ -665,6 +669,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
         return;
 
     proto = ntohs(*(uint16_t *)(pkt + 12));
+    qemu_mutex_lock(&slirp->lock);
     switch(proto) {
     case ETH_P_ARP:
         arp_input(slirp, pkt, pkt_len);
@@ -688,6 +693,7 @@ void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
     default:
         break;
     }
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 /* Output the IP packet to the ethernet device. Returns 0 if the packet must be
@@ -860,15 +866,21 @@ void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
                        const uint8_t *buf, int size)
 {
     int ret;
-    struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
+    struct socket *so;
+
+    qemu_mutex_lock(&slirp->lock);
+    so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
 
-    if (!so)
+    if (!so) {
+        qemu_mutex_unlock(&slirp->lock);
         return;
+    }
 
     ret = soreadbuf(so, (const char *)buf, size);
 
     if (ret > 0)
         tcp_output(sototcpcb(so));
+    qemu_mutex_unlock(&slirp->lock);
 }
 
 static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
slirp/slirp.h +1 -0
index 008360e..7ab0c70 100644
@@ -135,6 +135,7 @@ void free(void *ptr);
 
 #include "qemu/queue.h"
 #include "qemu/sockets.h"
+#include "qemu/thread.h"
 
 #include "libslirp.h"
 #include "ip.h"

slirp/slirp.h struct Slirp { · line 215

Quoted 8 lines
slirp/slirp.h +2 -0
@@ -207,6 +208,8 @@ struct Slirp {
     u_int last_slowtimo;
     int do_slowtimo;
 
+    /* lock to protect slirp running both on frontend or SlirpState context */
+    QemuMutex lock;
     /* virtual network configuration */
     struct in_addr vnetwork_addr;
     struct in_addr vnetwork_mask;

--
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE Corporate Competence Center Embedded Linux

#10 lore raw

Re: [Qemu-devel] [RFC PATCH v4 04/15] net: resolve race of tap backend and its peer

In reply to Stefan Hajnoczi
Quoted 27 lines · Stefan Hajnoczi
Quoted 26 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

When vhost net enabled, we should be sure that the user space fd handler is not in flight

---
 net/tap.c |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
net/tap.c +1 -0
index 35cbb6e..b5629e3 100644
@@ -41,6 +41,7 @@
 #include "qemu/error-report.h"
 
 #include "net/tap.h"
+#include "util/event_gsource.h"
 
 #include "hw/vhost_net.h"
 
@@ -327,6 +328,10 @@ static void tap_poll(NetClientState *nc, bool enable)
     tap_read_poll(s, enable);
     tap_write_poll(s, enable);
 
Quoted 7 lines

net/tap.c static void tap_poll(NetClientState *nc, bool enable) · line 334

Quoted 4 lines
net/tap.c +4 -0
+    if (!enable) {
+        /* need sync so vhost can take over polling */
+        g_source_remove_poll(&s->nsrc->source, &s->nsrc->gfd);
+    }

We must also re-enable it when .poll(nc, true) is called.

Yes, will fix it.

Quoted 3 lines

Please drop the comments the previous patch added. In fact, this patch can be squashed.

Ok.

Quoted 3 lines

I think there was another vhost sync comment which you haven't addressed here. See the previous patch.

It is related with multi-queue, and I check the code, it can be async.
I will drop the related comment in previous patch

Thanks, Pingfan

Quoted 1 line

Stefan

#14 lore raw

Re: [Qemu-devel] [RFC PATCH v4 06/15] net: port socket to GSource

In reply to Stefan Hajnoczi
Quoted 16 lines · Stefan Hajnoczi

net/socket.c static void net_socket_send(void *opaque) · line 163

Quoted 12 lines · Liu Ping Fan
net/socket.c +7 -1
@@ -160,7 +154,13 @@ static void net_socket_send(void *opaque)
         net_socket_read_poll(s, false);
         net_socket_write_poll(s, false);
         if (s->listen_fd != -1) {
-            qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+            nsrc = s->nsrc;
+            new_nsrc = event_source_new(s->listen_fd, net_socket_listen_handler,
+                                s);
+            s->nsrc = new_nsrc;
+            new_nsrc->gfd.events = G_IO_IN;
+            g_source_destroy(&nsrc->source);
+            s->nc.info->bind_ctx(&s->nc, NULL);

The following is equivalent:

Quoted 6 lines

event_source_release(s->nsrc);
s->nsrc = event_source_new(s->listen_fd, net_socket_listen_handler, s); s->nc.info->bind_ctx(&s->nc, NULL);

Then new_nsrc/nsrc can be dropped and the nsrc memory leak is avoided.

Apply here and the following same issues.

Quoted 3 lines

Note that gfd.events = G_IO_IN does not get used since prepare() overwrites gfd.events. Please drop and make sure read_poll == true.

Apply,

Quoted 3 lines

I'm a little worried that we're lacking G_IO_HUP | G_IO_ERR. Perhaps disconnect and network errors will be ignored.

NetSocketState can do limited things about these situation, perhaps, implement net_socket_can_receive() to export such message to frontend ?

Pingfan

Quoted 238 lines
Quoted 50 lines
net/socket.c +23 -0
         }
         closesocket(s->fd);
 
@@ -331,6 +331,14 @@ static void net_socket_cleanup(NetClientState *nc)
         closesocket(s->listen_fd);
         s->listen_fd = -1;
     }
+    event_source_release(s->nsrc);
+}
+
+static void net_socket_bind_ctx(NetClientState *nc, GMainContext *ctx)
+{
+    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
+
+    g_source_attach(&s->nsrc->source, ctx);
 }
 
 static NetClientInfo net_dgram_socket_info = {
@@ -338,8 +346,22 @@ static NetClientInfo net_dgram_socket_info = {
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive_dgram,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,
 };
 
+static gboolean net_socket_dgram_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send_dgram(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
                                                 const char *model,
                                                 const char *name,
@@ -350,6 +372,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
     socklen_t saddr_len;
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
      * Because this may be "shared" socket from a "master" process, datagrams would be recv()

net/socket.c static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, · line 421

Quoted 7 lines
net/socket.c +3 -1
@@ -393,7 +416,10 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
 
     s->fd = fd;
     s->listen_fd = -1;
-    s->send_fn = net_socket_send_dgram;
+    nsrc = event_source_new(fd, net_socket_dgram_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 21 lines
net/socket.c +2 -7
+    nc->info->bind_ctx(nc, NULL);
     net_socket_read_poll(s, true);
 
     /* mcast: save bound address as dst */
@@ -408,20 +434,28 @@ err:
     return NULL;
 }
 
-static void net_socket_connect(void *opaque)
-{
-    NetSocketState *s = opaque;
-    s->send_fn = net_socket_send;
-    net_socket_read_poll(s, true);
-}
-
 static NetClientInfo net_socket_info = {
     .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
     .size = sizeof(NetSocketState),
     .receive = net_socket_receive,
     .cleanup = net_socket_cleanup,
+    .bind_ctx = net_socket_bind_ctx,

net/socket.c err: · line 452

Quoted 10 lines
net/socket.c +8 -0
 };
 
+static gboolean net_socket_connect_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
+
+    new_nsrc = event_source_new(s->fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 18 lines
net/socket.c +7 -0
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+
+    return true;
+}
+
 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
                                                  const char *model,
                                                  const char *name,
@@ -429,6 +463,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
 {
     NetClientState *nc;
     NetSocketState *s;
+    EventGSource *nsrc;
 
     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
 

net/socket.c static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, · line 480

Quoted 7 lines
net/socket.c +3 -1
@@ -440,9 +475,16 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
     s->listen_fd = -1;
 
     if (is_connected) {
-        net_socket_connect(s);
+        nsrc = event_source_new(fd, net_socket_establish_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

net/socket.c static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, · line 485

Quoted 6 lines
net/socket.c +4 -1
+        nc->info->bind_ctx(nc, NULL);
     } else {
-        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
+        nsrc = event_source_new(fd, net_socket_connect_handler, s);
+        s->nsrc = nsrc;
+        nsrc->gfd.events = G_IO_IN;

Please drop.

Quoted 62 lines
net/socket.c +39 -10
+        nc->info->bind_ctx(nc, NULL);
+
     }
     return s;
 }
@@ -473,30 +515,69 @@ static NetSocketState *net_socket_fd_init(NetClientState *peer,
     return NULL;
 }
 
-static void net_socket_accept(void *opaque)
+static gboolean net_socket_establish_handler(gpointer data)
+{
+    EventGSource *nsrc = (EventGSource *)data;
+    NetSocketState *s = nsrc->opaque;
+
+    if (nsrc->gfd.revents & G_IO_IN) {
+        net_socket_send(s);
+    } else {
+        net_socket_writable(s);
+    }
+    return true;
+}
+
+static bool readable(void *opaque)
 {
     NetSocketState *s = opaque;
+
+    if (s->read_poll && net_socket_can_send(s)) {
+        return true;
+    }
+    return false;
+}
+
+static bool writable(void *opaque)
+{
+    NetSocketState *s = opaque;
+
+    if (s->write_poll) {
+        return true;
+    }
+    return false;
+}
+
+static gboolean net_socket_listen_handler(gpointer data)
+{
+    EventGSource *new_nsrc, *nsrc = data;
+    NetSocketState *s = nsrc->opaque;
     struct sockaddr_in saddr;
     socklen_t len;
     int fd;
 
-    for(;;) {
-        len = sizeof(saddr);
-        fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
-        if (fd < 0 && errno != EINTR) {
-            return;
-        } else if (fd >= 0) {
-            qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
-            break;
-        }
+    len = sizeof(saddr);
+    fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);

net/socket.c static NetSocketState *net_socket_fd_init(NetClientState *peer, · line 569

Quoted 10 lines
net/socket.c +5 -1
+    if (fd < 0 && errno != EINTR) {
+        return false;
     }
 
     s->fd = fd;
     s->nc.link_down = false;
-    net_socket_connect(s);
+    new_nsrc = event_source_new(fd, net_socket_establish_handler, s);
+    s->nsrc = new_nsrc;
+    new_nsrc->gfd.events = G_IO_IN|G_IO_OUT;

Please drop.

Quoted 23 lines
net/socket.c +9 -0
+    new_nsrc->readable = readable;
+    new_nsrc->writable = writable;
+    /* prevent more than one connect req */
+    g_source_destroy(&nsrc->source);
+    s->nc.info->bind_ctx(&s->nc, NULL);
+    net_socket_read_poll(s, true);
     snprintf(s->nc.info_str, sizeof(s->nc.info_str),
              "socket: connection from %s:%d",
              inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
+
+    return true;
 }
 
 static int net_socket_listen_init(NetClientState *peer,
@@ -508,6 +589,7 @@ static int net_socket_listen_init(NetClientState *peer,
     NetSocketState *s;
     struct sockaddr_in saddr;
     int fd, val, ret;
+    EventGSource *nsrc;
 
     if (parse_host_port(&saddr, host_str) < 0)
         return -1;

net/socket.c static int net_socket_listen_init(NetClientState *peer, · line 629

Quoted 7 lines
net/socket.c +3 -1
@@ -542,7 +624,11 @@ static int net_socket_listen_init(NetClientState *peer,
     s->listen_fd = fd;
     s->nc.link_down = true;
 
-    qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
+    nsrc = event_source_new(fd, net_socket_listen_handler, s);
+    s->nsrc = nsrc;
+    nsrc->gfd.events = G_IO_IN;

Please drop.

#34 lore raw

Re: [Qemu-devel] [RFC PATCH v4 15/15] slirp: use lock to protect the slirp_instances

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
Quoted 6 lines

grep'ing for slirp_instances points to more spots that work with that list (QTAILQ_FOREACH, QTAILQ_EMPTY, ...). So the same question here:
What are the usage rules? When do I _not_ need it when touching the list of instances, and why?

Quoted 7 lines
Quoted 6 lines

Well, I started reading at the top, but there are more lock-adding patches in this series. And the more locks we have, the higher the probability of ABBA gets. Therefore, please document from the beginning the lock order rules that shall prevent it (which may also be "never take other locks while holding this one" or "never hold other locks when taking this one").

slrip->lock is used to protect the frontend and backend to touch slirp's content at the same time. While slirp_instances_lock is used to protected the list ops only. For example, when the SlirpState's refcnt comes to zero(this can not happen with holding slirp->lock), finalize is called, and finally, slirp_cleanup is called to remove slrip from the list. The two locks have no overlap.

Regards, Pingfan

Quoted 4 lines

Yeah, the only sane ordering rules should be "hold nothing or just the BQL when taking this one". Everything else needs a very good justification...

Ok, will notice.
Thanks, Pingfan

Quoted 1 line

Paolo

#3 lore raw

Re: [Qemu-devel] [RFC PATCH v4 01/15] util: introduce gsource event abstration

In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi
Quoted 4 lines · Liu Ping Fan
util/event_gsource.c new file +4 -0
+static gboolean prepare(GSource *src, gint *time)
+{
+    EventGSource *nsrc = (EventGSource *)src;
+    int events = 0;

util/event_gsource.c line 33

Quoted 10 lines
util/event_gsource.c new file +10 -0
+
+    if (!nsrc->readable && !nsrc->writable) {
+        return false;
+    }
+    if (nsrc->readable && nsrc->readable(nsrc->opaque)) {
+        events |= G_IO_IN;
+    }
+    if ((nsrc->writable) && nsrc->writable(nsrc->opaque)) {
+        events |= G_IO_OUT;
+    }

G_IO_ERR, G_IO_HUP, G_IO_PRI?

Quoted 5 lines

Here is the select(2) to GCondition mapping:
rfds -> G_IO_IN | G_IO_HUP | G_IO_ERR
wfds -> G_IO_OUT | G_IO_ERR
xfds -> G_IO_PRI

Does G_IO_PRI only happen on read-in direction?

Quoted 4 lines

In other words, we're missing events by just using G_IO_IN and G_IO_OUT. Whether that matters depends on EventGSource users. For sockets it can matter.

I think you mean just prepare all of them, and let the dispatch decide how to handle them, right?

Quoted 6 lines

util/event_gsource.c line 80

Quoted 3 lines
util/event_gsource.c new file +3 -0
+void event_source_release(EventGSource *src)
+{
+    g_source_destroy(&src->source);

Leaks src.

All of the mem used by EventGSource are allocated by g_source_new, so g_source_destroy can reclaim all of them.

Quoted 7 lines

util/event_gsource.c line 83

Quoted 3 lines
util/event_gsource.c new file +3 -0
+}
+
+GPollFD *events_source_get_gfd(EventsGSource *src, int fd)

events_source_add_fd() seems like a better name since this function always allocates a new GPollFD, it never "gets" an existing one.

Thanks for pointing out. But see the reply to "unportable alloc_bmp" below.

Quoted 10 lines

util/event_gsource.c line 90

Quoted 7 lines
util/event_gsource.c new file +7 -0
+{
+    GPollFD *retfd;
+    unsigned long idx;
+
+    idx = find_first_zero_bit(src->alloc_bmp, src->bmp_sz);
+    if (idx == src->bmp_sz) {
+        //idx = src->bmp_sz;

Commented out line.

Apply,

Quoted 4 lines
Quoted 1 line

+void events_source_close_gfd(EventsGSource *src, GPollFD *pollfd)

"close" usually means close(2). I suggest "remove" instead.

Quoted 11 lines

util/event_gsource.c line 154

Quoted 7 lines
util/event_gsource.c new file +7 -0
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque)
+{
+    EventsGSource *src = (EventsGSource *)g_source_new(funcs, sizeof(EventsGSource));
+
+    /* 8bits size at initial */
+    src->bmp_sz = 8;
+    src->alloc_bmp = g_malloc0(src->bmp_sz >> 3);

This is unportable. alloc_bmp is unsigned long, you are allocating just one byte!

I had thought that resorting to bmp_sz to guarantee the bit-ops on alloc_bmp. And if EventsGSource->pollfds is allocated with 64 instance
at initialize, it cost too much. I can fix it with more fine code
when alloc_bmp's size growing.

Quoted 4 lines

Please drop the bitmap approach and use a doubly-linked list or another glib container type of your choice. It needs 3 operations: add, remove, and iterate.

But as the case for slirp, owning to network's connection and disconnection, the slirp's sockets can be dynamically changed quickly.
The bitmap approach is something like slab, while glib container
type lacks such support (maybe using two GArray inuse[], free[]).

Quoted 11 lines
Quoted 11 lines
util/event_gsource.h new file +11 -0
+/* multi fd drive gsource*/
+typedef struct EventsGSource {
+    GSource source;
+    /* 8 for initial, stand for 8 pollfds */
+    unsigned int bmp_sz;
+    unsigned long *alloc_bmp;
+    GPollFD *pollfds;
+    void *opaque;
+} EventsGSource;
+
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque);
Quoted 8 lines

util/event_gsource.h line 50

Quoted 5 lines
util/event_gsource.h new file +5 -0
+void events_source_release(EventsGSource *src);
+gboolean events_source_check(GSource *src);
+gboolean events_source_dispatch(GSource *src, GSourceFunc cb, gpointer data);
+GPollFD *events_source_get_gfd(EventsGSource *src, int fd);
+void events_source_close_gfd(EventsGSource *src, GPollFD *pollfd);

Why are check/dispatch public? Perhaps events_source_new() just needs a prepare() argument instead of exposing GSourceFuncs.

Ok, that is more closed and reasonable.

Thanks, Pingfan

#28 lore raw

Re: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition

In reply to liu ping fan
Quoted 7 lines · liu ping fan
Quoted 6 lines · Jan Kiszka
Quoted 4 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

Quoted 6 lines
Quoted 3 lines

What are the usage rules for this lock, what precisely is it protecting? Is it ensured that we do not take the BQL while holding this one?

It protect the slirp state, since slirp can be touched by slrip_input called by frontend(ex, e1000), also it can be touched by its event handler. With this lock, we do not need BQL

...but the BQL will, at least initially, remain to be everywhere. Every non-converted device model will hold it while calling into Slirp. So we have the ordering "BQL before Slirp lock" already. And we must ensure that there is no "BQL after Slirp lock". Can you guarantee this?

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE
Corporate Competence Center Embedded Linux
#4 lore raw

Re: [Qemu-devel] [RFC PATCH v4 01/15] util: introduce gsource event abstration

In reply to liu ping fan
Quoted 19 lines · liu ping fan
Quoted 18 lines · Stefan Hajnoczi
Quoted 4 lines · Liu Ping Fan
util/event_gsource.c new file +4 -0
+static gboolean prepare(GSource *src, gint *time)
+{
+    EventGSource *nsrc = (EventGSource *)src;
+    int events = 0;

util/event_gsource.c line 33

Quoted 10 lines
util/event_gsource.c new file +10 -0
+
+    if (!nsrc->readable && !nsrc->writable) {
+        return false;
+    }
+    if (nsrc->readable && nsrc->readable(nsrc->opaque)) {
+        events |= G_IO_IN;
+    }
+    if ((nsrc->writable) && nsrc->writable(nsrc->opaque)) {
+        events |= G_IO_OUT;
+    }

G_IO_ERR, G_IO_HUP, G_IO_PRI?

Quoted 6 lines
Quoted 5 lines

Here is the select(2) to GCondition mapping:
rfds -> G_IO_IN | G_IO_HUP | G_IO_ERR
wfds -> G_IO_OUT | G_IO_ERR
xfds -> G_IO_PRI

Does G_IO_PRI only happen on read-in direction?

Yes.

Quoted 6 lines
Quoted 4 lines

In other words, we're missing events by just using G_IO_IN and G_IO_OUT. Whether that matters depends on EventGSource users. For sockets it can matter.

I think you mean just prepare all of them, and let the dispatch decide how to handle them, right?

The user must decide which events to monitor. Otherwise the event loop may run at 100% CPU due to events that are monitored but not handled by the user.

Quoted 8 lines
Quoted 6 lines

util/event_gsource.c line 80

Quoted 3 lines
util/event_gsource.c new file +3 -0
+void event_source_release(EventGSource *src)
+{
+    g_source_destroy(&src->source);

Leaks src.

All of the mem used by EventGSource are allocated by g_source_new, so g_source_destroy can reclaim all of them.

Okay, then the bug is events_source_release() which calls g_free(src) after g_source_destroy(&src->source).

Quoted 16 lines
Quoted 11 lines

util/event_gsource.c line 154

Quoted 7 lines
util/event_gsource.c new file +7 -0
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque)
+{
+    EventsGSource *src = (EventsGSource *)g_source_new(funcs, sizeof(EventsGSource));
+
+    /* 8bits size at initial */
+    src->bmp_sz = 8;
+    src->alloc_bmp = g_malloc0(src->bmp_sz >> 3);

This is unportable. alloc_bmp is unsigned long, you are allocating just one byte!

I had thought that resorting to bmp_sz to guarantee the bit-ops on alloc_bmp. And if EventsGSource->pollfds is allocated with 64 instance
at initialize, it cost too much. I can fix it with more fine code
when alloc_bmp's size growing.

Quoted 8 lines
Quoted 4 lines

Please drop the bitmap approach and use a doubly-linked list or another glib container type of your choice. It needs 3 operations: add, remove, and iterate.

But as the case for slirp, owning to network's connection and disconnection, the slirp's sockets can be dynamically changed quickly.
The bitmap approach is something like slab, while glib container
type lacks such support (maybe using two GArray inuse[], free[]).

Doubly-linked list insertion and removal are O(1).

The linked list can be allocated with g_slice_alloc() which is efficient.

Iterating linked lists isn't cache-friendly but this is premature optimization. I bet the userspace TCP - pulling packets apart - is more of a CPU bottleneck than a doubly-linked list of fds.

Please use existing data structures instead of writing them from scratch unless there is a real need (e.g. profiling shows it matters).

#15 lore raw

Re: [Qemu-devel] [RFC PATCH v4 06/15] net: port socket to GSource

In reply to liu ping fan
Quoted 8 lines · liu ping fan
Quoted 4 lines · Stefan Hajnoczi

On Wed, Apr 17, 2013 at 04:39:15PM +0800, Liu Ping Fan wrote:
I'm a little worried that we're lacking G_IO_HUP | G_IO_ERR. Perhaps disconnect and network errors will be ignored.

NetSocketState can do limited things about these situation, perhaps, implement net_socket_can_receive() to export such message to frontend ?

net/socket.c uses G_IO_HUP | G_IO_ERR today, see
iohandler.c:qemu_iohandler_fill().

This patch *stops* using them. My question is: why stop and is it correct?

Stefan

#29 lore raw

Re: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition

In reply to Jan Kiszka
Quoted 11 lines · Jan Kiszka
Quoted 10 lines · liu ping fan
Quoted 9 lines · Jan Kiszka
Quoted 4 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

What are the usage rules for this lock, what precisely is it protecting? Is it ensured that we do not take the BQL while holding this one?

Quoted 9 lines
Quoted 3 lines

It protect the slirp state, since slirp can be touched by slrip_input called by frontend(ex, e1000), also it can be touched by its event handler. With this lock, we do not need BQL

...but the BQL will, at least initially, remain to be everywhere. Every non-converted device model will hold it while calling into Slirp. So we have the ordering "BQL before Slirp lock" already. And we must ensure that there is no "BQL after Slirp lock". Can you guarantee this?

Oh, yes, there is a potential ABBA lock problem. Especially for slirp backend, the NetClientState's receive() can be nested, the scene:
e1000 send packet -> net_slirp_receive() ...->arp_input()..-> ->slirp_output() ->e1000_receive()
Although currently, there is no extra lock required by e1000_receive(), but the nested model does cause the potential of ABBA lock problem, when beginning to covert the frontend(e1000). What about introducing slirp->lockstate and slirp->lock only used to protect it. So the users of slirp will protect agaist each other by slirp->lockstate, meanwhile, we can advoid the potential dead lock.

Regards,
Pingfan

Quoted 5 lines

Jan

--
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE Corporate Competence Center Embedded Linux

#5 lore raw

Re: [Qemu-devel] [RFC PATCH v4 01/15] util: introduce gsource event abstration

In reply to Stefan Hajnoczi
Quoted 82 lines · Stefan Hajnoczi
Quoted 19 lines · liu ping fan
Quoted 18 lines · Stefan Hajnoczi
Quoted 4 lines · Liu Ping Fan
util/event_gsource.c new file +4 -0
+static gboolean prepare(GSource *src, gint *time)
+{
+    EventGSource *nsrc = (EventGSource *)src;
+    int events = 0;

util/event_gsource.c line 33

Quoted 10 lines
util/event_gsource.c new file +10 -0
+
+    if (!nsrc->readable && !nsrc->writable) {
+        return false;
+    }
+    if (nsrc->readable && nsrc->readable(nsrc->opaque)) {
+        events |= G_IO_IN;
+    }
+    if ((nsrc->writable) && nsrc->writable(nsrc->opaque)) {
+        events |= G_IO_OUT;
+    }

G_IO_ERR, G_IO_HUP, G_IO_PRI?

Quoted 6 lines
Quoted 5 lines

Here is the select(2) to GCondition mapping:
rfds -> G_IO_IN | G_IO_HUP | G_IO_ERR
wfds -> G_IO_OUT | G_IO_ERR
xfds -> G_IO_PRI

Does G_IO_PRI only happen on read-in direction?

Yes.

Quoted 6 lines
Quoted 4 lines

In other words, we're missing events by just using G_IO_IN and G_IO_OUT. Whether that matters depends on EventGSource users. For sockets it can matter.

I think you mean just prepare all of them, and let the dispatch decide how to handle them, right?

The user must decide which events to monitor. Otherwise the event loop may run at 100% CPU due to events that are monitored but not handled by the user.

Quoted 8 lines
Quoted 6 lines

util/event_gsource.c line 80

Quoted 3 lines
util/event_gsource.c new file +3 -0
+void event_source_release(EventGSource *src)
+{
+    g_source_destroy(&src->source);

Leaks src.

All of the mem used by EventGSource are allocated by g_source_new, so g_source_destroy can reclaim all of them.

Okay, then the bug is events_source_release() which calls g_free(src) after g_source_destroy(&src->source).

Quoted 16 lines
Quoted 11 lines

util/event_gsource.c line 154

Quoted 7 lines
util/event_gsource.c new file +7 -0
+EventsGSource *events_source_new(GSourceFuncs *funcs, GSourceFunc dispatch_cb, void *opaque)
+{
+    EventsGSource *src = (EventsGSource *)g_source_new(funcs, sizeof(EventsGSource));
+
+    /* 8bits size at initial */
+    src->bmp_sz = 8;
+    src->alloc_bmp = g_malloc0(src->bmp_sz >> 3);

This is unportable. alloc_bmp is unsigned long, you are allocating just one byte!

I had thought that resorting to bmp_sz to guarantee the bit-ops on alloc_bmp. And if EventsGSource->pollfds is allocated with 64 instance
at initialize, it cost too much. I can fix it with more fine code
when alloc_bmp's size growing.

Quoted 8 lines
Quoted 4 lines

Please drop the bitmap approach and use a doubly-linked list or another glib container type of your choice. It needs 3 operations: add, remove, and iterate.

But as the case for slirp, owning to network's connection and disconnection, the slirp's sockets can be dynamically changed quickly.
The bitmap approach is something like slab, while glib container
type lacks such support (maybe using two GArray inuse[], free[]).

Doubly-linked list insertion and removal are O(1).

The linked list can be allocated with g_slice_alloc() which is efficient.

Quoted 6 lines

Iterating linked lists isn't cache-friendly but this is premature optimization. I bet the userspace TCP - pulling packets apart - is more of a CPU bottleneck than a doubly-linked list of fds.

Please use existing data structures instead of writing them from scratch unless there is a real need (e.g. profiling shows it matters).

Ok, thanks for the detail explaining.

Regards,
Pingfan

#16 lore raw

Re: [Qemu-devel] [RFC PATCH v4 06/15] net: port socket to GSource

In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi
Quoted 8 lines · liu ping fan
Quoted 4 lines · Stefan Hajnoczi

On Wed, Apr 17, 2013 at 04:39:15PM +0800, Liu Ping Fan wrote:
I'm a little worried that we're lacking G_IO_HUP | G_IO_ERR. Perhaps disconnect and network errors will be ignored.

NetSocketState can do limited things about these situation, perhaps, implement net_socket_can_receive() to export such message to frontend ?

Quoted 6 lines

net/socket.c uses G_IO_HUP | G_IO_ERR today, see
iohandler.c:qemu_iohandler_fill().

This patch *stops* using them. My question is: why stop and is it correct?

No, it is not. It leaves dead file descriptors unhandled. Will fix up in next version

Quoted 1 line

Stefan

#30 lore raw

Re: [Qemu-devel] [RFC PATCH v4 14/15] slirp: handle race condition

In reply to liu ping fan
Quoted 21 lines · liu ping fan
Quoted 11 lines · Jan Kiszka
Quoted 10 lines · liu ping fan
Quoted 9 lines · Jan Kiszka
Quoted 4 lines · Liu Ping Fan

From: Liu Ping Fan <[email protected]>

Slirp and its peer can run on different context at the same time. Using lock to protect

What are the usage rules for this lock, what precisely is it protecting? Is it ensured that we do not take the BQL while holding this one?

Quoted 9 lines
Quoted 3 lines

It protect the slirp state, since slirp can be touched by slrip_input called by frontend(ex, e1000), also it can be touched by its event handler. With this lock, we do not need BQL

...but the BQL will, at least initially, remain to be everywhere. Every non-converted device model will hold it while calling into Slirp. So we have the ordering "BQL before Slirp lock" already. And we must ensure that there is no "BQL after Slirp lock". Can you guarantee this?

Quoted 11 lines

Oh, yes, there is a potential ABBA lock problem. Especially for slirp backend, the NetClientState's receive() can be nested, the scene:
e1000 send packet -> net_slirp_receive() ...->arp_input()..-> ->slirp_output() ->e1000_receive()
Although currently, there is no extra lock required by e1000_receive(), but the nested model does cause the potential of ABBA lock problem, when beginning to covert the frontend(e1000). What about introducing slirp->lockstate and slirp->lock only used to protect it. So the users of slirp will protect agaist each other by slirp->lockstate, meanwhile, we can advoid the potential dead lock.

Drop the bad idea. The slirp->lock should be dropped before calling out direction method. And if_start() is need to fix

Quoted 7 lines

Regards,
Pingfan

Quoted 5 lines

Jan

--
Siemens AG, Corporate Technology, CT RTC ITP SDP-DE Corporate Competence Center Embedded Linux