2020-04-29 08:16:12 +08:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
|
|
|
|
/* Copyright (C) 2020 Facebook */
|
|
|
|
|
|
|
|
#include <errno.h>
|
2022-01-07 23:26:20 +08:00
|
|
|
#include <linux/err.h>
|
2020-04-29 08:16:12 +08:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <bpf/bpf.h>
|
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
2021-10-24 04:51:52 +08:00
|
|
|
#include <bpf/hashmap.h>
|
2020-04-29 08:16:12 +08:00
|
|
|
|
|
|
|
#include "json_writer.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
static const char * const link_type_name[] = {
|
|
|
|
[BPF_LINK_TYPE_UNSPEC] = "unspec",
|
|
|
|
[BPF_LINK_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
|
|
|
|
[BPF_LINK_TYPE_TRACING] = "tracing",
|
|
|
|
[BPF_LINK_TYPE_CGROUP] = "cgroup",
|
2020-05-10 01:59:20 +08:00
|
|
|
[BPF_LINK_TYPE_ITER] = "iter",
|
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-05-31 16:28:42 +08:00
|
|
|
[BPF_LINK_TYPE_NETNS] = "netns",
|
2022-03-31 23:45:54 +08:00
|
|
|
[BPF_LINK_TYPE_XDP] = "xdp",
|
|
|
|
[BPF_LINK_TYPE_PERF_EVENT] = "perf_event",
|
|
|
|
[BPF_LINK_TYPE_KPROBE_MULTI] = "kprobe_multi",
|
2022-05-11 04:59:19 +08:00
|
|
|
[BPF_LINK_TYPE_STRUCT_OPS] = "struct_ops",
|
2020-04-29 08:16:12 +08:00
|
|
|
};
|
|
|
|
|
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
2021-10-24 04:51:52 +08:00
|
|
|
static struct hashmap *link_table;
|
2021-10-24 04:51:51 +08:00
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
static int link_parse_fd(int *argc, char ***argv)
|
|
|
|
{
|
2020-08-01 02:28:29 +08:00
|
|
|
int fd;
|
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
if (is_prefix(**argv, "id")) {
|
|
|
|
unsigned int id;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
id = strtoul(**argv, &endptr, 0);
|
|
|
|
if (*endptr) {
|
|
|
|
p_err("can't parse %s as ID", **argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
2020-08-01 02:28:29 +08:00
|
|
|
fd = bpf_link_get_fd_by_id(id);
|
|
|
|
if (fd < 0)
|
|
|
|
p_err("failed to get link with ID %d: %s", id, strerror(errno));
|
|
|
|
return fd;
|
2020-04-29 08:16:12 +08:00
|
|
|
} else if (is_prefix(**argv, "pinned")) {
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
path = **argv;
|
|
|
|
NEXT_ARGP();
|
|
|
|
|
|
|
|
return open_obj_pinned_any(path, BPF_OBJ_LINK);
|
|
|
|
}
|
|
|
|
|
|
|
|
p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
show_link_header_json(struct bpf_link_info *info, json_writer_t *wtr)
|
|
|
|
{
|
|
|
|
jsonw_uint_field(wtr, "id", info->id);
|
|
|
|
if (info->type < ARRAY_SIZE(link_type_name))
|
|
|
|
jsonw_string_field(wtr, "type", link_type_name[info->type]);
|
|
|
|
else
|
|
|
|
jsonw_uint_field(wtr, "type", info->type);
|
|
|
|
|
|
|
|
jsonw_uint_field(json_wtr, "prog_id", info->prog_id);
|
|
|
|
}
|
|
|
|
|
2020-05-31 16:28:41 +08:00
|
|
|
static void show_link_attach_type_json(__u32 attach_type, json_writer_t *wtr)
|
|
|
|
{
|
|
|
|
if (attach_type < ARRAY_SIZE(attach_type_name))
|
|
|
|
jsonw_string_field(wtr, "attach_type",
|
|
|
|
attach_type_name[attach_type]);
|
|
|
|
else
|
|
|
|
jsonw_uint_field(wtr, "attach_type", attach_type);
|
|
|
|
}
|
|
|
|
|
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-22 02:44:20 +08:00
|
|
|
static bool is_iter_map_target(const char *target_name)
|
|
|
|
{
|
|
|
|
return strcmp(target_name, "bpf_map_elem") == 0 ||
|
|
|
|
strcmp(target_name, "bpf_sk_storage_map") == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr)
|
|
|
|
{
|
|
|
|
const char *target_name = u64_to_ptr(info->iter.target_name);
|
|
|
|
|
|
|
|
jsonw_string_field(wtr, "target_name", target_name);
|
|
|
|
|
|
|
|
if (is_iter_map_target(target_name))
|
|
|
|
jsonw_uint_field(wtr, "map_id", info->iter.map.map_id);
|
|
|
|
}
|
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
static int get_prog_info(int prog_id, struct bpf_prog_info *info)
|
|
|
|
{
|
|
|
|
__u32 len = sizeof(*info);
|
|
|
|
int err, prog_fd;
|
|
|
|
|
|
|
|
prog_fd = bpf_prog_get_fd_by_id(prog_id);
|
|
|
|
if (prog_fd < 0)
|
|
|
|
return prog_fd;
|
|
|
|
|
|
|
|
memset(info, 0, sizeof(*info));
|
|
|
|
err = bpf_obj_get_info_by_fd(prog_fd, info, &len);
|
|
|
|
if (err)
|
|
|
|
p_err("can't get prog info: %s", strerror(errno));
|
|
|
|
close(prog_fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_link_close_json(int fd, struct bpf_link_info *info)
|
|
|
|
{
|
|
|
|
struct bpf_prog_info prog_info;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
jsonw_start_object(json_wtr);
|
|
|
|
|
|
|
|
show_link_header_json(info, json_wtr);
|
|
|
|
|
|
|
|
switch (info->type) {
|
|
|
|
case BPF_LINK_TYPE_RAW_TRACEPOINT:
|
|
|
|
jsonw_string_field(json_wtr, "tp_name",
|
2020-08-14 04:49:37 +08:00
|
|
|
u64_to_ptr(info->raw_tracepoint.tp_name));
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
|
|
|
case BPF_LINK_TYPE_TRACING:
|
|
|
|
err = get_prog_info(info->prog_id, &prog_info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2020-06-24 22:31:24 +08:00
|
|
|
if (prog_info.type < prog_type_name_size)
|
2020-04-29 08:16:12 +08:00
|
|
|
jsonw_string_field(json_wtr, "prog_type",
|
|
|
|
prog_type_name[prog_info.type]);
|
|
|
|
else
|
|
|
|
jsonw_uint_field(json_wtr, "prog_type",
|
|
|
|
prog_info.type);
|
|
|
|
|
2020-05-31 16:28:41 +08:00
|
|
|
show_link_attach_type_json(info->tracing.attach_type,
|
|
|
|
json_wtr);
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
|
|
|
case BPF_LINK_TYPE_CGROUP:
|
|
|
|
jsonw_lluint_field(json_wtr, "cgroup_id",
|
|
|
|
info->cgroup.cgroup_id);
|
2020-05-31 16:28:41 +08:00
|
|
|
show_link_attach_type_json(info->cgroup.attach_type, json_wtr);
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
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-22 02:44:20 +08:00
|
|
|
case BPF_LINK_TYPE_ITER:
|
|
|
|
show_iter_json(info, json_wtr);
|
|
|
|
break;
|
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-05-31 16:28:42 +08:00
|
|
|
case BPF_LINK_TYPE_NETNS:
|
|
|
|
jsonw_uint_field(json_wtr, "netns_ino",
|
|
|
|
info->netns.netns_ino);
|
|
|
|
show_link_attach_type_json(info->netns.attach_type, json_wtr);
|
|
|
|
break;
|
2020-04-29 08:16:12 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
2021-10-24 04:51:52 +08:00
|
|
|
if (!hashmap__empty(link_table)) {
|
|
|
|
struct hashmap_entry *entry;
|
2020-04-29 08:16:12 +08:00
|
|
|
|
|
|
|
jsonw_name(json_wtr, "pinned");
|
|
|
|
jsonw_start_array(json_wtr);
|
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
2021-10-24 04:51:52 +08:00
|
|
|
hashmap__for_each_key_entry(link_table, entry,
|
|
|
|
u32_as_hash_field(info->id))
|
|
|
|
jsonw_string(json_wtr, entry->value);
|
2020-04-29 08:16:12 +08:00
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
}
|
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-20 07:17:02 +08:00
|
|
|
|
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
2021-10-24 04:51:54 +08:00
|
|
|
emit_obj_refs_json(refs_table, info->id, json_wtr);
|
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-20 07:17:02 +08:00
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
jsonw_end_object(json_wtr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_link_header_plain(struct bpf_link_info *info)
|
|
|
|
{
|
|
|
|
printf("%u: ", info->id);
|
|
|
|
if (info->type < ARRAY_SIZE(link_type_name))
|
|
|
|
printf("%s ", link_type_name[info->type]);
|
|
|
|
else
|
|
|
|
printf("type %u ", info->type);
|
|
|
|
|
|
|
|
printf("prog %u ", info->prog_id);
|
|
|
|
}
|
|
|
|
|
2020-05-31 16:28:41 +08:00
|
|
|
static void show_link_attach_type_plain(__u32 attach_type)
|
|
|
|
{
|
|
|
|
if (attach_type < ARRAY_SIZE(attach_type_name))
|
|
|
|
printf("attach_type %s ", attach_type_name[attach_type]);
|
|
|
|
else
|
|
|
|
printf("attach_type %u ", attach_type);
|
|
|
|
}
|
|
|
|
|
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-22 02:44:20 +08:00
|
|
|
static void show_iter_plain(struct bpf_link_info *info)
|
|
|
|
{
|
|
|
|
const char *target_name = u64_to_ptr(info->iter.target_name);
|
|
|
|
|
|
|
|
printf("target_name %s ", target_name);
|
|
|
|
|
|
|
|
if (is_iter_map_target(target_name))
|
|
|
|
printf("map_id %u ", info->iter.map.map_id);
|
|
|
|
}
|
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
static int show_link_close_plain(int fd, struct bpf_link_info *info)
|
|
|
|
{
|
|
|
|
struct bpf_prog_info prog_info;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
show_link_header_plain(info);
|
|
|
|
|
|
|
|
switch (info->type) {
|
|
|
|
case BPF_LINK_TYPE_RAW_TRACEPOINT:
|
|
|
|
printf("\n\ttp '%s' ",
|
2020-08-14 04:49:37 +08:00
|
|
|
(const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
|
|
|
case BPF_LINK_TYPE_TRACING:
|
|
|
|
err = get_prog_info(info->prog_id, &prog_info);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2020-06-24 22:31:24 +08:00
|
|
|
if (prog_info.type < prog_type_name_size)
|
2020-04-29 08:16:12 +08:00
|
|
|
printf("\n\tprog_type %s ",
|
|
|
|
prog_type_name[prog_info.type]);
|
|
|
|
else
|
|
|
|
printf("\n\tprog_type %u ", prog_info.type);
|
|
|
|
|
2020-05-31 16:28:41 +08:00
|
|
|
show_link_attach_type_plain(info->tracing.attach_type);
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
|
|
|
case BPF_LINK_TYPE_CGROUP:
|
|
|
|
printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);
|
2020-05-31 16:28:41 +08:00
|
|
|
show_link_attach_type_plain(info->cgroup.attach_type);
|
2020-04-29 08:16:12 +08:00
|
|
|
break;
|
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-22 02:44:20 +08:00
|
|
|
case BPF_LINK_TYPE_ITER:
|
|
|
|
show_iter_plain(info);
|
|
|
|
break;
|
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-05-31 16:28:42 +08:00
|
|
|
case BPF_LINK_TYPE_NETNS:
|
|
|
|
printf("\n\tnetns_ino %u ", info->netns.netns_ino);
|
|
|
|
show_link_attach_type_plain(info->netns.attach_type);
|
|
|
|
break;
|
2020-04-29 08:16:12 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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
2021-10-24 04:51:52 +08:00
|
|
|
if (!hashmap__empty(link_table)) {
|
|
|
|
struct hashmap_entry *entry;
|
2020-04-29 08:16:12 +08:00
|
|
|
|
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
2021-10-24 04:51:52 +08:00
|
|
|
hashmap__for_each_key_entry(link_table, entry,
|
|
|
|
u32_as_hash_field(info->id))
|
|
|
|
printf("\n\tpinned %s", (char *)entry->value);
|
2020-04-29 08:16:12 +08:00
|
|
|
}
|
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
2021-10-24 04:51:54 +08:00
|
|
|
emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
|
2020-04-29 08:16:12 +08:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_show_link(int fd)
|
|
|
|
{
|
|
|
|
struct bpf_link_info info;
|
|
|
|
__u32 len = sizeof(info);
|
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-22 02:44:20 +08:00
|
|
|
char buf[256];
|
2020-04-29 08:16:12 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
again:
|
|
|
|
err = bpf_obj_get_info_by_fd(fd, &info, &len);
|
|
|
|
if (err) {
|
|
|
|
p_err("can't get link info: %s",
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
|
|
|
|
!info.raw_tracepoint.tp_name) {
|
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-22 02:44:20 +08:00
|
|
|
info.raw_tracepoint.tp_name = (unsigned long)&buf;
|
|
|
|
info.raw_tracepoint.tp_name_len = sizeof(buf);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
if (info.type == BPF_LINK_TYPE_ITER &&
|
|
|
|
!info.iter.target_name) {
|
|
|
|
info.iter.target_name = (unsigned long)&buf;
|
|
|
|
info.iter.target_name_len = sizeof(buf);
|
2020-04-29 08:16:12 +08:00
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
show_link_close_json(fd, &info);
|
|
|
|
else
|
|
|
|
show_link_close_plain(fd, &info);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_show(int argc, char **argv)
|
|
|
|
{
|
|
|
|
__u32 id = 0;
|
|
|
|
int err, fd;
|
|
|
|
|
2021-10-24 04:51:51 +08:00
|
|
|
if (show_pinned) {
|
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
2021-10-24 04:51:52 +08:00
|
|
|
link_table = hashmap__new(hash_fn_for_key_as_id,
|
|
|
|
equal_fn_for_key_as_id, NULL);
|
2022-01-07 23:26:20 +08:00
|
|
|
if (IS_ERR(link_table)) {
|
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
2021-10-24 04:51:52 +08:00
|
|
|
p_err("failed to create hashmap for pinned paths");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
build_pinned_obj_table(link_table, BPF_OBJ_LINK);
|
2021-10-24 04:51:51 +08:00
|
|
|
}
|
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-20 07:17:02 +08:00
|
|
|
build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
|
2020-04-29 08:16:12 +08:00
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
fd = link_parse_fd(&argc, &argv);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
return do_show_link(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
return BAD_ARG();
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_start_array(json_wtr);
|
|
|
|
while (true) {
|
|
|
|
err = bpf_link_get_next_id(id, &id);
|
|
|
|
if (err) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
break;
|
|
|
|
p_err("can't get next link: %s%s", strerror(errno),
|
|
|
|
errno == EINVAL ? " -- kernel too old?" : "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = bpf_link_get_fd_by_id(id);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
continue;
|
|
|
|
p_err("can't get link by id (%u): %s",
|
|
|
|
id, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = do_show_link(fd);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (json_output)
|
|
|
|
jsonw_end_array(json_wtr);
|
|
|
|
|
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
2021-10-24 04:51:54 +08:00
|
|
|
delete_obj_refs_table(refs_table);
|
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-20 07:17:02 +08:00
|
|
|
|
2021-10-24 04:51:51 +08:00
|
|
|
if (show_pinned)
|
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
2021-10-24 04:51:52 +08:00
|
|
|
delete_pinned_obj_table(link_table);
|
2021-10-24 04:51:51 +08:00
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
return errno == ENOENT ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_pin(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = do_pin_any(argc, argv, link_parse_fd);
|
|
|
|
if (!err && json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-08-01 02:28:29 +08:00
|
|
|
static int do_detach(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int err, fd;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
p_err("link specifier is invalid or missing\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = link_parse_fd(&argc, &argv);
|
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err = bpf_link_detach(fd);
|
|
|
|
if (err)
|
|
|
|
err = -errno;
|
|
|
|
close(fd);
|
|
|
|
if (err) {
|
|
|
|
p_err("failed link detach: %s", strerror(-err));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_output)
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-29 08:16:12 +08:00
|
|
|
static int do_help(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (json_output) {
|
|
|
|
jsonw_null(json_wtr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: %1$s %2$s { show | list } [LINK]\n"
|
|
|
|
" %1$s %2$s pin LINK FILE\n"
|
2020-08-01 02:28:29 +08:00
|
|
|
" %1$s %2$s detach LINK\n"
|
2020-04-29 08:16:12 +08:00
|
|
|
" %1$s %2$s help\n"
|
|
|
|
"\n"
|
|
|
|
" " HELP_SPEC_LINK "\n"
|
2021-07-31 05:54:32 +08:00
|
|
|
" " HELP_SPEC_OPTIONS " |\n"
|
|
|
|
" {-f|--bpffs} | {-n|--nomount} }\n"
|
2020-04-29 08:16:12 +08:00
|
|
|
"",
|
|
|
|
bin_name, argv[-2]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct cmd cmds[] = {
|
|
|
|
{ "show", do_show },
|
|
|
|
{ "list", do_show },
|
|
|
|
{ "help", do_help },
|
|
|
|
{ "pin", do_pin },
|
2020-08-01 02:28:29 +08:00
|
|
|
{ "detach", do_detach },
|
2020-04-29 08:16:12 +08:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int do_link(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return cmd_select(cmds, argc, argv, do_help);
|
|
|
|
}
|