From patchwork Thu Feb 4 08:31:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 8214841 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id BC6459F6DA for ; Thu, 4 Feb 2016 08:37:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0C20C2037E for ; Thu, 4 Feb 2016 08:37:36 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2EEA92037C for ; Thu, 4 Feb 2016 08:37:35 +0000 (UTC) Received: from localhost ([::1]:40275 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRFPy-0000Gn-Ge for patchwork-qemu-devel@patchwork.kernel.org; Thu, 04 Feb 2016 03:37:34 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35217) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRFLS-0008IQ-9H for qemu-devel@nongnu.org; Thu, 04 Feb 2016 03:32:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aRFLP-0007tc-3H for qemu-devel@nongnu.org; Thu, 04 Feb 2016 03:32:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43885) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aRFLO-0007t0-RL for qemu-devel@nongnu.org; Thu, 04 Feb 2016 03:32:51 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 7BAA2C0C235F; Thu, 4 Feb 2016 08:32:50 +0000 (UTC) Received: from jason-ThinkPad-T430s.redhat.com (vpn1-7-60.pek2.redhat.com [10.72.7.60]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u148VnLM022765; Thu, 4 Feb 2016 03:32:45 -0500 From: Jason Wang To: peter.maydell@linaro.org, qemu-devel@nongnu.org Date: Thu, 4 Feb 2016 16:31:39 +0800 Message-Id: <1454574706-5681-11-git-send-email-jasowang@redhat.com> In-Reply-To: <1454574706-5681-1-git-send-email-jasowang@redhat.com> References: <1454574706-5681-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Samuel Thibault , Jason Wang , Guillaume Subiron Subject: [Qemu-devel] [PULL V2 10/17] slirp: Factorizing and cleaning solookup() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Guillaume Subiron solookup() was only compatible with TCP. Having the socket list in argument, it is now compatible with UDP too. Some optimization code is factorized inside the function (the function look at the last returned result before browsing the complete socket list). This prepares for IPv6 support. Signed-off-by: Guillaume Subiron Signed-off-by: Samuel Thibault Reviewed-by: Thomas Huth Signed-off-by: Jason Wang --- slirp/socket.c | 37 ++++++++++++++++++++++++------------- slirp/socket.h | 5 +++-- slirp/tcp_input.c | 13 +++---------- slirp/udp.c | 21 ++------------------- 4 files changed, 32 insertions(+), 44 deletions(-) diff --git a/slirp/socket.c b/slirp/socket.c index d1034fb..8f73e90 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -16,23 +16,34 @@ static void sofcantrcvmore(struct socket *so); static void sofcantsendmore(struct socket *so); struct socket * -solookup(struct socket *head, struct in_addr laddr, u_int lport, +solookup(struct socket **last, struct socket *head, + struct in_addr laddr, u_int lport, struct in_addr faddr, u_int fport) { - struct socket *so; - - for (so = head->so_next; so != head; so = so->so_next) { - if (so->so_lport == lport && - so->so_laddr.s_addr == laddr.s_addr && - so->so_faddr.s_addr == faddr.s_addr && - so->so_fport == fport) - break; - } + struct socket *so = *last; + + /* Optimisation */ + if (so != head && + so->so_lport == lport && + so->so_laddr.s_addr == laddr.s_addr && + (!faddr.s_addr || + (so->so_faddr.s_addr == faddr.s_addr && + so->so_fport == fport))) { + return so; + } - if (so == head) - return (struct socket *)NULL; - return so; + for (so = head->so_next; so != head; so = so->so_next) { + if (so->so_lport == lport && + so->so_laddr.s_addr == laddr.s_addr && + (!faddr.s_addr || + (so->so_faddr.s_addr == faddr.s_addr && + so->so_fport == fport))) { + *last = so; + return so; + } + } + return (struct socket *)NULL; } /* diff --git a/slirp/socket.h b/slirp/socket.h index b27bbb2..1c8c24c 100644 --- a/slirp/socket.h +++ b/slirp/socket.h @@ -87,8 +87,9 @@ struct socket { #define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */ #define SS_INCOMING 0x2000 /* Connection was initiated by a host on the internet */ -struct socket * solookup(struct socket *, struct in_addr, u_int, struct in_addr, u_int); -struct socket * socreate(Slirp *); +struct socket *solookup(struct socket **, struct socket *, + struct in_addr, u_int, struct in_addr, u_int); +struct socket *socreate(Slirp *); void sofree(struct socket *); int soread(struct socket *); void sorecvoob(struct socket *); diff --git a/slirp/tcp_input.c b/slirp/tcp_input.c index 4c3191d..5492061 100644 --- a/slirp/tcp_input.c +++ b/slirp/tcp_input.c @@ -320,16 +320,9 @@ tcp_input(struct mbuf *m, int iphlen, struct socket *inso) * Locate pcb for segment. */ findso: - so = slirp->tcp_last_so; - if (so->so_fport != ti->ti_dport || - so->so_lport != ti->ti_sport || - so->so_laddr.s_addr != ti->ti_src.s_addr || - so->so_faddr.s_addr != ti->ti_dst.s_addr) { - so = solookup(&slirp->tcb, ti->ti_src, ti->ti_sport, - ti->ti_dst, ti->ti_dport); - if (so) - slirp->tcp_last_so = so; - } + so = solookup(&slirp->tcp_last_so, &slirp->tcb, + ti->ti_src, ti->ti_sport, + ti->ti_dst, ti->ti_dport); /* * If the state is CLOSED (i.e., TCB does not exist) then diff --git a/slirp/udp.c b/slirp/udp.c index 8203eb1..126ef82 100644 --- a/slirp/udp.c +++ b/slirp/udp.c @@ -151,25 +151,8 @@ udp_input(register struct mbuf *m, int iphlen) /* * Locate pcb for datagram. */ - so = slirp->udp_last_so; - if (so == &slirp->udb || so->so_lport != uh->uh_sport || - so->so_laddr.s_addr != ip->ip_src.s_addr) { - struct socket *tmp; - - for (tmp = slirp->udb.so_next; tmp != &slirp->udb; - tmp = tmp->so_next) { - if (tmp->so_lport == uh->uh_sport && - tmp->so_laddr.s_addr == ip->ip_src.s_addr) { - so = tmp; - break; - } - } - if (tmp == &slirp->udb) { - so = NULL; - } else { - slirp->udp_last_so = so; - } - } + so = solookup(&slirp->udp_last_so, &slirp->udb, + ip->ip_src, uh->uh_sport, (struct in_addr) {0}, 0); if (so == NULL) { /*