]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: make ipvrf able to use libbpf and fix function name conflicts
authorHangbin Liu <haliu@redhat.com>
Mon, 23 Nov 2020 13:11:58 +0000 (21:11 +0800)
committerDavid Ahern <dsahern@gmail.com>
Wed, 25 Nov 2020 05:14:04 +0000 (22:14 -0700)
There are directly calls in libbpf for bpf program load/attach.
So we could just use two wrapper functions for ipvrf and convert
them with libbpf support.

Function bpf_prog_load() is removed as it's conflict with libbpf
function name.

bpf.c is moved to bpf_legacy.c for later main libbpf support in
iproute2.

Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Hangbin Liu <haliu@redhat.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
include/bpf_util.h
ip/ipvrf.c
lib/Makefile
lib/bpf_glue.c
lib/bpf_legacy.c [moved from lib/bpf.c with 99% similarity]

index dee5bb02019d594c708d2bb5981e4754dcbfdbd2..3235c34e7bbe030c78ff7620d37c513cfb959fd2 100644 (file)
@@ -274,12 +274,16 @@ int bpf_trace_pipe(void);
 
 void bpf_print_ops(struct rtattr *bpf_ops, __u16 len);
 
-int bpf_prog_load(enum bpf_prog_type type, const struct bpf_insn *insns,
-                 size_t size_insns, const char *license, char *log,
-                 size_t size_log);
+int bpf_prog_load_dev(enum bpf_prog_type type, const struct bpf_insn *insns,
+                     size_t size_insns, const char *license, __u32 ifindex,
+                     char *log, size_t size_log);
+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);
 
 int bpf_prog_attach_fd(int prog_fd, int target_fd, enum bpf_attach_type type);
 int bpf_prog_detach_fd(int target_fd, enum bpf_attach_type type);
+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);
 
index 28dd8e255fce7f4fdae8554ec60b39081aac5124..42779e5ca6714e2b804969aaa410e401f2de37fc 100644 (file)
@@ -256,8 +256,8 @@ static int prog_load(int idx)
                BPF_EXIT_INSN(),
        };
 
-       return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog),
-                            "GPL", bpf_log_buf, sizeof(bpf_log_buf));
+       return bpf_program_load(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog),
+                               "GPL", bpf_log_buf, sizeof(bpf_log_buf));
 }
 
 static int vrf_configure_cgroup(const char *path, int ifindex)
@@ -288,7 +288,7 @@ static int vrf_configure_cgroup(const char *path, int ifindex)
                goto out;
        }
 
-       if (bpf_prog_attach_fd(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)) {
+       if (bpf_program_attach(prog_fd, cg_fd, BPF_CGROUP_INET_SOCK_CREATE)) {
                fprintf(stderr, "Failed to attach prog to cgroup: '%s'\n",
                        strerror(errno));
                goto out;
index a02775a581e109dddbf90e1e735f8cd6b817729c..7c8a197cf8aaacda785d7879efd0af49646d6b76 100644 (file)
@@ -5,7 +5,7 @@ CFLAGS += -fPIC
 
 UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \
        inet_proto.o namespace.o json_writer.o json_print.o \
-       names.o color.o bpf.o bpf_glue.o exec.o fs.o cg_map.o
+       names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o
 
 NLOBJ=libgenl.o libnetlink.o mnl_utils.o
 
index 67c41c22b9e92ee81a54c43b375680f01e1e3d29..fa609bfee4d67d19daeb6df78bc511a042d41c63 100644 (file)
@@ -5,6 +5,29 @@
  *
  */
 #include "bpf_util.h"
+#ifdef HAVE_LIBBPF
+#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, 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
+       return bpf_prog_attach(prog_fd, target_fd, type, 0);
+#else
+       return bpf_prog_attach_fd(prog_fd, target_fd, type);
+#endif
+}
 
 #ifdef HAVE_LIBBPF
 static const char *_libbpf_compile_version = LIBBPF_VERSION;
similarity index 99%
rename from lib/bpf.c
rename to lib/bpf_legacy.c
index c7d45077c14e58f55a72237a9a55d7667c417c30..4246fb7695f9409ad0e6b57d6d0cf920dbbf4770 100644 (file)
--- a/lib/bpf.c
@@ -1087,10 +1087,9 @@ int bpf_prog_detach_fd(int target_fd, enum bpf_attach_type type)
        return bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
 }
 
-static int bpf_prog_load_dev(enum bpf_prog_type type,
-                            const struct bpf_insn *insns, size_t size_insns,
-                            const char *license, __u32 ifindex,
-                            char *log, size_t size_log)
+int bpf_prog_load_dev(enum bpf_prog_type type, const struct bpf_insn *insns,
+                     size_t size_insns, const char *license, __u32 ifindex,
+                     char *log, size_t size_log)
 {
        union bpf_attr attr = {};
 
@@ -1109,14 +1108,6 @@ static int bpf_prog_load_dev(enum bpf_prog_type type,
        return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
 }
 
-int bpf_prog_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;