xref: /linux/tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c (revision f4b369c6fe0ceaba2da2daff8c9eb415f85926dd)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Facebook */
3 #include "vmlinux.h"
4 #include "bpf_tracing_net.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include <bpf/bpf_core_read.h>
8 
9 void *sk_ptr = NULL;
10 int cookie_found = 0;
11 __u64 cookie = 0;
12 __u32 omem = 0;
13 
14 struct {
15 	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
16 	__uint(map_flags, BPF_F_NO_PREALLOC);
17 	__type(key, int);
18 	__type(value, int);
19 } sk_storage SEC(".maps");
20 
21 SEC("fexit/bpf_sk_storage_free")
22 int BPF_PROG(bpf_sk_storage_free, struct sock *sk)
23 {
24 	if (sk_ptr != sk)
25 		return 0;
26 
27 	if (sk->sk_cookie.counter != cookie)
28 		return 0;
29 
30 	cookie_found++;
31 	omem = sk->sk_omem_alloc.counter;
32 
33 	return 0;
34 }
35 
36 SEC("fentry/inet6_sock_destruct")
37 int BPF_PROG(inet6_sock_destruct, struct sock *sk)
38 {
39 	int *value;
40 
41 	if (!cookie || sk->sk_cookie.counter != cookie)
42 		return 0;
43 
44 	value = bpf_sk_storage_get(&sk_storage, sk, 0, 0);
45 	if (value && *value == 0xdeadbeef) {
46 		cookie_found++;
47 		sk_ptr = sk;
48 	}
49 
50 	return 0;
51 }
52 
53 char _license[] SEC("license") = "GPL";
54