1. 17 Dec, 2019 1 commit
  2. 16 Dec, 2019 17 commits
    • Andrii Nakryiko's avatar
      libbpf: Add zlib as a dependency in pkg-config template · dbd8f6ba
      Andrii Nakryiko authored
      List zlib as another dependency of libbpf in pkg-config template.
      Verified it is correctly resolved to proper -lz flag:
      
      $ make DESTDIR=/tmp/libbpf-install install
      $ pkg-config --libs /tmp/libbpf-install/usr/local/lib64/pkgconfig/libbpf.pc
      -L/usr/local/lib64 -lbpf
      $ pkg-config --libs --static /tmp/libbpf-install/usr/local/lib64/pkgconfig/libbpf.pc
      -L/usr/local/lib64 -lbpf -lelf -lz
      
      Fixes: 166750bc ("libbpf: Support libbpf-provided extern variables")
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Cc: Luca Boccassi <bluca@debian.org>
      Link: https://lore.kernel.org/bpf/20191216183830.3972964-1-andriin@fb.com
      dbd8f6ba
    • Toke Høiland-Jørgensen's avatar
      libbpf: Print hint about ulimit when getting permission denied error · dc3a2d25
      Toke Høiland-Jørgensen authored
      Probably the single most common error newcomers to XDP are stumped by is
      the 'permission denied' error they get when trying to load their program
      and 'ulimit -l' is set too low. For examples, see [0], [1].
      
      Since the error code is UAPI, we can't change that. Instead, this patch
      adds a few heuristics in libbpf and outputs an additional hint if they are
      met: If an EPERM is returned on map create or program load, and geteuid()
      shows we are root, and the current RLIMIT_MEMLOCK is not infinity, we
      output a hint about raising 'ulimit -l' as an additional log line.
      
      [0] https://marc.info/?l=xdp-newbies&m=157043612505624&w=2
      [1] https://github.com/xdp-project/xdp-tutorial/issues/86Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarJesper Dangaard Brouer <brouer@redhat.com>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/bpf/20191216181204.724953-1-toke@redhat.com
      dc3a2d25
    • Toke Høiland-Jørgensen's avatar
      samples/bpf: Attach XDP programs in driver mode by default · d50ecc46
      Toke Høiland-Jørgensen authored
      When attaching XDP programs, userspace can set flags to request the attach
      mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
      are requested, the kernel will attempt to attach in driver mode, and then
      silently fall back to SKB mode if this fails.
      
      The silent fallback is a major source of user confusion, as users will try
      to load a program on a device without XDP support, and instead of an error
      they will get the silent fallback behaviour, not notice, and then wonder
      why performance is not what they were expecting.
      
      In an attempt to combat this, let's switch all the samples to default to
      explicitly requesting driver-mode attach. As part of this, ensure that all
      the userspace utilities have a switch to enable SKB mode. For those that
      have a switch to request driver mode, keep it but turn it into a no-op.
      Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarJesper Dangaard Brouer <brouer@redhat.com>
      Acked-by: default avatarDavid Ahern <dsahern@gmail.com>
      Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
      d50ecc46
    • Toke Høiland-Jørgensen's avatar
      samples/bpf: Set -fno-stack-protector when building BPF programs · 45027897
      Toke Høiland-Jørgensen authored
      It seems Clang can in some cases turn on stack protection by default, which
      doesn't work with BPF. This was reported once before[0], but it seems the
      flag to explicitly turn off the stack protector wasn't added to the
      Makefile, so do that now.
      
      The symptom of this is compile errors like the following:
      
      error: <unknown>:0:0: in function bpf_prog1 i32 (%struct.__sk_buff*): A call to built-in function '__stack_chk_fail' is not supported.
      
      [0] https://www.spinics.net/lists/netdev/msg556400.htmlSigned-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191216103819.359535-1-toke@redhat.com
      45027897
    • Toke Høiland-Jørgensen's avatar
      samples/bpf: Add missing -lz to TPROGS_LDLIBS · 5615ed47
      Toke Høiland-Jørgensen authored
      Since libbpf now links against zlib, this needs to be included in the
      linker invocation for the userspace programs in samples/bpf that link
      statically against libbpf.
      
      Fixes: 166750bc ("libbpf: Support libbpf-provided extern variables")
      Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarBjörn Töpel <bjorn.topel@intel.com>
      Link: https://lore.kernel.org/bpf/20191216102405.353834-1-toke@redhat.com
      5615ed47
    • Prashant Bhole's avatar
      samples/bpf: Reintroduce missed build targets · 5984dc6c
      Prashant Bhole authored
      Add xdp_redirect and per_socket_stats_example in build targets.
      They got removed from build targets in Makefile reorganization.
      
      Fixes: 1d97c6c2 ("samples/bpf: Base target programs rules on Makefile.target")
      Signed-off-by: default avatarPrashant Bhole <prashantbhole.linux@gmail.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191216071619.25479-1-prashantbhole.linux@gmail.com
      5984dc6c
    • Paul Chaignon's avatar
      bpftool: Fix compilation warning on shadowed variable · 159ecc00
      Paul Chaignon authored
      The ident variable has already been declared at the top of the function
      and doesn't need to be re-declared.
      
      Fixes: 985ead41 ("bpftool: Add skeleton codegen command")
      Signed-off-by: default avatarPaul Chaignon <paul.chaignon@orange.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20191216112733.GA28366@Omicron
      159ecc00
    • Prashant Bhole's avatar
      libbpf: Fix build by renaming variables · a79ac2d1
      Prashant Bhole authored
      In btf__align_of() variable name 't' is shadowed by inner block
      declaration of another variable with same name. Patch renames
      variables in order to fix it.
      
        CC       sharedobjs/btf.o
      btf.c: In function ‘btf__align_of’:
      btf.c:303:21: error: declaration of ‘t’ shadows a previous local [-Werror=shadow]
        303 |   int i, align = 1, t;
            |                     ^
      btf.c:283:25: note: shadowed declaration is here
        283 |  const struct btf_type *t = btf__type_by_id(btf, id);
            |
      
      Fixes: 3d208f4c ("libbpf: Expose btf__align_of() API")
      Signed-off-by: default avatarPrashant Bhole <prashantbhole.linux@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Tested-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Link: https://lore.kernel.org/bpf/20191216082738.28421-1-prashantbhole.linux@gmail.com
      a79ac2d1
    • Alexei Starovoitov's avatar
      Merge branch 'support-flex-arrays' · 0849e102
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      Add support for flexible array accesses in a relocatable manner in BPF CO-RE.
      It's a typical pattern in C, and kernel in particular, to provide
      a fixed-length struct with zero-sized or dimensionless array at the end. In
      such cases variable-sized array contents follows immediately after the end of
      a struct. This patch set adds support for such access pattern by allowing
      accesses to such arrays.
      
      Patch #1 adds libbpf support. Patch #2 adds few test cases for validation.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0849e102
    • Andrii Nakryiko's avatar
      selftests/bpf: Add flexible array relocation tests · 5f2eecef
      Andrii Nakryiko authored
      Add few tests validation CO-RE relocation handling of flexible array accesses.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191215070844.1014385-3-andriin@fb.com
      5f2eecef
    • Andrii Nakryiko's avatar
      libbpf: Support flexible arrays in CO-RE · 1b484b30
      Andrii Nakryiko authored
      Some data stuctures in kernel are defined with either zero-sized array or
      flexible (dimensionless) array at the end of a struct. Actual data of such
      array follows in memory immediately after the end of that struct, forming its
      variable-sized "body" of elements. Support such access pattern in CO-RE
      relocation handling.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191215070844.1014385-2-andriin@fb.com
      1b484b30
    • Alexei Starovoitov's avatar
      Merge branch 'extern-var-support' · 01c6f7aa
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      It's often important for BPF program to know kernel version or some specific
      config values (e.g., CONFIG_HZ to convert jiffies to seconds) and change or
      adjust program logic based on their values. As of today, any such need has to
      be resolved by recompiling BPF program for specific kernel and kernel
      configuration. In practice this is usually achieved by using BCC and its
      embedded LLVM/Clang. With such set up #ifdef CONFIG_XXX and similar
      compile-time constructs allow to deal with kernel varieties.
      
      With CO-RE (Compile Once – Run Everywhere) approach, this is not an option,
      unfortunately. All such logic variations have to be done as a normal
      C language constructs (i.e., if/else, variables, etc), not a preprocessor
      directives. This patch series add support for such advanced scenarios through
      C extern variables. These extern variables will be recognized by libbpf and
      supplied through extra .extern internal map, similarly to global data. This
      .extern map is read-only, which allows BPF verifier to track its content
      precisely as constants. That gives an opportunity to have pre-compiled BPF
      program, which can potentially use BPF functionality (e.g., BPF helpers) or
      kernel features (types, fields, etc), that are available only on a subset of
      targeted kernels, while effectively eleminating (through verifier's dead code
      detection) such unsupported functionality for other kernels (typically, older
      versions). Patch #3 explicitly tests a scenario of using unsupported BPF
      helper, to validate the approach.
      
      This patch set heavily relies on BTF type information emitted by compiler for
      each extern variable declaration. Based on specific types, libbpf does strict
      checks of config data values correctness. See patch #1 for details.
      
      Outline of the patch set:
      - patch #1 does a small clean up of internal map names contants;
      - patch #2 adds all of the libbpf internal machinery for externs support,
        including setting up BTF information for .extern data section;
      - patch #3 adds support for .extern into BPF skeleton;
      - patch #4 adds externs selftests, as well as enhances test_skeleton.c test to
        validate mmap()-ed .extern datasection functionality.
      
      v3->v4:
      - clean up copyrights and rebase onto latest skeleton patches (Alexei);
      
      v2->v3:
      - truncate too long strings (Alexei);
      - clean ups, adding comments (Alexei);
      
      v1->v2:
      - use BTF type information for externs (Alexei);
      - add strings support;
      - add BPF skeleton support for .extern.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      01c6f7aa
    • Andrii Nakryiko's avatar
      selftests/bpf: Add tests for libbpf-provided externs · 330a73a7
      Andrii Nakryiko authored
      Add a set of tests validating libbpf-provided extern variables. One crucial
      feature that's tested is dead code elimination together with using invalid BPF
      helper. CONFIG_MISSING is not supposed to exist and should always be specified
      by libbpf as zero, which allows BPF verifier to correctly do branch pruning
      and not fail validation, when invalid BPF helper is called from dead if branch.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191214014710.3449601-5-andriin@fb.com
      330a73a7
    • Andrii Nakryiko's avatar
      bpftool: Generate externs datasec in BPF skeleton · 2ad97d47
      Andrii Nakryiko authored
      Add support for generation of mmap()-ed read-only view of libbpf-provided
      extern variables. As externs are not supposed to be provided by user code
      (that's what .data, .bss, and .rodata is for), don't mmap() it initially. Only
      after skeleton load is performed, map .extern contents as read-only memory.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191214014710.3449601-4-andriin@fb.com
      2ad97d47
    • Andrii Nakryiko's avatar
      libbpf: Support libbpf-provided extern variables · 166750bc
      Andrii Nakryiko authored
      Add support for extern variables, provided to BPF program by libbpf. Currently
      the following extern variables are supported:
        - LINUX_KERNEL_VERSION; version of a kernel in which BPF program is
          executing, follows KERNEL_VERSION() macro convention, can be 4- and 8-byte
          long;
        - CONFIG_xxx values; a set of values of actual kernel config. Tristate,
          boolean, strings, and integer values are supported.
      
      Set of possible values is determined by declared type of extern variable.
      Supported types of variables are:
      - Tristate values. Are represented as `enum libbpf_tristate`. Accepted values
        are **strictly** 'y', 'n', or 'm', which are represented as TRI_YES, TRI_NO,
        or TRI_MODULE, respectively.
      - Boolean values. Are represented as bool (_Bool) types. Accepted values are
        'y' and 'n' only, turning into true/false values, respectively.
      - Single-character values. Can be used both as a substritute for
        bool/tristate, or as a small-range integer:
        - 'y'/'n'/'m' are represented as is, as characters 'y', 'n', or 'm';
        - integers in a range [-128, 127] or [0, 255] (depending on signedness of
          char in target architecture) are recognized and represented with
          respective values of char type.
      - Strings. String values are declared as fixed-length char arrays. String of
        up to that length will be accepted and put in first N bytes of char array,
        with the rest of bytes zeroed out. If config string value is longer than
        space alloted, it will be truncated and warning message emitted. Char array
        is always zero terminated. String literals in config have to be enclosed in
        double quotes, just like C-style string literals.
      - Integers. 8-, 16-, 32-, and 64-bit integers are supported, both signed and
        unsigned variants. Libbpf enforces parsed config value to be in the
        supported range of corresponding integer type. Integers values in config can
        be:
        - decimal integers, with optional + and - signs;
        - hexadecimal integers, prefixed with 0x or 0X;
        - octal integers, starting with 0.
      
      Config file itself is searched in /boot/config-$(uname -r) location with
      fallback to /proc/config.gz, unless config path is specified explicitly
      through bpf_object_open_opts' kernel_config_path option. Both gzipped and
      plain text formats are supported. Libbpf adds explicit dependency on zlib
      because of this, but this shouldn't be a problem, given libelf already depends
      on zlib.
      
      All detected extern variables, are put into a separate .extern internal map.
      It, similarly to .rodata map, is marked as read-only from BPF program side, as
      well as is frozen on load. This allows BPF verifier to track extern values as
      constants and perform enhanced branch prediction and dead code elimination.
      This can be relied upon for doing kernel version/feature detection and using
      potentially unsupported field relocations or BPF helpers in a CO-RE-based BPF
      program, while still having a single version of BPF program running on old and
      new kernels. Selftests are validating this explicitly for unexisting BPF
      helper.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191214014710.3449601-3-andriin@fb.com
      166750bc
    • Andrii Nakryiko's avatar
      libbpf: Extract internal map names into constants · ac9d1389
      Andrii Nakryiko authored
      Instead of duplicating string literals, keep them in one place and consistent.
      Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20191214014710.3449601-2-andriin@fb.com
      ac9d1389
    • Alexei Starovoitov's avatar
      Merge branch 'bpf-obj-skel' · f7c0bbf2
      Alexei Starovoitov authored
      Andrii Nakryiko says:
      
      ====================
      This patch set introduces an alternative and complimentary to existing libbpf
      API interface for working with BPF objects, maps, programs, and global data
      from userspace side. This approach is relying on code generation. bpftool
      produces a struct (a.k.a. skeleton) tailored and specific to provided BPF
      object file. It includes hard-coded fields and data structures for every map,
      program, link, and global data present.
      
      Altogether this approach significantly reduces amount of userspace boilerplate
      code required to open, load, attach, and work with BPF objects. It improves
      attach/detach story, by providing pre-allocated space for bpf_links, and
      ensuring they are properly detached on shutdown. It allows to do away with by
      name/title lookups of maps and programs, because libbpf's skeleton API, in
      conjunction with generated code from bpftool, is filling in hard-coded fields
      with actual pointers to corresponding struct bpf_map/bpf_program/bpf_link.
      
      Also, thanks to BPF array mmap() support, working with global data (variables)
      from userspace is now as natural as it is from BPF side: each variable is just
      a struct field inside skeleton struct. Furthermore, this allows to have
      a natural way for userspace to pre-initialize global data (including
      previously impossible to initialize .rodata) by just assigning values to the
      same per-variable fields. Libbpf will carefully take into account this
      initialization image, will use it to pre-populate BPF maps at creation time,
      and will re-mmap() BPF map's contents at exactly the same userspace memory
      address such that it can continue working with all the same pointers without
      any interruptions. If kernel doesn't support mmap(), global data will still be
      successfully initialized, but after map creation global data structures inside
      skeleton will be NULL-ed out. This allows userspace application to gracefully
      handle lack of mmap() support, if necessary.
      
      A bunch of selftests are also converted to using skeletons, demonstrating
      significant simplification of userspace part of test and reduction in amount
      of code necessary.
      
      v3->v4:
      - add OPTS_VALID check to btf_dump__emit_type_decl (Alexei);
      - expose skeleton as LIBBPF_API functions (Alexei);
      - copyright clean up, update internal map init refactor (Alexei);
      
      v2->v3:
      - make skeleton part of public API;
      - expose btf_dump__emit_type_decl and btf__align_of APIs;
      - move LIBBPF_API and DECLARE_LIBBPF_OPTS into libbpf_common.h for reuse;
      
      v1->v2:
      - checkpatch.pl and reverse Christmas tree styling (Jakub);
      - sanitize variable names to accomodate in-function static vars;
      
      rfc->v1:
      - runqslower moved out into separate patch set waiting for vmlinux.h
        improvements;
      - skeleton generation code deals with unknown internal maps more gracefully.
      ====================
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f7c0bbf2
  3. 15 Dec, 2019 22 commits