This test was a bit obtuse before, add a shorter timeout when connectivity doesn't work and a '-d' debug flag for extra output. Signed-off-by: Joe Stringer <joe@xxxxxxxxxxx> --- tools/testing/selftests/bpf/test_sk_assign.c | 42 +++++++++++++++++++ tools/testing/selftests/bpf/test_sk_assign.sh | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_sk_assign.c b/tools/testing/selftests/bpf/test_sk_assign.c index 4b7b9bbe7859..51d3d01d5476 100644 --- a/tools/testing/selftests/bpf/test_sk_assign.c +++ b/tools/testing/selftests/bpf/test_sk_assign.c @@ -3,6 +3,8 @@ // Copyright (c) 2019 Cloudflare // Copyright (c) 2020 Isovalent. Inc. +#include <fcntl.h> +#include <signal.h> #include <string.h> #include <stdlib.h> #include <unistd.h> @@ -20,6 +22,14 @@ #define TEST_DADDR (0xC0A80203) +static bool debug; + +#define debugf(format, ...) \ +do { \ + if (debug) \ + printf(format, ##__VA_ARGS__); \ +} while (0) + static int start_server(const struct sockaddr *addr, socklen_t len) { int fd; @@ -49,6 +59,17 @@ static int start_server(const struct sockaddr *addr, socklen_t len) return fd; } +static void handle_timeout(int signum) +{ + if (signum == SIGALRM) + log_err("Timed out while connecting to server"); + kill(0, SIGKILL); +} + +static struct sigaction timeout_action = { + .sa_handler = handle_timeout, +}; + static int connect_to_server(const struct sockaddr *addr, socklen_t len) { int fd = -1; @@ -59,6 +80,12 @@ static int connect_to_server(const struct sockaddr *addr, socklen_t len) goto out; } + if (sigaction(SIGALRM, &timeout_action, NULL)) { + log_err("Failed to configure timeout signal"); + goto out; + } + + alarm(3); if (connect(fd, addr, len) == -1) { log_err("Fail to connect to server"); goto close_out; @@ -141,6 +168,17 @@ int main(int argc, char **argv) int server_v6 = -1; int err = 1; + if (argc > 1) { + if (!memcmp(argv[1], "-h", 2)) { + printf("usage: %s.sh [FLAGS]\n", argv[0]); + printf(" -d\tEnable debug logs\n"); + printf(" -h\tPrint help message\n"); + exit(1); + } + if (!memcmp(argv[1], "-d", 2)) + debug = true; + } + memset(&addr4, 0, sizeof(addr4)); addr4.sin_family = AF_INET; addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK); @@ -166,9 +204,11 @@ int main(int argc, char **argv) if (run_test(server, (const struct sockaddr *)&addr4, sizeof(addr4))) goto out; + debugf("ipv4 port: ok\n"); if (run_test(server_v6, (const struct sockaddr *)&addr6, sizeof(addr6))) goto out; + debugf("ipv6 port: ok\n"); /* Connect to unbound addresses */ addr4.sin_addr.s_addr = htonl(TEST_DADDR); @@ -176,9 +216,11 @@ int main(int argc, char **argv) if (run_test(server, (const struct sockaddr *)&addr4, sizeof(addr4))) goto out; + debugf("ipv4 addr: ok\n"); if (run_test(server_v6, (const struct sockaddr *)&addr6, sizeof(addr6))) goto out; + debugf("ipv6 addr: ok\n"); printf("ok\n"); err = 0; diff --git a/tools/testing/selftests/bpf/test_sk_assign.sh b/tools/testing/selftests/bpf/test_sk_assign.sh index de1df4e438de..5a84ad18f85a 100755 --- a/tools/testing/selftests/bpf/test_sk_assign.sh +++ b/tools/testing/selftests/bpf/test_sk_assign.sh @@ -19,4 +19,4 @@ tc qdisc add dev lo clsact tc filter add dev lo ingress bpf direct-action object-file ./test_sk_assign.o \ section "sk_assign_test" -exec ./test_sk_assign +exec ./test_sk_assign "$@" -- 2.20.1