Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Add getter and setter for SO_REUSEPORT through bpf_{g,s}etsockopt

Augment the current set of options that are accessible via
bpf_{g,s}etsockopt to also support SO_REUSEPORT.

Signed-off-by: Manu Bretelle <chantra@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20210310182305.1910312-1-chantra@fb.com

authored by

Manu Bretelle and committed by
Daniel Borkmann
6503b9f2 1211f4e9

+56
+6
net/core/filter.c
··· 4729 4729 sk->sk_prot->keepalive(sk, valbool); 4730 4730 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool); 4731 4731 break; 4732 + case SO_REUSEPORT: 4733 + sk->sk_reuseport = valbool; 4734 + break; 4732 4735 default: 4733 4736 ret = -EINVAL; 4734 4737 } ··· 4900 4897 break; 4901 4898 case SO_BINDTOIFINDEX: 4902 4899 *((int *)optval) = sk->sk_bound_dev_if; 4900 + break; 4901 + case SO_REUSEPORT: 4902 + *((int *)optval) = sk->sk_reuseport; 4903 4903 break; 4904 4904 default: 4905 4905 goto err_clear;
+25
tools/testing/selftests/bpf/progs/bind4_prog.c
··· 57 57 return 0; 58 58 } 59 59 60 + static __inline int bind_reuseport(struct bpf_sock_addr *ctx) 61 + { 62 + int val = 1; 63 + 64 + if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 65 + &val, sizeof(val))) 66 + return 1; 67 + if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 68 + &val, sizeof(val)) || !val) 69 + return 1; 70 + val = 0; 71 + if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 72 + &val, sizeof(val))) 73 + return 1; 74 + if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 75 + &val, sizeof(val)) || val) 76 + return 1; 77 + 78 + return 0; 79 + } 80 + 60 81 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt) 61 82 { 62 83 int old, tmp, new = 0xeb9f; ··· 146 125 147 126 /* Test for misc socket options. */ 148 127 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY)) 128 + return 0; 129 + 130 + /* Set reuseport and unset */ 131 + if (bind_reuseport(ctx)) 149 132 return 0; 150 133 151 134 ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
+25
tools/testing/selftests/bpf/progs/bind6_prog.c
··· 63 63 return 0; 64 64 } 65 65 66 + static __inline int bind_reuseport(struct bpf_sock_addr *ctx) 67 + { 68 + int val = 1; 69 + 70 + if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 71 + &val, sizeof(val))) 72 + return 1; 73 + if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 74 + &val, sizeof(val)) || !val) 75 + return 1; 76 + val = 0; 77 + if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 78 + &val, sizeof(val))) 79 + return 1; 80 + if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT, 81 + &val, sizeof(val)) || val) 82 + return 1; 83 + 84 + return 0; 85 + } 86 + 66 87 static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt) 67 88 { 68 89 int old, tmp, new = 0xeb9f; ··· 160 139 161 140 /* Test for misc socket options. */ 162 141 if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY)) 142 + return 0; 143 + 144 + /* Set reuseport and unset */ 145 + if (bind_reuseport(ctx)) 163 146 return 0; 164 147 165 148 ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);