bpf: Work around libbpf deprecations
Subsystems (3)
cover bpf: Work around libbpf deprecations
libbpf is deprecating APIs, so iproute2 needs to adapt. This will be an on-going effort, especially to handle legacy maps. This is a starter set for the low handing fruit.
David Ahern (3):
bpf_glue: Remove use of bpf_load_program from libbpf
bpf: Export bpf syscall wrapper
bpf: Remove use of bpf_create_map_xattr
include/bpf_util.h | 2 ++ lib/bpf_glue.c | 15 ++++++++------- lib/bpf_legacy.c | 19 +++++++------------ lib/bpf_libbpf.c | 24 ++++++++++++------------ 4 files changed, 29 insertions(+), 31 deletions(-)
Signature · 1 line
2.24.3 (Apple Git-128)
PATCH 1/3 bpf_glue: Remove use of bpf_load_program from libbpf
bpf_load_program is deprecated starting in v0.7. The preferred bpf_prog_load requires bpf_prog_load_opts from v0.6. This creates an ugly scenario for iproute2 to work across libbpf versions from v0.1 and up.
Since bpf_program_load is only used to load the builtin vrf program, just remove the libbpf call and use the legacy code.
--- lib/bpf_glue.c | 12 ------------ lib/bpf_legacy.c | 7 +++++++ 2 files changed, 7 insertions(+), 12 deletions(-)
lib/bpf_glue.c +0 -12
@@ -11,18 +11,6 @@#include <bpf/bpf.h> #endif -int bpf_program_load (enum bpf_prog_type type,const struct bpf_insn *insns, -size_t size_insns,const char *license,char *log, -size_t size_log) -{ -#ifdef HAVE_LIBBPF -return bpf_load_program (type, insns, size_insns /sizeof (struct bpf_insn ), - license,0 , log, size_log); -#else -return bpf_prog_load_dev (type, insns, size_insns, license,0 , log, size_log); -#endif -} -int bpf_program_attach (int prog_fd,int target_fd,enum bpf_attach_type type) {#ifdef HAVE_LIBBPF
lib/bpf_legacy.c +7 -0
@@ -1126,6 +1126,13 @@ int bpf_prog_load_dev(enum bpf_prog_type type, const struct bpf_insn *insns,return bpf (BPF_PROG_LOAD , &attr,sizeof (attr)); } +int bpf_program_load (enum bpf_prog_type type,const struct bpf_insn *insns, +size_t size_insns,const char *license,char *log, +size_t size_log) +{ +return bpf_prog_load_dev (type, insns, size_insns, license,0 , log, size_log); +} + #ifdef HAVE_ELF struct bpf_elf_prog { enum bpf_prog_type type;
Signature · 1 line
2.24.3 (Apple Git-128)
PATCH 2/3 bpf: Export bpf syscall wrapper
In reply to David AhernMove bpf syscall wrapper to bpf_glue to make it available to libbpf based functions.
--- include/bpf_util.h | 2 ++ lib/bpf_glue.c | 13 +++++++++++++ lib/bpf_legacy.c | 12 ------------ 3 files changed, 15 insertions(+), 12 deletions(-)
include/bpf_util.h +2 -0
@@ -287,6 +287,8 @@ int bpf_program_attach(int prog_fd, int target_fd, enum bpf_attach_type type);int bpf_dump_prog_info (FILE *f,uint32_t id); +int bpf (int cmd,union bpf_attr *attr,unsigned int size); +#ifdef HAVE_ELF int bpf_send_map_fds (const char *path,const char *obj); int bpf_recv_map_fds(const char *path, int *fds, struct bpf_map_aux *aux,
lib/bpf_glue.c +13 -0
@@ -4,13 +4,26 @@* Authors: Hangbin Liu <[email protected]> * */ +#include <sys/syscall.h> #include <limits.h> +#include <unistd.h> #include "bpf_util.h" #ifdef HAVE_LIBBPF #include <bpf/bpf.h> #endif +int bpf (int cmd,union bpf_attr *attr,unsigned int size) +{ +#ifdef __NR_bpf +return syscall (__NR_bpf, cmd, attr, size); +#else +fprintf (stderr,"No bpf syscall, kernel headers too old?\n" ); + errno =ENOSYS ; +return -1 ; +#endif +} +int bpf_program_attach (int prog_fd,int target_fd,enum bpf_attach_type type) {#ifdef HAVE_LIBBPF
lib/bpf_legacy.c +0 -12
@@ -33,7 +33,6 @@#include <sys/un.h> #include <sys/vfs.h> #include <sys/mount.h> -#include <sys/syscall.h> #include <sys/sendfile.h> #include <sys/resource.h>
@@ -134,17 +133,6 @@ static inline __u64 bpf_ptr_to_u64(const void *ptr)return (__u64 )(unsigned long )ptr; } -static int bpf (int cmd,union bpf_attr *attr,unsigned int size) -{ -#ifdef __NR_bpf -return syscall (__NR_bpf, cmd, attr, size); -#else -fprintf (stderr,"No bpf syscall, kernel headers too old?\n" ); - errno =ENOSYS ; -return -1 ; -#endif -} -static int bpf_map_update (int fd,const void *key,const void *value,uint64_t flags) {
Signature · 1 line
2.24.3 (Apple Git-128)
PATCH 3/3 bpf: Remove use of bpf_create_map_xattr
In reply to David Ahernbpf_create_map_xattr is deprecated in v0.7 in favor of bpf_map_create. bpf_map_create and its bpf_map_create_opts are not available across the range of v0.1 and up versions of libbpf, so change create_map to use the bpf syscall directly.
--- lib/bpf_libbpf.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
lib/bpf_libbpf.c +12 -12
@@ -54,18 +54,18 @@ static const char *get_bpf_program__section_name(const struct bpf_program *prog)static int create_map (const char *name,struct bpf_elf_map *map,__u32 ifindex,int inner_fd) { -struct bpf_create_map_attr map_attr = {}; - - map_attr.name = name; - map_attr.map_type = map->type; - map_attr.map_flags = map->flags; - map_attr.key_size = map->size_key; - map_attr.value_size = map->size_value; - map_attr.max_entries = map->max_elem; - map_attr.map_ifindex = ifindex; - map_attr.inner_map_fd = inner_fd; - -return bpf_create_map_xattr (&map_attr); +union bpf_attr attr = {}; + + attr.map_type = map->type; +strlcpy (attr.map_name, name,sizeof (attr.map_name)); + attr.map_flags = map->flags; + attr.key_size = map->size_key; + attr.value_size = map->size_value; + attr.max_entries = map->max_elem; + attr.map_ifindex = ifindex; + attr.inner_map_fd = inner_fd; + +return bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); } static int create_map_in_map(struct bpf_object *obj, struct bpf_map *map,
Signature · 1 line
2.24.3 (Apple Git-128)
This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:
Hello:
This series was applied to iproute2/iproute2-next.git (main) by David Ahern <[email protected]>:
Quoted 10 lines · On Sun, 27 Feb 2022 18:58:37 -0700 you wrote:
libbpf is deprecating APIs, so iproute2 needs to adapt. This will be an on-going effort, especially to handle legacy maps. This is a starter set for the low handing fruit.
David Ahern (3):
bpf_glue: Remove use ofbpf_load_programfrom libbpf
bpf: Export bpf syscall wrapper
bpf: Remove use ofbpf_create_map_xattr[...]
Here is the summary with links:
- [iproute2-next,1/3] bpf_glue: Remove use of bpf_load_program from libbpf
https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=873bb9751f84 - [iproute2-next,2/3] bpf: Export bpf syscall wrapper https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=ac4e0913beb1 - [iproute2-next,3/3] bpf: Remove use of bpf_create_map_xattr https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=d9977eafa719
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html