Skip to content

Commit 2fe256a

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Migrate selftests to bpf_map_create()
Conversion is straightforward for most cases. In few cases tests are using mutable map_flags and attribute structs, but bpf_map_create_opts can be used in the similar fashion, so there were no problems. Just lots of repetitive conversions. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20211124193233.3115996-5-andrii@kernel.org
1 parent 99a12a3 commit 2fe256a

21 files changed

+201
-256
lines changed

tools/testing/selftests/bpf/map_tests/array_map_batch_ops.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,6 @@ static void map_batch_verify(int *visited, __u32 max_entries, int *keys,
6868

6969
static void __test_map_lookup_and_update_batch(bool is_pcpu)
7070
{
71-
struct bpf_create_map_attr xattr = {
72-
.name = "array_map",
73-
.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY :
74-
BPF_MAP_TYPE_ARRAY,
75-
.key_size = sizeof(int),
76-
.value_size = sizeof(__s64),
77-
};
7871
int map_fd, *keys, *visited;
7972
__u32 count, total, total_success;
8073
const __u32 max_entries = 10;
@@ -86,10 +79,10 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu)
8679
.flags = 0,
8780
);
8881

89-
xattr.max_entries = max_entries;
90-
map_fd = bpf_create_map_xattr(&xattr);
82+
map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY : BPF_MAP_TYPE_ARRAY,
83+
"array_map", sizeof(int), sizeof(__s64), max_entries, NULL);
9184
CHECK(map_fd == -1,
92-
"bpf_create_map_xattr()", "error:%s\n", strerror(errno));
85+
"bpf_map_create()", "error:%s\n", strerror(errno));
9386

9487
value_size = sizeof(__s64);
9588
if (is_pcpu)

tools/testing/selftests/bpf/map_tests/htab_map_batch_ops.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,15 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu)
8383
int err, step, value_size;
8484
bool nospace_err;
8585
void *values;
86-
struct bpf_create_map_attr xattr = {
87-
.name = "hash_map",
88-
.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH :
89-
BPF_MAP_TYPE_HASH,
90-
.key_size = sizeof(int),
91-
.value_size = sizeof(int),
92-
};
9386
DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
9487
.elem_flags = 0,
9588
.flags = 0,
9689
);
9790

98-
xattr.max_entries = max_entries;
99-
map_fd = bpf_create_map_xattr(&xattr);
91+
map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH : BPF_MAP_TYPE_HASH,
92+
"hash_map", sizeof(int), sizeof(int), max_entries, NULL);
10093
CHECK(map_fd == -1,
101-
"bpf_create_map_xattr()", "error:%s\n", strerror(errno));
94+
"bpf_map_create()", "error:%s\n", strerror(errno));
10295

10396
value_size = is_pcpu ? sizeof(value) : sizeof(int);
10497
keys = malloc(max_entries * sizeof(int));

tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ static void map_batch_verify(int *visited, __u32 max_entries,
6464

6565
void test_lpm_trie_map_batch_ops(void)
6666
{
67-
struct bpf_create_map_attr xattr = {
68-
.name = "lpm_trie_map",
69-
.map_type = BPF_MAP_TYPE_LPM_TRIE,
70-
.key_size = sizeof(struct test_lpm_key),
71-
.value_size = sizeof(int),
72-
.map_flags = BPF_F_NO_PREALLOC,
73-
};
67+
LIBBPF_OPTS(bpf_map_create_opts, create_opts, .map_flags = BPF_F_NO_PREALLOC);
7468
struct test_lpm_key *keys, key;
7569
int map_fd, *values, *visited;
7670
__u32 step, count, total, total_success;
@@ -82,9 +76,10 @@ void test_lpm_trie_map_batch_ops(void)
8276
.flags = 0,
8377
);
8478

85-
xattr.max_entries = max_entries;
86-
map_fd = bpf_create_map_xattr(&xattr);
87-
CHECK(map_fd == -1, "bpf_create_map_xattr()", "error:%s\n",
79+
map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, "lpm_trie_map",
80+
sizeof(struct test_lpm_key), sizeof(int),
81+
max_entries, &create_opts);
82+
CHECK(map_fd == -1, "bpf_map_create()", "error:%s\n",
8883
strerror(errno));
8984

9085
keys = malloc(max_entries * sizeof(struct test_lpm_key));

tools/testing/selftests/bpf/map_tests/sk_storage_map.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,12 @@
1919
#include <test_btf.h>
2020
#include <test_maps.h>
2121

22-
static struct bpf_create_map_attr xattr = {
23-
.name = "sk_storage_map",
24-
.map_type = BPF_MAP_TYPE_SK_STORAGE,
25-
.map_flags = BPF_F_NO_PREALLOC,
26-
.max_entries = 0,
27-
.key_size = 4,
28-
.value_size = 8,
22+
static struct bpf_map_create_opts map_opts = {
23+
.sz = sizeof(map_opts),
2924
.btf_key_type_id = 1,
3025
.btf_value_type_id = 3,
3126
.btf_fd = -1,
27+
.map_flags = BPF_F_NO_PREALLOC,
3228
};
3329

3430
static unsigned int nr_sk_threads_done;
@@ -150,13 +146,13 @@ static int create_sk_storage_map(void)
150146
btf_fd = load_btf();
151147
CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
152148
btf_fd, errno);
153-
xattr.btf_fd = btf_fd;
149+
map_opts.btf_fd = btf_fd;
154150

155-
map_fd = bpf_create_map_xattr(&xattr);
156-
xattr.btf_fd = -1;
151+
map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
152+
map_opts.btf_fd = -1;
157153
close(btf_fd);
158154
CHECK(map_fd == -1,
159-
"bpf_create_map_xattr()", "errno:%d\n", errno);
155+
"bpf_map_create()", "errno:%d\n", errno);
160156

161157
return map_fd;
162158
}
@@ -463,20 +459,20 @@ static void test_sk_storage_map_basic(void)
463459
int cnt;
464460
int lock;
465461
} value = { .cnt = 0xeB9f, .lock = 0, }, lookup_value;
466-
struct bpf_create_map_attr bad_xattr;
462+
struct bpf_map_create_opts bad_xattr;
467463
int btf_fd, map_fd, sk_fd, err;
468464

469465
btf_fd = load_btf();
470466
CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
471467
btf_fd, errno);
472-
xattr.btf_fd = btf_fd;
468+
map_opts.btf_fd = btf_fd;
473469

474470
sk_fd = socket(AF_INET6, SOCK_STREAM, 0);
475471
CHECK(sk_fd == -1, "socket()", "sk_fd:%d errno:%d\n",
476472
sk_fd, errno);
477473

478-
map_fd = bpf_create_map_xattr(&xattr);
479-
CHECK(map_fd == -1, "bpf_create_map_xattr(good_xattr)",
474+
map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
475+
CHECK(map_fd == -1, "bpf_map_create(good_xattr)",
480476
"map_fd:%d errno:%d\n", map_fd, errno);
481477

482478
/* Add new elem */
@@ -560,31 +556,29 @@ static void test_sk_storage_map_basic(void)
560556
CHECK(!err || errno != ENOENT, "bpf_map_delete_elem()",
561557
"err:%d errno:%d\n", err, errno);
562558

563-
memcpy(&bad_xattr, &xattr, sizeof(xattr));
559+
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
564560
bad_xattr.btf_key_type_id = 0;
565-
err = bpf_create_map_xattr(&bad_xattr);
566-
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
561+
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
562+
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
567563
"err:%d errno:%d\n", err, errno);
568564

569-
memcpy(&bad_xattr, &xattr, sizeof(xattr));
565+
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
570566
bad_xattr.btf_key_type_id = 3;
571-
err = bpf_create_map_xattr(&bad_xattr);
572-
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
567+
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
568+
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
573569
"err:%d errno:%d\n", err, errno);
574570

575-
memcpy(&bad_xattr, &xattr, sizeof(xattr));
576-
bad_xattr.max_entries = 1;
577-
err = bpf_create_map_xattr(&bad_xattr);
578-
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
571+
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 1, &map_opts);
572+
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
579573
"err:%d errno:%d\n", err, errno);
580574

581-
memcpy(&bad_xattr, &xattr, sizeof(xattr));
575+
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
582576
bad_xattr.map_flags = 0;
583-
err = bpf_create_map_xattr(&bad_xattr);
577+
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
584578
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
585579
"err:%d errno:%d\n", err, errno);
586580

