From: "Björn Töpel" <bjorn.topel@gmail.com>
To: bjorn.topel@gmail.com, magnus.karlsson@intel.com,
magnus.karlsson@gmail.com, ast@kernel.org, daniel@iogearbox.net,
netdev@vger.kernel.org
Cc: "Björn Töpel" <bjorn.topel@intel.com>,
brouer@redhat.com, u9012063@gmail.com, qi.z.zhang@intel.com,
jakub.kicinski@netronome.com, andrew@lunn.ch
Subject: [PATCH bpf-next v2 5/7] libbpf: initial support for builtin BPF programs
Date: Mon, 17 Dec 2018 11:24:59 +0100 [thread overview]
Message-ID: <20181217102501.22019-6-bjorn.topel@gmail.com> (raw)
In-Reply-To: <20181217102501.22019-1-bjorn.topel@gmail.com>
From: Björn Töpel <bjorn.topel@intel.com>
This commit introduces builtin BPF programs to libbpf. A builtin
program is simply a BPF program that is bundled with libbpf.
The first builtin program is an XDP program, "xdp_xsk_redirect", which
is a trivial program that calls bpf_xsk_redirect for each received
packet, and redirects it to the corresponding XDP socket.
Two new functions are added to the libbpf API:
LIBBPF_API struct bpf_object *bpf_object__open_builtin(
enum bpf_prog_type prog_type);
LIBBPF_API struct bpf_program *
bpf_object__find_xdp_builtin_program(
struct bpf_object* obj,
enum libbpf_builtin_xdp_prog prog);
The first function is used to get a handle to the bpf_object
containing all builtin programs for a certain program type. The latter
is used to access a certain builtin program from the bpf_object. Note
that currenty only XDP is supported. When other program types are
supported, additional bpf_object__find_PROG_TYPE_builtin_program
function are required.
When/if packet cloning is introduced to XDP, another builtin program
candidate would be a program that clones all packets to an XDP socket.
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
---
tools/lib/bpf/libbpf.c | 85 ++++++++++++++++++++++++++++++++++++++++
tools/lib/bpf/libbpf.h | 14 +++++++
tools/lib/bpf/libbpf.map | 2 +
3 files changed, 101 insertions(+)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index e2bc75ee1614..d8551193862b 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -242,6 +242,31 @@ struct bpf_object {
};
#define obj_elf_valid(o) ((o)->efile.elf)
+struct libbpf_builtin_prog {
+ const char *name;
+ const struct bpf_insn *insns;
+ size_t size;
+};
+
+/*
+ * Builtin XDP program: LIBBPF_BUILTIN_XDP__XSK_REDIRECT
+ *
+ * Trivial XDP program that calls bpf_xsk_redirect() on every received
+ * frame.
+ */
+static const struct bpf_insn builtin_xdp_xsk_redirect_insn[] = {
+ BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_xsk_redirect),
+ BPF_EXIT_INSN(),
+};
+
+static const struct libbpf_builtin_prog libbpf_builtin_xdp_prog[] = {
+ [LIBBPF_BUILTIN_XDP__XSK_REDIRECT] = {
+ "xdp_xsk_redirect",
+ &builtin_xdp_xsk_redirect_insn[0],
+ sizeof(builtin_xdp_xsk_redirect_insn)
+ },
+};
+
void bpf_program__unload(struct bpf_program *prog)
{
int i;
@@ -2990,3 +3015,63 @@ bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
ring_buffer_write_tail(header, data_tail);
return ret;
}
+
+struct bpf_object *bpf_object__open_builtin(enum bpf_prog_type prog_type)
+{
+ struct bpf_program *prog;
+ struct bpf_object *obj;
+ int err, i;
+
+ /* Right now, only XDP is supported. */
+ if (prog_type != BPF_PROG_TYPE_XDP)
+ return ERR_PTR(-EINVAL);
+
+ obj = bpf_object__new("", NULL, 0);
+ if (IS_ERR(obj))
+ return NULL;
+
+ CHECK_ERR(bpf_object__init_license(obj, (void *)"GPL", sizeof("GPL")),
+ err, out);
+
+ for (i = 0; i < __LIBBPF_BUILTIN_XDP__END; i++) {
+ err = bpf_object__add_program(
+ obj,
+ (void *)libbpf_builtin_xdp_prog[i].insns,
+ libbpf_builtin_xdp_prog[i].size,
+ (char *)libbpf_builtin_xdp_prog[i].name, i);
+ if (err) {
+ pr_warning("failed to add builtin program %s\n",
+ libbpf_builtin_xdp_prog[i].name);
+ goto out;
+ }
+ }
+
+ bpf_object__for_each_program(prog, obj) {
+ bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
+
+ prog->name = strdup(libbpf_builtin_xdp_prog[prog->idx].name);
+ if (!prog->name) {
+ pr_warning("failed to allocate memory for name %s\n",
+ libbpf_builtin_xdp_prog[prog->idx].name);
+ goto out;
+ }
+ }
+
+ return obj;
+out:
+ bpf_object__close(obj);
+ return NULL;
+
+}
+
+struct bpf_program *bpf_object__find_xdp_builtin_program(
+ struct bpf_object *obj, enum libbpf_builtin_xdp_prog prog)
+{
+ if (!obj)
+ return NULL;
+
+ if (prog < 0 || prog >= __LIBBPF_BUILTIN_XDP__END)
+ return NULL;
+
+ return bpf_object__find_prog_by_idx(obj, prog);
+}
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 5f68d7b75215..0de0cc2da240 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -45,6 +45,15 @@ enum libbpf_errno {
__LIBBPF_ERRNO__END,
};
+enum libbpf_builtin_xdp_prog {
+ /*
+ * Trivial XDP program that calls bpf_xsk_redirect
+ * unconditionally for every received packet.
+ */
+ LIBBPF_BUILTIN_XDP__XSK_REDIRECT,
+ __LIBBPF_BUILTIN_XDP__END,
+};
+
LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
/*
@@ -75,6 +84,8 @@ struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
size_t obj_buf_sz,
const char *name);
+LIBBPF_API struct bpf_object *bpf_object__open_builtin(
+ enum bpf_prog_type prog_type);
LIBBPF_API int bpf_object__pin_maps(struct bpf_object *obj, const char *path);
LIBBPF_API int bpf_object__unpin_maps(struct bpf_object *obj,
const char *path);
@@ -94,6 +105,9 @@ LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
LIBBPF_API struct bpf_program *
bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
+LIBBPF_API struct bpf_program *
+bpf_object__find_xdp_builtin_program(struct bpf_object *obj,
+ enum libbpf_builtin_xdp_prog prog);
LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
#define bpf_object__for_each_safe(pos, tmp) \
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index cd02cd4e2cc3..58acd84d5ff3 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -121,6 +121,8 @@ LIBBPF_0.0.1 {
libbpf_prog_type_by_name;
libbpf_set_print;
libbpf_strerror;
+ bpf_object__open_builtin;
+ bpf_object__find_xdp_builtin_program;
local:
*;
};
--
2.19.1
next prev parent reply other threads:[~2018-12-17 10:25 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-17 10:24 [PATCH bpf-next v2 0/7] Add support for XDP_ATTACH Björn Töpel
2018-12-17 10:24 ` [PATCH bpf-next v2 1/7] xsk: simplify AF_XDP socket teardown Björn Töpel
2018-12-17 10:24 ` [PATCH bpf-next v2 2/7] xsk: add XDP_ATTACH bind() flag Björn Töpel
2018-12-17 10:24 ` [PATCH bpf-next v2 3/7] bpf: add bpf_xsk_redirect function Björn Töpel
2018-12-17 10:24 ` [PATCH bpf-next v2 4/7] tools/bpf: sync kernel uapi bpf.h to tools directory Björn Töpel
2018-12-17 10:24 ` Björn Töpel [this message]
2018-12-17 10:25 ` [PATCH bpf-next v2 6/7] samples: bpf: simplify/cleanup xdpsock Björn Töpel
2018-12-17 10:25 ` [PATCH bpf-next v2 7/7] samples: bpf: add support for XDP_ATTACH to xdpsock Björn Töpel
2018-12-17 12:50 ` [PATCH bpf-next v2 0/7] Add support for XDP_ATTACH Jesper Dangaard Brouer
2018-12-17 13:39 ` Björn Töpel
2018-12-17 14:10 ` Jesper Dangaard Brouer
2018-12-17 14:55 ` Magnus Karlsson
2018-12-17 15:30 ` Björn Töpel
2018-12-18 23:04 ` Alexei Starovoitov
2018-12-19 9:34 ` Björn Töpel
2018-12-19 16:23 ` Jesper Dangaard Brouer
2018-12-20 0:48 ` Alexei Starovoitov
2018-12-18 2:36 ` Jakub Kicinski
2018-12-18 8:59 ` Björn Töpel
2018-12-18 18:18 ` Jakub Kicinski
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=20181217102501.22019-6-bjorn.topel@gmail.com \
--to=bjorn.topel@gmail.com \
--cc=andrew@lunn.ch \
--cc=ast@kernel.org \
--cc=bjorn.topel@intel.com \
--cc=brouer@redhat.com \
--cc=daniel@iogearbox.net \
--cc=jakub.kicinski@netronome.com \
--cc=magnus.karlsson@gmail.com \
--cc=magnus.karlsson@intel.com \
--cc=netdev@vger.kernel.org \
--cc=qi.z.zhang@intel.com \
--cc=u9012063@gmail.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).