27 Jan, 2020

1 commit

  • tools/testing/selftests/bpf/Makefile supports overriding clang, llc and
    other tools so that custom ones can be used instead of those from PATH.
    It's convinient and heavily used by some users.

    Apply same rules to runqslower/Makefile.

    Signed-off-by: Andrey Ignatov
    Signed-off-by: Daniel Borkmann
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200124224142.1833678-1-rdna@fb.com

    Andrey Ignatov
     

24 Jan, 2020

1 commit


23 Jan, 2020

1 commit

  • Alexei Starovoitov says:

    ====================
    pull-request: bpf-next 2020-01-22

    The following pull-request contains BPF updates for your *net-next* tree.

    We've added 92 non-merge commits during the last 16 day(s) which contain
    a total of 320 files changed, 7532 insertions(+), 1448 deletions(-).

    The main changes are:

    1) function by function verification and program extensions from Alexei.

    2) massive cleanup of selftests/bpf from Toke and Andrii.

    3) batched bpf map operations from Brian and Yonghong.

    4) tcp congestion control in bpf from Martin.

    5) bulking for non-map xdp_redirect form Toke.

    6) bpf_send_signal_thread helper from Yonghong.
    ====================

    Signed-off-by: David S. Miller

    David S. Miller
     

21 Jan, 2020

