All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pavel Boldin <pboldin@mirantis.com>
To: dev@dpdk.org
Subject: [PATCH v5 4/4] DO NOT MERGE: Tests for new eventfd_link module
Date: Fri, 28 Aug 2015 21:51:20 +0300	[thread overview]
Message-ID: <1440787880-7079-4-git-send-email-pboldin@mirantis.com> (raw)
In-Reply-To: <1440787880-7079-1-git-send-email-pboldin@mirantis.com>
In-Reply-To: <1429184910-30186-2-git-send-email-pboldin@mirantis.com>

To use:
1. Compile and load the new eventfd_link module (as root):
 # (cd lib/librte_vhost/eventfd_link; make; insmod ./eventfd_link.ko)
2. Compile the test program:
 $ make -C test_eventfd_copy
3. Run it as root:
 # sudo ./test_eventfd_copy/test_eventfd_copy --check
 Stealing FD OK
---
 test_eventfd_copy/Makefile            |   8 ++
 test_eventfd_copy/rte_log.h           |   2 +
 test_eventfd_copy/test_eventfd_copy.c | 248 ++++++++++++++++++++++++++++++++++
 test_eventfd_copy/vhost-net.h         |   0
 4 files changed, 258 insertions(+)
 create mode 100644 test_eventfd_copy/Makefile
 create mode 100644 test_eventfd_copy/rte_log.h
 create mode 100644 test_eventfd_copy/test_eventfd_copy.c
 create mode 100644 test_eventfd_copy/vhost-net.h

