Commit Graph

1172 Commits

Author SHA1 Message Date
Andrii Nakryiko
553db8ba73 libbpf: add resizable non-thread safe internal hashmap
There is a need for fast point lookups inside libbpf for multiple use
cases (e.g., name resolution for BTF-to-C conversion, by-name lookups in
BTF for upcoming BPF CO-RE relocation support, etc). This patch
implements simple resizable non-thread safe hashmap using single linked
list chains.

Four different insert strategies are supported:
 - HASHMAP_ADD - only add key/value if key doesn't exist yet;
 - HASHMAP_SET - add key/value pair if key doesn't exist yet; otherwise,
   update value;
 - HASHMAP_UPDATE - update value, if key already exists; otherwise, do
   nothing and return -ENOENT;
 - HASHMAP_APPEND - always add key/value pair, even if key already exists.
   This turns hashmap into a multimap by allowing multiple values to be
   associated with the same key. Most useful read API for such hashmap is
   hashmap__for_each_key_entry() iteration. If hashmap__find() is still
   used, it will return last inserted key/value entry (first in a bucket
   chain).

For HASHMAP_SET and HASHMAP_UPDATE, old key/value pair is returned, so
that calling code can handle proper memory management, if necessary.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-29 10:27:27 -07:00
Andrii Nakryiko
939da1eb3f libbpf: add btf__parse_elf API to load .BTF and .BTF.ext
Loading BTF and BTF.ext from ELF file is a common need. Instead of
requiring every user to re-implement it, let's provide this API from
libbpf itself. It's mostly copy/paste from `bpftool btf dump`
implementation, which will be switched to libbpf's version in next
patch. btf__parse_elf allows to load BTF and optionally BTF.ext.
This is also useful for tests that need to load/work with BTF, loaded
from test ELF files.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-29 10:27:27 -07:00
Andrii Nakryiko
d557b32f71 libbpf: ensure libbpf.h is included along libbpf_internal.h
libbpf_internal.h expects a bunch of stuff defined in libbpf.h to be
defined. This patch makes sure that libbpf.h is always included.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-29 10:27:27 -07:00
Andrii Nakryiko
e60460f4e5 linux/err.h: add PTR_ERR_OR_ZERO
Add missing PTR_ERR_OR_ZERO implementation. Also add a bunch of
generated files to .gitignore.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
2019-05-29 10:19:20 -07:00
Andrii Nakryiko
59a641543e sync: latest libbpf changes from kernel
Syncing latest libbpf commits from kernel repository.
Baseline commit:   35c99ffa20edd3c24be352d28a63cd3a23121282
Checkpoint commit: f49aa1de98363b6c5fba4637678d6b0ba3d18065

Andrii Nakryiko (1):
  libbpf: move logging helpers into libbpf_internal.h

Gary Lin (1):
  tools/bpf: Sync kernel btf.h header

Stanislav Fomichev (1):
  libbpf: don't fail when feature probing fails

 include/uapi/linux/btf.h |  2 +-
 src/btf.c                |  2 +-
 src/libbpf.c             |  3 +--
 src/libbpf_internal.h    | 13 +++++++++++++
 src/libbpf_util.h        | 13 -------------
 src/xsk.c                |  2 +-
 6 files changed, 17 insertions(+), 18 deletions(-)

--
2.17.1
2019-05-23 15:09:13 -07:00
Andrii Nakryiko
51dc322283 libbpf: move logging helpers into libbpf_internal.h
libbpf_util.h header was recently exposed as public as a dependency of
xsk.h. In addition to memory barriers, it contained logging helpers,
which are not supposed to be exposed. This patch moves those into
libbpf_internal.h, which is kept as an internal header.

Cc: Stanislav Fomichev <sdf@google.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Fixes: 7080da890984 ("libbpf: add libbpf_util.h to header install.")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-23 15:09:13 -07:00
Stanislav Fomichev
256cf5fa87 libbpf: don't fail when feature probing fails
Otherwise libbpf is unusable from unprivileged process with
kernel.kernel.unprivileged_bpf_disabled=1.
All I get is EPERM from the probes, even if I just want to
open an ELF object and look at what progs/maps it has.

