Skip to contentSkip to conversation

udp: rehash on disconnect

PATCH In review Willem de Bruijn 4 participants 4 messages Feb 19, 2020 – Feb 20, 2020

Subsystems (3)
lore
Share & tools
lore

Willem de Bruijn

author maintainer
#0 lore raw

PATCH 1/1 udp: rehash on disconnect

From: Willem de Bruijn <[email protected]>

As of the below commit, udp sockets bound to a specific address can coexist with one bound to the any addr for the same port.

The commit also phased out the use of socket hashing based only on port (hslot), in favor of always hashing on {addr, port} (hslot2).

The change broke the following behavior with disconnect (AF_UNSPEC):

    server binds to 0.0.0.0:1337
    server connects to 127.0.0.1:80
    server disconnects
    client connects to 127.0.0.1:1337
    client sends "hello"
    server reads "hello"	// times out, packet did not find sk

On connect the server acquires a specific source addr suitable for routing to its destination. On disconnect it reverts to the any addr.

The connect call triggers a rehash to a different hslot2. On disconnect, add the same to return to the original hslot2.

Skip this step if the socket is going to be unhashed completely.

Fixes 4cdeeee9252a ("net: udp: prefer listeners bound to an address")
---
 net/ipv4/udp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/ipv4/udp.c +5 -1
index db76b96092991..08a41f1e1cd22 100644
@@ -1857,8 +1857,12 @@ int __udp_disconnect(struct sock *sk, int flags)
 	inet->inet_dport = 0;
 	sock_rps_reset_rxhash(sk);
 	sk->sk_bound_dev_if = 0;
-	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
+	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
 		inet_reset_saddr(sk);
+		if (sk->sk_prot->rehash &&
+		    (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+			sk->sk_prot->rehash(sk);
+	}
 
 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
 		sk->sk_prot->unhash(sk);
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 41 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

As of the below commit, udp sockets bound to a specific address can coexist with one bound to the any addr for the same port.

The commit also phased out the use of socket hashing based only on port (hslot), in favor of always hashing on {addr, port} (hslot2).

The change broke the following behavior with disconnect (AF_UNSPEC):

    server binds to 0.0.0.0:1337
    server connects to 127.0.0.1:80
    server disconnects
    client connects to 127.0.0.1:1337
    client sends "hello"
    server reads "hello"	// times out, packet did not find sk

On connect the server acquires a specific source addr suitable for routing to its destination. On disconnect it reverts to the any addr.

The connect call triggers a rehash to a different hslot2. On disconnect, add the same to return to the original hslot2.

Skip this step if the socket is going to be unhashed completely.

Fixes 4cdeeee9252a ("net: udp: prefer listeners bound to an address")
---
 net/ipv4/udp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/ipv4/udp.c +0 -0
index db76b96092991..08a41f1e1cd22 100644
@@ -1857,8 +1857,12 @@ int __udp_disconnect(struct sock *sk, int flags)
 	inet->inet_dport = 0;
 	sock_rps_reset_rxhash(sk);
 	sk->sk_bound_dev_if = 0;

net/ipv4/udp.c int __udp_disconnect(struct sock *sk, int flags) · line 1868

Quoted 10 lines
net/ipv4/udp.c +5 -1
-	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
+	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
 		inet_reset_saddr(sk);
+		if (sk->sk_prot->rehash &&
+		    (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+			sk->sk_prot->rehash(sk);
+	}
 
 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
 		sk->sk_prot->unhash(sk);
maintainer
#2 lore raw
In reply to Willem de Bruijn

From: Willem de Bruijn <[email protected]>
Date: Wed, 19 Feb 2020 14:16:32 -0500

Quoted 20 lines

From: Willem de Bruijn <[email protected]>

As of the below commit, udp sockets bound to a specific address can coexist with one bound to the any addr for the same port.

The commit also phased out the use of socket hashing based only on port (hslot), in favor of always hashing on {addr, port} (hslot2).

The change broke the following behavior with disconnect (AF_UNSPEC):

    server binds to 0.0.0.0:1337
    server connects to 127.0.0.1:80
    server disconnects
    client connects to 127.0.0.1:1337
    client sends "hello"
    server reads "hello"	// times out, packet did not find sk

On connect the server acquires a specific source addr suitable for routing to its destination. On disconnect it reverts to the any addr.

Quoted 8 lines

The connect call triggers a rehash to a different hslot2. On disconnect, add the same to return to the original hslot2.

Skip this step if the socket is going to be unhashed completely.

Fixes 4cdeeee9252a ("net: udp: prefer listeners bound to an address")

Applied and queued up for -stable, thanks Willem.

Tested-by
#3 lore raw
Quoted 27 lines · David Miller

From: Willem de Bruijn <[email protected]>
Date: Wed, 19 Feb 2020 14:16:32 -0500

Quoted 23 lines

From: Willem de Bruijn <[email protected]>

As of the below commit, udp sockets bound to a specific address can coexist with one bound to the any addr for the same port.

The commit also phased out the use of socket hashing based only on port (hslot), in favor of always hashing on {addr, port} (hslot2).

The change broke the following behavior with disconnect (AF_UNSPEC):

    server binds to 0.0.0.0:1337
    server connects to 127.0.0.1:80
    server disconnects
    client connects to 127.0.0.1:1337
    client sends "hello"
    server reads "hello"      // times out, packet did not find sk

On connect the server acquires a specific source addr suitable for routing to its destination. On disconnect it reverts to the any addr.

The connect call triggers a rehash to a different hslot2. On disconnect, add the same to return to the original hslot2.

Quoted 7 lines
Quoted 5 lines

Skip this step if the socket is going to be unhashed completely.

Fixes 4cdeeee9252a ("net: udp: prefer listeners bound to an address")

Applied and queued up for -stable, thanks Willem.

Successfully tested with the original server and the demo programs I posted.

Signature · 2 lines
Regards,
Pavel Roskin