29 Oct, 2020

1 commit


22 Oct, 2020

1 commit

  • Based on the discussion in [0], update the bpf_redirect_neigh() helper to
    accept an optional parameter specifying the nexthop information. This makes
    it possible to combine bpf_fib_lookup() and bpf_redirect_neigh() without
    incurring a duplicate FIB lookup - since the FIB lookup helper will return
    the nexthop information even if no neighbour is present, this can simply
    be passed on to bpf_redirect_neigh() if bpf_fib_lookup() returns
    BPF_FIB_LKUP_RET_NO_NEIGH. Thus fix & extend it before helper API is frozen.

    [0] https://lore.kernel.org/bpf/393e17fc-d187-3a8d-2f0d-a627c7c63fca@iogearbox.net/

    Signed-off-by: Toke Høiland-Jørgensen
    Signed-off-by: Daniel Borkmann
    Reviewed-by: David Ahern
    Link: https://lore.kernel.org/bpf/160322915615.32199.1187570224032024535.stgit@toke.dk

    Toke Høiland-Jørgensen
     

29 Sep, 2020

1 commit

  • A helper is added to support tracing kernel type information in BPF
    using the BPF Type Format (BTF). Its signature is

    long bpf_snprintf_btf(char *str, u32 str_size, struct btf_ptr *ptr,
    u32 btf_ptr_size, u64 flags);

    struct btf_ptr * specifies

    - a pointer to the data to be traced
    - the BTF id of the type of data pointed to
    - a flags field is provided for future use; these flags
    are not to be confused with the BTF_F_* flags
    below that control how the btf_ptr is displayed; the
    flags member of the struct btf_ptr may be used to
    disambiguate types in kernel versus module BTF, etc;
    the main distinction is the flags relate to the type
    and information needed in identifying it; not how it
    is displayed.

    For example a BPF program with a struct sk_buff *skb
    could do the following:

    static struct btf_ptr b = { };

    b.ptr = skb;
    b.type_id = __builtin_btf_type_id(struct sk_buff, 1);
    bpf_snprintf_btf(str, sizeof(str), &b, sizeof(b), 0, 0);

    Default output looks like this:

    (struct sk_buff){
    .transport_header = (__u16)65535,
    .mac_header = (__u16)65535,
    .end = (sk_buff_data_t)192,
    .head = (unsigned char *)0x000000007524fd8b,
    .data = (unsigned char *)0x000000007524fd8b,
    .truesize = (unsigned int)768,
    .users = (refcount_t){
    .refs = (atomic_t){
    .counter = (int)1,
    },
    },
    }

    Flags modifying display are as follows:

    - BTF_F_COMPACT: no formatting around type information
    - BTF_F_NONAME: no struct/union member names/types
    - BTF_F_PTR_RAW: show raw (unobfuscated) pointer values;
    equivalent to %px.
    - BTF_F_ZERO: show zero-valued struct/union members;
    they are not displayed by default

    Signed-off-by: Alan Maguire
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/1601292670-1616-4-git-send-email-alan.maguire@oracle.com

    Alan Maguire
     

26 Aug, 2020

1 commit

  • Adding d_path helper function that returns full path for
    given 'struct path' object, which needs to be the kernel
    BTF 'path' object. The path is returned in buffer provided
    'buf' of size 'sz' and is zero terminated.

    bpf_d_path(&file->f_path, buf, size);

    The helper calls directly d_path function, so there's only
    limited set of function it can be called from. Adding just
    very modest set for the start.

    Updating also bpf.h tools uapi header and adding 'path' to
    bpf_helpers_doc.py script.

    Signed-off-by: Jiri Olsa
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Acked-by: KP Singh
    Link: https://lore.kernel.org/bpf/20200825192124.710397-11-jolsa@kernel.org

    Jiri Olsa
     

18 Jul, 2020

