Skip to content

Commit a4b09a9

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Add non-CO-RE variants of BPF_CORE_READ() macro family
BPF_CORE_READ(), in addition to handling CO-RE relocations, also allows much nicer way to read data structures with nested pointers. Instead of writing a sequence of bpf_probe_read() calls to follow links, one can just write BPF_CORE_READ(a, b, c, d) to effectively do a->b->c->d read. This is a welcome ability when porting BCC code, which (in most cases) allows exactly the intuitive a->b->c->d variant. This patch adds non-CO-RE variants of BPF_CORE_READ() family of macros for cases where CO-RE is not supported (e.g., old kernels). In such cases, the property of shortening a sequence of bpf_probe_read()s to a simple BPF_PROBE_READ(a, b, c, d) invocation is still desirable, especially when porting BCC code to libbpf. Yet, no CO-RE relocation is going to be emitted. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20201218235614.2284956-3-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 792001f commit a4b09a9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tools/lib/bpf/bpf_core_read.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,18 @@ enum bpf_enum_value_kind {
308308
dst, (src), a, ##__VA_ARGS__) \
309309
})
310310

311+
/* Non-CO-RE variant of BPF_CORE_READ_INTO() */
312+
#define BPF_PROBE_READ_INTO(dst, src, a, ...) ({ \
313+
___core_read(bpf_probe_read, bpf_probe_read, \
314+
dst, (src), a, ##__VA_ARGS__) \
315+
})
316+
317+
/* Non-CO-RE variant of BPF_CORE_READ_USER_INTO() */
318+
#define BPF_PROBE_READ_USER_INTO(dst, src, a, ...) ({ \
319+
___core_read(bpf_probe_read_user, bpf_probe_read_user, \
320+
dst, (src), a, ##__VA_ARGS__) \
321+
})
322+
311323
/*
312324
* BPF_CORE_READ_STR_INTO() does same "pointer chasing" as
313325
* BPF_CORE_READ() for intermediate pointers, but then executes (and returns
@@ -324,6 +336,18 @@ enum bpf_enum_value_kind {
324336
dst, (src), a, ##__VA_ARGS__) \
325337
})
326338

339+
/* Non-CO-RE variant of BPF_CORE_READ_STR_INTO() */
340+
#define BPF_PROBE_READ_STR_INTO(dst, src, a, ...) ({ \
341+
___core_read(bpf_probe_read_str, bpf_probe_read, \
342+
dst, (src), a, ##__VA_ARGS__) \
343+
})
344+
345+
/* Non-CO-RE variant of BPF_CORE_READ_USER_STR_INTO() */
346+
#define BPF_PROBE_READ_USER_STR_INTO(dst, src, a, ...) ({ \
347+
___core_read(bpf_probe_read_user_str, bpf_probe_read_user, \
348+
dst, (src), a, ##__VA_ARGS__) \
349+
})
350+
327351
/*
328352
* BPF_CORE_READ() is used to simplify BPF CO-RE relocatable read, especially
329353
* when there are few pointer chasing steps.
@@ -361,5 +385,19 @@ enum bpf_enum_value_kind {
361385
__r; \
362386
})
363387

388+
/* Non-CO-RE variant of BPF_CORE_READ() */
389+
#define BPF_PROBE_READ(src, a, ...) ({ \
390+
___type((src), a, ##__VA_ARGS__) __r; \
391+
BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__); \
392+
__r; \
393+
})
394+
395+
/* Non-CO-RE variant of BPF_CORE_READ_USER() */
396+
#define BPF_PROBE_READ_USER(src, a, ...) ({ \
397+
___type((src), a, ##__VA_ARGS__) __r; \
398+
BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \
399+
__r; \
400+
})
401+
364402
#endif
365403

0 commit comments

Comments
 (0)