bpf: bpf_{g,s}etsockopt for struct bpf_sock_addr
Subsystems (6)
PATCH v3 1/1 bpf: bpf_{g,s}etsockopt for struct bpf_sock_addr
Currently, bpf_getsockopt and bpf_setsockopt helpers operate on the 'struct bpf_sock_ops' context in BPF_PROG_TYPE_SOCK_OPS program. Let's generalize them and make them available for 'struct bpf_sock_addr'. That way, in the future, we can allow those helpers in more places.
As an example, let's expose those 'struct bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.
v3:
* Expose custom helpers for bpf_sock_addr context instead of doing generic bpf_sock argument (as suggested by Daniel). Even with try_socket_lock that doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.
v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/
--- include/uapi/linux/bpf.h | 14 ++- net/core/filter.c | 118 ++++++++++++++---- tools/include/uapi/linux/bpf.h | 14 ++- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/progs/connect4_prog.c | 46 +++++++ 5 files changed, 166 insertions(+), 27 deletions(-)
include/uapi/linux/bpf.h +12 -2
@@ -1564,7 +1564,7 @@ union bpf_attr {* Return * 0 * -* int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) +* int bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) * Description * Emulate a call to **setsockopt()** on the socket associated to * *bpf_socket*, which must be a full socket. The *level* at
@@ -1572,6 +1572,11 @@ union bpf_attr {* must be specified, see **setsockopt(2)** for more information. * The option value of length *optlen* is pointed by *optval*. * +* *bpf_socket* should be one of the following: +* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. +* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** +* and **BPF_CGROUP_INET6_CONNECT**. +* * This helper actually implements a subset of **setsockopt()**. * It supports the following *level*\ s: *
@@ -1766,7 +1771,7 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * -* int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) +* int bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) * Description * Emulate a call to **getsockopt()** on the socket associated to * *bpf_socket*, which must be a full socket. The *level* at
@@ -1775,6 +1780,11 @@ union bpf_attr {* The retrieved value is stored in the structure pointed by * *opval* and of length *optlen*. * +* *bpf_socket* should be one of the following: +* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. +* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** +* and **BPF_CGROUP_INET6_CONNECT**. +* * This helper actually implements a subset of **getsockopt()**. * It supports the following *level*\ s: *
net/core/filter.c +95 -23
165 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +12 -2
@@ -1564,7 +1564,7 @@ union bpf_attr {* Return * 0 * -* int bpf_setsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) +* int bpf_setsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) * Description * Emulate a call to **setsockopt()** on the socket associated to * *bpf_socket*, which must be a full socket. The *level* at
@@ -1572,6 +1572,11 @@ union bpf_attr {* must be specified, see **setsockopt(2)** for more information. * The option value of length *optlen* is pointed by *optval*. * +* *bpf_socket* should be one of the following: +* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. +* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** +* and **BPF_CGROUP_INET6_CONNECT**. +* * This helper actually implements a subset of **setsockopt()**. * It supports the following *level*\ s: *
@@ -1766,7 +1771,7 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * -* int bpf_getsockopt(struct bpf_sock_ops *bpf_socket, int level, int optname, void *optval, int optlen) +* int bpf_getsockopt(void *bpf_socket, int level, int optname, void *optval, int optlen) * Description * Emulate a call to **getsockopt()** on the socket associated to * *bpf_socket*, which must be a full socket. The *level* at
@@ -1775,6 +1780,11 @@ union bpf_attr {* The retrieved value is stored in the structure pointed by * *opval* and of length *optlen*. * +* *bpf_socket* should be one of the following: +* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. +* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** +* and **BPF_CGROUP_INET6_CONNECT**. +* * This helper actually implements a subset of **getsockopt()**. * It supports the following *level*\ s: *
tools/testing/selftests/bpf/config +1 -0
@@ -37,3 +37,4 @@ CONFIG_IPV6_SIT=m CONFIG_BPF_JIT=y CONFIG_BPF_LSM=y CONFIG_SECURITY=y +CONFIG_TCP_CONG_DCTCP=y
tools/testing/selftests/bpf/progs/connect4_prog.c +46 -0
70 lines not on this page · raw message
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Quoted 9 lines · Stanislav Fomichev
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.
Quoted 5 lines
v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.
Quoted 15 lines · Stanislav Fomichev
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.
Quoted 7 lines
v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>---
To bad we had to fall back to ctx here.
Alexei Starovoitov
AppliedQuoted 20 lines · John Fastabend
Quoted 18 lines · Stanislav Fomichev
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/
Quoted 8 lines
Quoted 4 lines
Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>---
To bad we had to fall back to ctx here.
Acked-by John Fastabend <[email protected]>
Applied. Thanks
Stanislav Fomichev <[email protected]> [Thu, 2020-04-30 16:32 -0700]:
Quoted 21 lines
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
...
tools/testing/selftests/bpf/progs/connect4_prog.c int connect_v4_prog(struct bpf_sock_addr *ctx) · line 113
Quoted 10 lines
SEC("cgroup/connect4")
intconnect_v4_prog(struct bpf_sock_addr *ctx)
{tools/testing/selftests/bpf/progs/connect4_prog.c +3
-0@@ -66,6 +108,10 @@ int connect_v4_prog(struct bpf_sock_addr *ctx)bpf_sk_release (sk); +/* Rewrite congestion control. */ +if (ctx->type ==SOCK_STREAM &&set_cc (ctx)) +return 0 ;
Hi Stas,
This new check breaks one of tests in test_sock_addr:
root@arch-fb-vm1:/home/rdna/bpf-next/tools/testing/selftests/bpf ./test_sock_addr.sh
...
(test_sock_addr.c:1199: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
...
Summary: 34 PASSED, 1 FAILED
What the test does is it sets up TCPv4 server:
[pid 386] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 6
[pid 386] bind(6, {sa_family=AF_INET, sin_port=htons(4444), sin_addr=inet_addr("127.0.0.1")}, 128) = 0
[pid 386] listen(6, 128) = 0Then tries to connect to a fake IPv4:port and this connect4 program should redirect it to that TCP server, but only if every field in context has expected value.
But after that commit program started denying the connect:
[pid 386] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 7
[pid 386] connect(7, {sa_family=AF_INET, sin_port=htons(4040), sin_addr=inet_addr("192.168.1.254")}, 128) = -1 EPERM (Operation not permitted)
(test_sock_addr.c:1201: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
I verified that commenting out this new if fixes the problem, but haven't spent time root-causing it. Could you please look at it?
Thanks.
Signature · 1 line
Andrey Ignatov
Quoted 60 lines · Andrey Ignatov
Stanislav Fomichev <[email protected]> [Thu, 2020-04-30 16:32 -0700]:
Quoted 21 lines
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>...
tools/testing/selftests/bpf/progs/connect4_prog.c int connect_v4_prog(struct bpf_sock_addr *ctx) · line 113
Quoted 10 lines
SEC("cgroup/connect4")
intconnect_v4_prog(struct bpf_sock_addr *ctx)
{tools/testing/selftests/bpf/progs/connect4_prog.c +3
-0@@ -66,6 +108,10 @@ int connect_v4_prog(struct bpf_sock_addr *ctx)bpf_sk_release (sk); +/* Rewrite congestion control. */ +if (ctx->type ==SOCK_STREAM &&set_cc (ctx)) +return 0 ;Hi Stas,
This new check breaks one of tests in
test_sock_addr:root@arch-fb-vm1:/home/rdna/bpf-next/tools/testing/selftests/bpf ./test_sock_addr.sh
...
(test_sock_addr.c:1199: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
...
Summary: 34 PASSED, 1 FAILEDWhat the test does is it sets up TCPv4 server:
[pid 386] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 6 [pid 386] bind(6, {sa_family=AF_INET, sin_port=htons(4444), sin_addr=inet_addr("127.0.0.1")}, 128) = 0 [pid 386] listen(6, 128) = 0Then tries to connect to a fake IPv4:port and this connect4 program should redirect it to that TCP server, but only if every field in context has expected value.
But after that commit program started denying the connect:
Quoted 7 lines
[pid 386]
socket(PF_INET, SOCK_STREAM, IPPROTO_IP)= 7
[pid 386]connect(7, {sa_family=AF_INET, sin_port=htons(4040), sin_addr=inet_addr("192.168.1.254")}, 128)= -1 EPERM (Operation not permitted)
(test_sock_addr.c:1201: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]I verified that commenting out this new
iffixes the problem, but haven't spent time root-causing it. Could you please look at it?
Could you please confirm that you have CONFIG_TCP_CONG_DCTCP=y in your kernel config? (I've added it to tools/testing/selftests/bpf/config)
The test is now flipping CC to dctcp and back to default cubic. It can fail if dctcp is not compiled in.
Stanislav Fomichev <[email protected]> [Fri, 2020-05-01 15:07 -0700]:
Quoted 66 lines · Andrey Ignatov
Quoted 65 lines
Stanislav Fomichev <[email protected]> [Thu, 2020-04-30 16:32 -0700]:
Quoted 21 lines
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>...
tools/testing/selftests/bpf/progs/connect4_prog.c int connect_v4_prog(struct bpf_sock_addr *ctx) · line 113
Quoted 10 lines
SEC("cgroup/connect4")
intconnect_v4_prog(struct bpf_sock_addr *ctx)
{tools/testing/selftests/bpf/progs/connect4_prog.c +3
-0@@ -66,6 +108,10 @@ int connect_v4_prog(struct bpf_sock_addr *ctx)bpf_sk_release (sk); +/* Rewrite congestion control. */ +if (ctx->type ==SOCK_STREAM &&set_cc (ctx)) +return 0 ;Hi Stas,
This new check breaks one of tests in
test_sock_addr:root@arch-fb-vm1:/home/rdna/bpf-next/tools/testing/selftests/bpf ./test_sock_addr.sh
...
(test_sock_addr.c:1199: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
...
Summary: 34 PASSED, 1 FAILEDWhat the test does is it sets up TCPv4 server:
[pid 386] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 6 [pid 386] bind(6, {sa_family=AF_INET, sin_port=htons(4444), sin_addr=inet_addr("127.0.0.1")}, 128) = 0 [pid 386] listen(6, 128) = 0Then tries to connect to a fake IPv4:port and this connect4 program should redirect it to that TCP server, but only if every field in context has expected value.
But after that commit program started denying the connect:
[pid 386]
socket(PF_INET, SOCK_STREAM, IPPROTO_IP)= 7
[pid 386]connect(7, {sa_family=AF_INET, sin_port=htons(4040), sin_addr=inet_addr("192.168.1.254")}, 128)= -1 EPERM (Operation not permitted)
(test_sock_addr.c:1201: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
Quoted 6 lines
Quoted 2 lines
I verified that commenting out this new
iffixes the problem, but haven't spent time root-causing it. Could you please look at it?Could you please confirm that you have
CONFIG_TCP_CONG_DCTCP=yin your kernel config? (I've added it totools/testing/selftests/bpf/config)
The test is now flipping CC to dctcp and back to default cubic. It can fail if dctcp is not compiled in.
Right. Martin asked same question and indeed my testing VM didn't have dctcp enabled. With dctcp enabled it works fine.
I'm totally fine to keep dctcp enabled in my config or start using tools/testing/selftests/bpf/config (I've always used my own config).
Another options can be to switch from dctcp to more widely-used reno in the program (I tested, this works as well), or even check net/ipv4/tcp_available_congestion_control and use a pair of whatever cc available there, but up to you / BPF mainteiners really, as I said I'm personally fine to just enable dctcp.
Thanks.
Signature · 1 line
Andrey Ignatov
Quoted 78 lines · Andrey Ignatov
Stanislav Fomichev <[email protected]> [Fri, 2020-05-01 15:07 -0700]:
Quoted 66 lines · Andrey Ignatov
Quoted 65 lines
Stanislav Fomichev <[email protected]> [Thu, 2020-04-30 16:32 -0700]:
Quoted 21 lines
Currently,
bpf_getsockoptandbpf_setsockopthelpers operate on the 'structbpf_sock_ops' context inBPF_PROG_TYPE_SOCK_OPSprogram. Let's generalize them and make them available for 'structbpf_sock_addr'. That way, in the future, we can allow those helpers in more places.As an example, let's expose those 'struct
bpf_sock_addr' based helpers to BPF_CGROUP_INET{4,6}_CONNECT hooks. That way we can override CC before the connection is made.v3:
* Expose custom helpers forbpf_sock_addrcontext instead of doing genericbpf_sockargument (as suggested by Daniel). Even withtry_socket_lockthat doesn't sleep we have a problem where context sk is already locked and socket lock is non-nestable.v2:
* s/BPF_PROG_TYPE_CGROUP_SOCKOPT/BPF_PROG_TYPE_SOCK_OPS/Cc John Fastabend <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>...
tools/testing/selftests/bpf/progs/connect4_prog.c int connect_v4_prog(struct bpf_sock_addr *ctx) · line 113
Quoted 10 lines
SEC("cgroup/connect4")
intconnect_v4_prog(struct bpf_sock_addr *ctx)
{tools/testing/selftests/bpf/progs/connect4_prog.c +3
-0@@ -66,6 +108,10 @@ int connect_v4_prog(struct bpf_sock_addr *ctx)bpf_sk_release (sk); +/* Rewrite congestion control. */ +if (ctx->type ==SOCK_STREAM &&set_cc (ctx)) +return 0 ;Hi Stas,
This new check breaks one of tests in
test_sock_addr:root@arch-fb-vm1:/home/rdna/bpf-next/tools/testing/selftests/bpf ./test_sock_addr.sh
...
(test_sock_addr.c:1199: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]
...
Summary: 34 PASSED, 1 FAILEDWhat the test does is it sets up TCPv4 server:
[pid 386] socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 6 [pid 386] bind(6, {sa_family=AF_INET, sin_port=htons(4444), sin_addr=inet_addr("127.0.0.1")}, 128) = 0 [pid 386] listen(6, 128) = 0Then tries to connect to a fake IPv4:port and this connect4 program should redirect it to that TCP server, but only if every field in context has expected value.
But after that commit program started denying the connect:
[pid 386]
socket(PF_INET, SOCK_STREAM, IPPROTO_IP)= 7
[pid 386]connect(7, {sa_family=AF_INET, sin_port=htons(4040), sin_addr=inet_addr("192.168.1.254")}, 128)= -1 EPERM (Operation not permitted)
(test_sock_addr.c:1201: errno: Operation not permitted) Fail to connect to server Test case: connect4: rewrite IP & TCP port .. [FAIL]Quoted 6 lines
Quoted 2 lines
I verified that commenting out this new
iffixes the problem, but haven't spent time root-causing it. Could you please look at it?Could you please confirm that you have
CONFIG_TCP_CONG_DCTCP=yin your kernel config? (I've added it totools/testing/selftests/bpf/config)
The test is now flipping CC to dctcp and back to default cubic. It can fail if dctcp is not compiled in.Right. Martin asked same question and indeed my testing VM didn't have dctcp enabled. With dctcp enabled it works fine.
Quoted 8 lines
I'm totally fine to keep dctcp enabled in my config or start using
tools/testing/selftests/bpf/config(I've always used my own config).Another options can be to switch from dctcp to more widely-used reno in the program (I tested, this works as well), or even check
net/ipv4/tcp_available_congestion_controland use a pair of whatever cc available there, but up to you / BPF mainteiners really, as I said I'm personally fine to just enable dctcp.
Hm, good point, reno is always compiled in, I didn't think about it. Let me prepare a patch to do s/dctcp/reno/ in that test, thanks!