Commit Graph

17 Commits

Author SHA1 Message Date
Yauheni Kaliuta 503bec2387 bpf, x86: Generate trampolines from bpf_tramp_links
Bugzilla: https://bugzilla.redhat.com/2120968
Conflicts: already applied
  1d5f82d9dd47 ("bpf, x86: fix freeing of not-finalized bpf_prog_pack")

commit f7e0beaf39d3868dc700d4954b26cf8443c5d423
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Tue May 10 13:59:19 2022 -0700

    bpf, x86: Generate trampolines from bpf_tramp_links

    Replace struct bpf_tramp_progs with struct bpf_tramp_links to collect
    struct bpf_tramp_link(s) for a trampoline.  struct bpf_tramp_link
    extends bpf_link to act as a linked list node.

    arch_prepare_bpf_trampoline() accepts a struct bpf_tramp_links to
    collects all bpf_tramp_link(s) that a trampoline should call.

    Change BPF trampoline and bpf_struct_ops to pass bpf_tramp_links
    instead of bpf_tramp_progs.

    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220510205923.3206889-2-kuifeng@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Yauheni Kaliuta 952abb92ab bpftool: Add missing link types
Bugzilla: https://bugzilla.redhat.com/2120968

commit fff3dfab17866f6ac5c5666839f6132b6c52f306
Author: Milan Landaverde <milan@mdaverde.com>
Date:   Thu Mar 31 11:45:54 2022 -0400

    bpftool: Add missing link types
    
    Will display the link type names in bpftool link show output
    
    Signed-off-by: Milan Landaverde <milan@mdaverde.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220331154555.422506-3-milan@mdaverde.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-28 16:48:56 +02:00
Yauheni Kaliuta 11cec39963 bpftool: Fix error check when calling hashmap__new()
Bugzilla: http://bugzilla.redhat.com/2069045

commit 622a5b582cc27d3deedc38fcef68da2972e8e58d
Author: Mauricio Vásquez <mauricio@kinvolk.io>
Date:   Fri Jan 7 10:26:20 2022 -0500

    bpftool: Fix error check when calling hashmap__new()
    
    hashmap__new() encodes errors with ERR_PTR(), hence it's not valid to
    check the returned pointer against NULL and IS_ERR() has to be used
    instead.
    
    libbpf_get_error() can't be used in this case as hashmap__new() is not
    part of the public libbpf API and it'll continue using ERR_PTR() after
    libbpf 1.0.
    
    Fixes: 8f184732b60b ("bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects")
    Fixes: 2828d0d75b73 ("bpftool: Switch to libbpf's hashmap for programs/maps in BTF listing")
    Fixes: d6699f8e0f83 ("bpftool: Switch to libbpf's hashmap for PIDs/names references")
    Signed-off-by: Mauricio Vásquez <mauricio@kinvolk.io>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Song Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20220107152620.192327-2-mauricio@kinvolk.io

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:53 +03:00
Yauheni Kaliuta 765bcf6418 bpftool: Switch to libbpf's hashmap for PIDs/names references
Bugzilla: http://bugzilla.redhat.com/2069045

commit d6699f8e0f834b40db35466f704705ae757be11a
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Sat Oct 23 21:51:54 2021 +0100

    bpftool: Switch to libbpf's hashmap for PIDs/names references
    
    In order to show PIDs and names for processes holding references to BPF
    programs, maps, links, or BTF objects, bpftool creates hash maps to
    store all relevant information. This commit is part of a set that
    transitions from the kernel's hash map implementation to the one coming
    with libbpf.
    
    The motivation is to make bpftool less dependent of kernel headers, to
    ease the path to a potential out-of-tree mirror, like libbpf has.
    
    This is the third and final step of the transition, in which we convert
    the hash maps used for storing the information about the processes
    holding references to BPF objects (programs, maps, links, BTF), and at
    last we drop the inclusion of tools/include/linux/hashtable.h.
    
    Note: Checkpatch complains about the use of __weak declarations, and the
    missing empty lines after the bunch of empty function declarations when
    compiling without the BPF skeletons (none of these were introduced in
    this patch). We want to keep things as they are, and the reports should
    be safe to ignore.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-6-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta abac33d1f9 bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects
Bugzilla: http://bugzilla.redhat.com/2069045

commit 8f184732b60b74a8f8ba0d9a5c248bf611b1ebba
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Sat Oct 23 21:51:52 2021 +0100

    bpftool: Switch to libbpf's hashmap for pinned paths of BPF objects
    
    In order to show pinned paths for BPF programs, maps, or links when
    listing them with the "-f" option, bpftool creates hash maps to store
    all relevant paths under the bpffs. So far, it would rely on the
    kernel implementation (from tools/include/linux/hashtable.h).
    
    We can make bpftool rely on libbpf's implementation instead. The
    motivation is to make bpftool less dependent of kernel headers, to ease
    the path to a potential out-of-tree mirror, like libbpf has.
    
    This commit is the first step of the conversion: the hash maps for
    pinned paths for programs, maps, and links are converted to libbpf's
    hashmap.{c,h}. Other hash maps used for the PIDs of process holding
    references to BPF objects are left unchanged for now. On the build side,
    this requires adding a dependency to a second header internal to libbpf,
    and making it a dependency for the bootstrap bpftool version as well.
    The rest of the changes are a rather straightforward conversion.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-4-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Yauheni Kaliuta a947303109 bpftool: Do not expose and init hash maps for pinned path in main.c
Bugzilla: http://bugzilla.redhat.com/2069045

commit 46241271d18f3ae095b7ec3d9d136d8f4e28e025
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Sat Oct 23 21:51:51 2021 +0100

    bpftool: Do not expose and init hash maps for pinned path in main.c
    
    BPF programs, maps, and links, can all be listed with their pinned paths
    by bpftool, when the "-f" option is provided. To do so, bpftool builds
    hash maps containing all pinned paths for each kind of objects.
    
    These three hash maps are always initialised in main.c, and exposed
    through main.h. There appear to be no particular reason to do so: we can
    just as well make them static to the files that need them (prog.c,
    map.c, and link.c respectively), and initialise them only when we want
    to show objects and the "-f" switch is provided.
    
    This may prevent unnecessary memory allocations if the implementation of
    the hash maps was to change in the future.
    
    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20211023205154.6710-3-quentin@isovalent.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:46 +03:00
Jerome Marchand b1eab37952 tools: bpftool: Update and synchronise option list in doc and help msg
Bugzilla: http://bugzilla.redhat.com/2041365

commit c07ba629df97b796ca7bbdfbf4748266ead27745
Author: Quentin Monnet <quentin@isovalent.com>
Date:   Fri Jul 30 22:54:32 2021 +0100

    tools: bpftool: Update and synchronise option list in doc and help msg

    All bpftool commands support the options for JSON output and debug from
    libbpf. In addition, some commands support additional options
    corresponding to specific use cases.

    The list of options described in the man pages for the different
    commands are not always accurate. The messages for interactive help are
    mostly limited to HELP_SPEC_OPTIONS, and are even less representative of
    the actual set of options supported for the commands.

    Let's update the lists:

    - HELP_SPEC_OPTIONS is modified to contain the "default" options (JSON
      and debug), and to be extensible (no ending curly bracket).
    - All commands use HELP_SPEC_OPTIONS in their help message, and then
      complete the list with their specific options.
    - The lists of options in the man pages are updated.
    - The formatting of the list for bpftool.rst is adjusted to match
      formatting for the other man pages. This is for consistency, and also
      because it will be helpful in a future patch to automatically check
      that the files are synchronised.

    Signed-off-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210730215435.7095-5-quentin@isovalent.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-04-29 18:14:37 +02:00
Yonghong Song e60495eafd bpftool: Implement link_query for bpf iterators
The link query for bpf iterators is implemented.
Besides being shown to the user what bpf iterator
the link represents, the target_name is also used
to filter out what additional information should be
printed out, e.g., whether map_id should be shown or not.
The following is an example of bpf_iter link dump,
plain output or pretty output.

  $ bpftool link show
  11: iter  prog 59  target_name task
          pids test_progs(1749)
  34: iter  prog 173  target_name bpf_map_elem  map_id 127
          pids test_progs_1(1753)
  $ bpftool -p link show
  [{
          "id": 11,
          "type": "iter",
          "prog_id": 59,
          "target_name": "task",
          "pids": [{
                  "pid": 1749,
                  "comm": "test_progs"
              }
          ]
      },{
          "id": 34,
          "type": "iter",
          "prog_id": 173,
          "target_name": "bpf_map_elem",
          "map_id": 127,
          "pids": [{
                  "pid": 1753,
                  "comm": "test_progs_1"
              }
          ]
      }
  ]

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200821184420.574430-1-yhs@fb.com
2020-08-21 14:01:39 -07:00
Andrii Nakryiko 09f44b753a tools/bpftool: Fix compilation warnings in 32-bit mode
Fix few compilation warnings in bpftool when compiling in 32-bit mode.
Abstract away u64 to pointer conversion into a helper function.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200813204945.1020225-2-andriin@fb.com
2020-08-13 16:45:41 -07:00
Andrii Nakryiko 0e8c7c07f0 tools/bpftool: Add `link detach` subcommand
Add ability to force-detach BPF link. Also add missing error message, if
specified link ID is wrong.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Song Liu <songliubraving@fb.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20200731182830.286260-5-andriin@fb.com
2020-08-01 20:38:29 -07:00
Tobias Klauser 9023497d87 tools, bpftool: Define prog_type_name array only once
Define prog_type_name in prog.c instead of main.h so it is only defined
once. This leads to a slight decrease in the binary size of bpftool.

