linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <namhyung@kernel.org>, <alexei.starovoitov@gmail.com>,
	<masami.hiramatsu.pt@hitachi.com>, <acme@kernel.org>,
	<paulus@samba.org>, <a.p.zijlstra@chello.nl>, <mingo@redhat.com>,
	<jolsa@kernel.org>, <dsahern@gmail.com>, <daniel@iogearbox.net>,
	<brendan.d.gregg@gmail.com>
Cc: <lizefan@huawei.com>, <hekuang@huawei.com>, <xiakaixu@huawei.com>,
	<linux-kernel@vger.kernel.org>, <pi3orama@163.com>
Subject: [RFC PATCH v5 06/30] bpf tools: Open eBPF object file and do basic validation
Date: Mon, 1 Jun 2015 07:37:52 +0000	[thread overview]
Message-ID: <1433144296-74992-7-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1433144296-74992-1-git-send-email-wangnan0@huawei.com>

This patch defines basic interface of libbpf. 'struct bpf_object' will
be the handler of each object file. Its internal structure is hide to
user. eBPF object files are compiled by LLVM as ELF format. In this
patch, libelf is used to open those files, read EHDR and do basic
validation according to e_type and e_machine.

All elf related staffs are grouped together and reside in efile field of
'struct bpf_object'. bpf_object__elf_finish() is introduced to clear it.

After all eBPF programs in an object file are loaded, related ELF
information is useless. Close the object file and free those memory.

zfree() and zclose() are introduced to ensure setting NULL pointers and
negative file descriptors after resources are released.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
---
 tools/lib/bpf/libbpf.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h |   8 +++
 2 files changed, 160 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ded37a0..0824822 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -11,8 +11,12 @@
 #include <stdarg.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
 #include <asm/unistd.h>
 #include <linux/bpf.h>
+#include <libelf.h>
+#include <gelf.h>
 
 #include "libbpf.h"
 
@@ -52,3 +56,151 @@ void libbpf_set_print(libbpf_print_fn_t warn,
 	__pr_info = info;
 	__pr_debug = debug;
 }
