提交 d39d14ff 编写于 作者: D David Ahern 提交者: David S. Miller

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: NDavid Ahern <dsa@cumulusnetworks.com>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 3abef286
...@@ -121,6 +121,22 @@ static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base, ...@@ -121,6 +121,22 @@ static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
return inet_getpeer(base, &daddr, create); return inet_getpeer(base, &daddr, create);
} }
static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
const struct inetpeer_addr *b)
{
int i, n = (a->family == AF_INET ? 1 : 4);
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])
return -1;
return 1;
}
return 0;
}
/* can be called from BH context or outside */ /* can be called from BH context or outside */
void inet_putpeer(struct inet_peer *p); void inet_putpeer(struct inet_peer *p);
bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout); bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
......
...@@ -157,22 +157,6 @@ void __init inet_initpeers(void) ...@@ -157,22 +157,6 @@ void __init inet_initpeers(void)
INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker); INIT_DEFERRABLE_WORK(&gc_work, inetpeer_gc_worker);
} }
static int addr_compare(const struct inetpeer_addr *a,
const struct inetpeer_addr *b)
{
int i, n = (a->family == AF_INET ? 1 : 4);
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])
return -1;
return 1;
}
return 0;
}
#define rcu_deref_locked(X, BASE) \ #define rcu_deref_locked(X, BASE) \
rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock)) rcu_dereference_protected(X, lockdep_is_held(&(BASE)->lock.lock))
...@@ -188,7 +172,7 @@ static int addr_compare(const struct inetpeer_addr *a, ...@@ -188,7 +172,7 @@ static int addr_compare(const struct inetpeer_addr *a,
*stackptr++ = &_base->root; \ *stackptr++ = &_base->root; \
for (u = rcu_deref_locked(_base->root, _base); \ for (u = rcu_deref_locked(_base->root, _base); \
u != peer_avl_empty;) { \ u != peer_avl_empty;) { \
int cmp = addr_compare(_daddr, &u->daddr); \ int cmp = inetpeer_addr_cmp(_daddr, &u->daddr); \
if (cmp == 0) \ if (cmp == 0) \
break; \ break; \
if (cmp == -1) \ if (cmp == -1) \
...@@ -215,7 +199,7 @@ static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr, ...@@ -215,7 +199,7 @@ static struct inet_peer *lookup_rcu(const struct inetpeer_addr *daddr,
int count = 0; int count = 0;
while (u != peer_avl_empty) { while (u != peer_avl_empty) {
int cmp = addr_compare(daddr, &u->daddr); int cmp = inetpeer_addr_cmp(daddr, &u->daddr);
if (cmp == 0) { if (cmp == 0) {
/* Before taking a reference, check if this entry was /* Before taking a reference, check if this entry was
* deleted (refcnt=-1) * deleted (refcnt=-1)
......
...@@ -81,11 +81,7 @@ static void tcp_metric_set(struct tcp_metrics_block *tm, ...@@ -81,11 +81,7 @@ static void tcp_metric_set(struct tcp_metrics_block *tm,
static bool addr_same(const struct inetpeer_addr *a, static bool addr_same(const struct inetpeer_addr *a,
const struct inetpeer_addr *b) const struct inetpeer_addr *b)
{ {
if (a->family != b->family) return inetpeer_addr_cmp(a, b) == 0;
return false;
if (a->family == AF_INET)
return a->addr.a4 == b->addr.a4;
return ipv6_addr_equal(&a->addr.in6, &b->addr.in6);
} }
struct tcpm_hash_bucket { struct tcpm_hash_bucket {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册