aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/prog_tests/libbpf_get_fd_by_id_opts.c
blob: a3f238f51d05111c9885e787ee11f4f7b8550a7d (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (C) 2022 Huawei Technologies Duesseldorf GmbH
 *
 * Author: Roberto Sassu <roberto.sassu@huawei.com>
 */

#include <test_progs.h>

#include "test_libbpf_get_fd_by_id_opts.skel.h"

void test_libbpf_get_fd_by_id_opts(void)
{
	struct test_libbpf_get_fd_by_id_opts *skel;
	struct bpf_map_info info_m = {};
	__u32 len = sizeof(info_m), value;
	int ret, zero = 0, fd = -1;
	LIBBPF_OPTS(bpf_get_fd_by_id_opts, fd_opts_rdonly,
		.open_flags = BPF_F_RDONLY,
	);

	skel = test_libbpf_get_fd_by_id_opts__open_and_load();
	if (!ASSERT_OK_PTR(skel,
			   "test_libbpf_get_fd_by_id_opts__open_and_load"))
		return;

	ret = test_libbpf_get_fd_by_id_opts__attach(skel);
	if (!ASSERT_OK(ret, "test_libbpf_get_fd_by_id_opts__attach"))
		goto close_prog;

	ret = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.data_input),
				     &info_m, &len);
	if (!ASSERT_OK(ret, "bpf_map_get_info_by_fd"))
		goto close_prog;

	fd = bpf_map_get_fd_by_id(info_m.id);
	if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id"))
		goto close_prog;

	fd = bpf_map_get_fd_by_id_opts(info_m.id, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_get_fd_by_id_opts"))
		goto close_prog;

	fd = bpf_map_get_fd_by_id_opts(info_m.id, &fd_opts_rdonly);
	if (!ASSERT_GE(fd, 0, "bpf_map_get_fd_by_id_opts"))
		goto close_prog;

	/* Map lookup should work with read-only fd. */
	ret = bpf_map_lookup_elem(fd, &zero, &value);
	if (!ASSERT_OK(ret, "bpf_map_lookup_elem"))
		goto close_prog;

	if (!ASSERT_EQ(value, 0, "map value mismatch"))
		goto close_prog;

	/* Map update should not work with read-only fd. */
	ret = bpf_map_update_elem(fd, &zero, &len, BPF_ANY);
	if (!ASSERT_LT(ret, 0, "bpf_map_update_elem"))
		goto close_prog;

	/* Map update should work with read-write fd. */
	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.data_input), &zero,
				  &len, BPF_ANY);
	if (!ASSERT_OK(ret, "bpf_map_update_elem"))
		goto close_prog;

	/* Prog get fd with opts set should not work (no kernel support). */
	ret = bpf_prog_get_fd_by_id_opts(0, &fd_opts_rdonly);
	if (!ASSERT_EQ(ret, -EINVAL, "bpf_prog_get_fd_by_id_opts"))
		goto close_prog;

	/* Link get fd with opts set should not work (no kernel support). */
	ret = bpf_link_get_fd_by_id_opts(0, &fd_opts_rdonly);
	if (!ASSERT_EQ(ret, -EINVAL, "bpf_link_get_fd_by_id_opts"))
		goto close_prog;

	/* BTF get fd with opts set should not work (no kernel support). */
	ret = bpf_btf_get_fd_by_id_opts(0, &fd_opts_rdonly);
	ASSERT_EQ(ret, -EINVAL, "bpf_btf_get_fd_by_id_opts");

close_prog:
	if (fd >= 0)
		close(fd);

	test_libbpf_get_fd_by_id_opts__destroy(skel);
}