1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <linux/compiler.h>
4 #include <asm/barrier.h>
5 #include <test_progs.h>
6 #include <sys/mman.h>
7 #include <sys/epoll.h>
8 #include <time.h>
9 #include <sched.h>
10 #include <signal.h>
11 #include <pthread.h>
12 #include <sys/sysinfo.h>
13 #include <linux/perf_event.h>
14 #include <linux/ring_buffer.h>
15 #include "test_ringbuf.skel.h"
16 
17 #define EDONE 7777
18 
19 static int duration = 0;
20 
21 struct sample {
22 	int pid;
23 	int seq;
24 	long value;
25 	char comm[16];
26 };
27 
28 static int sample_cnt;
29 
30 static void atomic_inc(int *cnt)
31 {
32 	__atomic_add_fetch(cnt, 1, __ATOMIC_SEQ_CST);
33 }
34 
35 static int atomic_xchg(int *cnt, int val)
36 {
37 	return __atomic_exchange_n(cnt, val, __ATOMIC_SEQ_CST);
38 }
39 
40 static int process_sample(void *ctx, void *data, size_t len)
41 {
42 	struct sample *s = data;
43 
44 	atomic_inc(&sample_cnt);
45 
46 	switch (s->seq) {
47 	case 0:
48 		CHECK(s->value != 333, "sample1_value", "exp %ld, got %ld\n",
49 		      333L, s->value);
50 		return 0;
51 	case 1:
52 		CHECK(s->value != 777, "sample2_value", "exp %ld, got %ld\n",
53 		      777L, s->value);
54 		return -EDONE;
55 	default:
56 		/* we don't care about the rest */
57 		return 0;
58 	}
59 }
60 
61 static struct test_ringbuf *skel;
62 static struct ring_buffer *ringbuf;
63 
64 static void trigger_samples()
65 {
66 	skel->bss->dropped = 0;
67 	skel->bss->total = 0;
68 	skel->bss->discarded = 0;
69 
70 	/* trigger exactly two samples */
71 	skel->bss->value = 333;
72 	syscall(__NR_getpgid);
73 	skel->bss->value = 777;
74 	syscall(__NR_getpgid);
75 }
76 
77 static void *poll_thread(void *input)
78 {
79 	long timeout = (long)input;
80 
81 	return (void *)(long)ring_buffer__poll(ringbuf, timeout);
82 }
83 
84 void test_ringbuf(void)
85 {
86 	const size_t rec_sz = BPF_RINGBUF_HDR_SZ + sizeof(struct sample);
87 	pthread_t thread;
88 	long bg_ret = -1;
89 	int err, cnt;
90 
91 	skel = test_ringbuf__open_and_load();
92 	if (CHECK(!skel, "skel_open_load", "skeleton open&load failed\n"))
93 		return;
94 
95 	/* only trigger BPF program for current process */
96 	skel->bss->pid = getpid();
97 
98 	ringbuf = ring_buffer__new(bpf_map__fd(skel->maps.ringbuf),
99 				   process_sample, NULL, NULL);
100 	if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
101 		goto cleanup;
102 
103 	err = test_ringbuf__attach(skel);
104 	if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
105 		goto cleanup;
106 
107 	trigger_samples();
108 
109 	/* 2 submitted + 1 discarded records */
110 	CHECK(skel->bss->avail_data != 3 * rec_sz,
111 	      "err_avail_size", "exp %ld, got %ld\n",
112 	      3L * rec_sz, skel->bss->avail_data);
113 	CHECK(skel->bss->ring_size != 4096,
114 	      "err_ring_size", "exp %ld, got %ld\n",
115 	      4096L, skel->bss->ring_size);
116 	CHECK(skel->bss->cons_pos != 0,
117 	      "err_cons_pos", "exp %ld, got %ld\n",
118 	      0L, skel->bss->cons_pos);
119 	CHECK(skel->bss->prod_pos != 3 * rec_sz,
120 	      "err_prod_pos", "exp %ld, got %ld\n",
121 	      3L * rec_sz, skel->bss->prod_pos);
122 
123 	/* poll for samples */
124 	err = ring_buffer__poll(ringbuf, -1);
125 
126 	/* -EDONE is used as an indicator that we are done */
127 	if (CHECK(err != -EDONE, "err_done", "done err: %d\n", err))
128 		goto cleanup;
129 	cnt = atomic_xchg(&sample_cnt, 0);
130 	CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);
131 
132 	/* we expect extra polling to return nothing */
133 	err = ring_buffer__poll(ringbuf, 0);
134 	if (CHECK(err != 0, "extra_samples", "poll result: %d\n", err))
135 		goto cleanup;
136 	cnt = atomic_xchg(&sample_cnt, 0);
137 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
138 
139 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
140 	      0L, skel->bss->dropped);
141 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
142 	      2L, skel->bss->total);
143 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
144 	      1L, skel->bss->discarded);
145 
146 	/* now validate consumer position is updated and returned */
147 	trigger_samples();
148 	CHECK(skel->bss->cons_pos != 3 * rec_sz,
149 	      "err_cons_pos", "exp %ld, got %ld\n",
150 	      3L * rec_sz, skel->bss->cons_pos);
151 	err = ring_buffer__poll(ringbuf, -1);
152 	CHECK(err <= 0, "poll_err", "err %d\n", err);
153 	cnt = atomic_xchg(&sample_cnt, 0);
154 	CHECK(cnt != 2, "cnt", "exp %d samples, got %d\n", 2, cnt);
155 
156 	/* start poll in background w/ long timeout */
157 	err = pthread_create(&thread, NULL, poll_thread, (void *)(long)10000);
158 	if (CHECK(err, "bg_poll", "pthread_create failed: %d\n", err))
159 		goto cleanup;
160 
161 	/* turn off notifications now */
162 	skel->bss->flags = BPF_RB_NO_WAKEUP;
163 
164 	/* give background thread a bit of a time */
165 	usleep(50000);
166 	trigger_samples();
167 	/* sleeping arbitrarily is bad, but no better way to know that
168 	 * epoll_wait() **DID NOT** unblock in background thread
169 	 */
170 	usleep(50000);
171 	/* background poll should still be blocked */
172 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
173 	if (CHECK(err != EBUSY, "try_join", "err %d\n", err))
174 		goto cleanup;
175 
176 	/* BPF side did everything right */
177 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
178 	      0L, skel->bss->dropped);
179 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
180 	      2L, skel->bss->total);
181 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
182 	      1L, skel->bss->discarded);
183 	cnt = atomic_xchg(&sample_cnt, 0);
184 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
185 
186 	/* clear flags to return to "adaptive" notification mode */
187 	skel->bss->flags = 0;
188 
189 	/* produce new samples, no notification should be triggered, because
190 	 * consumer is now behind
191 	 */
192 	trigger_samples();
193 
194 	/* background poll should still be blocked */
195 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
196 	if (CHECK(err != EBUSY, "try_join", "err %d\n", err))
197 		goto cleanup;
198 
199 	/* still no samples, because consumer is behind */
200 	cnt = atomic_xchg(&sample_cnt, 0);
201 	CHECK(cnt != 0, "cnt", "exp %d samples, got %d\n", 0, cnt);
202 
203 	skel->bss->dropped = 0;
204 	skel->bss->total = 0;
205 	skel->bss->discarded = 0;
206 
207 	skel->bss->value = 333;
208 	syscall(__NR_getpgid);
209 	/* now force notifications */
210 	skel->bss->flags = BPF_RB_FORCE_WAKEUP;
211 	skel->bss->value = 777;
212 	syscall(__NR_getpgid);
213 
214 	/* now we should get a pending notification */
215 	usleep(50000);
216 	err = pthread_tryjoin_np(thread, (void **)&bg_ret);
217 	if (CHECK(err, "join_bg", "err %d\n", err))
218 		goto cleanup;
219 
220 	if (CHECK(bg_ret <= 0, "bg_ret", "epoll_wait result: %ld", bg_ret))
221 		goto cleanup;
222 
223 	/* due to timing variations, there could still be non-notified
224 	 * samples, so consume them here to collect all the samples
225 	 */
226 	err = ring_buffer__consume(ringbuf);
227 	CHECK(err < 0, "rb_consume", "failed: %d\b", err);
228 
229 	/* 3 rounds, 2 samples each */
230 	cnt = atomic_xchg(&sample_cnt, 0);
231 	CHECK(cnt != 6, "cnt", "exp %d samples, got %d\n", 6, cnt);
232 
233 	/* BPF side did everything right */
234 	CHECK(skel->bss->dropped != 0, "err_dropped", "exp %ld, got %ld\n",
235 	      0L, skel->bss->dropped);
236 	CHECK(skel->bss->total != 2, "err_total", "exp %ld, got %ld\n",
237 	      2L, skel->bss->total);
238 	CHECK(skel->bss->discarded != 1, "err_discarded", "exp %ld, got %ld\n",
239 	      1L, skel->bss->discarded);
240 
241 	test_ringbuf__detach(skel);
242 cleanup:
243 	ring_buffer__free(ringbuf);
244 	test_ringbuf__destroy(skel);
245 }
246