summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/prog_tests/metadata.c
blob: 8b67dfc10f5cfe3ccbdaad15ac095b13e485aebb (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
// SPDX-License-Identifier: GPL-2.0-only

/*
 * Copyright 2020 Google LLC.
 */

#include <test_progs.h>
#include <cgroup_helpers.h>
#include <network_helpers.h>

#include "metadata_unused.skel.h"
#include "metadata_used.skel.h"

static int duration;

static int prog_holds_map(int prog_fd, int map_fd)
{
	struct bpf_prog_info prog_info = {};
	struct bpf_map_info map_info = {};
	__u32 prog_info_len;
	__u32 map_info_len;
	__u32 *map_ids;
	int nr_maps;
	int ret;
	int i;

	map_info_len = sizeof(map_info);
	ret = bpf_map_get_info_by_fd(map_fd, &map_info, &map_info_len);
	if (ret)
		return -errno;

	prog_info_len = sizeof(prog_info);
	ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
	if (ret)
		return -errno;

	map_ids = calloc(prog_info.nr_map_ids, sizeof(__u32));
	if (!map_ids)
		return -ENOMEM;

	nr_maps = prog_info.nr_map_ids;
	memset(&prog_info, 0, sizeof(prog_info));
	prog_info.nr_map_ids = nr_maps;
	prog_info.map_ids = ptr_to_u64(map_ids);
	prog_info_len = sizeof(prog_info);

	ret = bpf_prog_get_info_by_fd(prog_fd, &prog_info, &prog_info_len);
	if (ret) {
		ret = -errno;
		goto free_map_ids;
	}

	ret = -ENOENT;
	for (i = 0; i < prog_info.nr_map_ids; i++) {
		if (map_ids[i] == map_info.id) {
			ret = 0;
			break;
		}
	}

free_map_ids:
	free(map_ids);
	return ret;
}

static void test_metadata_unused(void)
{
	struct metadata_unused *obj;
	int err;

	obj = metadata_unused__open_and_load();
	if (CHECK(!obj, "skel-load", "errno %d", errno))
		return;

	err = prog_holds_map(bpf_program__fd(obj->progs.prog),
			     bpf_map__fd(obj->maps.rodata));
	if (CHECK(err, "prog-holds-rodata", "errno: %d", err))
		return;

	/* Assert that we can access the metadata in skel and the values are
	 * what we expect.
	 */
	if (CHECK(strncmp(obj->rodata->bpf_metadata_a, "foo",
			  sizeof(obj->rodata->bpf_metadata_a)),
		  "bpf_metadata_a", "expected \"foo\", value differ"))
		goto close_bpf_object;
	if (CHECK(obj->rodata->bpf_metadata_b != 1, "bpf_metadata_b",
		  "expected 1, got %d", obj->rodata->bpf_metadata_b))
		goto close_bpf_object;

	/* Assert that binding metadata map to prog again succeeds. */
	err = bpf_prog_bind_map(bpf_program__fd(obj->progs.prog),
				bpf_map__fd(obj->maps.rodata), NULL);
	CHECK(err, "rebind_map", "errno %d, expected 0", errno);

close_bpf_object:
	metadata_unused__destroy(obj);
}

static void test_metadata_used(void)
{
	struct metadata_used *obj;
	int err;

	obj = metadata_used__open_and_load();
	if (CHECK(!obj, "skel-load", "errno %d", errno))
		return;

	err = prog_holds_map(bpf_program__fd(obj->progs.prog),
			     bpf_map__fd(obj->maps.rodata));
	if (CHECK(err, "prog-holds-rodata", "errno: %d", err))
		return;

	/* Assert that we can access the metadata in skel and the values are
	 * what we expect.
	 */
	if (CHECK(strncmp(obj->rodata->bpf_metadata_a, "bar",
			  sizeof(obj->rodata->bpf_metadata_a)),
		  "metadata_a", "expected \"bar\", value differ"))
		goto close_bpf_object;
	if (CHECK(obj->rodata->bpf_metadata_b != 2, "metadata_b",
		  "expected 2, got %d", obj->rodata->bpf_metadata_b))
		goto close_bpf_object;

	/* Assert that binding metadata map to prog again succeeds. */
	err = bpf_prog_bind_map(bpf_program__fd(obj->progs.prog),
				bpf_map__fd(obj->maps.rodata), NULL);
	CHECK(err, "rebind_map", "errno %d, expected 0", errno);

close_bpf_object:
	metadata_used__destroy(obj);
}

void test_metadata(void)
{
	if (test__start_subtest("unused"))
		test_metadata_unused();

	if (test__start_subtest("used"))
		test_metadata_used();
}