summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/skeleton.c
blob: fa153cf67b1bb984bcd29f08ebe40f65cc351e9f (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Facebook */

#include <test_progs.h>

struct s {
	int a;
	long long b;
} __attribute__((packed));

#include "test_skeleton.skel.h"

void test_skeleton(void)
{
	int duration = 0, err;
	struct test_skeleton* skel;
	struct test_skeleton__bss *bss;
	struct test_skeleton__data *data;
	struct test_skeleton__rodata *rodata;
	struct test_skeleton__kconfig *kcfg;

	skel = test_skeleton__open();
	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
		return;

	if (CHECK(skel->kconfig, "skel_kconfig", "kconfig is mmaped()!\n"))
		goto cleanup;

	bss = skel->bss;
	data = skel->data;
	rodata = skel->rodata;

	/* validate values are pre-initialized correctly */
	CHECK(data->in1 != -1, "in1", "got %d != exp %d\n", data->in1, -1);
	CHECK(data->out1 != -1, "out1", "got %d != exp %d\n", data->out1, -1);
	CHECK(data->in2 != -1, "in2", "got %lld != exp %lld\n", data->in2, -1LL);
	CHECK(data->out2 != -1, "out2", "got %lld != exp %lld\n", data->out2, -1LL);

	CHECK(bss->in3 != 0, "in3", "got %d != exp %d\n", bss->in3, 0);
	CHECK(bss->out3 != 0, "out3", "got %d != exp %d\n", bss->out3, 0);
	CHECK(bss->in4 != 0, "in4", "got %lld != exp %lld\n", bss->in4, 0LL);
	CHECK(bss->out4 != 0, "out4", "got %lld != exp %lld\n", bss->out4, 0LL);

	CHECK(rodata->in6 != 0, "in6", "got %d != exp %d\n", rodata->in6, 0);
	CHECK(bss->out6 != 0, "out6", "got %d != exp %d\n", bss->out6, 0);

	/* validate we can pre-setup global variables, even in .bss */
	data->in1 = 10;
	data->in2 = 11;
	bss->in3 = 12;
	bss->in4 = 13;
	rodata->in6 = 14;

	err = test_skeleton__load(skel);
	if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
		goto cleanup;

	/* validate pre-setup values are still there */
	CHECK(data->in1 != 10, "in1", "got %d != exp %d\n", data->in1, 10);
	CHECK(data->in2 != 11, "in2", "got %lld != exp %lld\n", data->in2, 11LL);
	CHECK(bss->in3 != 12, "in3", "got %d != exp %d\n", bss->in3, 12);
	CHECK(bss->in4 != 13, "in4", "got %lld != exp %lld\n", bss->in4, 13LL);
	CHECK(rodata->in6 != 14, "in6", "got %d != exp %d\n", rodata->in6, 14);

	/* now set new values and attach to get them into outX variables */
	data->in1 = 1;
	data->in2 = 2;
	bss->in3 = 3;
	bss->in4 = 4;
	bss->in5.a = 5;
	bss->in5.b = 6;
	kcfg = skel->kconfig;

	err = test_skeleton__attach(skel);
	if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
		goto cleanup;

	/* trigger tracepoint */
	usleep(1);

	CHECK(data->out1 != 1, "res1", "got %d != exp %d\n", data->out1, 1);
	CHECK(data->out2 != 2, "res2", "got %lld != exp %d\n", data->out2, 2);
	CHECK(bss->out3 != 3, "res3", "got %d != exp %d\n", (int)bss->out3, 3);
	CHECK(bss->out4 != 4, "res4", "got %lld != exp %d\n", bss->out4, 4);
	CHECK(bss->handler_out5.a != 5, "res5", "got %d != exp %d\n",
	      bss->handler_out5.a, 5);
	CHECK(bss->handler_out5.b != 6, "res6", "got %lld != exp %d\n",
	      bss->handler_out5.b, 6);
	CHECK(bss->out6 != 14, "res7", "got %d != exp %d\n", bss->out6, 14);

	CHECK(bss->bpf_syscall != kcfg->CONFIG_BPF_SYSCALL, "ext1",
	      "got %d != exp %d\n", bss->bpf_syscall, kcfg->CONFIG_BPF_SYSCALL);
	CHECK(bss->kern_ver != kcfg->LINUX_KERNEL_VERSION, "ext2",
	      "got %d != exp %d\n", bss->kern_ver, kcfg->LINUX_KERNEL_VERSION);

cleanup:
	test_skeleton__destroy(skel);
}