forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.bpf.h
45 lines (34 loc) · 1.01 KB
/
compat.bpf.h
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
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/* Copyright (c) 2022 Hengqi Chen */
#ifndef __COMPAT_BPF_H
#define __COMPAT_BPF_H
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#define MAX_EVENT_SIZE 10240
#define RINGBUF_SIZE (1024 * 256)
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__uint(key_size, sizeof(__u32));
__uint(value_size, MAX_EVENT_SIZE);
} heap SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, RINGBUF_SIZE);
} events SEC(".maps");
static __always_inline void *reserve_buf(__u64 size)
{
static const int zero = 0;
if (bpf_core_type_exists(struct bpf_ringbuf))
return bpf_ringbuf_reserve(&events, size, 0);
return bpf_map_lookup_elem(&heap, &zero);
}
static __always_inline long submit_buf(void *ctx, void *buf, __u64 size)
{
if (bpf_core_type_exists(struct bpf_ringbuf)) {
bpf_ringbuf_submit(buf, 0);
return 0;
}
return bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, buf, size);
}
#endif /* __COMPAT_BPF_H */