WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / test_ringbuf_multi.c
blobedf3b6953533ad7771c34375b1e3f07413dcbb06
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
7 char _license[] SEC("license") = "GPL";
9 struct sample {
10 int pid;
11 int seq;
12 long value;
13 char comm[16];
16 struct ringbuf_map {
17 __uint(type, BPF_MAP_TYPE_RINGBUF);
18 __uint(max_entries, 1 << 12);
19 } ringbuf1 SEC(".maps"),
20 ringbuf2 SEC(".maps");
22 struct {
23 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
24 __uint(max_entries, 4);
25 __type(key, int);
26 __array(values, struct ringbuf_map);
27 } ringbuf_arr SEC(".maps") = {
28 .values = {
29 [0] = &ringbuf1,
30 [2] = &ringbuf2,
34 /* inputs */
35 int pid = 0;
36 int target_ring = 0;
37 long value = 0;
39 /* outputs */
40 long total = 0;
41 long dropped = 0;
42 long skipped = 0;
44 SEC("tp/syscalls/sys_enter_getpgid")
45 int test_ringbuf(void *ctx)
47 int cur_pid = bpf_get_current_pid_tgid() >> 32;
48 struct sample *sample;
49 void *rb;
50 int zero = 0;
52 if (cur_pid != pid)
53 return 0;
55 rb = bpf_map_lookup_elem(&ringbuf_arr, &target_ring);
56 if (!rb) {
57 skipped += 1;
58 return 1;
61 sample = bpf_ringbuf_reserve(rb, sizeof(*sample), 0);
62 if (!sample) {
63 dropped += 1;
64 return 1;
67 sample->pid = pid;
68 bpf_get_current_comm(sample->comm, sizeof(sample->comm));
69 sample->value = value;
71 sample->seq = total;
72 total += 1;
74 bpf_ringbuf_submit(sample, 0);
76 return 0;