From patchwork Tue Jul 30 13:44:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Palethorpe X-Patchwork-Id: 1138986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45yd9b0slCz9s00 for ; Tue, 30 Jul 2019 23:45:38 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id A93B23C1D13 for ; Tue, 30 Jul 2019 15:45:35 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) by picard.linux.it (Postfix) with ESMTP id ED8833C1A4E for ; Tue, 30 Jul 2019 15:45:32 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id 1882B140098C for ; Tue, 30 Jul 2019 15:45:31 +0200 (CEST) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 447E7AD18 for ; Tue, 30 Jul 2019 13:45:31 +0000 (UTC) From: Richard Palethorpe To: ltp@lists.linux.it Date: Tue, 30 Jul 2019 15:44:55 +0200 Message-Id: <20190730134457.27845-2-rpalethorpe@suse.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190730134457.27845-1-rpalethorpe@suse.com> References: <20190724080328.16145-1-rpalethorpe@suse.com> <20190730134457.27845-1-rpalethorpe@suse.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/4] BPF: Sanity check creating and updating maps X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Palethorpe Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Richard Palethorpe --- runtest/syscalls | 2 + testcases/kernel/syscalls/bpf/.gitignore | 1 + testcases/kernel/syscalls/bpf/Makefile | 10 ++ testcases/kernel/syscalls/bpf/bpf_map01.c | 143 ++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 testcases/kernel/syscalls/bpf/.gitignore create mode 100644 testcases/kernel/syscalls/bpf/Makefile create mode 100644 testcases/kernel/syscalls/bpf/bpf_map01.c diff --git a/runtest/syscalls b/runtest/syscalls index 67dfed661..46880ee1d 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -32,6 +32,8 @@ bind01 bind01 bind02 bind02 bind03 bind03 +bpf_map01 bpf_map01 + brk01 brk01 capget01 capget01 diff --git a/testcases/kernel/syscalls/bpf/.gitignore b/testcases/kernel/syscalls/bpf/.gitignore new file mode 100644 index 000000000..f33532484 --- /dev/null +++ b/testcases/kernel/syscalls/bpf/.gitignore @@ -0,0 +1 @@ +bpf_map01 diff --git a/testcases/kernel/syscalls/bpf/Makefile b/testcases/kernel/syscalls/bpf/Makefile new file mode 100644 index 000000000..990c8c83c --- /dev/null +++ b/testcases/kernel/syscalls/bpf/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2019 Linux Test Project + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +CFLAGS += -D_GNU_SOURCE + +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/bpf/bpf_map01.c b/testcases/kernel/syscalls/bpf/bpf_map01.c new file mode 100644 index 000000000..fd0ec2c7c --- /dev/null +++ b/testcases/kernel/syscalls/bpf/bpf_map01.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2019 Richard Palethorpe + * + * Trivial Extended Berkeley Packet Filter (eBPF) test. + * + * Sanity check creating and updating maps. + */ + +#include +#include + +#include "config.h" +#include "tst_test.h" +#include "lapi/bpf.h" + +#define KEY_SZ 8 +#define VAL_SZ 1024 + +struct map_type { + uint32_t id; + char *name; +}; + +static const struct map_type map_types[] = { + {BPF_MAP_TYPE_HASH, "hash"}, + {BPF_MAP_TYPE_ARRAY, "array"} +}; + +static void *key; +static void *val0; +static void *val1; + +static void setup(void) +{ + key = SAFE_MALLOC(KEY_SZ); + memset(key, 0, (size_t) KEY_SZ); + val0 = SAFE_MALLOC(VAL_SZ); + val1 = SAFE_MALLOC(VAL_SZ); + memset(val1, 0, (size_t) VAL_SZ); +} + +void run(unsigned int n) +{ + int fd, i; + union bpf_attr attr; + + memset(&attr, 0, sizeof(attr)); + attr.map_type = map_types[n].id; + attr.key_size = n == 0 ? KEY_SZ : 4; + attr.value_size = VAL_SZ; + attr.max_entries = 1; + + TEST(bpf(BPF_MAP_CREATE, &attr, sizeof(attr))); + if (TST_RET == -1) { + if (TST_ERR == EPERM) { + tst_brk(TCONF | TTERRNO, + "bpf() requires CAP_SYS_ADMIN on this system"); + } else { + tst_brk(TFAIL | TTERRNO, "Failed to create %s map", + map_types[n].name); + } + } + tst_res(TPASS, "Created %s map", map_types[n].name); + fd = TST_RET; + + if (n == 0) + memcpy(key, "12345678", KEY_SZ); + else + memset(key, 0, 4); + + memset(&attr, 0, sizeof(attr)); + attr.map_fd = fd; + attr.key = ptr_to_u64(key); + attr.value = ptr_to_u64(val1); + + TEST(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr))); + if (n == 0) { + if (TST_RET != -1 || TST_ERR != ENOENT) { + tst_res(TFAIL | TTERRNO, + "Empty hash map lookup should fail with ENOENT"); + } else { + tst_res(TPASS | TTERRNO, "Empty hash map lookup"); + } + } else if (TST_RET != -1) { + for (i = 0;;) { + if (*(char *) val1 != 0) { + tst_res(TFAIL, + "Preallocated array map val not zero"); + } else if (++i >= VAL_SZ) { + tst_res(TPASS, + "Preallocated array map lookup"); + break; + } + } + } else { + tst_res(TFAIL | TERRNO, "Prellocated array map lookup"); + } + + memset(&attr, 0, sizeof(attr)); + attr.map_fd = fd; + attr.key = ptr_to_u64(key); + attr.value = ptr_to_u64(val0); + attr.flags = BPF_ANY; + + TEST(bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr))); + if (TST_RET == -1) { + tst_brk(TFAIL | TTERRNO, + "Update %s map element", + map_types[n].name); + } else { + tst_res(TPASS, + "Update %s map element", + map_types[n].name); + } + + memset(&attr, 0, sizeof(attr)); + attr.map_fd = fd; + attr.key = ptr_to_u64(key); + attr.value = ptr_to_u64(val1); + + TEST(bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr))); + if (TST_RET == -1) { + tst_res(TFAIL | TTERRNO, + "%s map lookup missing", + map_types[n].name); + } else if (memcmp(val0, val1, (size_t) VAL_SZ)) { + tst_res(TFAIL, + "%s map lookup returned different value", + map_types[n].name); + } else { + tst_res(TPASS, "%s map lookup", map_types[n].name); + } + + SAFE_CLOSE(fd); +} + +static struct tst_test test = { + .tcnt = 2, + .setup = setup, + .test = run, + .min_kver = "3.18", +};