summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/user_ringbuf_success.c
blob: b39093dd57159eaee7f8f05e683d086a6bb7c64c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "test_user_ringbuf.h"

char _license[] SEC("license") = "GPL";

struct {
	__uint(type, BPF_MAP_TYPE_USER_RINGBUF);
} user_ringbuf SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_RINGBUF);
} kernel_ringbuf SEC(".maps");

/* inputs */
int pid, err, val;

int read = 0;

/* Counter used for end-to-end protocol test */
__u64 kern_mutated = 0;
__u64 user_mutated = 0;
__u64 expected_user_mutated = 0;

static int
is_test_process(void)
{
	int cur_pid = bpf_get_current_pid_tgid() >> 32;

	return cur_pid == pid;
}

static long
record_sample(struct bpf_dynptr *dynptr, void *context)
{
	const struct sample *sample = NULL;
	struct sample stack_sample;
	int status;
	static int num_calls;

	if (num_calls++ % 2 == 0) {
		status = bpf_dynptr_read(&stack_sample, sizeof(stack_sample), dynptr, 0, 0);
		if (status) {
			bpf_printk("bpf_dynptr_read() failed: %d\n", status);
			err = 1;
			return 1;
		}
	} else {
		sample = bpf_dynptr_data(dynptr, 0, sizeof(*sample));
		if (!sample) {
			bpf_printk("Unexpectedly failed to get sample\n");
			err = 2;
			return 1;
		}
		stack_sample = *sample;
	}

	__sync_fetch_and_add(&read, 1);
	return 0;
}

static void
handle_sample_msg(const struct test_msg *msg)
{
	switch (msg->msg_op) {
	case TEST_MSG_OP_INC64:
		kern_mutated += msg->operand_64;
		break;
	case TEST_MSG_OP_INC32:
		kern_mutated += msg->operand_32;
		break;
	case TEST_MSG_OP_MUL64:
		kern_mutated *= msg->operand_64;
		break;
	case TEST_MSG_OP_MUL32:
		kern_mutated *= msg->operand_32;
		break;
	default:
		bpf_printk("Unrecognized op %d\n", msg->msg_op);
		err = 2;
	}
}

static long
read_protocol_msg(struct bpf_dynptr *dynptr, void *context)
{
	const struct test_msg *msg = NULL;

	msg = bpf_dynptr_data(dynptr, 0, sizeof(*msg));
	if (!msg) {
		err = 1;
		bpf_printk("Unexpectedly failed to get msg\n");
		return 0;
	}

	handle_sample_msg(msg);

	return 0;
}

static int publish_next_kern_msg(__u32 index, void *context)
{
	struct test_msg *msg = NULL;
	int operand_64 = TEST_OP_64;
	int operand_32 = TEST_OP_32;

	msg = bpf_ringbuf_reserve(&kernel_ringbuf, sizeof(*msg), 0);
	if (!msg) {
		err = 4;
		return 1;
	}

	switch (index % TEST_MSG_OP_NUM_OPS) {
	case TEST_MSG_OP_INC64:
		msg->operand_64 = operand_64;
		msg->msg_op = TEST_MSG_OP_INC64;
		expected_user_mutated += operand_64;
		break;
	case TEST_MSG_OP_INC32:
		msg->operand_32 = operand_32;
		msg->msg_op = TEST_MSG_OP_INC32;
		expected_user_mutated += operand_32;
		break;
	case TEST_MSG_OP_MUL64:
		msg->operand_64 = operand_64;
		msg->msg_op = TEST_MSG_OP_MUL64;
		expected_user_mutated *= operand_64;
		break;
	case TEST_MSG_OP_MUL32:
		msg->operand_32 = operand_32;
		msg->msg_op = TEST_MSG_OP_MUL32;
		expected_user_mutated *= operand_32;
		break;
	default:
		bpf_ringbuf_discard(msg, 0);
		err = 5;
		return 1;
	}

	bpf_ringbuf_submit(msg, 0);

	return 0;
}

static void
publish_kern_messages(void)
{
	if (expected_user_mutated != user_mutated) {
		bpf_printk("%lu != %lu\n", expected_user_mutated, user_mutated);
		err = 3;
		return;
	}

	bpf_loop(8, publish_next_kern_msg, NULL, 0);
}

SEC("fentry/" SYS_PREFIX "sys_prctl")
int test_user_ringbuf_protocol(void *ctx)
{
	long status = 0;
	struct sample *sample = NULL;
	struct bpf_dynptr ptr;

	if (!is_test_process())
		return 0;

	status = bpf_user_ringbuf_drain(&user_ringbuf, read_protocol_msg, NULL, 0);
	if (status < 0) {
		bpf_printk("Drain returned: %ld\n", status);
		err = 1;
		return 0;
	}

	publish_kern_messages();

	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getpgid")
int test_user_ringbuf(void *ctx)
{
	int status = 0;
	struct sample *sample = NULL;
	struct bpf_dynptr ptr;

	if (!is_test_process())
		return 0;

	err = bpf_user_ringbuf_drain(&user_ringbuf, record_sample, NULL, 0);

	return 0;
}

static long
do_nothing_cb(struct bpf_dynptr *dynptr, void *context)
{
	__sync_fetch_and_add(&read, 1);
	return 0;
}

SEC("fentry/" SYS_PREFIX "sys_getrlimit")
int test_user_ringbuf_epoll(void *ctx)
{
	long num_samples;

	if (!is_test_process())
		return 0;

	num_samples = bpf_user_ringbuf_drain(&user_ringbuf, do_nothing_cb, NULL, 0);
	if (num_samples <= 0)
		err = 1;

	return 0;
}