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
)
22 if (ctx
->type
!= SOCK_DGRAM
)
25 sk_storage
= bpf_sk_storage_get(&sk_map
, ctx
, 0,
26 BPF_SK_STORAGE_GET_F_CREATE
);
29 *sk_storage
= 0xdeadbeef;
31 __sync_fetch_and_add(&invocations
, 1);
34 /* BPF_CGROUP_INET_SOCK_RELEASE is _not_ called
35 * when we return an error from the BPF
41 __sync_fetch_and_add(&in_use
, 1);
45 SEC("cgroup/sock_release")
46 int sock_release(struct bpf_sock
*ctx
)
51 if (ctx
->type
!= SOCK_DGRAM
)
54 sk_storage
= bpf_sk_storage_get(&sk_map
, ctx
, 0, 0);
55 if (!sk_storage
|| *sk_storage
!= 0xdeadbeef)
58 __sync_fetch_and_add(&invocations
, 1);
59 __sync_fetch_and_add(&in_use
, -1);