Skip to content

Commit

Permalink
selftests/bpf: Migrate selftests to bpf_map_create()
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
anakryiko authored and borkmann committed Nov 25, 2021
1 parent 99a12a3 commit 2fe256a
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 256 deletions.
13 changes: 3 additions & 10 deletions tools/testing/selftests/bpf/map_tests/array_map_batch_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ static void map_batch_verify(int *visited, __u32 max_entries, int *keys,

static void __test_map_lookup_and_update_batch(bool is_pcpu)
{
struct bpf_create_map_attr xattr = {
.name = "array_map",
.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY :
BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(int),
.value_size = sizeof(__s64),
};
int map_fd, *keys, *visited;
__u32 count, total, total_success;
const __u32 max_entries = 10;
Expand All @@ -86,10 +79,10 @@ static void __test_map_lookup_and_update_batch(bool is_pcpu)
.flags = 0,
);

xattr.max_entries = max_entries;
map_fd = bpf_create_map_xattr(&xattr);
map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_ARRAY : BPF_MAP_TYPE_ARRAY,
"array_map", sizeof(int), sizeof(__s64), max_entries, NULL);
CHECK(map_fd == -1,
"bpf_create_map_xattr()", "error:%s\n", strerror(errno));
"bpf_map_create()", "error:%s\n", strerror(errno));

value_size = sizeof(__s64);
if (is_pcpu)
Expand Down
13 changes: 3 additions & 10 deletions tools/testing/selftests/bpf/map_tests/htab_map_batch_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,15 @@ void __test_map_lookup_and_delete_batch(bool is_pcpu)
int err, step, value_size;
bool nospace_err;
void *values;
struct bpf_create_map_attr xattr = {
.name = "hash_map",
.map_type = is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH :
BPF_MAP_TYPE_HASH,
.key_size = sizeof(int),
.value_size = sizeof(int),
};
DECLARE_LIBBPF_OPTS(bpf_map_batch_opts, opts,
.elem_flags = 0,
.flags = 0,
);

xattr.max_entries = max_entries;
map_fd = bpf_create_map_xattr(&xattr);
map_fd = bpf_map_create(is_pcpu ? BPF_MAP_TYPE_PERCPU_HASH : BPF_MAP_TYPE_HASH,
"hash_map", sizeof(int), sizeof(int), max_entries, NULL);
CHECK(map_fd == -1,
"bpf_create_map_xattr()", "error:%s\n", strerror(errno));
"bpf_map_create()", "error:%s\n", strerror(errno));

value_size = is_pcpu ? sizeof(value) : sizeof(int);
keys = malloc(max_entries * sizeof(int));
Expand Down
15 changes: 5 additions & 10 deletions tools/testing/selftests/bpf/map_tests/lpm_trie_map_batch_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ static void map_batch_verify(int *visited, __u32 max_entries,

void test_lpm_trie_map_batch_ops(void)
{
struct bpf_create_map_attr xattr = {
.name = "lpm_trie_map",
.map_type = BPF_MAP_TYPE_LPM_TRIE,
.key_size = sizeof(struct test_lpm_key),
.value_size = sizeof(int),
.map_flags = BPF_F_NO_PREALLOC,
};
LIBBPF_OPTS(bpf_map_create_opts, create_opts, .map_flags = BPF_F_NO_PREALLOC);
struct test_lpm_key *keys, key;
int map_fd, *values, *visited;
__u32 step, count, total, total_success;
Expand All @@ -82,9 +76,10 @@ void test_lpm_trie_map_batch_ops(void)
.flags = 0,
);

xattr.max_entries = max_entries;
map_fd = bpf_create_map_xattr(&xattr);
CHECK(map_fd == -1, "bpf_create_map_xattr()", "error:%s\n",
map_fd = bpf_map_create(BPF_MAP_TYPE_LPM_TRIE, "lpm_trie_map",
sizeof(struct test_lpm_key), sizeof(int),
max_entries, &create_opts);
CHECK(map_fd == -1, "bpf_map_create()", "error:%s\n",
strerror(errno));

keys = malloc(max_entries * sizeof(struct test_lpm_key));
Expand Down
50 changes: 22 additions & 28 deletions tools/testing/selftests/bpf/map_tests/sk_storage_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
#include <test_btf.h>
#include <test_maps.h>

static struct bpf_create_map_attr xattr = {
.name = "sk_storage_map",
.map_type = BPF_MAP_TYPE_SK_STORAGE,
.map_flags = BPF_F_NO_PREALLOC,
.max_entries = 0,
.key_size = 4,
.value_size = 8,
static struct bpf_map_create_opts map_opts = {
.sz = sizeof(map_opts),
.btf_key_type_id = 1,
.btf_value_type_id = 3,
.btf_fd = -1,
.map_flags = BPF_F_NO_PREALLOC,
};

static unsigned int nr_sk_threads_done;
Expand Down Expand Up @@ -150,13 +146,13 @@ static int create_sk_storage_map(void)
btf_fd = load_btf();
CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
btf_fd, errno);
xattr.btf_fd = btf_fd;
map_opts.btf_fd = btf_fd;

map_fd = bpf_create_map_xattr(&xattr);
xattr.btf_fd = -1;
map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
map_opts.btf_fd = -1;
close(btf_fd);
CHECK(map_fd == -1,
"bpf_create_map_xattr()", "errno:%d\n", errno);
"bpf_map_create()", "errno:%d\n", errno);

return map_fd;
}
Expand Down Expand Up @@ -463,20 +459,20 @@ static void test_sk_storage_map_basic(void)
int cnt;
int lock;
} value = { .cnt = 0xeB9f, .lock = 0, }, lookup_value;
struct bpf_create_map_attr bad_xattr;
struct bpf_map_create_opts bad_xattr;
int btf_fd, map_fd, sk_fd, err;

