libbpf: making bpf_prog_load() ignore name if kernel doesn't support
Subsystems (4)
PATCH 1/1 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 bpf.h file.
--- tools/lib/bpf/bpf.c | 6 ++---- tools/lib/bpf/bpf.h | 3 +++ tools/lib/bpf/libbpf.c | 11 +++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-)
tools/lib/bpf/bpf.c +2 -4
@@ -84,9 +84,7 @@ static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,return ensure_good_fd (fd); } -#define PROG_LOAD_ATTEMPTS 5 - -static inline int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts) +int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts) {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.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)); attr.license =ptr_to_u64 (license);
tools/lib/bpf/bpf.h +3 -0
@@ -35,6 +35,9 @@ extern "C" { #endif +#define PROG_LOAD_ATTEMPTS 5 +int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts); + int libbpf_set_memlock_rlim(size_t memlock_bytes); struct bpf_map_create_opts {
tools/lib/bpf/libbpf.c +9 -2
@@ -4419,10 +4419,17 @@ static int probe_kern_prog_name(void)BPF_MOV64_IMM (BPF_REG_0 ,0 ), BPF_EXIT_INSN(), }; -int ret, insn_cnt =ARRAY_SIZE (insns); +union bpf_attr attr = { + .prog_type =BPF_PROG_TYPE_SOCKET_FILTER , + .prog_name ="test" , + .license =ptr_to_u64 ("GPL" ), + .insns =ptr_to_u64 (insns), + .insn_cnt = (__u32 )ARRAY_SIZE (insns), + }; +int ret;/* 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,sizeof (attr),PROG_LOAD_ATTEMPTS );return probe_fd (ret); }
Signature · 1 line
2.31.1
Quoted 44 lines · Hangbin Liu
Similar with commit
10b62d6a38f7("libbpf: Add names for auxiliary maps"), let's makebpf_prog_load()also ignore name if kernel doesn't support program name.To achieve this, we need to call
sys_bpf_prog_load()directly inprobe_kern_prog_name()to avoid circular dependency.sys_bpf_prog_load()also need to be exported in thebpf.hfile.Signed-off-by Hangbin Liu <[email protected]>--- tools/lib/bpf/bpf.c | 6 ++---- tools/lib/bpf/bpf.h | 3 +++ tools/lib/bpf/libbpf.c | 11 +++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-)tools/lib/bpf/bpf.c +2
-4@@ -84,9 +84,7 @@ static inline int sys_bpf_fd(enum bpf_cmd cmd, union bpf_attr *attr,return ensure_good_fd (fd); } -#define PROG_LOAD_ATTEMPTS 5 - -static inline int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts) +int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts) {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.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)); attr.license =ptr_to_u64 (license);tools/lib/bpf/bpf.h +0
-0
tools/lib/bpf/bpf.h line 40
Quoted 6 lines
tools/lib/bpf/bpf.h +3
-0@@ -35,6 +35,9 @@extern "C" {#endif +#define PROG_LOAD_ATTEMPTS 5 +int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts); +
bpf.h is the user-facing header, should these go into libbpf_internal.h instead?
By the way, I observe that libbpf_set_memlock_rlim() in bpf.h (below) is not prefixed with LIBBPF_API, although it is exposed in the libbpf.map, Andrii is this expected?
Quoted 9 lines
tools/lib/bpf/bpf.h +0
-0int libbpf_set_memlock_rlim (size_t memlock_bytes);struct bpf_map_create_opts {tools/lib/bpf/libbpf.c +0
-0@@ -4419,10 +4419,17 @@ static int probe_kern_prog_name(void) BPF_MOV64_IMM(BPF_REG_0, 0),
tools/lib/bpf/libbpf.c static int probe_kern_prog_name(void) · line 4428
Quoted 10 lines
tools/lib/bpf/libbpf.c +7
-1BPF_EXIT_INSN(), }; -int ret, insn_cnt =ARRAY_SIZE (insns); +union bpf_attr attr = { + .prog_type =BPF_PROG_TYPE_SOCKET_FILTER , + .prog_name ="test" , + .license =ptr_to_u64 ("GPL" ), + .insns =ptr_to_u64 (insns), + .insn_cnt = (__u32 )ARRAY_SIZE (insns), + };
I think you cannot initialise "attr" directly, you need a "memset(&attr, 0, sizeof(attr));" first, in case the struct contains padding between the fields.
Quoted 8 lines
tools/lib/bpf/libbpf.c +2
-1+int ret;/* 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,sizeof (attr),PROG_LOAD_ATTEMPTS );return probe_fd (ret); }
Quoted 8 lines · Quentin Monnet
Quoted 8 lines
tools/lib/bpf/bpf.h +0
-0@@ -35,6 +35,9 @@extern "C" {#endif
Quoted 6 lines
tools/lib/bpf/bpf.h line 40
Quoted 3 lines
tools/lib/bpf/bpf.h +3
-0+#define PROG_LOAD_ATTEMPTS 5 +int sys_bpf_prog_load (union bpf_attr *attr,unsigned int size,int attempts); +
bpf.his the user-facing header, should these go intolibbpf_internal.hinstead?
libbpf_internal.h makes more sense, I wil move it.
Quoted 11 lines
tools/lib/bpf/bpf.h
Quoted 7 lines
tools/lib/bpf/bpf.h +7
-0+union bpf_attr attr = { + .prog_type =BPF_PROG_TYPE_SOCKET_FILTER , + .prog_name ="test" , + .license =ptr_to_u64 ("GPL" ), + .insns =ptr_to_u64 (insns), + .insn_cnt = (__u32 )ARRAY_SIZE (insns), + };I think you cannot initialise "attr" directly, you need a "
memset(&attr, 0, sizeof(attr));" first, in case the struct contains padding between the fields.
Thanks for the reminder. I forgot the padding issue.
I will post version 2.
Thanks
Hangbin