Instead of dying on probes, let's just pr_debug the error and
try to continue.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-23 15:09:13 -07:00
Gary Lin
e10521b1a8 tools/bpf: Sync kernel btf.h header
For the fix of BTF_INT_OFFSET().

Signed-off-by: Gary Lin <glin@suse.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-23 15:09:13 -07:00
Eelco Chaudron
672ae75b66 AF_XDP: Fix install_headers to also install AF_XDP ones
Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
2019-05-21 08:56:23 -07:00
Andrii Nakryiko
5188b0ca5c sync: latest libbpf changes from kernel
Syncing latest libbpf commits from kernel repository.
Baseline commit:   ba02de1aa04e392e15ef503c6dd5166915d9d4de
Checkpoint commit: 35c99ffa20edd3c24be352d28a63cd3a23121282

Andrii Nakryiko (1):
  libbpf: detect supported kernel BTF features and sanitize BTF

Björn Töpel (3):
  libbpf: fix invalid munmap call
  libbpf: proper XSKMAP cleanup
  libbpf: remove unnecessary cast-to-void

Daniel Borkmann (2):
  bpf, libbpf: handle old kernels more graceful wrt global data sections
  bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement

Daniel T. Lee (1):
  libbpf: fix samples/bpf build failure due to undefined UINT32_MAX

Magnus Karlsson (1):
  libbpf: remove compile time warning from libbpf_util.h

Martin KaFai Lau (2):
  bpf: Sync bpf.h to tools
  bpf: Support BPF_MAP_TYPE_SK_STORAGE in bpf map probing

Matt Mullins (1):
  tools: sync bpf.h

McCabe, Robert J (1):
  libbpf: fix BPF_LOG_BUF_SIZE off-by-one error

Quentin Monnet (1):
  tools: bpf: synchronise BPF UAPI header with tools

Vineet Gupta (1):
  tools/bpf: fix perf build error with uClibc (seen on ARC)

 include/uapi/linux/bpf.h | 177 +++++++++++++++++++----------
 src/bpf.c                |   2 +
 src/bpf.h                |   3 +-
 src/libbpf.c             | 236 ++++++++++++++++++++++++++++++++++++---
 src/libbpf_internal.h    |  27 +++++
 src/libbpf_probes.c      |  84 +++++++++++++-
 src/libbpf_util.h        |   2 +-
 src/xsk.c                | 184 +++++++++++++++---------------
 8 files changed, 544 insertions(+), 171 deletions(-)
 create mode 100644 src/libbpf_internal.h

--
2.17.1
2019-05-16 09:40:04 -07:00
Andrii Nakryiko
c1cdd86d6b libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.

This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
  - BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
  - BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
  - BTF_KIND_VAR -> BTF_KIND_INT
  - BTF_KIND_DATASEC -> BTF_KIND_STRUCT

Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.

v2->v3:
  - remove duplicate #defines from libbpf_util.h

v1->v2:
  - add internal libbpf_internal.h w/ common stuff
  - switch SK storage BTF to use new libbpf__probe_raw_btf()

Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-16 09:40:04 -07:00
Quentin Monnet
785c526594 tools: bpf: synchronise BPF UAPI header with tools
Synchronise the bpf.h header under tools, to report the fixes and
additions recently brought to the documentation for the BPF helpers.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-16 09:40:04 -07:00
Björn Töpel
c1ad4efe09 libbpf: remove unnecessary cast-to-void
The patches with fixes tags added a cast-to-void in the places when
the return value of a function was ignored.

This is not common practice in the kernel, and is therefore removed in
this patch.

Reported-by: Daniel Borkmann <daniel@iogearbox.net>
Fixes: 5750902a6e9b ("libbpf: proper XSKMAP cleanup")
Fixes: 0e6741f09297 ("libbpf: fix invalid munmap call")
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-16 09:40:04 -07:00
Vineet Gupta
65cfbbcc87 tools/bpf: fix perf build error with uClibc (seen on ARC)
When build perf for ARC recently, there was a build failure due to lack
of __NR_bpf.

