bpf: adding tests for map_in_map helpber in libbpf
adding test/example of bpf_map__set_inner_map_fd usage Signed-off-by: Nikita V. Shirokov <tehnerd@tehnerd.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
parent
addb9fc90f
commit
b1957c92eb
|
@ -38,7 +38,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test
|
||||||
test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \
|
test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \
|
||||||
get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \
|
get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \
|
||||||
test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \
|
test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \
|
||||||
test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o
|
test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \
|
||||||
|
test_map_in_map.o
|
||||||
|
|
||||||
# Order correspond to 'make run_tests' order
|
# Order correspond to 'make run_tests' order
|
||||||
TEST_PROGS := test_kmod.sh \
|
TEST_PROGS := test_kmod.sh \
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
// SPDX-License-Identifier: GPL-2.0
|
||||||
|
/* Copyright (c) 2018 Facebook */
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <linux/bpf.h>
|
||||||
|
#include <linux/types.h>
|
||||||
|
#include "bpf_helpers.h"
|
||||||
|
|
||||||
|
struct bpf_map_def SEC("maps") mim_array = {
|
||||||
|
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
|
||||||
|
.key_size = sizeof(int),
|
||||||
|
/* must be sizeof(__u32) for map in map */
|
||||||
|
.value_size = sizeof(__u32),
|
||||||
|
.max_entries = 1,
|
||||||
|
.map_flags = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct bpf_map_def SEC("maps") mim_hash = {
|
||||||
|
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
|
||||||
|
.key_size = sizeof(int),
|
||||||
|
/* must be sizeof(__u32) for map in map */
|
||||||
|
.value_size = sizeof(__u32),
|
||||||
|
.max_entries = 1,
|
||||||
|
.map_flags = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
SEC("xdp_mimtest")
|
||||||
|
int xdp_mimtest0(struct xdp_md *ctx)
|
||||||
|
{
|
||||||
|
int value = 123;
|
||||||
|
int key = 0;
|
||||||
|
void *map;
|
||||||
|
|
||||||
|
map = bpf_map_lookup_elem(&mim_array, &key);
|
||||||
|
if (!map)
|
||||||
|
return XDP_DROP;
|
||||||
|
|
||||||
|
bpf_map_update_elem(map, &key, &value, 0);
|
||||||
|
|
||||||
|
map = bpf_map_lookup_elem(&mim_hash, &key);
|
||||||
|
if (!map)
|
||||||
|
return XDP_DROP;
|
||||||
|
|
||||||
|
bpf_map_update_elem(map, &key, &value, 0);
|
||||||
|
|
||||||
|
return XDP_PASS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _version SEC("version") = 1;
|
||||||
|
char _license[] SEC("license") = "GPL";
|
|
@ -1125,6 +1125,94 @@ out_sockmap:
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define MAPINMAP_PROG "./test_map_in_map.o"
|
||||||
|
static void test_map_in_map(void)
|
||||||
|
{
|
||||||
|
struct bpf_program *prog;
|
||||||
|
struct bpf_object *obj;
|
||||||
|
struct bpf_map *map;
|
||||||
|
int mim_fd, fd, err;
|
||||||
|
int pos = 0;
|
||||||
|
|
||||||
|
obj = bpf_object__open(MAPINMAP_PROG);
|
||||||
|
|
||||||
|
fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int),
|
||||||
|
2, 0);
|
||||||
|
if (fd < 0) {
|
||||||
|
printf("Failed to create hashmap '%s'!\n", strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
map = bpf_object__find_map_by_name(obj, "mim_array");
|
||||||
|
if (IS_ERR(map)) {
|
||||||
|
printf("Failed to load array of maps from test prog\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
err = bpf_map__set_inner_map_fd(map, fd);
|
||||||
|
if (err) {
|
||||||
|
printf("Failed to set inner_map_fd for array of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
map = bpf_object__find_map_by_name(obj, "mim_hash");
|
||||||
|
if (IS_ERR(map)) {
|
||||||
|
printf("Failed to load hash of maps from test prog\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
err = bpf_map__set_inner_map_fd(map, fd);
|
||||||
|
if (err) {
|
||||||
|
printf("Failed to set inner_map_fd for hash of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
bpf_object__for_each_program(prog, obj) {
|
||||||
|
bpf_program__set_xdp(prog);
|
||||||
|
}
|
||||||
|
bpf_object__load(obj);
|
||||||
|
|
||||||
|
map = bpf_object__find_map_by_name(obj, "mim_array");
|
||||||
|
if (IS_ERR(map)) {
|
||||||
|
printf("Failed to load array of maps from test prog\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
mim_fd = bpf_map__fd(map);
|
||||||
|
if (mim_fd < 0) {
|
||||||
|
printf("Failed to get descriptor for array of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
|
||||||
|
if (err) {
|
||||||
|
printf("Failed to update array of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
map = bpf_object__find_map_by_name(obj, "mim_hash");
|
||||||
|
if (IS_ERR(map)) {
|
||||||
|
printf("Failed to load hash of maps from test prog\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
mim_fd = bpf_map__fd(map);
|
||||||
|
if (mim_fd < 0) {
|
||||||
|
printf("Failed to get descriptor for hash of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bpf_map_update_elem(mim_fd, &pos, &fd, 0);
|
||||||
|
if (err) {
|
||||||
|
printf("Failed to update hash of maps\n");
|
||||||
|
goto out_map_in_map;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
bpf_object__close(obj);
|
||||||
|
return;
|
||||||
|
|
||||||
|
out_map_in_map:
|
||||||
|
close(fd);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
#define MAP_SIZE (32 * 1024)
|
#define MAP_SIZE (32 * 1024)
|
||||||
|
|
||||||
static void test_map_large(void)
|
static void test_map_large(void)
|
||||||
|
@ -1600,6 +1688,8 @@ static void run_all_tests(void)
|
||||||
|
|
||||||
test_queuemap(0, NULL);
|
test_queuemap(0, NULL);
|
||||||
test_stackmap(0, NULL);
|
test_stackmap(0, NULL);
|
||||||
|
|
||||||
|
test_map_in_map();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
|
Loading…
Reference in New Issue