mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	net: tcp: prefer listeners bound to an address
A relatively common use case is to have several IPs configured on a host, and have different listeners for each of them. We would like to add a "catch all" listener on addr_any, to match incoming connections not served by any of the listeners bound to a specific address. However, port-only lookups can match addr_any sockets when sockets listening on specific addresses are present if so_reuseport flag is set. This patch eliminates lookups into port-only hashtable, as lookups by (addr,port) tuple are easily available. In addition, compute_score() is tweaked to _not_ match addr_any sockets to specific addresses, as hash collisions could result in the unwanted behavior described above. Tested: the patch compiles; full test in the last patch in this patchset. Existing reuseport_* selftests also pass. Suggested-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Peter Oskolkov <posk@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
		
							parent
							
								
									23b0269e58
								
							
						
					
					
						commit
						d9fbc7f643
					
				
					 1 changed files with 8 additions and 52 deletions
				
			
		| 
						 | 
					@ -234,24 +234,16 @@ static inline int compute_score(struct sock *sk, struct net *net,
 | 
				
			||||||
				const int dif, const int sdif, bool exact_dif)
 | 
									const int dif, const int sdif, bool exact_dif)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int score = -1;
 | 
						int score = -1;
 | 
				
			||||||
	struct inet_sock *inet = inet_sk(sk);
 | 
					 | 
				
			||||||
	bool dev_match;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (net_eq(sock_net(sk), net) && inet->inet_num == hnum &&
 | 
						if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
 | 
				
			||||||
			!ipv6_only_sock(sk)) {
 | 
								!ipv6_only_sock(sk)) {
 | 
				
			||||||
		__be32 rcv_saddr = inet->inet_rcv_saddr;
 | 
							if (sk->sk_rcv_saddr != daddr)
 | 
				
			||||||
		score = sk->sk_family == PF_INET ? 2 : 1;
 | 
					 | 
				
			||||||
		if (rcv_saddr) {
 | 
					 | 
				
			||||||
			if (rcv_saddr != daddr)
 | 
					 | 
				
			||||||
			return -1;
 | 
								return -1;
 | 
				
			||||||
			score += 4;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		dev_match = inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
 | 
					 | 
				
			||||||
						 dif, sdif);
 | 
					 | 
				
			||||||
		if (!dev_match)
 | 
					 | 
				
			||||||
			return -1;
 | 
					 | 
				
			||||||
		score += 4;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							score = sk->sk_family == PF_INET ? 2 : 1;
 | 
				
			||||||
		if (sk->sk_incoming_cpu == raw_smp_processor_id())
 | 
							if (sk->sk_incoming_cpu == raw_smp_processor_id())
 | 
				
			||||||
			score++;
 | 
								score++;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -307,26 +299,12 @@ struct sock *__inet_lookup_listener(struct net *net,
 | 
				
			||||||
				    const __be32 daddr, const unsigned short hnum,
 | 
									    const __be32 daddr, const unsigned short hnum,
 | 
				
			||||||
				    const int dif, const int sdif)
 | 
									    const int dif, const int sdif)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	unsigned int hash = inet_lhashfn(net, hnum);
 | 
					 | 
				
			||||||
	struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];
 | 
					 | 
				
			||||||
	bool exact_dif = inet_exact_dif_match(net, skb);
 | 
					 | 
				
			||||||
	struct inet_listen_hashbucket *ilb2;
 | 
						struct inet_listen_hashbucket *ilb2;
 | 
				
			||||||
	struct sock *sk, *result = NULL;
 | 
						struct sock *result = NULL;
 | 
				
			||||||
	int score, hiscore = 0;
 | 
					 | 
				
			||||||
	unsigned int hash2;
 | 
						unsigned int hash2;
 | 
				
			||||||
	u32 phash = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (ilb->count <= 10 || !hashinfo->lhash2)
 | 
					 | 
				
			||||||
		goto port_lookup;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/* Too many sk in the ilb bucket (which is hashed by port alone).
 | 
					 | 
				
			||||||
	 * Try lhash2 (which is hashed by port and addr) instead.
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hash2 = ipv4_portaddr_hash(net, daddr, hnum);
 | 
						hash2 = ipv4_portaddr_hash(net, daddr, hnum);
 | 
				
			||||||
	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
 | 
						ilb2 = inet_lhash2_bucket(hashinfo, hash2);
 | 
				
			||||||
	if (ilb2->count > ilb->count)
 | 
					 | 
				
			||||||
		goto port_lookup;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result = inet_lhash2_lookup(net, ilb2, skb, doff,
 | 
						result = inet_lhash2_lookup(net, ilb2, skb, doff,
 | 
				
			||||||
				    saddr, sport, daddr, hnum,
 | 
									    saddr, sport, daddr, hnum,
 | 
				
			||||||
| 
						 | 
					@ -335,34 +313,12 @@ struct sock *__inet_lookup_listener(struct net *net,
 | 
				
			||||||
		goto done;
 | 
							goto done;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Lookup lhash2 with INADDR_ANY */
 | 
						/* Lookup lhash2 with INADDR_ANY */
 | 
				
			||||||
 | 
					 | 
				
			||||||
	hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
 | 
						hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
 | 
				
			||||||
	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
 | 
						ilb2 = inet_lhash2_bucket(hashinfo, hash2);
 | 
				
			||||||
	if (ilb2->count > ilb->count)
 | 
					 | 
				
			||||||
		goto port_lookup;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	result = inet_lhash2_lookup(net, ilb2, skb, doff,
 | 
						result = inet_lhash2_lookup(net, ilb2, skb, doff,
 | 
				
			||||||
				    saddr, sport, daddr, hnum,
 | 
									    saddr, sport, htonl(INADDR_ANY), hnum,
 | 
				
			||||||
				    dif, sdif);
 | 
									    dif, sdif);
 | 
				
			||||||
	goto done;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
port_lookup:
 | 
					 | 
				
			||||||
	sk_for_each_rcu(sk, &ilb->head) {
 | 
					 | 
				
			||||||
		score = compute_score(sk, net, hnum, daddr,
 | 
					 | 
				
			||||||
				      dif, sdif, exact_dif);
 | 
					 | 
				
			||||||
		if (score > hiscore) {
 | 
					 | 
				
			||||||
			if (sk->sk_reuseport) {
 | 
					 | 
				
			||||||
				phash = inet_ehashfn(net, daddr, hnum,
 | 
					 | 
				
			||||||
						     saddr, sport);
 | 
					 | 
				
			||||||
				result = reuseport_select_sock(sk, phash,
 | 
					 | 
				
			||||||
							       skb, doff);
 | 
					 | 
				
			||||||
				if (result)
 | 
					 | 
				
			||||||
					goto done;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			result = sk;
 | 
					 | 
				
			||||||
			hiscore = score;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
done:
 | 
					done:
 | 
				
			||||||
	if (unlikely(IS_ERR(result)))
 | 
						if (unlikely(IS_ERR(result)))
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue