Commit Graph

41 Commits

Author SHA1 Message Date
Viktor Malik 053ed0b95e
bpftool: Add link dump support for BPF_LINK_TYPE_SOCKMAP
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 1f3e2091d25b2b140967480177fcaee2f0eebfb1
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Tue Apr 9 21:35:37 2024 -0700

    bpftool: Add link dump support for BPF_LINK_TYPE_SOCKMAP
    
    An example output looks like:
      $ bpftool link
        1776: sk_skb  prog 49730
                map_id 0  attach_type sk_skb_verdict
                pids test_progs(8424)
        1777: sk_skb  prog 49755
                map_id 0  attach_type sk_skb_stream_verdict
                pids test_progs(8424)
        1778: sk_msg  prog 49770
                map_id 8208  attach_type sk_msg_verdict
                pids test_progs(8424)
    
    Reviewed-by: John Fastabend <john.fastabend@gmail.com>
    Reviewed-by: Quentin Monnet <qmo@kernel.org>
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20240410043537.3737928-1-yonghong.song@linux.dev
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 14:37:17 +01:00
Jerome Marchand aedd1d6308 bpftool: Display cookie for kprobe multi link
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit b0dc037399b19a777d569dbd9e2e9bbd62f3b3b1
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Jan 19 12:05:05 2024 +0100

    bpftool: Display cookie for kprobe multi link

    Displaying cookies for kprobe multi link, in plain mode:

      # bpftool link
      ...
      1397: kprobe_multi  prog 47532
              kretprobe.multi  func_cnt 3
              addr             cookie           func [module]
              ffffffff82b370c0 3                bpf_fentry_test1
              ffffffff82b39780 1                bpf_fentry_test2
              ffffffff82b397a0 2                bpf_fentry_test3

    And in json mode:

      # bpftool link -j | jq
      ...
        {
          "id": 1397,
          "type": "kprobe_multi",
          "prog_id": 47532,
          "retprobe": true,
          "func_cnt": 3,
          "missed": 0,
          "funcs": [
            {
              "addr": 18446744071607382208,
              "func": "bpf_fentry_test1",
              "module": null,
              "cookie": 3
            },
            {
              "addr": 18446744071607392128,
              "func": "bpf_fentry_test2",
              "module": null,
              "cookie": 1
            },
            {
              "addr": 18446744071607392160,
              "func": "bpf_fentry_test3",
              "module": null,
              "cookie": 2
            }
          ]
        }

    Cookie is attached to specific address, and because we sort addresses
    before printing, we need to sort cookies the same way, hence adding
    the struct addr_cookie to keep and sort them together.

    Also adding missing dd.sym_count check to show_kprobe_multi_json.

    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20240119110505.400573-9-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:01 +02:00
Jerome Marchand 7ff4fc3e12 bpftool: Display cookie for perf event link probes
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 54258324b934aa8552c239c443272ec7aea55285
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Jan 19 12:05:04 2024 +0100

    bpftool: Display cookie for perf event link probes

    Displaying cookie for perf event link probes, in plain mode:

      # bpftool link
      17: perf_event  prog 90
              kprobe ffffffff82b1c2b0 bpf_fentry_test1  cookie 3735928559
      18: perf_event  prog 90
              kretprobe ffffffff82b1c2b0 bpf_fentry_test1  cookie 3735928559
      20: perf_event  prog 92
              tracepoint sched_switch  cookie 3735928559
      21: perf_event  prog 93
              event software:page-faults  cookie 3735928559
      22: perf_event  prog 91
              uprobe /proc/self/exe+0xd703c  cookie 3735928559

    And in json mode:

      # bpftool link -j | jq

      {
        "id": 30,
        "type": "perf_event",
        "prog_id": 160,
        "retprobe": false,
        "addr": 18446744071607272112,
        "func": "bpf_fentry_test1",
        "offset": 0,
        "missed": 0,
        "cookie": 3735928559
      }

      {
        "id": 33,
        "type": "perf_event",
        "prog_id": 162,
        "tracepoint": "sched_switch",
        "cookie": 3735928559
      }

      {
        "id": 34,
        "type": "perf_event",
        "prog_id": 163,
        "event_type": "software",
        "event_config": "page-faults",
        "cookie": 3735928559
      }

      {
        "id": 35,
        "type": "perf_event",
        "prog_id": 161,
        "retprobe": false,
        "file": "/proc/self/exe",
        "offset": 880700,
        "cookie": 3735928559
      }

    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20240119110505.400573-8-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:01 +02:00
Viktor Malik e5ee401c4d
bpftool: Fix wrong free call in do_show_link
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 2adb2e0fcdf3c6d8e28a5a9c33e458e1037ae5ad
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Jan 19 12:05:00 2024 +0100

    bpftool: Fix wrong free call in do_show_link
    
    The error path frees wrong array, it should be ref_ctr_offsets.
    
    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Fixes: a7795698f8b6 ("bpftool: Add support to display uprobe_multi links")
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20240119110505.400573-4-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:43 +02:00
Viktor Malik 62bad0391f
bpftool: Add support to display uprobe_multi links
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit a7795698f8b6c48283fa4334eb313bc1350b2864
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Sat Nov 25 20:31:30 2023 +0100

    bpftool: Add support to display uprobe_multi links
    
    Adding support to display details for uprobe_multi links,
    both plain:
    
      # bpftool link -p
      ...
      24: uprobe_multi  prog 126
              uprobe.multi  path /home/jolsa/bpf/test_progs  func_cnt 3  pid 4143
              offset             ref_ctr_offset     cookies
              0xd1f88            0xf5d5a8           0xdead
              0xd1f8f            0xf5d5aa           0xbeef
              0xd1f96            0xf5d5ac           0xcafe
    
    and json:
    
      # bpftool link -p
      [{
      ...
          },{
              "id": 24,
              "type": "uprobe_multi",
              "prog_id": 126,
              "retprobe": false,
              "path": "/home/jolsa/bpf/test_progs",
              "func_cnt": 3,
              "pid": 4143,
              "funcs": [{
                      "offset": 860040,
                      "ref_ctr_offset": 16111016,
                      "cookie": 57005
                  },{
                      "offset": 860047,
                      "ref_ctr_offset": 16111018,
                      "cookie": 48879
                  },{
                      "offset": 860054,
                      "ref_ctr_offset": 16111020,
                      "cookie": 51966
                  }
              ]
          }
      ]
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/bpf/20231125193130.834322-7-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:59 +02:00
Artem Savkov e49ffe635f bpftool: Implement link show support for netkit
JIRA: https://issues.redhat.com/browse/RHEL-23643

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 92a85e18ad4705c66ace55a19f4f8301ef0eb59f
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Tue Oct 24 23:49:01 2023 +0200

    bpftool: Implement link show support for netkit

    Add support to dump netkit link information to bpftool in similar way as
    we have for XDP. The netkit link info only exposes the ifindex and the
    attach_type.

    Below shows an example link dump output, and a cgroup link is included for
    comparison, too:

      # bpftool link
      [...]
      10: cgroup  prog 2466
            cgroup_id 1  attach_type cgroup_inet6_post_bind
      [...]
      8: netkit  prog 35
            ifindex nk1(18)  attach_type netkit_primary
      [...]

    Equivalent json output:

      # bpftool link --json
      [...]
      {
        "id": 10,
        "type": "cgroup",
        "prog_id": 2466,
        "cgroup_id": 1,
        "attach_type": "cgroup_inet6_post_bind"
      },
      [...]
      {
        "id": 12,
        "type": "netkit",
        "prog_id": 61,
        "devname": "nk1",
        "ifindex": 21,
        "attach_type": "netkit_primary"
      }
      [...]

    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
    Link: https://lore.kernel.org/r/20231024214904.29825-5-daniel@iogearbox.net
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-28 09:04:06 +01:00
Artem Savkov 5c772a1ebf bpftool: Implement link show support for xdp
JIRA: https://issues.redhat.com/browse/RHEL-23643

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit 053bbf9bff58864be880d7e9a5af586793dbb7de
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Wed Aug 16 11:56:51 2023 +0200

    bpftool: Implement link show support for xdp

    Add support to dump XDP link information to bpftool. This reuses the
    recently added show_link_ifindex_{plain,json}(). The XDP link info only
    exposes the ifindex.

    Below shows an example link dump output, and a cgroup link is included
    for comparison, too:

      # bpftool link
      [...]
      10: cgroup  prog 2466
            cgroup_id 1  attach_type cgroup_inet6_post_bind
      [...]
      16: xdp  prog 2477
            ifindex enp5s0(3)
      [...]

    Equivalent json output:

      # bpftool link --json
      [...]
      {
        "id": 10,
        "type": "cgroup",
        "prog_id": 2466,
        "cgroup_id": 1,
        "attach_type": "cgroup_inet6_post_bind"
      },
      [...]
      {
        "id": 16,
        "type": "xdp",
        "prog_id": 2477,
        "devname": "enp5s0",
        "ifindex": 3
      }
      [...]

    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/r/20230816095651.10014-2-daniel@iogearbox.net
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-28 09:04:05 +01:00
Artem Savkov d5278982a7 bpftool: Implement link show support for tcx
JIRA: https://issues.redhat.com/browse/RHEL-23643

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

commit e16e6c6df475b10b1ed933a6827798312612358f
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Wed Aug 16 11:56:50 2023 +0200

    bpftool: Implement link show support for tcx

    Add support to dump tcx link information to bpftool. This adds a
    common helper show_link_ifindex_{plain,json}() which can be reused
    also for other link types. The plain text and json device output is
    the same format as in bpftool net dump.

    Below shows an example link dump output along with a cgroup link
    for comparison:

      # bpftool link
      [...]
      10: cgroup  prog 1977
            cgroup_id 1  attach_type cgroup_inet6_post_bind
      [...]
      13: tcx  prog 2053
            ifindex enp5s0(3)  attach_type tcx_ingress
      14: tcx  prog 2080
            ifindex enp5s0(3)  attach_type tcx_egress
      [...]

    Equivalent json output:

      # bpftool link --json
      [...]
      {
        "id": 10,
        "type": "cgroup",
        "prog_id": 1977,
        "cgroup_id": 1,
        "attach_type": "cgroup_inet6_post_bind"
      },
      [...]
      {
        "id": 13,
        "type": "tcx",
        "prog_id": 2053,
        "devname": "enp5s0",
        "ifindex": 3,
        "attach_type": "tcx_ingress"
      },
      {
        "id": 14,
        "type": "tcx",
        "prog_id": 2080,
        "devname": "enp5s0",
        "ifindex": 3,
        "attach_type": "tcx_egress"
      }
      [...]

    Suggested-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Link: https://lore.kernel.org/r/20230816095651.10014-1-daniel@iogearbox.net
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-28 09:04:05 +01:00
Artem Savkov 899c6d142d bpftool: Display missed count for kprobe perf link
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit b563b9bae8c3a6583e34820856dc6eafc2239aaf
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:42 2023 +0200

    bpftool: Display missed count for kprobe perf link
    
    Adding 'missed' field to display missed counts for kprobes
    attached by perf event link, like:
    
      # bpftool link
      5: perf_event  prog 82
              kprobe ffffffff815203e0 ksys_write
      6: perf_event  prog 83
              kprobe ffffffff811d1e50 scheduler_tick  missed 682217
    
      # bpftool link -jp
      [{
              "id": 5,
              "type": "perf_event",
              "prog_id": 82,
              "retprobe": false,
              "addr": 18446744071584220128,
              "func": "ksys_write",
              "offset": 0,
              "missed": 0
          },{
              "id": 6,
              "type": "perf_event",
              "prog_id": 83,
              "retprobe": false,
              "addr": 18446744071580753488,
              "func": "scheduler_tick",
              "offset": 0,
              "missed": 693469
          }
      ]
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-7-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:51 +01:00
Artem Savkov fb769349b1 bpftool: Display missed count for kprobe_multi link
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit b24fc35521b09b5feaf5d06a75e8a43042592d0b
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:41 2023 +0200

    bpftool: Display missed count for kprobe_multi link
    
    Adding 'missed' field to display missed counts for kprobes
    attached by kprobe multi link, like:
    
      # bpftool link
      5: kprobe_multi  prog 76
              kprobe.multi  func_cnt 1  missed 1
              addr             func [module]
              ffffffffa039c030 fp3_test [fprobe_test]
    
      # bpftool link -jp
      [{
              "id": 5,
              "type": "kprobe_multi",
              "prog_id": 76,
              "retprobe": false,
              "func_cnt": 1,
              "missed": 1,
              "funcs": [{
                      "addr": 18446744072102723632,
                      "func": "fp3_test",
                      "module": "fprobe_test"
                  }
              ]
          }
      ]
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-6-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:51 +01:00
Jerome Marchand 1e96adc649 bpftool: Fix build warnings with -Wtype-limits
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 6a8faf10709161e7138202a8cf052b070971239f
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Wed Aug 30 03:03:25 2023 +0000

    bpftool: Fix build warnings with -Wtype-limits

    Quentin reported build warnings when building bpftool :

        link.c: In function ‘perf_config_hw_cache_str’:
        link.c:86:18: warning: comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
           86 |         if ((id) >= 0 && (id) < ARRAY_SIZE(array))      \
              |                  ^~
        link.c:320:20: note: in expansion of macro ‘perf_event_name’
          320 |         hw_cache = perf_event_name(evsel__hw_cache, config & 0xff);
              |                    ^~~~~~~~~~~~~~~
        [... more of the same for the other calls to perf_event_name ...]

    He also pointed out the reason and the solution:

      We're always passing unsigned, so it should be safe to drop the check on
      (id) >= 0.

    Fixes: 62b57e3ddd64 ("bpftool: Add perf event names")
    Reported-by: Quentin Monnet <quentin@isovalent.com>
    Suggested-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Closes: https://lore.kernel.org/bpf/a35d9a2d-54a0-49ec-9ed1-8fcf1369d3cc@isovalent.com
    Link: https://lore.kernel.org/bpf/20230830030325.3786-1-laoar.shao@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:02 +01:00
Jerome Marchand 76285bb285 bpftool: Show perf link info
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 88d6160737fa7bec9addb9c479b8166e88bc5ff5
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Sun Jul 9 02:56:30 2023 +0000

    bpftool: Show perf link info

    Enhance bpftool to display comprehensive information about exposed
    perf_event links, covering uprobe, kprobe, tracepoint, and generic perf
    event. The resulting output will include the following details:

    $ tools/bpf/bpftool/bpftool link show
    3: perf_event  prog 14
            event software:cpu-clock
            bpf_cookie 0
            pids perf_event(19483)
    4: perf_event  prog 14
            event hw-cache:LLC-load-misses
            bpf_cookie 0
            pids perf_event(19483)
    5: perf_event  prog 14
            event hardware:cpu-cycles
            bpf_cookie 0
            pids perf_event(19483)
    6: perf_event  prog 19
            tracepoint sched_switch
            bpf_cookie 0
            pids tracepoint(20947)
    7: perf_event  prog 26
            uprobe /home/dev/waken/bpf/uprobe/a.out+0x1338
            bpf_cookie 0
            pids uprobe(21973)
    8: perf_event  prog 27
            uretprobe /home/dev/waken/bpf/uprobe/a.out+0x1338
            bpf_cookie 0
            pids uprobe(21973)
    10: perf_event  prog 43
            kprobe ffffffffb70a9660 kernel_clone
            bpf_cookie 0
            pids kprobe(35275)
    11: perf_event  prog 41
            kretprobe ffffffffb70a9660 kernel_clone
            bpf_cookie 0
            pids kprobe(35275)

    $ tools/bpf/bpftool/bpftool link show -j
    [{"id":3,"type":"perf_event","prog_id":14,"event_type":"software","event_config":"cpu-clock","bpf_cookie":0,"pids":[{"pid":19483,"comm":"perf_event"}]},{"id":4,"type":"perf_event","prog_id":14,"event_type":"hw-cache","event_config":"LLC-load-misses","bpf_cookie":0,"pids":[{"pid":19483,"comm":"perf_event"}]},{"id":5,"type":"perf_event","prog_id":14,"event_type":"hardware","event_config":"cpu-cycles","bpf_cookie":0,"pids":[{"pid":19483,"comm":"perf_event"}]},{"id":6,"type":"perf_event","prog_id":19,"tracepoint":"sched_switch","bpf_cookie":0,"pids":[{"pid":20947,"comm":"tracepoint"}]},{"id":7,"type":"perf_event","prog_id":26,"retprobe":false,"file":"/home/dev/waken/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":21973,"comm":"uprobe"}]},{"id":8,"type":"perf_event","prog_id":27,"retprobe":true,"file":"/home/dev/waken/bpf/uprobe/a.out","offset":4920,"bpf_cookie":0,"pids":[{"pid":21973,"comm":"uprobe"}]},{"id":10,"type":"perf_event","prog_id":43,"retprobe":false,"addr":18446744072485508704,"func":"kernel_clone","offset":0,"bpf_cookie":0,"pids":[{"pid":35275,"comm":"kprobe"}]},{"id":11,"type":"perf_event","prog_id":41,"retprobe":true,"addr":18446744072485508704,"func":"kernel_clone","offset":0,"bpf_cookie":0,"pids":[{"pid":35275,"comm":"kprobe"}]}]

    For generic perf events, the displayed information in bpftool is limited to
    the type and configuration, while other attributes such as sample_period,
    sample_freq, etc., are not included.

    The kernel function address won't be exposed if it is not permitted by
    kptr_restrict. The result as follows when kptr_restrict is 2.

    $ tools/bpf/bpftool/bpftool link show
    3: perf_event  prog 14
            event software:cpu-clock
    4: perf_event  prog 14
            event hw-cache:LLC-load-misses
    5: perf_event  prog 14
            event hardware:cpu-cycles
    6: perf_event  prog 19
            tracepoint sched_switch
    7: perf_event  prog 26
            uprobe /home/dev/waken/bpf/uprobe/a.out+0x1338
    8: perf_event  prog 27
            uretprobe /home/dev/waken/bpf/uprobe/a.out+0x1338
    10: perf_event  prog 43
            kprobe kernel_clone
    11: perf_event  prog 41
            kretprobe kernel_clone

    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20230709025630.3735-11-laoar.shao@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-14 15:22:25 +01:00
Jerome Marchand 32d0724a0f bpftool: Add perf event names
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 62b57e3ddd64002f5b3f6fb2ea50b79a2994cfec
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Sun Jul 9 02:56:29 2023 +0000

    bpftool: Add perf event names

    Add new functions and macros to get perf event names. These names except
    the perf_type_name are all copied from
    tool/perf/util/{parse-events,evsel}.c, so that in the future we will
    have a good chance to use the same code.

    Suggested-by: Jiri Olsa <olsajiri@gmail.com>
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20230709025630.3735-10-laoar.shao@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-14 15:22:25 +01:00
Jerome Marchand 4c5c664f68 bpftool: Show kprobe_multi link info
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit edd7f49bb884139af9069dcf59d9f89f1f4ddf3f
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Sun Jul 9 02:56:23 2023 +0000

    bpftool: Show kprobe_multi link info

    Show the already expose kprobe_multi link info in bpftool. The result as
    follows,

    $ tools/bpf/bpftool/bpftool link show
    91: kprobe_multi  prog 244
            kprobe.multi  func_cnt 7
            addr             func [module]
            ffffffff98c44f20 schedule_timeout_interruptible
            ffffffff98c44f60 schedule_timeout_killable
            ffffffff98c44fa0 schedule_timeout_uninterruptible
            ffffffff98c44fe0 schedule_timeout_idle
            ffffffffc075b8d0 xfs_trans_get_efd [xfs]
            ffffffffc0768a10 xfs_trans_get_buf_map [xfs]
            ffffffffc076c320 xfs_trans_get_dqtrx [xfs]
            pids kprobe_multi(188367)
    92: kprobe_multi  prog 244
            kretprobe.multi  func_cnt 7
            addr             func [module]
            ffffffff98c44f20 schedule_timeout_interruptible
            ffffffff98c44f60 schedule_timeout_killable
            ffffffff98c44fa0 schedule_timeout_uninterruptible
            ffffffff98c44fe0 schedule_timeout_idle
            ffffffffc075b8d0 xfs_trans_get_efd [xfs]
            ffffffffc0768a10 xfs_trans_get_buf_map [xfs]
            ffffffffc076c320 xfs_trans_get_dqtrx [xfs]
            pids kprobe_multi(188367)

    $ tools/bpf/bpftool/bpftool link show -j
    [{"id":91,"type":"kprobe_multi","prog_id":244,"retprobe":false,"func_cnt":7,"funcs":[{"addr":18446744071977586464,"func":"schedule_timeout_interruptible","module":null},{"addr":18446744071977586528,"func":"schedule_timeout_killable","module":null},{"addr":18446744071977586592,"func":"schedule_timeout_uninterruptible","module":null},{"addr":18446744071977586656,"func":"schedule_timeout_idle","module":null},{"addr":18446744072643524816,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072643578384,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072643592992,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":188367,"comm":"kprobe_multi"}]},{"id":92,"type":"kprobe_multi","prog_id":244,"retprobe":true,"func_cnt":7,"funcs":[{"addr":18446744071977586464,"func":"schedule_timeout_interruptible","module":null},{"addr":18446744071977586528,"func":"schedule_timeout_killable","module":null},{"addr":18446744071977586592,"func":"schedule_timeout_uninterruptible","module":null},{"addr":18446744071977586656,"func":"schedule_timeout_idle","module":null},{"addr":18446744072643524816,"func":"xfs_trans_get_efd","module":"xfs"},{"addr":18446744072643578384,"func":"xfs_trans_get_buf_map","module":"xfs"},{"addr":18446744072643592992,"func":"xfs_trans_get_dqtrx","module":"xfs"}],"pids":[{"pid":188367,"comm":"kprobe_multi"}]}]

    When kptr_restrict is 2, the result is,

    $ tools/bpf/bpftool/bpftool link show
    91: kprobe_multi  prog 244
            kprobe.multi  func_cnt 7
    92: kprobe_multi  prog 244
            kretprobe.multi  func_cnt 7

    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20230709025630.3735-4-laoar.shao@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-14 15:22:25 +01:00
Viktor Malik 185d3f1238
bpftool: Show target_{obj,btf}_id in tracing link info
JIRA: https://issues.redhat.com/browse/RHEL-9957

commit d7e45eb4802bbb3343624711e43d23b22fe7cc55
Author: Yafang Shao <laoar.shao@gmail.com>
Date:   Wed May 17 10:31:26 2023 +0000

    bpftool: Show target_{obj,btf}_id in tracing link info
    
    The target_btf_id can help us understand which kernel function is
    linked by a tracing prog. The target_btf_id and target_obj_id have
    already been exposed to userspace, so we just need to show them.
    
    The result as follows,
    
    $ tools/bpf/bpftool/bpftool link show
    2: tracing  prog 13
            prog_type tracing  attach_type trace_fentry
            target_obj_id 1  target_btf_id 13964
            pids trace(10673)
    
    $ tools/bpf/bpftool/bpftool link show -j
    [{"id":2,"type":"tracing","prog_id":13,"prog_type":"tracing","attach_type":"trace_fentry","target_obj_id":1,"target_btf_id":13964,"pids":[{"pid":10673,"comm":"trace"}]}]
    
    Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20230517103126.68372-3-laoar.shao@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-10-26 17:06:13 +02:00
Viktor Malik 5010ffb510
bpftool: Show map IDs along with struct_ops links.
JIRA: https://issues.redhat.com/browse/RHEL-9957

commit 74fc8801edc279fb5f778eb91df48e4d3cd951a2
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Fri Apr 21 14:41:31 2023 -0700

    bpftool: Show map IDs along with struct_ops links.
    
    A new link type, BPF_LINK_TYPE_STRUCT_OPS, was added to attach
    struct_ops to links. (226bc6ae6405) It would be helpful for users to
    know which map is associated with the link.
    
    The assumption was that every link is associated with a BPF program, but
    this does not hold true for struct_ops. It would be better to display
    map_id instead of prog_id for struct_ops links. However, some tools may
    rely on the old assumption and need a prog_id.  The discussion on the
    mailing list suggests that tools should parse JSON format. We will maintain
    the existing JSON format by adding a map_id without removing prog_id. As
    for plain text format, we will remove prog_id from the header line and add
    a map_id for struct_ops links.
    
    Signed-off-by: Kui-Feng Lee <kuifeng@meta.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: John Fastabend <john.fastabend@gmail.com>
    Link: https://lore.kernel.org/bpf/20230421214131.352662-1-kuifeng@meta.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-10-12 11:40:54 +02:00
Artem Savkov 9bff341d53 tools: bpftool: print netfilter link info
Bugzilla: https://bugzilla.redhat.com/2221599

commit d0fe92fb5e3df6991c640fb9205d880b68603259
Author: Florian Westphal <fw@strlen.de>
Date:   Fri Apr 21 19:02:58 2023 +0200

    tools: bpftool: print netfilter link info
    
    Dump protocol family, hook and priority value:
    $ bpftool link
    2: netfilter  prog 14
            ip input prio -128
            pids install(3264)
    5: netfilter  prog 14
            ip6 forward prio 21
            pids a.out(3387)
    9: netfilter  prog 14
            ip prerouting prio 123
            pids a.out(5700)
    10: netfilter  prog 14
            ip input prio 21
            pids test2(5701)
    
    v2: Quentin Monnet suggested to also add 'bpftool net' support:
    
    $ bpftool net
    xdp:
    
    tc:
    
    flow_dissector:
    
    netfilter:
    
            ip prerouting prio 21 prog_id 14
            ip input prio -128 prog_id 14
            ip input prio 21 prog_id 14
            ip forward prio 21 prog_id 14
            ip output prio 21 prog_id 14
            ip postrouting prio 21 prog_id 14
    
    'bpftool net' only dumps netfilter link type, links are sorted by protocol
    family, hook and priority.
    
    v5: fix bpf ci failure: libbpf needs small update to prog_type_name[]
        and probe_prog_load helper.
    v4: don't fail with -EOPNOTSUPP in libbpf probe_prog_load, update
        prog_type_name[] with "netfilter" entry (bpf ci)
    v3: fix bpf.h copy, 'reserved' member was removed (Alexei)
        use p_err, not fprintf (Quentin)
    
    Suggested-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/eeeaac99-9053-90c2-aa33-cc1ecb1ae9ca@isovalent.com/
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Signed-off-by: Florian Westphal <fw@strlen.de>
    Link: https://lore.kernel.org/r/20230421170300.24115-6-fw@strlen.de
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:33 +02:00
Viktor Malik d152843a35 bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
Bugzilla: https://bugzilla.redhat.com/2178930

commit 38f0408ef756e738387f7d8f62b8d58ca5938da4
Author: Ilya Leoshkevich <iii@linux.ibm.com>
Date:   Wed Feb 15 00:12:16 2023 +0100

    bpftool: Use bpf_{btf,link,map,prog}_get_info_by_fd()
    
    Use the new type-safe wrappers around bpf_obj_get_info_by_fd().
    
    Split the bpf_obj_get_info_by_fd() call in build_btf_type_table() in
    two, since knowing the type helps with the Memory Sanitizer.
    
    Improve map_parse_fd_and_info() type safety by using
    struct bpf_map_info * instead of void * for info.
    
    Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20230214231221.249277-4-iii@linux.ibm.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:45:39 +02:00
Jerome Marchand 24d396b55c libbpf: Hashmap interface update to allow both long and void* keys/values
Bugzilla: https://bugzilla.redhat.com/2177177

Conflicts: Some minor changes due to missing commits 09b73fe9e3de
("perf smt: Compute SMT from topology") and f0c4b97a2927 ("perf test:
Add basic core_wide expression test").

commit c302378bc157f6a73b6cae4ca67f5f6aa931dcec
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Nov 9 16:26:09 2022 +0200

    libbpf: Hashmap interface update to allow both long and void* keys/values

    An update for libbpf's hashmap interface from void* -> void* to a
    polymorphic one, allowing both long and void* keys and values.

    This simplifies many use cases in libbpf as hashmaps there are mostly
    integer to integer.

    Perf copies hashmap implementation from libbpf and has to be
    updated as well.

    Changes to libbpf, selftests/bpf and perf are packed as a single
    commit to avoid compilation issues with any future bisect.

    Polymorphic interface is acheived by hiding hashmap interface
    functions behind auxiliary macros that take care of necessary
    type casts, for example:

        #define hashmap_cast_ptr(p)                                         \
            ({                                                              \
                    _Static_assert((p) == NULL || sizeof(*(p)) == sizeof(long),\
                                   #p " pointee should be a long-sized integer or a pointer"); \
                    (long *)(p);                                            \
            })

        bool hashmap_find(const struct hashmap *map, long key, long *value);

        #define hashmap__find(map, key, value) \
                    hashmap_find((map), (long)(key), hashmap_cast_ptr(value))

    - hashmap__find macro casts key and value parameters to long
      and long* respectively
    - hashmap_cast_ptr ensures that value pointer points to a memory
      of appropriate size.

    This hack was suggested by Andrii Nakryiko in [1].
    This is a follow up for [2].

    [1] https://lore.kernel.org/bpf/CAEf4BzZ8KFneEJxFAaNCCFPGqp20hSpS2aCj76uRk3-qZUH5xg@mail.gmail.com/
    [2] https://lore.kernel.org/bpf/af1facf9-7bc8-8a3d-0db4-7b3f333589a2@meta.com/T/#m65b28f1d6d969fcd318b556db6a3ad499a42607d

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20221109142611.879983-2-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:02 +02:00
Artem Savkov 1fd5704f0f bpftool: Show parameters of BPF task iterators.
Bugzilla: https://bugzilla.redhat.com/2166911

commit 6bdb6d6be019f697296f52c37865dd7b0ce80750
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Mon Sep 26 11:49:57 2022 -0700

    bpftool: Show parameters of BPF task iterators.
    
    Show tid or pid of iterators if giving an argument of tid or pid
    
    For example, the command `bpftool link list` may list following
    lines.
    
    1: iter  prog 2  target_name bpf_map
    2: iter  prog 3  target_name bpf_prog
    33: iter  prog 225  target_name task_file  tid 1644
            pids test_progs(1644)
    
    Link 33 is a task_file iterator with tid 1644.  For now, only targets
    of task, task_file and task_vma may be with tid or pid to filter out
    tasks other than those belonging to a process (pid) or a thread (tid).
    
    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
    Link: https://lore.kernel.org/bpf/20220926184957.208194-6-kuifeng@fb.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:19 +01:00
Artem Savkov dadbe9d86c bpftool: Add support for querying cgroup_iter link
Bugzilla: https://bugzilla.redhat.com/2166911

commit 6f95de6d713130c953af0a40b13c1da519f91c4e
Author: Hao Luo <haoluo@google.com>
Date:   Mon Aug 29 16:18:28 2022 -0700

    bpftool: Add support for querying cgroup_iter link
    
    Support dumping info of a cgroup_iter link. This includes
    showing the cgroup's id and the order for walking the cgroup
    hierarchy. Example output is as follows:
    
    > bpftool link show
    1: iter  prog 2  target_name bpf_map
    2: iter  prog 3  target_name bpf_prog
    3: iter  prog 12  target_name cgroup  cgroup_id 72  order self_only
    
    > bpftool -p link show
    [{
            "id": 1,
            "type": "iter",
            "prog_id": 2,
            "target_name": "bpf_map"
        },{
            "id": 2,
            "type": "iter",
            "prog_id": 3,
            "target_name": "bpf_prog"
        },{
            "id": 3,
            "type": "iter",
            "prog_id": 12,
            "target_name": "cgroup",
            "cgroup_id": 72,
            "order": "self_only"
        }
    ]
    
    Signed-off-by: Hao Luo <haoluo@google.com>
    Reviewed-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220829231828.1016835-1-haoluo@google.com
    Signed-off-by: Martin KaFai Lau <martin.lau@linux.dev>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-03-06 14:54:05 +01:00
Artem Savkov 5af0440cbb bpftool: Use libbpf_bpf_link_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit c7e7e279dc8367920bddbe96115beef4ec2519a3
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:28 2022 +0000

    bpftool: Use libbpf_bpf_link_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_link_type_str function instead of maintaining its own string
    representation for the bpf_link_type enum.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Acked-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-13-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Artem Savkov 03a91b85bf bpftool: Use libbpf_bpf_attach_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit 1ba5ad36e00f46e3f7676f5de6b87f5a2f57f1f1
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:25 2022 +0000

    bpftool: Use libbpf_bpf_attach_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_attach_type_str function instead of maintaining its own
    string representation for the bpf_attach_type enum.
    
    Note that contrary to other enum types, the variant names that bpftool
    maps bpf_attach_type to do not adhere a simple to follow rule. With
    bpf_prog_type, for example, the textual representation can easily be
    inferred by stripping the BPF_PROG_TYPE_ prefix and lowercasing the
    remaining string. bpf_attach_type violates this rule for various
    variants.
    We decided to fix up this deficiency with this change, meaning that
    bpftool uses the same textual representations as libbpf. Supporting
    tests, completion scripts, and man pages have been adjusted accordingly.
    However, we did add support for accepting (the now undocumented)
    original attach type names when they are provided by users.
    
    For the test (test_bpftool_synctypes.py), I have removed the enum
    representation checks, because we no longer mirror the various enum
    variant names in bpftool source code. For the man page, help text, and
    completion script checks we are now using enum definitions from
    uapi/linux/bpf.h as the source of truth directly.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-10-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
Artem Savkov ea100e07ed bpftool: Use libbpf_bpf_prog_type_str
Bugzilla: https://bugzilla.redhat.com/2137876

commit b700eeb406a6c1f4d955242e06151f11f13d3e29
Author: Daniel Müller <deso@posteo.net>
Date:   Mon May 23 23:04:19 2022 +0000

    bpftool: Use libbpf_bpf_prog_type_str
    
    This change switches bpftool over to using the recently introduced
    libbpf_bpf_prog_type_str function instead of maintaining its own string
    representation for the bpf_prog_type enum.
    
    Signed-off-by: Daniel Müller <deso@posteo.net>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Quentin Monnet <quentin@isovalent.com>
    Link: https://lore.kernel.org/bpf/20220523230428.3077108-4-deso@posteo.net

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-01-05 15:46:28 +01:00
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