| Auto-detecting system features:
|
| ...                     get_cpuid: [ OFF ]
| ...                           bpf: [ on  ]
|
| #  error __NR_bpf not defined. libbpf does not support your arch.
    ^~~~~
| bpf.c: In function 'sys_bpf':
| bpf.c:66:17: error: '__NR_bpf' undeclared (first use in this function)
|  return syscall(__NR_bpf, cmd, attr, size);
|                 ^~~~~~~~
|                 sys_bpf

Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Björn Töpel
9ffd53d73e libbpf: proper XSKMAP cleanup
The bpf_map_update_elem() function, when used on an XSKMAP, will fail
if not a valid AF_XDP socket is passed as value. Therefore, this is
function cannot be used to clear the XSKMAP. Instead, the
bpf_map_delete_elem() function should be used for that.

This patch also simplifies the code by breaking up
xsk_update_bpf_maps() into three smaller functions.

Reported-by: William Tu <u9012063@gmail.com>
Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets")
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
Tested-by: William Tu <u9012063@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Björn Töpel
97f453307d libbpf: fix invalid munmap call
When unmapping the AF_XDP memory regions used for the rings, an
invalid address was passed to the munmap() calls. Instead of passing
the beginning of the memory region, the descriptor region was passed
to munmap.

When the userspace application tried to tear down an AF_XDP socket,
the operation failed and the application would still have a reference
to socket it wished to get rid of.

Reported-by: William Tu <u9012063@gmail.com>
Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets")
Signed-off-by: Björn Töpel <bjorn.topel@intel.com>
Tested-by: William Tu <u9012063@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Martin KaFai Lau
f4bf5c3edd bpf: Support BPF_MAP_TYPE_SK_STORAGE in bpf map probing
This patch supports probing for the new BPF_MAP_TYPE_SK_STORAGE.
BPF_MAP_TYPE_SK_STORAGE enforces BTF usage, so the new probe
requires to create and load a BTF also.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Martin KaFai Lau
e87112ab24 bpf: Sync bpf.h to tools
This patch sync the bpf.h to tools/.

Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Matt Mullins
540d704584 tools: sync bpf.h
This adds BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, and fixes up the

	error: enumeration value ‘BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE’ not handled in switch [-Werror=switch-enum]

build errors it would otherwise cause in libbpf.

Signed-off-by: Matt Mullins <mmullins@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Daniel T. Lee
c438cecc54 libbpf: fix samples/bpf build failure due to undefined UINT32_MAX
Currently, building bpf samples will cause the following error.

    ./tools/lib/bpf/bpf.h:132:27: error: 'UINT32_MAX' undeclared here (not in a function) ..
     #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
                               ^
    ./samples/bpf/bpf_load.h:31:25: note: in expansion of macro 'BPF_LOG_BUF_SIZE'
     extern char bpf_log_buf[BPF_LOG_BUF_SIZE];
                             ^~~~~~~~~~~~~~~~

Due to commit 4519efa6f8ea ("libbpf: fix BPF_LOG_BUF_SIZE off-by-one error")
hard-coded size of BPF_LOG_BUF_SIZE has been replaced with UINT32_MAX which is
defined in <stdint.h> header.

Even with this change, bpf selftests are running fine since these are built
with clang and it includes header(-idirafter) from clang/6.0.0/include.
(it has <stdint.h>)

    clang -I. -I./include/uapi -I../../../include/uapi -idirafter /usr/local/include -idirafter /usr/include \
    -idirafter /usr/lib/llvm-6.0/lib/clang/6.0.0/include -idirafter /usr/include/x86_64-linux-gnu \
    -Wno-compare-distinct-pointer-types -O2 -target bpf -emit-llvm -c progs/test_sysctl_prog.c -o - | \
    llc -march=bpf -mcpu=generic  -filetype=obj -o /linux/tools/testing/selftests/bpf/test_sysctl_prog.o

