Skip to contentSkip to conversation

bpf samples: Uses libbpf in tools/lib to do BPF operations

PATCH In review Wang Nan 5 participants 35 messages Dec 17, 2015 – Dec 19, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Wang Nan

author
#0 lore raw

cover bpf samples: Uses libbpf in tools/lib to do BPF operations

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Patch 10/10 removes unneeded files.

Cc Alexei Starovoitov <[email protected]>
Cc Alex Gartrell <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Brenden Blanco <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc David S. Miller <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Kaixu Xia <[email protected]>
Cc Michael Holzheu <[email protected]>
Cc Yang Shi <[email protected]>

Wang Nan (10):
bpf samples: bpf: Fix tracex5_kern.c compiling error bpf tools: Define LD and RM in Makefile for 'make -R' bpf tools: Add const decoretor to 'license' and 'insns' for
bpf_load_program()
bpf tools: Switch to uapi style type names
bpf tools: Support load different type of programs
bpf tools: Support new map operations
bpf tools: Support BPF_OBJ_PIN and BPF_OBJ_GET
bpf samples: Add utils.[ch] for using BPF
bpf samples: Uses libbpf in tools/lib to deal with BPF operations bpf samples: Remove old BPF helpers

 samples/bpf/Makefile              |  65 +++----
 samples/bpf/bpf_load.c            | 345 --------------------------------------
 samples/bpf/bpf_load.h            |  27 ---
 samples/bpf/fds_example.c         |  26 +--
 samples/bpf/include/linux/err.h   |  56 +++++++
 samples/bpf/lathist_user.c        |  13 +-
 samples/bpf/libbpf.c              | 154 -----------------
 samples/bpf/sock_example.c        |  13 +-
 samples/bpf/sockex1_kern.c        |   2 +
 samples/bpf/sockex1_user.c        |  27 +--
 samples/bpf/sockex2_kern.c        |   2 +
 samples/bpf/sockex2_user.c        |  26 +--
 samples/bpf/sockex3_kern.c        |   2 +
 samples/bpf/sockex3_user.c        |  23 ++-
 samples/bpf/test_maps.c           |  80 ++++-----
 samples/bpf/test_verifier.c       |  13 +-
 samples/bpf/trace_output_user.c   |  17 +-
 samples/bpf/tracex1_user.c        |   9 +-
 samples/bpf/tracex2_user.c        |  31 ++--
 samples/bpf/tracex3_user.c        |  15 +-
 samples/bpf/tracex4_user.c        |  15 +-
 samples/bpf/tracex5_kern.c        |   1 +
 samples/bpf/tracex5_user.c        |   9 +-
 samples/bpf/tracex6_user.c        |  16 +-
 samples/bpf/utils.c               | 276 ++++++++++++++++++++++++++++++
 samples/bpf/{libbpf.h => utils.h} |  58 ++++---
 tools/lib/bpf/Makefile            |   2 +
 tools/lib/bpf/bpf.c               |  65 ++++++-
 tools/lib/bpf/bpf.h               |  16 +-
 tools/lib/bpf/libbpf.c            |  43 ++++-
 tools/lib/bpf/libbpf.h            |  16 ++
 31 files changed, 718 insertions(+), 745 deletions(-)
 delete mode 100644 samples/bpf/bpf_load.c
 delete mode 100644 samples/bpf/bpf_load.h
 create mode 100644 samples/bpf/include/linux/err.h
 delete mode 100644 samples/bpf/libbpf.c
 create mode 100644 samples/bpf/utils.c
 rename samples/bpf/{libbpf.h => utils.h} (81%)
Signature · 1 line
1.8.3.4
author
#1 lore raw

PATCH 1/10 bpf samples: bpf: Fix tracex5_kern.c compiling error

LLVM report compiling error:

/kpathsamples/bpf/tracex5_kern.c:33:15: error: use of undeclared identifier '__NR_getuid'; did you

       mean '__NR_vgetcpu'?
         if (sd.nr >= __NR_getuid && sd.nr <= __NR_getsid) {
                      ^~~~~~~~~~~
                      __NR_vgetcpu
 /kpath/arch/x86/include/uapi/asm/vsyscall.h:7:2: note: '__NR_vgetcpu' declared here
         __NR_vgetcpu,

This patch fix it by adding asm/unistd.h to it.

Cc Alexei Starovoitov <[email protected]>
Cc David S. Miller <[email protected]>
---
 samples/bpf/tracex5_kern.c | 1 +
 1 file changed, 1 insertion(+)
samples/bpf/tracex5_kern.c +1 -0
index b3f4295..2964efd 100644
@@ -8,6 +8,7 @@
 #include <linux/version.h>
 #include <uapi/linux/bpf.h>
 #include <uapi/linux/seccomp.h>
+#include <asm/unistd.h>
 #include "bpf_helpers.h"
 
 #define PROG(F) SEC("kprobe/"__stringify(F)) int bpf_func_##F
Signature · 1 line
1.8.3.4
author
#2 lore raw

PATCH 2/10 bpf tools: Define LD and RM in Makefile for 'make -R'

In reply to Wang Nan

Building libbpf with 'make -R' causes error:

 $ make -R

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

CC fixdep.o
LD fixdep-in.o
/bin/sh: -r: command not found
make[2]: *** [fixdep-in.o] Error 127
make[1]: *** [fixdep-in.o] Error 2
make: *** [fixdep] Error 2

Makefile for libbpf requires $(LD) and $(RM), but in case of 'make -R' the builtin variables are gone. This patch define default RM and LD so it won't fail in 'make -R'.

Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/lib/bpf/Makefile | 2 ++
 1 file changed, 2 insertions(+)
tools/lib/bpf/Makefile +2 -0
index 0b6e013..7bec12f 100644
@@ -5,6 +5,8 @@ BPF_PATCHLEVEL = 0
 BPF_EXTRAVERSION = 1
 
 MAKEFLAGS += --no-print-directory
+RM ?= rm -f
+LD ?= $(CROSS_COMPILE)ld
 
 ifeq ($(srctree),)
 srctree := $(patsubst %/,%,$(dir $(shell pwd)))
Signature · 1 line
1.8.3.4
author
#3 lore raw

PATCH 3/10 bpf tools: Add const decoretor to 'license' and 'insns' for bpf_load_program()

In reply to Wang Nan

Actually 'license' and 'insns' are const pointer. Before this patch using 'bpf_load_program(... "GPL", ...)' triggers a warning, which is unnecessary.

This patch makes these two arguments const pointers.

Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/lib/bpf/bpf.c | 8 ++++----
 tools/lib/bpf/bpf.h | 4 ++--
 2 files changed, 6 insertions(+), 6 deletions(-)
tools/lib/bpf/bpf.c +4 -4
index 1f91cc9..88e6a46 100644
@@ -55,8 +55,8 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size,
 	return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
 }
 
-int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
-		     size_t insns_cnt, char *license,
+int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
+		     size_t insns_cnt, const char *license,
 		     u32 kern_version, char *log_buf, size_t log_buf_sz)
 {
 	int fd;
@@ -65,8 +65,8 @@ int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
 	bzero(&attr, sizeof(attr));
 	attr.prog_type = type;
 	attr.insn_cnt = (__u32)insns_cnt;
-	attr.insns = ptr_to_u64(insns);
-	attr.license = ptr_to_u64(license);
+	attr.insns = ptr_to_u64((void *)insns);
+	attr.license = ptr_to_u64((void *)license);
 	attr.log_buf = ptr_to_u64(NULL);
 	attr.log_size = 0;
 	attr.log_level = 0;
tools/lib/bpf/bpf.h +2 -2
index a764655..00cfeae 100644
@@ -15,8 +15,8 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
 
 /* Recommend log buffer size */
 #define BPF_LOG_BUF_SIZE 65536
-int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
-		     size_t insns_cnt, char *license,
+int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
+		     size_t insns_cnt, const char *license,
 		     u32 kern_version, char *log_buf,
 		     size_t log_buf_sz);
 
Signature · 1 line
1.8.3.4
author
#4 lore raw

PATCH 4/10 bpf tools: Switch to uapi style type names

In reply to Wang Nan

Types "u64, u32" don't exist in uapi header (linux/types.h). Because of that directly include bpf.h causes error.

This patch fixes this by replacing all occurrences of "u32, u64" in API to "__u32, __u64". The later can be found in 'uapi/linux/types.h' so it would be safe for normal program (other than perf).

Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/lib/bpf/bpf.c | 4 ++--
 tools/lib/bpf/bpf.h | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)
tools/lib/bpf/bpf.c +2 -2
index 88e6a46..e0dccea 100644
@@ -57,7 +57,7 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size,
 
 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 		     size_t insns_cnt, const char *license,
-		     u32 kern_version, char *log_buf, size_t log_buf_sz)
+		     __u32 kern_version, char *log_buf, size_t log_buf_sz)
 {
 	int fd;
 	union bpf_attr attr;
@@ -85,7 +85,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 }
 
 int bpf_map_update_elem(int fd, void *key, void *value,
-			u64 flags)
+			__u64 flags)
 {
 	union bpf_attr attr;
 
tools/lib/bpf/bpf.h +3 -2
index 00cfeae..01a421a 100644
@@ -8,6 +8,7 @@
 #ifndef __BPF_BPF_H
 #define __BPF_BPF_H
 
+#include <linux/types.h>
 #include <linux/bpf.h>
 
 int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
@@ -17,9 +18,9 @@ int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
 #define BPF_LOG_BUF_SIZE 65536
 int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 		     size_t insns_cnt, const char *license,
-		     u32 kern_version, char *log_buf,
+		     __u32 kern_version, char *log_buf,
 		     size_t log_buf_sz);
 
 int bpf_map_update_elem(int fd, void *key, void *value,
-			u64 flags);
+			__u64 flags);
 #endif
Signature · 1 line
1.8.3.4
author
#5 lore raw

PATCH 5/10 bpf tools: Support load different type of programs

In reply to Wang Nan

Before this patch libbpf can only load program with type BPF_PROG_TYPE_KPROBE program. To make libbpf useful in other scenarios, this patch introduced bpf_program__set_type() and bpf_object__set_type() which allows setting program type.

This changes doesn't break old API. The default program type is BPF_PROG_TYPE_KPROBE. If caller wants to load program with other type, one of these API should be called before bpf_object__load() or in callback function of loader.

Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/lib/bpf/libbpf.c | 43 ++++++++++++++++++++++++++++++++++++++-----
 tools/lib/bpf/libbpf.h | 16 ++++++++++++++++
 2 files changed, 54 insertions(+), 5 deletions(-)
