1 // SPDX-License-Identifier: GPL-2.0
3 #include <test_progs.h>
8 #include "../test_btf.h"
10 static inline int new_map(void)
12 const char *name = NULL;
13 __u32 max_entries = 1;
17 return bpf_map_create(BPF_MAP_TYPE_ARRAY, name,
22 static int new_btf(void)
25 struct btf_header btf_hdr;
31 .version = BTF_VERSION,
32 .hdr_len = sizeof(struct btf_header),
33 .type_len = sizeof(raw_btf.types),
34 .str_off = offsetof(struct btf_blob, str) - offsetof(struct btf_blob, types),
35 .str_len = sizeof(raw_btf.str),
39 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8), /* [1] */
41 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
45 return bpf_btf_load(&raw_btf, sizeof(raw_btf), NULL);
48 #define Close(FD) do { \
55 static bool map_exists(__u32 id)
59 fd = bpf_map_get_fd_by_id(id);
67 static bool btf_exists(__u32 id)
71 fd = bpf_btf_get_fd_by_id(id);
79 static inline int bpf_prog_get_map_ids(int prog_fd, __u32 *nr_map_ids, __u32 *map_ids)
81 __u32 len = sizeof(struct bpf_prog_info);
82 struct bpf_prog_info info;
85 memset(&info, 0, len);
86 info.nr_map_ids = *nr_map_ids,
87 info.map_ids = ptr_to_u64(map_ids),
89 err = bpf_prog_get_info_by_fd(prog_fd, &info, &len);
90 if (!ASSERT_OK(err, "bpf_prog_get_info_by_fd"))
93 *nr_map_ids = info.nr_map_ids;
98 static int __load_test_prog(int map_fd, const int *fd_array, int fd_array_cnt)
100 /* A trivial program which uses one map */
101 struct bpf_insn insns[] = {
102 BPF_LD_MAP_FD(BPF_REG_1, map_fd),
103 BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
104 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
105 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
106 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
107 BPF_MOV64_IMM(BPF_REG_0, 0),
110 LIBBPF_OPTS(bpf_prog_load_opts, opts);
112 opts.fd_array = fd_array;
113 opts.fd_array_cnt = fd_array_cnt;
115 return bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, ARRAY_SIZE(insns), &opts);
118 static int load_test_prog(const int *fd_array, int fd_array_cnt)
124 if (!ASSERT_GE(map_fd, 0, "new_map"))
127 ret = __load_test_prog(map_fd, fd_array, fd_array_cnt);
132 static bool check_expected_map_ids(int prog_fd, int expected, __u32 *map_ids, __u32 *nr_map_ids)
136 err = bpf_prog_get_map_ids(prog_fd, nr_map_ids, map_ids);
137 if (!ASSERT_OK(err, "bpf_prog_get_map_ids"))
139 if (!ASSERT_EQ(*nr_map_ids, expected, "unexpected nr_map_ids"))
146 * Load a program, which uses one map. No fd_array maps are present.
147 * On return only one map is expected to be bound to prog.
149 static void check_fd_array_cnt__no_fd_array(void)
155 prog_fd = load_test_prog(NULL, 0);
156 if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
158 nr_map_ids = ARRAY_SIZE(map_ids);
159 check_expected_map_ids(prog_fd, 1, map_ids, &nr_map_ids);
164 * Load a program, which uses one map, and pass two extra, non-equal, maps in
165 * fd_array with fd_array_cnt=2. On return three maps are expected to be bound
168 static void check_fd_array_cnt__fd_array_ok(void)
170 int extra_fds[2] = { -1, -1 };
175 extra_fds[0] = new_map();
176 if (!ASSERT_GE(extra_fds[0], 0, "new_map"))
178 extra_fds[1] = new_map();
179 if (!ASSERT_GE(extra_fds[1], 0, "new_map"))
181 prog_fd = load_test_prog(extra_fds, 2);
182 if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
184 nr_map_ids = ARRAY_SIZE(map_ids);
185 if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids))
188 /* maps should still exist when original file descriptors are closed */
191 if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map_ids[0] should exist"))
193 if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map_ids[1] should exist"))
196 /* some fds might be invalid, so ignore return codes */
204 * Load a program with a few extra maps duplicated in the fd_array.
205 * After the load maps should only be referenced once.
207 static void check_fd_array_cnt__duplicated_maps(void)
209 int extra_fds[4] = { -1, -1, -1, -1 };
214 extra_fds[0] = extra_fds[2] = new_map();
215 if (!ASSERT_GE(extra_fds[0], 0, "new_map"))
217 extra_fds[1] = extra_fds[3] = new_map();
218 if (!ASSERT_GE(extra_fds[1], 0, "new_map"))
220 prog_fd = load_test_prog(extra_fds, 4);
221 if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
223 nr_map_ids = ARRAY_SIZE(map_ids);
224 if (!check_expected_map_ids(prog_fd, 3, map_ids, &nr_map_ids))
227 /* maps should still exist when original file descriptors are closed */
230 if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist"))
232 if (!ASSERT_EQ(map_exists(map_ids[1]), true, "map should exist"))
235 /* some fds might be invalid, so ignore return codes */
243 * Check that if maps which are referenced by a program are
244 * passed in fd_array, then they will be referenced only once
246 static void check_fd_array_cnt__referenced_maps_in_fd_array(void)
248 int extra_fds[1] = { -1 };
253 extra_fds[0] = new_map();
254 if (!ASSERT_GE(extra_fds[0], 0, "new_map"))
256 prog_fd = __load_test_prog(extra_fds[0], extra_fds, 1);
257 if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
259 nr_map_ids = ARRAY_SIZE(map_ids);
260 if (!check_expected_map_ids(prog_fd, 1, map_ids, &nr_map_ids))
263 /* map should still exist when original file descriptor is closed */
265 if (!ASSERT_EQ(map_exists(map_ids[0]), true, "map should exist"))
268 /* some fds might be invalid, so ignore return codes */
274 static int get_btf_id_by_fd(int btf_fd, __u32 *id)
276 struct bpf_btf_info info;
277 __u32 info_len = sizeof(info);
280 memset(&info, 0, info_len);
281 err = bpf_btf_get_info_by_fd(btf_fd, &info, &info_len);
290 * Check that fd_array operates properly for btfs. Namely, to check that
291 * passing a btf fd in fd_array increases its reference count, do the
293 * 1) Create a new btf, it's referenced only by a file descriptor, so refcnt=1
294 * 2) Load a BPF prog with fd_array[0] = btf_fd; now btf's refcnt=2
295 * 3) Close the btf_fd, now refcnt=1
296 * Wait and check that BTF stil exists.
298 static void check_fd_array_cnt__referenced_btfs(void)
300 int extra_fds[1] = { -1 };
306 extra_fds[0] = new_btf();
307 if (!ASSERT_GE(extra_fds[0], 0, "new_btf"))
309 prog_fd = load_test_prog(extra_fds, 1);
310 if (!ASSERT_GE(prog_fd, 0, "BPF_PROG_LOAD"))
313 /* btf should still exist when original file descriptor is closed */
314 err = get_btf_id_by_fd(extra_fds[0], &btf_id);
315 if (!ASSERT_GE(err, 0, "get_btf_id_by_fd"))
320 if (!ASSERT_GE(kern_sync_rcu(), 0, "kern_sync_rcu 1"))
323 if (!ASSERT_EQ(btf_exists(btf_id), true, "btf should exist"))
328 /* The program is freed by a workqueue, so no reliable
329 * way to sync, so just wait a bit (max ~1 second). */
330 for (tries = 100; tries >= 0; tries--) {
333 if (!btf_exists(btf_id))
339 PRINT_FAIL("btf should have been freed");
342 /* some fds might be invalid, so ignore return codes */
349 * Test that a program with trash in fd_array can't be loaded:
350 * only map and BTF file descriptors should be accepted.
352 static void check_fd_array_cnt__fd_array_with_trash(void)
354 int extra_fds[3] = { -1, -1, -1 };
357 extra_fds[0] = new_map();
358 if (!ASSERT_GE(extra_fds[0], 0, "new_map"))
360 extra_fds[1] = new_btf();
361 if (!ASSERT_GE(extra_fds[1], 0, "new_btf"))
364 /* trash 1: not a file descriptor */
365 extra_fds[2] = 0xbeef;
366 prog_fd = load_test_prog(extra_fds, 3);
367 if (!ASSERT_EQ(prog_fd, -EBADF, "prog should have been rejected with -EBADF"))
370 /* trash 2: not a map or btf */
371 extra_fds[2] = socket(AF_INET, SOCK_STREAM, 0);
372 if (!ASSERT_GE(extra_fds[2], 0, "socket"))
375 prog_fd = load_test_prog(extra_fds, 3);
376 if (!ASSERT_EQ(prog_fd, -EINVAL, "prog should have been rejected with -EINVAL"))
379 /* Validate that the prog is ok if trash is removed */
381 extra_fds[2] = new_btf();
382 if (!ASSERT_GE(extra_fds[2], 0, "new_btf"))
385 prog_fd = load_test_prog(extra_fds, 3);
386 if (!ASSERT_GE(prog_fd, 0, "prog should have been loaded"))
389 /* some fds might be invalid, so ignore return codes */
397 * Test that a program with too big fd_array can't be loaded.
399 static void check_fd_array_cnt__fd_array_too_big(void)
405 for (i = 0; i < 65; i++) {
406 extra_fds[i] = new_map();
407 if (!ASSERT_GE(extra_fds[i], 0, "new_map"))
411 prog_fd = load_test_prog(extra_fds, 65);
412 ASSERT_EQ(prog_fd, -E2BIG, "prog should have been rejected with -E2BIG");
416 Close(extra_fds[--i]);
419 void test_fd_array_cnt(void)
421 if (test__start_subtest("no-fd-array"))
422 check_fd_array_cnt__no_fd_array();
424 if (test__start_subtest("fd-array-ok"))
425 check_fd_array_cnt__fd_array_ok();
427 if (test__start_subtest("fd-array-dup-input"))
428 check_fd_array_cnt__duplicated_maps();
430 if (test__start_subtest("fd-array-ref-maps-in-array"))
431 check_fd_array_cnt__referenced_maps_in_fd_array();
433 if (test__start_subtest("fd-array-ref-btfs"))
434 check_fd_array_cnt__referenced_btfs();
436 if (test__start_subtest("fd-array-trash-input"))
437 check_fd_array_cnt__fd_array_with_trash();
439 if (test__start_subtest("fd-array-2big"))
440 check_fd_array_cnt__fd_array_too_big();