summaryrefslogtreecommitdiff
path: root/tests/kernel_version.c
blob: 692f94a1e2a08155eedfa1baf5a5aac8ad698ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Check kernel version decoding.
 *
 * Copyright (c) 2015-2020 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include "scno.h"

#ifdef HAVE_LINUX_BPF_H
# include <linux/bpf.h>
#endif

#include "bpf_attr.h"
#include "print_fields.h"

#include "xlat.h"
#include "xlat/bpf_commands.h"

#define CMD_STR(x) #x
static const char *errstr;

static void
print_bpf_attr(void)
{
#if XLAT_RAW
	printf("{prog_type=0x1f"
#else
	printf("{prog_type=0x1f /* BPF_PROG_TYPE_??? */"
#endif
		", insn_cnt=3134983661"
		", insns=NULL"
		", license=NULL"
		", log_level=24"
		", log_size=3141592653"
		", log_buf=NULL"
#if XLAT_RAW
		", kern_version=0xcafef00d"
#elif XLAT_VERBOSE
		", kern_version=0xcafef00d"
		" /* KERNEL_VERSION(51966, 240, 13) */"
#else
		", kern_version=KERNEL_VERSION(51966, 240, 13)"
#endif
		", prog_flags=0"
		", prog_name=\"\""
		", prog_ifindex=0"
		", expected_attach_type="
#if XLAT_RAW
		"0"
#elif XLAT_VERBOSE
		"0 /* BPF_CGROUP_INET_INGRESS */"
#else /* XLAT_ABBREV */
		"BPF_CGROUP_INET_INGRESS"
#endif
		", prog_btf_fd=0"
		", func_info_rec_size=0"
		", func_info=NULL"
		", func_info_cnt=0"
		", line_info_rec_size=0"
		", line_info=NULL"
		", line_info_cnt=0"
		", attach_btf_id=0"
		", attach_prog_fd=0}");
}

int
main(void)
{
	long ret;
	struct BPF_PROG_LOAD_struct prog = {
		.prog_type = 31,
		.insn_cnt = 0xbadc0ded,
		.insns = 0,
		.license = 0,
		.log_level = 24,
		.log_size = 3141592653U,
		.log_buf = 0,
		.kern_version = 0xcafef00d,
		.prog_flags = 0,
	};
	ret = syscall(__NR_bpf, BPF_PROG_LOAD, &prog, sizeof(prog));
	errstr = sprintrc(ret);
#if XLAT_RAW
	printf("bpf(%#x, ", BPF_PROG_LOAD);
#elif XLAT_VERBOSE
	printf("bpf(%#x /* %s */, ", BPF_PROG_LOAD, CMD_STR(BPF_PROG_LOAD));
#else
	printf("bpf(%s, ", CMD_STR(BPF_PROG_LOAD));
#endif
	print_bpf_attr();
	printf(", %u) = %s\n", (unsigned int) sizeof(prog), errstr);
	puts("+++ exited with 0 +++");
	return 0;
}