tools/lib/bpf/libbpf.c +38 -5
index 8334a5a..99be7f1 100644
@@ -66,6 +66,7 @@ void libbpf_set_print(libbpf_print_fn_t warn,
 #define ERRNO_OFFSET(e)		((e) - __LIBBPF_ERRNO__START)
 #define ERRCODE_OFFSET(c)	ERRNO_OFFSET(LIBBPF_ERRNO__##c)
 #define NR_ERRNO	(__LIBBPF_ERRNO__END - __LIBBPF_ERRNO__START)
+#define BPF_PROG_TYPE_MAX	BPF_PROG_TYPE_SCHED_ACT
 
 static const char *libbpf_strerror_table[NR_ERRNO] = {
 	[ERRCODE_OFFSET(LIBELF)]	= "Something wrong in libelf",
@@ -161,6 +162,7 @@ struct bpf_program {
 	struct bpf_object *obj;
 	void *priv;
 	bpf_program_clear_priv_t clear_priv;
+	enum bpf_prog_type type;
 };
 
 struct bpf_map {
@@ -323,6 +325,7 @@ bpf_object__add_program(struct bpf_object *obj, void *data, size_t size,
 	obj->programs = progs;
 	obj->nr_programs = nr_progs + 1;
 	prog.obj = obj;
+	prog.type = BPF_PROG_TYPE_KPROBE;
 	progs[nr_progs] = prog;
 	return 0;
 }
@@ -871,7 +874,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 }
 
 static int
-load_program(struct bpf_insn *insns, int insns_cnt,
+load_program(struct bpf_insn *insns, int insns_cnt, enum bpf_prog_type type,
 	     char *license, u32 kern_version, int *pfd)
 {
 	int ret;
@@ -884,9 +887,8 @@ load_program(struct bpf_insn *insns, int insns_cnt,
 	if (!log_buf)
 		pr_warning("Alloc log buffer for bpf loader error, continue without log\n");
 
-	ret = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
-			       insns_cnt, license, kern_version,
-			       log_buf, BPF_LOG_BUF_SIZE);
+	ret = bpf_load_program(type, insns, insns_cnt, license,
+			       kern_version, log_buf, BPF_LOG_BUF_SIZE);
 
 	if (ret >= 0) {
 		*pfd = ret;
@@ -945,7 +947,7 @@ bpf_program__load(struct bpf_program *prog,
 			pr_warning("Program '%s' is inconsistent: nr(%d) != 1\n",
 				   prog->section_name, prog->instances.nr);
 		}
-		err = load_program(prog->insns, prog->insns_cnt,
+		err = load_program(prog->insns, prog->insns_cnt, prog->type,
 				   license, kern_version, &fd);
 		if (!err)
 			prog->instances.fds[0] = fd;
@@ -976,6 +978,7 @@ bpf_program__load(struct bpf_program *prog,
 
 		err = load_program(result.new_insn_ptr,
 				   result.new_insn_cnt,
+				   prog->type,
 				   license, kern_version, &fd);
 
 		if (err) {
@@ -1192,6 +1195,25 @@ bpf_object__get_kversion(struct bpf_object *obj)
 	return obj->kern_version;
 }
 
+int bpf_object__set_type(struct bpf_object *obj,
+			 enum bpf_prog_type type)
+{
+	struct bpf_program *prog;
+	int err;
+
+	if (!obj || type <= BPF_PROG_TYPE_UNSPEC ||
+	    type > BPF_PROG_TYPE_MAX)
+		return -EINVAL;
+
+	bpf_object__for_each_program(prog, obj) {
+		err = bpf_program__set_type(prog, type);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 struct bpf_program *
 bpf_program__next(struct bpf_program *prev, struct bpf_object *obj)
 {
@@ -1281,6 +1303,17 @@ int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
 	return 0;
 }
 
+int bpf_program__set_type(struct bpf_program *prog,
+			  enum bpf_prog_type type)
+{
+	if (!prog || type <= BPF_PROG_TYPE_UNSPEC ||
+	    type > BPF_PROG_TYPE_MAX)
+		return -EINVAL;
+
+	prog->type = type;
+	return 0;
+}
+
 int bpf_program__nth_fd(struct bpf_program *prog, int n)
 {
 	int fd;
tools/lib/bpf/libbpf.h +16 -0
index a51594c..c9d4130 100644
@@ -11,6 +11,7 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <linux/err.h>
+#include <linux/bpf.h>
 
 enum libbpf_errno {
 	__LIBBPF_ERRNO__START = 4000,
@@ -58,6 +59,13 @@ int bpf_object__unload(struct bpf_object *obj);
 const char *bpf_object__get_name(struct bpf_object *obj);
 unsigned int bpf_object__get_kversion(struct bpf_object *obj);
 
+/*
+ * Set type of all programs in an object.
+ * See bpf_program__set_type().
+ */
+int bpf_object__set_type(struct bpf_object *obj,
+			 enum bpf_prog_type type);
+
 struct bpf_object *bpf_object__next(struct bpf_object *prev);
 #define bpf_object__for_each_safe(pos, tmp)			\
 	for ((pos) = bpf_object__next(NULL),		\
@@ -150,6 +158,14 @@ typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
 int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
 			  bpf_program_prep_t prep);
 
+/*
+ * Set type of a program. Default is BPF_PROG_TYPE_KPROBE.
+ * This function should be called before bpf_object__load()
+ * or in bpf_program_prep callback.
+ */
+int bpf_program__set_type(struct bpf_program *prog,
+			  enum bpf_prog_type type);
+
 int bpf_program__nth_fd(struct bpf_program *prog, int n);
 
 /*
Signature · 1 line
1.8.3.4
author
#6 lore raw

PATCH 6/10 bpf tools: Support new map operations

In reply to Wang Nan

Add bpf_map_lookup_elem(), bpf_map_delete_elem() and bpf_map_get_next_key() to libbpf so it can issue all BPF map operations.

Cc Arnaldo Carvalho de Melo <[email protected]>
---
 tools/lib/bpf/bpf.c | 32 ++++++++++++++++++++++++++++++++
 tools/lib/bpf/bpf.h |  4 ++++
 2 files changed, 36 insertions(+)
tools/lib/bpf/bpf.c +32 -0
index e0dccea..89c9d0b 100644
@@ -97,3 +97,35 @@ int bpf_map_update_elem(int fd, void *key, void *value,
 
 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
 }
+
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+		.value = ptr_to_u64(value),
+	};      
+
+	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_delete_elem(int fd, void *key)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+	};
+
+	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_get_next_key(int fd, void *key, void *next_key)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+		.next_key = ptr_to_u64(next_key),
+	};
+
+	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
+}
tools/lib/bpf/bpf.h +4 -0
index 01a421a..3d22048 100644
@@ -23,4 +23,8 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 
 int bpf_map_update_elem(int fd, void *key, void *value,
 			__u64 flags);
+
+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
 #endif
Signature · 1 line
1.8.3.4
#7 lore raw

Re: [PATCH 06/10] bpf tools: Support new map operations

[SNIP]

tools/lib/bpf/bpf.c int bpf_map_update_elem(int fd, void *key, void *value, · line 107

Quoted 7 lines
tools/lib/bpf/bpf.c +7 -0
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+		.value = ptr_to_u64(value),
+	};

Tailing whitespace here. Sorry...

author
#8 lore raw

PATCH v2 6/10 bpf tools: Support new map operations

In reply to Wang Nan

Add bpf_map_lookup_elem(), bpf_map_delete_elem() and bpf_map_get_next_key() to libbpf so it can issue all BPF map operations.

Cc Arnaldo Carvalho de Melo <[email protected]>

---

v1 -> v2: Remove tailing whitespaces.

---
 tools/lib/bpf/bpf.c | 32 ++++++++++++++++++++++++++++++++
 tools/lib/bpf/bpf.h |  4 ++++
 2 files changed, 36 insertions(+)
tools/lib/bpf/bpf.c +32 -0
index e0dccea..b8e7aaa 100644
@@ -97,3 +97,35 @@ int bpf_map_update_elem(int fd, void *key, void *value,
 
 	return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
 }
+
+int bpf_map_lookup_elem(int fd, void *key, void *value)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+		.value = ptr_to_u64(value),
+	};
+
+	return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_delete_elem(int fd, void *key)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+	};
+
+	return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
+}
+
+int bpf_map_get_next_key(int fd, void *key, void *next_key)
+{
+	union bpf_attr attr = {
+		.map_fd = fd,
+		.key = ptr_to_u64(key),
+		.next_key = ptr_to_u64(next_key),
+	};
+
+	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
+}
tools/lib/bpf/bpf.h +4 -0
index 01a421a..3d22048 100644
@@ -23,4 +23,8 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
 
 int bpf_map_update_elem(int fd, void *key, void *value,
 			__u64 flags);
+
+int bpf_map_lookup_elem(int fd, void *key, void *value);
+int bpf_map_delete_elem(int fd, void *key);
+int bpf_map_get_next_key(int fd, void *key, void *next_key);
 #endif
Signature · 1 line
1.8.3.4
author
#9 lore raw

PATCH 7/10 bpf tools: Support BPF_OBJ_PIN and BPF_OBJ_GET

In reply to Wang Nan

Commit b2197755b2633e164a439682fb05a9b5ea48f706 ("bpf: add support for persistent maps/progs") introduces two new operations to both map and program. This patch makes libbpf support it.

Cc Alexei Starovoitov <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 tools/lib/bpf/bpf.c | 21 +++++++++++++++++++++
 tools/lib/bpf/bpf.h |  3 +++
 2 files changed, 24 insertions(+)
tools/lib/bpf/bpf.c +21 -0
index 89c9d0b..5f174c6 100644
@@ -129,3 +129,24 @@ int bpf_map_get_next_key(int fd, void *key, void *next_key)
 
 	return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
 }
+
+int bpf_pin_object(int fd, const char *pathname)
+{
+	union bpf_attr attr;
+
+	bzero(&attr, sizeof(attr));
+	attr.pathname = ptr_to_u64((void *)pathname);
+	attr.bpf_fd = fd;
+
+	return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
+}
+
+int bpf_get_pinned_object(const char *pathname)
+{
+	union bpf_attr attr;
+
+	bzero(&attr, sizeof(attr));
+	attr.pathname = ptr_to_u64((void *)pathname);
+
+	return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
+}
tools/lib/bpf/bpf.h +3 -0
index 3d22048..c9f1a1c 100644
@@ -27,4 +27,7 @@ int bpf_map_update_elem(int fd, void *key, void *value,
 int bpf_map_lookup_elem(int fd, void *key, void *value);
 int bpf_map_delete_elem(int fd, void *key);
 int bpf_map_get_next_key(int fd, void *key, void *next_key);
+
+int bpf_pin_object(int fd, const char *pathname);
+int bpf_get_pinned_object(const char *pathname);
 #endif
Signature · 1 line
1.8.3.4
author
#10 lore raw

PATCH 8/10 bpf samples: Add utils.[ch] for using BPF

In reply to Wang Nan

We are going to uses libbpf to replace old libbpf.[ch] and bpf_load.[ch]. This is the first patch of this work. In this patch, several macros and helpers in libbpf.[ch] and bpf_load.[ch] are merged into utils.[ch]. utils.[ch] utilizes libbpf in tools/lib to deal with BPF related things. They would be compiled after Makefile changes.

Cc Alexei Starovoitov <[email protected]>
Cc Alex Gartrell <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Brenden Blanco <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc David S. Miller <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kaixu Xia <[email protected]>
Cc Michael Holzheu <[email protected]>
Cc Yang Shi <[email protected]>
---
 samples/bpf/include/linux/err.h |  56 ++++++++
 samples/bpf/utils.c             | 276 ++++++++++++++++++++++++++++++++++++++++
 samples/bpf/utils.h             | 217 +++++++++++++++++++++++++++++++
 3 files changed, 549 insertions(+)
 create mode 100644 samples/bpf/include/linux/err.h
 create mode 100644 samples/bpf/utils.c
 create mode 100644 samples/bpf/utils.h
samples/bpf/include/linux/err.h new file +56 -0
new file mode 100644
index 0000000..671b874
@@ -0,0 +1,56 @@
+#ifndef __TOOLS_LINUX_ERR_H
+#define __TOOLS_LINUX_ERR_H
+
+#include <asm/errno.h>
+
+#ifndef __must_check
+# define __must_check
+#endif
+#ifndef __force
+# define __force
+#endif
+#ifndef unlikely
+# define unlikely(x) x
+#endif
+
+/*
+ * Original kernel header comment:
+ *
+ * Kernel pointers have redundant information, so we can use a
+ * scheme where we can return either an error code or a normal
+ * pointer with the same return value.
+ *
+ * This should be a per-architecture thing, to allow different
+ * error and pointer decisions.
+ *
+ * Userspace note:
+ * The same principle works for userspace, because 'error' pointers
+ * fall down to the unused hole far from user space, as described
+ * in Documentation/x86/x86_64/mm.txt for x86_64 arch:
+ *
+ * 0000000000000000 - 00007fffffffffff (=47 bits) user space, different per mm hole caused by [48:63] sign extension
+ * ffffffffffe00000 - ffffffffffffffff (=2 MB) unused hole
+ *
+ * It should be the same case for other architectures, because
+ * this code is used in generic kernel code.
+ */
+#define MAX_ERRNO	4095
+
+#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void * __must_check ERR_PTR(long error_)
+{
+	return (void *) error_;
+}
+
+static inline long __must_check PTR_ERR(__force const void *ptr)
+{
+	return (long) ptr;
+}
+
+static inline bool __must_check IS_ERR(__force const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}
+
+#endif /* _LINUX_ERR_H */
samples/bpf/utils.c new file +276 -0

276 lines not on this page · raw message

samples/bpf/utils.h new file +217 -0

217 lines not on this page · raw message

Signature · 1 line
1.8.3.4
#11 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

Quoted 8 lines · Wang Nan

We are going to uses libbpf to replace old libbpf.[ch] and bpf_load.[ch]. This is the first patch of this work. In this patch, several macros and helpers in libbpf.[ch] and bpf_load.[ch] are merged into utils.[ch]. utils.[ch] utilizes libbpf in tools/lib to deal with BPF related things. They would be compiled after Makefile changes.

...

Quoted 6 lines
samples/bpf/include/linux/err.h new file +6 -0
+#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void * __must_check ERR_PTR(long error_)
+{
+	return (void *) error_;
+}

samples/bpf/include/linux/err.h line 54

Quoted 10 lines
samples/bpf/include/linux/err.h new file +10 -0
+
+static inline long __must_check PTR_ERR(__force const void *ptr)
+{
+	return (long) ptr;
+}
+
+static inline bool __must_check IS_ERR(__force const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}

why copy paste this? I don't see the code that uses that.

samples/bpf/utils.c line 242

Quoted 13 lines
samples/bpf/utils.c new file +13 -0
+	bpf_object__for_each_program(prog, obj) {
+		const char *event = bpf_program__title(prog, false);
+		int fd, err;
+
+		LIBBPF_PTR_ASSERT(event, goto errout);
+		__LIBBPF_ASSERT(fd = bpf_program__nth_fd(prog, 0),
+				>= 0,
+				goto errout);
+
+		if (strncmp(event, "kprobe/", 7) == 0)
+			err = create_kprobes(fd, event + 7, true);
+		else if (strncmp(event, "kretprobe/", 10) == 0)
+			err = create_kprobes(fd, event + 10, false);

I have a feeling that all bpf+socket, tcbpf1_kernc and trace_output_*.c are broken, since I don't see a code that attaches programs to sockets and to perf_event.
How did you test it?

Quoted 11 lines
samples/bpf/utils.h new file +5 -0
new file mode 100644
index 0000000..5962a68
@@ -0,0 +1,217 @@
+#ifndef __SAMPELS_UTILS_H
+#define __SAMPELS_UTILS_H
+
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>

samples/bpf/utils.h line 15

Quoted 10 lines
samples/bpf/utils.h new file +10 -0
+
+/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
+
+#define BPF_ALU64_REG(OP, DST, SRC)				\
+	((struct bpf_insn) {					\
+		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,	\
+		.dst_reg = DST,					\
+		.src_reg = SRC,					\
+		.off   = 0,					\
+		.imm   = 0 })

this probably belongs in tools/lib/bpf/bpf.h instead of samples.

The whole set depends on changes in perf/core tree, but
in net-next we have extra commit 30b50aa612018, so I don't see an easy way to route this patch without creating across-tree merge conflicts during merge window.
I'd suggest to apply all required work to tools/lib/bpf/ into perf/core and leave samples/bpf/ after merge window.

#12 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

Quoted 19 lines · Alexei Starovoitov
Quoted 8 lines · Wang Nan

We are going to uses libbpf to replace old libbpf.[ch] and bpf_load.[ch]. This is the first patch of this work. In this patch, several macros and helpers in libbpf.[ch] and bpf_load.[ch] are merged into utils.[ch]. utils.[ch] utilizes libbpf in tools/lib to deal with BPF related things. They would be compiled after Makefile changes.

...

Quoted 9 lines
samples/bpf/include/linux/err.h new file +9 -0
+#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void * __must_check ERR_PTR(long error_)
+{
+	return (void *) error_;
+}
+
+static inline long __must_check PTR_ERR(__force const void *ptr)
+{
Quoted 8 lines

samples/bpf/include/linux/err.h line 54

Quoted 7 lines
samples/bpf/include/linux/err.h new file +7 -0
+	return (long) ptr;
+}
+
+static inline bool __must_check IS_ERR(__force const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}

why copy paste this? I don't see the code that uses that.

This is a limitation in tools/lib/bpf/libbpf.h, which has a #include <linux/err.h> in its header.

libbpf.h requires this include because its API uses ERR_PTR() to encode error code. For example, when calling bpf_object__open(), caller should use IS_ERR() to check its return value instead of compare with NULL, and use PTR_ERR() to retrive error number.

However, linux/err.h is not a part of uapi. To make libbpf work, one has to create its own err.h.

Now I'm thinking provide LIBBPF_{IS_ERR,PTR_ERR}(), in libbpf itself.

Quoted 9 lines
Quoted 9 lines
samples/bpf/utils.c new file +9 -0
+	bpf_object__for_each_program(prog, obj) {
+		const char *event = bpf_program__title(prog, false);
+		int fd, err;
+
+		LIBBPF_PTR_ASSERT(event, goto errout);
+		__LIBBPF_ASSERT(fd = bpf_program__nth_fd(prog, 0),
+				>= 0,
+				goto errout);
+
Quoted 8 lines

samples/bpf/utils.c line 242

Quoted 4 lines
samples/bpf/utils.c new file +4 -0
+		if (strncmp(event, "kprobe/", 7) == 0)
+			err = create_kprobes(fd, event + 7, true);
+		else if (strncmp(event, "kretprobe/", 10) == 0)
+			err = create_kprobes(fd, event + 10, false);

I have a feeling that all bpf+socket, tcbpf1_kernc and trace_output_*.c are broken, since I don't see a code that attaches programs to sockets and to perf_event.
How did you test it?

I tested all samples (except tcbpf1_kern, because it is loaded by tc but tc has not switched to libbpf) in my environment. They are okay for me. There's no socket attaching code in this patchset because they are in sockex?_user.c like this:

        obj = load_bpf_file(filename);
        if (!obj)
                return 1;
        ...
        prog_fd = get_prog_fd(obj, 0);
        ...
        sock = open_raw_sock("lo");

        assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
                          sizeof(prog_fd)) == 0);

And I don't touch the setsockopt in all patches.

Quoted 14 lines
Quoted 14 lines
samples/bpf/utils.h new file +8 -0
new file mode 100644
index 0000000..5962a68
@@ -0,0 +1,217 @@
+#ifndef __SAMPELS_UTILS_H
+#define __SAMPELS_UTILS_H
+
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+
+/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
+
Quoted 8 lines

samples/bpf/utils.h line 15

Quoted 7 lines
samples/bpf/utils.h new file +7 -0
+#define BPF_ALU64_REG(OP, DST, SRC)				\
+	((struct bpf_insn) {					\
+		.code  = BPF_ALU64 | BPF_OP(OP) | BPF_X,	\
+		.dst_reg = DST,					\
+		.src_reg = SRC,					\
+		.off   = 0,					\
+		.imm   = 0 })

this probably belongs in tools/lib/bpf/bpf.h instead of samples.

Orignally they are macros defined in linux/filter.h. We have 3 filter.h in kernel tree:

include/linux/filter.h
include/uapi/linux/filter.h
tools/include/linux/filter.h

These macros belong to include/linux/filter.h, not part of uapi, so we have to do things like what we have done for tools/include/linux/filter.h.

What about moving them into include/uapi/linux/filter.h ? Then normal user programs like those in samples/bpf can access them easier.

Quoted 6 lines

The whole set depends on changes in perf/core tree, but
in net-next we have extra commit 30b50aa612018, so I don't see an easy way to route this patch without creating across-tree merge conflicts during merge window.
I'd suggest to apply all required work to tools/lib/bpf/ into perf/core and leave samples/bpf/ after merge window.

Good suggestion.

I'll resend them after the PowerPC building breakage fixing is collected.

Thank you.

#13 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

Quoted 12 lines · Wangnan (F)

This is a limitation in tools/lib/bpf/libbpf.h, which has a #include <linux/err.h>
in its header.

libbpf.h requires this include because its API uses ERR_PTR() to encode error code.
For example, when calling bpf_object__open(), caller should use IS_ERR() to check its
return value instead of compare with NULL, and use PTR_ERR() to retrive error number.

Quoted 3 lines

However, linux/err.h is not a part of uapi. To make libbpf work, one has to create its
own err.h.

Why tools/include/linux/err.h is not suitable for everyone?

Quoted 1 line

Now I'm thinking provide LIBBPF_{IS_ERR,PTR_ERR}(), in libbpf itself.

seems odd. we already have user space err.h in tools/include.

Quoted 1 line

And I don't touch the setsockopt in all patches.

ok, but where is the bit that does attach to perf_event to make trace_output work?

Quoted 1 line

Orignally they are macros defined in linux/filter.h.

no. they were never part of offical filter.h. Only in my earlier versions of bpf patches, but we decided to drop them before they got into net-next.

Quoted 3 lines

What about moving them into include/uapi/linux/filter.h ? Then normal user programs like those in samples/bpf can access them easier.

we don't want to add these macros to uapi.
Why not to add it to
tools/include/linux/filter.h
instead?

#14 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · Wangnan (F)

This is a limitation in tools/lib/bpf/libbpf.h, which has a #include <linux/err.h>
in its header.

libbpf.h requires this include because its API uses ERR_PTR() to encode error code.
For example, when calling bpf_object__open(), caller should use IS_ERR() to check its
return value instead of compare with NULL, and use PTR_ERR() to retrive error number.

Quoted 7 lines
Quoted 3 lines

However, linux/err.h is not a part of uapi. To make libbpf work, one has to create its
own err.h.

Why tools/include/linux/err.h is not suitable for everyone?

Quoted 1 line

Now I'm thinking provide LIBBPF_{IS_ERR,PTR_ERR}(), in libbpf itself.

seems odd. we already have user space err.h in tools/include.

Currently samples/bpf doesn't have an -I$(srctree)/tools/include.

I tried to add it into CFLAGS of samples/bpf. It causes other problems, This is what I get:

In file included from /home/w00229757/kernel-hydrogen/samples/bpf/sock_example.c:27:0:
/usr/include/linux/ip.h:101:2: error: unknown type name ‘__sum16’
__sum16 check;
^
make[3]: *** [samples/bpf/sock_example.o] Error 1
make[2]: *** [samples/bpf/] Error 2
make[1]: *** [sub-make] Error 2
make: *** [__sub-make] Error 2

And after fixing __sum16 in linux/types.h:

  HOSTCC  samples/bpf/tracex4_user.o
  HOSTLD  samples/bpf/tracex4
  HOSTCC  samples/bpf/tracex5_user.o
/kernel/samples/bpf/tracex5_user.c: In function ‘install_accept_all_seccomp’:
/kernel/samples/bpf/tracex5_user.c:15:21: error: array type has incomplete element type
  struct sock_filter filter[] = {
                     ^

/kernel/samples/bpf/tracex5_user.c:16:3: warning: implicit declaration of function ‘BPF_STMT’ [-Wimplicit-function-declaration]
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
^
/kernel/samples/bpf/tracex5_user.c:18:9: error: variable ‘prog’ has initializer but incomplete type

  struct sock_fprog prog = {
         ^

Finally we need to add sock_filter, sock_fprog, BPF_STMT into tools/include/linux/filter.h.

It is okay, but different from what I really want to do. I'll discuss this later.

Quoted 2 lines
Quoted 1 line

And I don't touch the setsockopt in all patches.

ok, but where is the bit that does attach to perf_event to make trace_output work?

I didn't change this test_bpf_perf_event() function (only the function name). It creates a bpf-output perf event. This event is inserted into a BPF_MAP_TYPE_PERF_EVENT_ARRAY by bpf_map_update_elem().

static void test_bpf_perf_event(int map_fd)

{
        struct perf_event_attr attr = {
                .sample_type = PERF_SAMPLE_RAW,
                .type = PERF_TYPE_SOFTWARE,
                .config = PERF_COUNT_SW_BPF_OUTPUT,
        };
        int key = 0;

        pmu_fd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, 

-1/*group_fd*/, 0);

        assert(pmu_fd >= 0);
        assert(bpf_map_update_elem(map_fd, &key, &pmu_fd, BPF_ANY) == 0);
        ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
}

And you read from this pmu_fd, get results. The logical is unchanged.

Quoted 5 lines
Quoted 2 lines

Orignally they are macros defined in linux/filter.h.

no. they were never part of offical filter.h. Only in my earlier versions of bpf patches, but we decided to drop them before they got into net-next.

Quoted 7 lines
Quoted 3 lines

What about moving them into include/uapi/linux/filter.h ? Then normal user programs like those in samples/bpf can access them easier.

we don't want to add these macros to uapi.
Why not to add it to
tools/include/linux/filter.h
instead?

What I want to do in this patchset is not only removing original libbpf.c and bpf_load.c. In fact I want libbpf in tools/lib/bpf becomes a public available library for other userspace tools (tc for example). Switching samples/bpf into libbpf is the first step of this goal. From doing this I found and fixed some limitation, like those missed BPF map operations. Making libbpf.h and bpf.h available for normal userspace programs is also important.

Having the above goal, I think you can understand why improving tools/include is not a good idea. You don't want to force a normal userspace program setup a similar header environment for using libbpf. It is relatively a small library. So it would be good if bpf.h and libbpf.h only depend on what can be found in uapi.

Thank you.

#15 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

Quoted 19 lines · Wangnan (F)
Quoted 16 lines · Alexei Starovoitov
Quoted 14 lines · Wangnan (F)

This is a limitation in tools/lib/bpf/libbpf.h, which has a #include <linux/err.h>
in its header.

libbpf.h requires this include because its API uses ERR_PTR() to encode error code.
For example, when calling bpf_object__open(), caller should use IS_ERR() to check its
return value instead of compare with NULL, and use PTR_ERR() to retrive error number.

However, linux/err.h is not a part of uapi. To make libbpf work, one has to create its
own err.h.

[SNIP]

Quoted 16 lines
Quoted 7 lines
Quoted 3 lines

What about moving them into include/uapi/linux/filter.h ? Then normal user programs like those in samples/bpf can access them easier.

we don't want to add these macros to uapi.
Why not to add it to
tools/include/linux/filter.h
instead?

What I want to do in this patchset is not only removing original libbpf.c and bpf_load.c. In fact I want libbpf in tools/lib/bpf becomes a public available library for other userspace tools (tc for example). Switching samples/bpf into libbpf is the first step of this goal. From doing this I found and fixed some limitation, like those missed BPF map operations. Making libbpf.h and bpf.h available for normal userspace programs is also important.

Quoted 6 lines

Having the above goal, I think you can understand why improving tools/include is not a good idea. You don't want to force a normal userspace program setup a similar header environment for using libbpf. It is relatively a small library. So it would be good if bpf.h and libbpf.h only depend on what can be found in uapi.

I suddenly realized that only linux/err.h causes problem. Those macros from filter.h are never accessed by libbpf. So we can drop those filter.h by making samples/bpf include from tools/include. However we still need a wrapper in libbpf to avoid including linux/err.h.

Thank you.

maintainer
#16 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

In reply to Wangnan (F)

On 12/18/2015 08:04 AM, Wangnan (F) wrote:
...

Quoted 3 lines

What I want to do in this patchset is not only removing original libbpf.c and bpf_load.c. In fact I want libbpf in tools/lib/bpf becomes a public available library for other userspace tools (tc for example).

Having this as a possible user space library seems fine.

Only speaking for the tc case specifically here (since you mention it as one example), there's currently no additional value for it. The current loader code there is functional and partially tailored for tc's needs and having this together with the iproute2 repo, it allows us to easily change/ adapt the internal code whenever needed.

The only dependency the loader code has is that the BPF syscall got compiled into the kernel and libelf, that's all. libelf is effectively available in distros for more than a decade(s?). And iproute2 is being shipped everywhere already as well (hence also iproute2's minimalism on library dependencies).

Switching that code would mean that iproute2 would then depend on libbpf which itself would depend on libelf, until every distro that ships iproute2 will also ship libbpf, it will take a bit. Changes on the code would then need to go through libbpf first, and iproute2 would need to wait until it becomes available to make use of it and probably need to implement some compat code for the time being. Further, as both are decoupled also testing effort increases to make sure nothing breaks among different versions.

That said, I prefer that tc's {cls,act}_bpf front-end is shipped to users as it's being done already and that they can use it /now/ as-is. Don't get me wrong, for other tools etc coming up in future, as mentioned, offering it as a public library makes totally sense as not every application developer would want to choose writing his own loader code.

Cheers,
Daniel

#17 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

发自我的 iPhone

Quoted 30 lines · 在 2015年12月18日,下午6:57,Daniel Borkmann <[email protected]> 写道:
Quoted 6 lines · Wangnan (F)

...
What I want to do in this patchset is not only removing original libbpf.c and bpf_load.c. In fact I want libbpf in tools/lib/bpf becomes a public available library for other userspace tools (tc for example).

Having this as a possible user space library seems fine.

Only speaking for the tc case specifically here (since you mention it as one example), there's currently no additional value for it. The current loader code there is functional and partially tailored for tc's needs and having this together with the iproute2 repo, it allows us to easily change/ adapt the internal code whenever needed.

The only dependency the loader code has is that the BPF syscall got compiled into the kernel and libelf, that's all. libelf is effectively available in distros for more than a decade(s?). And iproute2 is being shipped everywhere already as well (hence also iproute2's minimalism on library dependencies).

Switching that code would mean that iproute2 would then depend on libbpf which itself would depend on libelf, until every distro that ships iproute2 will also ship libbpf, it will take a bit. Changes on the code would then need to go through libbpf first, and iproute2 would need to wait until it becomes available to make use of it and probably need to implement some compat code for the time being. Further, as both are decoupled also testing effort increases to make sure nothing breaks among different versions.

Quoted 6 lines

That said, I prefer that tc's {cls,act}_bpf front-end is shipped to users as it's being done already and that they can use it /now/ as-is. Don't get me wrong, for other tools etc coming up in future, as mentioned, offering it as a public library makes totally sense as not every application developer would want to choose writing his own loader code.

Get it, and I remember you told me about this once.

There's another goal. When someone introduces new functions to eBPF, like object pin added in these days, switching samples to libbpf will lead he/she to help me improve libbpf, because if a new sample is needed, the libbpf backend must be update correspondingly :)

Quoted 2 lines

Cheers,
Daniel

maintainer
#18 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

On 12/18/2015 12:18 PM, pi3orama wrote:
...

Quoted 6 lines

Get it, and I remember you told me about this once.

There's another goal. When someone introduces new functions to eBPF, like object pin added in these days, switching samples to libbpf will lead he/she to help me improve libbpf, because if a new sample is needed, the libbpf backend must be update correspondingly :)

Yep, that's good and makes sense.

Thanks,
Daniel

#19 lore raw

Re: [PATCH 08/10] bpf samples: Add utils.[ch] for using BPF

In reply to Wangnan (F)
Quoted 4 lines · Wangnan (F)
Quoted 4 lines
Quoted 4 lines

However, linux/err.h is not a part of uapi. To make libbpf work, one has to create its
own err.h.

Quoted 8 lines
Quoted 4 lines

Why tools/include/linux/err.h is not suitable for everyone?

Quoted 1 line

Now I'm thinking provide LIBBPF_{IS_ERR,PTR_ERR}(), in libbpf itself.

seems odd. we already have user space err.h in tools/include.

Currently samples/bpf doesn't have an -I$(srctree)/tools/include.

I tried to add it into CFLAGS of samples/bpf. It causes other problems,

let's fix those problem then.

Quoted 8 lines

What I want to do in this patchset is not only removing original libbpf.c and bpf_load.c. In fact I want libbpf in tools/lib/bpf becomes a public available library for other userspace tools (tc for example). Switching samples/bpf into libbpf is the first step of this goal. From doing this I found and fixed some limitation, like those missed BPF map operations. Making libbpf.h and bpf.h available for normal userspace programs is also important.

Quoted 6 lines

Having the above goal, I think you can understand why improving tools/include
is not a good idea. You don't want to force a normal userspace program setup a similar header environment for using libbpf. It is relatively a small library. So it would be good if bpf.h and libbpf.h only depend on what can be found in uapi.

completely agree on the goal of making libbpf to be a standalone library, but disagree on tools/include dependency.
If you copy-paste err.h into libbpf either as-is or as LIBBPF_IS_ERR, it's not going to be enough. Soon you'll need another macro from tools/include and so on. imo it's much easier to include tools/include/ as part of standalone libbpf.

Also at the time of creation of tools/lib/bpf we agreed that it's LGPL just like tools/lib/traceevent, but I don't see any mention of it in the libbpf source.

author
#20 lore raw

PATCH 9/10 bpf samples: Uses libbpf in tools/lib to deal with BPF operations

In reply to Wang Nan

Since we already have libbpf in tools/lib, there's no need to maintain a duplicate BPF loading and operations library in samples.

This patch utilises previous introduced utils.[ch], which calls libbpf to do these work.

The main changing in this relative large patch is very simple:

1. Makefile modification, makes all hostprogs depend on libbpf.a. Add include and library searching patch and rules for linker. Remove old bpf_load.o and libbpf.o, replaces them with utils.o

2. Make sure there are correct 'version' section so the BPF source files can be loaded by libbpf.

3. Uses API provided by utils.h and libbpf.

Cc Alexei Starovoitov <[email protected]>
Cc Alex Gartrell <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Brenden Blanco <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc David S. Miller <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kaixu Xia <[email protected]>
Cc Michael Holzheu <[email protected]>
Cc Yang Shi <[email protected]>
---
 samples/bpf/Makefile            | 65 ++++++++++++++++++---------------
 samples/bpf/fds_example.c       | 26 +++++++-------
 samples/bpf/lathist_user.c      | 13 ++++---
 samples/bpf/sock_example.c      | 13 +++----
 samples/bpf/sockex1_kern.c      |  2 ++
 samples/bpf/sockex1_user.c      | 27 ++++++++------
 samples/bpf/sockex2_kern.c      |  2 ++
 samples/bpf/sockex2_user.c      | 26 ++++++++------
 samples/bpf/sockex3_kern.c      |  2 ++
 samples/bpf/sockex3_user.c      | 23 +++++++-----
 samples/bpf/test_maps.c         | 80 ++++++++++++++++++++---------------------
 samples/bpf/test_verifier.c     | 13 ++++---
 samples/bpf/trace_output_user.c | 17 +++++----
 samples/bpf/tracex1_user.c      |  9 +++--
 samples/bpf/tracex2_user.c      | 31 +++++++++-------
 samples/bpf/tracex3_user.c      | 15 ++++----
 samples/bpf/tracex4_user.c      | 15 ++++----
 samples/bpf/tracex5_user.c      |  9 +++--
 samples/bpf/tracex6_user.c      | 16 ++++-----
 19 files changed, 221 insertions(+), 183 deletions(-)
samples/bpf/Makefile +37 -28
index edd638b..edb7c04 100644
@@ -1,6 +1,20 @@
 # kbuild trick to avoid linker error. Can be omitted if a module is built.
 obj- := dummy.o
 
+INC_DIR = $(src)/include
+TOOLS_DIR = $(realpath $(srctree))/tools
+TOOLS_LIB_DIR = $(TOOLS_DIR)/lib
+LIBBPF_DIR = $(TOOLS_LIB_DIR)/bpf
+
+$(obj)/libbpf/libbpf.a:
+	$(Q)mkdir -p $(obj)/libbpf
+	$(MAKE) -C $(LIBBPF_DIR) O=$(realpath $(obj))/libbpf CFLAGS= LDFLAGS= V=1 $(realpath $(obj))/libbpf/libbpf.a
+
+HOSTCFLAGS += -I$(TOOLS_LIB_DIR) -I$(INC_DIR)
+HOSTLDFLAGS += -L$(obj)/libbpf
+HOSTCFLAGS += -I$(INC_DIR) -I$(TOOLS_LIB_DIR)
+HOST_LOADLIBES += -lelf -lbpf
+
 # List of programs to build
 hostprogs-y := test_verifier test_maps
 hostprogs-y += sock_example
@@ -17,21 +31,27 @@ hostprogs-y += tracex6
 hostprogs-y += trace_output
 hostprogs-y += lathist
 
-test_verifier-objs := test_verifier.o libbpf.o
-test_maps-objs := test_maps.o libbpf.o
-sock_example-objs := sock_example.o libbpf.o
-fds_example-objs := bpf_load.o libbpf.o fds_example.o
-sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
-sockex2-objs := bpf_load.o libbpf.o sockex2_user.o
-sockex3-objs := bpf_load.o libbpf.o sockex3_user.o
-tracex1-objs := bpf_load.o libbpf.o tracex1_user.o
-tracex2-objs := bpf_load.o libbpf.o tracex2_user.o
-tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
-tracex4-objs := bpf_load.o libbpf.o tracex4_user.o
-tracex5-objs := bpf_load.o libbpf.o tracex5_user.o
-tracex6-objs := bpf_load.o libbpf.o tracex6_user.o
-trace_output-objs := bpf_load.o libbpf.o trace_output_user.o
-lathist-objs := bpf_load.o libbpf.o lathist_user.o
+test_verifier-objs := test_verifier.o
+test_maps-objs := test_maps.o
+sock_example-objs := sock_example.o utils.o
+fds_example-objs := utils.o fds_example.o
+sockex1-objs := utils.o sockex1_user.o
+sockex2-objs := utils.o sockex2_user.o
+sockex3-objs := utils.o sockex3_user.o
+tracex1-objs := utils.o tracex1_user.o
+tracex2-objs := utils.o tracex2_user.o
+tracex3-objs := utils.o tracex3_user.o
+tracex4-objs := utils.o tracex4_user.o
+tracex5-objs := utils.o tracex5_user.o
+tracex6-objs := utils.o tracex6_user.o
+trace_output-objs := utils.o trace_output_user.o
+lathist-objs := utils.o lathist_user.o
+
+define add_depend
+$(foreach m, $(notdir $1), \
+	$(eval $(obj)/$m: $(obj)/libbpf/libbpf.a))
+endef
+$(call add_depend, $(hostprogs-y))
 
 # Tell kbuild to always build the programs
 always := $(hostprogs-y)
@@ -50,19 +70,8 @@ always += lathist_kern.o
 
 HOSTCFLAGS += -I$(objtree)/usr/include
 
-HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable
-HOSTLOADLIBES_fds_example += -lelf
-HOSTLOADLIBES_sockex1 += -lelf
-HOSTLOADLIBES_sockex2 += -lelf
-HOSTLOADLIBES_sockex3 += -lelf
-HOSTLOADLIBES_tracex1 += -lelf
-HOSTLOADLIBES_tracex2 += -lelf
-HOSTLOADLIBES_tracex3 += -lelf
-HOSTLOADLIBES_tracex4 += -lelf -lrt
-HOSTLOADLIBES_tracex5 += -lelf
-HOSTLOADLIBES_tracex6 += -lelf
-HOSTLOADLIBES_trace_output += -lelf -lrt
-HOSTLOADLIBES_lathist += -lelf
+HOSTLOADLIBES_tracex4 += -lrt
+HOSTLOADLIBES_trace_output += -lrt
 
 # point this to your LLVM backend with bpf support
 LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc
samples/bpf/fds_example.c +14 -12
index e2fd16c..bdcfce0 100644
@@ -12,8 +12,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 
-#include "bpf_load.h"
-#include "libbpf.h"
+#include "utils.h"
 
 #define BPF_F_PIN	(1 << 0)
 #define BPF_F_GET	(1 << 1)
@@ -55,11 +54,14 @@ static int bpf_prog_create(const char *object)
 	};
 
 	if (object) {
-		assert(!load_bpf_file((char *)object));
-		return prog_fd[0];
+		struct bpf_object *obj = load_bpf_file((char *)object);
+
+		assert(obj);
+		return get_prog_fd(obj, 0);
 	} else {
-		return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER,
-				     insns, sizeof(insns), "GPL", 0);
+		return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
+					insns, sizeof(insns) / sizeof(insns[0]),
+					"GPL", 0, NULL, 0);
 	}
 }
 
@@ -73,22 +75,22 @@ static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
 		printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
 		assert(fd > 0);
 
-		ret = bpf_obj_pin(fd, file);
+		ret = bpf_pin_object(fd, file);
 		printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
 		assert(ret == 0);
 	} else {
-		fd = bpf_obj_get(file);
+		fd = bpf_get_pinned_object(file);
 		printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
 		assert(fd > 0);
 	}
 
 	if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
-		ret = bpf_update_elem(fd, &key, &value, 0);
+		ret = bpf_map_update_elem(fd, &key, &value, 0);
 		printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
 		       ret, strerror(errno));
 		assert(ret == 0);
 	} else if (flags & BPF_F_KEY) {
-		ret = bpf_lookup_elem(fd, &key, &value);
+		ret = bpf_map_lookup_elem(fd, &key, &value);
 		printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
 		       ret, strerror(errno));
 		assert(ret == 0);
@@ -106,11 +108,11 @@ static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
 		printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
 		assert(fd > 0);
 
-		ret = bpf_obj_pin(fd, file);
+		ret = bpf_pin_object(fd, file);
 		printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
 		assert(ret == 0);
 	} else {
-		fd = bpf_obj_get(file);
+		fd = bpf_get_pinned_object(file);
 		printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
 		assert(fd > 0);
 	}
samples/bpf/lathist_user.c +6 -7
index 65da8c1..9160af8 100644
@@ -10,8 +10,7 @@
 #include <stdlib.h>
 #include <signal.h>
 #include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 #define MAX_ENTRIES	20
 #define MAX_CPU		4
@@ -73,7 +72,7 @@ static void get_data(int fd)
 	for (c = 0; c < MAX_CPU; c++) {
 		for (i = 0; i < MAX_ENTRIES; i++) {
 			key = c * MAX_ENTRIES + i;
-			bpf_lookup_elem(fd, &key, &value);
+			bpf_map_lookup_elem(fd, &key, &value);
 
 			cpu_hist[c].data[i] = value;
 			if (value > cpu_hist[c].max)
@@ -85,16 +84,16 @@ static void get_data(int fd)
 int main(int argc, char **argv)
 {
 	char filename[256];
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
 	while (1) {
-		get_data(map_fd[1]);
+		get_data(get_map_fd(obj, 1));
 		print_hist();
 		sleep(5);
 	}
samples/bpf/sock_example.c +7 -6
index a0ce251..a6a875f 100644
@@ -26,7 +26,7 @@
 #include <linux/if_ether.h>
 #include <linux/ip.h>
 #include <stddef.h>
-#include "libbpf.h"
+#include "utils.h"
 
 static int test_sock(void)
 {
@@ -55,8 +55,9 @@ static int test_sock(void)
 		BPF_EXIT_INSN(),
 	};
 
-	prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog),
-				"GPL", 0);
+	prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
+				   sizeof(prog) / sizeof(prog[0]),
+				   "GPL", 0, NULL, 0);
 	if (prog_fd < 0) {
 		printf("failed to load prog '%s'\n", strerror(errno));
 		goto cleanup;
@@ -72,13 +73,13 @@ static int test_sock(void)
 
 	for (i = 0; i < 10; i++) {
 		key = IPPROTO_TCP;
-		assert(bpf_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
 
 		key = IPPROTO_UDP;
-		assert(bpf_lookup_elem(map_fd, &key, &udp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
 
 		key = IPPROTO_ICMP;
-		assert(bpf_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
 
 		printf("TCP %lld UDP %lld ICMP %lld packets\n",
 		       tcp_cnt, udp_cnt, icmp_cnt);
samples/bpf/sockex1_kern.c +2 -0
index ed18e9a..0ab3d9a 100644
@@ -2,6 +2,7 @@
 #include <uapi/linux/if_ether.h>
 #include <uapi/linux/if_packet.h>
 #include <uapi/linux/ip.h>
+#include <linux/version.h>
 #include "bpf_helpers.h"
 
 struct bpf_map_def SEC("maps") my_map = {
@@ -27,3 +28,4 @@ int bpf_prog1(struct __sk_buff *skb)
 	return 0;
 }
 char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
samples/bpf/sockex1_user.c +16 -11
index 678ce46..d1ece36 100644
@@ -1,28 +1,33 @@
 #include <stdio.h>
 #include <assert.h>
-#include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
 #include <unistd.h>
 #include <arpa/inet.h>
+#include "utils.h"
 
 int main(int ac, char **argv)
 {
 	char filename[256];
 	FILE *f;
-	int i, sock;
+	int i, sock, map_fd, prog_fd;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
+		return 1;
+
+	map_fd = get_map_fd(obj, 0);
+	prog_fd = get_prog_fd(obj, 0);
+	if (map_fd < 0 || prog_fd < 0) {
+		fprintf(stderr, "failed to get file descriptor\n");
 		return 1;
 	}
 
 	sock = open_raw_sock("lo");
 
-	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
-			  sizeof(prog_fd[0])) == 0);
+	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
+			  sizeof(prog_fd)) == 0);
 
 	f = popen("ping -c5 localhost", "r");
 	(void) f;
@@ -32,13 +37,13 @@ int main(int ac, char **argv)
 		int key;
 
 		key = IPPROTO_TCP;
-		assert(bpf_lookup_elem(map_fd[0], &key, &tcp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
 
 		key = IPPROTO_UDP;
-		assert(bpf_lookup_elem(map_fd[0], &key, &udp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
 
 		key = IPPROTO_ICMP;
-		assert(bpf_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0);
+		assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
 
 		printf("TCP %lld UDP %lld ICMP %lld bytes\n",
 		       tcp_cnt, udp_cnt, icmp_cnt);
samples/bpf/sockex2_kern.c +2 -0
index ba0e177..46e2e77 100644
@@ -6,6 +6,7 @@
 #include <uapi/linux/ip.h>
 #include <uapi/linux/ipv6.h>
 #include <uapi/linux/if_tunnel.h>
+#include <linux/version.h>
 #define IP_MF		0x2000
 #define IP_OFFSET	0x1FFF
 
@@ -219,3 +220,4 @@ int bpf_prog2(struct __sk_buff *skb)
 }
 
 char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
samples/bpf/sockex2_user.c +16 -10
index 29a276d..3df6442 100644
@@ -1,10 +1,9 @@
 #include <stdio.h>
 #include <assert.h>
-#include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
 #include <unistd.h>
 #include <arpa/inet.h>
+#include <bpf/bpf.h>
+#include "utils.h"
 
 struct pair {
 	__u64 packets;
@@ -15,19 +14,26 @@ int main(int ac, char **argv)
 {
 	char filename[256];
 	FILE *f;
-	int i, sock;
+	int i, sock, map_fd, prog_fd;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
+		return 1;
+
+	map_fd = get_map_fd(obj, 0);
+	prog_fd = get_prog_fd(obj, 0);
+	if (map_fd < 0 || prog_fd < 0) {
+		fprintf(stderr, "failed to get file descriptor\n");
 		return 1;
 	}
 
 	sock = open_raw_sock("lo");
 
-	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
-			  sizeof(prog_fd[0])) == 0);
+	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
+			  sizeof(prog_fd)) == 0);
 
 	f = popen("ping -c5 localhost", "r");
 	(void) f;
@@ -36,8 +42,8 @@ int main(int ac, char **argv)
 		int key = 0, next_key;
 		struct pair value;
 
-		while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
-			bpf_lookup_elem(map_fd[0], &next_key, &value);
+		while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
+			bpf_map_lookup_elem(map_fd, &next_key, &value);
 			printf("ip %s bytes %lld packets %lld\n",
 			       inet_ntoa((struct in_addr){htonl(next_key)}),
 			       value.bytes, value.packets);
samples/bpf/sockex3_kern.c +2 -0
index 41ae2fd..6ac0d17 100644
@@ -13,6 +13,7 @@
 #include <uapi/linux/ipv6.h>
 #include <uapi/linux/if_tunnel.h>
 #include <uapi/linux/mpls.h>
+#include <linux/version.h>
 #define IP_MF		0x2000
 #define IP_OFFSET	0x1FFF
 
@@ -288,3 +289,4 @@ int main_prog(struct __sk_buff *skb)
 }
 
 char _license[] SEC("license") = "GPL";
+int _version SEC("version") = LINUX_VERSION_CODE;
samples/bpf/sockex3_user.c +15 -8
index 2617772..99a7126 100644
@@ -1,10 +1,10 @@
 #include <stdio.h>
 #include <assert.h>
 #include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
 #include <unistd.h>
 #include <arpa/inet.h>
+#include <bpf/bpf.h>
+#include "utils.h"
 
 struct flow_keys {
 	__be32 src;
@@ -25,18 +25,25 @@ int main(int argc, char **argv)
 {
 	char filename[256];
 	FILE *f;
-	int i, sock;
+	int i, sock, map_fd, prog_fd;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
+		return 1;
+
+	map_fd = get_map_fd(obj, 2);
+	prog_fd = get_prog_fd(obj, 4);
+	if (map_fd < 0 || prog_fd < 0) {
+		fprintf(stderr, "failed to get file descriptor\n");
 		return 1;
 	}
 
 	sock = open_raw_sock("lo");
 
-	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4],
+	assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
 			  sizeof(__u32)) == 0);
 
 	if (argc > 1)
@@ -51,8 +58,8 @@ int main(int argc, char **argv)
 
 		sleep(1);
 		printf("IP     src.port -> dst.port               bytes      packets\n");
-		while (bpf_get_next_key(map_fd[2], &key, &next_key) == 0) {
-			bpf_lookup_elem(map_fd[2], &next_key, &value);
+		while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
+			bpf_map_lookup_elem(map_fd, &next_key, &value);
 			printf("%s.%05d -> %s.%05d %12lld %12lld\n",
 			       inet_ntoa((struct in_addr){htonl(next_key.src)}),
 			       next_key.port16[0],
samples/bpf/test_maps.c +40 -40

203 lines not on this page · raw message

samples/bpf/test_verifier.c +9 -4
index 563c507..0566fc0 100644
@@ -17,13 +17,17 @@
 #include <stddef.h>
 #include <stdbool.h>
 #include <sys/resource.h>
-#include "libbpf.h"
+#include "utils.h"
+#include <bpf/bpf.h>
 
 #define MAX_INSNS 512
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
 
 #define MAX_FIXUPS 8
 
+#define LOG_BUF_SIZE 65536
+static char bpf_log_buf[LOG_BUF_SIZE];
+
 struct bpf_test {
 	const char *descr;
 	struct bpf_insn	insns[MAX_INSNS];
@@ -1250,9 +1254,10 @@ static int test(void)
 		}
 		printf("#%d %s ", i, tests[i].descr);
 
-		prog_fd = bpf_prog_load(prog_type ?: BPF_PROG_TYPE_SOCKET_FILTER,
-					prog, prog_len * sizeof(struct bpf_insn),
-					"GPL", 0);
+		bpf_log_buf[0] = '\0';
+		prog_fd = bpf_load_program(prog_type ?: BPF_PROG_TYPE_SOCKET_FILTER,
+					   prog, prog_len, "GPL", 0,
+					   bpf_log_buf, sizeof(bpf_log_buf));
 
 		if (unpriv && tests[i].result_unpriv != UNDEF)
 			expected_result = tests[i].result_unpriv;
samples/bpf/trace_output_user.c +8 -9
index 661a7d0..4ef02cb 100644
@@ -19,8 +19,7 @@
 #include <sys/mman.h>
 #include <time.h>
 #include <signal.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 static int pmu_fd;
 
@@ -61,7 +60,7 @@ struct perf_event_sample {
 	char data[];
 };
 
-void perf_event_read(print_fn fn)
+static void perf_event_read(print_fn fn)
 {
 	__u64 data_tail = header->data_tail;
 	__u64 data_head = header->data_head;
@@ -150,7 +149,7 @@ static void print_bpf_output(void *data, int size)
 	}
 }
 
-static void test_bpf_perf_event(void)
+static void test_bpf_perf_event(int map_fd)
 {
 	struct perf_event_attr attr = {
 		.sample_type = PERF_SAMPLE_RAW,
@@ -162,7 +161,7 @@ static void test_bpf_perf_event(void)
 	pmu_fd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
 
 	assert(pmu_fd >= 0);
-	assert(bpf_update_elem(map_fd[0], &key, &pmu_fd, BPF_ANY) == 0);
+	assert(bpf_map_update_elem(map_fd, &key, &pmu_fd, BPF_ANY) == 0);
 	ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
 }
 
@@ -170,15 +169,15 @@ int main(int argc, char **argv)
 {
 	char filename[256];
 	FILE *f;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
-	test_bpf_perf_event();
+	test_bpf_perf_event(get_map_fd(obj, 0));
 
 	if (perf_event_mmap(pmu_fd) < 0)
 		return 1;
samples/bpf/tracex1_user.c +4 -5
index 31a4818..0652339 100644
@@ -1,20 +1,19 @@
 #include <stdio.h>
 #include <linux/bpf.h>
 #include <unistd.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 int main(int ac, char **argv)
 {
 	FILE *f;
 	char filename[256];
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
 	f = popen("taskset 1 ping -c5 localhost", "r");
 	(void) f;
samples/bpf/tracex2_user.c +18 -13
index cd0241c..86700bb 100644
@@ -4,8 +4,9 @@
 #include <signal.h>
 #include <linux/bpf.h>
 #include <string.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include "utils.h"
 
 #define MAX_INDEX	64
 #define MAX_STARS	38
@@ -44,12 +45,12 @@ static void print_hist_for_pid(int fd, void *task)
 	long max_value = 0;
 	int i, ind;
 
-	while (bpf_get_next_key(fd, &key, &next_key) == 0) {
+	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
 		if (memcmp(&next_key, task, SIZE)) {
 			key = next_key;
 			continue;
 		}
-		bpf_lookup_elem(fd, &next_key, &value);
+		bpf_map_lookup_elem(fd, &next_key, &value);
 		ind = next_key.index;
 		data[ind] = value;
 		if (value && ind > max_ind)
@@ -76,7 +77,7 @@ static void print_hist(int fd)
 	int task_cnt = 0;
 	int i;
 
-	while (bpf_get_next_key(fd, &key, &next_key) == 0) {
+	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
 		int found = 0;
 
 		for (i = 0; i < task_cnt; i++)
@@ -97,9 +98,11 @@ static void print_hist(int fd)
 
 }
 
+static int hist_map_fd;
+
 static void int_exit(int sig)
 {
-	print_hist(map_fd[1]);
+	print_hist(hist_map_fd);
 	exit(0);
 }
 
@@ -108,7 +111,8 @@ int main(int ac, char **argv)
 	char filename[256];
 	long key, next_key, value;
 	FILE *f;
-	int i;
+	int i, map_fd;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
@@ -122,15 +126,16 @@ int main(int ac, char **argv)
 	f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
 	(void) f;
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
+	map_fd = get_map_fd(obj, 0);
+	hist_map_fd = get_map_fd(obj, 1);
 
 	for (i = 0; i < 5; i++) {
 		key = 0;
-		while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
-			bpf_lookup_elem(map_fd[0], &next_key, &value);
+		while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
+			bpf_map_lookup_elem(map_fd, &next_key, &value);
 			printf("location 0x%lx count %ld\n", next_key, value);
 			key = next_key;
 		}
@@ -138,7 +143,7 @@ int main(int ac, char **argv)
 			printf("\n");
 		sleep(1);
 	}
-	print_hist(map_fd[1]);
+	print_hist(hist_map_fd);
 
 	return 0;
 }
samples/bpf/tracex3_user.c +7 -8
index 0aaa933..4c26a6f 100644
@@ -11,8 +11,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
 
@@ -24,7 +23,7 @@ static void clear_stats(int fd)
 	__u64 value = 0;
 
 	for (key = 0; key < SLOTS; key++)
-		bpf_update_elem(fd, &key, &value, BPF_ANY);
+		bpf_map_update_elem(fd, &key, &value, BPF_ANY);
 }
 
 const char *color[] = {
@@ -83,7 +82,7 @@ static void print_hist(int fd)
 
 	for (key = 0; key < SLOTS; key++) {
 		value = 0;
-		bpf_lookup_elem(fd, &key, &value);
+		bpf_map_lookup_elem(fd, &key, &value);
 		cnt[key] = value;
 		total_events += value;
 		if (value > max_cnt)
@@ -105,13 +104,13 @@ int main(int ac, char **argv)
 {
 	char filename[256];
 	int i;
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
 	for (i = 1; i < ac; i++) {
 		if (strcmp(argv[i], "-a") == 0) {
@@ -142,7 +141,7 @@ int main(int ac, char **argv)
 	for (i = 0; ; i++) {
 		if (i % 20 == 0)
 			print_banner();
-		print_hist(map_fd[1]);
+		print_hist(get_map_fd(obj, 1));
 		sleep(2);
 	}
 
samples/bpf/tracex4_user.c +7 -8
index bc4a3bd..273bd33 100644
@@ -12,8 +12,7 @@
 #include <string.h>
 #include <time.h>
 #include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 struct pair {
 	long long val;
@@ -37,8 +36,8 @@ static void print_old_objects(int fd)
 	key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
 
 	key = -1;
-	while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
-		bpf_lookup_elem(map_fd[0], &next_key, &v);
+	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
+		bpf_map_lookup_elem(fd, &next_key, &v);
 		key = next_key;
 		if (val - v.val < 1000000000ll)
 			/* object was allocated more then 1 sec ago */
@@ -51,17 +50,17 @@ static void print_old_objects(int fd)
 int main(int ac, char **argv)
 {
 	char filename[256];
+	struct bpf_object *obj;
 	int i;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
 	for (i = 0; ; i++) {
-		print_old_objects(map_fd[1]);
+		print_old_objects(get_map_fd(obj, 0));
 		sleep(1);
 	}
 
samples/bpf/tracex5_user.c +4 -5
index a04dd3c..bcbd7fc 100644
@@ -4,8 +4,7 @@
 #include <linux/filter.h>
 #include <linux/seccomp.h>
 #include <sys/prctl.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 /* install fake seccomp program to enable seccomp code path inside the kernel,
  * so that our kprobe attached to seccomp_phase1() can be triggered
@@ -27,13 +26,13 @@ int main(int ac, char **argv)
 {
 	FILE *f;
 	char filename[256];
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
 	install_accept_all_seccomp();
 
samples/bpf/tracex6_user.c +7 -9
index 8ea4976..a400e31 100644
@@ -8,12 +8,11 @@
 #include <sys/ioctl.h>
 #include <linux/perf_event.h>
 #include <linux/bpf.h>
-#include "libbpf.h"
-#include "bpf_load.h"
+#include "utils.h"
 
 #define SAMPLE_PERIOD  0x7fffffffffffffffULL
 
-static void test_bpf_perf_event(void)
+static void test_bpf_perf_event(int map_fd)
 {
 	int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 	int *pmu_fd = malloc(nr_cpus * sizeof(int));
@@ -36,7 +35,7 @@ static void test_bpf_perf_event(void)
 			goto exit;
 		}
 
-		bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY);
+		bpf_map_update_elem(map_fd, &i, &pmu_fd[i], BPF_ANY);
 		ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
 	}
 
@@ -50,22 +49,21 @@ static void test_bpf_perf_event(void)
 exit:
 	for (i = 0; i < nr_cpus; i++)
 		close(pmu_fd[i]);
-	close(map_fd[0]);
 	free(pmu_fd);
 }
 
 int main(int argc, char **argv)
 {
 	char filename[256];
+	struct bpf_object *obj;
 
 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
 
-	if (load_bpf_file(filename)) {
-		printf("%s", bpf_log_buf);
+	obj = load_bpf_file(filename);
+	if (!obj)
 		return 1;
-	}
 
-	test_bpf_perf_event();
+	test_bpf_perf_event(get_map_fd(obj, 0));
 	read_trace_pipe();
 
 	return 0;
Signature · 1 line
1.8.3.4
author
#21 lore raw

PATCH 10/10 bpf samples: Remove old BPF helpers

In reply to Wang Nan

Since we have switched to libbpf in tools/lib, old bpf_load.c and libbpf.c can be removed safely.

Cc Alexei Starovoitov <[email protected]>
Cc Alex Gartrell <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Brenden Blanco <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc Daniel Wagner <[email protected]>
Cc David S. Miller <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kaixu Xia <[email protected]>
Cc Michael Holzheu <[email protected]>
Cc Yang Shi <[email protected]>
---
 samples/bpf/bpf_load.c | 345 -------------------------------------------------
 samples/bpf/bpf_load.h |  27 ----
 samples/bpf/libbpf.c   | 154 ----------------------
 samples/bpf/libbpf.h   | 201 ----------------------------
 4 files changed, 727 deletions(-)
 delete mode 100644 samples/bpf/bpf_load.c
 delete mode 100644 samples/bpf/bpf_load.h
 delete mode 100644 samples/bpf/libbpf.c
 delete mode 100644 samples/bpf/libbpf.h
samples/bpf/bpf_load.c deleted +0 -345
deleted file mode 100644
index da86a8e..0000000
@@ -1,345 +0,0 @@
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <libelf.h>
-#include <gelf.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <linux/bpf.h>
-#include <linux/filter.h>
-#include <linux/perf_event.h>
-#include <sys/syscall.h>
-#include <sys/ioctl.h>
-#include <sys/mman.h>
-#include <poll.h>
-#include <ctype.h>
-#include "libbpf.h"
-#include "bpf_helpers.h"
-#include "bpf_load.h"
-
-#define DEBUGFS "/sys/kernel/debug/tracing/"
-
-static char license[128];
-static int kern_version;
-static bool processed_sec[128];
-int map_fd[MAX_MAPS];
-int prog_fd[MAX_PROGS];
-int event_fd[MAX_PROGS];
-int prog_cnt;
-int prog_array_fd = -1;
-
-static int populate_prog_array(const char *event, int prog_fd)
-{
-	int ind = atoi(event), err;
-
-	err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
-	if (err < 0) {
-		printf("failed to store prog_fd in prog_array\n");
-		return -1;
-	}
-	return 0;
-}
-
-static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
-{
-	bool is_socket = strncmp(event, "socket", 6) == 0;
-	bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
-	bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
-	enum bpf_prog_type prog_type;
-	char buf[256];
-	int fd, efd, err, id;
-	struct perf_event_attr attr = {};
-
-	attr.type = PERF_TYPE_TRACEPOINT;
-	attr.sample_type = PERF_SAMPLE_RAW;
-	attr.sample_period = 1;
-	attr.wakeup_events = 1;
-
-	if (is_socket) {
-		prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
-	} else if (is_kprobe || is_kretprobe) {
-		prog_type = BPF_PROG_TYPE_KPROBE;
-	} else {
-		printf("Unknown event '%s'\n", event);
-		return -1;
-	}
-
-	fd = bpf_prog_load(prog_type, prog, size, license, kern_version);
-	if (fd < 0) {
-		printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf);
-		return -1;
-	}
-
-	prog_fd[prog_cnt++] = fd;
-
-	if (is_socket) {
-		event += 6;
-		if (*event != '/')
-			return 0;
-		event++;
-		if (!isdigit(*event)) {
-			printf("invalid prog number\n");
-			return -1;
-		}
-		return populate_prog_array(event, fd);
-	}
-
-	if (is_kprobe || is_kretprobe) {
-		if (is_kprobe)
-			event += 7;
-		else
-			event += 10;
-
-		if (*event == 0) {
-			printf("event name cannot be empty\n");
-			return -1;
-		}
-
-		if (isdigit(*event))
-			return populate_prog_array(event, fd);
-
-		snprintf(buf, sizeof(buf),
-			 "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events",
-			 is_kprobe ? 'p' : 'r', event, event);
-		err = system(buf);
-		if (err < 0) {
-			printf("failed to create kprobe '%s' error '%s'\n",
-			       event, strerror(errno));
-			return -1;
-		}
-	}
-
-	strcpy(buf, DEBUGFS);
-	strcat(buf, "events/kprobes/");
-	strcat(buf, event);
-	strcat(buf, "/id");
-
-	efd = open(buf, O_RDONLY, 0);
-	if (efd < 0) {
-		printf("failed to open event %s\n", event);
-		return -1;
-	}
-
-	err = read(efd, buf, sizeof(buf));
-	if (err < 0 || err >= sizeof(buf)) {
-		printf("read from '%s' failed '%s'\n", event, strerror(errno));
-		return -1;
-	}
-
-	close(efd);
-
-	buf[err] = 0;
-	id = atoi(buf);
-	attr.config = id;
-
-	efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
-	if (efd < 0) {
-		printf("event %d fd %d err %s\n", id, efd, strerror(errno));
-		return -1;
-	}
-	event_fd[prog_cnt - 1] = efd;
-	ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
-	ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
-
-	return 0;
-}
-
-static int load_maps(struct bpf_map_def *maps, int len)
-{
-	int i;
-
-	for (i = 0; i < len / sizeof(struct bpf_map_def); i++) {
-
-		map_fd[i] = bpf_create_map(maps[i].type,
-					   maps[i].key_size,
-					   maps[i].value_size,
-					   maps[i].max_entries);
-		if (map_fd[i] < 0)
-			return 1;
-
-		if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY)
-			prog_array_fd = map_fd[i];
-	}
-	return 0;
-}
-
-static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
-		   GElf_Shdr *shdr, Elf_Data **data)
-{
-	Elf_Scn *scn;
-
-	scn = elf_getscn(elf, i);
-	if (!scn)
-		return 1;
-
-	if (gelf_getshdr(scn, shdr) != shdr)
-		return 2;
-
-	*shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
-	if (!*shname || !shdr->sh_size)
-		return 3;
-
-	*data = elf_getdata(scn, 0);
-	if (!*data || elf_getdata(scn, *data) != NULL)
-		return 4;
-
-	return 0;
-}
-
-static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
-				GElf_Shdr *shdr, struct bpf_insn *insn)
-{
-	int i, nrels;
-
-	nrels = shdr->sh_size / shdr->sh_entsize;
-
-	for (i = 0; i < nrels; i++) {
-		GElf_Sym sym;
-		GElf_Rel rel;
-		unsigned int insn_idx;
-
-		gelf_getrel(data, i, &rel);
-
-		insn_idx = rel.r_offset / sizeof(struct bpf_insn);
-
-		gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
-
-		if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
-			printf("invalid relo for insn[%d].code 0x%x\n",
-			       insn_idx, insn[insn_idx].code);
-			return 1;
-		}
-		insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
-		insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)];
-	}
-
-	return 0;
-}
-
-int load_bpf_file(char *path)
-{
-	int fd, i;
-	Elf *elf;
-	GElf_Ehdr ehdr;
-	GElf_Shdr shdr, shdr_prog;
-	Elf_Data *data, *data_prog, *symbols = NULL;
-	char *shname, *shname_prog;
-
-	if (elf_version(EV_CURRENT) == EV_NONE)
-		return 1;
-
-	fd = open(path, O_RDONLY, 0);
-	if (fd < 0)
-		return 1;
-
-	elf = elf_begin(fd, ELF_C_READ, NULL);
-
-	if (!elf)
-		return 1;
-
-	if (gelf_getehdr(elf, &ehdr) != &ehdr)
-		return 1;
-
-	/* clear all kprobes */
-	i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events");
-
-	/* scan over all elf sections to get license and map info */
-	for (i = 1; i < ehdr.e_shnum; i++) {
-
-		if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
-			continue;
-
-		if (0) /* helpful for llvm debugging */
-			printf("section %d:%s data %p size %zd link %d flags %d\n",
-			       i, shname, data->d_buf, data->d_size,
-			       shdr.sh_link, (int) shdr.sh_flags);
-
-		if (strcmp(shname, "license") == 0) {
-			processed_sec[i] = true;
-			memcpy(license, data->d_buf, data->d_size);
-		} else if (strcmp(shname, "version") == 0) {
-			processed_sec[i] = true;
-			if (data->d_size != sizeof(int)) {
-				printf("invalid size of version section %zd\n",
-				       data->d_size);
-				return 1;
-			}
-			memcpy(&kern_version, data->d_buf, sizeof(int));
-		} else if (strcmp(shname, "maps") == 0) {
-			processed_sec[i] = true;
-			if (load_maps(data->d_buf, data->d_size))
-				return 1;
-		} else if (shdr.sh_type == SHT_SYMTAB) {
-			symbols = data;
-		}
-	}
-
-	/* load programs that need map fixup (relocations) */
-	for (i = 1; i < ehdr.e_shnum; i++) {
-
-		if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
-			continue;
-		if (shdr.sh_type == SHT_REL) {
-			struct bpf_insn *insns;
-
-			if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
-				    &shdr_prog, &data_prog))
-				continue;
-
-			insns = (struct bpf_insn *) data_prog->d_buf;
-
-			processed_sec[shdr.sh_info] = true;
-			processed_sec[i] = true;
-
-			if (parse_relo_and_apply(data, symbols, &shdr, insns))
-				continue;
-
-			if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
-			    memcmp(shname_prog, "kretprobe/", 10) == 0 ||
-			    memcmp(shname_prog, "socket", 6) == 0)
-				load_and_attach(shname_prog, insns, data_prog->d_size);
-		}
-	}
-
-	/* load programs that don't use maps */
-	for (i = 1; i < ehdr.e_shnum; i++) {
-
-		if (processed_sec[i])
-			continue;
-
-		if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
-			continue;
-
-		if (memcmp(shname, "kprobe/", 7) == 0 ||
-		    memcmp(shname, "kretprobe/", 10) == 0 ||
-		    memcmp(shname, "socket", 6) == 0)
-			load_and_attach(shname, data->d_buf, data->d_size);
-	}
-
-	close(fd);
-	return 0;
-}
-
-void read_trace_pipe(void)
-{
-	int trace_fd;
-
-	trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0);
-	if (trace_fd < 0)
-		return;
-
-	while (1) {
-		static char buf[4096];
-		ssize_t sz;
-
-		sz = read(trace_fd, buf, sizeof(buf));
-		if (sz > 0) {
-			buf[sz] = 0;
-			puts(buf);
-		}
-	}
-}
samples/bpf/bpf_load.h deleted +0 -27
deleted file mode 100644
index cbd7c2b..0000000
@@ -1,27 +0,0 @@
-#ifndef __BPF_LOAD_H
-#define __BPF_LOAD_H
-
-#define MAX_MAPS 32
-#define MAX_PROGS 32
-
-extern int map_fd[MAX_MAPS];
-extern int prog_fd[MAX_PROGS];
-extern int event_fd[MAX_PROGS];
-
-/* parses elf file compiled by llvm .c->.o
- * . parses 'maps' section and creates maps via BPF syscall
- * . parses 'license' section and passes it to syscall
- * . parses elf relocations for BPF maps and adjusts BPF_LD_IMM64 insns by
- *   storing map_fd into insn->imm and marking such insns as BPF_PSEUDO_MAP_FD
- * . loads eBPF programs via BPF syscall
- *
- * One ELF file can contain multiple BPF programs which will be loaded
- * and their FDs stored stored in prog_fd array
- *
- * returns zero on success
- */
-int load_bpf_file(char *path);
-
-void read_trace_pipe(void);
-
-#endif
samples/bpf/libbpf.c deleted +0 -154

