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
This commit is contained in:
committed by
Daniel Borkmann
parent
99a12a32fe
commit
2fe256a429
@ -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 */
|
||||
@ -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 */
|
||||
@ -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++) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -4074,7 +4074,7 @@ done:
|
||||
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;
|
||||
@ -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",
|
||||
@ -4290,7 +4285,7 @@ done:
|
||||
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 = {};
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
@ -15,22 +15,22 @@ static int prog_load_cnt(int verdict, int val)
|
||||
int cgroup_storage_fd, percpu_cgroup_storage_fd;
|
||||
|
||||
if (map_fd < 0)
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 8, 1, 0);
|
||||
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
|
||||
if (map_fd < 0) {
|
||||
printf("failed to create map '%s'\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
cgroup_storage_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_STORAGE,
|
||||
sizeof(struct bpf_cgroup_storage_key), 8, 0, 0);
|
||||
cgroup_storage_fd = bpf_map_create(BPF_MAP_TYPE_CGROUP_STORAGE, NULL,
|
||||
sizeof(struct bpf_cgroup_storage_key), 8, 0, NULL);
|
||||
if (cgroup_storage_fd < 0) {
|
||||
printf("failed to create map '%s'\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
percpu_cgroup_storage_fd = bpf_create_map(
|
||||
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE,
|
||||
sizeof(struct bpf_cgroup_storage_key), 8, 0, 0);
|
||||
percpu_cgroup_storage_fd = bpf_map_create(
|
||||
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, NULL,
|
||||
sizeof(struct bpf_cgroup_storage_key), 8, 0, NULL);
|
||||
if (percpu_cgroup_storage_fd < 0) {
|
||||
printf("failed to create map '%s'\n", strerror(errno));
|
||||
return -1;
|
||||
|
@ -241,8 +241,8 @@ void test_pinning(void)
|
||||
goto out;
|
||||
}
|
||||
|
||||
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(__u32),
|
||||
sizeof(__u64), 1, 0);
|
||||
map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(__u32),
|
||||
sizeof(__u64), 1, NULL);
|
||||
if (CHECK(map_fd < 0, "create pinmap manually", "fd %d\n", map_fd))
|
||||
goto out;
|
||||
|
||||
|
@ -62,8 +62,8 @@ void test_ringbuf_multi(void)
|
||||
if (CHECK(err != 0, "bpf_map__set_max_entries", "bpf_map__set_max_entries failed\n"))
|
||||
goto cleanup;
|
||||
|
||||
proto_fd = bpf_create_map(BPF_MAP_TYPE_RINGBUF, 0, 0, page_size, 0);
|
||||
if (CHECK(proto_fd < 0, "bpf_create_map", "bpf_create_map failed\n"))
|
||||
proto_fd = bpf_map_create(BPF_MAP_TYPE_RINGBUF, NULL, 0, 0, page_size, NULL);
|
||||
if (CHECK(proto_fd < 0, "bpf_map_create", "bpf_map_create failed\n"))
|
||||
goto cleanup;
|
||||
|
||||
err = bpf_map__set_inner_map_fd(skel->maps.ringbuf_hash, proto_fd);
|
||||
|
@ -66,29 +66,20 @@ static union sa46 {
|
||||
|
||||
static int create_maps(enum bpf_map_type inner_type)
|
||||
{
|
||||
struct bpf_create_map_attr attr = {};
|
||||
LIBBPF_OPTS(bpf_map_create_opts, opts);
|
||||
|
||||
inner_map_type = inner_type;
|
||||
|
||||
/* Creating reuseport_array */
|
||||
attr.name = "reuseport_array";
|
||||
attr.map_type = inner_type;
|
||||
attr.key_size = sizeof(__u32);
|
||||
attr.value_size = sizeof(__u32);
|
||||
attr.max_entries = REUSEPORT_ARRAY_SIZE;
|
||||
|
||||
reuseport_array = bpf_create_map_xattr(&attr);
|
||||
reuseport_array = bpf_map_create(inner_type, "reuseport_array",
|
||||
sizeof(__u32), sizeof(__u32), REUSEPORT_ARRAY_SIZE, NULL);
|
||||
RET_ERR(reuseport_array < 0, "creating reuseport_array",
|
||||
"reuseport_array:%d errno:%d\n", reuseport_array, errno);
|
||||
|
||||
/* Creating outer_map */
|
||||
attr.name = "outer_map";
|
||||
attr.map_type = BPF_MAP_TYPE_ARRAY_OF_MAPS;
|
||||
attr.key_size = sizeof(__u32);
|
||||
attr.value_size = sizeof(__u32);
|
||||
attr.max_entries = 1;
|
||||
attr.inner_map_fd = reuseport_array;
|
||||
outer_map = bpf_create_map_xattr(&attr);
|
||||
opts.inner_map_fd = reuseport_array;
|
||||
outer_map = bpf_map_create(BPF_MAP_TYPE_ARRAY_OF_MAPS, "outer_map",
|
||||
sizeof(__u32), sizeof(__u32), 1, &opts);
|
||||
RET_ERR(outer_map < 0, "creating outer_map",
|
||||
"outer_map:%d errno:%d\n", outer_map, errno);
|
||||
|
||||
|
@ -91,9 +91,9 @@ static void test_sockmap_create_update_free(enum bpf_map_type map_type)
|
||||
if (CHECK_FAIL(s < 0))
|
||||
return;
|
||||
|
||||
map = bpf_create_map(map_type, sizeof(int), sizeof(int), 1, 0);
|
||||
map = bpf_map_create(map_type, NULL, sizeof(int), sizeof(int), 1, NULL);
|
||||
if (CHECK_FAIL(map < 0)) {
|
||||
perror("bpf_create_map");
|
||||
perror("bpf_cmap_create");
|
||||
goto out;
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ static void run_tests(int family, enum bpf_map_type map_type)
|
||||
char test_name[MAX_TEST_NAME];
|
||||
int map;
|
||||
|
||||
map = bpf_create_map(map_type, sizeof(int), sizeof(int), 1, 0);
|
||||
map = bpf_map_create(map_type, NULL, sizeof(int), sizeof(int), 1, NULL);
|
||||
if (CHECK_FAIL(map < 0)) {
|
||||
perror("bpf_map_create");
|
||||
return;
|
||||
|
@ -502,8 +502,8 @@ static void test_lookup_32_bit_value(int family, int sotype, int mapfd)
|
||||
if (s < 0)
|
||||
return;
|
||||
|
||||
mapfd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP, sizeof(key),
|
||||
sizeof(value32), 1, 0);
|
||||
mapfd = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL, sizeof(key),
|
||||
sizeof(value32), 1, NULL);
|
||||
if (mapfd < 0) {
|
||||
FAIL_ERRNO("map_create");
|
||||
goto close;
|
||||
|
@ -80,7 +80,7 @@ static int fn(void)
|
||||
if (!ASSERT_OK(err, "creating " TDIR "/fs1/b"))
|
||||
goto out;
|
||||
|
||||
map = bpf_create_map(BPF_MAP_TYPE_ARRAY, 4, 4, 1, 0);
|
||||
map = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL);
|
||||
if (!ASSERT_GT(map, 0, "create_map(ARRAY)"))
|
||||
goto out;
|
||||
err = bpf_obj_pin(map, TDIR "/fs1/c");
|
||||
|
Reference in New Issue
Block a user