From: Alexei Starovoitov <ast@plumgrid.com>
To: "David S. Miller" <davem@davemloft.net>
Cc: Ingo Molnar <mingo@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Andy Lutomirski <luto@amacapital.net>,
Steven Rostedt <rostedt@goodmis.org>,
Daniel Borkmann <dborkman@redhat.com>,
Chema Gonzalez <chema@google.com>,
Eric Dumazet <edumazet@google.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Arnaldo Carvalho de Melo <acme@infradead.org>,
Jiri Olsa <jolsa@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
"H. Peter Anvin" <hpa@zytor.com>,
Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <keescook@chromium.org>,
linux-api@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH RFC v2 net-next 14/16] samples: bpf: add mini eBPF library to manipulate maps and programs
Date: Thu, 17 Jul 2014 21:20:04 -0700 [thread overview]
Message-ID: <1405657206-12060-15-git-send-email-ast@plumgrid.com> (raw)
In-Reply-To: <1405657206-12060-1-git-send-email-ast@plumgrid.com>
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license,
const struct bpf_map_fixup *fixups, int fixup_len);
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
---
samples/bpf/libbpf.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++
samples/bpf/libbpf.h | 22 ++++++++++
2 files changed, 131 insertions(+)
create mode 100644 samples/bpf/libbpf.c
create mode 100644 samples/bpf/libbpf.h
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c
new file mode 100644
index 000000000000..5460d1f3ad43
--- /dev/null
+++ b/samples/bpf/libbpf.c
@@ -0,0 +1,109 @@
+/* eBPF mini library */
+#include <stdlib.h>
+#include <linux/unistd.h>
+#include <unistd.h>
+#include <string.h>
+#include <linux/netlink.h>
+#include <linux/bpf.h>
+#include <errno.h>
+#include "libbpf.h"
+
+struct nlattr_u32 {
+ __u16 nla_len;
+ __u16 nla_type;
+ __u32 val;
+};
+
+int bpf_create_map(int key_size, int value_size, int max_entries)
+{
+ struct nlattr_u32 attr[] = {
+ {
+ .nla_len = sizeof(struct nlattr_u32),
+ .nla_type = BPF_MAP_KEY_SIZE,
+ .val = key_size,
+ },
+ {
+ .nla_len = sizeof(struct nlattr_u32),
+ .nla_type = BPF_MAP_VALUE_SIZE,
+ .val = value_size,
+ },
+ {
+ .nla_len = sizeof(struct nlattr_u32),
+ .nla_type = BPF_MAP_MAX_ENTRIES,
+ .val = max_entries,
+ },
+ };
+
+ return syscall(__NR_bpf, BPF_MAP_CREATE, BPF_MAP_TYPE_HASH, attr, sizeof(attr));
+}
+
+
+int bpf_update_elem(int fd, void *key, void *value)
+{
+ return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, fd, key, value);
+}
+
+int bpf_lookup_elem(int fd, void *key, void *value)
+{
+ return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, fd, key, value);
+}
+
+int bpf_delete_elem(int fd, void *key)
+{
+ return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, fd, key);
+}
+
+int bpf_get_next_key(int fd, void *key, void *next_key)
+{
+ return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, fd, key, next_key);
+}
+
+#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
+
+int bpf_prog_load(enum bpf_prog_type prog_type,
+ const struct bpf_insn *insns, int prog_len,
+ const char *license,
+ const struct bpf_map_fixup *fixups, int fixup_len)
+{
+ int nlattr_size, license_len, err;
+ void *nlattr, *ptr;
+
+ license_len = strlen(license) + 1;
+ nlattr_size = sizeof(struct nlattr) + prog_len + sizeof(struct nlattr) +
+ ROUND_UP(license_len, 4) + sizeof(struct nlattr) +
+ fixup_len;
+
+ ptr = nlattr = malloc(nlattr_size);
+
+ *(struct nlattr *) ptr = (struct nlattr) {
+ .nla_len = prog_len + sizeof(struct nlattr),
+ .nla_type = BPF_PROG_TEXT,
+ };
+ ptr += sizeof(struct nlattr);
+
+ memcpy(ptr, insns, prog_len);
+ ptr += prog_len;
+
+ *(struct nlattr *) ptr = (struct nlattr) {
+ .nla_len = ROUND_UP(license_len, 4) + sizeof(struct nlattr),
+ .nla_type = BPF_PROG_LICENSE,
+ };
+ ptr += sizeof(struct nlattr);
+
+ memcpy(ptr, license, license_len);
+ ptr += ROUND_UP(license_len, 4);
+
+ *(struct nlattr *) ptr = (struct nlattr) {
+ .nla_len = fixup_len + sizeof(struct nlattr),
+ .nla_type = BPF_PROG_MAP_FIXUP,
+ };
+ ptr += sizeof(struct nlattr);
+
+ memcpy(ptr, fixups, fixup_len);
+ ptr += fixup_len;
+
+ err = syscall(__NR_bpf, BPF_PROG_LOAD, prog_type, nlattr, nlattr_size);
+
+ free(nlattr);
+ return err;
+}
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h
new file mode 100644
index 000000000000..07f668e3dac0
--- /dev/null
+++ b/samples/bpf/libbpf.h
@@ -0,0 +1,22 @@
+/* eBPF mini library */
+#ifndef __LIBBPF_H
+#define __LIBBPF_H
+
+struct bpf_insn;
+
+int bpf_create_map(int key_size, int value_size, int max_entries);
+int bpf_update_elem(int fd, void *key, void *value);
+int bpf_lookup_elem(int fd, void *key, void *value);
+int bpf_delete_elem(int fd, void *key);
+int bpf_get_next_key(int fd, void *key, void *next_key);
+
+struct bpf_map_fixup {
+ int insn_idx;
+ int fd;
+};
+int bpf_prog_load(enum bpf_prog_type prog_type,
+ const struct bpf_insn *insns, int insn_len,
+ const char *license,
+ const struct bpf_map_fixup *fixups, int fixup_len);
+
+#endif
--
1.7.9.5
next prev parent reply other threads:[~2014-07-18 4:20 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-18 4:19 [PATCH RFC v2 net-next 00/16] BPF syscall, maps, verifier, samples Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 03/16] net: filter: rename struct sock_filter_int into bpf_insn Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 04/16] net: filter: split filter.h and expose eBPF to user space Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 05/16] bpf: introduce syscall(BPF, ...) and BPF maps Alexei Starovoitov
[not found] ` <1405657206-12060-6-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/w@public.gmane.org>
2014-07-23 18:02 ` Kees Cook
2014-07-23 19:30 ` Alexei Starovoitov
[not found] ` <1405657206-12060-1-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/w@public.gmane.org>
2014-07-18 4:19 ` [PATCH RFC v2 net-next 01/16] net: filter: split filter.c into two files Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 02/16] bpf: update MAINTAINERS entry Alexei Starovoitov
2014-07-23 17:37 ` Kees Cook
[not found] ` <CAGXu5j+mGDEPx5rCdVXzAdxR9SwQQ4ERM-Brxt4TmmncdPjzzg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-23 17:48 ` Alexei Starovoitov
[not found] ` <CAMEtUuzcMcYnRryykqb9LQWjnVmYi3ErMtGQ+9nyY5uS+OpwpQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-23 18:39 ` Kees Cook
2014-07-18 4:19 ` [PATCH RFC v2 net-next 06/16] bpf: enable bpf syscall on x64 Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 07/16] bpf: add lookup/update/delete/iterate methods to BPF maps Alexei Starovoitov
2014-07-23 18:25 ` Kees Cook
2014-07-23 19:49 ` Alexei Starovoitov
2014-07-23 20:25 ` Kees Cook
2014-07-23 21:22 ` Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 08/16] bpf: add hashtable type of " Alexei Starovoitov
[not found] ` <1405657206-12060-9-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/w@public.gmane.org>
2014-07-23 18:36 ` Kees Cook
[not found] ` <CAGXu5jK1hrwvPs7f+mSOer+81J9SBxwMrqb=6fFeZQ7hd-FMzA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-23 19:57 ` Alexei Starovoitov
[not found] ` <CAMEtUuyRUG6ZdRvL0JuakD0zPdYo3WoqQepK7_b_MW+r2aVzVA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-23 20:33 ` Kees Cook
2014-07-23 21:42 ` Alexei Starovoitov
2014-07-18 4:19 ` [PATCH RFC v2 net-next 09/16] bpf: expand BPF syscall with program load/unload Alexei Starovoitov
[not found] ` <1405657206-12060-10-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/w@public.gmane.org>
2014-07-23 19:00 ` Kees Cook
[not found] ` <CAGXu5j+gMepR-euMhBjYqMVHsdJM-Do5yc0rLw5dMK-C9BBieA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-23 20:22 ` Alexei Starovoitov
2014-07-18 4:20 ` [PATCH RFC v2 net-next 10/16] bpf: add eBPF verifier Alexei Starovoitov
[not found] ` <1405657206-12060-11-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/w@public.gmane.org>
2014-07-23 23:38 ` Kees Cook
2014-07-24 0:48 ` Alexei Starovoitov
2014-07-24 18:25 ` Andy Lutomirski
[not found] ` <CALCETrUt1yLXTt5pDZJd0s4s+4dhs0LB7tbJYd6p1+EbdNWw7w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-24 19:25 ` Alexei Starovoitov
[not found] ` <CAMEtUuygJoH+PBNksTwRHyRObCZixoTDFzmnHroErZHZLaGNbg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-08-12 19:32 ` Andy Lutomirski
[not found] ` <CALCETrUDb6kLH_Qp=YMm=m3P_hmEu3dfDTse=ptCsdpO9EEcAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-08-12 20:00 ` Alexei Starovoitov
[not found] ` <CAMEtUuz2DS8Ks0L15K0Jsj_nFjgvDh0TBwb+5pHzrdU+o6co_g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-08-12 20:10 ` Andy Lutomirski
[not found] ` <CALCETrUV+CSJzBzec7fRc6k4yDd+kCxz6Zi8zGNOQvACoKkP9A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-08-12 20:43 ` Alexei Starovoitov
2014-07-18 4:20 ` [PATCH RFC v2 net-next 11/16] bpf: allow eBPF programs to use maps Alexei Starovoitov
2014-07-18 4:20 ` [PATCH RFC v2 net-next 12/16] net: sock: allow eBPF programs to be attached to sockets Alexei Starovoitov
2014-07-18 4:20 ` [PATCH RFC v2 net-next 13/16] tracing: allow eBPF programs to be attached to events Alexei Starovoitov
2014-07-23 23:46 ` Kees Cook
[not found] ` <CAGXu5jJyw19h0+AwZEWSJt7hGnkRHAsf8fYim=XLzA-LH=svjw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2014-07-24 0:06 ` Alexei Starovoitov
2014-07-18 4:20 ` Alexei Starovoitov [this message]
2014-07-18 4:20 ` [PATCH RFC v2 net-next 15/16] samples: bpf: example of stateful socket filtering Alexei Starovoitov
2014-07-18 4:20 ` [PATCH RFC v2 net-next 16/16] samples: bpf: example of tracing filters with eBPF Alexei Starovoitov
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=1405657206-12060-15-git-send-email-ast@plumgrid.com \
--to=ast@plumgrid.com \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@infradead.org \
--cc=akpm@linux-foundation.org \
--cc=chema@google.com \
--cc=davem@davemloft.net \
--cc=dborkman@redhat.com \
--cc=edumazet@google.com \
--cc=hpa@zytor.com \
--cc=jolsa@redhat.com \
--cc=keescook@chromium.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=mingo@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
/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).