Skip to content
Snippets Groups Projects
  1. Jan 23, 2019
    • Quentin Monnet's avatar
      tools: bpftool: add C-style "#define" output for probes · d267cff4
      Quentin Monnet authored
      
      Make bpftool able to dump a subset of the parameters collected by
      probing the system as a listing of C-style #define macros, so that
      external projects can reuse the result of this probing and build
      BPF-based project in accordance with the features available on the
      system.
      
      The new "macros" keyword is used to select this output. An additional
      "prefix" keyword is added so that users can select a custom prefix for
      macro names, in order to avoid any namespace conflict.
      
      Sample output:
      
          # bpftool feature probe kernel macros prefix FOO_
          /*** System call availability ***/
          #define FOO_HAVE_BPF_SYSCALL
      
          /*** eBPF program types ***/
          #define FOO_HAVE_SOCKET_FILTER_PROG_TYPE
          #define FOO_HAVE_KPROBE_PROG_TYPE
          #define FOO_HAVE_SCHED_CLS_PROG_TYPE
          ...
      
          /*** eBPF map types ***/
          #define FOO_HAVE_HASH_MAP_TYPE
          #define FOO_HAVE_ARRAY_MAP_TYPE
          #define FOO_HAVE_PROG_ARRAY_MAP_TYPE
          ...
      
          /*** eBPF helper functions ***/
          /*
           * Use FOO_HAVE_PROG_TYPE_HELPER(prog_type_name, helper_name)
           * to determine if <helper_name> is available for <prog_type_name>,
           * e.g.
           *      #if FOO_HAVE_PROG_TYPE_HELPER(xdp, bpf_redirect)
           *              // do stuff with this helper
           *      #elif
           *              // use a workaround
           *      #endif
           */
          #define FOO_HAVE_PROG_TYPE_HELPER(prog_type, helper)        \
                  FOO_BPF__PROG_TYPE_ ## prog_type ## __HELPER_ ## helper
          ...
          #define FOO_BPF__PROG_TYPE_socket_filter__HELPER_bpf_probe_read 0
          #define FOO_BPF__PROG_TYPE_socket_filter__HELPER_bpf_ktime_get_ns 1
          #define FOO_BPF__PROG_TYPE_socket_filter__HELPER_bpf_trace_printk 1
          ...
      
      v3:
      - Change output for helpers again: add a
        HAVE_PROG_TYPE_HELPER(type, helper) macro that can be used to tell
        if <helper> is available for program <type>.
      
      v2:
      - #define-based output added as a distinct patch.
      - "HAVE_" prefix appended to macro names.
      - Output limited to bpf() syscall availability, BPF prog and map types,
        helper functions. In this version kernel config options, procfs
        parameter or kernel version are intentionally left aside.
      - Following the change on helper probes, format for helper probes in
        this output style has changed (now a list of compatible program
        types).
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      d267cff4
    • Quentin Monnet's avatar
      tools: bpftool: add probes for eBPF helper functions · 2d3ea5e8
      Quentin Monnet authored
      
      Similarly to what was done for program types and map types, add a set of
      probes to test the availability of the different eBPF helper functions
      on the current system.
      
      For each known program type, all known helpers are tested, in order to
      establish a compatibility matrix. Output is provided as a set of lists
      of available helpers, one per program type.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF helper functions...
          eBPF helpers supported for program type socket_filter:
                  - bpf_map_lookup_elem
                  - bpf_map_update_elem
                  - bpf_map_delete_elem
          ...
          eBPF helpers supported for program type kprobe:
                  - bpf_map_lookup_elem
                  - bpf_map_update_elem
                  - bpf_map_delete_elem
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "helpers": {
                  "socket_filter_available_helpers": ["bpf_map_lookup_elem", \
                          "bpf_map_update_elem","bpf_map_delete_elem", ...
                  ],
                  "kprobe_available_helpers": ["bpf_map_lookup_elem", \
                          "bpf_map_update_elem","bpf_map_delete_elem", ...
                  ],
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
      
      v4:
      - Use "enum bpf_func_id" instead of "__u32" in bpf_probe_helper()
        declaration for the type of the argument used to pass the id of
        the helper to probe.
      - Undef BPF_HELPER_MAKE_ENTRY after using it.
      
      v3:
      - Do not pass kernel version from bpftool to libbpf probes (kernel
        version for testing program with kprobes is retrieved directly from
        libbpf).
      - Dump one list of available helpers per program type (instead of one
        list of compatible program types per helper).
      
      v2:
      - Move probes from bpftool to libbpf.
      - Test all program types for each helper, print a list of working prog
        types for each helper.
      - Fall back on include/uapi/linux/bpf.h for names and ids of helpers.
      - Remove C-style macros output from this patch.
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2d3ea5e8
    • Quentin Monnet's avatar
      tools: bpftool: add probes for eBPF map types · f99e1663
      Quentin Monnet authored
      
      Add new probes for eBPF map types, to detect what are the ones available
      on the system. Try creating one map of each type, and see if the kernel
      complains.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF map types...
          eBPF map_type hash is available
          eBPF map_type array is available
          eBPF map_type prog_array is available
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "map_types": {
                  "have_hash_map_type": true,
                  "have_array_map_type": true,
                  "have_prog_array_map_type": true,
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
      
      v3:
      - Use a switch with all enum values for setting specific map parameters,
        so that gcc complains at compile time (-Wswitch-enum) if new map types
        were added to the kernel but libbpf was not updated.
      
      v2:
      - Move probes from bpftool to libbpf.
      - Remove C-style macros output from this patch.
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f99e1663
    • Quentin Monnet's avatar
      tools: bpftool: add probes for eBPF program types · 1bf4b058
      Quentin Monnet authored
      
      Introduce probes for supported BPF program types in libbpf, and call it
      from bpftool to test what types are available on the system. The probe
      simply consists in loading a very basic program of that type and see if
      the verifier complains or not.
      
      Sample output:
      
          # bpftool feature probe kernel
          ...
          Scanning eBPF program types...
          eBPF program_type socket_filter is available
          eBPF program_type kprobe is available
          eBPF program_type sched_cls is available
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              ...
              "program_types": {
                  "have_socket_filter_prog_type": true,
                  "have_kprobe_prog_type": true,
                  "have_sched_cls_prog_type": true,
                  ...
              }
          }
      
      v5:
      - In libbpf.map, move global symbol to a new LIBBPF_0.0.2 section.
      - Rename (non-API function) prog_load() as probe_load().
      
      v3:
      - Get kernel version for checking kprobes availability from libbpf
        instead of from bpftool. Do not pass kernel_version as an argument
        when calling libbpf probes.
      - Use a switch with all enum values for setting specific program
        parameters just before probing, so that gcc complains at compile time
        (-Wswitch-enum) if new prog types were added to the kernel but libbpf
        was not updated.
      - Add a comment in libbpf.h about setrlimit() usage to allow many
        consecutive probe attempts.
      
      v2:
      - Move probes from bpftool to libbpf.
      - Remove C-style macros output from this patch.
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      1bf4b058
    • Quentin Monnet's avatar
      tools: bpftool: add probes for kernel configuration options · 4567b983
      Quentin Monnet authored
      
      Add probes to dump a number of options set (or not set) for compiling
      the kernel image. These parameters provide information about what BPF
      components should be available on the system. A number of them are not
      directly related to eBPF, but are in fact used in the kernel as
      conditions on which to compile, or not to compile, some of the eBPF
      helper functions.
      
      Sample output:
      
          # bpftool feature probe kernel
          Scanning system configuration...
          ...
          CONFIG_BPF is set to y
          CONFIG_BPF_SYSCALL is set to y
          CONFIG_HAVE_EBPF_JIT is set to y
          ...
      
          # bpftool --pretty --json feature probe kernel
          {
              "system_config": {
                  ...
                  "CONFIG_BPF": "y",
                  "CONFIG_BPF_SYSCALL": "y",
                  "CONFIG_HAVE_EBPF_JIT": "y",
                  ...
              }
          }
      
      v5:
      - Declare options[] array in probe_kernel_image_config() as static.
      
      v4:
      - Add some options to the list:
          - CONFIG_TRACING
          - CONFIG_KPROBE_EVENTS
          - CONFIG_UPROBE_EVENTS
          - CONFIG_FTRACE_SYSCALLS
      - Add comments about those options in the source code.
      
      v3:
      - Add a comment about /proc/config.gz not being supported as a path for
        the config file at this time.
      - Use p_info() instead of p_err() on failure to get options from config
        file, as bpftool keeps probing other parameters and that would
        possibly create duplicate "error" entries for JSON.
      
      v2:
      - Remove C-style macros output from this patch.
      - NOT addressed: grouping of those config options into subsections
        (I don't see an easy way of grouping them at the moment, please see
        also the discussion on v1 thread).
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      4567b983
    • Quentin Monnet's avatar
      tools: bpftool: add probes for /proc/ eBPF parameters · 7a4522bb
      Quentin Monnet authored
      
      Add a set of probes to dump the eBPF-related parameters available from
      /proc/: availability of bpf() syscall for unprivileged users,
      JIT compiler status and hardening status, kallsyms exports status.
      
      Sample output:
      
          # bpftool feature probe kernel
          Scanning system configuration...
          bpf() syscall for unprivileged users is enabled
          JIT compiler is disabled
          JIT compiler hardening is disabled
          JIT compiler kallsyms exports are disabled
          Global memory limit for JIT compiler for unprivileged users \
                  is 264241152 bytes
          ...
      
          # bpftool --json --pretty feature probe kernel
          {
              "system_config": {
                  "unprivileged_bpf_disabled": 0,
                  "bpf_jit_enable": 0,
                  "bpf_jit_harden": 0,
                  "bpf_jit_kallsyms": 0,
                  "bpf_jit_limit": 264241152
              },
              ...
          }
      
      These probes are skipped if procfs is not mounted.
      
      v4:
      - Add bpf_jit_limit parameter.
      
      v2:
      - Remove C-style macros output from this patch.
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      7a4522bb
    • Quentin Monnet's avatar
      tools: bpftool: add basic probe capability, probe syscall availability · 49eb7ab3
      Quentin Monnet authored
      
      Add a new component and command for bpftool, in order to probe the
      system to dump a set of eBPF-related parameters so that users can know
      what features are available on the system.
      
      Parameters are dumped in plain or JSON output (with -j/-p options).
      
      The current patch introduces probing of one simple parameter:
      availability of the bpf() system call. Later commits
      will add other probes.
      
      Sample output:
      
          # bpftool feature probe kernel
          Scanning system call availability...
          bpf() syscall is available
      
          # bpftool --json --pretty feature probe kernel
          {
              "syscall_config": {
                  "have_bpf_syscall": true
              }
          }
      
      The optional "kernel" keyword enforces probing of the current system,
      which is the only possible behaviour at this stage. It can be safely
      omitted.
      
      The feature comes with the relevant man page, but bash completion will
      come in a dedicated commit.
      
      v3:
      - Do not probe kernel version. Contrarily to what is written below for
        v2, we can have the kernel version retrieved in libbpf instead of
        bpftool (in the patch adding probing for program types).
      
      v2:
      - Remove C-style macros output from this patch.
      - Even though kernel version is no longer needed for testing kprobes
        availability, note that we still collect it in this patch so that
        bpftool gets able to probe (in next patches) older kernels as well.
      
      Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Reviewed-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      49eb7ab3
  2. Jan 17, 2019
    • Peter Oskolkov's avatar
      bpf: fix a (false) compiler warning · d0b2818e
      Peter Oskolkov authored
      
      An older GCC compiler complains:
      
      kernel/bpf/verifier.c: In function 'bpf_check':
      kernel/bpf/verifier.c:4***:13: error: 'prev_offset' may be used uninitialized
            in this function [-Werror=maybe-uninitialized]
         } else if (krecord[i].insn_offset <= prev_offset) {
                   ^
      kernel/bpf/verifier.c:4***:38: note: 'prev_offset' was declared here
        u32 i, nfuncs, urec_size, min_size, prev_offset;
      
      Although the compiler is wrong here, the patch makes sure
      that prev_offset is always initialized, just to silence the warning.
      
      v2: fix a spelling error in the commit message.
      
      Signed-off-by: default avatarPeter Oskolkov <posk@google.com>
      Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      d0b2818e
    • Daniel Borkmann's avatar
      Merge branch 'bpf-bpftool-queue-stack' · 4edc01b8
      Daniel Borkmann authored
      
      Stanislav Fomichev says:
      
      ====================
      This patch series add support for queue/stack manipulations.
      
      It goes like this:
      
         commands by permitting empty keys.
      
      v2:
      * removed unneeded jsonw_null from patch #6
      * improved bash completions (and moved them into separate patch #7)
      ====================
      
      Reviewed-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      4edc01b8
    • Stanislav Fomichev's avatar
      bpftool: add bash completion for peek/push/enqueue/pop/dequeue · 55c70bff
      Stanislav Fomichev authored
      
      bpftool map peek id <TAB>      - suggests only queue and stack map ids
      bpftool map pop id <TAB>       - suggests only stack map ids
      bpftool map dequeue id <TAB>   - suggests only queue map ids
      
      bpftool map push id <TAB>      - suggests only stack map ids
      bpftool map enqueue id <TAB>   - suggests only queue map ids
      
      bpftool map push id 1 <TAB>    - suggests 'value', not 'key'
      bpftool map enqueue id 2 <TAB> - suggests 'value', not 'key'
      
      bpftool map update id <stack/queue type> - suggests 'value', not 'key'
      bpftool map lookup id <stack/queue type> - suggests nothing
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      55c70bff
    • Stanislav Fomichev's avatar
      bpftool: add pop and dequeue commands · 74f312ef
      Stanislav Fomichev authored
      
      This is intended to be used with queues and stacks, it pops and prints
      the last element via bpf_map_lookup_and_delete_elem.
      
      Example:
      
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map push pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map pop pinned /sys/fs/bpf/q
      value: 00 01 02 03
      bpftool map pop pinned /sys/fs/bpf/q
      Error: empty map
      
      bpftool map create /sys/fs/bpf/s type stack value 4 entries 10 name s
      bpftool map enqueue pinned /sys/fs/bpf/s value 0 1 2 3
      bpftool map dequeue pinned /sys/fs/bpf/s
      value: 00 01 02 03
      bpftool map dequeue pinned /sys/fs/bpf/s
      Error: empty map
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      74f312ef
    • Stanislav Fomichev's avatar
      bpftool: add push and enqueue commands · 549d4d3d
      Stanislav Fomichev authored
      
      This is intended to be used with queues and stacks and be more
      user-friendly than 'update' without the key.
      
      Example:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map push pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map peek pinned /sys/fs/bpf/q
      value: 00 01 02 03
      
      bpftool map create /sys/fs/bpf/s type stack value 4 entries 10 name s
      bpftool map enqueue pinned /sys/fs/bpf/s value 0 1 2 3
      bpftool map peek pinned /sys/fs/bpf/s
      value: 00 01 02 03
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      549d4d3d
    • Stanislav Fomichev's avatar
      bpftool: add peek command · 66cf6e0b
      Stanislav Fomichev authored
      
      This is intended to be used with queues and stacks and be more
      user-friendly than 'lookup' without key/value.
      
      Example:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map peek pinned /sys/fs/bpf/q
      value: 00 01 02 03
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      66cf6e0b
    • Stanislav Fomichev's avatar
      bpftool: don't print empty key/value for maps · 04a5d323
      Stanislav Fomichev authored
      
      When doing dump or lookup, don't print key if key_size == 0 or value if
      value_size == 0. The initial usecase is queue and stack, where we have
      only values.
      
      This is for regular output only, json still has all the fields.
      
      Before:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map lookup pinned /sys/fs/bpf/q
      key:   value: 00 01 02 03
      
      After:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map lookup pinned /sys/fs/bpf/q
      value: 00 01 02 03
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      04a5d323
    • Stanislav Fomichev's avatar
      bpftool: make key optional in lookup command · 8a89fff6
      Stanislav Fomichev authored
      
      Bpftool expects key for 'lookup' operations. For some map types, key should
      not be specified. Support looking up those map types.
      
      Before:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map lookup pinned /sys/fs/bpf/q
      Error: did not find key
      
      After:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      bpftool map lookup pinned /sys/fs/bpf/q
      key:   value: 00 01 02 03
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      8a89fff6
    • Stanislav Fomichev's avatar
      bpftool: make key and value optional in update command · 7d7209cb
      Stanislav Fomichev authored
      
      Bpftool expects both key and value for 'update' operations. For some
      map types, key should not be specified. Support updating those map types.
      
      Before:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      Error: did not find key
      
      After:
      bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q
      bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
      
      Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      7d7209cb
  3. Jan 16, 2019
  4. Jan 15, 2019
Loading