3f599399913b7859899fe088aadab55222a73642
[platform/kernel/linux-starfive.git] / tools / bpf / bpftool / map.c
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.h>
19
20 #include "btf.h"
21 #include "json_writer.h"
22 #include "main.h"
23
24 static 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_SOCKMAP]                  = "sockmap",
41         [BPF_MAP_TYPE_CPUMAP]                   = "cpumap",
42         [BPF_MAP_TYPE_XSKMAP]                   = "xskmap",
43         [BPF_MAP_TYPE_SOCKHASH]                 = "sockhash",
44         [BPF_MAP_TYPE_CGROUP_STORAGE]           = "cgroup_storage",
45         [BPF_MAP_TYPE_REUSEPORT_SOCKARRAY]      = "reuseport_sockarray",
46         [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE]    = "percpu_cgroup_storage",
47         [BPF_MAP_TYPE_QUEUE]                    = "queue",
48         [BPF_MAP_TYPE_STACK]                    = "stack",
49 };
50
51 static bool map_is_per_cpu(__u32 type)
52 {
53         return type == BPF_MAP_TYPE_PERCPU_HASH ||
54                type == BPF_MAP_TYPE_PERCPU_ARRAY ||
55                type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
56                type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
57 }
58
59 static bool map_is_map_of_maps(__u32 type)
60 {
61         return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
62                type == BPF_MAP_TYPE_HASH_OF_MAPS;
63 }
64
65 static bool map_is_map_of_progs(__u32 type)
66 {
67         return type == BPF_MAP_TYPE_PROG_ARRAY;
68 }
69
70 static int map_type_from_str(const char *type)
71 {
72         unsigned int i;
73
74         for (i = 0; i < ARRAY_SIZE(map_type_name); i++)
75                 /* Don't allow prefixing in case of possible future shadowing */
76                 if (map_type_name[i] && !strcmp(map_type_name[i], type))
77                         return i;
78         return -1;
79 }
80
81 static void *alloc_value(struct bpf_map_info *info)
82 {
83         if (map_is_per_cpu(info->type))
84                 return malloc(round_up(info->value_size, 8) *
85                               get_possible_cpus());
86         else
87                 return malloc(info->value_size);
88 }
89
90 int map_parse_fd(int *argc, char ***argv)
91 {
92         int fd;
93
94         if (is_prefix(**argv, "id")) {
95                 unsigned int id;
96                 char *endptr;
97
98                 NEXT_ARGP();
99
100                 id = strtoul(**argv, &endptr, 0);
101                 if (*endptr) {
102                         p_err("can't parse %s as ID", **argv);
103                         return -1;
104                 }
105                 NEXT_ARGP();
106
107                 fd = bpf_map_get_fd_by_id(id);
108                 if (fd < 0)
109                         p_err("get map by id (%u): %s", id, strerror(errno));
110                 return fd;
111         } else if (is_prefix(**argv, "pinned")) {
112                 char *path;
113
114                 NEXT_ARGP();
115
116                 path = **argv;
117                 NEXT_ARGP();
118
119                 return open_obj_pinned_any(path, BPF_OBJ_MAP);
120         }
121
122         p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
123         return -1;
124 }
125
126 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
127 {
128         int err;
129         int fd;
130
131         fd = map_parse_fd(argc, argv);
132         if (fd < 0)
133                 return -1;
134
135         err = bpf_obj_get_info_by_fd(fd, info, info_len);
136         if (err) {
137                 p_err("can't get map info: %s", strerror(errno));
138                 close(fd);
139                 return err;
140         }
141
142         return fd;
143 }
144
145 static int do_dump_btf(const struct btf_dumper *d,
146                        struct bpf_map_info *map_info, void *key,
147                        void *value)
148 {
149         int ret;
150
151         /* start of key-value pair */
152         jsonw_start_object(d->jw);
153
154         jsonw_name(d->jw, "key");
155
156         ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
157         if (ret)
158                 goto err_end_obj;
159
160         if (!map_is_per_cpu(map_info->type)) {
161                 jsonw_name(d->jw, "value");
162                 ret = btf_dumper_type(d, map_info->btf_value_type_id, value);
163         } else {
164                 unsigned int i, n, step;
165
166                 jsonw_name(d->jw, "values");
167                 jsonw_start_array(d->jw);
168                 n = get_possible_cpus();
169                 step = round_up(map_info->value_size, 8);
170                 for (i = 0; i < n; i++) {
171                         jsonw_start_object(d->jw);
172                         jsonw_int_field(d->jw, "cpu", i);
173                         jsonw_name(d->jw, "value");
174                         ret = btf_dumper_type(d, map_info->btf_value_type_id,
175                                               value + i * step);
176                         jsonw_end_object(d->jw);
177                         if (ret)
178                                 break;
179                 }
180                 jsonw_end_array(d->jw);
181         }
182
183 err_end_obj:
184         /* end of key-value pair */
185         jsonw_end_object(d->jw);
186
187         return ret;
188 }
189
190 static json_writer_t *get_btf_writer(void)
191 {
192         json_writer_t *jw = jsonw_new(stdout);
193
194         if (!jw)
195                 return NULL;
196         jsonw_pretty(jw, true);
197
198         return jw;
199 }
200
201 static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
202                              unsigned char *value, struct btf *btf)
203 {
204         jsonw_start_object(json_wtr);
205
206         if (!map_is_per_cpu(info->type)) {
207                 jsonw_name(json_wtr, "key");
208                 print_hex_data_json(key, info->key_size);
209                 jsonw_name(json_wtr, "value");
210                 print_hex_data_json(value, info->value_size);
211                 if (btf) {
212                         struct btf_dumper d = {
213                                 .btf = btf,
214                                 .jw = json_wtr,
215                                 .is_plain_text = false,
216                         };
217
218                         jsonw_name(json_wtr, "formatted");
219                         do_dump_btf(&d, info, key, value);
220                 }
221         } else {
222                 unsigned int i, n, step;
223
224                 n = get_possible_cpus();
225                 step = round_up(info->value_size, 8);
226
227                 jsonw_name(json_wtr, "key");
228                 print_hex_data_json(key, info->key_size);
229
230                 jsonw_name(json_wtr, "values");
231                 jsonw_start_array(json_wtr);
232                 for (i = 0; i < n; i++) {
233                         jsonw_start_object(json_wtr);
234
235                         jsonw_int_field(json_wtr, "cpu", i);
236
237                         jsonw_name(json_wtr, "value");
238                         print_hex_data_json(value + i * step,
239                                             info->value_size);
240
241                         jsonw_end_object(json_wtr);
242                 }
243                 jsonw_end_array(json_wtr);
244                 if (btf) {
245                         struct btf_dumper d = {
246                                 .btf = btf,
247                                 .jw = json_wtr,
248                                 .is_plain_text = false,
249                         };
250
251                         jsonw_name(json_wtr, "formatted");
252                         do_dump_btf(&d, info, key, value);
253                 }
254         }
255
256         jsonw_end_object(json_wtr);
257 }
258
259 static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
260                               const char *value)
261 {
262         int value_size = strlen(value);
263         bool single_line, break_names;
264
265         break_names = info->key_size > 16 || value_size > 16;
266         single_line = info->key_size + value_size <= 24 && !break_names;
267
268         printf("key:%c", break_names ? '\n' : ' ');
269         fprint_hex(stdout, key, info->key_size, " ");
270
271         printf(single_line ? "  " : "\n");
272
273         printf("value:%c%s", break_names ? '\n' : ' ', value);
274
275         printf("\n");
276 }
277
278 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
279                               unsigned char *value)
280 {
281         if (!map_is_per_cpu(info->type)) {
282                 bool single_line, break_names;
283
284                 break_names = info->key_size > 16 || info->value_size > 16;
285                 single_line = info->key_size + info->value_size <= 24 &&
286                         !break_names;
287
288                 if (info->key_size) {
289                         printf("key:%c", break_names ? '\n' : ' ');
290                         fprint_hex(stdout, key, info->key_size, " ");
291
292                         printf(single_line ? "  " : "\n");
293                 }
294
295                 if (info->value_size) {
296                         printf("value:%c", break_names ? '\n' : ' ');
297                         if (value)
298                                 fprint_hex(stdout, value, info->value_size,
299                                            " ");
300                         else
301                                 printf("<no entry>");
302                 }
303
304                 printf("\n");
305         } else {
306                 unsigned int i, n, step;
307
308                 n = get_possible_cpus();
309                 step = round_up(info->value_size, 8);
310
311                 if (info->key_size) {
312                         printf("key:\n");
313                         fprint_hex(stdout, key, info->key_size, " ");
314                         printf("\n");
315                 }
316                 if (info->value_size) {
317                         for (i = 0; i < n; i++) {
318                                 printf("value (CPU %02d):%c",
319                                        i, info->value_size > 16 ? '\n' : ' ');
320                                 if (value)
321                                         fprint_hex(stdout, value + i * step,
322                                                    info->value_size, " ");
323                                 else
324                                         printf("<no entry>");
325                                 printf("\n");
326                         }
327                 }
328         }
329 }
330
331 static char **parse_bytes(char **argv, const char *name, unsigned char *val,
332                           unsigned int n)
333 {
334         unsigned int i = 0, base = 0;
335         char *endptr;
336
337         if (is_prefix(*argv, "hex")) {
338                 base = 16;
339                 argv++;
340         }
341
342         while (i < n && argv[i]) {
343                 val[i] = strtoul(argv[i], &endptr, base);
344                 if (*endptr) {
345                         p_err("error parsing byte: %s", argv[i]);
346                         return NULL;
347                 }
348                 i++;
349         }
350
351         if (i != n) {
352                 p_err("%s expected %d bytes got %d", name, n, i);
353                 return NULL;
354         }
355
356         return argv + i;
357 }
358
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
428                         fd = prog_parse_fd(&argc, &argv);
429                         if (fd < 0)
430                                 return -1;
431
432                         *value_fd = value;
433                         **value_fd = fd;
434                 } else {
435                         argv = parse_bytes(argv, "value", value, value_size);
436                         if (!argv)
437                                 return -1;
438                 }
439
440                 return parse_elem(argv, info, key, NULL, key_size, value_size,
441                                   flags, NULL);
442         } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
443                    is_prefix(*argv, "exist")) {
444                 if (!flags) {
445                         p_err("flags specified multiple times: %s", *argv);
446                         return -1;
447                 }
448
449                 if (is_prefix(*argv, "any"))
450                         *flags = BPF_ANY;
451                 else if (is_prefix(*argv, "noexist"))
452                         *flags = BPF_NOEXIST;
453                 else if (is_prefix(*argv, "exist"))
454                         *flags = BPF_EXIST;
455
456                 return parse_elem(argv + 1, info, key, value, key_size,
457                                   value_size, NULL, value_fd);
458         }
459
460         p_err("expected key or value, got: %s", *argv);
461         return -1;
462 }
463
464 static int show_map_close_json(int fd, struct bpf_map_info *info)
465 {
466         char *memlock;
467
468         memlock = get_fdinfo(fd, "memlock");
469
470         jsonw_start_object(json_wtr);
471
472         jsonw_uint_field(json_wtr, "id", info->id);
473         if (info->type < ARRAY_SIZE(map_type_name))
474                 jsonw_string_field(json_wtr, "type",
475                                    map_type_name[info->type]);
476         else
477                 jsonw_uint_field(json_wtr, "type", info->type);
478
479         if (*info->name)
480                 jsonw_string_field(json_wtr, "name", info->name);
481
482         jsonw_name(json_wtr, "flags");
483         jsonw_printf(json_wtr, "%d", info->map_flags);
484
485         print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
486
487         jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
488         jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
489         jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
490
491         if (memlock)
492                 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
493         free(memlock);
494
495         if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
496                 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
497                 char *owner_jited = get_fdinfo(fd, "owner_jited");
498
499                 if (owner_prog_type) {
500                         unsigned int prog_type = atoi(owner_prog_type);
501
502                         if (prog_type < ARRAY_SIZE(prog_type_name))
503                                 jsonw_string_field(json_wtr, "owner_prog_type",
504                                                    prog_type_name[prog_type]);
505                         else
506                                 jsonw_uint_field(json_wtr, "owner_prog_type",
507                                                  prog_type);
508                 }
509                 if (atoi(owner_jited))
510                         jsonw_bool_field(json_wtr, "owner_jited", true);
511                 else
512                         jsonw_bool_field(json_wtr, "owner_jited", false);
513
514                 free(owner_prog_type);
515                 free(owner_jited);
516         }
517         close(fd);
518
519         if (!hash_empty(map_table.table)) {
520                 struct pinned_obj *obj;
521
522                 jsonw_name(json_wtr, "pinned");
523                 jsonw_start_array(json_wtr);
524                 hash_for_each_possible(map_table.table, obj, hash, info->id) {
525                         if (obj->id == info->id)
526                                 jsonw_string(json_wtr, obj->path);
527                 }
528                 jsonw_end_array(json_wtr);
529         }
530
531         jsonw_end_object(json_wtr);
532
533         return 0;
534 }
535
536 static int show_map_close_plain(int fd, struct bpf_map_info *info)
537 {
538         char *memlock;
539
540         memlock = get_fdinfo(fd, "memlock");
541
542         printf("%u: ", info->id);
543         if (info->type < ARRAY_SIZE(map_type_name))
544                 printf("%s  ", map_type_name[info->type]);
545         else
546                 printf("type %u  ", info->type);
547
548         if (*info->name)
549                 printf("name %s  ", info->name);
550
551         printf("flags 0x%x", info->map_flags);
552         print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
553         printf("\n");
554         printf("\tkey %uB  value %uB  max_entries %u",
555                info->key_size, info->value_size, info->max_entries);
556
557         if (memlock)
558                 printf("  memlock %sB", memlock);
559         free(memlock);
560
561         if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
562                 char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
563                 char *owner_jited = get_fdinfo(fd, "owner_jited");
564
565                 printf("\n\t");
566                 if (owner_prog_type) {
567                         unsigned int prog_type = atoi(owner_prog_type);
568
569                         if (prog_type < ARRAY_SIZE(prog_type_name))
570                                 printf("owner_prog_type %s  ",
571                                        prog_type_name[prog_type]);
572                         else
573                                 printf("owner_prog_type %d  ", prog_type);
574                 }
575                 if (atoi(owner_jited))
576                         printf("owner jited");
577                 else
578                         printf("owner not jited");
579
580                 free(owner_prog_type);
581                 free(owner_jited);
582         }
583         close(fd);
584
585         printf("\n");
586         if (!hash_empty(map_table.table)) {
587                 struct pinned_obj *obj;
588
589                 hash_for_each_possible(map_table.table, obj, hash, info->id) {
590                         if (obj->id == info->id)
591                                 printf("\tpinned %s\n", obj->path);
592                 }
593         }
594         return 0;
595 }
596
597 static int do_show(int argc, char **argv)
598 {
599         struct bpf_map_info info = {};
600         __u32 len = sizeof(info);
601         __u32 id = 0;
602         int err;
603         int fd;
604
605         if (show_pinned)
606                 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
607
608         if (argc == 2) {
609                 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
610                 if (fd < 0)
611                         return -1;
612
613                 if (json_output)
614                         return show_map_close_json(fd, &info);
615                 else
616                         return show_map_close_plain(fd, &info);
617         }
618
619         if (argc)
620                 return BAD_ARG();
621
622         if (json_output)
623                 jsonw_start_array(json_wtr);
624         while (true) {
625                 err = bpf_map_get_next_id(id, &id);
626                 if (err) {
627                         if (errno == ENOENT)
628                                 break;
629                         p_err("can't get next map: %s%s", strerror(errno),
630                               errno == EINVAL ? " -- kernel too old?" : "");
631                         break;
632                 }
633
634                 fd = bpf_map_get_fd_by_id(id);
635                 if (fd < 0) {
636                         if (errno == ENOENT)
637                                 continue;
638                         p_err("can't get map by id (%u): %s",
639                               id, strerror(errno));
640                         break;
641                 }
642
643                 err = bpf_obj_get_info_by_fd(fd, &info, &len);
644                 if (err) {
645                         p_err("can't get map info: %s", strerror(errno));
646                         close(fd);
647                         break;
648                 }
649
650                 if (json_output)
651                         show_map_close_json(fd, &info);
652                 else
653                         show_map_close_plain(fd, &info);
654         }
655         if (json_output)
656                 jsonw_end_array(json_wtr);
657
658         return errno == ENOENT ? 0 : -1;
659 }
660
661 static int dump_map_elem(int fd, void *key, void *value,
662                          struct bpf_map_info *map_info, struct btf *btf,
663                          json_writer_t *btf_wtr)
664 {
665         int num_elems = 0;
666         int lookup_errno;
667
668         if (!bpf_map_lookup_elem(fd, key, value)) {
669                 if (json_output) {
670                         print_entry_json(map_info, key, value, btf);
671                 } else {
672                         if (btf) {
673                                 struct btf_dumper d = {
674                                         .btf = btf,
675                                         .jw = btf_wtr,
676                                         .is_plain_text = true,
677                                 };
678
679                                 do_dump_btf(&d, map_info, key, value);
680                         } else {
681                                 print_entry_plain(map_info, key, value);
682                         }
683                         num_elems++;
684                 }
685                 return num_elems;
686         }
687
688         /* lookup error handling */
689         lookup_errno = errno;
690
691         if (map_is_map_of_maps(map_info->type) ||
692             map_is_map_of_progs(map_info->type))
693                 return 0;
694
695         if (json_output) {
696                 jsonw_name(json_wtr, "key");
697                 print_hex_data_json(key, map_info->key_size);
698                 jsonw_name(json_wtr, "value");
699                 jsonw_start_object(json_wtr);
700                 jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
701                 jsonw_end_object(json_wtr);
702         } else {
703                 if (errno == ENOENT)
704                         print_entry_plain(map_info, key, NULL);
705                 else
706                         print_entry_error(map_info, key,
707                                           strerror(lookup_errno));
708         }
709
710         return 0;
711 }
712
713 static int do_dump(int argc, char **argv)
714 {
715         struct bpf_map_info info = {};
716         void *key, *value, *prev_key;
717         unsigned int num_elems = 0;
718         __u32 len = sizeof(info);
719         json_writer_t *btf_wtr;
720         struct btf *btf = NULL;
721         int err;
722         int fd;
723
724         if (argc != 2)
725                 usage();
726
727         fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
728         if (fd < 0)
729                 return -1;
730
731         key = malloc(info.key_size);
732         value = alloc_value(&info);
733         if (!key || !value) {
734                 p_err("mem alloc failed");
735                 err = -1;
736                 goto exit_free;
737         }
738
739         prev_key = NULL;
740
741         err = btf__get_from_id(info.btf_id, &btf);
742         if (err) {
743                 p_err("failed to get btf");
744                 goto exit_free;
745         }
746
747         if (json_output)
748                 jsonw_start_array(json_wtr);
749         else
750                 if (btf) {
751                         btf_wtr = get_btf_writer();
752                         if (!btf_wtr) {
753                                 p_info("failed to create json writer for btf. falling back to plain output");
754                                 btf__free(btf);
755                                 btf = NULL;
756                         } else {
757                                 jsonw_start_array(btf_wtr);
758                         }
759                 }
760
761         while (true) {
762                 err = bpf_map_get_next_key(fd, prev_key, key);
763                 if (err) {
764                         if (errno == ENOENT)
765                                 err = 0;
766                         break;
767                 }
768                 num_elems += dump_map_elem(fd, key, value, &info, btf, btf_wtr);
769                 prev_key = key;
770         }
771
772         if (json_output)
773                 jsonw_end_array(json_wtr);
774         else if (btf) {
775                 jsonw_end_array(btf_wtr);
776                 jsonw_destroy(&btf_wtr);
777         } else {
778                 printf("Found %u element%s\n", num_elems,
779                        num_elems != 1 ? "s" : "");
780         }
781
782 exit_free:
783         free(key);
784         free(value);
785         close(fd);
786         btf__free(btf);
787
788         return err;
789 }
790
791 static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
792 {
793         *key = NULL;
794         *value = NULL;
795
796         if (info->key_size) {
797                 *key = malloc(info->key_size);
798                 if (!*key) {
799                         p_err("key mem alloc failed");
800                         return -1;
801                 }
802         }
803
804         if (info->value_size) {
805                 *value = alloc_value(info);
806                 if (!*value) {
807                         p_err("value mem alloc failed");
808                         free(*key);
809                         *key = NULL;
810                         return -1;
811                 }
812         }
813
814         return 0;
815 }
816
817 static int do_update(int argc, char **argv)
818 {
819         struct bpf_map_info info = {};
820         __u32 len = sizeof(info);
821         __u32 *value_fd = NULL;
822         __u32 flags = BPF_ANY;
823         void *key, *value;
824         int fd, err;
825
826         if (argc < 2)
827                 usage();
828
829         fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
830         if (fd < 0)
831                 return -1;
832
833         err = alloc_key_value(&info, &key, &value);
834         if (err)
835                 goto exit_free;
836
837         err = parse_elem(argv, &info, key, value, info.key_size,
838                          info.value_size, &flags, &value_fd);
839         if (err)
840                 goto exit_free;
841
842         err = bpf_map_update_elem(fd, key, value, flags);
843         if (err) {
844                 p_err("update failed: %s", strerror(errno));
845                 goto exit_free;
846         }
847
848 exit_free:
849         if (value_fd)
850                 close(*value_fd);
851         free(key);
852         free(value);
853         close(fd);
854
855         if (!err && json_output)
856                 jsonw_null(json_wtr);
857         return err;
858 }
859
860 static int do_lookup(int argc, char **argv)
861 {
862         struct bpf_map_info info = {};
863         __u32 len = sizeof(info);
864         json_writer_t *btf_wtr;
865         struct btf *btf = NULL;
866         void *key, *value;
867         int err;
868         int fd;
869
870         if (argc < 2)
871                 usage();
872
873         fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
874         if (fd < 0)
875                 return -1;
876
877         err = alloc_key_value(&info, &key, &value);
878         if (err)
879                 goto exit_free;
880
881         err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
882         if (err)
883                 goto exit_free;
884
885         err = bpf_map_lookup_elem(fd, key, value);
886         if (err) {
887                 if (errno == ENOENT) {
888                         if (json_output) {
889                                 jsonw_null(json_wtr);
890                         } else {
891                                 printf("key:\n");
892                                 fprint_hex(stdout, key, info.key_size, " ");
893                                 printf("\n\nNot found\n");
894                         }
895                 } else {
896                         p_err("lookup failed: %s", strerror(errno));
897                 }
898
899                 goto exit_free;
900         }
901
902         /* here means bpf_map_lookup_elem() succeeded */
903         err = btf__get_from_id(info.btf_id, &btf);
904         if (err) {
905                 p_err("failed to get btf");
906                 goto exit_free;
907         }
908
909         if (json_output) {
910                 print_entry_json(&info, key, value, btf);
911         } else if (btf) {
912                 /* if here json_wtr wouldn't have been initialised,
913                  * so let's create separate writer for btf
914                  */
915                 btf_wtr = get_btf_writer();
916                 if (!btf_wtr) {
917                         p_info("failed to create json writer for btf. falling back to plain output");
918                         btf__free(btf);
919                         btf = NULL;
920                         print_entry_plain(&info, key, value);
921                 } else {
922                         struct btf_dumper d = {
923                                 .btf = btf,
924                                 .jw = btf_wtr,
925                                 .is_plain_text = true,
926                         };
927
928                         do_dump_btf(&d, &info, key, value);
929                         jsonw_destroy(&btf_wtr);
930                 }
931         } else {
932                 print_entry_plain(&info, key, value);
933         }
934
935 exit_free:
936         free(key);
937         free(value);
938         close(fd);
939         btf__free(btf);
940
941         return err;
942 }
943
944 static int do_getnext(int argc, char **argv)
945 {
946         struct bpf_map_info info = {};
947         __u32 len = sizeof(info);
948         void *key, *nextkey;
949         int err;
950         int fd;
951
952         if (argc < 2)
953                 usage();
954
955         fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
956         if (fd < 0)
957                 return -1;
958
959         key = malloc(info.key_size);
960         nextkey = malloc(info.key_size);
961         if (!key || !nextkey) {
962                 p_err("mem alloc failed");
963                 err = -1;
964                 goto exit_free;
965         }
966
967         if (argc) {
968                 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
969                                  NULL, NULL);
970                 if (err)
971                         goto exit_free;
972         } else {
973                 free(key);
974                 key = NULL;
975         }
976
977         err = bpf_map_get_next_key(fd, key, nextkey);
978         if (err) {
979                 p_err("can't get next key: %s", strerror(errno));
980                 goto exit_free;
981         }
982
983         if (json_output) {
984                 jsonw_start_object(json_wtr);
985                 if (key) {
986                         jsonw_name(json_wtr, "key");
987                         print_hex_data_json(key, info.key_size);
988                 } else {
989                         jsonw_null_field(json_wtr, "key");
990                 }
991                 jsonw_name(json_wtr, "next_key");
992                 print_hex_data_json(nextkey, info.key_size);
993                 jsonw_end_object(json_wtr);
994         } else {
995                 if (key) {
996                         printf("key:\n");
997                         fprint_hex(stdout, key, info.key_size, " ");
998                         printf("\n");
999                 } else {
1000                         printf("key: None\n");
1001                 }
1002                 printf("next key:\n");
1003                 fprint_hex(stdout, nextkey, info.key_size, " ");
1004                 printf("\n");
1005         }
1006
1007 exit_free:
1008         free(nextkey);
1009         free(key);
1010         close(fd);
1011
1012         return err;
1013 }
1014
1015 static int do_delete(int argc, char **argv)
1016 {
1017         struct bpf_map_info info = {};
1018         __u32 len = sizeof(info);
1019         void *key;
1020         int err;
1021         int fd;
1022
1023         if (argc < 2)
1024                 usage();
1025
1026         fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1027         if (fd < 0)
1028                 return -1;
1029
1030         key = malloc(info.key_size);
1031         if (!key) {
1032                 p_err("mem alloc failed");
1033                 err = -1;
1034                 goto exit_free;
1035         }
1036
1037         err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1038         if (err)
1039                 goto exit_free;
1040
1041         err = bpf_map_delete_elem(fd, key);
1042         if (err)
1043                 p_err("delete failed: %s", strerror(errno));
1044
1045 exit_free:
1046         free(key);
1047         close(fd);
1048
1049         if (!err && json_output)
1050                 jsonw_null(json_wtr);
1051         return err;
1052 }
1053
1054 static int do_pin(int argc, char **argv)
1055 {
1056         int err;
1057
1058         err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
1059         if (!err && json_output)
1060                 jsonw_null(json_wtr);
1061         return err;
1062 }
1063
1064 static int do_create(int argc, char **argv)
1065 {
1066         struct bpf_create_map_attr attr = { NULL, };
1067         const char *pinfile;
1068         int err, fd;
1069
1070         if (!REQ_ARGS(7))
1071                 return -1;
1072         pinfile = GET_ARG();
1073
1074         while (argc) {
1075                 if (!REQ_ARGS(2))
1076                         return -1;
1077
1078                 if (is_prefix(*argv, "type")) {
1079                         NEXT_ARG();
1080
1081                         if (attr.map_type) {
1082                                 p_err("map type already specified");
1083                                 return -1;
1084                         }
1085
1086                         attr.map_type = map_type_from_str(*argv);
1087                         if ((int)attr.map_type < 0) {
1088                                 p_err("unrecognized map type: %s", *argv);
1089                                 return -1;
1090                         }
1091                         NEXT_ARG();
1092                 } else if (is_prefix(*argv, "name")) {
1093                         NEXT_ARG();
1094                         attr.name = GET_ARG();
1095                 } else if (is_prefix(*argv, "key")) {
1096                         if (parse_u32_arg(&argc, &argv, &attr.key_size,
1097                                           "key size"))
1098                                 return -1;
1099                 } else if (is_prefix(*argv, "value")) {
1100                         if (parse_u32_arg(&argc, &argv, &attr.value_size,
1101                                           "value size"))
1102                                 return -1;
1103                 } else if (is_prefix(*argv, "entries")) {
1104                         if (parse_u32_arg(&argc, &argv, &attr.max_entries,
1105                                           "max entries"))
1106                                 return -1;
1107                 } else if (is_prefix(*argv, "flags")) {
1108                         if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1109                                           "flags"))
1110                                 return -1;
1111                 } else if (is_prefix(*argv, "dev")) {
1112                         NEXT_ARG();
1113
1114                         if (attr.map_ifindex) {
1115                                 p_err("offload device already specified");
1116                                 return -1;
1117                         }
1118
1119                         attr.map_ifindex = if_nametoindex(*argv);
1120                         if (!attr.map_ifindex) {
1121                                 p_err("unrecognized netdevice '%s': %s",
1122                                       *argv, strerror(errno));
1123                                 return -1;
1124                         }
1125                         NEXT_ARG();
1126                 }
1127         }
1128
1129         if (!attr.name) {
1130                 p_err("map name not specified");
1131                 return -1;
1132         }
1133
1134         set_max_rlimit();
1135
1136         fd = bpf_create_map_xattr(&attr);
1137         if (fd < 0) {
1138                 p_err("map create failed: %s", strerror(errno));
1139                 return -1;
1140         }
1141
1142         err = do_pin_fd(fd, pinfile);
1143         close(fd);
1144         if (err)
1145                 return err;
1146
1147         if (json_output)
1148                 jsonw_null(json_wtr);
1149         return 0;
1150 }
1151
1152 static int do_help(int argc, char **argv)
1153 {
1154         if (json_output) {
1155                 jsonw_null(json_wtr);
1156                 return 0;
1157         }
1158
1159         fprintf(stderr,
1160                 "Usage: %s %s { show | list }   [MAP]\n"
1161                 "       %s %s create     FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1162                 "                              entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1163                 "                              [dev NAME]\n"
1164                 "       %s %s dump       MAP\n"
1165                 "       %s %s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1166                 "       %s %s lookup     MAP [key DATA]\n"
1167                 "       %s %s getnext    MAP [key DATA]\n"
1168                 "       %s %s delete     MAP  key DATA\n"
1169                 "       %s %s pin        MAP  FILE\n"
1170                 "       %s %s event_pipe MAP [cpu N index M]\n"
1171                 "       %s %s help\n"
1172                 "\n"
1173                 "       " HELP_SPEC_MAP "\n"
1174                 "       DATA := { [hex] BYTES }\n"
1175                 "       " HELP_SPEC_PROGRAM "\n"
1176                 "       VALUE := { DATA | MAP | PROG }\n"
1177                 "       UPDATE_FLAGS := { any | exist | noexist }\n"
1178                 "       TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1179                 "                 percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1180                 "                 lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1181                 "                 devmap | sockmap | cpumap | xskmap | sockhash |\n"
1182                 "                 cgroup_storage | reuseport_sockarray | percpu_cgroup_storage }\n"
1183                 "       " HELP_SPEC_OPTIONS "\n"
1184                 "",
1185                 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1186                 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1187                 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
1188                 bin_name, argv[-2]);
1189
1190         return 0;
1191 }
1192
1193 static const struct cmd cmds[] = {
1194         { "show",       do_show },
1195         { "list",       do_show },
1196         { "help",       do_help },
1197         { "dump",       do_dump },
1198         { "update",     do_update },
1199         { "lookup",     do_lookup },
1200         { "getnext",    do_getnext },
1201         { "delete",     do_delete },
1202         { "pin",        do_pin },
1203         { "event_pipe", do_event_pipe },
1204         { "create",     do_create },
1205         { 0 }
1206 };
1207
1208 int do_map(int argc, char **argv)
1209 {
1210         return cmd_select(cmds, argc, argv, do_help);
1211 }