samples: bpf: Refactor BPF map in map test with libbpf

From commit 646f02ffdd ("libbpf: Add BTF-defined map-in-map
support"), a way to define internal map in BTF-defined map has been
added.

Instead of using previous 'inner_map_idx' definition, the structure to
be used for the inner map can be directly defined using array directive.

    __array(values, struct inner_map)

This commit refactors map in map test program with libbpf by explicitly
defining inner map with BTF-defined format.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200707184855.30968-3-danieltimlee@gmail.com
This commit is contained in:
Daniel T. Lee 2020-07-08 03:48:53 +09:00 committed by Daniel Borkmann
parent af9bd3e333
commit 88795b4adb
3 changed files with 91 additions and 49 deletions

View File

@ -93,7 +93,7 @@ sampleip-objs := sampleip_user.o $(TRACE_HELPERS)
tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o tc_l2_redirect-objs := bpf_load.o tc_l2_redirect_user.o
lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o lwt_len_hist-objs := bpf_load.o lwt_len_hist_user.o
xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o xdp_tx_iptunnel-objs := xdp_tx_iptunnel_user.o
test_map_in_map-objs := bpf_load.o test_map_in_map_user.o test_map_in_map-objs := test_map_in_map_user.o
per_socket_stats_example-objs := cookie_uid_helper_example.o per_socket_stats_example-objs := cookie_uid_helper_example.o
xdp_redirect-objs := xdp_redirect_user.o xdp_redirect-objs := xdp_redirect_user.o
xdp_redirect_map-objs := xdp_redirect_map_user.o xdp_redirect_map-objs := xdp_redirect_map_user.o

View File

@ -11,7 +11,6 @@
#include <uapi/linux/bpf.h> #include <uapi/linux/bpf.h>
#include <uapi/linux/in6.h> #include <uapi/linux/in6.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include "bpf_legacy.h"
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h> #include <bpf/bpf_core_read.h>
#include "trace_common.h" #include "trace_common.h"
@ -19,60 +18,60 @@
#define MAX_NR_PORTS 65536 #define MAX_NR_PORTS 65536
/* map #0 */ /* map #0 */
struct bpf_map_def_legacy SEC("maps") port_a = { struct inner_a {
.type = BPF_MAP_TYPE_ARRAY, __uint(type, BPF_MAP_TYPE_ARRAY);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(int), __type(value, int);
.max_entries = MAX_NR_PORTS, __uint(max_entries, MAX_NR_PORTS);
}; } port_a SEC(".maps");
/* map #1 */ /* map #1 */
struct bpf_map_def_legacy SEC("maps") port_h = { struct inner_h {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(int), __type(value, int);
.max_entries = 1, __uint(max_entries, 1);
}; } port_h SEC(".maps");
/* map #2 */ /* map #2 */
struct bpf_map_def_legacy SEC("maps") reg_result_h = { struct {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(int), __type(value, int);
.max_entries = 1, __uint(max_entries, 1);
}; } reg_result_h SEC(".maps");
/* map #3 */ /* map #3 */
struct bpf_map_def_legacy SEC("maps") inline_result_h = { struct {
.type = BPF_MAP_TYPE_HASH, __uint(type, BPF_MAP_TYPE_HASH);
.key_size = sizeof(u32), __type(key, u32);
.value_size = sizeof(int), __type(value, int);
.max_entries = 1, __uint(max_entries, 1);
}; } inline_result_h SEC(".maps");
/* map #4 */ /* Test case #0 */ /* map #4 */ /* Test case #0 */
struct bpf_map_def_legacy SEC("maps") a_of_port_a = { struct {
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS, __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
.key_size = sizeof(u32), __uint(max_entries, MAX_NR_PORTS);
.inner_map_idx = 0, /* map_fd[0] is port_a */ __uint(key_size, sizeof(u32));
.max_entries = MAX_NR_PORTS, __array(values, struct inner_a); /* use inner_a as inner map */
}; } a_of_port_a SEC(".maps");
/* map #5 */ /* Test case #1 */ /* map #5 */ /* Test case #1 */
struct bpf_map_def_legacy SEC("maps") h_of_port_a = { struct {
.type = BPF_MAP_TYPE_HASH_OF_MAPS, __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
.key_size = sizeof(u32), __uint(max_entries, 1);
.inner_map_idx = 0, /* map_fd[0] is port_a */ __uint(key_size, sizeof(u32));
.max_entries = 1, __array(values, struct inner_a); /* use inner_a as inner map */
}; } h_of_port_a SEC(".maps");
/* map #6 */ /* Test case #2 */ /* map #6 */ /* Test case #2 */
struct bpf_map_def_legacy SEC("maps") h_of_port_h = { struct {
.type = BPF_MAP_TYPE_HASH_OF_MAPS, __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
.key_size = sizeof(u32), __uint(max_entries, 1);
.inner_map_idx = 1, /* map_fd[1] is port_h */ __uint(key_size, sizeof(u32));
.max_entries = 1, __array(values, struct inner_h); /* use inner_h as inner map */
}; } h_of_port_h SEC(".maps");
static __always_inline int do_reg_lookup(void *inner_map, u32 port) static __always_inline int do_reg_lookup(void *inner_map, u32 port)
{ {

View File

@ -11,7 +11,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_load.h" #include <bpf/libbpf.h>
static int map_fd[7];
#define PORT_A (map_fd[0]) #define PORT_A (map_fd[0])
#define PORT_H (map_fd[1]) #define PORT_H (map_fd[1])
@ -113,18 +115,59 @@ static void test_map_in_map(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
struct bpf_link *link = NULL;
struct bpf_program *prog;
struct bpf_object *obj;
char filename[256]; char filename[256];
assert(!setrlimit(RLIMIT_MEMLOCK, &r)); if (setrlimit(RLIMIT_MEMLOCK, &r)) {
perror("setrlimit(RLIMIT_MEMLOCK)");
return 1;
}
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj)) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
return 0;
}
if (load_bpf_file(filename)) { prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
printf("%s", bpf_log_buf); if (!prog) {
return 1; printf("finding a prog in obj file failed\n");
goto cleanup;
}
/* load BPF program */
if (bpf_object__load(obj)) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
goto cleanup;
}
map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a");
map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h");
map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h");
map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h");
map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a");
map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a");
map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h");
if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 ||
map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) {
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
goto cleanup;
}
link = bpf_program__attach(prog);
if (libbpf_get_error(link)) {
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
link = NULL;
goto cleanup;
} }
test_map_in_map(); test_map_in_map();
cleanup:
bpf_link__destroy(link);
bpf_object__close(obj);
return 0; return 0;
} }