1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <sys/socket.h>
5 #include <bpf/bpf_helpers.h>
7 int invocations = 0, in_use = 0;
10 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
11 __uint(map_flags, BPF_F_NO_PREALLOC);
14 } sk_map SEC(".maps");
16 SEC("cgroup/sock_create")
17 int sock(struct bpf_sock *ctx)
21 if (ctx->type != SOCK_DGRAM)
24 sk_storage = bpf_sk_storage_get(&sk_map, ctx, 0,
25 BPF_SK_STORAGE_GET_F_CREATE);
28 *sk_storage = 0xdeadbeef;
30 __sync_fetch_and_add(&invocations, 1);
33 /* BPF_CGROUP_INET_SOCK_RELEASE is _not_ called
34 * when we return an error from the BPF
40 __sync_fetch_and_add(&in_use, 1);
44 SEC("cgroup/sock_release")
45 int sock_release(struct bpf_sock *ctx)
49 if (ctx->type != SOCK_DGRAM)
52 sk_storage = bpf_sk_storage_get(&sk_map, ctx, 0, 0);
53 if (!sk_storage || *sk_storage != 0xdeadbeef)
56 __sync_fetch_and_add(&invocations, 1);
57 __sync_fetch_and_add(&in_use, -1);