From: Stanislav Fomichev <sdf@google.com>
To: netdev@vger.kernel.org, ast@kernel.org
Cc: davem@davemloft.net, daniel@iogearbox.net, ecree@solarflare.com,
quentin.monnet@netronome.com, Stanislav Fomichev <sdf@google.com>
Subject: [PATCH bpf-next v2 1/6] selftests/bpf: add map/prog type probe helpers
Date: Mon, 17 Dec 2018 10:25:49 -0800 [thread overview]
Message-ID: <20181217182554.52170-2-sdf@google.com> (raw)
In-Reply-To: <20181217182554.52170-1-sdf@google.com>
Export bpf_map_type_supported() and bpf_prog_type_supported() which
return true/false to indicate kernel support for the appropriate
program or map type. These helpers will be used in the next commits
to selectively skip test_verifier/test_maps tests.
bpf_map_type_supported() supports only limited set of maps for which we
do fixups in the test_verifier, for unknown maps it falls back to
'supported'.
Signed-off-by: Stanislav Fomichev <sdf@google.com>
---
tools/testing/selftests/bpf/probe_helpers.c | 68 +++++++++++++++++++++
tools/testing/selftests/bpf/probe_helpers.h | 10 +++
2 files changed, 78 insertions(+)
create mode 100644 tools/testing/selftests/bpf/probe_helpers.c
create mode 100644 tools/testing/selftests/bpf/probe_helpers.h
diff --git a/tools/testing/selftests/bpf/probe_helpers.c b/tools/testing/selftests/bpf/probe_helpers.c
new file mode 100644
index 000000000000..00467fdda813
--- /dev/null
+++ b/tools/testing/selftests/bpf/probe_helpers.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+#include <unistd.h>
+#include <bpf/bpf.h>
+
+#include "cgroup_helpers.h"
+#include "bpf_util.h"
+#include "../../../include/linux/filter.h"
+
+bool bpf_prog_type_supported(enum bpf_prog_type prog_type)
+{
+ struct bpf_load_program_attr attr;
+ struct bpf_insn insns[] = {
+ BPF_MOV64_IMM(BPF_REG_0, 0),
+ BPF_EXIT_INSN(),
+ };
+ int ret;
+
+ if (prog_type == BPF_PROG_TYPE_UNSPEC)
+ return true;
+
+ memset(&attr, 0, sizeof(attr));
+ attr.prog_type = prog_type;
+ attr.insns = insns;
+ attr.insns_cnt = ARRAY_SIZE(insns);
+ attr.license = "GPL";
+
+ ret = bpf_load_program_xattr(&attr, NULL, 0);
+ if (ret < 0)
+ return false;
+ close(ret);
+
+ return true;
+}
+
+bool bpf_map_type_supported(enum bpf_map_type map_type)
+{
+ int key_size, value_size, max_entries;
+ int fd;
+
+ key_size = sizeof(__u32);
+ value_size = sizeof(__u32);
+ max_entries = 1;
+
+ /* limited set of maps for test_verifier.c and test_maps.c */
+ switch (map_type) {
+ case BPF_MAP_TYPE_SOCKMAP:
+ case BPF_MAP_TYPE_SOCKHASH:
+ case BPF_MAP_TYPE_XSKMAP:
+ break;
+ case BPF_MAP_TYPE_STACK_TRACE:
+ value_size = sizeof(__u64);
+ case BPF_MAP_TYPE_CGROUP_STORAGE:
+ case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
+ key_size = sizeof(struct bpf_cgroup_storage_key);
+ value_size = sizeof(__u64);
+ max_entries = 0;
+ break;
+ default:
+ return true;
+ }
+
+ fd = bpf_create_map(map_type, key_size, value_size, max_entries, 0);
+ if (fd < 0)
+ return false;
+ close(fd);
+
+ return true;
+}
diff --git a/tools/testing/selftests/bpf/probe_helpers.h b/tools/testing/selftests/bpf/probe_helpers.h
new file mode 100644
index 000000000000..9a107d6fe936
--- /dev/null
+++ b/tools/testing/selftests/bpf/probe_helpers.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+#ifndef __PROBE_HELPERS_H
+#define __PROBE_HELPERS_H
+
+#include <linux/bpf.h>
+
+bool bpf_prog_type_supported(enum bpf_prog_type prog_type);
+bool bpf_map_type_supported(enum bpf_map_type map_type);
+
+#endif
--
2.20.0.405.gbc1bbc6f85-goog
next prev parent reply other threads:[~2018-12-17 18:26 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-17 18:25 [PATCH bpf-next v2 0/6] skip verifier/map tests if kernel support is missing Stanislav Fomichev
2018-12-17 18:25 ` Stanislav Fomichev [this message]
2018-12-18 23:21 ` [PATCH bpf-next v2 1/6] selftests/bpf: add map/prog type probe helpers Daniel Borkmann
2018-12-18 23:35 ` Stanislav Fomichev
2018-12-17 18:25 ` [PATCH bpf-next v2 2/6] selftests/bpf: skip sockmap in test_maps if kernel doesn't have support Stanislav Fomichev
2018-12-17 18:25 ` [PATCH bpf-next v2 3/6] selftests/bpf: skip verifier tests for unsupported program types Stanislav Fomichev
2018-12-17 18:25 ` [PATCH bpf-next v2 4/6] selftests/bpf: skip verifier tests for unsupported map types Stanislav Fomichev
2018-12-18 23:25 ` Daniel Borkmann
2018-12-19 0:02 ` Stanislav Fomichev
2018-12-20 20:51 ` Stanislav Fomichev
2018-12-20 22:38 ` Daniel Borkmann
2018-12-20 22:51 ` Stanislav Fomichev
2018-12-17 18:25 ` [PATCH bpf-next v2 5/6] selftests/bpf: mark verifier test that uses bpf_trace_printk as BPF_PROG_TYPE_TRACEPOINT Stanislav Fomichev
2018-12-17 18:25 ` [PATCH bpf-next v2 6/6] bpf: BPF_PROG_TYPE_CGROUP_{SKB,SOCK,SOCK_ADDR} require cgroups enabled Stanislav Fomichev
2018-12-18 21:25 ` [PATCH bpf-next v2 0/6] skip verifier/map tests if kernel support is missing Alexei Starovoitov
2018-12-18 21:30 ` Stanislav Fomichev
2018-12-18 23:18 ` Daniel Borkmann
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=20181217182554.52170-2-sdf@google.com \
--to=sdf@google.com \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=ecree@solarflare.com \
--cc=netdev@vger.kernel.org \
--cc=quentin.monnet@netronome.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).