Commit 9af6c844 authored by Jakub Sitnicki's avatar Jakub Sitnicki Committed by Alexei Starovoitov

selftests/bpf: Propagate errors during setup for reuseport tests

Prepare for switching reuseport tests to test_progs framework, where we
don't have the luxury to terminate the process on failure.

Modify setup helpers to signal failure via the return value with the help
of a macro similar to the one currently in use by the tests.
Signed-off-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191212102259.418536-8-jakub@cloudflare.com
parent ce7cb5f3
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
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;
static struct bpf_object *obj; static struct bpf_object *obj;
...@@ -55,7 +55,16 @@ static union sa46 { ...@@ -55,7 +55,16 @@ static union sa46 {
} \ } \
}) })
static void create_maps(void) #define RET_ERR(condition, tag, format...) ({ \
int __ret = !!(condition); \
if (__ret) { \
printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \
printf(format); \
return -1; \
} \
})
static int create_maps(void)
{ {
struct bpf_create_map_attr attr = {}; struct bpf_create_map_attr attr = {};
...@@ -67,8 +76,8 @@ static void create_maps(void) ...@@ -67,8 +76,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,57 +87,61 @@ static void create_maps(void) ...@@ -78,57 +87,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;
obj = bpf_object__open("test_select_reuseport_kern.o"); 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));
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); prog = bpf_program__next(NULL, obj);
CHECK(!prog, "get first bpf_program", "!prog\n"); 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)
...@@ -157,31 +170,34 @@ static int read_int_sysctl(const char *sysctl) ...@@ -157,31 +170,34 @@ 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)
...@@ -190,22 +206,25 @@ static void restore_sysctls(void) ...@@ -190,22 +206,25 @@ static void restore_sysctls(void)
write_int_sysctl(TCP_SYNCOOKIE_SYSCTL, saved_tcp_syncookie); 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 __u32 get_linum(void)
...@@ -683,9 +702,12 @@ static void cleanup_per_test(bool no_inner_map) ...@@ -683,9 +702,12 @@ static void cleanup_per_test(bool no_inner_map)
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 const char *family_str(sa_family_t family) static const char *family_str(sa_family_t family)
...@@ -765,16 +787,28 @@ static void test_all(void) ...@@ -765,16 +787,28 @@ static void test_all(void)
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
create_maps(); int ret = EXIT_FAILURE;
prepare_bpf_obj();
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); atexit(restore_sysctls);
if (enable_fastopen())
goto out;
if (disable_syncookie())
goto out;
test_all(); test_all();
ret = EXIT_SUCCESS;
out:
cleanup(); cleanup();
return 0; return ret;
} }
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