All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hou Tao <houtao1@huawei.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>
Cc: <netdev@vger.kernel.org>, <bpf@vger.kernel.org>, <houtao1@huawei.com>
Subject: [RFC PATCH bpf-next 3/3] selftests/bpf: add test for BPF STRUCT_OPS
Date: Wed, 15 Sep 2021 11:37:53 +0800	[thread overview]
Message-ID: <20210915033753.1201597-4-houtao1@huawei.com> (raw)
In-Reply-To: <20210915033753.1201597-1-houtao1@huawei.com>

Add two test cases for BPF STRUCT_OPS: one to check the return
value of BPF_PROG_TYPE_STRUCT_OPS is returned, another to check
the returned value through output parameter is expected.

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 .../selftests/bpf/prog_tests/bpf_dummy_ops.c  | 95 +++++++++++++++++++
 .../selftests/bpf/progs/bpf_dummy_ops.c       | 34 +++++++
 2 files changed, 129 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_dummy_ops.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_dummy_ops.c

diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_dummy_ops.c b/tools/testing/selftests/bpf/prog_tests/bpf_dummy_ops.c
new file mode 100644
index 000000000000..d9a45579c716
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_dummy_ops.c
@@ -0,0 +1,95 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
+#include <linux/err.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <test_progs.h>
+
+#include "bpf_dummy_ops.skel.h"
+
+#define OPS_CTL_CMD_SIZE 64
+
+static void do_ctl(const char *cmd)
+{
+	int duration = 0;
+	int fd;
+	size_t len;
+	ssize_t wr;
+
+	fd = open("/sys/kernel/bpf_test/dummy_ops_ctl", O_WRONLY);
+	if (CHECK(fd < 0, "open", "open errno %d", errno))
+		goto out;
+
+	len = strlen(cmd);
+	wr = write(fd, cmd, len);
+	if (CHECK(wr != len, "write", "write cmd %s errno %d", cmd, errno))
+		goto out;
+out:
+	if (fd >= 0)
+		close(fd);
+}
+
+static void test_ret_value(void)
+{
+	int duration = 0;
+	struct bpf_dummy_ops *skel;
+	struct bpf_link *link;
+	char cmd[OPS_CTL_CMD_SIZE];
+
+	skel = bpf_dummy_ops__open_and_load();
+	if (CHECK(!skel, "bpf_dummy_ops__open_and_load", "failed\n"))
+		return;
+
+	skel->bss->init_ret = 1024;
+	link = bpf_map__attach_struct_ops(skel->maps.dummy);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto out;
+
+	snprintf(cmd, sizeof(cmd), "init_1 %d", skel->bss->init_ret);
+	do_ctl(cmd);
+out:
+	bpf_link__destroy(link);
+	bpf_dummy_ops__destroy(skel);
+}
+
+static void test_ret_by_ptr(void)
+{
+	int duration = 0;
+	struct bpf_dummy_ops *skel;
+	struct bpf_link *link;
+	char cmd[OPS_CTL_CMD_SIZE];
+
+	skel = bpf_dummy_ops__open_and_load();
+	if (CHECK(!skel, "bpf_dummy_ops__open_and_load", "failed\n"))
+		return;
+
+	skel->bss->state_val = 0x5a;
+	link = bpf_map__attach_struct_ops(skel->maps.dummy);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
+		goto out;
+
+	snprintf(cmd, sizeof(cmd), "init_2 %d", skel->bss->state_val);
+	do_ctl(cmd);
+out:
+	bpf_link__destroy(link);
+	bpf_dummy_ops__destroy(skel);
+}
+
+void test_bpf_dummy_ops(void)
+{
+	if (!env.has_testmod) {
+		test__skip();
+		return;
+	}
+
+	if (test__start_subtest("ret_value"))
+		test_ret_value();
+	if (test__start_subtest("ret_by_ptr"))
+		test_ret_by_ptr();
+}
diff --git a/tools/testing/selftests/bpf/progs/bpf_dummy_ops.c b/tools/testing/selftests/bpf/progs/bpf_dummy_ops.c
new file mode 100644
index 000000000000..e414532b3fc0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/bpf_dummy_ops.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
+#include <stddef.h>
+#include <linux/bpf.h>
+#include <linux/types.h>
+#include <linux/stddef.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+struct bpf_dummy_ops_state {
+	int val;
+};
+
+struct bpf_dummy_ops {
+	int (*init)(void);
+};
+
+int state_val = 0;
+int init_ret = 0;
+
+SEC("struct_ops/dummy_ops_init")
+int BPF_PROG(dummy_ops_init, struct bpf_dummy_ops_state *state)
+{
+	if (state)
+		state->val = state_val;
+	return init_ret;
+}
+
+SEC(".struct_ops")
+struct bpf_dummy_ops dummy = {
+	.init = (void *)dummy_ops_init,
+};
+
+char _license[] SEC("license") = "GPL";
-- 
2.29.2


      parent reply	other threads:[~2021-09-15  3:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-15  3:37 [RFC PATCH bpf-next 0/3] introduce dummy BPF STRUCT_OPS Hou Tao
2021-09-15  3:37 ` [RFC PATCH bpf-next 1/3] bpf: add dummy BPF STRUCT_OPS for test purpose Hou Tao
2021-09-15 20:58   ` Martin KaFai Lau
2021-09-18  2:03     ` Hou Tao
2021-09-16  3:25   ` kernel test robot
2021-09-16  7:09   ` kernel test robot
2021-09-16  7:09     ` kernel test robot
2021-09-15  3:37 ` [RFC PATCH bpf-next 2/3] selftests/bpf: call dummy struct_ops in bpf_testmode Hou Tao
2021-09-15  3:37 ` Hou Tao [this message]

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=20210915033753.1201597-4-houtao1@huawei.com \
    --to=houtao1@huawei.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.