Skip to content

Commit

Permalink
bpf: Add test for syscall on fd array/htab lookup
Browse files Browse the repository at this point in the history
Checks are added to the existing sockex3 and test_map_in_map test.

Signed-off-by: Martin KaFai Lau <[email protected]>
Acked-by: Daniel Borkmann <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
iamkafai authored and davem330 committed Jun 29, 2017
1 parent 14dc6f0 commit a8744f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion samples/bpf/sockex3_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <arpa/inet.h>
#include <sys/resource.h>

#define PARSE_IP 3
#define PARSE_IP_PROG_FD (prog_fd[0])
#define PROG_ARRAY_FD (map_fd[0])

struct bpf_flow_keys {
__be32 src;
__be32 dst;
Expand All @@ -28,7 +32,9 @@ int main(int argc, char **argv)
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
char filename[256];
FILE *f;
int i, sock;
int i, sock, err, id, key = PARSE_IP;
struct bpf_prog_info info = {};
uint32_t info_len = sizeof(info);

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
setrlimit(RLIMIT_MEMLOCK, &r);
Expand All @@ -38,6 +44,13 @@ int main(int argc, char **argv)
return 1;
}

/* Test fd array lookup which returns the id of the bpf_prog */
err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len);
assert(!err);
err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id);
assert(!err);
assert(id == info.id);

sock = open_raw_sock("lo");

assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4],
Expand Down
17 changes: 17 additions & 0 deletions samples/bpf/test_map_in_map_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ static const char * const test_names[] = {

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

static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
{
struct bpf_map_info info = {};
uint32_t info_len = sizeof(info);
int ret, id;

ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
assert(!ret);

ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
assert(!ret);
assert(id == info.id);
}

static void populate_map(uint32_t port_key, int magic_result)
{
int ret;
Expand All @@ -45,12 +59,15 @@ static void populate_map(uint32_t port_key, int magic_result)

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

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

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

static void test_map_in_map(void)
Expand Down

0 comments on commit a8744f2

Please sign in to comment.