summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/cgroup_storage.c
blob: db1e4d2d328101aabe61472140982c820e1fb555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// SPDX-License-Identifier: GPL-2.0

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
	__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
	__type(key, struct bpf_cgroup_storage_key);
	__type(value, __u64);
} cgroup_storage SEC(".maps");

SEC("cgroup_skb/egress")
int bpf_prog(struct __sk_buff *skb)
{
	__u64 *counter;

	counter = bpf_get_local_storage(&cgroup_storage, 0);
	__sync_fetch_and_add(counter, 1);

	/* Drop one out of every two packets */
	return (*counter & 1);
}

char _license[] SEC("license") = "GPL";