But bpf samples are compiled with GCC, and it only searches and includes
headers declared at the target file. As '#include <stdint.h>' hasn't been
declared in tools/lib/bpf/bpf.h, it causes build failure of bpf samples.

    gcc -Wp,-MD,./samples/bpf/.sockex3_user.o.d -Wall -Wmissing-prototypes -Wstrict-prototypes \
    -O2 -fomit-frame-pointer -std=gnu89 -I./usr/include -I./tools/lib/ -I./tools/testing/selftests/bpf/ \
    -I./tools/  lib/ -I./tools/include -I./tools/perf -c -o ./samples/bpf/sockex3_user.o ./samples/bpf/sockex3_user.c;

This commit add declaration of '#include <stdint.h>' to tools/lib/bpf/bpf.h
to fix this problem.

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-16 09:40:04 -07:00
Daniel Borkmann
72ef206260 bpf, libbpf: fix segfault in bpf_object__init_maps' pr_debug statement
Ran into it while testing; in bpf_object__init_maps() data can be NULL
in the case where no map section is present. Therefore we simply cannot
access data->d_size before NULL test. Move the pr_debug() where it's
safe to access.

Fixes: d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Daniel Borkmann
29e229ef14 bpf, libbpf: handle old kernels more graceful wrt global data sections
Andrii reported a corner case where e.g. global static data is present
in the BPF ELF file in form of .data/.bss/.rodata section, but without
any relocations to it. Such programs could be loaded before commit
d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections"),
whereas afterwards if kernel lacks support then loading would fail.

Add a probing mechanism which skips setting up libbpf internal maps
in case of missing kernel support. In presence of relocation entries,
we abort the load attempt.

Fixes: d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections")
Reported-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
McCabe, Robert J
0167179e6f libbpf: fix BPF_LOG_BUF_SIZE off-by-one error
The BPF_PROG_LOAD condition for kernel version <= 5.1 is

   log->len_total > UINT_MAX >> 8 /* (16 * 1024 * 1024) - 1 */

Signed-off-by: McCabe, Robert J <robert.mccabe@rockwellcollins.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Magnus Karlsson
7285545e22 libbpf: remove compile time warning from libbpf_util.h
Having a helpful compile time warning in libbpf_util.h is not a good
idea since all warnings are treated as errors. Change this into a
comment in the code instead.

Fixes: b7e3a28019c9 ("libbpf: remove dependency on barrier.h in xsk.h")
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-05-16 09:40:04 -07:00
Andrii Nakryiko
3a41dddce2 Fix up README's markdown formatting
After making Github recognize README.md as markdown text,
there are a bunch of formatting problems. They are fixed in this commit.

Singed-off-by: Andrii Nakryiko <andriin@fb.com>
2019-05-16 09:39:52 -07:00
Andrii Nakryiko
58d39f2af3 filter.h: add missing BPF raw instruction macros
These are used in some of the new commits from bpf-next.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
2019-05-15 21:17:43 -07:00
Nirmoy Das
90df021013 README: rename to README.md
file rename needed so that github detect it as md file to show
build status

Signed-off-by: Nirmoy Das <ndas@suse.de>
2019-05-14 08:29:03 -07:00
hex
816253000e Travis CI: add gcc-8, gcc-8 ASan+UBSan tests 2019-04-30 11:39:37 -07:00
hex
7ed7e4f0ad Embed Travis CI status image into libbpf README
Show the build status in a convenient way.
Follow the instructions:
https://docs.travis-ci.com/user/status-images/
2019-04-30 11:39:37 -07:00
hex
4de9ffcffe Travis CI folow up
Fix ENV_VARS passing to `docker` so `CC` is set to `clang`.
Fix asan compiler option.
Specify all compiler options in `CFLAGS`.

Related discussion https://github.com/libbpf/libbpf/pull/36#discussion_r276522335
2019-04-30 11:39:37 -07:00
Andrii Nakryiko
910c475f09 sync: latest libbpf changes from kernel
Syncing latest libbpf commits from kernel repository.
Baseline commit:   3daf8e703ec3dcf73a27a7dcabbac152793eb114
Checkpoint commit: ba02de1aa04e392e15ef503c6dd5166915d9d4de

