net/reuseport: drop legacy code
authorPaolo Abeni <pabeni@redhat.com>
Thu, 30 Nov 2017 14:39:34 +0000 (15:39 +0100)
committerDavid S. Miller <davem@davemloft.net>
Thu, 30 Nov 2017 15:56:32 +0000 (10:56 -0500)
Since commit e32ea7e74727 ("soreuseport: fast reuseport UDP socket
selection") and commit c125e80b8868 ("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: Paolo Abeni <pabeni@redhat.com>
Acked-by: Craig Gallek <kraig@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/core/sock_reuseport.c
net/ipv4/inet_hashtables.c
net/ipv4/udp.c
net/ipv6/inet6_hashtables.c
net/ipv6/udp.c

index 5eeb1d20cc388732c1a58d5a5e13db3b872e7ddd..c5bb52bc73a18689c358389b5f1c89cf6a2402a2 100644 (file)
@@ -235,7 +235,9 @@ struct sock *reuseport_select_sock(struct sock *sk,
 
                if (prog && skb)
                        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)];
        }
 
index e7d15fb0d94d9790675356d3144d204b91eab984..427b705d7c6401ce7cd610d8af10f82abeabcfcf 100644 (file)
@@ -216,32 +216,25 @@ struct sock *__inet_lookup_listener(struct net *net,
 {
        unsigned int hash = inet_lhashfn(net, hnum);
        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);
        struct sock *sk, *result = NULL;
+       int score, hiscore = 0;
        u32 phash = 0;
 
        sk_for_each_rcu(sk, &ilb->head) {
                score = compute_score(sk, net, hnum, daddr,
                                      dif, sdif, exact_dif);
                if (score > hiscore) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                phash = inet_ehashfn(net, daddr, hnum,
                                                     saddr, sport);
                                result = reuseport_select_sock(sk, phash,
                                                               skb, doff);
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        result = sk;
                        hiscore = score;
-               } else if (score == hiscore && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(phash, matches) == 0)
-                               result = sk;
-                       phash = next_pseudo_random32(phash);
                }
        }
        return result;
index e4ff25c947c5e5b21ac4986d0327339f4f60d321..36f857c87fe2869c8f8048e4daea634f9217f9c6 100644 (file)
@@ -445,7 +445,7 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                                     struct sk_buff *skb)
 {
        struct sock *sk, *result;
-       int score, badness, matches = 0, reuseport = 0;
+       int score, badness;
        u32 hash = 0;
 
        result = NULL;
@@ -454,23 +454,16 @@ static struct sock *udp4_lib_lookup2(struct net *net,
                score = compute_score(sk, net, saddr, sport,
                                      daddr, hnum, dif, sdif, exact_dif);
                if (score > badness) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                hash = udp_ehashfn(net, daddr, hnum,
                                                   saddr, sport);
                                result = reuseport_select_sock(sk, hash, skb,
                                                        sizeof(struct udphdr));
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        badness = score;
                        result = sk;
-               } else if (score == badness && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(hash, matches) == 0)
-                               result = sk;
-                       hash = next_pseudo_random32(hash);
                }
        }
        return result;
@@ -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);
        struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
        bool exact_dif = udp_lib_exact_dif_match(net, skb);
-       int score, badness, matches = 0, reuseport = 0;
+       int score, badness;
        u32 hash = 0;
 
        if (hslot->count > 10) {
@@ -526,23 +519,16 @@ begin:
                score = compute_score(sk, net, saddr, sport,
                                      daddr, hnum, dif, sdif, exact_dif);
                if (score > badness) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                hash = udp_ehashfn(net, daddr, hnum,
                                                   saddr, sport);
                                result = reuseport_select_sock(sk, hash, skb,
                                                        sizeof(struct udphdr));
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        result = sk;
                        badness = score;
-               } else if (score == badness && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(hash, matches) == 0)
-                               result = sk;
-                       hash = next_pseudo_random32(hash);
                }
        }
        return result;
index b01858f5deb1711f24c0c38cba0a3e61d43b390c..0d1451381f5c61c61d1f004be674e2f6944a084f 100644 (file)
@@ -134,31 +134,24 @@ struct sock *inet6_lookup_listener(struct net *net,
 {
        unsigned int hash = inet_lhashfn(net, hnum);
        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);
        struct sock *sk, *result = NULL;
+       int score, hiscore = 0;
        u32 phash = 0;
 
        sk_for_each(sk, &ilb->head) {
                score = compute_score(sk, net, hnum, daddr, dif, sdif, exact_dif);
                if (score > hiscore) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                phash = inet6_ehashfn(net, daddr, hnum,
                                                      saddr, sport);
                                result = reuseport_select_sock(sk, phash,
                                                               skb, doff);
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        result = sk;
                        hiscore = score;
-               } else if (score == hiscore && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(phash, matches) == 0)
-                               result = sk;
-                       phash = next_pseudo_random32(phash);
                }
        }
        return result;
index 3f30fa313bf282d129668f6c0929a631edf0ca9d..c9f91c28b81d7ec93b0ccb8d22906336ba734926 100644 (file)
@@ -184,7 +184,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                struct udp_hslot *hslot2, struct sk_buff *skb)
 {
        struct sock *sk, *result;
-       int score, badness, matches = 0, reuseport = 0;
+       int score, badness;
        u32 hash = 0;
 
        result = NULL;
@@ -193,8 +193,7 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                score = compute_score(sk, net, saddr, sport,
                                      daddr, hnum, dif, sdif, exact_dif);
                if (score > badness) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                hash = udp6_ehashfn(net, daddr, hnum,
                                                    saddr, sport);
 
@@ -202,15 +201,9 @@ static struct sock *udp6_lib_lookup2(struct net *net,
                                                        sizeof(struct udphdr));
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        result = sk;
                        badness = score;
-               } else if (score == badness && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(hash, matches) == 0)
-                               result = sk;
-                       hash = next_pseudo_random32(hash);
                }
        }
        return result;
@@ -228,7 +221,7 @@ struct sock *__udp6_lib_lookup(struct net *net,
        unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
        struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
        bool exact_dif = udp6_lib_exact_dif_match(net, skb);
-       int score, badness, matches = 0, reuseport = 0;
+       int score, badness;
        u32 hash = 0;
 
        if (hslot->count > 10) {
@@ -267,23 +260,16 @@ begin:
                score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
                                      sdif, exact_dif);
                if (score > badness) {
-                       reuseport = sk->sk_reuseport;
-                       if (reuseport) {
+                       if (sk->sk_reuseport) {
                                hash = udp6_ehashfn(net, daddr, hnum,
                                                    saddr, sport);
                                result = reuseport_select_sock(sk, hash, skb,
                                                        sizeof(struct udphdr));
                                if (result)
                                        return result;
-                               matches = 1;
                        }
                        result = sk;
                        badness = score;
-               } else if (score == badness && reuseport) {
-                       matches++;
-                       if (reciprocal_scale(hash, matches) == 0)
-                               result = sk;
-                       hash = next_pseudo_random32(hash);
                }
        }
        return result;