-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Check bpf_sk_storage has uncharged sk_omem_alloc
This patch checks the sk_omem_alloc has been uncharged by bpf_sk_storage during the __sk_destruct. Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20230901231129.578493-4-martin.lau@linux.dev
- Loading branch information
Martin KaFai Lau
authored and
Daniel Borkmann
committed
Sep 6, 2023
1 parent
55d49f7
commit a96d1cf
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
tools/testing/selftests/bpf/prog_tests/sk_storage_omem_uncharge.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2023 Facebook */ | ||
#include <test_progs.h> | ||
#include <bpf/libbpf.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include "sk_storage_omem_uncharge.skel.h" | ||
|
||
void test_sk_storage_omem_uncharge(void) | ||
{ | ||
struct sk_storage_omem_uncharge *skel; | ||
int sk_fd = -1, map_fd, err, value; | ||
socklen_t optlen; | ||
|
||
skel = sk_storage_omem_uncharge__open_and_load(); | ||
if (!ASSERT_OK_PTR(skel, "skel open_and_load")) | ||
return; | ||
map_fd = bpf_map__fd(skel->maps.sk_storage); | ||
|
||
/* A standalone socket not binding to addr:port, | ||
* so nentns is not needed. | ||
*/ | ||
sk_fd = socket(AF_INET6, SOCK_STREAM, 0); | ||
if (!ASSERT_GE(sk_fd, 0, "socket")) | ||
goto done; | ||
|
||
optlen = sizeof(skel->bss->cookie); | ||
err = getsockopt(sk_fd, SOL_SOCKET, SO_COOKIE, &skel->bss->cookie, &optlen); | ||
if (!ASSERT_OK(err, "getsockopt(SO_COOKIE)")) | ||
goto done; | ||
|
||
value = 0; | ||
err = bpf_map_update_elem(map_fd, &sk_fd, &value, 0); | ||
if (!ASSERT_OK(err, "bpf_map_update_elem(value=0)")) | ||
goto done; | ||
|
||
value = 0xdeadbeef; | ||
err = bpf_map_update_elem(map_fd, &sk_fd, &value, 0); | ||
if (!ASSERT_OK(err, "bpf_map_update_elem(value=0xdeadbeef)")) | ||
goto done; | ||
|
||
err = sk_storage_omem_uncharge__attach(skel); | ||
if (!ASSERT_OK(err, "attach")) | ||
goto done; | ||
|
||
close(sk_fd); | ||
sk_fd = -1; | ||
|
||
ASSERT_EQ(skel->bss->cookie_found, 2, "cookie_found"); | ||
ASSERT_EQ(skel->bss->omem, 0, "omem"); | ||
|
||
done: | ||
sk_storage_omem_uncharge__destroy(skel); | ||
if (sk_fd != -1) | ||
close(sk_fd); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2023 Facebook */ | ||
#include "vmlinux.h" | ||
#include "bpf_tracing_net.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_core_read.h> | ||
|
||
void *local_storage_ptr = NULL; | ||
void *sk_ptr = NULL; | ||
int cookie_found = 0; | ||
__u64 cookie = 0; | ||
__u32 omem = 0; | ||
|
||
void *bpf_rdonly_cast(void *, __u32) __ksym; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_SK_STORAGE); | ||
__uint(map_flags, BPF_F_NO_PREALLOC); | ||
__type(key, int); | ||
__type(value, int); | ||
} sk_storage SEC(".maps"); | ||
|
||
SEC("fexit/bpf_local_storage_destroy") | ||
int BPF_PROG(bpf_local_storage_destroy, struct bpf_local_storage *local_storage) | ||
{ | ||
struct sock *sk; | ||
|
||
if (local_storage_ptr != local_storage) | ||
return 0; | ||
|
||
sk = bpf_rdonly_cast(sk_ptr, bpf_core_type_id_kernel(struct sock)); | ||
if (sk->sk_cookie.counter != cookie) | ||
return 0; | ||
|
||
cookie_found++; | ||
omem = sk->sk_omem_alloc.counter; | ||
local_storage_ptr = NULL; | ||
|
||
return 0; | ||
} | ||
|
||
SEC("fentry/inet6_sock_destruct") | ||
int BPF_PROG(inet6_sock_destruct, struct sock *sk) | ||
{ | ||
int *value; | ||
|
||
if (!cookie || sk->sk_cookie.counter != cookie) | ||
return 0; | ||
|
||
value = bpf_sk_storage_get(&sk_storage, sk, 0, 0); | ||
if (value && *value == 0xdeadbeef) { | ||
cookie_found++; | ||
sk_ptr = sk; | ||
local_storage_ptr = sk->sk_bpf_storage; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |