bpf_verif_scale.c 3.0 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2019 Facebook
#include <test_progs.h>
static int libbpf_debug_print(enum libbpf_print_level level,
			      const char *format, va_list args)
{
7 8 9 10
	if (level != LIBBPF_DEBUG) {
		test__vprintf(format, args);
		return 0;
	}
11 12 13

	if (!strstr(format, "verifier log"))
		return 0;
14 15
	test__vprintf("%s", args);
	return 0;
16 17
}

18
static int check_load(const char *file, enum bpf_prog_type type)
19 20
{
	struct bpf_prog_load_attr attr;
21
	struct bpf_object *obj = NULL;
22 23 24 25
	int err, prog_fd;

	memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
	attr.file = file;
26
	attr.prog_type = type;
27
	attr.log_level = 4;
28
	attr.prog_flags = BPF_F_TEST_RND_HI32;
29 30 31 32 33 34 35
	err = bpf_prog_load_xattr(&attr, &obj, &prog_fd);
	bpf_object__close(obj);
	if (err)
		error_cnt++;
	return err;
}

36 37 38 39 40 41
struct scale_test_def {
	const char *file;
	enum bpf_prog_type attach_type;
	bool fails;
};

42 43
void test_bpf_verif_scale(void)
{
44 45 46 47 48 49 50
	struct scale_test_def tests[] = {
		{ "loop3.o", BPF_PROG_TYPE_RAW_TRACEPOINT, true /* fails */ },

		{ "test_verif_scale1.o", BPF_PROG_TYPE_SCHED_CLS },
		{ "test_verif_scale2.o", BPF_PROG_TYPE_SCHED_CLS },
		{ "test_verif_scale3.o", BPF_PROG_TYPE_SCHED_CLS },

51
		/* full unroll by llvm */
52 53 54
		{ "pyperf50.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
		{ "pyperf100.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
		{ "pyperf180.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
55 56 57 58 59 60 61

		/* partial unroll. llvm will unroll loop ~150 times.
		 * C loop count -> 600.
		 * Asm loop count -> 4.
		 * 16k insns in loop body.
		 * Total of 5 such loops. Total program size ~82k insns.
		 */
62
		{ "pyperf600.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
63 64 65 66 67 68 69

		/* no unroll at all.
		 * C loop count -> 600.
		 * ASM loop count -> 600.
		 * ~110 insns in loop body.
		 * Total of 5 such loops. Total program size ~1500 insns.
		 */
70
		{ "pyperf600_nounroll.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
71

72 73
		{ "loop1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
		{ "loop2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
74
		{ "loop4.o", BPF_PROG_TYPE_SCHED_CLS },
75
		{ "loop5.o", BPF_PROG_TYPE_SCHED_CLS },
76 77 78 79 80

		/* partial unroll. 19k insn in a loop.
		 * Total program size 20.8k insn.
		 * ~350k processed_insns
		 */
81
		{ "strobemeta.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
82 83

		/* no unroll, tiny loops */
84 85 86 87 88 89 90 91
		{ "strobemeta_nounroll1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
		{ "strobemeta_nounroll2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },

		{ "test_sysctl_loop1.o", BPF_PROG_TYPE_CGROUP_SYSCTL },
		{ "test_sysctl_loop2.o", BPF_PROG_TYPE_CGROUP_SYSCTL },

		{ "test_xdp_loop.o", BPF_PROG_TYPE_XDP },
		{ "test_seg6_loop.o", BPF_PROG_TYPE_LWT_SEG6LOCAL },
92
	};
93
	libbpf_print_fn_t old_print_fn = NULL;
94
	int err, i;
95

96 97
	if (env.verifier_stats) {
		test__force_log();
98
		old_print_fn = libbpf_set_print(libbpf_debug_print);
99
	}
100

101 102
	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		const struct scale_test_def *test = &tests[i];
103

104 105
		if (!test__start_subtest(test->file))
			continue;
106

107 108 109 110 111 112 113
		err = check_load(test->file, test->attach_type);
		if (test->fails) { /* expected to fail */
			if (err)
				error_cnt--;
			else
				error_cnt++;
		}
114
	}
115

116
	if (env.verifier_stats)
117
		libbpf_set_print(old_print_fn);
118
}