5 commits

  • This adds support for specifying the libbpf include and object paths as
    arguments to the runqslower Makefile, to support reusing the libbpf version
    built as part of the selftests.

    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/157952561135.1683545.5660339645093141381.stgit@toke.dk

    Toke Høiland-Jørgensen
     
  • Since we are now consistently using the bpf/ prefix on #include directives,
    we don't need to include tools/lib/bpf in the include path. Remove it to
    make sure we don't inadvertently introduce new includes without the prefix.

    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/157952561027.1683545.1976265477926794138.stgit@toke.dk

    Toke Høiland-Jørgensen
     
  • Fix bpftool to include libbpf header files with the bpf/ prefix, to be
    consistent with external users of the library. Also ensure that all
    includes of exported libbpf header files (those that are exported on 'make
    install' of the library) use bracketed includes instead of quoted.

    To make sure no new files are introduced that doesn't include the bpf/
    prefix in its include, remove tools/lib/bpf from the include path entirely,
    and use tools/lib instead.

    Fixes: 6910d7d3867a ("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/157952560684.1683545.4765181397974997027.stgit@toke.dk

    Toke Høiland-Jørgensen
     
  • Fix the runqslower tool to include libbpf header files with the bpf/
    prefix, to be consistent with external users of the library. Also ensure
    that all includes of exported libbpf header files (those that are exported
    on 'make install' of the library) use bracketed includes instead of quoted.

    To not break the build, keep the old include path until everything has been
    changed to the new one; a subsequent patch will remove that.

    Fixes: 6910d7d3867a ("selftests/bpf: Ensure bpf_helper_defs.h are taken from selftests dir")
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/157952560457.1683545.9913736511685743625.stgit@toke.dk

    Toke Høiland-Jørgensen
     
  • The runqslower tool refuses to build without a file to read vmlinux BTF
    from. The build fails with an error message to override the location by
    setting the VMLINUX_BTF variable if autodetection fails. However, the
    Makefile doesn't actually work with that override - the error message is
    still emitted.

    Fix this by including the value of VMLINUX_BTF in the expansion, and only
    emitting the error message if the *result* is empty. Also permit running
    'make clean' even though no VMLINUX_BTF is set.

    Fixes: 9c01546d26d2 ("tools/bpf: Add runqslower tool to tools/bpf")
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/157952560237.1683545.17771785178857224877.stgit@toke.dk

    Toke Høiland-Jørgensen
     

20 Jan, 2020

1 commit


16 Jan, 2020

4 commits

  • This patch makes bpftool support dumping a map's value properly
    when the map's value type is a type of the running kernel's btf.
    (i.e. map_info.btf_vmlinux_value_type_id is set instead of
    map_info.btf_value_type_id). The first usecase is for the
    BPF_MAP_TYPE_STRUCT_OPS.

    Signed-off-by: Martin KaFai Lau
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200115230044.1103008-1-kafai@fb.com

    Martin KaFai Lau
     
  • This patch adds BPF_MAP_TYPE_STRUCT_OPS to "struct_ops" name mapping
    so that "bpftool map show" can print the "struct_ops" map type
    properly.

    [root@arch-fb-vm1 bpf]# ~/devshare/fb-kernel/linux/tools/bpf/bpftool/bpftool map show id 8
    8: struct_ops name dctcp flags 0x0
    key 4B value 256B max_entries 1 memlock 4096B
    btf_id 7

    Signed-off-by: Martin KaFai Lau
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200115230037.1102674-1-kafai@fb.com

    Martin KaFai Lau
     
  • The btf availability check is only done for plain text output.
    It causes the whole BTF output went missing when json_output
    is used.

    This patch simplifies the logic a little by avoiding passing "int btf" to
    map_dump().

    For plain text output, the btf_wtr is only created when the map has
    BTF (i.e. info->btf_id != 0). The nullness of "json_writer_t *wtr"
    in map_dump() alone can decide if dumping BTF output is needed.
    As long as wtr is not NULL, map_dump() will print out the BTF-described
    data whenever a map has BTF available (i.e. info->btf_id != 0)
    regardless of json or plain-text output.

    In do_dump(), the "int btf" is also renamed to "int do_plain_btf".

    Fixes: 99f9863a0c45 ("bpftool: Match maps by name")
    Signed-off-by: Martin KaFai Lau
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Cc: Paul Chaignon
    Link: https://lore.kernel.org/bpf/20200115230025.1101828-1-kafai@fb.com

    Martin KaFai Lau
     
  • When testing a map has btf or not, maps_have_btf() tests it by actually
    getting a btf_fd from sys_bpf(BPF_BTF_GET_FD_BY_ID). However, it
    forgot to btf__free() it.

    In maps_have_btf() stage, there is no need to test it by really
    calling sys_bpf(BPF_BTF_GET_FD_BY_ID). Testing non zero
    info.btf_id is good enough.

    Also, the err_close case is unnecessary, and also causes double
    close() because the calling func do_dump() will close() all fds again.

    Fixes: 99f9863a0c45 ("bpftool: Match maps by name")
    Signed-off-by: Martin KaFai Lau
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Cc: Paul Chaignon
    Link: https://lore.kernel.org/bpf/20200115230019.1101352-1-kafai@fb.com

    Martin KaFai Lau
     

15 Jan, 2020

1 commit


14 Jan, 2020

2 commits

  • Convert one of BCC tools (runqslower [0]) to BPF CO-RE + libbpf. It matches
    its BCC-based counterpart 1-to-1, supporting all the same parameters and
    functionality.

    runqslower tool utilizes BPF skeleton, auto-generated from BPF object file,
    as well as memory-mapped interface to global (read-only, in this case) data.
    Its Makefile also ensures auto-generation of "relocatable" vmlinux.h, which is
    necessary for BTF-typed raw tracepoints with direct memory access.

    [0] https://github.com/iovisor/bcc/blob/11bf5d02c895df9646c117c713082eb192825293/tools/runqslower.py

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200113073143.1779940-6-andriin@fb.com

    Andrii Nakryiko
     
  • This patch makes structs and unions, emitted through BTF dump, automatically
    CO-RE-relocatable (unless disabled with `#define BPF_NO_PRESERVE_ACCESS_INDEX`,
    specified before including generated header file).

    This effectivaly turns usual bpf_probe_read() call into equivalent of
    bpf_core_read(), by automatically applying builtin_preserve_access_index to
    any field accesses of types in generated C types header.

    This is especially useful for tp_btf/fentry/fexit BPF program types. They
    allow direct memory access, so BPF C code just uses straightfoward a->b->c
    access pattern to read data from kernel. But without kernel structs marked as
    CO-RE relocatable through preserve_access_index attribute, one has to enclose
    all the data reads into a special __builtin_preserve_access_index code block,
    like so:

    __builtin_preserve_access_index(({
    x = p->pid; /* where p is struct task_struct *, for example */
    }));

    This is very inconvenient and obscures the logic quite a bit. By marking all
    auto-generated types with preserve_access_index attribute the above code is
    reduced to just a clean and natural `x = p->pid;`.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200113073143.1779940-5-andriin@fb.com

    Andrii Nakryiko
     

12 Jan, 2020

1 commit

  • For plain text output, it incorrectly prints the pointer value
    "void *data". The "void *data" is actually pointing to memory that
    contains a bpf-map's value. The intention is to print the content of
    the bpf-map's value instead of printing the pointer pointing to the
    bpf-map's value.

    In this case, a member of the bpf-map's value is a pointer type.
    Thus, it should print the "*(void **)data".

    Fixes: 22c349e8db89 ("tools: bpftool: fix format strings and arguments for jsonw_printf()")
    Signed-off-by: Martin KaFai Lau
    Signed-off-by: Alexei Starovoitov
    Reviewed-by: Quentin Monnet
    Link: https://lore.kernel.org/bpf/20200110231644.3484151-1-kafai@fb.com

    Martin KaFai Lau
     

09 Jan, 2020

1 commit

  • Introduce a new probe section (misc) for probes not related to concrete
    map types, program types, functions or kernel configuration. Introduce a
    probe for large INSN limit as the first one in that section.

    Example outputs:

    # bpftool feature probe
    [...]
    Scanning miscellaneous eBPF features...
    Large program size limit is available

    # bpftool feature probe macros
    [...]
    /*** eBPF misc features ***/
    #define HAVE_HAVE_LARGE_INSN_LIMIT

    # bpftool feature probe -j | jq '.["misc"]'
    {
    "have_large_insn_limit": true
    }

    Signed-off-by: Michal Rostecki
    Signed-off-by: Daniel Borkmann
    Reviewed-by: Quentin Monnet
    Link: https://lore.kernel.org/bpf/20200108162428.25014-3-mrostecki@opensuse.org

    Michal Rostecki
     

28 Dec, 2019

1 commit

  • Daniel Borkmann says:

    ====================
    pull-request: bpf-next 2019-12-27

    The following pull-request contains BPF updates for your *net-next* tree.

    We've added 127 non-merge commits during the last 17 day(s) which contain
    a total of 110 files changed, 6901 insertions(+), 2721 deletions(-).

    There are three merge conflicts. Conflicts and resolution looks as follows:

    1) Merge conflict in net/bpf/test_run.c:

    There was a tree-wide cleanup c593642c8be0 ("treewide: Use sizeof_field() macro")
    which gets in the way with b590cb5f802d ("bpf: Switch to offsetofend in
    BPF_PROG_TEST_RUN"):

    <<<<<<< HEAD
    if (!range_is_zero(__skb, offsetof(struct __sk_buff, priority) +
    sizeof_field(struct __sk_buff, priority),
    =======
    if (!range_is_zero(__skb, offsetofend(struct __sk_buff, priority),
    >>>>>>> 7c8dce4b166113743adad131b5a24c4acc12f92c

    There are a few occasions that look similar to this. Always take the chunk with
    offsetofend(). Note that there is one where the fields differ in here:

    <<<<<<< HEAD
    if (!range_is_zero(__skb, offsetof(struct __sk_buff, tstamp) +
    sizeof_field(struct __sk_buff, tstamp),
    =======
    if (!range_is_zero(__skb, offsetofend(struct __sk_buff, gso_segs),
    >>>>>>> 7c8dce4b166113743adad131b5a24c4acc12f92c

    Just take the one with offsetofend() /and/ gso_segs. Latter is correct due to
    850a88cc4096 ("bpf: Expose __sk_buff wire_len/gso_segs to BPF_PROG_TEST_RUN").

    2) Merge conflict in arch/riscv/net/bpf_jit_comp.c:

    (I'm keeping Bjorn in Cc here for a double-check in case I got it wrong.)

    <<<<<<< HEAD
    if (is_13b_check(off, insn))
    return -1;
    emit(rv_blt(tcc, RV_REG_ZERO, off >> 1), ctx);
    =======
    emit_branch(BPF_JSLT, RV_REG_T1, RV_REG_ZERO, off, ctx);
    >>>>>>> 7c8dce4b166113743adad131b5a24c4acc12f92c

    Result should look like:

    emit_branch(BPF_JSLT, tcc, RV_REG_ZERO, off, ctx);

    3) Merge conflict in arch/riscv/include/asm/pgtable.h:

    <<<<<<< HEAD
    =======
    #define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1)
    #define VMALLOC_END (PAGE_OFFSET - 1)
    #define VMALLOC_START (PAGE_OFFSET - VMALLOC_SIZE)

    #define BPF_JIT_REGION_SIZE (SZ_128M)
    #define BPF_JIT_REGION_START (PAGE_OFFSET - BPF_JIT_REGION_SIZE)
    #define BPF_JIT_REGION_END (VMALLOC_END)

    /*
    * Roughly size the vmemmap space to be large enough to fit enough
    * struct pages to map half the virtual address space. Then
    * position vmemmap directly below the VMALLOC region.
    */
    #define VMEMMAP_SHIFT \
    (CONFIG_VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT)
    #define VMEMMAP_SIZE BIT(VMEMMAP_SHIFT)
    #define VMEMMAP_END (VMALLOC_START - 1)
    #define VMEMMAP_START (VMALLOC_START - VMEMMAP_SIZE)

    #define vmemmap ((struct page *)VMEMMAP_START)

    >>>>>>> 7c8dce4b166113743adad131b5a24c4acc12f92c

    Only take the BPF_* defines from there and move them higher up in the
    same file. Remove the rest from the chunk. The VMALLOC_* etc defines
    got moved via 01f52e16b868 ("riscv: define vmemmap before pfn_to_page
    calls"). Result:

    [...]
    #define __S101 PAGE_READ_EXEC
    #define __S110 PAGE_SHARED_EXEC
    #define __S111 PAGE_SHARED_EXEC

    #define VMALLOC_SIZE (KERN_VIRT_SIZE >> 1)
    #define VMALLOC_END (PAGE_OFFSET - 1)
    #define VMALLOC_START (PAGE_OFFSET - VMALLOC_SIZE)

    #define BPF_JIT_REGION_SIZE (SZ_128M)
    #define BPF_JIT_REGION_START (PAGE_OFFSET - BPF_JIT_REGION_SIZE)
    #define BPF_JIT_REGION_END (VMALLOC_END)

    /*
    * Roughly size the vmemmap space to be large enough to fit enough
    * struct pages to map half the virtual address space. Then
    * position vmemmap directly below the VMALLOC region.
    */
    #define VMEMMAP_SHIFT \
    (CONFIG_VA_BITS - PAGE_SHIFT - 1 + STRUCT_PAGE_MAX_SHIFT)
    #define VMEMMAP_SIZE BIT(VMEMMAP_SHIFT)
    #define VMEMMAP_END (VMALLOC_START - 1)
    #define VMEMMAP_START (VMALLOC_START - VMEMMAP_SIZE)

    [...]

    Let me know if there are any other issues.

    Anyway, the main changes are:

    1) Extend bpftool to produce a struct (aka "skeleton") tailored and specific
    to a provided BPF object file. This provides an alternative, simplified API
    compared to standard libbpf interaction. Also, add libbpf extern variable
    resolution for .kconfig section to import Kconfig data, from Andrii Nakryiko.

    2) Add BPF dispatcher for XDP which is a mechanism to avoid indirect calls by
    generating a branch funnel as discussed back in bpfconf'19 at LSF/MM. Also,
    add various BPF riscv JIT improvements, from Björn Töpel.

    3) Extend bpftool to allow matching BPF programs and maps by name,
    from Paul Chaignon.

    4) Support for replacing cgroup BPF programs attached with BPF_F_ALLOW_MULTI
    flag for allowing updates without service interruption, from Andrey Ignatov.

    5) Cleanup and simplification of ring access functions for AF_XDP with a
    bonus of 0-5% performance improvement, from Magnus Karlsson.

    6) Enable BPF JITs for x86-64 and arm64 by default. Also, final version of
    audit support for BPF, from Daniel Borkmann and latter with Jiri Olsa.

    7) Move and extend test_select_reuseport into BPF program tests under
    BPF selftests, from Jakub Sitnicki.

    8) Various BPF sample improvements for xdpsock for customizing parameters
    to set up and benchmark AF_XDP, from Jay Jayatheerthan.

    9) Improve libbpf to provide a ulimit hint on permission denied errors.
    Also change XDP sample programs to attach in driver mode by default,
    from Toke Høiland-Jørgensen.

    10) Extend BPF test infrastructure to allow changing skb mark from tc BPF
    programs, from Nikita V. Shirokov.

    11) Optimize prologue code sequence in BPF arm32 JIT, from Russell King.

    12) Fix xdp_redirect_cpu BPF sample to manually attach to tracepoints after
    libbpf conversion, from Jesper Dangaard Brouer.

    13) Minor misc improvements from various others.
    ====================

    Signed-off-by: David S. Miller

    David S. Miller
     

