diff mbox series

[PATCHv4,iproute2-next,2/5] lib: rename bpf.c to bpf_legacy.c

Message ID 20201109070802.3638167-3-haliu@redhat.com (mailing list archive)
State Not Applicable
Delegated to: Stephen Hemminger
Headers show
Series iproute2: add libbpf support | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Hangbin Liu Nov. 9, 2020, 7:07 a.m. UTC
This is a preparation for later main libbpf support in iproute2.
bpf.c is moved to bpf_legacy.c first.

A new file bpf_glue.c is added which could call both legacy libbpf code.
There are two wrapper functions added for ipvrf. Function
bpf_prog_load() is removed as it's conflict with libbpf function name.

v4: Add new file bpf_glue.c to for libbpf/legacy mixed bpf calls.
v2-v3: no update

Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Hangbin Liu <haliu@redhat.com>
---
 include/bpf_util.h          | 10 +++++++---
 ip/ipvrf.c                  |  6 +++---
 lib/Makefile                |  2 +-
 lib/bpf_glue.c              | 35 +++++++++++++++++++++++++++++++++++
 lib/{bpf.c => bpf_legacy.c} | 15 +++------------
 5 files changed, 49 insertions(+), 19 deletions(-)
 create mode 100644 lib/bpf_glue.c
 rename lib/{bpf.c => bpf_legacy.c} (99%)

Comments

David Ahern Nov. 14, 2020, 3:24 a.m. UTC | #1
On 11/9/20 12:07 AM, Hangbin Liu wrote:
> diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c
> new file mode 100644
> index 00000000..7626a893
> --- /dev/null
> +++ b/lib/bpf_glue.c

...

> +
> +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_load_load_dev(type, insns, size_insns, license, 0, log, size_log);
> +#endif
> +}
> +

Fails to compile:

$ LIBBPF_FORCE=off ./configure
$ make
...
/usr/bin/ld: ../lib/libutil.a(bpf_glue.o): in function `bpf_program_load':
bpf_glue.c:(.text+0x13): undefined reference to `bpf_load_load_dev'
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:27: ip] Error 1
make: *** [Makefile:64: all] Error 2
Hangbin Liu Nov. 16, 2020, 3:55 a.m. UTC | #2
On Fri, Nov 13, 2020 at 08:24:41PM -0700, David Ahern wrote:
> On 11/9/20 12:07 AM, Hangbin Liu wrote:
> > diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c
> > new file mode 100644
> > index 00000000..7626a893
> > --- /dev/null
> > +++ b/lib/bpf_glue.c
> 
> ...
> 
> > +
> > +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_load_load_dev(type, insns, size_insns, license, 0, log, size_log);
> > +#endif
> > +}
> > +
> 
> Fails to compile:
> 
> $ LIBBPF_FORCE=off ./configure
> $ make
> ...
> /usr/bin/ld: ../lib/libutil.a(bpf_glue.o): in function `bpf_program_load':
> bpf_glue.c:(.text+0x13): undefined reference to `bpf_load_load_dev'

Opps, sorry for the typo...
diff mbox series

Patch

diff --git a/include/bpf_util.h b/include/bpf_util.h
index 63db07ca..82217cc6 100644
--- a/include/bpf_util.h
+++ b/include/bpf_util.h
@@ -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);
 
diff --git a/ip/ipvrf.c b/ip/ipvrf.c
index 28dd8e25..42779e5c 100644
--- a/ip/ipvrf.c
+++ b/ip/ipvrf.c
@@ -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;
diff --git a/lib/Makefile b/lib/Makefile
index 7cba1857..c9502f6a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -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 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
 
diff --git a/lib/bpf_glue.c b/lib/bpf_glue.c
new file mode 100644
index 00000000..7626a893
--- /dev/null
+++ b/lib/bpf_glue.c
@@ -0,0 +1,35 @@ 
+/*
+ * bpf_glue.c	BPF code to call both legacy and libbpf code
+ *
+ *		This program is free software; you can redistribute it and/or
+ *		modify it under the terms of the GNU General Public License
+ *		as published by the Free Software Foundation; either version
+ *		2 of the License, or (at your option) any later version.
+ *
+ * Authors:	Hangbin Liu <haliu@redhat.com>
+ *
+ */
+#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_load_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
+}
diff --git a/lib/bpf.c b/lib/bpf_legacy.c
similarity index 99%
rename from lib/bpf.c
rename to lib/bpf_legacy.c
index c7d45077..4246fb76 100644
--- a/lib/bpf.c
+++ b/lib/bpf_legacy.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;