Alan Maguire (2):
  bpf: sync bpf.h to tools/ for BPF_F_ADJ_ROOM_ENCAP_L2
  bpf: fix whitespace for ENCAP_L2 defines in bpf.h

Andrey Ignatov (3):
  bpf: Sync bpf.h to tools/
  libbpf: Support sysctl hook
  bpf: Sync bpf.h to tools/

Andrii Nakryiko (2):
  btf: add support for VAR and DATASEC in btf_dedup()
  libbpf: fix printf formatter for ptrdiff_t argument

Magnus Karlsson (4):
  libbpf: fix XDP socket ring buffer memory ordering
  libbpf: remove likely/unlikely in xsk.h
  libbpf: remove dependency on barrier.h in xsk.h
  libbpf: optimize barrier for XDP socket rings

 include/uapi/linux/bpf.h | 149 ++++++++++++++++++++++++++++++++++++++-
 src/btf.c                |  29 +++++++-
 src/libbpf.c             |   5 +-
 src/libbpf_probes.c      |   1 +
 src/libbpf_util.h        |  30 ++++++++
 src/xsk.h                |  22 ++++--
 6 files changed, 226 insertions(+), 10 deletions(-)

--
2.17.1
2019-04-19 09:52:37 -07:00
Magnus Karlsson
59c979cadc libbpf: optimize barrier for XDP socket rings
The full memory barrier in the XDP socket rings on the consumer side
between the load of the data and the store of the consumer ring is
there to protect the store from being executed before the load of the
data. If this was allowed to happen, the producer might overwrite the
data field with a new entry before the consumer got the chance to read
it.

On x86, stores are guaranteed not to be reordered with older loads, so
it does not need a full memory barrier here. A compile time barrier
would be enough. This patch introdcues a new primitive in
libbpf_util.h that implements a new barrier type (libbpf_smp_rwmb)
hindering stores to be reordered with older loads. It is then used in
the XDP socket ring access code in libbpf to improve performance.

Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Magnus Karlsson
b0404eeba8 libbpf: remove dependency on barrier.h in xsk.h
The use of smp_rmb() and smp_wmb() creates a Linux header dependency
on barrier.h that is unnecessary in most parts. This patch implements
the two small defines that are needed from barrier.h. As a bonus, the
new implementations are faster than the default ones as they default
to sfence and lfence for x86, while we only need a compiler barrier in
our case. Just as it is when the same ring access code is compiled in
the kernel.

Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets")
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Magnus Karlsson
b0abd1bc24 libbpf: remove likely/unlikely in xsk.h
This patch removes the use of likely and unlikely in xsk.h since they
create a dependency on Linux headers as reported by several
users. There have also been reports that the use of these decreases
performance as the compiler puts the code on two different cache lines
instead of on a single one. All in all, I think we are better off
without them.

Fixes: 1cad07884239 ("libbpf: add support for using AF_XDP sockets")
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Magnus Karlsson
01d7b7ebbf libbpf: fix XDP socket ring buffer memory ordering
The ring buffer code of	XDP sockets is missing a memory	barrier	on the
consumer side between the load of the data and the write that signals
that it is ok for the producer to put new data into the buffer. On
architectures that does not guarantee that stores are not reordered
with older loads, the producer might put data into the ring before the
consumer had the chance to read it. As IA does guarantee this
ordering, it would only need a compiler barrier here, but there are no
primitives in barrier.h for this specific case (hinder writes to be ordered
before older reads) so I had to add a smp_mb() here which will
translate into a run-time synch operation on IA.

Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Andrii Nakryiko
6afcd645c8 libbpf: fix printf formatter for ptrdiff_t argument
Using %ld for printing out value of ptrdiff_t type is not portable
between 32-bit and 64-bit archs. This is causing compilation errors for
libbpf on 32-bit platform (discovered as part of an effort to integrate
libbpf into systemd ([0])). Proper formatter is %td, which is used in
this patch.

v2->v1:
  - add Reported-by
  - provide more context on how this issue was discovered

