tools/testing/selftests/hid/hid_bpf.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/hid/hid_bpf.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/hid/hid_bpf.c
Extension
.c
Size
23492 bytes
Lines
912
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

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;
};

FIXTURE(hid_bpf) {
	struct uhid_device hid;
	int hidraw_fd;
	struct hid *skel;
	struct bpf_link *hid_links[3]; /* max number of programs loaded in a single test */
};
static void detach_bpf(FIXTURE_DATA(hid_bpf) * self)
{
	int i;

	if (self->hidraw_fd)
		close(self->hidraw_fd);
	self->hidraw_fd = 0;

	if (!self->skel)
		return;

	hid__detach(self->skel);

	for (i = 0; i < ARRAY_SIZE(self->hid_links); i++) {
		if (self->hid_links[i])
			bpf_link__destroy(self->hid_links[i]);
	}

	hid__destroy(self->skel);
	self->skel = NULL;
}

FIXTURE_TEARDOWN(hid_bpf) {
	void *uhid_err;

	uhid_destroy(_metadata, &self->hid);

	detach_bpf(self);
	pthread_join(self->hid.tid, &uhid_err);
}
#define TEARDOWN_LOG(fmt, ...) do { \
	TH_LOG(fmt, ##__VA_ARGS__); \
	hid_bpf_teardown(_metadata, self, variant); \
} while (0)

FIXTURE_SETUP(hid_bpf)
{
	int err;

	err = setup_uhid(_metadata, &self->hid, BUS_USB, 0x0001, 0x0a36, rdesc, sizeof(rdesc));
	ASSERT_OK(err);
}

struct test_program {
	const char *name;
	int insert_head;
};
#define LOAD_PROGRAMS(progs) \
	load_programs(progs, ARRAY_SIZE(progs), _metadata, self, variant)
#define LOAD_BPF \
	load_programs(NULL, 0, _metadata, self, variant)
static void load_programs(const struct test_program programs[],
			  const size_t progs_count,
			  struct __test_metadata *_metadata,
			  FIXTURE_DATA(hid_bpf) * self,
			  const FIXTURE_VARIANT(hid_bpf) * variant)
{
	struct bpf_map *iter_map;
	int err = -EINVAL;

	ASSERT_LE(progs_count, ARRAY_SIZE(self->hid_links))
		TH_LOG("too many programs are to be loaded");

	/* open the bpf file */
	self->skel = hid__open();
	ASSERT_OK_PTR(self->skel) TEARDOWN_LOG("Error while calling hid__open");

	for (int i = 0; i < progs_count; i++) {
		struct bpf_program *prog;
		struct bpf_map *map;
		int *ops_hid_id;

		prog = bpf_object__find_program_by_name(*self->skel->skeleton->obj,
							programs[i].name);
		ASSERT_OK_PTR(prog) TH_LOG("can not find program by name '%s'", programs[i].name);

Annotation

Implementation Notes