Skip to content

Commit 638a485

Browse files
Andrea Righianakryiko
authored andcommitted
selftests/bpf: Add ring_buffer__consume_n test.
Add a testcase for the ring_buffer__consume_n() API. The test produces multiple samples in a ring buffer, using a sys_getpid() fentry prog, and consumes them from user-space in batches, rather than consuming all of them greedily, like ring_buffer__consume() does. Signed-off-by: Andrea Righi <andrea.righi@canonical.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Link: https://lore.kernel.org/lkml/CAEf4BzaR4zqUpDmj44KNLdpJ=Tpa97GrvzuzVNO5nM6b7oWd1w@mail.gmail.com Link: https://lore.kernel.org/bpf/20240425140627.112728-1-andrea.righi@canonical.com
1 parent 8ec3bf5 commit 638a485

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
456456
LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \
457457
trace_printk.c trace_vprintk.c map_ptr_kern.c \
458458
core_kern.c core_kern_overflow.c test_ringbuf.c \
459-
test_ringbuf_map_key.c
459+
test_ringbuf_n.c test_ringbuf_map_key.c
460460

461461
# Generate both light skeleton and libbpf skeleton for these
462462
LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test.c \

tools/testing/selftests/bpf/prog_tests/ringbuf.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/perf_event.h>
1414
#include <linux/ring_buffer.h>
1515
#include "test_ringbuf.lskel.h"
16+
#include "test_ringbuf_n.lskel.h"
1617
#include "test_ringbuf_map_key.lskel.h"
1718

1819
#define EDONE 7777
@@ -326,6 +327,68 @@ static void ringbuf_subtest(void)
326327
test_ringbuf_lskel__destroy(skel);
327328
}
328329

330+
/*
331+
* Test ring_buffer__consume_n() by producing N_TOT_SAMPLES samples in the ring
332+
* buffer, via getpid(), and consuming them in chunks of N_SAMPLES.
333+
*/
334+
#define N_TOT_SAMPLES 32
335+
#define N_SAMPLES 4
336+
337+
/* Sample value to verify the callback validity */
338+
#define SAMPLE_VALUE 42L
339+
340+
static int process_n_sample(void *ctx, void *data, size_t len)
341+
{
342+
struct sample *s = data;
343+
344+
ASSERT_EQ(s->value, SAMPLE_VALUE, "sample_value");
345+
346+
return 0;
347+
}
348+
349+
static void ringbuf_n_subtest(void)
350+
{
351+
struct test_ringbuf_n_lskel *skel_n;
352+
int err, i;
353+
354+
skel_n = test_ringbuf_n_lskel__open();
355+
if (!ASSERT_OK_PTR(skel_n, "test_ringbuf_n_lskel__open"))
356+
return;
357+
358+
skel_n->maps.ringbuf.max_entries = getpagesize();
359+
skel_n->bss->pid = getpid();
360+
361+
err = test_ringbuf_n_lskel__load(skel_n);
362+
if (!ASSERT_OK(err, "test_ringbuf_n_lskel__load"))
363+
goto cleanup;
364+
365+
ringbuf = ring_buffer__new(skel_n->maps.ringbuf.map_fd,
366+
process_n_sample, NULL, NULL);
367+
if (!ASSERT_OK_PTR(ringbuf, "ring_buffer__new"))
368+
goto cleanup;
369+
370+
err = test_ringbuf_n_lskel__attach(skel_n);
371+
if (!ASSERT_OK(err, "test_ringbuf_n_lskel__attach"))
372+
goto cleanup_ringbuf;
373+
374+
/* Produce N_TOT_SAMPLES samples in the ring buffer by calling getpid() */
375+
skel_n->bss->value = SAMPLE_VALUE;
376+
for (i = 0; i < N_TOT_SAMPLES; i++)
377+
syscall(__NR_getpgid);
378+
379+
/* Consume all samples from the ring buffer in batches of N_SAMPLES */
380+
for (i = 0; i < N_TOT_SAMPLES; i += err) {
381+
err = ring_buffer__consume_n(ringbuf, N_SAMPLES);
382+
if (!ASSERT_EQ(err, N_SAMPLES, "rb_consume"))
383+
goto cleanup_ringbuf;
384+
}
385+
386+
cleanup_ringbuf:
387+
ring_buffer__free(ringbuf);
388+
cleanup:
389+
test_ringbuf_n_lskel__destroy(skel_n);
390+
}
391+
329392
static int process_map_key_sample(void *ctx, void *data, size_t len)
330393
{
331394
struct sample *s;
@@ -384,6 +447,8 @@ void test_ringbuf(void)
384447
{
385448
if (test__start_subtest("ringbuf"))
386449
ringbuf_subtest();
450+
if (test__start_subtest("ringbuf_n"))
451+
ringbuf_n_subtest();
387452
if (test__start_subtest("ringbuf_map_key"))
388453
ringbuf_map_key_subtest();
389454
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2024 Andrea Righi <andrea.righi@canonical.com>
3+
4+
#include <linux/bpf.h>
5+
#include <sched.h>
6+
#include <unistd.h>
7+
#include <bpf/bpf_helpers.h>
8+
#include "bpf_misc.h"
9+
10+
char _license[] SEC("license") = "GPL";
11+
12+
#define TASK_COMM_LEN 16
13+
14+
struct sample {
15+
int pid;
16+
long value;
17+
char comm[16];
18+
};
19+
20+
struct {
21+
__uint(type, BPF_MAP_TYPE_RINGBUF);
22+
} ringbuf SEC(".maps");
23+
24+
int pid = 0;
25+
long value = 0;
26+
27+
SEC("fentry/" SYS_PREFIX "sys_getpgid")
28+
int test_ringbuf_n(void *ctx)
29+
{
30+
int cur_pid = bpf_get_current_pid_tgid() >> 32;
31+
struct sample *sample;
32+
33+
if (cur_pid != pid)
34+
return 0;
35+
36+
sample = bpf_ringbuf_reserve(&ringbuf, sizeof(*sample), 0);
37+
if (!sample)
38+
return 0;
39+
40+
sample->pid = pid;
41+
sample->value = value;
42+
bpf_get_current_comm(sample->comm, sizeof(sample->comm));
43+
44+
bpf_ringbuf_submit(sample, 0);
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)