[CRIU] [PATCH 15/21] zdtm: add test cases on TCP_CLOSE_WAIT and TCP_LAST_ACK states

Dmitry Safonov 0x7f454c46 at gmail.com
Thu Jan 19 06:58:56 PST 2017


Hi Andrei,

2016-11-28 22:27 GMT+03:00 Andrei Vagin <avagin at openvz.org>:
> From: Andrey Vagin <avagin at openvz.org>
>
> TCP_CLOSE_WAIT remote side has shutdown and is waiting for
>                us to finish writing our data and to shutdown
>                (we have to close() to move on to LAST_ACK)
>
> TCP_LAST_ACK   out side has shutdown after remote has
>                shutdown.  There may still be data in our
>                buffer that we have to finish sending
>
> Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
> ---
>  test/zdtm/static/Makefile                    |   8 +
>  test/zdtm/static/socket-tcp-close-wait.c     | 266 +++++++++++++++++++++++++++
>  test/zdtm/static/socket-tcp-close-wait.desc  |   1 +
>  test/zdtm/static/socket-tcp-last-ack.c       |   1 +
>  test/zdtm/static/socket-tcp-last-ack.desc    |   1 +
>  test/zdtm/static/socket-tcp6-close-wait.c    |   1 +
>  test/zdtm/static/socket-tcp6-close-wait.desc |   1 +
>  test/zdtm/static/socket-tcp6-last-ack.c      |   1 +
>  test/zdtm/static/socket-tcp6-last-ack.desc   |   1 +
>  9 files changed, 281 insertions(+)
>  create mode 100644 test/zdtm/static/socket-tcp-close-wait.c
>  create mode 100644 test/zdtm/static/socket-tcp-close-wait.desc
>  create mode 120000 test/zdtm/static/socket-tcp-last-ack.c
>  create mode 100644 test/zdtm/static/socket-tcp-last-ack.desc
>  create mode 120000 test/zdtm/static/socket-tcp6-close-wait.c
>  create mode 120000 test/zdtm/static/socket-tcp6-close-wait.desc
>  create mode 120000 test/zdtm/static/socket-tcp6-last-ack.c
>  create mode 120000 test/zdtm/static/socket-tcp6-last-ack.desc
>
> diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
> index 12d74dd..43c3278 100644
> --- a/test/zdtm/static/Makefile
> +++ b/test/zdtm/static/Makefile
> @@ -77,6 +77,10 @@ TST_NOFILE   :=                              \
>                 socket-tcp6-fin-wait1           \
>                 socket-tcp-fin-wait2            \
>                 socket-tcp6-fin-wait2           \
> +               socket-tcp-close-wait           \
> +               socket-tcp6-close-wait          \
> +               socket-tcp-last-ack             \
> +               socket-tcp6-last-ack            \
>                 sock_opts00                     \
>                 sock_opts01                     \
>                 sk-unix-unconn                  \
> @@ -424,6 +428,10 @@ socket-tcp-fin-wait1:      override CFLAGS += -D ZDTM_TCP_FIN_WAIT1
>  socket-tcp-fin-wait2:  override CFLAGS += -D ZDTM_TCP_FIN_WAIT2
>  socket-tcp6-fin-wait1: override CFLAGS += -D ZDTM_TCP_FIN_WAIT1 -D ZDTM_IPV6
>  socket-tcp6-fin-wait2: override CFLAGS += -D ZDTM_TCP_FIN_WAIT2 -D ZDTM_IPV6
> +socket-tcp-close-wait: override CFLAGS += -D ZDTM_TCP_CLOSE_WAIT
> +socket-tcp6-close-wait:        override CFLAGS += -D ZDTM_TCP_CLOSE_WAIT -D ZDTM_IPV6
> +socket-tcp-last-ack:   override CFLAGS += -D ZDTM_TCP_LAST_ACK
> +socket-tcp6-last-ack:  override CFLAGS += -D ZDTM_TCP_LAST_ACK -D ZDTM_IPV6
>
>  $(LIB):        force
>         $(Q) $(MAKE) -C $(LIBDIR)
> diff --git a/test/zdtm/static/socket-tcp-close-wait.c b/test/zdtm/static/socket-tcp-close-wait.c
> new file mode 100644
> index 0000000..3b2681c
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp-close-wait.c
> @@ -0,0 +1,266 @@
> +#include "zdtmtst.h"
> +
> +#ifdef ZDTM_IPV6
> +#define ZDTM_FAMILY AF_INET6
> +#else
> +#define ZDTM_FAMILY AF_INET
> +#endif
> +
> +const char *test_doc = "Check sockets in TCP_WAIT_STOP and TCP_LAST_ACK states\n";
> +const char *test_author = "Andrey Vagin <avagin at parallels.com";
> +
> +#include <stdio.h>
> +#include <sys/types.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <errno.h>
> +#include <stdlib.h>
> +#include <signal.h>
> +#include <fcntl.h>
> +#include <sys/socket.h>
> +#include <netinet/tcp.h>
> +
> +static int port = 8880;
> +
> +#define BUF_SIZE 4096
> +
> +int fill_sock_buf(int fd)
> +{
> +       int flags;
> +       int size;
> +       int ret;
> +
> +       flags = fcntl(fd, F_GETFL, 0);
> +       if (flags == -1) {
> +               pr_err("Can't get flags");
> +               return -1;
> +       }
> +       if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
> +               pr_err("Can't set flags");
> +               return -1;
> +       }
> +
> +       size = 0;
> +       while (1) {
> +               char zdtm[] = "zdtm test packet";
> +               ret = write(fd, zdtm, sizeof(zdtm));
> +               if (ret == -1) {
> +                       if (errno == EAGAIN)
> +                               break;
> +                       pr_err("write");
> +                       return -1;
> +               }
> +               size += ret;
> +       }
> +
> +       if (fcntl(fd, F_SETFL, flags) == -1) {
> +               pr_err("Can't set flags");
> +               return -1;
> +       }
> +
> +       test_msg("snd_size = %d\n", size);
> +
> +       return size;
> +}
> +
> +static int clean_sk_buf(int fd)
> +{
> +       int size, ret;
> +       char buf[BUF_SIZE];
> +
> +       size = 0;
> +       while (1) {
> +               ret = read(fd, buf, sizeof(buf));
> +               if (ret == -1) {
> +                       pr_err("read");
> +                       return -11;
> +               }
> +
> +               if (ret == 0)
> +                       break;
> +
> +               size += ret;
> +       }
> +
> +       test_msg("rcv_size = %d\n", size);
> +
> +       return size;
> +}
> +
> +#define TEST_MSG "Hello World!"
> +
> +int main(int argc, char **argv)
> +{
> +       char *newns = getenv("ZDTM_NEWNS");
> +       int fd, fd_s, ctl_fd;
> +       pid_t extpid;
> +       int pfd[2];
> +       int ret, snd_size = 0, rcv_size = 0;
> +#ifndef ZDTM_TCP_LAST_ACK
> +       char buf[BUF_SIZE];
> +#endif
> +
> +       if (newns)
> +               test_init(argc, argv);
> +
> +       if (pipe(pfd)) {
> +               pr_err("pipe() failed");
> +               return 1;
> +       }
> +
> +       extpid = fork();
> +       if (extpid < 0) {
> +               pr_err("fork() failed");
> +               return 1;
> +       } else if (extpid == 0) {
> +               int size = 0;
> +               char c;
> +
> +               if (!newns)
> +                       test_ext_init(argc, argv);
> +
> +               close(pfd[1]);
> +               if (read(pfd[0], &port, sizeof(port)) != sizeof(port)) {
> +                       pr_err("Can't read port\n");
> +                       return 1;
> +               }

close(pfd[0]);

> +
> +               fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
> +               if (fd < 0)
> +                       return 1;
> +
> +               ctl_fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
> +               if (fd < 0)
> +                       return 1;
> +
> +               if (read(ctl_fd, &size, sizeof(size)) != sizeof(size)) {
> +                       pr_err("write");

pr_err("read");

> +                       return 1;
> +               }
> +
> +               if (shutdown(fd, SHUT_WR) == -1) {
> +                       pr_err("shutdown");
> +                       return 1;
> +               }
> +
> +               if (write(ctl_fd, &size, sizeof(size)) != sizeof(size)) {
> +                       pr_err("write");
> +                       return 1;
> +               }
> +
> +               if (read(ctl_fd, &c, 1) != 0) {
> +                       pr_err("read");
> +                       return 1;
> +               }
> +
> +#ifdef ZDTM_TCP_LAST_ACK
> +               size = clean_sk_buf(fd);
> +               if (size < 0)
> +                       return 1;
> +#else
> +               if (read(fd, buf, sizeof(buf)) != sizeof(TEST_MSG) ||
> +                   strncmp(buf, TEST_MSG, sizeof(TEST_MSG))) {
> +                       pr_err("read");
> +                       return 1;
> +               }
> +#endif
> +
> +               write(ctl_fd, &size, sizeof(size));
> +               close(fd);
> +
> +               return 0;
> +       }
> +
> +       if (!newns)
> +               test_init(argc, argv);
> +
> +       if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
> +               pr_err("initializing server failed");
> +               return 1;
> +       }
> +
> +       close(pfd[0]);
> +       if (write(pfd[1], &port, sizeof(port)) != sizeof(port)) {
> +               pr_err("Can't send port");
> +               return 1;
> +       }
> +       close(pfd[1]);
> +
> +       /*
> +        * parent is server of TCP connection
> +        */
> +       fd = tcp_accept_server(fd_s);
> +       if (fd < 0) {
> +               pr_err("can't accept client connection %m");
> +               return 1;
> +       }
> +
> +       ctl_fd = tcp_accept_server(fd_s);
> +       if (ctl_fd < 0) {
> +               pr_err("can't accept client connection %m");
> +               return 1;
> +       }
> +
> +#ifdef ZDTM_TCP_LAST_ACK
> +       snd_size = fill_sock_buf(fd);
> +       if (snd_size <= 0)
> +               return 1;
> +#endif
> +
> +       if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {

should possibly write snd_size here, not uninitialized ret.

> +               pr_err("read");
> +               return 1;
> +       }
> +
> +       if (read(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
> +               pr_err("read");
> +               return 1;
> +       }
> +
> +#ifdef ZDTM_TCP_LAST_ACK
> +       if (shutdown(fd, SHUT_WR) == -1) {
> +               pr_err("shutdown");
> +               return 1;
> +       }
> +#endif
> +
> +       test_daemon();
> +       test_waitsig();
> +
> +       if (shutdown(ctl_fd, SHUT_WR) == -1) {
> +               pr_err("shutdown");
> +               return 1;
> +       }
> +
> +#ifndef ZDTM_TCP_LAST_ACK
> +       if (write(fd, TEST_MSG, sizeof(TEST_MSG)) != sizeof(TEST_MSG)) {
> +               pr_err("write");
> +               return 1;
> +       }
> +
> +       if (shutdown(fd, SHUT_WR) == -1) {
> +               pr_err("shutdown");
> +               return 1;
> +       }
> +#endif
> +
> +       rcv_size = clean_sk_buf(fd);
> +
> +       if (ret != rcv_size) {
> +               fail("The child sent %d bytes, but the parent received %d bytes\n", ret, rcv_size);
> +               return 1;
> +       }
> +
> +       if (read(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
> +               pr_err("read");
> +               return 1;
> +       }
> +
> +       if (ret != snd_size) {
> +               fail("The parent sent %d bytes, but the child received %d bytes\n", snd_size, ret);
> +               return 1;
> +       }
> +
> +       pass();
> +       return 0;
> +}
> diff --git a/test/zdtm/static/socket-tcp-close-wait.desc b/test/zdtm/static/socket-tcp-close-wait.desc
> new file mode 100644
> index 0000000..155e103
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp-close-wait.desc
> @@ -0,0 +1 @@
> +{'opts': '--tcp-established', 'flags': 'nouser samens', 'feature' : 'tcp_half_closed'}
> diff --git a/test/zdtm/static/socket-tcp-last-ack.c b/test/zdtm/static/socket-tcp-last-ack.c
> new file mode 120000
> index 0000000..20d7e78
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp-last-ack.c
> @@ -0,0 +1 @@
> +socket-tcp-close-wait.c
> \ No newline at end of file
> diff --git a/test/zdtm/static/socket-tcp-last-ack.desc b/test/zdtm/static/socket-tcp-last-ack.desc
> new file mode 100644
> index 0000000..a0ff359
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp-last-ack.desc
> @@ -0,0 +1 @@
> +{'flavor': 'h', 'opts': '--tcp-established', 'flags': 'nouser samens', 'feature' : 'tcp_half_closed'}
> diff --git a/test/zdtm/static/socket-tcp6-close-wait.c b/test/zdtm/static/socket-tcp6-close-wait.c
> new file mode 120000
> index 0000000..20d7e78
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp6-close-wait.c
> @@ -0,0 +1 @@
> +socket-tcp-close-wait.c
> \ No newline at end of file
> diff --git a/test/zdtm/static/socket-tcp6-close-wait.desc b/test/zdtm/static/socket-tcp6-close-wait.desc
> new file mode 120000
> index 0000000..df1973c
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp6-close-wait.desc
> @@ -0,0 +1 @@
> +socket-tcp-close-wait.desc
> \ No newline at end of file
> diff --git a/test/zdtm/static/socket-tcp6-last-ack.c b/test/zdtm/static/socket-tcp6-last-ack.c
> new file mode 120000
> index 0000000..1f0bedf
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp6-last-ack.c
> @@ -0,0 +1 @@
> +socket-tcp-last-ack.c
> \ No newline at end of file
> diff --git a/test/zdtm/static/socket-tcp6-last-ack.desc b/test/zdtm/static/socket-tcp6-last-ack.desc
> new file mode 120000
> index 0000000..caace98
> --- /dev/null
> +++ b/test/zdtm/static/socket-tcp6-last-ack.desc
> @@ -0,0 +1 @@
> +socket-tcp-last-ack.desc
> \ No newline at end of file
> --
> 2.7.4
>

-- 
             Dmitry


More information about the CRIU mailing list