From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:44902) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UP4kn-0003DH-JJ for qemu-devel@nongnu.org; Mon, 08 Apr 2013 01:36:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UP4ki-000373-GC for qemu-devel@nongnu.org; Mon, 08 Apr 2013 01:36:29 -0400 Received: from e28smtp03.in.ibm.com ([122.248.162.3]:51746) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UP4kh-00036E-Qx for qemu-devel@nongnu.org; Mon, 08 Apr 2013 01:36:24 -0400 Received: from /spool/local by e28smtp03.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 8 Apr 2013 11:02:34 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id BD676E0055 for ; Mon, 8 Apr 2013 11:08:05 +0530 (IST) Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r385aEte9109894 for ; Mon, 8 Apr 2013 11:06:14 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r385aFh8005119 for ; Mon, 8 Apr 2013 05:36:16 GMT From: Liu Ping Fan Date: Mon, 8 Apr 2013 13:36:05 +0800 Message-Id: <1365399368-26967-3-git-send-email-pingfank@linux.vnet.ibm.com> In-Reply-To: <1365399368-26967-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1365399368-26967-1-git-send-email-pingfank@linux.vnet.ibm.com> Subject: [Qemu-devel] [RFC PATCH v3 2/5] net: port tap onto glib List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Paolo Bonzini , Stefan Hajnoczi , Anthony Liguori , mdroth Signed-off-by: Liu Ping Fan --- net/tap.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 47 insertions(+), 11 deletions(-) diff --git a/net/tap.c b/net/tap.c index daab350..e19bb07 100644 --- a/net/tap.c +++ b/net/tap.c @@ -70,25 +70,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) +{ + NetClientSource *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) @@ -298,6 +321,7 @@ 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); } @@ -309,6 +333,11 @@ int tap_get_fd(NetClientState *nc) return s->fd; } +static void tap_bind_ctx(NetClientState *nc, GMainContext *ctx) +{ + g_source_attach(&nc->nsrc->source, ctx); +} + /* fd support */ static NetClientInfo net_tap_info = { @@ -319,6 +348,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,13 +626,19 @@ static int net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, int vnet_hdr, int fd) { TAPState *s; + NetClientSource *nsrc; s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); if (!s) { close(fd); return -1; } - + nsrc = net_source_new(s->fd, tap_handler, s); + nsrc->gfd.events = G_IO_IN|G_IO_OUT; + nsrc->readable = readable; + nsrc->writable = writable; + s->nc.nsrc = nsrc; + s->nc.info->bind_ctx(&s->nc, NULL); if (tap_set_sndbuf(s->fd, tap) < 0) { return -1; } @@ -843,8 +879,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 +897,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; } -- 1.7.4.4