1 commit

  • Add a new program type BPF_PROG_TYPE_SK_LOOKUP with a dedicated attach type
    BPF_SK_LOOKUP. The new program kind is to be invoked by the transport layer
    when looking up a listening socket for a new connection request for
    connection oriented protocols, or when looking up an unconnected socket for
    a packet for connection-less protocols.

    When called, SK_LOOKUP BPF program can select a socket that will receive
    the packet. This serves as a mechanism to overcome the limits of what
    bind() API allows to express. Two use-cases driving this work are:

    (1) steer packets destined to an IP range, on fixed port to a socket

    192.0.2.0/24, port 80 -> NGINX socket

    (2) steer packets destined to an IP address, on any port to a socket

    198.51.100.1, any port -> L7 proxy socket

    In its run-time context program receives information about the packet that
    triggered the socket lookup. Namely IP version, L4 protocol identifier, and
    address 4-tuple. Context can be further extended to include ingress
    interface identifier.

    To select a socket BPF program fetches it from a map holding socket
    references, like SOCKMAP or SOCKHASH, and calls bpf_sk_assign(ctx, sk, ...)
    helper to record the selection. Transport layer then uses the selected
    socket as a result of socket lookup.

    In its basic form, SK_LOOKUP acts as a filter and hence must return either
    SK_PASS or SK_DROP. If the program returns with SK_PASS, transport should
    look for a socket to receive the packet, or use the one selected by the
    program if available, while SK_DROP informs the transport layer that the
    lookup should fail.

    This patch only enables the user to attach an SK_LOOKUP program to a
    network namespace. Subsequent patches hook it up to run on local delivery
    path in ipv4 and ipv6 stacks.

    Suggested-by: Marek Majkowski
    Signed-off-by: Jakub Sitnicki
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20200717103536.397595-3-jakub@cloudflare.com

    Jakub Sitnicki
     

01 Jul, 2020

1 commit

  • Introduce helper bpf_get_task_stack(), which dumps stack trace of given
    task. This is different to bpf_get_stack(), which gets stack track of
    current task. One potential use case of bpf_get_task_stack() is to call
    it from bpf_iter__task and dump all /proc//stack to a seq_file.

    bpf_get_task_stack() uses stack_trace_save_tsk() instead of
    get_perf_callchain() for kernel stack. The benefit of this choice is that
    stack_trace_save_tsk() doesn't require changes in arch/. The downside of
    using stack_trace_save_tsk() is that stack_trace_save_tsk() dumps the
    stack trace to unsigned long array. For 32-bit systems, we need to
    translate it to u64 array.

    Signed-off-by: Song Liu
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200630062846.664389-3-songliubraving@fb.com

    Song Liu
     

25 Jun, 2020

3 commits

  • The helper is used in tracing programs to cast a socket
    pointer to a udp6_sock pointer.
    The return value could be NULL if the casting is illegal.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Cc: Eric Dumazet
    Link: https://lore.kernel.org/bpf/20200623230815.3988481-1-yhs@fb.com

    Yonghong Song
     
  • Three more helpers are added to cast a sock_common pointer to
    an tcp_sock, tcp_timewait_sock or a tcp_request_sock for
    tracing programs.

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20200623230811.3988277-1-yhs@fb.com

    Yonghong Song
     
  • The helper is used in tracing programs to cast a socket
    pointer to a tcp6_sock pointer.
    The return value could be NULL if the casting is illegal.

    A new helper return type RET_PTR_TO_BTF_ID_OR_NULL is added
    so the verifier is able to deduce proper return types for the helper.

    Different from the previous BTF_ID based helpers,
    the bpf_skc_to_tcp6_sock() argument can be several possible
    btf_ids. More specifically, all possible socket data structures
    with sock_common appearing in the first in the memory layout.
    This patch only added socket types related to tcp and udp.

    All possible argument btf_id and return value btf_id
    for helper bpf_skc_to_tcp6_sock() are pre-calculcated and
    cached. In the future, it is even possible to precompute
    these btf_id's at kernel build time.

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

    Yonghong Song
     

12 May, 2020

1 commit

  • Minor improvements to the documentation for BPF helpers:

    * Fix formatting for the description of "bpf_socket" for
    bpf_getsockopt() and bpf_setsockopt(), thus suppressing two warnings
    from rst2man about "Unexpected indentation".
    * Fix formatting for return values for bpf_sk_assign() and seq_file
    helpers.
    * Fix and harmonise formatting, in particular for function/struct names.
    * Remove blank lines before "Return:" sections.
    * Replace tabs found in the middle of text lines.
    * Fix typos.
    * Add a note to the footer (in Python script) about "bpftool feature
    probe", including for listing features available to unprivileged
    users, and add a reference to bpftool man page.

    Thanks to Florian for reporting two typos (duplicated words).

    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20200511161536.29853-4-quentin@isovalent.com

    Quentin Monnet
     

10 May, 2020

