From patchwork Wed Mar 2 14:52:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Milard X-Patchwork-Id: 63384 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp2395446lbc; Wed, 2 Mar 2016 05:54:16 -0800 (PST) X-Received: by 10.140.140.196 with SMTP id 187mr34029814qhm.29.1456926856361; Wed, 02 Mar 2016 05:54:16 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id f20si35997617qhe.122.2016.03.02.05.54.15; Wed, 02 Mar 2016 05:54:16 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id C4F2261D30; Wed, 2 Mar 2016 13:54:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5278F650A7; Wed, 2 Mar 2016 13:53:15 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id C643661D30; Wed, 2 Mar 2016 13:53:05 +0000 (UTC) Received: from mail-lf0-f44.google.com (mail-lf0-f44.google.com [209.85.215.44]) by lists.linaro.org (Postfix) with ESMTPS id BA9E761D30 for ; Wed, 2 Mar 2016 13:52:58 +0000 (UTC) Received: by mail-lf0-f44.google.com with SMTP id j186so60933137lfg.2 for ; Wed, 02 Mar 2016 05:52:58 -0800 (PST) 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; bh=FFOm5l1ZOpBMGaIIEDqsmsSObO9Eo83tU8hVQv7UFUk=; b=KhBgFHzArRTJdBruqHu1XSu3Vdbzd8uccoxORG/cKpgNSflpK/0UwNkKIR/+TbcUU6 YA3U7vdbLrlNeaH0Zc1VwfoLB8ST1InqFxMTLhBZhqXglS6YP8+PpFZeTF6xJswt6iBR IUcZQVKYkovzmGig2so+xTfSspL4kv806DJO66627Qs5og7MNFvD45sSOyPkf+h5JfxC 2It8/xKsKAt7PmA1FChR1DKY7cxNjTH5xtPcysIBU3zLjIGn3S4Rg5aYtP5eKBVV+fZk E6hLFkIBLfiUIz7h5NCIKO5BQb5WdlPOuKYMmdNaKBRpkHsHTvrcZ+Poy2Iifx07lXzY xd4g== X-Gm-Message-State: AD7BkJLDCRAseM+snaSclL5LPpzhBVaTbZJK0hTiSvdEhM7ehB4JwHLkaP+eYa/IJCrq1rtzMDA= X-Received: by 10.25.88.69 with SMTP id m66mr8172794lfb.137.1456926777639; Wed, 02 Mar 2016 05:52:57 -0800 (PST) Received: from erachmi-ericsson.ki.sw.ericsson.se (c-83-233-90-46.cust.bredband2.com. [83.233.90.46]) by smtp.gmail.com with ESMTPSA id m64sm4327851lfd.36.2016.03.02.05.52.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 02 Mar 2016 05:52:57 -0800 (PST) From: Christophe Milard To: maxim.uvarov@linaro.org, mike.holmes@linaro.org, bill.fischofer@linaro.org Date: Wed, 2 Mar 2016 15:52:03 +0100 Message-Id: <1456930323-45141-3-git-send-email-christophe.milard@linaro.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1456930323-45141-1-git-send-email-christophe.milard@linaro.org> References: <1456930323-45141-1-git-send-email-christophe.milard@linaro.org> X-Topics: patch Cc: lng-odp@lists.linaro.org Subject: [lng-odp] [PATCHv2 2/2] linux-generic: test: ODP_SHM_PROC test X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Adding a platform side test to test sharing memory between ODP and linux. Also tests that the shared memory device name (under /dev/shm/) is scoped. Signed-off-by: Christophe Milard Reviewed-and-tested-by: Bill Fischofer --- platform/linux-generic/m4/configure.m4 | 1 + platform/linux-generic/test/Makefile.am | 4 +- platform/linux-generic/test/shmem/.gitignore | 2 + platform/linux-generic/test/shmem/Makefile.am | 18 +++ platform/linux-generic/test/shmem/shmem.h | 21 +++ platform/linux-generic/test/shmem/shmem_common.h | 23 ++++ platform/linux-generic/test/shmem/shmem_linux.c | 156 +++++++++++++++++++++++ platform/linux-generic/test/shmem/shmem_linux.h | 9 ++ platform/linux-generic/test/shmem/shmem_odp.c | 74 +++++++++++ platform/linux-generic/test/shmem/shmem_odp.h | 7 + 10 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 platform/linux-generic/test/shmem/.gitignore create mode 100644 platform/linux-generic/test/shmem/Makefile.am create mode 100644 platform/linux-generic/test/shmem/shmem.h create mode 100644 platform/linux-generic/test/shmem/shmem_common.h create mode 100644 platform/linux-generic/test/shmem/shmem_linux.c create mode 100644 platform/linux-generic/test/shmem/shmem_linux.h create mode 100644 platform/linux-generic/test/shmem/shmem_odp.c create mode 100644 platform/linux-generic/test/shmem/shmem_odp.h diff --git a/platform/linux-generic/m4/configure.m4 b/platform/linux-generic/m4/configure.m4 index df6dc64..f24e0fb 100644 --- a/platform/linux-generic/m4/configure.m4 +++ b/platform/linux-generic/m4/configure.m4 @@ -23,4 +23,5 @@ m4_include([platform/linux-generic/m4/odp_pcap.m4]) AC_CONFIG_FILES([platform/linux-generic/Makefile platform/linux-generic/test/Makefile + platform/linux-generic/test/shmem/Makefile platform/linux-generic/test/pktio/Makefile]) diff --git a/platform/linux-generic/test/Makefile.am b/platform/linux-generic/test/Makefile.am index db923b8..c24afe9 100644 --- a/platform/linux-generic/test/Makefile.am +++ b/platform/linux-generic/test/Makefile.am @@ -1,11 +1,13 @@ include $(top_srcdir)/test/Makefile.inc TESTS_ENVIRONMENT += TEST_DIR=${top_builddir}/test/validation -ODP_MODULES = pktio +ODP_MODULES = pktio \ + shmem if test_vald TESTS = pktio/pktio_run \ pktio/pktio_run_tap \ + shmem/shmem_linux \ ${top_builddir}/test/validation/atomic/atomic_main$(EXEEXT) \ ${top_builddir}/test/validation/barrier/barrier_main$(EXEEXT) \ ${top_builddir}/test/validation/buffer/buffer_main$(EXEEXT) \ diff --git a/platform/linux-generic/test/shmem/.gitignore b/platform/linux-generic/test/shmem/.gitignore new file mode 100644 index 0000000..7627079 --- /dev/null +++ b/platform/linux-generic/test/shmem/.gitignore @@ -0,0 +1,2 @@ +shmem_linux +shmem_odp diff --git a/platform/linux-generic/test/shmem/Makefile.am b/platform/linux-generic/test/shmem/Makefile.am new file mode 100644 index 0000000..ffec6ee --- /dev/null +++ b/platform/linux-generic/test/shmem/Makefile.am @@ -0,0 +1,18 @@ +include ../Makefile.inc + +#the main test program is shmem_linux, which, in turn, starts a shmem_odp: +test_PROGRAMS = shmem_linux$(EXEEXT) +test_extra_PROGRAMS = shmem_odp$(EXEEXT) +test_extradir = $(testdir) + +#shmem_linux is stand alone, pure linux (no ODP): +dist_shmem_linux_SOURCES = shmem_linux.c +shmem_linux_LDFLAGS = $(AM_LDFLAGS) -lrt + +#shmem_odp is the odp part: +dist_shmem_odp_SOURCES = shmem_odp.c +shmem_odp_CFLAGS = $(AM_CFLAGS) \ + $(INCCUNIT_COMMON) \ + $(INCODP) +shmem_odp_LDFLAGS = $(AM_LDFLAGS) +shmem_odp_LDADD = $(LIBCUNIT_COMMON) $(LIBODP) diff --git a/platform/linux-generic/test/shmem/shmem.h b/platform/linux-generic/test/shmem/shmem.h new file mode 100644 index 0000000..2368a2e --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _ODP_LINUX_TEST_SHMEM_H_ +#define _ODP_LINUX_TEST_SHMEM_H_ + +#include + +/* test functions: */ +void shmem_test_odp_shm_proc(void); + +/* test arrays: */ +extern odp_testinfo_t shmem_linux_suite[]; + +/* test registry: */ +extern odp_suiteinfo_t shmem_linux_suites[]; + +#endif diff --git a/platform/linux-generic/test/shmem/shmem_common.h b/platform/linux-generic/test/shmem/shmem_common.h new file mode 100644 index 0000000..16227ec --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem_common.h @@ -0,0 +1,23 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _COMMON_TEST_SHMEM_H_ +#define _COMMON_TEST_SHMEM_H_ + +#define ODP_SHM_NAME "odp_linux_shared_mem" +#define FIFO_NAME_FMT "/tmp/shmem_test_fifo-%d" +#define ALIGN_SIZE (128) +#define TEST_SHARE_FOO (0xf0f0f0f0) +#define TEST_SHARE_BAR (0xf0f0f0f) +#define TEST_FAILURE 'F' +#define TEST_SUCCESS 'S' + +typedef struct { + uint32_t foo; + uint32_t bar; +} test_shared_linux_data_t; + +#endif diff --git a/platform/linux-generic/test/shmem/shmem_linux.c b/platform/linux-generic/test/shmem/shmem_linux.c new file mode 100644 index 0000000..516bbe4 --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem_linux.c @@ -0,0 +1,156 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* this test makes sure that odp shared memory created with the ODP_SHM_PROC + * flag is visible under linux. It therefore checks both that the device + * name under /dev/shm is correct, and also checks that the memory contents + * is indeed shared. + * we want: + * -the odp test to run using C UNIT + * -the main process to return the correct return code. + * (for the autotools test harness) + * + * To achieve this, the flow of operations is as follows: + * + * linux process (main, non odp) | ODP process + * (shmem_linux.c) | (shmem_odp.c) + * | + * main() | + * forks odp process | allocate shmem + * wait for named pipe creation | populate shmem + * | create named pipe + * read shared memory | wait for test report in fifo + * check if memory contents is OK | + * if OK, write "S" in fifo, else "F" | report success or failure to C-Unit + * wait for child terminaison & status| terminate with usual F/S status + * terminate with same status as child| + * | + * \|/ + * time + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "shmem_linux.h" +#include "shmem_common.h" + +#define ODP_APP_NAME "shmem_odp" /* name of the odp program, in this dir */ +#define DEVNAME_FMT "odp-%d-%s" /* shm device format: odp-- */ + +void test_success(char *fifo_name, int fd, pid_t odp_app) +{ + int status; + int nb_char; + char result = TEST_SUCCESS; + /* write "Success" to the FIFO */ + nb_char = write(fd, &result, sizeof(char)); + close(fd); + /* wait for the odp app to terminate */ + waitpid(odp_app, &status, 0); + /* if the write failed, report an error anyway */ + if (nb_char != 1) + status = 1; + unlink(fifo_name); + exit(status); /* the status reported by the odp side is returned */ +} + +void test_failure(char *fifo_name, int fd, pid_t odp_app) +{ + int status; + char result; + + int nb_char __attribute__((unused)); /*ignored: we fail anyway */ + + result = TEST_FAILURE; + /* write "Success" to the FIFO */ + nb_char = write(fd, &result, sizeof(char)); + close(fd); + /* wait for the odp app to terminate */ + waitpid(odp_app, &status, 0); + unlink(fifo_name); + exit(1); /* error */ +} + +int main(int argc __attribute__((unused)), char *argv[]) +{ + char prg_name[PATH_MAX]; + char odp_name[PATH_MAX]; + int nb_sec = 0; + int size; + pid_t odp_app; + char *odp_params = NULL; + char fifo_name[PATH_MAX]; /* fifo for linux->odp feedback */ + int fifo_fd; + char shm_devname[PATH_MAX];/* shared mem device name, under /dev/shm */ + int shm_fd; + test_shared_linux_data_t *addr; + + /* odp app is in the same directory as this file: */ + strncpy(prg_name, argv[0], PATH_MAX); + sprintf(odp_name, "%s/%s", dirname(prg_name), ODP_APP_NAME); + + /* start the ODP application: */ + odp_app = fork(); + if (odp_app < 0) /* error */ + exit(1); + + if (odp_app == 0) /* child */ + execv(odp_name, &odp_params); + + /* wait max 30 sec for the fifo to be created by the ODP side. + * Just die if time expire as there is no fifo to communicate + * through... */ + sprintf(fifo_name, FIFO_NAME_FMT, odp_app); + while (access(fifo_name, W_OK) != 0) { + sleep(1); + if (nb_sec++ == 30) + exit(1); + } + fifo_fd = open(fifo_name, O_WRONLY); + printf("pipe found\n"); + + /* the linux named pipe has now been found, meaning that the + * ODP application is up and running, and has allocated shmem. + * check to see if linux can see the created shared memory: */ + + sprintf(shm_devname, DEVNAME_FMT, odp_app, ODP_SHM_NAME); + + /* O_CREAT flag not given => failure if shm_devname does not already + * exist */ + shm_fd = shm_open(shm_devname, O_RDONLY, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (shm_fd == -1) + test_failure(fifo_name, shm_fd, odp_app); + + /* we know that the linux generic ODP actually allocates the required + * size + alignment and aligns the returned address after. + * we must do the same here: */ + size = sizeof(test_shared_linux_data_t) + ALIGN_SIZE; + addr = mmap(NULL, size, PROT_READ, MAP_SHARED, shm_fd, 0); + if (addr == MAP_FAILED) + test_failure(fifo_name, shm_fd, odp_app); + + /* perform manual alignment */ + addr = (test_shared_linux_data_t *)((((unsigned long int)addr + + ALIGN_SIZE - 1) / ALIGN_SIZE) * ALIGN_SIZE); + + /* check that we see what the ODP application wrote in the memory */ + if ((addr->foo == TEST_SHARE_FOO) && (addr->bar == TEST_SHARE_BAR)) + test_success(fifo_name, fifo_fd, odp_app); + else + test_failure(fifo_name, fifo_fd, odp_app); +} diff --git a/platform/linux-generic/test/shmem/shmem_linux.h b/platform/linux-generic/test/shmem/shmem_linux.h new file mode 100644 index 0000000..a07a775 --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem_linux.h @@ -0,0 +1,9 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +void test_success(char *fifo_name, int fd, pid_t odp_app); +void test_failure(char *fifo_name, int fd, pid_t odp_app); +int main(int argc, char *argv[]); diff --git a/platform/linux-generic/test/shmem/shmem_odp.c b/platform/linux-generic/test/shmem/shmem_odp.c new file mode 100644 index 0000000..df584e6 --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem_odp.c @@ -0,0 +1,74 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include "shmem_odp.h" +#include "shmem_common.h" + +#define TEST_SHARE_FOO (0xf0f0f0f0) +#define TEST_SHARE_BAR (0xf0f0f0f) + +void shmem_test_odp_shm_proc(void) +{ + char fifo_name[PATH_MAX]; + int fd; + odp_shm_t shm; + test_shared_data_t *test_shared_data; + char test_result; + + shm = odp_shm_reserve(ODP_SHM_NAME, + sizeof(test_shared_data_t), + ALIGN_SIZE, ODP_SHM_PROC); + CU_ASSERT_FATAL(ODP_SHM_INVALID != shm); + test_shared_data = odp_shm_addr(shm); + CU_ASSERT_FATAL(NULL != test_shared_data); + test_shared_data->foo = TEST_SHARE_FOO; + test_shared_data->bar = TEST_SHARE_BAR; + + odp_mb_full(); + + /* open the fifo: this will indicate to linux process that it can + * start the shmem lookup and check if it sees the data */ + sprintf(fifo_name, FIFO_NAME_FMT, getpid()); + CU_ASSERT_FATAL(mkfifo(fifo_name, 0666) == 0); + + /* read from the fifo: the linux process result: */ + fd = open(fifo_name, O_RDONLY); + CU_ASSERT_FATAL(fd >= 0); + + CU_ASSERT(read(fd, &test_result, sizeof(char)) == 1); + CU_ASSERT_FATAL(test_result == TEST_SUCCESS); + + CU_ASSERT(odp_shm_free(shm) == 0); +} + +odp_testinfo_t shmem_suite[] = { + ODP_TEST_INFO(shmem_test_odp_shm_proc), + ODP_TEST_INFO_NULL, +}; + +odp_suiteinfo_t shmem_suites[] = { + {"Shared Memory", NULL, NULL, shmem_suite}, + ODP_SUITE_INFO_NULL, +}; + +int main(void) +{ + int ret = odp_cunit_register(shmem_suites); + + if (ret == 0) + ret = odp_cunit_run(); + + return ret; +} diff --git a/platform/linux-generic/test/shmem/shmem_odp.h b/platform/linux-generic/test/shmem/shmem_odp.h new file mode 100644 index 0000000..6fed569 --- /dev/null +++ b/platform/linux-generic/test/shmem/shmem_odp.h @@ -0,0 +1,7 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +void shmem_test_odp_shm_proc(void);