linux/tools/testing/selftests/bpf/progs/cgroup_storage.c
Alexis Lothoré (eBPF Foundation) 37a14cfd66 selftests/bpf: convert test_cgroup_storage to test_progs
test_cgroup_storage is currently a standalone program which is not run
when executing test_progs.

Convert it to the test_progs framework so it can be automatically executed
in CI. The conversion led to the following changes:
- converted the raw bpf program in the userspace test file into a dedicated
  test program in progs/ dir
- reduced the scope of cgroup_storage test: the content from this test
  overlaps with some other tests already present in test_progs, most
  notably netcnt and cgroup_storage_multi*. Those tests already check
  extensively local storage, per-cpu local storage, cgroups interaction,
  etc. So the new test only keep the part testing that the program return
  code (based on map content) properly leads to packet being passed or
  dropped.

Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
Link: https://lore.kernel.org/r/20240813-convert_cgroup_tests-v4-2-a33c03458cf6@bootlin.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
2024-08-14 18:10:47 -07:00

24 lines
522 B
C

// 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";