1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_helpers.h>
7 #include "test_user_ringbuf.h"
9 char _license[] SEC("license") = "GPL";
12 __uint(type, BPF_MAP_TYPE_USER_RINGBUF);
13 } user_ringbuf SEC(".maps");
16 __uint(type, BPF_MAP_TYPE_RINGBUF);
17 } kernel_ringbuf SEC(".maps");
24 /* Counter used for end-to-end protocol test */
25 __u64 kern_mutated = 0;
26 __u64 user_mutated = 0;
27 __u64 expected_user_mutated = 0;
32 int cur_pid = bpf_get_current_pid_tgid() >> 32;
34 return cur_pid == pid;
38 record_sample(struct bpf_dynptr *dynptr, void *context)
40 const struct sample *sample = NULL;
41 struct sample stack_sample;
45 if (num_calls++ % 2 == 0) {
46 status = bpf_dynptr_read(&stack_sample, sizeof(stack_sample), dynptr, 0, 0);
48 bpf_printk("bpf_dynptr_read() failed: %d\n", status);
53 sample = bpf_dynptr_data(dynptr, 0, sizeof(*sample));
55 bpf_printk("Unexpectedly failed to get sample\n");
59 stack_sample = *sample;
62 __sync_fetch_and_add(&read, 1);
67 handle_sample_msg(const struct test_msg *msg)
69 switch (msg->msg_op) {
70 case TEST_MSG_OP_INC64:
71 kern_mutated += msg->operand_64;
73 case TEST_MSG_OP_INC32:
74 kern_mutated += msg->operand_32;
76 case TEST_MSG_OP_MUL64:
77 kern_mutated *= msg->operand_64;
79 case TEST_MSG_OP_MUL32:
80 kern_mutated *= msg->operand_32;
83 bpf_printk("Unrecognized op %d\n", msg->msg_op);
89 read_protocol_msg(struct bpf_dynptr *dynptr, void *context)
91 const struct test_msg *msg = NULL;
93 msg = bpf_dynptr_data(dynptr, 0, sizeof(*msg));
96 bpf_printk("Unexpectedly failed to get msg\n");
100 handle_sample_msg(msg);
105 static int publish_next_kern_msg(__u32 index, void *context)
107 struct test_msg *msg = NULL;
108 int operand_64 = TEST_OP_64;
109 int operand_32 = TEST_OP_32;
111 msg = bpf_ringbuf_reserve(&kernel_ringbuf, sizeof(*msg), 0);
117 switch (index % TEST_MSG_OP_NUM_OPS) {
118 case TEST_MSG_OP_INC64:
119 msg->operand_64 = operand_64;
120 msg->msg_op = TEST_MSG_OP_INC64;
121 expected_user_mutated += operand_64;
123 case TEST_MSG_OP_INC32:
124 msg->operand_32 = operand_32;
125 msg->msg_op = TEST_MSG_OP_INC32;
126 expected_user_mutated += operand_32;
128 case TEST_MSG_OP_MUL64:
129 msg->operand_64 = operand_64;
130 msg->msg_op = TEST_MSG_OP_MUL64;
131 expected_user_mutated *= operand_64;
133 case TEST_MSG_OP_MUL32:
134 msg->operand_32 = operand_32;
135 msg->msg_op = TEST_MSG_OP_MUL32;
136 expected_user_mutated *= operand_32;
139 bpf_ringbuf_discard(msg, 0);
144 bpf_ringbuf_submit(msg, 0);
150 publish_kern_messages(void)
152 if (expected_user_mutated != user_mutated) {
153 bpf_printk("%lu != %lu\n", expected_user_mutated, user_mutated);
158 bpf_loop(8, publish_next_kern_msg, NULL, 0);
161 SEC("fentry/" SYS_PREFIX "sys_prctl")
162 int test_user_ringbuf_protocol(void *ctx)
166 if (!is_test_process())
169 status = bpf_user_ringbuf_drain(&user_ringbuf, read_protocol_msg, NULL, 0);
171 bpf_printk("Drain returned: %ld\n", status);
176 publish_kern_messages();
181 SEC("fentry/" SYS_PREFIX "sys_getpgid")
182 int test_user_ringbuf(void *ctx)
184 if (!is_test_process())
187 err = bpf_user_ringbuf_drain(&user_ringbuf, record_sample, NULL, 0);
193 do_nothing_cb(struct bpf_dynptr *dynptr, void *context)
195 __sync_fetch_and_add(&read, 1);
199 SEC("fentry/" SYS_PREFIX "sys_prlimit64")
200 int test_user_ringbuf_epoll(void *ctx)
204 if (!is_test_process())
207 num_samples = bpf_user_ringbuf_drain(&user_ringbuf, do_nothing_cb, NULL, 0);
208 if (num_samples <= 0)