1 commit

  • Two helpers bpf_seq_printf and bpf_seq_write, are added for
    writing data to the seq_file buffer.

    bpf_seq_printf supports common format string flag/width/type
    fields so at least I can get identical results for
    netlink and ipv6_route targets.

    For bpf_seq_printf and bpf_seq_write, return value -EOVERFLOW
    specifically indicates a write failure due to overflow, which
    means the object will be repeated in the next bpf invocation
    if object collection stays the same. Note that if the object
    collection is changed, depending how collection traversal is
    done, even if the object still in the collection, it may not
    be visited.

    For bpf_seq_printf, format %s, %p{i,I}{4,6} needs to
    read kernel memory. Reading kernel memory may fail in
    the following two cases:
    - invalid kernel address, or
    - valid kernel address but requiring a major fault
    If reading kernel memory failed, the %s string will be
    an empty string and %p{i,I}{4,6} will be all 0.
    Not returning error to bpf program is consistent with
    what bpf_trace_printk() does for now.

    bpf_seq_printf may return -EBUSY meaning that internal percpu
    buffer for memory copy of strings or other pointees is
    not available. Bpf program can return 1 to indicate it
    wants the same object to be repeated. Right now, this should not
    happen on no-RT kernels since migrate_disable(), which guards
    bpf prog call, calls preempt_disable().

    Signed-off-by: Yonghong Song
    Signed-off-by: Alexei Starovoitov
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20200509175914.2476661-1-yhs@fb.com

    Yonghong Song
     

14 Mar, 2020

1 commit

  • When compiling bpftool the following warning is found: "declaration of
    'struct bpf_pidns_info' will not be visible outside of this function."
    This patch adds struct bpf_pidns_info to type_fwds array to fix this.

    Fixes: b4490c5c4e02 ("bpf: Added new helper bpf_get_ns_current_pid_tgid")
    Signed-off-by: Carlos Neira
    Signed-off-by: Daniel Borkmann
    Reviewed-by: Quentin Monnet
    Acked-by: Martin KaFai Lau
    Link: https://lore.kernel.org/bpf/20200313154650.13366-1-cneirabustos@gmail.com

    Carlos Neira
     

13 Mar, 2020

1 commit

  • New bpf helper bpf_get_ns_current_pid_tgid,
    This helper will return pid and tgid from current task
    which namespace matches dev_t and inode number provided,
    this will allows us to instrument a process inside a container.

    Signed-off-by: Carlos Neira
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song
    Link: https://lore.kernel.org/bpf/20200304204157.58695-3-cneirabustos@gmail.com

    Carlos Neira
     

26 Feb, 2020

1 commit


14 Jan, 2020

1 commit

  • bpf_helpers_doc.py script, used to generate bpf_helper_defs.h, unconditionally
    emits one informational message to stderr. Remove it and preserve stderr to
    contain only relevant errors. Also make sure script invocations command is
    muted by default in libbpf's Makefile.

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

    Andrii Nakryiko
     

21 Oct, 2019

1 commit

  • Don't generate a broken bpf_helper_defs.h header if the helper script needs
    updating because it doesn't recognize a newly added type. Instead print an
    error that explains why the build is failing, clean up the partially
    generated header and stop.

    v1->v2:
    - Switched from temporary file to .DELETE_ON_ERROR.

    Fixes: 456a513bb5d4 ("scripts/bpf: Emit an #error directive known types list needs updating")
    Suggested-by: Andrii Nakryiko
    Signed-off-by: Jakub Sitnicki
    Signed-off-by: Alexei Starovoitov
    Acked-by: Yonghong Song
    Acked-by: Andrii Nakryiko
    Link: https://lore.kernel.org/bpf/20191020112344.19395-1-jakub@cloudflare.com

    Jakub Sitnicki
     

16 Oct, 2019

1 commit

  • Make the compiler report a clear error when bpf_helpers_doc.py needs
    updating rather than rely on the fact that Clang fails to compile
    English:

    ../../../lib/bpf/bpf_helper_defs.h:2707:1: error: unknown type name 'Unrecognized'
    Unrecognized type 'struct bpf_inet_lookup', please add it to known types!

    Signed-off-by: Jakub Sitnicki
    Signed-off-by: Daniel Borkmann
    Link: https://lore.kernel.org/bpf/20191016085811.11700-1-jakub@cloudflare.com

    Jakub Sitnicki
     

10 Oct, 2019

1 commit

  • Fix typo in struct xpd_md, generated from bpf_helpers_doc.py, which is
    causing compilation warnings for programs using bpf_helpers.h

    Fixes: 7a387bed47f7 ("scripts/bpf: teach bpf_helpers_doc.py to dump BPF helper definitions")
    Signed-off-by: Andrii Nakryiko
    Signed-off-by: Alexei Starovoitov
    Link: https://lore.kernel.org/bpf/20191010042534.290562-1-andriin@fb.com

    Andrii Nakryiko
     

07 Oct, 2019

1 commit


13 May, 2019