btf_fd = load_btf();
CHECK(btf_fd == -1, "bpf_load_btf", "btf_fd:%d errno:%d\n",
btf_fd, errno);
xattr.btf_fd = btf_fd;
map_opts.btf_fd = btf_fd;

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

map_fd = bpf_create_map_xattr(&xattr);
CHECK(map_fd == -1, "bpf_create_map_xattr(good_xattr)",
map_fd = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &map_opts);
CHECK(map_fd == -1, "bpf_map_create(good_xattr)",
"map_fd:%d errno:%d\n", map_fd, errno);

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

memcpy(&bad_xattr, &xattr, sizeof(xattr));
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
bad_xattr.btf_key_type_id = 0;
err = bpf_create_map_xattr(&bad_xattr);
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
"err:%d errno:%d\n", err, errno);

memcpy(&bad_xattr, &xattr, sizeof(xattr));
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
bad_xattr.btf_key_type_id = 3;
err = bpf_create_map_xattr(&bad_xattr);
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
"err:%d errno:%d\n", err, errno);

memcpy(&bad_xattr, &xattr, sizeof(xattr));
bad_xattr.max_entries = 1;
err = bpf_create_map_xattr(&bad_xattr);
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 1, &map_opts);
CHECK(!err || errno != EINVAL, "bpf_map_create(bad_xattr)",
"err:%d errno:%d\n", err, errno);

memcpy(&bad_xattr, &xattr, sizeof(xattr));
memcpy(&bad_xattr, &map_opts, sizeof(map_opts));
bad_xattr.map_flags = 0;
err = bpf_create_map_xattr(&bad_xattr);
err = bpf_map_create(BPF_MAP_TYPE_SK_STORAGE, "sk_storage_map", 4, 8, 0, &bad_xattr);
CHECK(!err || errno != EINVAL, "bap_create_map_xattr(bad_xattr)",
"err:%d errno:%d\n", err, errno);

xattr.btf_fd = -1;
map_opts.btf_fd = -1;
close(btf_fd);
close(map_fd);
close(sk_fd);
Expand Down
36 changes: 19 additions & 17 deletions tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@

static void test_fail_cases(void)
{
LIBBPF_OPTS(bpf_map_create_opts, opts);
__u32 value;
int fd, err;

/* Invalid key size */
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 4, sizeof(value), 100, 0);
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid key size"))
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 4, sizeof(value), 100, NULL);
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid key size"))
close(fd);

/* Invalid value size */
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, 0, 100, 0);
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid value size 0"))
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, 0, 100, NULL);
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid value size 0"))
close(fd);

/* Invalid max entries size */
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 0, 0);
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid max entries size"))
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 0, NULL);
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid max entries size"))
close(fd);

/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
BPF_F_NO_PREALLOC);
if (!ASSERT_LT(fd, 0, "bpf_create_map bloom filter invalid flags"))
opts.map_flags = BPF_F_NO_PREALLOC;
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid flags"))
close(fd);

fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100, 0);
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter"))
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, NULL);
if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter"))
return;

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

static void test_success_cases(void)
{
LIBBPF_OPTS(bpf_map_create_opts, opts);
char value[11];
int fd, err;

/* Create a map */
fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(value), 100,
BPF_F_ZERO_SEED | BPF_F_NUMA_NODE);
if (!ASSERT_GE(fd, 0, "bpf_create_map bloom filter success case"))
opts.map_flags = BPF_F_ZERO_SEED | BPF_F_NUMA_NODE;
fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter success case"))
return;

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

/* Create a bloom filter map that will be used as the inner map */
inner_map_fd = bpf_create_map(BPF_MAP_TYPE_BLOOM_FILTER, 0, sizeof(*rand_vals),
nr_rand_vals, 0);
if (!ASSERT_GE(inner_map_fd, 0, "bpf_create_map bloom filter inner map"))
inner_map_fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(*rand_vals),
nr_rand_vals, NULL);
if (!ASSERT_GE(inner_map_fd, 0, "bpf_map_create bloom filter inner map"))
return;

