Skip to content

Commit f3c6cd6

Browse files
committed
check the supporting of bpf helper by libbpf_probe
Signed-off-by: Menglong Dong <imagedong@tencent.com>
1 parent e42e759 commit f3c6cd6

7 files changed

Lines changed: 35 additions & 8 deletions

File tree

common.mk

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ ifeq ($(wildcard $(HEADERS)),)
5454
$(error kernel headers not exist in COMPAT mdoe, please install it)
5555
endif
5656
kheaders_cmd := ln -s vmlinux_header.h kheaders.h
57-
CFLAGS += -DCOMPAT_MODE
58-
BPF_CFLAGS += $(KERNEL_CFLAGS) -DBPF_NO_GLOBAL_DATA \
59-
-g
57+
CFLAGS += -DCOMPAT_MODE -DBPF_NO_GLOBAL_DATA
58+
BPF_CFLAGS += $(KERNEL_CFLAGS) -g
6059
else
6160
kheaders_cmd := ln -s ../shared/bpf/vmlinux.h kheaders.h
6261
BPF_CFLAGS += -target bpf -g

shared/bpf/skb_macro.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
4242
#endif
4343

44+
/* redefine all the CO-RE usage in COMPAT mode */
4445
#ifdef COMPAT_MODE
45-
#define bpf_core_helper_exist(name) false
46-
4746
#undef bpf_core_type_exists
4847
#define bpf_core_type_exists(type) false
4948

@@ -55,9 +54,6 @@
5554

5655
#undef bpf_core_field_offset
5756
#define bpf_core_field_offset(type, field) offsetof(type, field)
58-
#else
59-
#define bpf_core_helper_exist(name) \
60-
bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_##name)
6157
#endif
6258

6359
#endif

shared/bpf/skb_parse.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ struct {
4141
__uint(max_entries, 1);
4242
} m_config SEC(".maps");
4343

44+
#ifndef BPF_NO_GLOBAL_DATA
45+
static const volatile bool bpf_func_exist[BPF_LOCAL_FUNC_MAX] = {0};
46+
#define bpf_core_helper_exist(name) bpf_func_exist[BPF_LOCAL_FUNC_##name]
47+
#else
48+
#define bpf_core_helper_exist(name) false
49+
#endif
50+
4451
#define CONFIG() ({ \
4552
int _key = 0; \
4653
void * _v = bpf_map_lookup_elem(&m_config, &_key); \

shared/bpf/skb_shared.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,15 @@ typedef struct {
176176
#endif
177177
typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH];
178178

179+
#define BPF_LOCAL_FUNC_MAPPER(FN, args...) \
180+
FN(jiffies64, ##args) \
181+
FN(get_func_ret, ##args)
182+
183+
#define FN(name) BPF_LOCAL_FUNC_##name,
184+
enum {
185+
BPF_LOCAL_FUNC_MAPPER(FN)
186+
BPF_LOCAL_FUNC_MAX,
187+
};
188+
#undef FN
189+
179190
#endif

shared/bpf_utils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,16 @@ int compat_bpf_attach_kprobe(int fd, char *name, bool ret);
6666
const struct btf_type *btf_get_type(char *name);
6767
int btf_get_arg_count(char *name);
6868

69+
#ifndef BPF_NO_GLOBAL_DATA
70+
#undef BPF_FUNC_CHECK
71+
#define BPF_FUNC_CHECK(name, data, type) \
72+
data[BPF_LOCAL_FUNC_##name] = libbpf_probe_bpf_helper(type, \
73+
BPF_FUNC_##name, NULL);
74+
75+
#define bpf_func_init(skel, type) \
76+
BPF_LOCAL_FUNC_MAPPER(BPF_FUNC_CHECK, skel->rodata->bpf_func_exist, type)
77+
#else
78+
#define bpf_func_init(data, type)
79+
#endif
80+
6981
#endif

src/trace_probe.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static int probe_trace_load()
8888

8989
/* set the max entries of perf event map to current cpu count */
9090
bpf_map__set_max_entries(skel->maps.m_event, get_nprocs_conf());
91+
bpf_func_init(skel, BPF_PROG_TYPE_KPROBE);
9192

9293
trace_ctx.obj = skel->obj;
9394
if (trace_pre_load() || kprobe__load(skel)) {

src/trace_tracing.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ static int tracing_trace_load()
152152

153153
/* set the max entries of perf event map to current cpu count */
154154
bpf_map__set_max_entries(skel->maps.m_event, get_nprocs_conf());
155+
bpf_func_init(skel, BPF_PROG_TYPE_TRACING);
155156

156157
trace_ctx.obj = skel->obj;
157158
tracing_load_rules();

0 commit comments

Comments
 (0)