Skip to content

Commit

Permalink
bpf: Add tests for map-in-map
Browse files Browse the repository at this point in the history
Test cases for array of maps and hash of maps.

Signed-off-by: Martin KaFai Lau <[email protected]>
Acked-by: Alexei Starovoitov <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
iamkafai authored and davem330 committed Mar 22, 2017
1 parent bcc6b1b commit fb30d4b
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 18 deletions.
4 changes: 4 additions & 0 deletions samples/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ hostprogs-y += sampleip
hostprogs-y += tc_l2_redirect
hostprogs-y += lwt_len_hist
hostprogs-y += xdp_tx_iptunnel
hostprogs-y += test_map_in_map

# Libbpf dependencies
LIBBPF := ../../tools/lib/bpf/bpf.o
Expand Down Expand Up @@ -72,6 +73,7 @@ sampleip-objs := bpf_load.o $(LIBBPF) sampleip_user.o
tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o
lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o
xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o
test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o

# Tell kbuild to always build the programs
always := $(hostprogs-y)
Expand Down Expand Up @@ -105,6 +107,7 @@ always += trace_event_kern.o
always += sampleip_kern.o
always += lwt_len_hist_kern.o
always += xdp_tx_iptunnel_kern.o
always += test_map_in_map_kern.o

HOSTCFLAGS += -I$(objtree)/usr/include
HOSTCFLAGS += -I$(srctree)/tools/lib/
Expand Down Expand Up @@ -139,6 +142,7 @@ HOSTLOADLIBES_sampleip += -lelf
HOSTLOADLIBES_tc_l2_redirect += -l elf
HOSTLOADLIBES_lwt_len_hist += -l elf
HOSTLOADLIBES_xdp_tx_iptunnel += -lelf
HOSTLOADLIBES_test_map_in_map += -lelf

# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
Expand Down
1 change: 1 addition & 0 deletions samples/bpf/bpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct bpf_map_def {
unsigned int value_size;
unsigned int max_entries;
unsigned int map_flags;
unsigned int inner_map_idx;
};

static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
Expand Down
22 changes: 17 additions & 5 deletions samples/bpf/bpf_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct bpf_map_def {
unsigned int value_size;
unsigned int max_entries;
unsigned int map_flags;
unsigned int inner_map_idx;
};

static int populate_prog_array(const char *event, int prog_fd)
Expand Down Expand Up @@ -198,11 +199,22 @@ static int load_maps(struct bpf_map_def *maps, int len)

for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {

map_fd[i] = bpf_create_map(maps[i].type,
maps[i].key_size,
maps[i].value_size,
maps[i].max_entries,
maps[i].map_flags);
if (maps[i].type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
maps[i].type == BPF_MAP_TYPE_HASH_OF_MAPS) {
int inner_map_fd = map_fd[maps[i].inner_map_idx];

map_fd[i] = bpf_create_map_in_map(maps[i].type,
maps[i].key_size,
inner_map_fd,
maps[i].max_entries,
maps[i].map_flags);
} else {
map_fd[i] = bpf_create_map(maps[i].type,
maps[i].key_size,
maps[i].value_size,
maps[i].max_entries,
maps[i].map_flags);
}
if (map_fd[i] < 0) {
printf("failed to create a map: %d %s\n",
errno, strerror(errno));
Expand Down
173 changes: 173 additions & 0 deletions samples/bpf/test_map_in_map_kern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* Copyright (c) 2017 Facebook
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*/
#define KBUILD_MODNAME "foo"
#include <linux/ptrace.h>
#include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/in6.h>
#include "bpf_helpers.h"

#define MAX_NR_PORTS 65536

/* map #0 */
struct bpf_map_def SEC("maps") port_a = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = sizeof(u32),
.value_size = sizeof(int),
.max_entries = MAX_NR_PORTS,
};

/* map #1 */
struct bpf_map_def SEC("maps") port_h = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(u32),
.value_size = sizeof(int),
.max_entries = 1,
};

/* map #2 */
struct bpf_map_def SEC("maps") reg_result_h = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(u32),
.value_size = sizeof(int),
.max_entries = 1,
};

/* map #3 */
struct bpf_map_def SEC("maps") inline_result_h = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(u32),
.value_size = sizeof(int),
.max_entries = 1,
};

/* map #4 */ /* Test case #0 */
struct bpf_map_def SEC("maps") a_of_port_a = {
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
.key_size = sizeof(u32),
.inner_map_idx = 0, /* map_fd[0] is port_a */
.max_entries = MAX_NR_PORTS,
};

/* map #5 */ /* Test case #1 */
struct bpf_map_def SEC("maps") h_of_port_a = {
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.key_size = sizeof(u32),
.inner_map_idx = 0, /* map_fd[0] is port_a */
.max_entries = 1,
};

/* map #6 */ /* Test case #2 */
struct bpf_map_def SEC("maps") h_of_port_h = {
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.key_size = sizeof(u32),
.inner_map_idx = 1, /* map_fd[1] is port_h */
.max_entries = 1,
};

static __always_inline int do_reg_lookup(void *inner_map, u32 port)
{
int *result;

result = bpf_map_lookup_elem(inner_map, &port);
return result ? *result : -ENOENT;
}

