diff options
Diffstat (limited to 'tools/bpf/bpftool/map.c')
| -rw-r--r-- | tools/bpf/bpftool/map.c | 45 | 
1 files changed, 28 insertions, 17 deletions
| diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index 407071d54ab1..cae1f1119296 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c @@ -17,6 +17,7 @@  #include <bpf/bpf.h>  #include <bpf/btf.h> +#include <bpf/hashmap.h>  #include "json_writer.h"  #include "main.h" @@ -56,6 +57,8 @@ const char * const map_type_name[] = {  const size_t map_type_name_size = ARRAY_SIZE(map_type_name); +static struct hashmap *map_table; +  static bool map_is_per_cpu(__u32 type)  {  	return type == BPF_MAP_TYPE_PERCPU_HASH || @@ -535,19 +538,18 @@ static int show_map_close_json(int fd, struct bpf_map_info *info)  	if (info->btf_id)  		jsonw_int_field(json_wtr, "btf_id", info->btf_id); -	if (!hash_empty(map_table.table)) { -		struct pinned_obj *obj; +	if (!hashmap__empty(map_table)) { +		struct hashmap_entry *entry;  		jsonw_name(json_wtr, "pinned");  		jsonw_start_array(json_wtr); -		hash_for_each_possible(map_table.table, obj, hash, info->id) { -			if (obj->id == info->id) -				jsonw_string(json_wtr, obj->path); -		} +		hashmap__for_each_key_entry(map_table, entry, +					    u32_as_hash_field(info->id)) +			jsonw_string(json_wtr, entry->value);  		jsonw_end_array(json_wtr);  	} -	emit_obj_refs_json(&refs_table, info->id, json_wtr); +	emit_obj_refs_json(refs_table, info->id, json_wtr);  	jsonw_end_object(json_wtr); @@ -610,13 +612,12 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)  	}  	close(fd); -	if (!hash_empty(map_table.table)) { -		struct pinned_obj *obj; +	if (!hashmap__empty(map_table)) { +		struct hashmap_entry *entry; -		hash_for_each_possible(map_table.table, obj, hash, info->id) { -			if (obj->id == info->id) -				printf("\n\tpinned %s", obj->path); -		} +		hashmap__for_each_key_entry(map_table, entry, +					    u32_as_hash_field(info->id)) +			printf("\n\tpinned %s", (char *)entry->value);  	}  	printf("\n"); @@ -636,7 +637,7 @@ static int show_map_close_plain(int fd, struct bpf_map_info *info)  	if (frozen)  		printf("%sfrozen", info->btf_id ? "  " : ""); -	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids "); +	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");  	printf("\n");  	return 0; @@ -694,8 +695,15 @@ static int do_show(int argc, char **argv)  	int err;  	int fd; -	if (show_pinned) -		build_pinned_obj_table(&map_table, BPF_OBJ_MAP); +	if (show_pinned) { +		map_table = hashmap__new(hash_fn_for_key_as_id, +					 equal_fn_for_key_as_id, NULL); +		if (!map_table) { +			p_err("failed to create hashmap for pinned paths"); +			return -1; +		} +		build_pinned_obj_table(map_table, BPF_OBJ_MAP); +	}  	build_obj_refs_table(&refs_table, BPF_OBJ_MAP);  	if (argc == 2) @@ -740,7 +748,10 @@ static int do_show(int argc, char **argv)  	if (json_output)  		jsonw_end_array(json_wtr); -	delete_obj_refs_table(&refs_table); +	delete_obj_refs_table(refs_table); + +	if (show_pinned) +		delete_pinned_obj_table(map_table);  	return errno == ENOENT ? 0 : -1;  } |