587-
xattr.btf_fd = -1;
581+
map_opts.btf_fd = -1;
588582
close(btf_fd);
589583
close(map_fd);
590584
close(sk_fd);

tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,33 @@
77

88
static void test_fail_cases(void)
99
{
10+
LIBBPF_OPTS(bpf_map_create_opts, opts);
1011
__u32 value;
1112
int fd, err;
1213

1314
/* Invalid key size */
14-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 4, sizeof(value), 100, 0);
15-
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid key size"))
15+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 4, sizeof(value), 100, NULL);
16+
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid key size"))
1617
close(fd);
1718

1819
/* Invalid value size */
19-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, 0, 100, 0);
20-
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid value size 0"))
20+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, 0, 100, NULL);
21+
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid value size 0"))
2122
close(fd);
2223

2324
/* Invalid max entries size */
24-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 0, 0);
25-
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid max entries size"))
25+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 0, NULL);
26+
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid max entries size"))
2627
close(fd);
2728

2829
/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
29-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
30-
BPF_F_NO_PREALLOC);
31-
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid flags"))
30+
opts.map_flags = BPF_F_NO_PREALLOC;
31+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
32+
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid flags"))
3233
close(fd);
3334

34-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100, 0);
35-
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter"))
35+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, NULL);
36+
if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter"))
3637
return;
3738

3839
/* Test invalid flags */
@@ -56,13 +57,14 @@ static void test_fail_cases(void)
5657

5758
static void test_success_cases(void)
5859
{
60+
LIBBPF_OPTS(bpf_map_create_opts, opts);
5961
char value[11];
6062
int fd, err;
6163

6264
/* Create a map */
63-
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
64-
BPF_F_ZERO_SEED | BPF_F_NUMA_NODE);
65-
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter success case"))
65+
opts.map_flags = BPF_F_ZERO_SEED | BPF_F_NUMA_NODE;
66+
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
67+
if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter success case"))
6668
return;
6769

6870
/* Add a value to the bloom filter */
@@ -100,9 +102,9 @@ static void test_inner_map(struct bloom_filter_map *skel, const __u32 *rand_vals
100102
struct bpf_link *link;
101103

102104
/* Create a bloom filter map that will be used as the inner map */
103-
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(*rand_vals),
104-
nr_rand_vals, 0);
105-
if (!ASSERT_GE(inner_map_fd, 0, "bpf_create_map bloom filter inner map"))
105+
inner_map_fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(*rand_vals),
106+
nr_rand_vals, NULL);
107+
if (!ASSERT_GE(inner_map_fd, 0, "bpf_map_create bloom filter inner map"))
106108
return;
107109

