bpf: add BPF_CGROUP_INET_SOCK_RELEASE hook
Subsystems (9)
PATCH v2 1/4 bpf: add BPF_CGROUP_INET_SOCK_RELEASE hook
Sometimes it's handy to know when the socket gets freed. In particular, we'd like to try to use a smarter allocation of ports for bpf_bind and explore the possibility of limiting the number of SOCK_DGRAM sockets the process can have. Adding a release pair to existing BPF_CGROUP_INET_SOCK_CREATE can unlock both of the mentioned features.
The only questionable part here is the sock->sk check in the inet_release. Looking at the places where we do 'sock->sk = NULL', I don't understand how it can race with inet_release and why the check is there (it's been there since the initial git import). Otherwise, the change itself is pretty simple, we add a BPF hook to the inet_release and avoid calling it for kernel sockets.
v2:
* fix compile issue with CONFIG_CGROUP_BPF=n (kernel test robot)
--- include/linux/bpf-cgroup.h | 4 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 3 +++ net/core/filter.c | 1 + net/ipv4/af_inet.c | 3 +++ 5 files changed, 12 insertions(+)
include/linux/bpf-cgroup.h +4 -0
@@ -210,6 +210,9 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,#define BPF_CGROUP_RUN_PROG_INET_SOCK (sk) \ BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE) +#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE (sk) \ + BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE) +#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND (sk) \ BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
@@ -401,6 +404,7 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,#define BPF_CGROUP_RUN_PROG_INET_INGRESS (sk,skb) ({ 0; })#define BPF_CGROUP_RUN_PROG_INET_EGRESS (sk,skb) ({ 0; })#define BPF_CGROUP_RUN_PROG_INET_SOCK (sk) ({ 0; }) +#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE (sk) ({ 0; })#define BPF_CGROUP_RUN_PROG_INET4_BIND (sk, uaddr) ({ 0; })#define BPF_CGROUP_RUN_PROG_INET6_BIND (sk, uaddr) ({ 0; })#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND (sk) ({ 0; })
include/uapi/linux/bpf.h +1 -0
@@ -226,6 +226,7 @@ enum bpf_attach_type {BPF_CGROUP_INET4_GETSOCKNAME ,BPF_CGROUP_INET6_GETSOCKNAME ,BPF_XDP_DEVMAP , +BPF_CGROUP_INET_SOCK_RELEASE , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/syscall.c +3 -0
@@ -1994,6 +1994,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,case BPF_PROG_TYPE_CGROUP_SOCK :switch (expected_attach_type) {case BPF_CGROUP_INET_SOCK_CREATE : +case BPF_CGROUP_INET_SOCK_RELEASE :case BPF_CGROUP_INET4_POST_BIND :case BPF_CGROUP_INET6_POST_BIND :return 0 ;
@@ -2792,6 +2793,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)return BPF_PROG_TYPE_CGROUP_SKB ;break ;case BPF_CGROUP_INET_SOCK_CREATE : +case BPF_CGROUP_INET_SOCK_RELEASE :case BPF_CGROUP_INET4_POST_BIND :case BPF_CGROUP_INET6_POST_BIND :return BPF_PROG_TYPE_CGROUP_SOCK ;
@@ -2942,6 +2944,7 @@ static int bpf_prog_query(const union bpf_attr *attr,case BPF_CGROUP_INET_INGRESS :case BPF_CGROUP_INET_EGRESS :case BPF_CGROUP_INET_SOCK_CREATE : +case BPF_CGROUP_INET_SOCK_RELEASE :case BPF_CGROUP_INET4_BIND :case BPF_CGROUP_INET6_BIND :case BPF_CGROUP_INET4_POST_BIND :
net/core/filter.c +1 -0
@@ -6855,6 +6855,7 @@ static bool __sock_filter_check_attach_type(int off,case offsetof(struct bpf_sock , priority):switch (attach_type) {case BPF_CGROUP_INET_SOCK_CREATE : +case BPF_CGROUP_INET_SOCK_RELEASE : goto full_access;default :return false;
net/ipv4/af_inet.c +3 -0
@@ -411,6 +411,9 @@ int inet_release(struct socket *sock)if (sk) {long timeout; +if (!sk->sk_kern_sock) +BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE (sk); +/* Applications forget to leave groups before exiting */ ip_mc_drop_socket (sk);
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog
PATCH v2 2/4 libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE
Add auto-detection for the cgroup/sock_release programs.
--- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 ++ 2 files changed, 3 insertions(+)
tools/include/uapi/linux/bpf.h +1 -0
@@ -226,6 +226,7 @@ enum bpf_attach_type {BPF_CGROUP_INET4_GETSOCKNAME ,BPF_CGROUP_INET6_GETSOCKNAME ,BPF_XDP_DEVMAP , +BPF_CGROUP_INET_SOCK_RELEASE , __MAX_BPF_ATTACH_TYPE };
tools/lib/bpf/libbpf.c +2 -0
@@ -6670,6 +6670,8 @@ static const struct bpf_sec_def section_defs[] = {BPF_APROG_SEC ("cgroup_skb/egress" ,BPF_PROG_TYPE_CGROUP_SKB ,BPF_CGROUP_INET_EGRESS ),BPF_APROG_COMPAT ("cgroup/skb" ,BPF_PROG_TYPE_CGROUP_SKB ), +BPF_EAPROG_SEC ("cgroup/sock_release" ,BPF_PROG_TYPE_CGROUP_SOCK , +BPF_CGROUP_INET_SOCK_RELEASE ),BPF_APROG_SEC ("cgroup/sock" ,BPF_PROG_TYPE_CGROUP_SOCK ,BPF_CGROUP_INET_SOCK_CREATE ), BPF_EAPROG_SEC("cgroup/post_bind4", BPF_PROG_TYPE_CGROUP_SOCK,
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog
Re: [PATCH bpf-next v2 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE
Quoted 5 lines · Stanislav Fomichev
Add auto-detection for the cgroup/sock_release programs.
Signed-off-by Stanislav Fomichev <[email protected]>---
Left a bunch of comments on v1 of this series, oops.
But where is the cover letter? Did I miss it?
Quoted 4 lines
tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 ++ 2 files changed, 3 insertions(+)
[...]
Re: [PATCH bpf-next v2 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE
Quoted 9 lines · Andrii Nakryiko
Quoted 5 lines · Stanislav Fomichev
Add auto-detection for the cgroup/sock_release programs.
Signed-off-by Stanislav Fomichev <[email protected]>---
Left a bunch of comments on v1 of this series, oops.
No worries, there is not much changed since then, thanks for the review!
Quoted 1 line
But where is the cover letter? Did I miss it?
I didn't feel like it deserves a cover letter. The main patch is small, most of the changes are in libbpf/selftests and I mostly split them to make it easier for you to push to github (is it still a requirement or I shouldn't worry about it anymore?).
Re: [PATCH bpf-next v2 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE
Quoted 11 lines · Stanislav Fomichev
Quoted 8 lines · Andrii Nakryiko
Quoted 5 lines · Stanislav Fomichev
Add auto-detection for the cgroup/sock_release programs.
Signed-off-by Stanislav Fomichev <[email protected]>---
Quoted 8 lines
Quoted 1 line
Left a bunch of comments on v1 of this series, oops.
No worries, there is not much changed since then, thanks for the review!
Quoted 1 line
But where is the cover letter? Did I miss it?
I didn't feel like it deserves a cover letter. The main patch is small, most of the changes are in libbpf/selftests and I mostly split them to make it easier for you to push to github (is it still a requirement or I shouldn't worry about it anymore?).
It's not a requirement, sync script is pretty smart about all that, so no need to do it specifically for the sync purposes. Still a good idea to have selftests separately, IMO.
As for the cover letter, unless it's some trivial independent fixes, cover letter ties everything together and is a good place to track "what changed between versions". So I'd recommend adding it anyways.
Re: [PATCH bpf-next v2 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE
Quoted 4 lines · Andrii Nakryiko
As for the cover letter, unless it's some trivial independent fixes, cover letter ties everything together and is a good place to track "what changed between versions". So I'd recommend adding it anyways.
+1
For independent fixes it's ok to skip cover, but it's often nice to have one. New release hook like this one certainly deserves a cover.
PATCH v2 3/4 bpftool: support BPF_CGROUP_INET_SOCK_RELEASE
In reply to Stanislav FomichevSupport attaching to sock_release from the bpftool.
--- tools/bpf/bpftool/main.h | 1 + 1 file changed, 1 insertion(+)
tools/bpf/bpftool/main.h +1 -0
@@ -92,6 +92,7 @@ static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { [BPF_CGROUP_INET_INGRESS] = "ingress", [BPF_CGROUP_INET_EGRESS] = "egress", [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", + [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release", [BPF_CGROUP_SOCK_OPS] = "sock_ops", [BPF_CGROUP_DEVICE] = "device", [BPF_CGROUP_INET4_BIND] = "bind4",
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 3/4] bpftool: support BPF_CGROUP_INET_SOCK_RELEASE
Quoted 12 lines · Stanislav Fomichev
Support attaching to
sock_releasefrom the bpftool.Signed-off-by Stanislav Fomichev <[email protected]>--- tools/bpf/bpftool/main.h | 1 + 1 file changed, 1 insertion(+)tools/bpf/bpftool/main.h +0
-0
tools/bpf/bpftool/main.h static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { · line 98
Quoted 7 lines
tools/bpf/bpftool/main.h +1
-0@@ -92,6 +92,7 @@ static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { [BPF_CGROUP_INET_INGRESS] = "ingress", [BPF_CGROUP_INET_EGRESS] = "egress", [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", + [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release", [BPF_CGROUP_SOCK_OPS] = "sock_ops", [BPF_CGROUP_DEVICE] = "device", [BPF_CGROUP_INET4_BIND] = "bind4",
This one is not on latest bpf-next, needs rebase due to 16d37ee3d2b1 ("tools, bpftool: Define attach_type_name array only once").
Thanks,
Daniel
Re: [PATCH bpf-next v2 3/4] bpftool: support BPF_CGROUP_INET_SOCK_RELEASE
Quoted 9 lines · Daniel Borkmann
Quoted 7 lines · Stanislav Fomichev
Support attaching to
sock_releasefrom the bpftool.Signed-off-by Stanislav Fomichev <[email protected]>--- tools/bpf/bpftool/main.h | 1 + 1 file changed, 1 insertion(+)
Quoted 15 lines
tools/bpf/bpftool/main.h static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { · line 98
Quoted 12 lines
tools/bpf/bpftool/main.h +1
-0@@ -92,6 +92,7 @@ static const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = { [BPF_CGROUP_INET_INGRESS] = "ingress", [BPF_CGROUP_INET_EGRESS] = "egress", [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create", + [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release", [BPF_CGROUP_SOCK_OPS] = "sock_ops", [BPF_CGROUP_DEVICE] = "device", [BPF_CGROUP_INET4_BIND] = "bind4",This one is not on latest bpf-next, needs rebase due to
16d37ee3d2b1("tools, bpftool: Defineattach_type_namearray only once").
Sure, will follow up with a v3 to address Andrii's suggestions + will rebase on top of the latest bpf-next!
PATCH v2 4/4 selftests/bpf: test BPF_CGROUP_INET_SOCK_RELEASE
In reply to Stanislav FomichevSimple test that enforces a single SOCK_DGRAM socker per cgroup.
--- .../selftests/bpf/prog_tests/udp_limit.c | 71 +++++++++++++++++++ tools/testing/selftests/bpf/progs/udp_limit.c | 42 +++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/udp_limit.c create mode 100644 tools/testing/selftests/bpf/progs/udp_limit.c
tools/testing/selftests/bpf/prog_tests/udp_limit.c new file +71 -0
@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "udp_limit.skel.h" + +#include <sys/types.h> +#include <sys/socket.h> + +void test_udp_limit (void ) +{ +struct udp_limit *skel; +int cgroup_fd; +int fd1, fd2; +int err; + + cgroup_fd =test__join_cgroup ("/udp_limit" ); +if (CHECK_FAIL (cgroup_fd <0 )) +return ; + + skel =udp_limit__open_and_load (); +if (CHECK_FAIL (!skel)) + goto close_cgroup_fd; + + err =bpf_prog_attach (bpf_program__fd (skel->progs.sock), + cgroup_fd,BPF_CGROUP_INET_SOCK_CREATE ,0 ); +if (CHECK_FAIL (err)) + goto close_skeleton; + + err =bpf_prog_attach (bpf_program__fd (skel->progs.sock_release), + cgroup_fd,BPF_CGROUP_INET_SOCK_RELEASE ,0 ); +if (CHECK_FAIL (err)) + goto close_skeleton; + +/* BPF program enforces a single UDP socket per cgroup, +* verify that. +*/ + fd1 =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (CHECK_FAIL (fd1 <0 )) + goto close_skeleton; + + fd2 =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (CHECK_FAIL (fd2 !=-1 )) + goto close_fd1; + +/* We can reopen again after close. */ +close (fd1); + + fd1 =socket (AF_INET ,SOCK_DGRAM ,0 ); +if (CHECK_FAIL (fd1 <0 )) + goto close_skeleton; + +/* Make sure the program was invoked the expected +* number of times: +* - open fd1 - BPF_CGROUP_INET_SOCK_CREATE +* - attempt to openfd2 - BPF_CGROUP_INET_SOCK_CREATE +* - close fd1 - BPF_CGROUP_INET_SOCK_RELEASE +* - open fd1 again - BPF_CGROUP_INET_SOCK_CREATE +*/ +if (CHECK_FAIL (skel->bss->invocations !=4 )) + goto close_fd1; + +/* We should still have a single socket in use */ +if (CHECK_FAIL (skel->bss->in_use !=1 )) + goto close_fd1; + +close_fd1: +close (fd1); +close_skeleton: +udp_limit__destroy (skel); +close_cgroup_fd: +close (cgroup_fd); +}
tools/testing/selftests/bpf/progs/udp_limit.c new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.27.0.212.ge8ba1cc988-goog