Skip to content

Commit edd2340

Browse files
chantrakernel-patches-bot
authored andcommitted
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 <[email protected]>
1 parent bf289e1 commit edd2340

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

net/core/filter.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4729,6 +4729,9 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
47294729
sk->sk_prot->keepalive(sk, valbool);
47304730
sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
47314731
break;
4732+
case SO_REUSEPORT:
4733+
sk->sk_reuseport = valbool;
4734+
break;
47324735
default:
47334736
ret = -EINVAL;
47344737
}
@@ -4898,6 +4901,9 @@ static int _bpf_getsockopt(struct sock *sk, int level, int optname,
48984901
case SO_BINDTOIFINDEX:
48994902
*((int *)optval) = sk->sk_bound_dev_if;
49004903
break;
4904+
case SO_REUSEPORT:
4905+
*((int *)optval) = sk->sk_reuseport;
4906+
break;
49014907
default:
49024908
goto err_clear;
49034909
}

tools/testing/selftests/bpf/progs/bind4_prog.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ static __inline int bind_to_device(struct bpf_sock_addr *ctx)
5757
return 0;
5858
}
5959

60+
static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
61+
{
62+
63+
int val = 1;
64+
65+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
66+
&val, sizeof(val)))
67+
return 1;
68+
if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
69+
&val, sizeof(val)) || !val)
70+
return 1;
71+
val = 0;
72+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
73+
&val, sizeof(val)))
74+
return 1;
75+
if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
76+
&val, sizeof(val)) || val)
77+
return 1;
78+
79+
80+
return 0;
81+
}
82+
6083
static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
6184
{
6285
int old, tmp, new = 0xeb9f;
@@ -127,6 +150,10 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)
127150
if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
128151
return 0;
129152

153+
/* Set reuseport and unset */
154+
if (bind_reuseport(ctx))
155+
return 0;
156+
130157
ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
131158
ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
132159

tools/testing/selftests/bpf/progs/bind6_prog.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ static __inline int bind_to_device(struct bpf_sock_addr *ctx)
6363
return 0;
6464
}
6565

66+
static __inline int bind_reuseport(struct bpf_sock_addr *ctx)
67+
{
68+
69+
int val = 1;
70+
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+
val = 0;
78+
if (bpf_setsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
79+
&val, sizeof(val)))
80+
return 1;
81+
if (bpf_getsockopt(ctx, SOL_SOCKET, SO_REUSEPORT,
82+
&val, sizeof(val)) || val)
83+
return 1;
84+
85+
86+
return 0;
87+
}
88+
6689
static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
6790
{
6891
int old, tmp, new = 0xeb9f;
@@ -141,6 +164,10 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)
141164
if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
142165
return 0;
143166

167+
/* Set reuseport and unset */
168+
if (bind_reuseport(ctx))
169+
return 0;
170+
144171
ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
145172
ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
146173
ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);

0 commit comments

Comments
 (0)