for (i = 0; i < nr_rand_vals; i++) {
Expand Down
8 changes: 4 additions & 4 deletions tools/testing/selftests/bpf/prog_tests/bpf_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,12 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
* fills seq_file buffer and then the other will trigger
* overflow and needs restart.
*/
map1_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
if (CHECK(map1_fd < 0, "bpf_create_map",
map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
if (CHECK(map1_fd < 0, "bpf_map_create",
"map_creation failed: %s\n", strerror(errno)))
goto out;
map2_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
if (CHECK(map2_fd < 0, "bpf_create_map",
map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
if (CHECK(map2_fd < 0, "bpf_map_create",
"map_creation failed: %s\n", strerror(errno)))
goto free_map1;

Expand Down
51 changes: 18 additions & 33 deletions tools/testing/selftests/bpf/prog_tests/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4074,7 +4074,7 @@ static void *btf_raw_create(const struct btf_header *hdr,
static void do_test_raw(unsigned int test_num)
{
struct btf_raw_test *test = &raw_tests[test_num - 1];
struct bpf_create_map_attr create_attr = {};
LIBBPF_OPTS(bpf_map_create_opts, opts);
int map_fd = -1, btf_fd = -1;
unsigned int raw_btf_size;
struct btf_header *hdr;
Expand Down Expand Up @@ -4117,16 +4117,11 @@ static void do_test_raw(unsigned int test_num)
if (err || btf_fd < 0)
goto done;

create_attr.name = test->map_name;
create_attr.map_type = test->map_type;
create_attr.key_size = test->key_size;
create_attr.value_size = test->value_size;
create_attr.max_entries = test->max_entries;
create_attr.btf_fd = btf_fd;
create_attr.btf_key_type_id = test->key_type_id;
create_attr.btf_value_type_id = test->value_type_id;

map_fd = bpf_create_map_xattr(&create_attr);
opts.btf_fd = btf_fd;
opts.btf_key_type_id = test->key_type_id;
opts.btf_value_type_id = test->value_type_id;
map_fd = bpf_map_create(test->map_type, test->map_name,
test->key_size, test->value_size, test->max_entries, &opts);

err = ((map_fd < 0) != test->map_create_err);
CHECK(err, "map_fd:%d test->map_create_err:%u",
Expand Down Expand Up @@ -4290,7 +4285,7 @@ static int test_big_btf_info(unsigned int test_num)
static int test_btf_id(unsigned int test_num)
{
const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
struct bpf_create_map_attr create_attr = {};
LIBBPF_OPTS(bpf_map_create_opts, opts);
uint8_t *raw_btf = NULL, *user_btf[2] = {};
int btf_fd[2] = {-1, -1}, map_fd = -1;
struct bpf_map_info map_info = {};
Expand Down Expand Up @@ -4355,16 +4350,11 @@ static int test_btf_id(unsigned int test_num)
}

/* Test btf members in struct bpf_map_info */
create_attr.name = "test_btf_id";
create_attr.map_type = BPF_MAP_TYPE_ARRAY;
create_attr.key_size = sizeof(int);
create_attr.value_size = sizeof(unsigned int);
create_attr.max_entries = 4;
create_attr.btf_fd = btf_fd[0];
create_attr.btf_key_type_id = 1;
create_attr.btf_value_type_id = 2;

map_fd = bpf_create_map_xattr(&create_attr);
opts.btf_fd = btf_fd[0];
opts.btf_key_type_id = 1;
opts.btf_value_type_id = 2;
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "test_btf_id",
sizeof(int), sizeof(int), 4, &opts);
if (CHECK(map_fd < 0, "errno:%d", errno)) {
err = -1;
goto done;
Expand Down Expand Up @@ -5153,7 +5143,7 @@ static void do_test_pprint(int test_num)
{
const struct btf_raw_test *test = &pprint_test_template[test_num];
enum pprint_mapv_kind_t mapv_kind = test->mapv_kind;
struct bpf_create_map_attr create_attr = {};
LIBBPF_OPTS(bpf_map_create_opts, opts);
bool ordered_map, lossless_map, percpu_map;
int err, ret, num_cpus, rounded_value_size;
unsigned int key, nr_read_elems;
Expand Down Expand Up @@ -5189,16 +5179,11 @@ static void do_test_pprint(int test_num)
goto done;
}

create_attr.name = test->map_name;
create_attr.map_type = test->map_type;
create_attr.key_size = test->key_size;
create_attr.value_size = test->value_size;
create_attr.max_entries = test->max_entries;
create_attr.btf_fd = btf_fd;
create_attr.btf_key_type_id = test->key_type_id;
create_attr.btf_value_type_id = test->value_type_id;

map_fd = bpf_create_map_xattr(&create_attr);
opts.btf_fd = btf_fd;
opts.btf_key_type_id = test->key_type_id;
opts.btf_value_type_id = test->value_type_id;
map_fd = bpf_map_create(test->map_type, test->map_name,
test->key_size, test->value_size, test->max_entries, &opts);
if (CHECK(map_fd < 0, "errno:%d", errno)) {
err = -1;
goto done;
Expand Down
Loading

0 comments on commit 2fe256a

Please sign in to comment.