Skip to content

Commit 4cdeeee

Browse files
Peter Oskolkovdavem330
authored andcommitted
net: udp: 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>
1 parent 8e2ea53 commit 4cdeeee

File tree

1 file changed

+19
-57
lines changed

1 file changed

+19
-57
lines changed

net/ipv4/udp.c

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,12 @@ static int compute_score(struct sock *sk, struct net *net,
380380
ipv6_only_sock(sk))
381381
return -1;
382382

383-
score = (sk->sk_family == PF_INET) ? 2 : 1;
384-
inet = inet_sk(sk);
383+
if (sk->sk_rcv_saddr != daddr)
384+
return -1;
385385

386-
if (inet->inet_rcv_saddr) {
387-
if (inet->inet_rcv_saddr != daddr)
388-
return -1;
389-
score += 4;
390-
}
386+
score = (sk->sk_family == PF_INET) ? 2 : 1;
391387

388+
inet = inet_sk(sk);
392389
if (inet->inet_daddr) {
393390
if (inet->inet_daddr != saddr)
394391
return -1;
@@ -464,65 +461,30 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
464461
__be16 sport, __be32 daddr, __be16 dport, int dif,
465462
int sdif, struct udp_table *udptable, struct sk_buff *skb)
466463
{
467-
struct sock *sk, *result;
464+
struct sock *result;
468465
unsigned short hnum = ntohs(dport);
469-
unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
470-
struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
466+
unsigned int hash2, slot2;
467+
struct udp_hslot *hslot2;
471468
bool exact_dif = udp_lib_exact_dif_match(net, skb);
472-
int score, badness;
473-
u32 hash = 0;
474469

475-
if (hslot->count > 10) {
476-
hash2 = ipv4_portaddr_hash(net, daddr, hnum);
470+
hash2 = ipv4_portaddr_hash(net, daddr, hnum);
471+
slot2 = hash2 & udptable->mask;
472+
hslot2 = &udptable->hash2[slot2];
473+
474+
result = udp4_lib_lookup2(net, saddr, sport,
475+
daddr, hnum, dif, sdif,
476+
exact_dif, hslot2, skb);
477+
if (!result) {
478+
hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
477479
slot2 = hash2 & udptable->mask;
478480
hslot2 = &udptable->hash2[slot2];
479-
if (hslot->count < hslot2->count)
480-
goto begin;
481481

482482
result = udp4_lib_lookup2(net, saddr, sport,
483-
daddr, hnum, dif, sdif,
483+
htonl(INADDR_ANY), hnum, dif, sdif,
484484
exact_dif, hslot2, skb);
485-
if (!result) {
486-
unsigned int old_slot2 = slot2;
487-
hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
488-
slot2 = hash2 & udptable->mask;
489-
/* avoid searching the same slot again. */
490-
if (unlikely(slot2 == old_slot2))
491-
return result;
492-
493-
hslot2 = &udptable->hash2[slot2];
494-
if (hslot->count < hslot2->count)
495-
goto begin;
496-
497-
result = udp4_lib_lookup2(net, saddr, sport,
498-
daddr, hnum, dif, sdif,
499-
exact_dif, hslot2, skb);
500-
}
501-
if (unlikely(IS_ERR(result)))
502-
return NULL;
503-
return result;
504-
}
505-
begin:
506-
result = NULL;
507-
badness = 0;
508-
sk_for_each_rcu(sk, &hslot->head) {
509-
score = compute_score(sk, net, saddr, sport,
510-
daddr, hnum, dif, sdif, exact_dif);
511-
if (score > badness) {
512-
if (sk->sk_reuseport) {
513-
hash = udp_ehashfn(net, daddr, hnum,
514-
saddr, sport);
515-
result = reuseport_select_sock(sk, hash, skb,
516-
sizeof(struct udphdr));
517-
if (unlikely(IS_ERR(result)))
518-
return NULL;
519-
if (result)
520-
return result;
521-
}
522-
result = sk;
523-
badness = score;
524-
}
525485
}
486+
if (unlikely(IS_ERR(result)))
487+
return NULL;
526488
return result;
527489
}
528490
EXPORT_SYMBOL_GPL(__udp4_lib_lookup);

0 commit comments

Comments
 (0)