154 lines not on this page · raw message

samples/bpf/libbpf.h deleted +0 -201

201 lines not on this page · raw message

Signature · 1 line
1.8.3.4
#22 lore raw
In reply to Wang Nan

Hi,

Quoted 8 lines · Wang Nan

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Quoted 7 lines

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Patch 10/10 removes unneeded files.

Which tree did you use for your patches? I tried to apply them against mainline and net-next which didn't really work out.

cheers,
daniel

#23 lore raw
Quoted 14 lines · Daniel Wagner

Hi,

Quoted 11 lines · Wang Nan

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Quoted 6 lines
Quoted 4 lines

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Patch 10/10 removes unneeded files.

Which tree did you use for your patches? I tried to apply them against mainline and net-next which didn't really work out.

These patches based on Arnaldo's 'perf/core' because of those libbpf changes.

Which tree is the right one for this? net-next?

Thank you.

Quoted 2 lines

cheers,
daniel

#24 lore raw
Quoted 16 lines · Wangnan (F)
Quoted 15 lines · Daniel Wagner
Quoted 14 lines · Wang Nan

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Patch 10/10 removes unneeded files.

Which tree did you use for your patches? I tried to apply them against mainline and net-next which didn't really work out.

These patches based on Arnaldo's 'perf/core' because of those libbpf changes.

