All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cong Wang <amwang@redhat.com>
To: netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <eric.dumazet@gmail.com>,
	Cong Wang <amwang@redhat.com>
Subject: [Patch net-next v2 3/8] inetpeer: use generic struct in_addr_gen
Date: Fri,  2 Aug 2013 15:14:29 +0800	[thread overview]
Message-ID: <1375427674-21735-4-git-send-email-amwang@redhat.com> (raw)
In-Reply-To: <1375427674-21735-1-git-send-email-amwang@redhat.com>

From: Cong Wang <amwang@redhat.com>

Similar definition of struct inetpeer_addr is used by bridge multciast
code too, therefore it makes sense to introduce a generic struct
in_addr_gen, so that code can be shared.

There is no more overhead for inet_peer structs now.

Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Cong Wang <amwang@redhat.com>
---
 include/net/inet_addr.h |   22 ++++++++++++++
 include/net/inetpeer.h  |   25 ++++-----------
 net/ipv4/inetpeer.c     |   33 +++++++++++++-------
 net/ipv4/tcp_metrics.c  |   74 ++++++++++++++++++----------------------------
 4 files changed, 79 insertions(+), 75 deletions(-)

diff --git a/include/net/inet_addr.h b/include/net/inet_addr.h
index 7289aed..ee80df4 100644
--- a/include/net/inet_addr.h
+++ b/include/net/inet_addr.h
@@ -13,6 +13,28 @@ union inet_addr {
 	struct sockaddr sa;
 };
 
+/* a simpler definition of generic inet addr, without additional
+ * information for IPv6 address.
+ */
+struct in_addr_gen {
+	union {
+		struct in_addr	in_addr;
+		struct in6_addr	in6_addr;
+	};
+	unsigned short int	family;
+};
+
+static inline
+bool in_addr_gen_equal(const struct in_addr_gen *a, const struct in_addr_gen *b)
+{
+	if (a->family != b->family)
+		return false;
+	if (a->family == AF_INET6)
+		return ipv6_addr_equal(&a->in6_addr, &b->in6_addr);
+	else
+		return a->in_addr.s_addr == b->in_addr.s_addr;
+}
+
 #if IS_ENABLED(CONFIG_IPV6)
 static inline
 bool inet_addr_equal(const union inet_addr *a, const union inet_addr *b)
diff --git a/include/net/inetpeer.h b/include/net/inetpeer.h
index 53f464d..9574843 100644
--- a/include/net/inetpeer.h
+++ b/include/net/inetpeer.h
@@ -13,24 +13,13 @@
 #include <linux/spinlock.h>
 #include <linux/rtnetlink.h>
 #include <net/ipv6.h>
+#include <net/inet_addr.h>
 #include <linux/atomic.h>
 
-struct inetpeer_addr_base {
-	union {
-		__be32			a4;
-		__be32			a6[4];
-	};
-};
-
-struct inetpeer_addr {
-	struct inetpeer_addr_base	addr;
-	__u16				family;
-};
-
 struct inet_peer {
 	/* group together avl_left,avl_right,v4daddr to speedup lookups */
 	struct inet_peer __rcu	*avl_left, *avl_right;
-	struct inetpeer_addr	daddr;
+	struct in_addr_gen	daddr;
 	__u32			avl_height;
 
 	u32			metrics[RTAX_MAX];
@@ -133,16 +122,16 @@ static inline bool inet_metrics_new(const struct inet_peer *p)
 
 /* can be called with or without local BH being disabled */
 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
-			       const struct inetpeer_addr *daddr,
+			       const struct in_addr_gen *daddr,
 			       int create);
 
 static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
 						__be32 v4daddr,
 						int create)
 {
-	struct inetpeer_addr daddr;
+	struct in_addr_gen daddr;
 
-	daddr.addr.a4 = v4daddr;
+	daddr.in_addr.s_addr = v4daddr;
 	daddr.family = AF_INET;
 	return inet_getpeer(base, &daddr, create);
 }
@@ -151,9 +140,9 @@ static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
 						const struct in6_addr *v6daddr,
 						int create)
 {
-	struct inetpeer_addr daddr;
+	struct in_addr_gen daddr;
 
-	*(struct in6_addr *)daddr.addr.a6 = *v6daddr;
+	daddr.in6_addr = *v6daddr;
 	daddr.family = AF_INET6;
 	return inet_getpeer(base, &daddr, create);
 }
diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
index 000e3d2..70ffc9e 100644
--- a/net/ipv4/inetpeer.c
+++ b/net/ipv4/inetpeer.c
@@ -197,17 +197,26 @@ void __init inet_initpeers(void)
 	INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker);
 }
 
