From patchwork Fri Aug 28 18:51:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Boldin X-Patchwork-Id: 6821 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 50CC88DA3; Fri, 28 Aug 2015 20:51:32 +0200 (CEST) Received: from mail-wi0-f178.google.com (mail-wi0-f178.google.com [209.85.212.178]) by dpdk.org (Postfix) with ESMTP id 2009F8BA7 for ; Fri, 28 Aug 2015 20:51:30 +0200 (CEST) Received: by wiae7 with SMTP id e7so5199082wia.0 for ; Fri, 28 Aug 2015 11:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mirantis.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=2Lq75YQvXphPEgUTap30WbiwUxngu/Wi8hGxMR78mrE=; b=IlWKhN4BxzT58gnM7cyxj5PubI079TqOik0PnFbdUFq7EtZHjDlz4amrp5fFpjOKEr g4x1elR6bI9CDpAa2yGRabOyOO3ux28aLHTYYbx6nHRhgZb4OsjIMspMhc7WxckfPP7x ftVPfRCk8iabA3NN2Huyf6gI03B3tccixvjmg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=2Lq75YQvXphPEgUTap30WbiwUxngu/Wi8hGxMR78mrE=; b=hIxd2g4Ni6vPgqDZUXGQtGhuDZVZt+5LvMRE0SePEdDocYB/Q12TLUqpze9F/X0Gzu M0+Ua/AzjlT24Nb/ZKHpQ4UgW742Ad/ztRcGqf2ZepH0gxcod9FJCjdYEGtaWpdTJBQK Ii4oWkNnQYZbzPOsXUM/wQwdCVWJTkOQj/OKTOEUCPhhI6aKq2IrhObVbljGro2RK8ul HJ7+kUnMMAEzH8wEo5ErPi0bzk0dQRSfOJsgIkkpkTXq5Qh2fo/EgPffXhnTJOq4q67R TDTWIVXMacn/MdVjpEOxZy8EiJaMRamdc9945EOXjWm/y/uvpm8p+Mh2zBD2yflz0wjR 94Lw== X-Gm-Message-State: ALoCoQlMgc94mgvNMWhDXsPbuerNAS5c0dkPT0meVOAwHka5Nu/u74MD+tOBicqdynG4GvkRE3K7 X-Received: by 10.194.78.101 with SMTP id a5mr12229655wjx.35.1440787889983; Fri, 28 Aug 2015 11:51:29 -0700 (PDT) Received: from pboldin-pc.kha.mirantis.net ([194.213.110.67]) by smtp.gmail.com with ESMTPSA id lg6sm9279299wjb.10.2015.08.28.11.51.29 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 28 Aug 2015 11:51:29 -0700 (PDT) From: Pavel Boldin To: dev@dpdk.org Date: Fri, 28 Aug 2015 21:51:20 +0300 Message-Id: <1440787880-7079-4-git-send-email-pboldin@mirantis.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1440787880-7079-1-git-send-email-pboldin@mirantis.com> References: <1440787880-7079-1-git-send-email-pboldin@mirantis.com> In-Reply-To: <1429184910-30186-2-git-send-email-pboldin@mirantis.com> References: <1429184910-30186-2-git-send-email-pboldin@mirantis.com> Subject: [dpdk-dev] [PATCH v5 4/4] DO NOT MERGE: Tests for new eventfd_link module X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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/vhost-net.h b/test_eventfd_copy/vhost-net.h new file mode 100644 index 0000000..e69de29 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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; +}