Okay, I'll try with this one.

Quoted 1 line

Which tree is the right one for this? net-next?

The patches I was involved were routed via net-next but I don't know if that is the right tree for this patch set.

cheers,
daniel

#25 lore raw
Quoted 17 lines · Daniel Wagner
Quoted 16 lines · Wangnan (F)
Quoted 15 lines · Daniel Wagner
Quoted 14 lines · Wang Nan

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Patch 10/10 removes unneeded files.

Which tree did you use for your patches? I tried to apply them against mainline and net-next which didn't really work out.

These patches based on Arnaldo's 'perf/core' because of those libbpf changes.

Okay, I'll try with this one.

I applied those patches on top of

5c560cfcf1c0 ("tools subcmd: Rename subcmd header include guards")

Patch number 2 didn't apply cleanly. After fixing this manually I was able to continue to the build step:

$ make samples/bpf/
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
No libelf found
Makefile:203: recipe for target 'elfdep' failed
make[2]: *** [elfdep] Error 255
samples/bpf/Makefile:10: recipe for target 'samples/bpf/libbpf/libbpf.a' failed
make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2

Executing the above command line by myself in order to figure out what's going on ended in this mess:

$ make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
make: Entering directory '/home/wagi/src/linux/tools/lib/bpf'

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

[...]

