diff options
author | Martin KaFai Lau <kafai@fb.com> | 2017-06-28 02:08:35 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-06-29 13:13:26 -0400 |
commit | a8744f2528c14e4545c6071b6681ab17607be2fa (patch) | |
tree | 7e7b11a58be303f42072fd51c260354cf110b5e2 /samples | |
parent | 14dc6f04f49dc12614d7e90928b495b8d73cd471 (diff) |
bpf: Add test for syscall on fd array/htab lookup
Checks are added to the existing sockex3 and test_map_in_map test.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/bpf/sockex3_user.c | 15 | ||||
-rw-r--r-- | samples/bpf/test_map_in_map_user.c | 17 |
2 files changed, 31 insertions, 1 deletions
diff --git a/samples/bpf/sockex3_user.c b/samples/bpf/sockex3_user.c index b5524d417eb5..877ecf8fc5ac 100644 --- a/samples/bpf/sockex3_user.c +++ b/samples/bpf/sockex3_user.c | |||
@@ -8,6 +8,10 @@ | |||
8 | #include <arpa/inet.h> | 8 | #include <arpa/inet.h> |
9 | #include <sys/resource.h> | 9 | #include <sys/resource.h> |
10 | 10 | ||
11 | #define PARSE_IP 3 | ||
12 | #define PARSE_IP_PROG_FD (prog_fd[0]) | ||
13 | #define PROG_ARRAY_FD (map_fd[0]) | ||
14 | |||
11 | struct bpf_flow_keys { | 15 | struct bpf_flow_keys { |
12 | __be32 src; | 16 | __be32 src; |
13 | __be32 dst; | 17 | __be32 dst; |
@@ -28,7 +32,9 @@ int main(int argc, char **argv) | |||
28 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; | 32 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
29 | char filename[256]; | 33 | char filename[256]; |
30 | FILE *f; | 34 | FILE *f; |
31 | int i, sock; | 35 | int i, sock, err, id, key = PARSE_IP; |
36 | struct bpf_prog_info info = {}; | ||
37 | uint32_t info_len = sizeof(info); | ||
32 | 38 | ||
33 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 39 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
34 | setrlimit(RLIMIT_MEMLOCK, &r); | 40 | setrlimit(RLIMIT_MEMLOCK, &r); |
@@ -38,6 +44,13 @@ int main(int argc, char **argv) | |||
38 | return 1; | 44 | return 1; |
39 | } | 45 | } |
40 | 46 | ||
47 | /* Test fd array lookup which returns the id of the bpf_prog */ | ||
48 | err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len); | ||
49 | assert(!err); | ||
50 | err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id); | ||
51 | assert(!err); | ||
52 | assert(id == info.id); | ||
53 | |||
41 | sock = open_raw_sock("lo"); | 54 | sock = open_raw_sock("lo"); |
42 | 55 | ||
43 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4], | 56 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4], |
diff --git a/samples/bpf/test_map_in_map_user.c b/samples/bpf/test_map_in_map_user.c index f62fdc2bd428..1aca18539d8d 100644 --- a/samples/bpf/test_map_in_map_user.c +++ b/samples/bpf/test_map_in_map_user.c | |||
@@ -32,6 +32,20 @@ static const char * const test_names[] = { | |||
32 | 32 | ||
33 | #define NR_TESTS (sizeof(test_names) / sizeof(*test_names)) | 33 | #define NR_TESTS (sizeof(test_names) / sizeof(*test_names)) |
34 | 34 | ||
35 | static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key) | ||
36 | { | ||
37 | struct bpf_map_info info = {}; | ||
38 | uint32_t info_len = sizeof(info); | ||
39 | int ret, id; | ||
40 | |||
41 | ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len); | ||
42 | assert(!ret); | ||
43 | |||
44 | ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id); | ||
45 | assert(!ret); | ||
46 | assert(id == info.id); | ||
47 | } | ||
48 | |||
35 | static void populate_map(uint32_t port_key, int magic_result) | 49 | static void populate_map(uint32_t port_key, int magic_result) |
36 | { | 50 | { |
37 | int ret; | 51 | int ret; |
@@ -45,12 +59,15 @@ static void populate_map(uint32_t port_key, int magic_result) | |||
45 | 59 | ||
46 | ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY); | 60 | ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY); |
47 | assert(!ret); | 61 | assert(!ret); |
62 | check_map_id(PORT_A, A_OF_PORT_A, port_key); | ||
48 | 63 | ||
49 | ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST); | 64 | ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST); |
50 | assert(!ret); | 65 | assert(!ret); |
66 | check_map_id(PORT_A, H_OF_PORT_A, port_key); | ||
51 | 67 | ||
52 | ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST); | 68 | ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST); |
53 | assert(!ret); | 69 | assert(!ret); |
70 | check_map_id(PORT_H, H_OF_PORT_H, port_key); | ||
54 | } | 71 | } |
55 | 72 | ||
56 | static void test_map_in_map(void) | 73 | static void test_map_in_map(void) |