27 Dec, 2019

1 commit

  • When auto-generated BPF skeleton C code is included from C++ application, it
    triggers compilation error due to void * being implicitly casted to whatever
    target pointer type. This is supported by C, but not C++. To solve this
    problem, add explicit casts, where necessary.

    To ensure issues like this are captured going forward, add skeleton usage in
    test_cpp test.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20191226210253.3132060-1-andriin@fb.com

    Andrii Nakryiko
     

26 Dec, 2019

1 commit

  • Currently, when bpftool cgroup show has an error, no error
    message is printed. This is confusing because the user may think the
    result is empty.

    Before the change:

    $ bpftool cgroup show /sys/fs/cgroup
    ID AttachType AttachFlags Name
    $ echo $?
    255

    After the change:
    $ ./bpftool cgroup show /sys/fs/cgroup
    Error: can't query bpf programs attached to /sys/fs/cgroup: Operation
    not permitted

    v2: Rename check_query_cgroup_progs to cgroup_has_attached_progs

    Signed-off-by: Hechao Li
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20191224011742.3714301-1-hechaol@fb.com

    Hechao Li
     

19 Dec, 2019

3 commits

  • Move Kconfig-provided externs into custom .kconfig section. Add __kconfig into
    bpf_helpers.h for user convenience. Update selftests accordingly.

    Suggested-by: Daniel Borkmann
    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20191219002837.3074619-2-andriin@fb.com

    Andrii Nakryiko
     
  • Work-around what appears to be a bug in rst2man convertion tool, used to
    create man pages out of reStructureText-formatted documents. If text line
    starts with dot, rst2man will put it in resulting man file verbatim. This
    seems to cause man tool to interpret it as a directive/command (e.g., `.bs`), and
    subsequently not render entire line because it's unrecognized one.

    Enclose '.xxx' words in extra formatting to work around.

    Fixes: cb21ac588546 ("bpftool: Add gen subcommand manpage")
    Reported-by: Alexei Starovoitov
    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song <yhs@fb.com
    Link: https://lore.kernel.org/bpf/20191218221707.2552199-1-andriin@fb.com

    Andrii Nakryiko
     
  • Change format string referring to just single argument out of two available.
    Some versions of libc can reject such format string.

    Reported-by: Nikita Shirokov
    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song
    Link: https://lore.kernel.org/bpf/20191218214314.2403729-1-andriin@fb.com

    Andrii Nakryiko
     

