From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id 337401CD4A for ; Fri, 6 Apr 2018 20:50:04 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Apr 2018 11:50:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,416,1517904000"; d="scan'208";a="45590445" Received: from sivswdev02.ir.intel.com (HELO localhost.localdomain) ([10.237.217.46]) by orsmga001.jf.intel.com with ESMTP; 06 Apr 2018 11:50:02 -0700 From: Konstantin Ananyev To: dev@dpdk.org Cc: Konstantin Ananyev Date: Fri, 6 Apr 2018 19:49:40 +0100 Message-Id: <1523040581-2522-9-git-send-email-konstantin.ananyev@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: <1522431163-25621-2-git-send-email-konstantin.ananyev@intel.com> References: <1522431163-25621-2-git-send-email-konstantin.ananyev@intel.com> Subject: [dpdk-dev] [PATCH v3 08/10] test: introduce functional test for librte_bpf X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Apr 2018 18:50:04 -0000 Signed-off-by: Konstantin Ananyev --- test/test/Makefile | 2 + test/test/meson.build | 2 + test/test/test_bpf.c | 633 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 637 insertions(+) create mode 100644 test/test/test_bpf.c diff --git a/test/test/Makefile b/test/test/Makefile index a88cc38bf..61ac6880d 100644 --- a/test/test/Makefile +++ b/test/test/Makefile @@ -193,6 +193,8 @@ endif SRCS-$(CONFIG_RTE_LIBRTE_KVARGS) += test_kvargs.c +SRCS-$(CONFIG_RTE_LIBRTE_BPF) += test_bpf.c + CFLAGS += -DALLOW_EXPERIMENTAL_API CFLAGS += -O3 diff --git a/test/test/meson.build b/test/test/meson.build index eb3d87a4d..101446984 100644 --- a/test/test/meson.build +++ b/test/test/meson.build @@ -8,6 +8,7 @@ test_sources = files('commands.c', 'test_alarm.c', 'test_atomic.c', 'test_barrier.c', + 'test_bpf.c', 'test_byteorder.c', 'test_cmdline.c', 'test_cmdline_cirbuf.c', @@ -98,6 +99,7 @@ test_sources = files('commands.c', ) test_deps = ['acl', + 'bpf', 'cfgfile', 'cmdline', 'cryptodev', diff --git a/test/test/test_bpf.c b/test/test/test_bpf.c new file mode 100644 index 000000000..20b6de9de --- /dev/null +++ b/test/test/test_bpf.c @@ -0,0 +1,633 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2018 Intel Corporation + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "test.h" + +/* + * Basic functional tests for librte_bpf. + * The main procedure - load eBPF program, execute it and + * compare restuls with expected values. + * Note that more tests have to be added to cover remaining instructions. + */ + +struct dummy_offset { + uint64_t u64; + uint32_t u32; + uint16_t u16; + uint8_t u8; +}; + +struct dummy_vect8 { + struct dummy_offset in[8]; + struct dummy_offset out[8]; +}; + +#define TEST_FILL_1 0xDEADBEEF + +#define TEST_MUL_1 21 +#define TEST_MUL_2 -100 + +struct bpf_test { + const char *name; + size_t arg_sz; + struct rte_bpf_prm prm; + void (*prepare)(void *); + int (*check_result)(uint64_t, const void *); +}; + +/* store immediate test-cases */ +static const struct bpf_insn test_store1_prog[] = { + { + .code = (BPF_ST | BPF_MEM | BPF_B), + .dst_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u8), + .imm = TEST_FILL_1, + }, + { + .code = (BPF_ST | BPF_MEM | BPF_H), + .dst_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u16), + .imm = TEST_FILL_1, + }, + { + .code = (BPF_ST | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u32), + .imm = TEST_FILL_1, + }, + { + .code = (BPF_ST | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u64), + .imm = TEST_FILL_1, + }, + /* return 1 */ + { + .code = (BPF_ALU | BPF_MOV | BPF_K), + .dst_reg = BPF_REG_0, + .imm = 1, + }, + { + .code = (BPF_JMP | BPF_EXIT), + }, +}; + +static void +test_store1_prepare(void *arg) +{ + struct dummy_offset *df; + + df = arg; + memset(df, 0, sizeof(*df)); +} + +static int +test_store1_check(uint64_t rc, const void *arg) +{ + const struct dummy_offset *dft; + struct dummy_offset dfe; + + dft = arg; + + if (rc != 1) { + printf("%s@%d: invalid return value %" PRIu64 "\n", + __func__, __LINE__, rc); + return -1; + } + + memset(&dfe, 0, sizeof(dfe)); + dfe.u64 = (int32_t)TEST_FILL_1; + dfe.u32 = dfe.u64; + dfe.u16 = dfe.u64; + dfe.u8 = dfe.u64; + + if (memcmp(dft, &dfe, sizeof(dfe)) != 0) { + printf("%s: invalid value\n", __func__); + rte_memdump(stdout, "expected", &dfe, sizeof(dfe)); + rte_memdump(stdout, "result", dft, sizeof(*dft)); + return -1; + } + + return 0; +} + +/* store register test-cases */ +static const struct bpf_insn test_store2_prog[] = { + + { + .code = (BPF_ALU64 | BPF_MOV | BPF_K), + .dst_reg = BPF_REG_2, + .imm = TEST_FILL_1, + }, + { + .code = (BPF_STX | BPF_MEM | BPF_B), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_offset, u8), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_H), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_offset, u16), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_offset, u32), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_offset, u64), + }, + /* return 1 */ + { + .code = (BPF_ALU | BPF_MOV | BPF_K), + .dst_reg = BPF_REG_0, + .imm = 1, + }, + { + .code = (BPF_JMP | BPF_EXIT), + }, +}; + +/* load test-cases */ +static const struct bpf_insn test_load1_prog[] = { + + { + .code = (BPF_LDX | BPF_MEM | BPF_B), + .dst_reg = BPF_REG_2, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u8), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_H), + .dst_reg = BPF_REG_3, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u16), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u32), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_0, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_offset, u64), + }, + /* return sum */ + { + .code = (BPF_ALU64 | BPF_ADD | BPF_X), + .dst_reg = BPF_REG_0, + .src_reg = BPF_REG_4, + }, + { + .code = (BPF_ALU64 | BPF_ADD | BPF_X), + .dst_reg = BPF_REG_0, + .src_reg = BPF_REG_3, + }, + { + .code = (BPF_ALU64 | BPF_ADD | BPF_X), + .dst_reg = BPF_REG_0, + .src_reg = BPF_REG_2, + }, + { + .code = (BPF_JMP | BPF_EXIT), + }, +}; + +static void +test_load1_prepare(void *arg) +{ + struct dummy_offset *df; + + df = arg; + + memset(df, 0, sizeof(*df)); + df->u64 = (int32_t)TEST_FILL_1; + df->u32 = df->u64; + df->u16 = df->u64; + df->u8 = df->u64; +} + +static int +test_load1_check(uint64_t rc, const void *arg) +{ + uint64_t v; + const struct dummy_offset *dft; + + dft = arg; + v = dft->u64; + v += dft->u32; + v += dft->u16; + v += dft->u8; + + if (v != rc) { + printf("%s@%d: invalid return value " + "expected=0x%" PRIx64 ", actual=0x%" PRIx64 "\n", + __func__, __LINE__, v, rc); + return -1; + } + return 0; +} + +/* alu mul test-cases */ +static const struct bpf_insn test_mul1_prog[] = { + + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_2, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[0].u32), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_3, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[1].u64), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[2].u32), + }, + { + .code = (BPF_ALU | BPF_MUL | BPF_K), + .dst_reg = BPF_REG_2, + .imm = TEST_MUL_1, + }, + { + .code = (BPF_ALU64 | BPF_MUL | BPF_K), + .dst_reg = BPF_REG_3, + .imm = TEST_MUL_2, + }, + { + .code = (BPF_ALU | BPF_MUL | BPF_X), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_2, + }, + { + .code = (BPF_ALU64 | BPF_MUL | BPF_X), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_3, + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_vect8, out[0].u64), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_3, + .off = offsetof(struct dummy_vect8, out[1].u64), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_4, + .off = offsetof(struct dummy_vect8, out[2].u64), + }, + /* return 1 */ + { + .code = (BPF_ALU | BPF_MOV | BPF_K), + .dst_reg = BPF_REG_0, + .imm = 1, + }, + { + .code = (BPF_JMP | BPF_EXIT), + }, +}; + +static void +test_mul1_prepare(void *arg) +{ + struct dummy_vect8 *dv; + uint64_t v; + + dv = arg; + + v = rte_rand(); + + memset(dv, 0, sizeof(*dv)); + dv->in[0].u32 = v; + dv->in[1].u64 = v << 12 | v >> 6; + dv->in[2].u32 = -v; +} + +static int +test_mul1_check(uint64_t rc, const void *arg) +{ + uint64_t r2, r3, r4; + const struct dummy_vect8 *dvt; + struct dummy_vect8 dve; + + dvt = arg; + + if (rc != 1) { + printf("%s@%d: invalid return value %" PRIu64 "\n", + __func__, __LINE__, rc); + return -1; + } + + memset(&dve, 0, sizeof(dve)); + + r2 = dvt->in[0].u32; + r3 = dvt->in[1].u64; + r4 = dvt->in[2].u32; + + r2 = (uint32_t)r2 * TEST_MUL_1; + r3 *= TEST_MUL_2; + r4 = (uint32_t)(r4 * r2); + r4 *= r3; + + dve.out[0].u64 = r2; + dve.out[1].u64 = r3; + dve.out[2].u64 = r4; + + if (memcmp(dvt->out, dve.out, sizeof(dve.out)) != 0) { + printf("%s: invalid value\n", __func__); + rte_memdump(stdout, "expected", dve.out, sizeof(dve.out)); + rte_memdump(stdout, "result", dvt->out, sizeof(dvt->out)); + return -1; + } + + return 0; +} + +/* alu div test-cases */ +static const struct bpf_insn test_div1_prog[] = { + + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_2, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[0].u32), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_3, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[1].u64), + }, + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[2].u32), + }, + { + .code = (BPF_ALU | BPF_DIV | BPF_K), + .dst_reg = BPF_REG_2, + .imm = TEST_MUL_1, + }, + { + .code = (BPF_ALU64 | BPF_MOD | BPF_K), + .dst_reg = BPF_REG_3, + .imm = TEST_MUL_2, + }, + { + .code = (BPF_ALU64 | BPF_OR | BPF_K), + .dst_reg = BPF_REG_2, + .imm = 1, + }, + { + .code = (BPF_ALU64 | BPF_OR | BPF_K), + .dst_reg = BPF_REG_3, + .imm = 1, + }, + { + .code = (BPF_ALU | BPF_MOD | BPF_X), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_2, + }, + { + .code = (BPF_ALU64 | BPF_DIV | BPF_X), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_3, + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_2, + .off = offsetof(struct dummy_vect8, out[0].u64), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_3, + .off = offsetof(struct dummy_vect8, out[1].u64), + }, + { + .code = (BPF_STX | BPF_MEM | BPF_DW), + .dst_reg = BPF_REG_1, + .src_reg = BPF_REG_4, + .off = offsetof(struct dummy_vect8, out[2].u64), + }, + /* check that we can handle division by zero gracefully. */ + { + .code = (BPF_LDX | BPF_MEM | BPF_W), + .dst_reg = BPF_REG_2, + .src_reg = BPF_REG_1, + .off = offsetof(struct dummy_vect8, in[3].u32), + }, + { + .code = (BPF_ALU | BPF_DIV | BPF_X), + .dst_reg = BPF_REG_4, + .src_reg = BPF_REG_2, + }, + /* return 1 */ + { + .code = (BPF_ALU | BPF_MOV | BPF_K), + .dst_reg = BPF_REG_0, + .imm = 1, + }, + { + .code = (BPF_JMP | BPF_EXIT), + }, +}; + +static int +test_div1_check(uint64_t rc, const void *arg) +{ + uint64_t r2, r3, r4; + const struct dummy_vect8 *dvt; + struct dummy_vect8 dve; + + dvt = arg; + + /* + * in the test prog we attempted to divide by zero. + * so it should return 0. + */ + if (rc != 0) { + printf("%s@%d: invalid return value %" PRIu64 "\n", + __func__, __LINE__, rc); + return -1; + } + + memset(&dve, 0, sizeof(dve)); + + r2 = dvt->in[0].u32; + r3 = dvt->in[1].u64; + r4 = dvt->in[2].u32; + + r2 = (uint32_t)r2 / TEST_MUL_1; + r3 %= TEST_MUL_2; + r2 |= 1; + r3 |= 1; + r4 = (uint32_t)(r4 % r2); + r4 /= r3; + + dve.out[0].u64 = r2; + dve.out[1].u64 = r3; + dve.out[2].u64 = r4; + + if (memcmp(dvt->out, dve.out, sizeof(dve.out)) != 0) { + printf("%s: invalid value\n", __func__); + rte_memdump(stdout, "expected", dve.out, sizeof(dve.out)); + rte_memdump(stdout, "result", dvt->out, sizeof(dvt->out)); + return -1; + } + + return 0; +} + +static const struct bpf_test tests[] = { + { + .name = "test_store1", + .arg_sz = sizeof(struct dummy_offset), + .prm = { + .ins = test_store1_prog, + .nb_ins = RTE_DIM(test_store1_prog), + .prog_type = RTE_BPF_PROG_TYPE_UNSPEC, + }, + .prepare = test_store1_prepare, + .check_result = test_store1_check, + }, + { + .name = "test_store2", + .arg_sz = sizeof(struct dummy_offset), + .prm = { + .ins = test_store2_prog, + .nb_ins = RTE_DIM(test_store2_prog), + .prog_type = RTE_BPF_PROG_TYPE_UNSPEC, + }, + .prepare = test_store1_prepare, + .check_result = test_store1_check, + }, + { + .name = "test_load1", + .arg_sz = sizeof(struct dummy_offset), + .prm = { + .ins = test_load1_prog, + .nb_ins = RTE_DIM(test_load1_prog), + .prog_type = RTE_BPF_PROG_TYPE_UNSPEC, + }, + .prepare = test_load1_prepare, + .check_result = test_load1_check, + }, + { + .name = "test_mul1", + .arg_sz = sizeof(struct dummy_vect8), + .prm = { + .ins = test_mul1_prog, + .nb_ins = RTE_DIM(test_mul1_prog), + .prog_type = RTE_BPF_PROG_TYPE_UNSPEC, + }, + .prepare = test_mul1_prepare, + .check_result = test_mul1_check, + }, + { + .name = "test_div1", + .arg_sz = sizeof(struct dummy_vect8), + .prm = { + .ins = test_div1_prog, + .nb_ins = RTE_DIM(test_div1_prog), + .prog_type = RTE_BPF_PROG_TYPE_UNSPEC, + }, + .prepare = test_mul1_prepare, + .check_result = test_div1_check, + }, +}; + +static int +run_test(const struct bpf_test *tst) +{ + int32_t ret, rv; + int64_t rc; + struct rte_bpf *bpf; + struct rte_bpf_jit jit; + uint8_t tbuf[tst->arg_sz]; + + printf("%s(%s) start\n", __func__, tst->name); + + bpf = rte_bpf_load(&tst->prm); + if (bpf == NULL) { + printf("%s%d: failed to load bpf code, error=%d(%s);\n", + __func__, __LINE__, rte_errno, strerror(rte_errno)); + return -1; + } + + tst->prepare(tbuf); + + rc = rte_bpf_exec(bpf, tbuf); + ret = tst->check_result(rc, tbuf); + if (ret != 0) { + printf("%s%d: check_result(%s) failed, error: %d(%s);\n", + __func__, __LINE__, tst->name, ret, strerror(ret)); + } + + rte_bpf_get_jit(bpf, &jit); + if (jit.func == NULL) + return 0; + + tst->prepare(tbuf); + rc = jit.func(tbuf); + rv = tst->check_result(rc, tbuf); + ret |= rv; + if (rv != 0) { + printf("%s%d: check_result(%s) failed, error: %d(%s);\n", + __func__, __LINE__, tst->name, rv, strerror(ret)); + } + + rte_bpf_destroy(bpf); + return ret; + +} + +static int +test_bpf(void) +{ + int32_t rc; + uint32_t i; + + rc = 0; + for (i = 0; i != RTE_DIM(tests); i++) + rc |= run_test(tests + i); + + return rc; +} + +REGISTER_TEST_COMMAND(bpf_autotest, test_bpf); -- 2.13.6