1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <sys/socket.h> 4 #include <linux/bpf.h> 5 #include <bpf/bpf_helpers.h> 6 7 int invocations = 0, in_use = 0; 8 9 SEC("cgroup/sock_create") 10 int sock(struct bpf_sock *ctx) 11 { 12 __u32 key; 13 14 if (ctx->type != SOCK_DGRAM) 15 return 1; 16 17 __sync_fetch_and_add(&invocations, 1); 18 19 if (in_use > 0) { 20 /* BPF_CGROUP_INET_SOCK_RELEASE is _not_ called 21 * when we return an error from the BPF 22 * program! 23 */ 24 return 0; 25 } 26 27 __sync_fetch_and_add(&in_use, 1); 28 return 1; 29 } 30 31 SEC("cgroup/sock_release") 32 int sock_release(struct bpf_sock *ctx) 33 { 34 __u32 key; 35 36 if (ctx->type != SOCK_DGRAM) 37 return 1; 38 39 __sync_fetch_and_add(&invocations, 1); 40 __sync_fetch_and_add(&in_use, -1); 41 return 1; 42 } 43