summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/rhash.c
blob: aea4de8dc7811f3f4068ff8a52a3af4eb5ebc483 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */

#include <vmlinux.h>
#include <stdbool.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_misc.h"

#define ENOENT 2
#define EEXIST 17

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

int err;

struct elem {
	char arr[128];
	int val;
};

struct special_elem {
	struct task_struct __kptr *task;
	int val;
};

struct {
	__uint(type, BPF_MAP_TYPE_RHASH);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__uint(max_entries, 128);
	__type(key, int);
	__type(value, struct elem);
} rhmap SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_RHASH);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__uint(max_entries, 1);
	__type(key, int);
	__type(value, struct special_elem);
} special_fields SEC(".maps");

extern struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym;
extern void bpf_task_release(struct task_struct *p) __ksym;

SEC("syscall")
int test_rhash_lookup_update(void *ctx)
{
	int key = 5;
	struct elem empty = {.val = 3, .arr = {0}};
	struct elem *e;

	err = 1;
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e)
		return 1;

	err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
	if (err)
		return 1;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != empty.val) {
		err = 2;
		return 2;
	}

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_delete(void *ctx)
{
	int key = 6;
	struct elem empty = {.val = 4, .arr = {0}};
	struct elem *e;

	err = 1;
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e)
		return 1;

	err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
	if (err)
		return 2;

	err = bpf_map_delete_elem(&rhmap, &key);
	if (err)
		return 3;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e) {
		err = 4;
		return 4;
	}

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_elements(void *ctx)
{
	int key = 0;
	struct elem empty = {.val = 4, .arr = {0}};
	struct elem *e;
	int i;

	err = 1;

	for (i = 0; i < 128; ++i) {
		key = i;
		e = bpf_map_lookup_elem(&rhmap, &key);
		if (e)
			return 1;

		empty.val = key;
		err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
		if (err)
			return 2;

		e = bpf_map_lookup_elem(&rhmap, &key);
		if (!e || e->val != key) {
			err = 4;
			return 4;
		}
	}

	for (i = 0; i < 128; ++i) {
		key = i;
		err = bpf_map_delete_elem(&rhmap, &key);
		if (err)
			return 3;

		e = bpf_map_lookup_elem(&rhmap, &key);
		if (e) {
			err = 5;
			return 5;
		}
	}

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_exist(void *ctx)
{
	int key = 10;
	struct elem val1 = {.val = 100, .arr = {0}};
	struct elem val2 = {.val = 200, .arr = {0}};
	struct elem *e;
	int ret;

	err = 1;

	/* BPF_EXIST on non-existent key should fail with -ENOENT */
	ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_EXIST);
	if (ret != -ENOENT)
		return 1;

	/* Insert element first */
	ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_NOEXIST);
	if (ret)
		return 2;

	/* Verify initial value */
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != 100)
		return 3;

	/* BPF_EXIST on existing key should succeed and update value */
	ret = bpf_map_update_elem(&rhmap, &key, &val2, BPF_EXIST);
	if (ret)
		return 4;

	/* Verify value was updated */
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != 200)
		return 5;

	/* Cleanup */
	bpf_map_delete_elem(&rhmap, &key);
	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_any(void *ctx)
{
	int key = 11;
	struct elem val1 = {.val = 111, .arr = {0}};
	struct elem val2 = {.val = 222, .arr = {0}};
	struct elem *e;
	int ret;

	err = 1;

	/* BPF_ANY on non-existent key should insert */
	ret = bpf_map_update_elem(&rhmap, &key, &val1, BPF_ANY);
	if (ret)
		return 1;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != 111)
		return 2;

	/* BPF_ANY on existing key should update */
	ret = bpf_map_update_elem(&rhmap, &key, &val2, BPF_ANY);
	if (ret)
		return 3;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != 222)
		return 4;

	/* Cleanup */
	bpf_map_delete_elem(&rhmap, &key);
	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_noexist_duplicate(void *ctx)
{
	int key = 12;
	struct elem val = {.val = 600, .arr = {0}};
	int ret;

	err = 1;

	/* Insert element */
	ret = bpf_map_update_elem(&rhmap, &key, &val, BPF_NOEXIST);
	if (ret)
		return 1;

	/* Try to insert again with BPF_NOEXIST - should fail with -EEXIST */
	ret = bpf_map_update_elem(&rhmap, &key, &val, BPF_NOEXIST);
	if (ret != -EEXIST)
		return 2;

	/* Cleanup */
	bpf_map_delete_elem(&rhmap, &key);
	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_delete_nonexistent(void *ctx)
{
	int key = 99999;
	int ret;

	err = 1;

	/* Delete non-existent key should return -ENOENT */
	ret = bpf_map_delete_elem(&rhmap, &key);
	if (ret != -ENOENT)
		return 1;

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_kptr_update(void *ctx)
{
	struct special_elem val1 = { .val = 1 };
	struct special_elem val2 = { .val = 2 };
	struct task_struct *task, *old;
	struct special_elem *elem;
	int key = 0;

	err = 1;
	if (bpf_map_update_elem(&special_fields, &key, &val1, BPF_NOEXIST))
		return 1;

	err = 2;
	elem = bpf_map_lookup_elem(&special_fields, &key);
	if (!elem)
		return 2;

	err = 3;
	task = bpf_task_acquire(bpf_get_current_task_btf());
	if (!task)
		return 3;

	err = 4;
	old = bpf_kptr_xchg(&elem->task, task);
	if (old) {
		bpf_task_release(old);
		return 4;
	}

	err = 5;
	if (bpf_map_update_elem(&special_fields, &key, &val2, BPF_EXIST))
		return 5;

	err = 6;
	elem = bpf_map_lookup_elem(&special_fields, &key);
	if (!elem || elem->val != 2)
		return 6;

	err = 7;
	old = bpf_kptr_xchg(&elem->task, NULL);
	if (!old)
		return 7;
	bpf_task_release(old);

	err = 8;
	if (bpf_map_delete_elem(&special_fields, &key))
		return 8;

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_kptr_delete(void *ctx)
{
	struct special_elem val = {};
	struct task_struct *task, *old;
	struct special_elem *elem;
	int key = 0;

	err = 1;
	if (bpf_map_update_elem(&special_fields, &key, &val, BPF_NOEXIST))
		return 1;

	err = 2;
	elem = bpf_map_lookup_elem(&special_fields, &key);
	if (!elem)
		return 2;

	err = 3;
	task = bpf_task_acquire(bpf_get_current_task_btf());
	if (!task)
		return 3;

	err = 4;
	old = bpf_kptr_xchg(&elem->task, task);
	if (old) {
		bpf_task_release(old);
		return 4;
	}

	err = 5;
	if (bpf_map_delete_elem(&special_fields, &key))
		return 5;

	err = 6;
	old = bpf_kptr_xchg(&elem->task, NULL);
	if (!old)
		return 6;
	bpf_task_release(old);

	err = 0;
	return 0;
}