108110
for (i = 0; i < nr_rand_vals; i++) {

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
469469
* fills seq_file buffer and then the other will trigger
470470
* overflow and needs restart.
471471
*/
472-
map1_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
473-
if (CHECK(map1_fd < 0, "bpf_create_map",
472+
map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
473+
if (CHECK(map1_fd < 0, "bpf_map_create",
474474
"map_creation failed: %s\n", strerror(errno)))
475475
goto out;
476-
map2_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
477-
if (CHECK(map2_fd < 0, "bpf_create_map",
476+
map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
477+
if (CHECK(map2_fd < 0, "bpf_map_create",
478478
"map_creation failed: %s\n", strerror(errno)))
479479
goto free_map1;
480480

tools/testing/selftests/bpf/prog_tests/btf.c

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,7 +4074,7 @@ static void *btf_raw_create(const struct btf_header *hdr,
40744074
static void do_test_raw(unsigned int test_num)
40754075
{
40764076
struct btf_raw_test *test = &raw_tests[test_num - 1];
4077-
struct bpf_create_map_attr create_attr = {};
4077+
LIBBPF_OPTS(bpf_map_create_opts, opts);
40784078
int map_fd = -1, btf_fd = -1;
40794079
unsigned int raw_btf_size;
40804080
struct btf_header *hdr;
@@ -4117,16 +4117,11 @@ static void do_test_raw(unsigned int test_num)
41174117
if (err || btf_fd < 0)
41184118
goto done;
41194119

4120-
create_attr.name = test->map_name;
4121-
create_attr.map_type = test->map_type;
4122-
create_attr.key_size = test->key_size;
4123-
create_attr.value_size = test->value_size;
4124-
create_attr.max_entries = test->max_entries;
4125-
create_attr.btf_fd = btf_fd;
4126-
create_attr.btf_key_type_id = test->key_type_id;
4127-
create_attr.btf_value_type_id = test->value_type_id;
4128-
4129-
map_fd = bpf_create_map_xattr(&create_attr);
4120+
opts.btf_fd = btf_fd;
4121+
opts.btf_key_type_id = test->key_type_id;
4122+
opts.btf_value_type_id = test->value_type_id;
4123+
map_fd = bpf_map_create(test->map_type, test->map_name,
4124+
test->key_size, test->value_size, test->max_entries, &opts);
41304125

41314126
err = ((map_fd < 0) != test->map_create_err);
41324127
CHECK(err, "map_fd:%d test->map_create_err:%u",
@@ -4290,7 +4285,7 @@ static int test_big_btf_info(unsigned int test_num)
42904285
static int test_btf_id(unsigned int test_num)
42914286
{
42924287
const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
4293-
struct bpf_create_map_attr create_attr = {};
4288+
LIBBPF_OPTS(bpf_map_create_opts, opts);
42944289
uint8_t *raw_btf = NULL, *user_btf[2] = {};
42954290
int btf_fd[2] = {-1, -1}, map_fd = -1;
42964291
struct bpf_map_info map_info = {};
@@ -4355,16 +4350,11 @@ static int test_btf_id(unsigned int test_num)
43554350
}
43564351

43574352
/* Test btf members in struct bpf_map_info */
4358-
create_attr.name = "test_btf_id";
4359-
create_attr.map_type = BPF_MAP_TYPE_ARRAY;
4360-
create_attr.key_size = sizeof(int);
4361-
create_attr.value_size = sizeof(unsigned int);
4362-
create_attr.max_entries = 4;
4363-
create_attr.btf_fd = btf_fd[0];
4364-
create_attr.btf_key_type_id = 1;
4365-
create_attr.btf_value_type_id = 2;
4366-
4367-
map_fd = bpf_create_map_xattr(&create_attr);
4353+
opts.btf_fd = btf_fd[0];
4354+
opts.btf_key_type_id = 1;
4355+
opts.btf_value_type_id = 2;
4356+
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_btf_id",
4357+
sizeof(int), sizeof(int), 4, &opts);
43684358
if (CHECK(map_fd < 0, "errno:%d", errno)) {
43694359
err = -1;
43704360
goto done;
@@ -5153,7 +5143,7 @@ static void do_test_pprint(int test_num)
51535143
{
51545144
const struct btf_raw_test *test = &pprint_test_template[test_num];
51555145
enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
5156-
struct bpf_create_map_attr create_attr = {};
5146+
LIBBPF_OPTS(bpf_map_create_opts, opts);
51575147
bool ordered_map, lossless_map, percpu_map;
51585148
int err, ret, num_cpus, rounded_value_size;
51595149
unsigned int key, nr_read_elems;
@@ -5189,16 +5179,11 @@ static void do_test_pprint(int test_num)
51895179
goto done;
51905180
}
51915181

5192-
create_attr.name = test->map_name;
5193-
create_attr.map_type = test->map_type;
5194-
create_attr.key_size = test->key_size;
5195-
create_attr.value_size = test->value_size;
5196-
create_attr.max_entries = test->max_entries;
5197-
create_attr.btf_fd = btf_fd;
5198-
create_attr.btf_key_type_id = test->key_type_id;
5199-
create_attr.btf_value_type_id = test->value_type_id;
5200-
5201-
map_fd = bpf_create_map_xattr(&create_attr);
5182+
opts.btf_fd = btf_fd;
5183+
opts.btf_key_type_id = test->key_type_id;
5184+
opts.btf_value_type_id = test->value_type_id;
5185+
map_fd = bpf_map_create(test->map_type, test->map_name,
5186+
test->key_size, test->value_size, test->max_entries, &opts);
52025187
if (CHECK(map_fd < 0, "errno:%d", errno)) {
52035188
err = -1;
52045189
goto done;

0 commit comments

Comments
 (0)