diff --git a/test_eventfd_copy/Makefile b/test_eventfd_copy/Makefile
new file mode 100644
index 0000000..99b3ae3
--- /dev/null
+++ b/test_eventfd_copy/Makefile
@@ -0,0 +1,8 @@
+
+
+test_eventfd_copy: test_eventfd_copy.c \
+                   ../lib/librte_vhost/vhost_cuse/eventfd_copy.c
+	gcc -o $@ $^ \
+	    -I . \
+	    -I ../lib/librte_vhost/ \
+            -I ../lib/librte_vhost/vhost_cuse/
diff --git a/test_eventfd_copy/rte_log.h b/test_eventfd_copy/rte_log.h
new file mode 100644
index 0000000..ffc64c9
--- /dev/null
+++ b/test_eventfd_copy/rte_log.h
@@ -0,0 +1,2 @@
+
+#define RTE_LOG(...)
diff --git a/test_eventfd_copy/test_eventfd_copy.c b/test_eventfd_copy/test_eventfd_copy.c
new file mode 100644
index 0000000..9dfa414
--- /dev/null
+++ b/test_eventfd_copy/test_eventfd_copy.c
@@ -0,0 +1,248 @@
+/*-
+ *   BSD LICENSE
+ *   Copyright(c) 2015 Mirantis Inc. All rights reserved.
+ *   Based on `eventfd_copy.c` from the Intel's DPDK
+
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <unistd.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "eventfd_copy.h"
+
+static void
+usage()
+{
+	fprintf(stderr,
+"test_eventfd_copy (--file-nr [number] | --check)\n"
+"\t--file-nr\tchecks that there is no `struct file' leakage by ensuring that\n"
+"\t\t\t `/proc/sys/fs/file-nr' value is not growing while `eventfd_copy'ing\n"
+"\t\t\t number times (default is 100000)\n"
+"\t--check\t\tchecks that the `fd' is moved correctly\n");
+}
+
+static int
+read_proc_file_nr()
+{
+	int fd, ret;
+	char buf[1024];
+	fd = open("/proc/sys/fs/file-nr", O_RDONLY);
+	if (fd < 0) {
+		perror("open file-nr");
+		return -1;
+	}
+
+	if (read(fd, buf, 1024) < 0) {
+		perror("read");
+		close(fd);
+		return -1;
+	}
+
+	ret = atoi(buf);
+
+	close(fd);
+
+	return ret;
+}
+
+static int
+check_file_nr(int count)
+{
+        pid_t pid;
+	int i, fd;
+
+	pid = getpid();
+
+	fprintf(stderr, "dummy eventfd_copy check for %d file(s)\n", count);
+	fprintf(stdout, "file-nr before: %d\n", read_proc_file_nr());
+        for (i = 0; i < count; ++i) {
+                fd = eventfd_copy(2, pid);
+                if (fd < 0)
+			return 1;
+                close(fd);
+        }
+	fprintf(stdout, "file-nr after: %d\n", read_proc_file_nr());
+
+	return 0;
+}
+
+
+#define FD_TO_LINK 42
+#define SALT	"The Life, the Universe and everything"
+
+/* checks link:
+   0. opens a pipe
+   1. forks
+   2. child opens tempfile
+   3. child dups tempfile fd to fd=42
+   4. child writes salt to the tempfile
+   5. child notifies parent by writing to a pipe
+   6. parent steals child fd
+   7. parent kills child
+   8. parent checks file content
+   9. parent removes tempfile
+ */
+static int
+check_link(void)
+{
+	pid_t cpid;
+	int pipefd[2];
+	char buf;
+
+	if (pipe(pipefd) < 0) {
+		perror("pipe");
+		return 1;
+	}
+
+	cpid = fork();
+	if (cpid == -1) {
+		perror("fork");
+		return 1;
+	}
+
+	if (cpid) {
+		int stolen_fd, ret = 1;
+		char buf[1024], tmpfname[1024] = "";
+
+		close(pipefd[1]);
+
+		if ((ret = read(pipefd[0], tmpfname, 1024)) < 0) {
+			perror("read pipefd");
+			goto parent_out;
+		}
+		close(pipefd[0]);
+
+		stolen_fd = eventfd_copy(FD_TO_LINK, cpid);
+		if (stolen_fd < 0) {
+			goto parent_out;
+		}
+
+		if (lseek(stolen_fd, 0, SEEK_SET) < 0) {
+			perror("lseek");
+			goto parent_out;
+		}
+
+		if (read(stolen_fd, buf, 1024) < 0) {
+			perror("read stolen salt");
+			goto parent_out;
+		}
+
+		if (strcmp(buf, SALT)) {
+			fprintf(stdout, "Stealing FD failed\n");
+		}
+		else {
+			fprintf(stdout, "Stealing FD OK\n");
+		}
+
+		close(stolen_fd);
+
+		ret = 0;
+parent_out:
+		if (tmpfname[0])
+			unlink(tmpfname);
+		kill(cpid, SIGKILL);
+		wait(NULL);
+		return ret;
+	}
+
+	if (cpid == 0) {
+		int fd;
+		char fname[] = "/tmp/linkXXXXXX";
+
+		close(pipefd[0]);
+
+		fd = mkstemp(fname);
+		if (fd < 0) {
+			perror("mkstemp");
+			return 1;
+		}
+
+		if (dup2(fd, FD_TO_LINK) < 0) {
+			perror("dup2");
+			close(fd);
+			return 1;
+		}
+
+		close(fd);
+
+		if (write(FD_TO_LINK, (void*)SALT, strlen(SALT)) < 0) {
+			perror("write salt");
+			return 1;
+		}
+
+		fsync(FD_TO_LINK);
+
+		if (write(pipefd[1], fname, strlen(fname)) < 0) {
+			perror("write pipe");
+			return 1;
+		}
+
+		close(pipefd[1]);
+
+		while (1) {
+			sleep(1);
+		}
+
+		return 0;
+	}
+}
+
+int
+main(int argc, const char** argv)
+{
+	if (argc < 2) {
+		usage();
+		return 0;
+	}
+
+	if (!strcmp(argv[1], "--file-nr")) {
+		int count = 100000;
+		if (argc >= 3)
+			count = atoi(argv[2]);
+		return check_file_nr(count);
+	}
+
+	if (!strcmp(argv[1], "--check")) {
+		return check_link();
+	}
+
+err:
+	usage();
+	return 1;
+}
diff --git a/test_eventfd_copy/vhost-net.h b/test_eventfd_copy/vhost-net.h
new file mode 100644
index 0000000..e69de29
-- 
1.9.1

  parent reply	other threads:[~2015-08-28 18:51 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-16 16:40 [PATCH] Fix `eventfd_link' module leakages and races Pavel Boldin
     [not found] ` <1426524059-30886-1-git-send-email-pboldin+dpdk-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-03-16 17:55   ` Neil Horman
2015-03-17  1:29   ` Ouyang, Changchun
     [not found]     ` <F52918179C57134FAEC9EA62FA2F962511A5E173-E2R4CRU6q/6iAffOGbnezLfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-03-20 15:57       ` Pavel Boldin
2015-03-18 13:16   ` [PATCH v2] " Pavel Boldin
     [not found]     ` <1426684571-14782-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-03-23 11:15       ` Thomas Monjalon
2015-03-23 11:36         ` Pavel Boldin
     [not found]           ` <CACf4_B8buRSAPCFO+fWJjQ8it5n0cgN789jSrsB6Bc-cJGOyHQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-03-23 11:44             ` Thomas Monjalon
2015-03-23 15:15       ` [PATCH v3] vhost: Refactor module `eventfd_link' Pavel Boldin
     [not found]         ` <1427123731-15654-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-02 17:01           ` [PATCH v4 0/5] " Pavel Boldin
     [not found]             ` <1427994080-10163-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-02 17:01               ` [PATCH v4 1/5] vhost: eventfd_link: moving ioctl to a function Pavel Boldin
2015-05-07  7:57                 ` Xie, Huawei
     [not found]                   ` <C37D651A908B024F974696C65296B57B0F476887-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2015-05-07 13:16                     ` Pavel Boldin
2015-05-18  6:06                       ` Xie, Huawei
2015-05-18  9:16                         ` Pavel Boldin
2015-06-18  3:08                       ` Xie, Huawei
2015-04-02 17:01               ` [PATCH v4 2/5] vhost: eventfd_link: add function fget_from_files Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 3/5] vhost: eventfd_link: fix ioctl return values Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 4/5] vhost: eventfd_link: replace copy-pasted sys_close Pavel Boldin
2015-04-02 17:01               ` [PATCH v4 5/5] vhost: eventfd_link: removing extra #includes Pavel Boldin
2015-04-16 11:48               ` [PATCH v5 0/5] Refactor module `eventfd_link' Pavel Boldin
     [not found]                 ` <1429184910-30186-1-git-send-email-pboldin-nYU0QVwCCFFWk0Htik3J/w@public.gmane.org>
2015-04-16 11:48                   ` [PATCH v5 1/5] vhost: eventfd_link: moving ioctl to a function Pavel Boldin
2015-08-28 18:51                     ` [PATCH v5 1/4] vhost: eventfd_link: refactoring EVENTFD_COPY handler Pavel Boldin
2015-09-23 20:25                       ` Pavel Boldin
2015-09-29 19:42                         ` Thomas Monjalon
2015-09-29 23:29                           ` Pavel Boldin
2015-10-20  9:06                       ` Xie, Huawei
2015-10-28 18:33                       ` [PATCH v6 0/3] vhost: eventfd_link refactoring Pavel Boldin
2015-10-29 18:33                         ` Xie, Huawei
2015-10-30 19:10                           ` Thomas Monjalon
2015-10-28 18:33                       ` [PATCH v6 1/3] vhost: eventfd_link: refactoring EVENTFD_COPY handler Pavel Boldin
2015-10-28 18:33                       ` [PATCH v6 2/3] vhost: add EVENTFD_COPY2 ioctl Pavel Boldin
2015-10-28 18:33                       ` [PATCH v6 3/3] vhost: using EVENTFD_COPY2 Pavel Boldin
2015-08-28 18:51                     ` [PATCH v5 2/4] vhost: add EVENTFD_COPY2 ioctl Pavel Boldin
2015-10-20  9:43                       ` Xie, Huawei
2015-08-28 18:51                     ` [PATCH v5 3/4] vhost: using EVENTFD_COPY2 Pavel Boldin
2015-10-20  9:52                       ` Xie, Huawei
2015-10-21 12:16                         ` Pavel Boldin
2015-10-26  1:45                           ` Xie, Huawei
2015-10-28 18:35                             ` Pavel Boldin
2015-08-28 18:51                     ` Pavel Boldin [this message]
2015-04-16 11:48                   ` [PATCH v5 2/5] vhost: eventfd_link: add function fget_from_files Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 3/5] vhost: eventfd_link: fix ioctl return values Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 4/5] vhost: eventfd_link: replace copy-pasted sys_close Pavel Boldin
2015-05-07  6:54                     ` Xie, Huawei
2015-06-17 15:24                       ` Thomas Monjalon
2015-07-09  0:59                         ` Thomas Monjalon
2015-07-10 14:27                         ` Xie, Huawei
2015-07-10 14:50                           ` Pavel Boldin
2015-07-10 15:32                             ` Xie, Huawei
2015-07-10 15:42                             ` Xie, Huawei
2015-07-10 15:49                               ` Thomas Monjalon
2015-07-10 16:06                                 ` Xie, Huawei
2015-07-11 15:08                               ` Pavel Boldin
2015-07-13  1:59                                 ` Xie, Huawei
2015-07-19 12:39                                   ` Pavel Boldin
2015-04-16 11:48                   ` [PATCH v5 5/5] vhost: eventfd_link: removing extra #includes Pavel Boldin
2015-04-28 14:35                   ` [PATCH v5 0/5] Refactor module `eventfd_link' Thomas Monjalon
2015-05-04  5:29                     ` Xie, Huawei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1440787880-7079-4-git-send-email-pboldin@mirantis.com \
    --to=pboldin@mirantis.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.