samples/bpf/fds_example.c: In function ‘bpf_do_map’:
samples/bpf/fds_example.c:78:9: warning: implicit declaration of function ‘bpf_pin_object’ [-Wimplicit-function-declaration]

   ret = bpf_pin_object(fd, file);
         ^
samples/bpf/fds_example.c:82:8: warning: implicit declaration of function ‘bpf_get_pinned_object’ [-Wimplicit-function-declaration]
   fd = bpf_get_pinned_object(file);
        ^

gcc -Lsamples/bpf/libbpf -o samples/bpf/fds_example samples/bpf/utils.o samples/bpf/fds_example.o -lelf -lbpf
samples/bpf/fds_example.o: In function `main':
fds_example.c:(.text.startup+0x20e): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2b0): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2f3): undefined reference to bpf_get_pinned_object' fds_example.c:(.text.startup+0x344): undefined reference to bpf_get_pinned_object'
collect2: error: ld returned 1 exit status
scripts/Makefile.host:100: recipe for target 'samples/bpf/fds_example' failed
make[1]: *** [samples/bpf/fds_example] Error 1
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2
[wagi@handman linux (bpf-test)]$ make samples/bpf/

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  HOSTLD  samples/bpf/fds_example

samples/bpf/fds_example.o: In function `main':
fds_example.c:(.text.startup+0x20e): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2b0): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2f3): undefined reference to bpf_get_pinned_object' fds_example.c:(.text.startup+0x344): undefined reference to bpf_get_pinned_object'
collect2: error: ld returned 1 exit status

