From cd1570b15ee0b0fea89f3a8651640b24117d513a Mon Sep 17 00:00:00 2001 From: Zeyad Yasser Date: Thu, 29 Jul 2021 20:18:21 +0200 Subject: [PATCH] zdtm: add ipv6 variants of net_lock_socket_* tests v2: remove unnecessary elif and else after return in wait_server_addr() v3: use IOError instead of FileNotFoundError for python2 compatibility Signed-off-by: Zeyad Yasser --- test/zdtm/static/Makefile | 4 +++ test/zdtm/static/net_lock_socket_iptables.c | 25 +++++++++++++++---- .../zdtm/static/net_lock_socket_iptables.hook | 22 +++++++++++----- test/zdtm/static/net_lock_socket_iptables6.c | 1 + .../static/net_lock_socket_iptables6.desc | 5 ++++ .../static/net_lock_socket_iptables6.hook | 1 + test/zdtm/static/net_lock_socket_nftables6.c | 1 + .../static/net_lock_socket_nftables6.desc | 6 +++++ .../static/net_lock_socket_nftables6.hook | 1 + 9 files changed, 55 insertions(+), 11 deletions(-) create mode 120000 test/zdtm/static/net_lock_socket_iptables6.c create mode 100644 test/zdtm/static/net_lock_socket_iptables6.desc create mode 120000 test/zdtm/static/net_lock_socket_iptables6.hook create mode 120000 test/zdtm/static/net_lock_socket_nftables6.c create mode 100644 test/zdtm/static/net_lock_socket_nftables6.desc create mode 120000 test/zdtm/static/net_lock_socket_nftables6.hook diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile index ec408c4ea..c9e6589f0 100644 --- a/test/zdtm/static/Makefile +++ b/test/zdtm/static/Makefile @@ -220,7 +220,9 @@ TST_NOFILE := \ netns_lock_iptables \ netns_lock_nftables \ net_lock_socket_iptables \ + net_lock_socket_iptables6 \ net_lock_socket_nftables \ + net_lock_socket_nftables6 \ netns_sub \ netns_sub_veth \ netns_sub_sysctl \ @@ -583,6 +585,8 @@ clone_fs: LDLIBS += -pthread # we have to explicitly specify both .o and .d for this case: netns_sub_veth.o netns_sub_veth.d: CPPFLAGS += $(call pkg-cflags, libnl-3.0) netns_sub_veth: LDLIBS += $(call pkg-libs, libnl-route-3.0 libnl-3.0) +net_lock_socket_iptables6: CFLAGS += -D ZDTM_IPV6 +net_lock_socket_nftables6: CFLAGS += -D ZDTM_IPV6 symlink01: CFLAGS += -DZDTM_UNLINK_SYMLINK socket-tcp-fin-wait1: CFLAGS += -D ZDTM_TCP_FIN_WAIT1 diff --git a/test/zdtm/static/net_lock_socket_iptables.c b/test/zdtm/static/net_lock_socket_iptables.c index cc30bc2f9..d9c438c4c 100644 --- a/test/zdtm/static/net_lock_socket_iptables.c +++ b/test/zdtm/static/net_lock_socket_iptables.c @@ -1,5 +1,11 @@ #include "zdtmtst.h" +#if defined(ZDTM_IPV6) +#define ZDTM_FAMILY AF_INET6 +#else +#define ZDTM_FAMILY AF_INET +#endif + const char *test_doc = "Check that sockets are locked between dump and restore\n"; const char *test_author = "Zeyad Yasser "; @@ -14,22 +20,31 @@ static int port = 8880; int main(int argc, char **argv) { char buf[5]; - int fd_s, fd_sock, fd_sync, buf_len; + int fd_s, fd_sock, buf_len; + FILE *f_sync; test_init(argc, argv); - if ((fd_s = tcp_init_server(AF_INET, &port)) < 0) { + if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) { pr_err("initializing server failed\n"); return 1; } // Server is ready to accept sockets - fd_sync = open(SYNCFILE_PATH, O_CREAT, 0444); - if (fd_sync == -1) { + f_sync = fopen(SYNCFILE_PATH, "w"); + if (f_sync == NULL) { pr_perror("cannot create sync file"); return 1; } - close(fd_sync); +#if defined(ZDTM_IPV6) + if (fprintf(f_sync, "ipv6") < 0) { +#else + if (fprintf(f_sync, "ipv4") < 0) { +#endif + pr_perror("cannot write to sync file"); + return 1; + } + fclose(f_sync); fd_sock = tcp_accept_server(fd_s); diff --git a/test/zdtm/static/net_lock_socket_iptables.hook b/test/zdtm/static/net_lock_socket_iptables.hook index 22baf7b3c..0ee147eb2 100755 --- a/test/zdtm/static/net_lock_socket_iptables.hook +++ b/test/zdtm/static/net_lock_socket_iptables.hook @@ -10,12 +10,22 @@ TIMEOUT = 0.1 INTERNAL_SERVER = "\0internal_server" SYNCFILE = "zdtm/static/socket_lock.sync" -def wait_sync_file(): +def wait_server_addr(): for _ignore in range(3): - if os.path.exists(SYNCFILE): + try: + with open(SYNCFILE, "r") as f: + addr = f.read(4) os.remove(SYNCFILE) - return - time.sleep(1) + + if addr == "ipv4": + return "127.0.0.1" + + if addr == "ipv6": + return "::1" + + raise Exception("Invalid address type") + except IOError: + time.sleep(1) raise TimeoutError("Sync timeout: file ({}) not found".format(SYNCFILE)) @@ -25,13 +35,13 @@ if sys.argv[1] == "--post-start": internal_sock.listen(1) # Wait for test server to be ready - wait_sync_file() + server_addr = wait_server_addr() pid = os.fork() if pid == 0: os.setsid() # Detach from parent - test_sock = socket.create_connection(("127.0.0.1", PORT), TIMEOUT) + test_sock = socket.create_connection((server_addr, PORT), TIMEOUT) while True: internal_conn, _ignore = internal_sock.accept() diff --git a/test/zdtm/static/net_lock_socket_iptables6.c b/test/zdtm/static/net_lock_socket_iptables6.c new file mode 120000 index 000000000..8e229da81 --- /dev/null +++ b/test/zdtm/static/net_lock_socket_iptables6.c @@ -0,0 +1 @@ +net_lock_socket_iptables.c \ No newline at end of file diff --git a/test/zdtm/static/net_lock_socket_iptables6.desc b/test/zdtm/static/net_lock_socket_iptables6.desc new file mode 100644 index 000000000..7231886da --- /dev/null +++ b/test/zdtm/static/net_lock_socket_iptables6.desc @@ -0,0 +1,5 @@ +{ + 'flavor': 'h', + 'flags': 'suid excl', + 'opts': '--tcp-established --network-lock iptables', +} diff --git a/test/zdtm/static/net_lock_socket_iptables6.hook b/test/zdtm/static/net_lock_socket_iptables6.hook new file mode 120000 index 000000000..ecf65f7c4 --- /dev/null +++ b/test/zdtm/static/net_lock_socket_iptables6.hook @@ -0,0 +1 @@ +net_lock_socket_iptables.hook \ No newline at end of file diff --git a/test/zdtm/static/net_lock_socket_nftables6.c b/test/zdtm/static/net_lock_socket_nftables6.c new file mode 120000 index 000000000..8e229da81 --- /dev/null +++ b/test/zdtm/static/net_lock_socket_nftables6.c @@ -0,0 +1 @@ +net_lock_socket_iptables.c \ No newline at end of file diff --git a/test/zdtm/static/net_lock_socket_nftables6.desc b/test/zdtm/static/net_lock_socket_nftables6.desc new file mode 100644 index 000000000..fd8a431ed --- /dev/null +++ b/test/zdtm/static/net_lock_socket_nftables6.desc @@ -0,0 +1,6 @@ +{ + 'flavor': 'h', + 'flags': 'suid excl', + 'feature': 'network_lock_nftables', + 'opts': '--tcp-established --network-lock nftables', +} diff --git a/test/zdtm/static/net_lock_socket_nftables6.hook b/test/zdtm/static/net_lock_socket_nftables6.hook new file mode 120000 index 000000000..ecf65f7c4 --- /dev/null +++ b/test/zdtm/static/net_lock_socket_nftables6.hook @@ -0,0 +1 @@ +net_lock_socket_iptables.hook \ No newline at end of file