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>,
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>
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,
Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [PATCH bpf-next v2 06/28] samples/bpf: add new hid_mouse example
Date: Fri, 4 Mar 2022 18:28:30 +0100 [thread overview]
Message-ID: <20220304172852.274126-7-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20220304172852.274126-1-benjamin.tissoires@redhat.com>
Everything should be available in the selftest part of the tree, but
providing an example without uhid and hidraw will be more easy to
follow for users.
Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
changes in v2:
- split the series by bpf/libbpf/hid/selftests and samples
---
samples/bpf/.gitignore | 1 +
samples/bpf/Makefile | 4 ++
samples/bpf/hid_mouse_kern.c | 66 ++++++++++++++++++
samples/bpf/hid_mouse_user.c | 129 +++++++++++++++++++++++++++++++++++
4 files changed, 200 insertions(+)
create mode 100644 samples/bpf/hid_mouse_kern.c
create mode 100644 samples/bpf/hid_mouse_user.c
diff --git a/samples/bpf/.gitignore b/samples/bpf/.gitignore
index 0e7bfdbff80a..65440bd618b2 100644
--- a/samples/bpf/.gitignore
+++ b/samples/bpf/.gitignore
@@ -2,6 +2,7 @@
cpustat
fds_example
hbm
+hid_mouse
ibumad
lathist
lwt_len_hist
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 38638845db9d..84ef458487df 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -60,6 +60,8 @@ tprogs-y += xdp_redirect_map
tprogs-y += xdp_redirect
tprogs-y += xdp_monitor
+tprogs-y += hid_mouse
+
# Libbpf dependencies
LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
@@ -124,6 +126,7 @@ xdp_redirect_cpu-objs := xdp_redirect_cpu_user.o $(XDP_SAMPLE)
xdp_redirect_map-objs := xdp_redirect_map_user.o $(XDP_SAMPLE)
xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE)
xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE)
+hid_mouse-objs := hid_mouse_user.o
# Tell kbuild to always build the programs
always-y := $(tprogs-y)
@@ -181,6 +184,7 @@ always-y += ibumad_kern.o
always-y += hbm_out_kern.o
always-y += hbm_edt_kern.o
always-y += xdpsock_kern.o
+always-y += hid_mouse_kern.o
ifeq ($(ARCH), arm)
# Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
diff --git a/samples/bpf/hid_mouse_kern.c b/samples/bpf/hid_mouse_kern.c
new file mode 100644
index 000000000000..c24a12e06b40
--- /dev/null
+++ b/samples/bpf/hid_mouse_kern.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2021 Benjamin Tissoires
+ */
+#include <linux/version.h>
+#include <uapi/linux/bpf.h>
+#include <uapi/linux/bpf_hid.h>
+#include <bpf/bpf_helpers.h>
+
+SEC("hid/device_event")
+int hid_y_event(struct hid_bpf_ctx *ctx)
+{
+ s16 y;
+
+ bpf_printk("event: %02x size: %d", ctx->type, ctx->size);
+ bpf_printk("incoming event: %02x %02x %02x",
+ ctx->data[0],
+ ctx->data[1],
+ ctx->data[2]);
+ bpf_printk(" %02x %02x %02x",
+ ctx->data[3],
+ ctx->data[4],
+ ctx->data[5]);
+ bpf_printk(" %02x %02x %02x",
+ ctx->data[6],
+ ctx->data[7],
+ ctx->data[8]);
+
+ y = ctx->data[3] | (ctx->data[4] << 8);
+
+ y = -y;
+
+ ctx->data[3] = y & 0xFF;
+ ctx->data[4] = (y >> 8) & 0xFF;
+
+ bpf_printk("modified event: %02x %02x %02x",
+ ctx->data[0],
+ ctx->data[1],
+ ctx->data[2]);
+ bpf_printk(" %02x %02x %02x",
+ ctx->data[3],
+ ctx->data[4],
+ ctx->data[5]);
+ bpf_printk(" %02x %02x %02x",
+ ctx->data[6],
+ ctx->data[7],
+ ctx->data[8]);
+
+ return 0;
+}
+
+SEC("hid/device_event")
+int hid_x_event(struct hid_bpf_ctx *ctx)
+{
+ s16 x;
+
+ x = ctx->data[1] | (ctx->data[2] << 8);
+
+ x = -x;
+
+ ctx->data[1] = x & 0xFF;
+ ctx->data[2] = (x >> 8) & 0xFF;
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
+u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/hid_mouse_user.c b/samples/bpf/hid_mouse_user.c
new file mode 100644
index 000000000000..d4f37caca2fa
--- /dev/null
+++ b/samples/bpf/hid_mouse_user.c
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2021 Benjamin Tissoires
+ */
+#include <linux/bpf.h>
+#include <linux/if_link.h>
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <sys/resource.h>
+
+#include "bpf_util.h"
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+
+static char *sysfs_path;
+static int sysfs_fd;
+static int prog_count;
+
+struct prog {
+ int fd;
+ struct bpf_link *link;
+ enum bpf_attach_type type;
+};
+
+static struct prog progs[10];
+
+static void int_exit(int sig)
+{
+ for (prog_count--; prog_count >= 0; prog_count--)
+ bpf_link__destroy(progs[prog_count].link);
+
+ close(sysfs_fd);
+ exit(0);
+}
+
+static void usage(const char *prog)
+{
+ fprintf(stderr,
+ "%s: %s /sys/bus/hid/devices/0BUS:0VID:0PID:00ID/uevent\n\n",
+ __func__, prog);
+}
+
+int main(int argc, char **argv)
+{
+ struct bpf_prog_info info = {};
+ __u32 info_len = sizeof(info);
+ const char *optstr = "";
+ struct bpf_object *obj;
+ struct bpf_program *prog;
+ int opt;
+ char filename[256];
+ int err;
+
+ while ((opt = getopt(argc, argv, optstr)) != -1) {
+ switch (opt) {
+ default:
+ usage(basename(argv[0]));
+ return 1;
+ }
+ }
+
+ if (optind == argc) {
+ usage(basename(argv[0]));
+ return 1;
+ }
+
+ sysfs_path = argv[optind];
+ if (!sysfs_path) {
+ perror("sysfs");
+ return 1;
+ }
+
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
+ obj = bpf_object__open_file(filename, NULL);
+ err = libbpf_get_error(obj);
+ if (err) {
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
+ obj = NULL;
+ err = 1;
+ goto cleanup;
+ }
+
+ /* load BPF program */
+ err = bpf_object__load(obj);
+ if (err) {
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
+ goto cleanup;
+ }
+
+ sysfs_fd = open(sysfs_path, O_RDONLY);
+
+ bpf_object__for_each_program(prog, obj) {
+ progs[prog_count].fd = bpf_program__fd(prog);
+ progs[prog_count].type = bpf_program__get_expected_attach_type(prog);
+ progs[prog_count].link = bpf_program__attach_hid(prog, sysfs_fd);
+ if (libbpf_get_error(progs[prog_count].link)) {
+ fprintf(stderr, "bpf_prog_attach: err=%m\n");
+ progs[prog_count].fd = 0;
+ progs[prog_count].link = NULL;
+ goto cleanup;
+ }
+ prog_count++;
+ }
+
+ signal(SIGINT, int_exit);
+ signal(SIGTERM, int_exit);
+
+ err = bpf_obj_get_info_by_fd(progs[0].fd, &info, &info_len);
+ if (err) {
+ printf("can't get prog info - %s\n", strerror(errno));
+ goto cleanup;
+ }
+
+ while (1)
+ ;
+
+ cleanup:
+ for (prog_count--; prog_count >= 0; prog_count--)
+ bpf_link__destroy(progs[prog_count].link);
+
+ bpf_object__close(obj);
+ return err;
+}
--
2.35.1
next prev parent reply other threads:[~2022-03-04 17:31 UTC|newest]
Thread overview: 73+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-04 17:28 [PATCH bpf-next v2 00/28] Introduce eBPF support for HID devices Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 01/28] bpf: add new is_sys_admin_prog_type() helper Benjamin Tissoires
2022-03-04 23:12 ` Song Liu
2022-03-05 10:07 ` Benjamin Tissoires
2022-03-05 16:58 ` Sean Young
2022-03-07 18:26 ` Song Liu
2022-03-07 18:23 ` Song Liu
2022-03-09 8:27 ` Sean Young
2022-03-04 17:28 ` [PATCH bpf-next v2 02/28] bpf: introduce hid program type Benjamin Tissoires
2022-03-04 18:21 ` Greg KH
2022-03-07 17:57 ` Benjamin Tissoires
2022-03-05 0:02 ` Song Liu
2022-03-07 18:39 ` Benjamin Tissoires
2022-03-08 0:56 ` Song Liu
2022-03-08 9:20 ` Benjamin Tissoires
2022-03-11 17:16 ` Benjamin Tissoires
2022-03-05 0:20 ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 03/28] HID: hook up with bpf Benjamin Tissoires
2022-03-04 18:24 ` Greg KH
2022-03-05 0:23 ` Song Liu
2022-03-15 16:29 ` Tero Kristo
2022-03-04 17:28 ` [PATCH bpf-next v2 04/28] libbpf: add HID program type and API Benjamin Tissoires
2022-03-05 0:31 ` Song Liu
2022-03-08 1:30 ` Andrii Nakryiko
2022-03-08 1:38 ` Song Liu
2022-03-08 5:52 ` Andrii Nakryiko
2022-03-04 17:28 ` [PATCH bpf-next v2 05/28] selftests/bpf: add tests for the HID-bpf initial implementation Benjamin Tissoires
2022-03-05 0:41 ` Song Liu
2022-03-05 10:10 ` Benjamin Tissoires
2022-03-04 17:28 ` Benjamin Tissoires [this message]
2022-03-04 18:26 ` [PATCH bpf-next v2 06/28] samples/bpf: add new hid_mouse example Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 07/28] bpf/hid: add a new attach type to change the report descriptor Benjamin Tissoires
2022-03-04 18:32 ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 08/28] HID: allow to change the report descriptor from an eBPF program Benjamin Tissoires
2022-03-04 18:32 ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 09/28] libbpf: add new attach type BPF_HID_RDESC_FIXUP Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 10/28] selftests/bpf: add report descriptor fixup tests Benjamin Tissoires
2022-03-04 18:34 ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 11/28] samples/bpf: add a report descriptor fixup Benjamin Tissoires
2022-03-04 18:36 ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 12/28] bpf/hid: add hid_{get|set}_data helpers Benjamin Tissoires
2022-03-04 18:37 ` Greg KH
2022-03-04 18:41 ` Greg KH
2022-03-05 10:33 ` Benjamin Tissoires
2022-03-05 10:47 ` Greg KH
2022-03-11 0:40 ` Song Liu
2022-03-11 15:08 ` Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 13/28] HID: bpf: implement hid_bpf_get|set_data Benjamin Tissoires
2022-03-04 18:39 ` Greg KH
2022-03-04 17:28 ` [PATCH bpf-next v2 14/28] selftests/bpf: add tests for hid_{get|set}_data helpers Benjamin Tissoires
2022-03-15 16:49 ` Tero Kristo
2022-03-15 17:02 ` Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 15/28] bpf/hid: add new BPF type to trigger commands from userspace Benjamin Tissoires
2022-03-11 0:46 ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 16/28] libbpf: add new attach type BPF_HID_USER_EVENT Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 17/28] selftests/bpf: add test for user call of HID bpf programs Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 18/28] selftests/bpf: hid: rely on uhid event to know if a test device is ready Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 19/28] bpf/hid: add bpf_hid_raw_request helper function Benjamin Tissoires
2022-03-11 0:50 ` Song Liu
2022-03-04 17:28 ` [PATCH bpf-next v2 20/28] HID: add implementation of bpf_hid_raw_request Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 21/28] selftests/bpf: add tests for bpf_hid_hw_request Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 22/28] bpf/verifier: prevent non GPL programs to be loaded against HID Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 23/28] HID: bpf: compute only the required buffer size for the device Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 24/28] HID: bpf: only call hid_bpf_raw_event() if a ctx is available Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 25/28] bpf/hid: Add a flag to add the program at the beginning of the list Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 26/28] libbpf: add handling for BPF_F_INSERT_HEAD in HID programs Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 27/28] selftests/bpf: Add a test for BPF_F_INSERT_HEAD Benjamin Tissoires
2022-03-04 17:28 ` [PATCH bpf-next v2 28/28] samples/bpf: fix bpf_program__attach_hid() api change Benjamin Tissoires
2022-03-05 1:13 ` [PATCH bpf-next v2 00/28] Introduce eBPF support for HID devices Song Liu
2022-03-05 10:23 ` Benjamin Tissoires
2022-03-07 18:11 ` Song Liu
2022-03-08 13:37 ` Benjamin Tissoires
2022-03-15 17:04 ` 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=20220304172852.274126-7-benjamin.tissoires@redhat.com \
--to=benjamin.tissoires@redhat.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--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-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--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).