+
+/* Copied from tools/perf/util/util.h */
+#ifndef zfree
+# define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
+#endif
+
+#ifndef zclose
+# define zclose(fd) ({			\
+	int ___err = 0;			\
+	if ((fd) >= 0)			\
+		___err = close((fd));	\
+	fd = -1;			\
+	___err;})
+#endif
+
+#ifdef HAVE_LIBELF_MMAP_SUPPORT
+# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ_MMAP
+#else
+# define LIBBPF_ELF_C_READ_MMAP ELF_C_READ
+#endif
+
+struct bpf_object {
+	/*
+	 * Information when doing elf related work. Only valid if fd
+	 * is valid.
+	 */
+	struct {
+		int fd;
+		Elf *elf;
+		GElf_Ehdr ehdr;
+	} efile;
+	char path[];
+};
+#define obj_elf_valid(o)	((o)->efile.fd >= 0)
+
+static struct bpf_object *bpf_object__new(const char *path)
+{
+	struct bpf_object *obj;
+
+	obj = calloc(1, sizeof(struct bpf_object) + strlen(path) + 1);
+	if (!obj) {
+		pr_warning("alloc memory failed for %s\n", path);
+		return NULL;
+	}
+
+	strcpy(obj->path, path);
+	obj->efile.fd = -1;
+	return obj;
+}
+
+static void bpf_object__elf_finish(struct bpf_object *obj)
+{
+	if (!obj_elf_valid(obj))
+		return;
+
+	if (obj->efile.elf) {
+		elf_end(obj->efile.elf);
+		obj->efile.elf = NULL;
+	}
+	zclose(obj->efile.fd);
+}
+
+static int bpf_object__elf_init(struct bpf_object *obj)
+{
+	int err = 0;
+	GElf_Ehdr *ep;
+
+	if (obj_elf_valid(obj)) {
+		pr_warning("elf init: internal error\n");
+		return -EEXIST;
+	}
+	
+	obj->efile.fd = open(obj->path, O_RDONLY);
+	if (obj->efile.fd < 0) {
+		pr_warning("failed to open %s: %s\n", obj->path,
+				strerror(errno));
+		return -errno;
+	}
+
+	obj->efile.elf = elf_begin(obj->efile.fd,
+				 LIBBPF_ELF_C_READ_MMAP,
+				 NULL);
+	if (!obj->efile.elf) {
+		pr_warning("failed to open %s as ELF file\n",
+				obj->path);
+		err = -EINVAL;
+		goto errout;
+	}
+
+	if (!gelf_getehdr(obj->efile.elf, &obj->efile.ehdr)) {
+		pr_warning("failed to get EHDR from %s\n",
+				obj->path);
+		err = -EINVAL;
+		goto errout;
+	}
+	ep = &obj->efile.ehdr;
+
+	if ((ep->e_type != ET_REL) || (ep->e_machine != 0)) {
+		pr_warning("%s is not an eBPF object file\n",
+			obj->path);
+		err = -EINVAL;
+		goto errout;
+	}
+
+	return 0;
+errout:
+	bpf_object__elf_finish(obj);
+	return err;
+}
+
+struct bpf_object *bpf_object__open(const char *path)
+{
+	struct bpf_object *obj;
+
+	/* param validation */
+	if (!path)
+		return NULL;
+
+ 	pr_debug("loading %s\n", path);
+
+	if (elf_version(EV_CURRENT) == EV_NONE) {
+		pr_warning("failed to init libelf for %s\n", path);
+		return NULL;
+	}
+
+	obj = bpf_object__new(path);
+	if (!obj)
+		return NULL;
+
+	if (bpf_object__elf_init(obj))
+		goto out;
+
+	bpf_object__elf_finish(obj);
+	return obj;
+out:
+	bpf_object__close(obj);
+	return NULL;
+}
+
+void bpf_object__close(struct bpf_object *obj)
+{
+	if (!obj)
+		return;
+
+	bpf_object__elf_finish(obj);
+
+	free(obj);
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 8d1eeba..ec3301c 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -8,6 +8,8 @@
 #ifndef __BPF_LIBBPF_H
 #define __BPF_LIBBPF_H
 
+#include <stdio.h>
+
 /*
  * In include/linux/compiler-gcc.h, __printf is defined. However
  * it should be better if libbpf.h doesn't depend on Linux header file.
@@ -20,4 +22,10 @@ void libbpf_set_print(libbpf_print_fn_t warn,
 		      libbpf_print_fn_t info,
 		      libbpf_print_fn_t debug);
 
+/* Hide internal to user */
+struct bpf_object;
+
+struct bpf_object *bpf_object__open(const char *path);
+void bpf_object__close(struct bpf_object *object);
+
 #endif
-- 
1.8.3.4


  parent reply	other threads:[~2015-06-01 10:02 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-01  7:37 [RFC PATCH v5 00/30] perf tools: filtering events using eBPF programs Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 01/30] perf tools: Move linux/kernel.h to tools/include Wang Nan
2015-06-04 14:11   ` [tip:perf/core] " tip-bot for Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 02/30] perf tools: Move linux/{list.h,poison.h} " Wang Nan
2015-06-04 14:11   ` [tip:perf/core] tools: Move tools/perf/util/include/linux/{list.h ,poison.h} " tip-bot for Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 03/30] tools build: Add feature check for eBPF API Wang Nan
2015-06-02 22:11   ` Arnaldo Carvalho de Melo
2015-06-03  1:15     ` Wangnan (F)
2015-06-03 13:47       ` Arnaldo Carvalho de Melo
2015-06-01  7:37 ` [RFC PATCH v5 04/30] bpf tools: Introduce 'bpf' library to tools Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 05/30] bpf tools: Allow caller to set printing function Wang Nan
2015-06-01  7:37 ` Wang Nan [this message]
2015-06-03 21:33   ` [RFC PATCH v5 06/30] bpf tools: Open eBPF object file and do basic validation Arnaldo Carvalho de Melo
2015-06-03 21:35     ` Arnaldo Carvalho de Melo
2015-06-04  2:18       ` Wangnan (F)
2015-06-01  7:37 ` [RFC PATCH v5 07/30] bpf tools: Check endianess and make libbpf fail early Wang Nan
2015-06-03 21:52   ` Arnaldo Carvalho de Melo
2015-06-01  7:37 ` [RFC PATCH v5 08/30] bpf tools: Iterate over ELF sections to collect information Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 09/30] bpf tools: Collect version and license from ELF sections Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 10/30] bpf tools: Collect map definitions from 'maps' section Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 11/30] bpf tools: Collect symbol table from SHT_SYMTAB section Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 12/30] bpf tools: Collect eBPF programs from their own sections Wang Nan
2015-06-01  7:37 ` [RFC PATCH v5 13/30] bpf tools: Collect relocation sections from SHT_REL sections Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 14/30] bpf tools: Record map accessing instructions for each program Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 15/30] bpf tools: Add bpf.c/h for common bpf operations Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 16/30] bpf tools: Create eBPF maps defined in an object file Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 17/30] bpf tools: Relocate eBPF programs Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 18/30] bpf tools: Introduce bpf_load_program() to bpf.c Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 19/30] bpf tools: Load eBPF programs in object files into kernel Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 20/30] bpf tools: Introduce accessors for struct bpf_program Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 21/30] bpf tools: Introduce accessors for struct bpf_object Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 22/30] bpf tools: Link all bpf objects onto a list Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 23/30] perf tools: Make perf depend on libbpf Wang Nan
2015-06-03  2:45   ` [RFC PATCH v6 " Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 24/30] perf record: Enable passing bpf object file to --event Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 25/30] perf tools: Parse probe points of eBPF programs during preparation Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 26/30] perf probe: Attach trace_probe_event with perf_probe_event Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 27/30] perf record: Probe at kprobe points Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 28/30] perf record: Load all eBPF object into kernel Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 29/30] perf tools: Add bpf_fd field to evsel and config it Wang Nan
2015-06-01  7:38 ` [RFC PATCH v5 30/30] perf tools: Attach eBPF program to perf event Wang Nan
2015-06-02  0:48 ` [RFC PATCH v5 00/30] perf tools: filtering events using eBPF programs Alexei Starovoitov
2015-06-02  1:15   ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1433144296-74992-7-git-send-email-wangnan0@huawei.com \
    --to=wangnan0@huawei.com \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=alexei.starovoitov@gmail.com \
    --cc=brendan.d.gregg@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=dsahern@gmail.com \
    --cc=hekuang@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=paulus@samba.org \
    --cc=pi3orama@163.com \
    --cc=xiakaixu@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).