summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/kfree_skb.c
blob: ce10d2fc3a6c8d0baa541de9c6d0c22931500205 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include <network_helpers.h>
#include "kfree_skb.skel.h"

struct meta {
	int ifindex;
	__u32 cb32_0;
	__u8 cb8_0;
};

static union {
	__u32 cb32[5];
	__u8 cb8[20];
} cb = {
	.cb32[0] = 0x81828384,
};

static void on_sample(void *ctx, int cpu, void *data, __u32 size)
{
	struct meta *meta = (struct meta *)data;
	struct ipv6_packet *pkt_v6 = data + sizeof(*meta);
	int duration = 0;

	if (CHECK(size != 72 + sizeof(*meta), "check_size", "size %u != %zu\n",
		  size, 72 + sizeof(*meta)))
		return;
	if (CHECK(meta->ifindex != 1, "check_meta_ifindex",
		  "meta->ifindex = %d\n", meta->ifindex))
		/* spurious kfree_skb not on loopback device */
		return;
	if (CHECK(meta->cb8_0 != cb.cb8[0], "check_cb8_0", "cb8_0 %x != %x\n",
		  meta->cb8_0, cb.cb8[0]))
		return;
	if (CHECK(meta->cb32_0 != cb.cb32[0], "check_cb32_0",
		  "cb32_0 %x != %x\n",
		  meta->cb32_0, cb.cb32[0]))
		return;
	if (CHECK(pkt_v6->eth.h_proto != 0xdd86, "check_eth",
		  "h_proto %x\n", pkt_v6->eth.h_proto))
		return;
	if (CHECK(pkt_v6->iph.nexthdr != 6, "check_ip",
		  "iph.nexthdr %x\n", pkt_v6->iph.nexthdr))
		return;
	if (CHECK(pkt_v6->tcp.doff != 5, "check_tcp",
		  "tcp.doff %x\n", pkt_v6->tcp.doff))
		return;

	*(bool *)ctx = true;
}

/* TODO: fix kernel panic caused by this test in parallel mode */
void serial_test_kfree_skb(void)
{
	struct __sk_buff skb = {};
	struct bpf_prog_test_run_attr tattr = {
		.data_in = &pkt_v6,
		.data_size_in = sizeof(pkt_v6),
		.ctx_in = &skb,
		.ctx_size_in = sizeof(skb),
	};
	struct kfree_skb *skel = NULL;
	struct bpf_link *link;
	struct bpf_object *obj;
	struct perf_buffer *pb = NULL;
	int err;
	bool passed = false;
	__u32 duration = 0;
	const int zero = 0;
	bool test_ok[2];

	err = bpf_prog_test_load("./test_pkt_access.o", BPF_PROG_TYPE_SCHED_CLS,
			    &obj, &tattr.prog_fd);
	if (CHECK(err, "prog_load sched cls", "err %d errno %d\n", err, errno))
		return;

	skel = kfree_skb__open_and_load();
	if (!ASSERT_OK_PTR(skel, "kfree_skb_skel"))
		goto close_prog;

	link = bpf_program__attach_raw_tracepoint(skel->progs.trace_kfree_skb, NULL);
	if (!ASSERT_OK_PTR(link, "attach_raw_tp"))
		goto close_prog;
	skel->links.trace_kfree_skb = link;

	link = bpf_program__attach_trace(skel->progs.fentry_eth_type_trans);
	if (!ASSERT_OK_PTR(link, "attach fentry"))
		goto close_prog;
	skel->links.fentry_eth_type_trans = link;

	link = bpf_program__attach_trace(skel->progs.fexit_eth_type_trans);
	if (!ASSERT_OK_PTR(link, "attach fexit"))
		goto close_prog;
	skel->links.fexit_eth_type_trans = link;

	/* set up perf buffer */
	pb = perf_buffer__new(bpf_map__fd(skel->maps.perf_buf_map), 1,
			      on_sample, NULL, &passed, NULL);
	if (!ASSERT_OK_PTR(pb, "perf_buf__new"))
		goto close_prog;

	memcpy(skb.cb, &cb, sizeof(cb));
	err = bpf_prog_test_run_xattr(&tattr);
	duration = tattr.duration;
	CHECK(err || tattr.retval, "ipv6",
	      "err %d errno %d retval %d duration %d\n",
	      err, errno, tattr.retval, duration);

	/* read perf buffer */
	err = perf_buffer__poll(pb, 100);
	if (CHECK(err < 0, "perf_buffer__poll", "err %d\n", err))
		goto close_prog;

	/* make sure kfree_skb program was triggered
	 * and it sent expected skb into ring buffer
	 */
	ASSERT_TRUE(passed, "passed");

	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.bss), &zero, test_ok);
	if (CHECK(err, "get_result",
		  "failed to get output data: %d\n", err))
		goto close_prog;

	CHECK_FAIL(!test_ok[0] || !test_ok[1]);
close_prog:
	perf_buffer__free(pb);
	bpf_object__close(obj);
	kfree_skb__destroy(skel);
}