static __always_inline int do_inline_array_lookup(void *inner_map, u32 port)
{
int *result;

if (inner_map != &port_a)
return -EINVAL;

result = bpf_map_lookup_elem(&port_a, &port);
return result ? *result : -ENOENT;
}

static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
{
int *result;

if (inner_map != &port_h)
return -EINVAL;

result = bpf_map_lookup_elem(&port_h, &port);
return result ? *result : -ENOENT;
}

SEC("kprobe/sys_connect")
int trace_sys_connect(struct pt_regs *ctx)
{
struct sockaddr_in6 *in6;
u16 test_case, port, dst6[8];
int addrlen, ret, inline_ret, ret_key = 0;
u32 port_key;
void *outer_map, *inner_map;
bool inline_hash = false;

in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
addrlen = (int)PT_REGS_PARM3(ctx);

if (addrlen != sizeof(*in6))
return 0;

ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
if (ret) {
inline_ret = ret;
goto done;
}

if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
return 0;

test_case = dst6[7];

ret = bpf_probe_read(&port, sizeof(port), &in6->sin6_port);
if (ret) {
inline_ret = ret;
goto done;
}

port_key = port;

ret = -ENOENT;
if (test_case == 0) {
outer_map = &a_of_port_a;
} else if (test_case == 1) {
outer_map = &h_of_port_a;
} else if (test_case == 2) {
outer_map = &h_of_port_h;
} else {
ret = __LINE__;
inline_ret = ret;
goto done;
}

inner_map = bpf_map_lookup_elem(outer_map, &port_key);
if (!inner_map) {
ret = __LINE__;
inline_ret = ret;
goto done;
}

ret = do_reg_lookup(inner_map, port_key);

if (test_case == 0 || test_case == 1)
inline_ret = do_inline_array_lookup(inner_map, port_key);
else
inline_ret = do_inline_hash_lookup(inner_map, port_key);

done:
bpf_map_update_elem(&reg_result_h, &ret_key, &ret, BPF_ANY);
bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY);

return 0;
}

char _license[] SEC("license") = "GPL";
u32 _version SEC("version") = LINUX_VERSION_CODE;
116 changes: 116 additions & 0 deletions samples/bpf/test_map_in_map_user.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2017 Facebook
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*/
#include <sys/resource.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include "libbpf.h"
#include "bpf_load.h"

#define PORT_A (map_fd[0])
#define PORT_H (map_fd[1])
#define REG_RESULT_H (map_fd[2])
#define INLINE_RESULT_H (map_fd[3])
#define A_OF_PORT_A (map_fd[4]) /* Test case #0 */
#define H_OF_PORT_A (map_fd[5]) /* Test case #1 */
#define H_OF_PORT_H (map_fd[6]) /* Test case #2 */

static const char * const test_names[] = {
"Array of Array",
"Hash of Array",
"Hash of Hash",
};

#define NR_TESTS (sizeof(test_names) / sizeof(*test_names))

static void populate_map(uint32_t port_key, int magic_result)
{
int ret;

ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
assert(!ret);

ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
BPF_NOEXIST);
assert(!ret);

ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
assert(!ret);

ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
assert(!ret);

ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
assert(!ret);
}

static void test_map_in_map(void)
{
struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
uint32_t result_key = 0, port_key;
int result, inline_result;
int magic_result = 0xfaceb00c;
int ret;
int i;

port_key = rand() & 0x00FF;
populate_map(port_key, magic_result);

in6.sin6_addr.s6_addr16[0] = 0xdead;
in6.sin6_addr.s6_addr16[1] = 0xbeef;
in6.sin6_port = port_key;

for (i = 0; i < NR_TESTS; i++) {
printf("%s: ", test_names[i]);

in6.sin6_addr.s6_addr16[7] = i;
ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
assert(ret == -1 && errno == EBADF);

ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
assert(!ret);

ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
&inline_result);
assert(!ret);

if (result != magic_result || inline_result != magic_result) {
printf("Error. result:%d inline_result:%d\n",
result, inline_result);
exit(1);
}

bpf_map_delete_elem(REG_RESULT_H, &result_key);
bpf_map_delete_elem(INLINE_RESULT_H, &result_key);

printf("Pass\n");
}
}

int main(int argc, char **argv)
{
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
char filename[256];

assert(!setrlimit(RLIMIT_MEMLOCK, &r));

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

if (load_bpf_file(filename)) {
printf("%s", bpf_log_buf);
return 1;
}

test_map_in_map();

return 0;
}
3 changes: 3 additions & 0 deletions tools/include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ enum bpf_map_type {
BPF_MAP_TYPE_LRU_HASH,
BPF_MAP_TYPE_LRU_PERCPU_HASH,
BPF_MAP_TYPE_LPM_TRIE,
BPF_MAP_TYPE_ARRAY_OF_MAPS,
BPF_MAP_TYPE_HASH_OF_MAPS,
};

enum bpf_prog_type {
Expand Down Expand Up @@ -152,6 +154,7 @@ union bpf_attr {
__u32 value_size; /* size of value in bytes */
__u32 max_entries; /* max number of entries in a map */
__u32 map_flags; /* prealloc or not */
__u32 inner_map_fd; /* fd pointing to the inner map */
};

struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
Expand Down
Loading

0 comments on commit fb30d4b

Please sign in to comment.