mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-04 07:39:07 +08:00
libbpf: Making bpf_prog_load() ignore name if kernel doesn't support
Similar with commit 10b62d6a38f7 ("libbpf: Add names for auxiliary maps"),
let's make bpf_prog_load() also ignore name if kernel doesn't support
program name.
To achieve this, we need to call sys_bpf_prog_load() directly in
probe_kern_prog_name() to avoid circular dependency. sys_bpf_prog_load()
also need to be exported in the libbpf_internal.h file.
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20220813000936.6464-1-liuhangbin@gmail.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
8be13ee80b
commit
079bc8536d
@@ -84,9 +84,7 @@ static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,
|
|||||||
return ensure_good_fd(fd);
|
return ensure_good_fd(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define PROG_LOAD_ATTEMPTS 5
|
int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
|
||||||
|
|
||||||
static inline int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts)
|
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
@@ -263,7 +261,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type,
|
|||||||
attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
|
attr.prog_ifindex = OPTS_GET(opts, prog_ifindex, 0);
|
||||||
attr.kern_version = OPTS_GET(opts, kern_version, 0);
|
attr.kern_version = OPTS_GET(opts, kern_version, 0);
|
||||||
|
|
||||||
if (prog_name)
|
if (prog_name && kernel_supports(NULL, FEAT_PROG_NAME))
|
||||||
libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
|
libbpf_strlcpy(attr.prog_name, prog_name, sizeof(attr.prog_name));
|
||||||
attr.license = ptr_to_u64(license);
|
attr.license = ptr_to_u64(license);
|
||||||
|
|
||||||
|
|||||||
13
src/libbpf.c
13
src/libbpf.c
@@ -4415,14 +4415,23 @@ static int probe_fd(int fd)
|
|||||||
|
|
||||||
static int probe_kern_prog_name(void)
|
static int probe_kern_prog_name(void)
|
||||||
{
|
{
|
||||||
|
const size_t attr_sz = offsetofend(union bpf_attr, prog_name);
|
||||||
struct bpf_insn insns[] = {
|
struct bpf_insn insns[] = {
|
||||||
BPF_MOV64_IMM(BPF_REG_0, 0),
|
BPF_MOV64_IMM(BPF_REG_0, 0),
|
||||||
BPF_EXIT_INSN(),
|
BPF_EXIT_INSN(),
|
||||||
};
|
};
|
||||||
int ret, insn_cnt = ARRAY_SIZE(insns);
|
union bpf_attr attr;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
memset(&attr, 0, attr_sz);
|
||||||
|
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
|
||||||
|
attr.license = ptr_to_u64("GPL");
|
||||||
|
attr.insns = ptr_to_u64(insns);
|
||||||
|
attr.insn_cnt = (__u32)ARRAY_SIZE(insns);
|
||||||
|
libbpf_strlcpy(attr.prog_name, "libbpf_nametest", sizeof(attr.prog_name));
|
||||||
|
|
||||||
/* make sure loading with name works */
|
/* make sure loading with name works */
|
||||||
ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "test", "GPL", insns, insn_cnt, NULL);
|
ret = sys_bpf_prog_load(&attr, attr_sz, PROG_LOAD_ATTEMPTS);
|
||||||
return probe_fd(ret);
|
return probe_fd(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -573,4 +573,7 @@ static inline bool is_pow_of_2(size_t x)
|
|||||||
return x && (x & (x - 1)) == 0;
|
return x && (x & (x - 1)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define PROG_LOAD_ATTEMPTS 5
|
||||||
|
int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
|
||||||
|
|
||||||
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
|
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user