1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #include <assert.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <linux/err.h>
8 #include <linux/kernel.h>
9 #include <net/if.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #include <bpf/bpf.h>
19 #include <bpf/btf.h>
20
21 #include "json_writer.h"
22 #include "main.h"
23
24 const char * const map_type_name[] = {
25 [BPF_MAP_TYPE_UNSPEC] = "unspec",
26 [BPF_MAP_TYPE_HASH] = "hash",
27 [BPF_MAP_TYPE_ARRAY] = "array",
28 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
29 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
30 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
31 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
32 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
33 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
34 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
35 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
36 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
37 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
38 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
39 [BPF_MAP_TYPE_DEVMAP] = "devmap",
40 [BPF_MAP_TYPE_DEVMAP_HASH] = "devmap_hash",
41 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
42 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
43 [BPF_MAP_TYPE_XSKMAP] = "xskmap",
44 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
45 [BPF_MAP_TYPE_CGROUP_STORAGE] = "cgroup_storage",
46 [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY] = "reuseport_sockarray",
47 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
48 [BPF_MAP_TYPE_QUEUE] = "queue",
49 [BPF_MAP_TYPE_STACK] = "stack",
50 [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage",
51 [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops",
52 [BPF_MAP_TYPE_RINGBUF] = "ringbuf",
53 [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage",
54 [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage",
55 };
56
57 const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
58
map_is_per_cpu(__u32 type)59 static bool map_is_per_cpu(__u32 type)
60 {
61 return type == BPF_MAP_TYPE_PERCPU_HASH ||
62 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
63 type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
64 type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
65 }
66
map_is_map_of_maps(__u32 type)67 static bool map_is_map_of_maps(__u32 type)
68 {
69 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
70 type == BPF_MAP_TYPE_HASH_OF_MAPS;
71 }
72
map_is_map_of_progs(__u32 type)73 static bool map_is_map_of_progs(__u32 type)
74 {
75 return type == BPF_MAP_TYPE_PROG_ARRAY;
76 }
77
map_type_from_str(const char * type)78 static int map_type_from_str(const char *type)
79 {
80 unsigned int i;
81
82 for (i = 0; i < ARRAY_SIZE(map_type_name); i++)
83 /* Don't allow prefixing in case of possible future shadowing */
84 if (map_type_name[i] && !strcmp(map_type_name[i], type))
85 return i;
86 return -1;
87 }
88
alloc_value(struct bpf_map_info * info)89 static void *alloc_value(struct bpf_map_info *info)
90 {
91 if (map_is_per_cpu(info->type))
92 return malloc(round_up(info->value_size, 8) *
93 get_possible_cpus());
94 else
95 return malloc(info->value_size);
96 }
97
do_dump_btf(const struct btf_dumper * d,struct bpf_map_info * map_info,void * key,void * value)98 static int do_dump_btf(const struct btf_dumper *d,
99 struct bpf_map_info *map_info, void *key,
100 void *value)
101 {
102 __u32 value_id;
103 int ret = 0;
104
105 /* start of key-value pair */
106 jsonw_start_object(d->jw);
107
108 if (map_info->btf_key_type_id) {
109 jsonw_name(d->jw, "key");
110
111 ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
112 if (ret)
113 goto err_end_obj;
114 }
115
116 value_id = map_info->btf_vmlinux_value_type_id ?
117 : map_info->btf_value_type_id;
118
119 if (!map_is_per_cpu(map_info->type)) {
120 jsonw_name(d->jw, "value");
121 ret = btf_dumper_type(d, value_id, value);
122 } else {
123 unsigned int i, n, step;
124
125 jsonw_name(d->jw, "values");
126 jsonw_start_array(d->jw);
127 n = get_possible_cpus();
128 step = round_up(map_info->value_size, 8);
129 for (i = 0; i < n; i++) {
130 jsonw_start_object(d->jw);
131 jsonw_int_field(d->jw, "cpu", i);
132 jsonw_name(d->jw, "value");
133 ret = btf_dumper_type(d, value_id, value + i * step);
134 jsonw_end_object(d->jw);
135 if (ret)
136 break;
137 }
138 jsonw_end_array(d->jw);
139 }
140
141 err_end_obj:
142 /* end of key-value pair */
143 jsonw_end_object(d->jw);
144
145 return ret;
146 }
147
get_btf_writer(void)148 static json_writer_t *get_btf_writer(void)
149 {
150 json_writer_t *jw = jsonw_new(stdout);
151
152 if (!jw)
153 return NULL;
154 jsonw_pretty(jw, true);
155
156 return jw;
157 }
158
print_entry_json(struct bpf_map_info * info,unsigned char * key,unsigned char * value,struct btf * btf)159 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
160 unsigned char *value, struct btf *btf)
161 {
162 jsonw_start_object(json_wtr);
163
164 if (!map_is_per_cpu(info->type)) {
165 jsonw_name(json_wtr, "key");
166 print_hex_data_json(key, info->key_size);
167 jsonw_name(json_wtr, "value");
168 print_hex_data_json(value, info->value_size);
169 if (btf) {
170 struct btf_dumper d = {
171 .btf = btf,
172 .jw = json_wtr,
173 .is_plain_text = false,
174 };
175
176 jsonw_name(json_wtr, "formatted");
177 do_dump_btf(&d, info, key, value);
178 }
179 } else {
180 unsigned int i, n, step;
181
182 n = get_possible_cpus();
183 step = round_up(info->value_size, 8);
184
185 jsonw_name(json_wtr, "key");
186 print_hex_data_json(key, info->key_size);
187
188 jsonw_name(json_wtr, "values");
189 jsonw_start_array(json_wtr);
190 for (i = 0; i < n; i++) {
191 jsonw_start_object(json_wtr);
192
193 jsonw_int_field(json_wtr, "cpu", i);
194
195 jsonw_name(json_wtr, "value");
196 print_hex_data_json(value + i * step,
197 info->value_size);
198
199 jsonw_end_object(json_wtr);
200 }
201 jsonw_end_array(json_wtr);
202 if (btf) {
203 struct btf_dumper d = {
204 .btf = btf,
205 .jw = json_wtr,
206 .is_plain_text = false,
207 };
208
209 jsonw_name(json_wtr, "formatted");
210 do_dump_btf(&d, info, key, value);
211 }
212 }
213
214 jsonw_end_object(json_wtr);
215 }
216
217 static void
print_entry_error_msg(struct bpf_map_info * info,unsigned char * key,const char * error_msg)218 print_entry_error_msg(struct bpf_map_info *info, unsigned char *key,
219 const char *error_msg)
220 {
221 int msg_size = strlen(error_msg);
222 bool single_line, break_names;
223
224 break_names = info->key_size > 16 || msg_size > 16;
225 single_line = info->key_size + msg_size <= 24 && !break_names;
226
227 printf("key:%c", break_names ? '\n' : ' ');
228 fprint_hex(stdout, key, info->key_size, " ");
229
230 printf(single_line ? " " : "\n");
231
232 printf("value:%c%s", break_names ? '\n' : ' ', error_msg);
233
234 printf("\n");
235 }
236
237 static void
print_entry_error(struct bpf_map_info * map_info,void * key,int lookup_errno)238 print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno)
239 {
240 /* For prog_array maps or arrays of maps, failure to lookup the value
241 * means there is no entry for that key. Do not print an error message
242 * in that case.
243 */
244 if ((map_is_map_of_maps(map_info->type) ||
245 map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT)
246 return;
247
248 if (json_output) {
249 jsonw_start_object(json_wtr); /* entry */
250 jsonw_name(json_wtr, "key");
251 print_hex_data_json(key, map_info->key_size);
252 jsonw_name(json_wtr, "value");
253 jsonw_start_object(json_wtr); /* error */
254 jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
255 jsonw_end_object(json_wtr); /* error */
256 jsonw_end_object(json_wtr); /* entry */
257 } else {
258 const char *msg = NULL;
259
260 if (lookup_errno == ENOENT)
261 msg = "<no entry>";
262 else if (lookup_errno == ENOSPC &&
263 map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
264 msg = "<cannot read>";
265
266 print_entry_error_msg(map_info, key,
267 msg ? : strerror(lookup_errno));
268 }
269 }
270
print_entry_plain(struct bpf_map_info * info,unsigned char * key,unsigned char * value)271 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
272 unsigned char *value)
273 {
274 if (!map_is_per_cpu(info->type)) {
275 bool single_line, break_names;
276
277 break_names = info->key_size > 16 || info->value_size > 16;
278 single_line = info->key_size + info->value_size <= 24 &&
279 !break_names;
280
281 if (info->key_size) {
282 printf("key:%c", break_names ? '\n' : ' ');
283 fprint_hex(stdout, key, info->key_size, " ");
284
285 printf(single_line ? " " : "\n");
286 }
287
288 if (info->value_size) {
289 printf("value:%c", break_names ? '\n' : ' ');
290 fprint_hex(stdout, value, info->value_size, " ");
291 }
292
293 printf("\n");
294 } else {
295 unsigned int i, n, step;
296
297 n = get_possible_cpus();
298 step = round_up(info->value_size, 8);
299
300 if (info->key_size) {
301 printf("key:\n");
302 fprint_hex(stdout, key, info->key_size, " ");
303 printf("\n");
304 }
305 if (info->value_size) {
306 for (i = 0; i < n; i++) {
307 printf("value (CPU %02d):%c",
308 i, info->value_size > 16 ? '\n' : ' ');
309 fprint_hex(stdout, value + i * step,
310 info->value_size, " ");
311 printf("\n");
312 }
313 }
314 }
315 }
316
parse_bytes(char ** argv,const char * name,unsigned char * val,unsigned int n)317 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
318 unsigned int n)
319 {
320 unsigned int i = 0, base = 0;
321 char *endptr;
322
323 if (is_prefix(*argv, "hex")) {
324 base = 16;
325 argv++;
326 }
327
328 while (i < n && argv[i]) {
329 val[i] = strtoul(argv[i], &endptr, base);
330 if (*endptr) {
331 p_err("error parsing byte: %s", argv[i]);
332 return NULL;
333 }
334 i++;
335 }
336
337 if (i != n) {
338 p_err("%s expected %d bytes got %d", name, n, i);
339 return NULL;
340 }
341
342 return argv + i;
343 }
344
345 /* on per cpu maps we must copy the provided value on all value instances */
fill_per_cpu_value(struct bpf_map_info * info,void * value)346 static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
347 {
348 unsigned int i, n, step;
349
350 if (!map_is_per_cpu(info->type))
351 return;
352
353 n = get_possible_cpus();
354 step = round_up(info->value_size, 8);
355 for (i = 1; i < n; i++)
356 memcpy(value + i * step, value, info->value_size);
357 }
358
parse_elem(char ** argv,struct bpf_map_info * info,void * key,void * value,__u32 key_size,__u32 value_size,__u32 * flags,__u32 ** value_fd)359 static int parse_elem(char **argv, struct bpf_map_info *info,
360 void *key, void *value, __u32 key_size, __u32 value_size,
361 __u32 *flags, __u32 **value_fd)
362 {
363 if (!*argv) {
364 if (!key && !value)
365 return 0;
366 p_err("did not find %s", key ? "key" : "value");
367 return -1;
368 }
369
370 if (is_prefix(*argv, "key")) {
371 if (!key) {
372 if (key_size)
373 p_err("duplicate key");
374 else
375 p_err("unnecessary key");
376 return -1;
377 }
378
379 argv = parse_bytes(argv + 1, "key", key, key_size);
380 if (!argv)
381 return -1;
382
383 return parse_elem(argv, info, NULL, value, key_size, value_size,
384 flags, value_fd);
385 } else if (is_prefix(*argv, "value")) {
386 int fd;
387
388 if (!value) {
389 if (value_size)
390 p_err("duplicate value");
391 else
392 p_err("unnecessary value");
393 return -1;
394 }
395
396 argv++;
397
398 if (map_is_map_of_maps(info->type)) {
399 int argc = 2;
400
401 if (value_size != 4) {
402 p_err("value smaller than 4B for map in map?");
403 return -1;
404 }
405 if (!argv[0] || !argv[1]) {
406 p_err("not enough value arguments for map in map");
407 return -1;
408 }
409
410 fd = map_parse_fd(&argc, &argv);
411 if (fd < 0)
412 return -1;
413
414 *value_fd = value;
415 **value_fd = fd;
416 } else if (map_is_map_of_progs(info->type)) {
417 int argc = 2;
418
419 if (value_size != 4) {
420 p_err("value smaller than 4B for map of progs?");
421 return -1;
422 }
423 if (!argv[0] || !argv[1]) {
424 p_err("not enough value arguments for map of progs");
425 return -1;
426 }
427 if (is_prefix(*argv, "id"))
428 p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
429 " by some process or pinned otherwise update will be lost");
430
431 fd = prog_parse_fd(&argc, &argv);
432 if (fd < 0)
433 return -1;
434
435 *value_fd = value;
436 **value_fd = fd;
437 } else {
438 argv = parse_bytes(argv, "value", value, value_size);
439 if (!argv)
440 return -1;
441
442 fill_per_cpu_value(info, value);
443 }
444
445 return parse_elem(argv, info, key, NULL, key_size, value_size,
446 flags, NULL);
447 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
448 is_prefix(*argv, "exist")) {
449 if (!flags) {
450 p_err("flags specified multiple times: %s", *argv);
451 return -1;
452 }
453
454 if (is_prefix(*argv, "any"))
455 *flags = BPF_ANY;
456 else if (is_prefix(*argv, "noexist"))
457 *flags = BPF_NOEXIST;
458 else if (is_prefix(*argv, "exist"))
459 *flags = BPF_EXIST;
460
461 return parse_elem(argv + 1, info, key, value, key_size,
462 value_size, NULL, value_fd);
463 }
464
465 p_err("expected key or value, got: %s", *argv);
466 return -1;
467 }
468
show_map_header_json(struct bpf_map_info * info,json_writer_t * wtr)469 static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr)
470 {
471 jsonw_uint_field(wtr, "id", info->id);
472 if (info->type < ARRAY_SIZE(map_type_name))
473 jsonw_string_field(wtr, "type", map_type_name[info->type]);
474 else
475 jsonw_uint_field(wtr, "type", info->type);
476
477 if (*info->name)
478 jsonw_string_field(wtr, "name", info->name);
479
480 jsonw_name(wtr, "flags");
481 jsonw_printf(wtr, "%d", info->map_flags);
482 }
483
show_map_close_json(int fd,struct bpf_map_info * info)484 static int show_map_close_json(int fd, struct bpf_map_info *info)
485 {
486 char *memlock, *frozen_str;
487 int frozen = 0;
488
489 memlock = get_fdinfo(fd, "memlock");
490 frozen_str = get_fdinfo(fd, "frozen");
491
492 jsonw_start_object(json_wtr);
493
494 show_map_header_json(info, json_wtr);
495
496 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
497
498 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
499 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
500 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
501
502 if (memlock)
503 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
504 free(memlock);
505
506 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
507 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
508 char *owner_jited = get_fdinfo(fd, "owner_jited");
509
510 if (owner_prog_type) {
511 unsigned int prog_type = atoi(owner_prog_type);
512
513 if (prog_type < prog_type_name_size)
514 jsonw_string_field(json_wtr, "owner_prog_type",
515 prog_type_name[prog_type]);
516 else
517 jsonw_uint_field(json_wtr, "owner_prog_type",
518 prog_type);
519 }
520 if (owner_jited)
521 jsonw_bool_field(json_wtr, "owner_jited",
522 !!atoi(owner_jited));
523
524 free(owner_prog_type);
525 free(owner_jited);
526 }
527 close(fd);
528
529 if (frozen_str) {
530 frozen = atoi(frozen_str);
531 free(frozen_str);
532 }
533 jsonw_int_field(json_wtr, "frozen", frozen);
534
535 if (info->btf_id)
536 jsonw_int_field(json_wtr, "btf_id", info->btf_id);
537
538 if (!hash_empty(map_table.table)) {
539 struct pinned_obj *obj;
540
541 jsonw_name(json_wtr, "pinned");
542 jsonw_start_array(json_wtr);
543 hash_for_each_possible(map_table.table, obj, hash, info->id) {
544 if (obj->id == info->id)
545 jsonw_string(json_wtr, obj->path);
546 }
547 jsonw_end_array(json_wtr);
548 }
549
550 emit_obj_refs_json(&refs_table, info->id, json_wtr);
551
552 jsonw_end_object(json_wtr);
553
554 return 0;
555 }
556
show_map_header_plain(struct bpf_map_info * info)557 static void show_map_header_plain(struct bpf_map_info *info)
558 {
559 printf("%u: ", info->id);
560 if (info->type < ARRAY_SIZE(map_type_name))
561 printf("%s ", map_type_name[info->type]);
562 else
563 printf("type %u ", info->type);
564
565 if (*info->name)
566 printf("name %s ", info->name);
567
568 printf("flags 0x%x", info->map_flags);
569 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
570 printf("\n");
571 }
572
show_map_close_plain(int fd,struct bpf_map_info * info)573 static int show_map_close_plain(int fd, struct bpf_map_info *info)
574 {
575 char *memlock, *frozen_str;
576 int frozen = 0;
577
578 memlock = get_fdinfo(fd, "memlock");
579 frozen_str = get_fdinfo(fd, "frozen");
580
581 show_map_header_plain(info);
582 printf("\tkey %uB value %uB max_entries %u",
583 info->key_size, info->value_size, info->max_entries);
584
585 if (memlock)
586 printf(" memlock %sB", memlock);
587 free(memlock);
588
589 if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
590 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
591 char *owner_jited = get_fdinfo(fd, "owner_jited");
592
593 if (owner_prog_type || owner_jited)
594 printf("\n\t");
595 if (owner_prog_type) {
596 unsigned int prog_type = atoi(owner_prog_type);
597
598 if (prog_type < prog_type_name_size)
599 printf("owner_prog_type %s ",
600 prog_type_name[prog_type]);
601 else
602 printf("owner_prog_type %d ", prog_type);
603 }
604 if (owner_jited)
605 printf("owner%s jited",
606 atoi(owner_jited) ? "" : " not");
607
608 free(owner_prog_type);
609 free(owner_jited);
610 }
611 close(fd);
612
613 if (!hash_empty(map_table.table)) {
614 struct pinned_obj *obj;
615
616 hash_for_each_possible(map_table.table, obj, hash, info->id) {
617 if (obj->id == info->id)
618 printf("\n\tpinned %s", obj->path);
619 }
620 }
621 printf("\n");
622
623 if (frozen_str) {
624 frozen = atoi(frozen_str);
625 free(frozen_str);
626 }
627
628 if (!info->btf_id && !frozen)
629 return 0;
630
631 printf("\t");
632
633 if (info->btf_id)
634 printf("btf_id %d", info->btf_id);
635
636 if (frozen)
637 printf("%sfrozen", info->btf_id ? " " : "");
638
639 emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
640
641 printf("\n");
642 return 0;
643 }
644
do_show_subset(int argc,char ** argv)645 static int do_show_subset(int argc, char **argv)
646 {
647 struct bpf_map_info info = {};
648 __u32 len = sizeof(info);
649 int *fds = NULL;
650 int nb_fds, i;
651 int err = -1;
652
653 fds = malloc(sizeof(int));
654 if (!fds) {
655 p_err("mem alloc failed");
656 return -1;
657 }
658 nb_fds = map_parse_fds(&argc, &argv, &fds);
659 if (nb_fds < 1)
660 goto exit_free;
661
662 if (json_output && nb_fds > 1)
663 jsonw_start_array(json_wtr); /* root array */
664 for (i = 0; i < nb_fds; i++) {
665 err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
666 if (err) {
667 p_err("can't get map info: %s",
668 strerror(errno));
669 for (; i < nb_fds; i++)
670 close(fds[i]);
671 break;
672 }
673
674 if (json_output)
675 show_map_close_json(fds[i], &info);
676 else
677 show_map_close_plain(fds[i], &info);
678
679 close(fds[i]);
680 }
681 if (json_output && nb_fds > 1)
682 jsonw_end_array(json_wtr); /* root array */
683
684 exit_free:
685 free(fds);
686 return err;
687 }
688
do_show(int argc,char ** argv)689 static int do_show(int argc, char **argv)
690 {
691 struct bpf_map_info info = {};
692 __u32 len = sizeof(info);
693 __u32 id = 0;
694 int err;
695 int fd;
696
697 if (show_pinned)
698 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
699 build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
700
701 if (argc == 2)
702 return do_show_subset(argc, argv);
703
704 if (argc)
705 return BAD_ARG();
706
707 if (json_output)
708 jsonw_start_array(json_wtr);
709 while (true) {
710 err = bpf_map_get_next_id(id, &id);
711 if (err) {
712 if (errno == ENOENT)
713 break;
714 p_err("can't get next map: %s%s", strerror(errno),
715 errno == EINVAL ? " -- kernel too old?" : "");
716 break;
717 }
718
719 fd = bpf_map_get_fd_by_id(id);
720 if (fd < 0) {
721 if (errno == ENOENT)
722 continue;
723 p_err("can't get map by id (%u): %s",
724 id, strerror(errno));
725 break;
726 }
727
728 err = bpf_obj_get_info_by_fd(fd, &info, &len);
729 if (err) {
730 p_err("can't get map info: %s", strerror(errno));
731 close(fd);
732 break;
733 }
734
735 if (json_output)
736 show_map_close_json(fd, &info);
737 else
738 show_map_close_plain(fd, &info);
739 }
740 if (json_output)
741 jsonw_end_array(json_wtr);
742
743 delete_obj_refs_table(&refs_table);
744
745 return errno == ENOENT ? 0 : -1;
746 }
747
dump_map_elem(int fd,void * key,void * value,struct bpf_map_info * map_info,struct btf * btf,json_writer_t * btf_wtr)748 static int dump_map_elem(int fd, void *key, void *value,
749 struct bpf_map_info *map_info, struct btf *btf,
750 json_writer_t *btf_wtr)
751 {
752 if (bpf_map_lookup_elem(fd, key, value)) {
753 print_entry_error(map_info, key, errno);
754 return -1;
755 }
756
757 if (json_output) {
758 print_entry_json(map_info, key, value, btf);
759 } else if (btf) {
760 struct btf_dumper d = {
761 .btf = btf,
762 .jw = btf_wtr,
763 .is_plain_text = true,
764 };
765
766 do_dump_btf(&d, map_info, key, value);
767 } else {
768 print_entry_plain(map_info, key, value);
769 }
770
771 return 0;
772 }
773
maps_have_btf(int * fds,int nb_fds)774 static int maps_have_btf(int *fds, int nb_fds)
775 {
776 struct bpf_map_info info = {};
777 __u32 len = sizeof(info);
778 int err, i;
779
780 for (i = 0; i < nb_fds; i++) {
781 err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
782 if (err) {
783 p_err("can't get map info: %s", strerror(errno));
784 return -1;
785 }
786
787 if (!info.btf_id)
788 return 0;
789 }
790
791 return 1;
792 }
793
794 static struct btf *btf_vmlinux;
795
get_map_kv_btf(const struct bpf_map_info * info)796 static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
797 {
798 struct btf *btf = NULL;
799
800 if (info->btf_vmlinux_value_type_id) {
801 if (!btf_vmlinux) {
802 btf_vmlinux = libbpf_find_kernel_btf();
803 if (IS_ERR(btf_vmlinux))
804 p_err("failed to get kernel btf");
805 }
806 return btf_vmlinux;
807 } else if (info->btf_value_type_id) {
808 int err;
809
810 btf = btf__load_from_kernel_by_id(info->btf_id);
811 err = libbpf_get_error(btf);
812 if (err) {
813 p_err("failed to get btf");
814 btf = ERR_PTR(err);
815 }
816 }
817
818 return btf;
819 }
820
free_map_kv_btf(struct btf * btf)821 static void free_map_kv_btf(struct btf *btf)
822 {
823 if (!IS_ERR(btf) && btf != btf_vmlinux)
824 btf__free(btf);
825 }
826
free_btf_vmlinux(void)827 static void free_btf_vmlinux(void)
828 {
829 if (!IS_ERR(btf_vmlinux))
830 btf__free(btf_vmlinux);
831 }
832
833 static int
map_dump(int fd,struct bpf_map_info * info,json_writer_t * wtr,bool show_header)834 map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
835 bool show_header)
836 {
837 void *key, *value, *prev_key;
838 unsigned int num_elems = 0;
839 struct btf *btf = NULL;
840 int err;
841
842 key = malloc(info->key_size);
843 value = alloc_value(info);
844 if (!key || !value) {
845 p_err("mem alloc failed");
846 err = -1;
847 goto exit_free;
848 }
849
850 prev_key = NULL;
851
852 if (wtr) {
853 btf = get_map_kv_btf(info);
854 if (IS_ERR(btf)) {
855 err = PTR_ERR(btf);
856 goto exit_free;
857 }
858
859 if (show_header) {
860 jsonw_start_object(wtr); /* map object */
861 show_map_header_json(info, wtr);
862 jsonw_name(wtr, "elements");
863 }
864 jsonw_start_array(wtr); /* elements */
865 } else if (show_header) {
866 show_map_header_plain(info);
867 }
868
869 if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
870 info->value_size != 8)
871 p_info("Warning: cannot read values from %s map with value_size != 8",
872 map_type_name[info->type]);
873 while (true) {
874 err = bpf_map_get_next_key(fd, prev_key, key);
875 if (err) {
876 if (errno == ENOENT)
877 err = 0;
878 break;
879 }
880 if (!dump_map_elem(fd, key, value, info, btf, wtr))
881 num_elems++;
882 prev_key = key;
883 }
884
885 if (wtr) {
886 jsonw_end_array(wtr); /* elements */
887 if (show_header)
888 jsonw_end_object(wtr); /* map object */
889 } else {
890 printf("Found %u element%s\n", num_elems,
891 num_elems != 1 ? "s" : "");
892 }
893
894 exit_free:
895 free(key);
896 free(value);
897 close(fd);
898 free_map_kv_btf(btf);
899
900 return err;
901 }
902
do_dump(int argc,char ** argv)903 static int do_dump(int argc, char **argv)
904 {
905 json_writer_t *wtr = NULL, *btf_wtr = NULL;
906 struct bpf_map_info info = {};
907 int nb_fds, i = 0;
908 __u32 len = sizeof(info);
909 int *fds = NULL;
910 int err = -1;
911
912 if (argc != 2)
913 usage();
914
915 fds = malloc(sizeof(int));
916 if (!fds) {
917 p_err("mem alloc failed");
918 return -1;
919 }
920 nb_fds = map_parse_fds(&argc, &argv, &fds);
921 if (nb_fds < 1)
922 goto exit_free;
923
924 if (json_output) {
925 wtr = json_wtr;
926 } else {
927 int do_plain_btf;
928
929 do_plain_btf = maps_have_btf(fds, nb_fds);
930 if (do_plain_btf < 0)
931 goto exit_close;
932
933 if (do_plain_btf) {
934 btf_wtr = get_btf_writer();
935 wtr = btf_wtr;
936 if (!btf_wtr)
937 p_info("failed to create json writer for btf. falling back to plain output");
938 }
939 }
940
941 if (wtr && nb_fds > 1)
942 jsonw_start_array(wtr); /* root array */
943 for (i = 0; i < nb_fds; i++) {
944 if (bpf_obj_get_info_by_fd(fds[i], &info, &len)) {
945 p_err("can't get map info: %s", strerror(errno));
946 break;
947 }
948 err = map_dump(fds[i], &info, wtr, nb_fds > 1);
949 if (!wtr && i != nb_fds - 1)
950 printf("\n");
951
952 if (err)
953 break;
954 close(fds[i]);
955 }
956 if (wtr && nb_fds > 1)
957 jsonw_end_array(wtr); /* root array */
958
959 if (btf_wtr)
960 jsonw_destroy(&btf_wtr);
961 exit_close:
962 for (; i < nb_fds; i++)
963 close(fds[i]);
964 exit_free:
965 free(fds);
966 free_btf_vmlinux();
967 return err;
968 }
969
alloc_key_value(struct bpf_map_info * info,void ** key,void ** value)970 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
971 {
972 *key = NULL;
973 *value = NULL;
974
975 if (info->key_size) {
976 *key = malloc(info->key_size);
977 if (!*key) {
978 p_err("key mem alloc failed");
979 return -1;
980 }
981 }
982
983 if (info->value_size) {
984 *value = alloc_value(info);
985 if (!*value) {
986 p_err("value mem alloc failed");
987 free(*key);
988 *key = NULL;
989 return -1;
990 }
991 }
992
993 return 0;
994 }
995
do_update(int argc,char ** argv)996 static int do_update(int argc, char **argv)
997 {
998 struct bpf_map_info info = {};
999 __u32 len = sizeof(info);
1000 __u32 *value_fd = NULL;
1001 __u32 flags = BPF_ANY;
1002 void *key, *value;
1003 int fd, err;
1004
1005 if (argc < 2)
1006 usage();
1007
1008 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1009 if (fd < 0)
1010 return -1;
1011
1012 err = alloc_key_value(&info, &key, &value);
1013 if (err)
1014 goto exit_free;
1015
1016 err = parse_elem(argv, &info, key, value, info.key_size,
1017 info.value_size, &flags, &value_fd);
1018 if (err)
1019 goto exit_free;
1020
1021 err = bpf_map_update_elem(fd, key, value, flags);
1022 if (err) {
1023 p_err("update failed: %s", strerror(errno));
1024 goto exit_free;
1025 }
1026
1027 exit_free:
1028 if (value_fd)
1029 close(*value_fd);
1030 free(key);
1031 free(value);
1032 close(fd);
1033
1034 if (!err && json_output)
1035 jsonw_null(json_wtr);
1036 return err;
1037 }
1038
print_key_value(struct bpf_map_info * info,void * key,void * value)1039 static void print_key_value(struct bpf_map_info *info, void *key,
1040 void *value)
1041 {
1042 json_writer_t *btf_wtr;
1043 struct btf *btf;
1044
1045 btf = get_map_kv_btf(info);
1046 if (libbpf_get_error(btf))
1047 return;
1048
1049 if (json_output) {
1050 print_entry_json(info, key, value, btf);
1051 } else if (btf) {
1052 /* if here json_wtr wouldn't have been initialised,
1053 * so let's create separate writer for btf
1054 */
1055 btf_wtr = get_btf_writer();
1056 if (!btf_wtr) {
1057 p_info("failed to create json writer for btf. falling back to plain output");
1058 btf__free(btf);
1059 btf = NULL;
1060 print_entry_plain(info, key, value);
1061 } else {
1062 struct btf_dumper d = {
1063 .btf = btf,
1064 .jw = btf_wtr,
1065 .is_plain_text = true,
1066 };
1067
1068 do_dump_btf(&d, info, key, value);
1069 jsonw_destroy(&btf_wtr);
1070 }
1071 } else {
1072 print_entry_plain(info, key, value);
1073 }
1074 btf__free(btf);
1075 }
1076
do_lookup(int argc,char ** argv)1077 static int do_lookup(int argc, char **argv)
1078 {
1079 struct bpf_map_info info = {};
1080 __u32 len = sizeof(info);
1081 void *key, *value;
1082 int err;
1083 int fd;
1084
1085 if (argc < 2)
1086 usage();
1087
1088 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1089 if (fd < 0)
1090 return -1;
1091
1092 err = alloc_key_value(&info, &key, &value);
1093 if (err)
1094 goto exit_free;
1095
1096 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1097 if (err)
1098 goto exit_free;
1099
1100 err = bpf_map_lookup_elem(fd, key, value);
1101 if (err) {
1102 if (errno == ENOENT) {
1103 if (json_output) {
1104 jsonw_null(json_wtr);
1105 } else {
1106 printf("key:\n");
1107 fprint_hex(stdout, key, info.key_size, " ");
1108 printf("\n\nNot found\n");
1109 }
1110 } else {
1111 p_err("lookup failed: %s", strerror(errno));
1112 }
1113
1114 goto exit_free;
1115 }
1116
1117 /* here means bpf_map_lookup_elem() succeeded */
1118 print_key_value(&info, key, value);
1119
1120 exit_free:
1121 free(key);
1122 free(value);
1123 close(fd);
1124
1125 return err;
1126 }
1127
do_getnext(int argc,char ** argv)1128 static int do_getnext(int argc, char **argv)
1129 {
1130 struct bpf_map_info info = {};
1131 __u32 len = sizeof(info);
1132 void *key, *nextkey;
1133 int err;
1134 int fd;
1135
1136 if (argc < 2)
1137 usage();
1138
1139 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1140 if (fd < 0)
1141 return -1;
1142
1143 key = malloc(info.key_size);
1144 nextkey = malloc(info.key_size);
1145 if (!key || !nextkey) {
1146 p_err("mem alloc failed");
1147 err = -1;
1148 goto exit_free;
1149 }
1150
1151 if (argc) {
1152 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
1153 NULL, NULL);
1154 if (err)
1155 goto exit_free;
1156 } else {
1157 free(key);
1158 key = NULL;
1159 }
1160
1161 err = bpf_map_get_next_key(fd, key, nextkey);
1162 if (err) {
1163 p_err("can't get next key: %s", strerror(errno));
1164 goto exit_free;
1165 }
1166
1167 if (json_output) {
1168 jsonw_start_object(json_wtr);
1169 if (key) {
1170 jsonw_name(json_wtr, "key");
1171 print_hex_data_json(key, info.key_size);
1172 } else {
1173 jsonw_null_field(json_wtr, "key");
1174 }
1175 jsonw_name(json_wtr, "next_key");
1176 print_hex_data_json(nextkey, info.key_size);
1177 jsonw_end_object(json_wtr);
1178 } else {
1179 if (key) {
1180 printf("key:\n");
1181 fprint_hex(stdout, key, info.key_size, " ");
1182 printf("\n");
1183 } else {
1184 printf("key: None\n");
1185 }
1186 printf("next key:\n");
1187 fprint_hex(stdout, nextkey, info.key_size, " ");
1188 printf("\n");
1189 }
1190
1191 exit_free:
1192 free(nextkey);
1193 free(key);
1194 close(fd);
1195
1196 return err;
1197 }
1198
do_delete(int argc,char ** argv)1199 static int do_delete(int argc, char **argv)
1200 {
1201 struct bpf_map_info info = {};
1202 __u32 len = sizeof(info);
1203 void *key;
1204 int err;
1205 int fd;
1206
1207 if (argc < 2)
1208 usage();
1209
1210 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1211 if (fd < 0)
1212 return -1;
1213
1214 key = malloc(info.key_size);
1215 if (!key) {
1216 p_err("mem alloc failed");
1217 err = -1;
1218 goto exit_free;
1219 }
1220
1221 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1222 if (err)
1223 goto exit_free;
1224
1225 err = bpf_map_delete_elem(fd, key);
1226 if (err)
1227 p_err("delete failed: %s", strerror(errno));
1228
1229 exit_free:
1230 free(key);
1231 close(fd);
1232
1233 if (!err && json_output)
1234 jsonw_null(json_wtr);
1235 return err;
1236 }
1237
do_pin(int argc,char ** argv)1238 static int do_pin(int argc, char **argv)
1239 {
1240 int err;
1241
1242 err = do_pin_any(argc, argv, map_parse_fd);
1243 if (!err && json_output)
1244 jsonw_null(json_wtr);
1245 return err;
1246 }
1247
do_create(int argc,char ** argv)1248 static int do_create(int argc, char **argv)
1249 {
1250 struct bpf_create_map_attr attr = { NULL, };
1251 const char *pinfile;
1252 int err = -1, fd;
1253
1254 if (!REQ_ARGS(7))
1255 return -1;
1256 pinfile = GET_ARG();
1257
1258 while (argc) {
1259 if (!REQ_ARGS(2))
1260 return -1;
1261
1262 if (is_prefix(*argv, "type")) {
1263 NEXT_ARG();
1264
1265 if (attr.map_type) {
1266 p_err("map type already specified");
1267 goto exit;
1268 }
1269
1270 attr.map_type = map_type_from_str(*argv);
1271 if ((int)attr.map_type < 0) {
1272 p_err("unrecognized map type: %s", *argv);
1273 goto exit;
1274 }
1275 NEXT_ARG();
1276 } else if (is_prefix(*argv, "name")) {
1277 NEXT_ARG();
1278 attr.name = GET_ARG();
1279 } else if (is_prefix(*argv, "key")) {
1280 if (parse_u32_arg(&argc, &argv, &attr.key_size,
1281 "key size"))
1282 goto exit;
1283 } else if (is_prefix(*argv, "value")) {
1284 if (parse_u32_arg(&argc, &argv, &attr.value_size,
1285 "value size"))
1286 goto exit;
1287 } else if (is_prefix(*argv, "entries")) {
1288 if (parse_u32_arg(&argc, &argv, &attr.max_entries,
1289 "max entries"))
1290 goto exit;
1291 } else if (is_prefix(*argv, "flags")) {
1292 if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1293 "flags"))
1294 goto exit;
1295 } else if (is_prefix(*argv, "dev")) {
1296 NEXT_ARG();
1297
1298 if (attr.map_ifindex) {
1299 p_err("offload device already specified");
1300 goto exit;
1301 }
1302
1303 attr.map_ifindex = if_nametoindex(*argv);
1304 if (!attr.map_ifindex) {
1305 p_err("unrecognized netdevice '%s': %s",
1306 *argv, strerror(errno));
1307 goto exit;
1308 }
1309 NEXT_ARG();
1310 } else if (is_prefix(*argv, "inner_map")) {
1311 struct bpf_map_info info = {};
1312 __u32 len = sizeof(info);
1313 int inner_map_fd;
1314
1315 NEXT_ARG();
1316 if (!REQ_ARGS(2))
1317 usage();
1318 inner_map_fd = map_parse_fd_and_info(&argc, &argv,
1319 &info, &len);
1320 if (inner_map_fd < 0)
1321 return -1;
1322 attr.inner_map_fd = inner_map_fd;
1323 } else {
1324 p_err("unknown arg %s", *argv);
1325 goto exit;
1326 }
1327 }
1328
1329 if (!attr.name) {
1330 p_err("map name not specified");
1331 goto exit;
1332 }
1333
1334 set_max_rlimit();
1335
1336 fd = bpf_create_map_xattr(&attr);
1337 if (fd < 0) {
1338 p_err("map create failed: %s", strerror(errno));
1339 goto exit;
1340 }
1341
1342 err = do_pin_fd(fd, pinfile);
1343 close(fd);
1344 if (err)
1345 goto exit;
1346
1347 if (json_output)
1348 jsonw_null(json_wtr);
1349
1350 exit:
1351 if (attr.inner_map_fd > 0)
1352 close(attr.inner_map_fd);
1353
1354 return err;
1355 }
1356
do_pop_dequeue(int argc,char ** argv)1357 static int do_pop_dequeue(int argc, char **argv)
1358 {
1359 struct bpf_map_info info = {};
1360 __u32 len = sizeof(info);
1361 void *key, *value;
1362 int err;
1363 int fd;
1364
1365 if (argc < 2)
1366 usage();
1367
1368 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1369 if (fd < 0)
1370 return -1;
1371
1372 err = alloc_key_value(&info, &key, &value);
1373 if (err)
1374 goto exit_free;
1375
1376 err = bpf_map_lookup_and_delete_elem(fd, key, value);
1377 if (err) {
1378 if (errno == ENOENT) {
1379 if (json_output)
1380 jsonw_null(json_wtr);
1381 else
1382 printf("Error: empty map\n");
1383 } else {
1384 p_err("pop failed: %s", strerror(errno));
1385 }
1386
1387 goto exit_free;
1388 }
1389
1390 print_key_value(&info, key, value);
1391
1392 exit_free:
1393 free(key);
1394 free(value);
1395 close(fd);
1396
1397 return err;
1398 }
1399
do_freeze(int argc,char ** argv)1400 static int do_freeze(int argc, char **argv)
1401 {
1402 int err, fd;
1403
1404 if (!REQ_ARGS(2))
1405 return -1;
1406
1407 fd = map_parse_fd(&argc, &argv);
1408 if (fd < 0)
1409 return -1;
1410
1411 if (argc) {
1412 close(fd);
1413 return BAD_ARG();
1414 }
1415
1416 err = bpf_map_freeze(fd);
1417 close(fd);
1418 if (err) {
1419 p_err("failed to freeze map: %s", strerror(errno));
1420 return err;
1421 }
1422
1423 if (json_output)
1424 jsonw_null(json_wtr);
1425
1426 return 0;
1427 }
1428
do_help(int argc,char ** argv)1429 static int do_help(int argc, char **argv)
1430 {
1431 if (json_output) {
1432 jsonw_null(json_wtr);
1433 return 0;
1434 }
1435
1436 fprintf(stderr,
1437 "Usage: %1$s %2$s { show | list } [MAP]\n"
1438 " %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1439 " entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1440 " [inner_map MAP] [dev NAME]\n"
1441 " %1$s %2$s dump MAP\n"
1442 " %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1443 " %1$s %2$s lookup MAP [key DATA]\n"
1444 " %1$s %2$s getnext MAP [key DATA]\n"
1445 " %1$s %2$s delete MAP key DATA\n"
1446 " %1$s %2$s pin MAP FILE\n"
1447 " %1$s %2$s event_pipe MAP [cpu N index M]\n"
1448 " %1$s %2$s peek MAP\n"
1449 " %1$s %2$s push MAP value VALUE\n"
1450 " %1$s %2$s pop MAP\n"
1451 " %1$s %2$s enqueue MAP value VALUE\n"
1452 " %1$s %2$s dequeue MAP\n"
1453 " %1$s %2$s freeze MAP\n"
1454 " %1$s %2$s help\n"
1455 "\n"
1456 " " HELP_SPEC_MAP "\n"
1457 " DATA := { [hex] BYTES }\n"
1458 " " HELP_SPEC_PROGRAM "\n"
1459 " VALUE := { DATA | MAP | PROG }\n"
1460 " UPDATE_FLAGS := { any | exist | noexist }\n"
1461 " TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1462 " percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1463 " lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1464 " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n"
1465 " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n"
1466 " queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n"
1467 " task_storage }\n"
1468 " " HELP_SPEC_OPTIONS " |\n"
1469 " {-f|--bpffs} | {-n|--nomount} }\n"
1470 "",
1471 bin_name, argv[-2]);
1472
1473 return 0;
1474 }
1475
1476 static const struct cmd cmds[] = {
1477 { "show", do_show },
1478 { "list", do_show },
1479 { "help", do_help },
1480 { "dump", do_dump },
1481 { "update", do_update },
1482 { "lookup", do_lookup },
1483 { "getnext", do_getnext },
1484 { "delete", do_delete },
1485 { "pin", do_pin },
1486 { "event_pipe", do_event_pipe },
1487 { "create", do_create },
1488 { "peek", do_lookup },
1489 { "push", do_update },
1490 { "enqueue", do_update },
1491 { "pop", do_pop_dequeue },
1492 { "dequeue", do_pop_dequeue },
1493 { "freeze", do_freeze },
1494 { 0 }
1495 };
1496
do_map(int argc,char ** argv)1497 int do_map(int argc, char **argv)
1498 {
1499 return cmd_select(cmds, argc, argv, do_help);
1500 }
1501