What is the canonical why to build the samples? I must doing something really stupid I guess.

cheers,
daniel

#26 lore raw
Quoted 21 lines · Daniel Wagner
Quoted 20 lines · Daniel Wagner
Quoted 19 lines · Wangnan (F)
Quoted 18 lines · Daniel Wagner
Quoted 8 lines · Wang Nan

Since we already have libbpf in tools/lib, we don't need to maintain another bpf loader and operations library in samples/bpf.

In patchset:

Patch 1/10 - 7/10 improves libbpf, add missing features to support samples,

Quoted 7 lines

Patch 8/10 adds utils.[ch], which creates similar API like old bpf_load.c and libbpf.c.

Patch 9/10 replace all sampels to use API provides by utils.[ch] and libbpf.

Patch 10/10 removes unneeded files.

Which tree did you use for your patches? I tried to apply them against mainline and net-next which didn't really work out.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

These patches based on Arnaldo's 'perf/core' because of those libbpf changes.

Okay, I'll try with this one.

I applied those patches on top of

5c560cfcf1c0 ("tools subcmd: Rename subcmd header include guards")

Patch number 2 didn't apply cleanly.

Because I have another patch in my local tree which also modifis bpf Makefile:

http://lkml.kernel.org/g/[email protected]

sorry.

Quoted 19 lines

