1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <test_progs.h>
4 #include <sys/epoll.h>
5 #include "test_ringbuf_multi.skel.h"
6 
7 static int duration = 0;
8 
9 struct sample {
10 	int pid;
11 	int seq;
12 	long value;
13 	char comm[16];
14 };
15 
16 static int process_sample(void *ctx, void *data, size_t len)
17 {
18 	int ring = (unsigned long)ctx;
19 	struct sample *s = data;
20 
21 	switch (s->seq) {
22 	case 0:
23 		CHECK(ring != 1, "sample1_ring", "exp %d, got %d\n", 1, ring);
24 		CHECK(s->value != 333, "sample1_value", "exp %ld, got %ld\n",
25 		      333L, s->value);
26 		break;
27 	case 1:
28 		CHECK(ring != 2, "sample2_ring", "exp %d, got %d\n", 2, ring);
29 		CHECK(s->value != 777, "sample2_value", "exp %ld, got %ld\n",
30 		      777L, s->value);
31 		break;
32 	default:
33 		CHECK(true, "extra_sample", "unexpected sample seq %d, val %ld\n",
34 		      s->seq, s->value);
35 		return -1;
36 	}
37 
38 	return 0;
39 }
40 
41 void test_ringbuf_multi(void)
42 {
43 	struct test_ringbuf_multi *skel;
44 	struct ring_buffer *ringbuf;
45 	int err;
46 
47 	skel = test_ringbuf_multi__open_and_load();
48 	if (CHECK(!skel, "skel_open_load", "skeleton open&load failed\n"))
49 		return;
50 
51 	/* only trigger BPF program for current process */
52 	skel->bss->pid = getpid();
53 
54 	ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf1),
55 				   process_sample, (void *)(long)1, NULL);
56 	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
57 		goto cleanup;
58 
59 	err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
60 			      process_sample, (void *)(long)2);
61 	if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
62 		goto cleanup;
63 
64 	err = test_ringbuf_multi__attach(skel);
65 	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
66 		goto cleanup;
67 
68 	/* trigger few samples, some will be skipped */
69 	skel->bss->target_ring = 0;
70 	skel->bss->value = 333;
71 	syscall(__NR_getpgid);
72 
73 	/* skipped, no ringbuf in slot 1 */
74 	skel->bss->target_ring = 1;
75 	skel->bss->value = 555;
76 	syscall(__NR_getpgid);
77 
78 	skel->bss->target_ring = 2;
79 	skel->bss->value = 777;
80 	syscall(__NR_getpgid);
81 
82 	/* poll for samples, should get 2 ringbufs back */
83 	err = ring_buffer__poll(ringbuf, -1);
84 	if (CHECK(err != 2, "poll_res", "expected 2 records, got %d\n", err))
85 		goto cleanup;
86 
87 	/* expect extra polling to return nothing */
88 	err = ring_buffer__poll(ringbuf, 0);
89 	if (CHECK(err < 0, "extra_samples", "poll result: %d\n", err))
90 		goto cleanup;
91 
92 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
93 	      0L, skel->bss->dropped);
94 	CHECK(skel->bss->skipped != 1, "err_skipped", "exp %ld, got %ld\n",
95 	      1L, skel->bss->skipped);
96 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
97 	      2L, skel->bss->total);
98 
99 cleanup:
100 	ring_buffer__free(ringbuf);
101 	test_ringbuf_multi__destroy(skel);
102 }
103