1 commit

  • The script broke on parsing function prototype for bpf_strtoul(). This
    is because the last argument for the function is a pointer to an
    "unsigned long". The current version of the script only accepts "const"
    and "struct", but not "unsigned", at the beginning of argument types
    made of several words.

    One solution could be to add "unsigned" to the list, but the issue could
    come up again in the future (what about "long int"?). It turns out we do
    not need to have such restrictions on the words: so let's simply accept
    any series of words instead.

    Reported-by: Yonghong Song
    Signed-off-by: Quentin Monnet
    Acked-by: Jakub Kicinski
    Signed-off-by: Daniel Borkmann

    Quentin Monnet
     

17 May, 2018

1 commit

  • Documentation for eBPF helpers can be parsed from bpf.h and eventually
    turned into a man page. Commit 6f96674dbd8c ("bpf: relax constraints on
    formatting for eBPF helper documentation") changed the script used to
    parse it, in order to allow for different indent style and to ease the
    work for writing documentation for future helpers.

    The script currently considers that the first tab can be replaced by 6
    to 8 spaces. But the documentation for bpf_fib_lookup() uses a mix of
    tabs (for the "Description" part) and of spaces ("Return" part), and
    only has 5 space long indent for the latter.

    We probably do not want to change the values accepted by the script each
    time a new helper gets a new indent style. However, it is worth noting
    that with those 5 spaces, the "Description" and "Return" part *look*
    aligned in the generated patch and in `git show`, so it is likely other
    helper authors will use the same length. Therefore, allow for helper
    documentation to use 5 spaces only for the first indent level.

    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann

    Quentin Monnet
     

02 May, 2018

1 commit

  • The Python script used to parse and extract eBPF helpers documentation
    from include/uapi/linux/bpf.h expects a very specific formatting for the
    descriptions (single dot represents a space, '>' stands for a tab):

    /*
    ...
    *.int bpf_helper(list of arguments)
    *.> Description
    *.> > Start of description
    *.> > Another line of description
    *.> > And yet another line of description
    *.> Return
    *.> > 0 on success, or a negative error in case of failure
    ...
    */

    This is too strict, and painful for developers who wants to add
    documentation for new helpers. Worse, it is extremely difficult to check
    that the formatting is correct during reviews. Change the format
    expected by the script and make it more flexible. The script now works
    whether or not the initial space (right after the star) is present, and
    accepts both tabs and white spaces (or a combination of both) for
    indenting description sections and contents.

    Concretely, something like the following would now be supported:

    /*
    ...
    *int bpf_helper(list of arguments)
    *......Description
    *.> > Start of description...
    *> > Another line of description
    *..............And yet another line of description
    *> Return
    *.> ........0 on success, or a negative error in case of failure
    ...
    */

    While at it, remove unnecessary carets from each regex used with match()
    in the script. They are redundant, as match() tries to match from the
    beginning of the string by default.

    v2: Remove unnecessary caret when a regex is used with match().

    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann

    Quentin Monnet
     

27 Apr, 2018

1 commit

  • Remove previous "overview" of eBPF helpers from user bpf.h header.
    Replace it by a comment explaining how to process the new documentation
    (to come in following patches) with a Python script to produce RST, then
    man page documentation.

    Also add the aforementioned Python script under scripts/. It is used to
    process include/uapi/linux/bpf.h and to extract helper descriptions, to
    turn it into a RST document that can further be processed with rst2man
    to produce a man page. The script takes one "--filename "
    option. If the script is launched from scripts/ in the kernel root
    directory, it should be able to find the location of the header to
    parse, and "--filename " is then optional. If it cannot
    find the file, then the option becomes mandatory. RST-formatted
    documentation is printed to standard output.

    Typical workflow for producing the final man page would be:

    $ ./scripts/bpf_helpers_doc.py \
    --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst
    $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7
    $ man /tmp/bpf-helpers.7

    Note that the tool kernel-doc cannot be used to document eBPF helpers,
    whose signatures are not available directly in the header files
    (pre-processor directives are used to produce them at the beginning of
    the compilation process).

    v4:
    - Also remove overviews for newly added bpf_xdp_adjust_tail() and
    bpf_skb_get_xfrm_state().
    - Remove vague statement about what helpers are restricted to GPL
    programs in "LICENSE" section for man page footer.
    - Replace license boilerplate with SPDX tag for Python script.

    v3:
    - Change license for man page.
    - Remove "for safety reasons" from man page header text.
    - Change "packets metadata" to "packets" in man page header text.
    - Move and fix comment on helpers introducing no overhead.
    - Remove "NOTES" section from man page footer.
    - Add "LICENSE" section to man page footer.
    - Edit description of file include/uapi/linux/bpf.h in man page footer.

    Signed-off-by: Quentin Monnet
    Signed-off-by: Daniel Borkmann

    Quentin Monnet