After fixing this manually
I was able to continue to the build step:

$ make samples/bpf/
   CHK     include/config/kernel.release
   CHK     include/generated/uapi/linux/version.h
   CHK     include/generated/utsrelease.h
   CHK     include/generated/bounds.h
   CHK     include/generated/timeconst.h
   CHK     include/generated/asm-offsets.h
   CALL    scripts/checksyscalls.sh
make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
No libelf found
Makefile:203: recipe for target 'elfdep' failed
make[2]: *** [elfdep] Error 255
samples/bpf/Makefile:10: recipe for target 'samples/bpf/libbpf/libbpf.a' failed
make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2

When you see this, could you please have a look at:

/home/wagi/src/linux/samples/bpf/libbpf/feature/test-*.make.output

?

This error means it can't find libelf on your platform, but it is not true obviously.

Quoted 41 lines

Executing the above command line by myself in order to figure out what's going on ended in this mess:

$ make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
make: Entering directory '/home/wagi/src/linux/tools/lib/bpf'

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

[...]

samples/bpf/fds_example.c: In function ‘bpf_do_map’:
samples/bpf/fds_example.c:78:9: warning: implicit declaration of function ‘bpf_pin_object’ [-Wimplicit-function-declaration]

    ret = bpf_pin_object(fd, file);
          ^
samples/bpf/fds_example.c:82:8: warning: implicit declaration of function ‘bpf_get_pinned_object’ [-Wimplicit-function-declaration]
    fd = bpf_get_pinned_object(file);
         ^

gcc -Lsamples/bpf/libbpf -o samples/bpf/fds_example samples/bpf/utils.o samples/bpf/fds_example.o -lelf -lbpf
samples/bpf/fds_example.o: In function `main':
fds_example.c:(.text.startup+0x20e): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2b0): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2f3): undefined reference to bpf_get_pinned_object' fds_example.c:(.text.startup+0x344): undefined reference to bpf_get_pinned_object'
collect2: error: ld returned 1 exit status
scripts/Makefile.host:100: recipe for target 'samples/bpf/fds_example' failed
make[1]: *** [samples/bpf/fds_example] Error 1
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2
[wagi@handman linux (bpf-test)]$ make samples/bpf/

   CHK     include/config/kernel.release
   CHK     include/generated/uapi/linux/version.h
   CHK     include/generated/utsrelease.h
   CHK     include/generated/bounds.h
   CHK     include/generated/timeconst.h
   CHK     include/generated/asm-offsets.h
   CALL    scripts/checksyscalls.sh
   HOSTLD  samples/bpf/fds_example
Quoted 7 lines

samples/bpf/fds_example.o: In function `main':
fds_example.c:(.text.startup+0x20e): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2b0): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2f3): undefined reference to bpf_get_pinned_object' fds_example.c:(.text.startup+0x344): undefined reference to bpf_get_pinned_object'
collect2: error: ld returned 1 exit status

Could you please check whether this patch:

bpf tools: Support BPF_OBJ_PIN and BPF_OBJ_GET

is applied properly?

Thank you.

#27 lore raw
Quoted 10 lines · Wangnan (F)
Quoted 2 lines · Daniel Wagner

On 12/17/2015 08:03 AM, Daniel Wagner wrote:
Patch number 2 didn't apply cleanly.

Because I have another patch in my local tree which also modifis bpf Makefile:

http://lkml.kernel.org/g/[email protected]

sorry.

Ah, that explains it this problem.

Quoted 23 lines
Quoted 14 lines

After fixing this manually
I was able to continue to the build step:

$ make samples/bpf/
   CHK     include/config/kernel.release
   CHK     include/generated/uapi/linux/version.h
   CHK     include/generated/utsrelease.h
   CHK     include/generated/bounds.h
   CHK     include/generated/timeconst.h
   CHK     include/generated/asm-offsets.h
   CALL    scripts/checksyscalls.sh
make -C /home/wagi/src/linux/tools/lib/bpf
O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1
/home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
Quoted 8 lines
No libelf found
Makefile:203: recipe for target 'elfdep' failed
make[2]: *** [elfdep] Error 255
samples/bpf/Makefile:10: recipe for target
'samples/bpf/libbpf/libbpf.a' failed
make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2
Quoted 5 lines

When you see this, could you please have a look at:

/home/wagi/src/linux/samples/bpf/libbpf/feature/test-*.make.output

?

test-libpython.c:1:20: fatal error: Python.h: No such file or directory

$ find /usr/include -name Python.h
/usr/include/python3.4m/Python.h
/usr/include/python2.7/Python.h

Adding a symlink to /usr/include/Python.h to
/usr/include/python2.7/Python.h didn't help.

Quoted 2 lines

This error means it can't find libelf on your platform, but it is not true obviously.

Yeah look like. Funny thing though is if I build libelf directly

$  make -C tools/lib/bpf/
make: Entering directory '/home/wagi/src/linux/tools/lib/bpf'

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

  CC       fixdep.o
  LD       fixdep-in.o
  LINK     fixdep
  CC       libbpf.o
  CC       bpf.o
  LD       libbpf-in.o
  LINK     libbpf.a
  LINK     libbpf.so

make: Leaving directory '/home/wagi/src/linux/tools/lib/bpf'

all is fine.

Quoted 11 lines
Quoted 10 lines

fds_example.c:(.text.startup+0x20e): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2b0): undefined reference to bpf_pin_object' fds_example.c:(.text.startup+0x2f3): undefined reference to bpf_get_pinned_object' fds_example.c:(.text.startup+0x344): undefined reference to bpf_get_pinned_object'
collect2: error: ld returned 1 exit status

Quoted 5 lines

Could you please check whether this patch:

bpf tools: Support BPF_OBJ_PIN and BPF_OBJ_GET

is applied properly?

No it wasn't. Sorry about that one. I didn't pay enough attention when applying them. The v2 of 6 did upset my workflow.

#28 lore raw
Quoted 34 lines · Daniel Wagner
Quoted 9 lines · Wangnan (F)
Quoted 2 lines · Daniel Wagner

On 12/17/2015 08:03 AM, Daniel Wagner wrote:
Patch number 2 didn't apply cleanly.

Because I have another patch in my local tree which also modifis bpf Makefile:

http://lkml.kernel.org/g/[email protected]

sorry.

Ah, that explains it this problem.

Quoted 22 lines
Quoted 22 lines

After fixing this manually
I was able to continue to the build step:

$ make samples/bpf/
    CHK     include/config/kernel.release
    CHK     include/generated/uapi/linux/version.h
    CHK     include/generated/utsrelease.h
    CHK     include/generated/bounds.h
    CHK     include/generated/timeconst.h
    CHK     include/generated/asm-offsets.h
    CALL    scripts/checksyscalls.sh
make -C /home/wagi/src/linux/tools/lib/bpf
O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1
/home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
No libelf found
Makefile:203: recipe for target 'elfdep' failed
make[2]: *** [elfdep] Error 255
samples/bpf/Makefile:10: recipe for target
'samples/bpf/libbpf/libbpf.a' failed
make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2
Quoted 6 lines
Quoted 5 lines

When you see this, could you please have a look at:

/home/wagi/src/linux/samples/bpf/libbpf/feature/test-*.make.output

?

test-libpython.c:1:20: fatal error: Python.h: No such file or directory

So it is the content in test-all.make.output ? Then it is not a problem. It is only a fastpath which tries to check all features by one test. On most platform it would fail.

BPF related feature check is not in test-all. It is a potential bug, but I don't think it causes your problem.

Another problem is you didn't see this in the first failure:

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

This only happen when you already have a FEATURE-DUMP.libbpf in that directory and it is same as the feature check result.

Could you please remove samples/bpf in your building tree and try again? After you see the failure, what's the content of

/home/wagi/src/linux/samples/bpf/libbpf/FEATURE-* ?

Thank you.

#29 lore raw
Quoted 7 lines · Wangnan (F)
Quoted 6 lines · Daniel Wagner
Quoted 5 lines · Wangnan (F)
Quoted 2 lines · Daniel Wagner

On 12/17/2015 08:03 AM, Daniel Wagner wrote:
Patch number 2 didn't apply cleanly.

When you see this, could you please have a look at:

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

/home/wagi/src/linux/samples/bpf/libbpf/feature/test-*.make.output

?

test-libpython.c:1:20: fatal error: Python.h: No such file or directory

So it is the content in test-all.make.output ? Then it is not a problem. It is only a fastpath which tries to check all features by one test. On most platform it would fail.

I see.

Quoted 9 lines

BPF related feature check is not in test-all. It is a potential bug, but I don't think it causes your problem.

Another problem is you didn't see this in the first failure:

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

Quoted 7 lines

This only happen when you already have a FEATURE-DUMP.libbpf in that directory and it is same as the feature check result.

Could you please remove samples/bpf in your building tree and try again? After you see the failure, what's the content of

/home/wagi/src/linux/samples/bpf/libbpf/FEATURE-* ?

I cleanup up my tree (rm samples/bpf/ and git checkout samples/bpf) and then

$ make samples/bpf/ 
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  LD      samples/bpf/built-in.o
make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a

Auto-detecting system features:
... libelf: [ OFF ]
... bpf: [ OFF ]

No libelf found
Makefile:203: recipe for target 'elfdep' failed
make[2]: *** [elfdep] Error 255
samples/bpf/Makefile:10: recipe for target 'samples/bpf/libbpf/libbpf.a' failed make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2

$ cat /home/wagi/src/linux/samples/bpf/libbpf/FEATURE-*
feature-libelf(0) feature-bpf(0)
$ make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
make: Entering directory '/home/wagi/src/linux/tools/lib/bpf'

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

make -f /home/wagi/src/linux/tools/build/Makefile.build dir=. obj=libbpf
gcc -Wp,-MD,/home/wagi/src/linux/samples/bpf/libbpf/.libbpf.o.d,-MT,/home/wagi/src/linux/samples/bpf/libbpf/libbpf.o -DHAVE_LIBELF_MMAP_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wshadow -Wstrict-aliasing=3 -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Werror -Wall -fPIC -I. -I/home/wagi/src/linux/tools/include -I/home/wagi/src/linux/arch//include/uapi -I/home/wagi/src/linux/include/uapi -D"BUILD_STR(s)=#s" -c -o /home/wagi/src/linux/samples/bpf/libbpf/libbpf.o libbpf.c
gcc -Wp,-MD,/home/wagi/src/linux/samples/bpf/libbpf/.bpf.o.d,-MT,/home/wagi/src/linux/samples/bpf/libbpf/bpf.o -DHAVE_LIBELF_MMAP_SUPPORT -DHAVE_ELF_GETPHDRNUM_SUPPORT -Wbad-function-cast -Wdeclaration-after-statement -Wformat-security -Wformat-y2k -Winit-self -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wno-system-headers -Wold-style-definition -Wpacked -Wredundant-decls -Wshadow -Wstrict-aliasing=3 -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wundef -Wwrite-strings -Wformat -Werror -Wall -fPIC -I. -I/home/wagi/src/linux/tools/include -I/home/wagi/src/linux/arch//include/uapi -I/home/wagi/src/linux/include/uapi -D"BUILD_STR(s)=#s" -c -o /home/wagi/src/linux/samples/bpf/libbpf/bpf.o bpf.c
ld -r -o /home/wagi/src/linux/samples/bpf/libbpf/libbpf-in.o /home/wagi/src/linux/samples/bpf/libbpf/libbpf.o /home/wagi/src/linux/samples/bpf/libbpf/bpf.o
rm -f /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a; ar rcs /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a /home/wagi/src/linux/samples/bpf/libbpf/libbpf-in.o
make: Leaving directory '/home/wagi/src/linux/tools/lib/bpf'

