udp: Introduce UDP_STOP_RCV option for UDP
Subsystems (4)
RFC 1/2 udp: Introduce UDP_STOP_RCV option for UDP
For some services we are using "established-over-unconnected" model.
'''
// create unconnected socket and 'listen()'srv_fd = socket(AF_INET, SOCK_DGRAM)setsockopt(srv_fd, SO_REUSEPORT)bind(srv_fd, SERVER_ADDR, SERVER_PORT)
// 'accept()'
data, client_addr = recvmsg(srv_fd)
// create a connected socket for this requestcli_fd = socket(AF_INET, SOCK_DGRAM)setsockopt(cli_fd, SO_REUSEPORT)bind(cli_fd, SERVER_ADDR, SERVER_PORT)connect(cli, client_addr)
...
// do handshake with cli_fd
'''
This programming pattern simulates accept() using UDP, creating a new socket for each client request. The server can then use separate sockets to handle client requests, avoiding the need to use a single UDP socket for I/O transmission.
But there is a race condition between the bind() and connect() of the connected socket:
We might receive unexpected packets belonging to the unconnected socket before connect() is executed, which is not what we need.
(Of course, before connect(), the unconnected socket will also receive packets from the connected socket, which is easily resolved because upper-layer protocols typically require explicit boundaries, and we receive a complete packet before creating a connected socket.)
Before this patch, the connected socket had to filter requests at recvmsg time, acting as a dispatcher to some extent. With this patch, we can consider the bind and connect operations to be atomic.
--- include/linux/udp.h | 1 + include/uapi/linux/udp.h | 1 + net/ipv4/udp.c | 13 ++++++++++--- net/ipv6/udp.c | 5 +++-- 4 files changed, 15 insertions(+), 5 deletions(-)
include/linux/udp.h +1 -0
@@ -42,6 +42,7 @@ enum {UDP_FLAGS_ENCAP_ENABLED ,/* This socket enabled encap */ UDP_FLAGS_UDPLITE_SEND_CC ,/* set via udplite setsockopt */ UDP_FLAGS_UDPLITE_RECV_CC ,/* set via udplite setsockopt */ + UDP_FLAGS_STOP_RCV, /* Stop receiving packets */ }; struct udp_sock {
include/uapi/linux/udp.h +1 -0
@@ -34,6 +34,7 @@ struct udphdr {#define UDP_NO_CHECK6_RX 102/* Disable accepting checksum for UDP6 */ #define UDP_SEGMENT 103/* Set GSO segmentation size */ #define UDP_GRO 104/* This socket can receive UDP GRO packets */ +#define UDP_STOP_RCV 105/* This socket will not receive any packets */ /* UDP encapsulation types */ #define UDP_ENCAP_ESPINUDP_NON_IKE 1/* unused draft-ietf-ipsec-nat-t-ike-00/01 */
net/ipv4/udp.c +10 -3
@@ -376,7 +376,8 @@ static int compute_score(struct sock *sk, const struct net *net,if (!net_eq (sock_net (sk), net) ||udp_sk (sk)->udp_port_hash != hnum || -ipv6_only_sock (sk)) +ipv6_only_sock (sk) || +udp_test_bit (STOP_RCV , sk))return -1 ;if (sk->sk_rcv_saddr != daddr)
@@ -494,7 +495,7 @@ static struct sock *udp4_lib_lookup2(const struct net *net, result =inet_lookup_reuseport (net, sk, skb,sizeof (struct udphdr ), saddr, sport, daddr, hnum, udp_ehashfn); -if (!result) { +if (!result ||udp_test_bit (STOP_RCV , result)) { result = sk;continue ; }
@@ -3031,7 +3032,9 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname,set_xfrm_gro_udp_encap_rcv (up->encap_type, sk->sk_family, sk);sockopt_release_sock (sk);break ; - +case UDP_STOP_RCV : +udp_assign_bit (STOP_RCV , sk, valbool); +break ;/* * UDP-Lite's partial checksum coverage (RFC 3828). */
@@ -3120,6 +3123,10 @@ int udp_lib_getsockopt(struct sock *sk, int level, int optname, val =udp_test_bit (GRO_ENABLED , sk);break ; +case UDP_STOP_RCV : + val =udp_test_bit (STOP_RCV , sk); +break ; +/* The following two cannot be changed on UDP sockets, the return is * always 0 (which corresponds to the full checksum coverage of UDP). */ case UDPLITE_SEND_CSCOV :
net/ipv6/udp.c +3 -2
@@ -137,7 +137,8 @@ static int compute_score(struct sock *sk, const struct net *net,if (!net_eq (sock_net (sk), net) ||udp_sk (sk)->udp_port_hash != hnum || - sk->sk_family !=PF_INET6 ) + sk->sk_family !=PF_INET6 || +udp_test_bit (STOP_RCV , sk))return -1 ;if (!ipv6_addr_equal (&sk->sk_v6_rcv_saddr, daddr))
@@ -245,7 +246,7 @@ static struct sock *udp6_lib_lookup2(const struct net *net, result =inet6_lookup_reuseport (net, sk, skb,sizeof (struct udphdr ), saddr, sport, daddr, hnum, udp6_ehashfn); -if (!result) { +if (!result ||udp_test_bit (STOP_RCV , result)) { result = sk;continue ; }
Signature · 1 line
2.47.1
RFC 2/2 selftests/net: Add udp UDP_STOP_RCV selftest
Add a new selftest, which uses UDP_STOP_RCV to make UDP simulate TCP's listen and accept.
--- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 1 + .../testing/selftests/net/test_udp_stop_rcv.c | 275 ++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 tools/testing/selftests/net/test_udp_stop_rcv.c
tools/testing/selftests/net/.gitignore +1 -0
@@ -61,3 +61,4 @@ udpgso udpgso_bench_rx udpgso_bench_tx unix_connect +test_udp_stop_rcv
tools/testing/selftests/net/Makefile +1 -0
@@ -108,6 +108,7 @@ TEST_GEN_PROGS += proc_net_pktgen TEST_PROGS += lwt_dst_cache_ref_loop.sh TEST_PROGS += skf_net_off.sh TEST_GEN_FILES += skf_net_off +TEST_GEN_FILES += test_udp_stop_rcv # YNL files, must be before "include ..lib.mk" YNL_GEN_FILES := busy_poller netlink-dumps
tools/testing/selftests/net/test_udp_stop_rcv.c new file +275 -0
275 lines not on this page · raw message
Signature · 1 line
2.47.1
From: Jiayuan Chen <[email protected]>
Date: Thu, 1 May 2025 11:51:08 +0800
Quoted 34 lines
For some services we are using "established-over-unconnected" model.
'''
// create unconnected socket and 'listen()'srv_fd=socket(AF_INET, SOCK_DGRAM)setsockopt(srv_fd, SO_REUSEPORT)bind(srv_fd, SERVER_ADDR, SERVER_PORT)// '
accept()'
data,client_addr=recvmsg(srv_fd)// create a connected socket for this request
cli_fd=socket(AF_INET, SOCK_DGRAM)setsockopt(cli_fd, SO_REUSEPORT)bind(cli_fd, SERVER_ADDR, SERVER_PORT)connect(cli, client_addr)
...
// do handshake withcli_fd
'''This programming pattern simulates
accept()using UDP, creating a new socket for each client request. The server can then use separate sockets to handle client requests, avoiding the need to use a single UDP socket for I/O transmission.But there is a race condition between the
bind()andconnect()of the connected socket:
We might receive unexpected packets belonging to the unconnected socket beforeconnect()is executed, which is not what we need.
(Of course, beforeconnect(), the unconnected socket will also receive packets from the connected socket, which is easily resolved because upper-layer protocols typically require explicit boundaries, and we receive a complete packet before creating a connected socket.)
Quoted 3 lines
Before this patch, the connected socket had to filter requests at recvmsg time, acting as a dispatcher to some extent. With this patch, we can consider the bind and connect operations to be atomic.
SO_ATTACH_REUSEPORT_EBPF is what you want.
The socket won't receive any packets until the socket is added to the BPF map.
No need to reinvent a subset of BPF functionalities.
Quoted 81 lines · Kuniyuki Iwashima
From: Jiayuan Chen <[email protected]>
Date: Thu, 1 May 2025 11:51:08 +0800
Quoted 69 lines
For some services we are using "established-over-unconnected" model.
'''
// create unconnected socket and '
listen()'
srv_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(srv_fd, SO_REUSEPORT)
bind(srv_fd, SERVER_ADDR, SERVER_PORT)// '
accept()'data,
client_addr=recvmsg(srv_fd)// create a connected socket for this request
cli_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(cli_fd, SO_REUSEPORT)
bind(cli_fd, SERVER_ADDR, SERVER_PORT)
connect(cli, client_addr)...
// do handshake with
cli_fd'''
This programming pattern simulates
accept()using UDP, creating a newsocket for each client request. The server can then use separate sockets
to handle client requests, avoiding the need to use a single UDP socket
for I/O transmission.
But there is a race condition between the
bind()andconnect()of theconnected socket:
We might receive unexpected packets belonging to the unconnected socket
before
connect()is executed, which is not what we need.(Of course, before
connect(), the unconnected socket will also receivepackets from the connected socket, which is easily resolved because
upper-layer protocols typically require explicit boundaries, and we
receive a complete packet before creating a connected socket.)
Quoted 6 lines
Before this patch, the connected socket had to filter requests at recvmsg
time, acting as a dispatcher to some extent. With this patch, we can
consider the bind and connect operations to be atomic.
Quoted 8 lines
SO_ATTACH_REUSEPORT_EBPFis what you want.The socket won't receive any packets until the socket is added to
the BPF map.
No need to reinvent a subset of BPF functionalities.
I think this feature is for selecting one socket, not filtering out certain sockets.
Does this mean that I need to first capture all sockets bound to the same port, and then if the kernel selects a socket that I don't want to receive packets on, I'll need to implement an algorithm in the BPF program to choose another socket from the ones I've captured, in order to avoid returning that socket?
This looks like it completely bypasses the kernel's built-in scoring logic. Or is expanding BPF_PROG_TYPE_SK_REUSEPORT to have filtering capabilities also an acceptable solution?
From: "Jiayuan Chen" <[email protected]>
Date: Thu, 01 May 2025 06:22:17 +0000
Quoted 92 lines
Quoted 81 lines · Kuniyuki Iwashima
From: Jiayuan Chen <[email protected]>
Date: Thu, 1 May 2025 11:51:08 +0800
Quoted 69 lines
For some services we are using "established-over-unconnected" model.
'''
// create unconnected socket and '
listen()'
srv_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(srv_fd, SO_REUSEPORT)
bind(srv_fd, SERVER_ADDR, SERVER_PORT)// '
accept()'data,
client_addr=recvmsg(srv_fd)// create a connected socket for this request
cli_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(cli_fd, SO_REUSEPORT)
bind(cli_fd, SERVER_ADDR, SERVER_PORT)
connect(cli, client_addr)...
// do handshake with
cli_fd'''
This programming pattern simulates
accept()using UDP, creating a newsocket for each client request. The server can then use separate sockets
to handle client requests, avoiding the need to use a single UDP socket
for I/O transmission.
But there is a race condition between the
bind()andconnect()of theconnected socket:
We might receive unexpected packets belonging to the unconnected socket
before
connect()is executed, which is not what we need.(Of course, before
connect(), the unconnected socket will also receivepackets from the connected socket, which is easily resolved because
upper-layer protocols typically require explicit boundaries, and we
receive a complete packet before creating a connected socket.)
Quoted 6 lines
Before this patch, the connected socket had to filter requests at recvmsg
time, acting as a dispatcher to some extent. With this patch, we can
consider the bind and connect operations to be atomic.
Quoted 8 lines
SO_ATTACH_REUSEPORT_EBPFis what you want.The socket won't receive any packets until the socket is added to
the BPF map.
No need to reinvent a subset of BPF functionalities.
Quoted 8 lines
I think this feature is for selecting one socket, not filtering out certain sockets.
Does this mean that I need to first capture all sockets bound to the same port, and then if the kernel selects a socket that I don't want to receive packets on, I'll need to implement an algorithm in the BPF program to choose another socket from the ones I've captured, in order to avoid returning that socket?
Right.
If you want a set of sockets to listen on the port, you can implement as such with BPF; register the sockets to the BPF map, and if kernel pick up other sockets and triggers the BPF prog, just return one of the registerd sk.
Even when you have connect()ed sockets on the same port, kernel will fall back to the normal scoring to find the best one, and it's not a problem as the last 'result' is one selected by BPF or a connected sk, and the packet won't be routed to not-yet-registered unconnected sk.
Quoted 4 lines
This looks like it completely bypasses the kernel's built-in scoring logic. Or is expanding
BPF_PROG_TYPE_SK_REUSEPORTto have filtering capabilities also an acceptable solution?
Willem de Bruijn
maintainerQuoted 110 lines · Kuniyuki Iwashima
From: "Jiayuan Chen" <[email protected]>
Date: Thu, 01 May 2025 06:22:17 +0000Quoted 92 lines
Quoted 81 lines · Kuniyuki Iwashima
From: Jiayuan Chen <[email protected]>
Date: Thu, 1 May 2025 11:51:08 +0800
Quoted 69 lines
For some services we are using "established-over-unconnected" model.
'''
// create unconnected socket and '
listen()'
srv_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(srv_fd, SO_REUSEPORT)
bind(srv_fd, SERVER_ADDR, SERVER_PORT)// '
accept()'data,
client_addr=recvmsg(srv_fd)// create a connected socket for this request
cli_fd=socket(AF_INET, SOCK_DGRAM)
setsockopt(cli_fd, SO_REUSEPORT)
bind(cli_fd, SERVER_ADDR, SERVER_PORT)
connect(cli, client_addr)...
// do handshake with
cli_fd'''
This programming pattern simulates
accept()using UDP, creating a newsocket for each client request. The server can then use separate sockets
to handle client requests, avoiding the need to use a single UDP socket
for I/O transmission.
But there is a race condition between the
bind()andconnect()of theconnected socket:
We might receive unexpected packets belonging to the unconnected socket
before
connect()is executed, which is not what we need.(Of course, before
connect(), the unconnected socket will also receivepackets from the connected socket, which is easily resolved because
upper-layer protocols typically require explicit boundaries, and we
receive a complete packet before creating a connected socket.)
Quoted 6 lines
Before this patch, the connected socket had to filter requests at recvmsg
time, acting as a dispatcher to some extent. With this patch, we can
consider the bind and connect operations to be atomic.
Quoted 8 lines
SO_ATTACH_REUSEPORT_EBPFis what you want.The socket won't receive any packets until the socket is added to
the BPF map.
No need to reinvent a subset of BPF functionalities.
Quoted 8 lines
I think this feature is for selecting one socket, not filtering out certain sockets.
Does this mean that I need to first capture all sockets bound to the same port, and then if the kernel selects a socket that I don't want to receive packets on, I'll need to implement an algorithm in the BPF program to choose another socket from the ones I've captured, in order to avoid returning that socket?
Right.
If you want a set of sockets to listen on the port, you can implement as such with BPF; register the sockets to the BPF map, and if kernel pick up other sockets and triggers the BPF prog, just return one of the registerd sk.
Quoted 10 lines
Even when you have connect()ed sockets on the same port, kernel will fall back to the normal scoring to find the best one, and it's not a problem as the last 'result' is one selected by BPF or a connected sk, and the packet won't be routed to not-yet-registered unconnected sk.
Quoted 4 lines
This looks like it completely bypasses the kernel's built-in scoring logic. Or is expanding
BPF_PROG_TYPE_SK_REUSEPORTto have filtering capabilities also an acceptable solution?
Reuseport BPF exists because we want to avoid having to continue to add custom rules in C for each scenario.
In this case, I did wonder whether it is possible to avoid hitting the soon-to-be connected socket with the standard reuseport algorithm in reuseport_select_sock_by_hash.
Setting SO_INCOMING_CPU to a cpu on which no packets arrive will lower its priority relative to other sockets. It's a bit of a hack, but should work?