For now, the tcp socket lookup will terminate if the socket is reuse port in inet_lhash2_lookup(), which makes the socket is not the best match.
For example, we have socket1 and socket2 both listen on "0.0.0.0:1234", but socket1 bind on "eth0". We create socket1 first, and then socket2. Then, all connections will goto socket2, which is not expected, as socket1 has higher priority.
This can cause unexpected behavior if TCP MD5 keys is used, as described in Documentation/networking/vrf.rst -> Applications.
Therefor, we lookup the best matched socket first, and then do the reuse port logic. This can increase some overhead if there are many reuse port socket :/
Fixes: c125e80b8868 ("soreuseport: fast reuseport TCP socket selection") Signed-off-by: Menglong Dong dongml2@chinatelecom.cn --- v3: * use the approach in V1 * add the Fixes tag --- net/ipv4/inet_hashtables.c | 13 +++++++------ net/ipv6/inet6_hashtables.c | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c index ceeeec9b7290..51751337f394 100644 --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -389,17 +389,18 @@ static struct sock *inet_lhash2_lookup(const struct net *net, sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) { score = compute_score(sk, net, hnum, daddr, dif, sdif); if (score > hiscore) { - result = inet_lookup_reuseport(net, sk, skb, doff, - saddr, sport, daddr, hnum, inet_ehashfn); - if (result) - return result; - result = sk; hiscore = score; } }
- return result; + if (!result) + return NULL; + + sk = inet_lookup_reuseport(net, result, skb, doff, + saddr, sport, daddr, hnum, inet_ehashfn); + + return sk ? sk : result; }
struct sock *inet_lookup_run_sk_lookup(const struct net *net, diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c index 76ee521189eb..2554f26d6c20 100644 --- a/net/ipv6/inet6_hashtables.c +++ b/net/ipv6/inet6_hashtables.c @@ -161,17 +161,18 @@ static struct sock *inet6_lhash2_lookup(const struct net *net, sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) { score = compute_score(sk, net, hnum, daddr, dif, sdif); if (score > hiscore) { - result = inet6_lookup_reuseport(net, sk, skb, doff, - saddr, sport, daddr, hnum, inet6_ehashfn); - if (result) - return result; - result = sk; hiscore = score; } }
- return result; + if (!result) + return NULL; + + sk = inet6_lookup_reuseport(net, result, skb, doff, + saddr, sport, daddr, hnum, inet6_ehashfn); + + return sk ? sk : result; }
struct sock *inet6_lookup_run_sk_lookup(const struct net *net,