$  make samples/bpf/
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  HOSTCC  samples/bpf/test_verifier.o
  HOSTLD  samples/bpf/test_verifier
  HOSTCC  samples/bpf/test_maps.o
  HOSTLD  samples/bpf/test_maps
  HOSTCC  samples/bpf/sock_example.o
  HOSTCC  samples/bpf/utils.o
  HOSTLD  samples/bpf/sock_example
  HOSTCC  samples/bpf/fds_example.o
  HOSTLD  samples/bpf/fds_example
  HOSTCC  samples/bpf/sockex1_user.o
  HOSTLD  samples/bpf/sockex1
  HOSTCC  samples/bpf/sockex2_user.o
  HOSTLD  samples/bpf/sockex2
  HOSTCC  samples/bpf/sockex3_user.o
  HOSTLD  samples/bpf/sockex3
  HOSTCC  samples/bpf/tracex1_user.o
  HOSTLD  samples/bpf/tracex1
  HOSTCC  samples/bpf/tracex2_user.o
  HOSTLD  samples/bpf/tracex2
  HOSTCC  samples/bpf/tracex3_user.o
  HOSTLD  samples/bpf/tracex3
  HOSTCC  samples/bpf/tracex4_user.o
  HOSTLD  samples/bpf/tracex4
  HOSTCC  samples/bpf/tracex5_user.o
  HOSTLD  samples/bpf/tracex5
  HOSTCC  samples/bpf/tracex6_user.o
  HOSTLD  samples/bpf/tracex6
  HOSTCC  samples/bpf/trace_output_user.o
  HOSTLD  samples/bpf/trace_output
  HOSTCC  samples/bpf/lathist_user.o
  HOSTLD  samples/bpf/lathist
clang  -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/5.1.1/include -I./arch/x86/include -Iarch/x86/include/generated/uapi -Iarch/x86/include/generated  -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h  \
        -D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \
        -O2 -emit-llvm -c samples/bpf/sockex1_kern.c -o -| ./tools/bpf/llvm/bld/Debug+Asserts/bin/llc -march=bpf -filetype=obj -o samples/bpf/sockex1_kern.o
/bin/sh: line 2: ./tools/bpf/llvm/bld/Debug+Asserts/bin/llc: No such file or directory
clang: error: unable to execute command: Broken pipe
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 3.5.0 (tags/RELEASE_350/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
clang: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/sockex1_kern-d40fee.c
clang: note: diagnostic msg: /tmp/sockex1_kern-d40fee.sh
clang: note: diagnostic msg:

********************
samples/bpf/Makefile:83: recipe for target 'samples/bpf/sockex1_kern.o' failed make[1]: *** [samples/bpf/sockex1_kern.o] Error 127 Makefile:1550: recipe for target 'samples/bpf/' failed

So the last error is clear. llc is missing I need to rebuild it. There is something funny going on for the first call 'make samples/bpf/'.

author
#30 lore raw

PATCH 1/1 tools build: Output more data during feature detection

Even with current '.make.output' finding the root cause for missing feature is not enough. This patch adds extra information to .make.output. It also creates a .make.makeoutput to collect output of 'make'.

Cc Daniel Wagner <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/build/Makefile.feature | 4 ++--
 tools/build/feature/Makefile | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)
tools/build/Makefile.feature +2 -2
index 6c0519d..84927df 100644
@@ -1,13 +1,13 @@
 feature_dir := $(srctree)/tools/build/feature
 
 ifneq ($(OUTPUT),)
-  OUTPUT_FEATURES = $(OUTPUT)feature/
+  OUTPUT_FEATURES = $(OUTPUT)feature$(FEATURE_USER)/
   $(shell mkdir -p $(OUTPUT_FEATURES))
 endif
 
 feature_check = $(eval $(feature_check_code))
 define feature_check_code
-  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
+  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >$(OUTPUT_FEATURES)test-$1.make.makeoutput 2>&1 && echo 1 || echo 0)
 endef
 
 feature_set = $(eval $(feature_set_code))
tools/build/feature/Makefile +6 -1
index bf8f035..842973a 100644
@@ -45,7 +45,12 @@ PKG_CONFIG := $(CROSS_COMPILE)pkg-config
 all: $(FILES)
 
 __BUILD = $(CC) $(CFLAGS) -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
-  BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1
+  BUILD = echo PWD=$(shell pwd) > $(@:.bin=.make.output); \
+	  echo PATH=$$PATH >> $(@:.bin=.make.output); \
+	  echo CFLAGS=$$CFLAGS >> $(@:.bin=.make.output); \
+	  echo LDFLAGS=$$LDFLAGS >> $(@:.bin=.make.output); \
+	  echo '$(__BUILD)' >> $(@:.bin=.make.output) ; \
+	  $(__BUILD) >> $(@:.bin=.make.output) 2>&1
 
 ###############################
 
Signature · 1 line
1.8.3.4
#31 lore raw
In reply to Daniel Wagner
Quoted 46 lines · Daniel Wagner
Quoted 7 lines · Wangnan (F)
Quoted 6 lines · Daniel Wagner
Quoted 5 lines · Wangnan (F)
Quoted 2 lines · Daniel Wagner

On 12/17/2015 08:03 AM, Daniel Wagner wrote:
Patch number 2 didn't apply cleanly.

When you see this, could you please have a look at:

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

/home/wagi/src/linux/samples/bpf/libbpf/feature/test-*.make.output

?

test-libpython.c:1:20: fatal error: Python.h: No such file or directory

So it is the content in test-all.make.output ? Then it is not a problem. It is only a fastpath which tries to check all features by one test. On most platform it would fail.

I see.

Quoted 9 lines

BPF related feature check is not in test-all. It is a potential bug, but I don't think it causes your problem.

Another problem is you didn't see this in the first failure:

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

Quoted 7 lines

This only happen when you already have a FEATURE-DUMP.libbpf in that directory and it is same as the feature check result.

Could you please remove samples/bpf in your building tree and try again? After you see the failure, what's the content of

/home/wagi/src/linux/samples/bpf/libbpf/FEATURE-* ?

I cleanup up my tree (rm samples/bpf/ and git checkout samples/bpf) and then

$ make samples/bpf/
   CHK     include/config/kernel.release
   CHK     include/generated/uapi/linux/version.h
   CHK     include/generated/utsrelease.h
   CHK     include/generated/bounds.h
   CHK     include/generated/timeconst.h
   CHK     include/generated/asm-offsets.h
   CALL    scripts/checksyscalls.sh
   LD      samples/bpf/built-in.o
make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a
Quoted 3 lines

Auto-detecting system features:
... libelf: [ OFF ]
... bpf: [ OFF ]

That's good. This is not a magic. I guess something in your top makefile causes this feature check failure. I sent a patch on feature checker to you. It will appear as [1] soon. Could you please apply it and try again?

This time you will see test-*.make.output and test-*.make.makeoutput. test-*.make.output lists your PATH, PWD and the gcc command you use for feature check. test-*.make.makeoutput is the output of 'make' itself. I think from these information we can find the root cause easier.

Thank you.

[1] http://lkml.kernel.org/g/[email protected]

#32 lore raw
Quoted 1 line · Wangnan (F)

That's good. This is not a magic.

That is what you say! :)

Quoted 4 lines

I guess something in your top makefile
causes this feature check failure. I sent a patch on feature checker to you.
It will appear as [1] soon. Could you please apply it and try again?

Sure.

Quoted 5 lines

This time you will see test-*.make.output and test-*.make.makeoutput. test-*.make.output lists your PATH, PWD and the gcc command you use for feature check. test-*.make.makeoutput is the output of 'make' itself. I think
from these information we can find the root cause easier.

$ ls samples/bpf/libbpf/feature.libbpf/*
samples/bpf/libbpf/feature.libbpf/test-all.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-bpf.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf-getphdrnum.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf-mmap.make.makeoutput
$ cat samples/bpf/libbpf/feature.libbpf/*
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
$ find -name *.make.output
./tools/build/feature/test-gtk2.make.output
./tools/build/feature/test-libpython.make.output
./tools/build/feature/test-libunwind.make.output
./tools/build/feature/test-bionic.make.output
./tools/build/feature/test-libelf-mmap.make.output
./tools/build/feature/test-numa_num_possible_cpus.make.output
./tools/build/feature/test-libaudit.make.output
./tools/build/feature/test-libslang.make.output
./tools/build/feature/test-libdw-dwarf-unwind.make.output
./tools/build/feature/test-libbfd.make.output
./tools/build/feature/test-get_cpuid.make.output
./tools/build/feature/test-zlib.make.output
./tools/build/feature/test-libelf.make.output
./tools/build/feature/test-libperl.make.output
./tools/build/feature/test-bpf.make.output
./tools/build/feature/test-dwarf.make.output
./tools/build/feature/test-libnuma.make.output
./tools/build/feature/test-glibc.make.output
./tools/build/feature/test-cplus-demangle.make.output
./tools/build/feature/test-libelf-getphdrnum.make.output
./tools/build/feature/test-all.make.output
./tools/build/feature/test-lzma.make.output
$ ls -l  tools/build/feature/*.make.output
-rw-rw-r-- 1 wagi wagi 419 Dec 17 14:39 tools/build/feature/test-all.make.output
-rw-rw-r-- 1 wagi wagi 104 Oct 20 09:59 tools/build/feature/test-bionic.make.output
-rw-rw-r-- 1 wagi wagi   0 Dec 17 09:01 tools/build/feature/test-bpf.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-cplus-demangle.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-dwarf.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-get_cpuid.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-glibc.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-gtk2.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libaudit.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libbfd.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libdw-dwarf-unwind.make.output
-rw-rw-r-- 1 wagi wagi   0 Dec 17 09:01 tools/build/feature/test-libelf-getphdrnum.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libelf.make.output
-rw-rw-r-- 1 wagi wagi   0 Dec 17 09:01 tools/build/feature/test-libelf-mmap.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libnuma.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libperl.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libpython.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libslang.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-libunwind.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-lzma.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-numa_num_possible_cpus.make.output
-rw-rw-r-- 1 wagi wagi   0 Oct 20 09:59 tools/build/feature/test-zlib.make.output
$ cat tools/build/feature/test-all.make.output
In file included from /usr/include/Python.h:7:0,
                 from test-libpython.c:1,
                 from test-all.c:13:
/usr/lib64/perl5/CORE/patchlevel.h:135:2: error: ‘NULL’ undeclared here (not in a function)
  NULL
  ^
In file included from test-libpython.c:1:0,
                 from test-all.c:13:
/usr/include/Python.h:8:22: fatal error: pyconfig.h: No such file or directory
compilation terminated.
$ cat tools/build/feature/test-bionic.make.output
test-bionic.c:1:31: fatal error: android/api-level.h: No such file or directory
compilation terminated.

Could it be that $(srcdir) somehow is not correct? I exchanged

	feature_dir := $(srctree)/tools/build/feature

to
feature_dir := /home/wagi/src/linux/tools/build/feature

and then the feature check worked in this case but still no fun:

$ make samples/bpf/
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CHK include/generated/bounds.h
CHK include/generated/timeconst.h
CHK include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
make -C /home/wagi/src/linux/tools/lib/bpf O=/home/wagi/src/linux/samples/bpf/libbpf CFLAGS= LDFLAGS= V=1 /home/wagi/src/linux/samples/bpf/libbpf/libbpf.a

Auto-detecting system features:
... libelf: [ on ]
... bpf: [ on ]

make -f ./tools/build/Makefile.build dir=. obj=libbpf
make[3]: tools/build/Makefile.build: No such file or directory
make[3]: *** No rule to make target 'tools/build/Makefile.build'. Stop. Makefile:155: recipe for target '/home/wagi/src/linux/samples/bpf/libbpf/libbpf-in.o' failed make[2]: *** [/home/wagi/src/linux/samples/bpf/libbpf/libbpf-in.o] Error 2 samples/bpf/Makefile:10: recipe for target 'samples/bpf/libbpf/libbpf.a' failed make[1]: *** [samples/bpf/libbpf/libbpf.a] Error 2
Makefile:1550: recipe for target 'samples/bpf/' failed
make: *** [samples/bpf/] Error 2

#33 lore raw

发自我的 iPhone

Quoted 27 lines · 在 2015年12月18日,下午8:55,Daniel Wagner <[email protected]> 写道:
Quoted 3 lines · Wangnan (F)

That's good. This is not a magic.

That is what you say! :)

Quoted 4 lines

I guess something in your top makefile
causes this feature check failure. I sent a patch on feature checker to you.
It will appear as [1] soon. Could you please apply it and try again?

Sure.

Quoted 5 lines

This time you will see test-*.make.output and test-*.make.makeoutput. test-*.make.output lists your PATH, PWD and the gcc command you use for feature check. test-*.make.makeoutput is the output of 'make' itself. I think
from these information we can find the root cause easier.

$ ls samples/bpf/libbpf/feature.libbpf/*
samples/bpf/libbpf/feature.libbpf/test-all.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-bpf.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf-getphdrnum.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf.make.makeoutput
samples/bpf/libbpf/feature.libbpf/test-libelf-mmap.make.makeoutput
Quoted 7 lines
$ cat samples/bpf/libbpf/feature.libbpf/*
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.
make[2]: *** tools/build/feature: No such file or directory.  Stop.

Thank you for your information.

I think the reason of the failure is the missing of srctree. I skipped a patch [1] because I think it is independent with this patchset, but I was wrong.

Could you please add the srctree setting code block from [1] to your tools/lib/bpf/Makefile and try again? You don't need other part of it, or you have to apply the whole patchset because it moves a file.

[1] http://lkml.kernel.org/g/[email protected]

Thank you.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#35 lore raw
Quoted 11 lines · On 12/18/2015 02:50 PM, pi3orama wrote:
Quoted 6 lines

Could it be that $(srcdir) somehow is not correct? I exchanged

   feature_dir := $(srctree)/tools/build/feature

to
feature_dir := /home/wagi/src/linux/tools/build/feature

Can you remember the patch set I skipped?

http://lkml.kernel.org/g/[email protected]
<http://lkml.kernel.org/g/[email protected]>

I'll give them a try next week.

Quoted 13 lines

I thought that patch set is independent with this one but I was wrong. The reason
of your problem is the missing of srctree. Could you please add the definition of
srctree and try again? Like this:

(quoted diff) +6 -0
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(shell pwd)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+#$(info Determined 'srctree' to be $(srctree))
+endif
Quoted 1 line

At the head of tools/lib/bpf/Makefile

I tried that one but it didn't change unfortunately. I saw that code snipped is already in tools/lib/bpf/Makefile line 62. It exists since

1b76c13e4b36 ("bpf tools: Introduce 'bpf' library and add bpf feature check")