Before:

   text	   data	    bss	    dec	    hex	filename
 401032	  11936	1573160	1986128	 1e4e50	bpftool

After:

   text	   data	    bss	    dec	    hex	filename
 399024	  11168	1573160	1983352	 1e4378	bpftool

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200624143124.12914-1-tklauser@distanz.ch
2020-06-25 16:06:01 +02:00
Andrii Nakryiko d53dee3fe0 tools/bpftool: Show info for processes holding BPF map/prog/link/btf FDs
Add bpf_iter-based way to find all the processes that hold open FDs against
BPF object (map, prog, link, btf). bpftool always attempts to discover this,
but will silently give up if kernel doesn't yet support bpf_iter BPF programs.
Process name and PID are emitted for each process (task group).

Sample output for each of 4 BPF objects:

$ sudo ./bpftool prog show
2694: cgroup_device  tag 8c42dee26e8cd4c2  gpl
        loaded_at 2020-06-16T15:34:32-0700  uid 0
        xlated 648B  jited 409B  memlock 4096B
        pids systemd(1)
2907: cgroup_skb  name egress  tag 9ad187367cf2b9e8  gpl
        loaded_at 2020-06-16T18:06:54-0700  uid 0
        xlated 48B  jited 59B  memlock 4096B  map_ids 2436
        btf_id 1202
        pids test_progs(2238417), test_progs(2238445)

$ sudo ./bpftool map show
2436: array  name test_cgr.bss  flags 0x400
        key 4B  value 8B  max_entries 1  memlock 8192B
        btf_id 1202
        pids test_progs(2238417), test_progs(2238445)
2445: array  name pid_iter.rodata  flags 0x480
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 1214  frozen
        pids bpftool(2239612)

$ sudo ./bpftool link show
61: cgroup  prog 2908
        cgroup_id 375301  attach_type egress
        pids test_progs(2238417), test_progs(2238445)
62: cgroup  prog 2908
        cgroup_id 375344  attach_type egress
        pids test_progs(2238417), test_progs(2238445)

$ sudo ./bpftool btf show
1202: size 1527B  prog_ids 2908,2907  map_ids 2436
        pids test_progs(2238417), test_progs(2238445)
1242: size 34684B
        pids bpftool(2258892)

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200619231703.738941-9-andriin@fb.com
2020-06-22 17:01:49 -07:00
Jakub Sitnicki e948947a6e bpftool: Support link show for netns-attached links
Make `bpf link show` aware of new link type, that is links attached to
netns. When listing netns-attached links, display netns inode number as its
identifier and link attach type.

Sample session:

  # readlink /proc/self/ns/net
  net:[4026532251]
  # bpftool prog show
  357: flow_dissector  tag a04f5eef06a7f555  gpl
          loaded_at 2020-05-30T16:53:51+0200  uid 0
          xlated 16B  jited 37B  memlock 4096B
  358: flow_dissector  tag a04f5eef06a7f555  gpl
          loaded_at 2020-05-30T16:53:51+0200  uid 0
          xlated 16B  jited 37B  memlock 4096B
  # bpftool link show
  108: netns  prog 357
          netns_ino 4026532251  attach_type flow_dissector
  # bpftool link -jp show
  [{
          "id": 108,
          "type": "netns",
          "prog_id": 357,
          "netns_ino": 4026532251,
          "attach_type": "flow_dissector"
      }
  ]

  (... after netns is gone ...)

  # bpftool link show
  108: netns  prog 357
          netns_ino 0  attach_type flow_dissector
  # bpftool link -jp show
  [{
          "id": 108,
          "type": "netns",
          "prog_id": 357,
          "netns_ino": 0,
          "attach_type": "flow_dissector"
      }
  ]

Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200531082846.2117903-9-jakub@cloudflare.com
2020-06-01 15:21:03 -07:00
Jakub Sitnicki be6e19818b bpftool: Extract helpers for showing link attach type
Code for printing link attach_type is duplicated in a couple of places, and
likely will be duplicated for future link types as well. Create helpers to
prevent duplication.

