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 
process_sample(void * ctx,void * data,size_t len)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 
test_ringbuf_multi(void)41 void test_ringbuf_multi(void)
42 {
43 	struct test_ringbuf_multi *skel;
44 	struct ring_buffer *ringbuf = NULL;
45 	int err;
46 	int page_size = getpagesize();
47 	int proto_fd = -1;
48 
49 	skel = test_ringbuf_multi__open();
50 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
51 		return;
52 
53 	/* validate ringbuf size adjustment logic */
54 	ASSERT_EQ(bpf_map__max_entries(skel->maps.ringbuf1), page_size, "rb1_size_before");
55 	ASSERT_OK(bpf_map__set_max_entries(skel->maps.ringbuf1, page_size + 1), "rb1_resize");
56 	ASSERT_EQ(bpf_map__max_entries(skel->maps.ringbuf1), 2 * page_size, "rb1_size_after");
57 	ASSERT_OK(bpf_map__set_max_entries(skel->maps.ringbuf1, page_size), "rb1_reset");
58 	ASSERT_EQ(bpf_map__max_entries(skel->maps.ringbuf1), page_size, "rb1_size_final");
59 
60 	proto_fd = bpf_map_create(BPF_MAP_TYPE_RINGBUF, NULL, 0, 0, page_size, NULL);
61 	if (CHECK(proto_fd < 0, "bpf_map_create", "bpf_map_create failed\n"))
62 		goto cleanup;
63 
64 	err = bpf_map__set_inner_map_fd(skel->maps.ringbuf_hash, proto_fd);
65 	if (CHECK(err != 0, "bpf_map__set_inner_map_fd", "bpf_map__set_inner_map_fd failed\n"))
66 		goto cleanup;
67 
68 	err = test_ringbuf_multi__load(skel);
69 	if (CHECK(err != 0, "skel_load", "skeleton load failed\n"))
70 		goto cleanup;
71 
72 	close(proto_fd);
73 	proto_fd = -1;
74 
75 	/* make sure we can't resize ringbuf after object load */
76 	if (!ASSERT_ERR(bpf_map__set_max_entries(skel->maps.ringbuf1, 3 * page_size), "rb1_resize_after_load"))
77 		goto cleanup;
78 
79 	/* only trigger BPF program for current process */
80 	skel->bss->pid = getpid();
81 
82 	ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf1),
83 				   process_sample, (void *)(long)1, NULL);
84 	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
85 		goto cleanup;
86 
87 	err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
88 			      process_sample, (void *)(long)2);
89 	if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
90 		goto cleanup;
91 
92 	err = test_ringbuf_multi__attach(skel);
93 	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
94 		goto cleanup;
95 
96 	/* trigger few samples, some will be skipped */
97 	skel->bss->target_ring = 0;
98 	skel->bss->value = 333;
99 	syscall(__NR_getpgid);
100 
101 	/* skipped, no ringbuf in slot 1 */
102 	skel->bss->target_ring = 1;
103 	skel->bss->value = 555;
104 	syscall(__NR_getpgid);
105 
106 	skel->bss->target_ring = 2;
107 	skel->bss->value = 777;
108 	syscall(__NR_getpgid);
109 
110 	/* poll for samples, should get 2 ringbufs back */
111 	err = ring_buffer__poll(ringbuf, -1);
112 	if (CHECK(err != 2, "poll_res", "expected 2 records, got %d\n", err))
113 		goto cleanup;
114 
115 	/* expect extra polling to return nothing */
116 	err = ring_buffer__poll(ringbuf, 0);
117 	if (CHECK(err < 0, "extra_samples", "poll result: %d\n", err))
118 		goto cleanup;
119 
120 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
121 	      0L, skel->bss->dropped);
122 	CHECK(skel->bss->skipped != 1, "err_skipped", "exp %ld, got %ld\n",
123 	      1L, skel->bss->skipped);
124 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
125 	      2L, skel->bss->total);
126 
127 cleanup:
128 	if (proto_fd >= 0)
129 		close(proto_fd);
130 	ring_buffer__free(ringbuf);
131 	test_ringbuf_multi__destroy(skel);
132 }
133