summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/bpftool_ringbuf.c
blob: bbbc93c30a833b354b496d88bb25b8cb9b55f12b (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
// SPDX-License-Identifier: GPL-2.0
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

struct {
	__uint(type, BPF_MAP_TYPE_RINGBUF);
} ringbuf SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
	__uint(key_size, sizeof(__u32));
	__uint(value_size, sizeof(__u32));
	__uint(max_entries, 1);
} perfbuf SEC(".maps");

/*
 * Include the u32 raw size in perf's 8-byte alignment to avoid padding.
 * Keep plain output large enough to flush stdio before the consumer exits.
 */
const unsigned char perf_payload[4092] = { 0x00, 0xff };

int record;
int output_err;

SEC("socket")
int produce(struct __sk_buff *skb)
{
	unsigned char first[] = { 0x00, 0xff };
	unsigned char second[] = { 1, 2, 3, 4, 5 };
	unsigned char third[] = {
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
	};

	if (record == 0)
		output_err = bpf_ringbuf_output(&ringbuf, first, sizeof(first), 0);
	else if (record == 1)
		output_err = bpf_ringbuf_output(&ringbuf, second, sizeof(second), 0);
	else if (record == 2)
		output_err = bpf_ringbuf_output(&ringbuf, third, sizeof(third), 0);
	else
		output_err = bpf_perf_event_output(skb, &perfbuf, BPF_F_CURRENT_CPU,
						   (void *)perf_payload, sizeof(perf_payload));
	return 0;
}

char LICENSE[] SEC("license") = "GPL";