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
[BPF_LINK_TYPE_NETNS] = "netns",
};
+static struct pinned_obj_table link_table;
+
static int link_parse_fd(int *argc, char ***argv)
{
int fd;
__u32 id = 0;
int err, fd;
- if (show_pinned)
+ if (show_pinned) {
+ hash_init(link_table.table);
build_pinned_obj_table(&link_table, BPF_OBJ_LINK);
+ }
build_obj_refs_table(&refs_table, BPF_OBJ_LINK);
if (argc == 2) {
delete_obj_refs_table(&refs_table);
+ if (show_pinned)
+ delete_pinned_obj_table(&link_table);
+
return errno == ENOENT ? 0 : -1;
}
bool relaxed_maps;
bool use_loader;
struct btf *base_btf;
-struct pinned_obj_table prog_table;
-struct pinned_obj_table map_table;
-struct pinned_obj_table link_table;
struct obj_refs_table refs_table;
static void __noreturn clean_and_exit(int i)
block_mount = false;
bin_name = argv[0];
- hash_init(prog_table.table);
- hash_init(map_table.table);
- hash_init(link_table.table);
-
opterr = 0;
while ((opt = getopt_long(argc, argv, "VhpjfLmndB:",
options, NULL)) >= 0) {
if (json_output)
jsonw_destroy(&json_wtr);
- if (show_pinned) {
- delete_pinned_obj_table(&prog_table);
- delete_pinned_obj_table(&map_table);
- delete_pinned_obj_table(&link_table);
- }
btf__free(base_btf);
return ret;
extern bool relaxed_maps;
extern bool use_loader;
extern struct btf *base_btf;
-extern struct pinned_obj_table prog_table;
-extern struct pinned_obj_table map_table;
-extern struct pinned_obj_table link_table;
extern struct obj_refs_table refs_table;
void __printf(1, 2) p_err(const char *fmt, ...);
const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
+static struct pinned_obj_table map_table;
+
static bool map_is_per_cpu(__u32 type)
{
return type == BPF_MAP_TYPE_PERCPU_HASH ||
int err;
int fd;
- if (show_pinned)
+ if (show_pinned) {
+ hash_init(map_table.table);
build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
+ }
build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
if (argc == 2)
delete_obj_refs_table(&refs_table);
+ if (show_pinned)
+ delete_pinned_obj_table(&map_table);
+
return errno == ENOENT ? 0 : -1;
}
[__MAX_BPF_ATTACH_TYPE] = NULL,
};
+static struct pinned_obj_table prog_table;
+
static enum bpf_attach_type parse_attach_type(const char *str)
{
enum bpf_attach_type type;
int err;
int fd;
- if (show_pinned)
+ if (show_pinned) {
+ hash_init(prog_table.table);
build_pinned_obj_table(&prog_table, BPF_OBJ_PROG);
+ }
build_obj_refs_table(&refs_table, BPF_OBJ_PROG);
if (argc == 2)
delete_obj_refs_table(&refs_table);
+ if (show_pinned)
+ delete_pinned_obj_table(&prog_table);
+
return err;
}