18 Dec, 2019

2 commits

  • Add bpftool-gen.rst describing skeleton on the high level. Also include
    a small, but complete, example BPF app (BPF side, userspace side, generated
    skeleton) in example section to demonstrate skeleton API and its usage.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song
    Link: https://lore.kernel.org/bpf/20191218052552.2915188-4-andriin@fb.com

    Andrii Nakryiko
     
  • Embed contents of BPF object file used for BPF skeleton generation inside
    skeleton itself. This allows to keep BPF object file and its skeleton in sync
    at all times, and simpifies skeleton instantiation.

    Also switch existing selftests to not require BPF_EMBED_OBJ anymore.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song
    Link: https://lore.kernel.org/bpf/20191218052552.2915188-2-andriin@fb.com

    Andrii Nakryiko
     

16 Dec, 2019

8 commits

  • The ident variable has already been declared at the top of the function
    and doesn't need to be re-declared.

    Fixes: 985ead416df39 ("bpftool: Add skeleton codegen command")
    Signed-off-by: Paul Chaignon
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20191216112733.GA28366@Omicron

    Paul Chaignon
     
  • 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: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20191214014710.3449601-4-andriin@fb.com

    Andrii Nakryiko
     
  • Add BASH completions for gen sub-command.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Cc: Quentin Monnet
    Link: https://lore.kernel.org/bpf/20191214014341.3442258-18-andriin@fb.com

    Andrii Nakryiko
     
  • Add `bpftool gen skeleton` command, which takes in compiled BPF .o object file
    and dumps a BPF skeleton struct and related code to work with that skeleton.
    Skeleton itself is tailored to a specific structure of provided BPF object
    file, containing accessors (just plain struct fields) for every map and
    program, as well as dedicated space for bpf_links. If BPF program is using
    global variables, corresponding structure definitions of compatible memory
    layout are emitted as well, making it possible to initialize and subsequently
    read/update global variables values using simple and clear C syntax for
    accessing fields. This skeleton majorly improves usability of
    opening/loading/attaching of BPF object, as well as interacting with it
    throughout the lifetime of loaded BPF object.

    Generated skeleton struct has the following structure:

    struct {
    /* used by libbpf's skeleton API */
    struct bpf_object_skeleton *skeleton;
    /* bpf_object for libbpf APIs */
    struct bpf_object *obj;
    struct {
    /* for every defined map in BPF object: */
    struct bpf_map *;
    } maps;
    struct {
    /* for every program in BPF object: */
    struct bpf_program *;
    } progs;
    struct {
    /* for every program in BPF object: */
    struct bpf_link *;
    } links;
    /* for every present global data section: */
    struct __ {
    /* memory layout of corresponding data section,
    * with every defined variable represented as a struct field
    * with exactly the same type, but without const/volatile
    * modifiers, e.g.:
    */
    int *my_var_1;
    ...
    } *;
    };

    This provides great usability improvements:
    - no need to look up maps and programs by name, instead just
    my_obj->maps.my_map or my_obj->progs.my_prog would give necessary
    bpf_map/bpf_program pointers, which user can pass to existing libbpf APIs;
    - pre-defined places for bpf_links, which will be automatically populated for
    program types that libbpf knows how to attach automatically (currently
    tracepoints, kprobe/kretprobe, raw tracepoint and tracing programs). On
    tearing down skeleton, all active bpf_links will be destroyed (meaning BPF
    programs will be detached, if they are attached). For cases in which libbpf
    doesn't know how to auto-attach BPF program, user can manually create link
    after loading skeleton and they will be auto-detached on skeleton
    destruction:

    my_obj->links.my_fancy_prog = bpf_program__attach_cgroup_whatever(
    my_obj->progs.my_fancy_prog, rodata->my_var = 123;
    my_obj__load(skel); /* 123 will be initialization value for my_var */

    After load, if kernel supports mmap() for BPF arrays, user can still read
    (and write for .bss and .data) variables values, but at that point it will
    be directly mmap()-ed to BPF array, backing global variables. This allows to
    seamlessly exchange data with BPF side. From userspace program's POV, all
    the pointers and memory contents stay the same, but mapped kernel memory
    changes to point to created map.
    If kernel doesn't yet support mmap() for BPF arrays, it's still possible to
    use those data section structs to pre-initialize .bss, .data, and .rodata,
    but after load their pointers will be reset to NULL, allowing user code to
    gracefully handle this condition, if necessary.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20191214014341.3442258-14-andriin@fb.com

    Andrii Nakryiko
     
  • Few libbpf APIs are not public but currently exposed through libbpf.h to be
    used by bpftool. Move them to libbpf_internal.h, where intent of being
    non-stable and non-public is much more obvious.

    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20191214014341.3442258-4-andriin@fb.com

    Andrii Nakryiko
     
  • This patch implements lookup by name for maps and changes the behavior of
    lookups by tag to be consistent with prog subcommands. Similarly to
    program subcommands, the show and dump commands will return all maps with
    the given name (or tag), whereas other commands will error out if several
    maps have the same name (resp. tag).

    When a map has BTF info, it is dumped in JSON with available BTF info.
    This patch requires that all matched maps have BTF info before switching
    the output format to JSON.

    Signed-off-by: Paul Chaignon
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/8de1c9f273860b3ea1680502928f4da2336b853e.1576263640.git.paul.chaignon@gmail.com

    Paul Chaignon
     
  • When working with frequently modified BPF programs, both the ID and the
    tag may change. bpftool currently doesn't provide a "stable" way to match
    such programs.

    This patch implements lookup by name for programs. The show and dump
    commands will return all programs with the given name, whereas other
    commands will error out if several programs have the same name.

    Signed-off-by: Paul Chaignon
    Signed-off-by: Alexei Starovoitov
    Reviewed-by: Quentin Monnet
    Link: https://lore.kernel.org/bpf/b5fc1a5dcfaeb5f16fc80295cdaa606dd2d91534.1576263640.git.paul.chaignon@gmail.com

    Paul Chaignon
     
  • When several BPF programs have the same tag, bpftool matches only the
    first (in ID order). This patch changes that behavior such that dump and
    show commands return all matched programs. Commands that require a single
    program (e.g., pin and attach) will error out if given a tag that matches
    several. bpftool prog dump will also error out if file or visual are
    given and several programs have the given tag.

    In the case of the dump command, a program header is added before each
    dump only if the tag matches several programs; this patch doesn't change
    the output if a single program matches. The output when several
    programs match thus looks as follows.

    $ ./bpftool prog dump xlated tag 6deef7357e7b4530
    3: cgroup_skb tag 6deef7357e7b4530 gpl
    0: (bf) r6 = r1
    [...]
    7: (95) exit

    4: cgroup_skb tag 6deef7357e7b4530 gpl
    0: (bf) r6 = r1
    [...]
    7: (95) exit

    Signed-off-by: Paul Chaignon
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/fb1fe943202659a69cd21dd5b907c205af1e1e22.1576263640.git.paul.chaignon@gmail.com

    Paul Chaignon
     

11 Dec, 2019

1 commit

  • When the kptr_restrict sysctl is set, the kernel can fail to return
    jited_ksyms or jited_prog_insns, but still have positive values in
    nr_jited_ksyms and jited_prog_len. This causes bpftool to crash when
    trying to dump the program because it only checks the len fields not
    the actual pointers to the instructions and ksyms.

    Fix this by adding the missing checks.

    Fixes: 71bb428fe2c1 ("tools: bpf: add bpftool")
    Fixes: f84192ee00b7 ("tools: bpftool: resolve calls without using imm field")
    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Daniel Borkmann
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20191210181412.151226-1-toke@redhat.com

    Toke Høiland-Jørgensen
     

25 Nov, 2019

2 commits

  • Building selftests with 'make TARGETS=bpf kselftest' was fixed in commit
    55d554f5d140 ("tools: bpf: Use !building_out_of_srctree to determine
    srctree"). However, by updating $(srctree) in tools/bpf/Makefile for
    in-tree builds only, we leave out the case where we pass an output
    directory to build BPF tools, but $(srctree) is not set. This
    typically happens for:

    $ make -s tools/bpf O=/tmp/foo
    Makefile:40: /tools/build/Makefile.feature: No such file or directory

    Fix it by updating $(srctree) in the Makefile not only for out-of-tree
    builds, but also if $(srctree) is empty.

    Detected with test_bpftool_build.sh.

    Fixes: 55d554f5d140 ("tools: bpf: Use !building_out_of_srctree to determine srctree")
    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann
    Acked-by: Jakub Kicinski
    Link: https://lore.kernel.org/bpf/20191119105626.21453-1-quentin.monnet@netronome.com

    Quentin Monnet
     
  • When building bpftool, a warning was introduced by commit a94364603610
    ("bpftool: Allow to read btf as raw data"), because the return value
    from a call to 'read()' is ignored. Let's address it.

    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann
    Reviewed-by: Jakub Kicinski
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20191119111706.22440-1-quentin.monnet@netronome.com

    Quentin Monnet
     

07 Nov, 2019

1 commit

  • When compiling larger programs with bpf_asm, it's possible to
    accidentally exceed jt/jf range, in which case it won't complain, but
    rather silently emit a truncated offset, leading to a "happy debugging"
    situation.

    Add a warning to help detecting such issues. It could be made an error
    instead, but this might break compilation of existing code (which might
    be working by accident).

    Signed-off-by: Ilya Leoshkevich
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20191107100349.88976-1-iii@linux.ibm.com

    Ilya Leoshkevich
     

27 Oct, 2019

1 commit

  • Daniel Borkmann says:

    ====================
    pull-request: bpf-next 2019-10-27

    The following pull-request contains BPF updates for your *net-next* tree.

    We've added 52 non-merge commits during the last 11 day(s) which contain
    a total of 65 files changed, 2604 insertions(+), 1100 deletions(-).

    The main changes are:

    1) Revolutionize BPF tracing by using in-kernel BTF to type check BPF
    assembly code. The work here teaches BPF verifier to recognize
    kfree_skb()'s first argument as 'struct sk_buff *' in tracepoints
    such that verifier allows direct use of bpf_skb_event_output() helper
    used in tc BPF et al (w/o probing memory access) that dumps skb data
    into perf ring buffer. Also add direct loads to probe memory in order
    to speed up/replace bpf_probe_read() calls, from Alexei Starovoitov.

    2) Big batch of changes to improve libbpf and BPF kselftests. Besides
    others: generalization of libbpf's CO-RE relocation support to now
    also include field existence relocations, revamp the BPF kselftest
    Makefile to add test runner concept allowing to exercise various
    ways to build BPF programs, and teach bpf_object__open() and friends
    to automatically derive BPF program type/expected attach type from
    section names to ease their use, from Andrii Nakryiko.

    3) Fix deadlock in stackmap's build-id lookup on rq_lock(), from Song Liu.

    4) Allow to read BTF as raw data from bpftool. Most notable use case
    is to dump /sys/kernel/btf/vmlinux through this, from Jiri Olsa.

    5) Use bpf_redirect_map() helper in libbpf's AF_XDP helper prog which
    manages to improve "rx_drop" performance by ~4%., from Björn Töpel.

    6) Fix to restore the flow dissector after reattach BPF test and also
    fix error handling in bpf_helper_defs.h generation, from Jakub Sitnicki.

    7) Improve verifier's BTF ctx access for use outside of raw_tp, from
    Martin KaFai Lau.

    8) Improve documentation for AF_XDP with new sections and to reflect
    latest features, from Magnus Karlsson.

    9) Add back 'version' section parsing to libbpf for old kernels, from
    John Fastabend.

    10) Fix strncat bounds error in libbpf's libbpf_prog_type_by_name(),
    from KP Singh.

    11) Turn on -mattr=+alu32 in LLVM by default for BPF kselftests in order
    to improve insn coverage for built BPF progs, from Yonghong Song.

    12) Misc minor cleanups and fixes, from various others.
    ====================

    Signed-off-by: David S. Miller

    David S. Miller
     

26 Oct, 2019

1 commit

  • The bpftool interface stays the same, but now it's possible
    to run it over BTF raw data, like:

    $ bpftool btf dump file /sys/kernel/btf/vmlinux
    [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
    [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
    [3] CONST '(anon)' type_id=2

    Signed-off-by: Jiri Olsa
    Signed-off-by: Daniel Borkmann
    Acked-by: Andrii Nakryiko
    Acked-by: Jakub Kicinski
    Link: https://lore.kernel.org/bpf/20191024133025.10691-1-jolsa@kernel.org

    Jiri Olsa