WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / udp_limit.c
blob165e3c2dd9a3d3604b64163c75ce471983a0ae80
1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <sys/socket.h>
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
7 int invocations = 0, in_use = 0;
9 struct {
10 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
11 __uint(map_flags, BPF_F_NO_PREALLOC);
12 __type(key, int);
13 __type(value, int);
14 } sk_map SEC(".maps");
16 SEC("cgroup/sock_create")
17 int sock(struct bpf_sock *ctx)
19 int *sk_storage;
20 __u32 key;
22 if (ctx->type != SOCK_DGRAM)
23 return 1;
25 sk_storage = bpf_sk_storage_get(&sk_map, ctx, 0,
26 BPF_SK_STORAGE_GET_F_CREATE);
27 if (!sk_storage)
28 return 0;
29 *sk_storage = 0xdeadbeef;
31 __sync_fetch_and_add(&invocations, 1);
33 if (in_use > 0) {
34 /* BPF_CGROUP_INET_SOCK_RELEASE is _not_ called
35 * when we return an error from the BPF
36 * program!
38 return 0;
41 __sync_fetch_and_add(&in_use, 1);
42 return 1;
45 SEC("cgroup/sock_release")
46 int sock_release(struct bpf_sock *ctx)
48 int *sk_storage;
49 __u32 key;
51 if (ctx->type != SOCK_DGRAM)
52 return 1;
54 sk_storage = bpf_sk_storage_get(&sk_map, ctx, 0, 0);
55 if (!sk_storage || *sk_storage != 0xdeadbeef)
56 return 0;
58 __sync_fetch_and_add(&invocations, 1);
59 __sync_fetch_and_add(&in_use, -1);
60 return 1;