Lines Matching +full:key +full:- +full:2
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
59 return -1; in map_type_from_str()
64 if (map_is_per_cpu(info->type)) in alloc_value()
65 return malloc(round_up(info->value_size, 8) * in alloc_value()
68 return malloc(info->value_size); in alloc_value()
72 struct bpf_map_info *map_info, void *key, in do_dump_btf() argument
78 /* start of key-value pair */ in do_dump_btf()
79 jsonw_start_object(d->jw); in do_dump_btf()
81 if (map_info->btf_key_type_id) { in do_dump_btf()
82 jsonw_name(d->jw, "key"); in do_dump_btf()
84 ret = btf_dumper_type(d, map_info->btf_key_type_id, key); in do_dump_btf()
89 value_id = map_info->btf_vmlinux_value_type_id ? in do_dump_btf()
90 : map_info->btf_value_type_id; in do_dump_btf()
92 if (!map_is_per_cpu(map_info->type)) { in do_dump_btf()
93 jsonw_name(d->jw, "value"); in do_dump_btf()
98 jsonw_name(d->jw, "values"); in do_dump_btf()
99 jsonw_start_array(d->jw); in do_dump_btf()
101 step = round_up(map_info->value_size, 8); in do_dump_btf()
103 jsonw_start_object(d->jw); in do_dump_btf()
104 jsonw_int_field(d->jw, "cpu", i); in do_dump_btf()
105 jsonw_name(d->jw, "value"); in do_dump_btf()
107 jsonw_end_object(d->jw); in do_dump_btf()
111 jsonw_end_array(d->jw); in do_dump_btf()
115 /* end of key-value pair */ in do_dump_btf()
116 jsonw_end_object(d->jw); in do_dump_btf()
132 static void print_entry_json(struct bpf_map_info *info, unsigned char *key, in print_entry_json() argument
137 if (!map_is_per_cpu(info->type)) { in print_entry_json()
138 jsonw_name(json_wtr, "key"); in print_entry_json()
139 print_hex_data_json(key, info->key_size); in print_entry_json()
141 print_hex_data_json(value, info->value_size); in print_entry_json()
142 if (map_is_map_of_maps(info->type)) in print_entry_json()
153 do_dump_btf(&d, info, key, value); in print_entry_json()
159 step = round_up(info->value_size, 8); in print_entry_json()
161 jsonw_name(json_wtr, "key"); in print_entry_json()
162 print_hex_data_json(key, info->key_size); in print_entry_json()
173 info->value_size); in print_entry_json()
186 do_dump_btf(&d, info, key, value); in print_entry_json()
194 print_entry_error_msg(struct bpf_map_info *info, unsigned char *key, in print_entry_error_msg() argument
200 break_names = info->key_size > 16 || msg_size > 16; in print_entry_error_msg()
201 single_line = info->key_size + msg_size <= 24 && !break_names; in print_entry_error_msg()
203 printf("key:%c", break_names ? '\n' : ' '); in print_entry_error_msg()
204 fprint_hex(stdout, key, info->key_size, " "); in print_entry_error_msg()
214 print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno) in print_entry_error() argument
217 * means there is no entry for that key. Do not print an error message in print_entry_error()
220 if ((map_is_map_of_maps(map_info->type) || in print_entry_error()
221 map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT) in print_entry_error()
226 jsonw_name(json_wtr, "key"); in print_entry_error()
227 print_hex_data_json(key, map_info->key_size); in print_entry_error()
239 map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) in print_entry_error()
242 print_entry_error_msg(map_info, key, in print_entry_error()
247 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key, in print_entry_plain() argument
250 if (!map_is_per_cpu(info->type)) { in print_entry_plain()
253 break_names = info->key_size > 16 || info->value_size > 16; in print_entry_plain()
254 single_line = info->key_size + info->value_size <= 24 && in print_entry_plain()
257 if (info->key_size) { in print_entry_plain()
258 printf("key:%c", break_names ? '\n' : ' '); in print_entry_plain()
259 fprint_hex(stdout, key, info->key_size, " "); in print_entry_plain()
264 if (info->value_size) { in print_entry_plain()
265 if (map_is_map_of_maps(info->type)) { in print_entry_plain()
270 fprint_hex(stdout, value, info->value_size, " "); in print_entry_plain()
279 step = round_up(info->value_size, 8); in print_entry_plain()
281 if (info->key_size) { in print_entry_plain()
282 printf("key:\n"); in print_entry_plain()
283 fprint_hex(stdout, key, info->key_size, " "); in print_entry_plain()
286 if (info->value_size) { in print_entry_plain()
289 i, info->value_size > 16 ? '\n' : ' '); in print_entry_plain()
291 info->value_size, " "); in print_entry_plain()
331 if (!map_is_per_cpu(info->type)) in fill_per_cpu_value()
335 step = round_up(info->value_size, 8); in fill_per_cpu_value()
337 memcpy(value + i * step, value, info->value_size); in fill_per_cpu_value()
341 void *key, void *value, __u32 key_size, __u32 value_size, in parse_elem() argument
345 if (!key && !value) in parse_elem()
347 p_err("did not find %s", key ? "key" : "value"); in parse_elem()
348 return -1; in parse_elem()
351 if (is_prefix(*argv, "key")) { in parse_elem()
352 if (!key) { in parse_elem()
354 p_err("duplicate key"); in parse_elem()
356 p_err("unnecessary key"); in parse_elem()
357 return -1; in parse_elem()
360 argv = parse_bytes(argv + 1, "key", key, key_size); in parse_elem()
362 return -1; in parse_elem()
374 return -1; in parse_elem()
379 if (map_is_map_of_maps(info->type)) { in parse_elem()
380 int argc = 2; in parse_elem()
384 return -1; in parse_elem()
388 return -1; in parse_elem()
393 return -1; in parse_elem()
397 } else if (map_is_map_of_progs(info->type)) { in parse_elem()
398 int argc = 2; in parse_elem()
402 return -1; in parse_elem()
406 return -1; in parse_elem()
414 return -1; in parse_elem()
421 return -1; in parse_elem()
426 return parse_elem(argv, info, key, NULL, key_size, value_size, in parse_elem()
432 return -1; in parse_elem()
442 return parse_elem(argv + 1, info, key, value, key_size, in parse_elem()
446 p_err("expected key or value, got: %s", *argv); in parse_elem()
447 return -1; in parse_elem()
454 jsonw_uint_field(wtr, "id", info->id); in show_map_header_json()
455 map_type_str = libbpf_bpf_map_type_str(info->type); in show_map_header_json()
459 jsonw_uint_field(wtr, "type", info->type); in show_map_header_json()
461 if (*info->name) in show_map_header_json()
462 jsonw_string_field(wtr, "name", info->name); in show_map_header_json()
465 jsonw_printf(wtr, "%d", info->map_flags); in show_map_header_json()
480 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino); in show_map_close_json()
482 jsonw_uint_field(json_wtr, "bytes_key", info->key_size); in show_map_close_json()
483 jsonw_uint_field(json_wtr, "bytes_value", info->value_size); in show_map_close_json()
484 jsonw_uint_field(json_wtr, "max_entries", info->max_entries); in show_map_close_json()
490 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) { in show_map_close_json()
521 if (info->btf_id) in show_map_close_json()
522 jsonw_int_field(json_wtr, "btf_id", info->btf_id); in show_map_close_json()
529 hashmap__for_each_key_entry(map_table, entry, info->id) in show_map_close_json()
530 jsonw_string(json_wtr, entry->pvalue); in show_map_close_json()
534 emit_obj_refs_json(refs_table, info->id, json_wtr); in show_map_close_json()
545 printf("%u: ", info->id); in show_map_header_plain()
547 map_type_str = libbpf_bpf_map_type_str(info->type); in show_map_header_plain()
551 printf("type %u ", info->type); in show_map_header_plain()
553 if (*info->name) in show_map_header_plain()
554 printf("name %s ", info->name); in show_map_header_plain()
556 printf("flags 0x%x", info->map_flags); in show_map_header_plain()
557 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino); in show_map_header_plain()
571 info->key_size, info->value_size, info->max_entries); in show_map_close_plain()
577 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) { in show_map_close_plain()
605 hashmap__for_each_key_entry(map_table, entry, info->id) in show_map_close_plain()
606 printf("\n\tpinned %s", (char *)entry->pvalue); in show_map_close_plain()
614 if (info->btf_id || frozen) in show_map_close_plain()
617 if (info->btf_id) in show_map_close_plain()
618 printf("btf_id %d", info->btf_id); in show_map_close_plain()
621 printf("%sfrozen", info->btf_id ? " " : ""); in show_map_close_plain()
623 emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); in show_map_close_plain()
635 int err = -1; in do_show_subset()
640 return -1; in do_show_subset()
686 return -1; in do_show()
692 if (argc == 2) in do_show()
706 errno == EINVAL ? " -- kernel too old?" : ""); in do_show()
739 return errno == ENOENT ? 0 : -1; in do_show()
742 static int dump_map_elem(int fd, void *key, void *value, in dump_map_elem() argument
746 if (bpf_map_lookup_elem(fd, key, value)) { in dump_map_elem()
747 print_entry_error(map_info, key, errno); in dump_map_elem()
748 return -1; in dump_map_elem()
752 print_entry_json(map_info, key, value, btf); in dump_map_elem()
760 do_dump_btf(&d, map_info, key, value); in dump_map_elem()
762 print_entry_plain(map_info, key, value); in dump_map_elem()
778 return -1; in maps_have_btf()
794 if (info->btf_vmlinux_value_type_id) { in get_map_kv_btf()
799 return -errno; in get_map_kv_btf()
803 } else if (info->btf_value_type_id) { in get_map_kv_btf()
804 *btf = btf__load_from_kernel_by_id(info->btf_id); in get_map_kv_btf()
806 err = -errno; in get_map_kv_btf()
826 void *key, *value, *prev_key; in map_dump() local
831 key = malloc(info->key_size); in map_dump()
833 if (!key || !value) { in map_dump()
835 err = -1; in map_dump()
857 if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && in map_dump()
858 info->value_size != 8) { in map_dump()
861 map_type_str = libbpf_bpf_map_type_str(info->type); in map_dump()
866 err = bpf_map_get_next_key(fd, prev_key, key); in map_dump()
872 if (!dump_map_elem(fd, key, value, info, btf, wtr)) in map_dump()
874 prev_key = key; in map_dump()
887 free(key); in map_dump()
902 int err = -1; in do_dump()
904 if (argc != 2) in do_dump()
910 return -1; in do_dump()
941 if (!wtr && i != nb_fds - 1) in do_dump()
962 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value) in alloc_key_value() argument
964 *key = NULL; in alloc_key_value()
967 if (info->key_size) { in alloc_key_value()
968 *key = malloc(info->key_size); in alloc_key_value()
969 if (!*key) { in alloc_key_value()
970 p_err("key mem alloc failed"); in alloc_key_value()
971 return -1; in alloc_key_value()
975 if (info->value_size) { in alloc_key_value()
979 free(*key); in alloc_key_value()
980 *key = NULL; in alloc_key_value()
981 return -1; in alloc_key_value()
994 void *key, *value; in do_update() local
997 if (argc < 2) in do_update()
1002 return -1; in do_update()
1004 err = alloc_key_value(&info, &key, &value); in do_update()
1008 err = parse_elem(argv, &info, key, value, info.key_size, in do_update()
1013 err = bpf_map_update_elem(fd, key, value, flags); in do_update()
1022 free(key); in do_update()
1031 static void print_key_value(struct bpf_map_info *info, void *key, in print_key_value() argument
1041 print_entry_json(info, key, value, btf); in print_key_value()
1051 print_entry_plain(info, key, value); in print_key_value()
1059 do_dump_btf(&d, info, key, value); in print_key_value()
1063 print_entry_plain(info, key, value); in print_key_value()
1072 void *key, *value; in do_lookup() local
1076 if (argc < 2) in do_lookup()
1081 return -1; in do_lookup()
1083 err = alloc_key_value(&info, &key, &value); in do_lookup()
1087 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL); in do_lookup()
1091 err = bpf_map_lookup_elem(fd, key, value); in do_lookup()
1097 printf("key:\n"); in do_lookup()
1098 fprint_hex(stdout, key, info.key_size, " "); in do_lookup()
1109 print_key_value(&info, key, value); in do_lookup()
1112 free(key); in do_lookup()
1123 void *key, *nextkey; in do_getnext() local
1127 if (argc < 2) in do_getnext()
1132 return -1; in do_getnext()
1134 key = malloc(info.key_size); in do_getnext()
1136 if (!key || !nextkey) { in do_getnext()
1138 err = -1; in do_getnext()
1143 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, in do_getnext()
1148 free(key); in do_getnext()
1149 key = NULL; in do_getnext()
1152 err = bpf_map_get_next_key(fd, key, nextkey); in do_getnext()
1154 p_err("can't get next key: %s", strerror(errno)); in do_getnext()
1160 if (key) { in do_getnext()
1161 jsonw_name(json_wtr, "key"); in do_getnext()
1162 print_hex_data_json(key, info.key_size); in do_getnext()
1164 jsonw_null_field(json_wtr, "key"); in do_getnext()
1170 if (key) { in do_getnext()
1171 printf("key:\n"); in do_getnext()
1172 fprint_hex(stdout, key, info.key_size, " "); in do_getnext()
1175 printf("key: None\n"); in do_getnext()
1177 printf("next key:\n"); in do_getnext()
1184 free(key); in do_getnext()
1194 void *key; in do_delete() local
1198 if (argc < 2) in do_delete()
1203 return -1; in do_delete()
1205 key = malloc(info.key_size); in do_delete()
1206 if (!key) { in do_delete()
1208 err = -1; in do_delete()
1212 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL); in do_delete()
1216 err = bpf_map_delete_elem(fd, key); in do_delete()
1221 free(key); in do_delete()
1246 int err = -1, fd; in do_create()
1249 return -1; in do_create()
1253 if (!REQ_ARGS(2)) in do_create()
1254 return -1; in do_create()
1273 } else if (is_prefix(*argv, "key")) { in do_create()
1275 "key size")) in do_create()
1315 if (!REQ_ARGS(2)) in do_create()
1320 return -1; in do_create()
1360 void *key, *value; in do_pop_dequeue() local
1364 if (argc < 2) in do_pop_dequeue()
1369 return -1; in do_pop_dequeue()
1371 err = alloc_key_value(&info, &key, &value); in do_pop_dequeue()
1375 err = bpf_map_lookup_and_delete_elem(fd, key, value); in do_pop_dequeue()
1389 print_key_value(&info, key, value); in do_pop_dequeue()
1392 free(key); in do_pop_dequeue()
1403 if (!REQ_ARGS(2)) in do_freeze()
1404 return -1; in do_freeze()
1408 return -1; in do_freeze()
1436 "Usage: %1$s %2$s { show | list } [MAP]\n" in do_help()
1437 " %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n" in do_help()
1440 " %1$s %2$s dump MAP\n" in do_help()
1441 " %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n" in do_help()
1442 " %1$s %2$s lookup MAP [key DATA]\n" in do_help()
1443 " %1$s %2$s getnext MAP [key DATA]\n" in do_help()
1444 " %1$s %2$s delete MAP key DATA\n" in do_help()
1445 " %1$s %2$s pin MAP FILE\n" in do_help()
1446 " %1$s %2$s event_pipe MAP [cpu N index M]\n" in do_help()
1447 " %1$s %2$s peek MAP\n" in do_help()
1448 " %1$s %2$s push MAP value VALUE\n" in do_help()
1449 " %1$s %2$s pop MAP\n" in do_help()
1450 " %1$s %2$s enqueue MAP value VALUE\n" in do_help()
1451 " %1$s %2$s dequeue MAP\n" in do_help()
1452 " %1$s %2$s freeze MAP\n" in do_help()
1453 " %1$s %2$s help\n" in do_help()
1468 " {-f|--bpffs} | {-n|--nomount} }\n" in do_help()
1470 bin_name, argv[-2]); in do_help()