fexit_stress.c 2.0 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Facebook */
#include <test_progs.h>

5 6
/* that's kernel internal BPF_MAX_TRAMP_PROGS define */
#define CNT 38
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

void test_fexit_stress(void)
{
	char test_skb[128] = {};
	int fexit_fd[CNT] = {};
	int link_fd[CNT] = {};
	__u32 duration = 0;
	char error[4096];
	__u32 prog_ret;
	int err, i, filter_fd;

	const struct bpf_insn trace_program[] = {
		BPF_MOV64_IMM(BPF_REG_0, 0),
		BPF_EXIT_INSN(),
	};

23
	LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
24
		.expected_attach_type = BPF_TRACE_FEXIT,
25 26 27
		.log_buf = error,
		.log_size = sizeof(error),
	);
28 29 30 31 32 33

	const struct bpf_insn skb_program[] = {
		BPF_MOV64_IMM(BPF_REG_0, 0),
		BPF_EXIT_INSN(),
	};

34 35 36 37
	LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
		.log_buf = error,
		.log_size = sizeof(error),
	);
38 39

	err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
40
					 trace_opts.expected_attach_type);
41 42
	if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err))
		goto out;
43
	trace_opts.attach_btf_id = err;
44 45

	for (i = 0; i < CNT; i++) {
46 47 48 49
		fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
					    trace_program,
					    sizeof(trace_program) / sizeof(struct bpf_insn),
					    &trace_opts);
50 51 52 53 54 55 56 57 58
		if (CHECK(fexit_fd[i] < 0, "fexit loaded",
			  "failed: %d errno %d\n", fexit_fd[i], errno))
			goto out;
		link_fd[i] = bpf_raw_tracepoint_open(NULL, fexit_fd[i]);
		if (CHECK(link_fd[i] < 0, "fexit attach failed",
			  "prog %d failed: %d err %d\n", i, link_fd[i], errno))
			goto out;
	}

59 60 61
	filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
				  skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
				  &skb_opts);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
		  filter_fd, errno))
		goto out;

	err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0,
				0, &prog_ret, 0);
	close(filter_fd);
	CHECK_FAIL(err);
out:
	for (i = 0; i < CNT; i++) {
		if (link_fd[i])
			close(link_fd[i]);
		if (fexit_fd[i])
			close(fexit_fd[i]);
	}
}