mirror of
https://github.com/torvalds/linux.git
synced 2024-11-22 20:22:09 +00:00
libbpf: add bpf_core_cast() macro
Add bpf_core_cast() macro that wraps bpf_rdonly_cast() kfunc. It's more ergonomic than kfunc, as it automatically extracts btf_id with bpf_core_type_id_kernel(), and works with type names. It also casts result to (T *) pointer. See the definition of the macro, it's self-explanatory. libbpf declares bpf_rdonly_cast() extern as __weak __ksym and should be safe to not conflict with other possible declarations in user code. But we do have a conflict with current BPF selftests that declare their externs with first argument as `void *obj`, while libbpf opts into more permissive `const void *obj`. This causes conflict, so we fix up BPF selftests uses in the same patch. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240130212023.183765-2-andrii@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
This commit is contained in:
parent
4d8ebe1304
commit
20d59ee551
@ -2,6 +2,8 @@
|
||||
#ifndef __BPF_CORE_READ_H__
|
||||
#define __BPF_CORE_READ_H__
|
||||
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
/*
|
||||
* enum bpf_field_info_kind is passed as a second argument into
|
||||
* __builtin_preserve_field_info() built-in to get a specific aspect of
|
||||
@ -292,6 +294,17 @@ enum bpf_enum_value_kind {
|
||||
#define bpf_core_read_user_str(dst, sz, src) \
|
||||
bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src))
|
||||
|
||||
extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
|
||||
|
||||
/*
|
||||
* Cast provided pointer *ptr* into a pointer to a specified *type* in such
|
||||
* a way that BPF verifier will become aware of associated kernel-side BTF
|
||||
* type. This allows to access members of kernel types directly without the
|
||||
* need to use BPF_CORE_READ() macros.
|
||||
*/
|
||||
#define bpf_core_cast(ptr, type) \
|
||||
((typeof(type) *)bpf_rdonly_cast((ptr), bpf_core_type_id_kernel(type)))
|
||||
|
||||
#define ___concat(a, b) a ## b
|
||||
#define ___apply(fn, n) ___concat(fn, n)
|
||||
#define ___nth(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11, N, ...) N
|
||||
|
@ -63,7 +63,7 @@ extern int bpf_sk_assign_tcp_reqsk(struct __sk_buff *skb, struct sock *sk,
|
||||
|
||||
void *bpf_cast_to_kern_ctx(void *) __ksym;
|
||||
|
||||
void *bpf_rdonly_cast(void *obj, __u32 btf_id) __ksym;
|
||||
extern void *bpf_rdonly_cast(const void *obj, __u32 btf_id) __ksym __weak;
|
||||
|
||||
extern int bpf_get_file_xattr(struct file *file, const char *name,
|
||||
struct bpf_dynptr *value_ptr) __ksym;
|
||||
|
@ -12,8 +12,6 @@ int cookie_found = 0;
|
||||
__u64 cookie = 0;
|
||||
__u32 omem = 0;
|
||||
|
||||
void *bpf_rdonly_cast(void *, __u32) __ksym;
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
|
||||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
#include <bpf/bpf_core_read.h>
|
||||
#include "bpf_kfuncs.h"
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
|
||||
@ -19,9 +20,6 @@ char name[IFNAMSIZ];
|
||||
unsigned int inum;
|
||||
unsigned int meta_len, frag0_len, kskb_len, kskb2_len;
|
||||
|
||||
void *bpf_cast_to_kern_ctx(void *) __ksym;
|
||||
void *bpf_rdonly_cast(void *, __u32) __ksym;
|
||||
|
||||
SEC("?xdp")
|
||||
int md_xdp(struct xdp_md *ctx)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user