Skip to content

Commit 99f3a06

Browse files
iamkafaiborkmann
authored andcommitted
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. Cc: Craig Gallek <kraig@google.com> Signed-off-by: Martin KaFai Lau <kafai@fb.com> Reviewed-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent e55d54f commit 99f3a06

8 files changed

Lines changed: 40 additions & 0 deletions

File tree

‎arch/alpha/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
#define SO_RCVTIMEO_NEW 66
123123
#define SO_SNDTIMEO_NEW 67
124124

125+
#define SO_DETACH_REUSEPORT_BPF 68
126+
125127
#if !defined(__KERNEL__)
126128

127129
#if __BITS_PER_LONG == 64

‎arch/mips/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@
133133
#define SO_RCVTIMEO_NEW 66
134134
#define SO_SNDTIMEO_NEW 67
135135

136+
#define SO_DETACH_REUSEPORT_BPF 68
137+
136138
#if !defined(__KERNEL__)
137139

138140
#if __BITS_PER_LONG == 64

‎arch/parisc/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
#define SO_RCVTIMEO_NEW 0x4040
115115
#define SO_SNDTIMEO_NEW 0x4041
116116

117+
#define SO_DETACH_REUSEPORT_BPF 0x4042
118+
117119
#if !defined(__KERNEL__)
118120

119121
#if __BITS_PER_LONG == 64

‎arch/sparc/include/uapi/asm/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
#define SO_RCVTIMEO_NEW 0x0044
116116
#define SO_SNDTIMEO_NEW 0x0045
117117

118+
#define SO_DETACH_REUSEPORT_BPF 0x0047
119+
118120
#if !defined(__KERNEL__)
119121

120122

‎include/net/sock_reuseport.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern struct sock *reuseport_select_sock(struct sock *sk,
3535
struct sk_buff *skb,
3636
int hdr_len);
3737
extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
38+
extern int reuseport_detach_prog(struct sock *sk);
39+
3840
int reuseport_get_id(struct sock_reuseport *reuse);
3941

4042
#endif /* _SOCK_REUSEPORT_H */

‎include/uapi/asm-generic/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
#define SO_RCVTIMEO_NEW 66
118118
#define SO_SNDTIMEO_NEW 67
119119

120+
#define SO_DETACH_REUSEPORT_BPF 68
121+
120122
#if !defined(__KERNEL__)
121123

122124
#if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))

‎net/core/sock.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,10 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
10451045
}
10461046
break;
10471047

1048+
case SO_DETACH_REUSEPORT_BPF:
1049+
ret = reuseport_detach_prog(sk);
1050+
break;
1051+
10481052
case SO_DETACH_FILTER:
10491053
ret = sk_detach_filter(sk);
10501054
break;

‎net/core/sock_reuseport.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,27 @@ int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)
332332
return 0;
333333
}
334334
EXPORT_SYMBOL(reuseport_attach_prog);
335+
336+
int reuseport_detach_prog(struct sock *sk)
337+
{
338+
struct sock_reuseport *reuse;
339+
struct bpf_prog *old_prog;
340+
341+
if (!rcu_access_pointer(sk->sk_reuseport_cb))
342+
return sk->sk_reuseport ? -ENOENT : -EINVAL;
343+
344+
old_prog = NULL;
345+
spin_lock_bh(&reuseport_lock);
346+
reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
347+
lockdep_is_held(&reuseport_lock));
348+
rcu_swap_protected(reuse->prog, old_prog,
349+
lockdep_is_held(&reuseport_lock));
350+
spin_unlock_bh(&reuseport_lock);
351+
352+
if (!old_prog)
353+
return -ENOENT;
354+
355+
sk_reuseport_prog_free(old_prog);
356+
return 0;
357+
}
358+
EXPORT_SYMBOL(reuseport_detach_prog);

0 commit comments

Comments
 (0)