bpf: net: Detach BPF prog from reuseport sk
Subsystems (11)
v3 cover bpf: net: Detach BPF prog from reuseport sk
v3:
- Use rcu_swap_protected (Stanislav Fomichev)
- Use 0x0047 for SO_DETACH_REUSEPORT_BPF for sparc (kbuild test robot <[email protected]>)
v2:
- Copy asm-generic/socket.h to tools/ in the new patch 2 (Stanislav Fomichev)
This patch adds SO_DETACH_REUSEPORT_BPF to detach BPF prog from reuseport sk.
Martin KaFai Lau (3):
bpf: net: Add SO_DETACH_REUSEPORT_BPF
bpf: Sync asm-generic/socket.h to tools/
bpf: Add test for SO_REUSEPORT_DETACH_BPF
arch/alpha/include/uapi/asm/socket.h | 2 +
arch/mips/include/uapi/asm/socket.h | 2 +
arch/parisc/include/uapi/asm/socket.h | 2 +
arch/sparc/include/uapi/asm/socket.h | 2 +
include/net/sock_reuseport.h | 2 +
include/uapi/asm-generic/socket.h | 2 +
net/core/sock.c | 4 ++
net/core/sock_reuseport.c | 24 +++++++++
.../include}/uapi/asm-generic/socket.h | 2 +
.../selftests/bpf/test_select_reuseport.c | 54 +++++++++++++++++++
10 files changed, 96 insertions(+)
copy {include => tools/include}/uapi/asm-generic/socket.h (98%)Signature · 1 line
2.17.1
PATCH v3 1/3 bpf: net: Add SO_DETACH_REUSEPORT_BPF
There is SO_ATTACH_REUSEPORT_[CE]BPF but there is no DETACH. This patch adds SO_DETACH_REUSEPORT_BPF sockopt. The same sockopt can be used to undo both SO_ATTACH_REUSEPORT_[CE]BPF.
reseport_detach_prog() is added and it is mostly a mirror of the existing reuseport_attach_prog(). The differences are, it does not call reuseport_alloc() and returns -ENOENT when there is no old prog.
--- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ include/net/sock_reuseport.h | 2 ++ include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 4 ++++ net/core/sock_reuseport.c | 24 ++++++++++++++++++++++++ 8 files changed, 40 insertions(+)
arch/alpha/include/uapi/asm/socket.h +2 -0
@@ -122,6 +122,8 @@#define SO_RCVTIMEO_NEW 66#define SO_SNDTIMEO_NEW 67 +#define SO_DETACH_REUSEPORT_BPF 68 +#if !defined(__KERNEL__)#if __BITS_PER_LONG ==64
arch/mips/include/uapi/asm/socket.h +2 -0
@@ -133,6 +133,8 @@#define SO_RCVTIMEO_NEW 66#define SO_SNDTIMEO_NEW 67 +#define SO_DETACH_REUSEPORT_BPF 68 +#if !defined(__KERNEL__)#if __BITS_PER_LONG ==64
arch/parisc/include/uapi/asm/socket.h +2 -0
@@ -114,6 +114,8 @@#define SO_RCVTIMEO_NEW 0x4040#define SO_SNDTIMEO_NEW 0x4041 +#define SO_DETACH_REUSEPORT_BPF 0x4042 +#if !defined(__KERNEL__)#if __BITS_PER_LONG ==64
arch/sparc/include/uapi/asm/socket.h +2 -0
@@ -115,6 +115,8 @@#define SO_RCVTIMEO_NEW 0x0044#define SO_SNDTIMEO_NEW 0x0045 +#define SO_DETACH_REUSEPORT_BPF 0x0047 +#if !defined(__KERNEL__)
include/net/sock_reuseport.h +2 -0
@@ -35,6 +35,8 @@ extern struct sock *reuseport_select_sock(struct sock *sk,struct sk_buff *skb, int hdr_len);extern int reuseport_attach_prog (struct sock *sk,struct bpf_prog *prog); +extern int reuseport_detach_prog (struct sock *sk); +int reuseport_get_id (struct sock_reuseport *reuse);#endif /* _SOCK_REUSEPORT_H */
include/uapi/asm-generic/socket.h +2 -0
@@ -117,6 +117,8 @@#define SO_RCVTIMEO_NEW 66#define SO_SNDTIMEO_NEW 67 +#define SO_DETACH_REUSEPORT_BPF 68 +#if !defined(__KERNEL__)#if __BITS_PER_LONG ==64 || (defined(__x86_64__) && defined(__ILP32__))
net/core/sock.c +4 -0
@@ -1045,6 +1045,10 @@ int sock_setsockopt(struct socket *sock, int level, int optname, }break ; +case SO_DETACH_REUSEPORT_BPF : + ret =reuseport_detach_prog (sk); +break ; +case SO_DETACH_FILTER : ret =sk_detach_filter (sk);break ;
net/core/sock_reuseport.c +24 -0
@@ -332,3 +332,27 @@ int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)return 0 ; }EXPORT_SYMBOL (reuseport_attach_prog); + +int reuseport_detach_prog (struct sock *sk) +{ +struct sock_reuseport *reuse; +struct bpf_prog *old_prog; + +if (!rcu_access_pointer (sk->sk_reuseport_cb)) +return sk->sk_reuseport ? -ENOENT : -EINVAL ; + + old_prog =NULL ; +spin_lock_bh (&reuseport_lock); + reuse =rcu_dereference_protected (sk->sk_reuseport_cb, +lockdep_is_held (&reuseport_lock)); +rcu_swap_protected (reuse->prog, old_prog, +lockdep_is_held (&reuseport_lock)); +spin_unlock_bh (&reuseport_lock); + +if (!old_prog) +return -ENOENT ; + +sk_reuseport_prog_free (old_prog); +return 0 ; +} +EXPORT_SYMBOL (reuseport_detach_prog);
Signature · 1 line
2.17.1
PATCH v3 2/3 bpf: Sync asm-generic/socket.h to tools/
In reply to Martin KaFai LauSO_DETACH_REUSEPORT_BPF is needed for the test in the next patch. It is defined in the socket.h.
--- tools/include/uapi/asm-generic/socket.h | 147 ++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 tools/include/uapi/asm-generic/socket.h
tools/include/uapi/asm-generic/socket.h new file +147 -0
@@ -0,0 +1,147 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef __ASM_GENERIC_SOCKET_H +#define __ASM_GENERIC_SOCKET_H + +#include <linux/posix_types.h> +#include <asm/sockios.h> + +/* For setsockopt(2) */ +#define SOL_SOCKET 1 + +#define SO_DEBUG 1 +#define SO_REUSEADDR 2 +#define SO_TYPE 3 +#define SO_ERROR 4 +#define SO_DONTROUTE 5 +#define SO_BROADCAST 6 +#define SO_SNDBUF 7 +#define SO_RCVBUF 8 +#define SO_SNDBUFFORCE 32 +#define SO_RCVBUFFORCE 33 +#define SO_KEEPALIVE 9 +#define SO_OOBINLINE 10 +#define SO_NO_CHECK 11 +#define SO_PRIORITY 12 +#define SO_LINGER 13 +#define SO_BSDCOMPAT 14 +#define SO_REUSEPORT 15 +#ifndef SO_PASSCRED /* powerpc only differs in these */ +#define SO_PASSCRED 16 +#define SO_PEERCRED 17 +#define SO_RCVLOWAT 18 +#define SO_SNDLOWAT 19 +#define SO_RCVTIMEO_OLD 20 +#define SO_SNDTIMEO_OLD 21 +#endif + +/* Security levels - as per NRL IPv6 - don't actually do anything */ +#define SO_SECURITY_AUTHENTICATION 22 +#define SO_SECURITY_ENCRYPTION_TRANSPORT 23 +#define SO_SECURITY_ENCRYPTION_NETWORK 24 + +#define SO_BINDTODEVICE 25 + +/* Socket filtering */ +#define SO_ATTACH_FILTER 26 +#define SO_DETACH_FILTER 27 +#define SO_GET_FILTER SO_ATTACH_FILTER + +#define SO_PEERNAME 28 + +#define SO_ACCEPTCONN 30 + +#define SO_PEERSEC 31 +#define SO_PASSSEC 34 + +#define SO_MARK 36 + +#define SO_PROTOCOL 38 +#define SO_DOMAIN 39 + +#define SO_RXQ_OVFL 40 + +#define SO_WIFI_STATUS 41 +#define SCM_WIFI_STATUS SO_WIFI_STATUS +#define SO_PEEK_OFF 42 + +/* Instruct lower device to use last 4-bytes of skb data as FCS */ +#define SO_NOFCS 43 + +#define SO_LOCK_FILTER 44 + +#define SO_SELECT_ERR_QUEUE 45 + +#define SO_BUSY_POLL 46 + +#define SO_MAX_PACING_RATE 47 + +#define SO_BPF_EXTENSIONS 48 + +#define SO_INCOMING_CPU 49 + +#define SO_ATTACH_BPF 50 +#define SO_DETACH_BPF SO_DETACH_FILTER + +#define SO_ATTACH_REUSEPORT_CBPF 51 +#define SO_ATTACH_REUSEPORT_EBPF 52 + +#define SO_CNX_ADVICE 53 + +#define SCM_TIMESTAMPING_OPT_STATS 54 + +#define SO_MEMINFO 55 + +#define SO_INCOMING_NAPI_ID 56 + +#define SO_COOKIE 57 + +#define SCM_TIMESTAMPING_PKTINFO 58 + +#define SO_PEERGROUPS 59 + +#define SO_ZEROCOPY 60 + +#define SO_TXTIME 61 +#define SCM_TXTIME SO_TXTIME + +#define SO_BINDTOIFINDEX 62 + +#define SO_TIMESTAMP_OLD 29 +#define SO_TIMESTAMPNS_OLD 35 +#define SO_TIMESTAMPING_OLD 37 + +#define SO_TIMESTAMP_NEW 63 +#define SO_TIMESTAMPNS_NEW 64 +#define SO_TIMESTAMPING_NEW 65 + +#define SO_RCVTIMEO_NEW 66 +#define SO_SNDTIMEO_NEW 67 + +#define SO_DETACH_REUSEPORT_BPF 68 + +#if !defined(__KERNEL__) + +#if __BITS_PER_LONG ==64 || (defined(__x86_64__) && defined(__ILP32__)) +/* on 64-bit and x32, avoid the ?: operator */ +#define SO_TIMESTAMP SO_TIMESTAMP_OLD +#define SO_TIMESTAMPNS SO_TIMESTAMPNS_OLD +#define SO_TIMESTAMPING SO_TIMESTAMPING_OLD + +#define SO_RCVTIMEO SO_RCVTIMEO_OLD +#define SO_SNDTIMEO SO_SNDTIMEO_OLD +#else +#define SO_TIMESTAMP (sizeof(time_t) == sizeof(__kernel_long_t) ? SO_TIMESTAMP_OLD : SO_TIMESTAMP_NEW) +#define SO_TIMESTAMPNS (sizeof(time_t) == sizeof(__kernel_long_t) ? SO_TIMESTAMPNS_OLD : SO_TIMESTAMPNS_NEW) +#define SO_TIMESTAMPING (sizeof(time_t) == sizeof(__kernel_long_t) ? SO_TIMESTAMPING_OLD : SO_TIMESTAMPING_NEW) + +#define SO_RCVTIMEO (sizeof(time_t) == sizeof(__kernel_long_t) ? SO_RCVTIMEO_OLD : SO_RCVTIMEO_NEW) +#define SO_SNDTIMEO (sizeof(time_t) == sizeof(__kernel_long_t) ? SO_SNDTIMEO_OLD : SO_SNDTIMEO_NEW) +#endif + +#define SCM_TIMESTAMP SO_TIMESTAMP +#define SCM_TIMESTAMPNS SO_TIMESTAMPNS +#define SCM_TIMESTAMPING SO_TIMESTAMPING + +#endif + +#endif /* __ASM_GENERIC_SOCKET_H */
Signature · 1 line
2.17.1
PATCH v3 3/3 bpf: Add test for SO_REUSEPORT_DETACH_BPF
In reply to Martin KaFai LauThis patch adds a test for the new sockopt SO_REUSEPORT_DETACH_BPF.
--- .../selftests/bpf/test_select_reuseport.c | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+)
tools/testing/selftests/bpf/test_select_reuseport.c +54 -0
@@ -523,6 +523,58 @@ static void test_pass_on_err(int type, sa_family_t family)printf ("OK\n" ); } +static void test_detach_bpf (int type,sa_family_t family) +{ +#ifdef SO_DETACH_REUSEPORT_BPF +__u32 nr_run_before =0 , nr_run_after =0 , tmp, i; +struct epoll_event ev; +int cli_fd, err, nev; +struct cmd cmd = {}; +int optvalue =0 ; + +printf ("%s: " , __func__); + err =setsockopt (sk_fds[0 ],SOL_SOCKET ,SO_DETACH_REUSEPORT_BPF , + &optvalue,sizeof (optvalue)); +CHECK (err ==-1 ,"setsockopt(SO_DETACH_REUSEPORT_BPF)" , +"err:%d errno:%d\n" , err, errno); + + err =setsockopt (sk_fds[1 ],SOL_SOCKET ,SO_DETACH_REUSEPORT_BPF , + &optvalue,sizeof (optvalue)); +CHECK (err ==0 || errno !=ENOENT ,"setsockopt(SO_DETACH_REUSEPORT_BPF)" , +"err:%d errno:%d\n" , err, errno); + +for (i =0 ; i <NR_RESULTS ; i++) { + err =bpf_map_lookup_elem (result_map, &i, &tmp); +CHECK (err ==-1 ,"lookup_elem(result_map)" , +"i:%u err:%d errno:%d\n" , i, err, errno); + nr_run_before += tmp; + } + + cli_fd =send_data (type, family, &cmd,sizeof (cmd),PASS ); + nev =epoll_wait (epfd, &ev,1 ,5 ); +CHECK (nev <=0 ,"nev <= 0" , +"nev:%d expected:1 type:%d family:%d data:(0, 0)\n" , + nev, type, family); + +for (i =0 ; i <NR_RESULTS ; i++) { + err =bpf_map_lookup_elem (result_map, &i, &tmp); +CHECK (err ==-1 ,"lookup_elem(result_map)" , +"i:%u err:%d errno:%d\n" , i, err, errno); + nr_run_after += tmp; + } + +CHECK (nr_run_before != nr_run_after, +"nr_run_before != nr_run_after" , +"nr_run_before:%u nr_run_after:%u\n" , + nr_run_before, nr_run_after); + +printf ("OK\n" ); +close (cli_fd); +#else +printf ("%s: SKIP\n" , __func__); +#endif +} +static void prepare_sk_fds (int type,sa_family_t family,bool inany) {const int first =REUSEPORT_ARRAY_SIZE -1 ;
@@ -664,6 +716,8 @@ static void test_all(void)test_pass (type, family);test_syncookie (type, family);test_pass_on_err (type, family); +/* Must be the last test */ +test_detach_bpf (type, family);cleanup_per_test ();printf ("\n" );
Signature · 1 line
2.17.1
Quoted 9 lines · Martin KaFai Lau
v3:
- Usercu_swap_protected(Stanislav Fomichev)
- Use0x0047forSO_DETACH_REUSEPORT_BPFfor sparc (kbuild test robot <[email protected]>)v2:
- Copyasm-generic/socket.htotools/in the new patch 2 (Stanislav Fomichev)This patch adds
SO_DETACH_REUSEPORT_BPFto detach BPF prog from reuseport sk.
For the series:
Quoted 21 lines
Martin KaFai Lau (3):
bpf: net: AddSO_DETACH_REUSEPORT_BPF
bpf: Syncasm-generic/socket.htotools/
bpf: Add test forSO_REUSEPORT_DETACH_BPFarch/alpha/include/uapi/asm/socket.h | 2 + arch/mips/include/uapi/asm/socket.h | 2 + arch/parisc/include/uapi/asm/socket.h | 2 + arch/sparc/include/uapi/asm/socket.h | 2 + include/net/sock_reuseport.h | 2 + include/uapi/asm-generic/socket.h | 2 + net/core/sock.c | 4 ++ net/core/sock_reuseport.c | 24 +++++++++ .../include}/uapi/asm-generic/socket.h | 2 + .../selftests/bpf/test_select_reuseport.c | 54 +++++++++++++++++++ 10 files changed, 96 insertions(+) copy {include => tools/include}/uapi/asm-generic/socket.h (98%)Signature · 1 line
2.17.1
Quoted 15 lines · Martin KaFai Lau
v3:
- Usercu_swap_protected(Stanislav Fomichev)
- Use0x0047forSO_DETACH_REUSEPORT_BPFfor sparc (kbuild test robot <[email protected]>)v2:
- Copyasm-generic/socket.htotools/in the new patch 2 (Stanislav Fomichev)This patch adds
SO_DETACH_REUSEPORT_BPFto detach BPF prog from reuseport sk.Martin KaFai Lau (3):
bpf: net: AddSO_DETACH_REUSEPORT_BPF
bpf: Syncasm-generic/socket.htotools/
bpf: Add test forSO_REUSEPORT_DETACH_BPF
Quoted 13 lines
arch/alpha/include/uapi/asm/socket.h | 2 +
arch/mips/include/uapi/asm/socket.h | 2 +
arch/parisc/include/uapi/asm/socket.h | 2 +
arch/sparc/include/uapi/asm/socket.h | 2 +
include/net/sock_reuseport.h | 2 +
include/uapi/asm-generic/socket.h | 2 +
net/core/sock.c | 4 ++
net/core/sock_reuseport.c | 24 +++++++++
.../include}/uapi/asm-generic/socket.h | 2 +
.../selftests/bpf/test_select_reuseport.c | 54 +++++++++++++++++++
10 files changed, 96 insertions(+)
copy {include => tools/include}/uapi/asm-generic/socket.h (98%)Applied, thanks!