-static int addr_compare(const struct inetpeer_addr *a,
-			const struct inetpeer_addr *b)
+static int addr_compare(const struct in_addr_gen *a,
+			const struct in_addr_gen *b)
 {
-	int i, n = (a->family == AF_INET ? 1 : 4);
+	int i;
 
-	for (i = 0; i < n; i++) {
-		if (a->addr.a6[i] == b->addr.a6[i])
-			continue;
-		if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
+	if (a->family == AF_INET) {
+		if (a->in_addr.s_addr == b->in_addr.s_addr)
+			return 0;
+		if ((__force u32)a->in_addr.s_addr <
+		    (__force u32)b->in_addr.s_addr)
 			return -1;
-		return 1;
+	} else {
+		for (i = 0; i < 4; i++) {
+			if (a->in6_addr.s6_addr32[i] == b->in6_addr.s6_addr32[i])
+				continue;
+			if ((__force u32)a->in6_addr.s6_addr32[i] <
+			    (__force u32)b->in6_addr.s6_addr32[i])
+				return -1;
+			return 1;
+		}
 	}
 
 	return 0;
@@ -248,7 +257,7 @@ static int addr_compare(const struct inetpeer_addr *a,
  * But every pointer we follow is guaranteed to be valid thanks to RCU.
  * We exit from this function if number of links exceeds PEER_MAXDEPTH
  */
-static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
+static struct inet_peer *lookup_rcu(const struct in_addr_gen *daddr,
 				    struct inet_peer_base *base)
 {
 	struct inet_peer *u = rcu_dereference(base->root);
@@ -457,7 +466,7 @@ static int inet_peer_gc(struct inet_peer_base *base,
 }
 
 struct inet_peer *inet_getpeer(struct inet_peer_base *base,
-			       const struct inetpeer_addr *daddr,
+			       const struct in_addr_gen *daddr,
 			       int create)
 {
 	struct inet_peer __rcu **stack[PEER_MAXDEPTH], ***stackptr;
@@ -506,8 +515,8 @@ relookup:
 		atomic_set(&p->rid, 0);
 		atomic_set(&p->ip_id_count,
 				(daddr->family == AF_INET) ?
-					secure_ip_id(daddr->addr.a4) :
-					secure_ipv6_id(daddr->addr.a6));
+					secure_ip_id(daddr->in_addr.s_addr) :
+					secure_ipv6_id(daddr->in6_addr.s6_addr32));
 		p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
 		p->rate_tokens = 0;
 		/* 60*HZ is arbitrary, but chosen enough high so that the first
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index f6a005c..3bb3789 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -31,7 +31,7 @@ struct tcp_fastopen_metrics {
 
 struct tcp_metrics_block {
 	struct tcp_metrics_block __rcu	*tcpm_next;
-	struct inetpeer_addr		tcpm_addr;
+	struct in_addr_gen		tcpm_addr;
 	unsigned long			tcpm_stamp;
 	u32				tcpm_ts;
 	u32				tcpm_ts_stamp;
@@ -74,22 +74,6 @@ static void tcp_metric_set_msecs(struct tcp_metrics_block *tm,
 	tm->tcpm_vals[idx] = jiffies_to_msecs(val);
 }
 
-static bool addr_same(const struct inetpeer_addr *a,
-		      const struct inetpeer_addr *b)
-{
-	const struct in6_addr *a6, *b6;
-
-	if (a->family != b->family)
-		return false;
-	if (a->family == AF_INET)
-		return a->addr.a4 == b->addr.a4;
-
-	a6 = (const struct in6_addr *) &a->addr.a6[0];
-	b6 = (const struct in6_addr *) &b->addr.a6[0];
-
-	return ipv6_addr_equal(a6, b6);
-}
-
 struct tcpm_hash_bucket {
 	struct tcp_metrics_block __rcu	*chain;
 };
@@ -131,7 +115,7 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst,
 }
 
 static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
-					  struct inetpeer_addr *addr,
+					  struct in_addr_gen *addr,
 					  unsigned int hash,
 					  bool reclaim)
 {
@@ -189,7 +173,7 @@ static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, in
 	return NULL;
 }
 
-static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
+static struct tcp_metrics_block *__tcp_get_metrics(const struct in_addr_gen *addr,
 						   struct net *net, unsigned int hash)
 {
 	struct tcp_metrics_block *tm;
@@ -197,7 +181,7 @@ static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *a
 
 	for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
 	     tm = rcu_dereference(tm->tcpm_next)) {
-		if (addr_same(&tm->tcpm_addr, addr))
+		if (in_addr_gen_equal(&tm->tcpm_addr, addr))
 			break;
 		depth++;
 	}
@@ -208,18 +192,18 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
 						       struct dst_entry *dst)
 {
 	struct tcp_metrics_block *tm;
-	struct inetpeer_addr addr;
+	struct in_addr_gen addr;
 	unsigned int hash;
 	struct net *net;
 
 	addr.family = req->rsk_ops->family;
 	switch (addr.family) {
 	case AF_INET:
-		addr.addr.a4 = inet_rsk(req)->rmt_addr;
-		hash = (__force unsigned int) addr.addr.a4;
+		addr.in_addr.s_addr = inet_rsk(req)->rmt_addr;
+		hash = (__force unsigned int) addr.in_addr.s_addr;
 		break;
 	case AF_INET6:
-		*(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr;
+		addr.in6_addr = inet6_rsk(req)->rmt_addr;
 		hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr);
 		break;
 	default:
@@ -231,7 +215,7 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
 
 	for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
 	     tm = rcu_dereference(tm->tcpm_next)) {
-		if (addr_same(&tm->tcpm_addr, &addr))
+		if (in_addr_gen_equal(&tm->tcpm_addr, &addr))
 			break;
 	}
 	tcpm_check_stamp(tm, dst);
@@ -242,19 +226,19 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
 {
 	struct inet6_timewait_sock *tw6;
 	struct tcp_metrics_block *tm;
-	struct inetpeer_addr addr;
+	struct in_addr_gen addr;
 	unsigned int hash;
 	struct net *net;
 
 	addr.family = tw->tw_family;
 	switch (addr.family) {
 	case AF_INET:
-		addr.addr.a4 = tw->tw_daddr;
-		hash = (__force unsigned int) addr.addr.a4;
+		addr.in_addr.s_addr = tw->tw_daddr;
+		hash = (__force unsigned int) addr.in_addr.s_addr;
 		break;
 	case AF_INET6:
 		tw6 = inet6_twsk((struct sock *)tw);
-		*(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr;
+		addr.in6_addr = tw6->tw_v6_daddr;
 		hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
 		break;
 	default:
@@ -266,7 +250,7 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
 
 	for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
 	     tm = rcu_dereference(tm->tcpm_next)) {
-		if (addr_same(&tm->tcpm_addr, &addr))
+		if (in_addr_gen_equal(&tm->tcpm_addr, &addr))
 			break;
 	}
 	return tm;
@@ -277,7 +261,7 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
 						 bool create)
 {
 	struct tcp_metrics_block *tm;
-	struct inetpeer_addr addr;
+	struct in_addr_gen addr;
 	unsigned int hash;
 	struct net *net;
 	bool reclaim;
@@ -285,11 +269,11 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
 	addr.family = sk->sk_family;
 	switch (addr.family) {
 	case AF_INET:
-		addr.addr.a4 = inet_sk(sk)->inet_daddr;
-		hash = (__force unsigned int) addr.addr.a4;
+		addr.in_addr.s_addr = inet_sk(sk)->inet_daddr;
+		hash = (__force unsigned int) addr.in_addr.s_addr;
 		break;
 	case AF_INET6:
-		*(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr;
+		addr.in6_addr = inet6_sk(sk)->daddr;
 		hash = ipv6_addr_hash(&inet6_sk(sk)->daddr);
 		break;
 	default:
@@ -725,12 +709,12 @@ static int tcp_metrics_fill_info(struct sk_buff *msg,
 	switch (tm->tcpm_addr.family) {
 	case AF_INET:
 		if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4,
-				tm->tcpm_addr.addr.a4) < 0)
+				tm->tcpm_addr.in_addr.s_addr) < 0)
 			goto nla_put_failure;
 		break;
 	case AF_INET6:
 		if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16,
-			    tm->tcpm_addr.addr.a6) < 0)
+			    tm->tcpm_addr.in6_addr.s6_addr32) < 0)
 			goto nla_put_failure;
 		break;
 	default:
@@ -853,7 +837,7 @@ done:
 	return skb->len;
 }
 
-static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
+static int parse_nl_addr(struct genl_info *info, struct in_addr_gen *addr,
 			 unsigned int *hash, int optional)
 {
 	struct nlattr *a;
@@ -861,8 +845,8 @@ static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
 	a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV4];
 	if (a) {
 		addr->family = AF_INET;
-		addr->addr.a4 = nla_get_be32(a);
-		*hash = (__force unsigned int) addr->addr.a4;
+		addr->in_addr.s_addr = nla_get_be32(a);
+		*hash = (__force unsigned int) addr->in_addr.s_addr;
 		return 0;
 	}
 	a = info->attrs[TCP_METRICS_ATTR_ADDR_IPV6];
@@ -870,8 +854,8 @@ static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
 		if (nla_len(a) != sizeof(struct in6_addr))
 			return -EINVAL;
 		addr->family = AF_INET6;
-		memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6));
-		*hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6);
+		memcpy(&addr->in6_addr, nla_data(a), sizeof(addr->in6_addr));
+		*hash = ipv6_addr_hash(&addr->in6_addr);
 		return 0;
 	}
 	return optional ? 1 : -EAFNOSUPPORT;
@@ -880,7 +864,7 @@ static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
 static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
 {
 	struct tcp_metrics_block *tm;
-	struct inetpeer_addr addr;
+	struct in_addr_gen addr;
 	unsigned int hash;
 	struct sk_buff *msg;
 	struct net *net = genl_info_net(info);
@@ -905,7 +889,7 @@ static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
 	rcu_read_lock();
 	for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
 	     tm = rcu_dereference(tm->tcpm_next)) {
-		if (addr_same(&tm->tcpm_addr, &addr)) {
+		if (in_addr_gen_equal(&tm->tcpm_addr, &addr)) {
 			ret = tcp_metrics_fill_info(msg, tm);
 			break;
 		}
@@ -960,7 +944,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
 	struct tcpm_hash_bucket *hb;
 	struct tcp_metrics_block *tm;
 	struct tcp_metrics_block __rcu **pp;
-	struct inetpeer_addr addr;
+	struct in_addr_gen addr;
 	unsigned int hash;
 	struct net *net = genl_info_net(info);
 	int ret;
@@ -977,7 +961,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
 	spin_lock_bh(&tcp_metrics_lock);
 	for (tm = deref_locked_genl(*pp); tm;
 	     pp = &tm->tcpm_next, tm = deref_locked_genl(*pp)) {
-		if (addr_same(&tm->tcpm_addr, &addr)) {
+		if (in_addr_gen_equal(&tm->tcpm_addr, &addr)) {
 			*pp = tm->tcpm_next;
 			break;
 		}
-- 
1.7.7.6

  parent reply	other threads:[~2013-08-02  7:15 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-02  7:14 [Patch net-next v2 0/8] net: introduce generic type and helpers for IP address Cong Wang
2013-08-02  7:14 ` [Patch net-next v2 1/8] net: introduce generic union inet_addr Cong Wang
2013-08-02 21:50   ` David Miller
2013-08-05  3:09     ` Cong Wang
2013-08-02  7:14 ` [Patch net-next v2 2/8] net: introduce generic simple_inet_pton() Cong Wang
2013-08-02  7:14 ` Cong Wang [this message]
2013-08-02  7:14 ` [Patch net-next v2 4/8] bridge: use generic struct in_addr_gen Cong Wang
     [not found] ` <1375427674-21735-1-git-send-email-amwang-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2013-08-02  7:14   ` [Patch net-next v2 5/8] sunrpc: use generic union inet_addr Cong Wang
2013-08-02  7:14     ` Cong Wang
2013-08-02 13:36     ` Jeff Layton
     [not found]       ` <20130802093625.2c70a330-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2013-08-05  3:14         ` Cong Wang
2013-08-05  3:14           ` Cong Wang
2013-08-06 10:28           ` Jeff Layton
2013-08-06 10:28             ` Jeff Layton
     [not found]             ` <20130806062801.67714276-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2013-08-07 12:27               ` Cong Wang
2013-08-07 12:27                 ` Cong Wang
2013-08-07 13:21                 ` Jeff Layton
     [not found]                   ` <20130807092123.451e93db-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org>
2013-08-08  1:37                     ` Cong Wang
2013-08-08  1:37                       ` Cong Wang
2013-08-07 13:30                 ` Jim Rees
2013-08-07 13:30                   ` Jim Rees
2013-08-02  7:14 ` [Patch net-next v2 6/8] fs: use generic union inet_addr and helper functions Cong Wang
2013-08-02  7:14   ` [Cluster-devel] " Cong Wang
2013-08-02 10:31   ` Christoph Hellwig
2013-08-02 10:31     ` Christoph Hellwig
     [not found]     ` <20130802103107.GA17244-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>
2013-08-05  3:16       ` Cong Wang
2013-08-05  3:16         ` Cong Wang
2013-08-05  3:16         ` Cong Wang
2013-08-02  7:14 ` [Patch net-next v2 7/8] sctp: use generic union inet_addr Cong Wang
2013-08-02  7:14 ` [Patch net-next v2 8/8] selinux: " Cong Wang
2013-08-02 14:34   ` Paul Moore
2013-08-02 21:51     ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1375427674-21735-4-git-send-email-amwang@redhat.com \
    --to=amwang@redhat.com \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.