From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Greg KH <gregkh@linuxfoundation.org>,
Jiri Kosina <jikos@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
Yonghong Song <yhs@fb.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@kernel.org>, Shuah Khan <shuah@kernel.org>,
Dave Marchevsky <davemarchevsky@fb.com>,
Joe Stringer <joe@cilium.io>, Jonathan Corbet <corbet@lwn.net>
Cc: Tero Kristo <tero.kristo@linux.intel.com>,
linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
netdev@vger.kernel.org, bpf@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org,
Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [PATCH bpf-next v7 18/24] selftests/bpf: add tests for bpf_hid_hw_request
Date: Thu, 21 Jul 2022 17:36:19 +0200 [thread overview]
Message-ID: <20220721153625.1282007-19-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20220721153625.1282007-1-benjamin.tissoires@redhat.com>
Add tests for the newly implemented function.
We test here only the GET_REPORT part because the other calls are pure
HID protocol and won't infer the result of the test of the bpf hook.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
no changes in v7
changes in v6:
- fixed copy/paste in prog_tests when calling ASSERT_OK
- removed the need for memcpy now that kfuncs can access ctx
changes in v5:
- use the new hid_bpf_allocate_context() API
- remove the need for ctx_in for syscall TEST_RUN
changes in v3:
- use the new hid_get_data API
- directly use HID_FEATURE_REPORT and HID_REQ_GET_REPORT from uapi
changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
---
tools/testing/selftests/bpf/prog_tests/hid.c | 114 ++++++++++++++++---
tools/testing/selftests/bpf/progs/hid.c | 43 +++++++
2 files changed, 139 insertions(+), 18 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/hid.c b/tools/testing/selftests/bpf/prog_tests/hid.c
index 47bc0a30c275..19172d3e0f44 100644
--- a/tools/testing/selftests/bpf/prog_tests/hid.c
+++ b/tools/testing/selftests/bpf/prog_tests/hid.c
@@ -77,12 +77,23 @@ static unsigned char rdesc[] = {
0xc0, /* END_COLLECTION */
};
+static u8 feature_data[] = { 1, 2 };
+
struct attach_prog_args {
int prog_fd;
unsigned int hid;
int retval;
};
+struct hid_hw_request_syscall_args {
+ __u8 data[10];
+ unsigned int hid;
+ int retval;
+ size_t size;
+ enum hid_report_type type;
+ __u8 request_type;
+};
+
static pthread_mutex_t uhid_started_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t uhid_started = PTHREAD_COND_INITIALIZER;
@@ -142,7 +153,7 @@ static void destroy(int fd)
static int uhid_event(int fd)
{
- struct uhid_event ev;
+ struct uhid_event ev, answer;
ssize_t ret;
memset(&ev, 0, sizeof(ev));
@@ -183,6 +194,15 @@ static int uhid_event(int fd)
break;
case UHID_GET_REPORT:
fprintf(stderr, "UHID_GET_REPORT from uhid-dev\n");
+
+ answer.type = UHID_GET_REPORT_REPLY;
+ answer.u.get_report_reply.id = ev.u.get_report.id;
+ answer.u.get_report_reply.err = ev.u.get_report.rnum == 1 ? 0 : -EIO;
+ answer.u.get_report_reply.size = sizeof(feature_data);
+ memcpy(answer.u.get_report_reply.data, feature_data, sizeof(feature_data));
+
+ uhid_write(fd, &answer);
+
break;
case UHID_SET_REPORT:
fprintf(stderr, "UHID_SET_REPORT from uhid-dev\n");
@@ -391,6 +411,7 @@ static int open_hidraw(int dev_id)
struct test_params {
struct hid *skel;
int hidraw_fd;
+ int hid_id;
};
static int prep_test(int dev_id, const char *prog_name, struct test_params *test_data)
@@ -419,27 +440,33 @@ static int prep_test(int dev_id, const char *prog_name, struct test_params *test
if (!ASSERT_OK_PTR(hid_skel, "hid_skel_open"))
goto cleanup;
- prog = bpf_object__find_program_by_name(*hid_skel->skeleton->obj, prog_name);
- if (!ASSERT_OK_PTR(prog, "find_prog_by_name"))
- goto cleanup;
+ if (prog_name) {
+ prog = bpf_object__find_program_by_name(*hid_skel->skeleton->obj, prog_name);
+ if (!ASSERT_OK_PTR(prog, "find_prog_by_name"))
+ goto cleanup;
- bpf_program__set_autoload(prog, true);
+ bpf_program__set_autoload(prog, true);
- err = hid__load(hid_skel);
- if (!ASSERT_OK(err, "hid_skel_load"))
- goto cleanup;
+ err = hid__load(hid_skel);
+ if (!ASSERT_OK(err, "hid_skel_load"))
+ goto cleanup;
- attach_fd = bpf_program__fd(hid_skel->progs.attach_prog);
- if (!ASSERT_GE(attach_fd, 0, "locate attach_prog")) {
- err = attach_fd;
- goto cleanup;
- }
+ attach_fd = bpf_program__fd(hid_skel->progs.attach_prog);
+ if (!ASSERT_GE(attach_fd, 0, "locate attach_prog")) {
+ err = attach_fd;
+ goto cleanup;
+ }
- args.prog_fd = bpf_program__fd(prog);
- err = bpf_prog_test_run_opts(attach_fd, &tattr);
- snprintf(buf, sizeof(buf), "attach_hid(%s)", prog_name);
- if (!ASSERT_EQ(args.retval, 0, buf))
- goto cleanup;
+ args.prog_fd = bpf_program__fd(prog);
+ err = bpf_prog_test_run_opts(attach_fd, &tattr);
+ snprintf(buf, sizeof(buf), "attach_hid(%s)", prog_name);
+ if (!ASSERT_EQ(args.retval, 0, buf))
+ goto cleanup;
+ } else {
+ err = hid__load(hid_skel);
+ if (!ASSERT_OK(err, "hid_skel_load"))
+ goto cleanup;
+ }
hidraw_fd = open_hidraw(dev_id);
if (!ASSERT_GE(hidraw_fd, 0, "open_hidraw"))
@@ -447,6 +474,7 @@ static int prep_test(int dev_id, const char *prog_name, struct test_params *test
test_data->skel = hid_skel;
test_data->hidraw_fd = hidraw_fd;
+ test_data->hid_id = hid_id;
return 0;
@@ -693,6 +721,54 @@ static int test_hid_change_report(int uhid_fd, int dev_id)
return ret;
}
+/*
+ * Attach hid_user_raw_request to the given uhid device,
+ * call the bpf program from userspace
+ * check that the program is called and does the expected.
+ */
+static int test_hid_user_raw_request_call(int uhid_fd, int dev_id)
+{
+ struct test_params params;
+ int err, prog_fd;
+ int ret = -1;
+ struct hid_hw_request_syscall_args args = {
+ .retval = -1,
+ .type = HID_FEATURE_REPORT,
+ .request_type = HID_REQ_GET_REPORT,
+ .size = 10,
+ };
+ DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs,
+ .ctx_in = &args,
+ .ctx_size_in = sizeof(args),
+ );
+
+ err = prep_test(dev_id, NULL, ¶ms);
+ if (!ASSERT_EQ(err, 0, "prep_test()"))
+ goto cleanup;
+
+ args.hid = params.hid_id;
+ args.data[0] = 1; /* report ID */
+
+ prog_fd = bpf_program__fd(params.skel->progs.hid_user_raw_request);
+
+ err = bpf_prog_test_run_opts(prog_fd, &tattrs);
+ if (!ASSERT_EQ(err, 0, "bpf_prog_test_run_opts"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(args.retval, 2, "bpf_prog_test_run_opts_retval"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(args.data[1], 2, "hid_user_raw_request_check_in"))
+ goto cleanup;
+
+ ret = 0;
+
+cleanup:
+ cleanup_test(¶ms);
+
+ return ret;
+}
+
void serial_test_hid_bpf(void)
{
int err, uhid_fd;
@@ -720,6 +796,8 @@ void serial_test_hid_bpf(void)
ASSERT_OK(err, "hid_attach_detach");
err = test_hid_change_report(uhid_fd, dev_id);
ASSERT_OK(err, "hid_change_report");
+ err = test_hid_user_raw_request_call(uhid_fd, dev_id);
+ ASSERT_OK(err, "hid_user_raw_request");
destroy(uhid_fd);
diff --git a/tools/testing/selftests/bpf/progs/hid.c b/tools/testing/selftests/bpf/progs/hid.c
index ee7529c47ad8..0be319c11575 100644
--- a/tools/testing/selftests/bpf/progs/hid.c
+++ b/tools/testing/selftests/bpf/progs/hid.c
@@ -10,6 +10,13 @@ extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
unsigned int offset,
const size_t __sz) __ksym;
extern int hid_bpf_attach_prog(unsigned int hid_id, int prog_fd, u32 flags) __ksym;
+extern struct hid_bpf_ctx *hid_bpf_allocate_context(unsigned int hid_id) __ksym;
+extern void hid_bpf_release_context(struct hid_bpf_ctx *ctx) __ksym;
+extern int hid_bpf_hw_request(struct hid_bpf_ctx *ctx,
+ __u8 *data,
+ size_t buf__sz,
+ enum hid_report_type type,
+ enum hid_class_request reqtype) __ksym;
struct attach_prog_args {
int prog_fd;
@@ -56,3 +63,39 @@ int attach_prog(struct attach_prog_args *ctx)
0);
return 0;
}
+
+struct hid_hw_request_syscall_args {
+ /* data needs to come at offset 0 so we can do a memcpy into it */
+ __u8 data[10];
+ unsigned int hid;
+ int retval;
+ size_t size;
+ enum hid_report_type type;
+ __u8 request_type;
+};
+
+SEC("syscall")
+int hid_user_raw_request(struct hid_hw_request_syscall_args *args)
+{
+ struct hid_bpf_ctx *ctx;
+ const size_t size = args->size;
+ int i, ret = 0;
+
+ if (size > sizeof(args->data))
+ return -7; /* -E2BIG */
+
+ ctx = hid_bpf_allocate_context(args->hid);
+ if (!ctx)
+ return -1; /* EPERM check */
+
+ ret = hid_bpf_hw_request(ctx,
+ args->data,
+ size,
+ args->type,
+ args->request_type);
+ args->retval = ret;
+
+ hid_bpf_release_context(ctx);
+
+ return 0;
+}
--
2.36.1
next prev parent reply other threads:[~2022-07-21 15:41 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-21 15:36 [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 01/24] selftests/bpf: fix config for CLS_BPF Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 02/24] bpf/verifier: allow kfunc to read user provided context Benjamin Tissoires
2022-07-21 20:15 ` Kumar Kartikeya Dwivedi
2022-07-22 8:43 ` Benjamin Tissoires
2022-07-22 8:45 ` [PATCH bpf-next v8 " Benjamin Tissoires
2022-07-22 16:16 ` Alexei Starovoitov
2022-07-25 16:36 ` Benjamin Tissoires
2022-08-24 9:57 ` Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 03/24] bpf/verifier: do not clear meta in check_mem_size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 04/24] selftests/bpf: add test for accessing ctx from syscall program type Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 05/24] bpf/verifier: allow kfunc to return an allocated mem Benjamin Tissoires
2022-07-21 21:05 ` Kumar Kartikeya Dwivedi
2022-07-21 15:36 ` [PATCH bpf-next v7 06/24] selftests/bpf: Add tests for kfunc returning a memory pointer Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 07/24] bpf: prepare for more bpf syscall to be used from kernel and user space Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 08/24] libbpf: add map_get_fd_by_id and map_delete_elem in light skeleton Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 09/24] HID: core: store the unique system identifier in hid_device Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 10/24] HID: export hid_report_type to uapi Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 11/24] HID: convert defines of HID class requests into a proper enum Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 12/24] HID: Kconfig: split HID support and hid-core compilation Benjamin Tissoires
2022-07-28 14:14 ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 13/24] HID: initial BPF implementation Benjamin Tissoires
2022-07-28 14:15 ` Greg KH
2022-08-24 9:41 ` Benjamin Tissoires
2022-07-28 14:19 ` Greg KH
2022-08-24 9:48 ` Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 14/24] selftests/bpf: add tests for the HID-bpf initial implementation Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 15/24] HID: bpf: allocate data memory for device_event BPF programs Benjamin Tissoires
2022-07-28 15:10 ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 16/24] selftests/bpf/hid: add test to change the report size Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 17/24] HID: bpf: introduce hid_hw_request() Benjamin Tissoires
2022-07-28 15:10 ` Greg KH
2022-07-21 15:36 ` Benjamin Tissoires [this message]
2022-07-21 15:36 ` [PATCH bpf-next v7 19/24] HID: bpf: allow to change the report descriptor Benjamin Tissoires
2022-07-28 15:10 ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 20/24] selftests/bpf: add report descriptor fixup tests Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 21/24] selftests/bpf: Add a test for BPF_F_INSERT_HEAD Benjamin Tissoires
2022-07-21 15:36 ` [PATCH bpf-next v7 22/24] samples/bpf: add new hid_mouse example Benjamin Tissoires
2022-07-28 15:13 ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 23/24] HID: bpf: add Surface Dial example Benjamin Tissoires
2022-07-28 15:12 ` Greg KH
2022-07-21 15:36 ` [PATCH bpf-next v7 24/24] Documentation: add HID-BPF docs Benjamin Tissoires
2022-08-25 2:52 ` Bagas Sanjaya
2022-08-30 13:30 ` Benjamin Tissoires
2022-08-03 7:41 ` [PATCH bpf-next v7 00/24] Introduce eBPF support for HID devices Tero Kristo
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=20220721153625.1282007-19-benjamin.tissoires@redhat.com \
--to=benjamin.tissoires@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=davemarchevsky@fb.com \
--cc=gregkh@linuxfoundation.org \
--cc=jikos@kernel.org \
--cc=joe@cilium.io \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=shuah@kernel.org \
--cc=songliubraving@fb.com \
--cc=tero.kristo@linux.intel.com \
--cc=yhs@fb.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).