Commit 7708bd43 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'reuseport_to_test_progs'

Jakub Sitnicki says:

====================
This change has been suggested by Martin Lau [0] during a review of a
related patch set that extends reuseport tests [1].

Patches 1 & 2 address a warning due to unrecognized section name from
libbpf when running reuseport tests. We don't want to carry this warning
into test_progs.

Patches 3-8 massage the reuseport tests to ease the switch to test_progs
framework. The intention here is to show the work. Happy to squash these,
if needed.

Patches 9-10 do the actual move and conversion to test_progs.

Output from a test_progs run after changes pasted below.

Thanks,
Jakub

[0] https://lore.kernel.org/bpf/20191123110751.6729-1-jakub@cloudflare.com/T/#m607d822caeb1eb5db101172821a78cc3896ff1c3
[1] https://lore.kernel.org/bpf/20191123110751.6729-1-jakub@cloudflare.com/T/#m55881bae9fb6e34837d07a0c0a7ffbc138f8d06f
====================
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 679152d3 7ee0d4e9
...@@ -4983,6 +4983,7 @@ static const struct { ...@@ -4983,6 +4983,7 @@ static const struct {
enum bpf_attach_type attach_type; enum bpf_attach_type attach_type;
} section_names[] = { } section_names[] = {
BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
BPF_PROG_SEC("sk_reuseport", BPF_PROG_TYPE_SK_REUSEPORT),
BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE),
BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE), BPF_PROG_SEC("uprobe/", BPF_PROG_TYPE_KPROBE),
BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE),
......
...@@ -28,7 +28,7 @@ LDLIBS += -lcap -lelf -lrt -lpthread ...@@ -28,7 +28,7 @@ LDLIBS += -lcap -lelf -lrt -lpthread
TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \ test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
test_cgroup_storage test_select_reuseport \ test_cgroup_storage \
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \ test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
test_cgroup_attach test_progs-no_alu32 test_cgroup_attach test_progs-no_alu32
......
...@@ -20,8 +20,11 @@ ...@@ -20,8 +20,11 @@
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "bpf_rlimit.h" #include "bpf_rlimit.h"
#include "bpf_util.h" #include "bpf_util.h"
#include "test_progs.h"
#include "test_select_reuseport_common.h" #include "test_select_reuseport_common.h"
#define MAX_TEST_NAME 80
#define MIN_TCPHDR_LEN 20 #define MIN_TCPHDR_LEN 20
#define UDPHDR_LEN 8 #define UDPHDR_LEN 8
...@@ -32,11 +35,11 @@ ...@@ -32,11 +35,11 @@
static int result_map, tmp_index_ovr_map, linum_map, data_check_map; static int result_map, tmp_index_ovr_map, linum_map, data_check_map;
static enum result expected_results[NR_RESULTS]; static enum result expected_results[NR_RESULTS];
static int sk_fds[REUSEPORT_ARRAY_SIZE]; static int sk_fds[REUSEPORT_ARRAY_SIZE];
static int reuseport_array, outer_map; static int reuseport_array = -1, outer_map = -1;
static int select_by_skb_data_prog; static int select_by_skb_data_prog;
static int saved_tcp_syncookie; static int saved_tcp_syncookie = -1;
static struct bpf_object *obj; static struct bpf_object *obj;
static int saved_tcp_fo; static int saved_tcp_fo = -1;
static __u32 index_zero; static __u32 index_zero;
static int epfd; static int epfd;
...@@ -46,16 +49,21 @@ static union sa46 { ...@@ -46,16 +49,21 @@ static union sa46 {
sa_family_t family; sa_family_t family;
} srv_sa; } srv_sa;
#define CHECK(condition, tag, format...) ({ \ #define RET_IF(condition, tag, format...) ({ \
int __ret = !!(condition); \ if (CHECK_FAIL(condition)) { \
if (__ret) { \ printf(tag " " format); \
printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \ return; \
printf(format); \ } \
exit(-1); \ })
#define RET_ERR(condition, tag, format...) ({ \
if (CHECK_FAIL(condition)) { \
printf(tag " " format); \
return -1; \
} \ } \
}) })
static void create_maps(void) static int create_maps(void)
{ {
struct bpf_create_map_attr attr = {}; struct bpf_create_map_attr attr = {};
...@@ -67,8 +75,8 @@ static void create_maps(void) ...@@ -67,8 +75,8 @@ static void create_maps(void)
attr.max_entries = REUSEPORT_ARRAY_SIZE; attr.max_entries = REUSEPORT_ARRAY_SIZE;
reuseport_array = bpf_create_map_xattr(&attr); reuseport_array = bpf_create_map_xattr(&attr);
CHECK(reuseport_array == -1, "creating reuseport_array", RET_ERR(reuseport_array == -1, "creating reuseport_array",
"reuseport_array:%d errno:%d\n", reuseport_array, errno); "reuseport_array:%d errno:%d\n", reuseport_array, errno);
/* Creating outer_map */ /* Creating outer_map */
attr.name = "outer_map"; attr.name = "outer_map";
...@@ -78,63 +86,61 @@ static void create_maps(void) ...@@ -78,63 +86,61 @@ static void create_maps(void)
attr.max_entries = 1; attr.max_entries = 1;
attr.inner_map_fd = reuseport_array; attr.inner_map_fd = reuseport_array;
outer_map = bpf_create_map_xattr(&attr); outer_map = bpf_create_map_xattr(&attr);
CHECK(outer_map == -1, "creating outer_map", RET_ERR(outer_map == -1, "creating outer_map",
"outer_map:%d errno:%d\n", outer_map, errno); "outer_map:%d errno:%d\n", outer_map, errno);
return 0;
} }
static void prepare_bpf_obj(void) static int prepare_bpf_obj(void)
{ {
struct bpf_program *prog; struct bpf_program *prog;
struct bpf_map *map; struct bpf_map *map;
int err; int err;
struct bpf_object_open_attr attr = {
.file = "test_select_reuseport_kern.o",
.prog_type = BPF_PROG_TYPE_SK_REUSEPORT,
};
obj = bpf_object__open_xattr(&attr); obj = bpf_object__open("test_select_reuseport_kern.o");
CHECK(IS_ERR_OR_NULL(obj), "open test_select_reuseport_kern.o", RET_ERR(IS_ERR_OR_NULL(obj), "open test_select_reuseport_kern.o",
"obj:%p PTR_ERR(obj):%ld\n", obj, PTR_ERR(obj)); "obj:%p PTR_ERR(obj):%ld\n", obj, PTR_ERR(obj));
prog = bpf_program__next(NULL, obj);
CHECK(!prog, "get first bpf_program", "!prog\n");
bpf_program__set_type(prog, attr.prog_type);
map = bpf_object__find_map_by_name(obj, "outer_map"); map = bpf_object__find_map_by_name(obj, "outer_map");
CHECK(!map, "find outer_map", "!map\n"); RET_ERR(!map, "find outer_map", "!map\n");
err = bpf_map__reuse_fd(map, outer_map); err = bpf_map__reuse_fd(map, outer_map);
CHECK(err, "reuse outer_map", "err:%d\n", err); RET_ERR(err, "reuse outer_map", "err:%d\n", err);
err = bpf_object__load(obj); err = bpf_object__load(obj);
CHECK(err, "load bpf_object", "err:%d\n", err); RET_ERR(err, "load bpf_object", "err:%d\n", err);
prog = bpf_program__next(NULL, obj);
RET_ERR(!prog, "get first bpf_program", "!prog\n");
select_by_skb_data_prog = bpf_program__fd(prog); select_by_skb_data_prog = bpf_program__fd(prog);
CHECK(select_by_skb_data_prog == -1, "get prog fd", RET_ERR(select_by_skb_data_prog == -1, "get prog fd",
"select_by_skb_data_prog:%d\n", select_by_skb_data_prog); "select_by_skb_data_prog:%d\n", select_by_skb_data_prog);
map = bpf_object__find_map_by_name(obj, "result_map"); map = bpf_object__find_map_by_name(obj, "result_map");
CHECK(!map, "find result_map", "!map\n"); RET_ERR(!map, "find result_map", "!map\n");
result_map = bpf_map__fd(map); result_map = bpf_map__fd(map);
CHECK(result_map == -1, "get result_map fd", RET_ERR(result_map == -1, "get result_map fd",
"result_map:%d\n", result_map); "result_map:%d\n", result_map);
map = bpf_object__find_map_by_name(obj, "tmp_index_ovr_map"); map = bpf_object__find_map_by_name(obj, "tmp_index_ovr_map");
CHECK(!map, "find tmp_index_ovr_map", "!map\n"); RET_ERR(!map, "find tmp_index_ovr_map\n", "!map");
tmp_index_ovr_map = bpf_map__fd(map); tmp_index_ovr_map = bpf_map__fd(map);
CHECK(tmp_index_ovr_map == -1, "get tmp_index_ovr_map fd", RET_ERR(tmp_index_ovr_map == -1, "get tmp_index_ovr_map fd",
"tmp_index_ovr_map:%d\n", tmp_index_ovr_map); "tmp_index_ovr_map:%d\n", tmp_index_ovr_map);
map = bpf_object__find_map_by_name(obj, "linum_map"); map = bpf_object__find_map_by_name(obj, "linum_map");
CHECK(!map, "find linum_map", "!map\n"); RET_ERR(!map, "find linum_map", "!map\n");
linum_map = bpf_map__fd(map); linum_map = bpf_map__fd(map);
CHECK(linum_map == -1, "get linum_map fd", RET_ERR(linum_map == -1, "get linum_map fd",
"linum_map:%d\n", linum_map); "linum_map:%d\n", linum_map);
map = bpf_object__find_map_by_name(obj, "data_check_map"); map = bpf_object__find_map_by_name(obj, "data_check_map");
CHECK(!map, "find data_check_map", "!map\n"); RET_ERR(!map, "find data_check_map", "!map\n");
data_check_map = bpf_map__fd(map); data_check_map = bpf_map__fd(map);
CHECK(data_check_map == -1, "get data_check_map fd", RET_ERR(data_check_map == -1, "get data_check_map fd",
"data_check_map:%d\n", data_check_map); "data_check_map:%d\n", data_check_map);
return 0;
} }
static void sa46_init_loopback(union sa46 *sa, sa_family_t family) static void sa46_init_loopback(union sa46 *sa, sa_family_t family)
...@@ -163,65 +169,73 @@ static int read_int_sysctl(const char *sysctl) ...@@ -163,65 +169,73 @@ static int read_int_sysctl(const char *sysctl)
int fd, ret; int fd, ret;
fd = open(sysctl, 0); fd = open(sysctl, 0);
CHECK(fd == -1, "open(sysctl)", "sysctl:%s fd:%d errno:%d\n", RET_ERR(fd == -1, "open(sysctl)",
sysctl, fd, errno); "sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
ret = read(fd, buf, sizeof(buf)); ret = read(fd, buf, sizeof(buf));
CHECK(ret <= 0, "read(sysctl)", "sysctl:%s ret:%d errno:%d\n", RET_ERR(ret <= 0, "read(sysctl)",
sysctl, ret, errno); "sysctl:%s ret:%d errno:%d\n", sysctl, ret, errno);
close(fd);
close(fd);
return atoi(buf); return atoi(buf);
} }
static void write_int_sysctl(const char *sysctl, int v) static int write_int_sysctl(const char *sysctl, int v)
{ {
int fd, ret, size; int fd, ret, size;
char buf[16]; char buf[16];
fd = open(sysctl, O_RDWR); fd = open(sysctl, O_RDWR);
CHECK(fd == -1, "open(sysctl)", "sysctl:%s fd:%d errno:%d\n", RET_ERR(fd == -1, "open(sysctl)",
sysctl, fd, errno); "sysctl:%s fd:%d errno:%d\n", sysctl, fd, errno);
size = snprintf(buf, sizeof(buf), "%d", v); size = snprintf(buf, sizeof(buf), "%d", v);
ret = write(fd, buf, size); ret = write(fd, buf, size);
CHECK(ret != size, "write(sysctl)", RET_ERR(ret != size, "write(sysctl)",
"sysctl:%s ret:%d size:%d errno:%d\n", sysctl, ret, size, errno); "sysctl:%s ret:%d size:%d errno:%d\n",
sysctl, ret, size, errno);
close(fd); close(fd);
return 0;
} }
static void restore_sysctls(void) static void restore_sysctls(void)
{ {
write_int_sysctl(TCP_FO_SYSCTL, saved_tcp_fo); if (saved_tcp_fo != -1)
write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, saved_tcp_syncookie); write_int_sysctl(TCP_FO_SYSCTL, saved_tcp_fo);
if (saved_tcp_syncookie != -1)
write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, saved_tcp_syncookie);
} }
static void enable_fastopen(void) static int enable_fastopen(void)
{ {
int fo; int fo;
fo = read_int_sysctl(TCP_FO_SYSCTL); fo = read_int_sysctl(TCP_FO_SYSCTL);
write_int_sysctl(TCP_FO_SYSCTL, fo | 7); if (fo < 0)
return -1;
return write_int_sysctl(TCP_FO_SYSCTL, fo | 7);
} }
static void enable_syncookie(void) static int enable_syncookie(void)
{ {
write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 2); return write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 2);
} }
static void disable_syncookie(void) static int disable_syncookie(void)
{ {
write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 0); return write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, 0);
} }
static __u32 get_linum(void) static long get_linum(void)
{ {
__u32 linum; __u32 linum;
int err; int err;
err = bpf_map_lookup_elem(linum_map, &index_zero, &linum); err = bpf_map_lookup_elem(linum_map, &index_zero, &linum);
CHECK(err == -1, "lookup_elem(linum_map)", "err:%d errno:%d\n", RET_ERR(err == -1, "lookup_elem(linum_map)", "err:%d errno:%d\n",
err, errno); err, errno);
return linum; return linum;
} }
...@@ -237,12 +251,12 @@ static void check_data(int type, sa_family_t family, const struct cmd *cmd, ...@@ -237,12 +251,12 @@ static void check_data(int type, sa_family_t family, const struct cmd *cmd,
addrlen = sizeof(cli_sa); addrlen = sizeof(cli_sa);
err = getsockname(cli_fd, (struct sockaddr *)&cli_sa, err = getsockname(cli_fd, (struct sockaddr *)&cli_sa,
&addrlen); &addrlen);
CHECK(err == -1, "getsockname(cli_fd)", "err:%d errno:%d\n", RET_IF(err == -1, "getsockname(cli_fd)", "err:%d errno:%d\n",
err, errno); err, errno);
err = bpf_map_lookup_elem(data_check_map, &index_zero, &result); err = bpf_map_lookup_elem(data_check_map, &index_zero, &result);
CHECK(err == -1, "lookup_elem(data_check_map)", "err:%d errno:%d\n", RET_IF(err == -1, "lookup_elem(data_check_map)", "err:%d errno:%d\n",
err, errno); err, errno);
if (type == SOCK_STREAM) { if (type == SOCK_STREAM) {
expected.len = MIN_TCPHDR_LEN; expected.len = MIN_TCPHDR_LEN;
...@@ -284,22 +298,22 @@ static void check_data(int type, sa_family_t family, const struct cmd *cmd, ...@@ -284,22 +298,22 @@ static void check_data(int type, sa_family_t family, const struct cmd *cmd,
printf("expected: (0x%x, %u, %u)\n", printf("expected: (0x%x, %u, %u)\n",
expected.eth_protocol, expected.ip_protocol, expected.eth_protocol, expected.ip_protocol,
expected.bind_inany); expected.bind_inany);
CHECK(1, "data_check result != expected", RET_IF(1, "data_check result != expected",
"bpf_prog_linum:%u\n", get_linum()); "bpf_prog_linum:%ld\n", get_linum());
} }
CHECK(!result.hash, "data_check result.hash empty", RET_IF(!result.hash, "data_check result.hash empty",
"result.hash:%u", result.hash); "result.hash:%u", result.hash);
expected.len += cmd ? sizeof(*cmd) : 0; expected.len += cmd ? sizeof(*cmd) : 0;
if (type == SOCK_STREAM) if (type == SOCK_STREAM)
CHECK(expected.len > result.len, "expected.len > result.len", RET_IF(expected.len > result.len, "expected.len > result.len",
"expected.len:%u result.len:%u bpf_prog_linum:%u\n", "expected.len:%u result.len:%u bpf_prog_linum:%ld\n",
expected.len, result.len, get_linum()); expected.len, result.len, get_linum());
else else
CHECK(expected.len != result.len, "expected.len != result.len", RET_IF(expected.len != result.len, "expected.len != result.len",
"expected.len:%u result.len:%u bpf_prog_linum:%u\n", "expected.len:%u result.len:%u bpf_prog_linum:%ld\n",
expected.len, result.len, get_linum()); expected.len, result.len, get_linum());
} }
static void check_results(void) static void check_results(void)
...@@ -310,8 +324,8 @@ static void check_results(void) ...@@ -310,8 +324,8 @@ static void check_results(void)
for (i = 0; i < NR_RESULTS; i++) { for (i = 0; i < NR_RESULTS; i++) {
err = bpf_map_lookup_elem(result_map, &i, &results[i]); err = bpf_map_lookup_elem(result_map, &i, &results[i]);
CHECK(err == -1, "lookup_elem(result_map)", RET_IF(err == -1, "lookup_elem(result_map)",
"i:%u err:%d errno:%d\n", i, err, errno); "i:%u err:%d errno:%d\n", i, err, errno);
} }
for (i = 0; i < NR_RESULTS; i++) { for (i = 0; i < NR_RESULTS; i++) {
...@@ -337,10 +351,10 @@ static void check_results(void) ...@@ -337,10 +351,10 @@ static void check_results(void)
printf(", %u", expected_results[i]); printf(", %u", expected_results[i]);
printf("]\n"); printf("]\n");
CHECK(expected_results[broken] != results[broken], RET_IF(expected_results[broken] != results[broken],
"unexpected result", "unexpected result",
"expected_results[%u] != results[%u] bpf_prog_linum:%u\n", "expected_results[%u] != results[%u] bpf_prog_linum:%ld\n",
broken, broken, get_linum()); broken, broken, get_linum());
} }
static int send_data(int type, sa_family_t family, void *data, size_t len, static int send_data(int type, sa_family_t family, void *data, size_t len,
...@@ -350,17 +364,17 @@ static int send_data(int type, sa_family_t family, void *data, size_t len, ...@@ -350,17 +364,17 @@ static int send_data(int type, sa_family_t family, void *data, size_t len,
int fd, err; int fd, err;
fd = socket(family, type, 0); fd = socket(family, type, 0);
CHECK(fd == -1, "socket()", "fd:%d errno:%d\n", fd, errno); RET_ERR(fd == -1, "socket()", "fd:%d errno:%d\n", fd, errno);
sa46_init_loopback(&cli_sa, family); sa46_init_loopback(&cli_sa, family);
err = bind(fd, (struct sockaddr *)&cli_sa, sizeof(cli_sa)); err = bind(fd, (struct sockaddr *)&cli_sa, sizeof(cli_sa));
CHECK(fd == -1, "bind(cli_sa)", "err:%d errno:%d\n", err, errno); RET_ERR(fd == -1, "bind(cli_sa)", "err:%d errno:%d\n", err, errno);
err = sendto(fd, data, len, MSG_FASTOPEN, (struct sockaddr *)&srv_sa, err = sendto(fd, data, len, MSG_FASTOPEN, (struct sockaddr *)&srv_sa,
sizeof(srv_sa)); sizeof(srv_sa));
CHECK(err != len && expected >= PASS, RET_ERR(err != len && expected >= PASS,
"sendto()", "family:%u err:%d errno:%d expected:%d\n", "sendto()", "family:%u err:%d errno:%d expected:%d\n",
family, err, errno, expected); family, err, errno, expected);
return fd; return fd;
} }
...@@ -375,47 +389,49 @@ static void do_test(int type, sa_family_t family, struct cmd *cmd, ...@@ -375,47 +389,49 @@ static void do_test(int type, sa_family_t family, struct cmd *cmd,
cli_fd = send_data(type, family, cmd, cmd ? sizeof(*cmd) : 0, cli_fd = send_data(type, family, cmd, cmd ? sizeof(*cmd) : 0,
expected); expected);
if (cli_fd < 0)
return;
nev = epoll_wait(epfd, &ev, 1, expected >= PASS ? 5 : 0); nev = epoll_wait(epfd, &ev, 1, expected >= PASS ? 5 : 0);
CHECK((nev <= 0 && expected >= PASS) || RET_IF((nev <= 0 && expected >= PASS) ||
(nev > 0 && expected < PASS), (nev > 0 && expected < PASS),
"nev <> expected", "nev <> expected",
"nev:%d expected:%d type:%d family:%d data:(%d, %d)\n", "nev:%d expected:%d type:%d family:%d data:(%d, %d)\n",
nev, expected, type, family, nev, expected, type, family,
cmd ? cmd->reuseport_index : -1, cmd ? cmd->reuseport_index : -1,
cmd ? cmd->pass_on_failure : -1); cmd ? cmd->pass_on_failure : -1);
check_results(); check_results();
check_data(type, family, cmd, cli_fd); check_data(type, family, cmd, cli_fd);
if (expected < PASS) if (expected < PASS)
return; return;
CHECK(expected != PASS_ERR_SK_SELECT_REUSEPORT && RET_IF(expected != PASS_ERR_SK_SELECT_REUSEPORT &&
cmd->reuseport_index != ev.data.u32, cmd->reuseport_index != ev.data.u32,
"check cmd->reuseport_index", "check cmd->reuseport_index",
"cmd:(%u, %u) ev.data.u32:%u\n", "cmd:(%u, %u) ev.data.u32:%u\n",
cmd->pass_on_failure, cmd->reuseport_index, ev.data.u32); cmd->pass_on_failure, cmd->reuseport_index, ev.data.u32);
srv_fd = sk_fds[ev.data.u32]; srv_fd = sk_fds[ev.data.u32];
if (type == SOCK_STREAM) { if (type == SOCK_STREAM) {
int new_fd = accept(srv_fd, NULL, 0); int new_fd = accept(srv_fd, NULL, 0);
CHECK(new_fd == -1, "accept(srv_fd)", RET_IF(new_fd == -1, "accept(srv_fd)",
"ev.data.u32:%u new_fd:%d errno:%d\n", "ev.data.u32:%u new_fd:%d errno:%d\n",
ev.data.u32, new_fd, errno); ev.data.u32, new_fd, errno);
nread = recv(new_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT); nread = recv(new_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT);
CHECK(nread != sizeof(rcv_cmd), RET_IF(nread != sizeof(rcv_cmd),
"recv(new_fd)", "recv(new_fd)",
"ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n", "ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n",
ev.data.u32, nread, sizeof(rcv_cmd), errno); ev.data.u32, nread, sizeof(rcv_cmd), errno);
close(new_fd); close(new_fd);
} else { } else {
nread = recv(srv_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT); nread = recv(srv_fd, &rcv_cmd, sizeof(rcv_cmd), MSG_DONTWAIT);
CHECK(nread != sizeof(rcv_cmd), RET_IF(nread != sizeof(rcv_cmd),
"recv(sk_fds)", "recv(sk_fds)",
"ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n", "ev.data.u32:%u nread:%zd sizeof(rcv_cmd):%zu errno:%d\n",
ev.data.u32, nread, sizeof(rcv_cmd), errno); ev.data.u32, nread, sizeof(rcv_cmd), errno);
} }
close(cli_fd); close(cli_fd);
...@@ -428,18 +444,14 @@ static void test_err_inner_map(int type, sa_family_t family) ...@@ -428,18 +444,14 @@ static void test_err_inner_map(int type, sa_family_t family)
.pass_on_failure = 0, .pass_on_failure = 0,
}; };
printf("%s: ", __func__);
expected_results[DROP_ERR_INNER_MAP]++; expected_results[DROP_ERR_INNER_MAP]++;
do_test(type, family, &cmd, DROP_ERR_INNER_MAP); do_test(type, family, &cmd, DROP_ERR_INNER_MAP);
printf("OK\n");
} }
static void test_err_skb_data(int type, sa_family_t family) static void test_err_skb_data(int type, sa_family_t family)
{ {
printf("%s: ", __func__);
expected_results[DROP_ERR_SKB_DATA]++; expected_results[DROP_ERR_SKB_DATA]++;
do_test(type, family, NULL, DROP_ERR_SKB_DATA); do_test(type, family, NULL, DROP_ERR_SKB_DATA);
printf("OK\n");
} }
static void test_err_sk_select_port(int type, sa_family_t family) static void test_err_sk_select_port(int type, sa_family_t family)
...@@ -449,10 +461,8 @@ static void test_err_sk_select_port(int type, sa_family_t family) ...@@ -449,10 +461,8 @@ static void test_err_sk_select_port(int type, sa_family_t family)
.pass_on_failure = 0, .pass_on_failure = 0,
}; };
printf("%s: ", __func__);
expected_results[DROP_ERR_SK_SELECT_REUSEPORT]++; expected_results[DROP_ERR_SK_SELECT_REUSEPORT]++;
do_test(type, family, &cmd, DROP_ERR_SK_SELECT_REUSEPORT); do_test(type, family, &cmd, DROP_ERR_SK_SELECT_REUSEPORT);
printf("OK\n");
} }
static void test_pass(int type, sa_family_t family) static void test_pass(int type, sa_family_t family)
...@@ -460,14 +470,12 @@ static void test_pass(int type, sa_family_t family) ...@@ -460,14 +470,12 @@ static void test_pass(int type, sa_family_t family)
struct cmd cmd; struct cmd cmd;
int i; int i;
printf("%s: ", __func__);
cmd.pass_on_failure = 0; cmd.pass_on_failure = 0;
for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) { for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) {
expected_results[PASS]++; expected_results[PASS]++;
cmd.reuseport_index = i; cmd.reuseport_index = i;
do_test(type, family, &cmd, PASS); do_test(type, family, &cmd, PASS);
} }
printf("OK\n");
} }
static void test_syncookie(int type, sa_family_t family) static void test_syncookie(int type, sa_family_t family)
...@@ -481,7 +489,6 @@ static void test_syncookie(int type, sa_family_t family) ...@@ -481,7 +489,6 @@ static void test_syncookie(int type, sa_family_t family)
if (type != SOCK_STREAM) if (type != SOCK_STREAM)
return; return;
printf("%s: ", __func__);
/* /*
* +1 for TCP-SYN and * +1 for TCP-SYN and
* +1 for the TCP-ACK (ack the syncookie) * +1 for the TCP-ACK (ack the syncookie)
...@@ -497,17 +504,16 @@ static void test_syncookie(int type, sa_family_t family) ...@@ -497,17 +504,16 @@ static void test_syncookie(int type, sa_family_t family)
*/ */
err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero, err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero,
&tmp_index, BPF_ANY); &tmp_index, BPF_ANY);
CHECK(err == -1, "update_elem(tmp_index_ovr_map, 0, 1)", RET_IF(err == -1, "update_elem(tmp_index_ovr_map, 0, 1)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
do_test(type, family, &cmd, PASS); do_test(type, family, &cmd, PASS);
err = bpf_map_lookup_elem(tmp_index_ovr_map, &index_zero, err = bpf_map_lookup_elem(tmp_index_ovr_map, &index_zero,
&tmp_index); &tmp_index);
CHECK(err == -1 || tmp_index != -1, RET_IF(err == -1 || tmp_index != -1,
"lookup_elem(tmp_index_ovr_map)", "lookup_elem(tmp_index_ovr_map)",
"err:%d errno:%d tmp_index:%d\n", "err:%d errno:%d tmp_index:%d\n",
err, errno, tmp_index); err, errno, tmp_index);
disable_syncookie(); disable_syncookie();
printf("OK\n");
} }
static void test_pass_on_err(int type, sa_family_t family) static void test_pass_on_err(int type, sa_family_t family)
...@@ -517,10 +523,8 @@ static void test_pass_on_err(int type, sa_family_t family) ...@@ -517,10 +523,8 @@ static void test_pass_on_err(int type, sa_family_t family)
.pass_on_failure = 1, .pass_on_failure = 1,
}; };
printf("%s: ", __func__);
expected_results[PASS_ERR_SK_SELECT_REUSEPORT] += 1; expected_results[PASS_ERR_SK_SELECT_REUSEPORT] += 1;
do_test(type, family, &cmd, PASS_ERR_SK_SELECT_REUSEPORT); do_test(type, family, &cmd, PASS_ERR_SK_SELECT_REUSEPORT);
printf("OK\n");
} }
static void test_detach_bpf(int type, sa_family_t family) static void test_detach_bpf(int type, sa_family_t family)
...@@ -532,46 +536,47 @@ static void test_detach_bpf(int type, sa_family_t family) ...@@ -532,46 +536,47 @@ static void test_detach_bpf(int type, sa_family_t family)
struct cmd cmd = {}; struct cmd cmd = {};
int optvalue = 0; int optvalue = 0;
printf("%s: ", __func__);
err = setsockopt(sk_fds[0], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF, err = setsockopt(sk_fds[0], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF,
&optvalue, sizeof(optvalue)); &optvalue, sizeof(optvalue));
CHECK(err == -1, "setsockopt(SO_DETACH_REUSEPORT_BPF)", RET_IF(err == -1, "setsockopt(SO_DETACH_REUSEPORT_BPF)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
err = setsockopt(sk_fds[1], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF, err = setsockopt(sk_fds[1], SOL_SOCKET, SO_DETACH_REUSEPORT_BPF,
&optvalue, sizeof(optvalue)); &optvalue, sizeof(optvalue));
CHECK(err == 0 || errno != ENOENT, "setsockopt(SO_DETACH_REUSEPORT_BPF)", RET_IF(err == 0 || errno != ENOENT,
"err:%d errno:%d\n", err, errno); "setsockopt(SO_DETACH_REUSEPORT_BPF)",
"err:%d errno:%d\n", err, errno);
for (i = 0; i < NR_RESULTS; i++) { for (i = 0; i < NR_RESULTS; i++) {
err = bpf_map_lookup_elem(result_map, &i, &tmp); err = bpf_map_lookup_elem(result_map, &i, &tmp);
CHECK(err == -1, "lookup_elem(result_map)", RET_IF(err == -1, "lookup_elem(result_map)",
"i:%u err:%d errno:%d\n", i, err, errno); "i:%u err:%d errno:%d\n", i, err, errno);
nr_run_before += tmp; nr_run_before += tmp;
} }
cli_fd = send_data(type, family, &cmd, sizeof(cmd), PASS); cli_fd = send_data(type, family, &cmd, sizeof(cmd), PASS);
if (cli_fd < 0)
return;
nev = epoll_wait(epfd, &ev, 1, 5); nev = epoll_wait(epfd, &ev, 1, 5);
CHECK(nev <= 0, "nev <= 0", RET_IF(nev <= 0, "nev <= 0",
"nev:%d expected:1 type:%d family:%d data:(0, 0)\n", "nev:%d expected:1 type:%d family:%d data:(0, 0)\n",
nev, type, family); nev, type, family);
for (i = 0; i < NR_RESULTS; i++) { for (i = 0; i < NR_RESULTS; i++) {
err = bpf_map_lookup_elem(result_map, &i, &tmp); err = bpf_map_lookup_elem(result_map, &i, &tmp);
CHECK(err == -1, "lookup_elem(result_map)", RET_IF(err == -1, "lookup_elem(result_map)",
"i:%u err:%d errno:%d\n", i, err, errno); "i:%u err:%d errno:%d\n", i, err, errno);
nr_run_after += tmp; nr_run_after += tmp;
} }
CHECK(nr_run_before != nr_run_after, RET_IF(nr_run_before != nr_run_after,
"nr_run_before != nr_run_after", "nr_run_before != nr_run_after",
"nr_run_before:%u nr_run_after:%u\n", "nr_run_before:%u nr_run_after:%u\n",
nr_run_before, nr_run_after); nr_run_before, nr_run_after);
printf("OK\n");
close(cli_fd); close(cli_fd);
#else #else
printf("%s: SKIP\n", __func__); test__skip();
#endif #endif
} }
...@@ -594,73 +599,83 @@ static void prepare_sk_fds(int type, sa_family_t family, bool inany) ...@@ -594,73 +599,83 @@ static void prepare_sk_fds(int type, sa_family_t family, bool inany)
*/ */
for (i = first; i >= 0; i--) { for (i = first; i >= 0; i--) {
sk_fds[i] = socket(family, type, 0); sk_fds[i] = socket(family, type, 0);
CHECK(sk_fds[i] == -1, "socket()", "sk_fds[%d]:%d errno:%d\n", RET_IF(sk_fds[i] == -1, "socket()", "sk_fds[%d]:%d errno:%d\n",
i, sk_fds[i], errno); i, sk_fds[i], errno);
err = setsockopt(sk_fds[i], SOL_SOCKET, SO_REUSEPORT, err = setsockopt(sk_fds[i], SOL_SOCKET, SO_REUSEPORT,
&optval, sizeof(optval)); &optval, sizeof(optval));
CHECK(err == -1, "setsockopt(SO_REUSEPORT)", RET_IF(err == -1, "setsockopt(SO_REUSEPORT)",
"sk_fds[%d] err:%d errno:%d\n", "sk_fds[%d] err:%d errno:%d\n",
i, err, errno); i, err, errno);
if (i == first) { if (i == first) {
err = setsockopt(sk_fds[i], SOL_SOCKET, err = setsockopt(sk_fds[i], SOL_SOCKET,
SO_ATTACH_REUSEPORT_EBPF, SO_ATTACH_REUSEPORT_EBPF,
&select_by_skb_data_prog, &select_by_skb_data_prog,
sizeof(select_by_skb_data_prog)); sizeof(select_by_skb_data_prog));
CHECK(err == -1, "setsockopt(SO_ATTACH_REUEPORT_EBPF)", RET_IF(err == -1, "setsockopt(SO_ATTACH_REUEPORT_EBPF)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
} }
err = bind(sk_fds[i], (struct sockaddr *)&srv_sa, addrlen); err = bind(sk_fds[i], (struct sockaddr *)&srv_sa, addrlen);
CHECK(err == -1, "bind()", "sk_fds[%d] err:%d errno:%d\n", RET_IF(err == -1, "bind()", "sk_fds[%d] err:%d errno:%d\n",
i, err, errno); i, err, errno);
if (type == SOCK_STREAM) { if (type == SOCK_STREAM) {
err = listen(sk_fds[i], 10); err = listen(sk_fds[i], 10);
CHECK(err == -1, "listen()", RET_IF(err == -1, "listen()",
"sk_fds[%d] err:%d errno:%d\n", "sk_fds[%d] err:%d errno:%d\n",
i, err, errno); i, err, errno);
} }
err = bpf_map_update_elem(reuseport_array, &i, &sk_fds[i], err = bpf_map_update_elem(reuseport_array, &i, &sk_fds[i],
BPF_NOEXIST); BPF_NOEXIST);
CHECK(err == -1, "update_elem(reuseport_array)", RET_IF(err == -1, "update_elem(reuseport_array)",
"sk_fds[%d] err:%d errno:%d\n", i, err, errno); "sk_fds[%d] err:%d errno:%d\n", i, err, errno);
if (i == first) { if (i == first) {
socklen_t addrlen = sizeof(srv_sa); socklen_t addrlen = sizeof(srv_sa);
err = getsockname(sk_fds[i], (struct sockaddr *)&srv_sa, err = getsockname(sk_fds[i], (struct sockaddr *)&srv_sa,
&addrlen); &addrlen);
CHECK(err == -1, "getsockname()", RET_IF(err == -1, "getsockname()",
"sk_fds[%d] err:%d errno:%d\n", i, err, errno); "sk_fds[%d] err:%d errno:%d\n", i, err, errno);
} }
} }
epfd = epoll_create(1); epfd = epoll_create(1);
CHECK(epfd == -1, "epoll_create(1)", RET_IF(epfd == -1, "epoll_create(1)",
"epfd:%d errno:%d\n", epfd, errno); "epfd:%d errno:%d\n", epfd, errno);
ev.events = EPOLLIN; ev.events = EPOLLIN;
for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) { for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++) {
ev.data.u32 = i; ev.data.u32 = i;
err = epoll_ctl(epfd, EPOLL_CTL_ADD, sk_fds[i], &ev); err = epoll_ctl(epfd, EPOLL_CTL_ADD, sk_fds[i], &ev);
CHECK(err, "epoll_ctl(EPOLL_CTL_ADD)", "sk_fds[%d]\n", i); RET_IF(err, "epoll_ctl(EPOLL_CTL_ADD)", "sk_fds[%d]\n", i);
} }
} }
static void setup_per_test(int type, unsigned short family, bool inany) static void setup_per_test(int type, sa_family_t family, bool inany,
bool no_inner_map)
{ {
int ovr = -1, err; int ovr = -1, err;
prepare_sk_fds(type, family, inany); prepare_sk_fds(type, family, inany);
err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero, &ovr, err = bpf_map_update_elem(tmp_index_ovr_map, &index_zero, &ovr,
BPF_ANY); BPF_ANY);
CHECK(err == -1, "update_elem(tmp_index_ovr_map, 0, -1)", RET_IF(err == -1, "update_elem(tmp_index_ovr_map, 0, -1)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
/* Install reuseport_array to outer_map? */
if (no_inner_map)
return;
err = bpf_map_update_elem(outer_map, &index_zero, &reuseport_array,
BPF_ANY);
RET_IF(err == -1, "update_elem(outer_map, 0, reuseport_array)",
"err:%d errno:%d\n", err, errno);
} }
static void cleanup_per_test(void) static void cleanup_per_test(bool no_inner_map)
{ {
int i, err; int i, err;
...@@ -668,75 +683,124 @@ static void cleanup_per_test(void) ...@@ -668,75 +683,124 @@ static void cleanup_per_test(void)
close(sk_fds[i]); close(sk_fds[i]);
close(epfd); close(epfd);
/* Delete reuseport_array from outer_map? */
if (no_inner_map)
return;
err = bpf_map_delete_elem(outer_map, &index_zero); err = bpf_map_delete_elem(outer_map, &index_zero);
CHECK(err == -1, "delete_elem(outer_map)", RET_IF(err == -1, "delete_elem(outer_map)",
"err:%d errno:%d\n", err, errno); "err:%d errno:%d\n", err, errno);
} }
static void cleanup(void) static void cleanup(void)
{ {
close(outer_map); if (outer_map != -1)
close(reuseport_array); close(outer_map);
bpf_object__close(obj); if (reuseport_array != -1)
close(reuseport_array);
if (obj)
bpf_object__close(obj);
} }
static void test_all(void) static const char *family_str(sa_family_t family)
{ {
/* Extra SOCK_STREAM to test bind_inany==true */ switch (family) {
const int types[] = { SOCK_STREAM, SOCK_DGRAM, SOCK_STREAM }; case AF_INET:
const char * const type_strings[] = { "TCP", "UDP", "TCP" }; return "IPv4";
const char * const family_strings[] = { "IPv6", "IPv4" }; case AF_INET6:
const unsigned short families[] = { AF_INET6, AF_INET }; return "IPv6";
const bool bind_inany[] = { false, false, true }; default:
int t, f, err; return "unknown";
}
for (f = 0; f < ARRAY_SIZE(families); f++) { }
unsigned short family = families[f];
static const char *sotype_str(int sotype)
for (t = 0; t < ARRAY_SIZE(types); t++) { {
bool inany = bind_inany[t]; switch (sotype) {
int type = types[t]; case SOCK_STREAM:
return "TCP";
printf("######## %s/%s %s ########\n", case SOCK_DGRAM:
family_strings[f], type_strings[t], return "UDP";
inany ? " INANY " : "LOOPBACK"); default:
return "unknown";
setup_per_test(type, family, inany); }
}
test_err_inner_map(type, family);
#define TEST_INIT(fn, ...) { fn, #fn, __VA_ARGS__ }
/* Install reuseport_array to the outer_map */
err = bpf_map_update_elem(outer_map, &index_zero, static void test_config(int sotype, sa_family_t family, bool inany)
&reuseport_array, BPF_ANY); {
CHECK(err == -1, "update_elem(outer_map)", const struct test {
"err:%d errno:%d\n", err, errno); void (*fn)(int sotype, sa_family_t family);
const char *name;
test_err_skb_data(type, family); bool no_inner_map;
test_err_sk_select_port(type, family); } tests[] = {
test_pass(type, family); TEST_INIT(test_err_inner_map, true /* no_inner_map */),
test_syncookie(type, family); TEST_INIT(test_err_skb_data),
test_pass_on_err(type, family); TEST_INIT(test_err_sk_select_port),
/* Must be the last test */ TEST_INIT(test_pass),
test_detach_bpf(type, family); TEST_INIT(test_syncookie),
TEST_INIT(test_pass_on_err),
cleanup_per_test(); TEST_INIT(test_detach_bpf),
printf("\n"); };
} char s[MAX_TEST_NAME];
const struct test *t;
for (t = tests; t < tests + ARRAY_SIZE(tests); t++) {
snprintf(s, sizeof(s), "%s/%s %s %s",
family_str(family), sotype_str(sotype),
inany ? "INANY" : "LOOPBACK", t->name);
if (!test__start_subtest(s))
continue;
setup_per_test(sotype, family, inany, t->no_inner_map);
t->fn(sotype, family);
cleanup_per_test(t->no_inner_map);
} }
} }
int main(int argc, const char **argv) #define BIND_INANY true
static void test_all(void)
{ {
create_maps(); const struct config {
prepare_bpf_obj(); int sotype;
sa_family_t family;
bool inany;
} configs[] = {
{ SOCK_STREAM, AF_INET },
{ SOCK_STREAM, AF_INET, BIND_INANY },
{ SOCK_STREAM, AF_INET6 },
{ SOCK_STREAM, AF_INET6, BIND_INANY },
{ SOCK_DGRAM, AF_INET },
{ SOCK_DGRAM, AF_INET6 },
};
const struct config *c;
for (c = configs; c < configs + ARRAY_SIZE(configs); c++)
test_config(c->sotype, c->family, c->inany);
}
void test_select_reuseport(void)
{
if (create_maps())
goto out;
if (prepare_bpf_obj())
goto out;
saved_tcp_fo = read_int_sysctl(TCP_FO_SYSCTL); saved_tcp_fo = read_int_sysctl(TCP_FO_SYSCTL);
saved_tcp_syncookie = read_int_sysctl(TCP_SYNCOOKIE_SYSCTL); saved_tcp_syncookie = read_int_sysctl(TCP_SYNCOOKIE_SYSCTL);
enable_fastopen(); if (saved_tcp_syncookie < 0 || saved_tcp_syncookie < 0)
disable_syncookie(); goto out;
atexit(restore_sysctls);
test_all(); if (enable_fastopen())
goto out;
if (disable_syncookie())
goto out;
test_all();
out:
cleanup(); cleanup();
return 0; restore_sysctls();
} }
...@@ -62,7 +62,7 @@ struct { ...@@ -62,7 +62,7 @@ struct {
goto done; \ goto done; \
}) })
SEC("select_by_skb_data") SEC("sk_reuseport")
int _select_by_skb_data(struct sk_reuseport_md *reuse_md) int _select_by_skb_data(struct sk_reuseport_md *reuse_md)
{ {
__u32 linum, index = 0, flags = 0, index_zero = 0; __u32 linum, index = 0, flags = 0, index_zero = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment