Commit e94a62f5 authored by Paolo Abeni's avatar Paolo Abeni Committed by David S. Miller

net/reuseport: drop legacy code

Since commit e32ea7e7 ("soreuseport: fast reuseport UDP socket
selection") and commit c125e80b ("soreuseport: fast reuseport
TCP socket selection") the relevant reuseport socket matching the current
packet is selected by the reuseport_select_sock() call. The only
exceptions are invalid BPF filters/filters returning out-of-range
indices.
In the latter case the code implicitly falls back to using the hash
demultiplexing, but instead of selecting the socket inside the
reuseport_select_sock() function, it relies on the hash selection
logic introduced with the early soreuseport implementation.

With this patch, in case of a BPF filter returning a bad socket
index value, we fall back to hash-based selection inside the
reuseport_select_sock() body, so that we can drop some duplicate
code in the ipv4 and ipv6 stack.

This also allows faster lookup in the above scenario and will allow
us to avoid computing the hash value for successful, BPF based
demultiplexing - in a later patch.
Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
Acked-by: default avatarCraig Gallek <kraig@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 0fc66ddf
...@@ -235,7 +235,9 @@ struct sock *reuseport_select_sock(struct sock *sk, ...@@ -235,7 +235,9 @@ struct sock *reuseport_select_sock(struct sock *sk,
if (prog && skb) if (prog && skb)
sk2 = run_bpf(reuse, socks, prog, skb, hdr_len); sk2 = run_bpf(reuse, socks, prog, skb, hdr_len);
else
/* no bpf or invalid bpf result: fall back to hash usage */
if (!sk2)
sk2 = reuse->socks[reciprocal_scale(hash, socks)]; sk2 = reuse->socks[reciprocal_scale(hash, socks)];
} }
......
...@@ -216,32 +216,25 @@ struct sock *__inet_lookup_listener(struct net *net, ...@@ -216,32 +216,25 @@ struct sock *__inet_lookup_listener(struct net *net,
{ {
unsigned int hash = inet_lhashfn(net, hnum); unsigned int hash = inet_lhashfn(net, hnum);
struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash]; struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
int score, hiscore = 0, matches = 0, reuseport = 0;
bool exact_dif = inet_exact_dif_match(net, skb); bool exact_dif = inet_exact_dif_match(net, skb);
struct sock *sk, *result = NULL; struct sock *sk, *result = NULL;
int score, hiscore = 0;
u32 phash = 0; u32 phash = 0;
sk_for_each_rcu(sk, &ilb->head) { sk_for_each_rcu(sk, &ilb->head) {
score = compute_score(sk, net, hnum, daddr, score = compute_score(sk, net, hnum, daddr,
dif, sdif, exact_dif); dif, sdif, exact_dif);
if (score > hiscore) { if (score > hiscore) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
phash = inet_ehashfn(net, daddr, hnum, phash = inet_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, phash, result = reuseport_select_sock(sk, phash,
skb, doff); skb, doff);
if (result) if (result)
return result; return result;
matches = 1;
} }
result = sk; result = sk;
hiscore = score; hiscore = score;
} else if (score == hiscore && reuseport) {
matches++;
if (reciprocal_scale(phash, matches) == 0)
result = sk;
phash = next_pseudo_random32(phash);
} }
} }
return result; return result;
......
...@@ -445,7 +445,7 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -445,7 +445,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
struct sk_buff *skb) struct sk_buff *skb)
{ {
struct sock *sk, *result; struct sock *sk, *result;
int score, badness, matches = 0, reuseport = 0; int score, badness;
u32 hash = 0; u32 hash = 0;
result = NULL; result = NULL;
...@@ -454,23 +454,16 @@ static struct sock *udp4_lib_lookup2(struct net *net, ...@@ -454,23 +454,16 @@ static struct sock *udp4_lib_lookup2(struct net *net,
score = compute_score(sk, net, saddr, sport, score = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif, exact_dif); daddr, hnum, dif, sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
hash = udp_ehashfn(net, daddr, hnum, hash = udp_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, hash, skb, result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result) if (result)
return result; return result;
matches = 1;
} }
badness = score; badness = score;
result = sk; result = sk;
} else if (score == badness && reuseport) {
matches++;
if (reciprocal_scale(hash, matches) == 0)
result = sk;
hash = next_pseudo_random32(hash);
} }
} }
return result; return result;
...@@ -488,7 +481,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, ...@@ -488,7 +481,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
bool exact_dif = udp_lib_exact_dif_match(net, skb); bool exact_dif = udp_lib_exact_dif_match(net, skb);
int score, badness, matches = 0, reuseport = 0; int score, badness;
u32 hash = 0; u32 hash = 0;
if (hslot->count > 10) { if (hslot->count > 10) {
...@@ -526,23 +519,16 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, ...@@ -526,23 +519,16 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
score = compute_score(sk, net, saddr, sport, score = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif, exact_dif); daddr, hnum, dif, sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
hash = udp_ehashfn(net, daddr, hnum, hash = udp_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, hash, skb, result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result) if (result)
return result; return result;
matches = 1;
} }
result = sk; result = sk;
badness = score; badness = score;
} else if (score == badness && reuseport) {
matches++;
if (reciprocal_scale(hash, matches) == 0)
result = sk;
hash = next_pseudo_random32(hash);
} }
} }
return result; return result;
......
...@@ -134,31 +134,24 @@ struct sock *inet6_lookup_listener(struct net *net, ...@@ -134,31 +134,24 @@ struct sock *inet6_lookup_listener(struct net *net,
{ {
unsigned int hash = inet_lhashfn(net, hnum); unsigned int hash = inet_lhashfn(net, hnum);
struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash]; struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
int score, hiscore = 0, matches = 0, reuseport = 0;
bool exact_dif = inet6_exact_dif_match(net, skb); bool exact_dif = inet6_exact_dif_match(net, skb);
struct sock *sk, *result = NULL; struct sock *sk, *result = NULL;
int score, hiscore = 0;
u32 phash = 0; u32 phash = 0;
sk_for_each(sk, &ilb->head) { sk_for_each(sk, &ilb->head) {
score = compute_score(sk, net, hnum, daddr, dif, sdif, exact_dif); score = compute_score(sk, net, hnum, daddr, dif, sdif, exact_dif);
if (score > hiscore) { if (score > hiscore) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
phash = inet6_ehashfn(net, daddr, hnum, phash = inet6_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, phash, result = reuseport_select_sock(sk, phash,
skb, doff); skb, doff);
if (result) if (result)
return result; return result;
matches = 1;
} }
result = sk; result = sk;
hiscore = score; hiscore = score;
} else if (score == hiscore && reuseport) {
matches++;
if (reciprocal_scale(phash, matches) == 0)
result = sk;
phash = next_pseudo_random32(phash);
} }
} }
return result; return result;
......
...@@ -184,7 +184,7 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -184,7 +184,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
struct udp_hslot *hslot2, struct sk_buff *skb) struct udp_hslot *hslot2, struct sk_buff *skb)
{ {
struct sock *sk, *result; struct sock *sk, *result;
int score, badness, matches = 0, reuseport = 0; int score, badness;
u32 hash = 0; u32 hash = 0;
result = NULL; result = NULL;
...@@ -193,8 +193,7 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -193,8 +193,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
score = compute_score(sk, net, saddr, sport, score = compute_score(sk, net, saddr, sport,
daddr, hnum, dif, sdif, exact_dif); daddr, hnum, dif, sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
hash = udp6_ehashfn(net, daddr, hnum, hash = udp6_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
...@@ -202,15 +201,9 @@ static struct sock *udp6_lib_lookup2(struct net *net, ...@@ -202,15 +201,9 @@ static struct sock *udp6_lib_lookup2(struct net *net,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result) if (result)
return result; return result;
matches = 1;
} }
result = sk; result = sk;
badness = score; badness = score;
} else if (score == badness && reuseport) {
matches++;
if (reciprocal_scale(hash, matches) == 0)
result = sk;
hash = next_pseudo_random32(hash);
} }
} }
return result; return result;
...@@ -228,7 +221,7 @@ struct sock *__udp6_lib_lookup(struct net *net, ...@@ -228,7 +221,7 @@ struct sock *__udp6_lib_lookup(struct net *net,
unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask); unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
struct udp_hslot *hslot2, *hslot = &udptable->hash[slot]; struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
bool exact_dif = udp6_lib_exact_dif_match(net, skb); bool exact_dif = udp6_lib_exact_dif_match(net, skb);
int score, badness, matches = 0, reuseport = 0; int score, badness;
u32 hash = 0; u32 hash = 0;
if (hslot->count > 10) { if (hslot->count > 10) {
...@@ -267,23 +260,16 @@ struct sock *__udp6_lib_lookup(struct net *net, ...@@ -267,23 +260,16 @@ struct sock *__udp6_lib_lookup(struct net *net,
score = compute_score(sk, net, saddr, sport, daddr, hnum, dif, score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
sdif, exact_dif); sdif, exact_dif);
if (score > badness) { if (score > badness) {
reuseport = sk->sk_reuseport; if (sk->sk_reuseport) {
if (reuseport) {
hash = udp6_ehashfn(net, daddr, hnum, hash = udp6_ehashfn(net, daddr, hnum,
saddr, sport); saddr, sport);
result = reuseport_select_sock(sk, hash, skb, result = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr)); sizeof(struct udphdr));
if (result) if (result)
return result; return result;
matches = 1;
} }
result = sk; result = sk;
badness = score; badness = score;
} else if (score == badness && reuseport) {
matches++;
if (reciprocal_scale(hash, matches) == 0)
result = sk;
hash = next_pseudo_random32(hash);
} }
} }
return result; return result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment