mirror of
https://github.com/torvalds/linux.git
synced 2024-11-10 14:11:52 +00:00
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
This commit is contained in:
parent
55d49f750b
commit
a96d1cfb2d
@ -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);
|
||||
}
|
@ -88,6 +88,7 @@
|
||||
#define sk_v6_rcv_saddr __sk_common.skc_v6_rcv_saddr
|
||||
#define sk_flags __sk_common.skc_flags
|
||||
#define sk_reuse __sk_common.skc_reuse
|
||||
#define sk_cookie __sk_common.skc_cookie
|
||||
|
||||
#define s6_addr32 in6_u.u6_addr32
|
||||
|
||||
|
61
tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
Normal file
61
tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
Normal file
@ -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";
|
Loading…
Reference in New Issue
Block a user