From patchwork Fri Jan 23 06:02:33 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Timo Teras X-Patchwork-Id: 19976 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by ozlabs.org (Postfix) with ESMTP id D1CB8DDF45 for ; Fri, 23 Jan 2009 17:02:46 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751364AbZAWGCk (ORCPT ); Fri, 23 Jan 2009 01:02:40 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1751302AbZAWGCj (ORCPT ); Fri, 23 Jan 2009 01:02:39 -0500 Received: from fg-out-1718.google.com ([72.14.220.157]:10199 "EHLO fg-out-1718.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751026AbZAWGCj (ORCPT ); Fri, 23 Jan 2009 01:02:39 -0500 Received: by fg-out-1718.google.com with SMTP id 19so2418788fgg.17 for ; Thu, 22 Jan 2009 22:02:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:sender:from:to:cc:subject :date:message-id:x-mailer; bh=bLQHh9z/2SbsNSZ2qE+FVs8M/fyxtKVzlKWySBe+M2E=; b=ZlCJwFQB35w6JjyJLy9/uhr0rPxdZNmjJg9NHgb9lQkqmtdg40EUZ5RTwuexiWmHkC 9rg25mKf1yqB2Af+45JhW0ogaYJgkQAFLDEOUaq753c3cBG9sgmuU4635J+1CPOWB9Y7 /jaJYJtsahKGy5nxQ0Hov9xiGsu4L9SQ12YyU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:cc:subject:date:message-id:x-mailer; b=o+7KXTVNJw6Z1rwjyqmhaVUX1mLOhpR0So33tslGgK4RFBgBlDkiE32/Ojuqk7HKYf ZAKL4qS/QCePhiuXZeVKD5GnX8XZJJP23unjRZLOgfi0Wv/OoUw7VIcFDNsdol+hWwrk 8JY3me1B8Cxgl61K+ZTheugU4V6vwjd/UWnHU= Received: by 10.223.106.69 with SMTP id w5mr1427653fao.17.1232690557183; Thu, 22 Jan 2009 22:02:37 -0800 (PST) Received: from localhost.localdomain (xdsl-83-150-94-239.nebulazone.fi [83.150.94.239]) by mx.google.com with ESMTPS id y15sm18042618fkd.12.2009.01.22.22.02.36 (version=SSLv3 cipher=RC4-MD5); Thu, 22 Jan 2009 22:02:36 -0800 (PST) From: Timo Teras To: netdev@vger.kernel.org Cc: Timo Teras Subject: [PATCH net-next] gre: optimize hash lookup Date: Fri, 23 Jan 2009 08:02:33 +0200 Message-Id: <1232690553-31865-1-git-send-email-timo.teras@iki.fi> X-Mailer: git-send-email 1.5.6.3 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Instead of keeping candidate tunnel device from all categories, keep only one candidate with best score. This optimizes stack usage and speeds up exit code. Signed-off-by: Timo Teras --- net/ipv4/ip_gre.c | 69 ++++++++++++++++++++++++++++++---------------------- 1 files changed, 40 insertions(+), 29 deletions(-) diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 4a43739..07a188a 100644 --- a/net/ipv4/ip_gre.c +++ b/net/ipv4/ip_gre.c @@ -172,11 +172,11 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, int link = dev->ifindex; unsigned h0 = HASH(remote); unsigned h1 = HASH(key); - struct ip_tunnel *t, *sel[4] = { NULL, NULL, NULL, NULL }; + struct ip_tunnel *t, *cand = NULL; struct ipgre_net *ign = net_generic(net, ipgre_net_id); int dev_type = (gre_proto == htons(ETH_P_TEB)) ? ARPHRD_ETHER : ARPHRD_IPGRE; - int idx; + int score, cand_score = 4; for (t = ign->tunnels_r_l[h0^h1]; t; t = t->next) { if (local != t->parms.iph.saddr || @@ -189,15 +189,18 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, t->dev->type != dev_type) continue; - idx = 0; + score = 0; if (t->parms.link != link) - idx |= 1; + score |= 1; if (t->dev->type != dev_type) - idx |= 2; - if (idx == 0) + score |= 2; + if (score == 0) return t; - if (sel[idx] == NULL) - sel[idx] = t; + + if (score < cand_score) { + cand = t; + cand_score = score; + } } for (t = ign->tunnels_r[h0^h1]; t; t = t->next) { @@ -210,15 +213,18 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, t->dev->type != dev_type) continue; - idx = 0; + score = 0; if (t->parms.link != link) - idx |= 1; + score |= 1; if (t->dev->type != dev_type) - idx |= 2; - if (idx == 0) + score |= 2; + if (score == 0) return t; - if (sel[idx] == NULL) - sel[idx] = t; + + if (score < cand_score) { + cand = t; + cand_score = score; + } } for (t = ign->tunnels_l[h1]; t; t = t->next) { @@ -233,15 +239,18 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, t->dev->type != dev_type) continue; - idx = 0; + score = 0; if (t->parms.link != link) - idx |= 1; + score |= 1; if (t->dev->type != dev_type) - idx |= 2; - if (idx == 0) + score |= 2; + if (score == 0) return t; - if (sel[idx] == NULL) - sel[idx] = t; + + if (score < cand_score) { + cand = t; + cand_score = score; + } } for (t = ign->tunnels_wc[h1]; t; t = t->next) { @@ -253,20 +262,22 @@ static struct ip_tunnel * ipgre_tunnel_lookup(struct net_device *dev, t->dev->type != dev_type) continue; - idx = 0; + score = 0; if (t->parms.link != link) - idx |= 1; + score |= 1; if (t->dev->type != dev_type) - idx |= 2; - if (idx == 0) + score |= 2; + if (score == 0) return t; - if (sel[idx] == NULL) - sel[idx] = t; + + if (score < cand_score) { + cand = t; + cand_score = score; + } } - for (idx = 1; idx < ARRAY_SIZE(sel); idx++) - if (sel[idx] != NULL) - return sel[idx]; + if (cand != NULL) + return cand; if (ign->fb_tunnel_dev->flags & IFF_UP) return netdev_priv(ign->fb_tunnel_dev);