aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Ahern <dsa@cumulusnetworks.com>2015-08-27 19:07:01 -0400
committerDavid S. Miller <davem@davemloft.net>2015-08-28 16:32:36 -0400
commitd39d14ffa24cca9f0e44aa4a63315f4c44c56a93 (patch)
treed19cc95a22ad69e2164b9b46609ce67c577af77f
parent3abef286cf2f138de353fb0b54453621de961043 (diff)
net: Add helper function to compare inetpeer addresses
tcp_metrics and inetpeer both have functions to compare inetpeer addresses. Consolidate into 1 version. Signed-off-by: David Ahern <dsa@cumulusnetworks.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/net/inetpeer.h16
-rw-r--r--net/ipv4/inetpeer.c20
-rw-r--r--net/ipv4/tcp_metrics.c6
3 files changed, 19 insertions, 23 deletions
diff --git a/include/net/inetpeer.h b/include/net/inetpeer.h
index f75b9e7036a2..9d9b3446731d 100644
--- a/include/net/inetpeer.h
+++ b/include/net/inetpeer.h
@@ -121,6 +121,22 @@ static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
121 return inet_getpeer(base, &daddr, create); 121 return inet_getpeer(base, &daddr, create);
122} 122}
123 123
124static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
125 const struct inetpeer_addr *b)
126{
127 int i, n = (a->family == AF_INET ? 1 : 4);
128
129 for (i = 0; i < n; i++) {
130 if (a->addr.a6[i] == b->addr.a6[i])
131 continue;
132 if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
133 return -1;
134 return 1;
135 }
136
137 return 0;
138}
139
124/* can be called from BH context or outside */ 140/* can be called from BH context or outside */
125void inet_putpeer(struct inet_peer *p); 141void inet_putpeer(struct inet_peer *p);
126bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout); 142bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
diff --git a/net/ipv4/inetpeer.c b/net/ipv4/inetpeer.c
index 241afd743d2c..86fa45809540 100644
--- a/net/ipv4/inetpeer.c
+++ b/net/ipv4/inetpeer.c
@@ -157,22 +157,6 @@ void __init inet_initpeers(void)
157 INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker); 157 INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker);
158} 158}
159 159
160static int addr_compare(const struct inetpeer_addr *a,
161 const struct inetpeer_addr *b)
162{
163 int i, n = (a->family == AF_INET ? 1 : 4);
164
165 for (i = 0; i < n; i++) {
166 if (a->addr.a6[i] == b->addr.a6[i])
167 continue;
168 if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
169 return -1;
170 return 1;
171 }
172
173 return 0;
174}
175
176#define rcu_deref_locked(X, BASE) \ 160#define rcu_deref_locked(X, BASE) \
177 rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock)) 161 rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock))
178 162
@@ -188,7 +172,7 @@ static int addr_compare(const struct inetpeer_addr *a,
188 *stackptr++ = &_base->root; \ 172 *stackptr++ = &_base->root; \
189 for (u = rcu_deref_locked(_base->root, _base); \ 173 for (u = rcu_deref_locked(_base->root, _base); \
190 u != peer_avl_empty;) { \ 174 u != peer_avl_empty;) { \
191 int cmp = addr_compare(_daddr, &u->daddr); \ 175 int cmp = inetpeer_addr_cmp(_daddr, &u->daddr); \
192 if (cmp == 0) \ 176 if (cmp == 0) \
193 break; \ 177 break; \
194 if (cmp == -1) \ 178 if (cmp == -1) \
@@ -215,7 +199,7 @@ static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
215 int count = 0; 199 int count = 0;
216 200
217 while (u != peer_avl_empty) { 201 while (u != peer_avl_empty) {
218 int cmp = addr_compare(daddr, &u->daddr); 202 int cmp = inetpeer_addr_cmp(daddr, &u->daddr);
219 if (cmp == 0) { 203 if (cmp == 0) {
220 /* Before taking a reference, check if this entry was 204 /* Before taking a reference, check if this entry was
221 * deleted (refcnt=-1) 205 * deleted (refcnt=-1)
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index 4ef4dd4bf38c..c8cbc2b4b792 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -81,11 +81,7 @@ static void tcp_metric_set(struct tcp_metrics_block *tm,
81static bool addr_same(const struct inetpeer_addr *a, 81static bool addr_same(const struct inetpeer_addr *a,
82 const struct inetpeer_addr *b) 82 const struct inetpeer_addr *b)
83{ 83{
84 if (a->family != b->family) 84 return inetpeer_addr_cmp(a, b) == 0;
85 return false;
86 if (a->family == AF_INET)
87 return a->addr.a4 == b->addr.a4;
88 return ipv6_addr_equal(&a->addr.in6, &b->addr.in6);
89} 85}
90 86
91struct tcpm_hash_bucket { 87struct tcpm_hash_bucket {