[0] https://github.com/systemd/systemd/pull/12151

Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Alan Maguire
7ee515fed9 bpf: fix whitespace for ENCAP_L2 defines in bpf.h
replace tab after #define with space in line with rest of definitions

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-19 09:52:37 -07:00
Andrii Nakryiko
f0e76d6e9e btf: add support for VAR and DATASEC in btf_dedup()
This patch adds support for VAR and DATASEC in btf_dedup(). VAR/DATASEC
are never deduplicated, but they need to be processed anyway as types
they refer to might need to be remapped due to deduplication and
compaction.

Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Yonghong Song <yhs@fb.com>
Cc: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-19 09:52:37 -07:00
Andrey Ignatov
6be72ba83c bpf: Sync bpf.h to tools/
Sync bpf_strtoX related bpf UAPI changes to tools/.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Andrey Ignatov
6cf6bed724 libbpf: Support sysctl hook
Support BPF_PROG_TYPE_CGROUP_SYSCTL program in libbpf: identifying
program and attach types by section name, probe.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Andrey Ignatov
805f4593af bpf: Sync bpf.h to tools/
Sync BPF_PROG_TYPE_CGROUP_SYSCTL related bpf UAPI changes to tools/.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-19 09:52:37 -07:00
Alan Maguire
b18e6122b6 bpf: sync bpf.h to tools/ for BPF_F_ADJ_ROOM_ENCAP_L2
Sync include/uapi/linux/bpf.h with tools/ equivalent to add
BPF_F_ADJ_ROOM_ENCAP_L2(len) macro.

Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-19 09:52:37 -07:00
Andrii Nakryiko
61419aae26 sync-kernel.sh: detect synced commits, interactively resolve conflicts
1. There is one commit ("libbpf: fix btf_dedup equivalence check
   handling of different kinds"), which was originally committed into bpf
   tree and manually synced into libbpf (it was a critical bug fix). Now
   that bpf and bpf-next trees were merged, this causes duplicate commits in
   libbpf. To address this, check is added to see if libbpf repo already has
   a commit with the same subject and allow to skip it.
2. Just checking by subject is not good enough, as we currently have few
   different commits with the same subject "bpf: Sync bpf.h to tools/".
   To address this, script now will ask user whether to skip or cherry-pick
   commit. This allows for human to double-check the commit and make
   informed devision.
3. There is a commit ('bfb35c27c65f bpf: fix whitespace for ENCAP_L2
   defines in bpf.h'), which modifies both include/uapi/linux/bpf.h and
   tools/include/uapi/linux/bpf.h in the same commit. This break the
   sync script, as it doesn't cherry-pick commits that hasn't modified
   libbpf files. So at the time that commit bfb35c27c65f is applied, linux
   repo is missing some preceding changes to include/uapi/linux/bpf.h. They
   would normally be ignored later during repo filtering, but to do
   cherry-pick they are necessary. It's hard to fix it automatically, so
   script will wait for user to resolve the issue and continue.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
2019-04-18 17:00:48 -07:00
hex
2d1959ff60 Introduce Travis CI tests
In order to libbpf to be used in systemd some testing is required, see related
discussions in https://github.com/systemd/systemd/pull/12151 and
https://github.com/libbpf/libbpf/pull/29
The tests introduced here mirrors the tests of systemd:
For Debian: build with gcc, gcc + asan, clang, clang + asan
Debian tests use `docker` virtualization
Fror Ubuntu Xenial: build with gcc

The differences:
Install only libelf and it's dependencies.
Instead of Meson build system `make` is used, so `make` remains the preferred
method of building and `meson.build` doesn't get rooted in `libbpf`.

`travis_wait.bash` is kept as a workaround for
https://github.com/travis-ci/travis-ci/issues/9979

An example of testing UI: https://travis-ci.org/wat-ze-hex/libbpf
2019-04-17 14:19:40 -07:00
hex
65b4e9359a add meson.build for meson build system
Make it possible to include libbpf into projects built by Meson build system as
a subproject. This is the case e.g. for systemd.

meson.build declares dependencies which may be used in parent projects:
`libbpf_static_dep` for static library
`libbpf_shared_dep` for shared library
`bpf_includes` provides the location of bpf.h, btf.h and libbpf.h

Parent projects need to introduce git wrap to build libbpf:
https://github.com/mesonbuild/meson/blob/master/docs/markdown/Wrap-dependency-system-manual.md#wrap-git

An alternative approach is applying a patch with meson.build for libbpf while
building a parent project:
https://github.com/mesonbuild/meson/blob/master/docs/markdown/Wrap-dependency-system-manual.md#wrap-file
Imo it's less transparent and would add more headache.

meson.build is placed in the source root because that's how Meson expects it to
be placed.

The related discussion is in https://github.com/systemd/systemd/pull/12151
2019-04-15 21:39:11 -07:00
Daniel Borkmann
8bbb21c227 libbpf: fix extraversion in makefile to that of upstream
Backport in f49907472f ("sync: latest libbpf changes from kernel")
missed to bump Makefile's EXTRAVERSION to 3.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-12 08:19:53 -07:00
Daniel Borkmann
dd3fc8bde1 sync: latest tooling changes from kernel
[ upstream commit 6b7a21140fca461c6d8d5c65a3746e7da50a409e ]

Needed a custom backport. I've changed fallbacks so that we can
partially pull in barriers step by step. I've fixed x86 ones and
added corresponding arm64 barriers.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-11 15:55:59 -07:00
Yonghong Song
5844f6e4dd sync: latest libbpf changes from kernel
Syncing latest libbpf commits from kernel repository.
Baseline commit:   50bd645b3a21a374dbd0fa8273a5f4e98001fb05
Checkpoint commit: 3daf8e703ec3dcf73a27a7dcabbac152793eb114

Andrey Ignatov (1):
  libbpf: Fix build with gcc-8

Stanislav Fomichev (1):
  libbpf: add support for ctx_{size, }_{in, out} in BPF_PROG_TEST_RUN

 include/uapi/linux/bpf.h | 7 +++++++
 src/bpf.c                | 5 +++++
 src/bpf.h                | 5 +++++
 src/libbpf.c             | 2 +-
 4 files changed, 18 insertions(+), 1 deletion(-)

--
2.17.1
2019-04-11 09:01:12 -07:00
Stanislav Fomichev
e2871d1c75 libbpf: add support for ctx_{size, }_{in, out} in BPF_PROG_TEST_RUN
Support recently introduced input/output context for test runs.
We extend only bpf_prog_test_run_xattr. bpf_prog_test_run is
unextendable and left as is.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-11 09:01:12 -07:00
Andrey Ignatov
ac2254d0ea libbpf: Fix build with gcc-8
Reported in [1].

With gcc 8.3.0 the following error is issued:

  cc -Ibpf@sta -I. -I.. -I.././include -I.././include/uapi
  -fdiagnostics-color=always -fsanitize=address,undefined -fno-omit-frame-pointer
  -pipe -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Werror -g -fPIC -g -O2
  -Werror -Wall -Wno-pointer-arith -Wno-sign-compare  -MD -MQ
  'bpf@sta/src_libbpf.c.o' -MF 'bpf@sta/src_libbpf.c.o.d' -o
  'bpf@sta/src_libbpf.c.o' -c ../src/libbpf.c
  ../src/libbpf.c: In function 'bpf_object__elf_collect':
  ../src/libbpf.c:947:18: error: 'map_def_sz' may be used uninitialized in this
  function [-Werror=maybe-uninitialized]
     if (map_def_sz <= sizeof(struct bpf_map_def)) {
         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ../src/libbpf.c:827:18: note: 'map_def_sz' was declared here
    int i, map_idx, map_def_sz, nr_syms, nr_maps = 0, nr_maps_glob = 0;
                    ^~~~~~~~~~

According to [2] -Wmaybe-uninitialized is enabled by -Wall.
Same error is generated by clang's -Wconditional-uninitialized.

[1] https://github.com/libbpf/libbpf/pull/29#issuecomment-481902601
[2] https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Fixes: d859900c4c56 ("bpf, libbpf: support global data/bss/rodata sections")
Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Signed-off-by: Andrey Ignatov <rdna@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-04-11 09:01:12 -07:00