port network layer onto glib
Subsystems (2)
cover port network layer onto glib
summary:
patch1: GSource event abstraction
patch2~6: port network backend to glib
patch7~10: make network core re-entrant
patch11~14: port the slirp backend onto glib
The slirp->lock's deadlock problem has been eliminated and works fine. And other components seems more stable, so I change from RFCv- to v-
rfcv5->v1:
1. re-arrange logic for net/socket.c
rfcv4->rfcv5:
1.use GList to reimplement EventsGSource
2.make readable()/writable() return events which the backend is interested in 3.fix the slirp->lock's potential deadlock issue
rfcv3->rfcv4:
1.separate GSource event to dedicated file
2.integrated with net core re-entrant
3.make slirp/ re-entrant
rfcv2->rfcv3:
1.drop hub and the frontend(virtio net)
2.split the patch for NetClientSource
rfcv1->rfcv2:
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 (14):
util: introduce gsource event abstraction
net: introduce bind_ctx to NetClientInfo
net: port vde onto GSource
net: port socket to GSource
net: port tap onto 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 | 35 +++- net/socket.c | 194 +++++++++++--- net/tap-win32.c | 31 ++- net/tap.c | 64 ++++- net/vde.c | 31 ++- slirp/if.c | 57 +++- slirp/libslirp.h | 7 +- slirp/main.h | 3 +- slirp/mbuf.h | 2 + slirp/slirp.c | 670 ++++++++++++++++++++++--------------------- slirp/slirp.h | 11 +- slirp/socket.c | 2 + slirp/socket.h | 1 + stubs/slirp.c | 8 - util/Makefile.objs | 1 + util/event_gsource.c | 157 ++++++++++ util/event_gsource.h | 49 ++++ 24 files changed, 1105 insertions(+), 416 deletions(-) create mode 100644 util/event_gsource.c create mode 100644 util/event_gsource.h
Signature · 1 line
1.7.4.4
PATCH 1/14 util: introduce gsource event abstraction
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 dynamically changed backend file, ex, slirp.
--- util/Makefile.objs | 1 + util/event_gsource.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/event_gsource.h | 49 ++++++++++++++++ 3 files changed, 207 insertions(+), 0 deletions(-) create mode 100644 util/event_gsource.c create mode 100644 util/event_gsource.h
util/Makefile.objs +1 -0
@@ -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 +157 -0
157 lines not on this page · raw message
util/event_gsource.h new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
1.7.4.4
PATCH 2/14 net: introduce bind_ctx to NetClientInfo
In reply to Liu Ping FanFrom: 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
@@ -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
PATCH 3/14 net: port vde onto GSource
In reply to Liu Ping FanFrom: Liu Ping Fan <[email protected]>
--- net/vde.c | 31 +++++++++++++++++++++++++++++-- 1 files changed, 29 insertions(+), 2 deletions(-)
net/vde.c +29 -2
@@ -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,20 +62,43 @@ 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 |G_IO_HUP |G_IO_ERR )) { +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 gushort readable (void *opaque) +{ +return G_IO_IN |G_IO_HUP |G_IO_ERR ; +} +static int net_vde_init (NetClientState *peer,const char *model,const char *name,const char *sock,int port,const char *group,int mode)
@@ -104,7 +129,9 @@ 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); + s->nsrc =event_source_new (vde_datafd (vde), vde_handler, s); + s->nsrc->readable = readable; + nc->info->bind_ctx (nc,NULL );return 0 ; }
Signature · 1 line
1.7.4.4
PATCH 4/14 net: port socket to GSource
In reply to Liu Ping FanFrom: 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 | 194 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 161 insertions(+), 33 deletions(-)
net/socket.c +161 -33
@@ -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)
@@ -141,6 +134,59 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,return ret; } +static gushort socket_connecting_writable (void *opaque) +{ +return G_IO_OUT |G_IO_ERR ; +} + +static gushort socket_listen_readable (void *opaque) +{ +return G_IO_IN |G_IO_HUP |G_IO_ERR ; +} + +static gushort socket_establish_readable (void *opaque) +{ +NetSocketState *s = opaque; + +/* rely on net_socket_send to handle err */ +if (s->read_poll &&net_socket_can_send (s)) { +return G_IO_IN |G_IO_HUP |G_IO_ERR ; + } +return 0 ; +} + +static gushort socket_establish_writable (void *opaque) +{ +NetSocketState *s = opaque; + +if (s->write_poll) { +return G_IO_OUT |G_IO_HUP |G_IO_ERR ; + } +return 0 ; +} + +static gushort socket_dgram_readable (void *opaque) +{ +NetSocketState *s = opaque; + +/* rely on net_socket_send_dgram to handle err */ +if (s->read_poll &&net_socket_can_send (s)) { +return G_IO_IN |G_IO_ERR ; + } +return 0 ; +} + +static gushort socket_dgram_writable (void *opaque) +{ +NetSocketState *s = opaque; + +if (s->write_poll) { +return G_IO_OUT |G_IO_ERR ; + } +return 0 ; +} + +/* common handler for accept-established or connecting case */ static void net_socket_send (void *opaque) {NetSocketState *s = opaque;
@@ -159,8 +205,13 @@ static void net_socket_send(void *opaque) eoc:net_socket_read_poll (s, false);net_socket_write_poll (s, false); +/* rely on this to tell the accept-established or connecting case */ if (s->listen_fd !=-1 ) { -qemu_set_fd_handler (s->listen_fd, net_socket_accept,NULL , s); +event_source_release (s->nsrc); + s->nsrc =event_source_new (s->listen_fd, net_socket_listen_handler, + s); + s->nsrc->readable = socket_listen_readable; + s->nc.info->bind_ctx (&s->nc,NULL ); }closesocket (s->fd);
@@ -231,6 +282,8 @@ static void net_socket_send_dgram(void *opaque)/* end of connection */ net_socket_read_poll (s, false);net_socket_write_poll (s, false); +/* for dgram err, removing it */ +g_source_remove_poll (&s->nsrc->source, &s->nsrc->gfd);return ; }qemu_send_packet (&s->nc, s->buf, size);
@@ -331,6 +384,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 +399,24 @@ 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; + +/* for err, unregister the handler */ +if (nsrc->gfd.revents & (G_IO_IN |G_IO_ERR )) { +net_socket_send_dgram (s); + } +if (nsrc->gfd.revents & (G_IO_OUT |G_IO_ERR )) { +net_socket_writable (s); + } +return true; +} + static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, const char *model, const char *name,
@@ -393,8 +470,12 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, s->fd = fd; s->listen_fd =-1 ; - s->send_fn = net_socket_send_dgram; + s->nsrc =event_source_new (fd, net_socket_dgram_handler, s); + s->nsrc->readable = socket_dgram_readable; + s->nsrc->writable = socket_dgram_writable; + nc->info->bind_ctx (nc,NULL );net_socket_read_poll (s, true); +net_socket_write_poll (s, true);/* mcast: save bound address as dst */ if (is_connected) {
@@ -408,20 +489,31 @@ 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 *nsrc = data; +NetSocketState *s = nsrc->opaque; + +event_source_release (s->nsrc); +/* For error handle, delay to net_socket_establish_handler */ + s->nsrc =event_source_new (s->fd, net_socket_establish_handler, s); + s->nsrc->readable = socket_establish_readable; + s->nsrc->writable = socket_establish_writable; + s->nc.info->bind_ctx (&s->nc,NULL ); +net_socket_read_poll (s, true); +net_socket_write_poll (s, true); + +return true; +} + static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, const char *model, const char *name,
@@ -440,9 +532,20 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, s->listen_fd =-1 ;if (is_connected) { -net_socket_connect (s); +assert (!s->nsrc); + s->nsrc =event_source_new (fd, net_socket_establish_handler, s); + s->nsrc->readable = socket_establish_readable; + s->nsrc->writable = socket_establish_writable; + nc->info->bind_ctx (nc,NULL ); +net_socket_read_poll (s, true); +net_socket_write_poll (s, true); }else { -qemu_set_fd_handler (s->fd,NULL , net_socket_connect, s); +assert (!s->nsrc); + s->nsrc =event_source_new (fd, net_socket_connect_handler, s); + s->nsrc->writable = socket_connecting_writable; + nc->info->bind_ctx (nc,NULL ); +net_socket_read_poll (s, false); +net_socket_write_poll (s, true); }return s; }
@@ -473,30 +576,52 @@ 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) { -NetSocketState *s = opaque; +EventGSource *nsrc = (EventGSource *)data; +NetSocketState *s = nsrc->opaque; + +if (nsrc->gfd.revents & (G_IO_IN |G_IO_HUP |G_IO_ERR )) { +net_socket_send (s); + } +if ((nsrc->gfd.revents & (G_IO_OUT |G_IO_HUP |G_IO_ERR ))) { +net_socket_writable (s); + } +return true; +} + +static gboolean net_socket_listen_handler (gpointer data) +{ +EventGSource *nsrc = data; +NetSocketState *s = nsrc->opaque;struct sockaddr_in saddr;socklen_t len;int fd; -for (;;) { - len =sizeof (saddr); + len =sizeof (saddr); +do { fd =qemu_accept (s->listen_fd, (struct sockaddr *)&saddr, &len);if (fd <0 && errno !=EINTR ) { -return ; +return false; }else if (fd >=0 ) { -qemu_set_fd_handler (s->listen_fd,NULL ,NULL ,NULL );break ; } - } + }while (true); s->fd = fd; s->nc.link_down = false; -net_socket_connect (s); +/* prevent more than one connect req */ +event_source_release (s->nsrc); + s->nsrc =event_source_new (fd, net_socket_establish_handler, s); + s->nsrc->readable = socket_establish_readable; + s->nsrc->writable = socket_establish_writable; + 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,
@@ -542,7 +667,10 @@ 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); + s->nsrc =event_source_new (fd, net_socket_listen_handler, s); + s->nsrc->readable = socket_listen_readable; + nc->info->bind_ctx (nc,NULL ); +return 0 ; }
Signature · 1 line
1.7.4.4
PATCH 5/14 net: port tap onto GSource
In reply to Liu Ping FanFrom: Liu Ping Fan <[email protected]>
--- net/tap.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 54 insertions(+), 10 deletions(-)
net/tap.c +54 -10
@@ -41,6 +41,7 @@#include "qemu/error-report.h" #include "net/tap.h" +#include "util/event_gsource.h" #include "hw/vhost_net.h"
@@ -62,6 +63,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 +72,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 gushort 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 G_IO_IN ; + } +return 0 ; +} + +static gushort writable (void *opaque) +{ +TAPState *s = opaque; + +if (s->enabled && s->write_poll) { +return G_IO_OUT ; + } +return 0 ; +} + +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 +316,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 ; }
@@ -300,6 +326,12 @@ static void tap_poll(NetClientState *nc, bool enable)TAPState *s =DO_UPCAST (TAPState, nc, nc);tap_read_poll (s, enable);tap_write_poll (s, enable); + +if (!enable) { +g_source_remove_poll (&s->nsrc->source, &s->nsrc->gfd); + }else { +g_source_add_poll (&s->nsrc->source, &s->nsrc->gfd); + } }int tap_get_fd (NetClientState *nc)
@@ -309,6 +341,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 +358,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 +636,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 +647,11 @@ 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->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);
@@ -844,7 +890,6 @@ int tap_enable(NetClientState *nc) ret =tap_fd_enable (s->fd);if (ret ==0 ) { s->enabled = true; -tap_update_fd_handler (s); }return ret; }
@@ -862,7 +907,6 @@ int tap_disable(NetClientState *nc)if (ret ==0 ) {qemu_purge_queued_packets (nc); s->enabled = false; -tap_update_fd_handler (s); }return ret; }
Signature · 1 line
1.7.4.4
PATCH 6/14 net: port tap-win32 onto GSource
In reply to Liu Ping FanFrom: Liu Ping Fan <[email protected]>
--- net/tap-win32.c | 31 +++++++++++++++++++++++++++++-- 1 files changed, 29 insertions(+), 2 deletions(-)
net/tap-win32.c +29 -2
@@ -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,13 +670,37 @@ 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 gushort readable (void *opaque) +{ +return G_IO_IN ; +} +static int tap_win32_init (NetClientState *peer,const char *model,const char *name,const char *ifname) {
@@ -697,7 +722,9 @@ 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); + s->nsrc =event_source_new (s->handle->tap_semaphore, tap_win32_handler, s); + s->nsrc->readable = readable; + nc->info->bind_ctx (&s->nc,NULL );return 0 ; }
Signature · 1 line
1.7.4.4
PATCH 7/14 net: hub use lock to protect ports list
In reply to Liu Ping FanFrom: 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
@@ -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
Re: [Qemu-devel] [PATCH v1 07/14] net: hub use lock to protect ports list
Quoted 8 lines · Liu Ping Fan
From: Liu Ping Fan <[email protected]>
Hub ports will run on multi-threads, so use lock to protect them.
Signed-off-by Liu Ping Fan <[email protected]>--- net/hub.c | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-)
Does this rely on net queue re-entrancy detection? Otherwise we'll deadlock when slirp re-enters the net layer, for example for ARP requests.
I suggest moving this later in the patch series when the net queue is protected already.
Stefan
Re: [Qemu-devel] [PATCH v1 07/14] net: hub use lock to protect ports list
Quoted 5 lines · Stefan Hajnoczi
Quoted 4 lines · Liu Ping Fan
From: Liu Ping Fan <[email protected]>
Hub ports will run on multi-threads, so use lock to protect them.
Quoted 9 lines
Quoted 4 lines
Signed-off-by Liu Ping Fan <[email protected]>--- net/hub.c | 25 ++++++++++++++++++++++++- 1 files changed, 24 insertions(+), 1 deletions(-)Does this rely on net queue re-entrancy detection? Otherwise we'll deadlock when slirp re-enters the net layer, for example for ARP requests.
Yes, you are right.
Quoted 3 lines
I suggest moving this later in the patch series when the net queue is protected already.
Ok.
Thanks
Pingfan
Quoted 1 line
Stefan
PATCH 8/14 net: introduce lock to protect NetQueue
In reply to Liu Ping FanFrom: 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
@@ -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
Re: [Qemu-devel] [PATCH v1 08/14] net: introduce lock to protect NetQueue
Quoted 31 lines · 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.
Signed-off-by Liu Ping Fan <[email protected]>--- net/queue.c | 11 +++++++++++ 1 files changed, 11 insertions(+), 0 deletions(-)net/queue.c +2
-0@@ -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 ;
net/queue.c static void qemu_net_queue_append(NetQueue *queue, · line 114
Quoted 6 lines
net/queue.c +2
-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);
nq_count must be protected too.
PATCH 9/14 net: introduce lock to protect NetClientState's peer's access
In reply to Liu Ping FanFrom: 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
@@ -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
@@ -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
PATCH 10/14 net: make netclient re-entrant with refcnt
In reply to Liu Ping FanFrom: 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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 11/14 slirp: make timeout local
In reply to Liu Ping FanFrom: 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
@@ -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,13 @@ 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 = ((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 +296,9 @@ 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 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
@@ -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
PATCH 12/14 slirp: make slirp event dispatch based on slirp instance, not global
In reply to Liu Ping FanFrom: 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.
The logic in slirp_pollfds_fill/_poll is not changed, but due to drop of the functions, rearrange the code to obey the coding style. For other minor changes, they accord to the nearby style.
--- main-loop.c | 4 - net/slirp.c | 32 +++ slirp/libslirp.h | 7 +- slirp/slirp.c | 567 +++++++++++++++++++++++++----------------------------- slirp/socket.c | 2 + slirp/socket.h | 1 + stubs/slirp.c | 8 - 7 files changed, 299 insertions(+), 322 deletions(-)
main-loop.c +0 -4
@@ -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 +32 -0
@@ -36,6 +36,7 @@#include "qemu/sockets.h" #include "slirp/libslirp.h" #include "char/char.h" +#include "util/event_gsource.h" static int get_str_sep (char *buf,int buf_size,const char **pp,int sep) {
@@ -76,6 +77,7 @@ typedef struct SlirpState {#ifndef _WIN32char smb_dir[128 ];#endif +EventsGSource *slirp_src; } SlirpState;static struct slirp_config_str *slirp_configs;
@@ -120,17 +122,44 @@ static void net_slirp_cleanup(NetClientState *nc)SlirpState *s =DO_UPCAST (SlirpState, nc, nc);slirp_cleanup (s->slirp); +events_source_release (s->slirp_src);slirp_smb_cleanup (s);QTAILQ_REMOVE (&slirp_stacks, s, entry); } +static void net_slirp_bind_ctx (NetClientState *nc,GMainContext *ctx) +{ +SlirpState *s =DO_UPCAST (SlirpState, nc, nc); + +g_source_attach (&s->slirp_src->source, ctx); +} +static NetClientInfo net_slirp_info = { .type =NET_CLIENT_OPTIONS_KIND_USER , .size =sizeof (SlirpState), .receive = net_slirp_receive, .cleanup = net_slirp_cleanup, + .bind_ctx = net_slirp_bind_ctx, }; +GPollFD *slirp_gsource_get_gfd (void *opaque,int fd) +{ +GPollFD *retfd; +SlirpState *s = opaque; +EventsGSource *src = s->slirp_src; + retfd =events_source_add_gfd (src, fd); + +return retfd; +} + +void slirp_gsource_close_gfd (void *opaque,GPollFD *pollfd) +{ +SlirpState *s = opaque; +EventsGSource *src = s->slirp_src; + +events_source_remove_gfd (src, pollfd); +} + static int net_slirp_init(NetClientState *peer, const char *model, const char *name, int restricted, const char *vnetwork, const char *vhost,
@@ -244,6 +273,8 @@ static int net_slirp_init(NetClientState *peer, const char *model, s->slirp =slirp_init (restricted, net, mask, host, vhostname, tftp_export, bootfile, dhcp, dns, dnssearch, s); + s->slirp_src =events_source_new (slirp_prepare, slirp_handler, s->slirp); +QTAILQ_INSERT_TAIL (&slirp_stacks, s, entry);for (config = slirp_configs; config; config = config->next) {
@@ -266,6 +297,7 @@ static int net_slirp_init(NetClientState *peer, const char *model, goto error; }#endif + s->nc.info->bind_ctx (&s->nc,NULL );return 0 ;
slirp/libslirp.h +4 -3
@@ -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 +260 -307
652 lines not on this page · raw message
slirp/socket.c +2 -0
@@ -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
@@ -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
@@ -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
PATCH 13/14 slirp: handle race condition
In reply to Liu Ping FanFrom: Liu Ping Fan <[email protected]>
Slirp and its peer can run on different context at the same time. Using lock to protect. Lock rule: no extra lock can be hold after slirp->lock. This will protect us from deadlock when calling to peer.
As to coding style, they accord to the nearby code's style.
--- slirp/if.c | 57 ++++++++++++++++++++++++++++++++-------- slirp/main.h | 3 +- slirp/mbuf.h | 2 + slirp/slirp.c | 81 ++++++++++++++++++++++++++++++++++++++++++--------------- slirp/slirp.h | 6 +++- 5 files changed, 115 insertions(+), 34 deletions(-)
slirp/if.c +46 -11
@@ -132,12 +132,21 @@ diddit: } } -#ifndef FULL_BOLT -/* -* This prevents us from malloc()ing too many mbufs -*/ -if_start (ifm->slirp); -#endif +} + +static void mbuf_free (gpointer data,gpointer user_data) +{ +struct mbuf *ifm = data; +m_free (ifm); +} + +static void if_send_free (gpointer data,gpointer user_data) +{ +struct mbuf *ifm = data; +Slirp *slirp = user_data; + +if_encap (slirp, ifm); +m_free (ifm); }/*
@@ -156,7 +165,10 @@ void if_start(Slirp *slirp) {uint64_t now =qemu_get_clock_ns (rt_clock);bool from_batchq, next_from_batchq; -struct mbuf *ifm, *ifm_next, *ifqt; +struct mbuf *ifm, *ifm_next, *ifqt, *mclone; +GList *drop_list, *send_list; + drop_list = send_list =NULL ; +int ret;DEBUG_CALL ("if_start" );
@@ -192,9 +204,27 @@ void if_start(Slirp *slirp) }/* Try to send packet unless it already expired */ -if (ifm->expiration_date >= now && !if_encap (slirp, ifm)) { -/* Packet is delayed due to pending ARP resolution */ -continue ; +if (ifm->expiration_date < now) { + drop_list =g_list_append (drop_list, ifm); + }else { + ret =if_query (slirp, ifm); +switch (ret) { +case 2 : + send_list =g_list_append (send_list, ifm); +break ; +case 1 : + mclone =m_get (slirp); +m_copy (mclone, ifm,0 , ifm->m_len); + mclone->arp_requested = true; + send_list =g_list_append (send_list, mclone); +/* Packet is delayed due to pending ARP resolution */ +continue ; +case 0 : +continue ; +case -1 : + drop_list =g_list_append (drop_list, ifm); +break ; + } }if (ifm == slirp->next_m) {
@@ -230,8 +260,13 @@ void if_start(Slirp *slirp) ifm->ifq_so->so_nqueued =0 ; } -m_free (ifm); } slirp->if_start_busy = false; +qemu_mutex_unlock (&slirp->lock); + +g_list_foreach (drop_list, mbuf_free,NULL ); +g_list_free (drop_list); +g_list_foreach (send_list, if_send_free, slirp); +g_list_free (send_list); }
slirp/main.h +2 -1
@@ -44,7 +44,8 @@ extern int tcp_keepintvl;#define PROTO_PPP 0x2#endif -int if_encap (Slirp *slirp,struct mbuf *ifm); +int if_query (Slirp *slirp,struct mbuf *ifm); +void if_encap (Slirp *slirp,struct mbuf *ifm);ssize_t slirp_send (struct socket *so,const void *buf,size_t len,int flags);#endif
slirp/mbuf.h +2 -0
@@ -34,6 +34,7 @@#define _MBUF_H_#define MINCSIZE 4096/* Amount to increase mbuf if too small */ +#define ETH_ALEN 6/* * Macros for type conversion
@@ -82,6 +83,7 @@ struct m_hdr {struct mbuf {struct m_hdr m_hdr;Slirp *slirp; +uint8_t ethaddr[ETH_ALEN ];bool arp_requested;uint64_t expiration_date;/* start of dynamic buffer area, must be last element */
slirp/slirp.c +60 -21
193 lines not on this page · raw message
slirp/slirp.h +5 -1
@@ -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"
@@ -158,7 +159,6 @@ void free(void *ptr);#include "bootp.h" #include "tftp.h" -#define ETH_ALEN 6#define ETH_HLEN 14#define ETH_P_IP 0x0800/* Internet Protocol packet */
@@ -207,6 +207,10 @@ struct Slirp {u_int last_slowtimo;int do_slowtimo; +/* lock to protect slirp running both on frontend or SlirpState context. +* Lock rule: biglock ->lock. Should be dropped before calling peer. +*/ +QemuMutex lock;/* virtual network configuration */ struct in_addr vnetwork_addr;struct in_addr vnetwork_mask;
Signature · 1 line
1.7.4.4
PATCH 14/14 slirp: use lock to protect the slirp_instances
In reply to Liu Ping FanFrom: Liu Ping Fan <[email protected]>
slirps will run on dedicated thread, and dynamically join or disjoin this list, so need lock 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
@@ -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
@@ -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); }
@@ -1167,3 +1175,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
Quoted 71 lines · Liu Ping Fan
summary:
patch1: GSource event abstraction
patch2~6: port network backend to glib
patch7~10: make network core re-entrant
patch11~14: port the slirp backend onto glibThe slirp->lock's deadlock problem has been eliminated and works fine. And other components seems more stable, so I change from RFCv- to v-
rfcv5->v1:
1. re-arrange logic fornet/socket.c
rfcv4->rfcv5:
1.use GList to reimplement EventsGSource
2.make readable()/writable() return events which the backend is interested in 3.fix the slirp->lock's potential deadlock issue
rfcv3->rfcv4:
1.separate GSource event to dedicated file
2.integrated with net core re-entrant
3.make slirp/ re-entrant
rfcv2->rfcv3:
1.drop hub and the frontend(virtio net)
2.split the patch for NetClientSource
rfcv1->rfcv2:
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 dataplaneLiu Ping Fan (14):
util: introduce gsource event abstraction
net: introducebind_ctxto NetClientInfo
net: port vde onto GSource
net: port socket to GSource
net: port tap onto 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 theslirp_instanceshw/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 | 35 +++- net/socket.c | 194 +++++++++++--- net/tap-win32.c | 31 ++- net/tap.c | 64 ++++- net/vde.c | 31 ++- slirp/if.c | 57 +++- slirp/libslirp.h | 7 +- slirp/main.h | 3 +- slirp/mbuf.h | 2 + slirp/slirp.c | 670 ++++++++++++++++++++++--------------------- slirp/slirp.h | 11 +- slirp/socket.c | 2 +
Quoted 8 lines
slirp/socket.h | 1 + stubs/slirp.c | 8 - util/Makefile.objs | 1 + util/event_gsource.c | 157 ++++++++++ util/event_gsource.h | 49 ++++ 24 files changed, 1105 insertions(+), 416 deletions(-) create mode 100644 util/event_gsource.c create mode 100644 util/event_gsource.h
Hi Ping Fan,
Sorry for the slow review, I'm focussing on QEMU 1.5 at the moment but will look at this series soon.
Stefan
Quoted 81 lines · Stefan Hajnoczi
Quoted 71 lines · Liu Ping Fan
summary:
patch1: GSource event abstraction
patch2~6: port network backend to glib
patch7~10: make network core re-entrant
patch11~14: port the slirp backend onto glibThe slirp->lock's deadlock problem has been eliminated and works fine. And other components seems more stable, so I change from RFCv- to v-
rfcv5->v1:
1. re-arrange logic fornet/socket.c
rfcv4->rfcv5:
1.use GList to reimplement EventsGSource
2.make readable()/writable() return events which the backend is interested in 3.fix the slirp->lock's potential deadlock issue
rfcv3->rfcv4:
1.separate GSource event to dedicated file
2.integrated with net core re-entrant
3.make slirp/ re-entrant
rfcv2->rfcv3:
1.drop hub and the frontend(virtio net)
2.split the patch for NetClientSource
rfcv1->rfcv2:
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 dataplaneLiu Ping Fan (14):
util: introduce gsource event abstraction
net: introducebind_ctxto NetClientInfo
net: port vde onto GSource
net: port socket to GSource
net: port tap onto 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 theslirp_instanceshw/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 | 35 +++- net/socket.c | 194 +++++++++++--- net/tap-win32.c | 31 ++- net/tap.c | 64 ++++- net/vde.c | 31 ++- slirp/if.c | 57 +++- slirp/libslirp.h | 7 +- slirp/main.h | 3 +- slirp/mbuf.h | 2 + slirp/slirp.c | 670 ++++++++++++++++++++++--------------------- slirp/slirp.h | 11 +- slirp/socket.c | 2 +Quoted 8 lines
slirp/socket.h | 1 + stubs/slirp.c | 8 - util/Makefile.objs | 1 + util/event_gsource.c | 157 ++++++++++ util/event_gsource.h | 49 ++++ 24 files changed, 1105 insertions(+), 416 deletions(-) create mode 100644 util/event_gsource.c create mode 100644 util/event_gsource.h
Quoted 4 lines
Hi Ping Fan,
Sorry for the slow review, I'm focussing on QEMU 1.5 at the moment but will look at this series soon.
Thank you very much :)
And as mentioned, please skip patch2~6: port network backend to glib. I think mdroth method is better than mind, will port onto his.
Regards,
Pingfan
Quoted 1 line
Stefan