Suggested-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200531082846.2117903-8-jakub@cloudflare.com
2020-06-01 15:21:03 -07:00
Quentin Monnet 90040351a8 tools, bpftool: Clean subcommand help messages
This is a clean-up for the formatting of the do_help functions for
bpftool's subcommands. The following fixes are included:

- Do not use argv[-2] for "iter" help message, as the help is shown by
  default if no "iter" action is selected, resulting in messages looking
  like "./bpftool bpftool pin...".

- Do not print unused HELP_SPEC_PROGRAM in help message for "bpftool
  link".

- Andrii used argument indexing to avoid having multiple occurrences of
  bin_name and argv[-2] in the fprintf() for the help message, for
  "bpftool gen" and "bpftool link". Let's reuse this for all other help
  functions. We can remove up to thirty arguments for the "bpftool map"
  help message.

- Harmonise all functions, e.g. use ending quotes-comma on a separate
  line.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200523010751.23465-1-quentin@isovalent.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-06-01 14:38:18 -07:00
Yonghong Song 9406b485de tools/bpftool: Add bpf_iter support for bptool
Currently, only one command is supported
  bpftool iter pin <bpf_prog.o> <path>

It will pin the trace/iter bpf program in
the object file <bpf_prog.o> to the <path>
where <path> should be on a bpffs mount.

For example,
  $ bpftool iter pin ./bpf_iter_ipv6_route.o \
    /sys/fs/bpf/my_route
User can then do a `cat` to print out the results:
  $ cat /sys/fs/bpf/my_route
    fe800000000000000000000000000000 40 00000000000000000000000000000000 ...
    00000000000000000000000000000000 00 00000000000000000000000000000000 ...
    00000000000000000000000000000001 80 00000000000000000000000000000000 ...
    fe800000000000008c0162fffebdfd57 80 00000000000000000000000000000000 ...
    ff000000000000000000000000000000 08 00000000000000000000000000000000 ...
    00000000000000000000000000000000 00 00000000000000000000000000000000 ...

The implementation for ipv6_route iterator is in one of subsequent
patches.

This patch also added BPF_LINK_TYPE_ITER to link query.

In the future, we may add additional parameters to pin command
by parameterizing the bpf iterator. For example, a map_id or pid
may be added to let bpf program only traverses a single map or task,
similar to kernel seq_file single_open().

We may also add introspection command for targets/iterators by
leveraging the bpf_iter itself.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200509175920.2477247-1-yhs@fb.com
2020-05-09 17:05:27 -07:00
Andrii Nakryiko c5481f9a95 bpftool: Add bpf_link show and pin support
Add `bpftool link show` and `bpftool link pin` commands.

Example plain output for `link show` (with showing pinned paths):

[vmuser@archvm bpf]$ sudo ~/local/linux/tools/bpf/bpftool/bpftool -f link
1: tracing  prog 12
        prog_type tracing  attach_type fentry
        pinned /sys/fs/bpf/my_test_link
        pinned /sys/fs/bpf/my_test_link2
2: tracing  prog 13
        prog_type tracing  attach_type fentry
3: tracing  prog 14
        prog_type tracing  attach_type fentry
4: tracing  prog 15
        prog_type tracing  attach_type fentry
5: tracing  prog 16
        prog_type tracing  attach_type fentry
6: tracing  prog 17
        prog_type tracing  attach_type fentry
7: raw_tracepoint  prog 21
        tp 'sys_enter'
8: cgroup  prog 25
        cgroup_id 584  attach_type egress
9: cgroup  prog 25
        cgroup_id 599  attach_type egress
10: cgroup  prog 25
        cgroup_id 614  attach_type egress
11: cgroup  prog 25
        cgroup_id 629  attach_type egress

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20200429001614.1544-9-andriin@fb.com
2020-04-28 17:27:08 -07:00