blob: d7344fcd208926df8e2c1493a27c4ad7aa5c950f [file] [log] [blame]
Jakub Kicinski02ff58d2018-12-12 19:59:25 -08001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003
Jakub Kicinski71bb4282017-10-04 20:10:04 -07004#include <assert.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -07005#include <errno.h>
6#include <fcntl.h>
Okash Khawaja2d3feca2018-07-13 21:57:04 -07007#include <linux/err.h>
Yonghong Song573b3aa2018-07-30 08:49:03 -07008#include <linux/kernel.h>
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07009#include <net/if.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070010#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
Okash Khawaja2d3feca2018-07-13 21:57:04 -070020#include "btf.h"
21#include "json_writer.h"
Jakub Kicinski71bb4282017-10-04 20:10:04 -070022#include "main.h"
23
24static const char * const map_type_name[] = {
David Calaveraffac28f2018-11-23 15:58:39 -080025 [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",
Roman Gushchine5487092018-09-28 14:45:51 +000046 [BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE] = "percpu_cgroup_storage",
David Calaveraffac28f2018-11-23 15:58:39 -080047 [BPF_MAP_TYPE_QUEUE] = "queue",
48 [BPF_MAP_TYPE_STACK] = "stack",
Jakub Kicinski71bb4282017-10-04 20:10:04 -070049};
50
Jakub Kicinski71bb4282017-10-04 20:10:04 -070051static bool map_is_per_cpu(__u32 type)
52{
53 return type == BPF_MAP_TYPE_PERCPU_HASH ||
54 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
Roman Gushchine5487092018-09-28 14:45:51 +000055 type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
56 type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
Jakub Kicinski71bb4282017-10-04 20:10:04 -070057}
58
59static 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
65static bool map_is_map_of_progs(__u32 type)
66{
67 return type == BPF_MAP_TYPE_PROG_ARRAY;
68}
69
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -070070static 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -070081static void *alloc_value(struct bpf_map_info *info)
82{
83 if (map_is_per_cpu(info->type))
Yonghong Song573b3aa2018-07-30 08:49:03 -070084 return malloc(round_up(info->value_size, 8) *
85 get_possible_cpus());
Jakub Kicinski71bb4282017-10-04 20:10:04 -070086 else
87 return malloc(info->value_size);
88}
89
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -070090int map_parse_fd(int *argc, char ***argv)
Jakub Kicinski71bb4282017-10-04 20:10:04 -070091{
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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700102 p_err("can't parse %s as ID", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700103 return -1;
104 }
105 NEXT_ARGP();
106
107 fd = bpf_map_get_fd_by_id(id);
108 if (fd < 0)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700109 p_err("get map by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700110 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
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700122 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700123 return -1;
124}
125
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700126int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700127{
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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700137 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700138 close(fd);
139 return err;
140 }
141
142 return fd;
143}
144
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700145static 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
Yonghong Song1a86ad82018-08-29 14:43:15 -0700160 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;
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700165
Yonghong Song1a86ad82018-08-29 14:43:15 -0700166 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 }
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700182
183err_end_obj:
184 /* end of key-value pair */
185 jsonw_end_object(d->jw);
186
187 return ret;
188}
189
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700190static 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
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700201static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700202 unsigned char *value, struct btf *btf)
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700203{
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);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700211 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 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700221 } else {
Yonghong Song573b3aa2018-07-30 08:49:03 -0700222 unsigned int i, n, step;
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700223
224 n = get_possible_cpus();
Yonghong Song573b3aa2018-07-30 08:49:03 -0700225 step = round_up(info->value_size, 8);
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700226
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");
Yonghong Song573b3aa2018-07-30 08:49:03 -0700238 print_hex_data_json(value + i * step,
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700239 info->value_size);
240
241 jsonw_end_object(json_wtr);
242 }
243 jsonw_end_array(json_wtr);
Yonghong Song1a86ad82018-08-29 14:43:15 -0700244 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 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700254 }
255
256 jsonw_end_object(json_wtr);
257}
258
Prashant Bhole8ec92dc2018-10-09 10:04:52 +0900259static 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
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700278static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
279 unsigned char *value)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700280{
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
Stanislav Fomichev04a5d322019-01-16 11:10:01 -0800288 if (info->key_size) {
289 printf("key:%c", break_names ? '\n' : ' ');
290 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700291
Stanislav Fomichev04a5d322019-01-16 11:10:01 -0800292 printf(single_line ? " " : "\n");
293 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700294
Stanislav Fomichev04a5d322019-01-16 11:10:01 -0800295 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 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700303
304 printf("\n");
305 } else {
Yonghong Song573b3aa2018-07-30 08:49:03 -0700306 unsigned int i, n, step;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700307
308 n = get_possible_cpus();
Yonghong Song573b3aa2018-07-30 08:49:03 -0700309 step = round_up(info->value_size, 8);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700310
Stanislav Fomichev04a5d322019-01-16 11:10:01 -0800311 if (info->key_size) {
312 printf("key:\n");
313 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700314 printf("\n");
315 }
Stanislav Fomichev04a5d322019-01-16 11:10:01 -0800316 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 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700328 }
329}
330
331static char **parse_bytes(char **argv, const char *name, unsigned char *val,
332 unsigned int n)
333{
Quentin Monnet0c90f222018-04-17 19:46:34 -0700334 unsigned int i = 0, base = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700335 char *endptr;
336
Quentin Monnet0c90f222018-04-17 19:46:34 -0700337 if (is_prefix(*argv, "hex")) {
338 base = 16;
339 argv++;
340 }
341
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700342 while (i < n && argv[i]) {
Quentin Monnet0c90f222018-04-17 19:46:34 -0700343 val[i] = strtoul(argv[i], &endptr, base);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700344 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700345 p_err("error parsing byte: %s", argv[i]);
Quentin Monnetd9c0b482017-10-19 15:46:23 -0700346 return NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700347 }
348 i++;
349 }
350
351 if (i != n) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700352 p_err("%s expected %d bytes got %d", name, n, i);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700353 return NULL;
354 }
355
356 return argv + i;
357}
358
359static 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;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700366 p_err("did not find %s", key ? "key" : "value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700367 return -1;
368 }
369
370 if (is_prefix(*argv, "key")) {
371 if (!key) {
372 if (key_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700373 p_err("duplicate key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700374 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700375 p_err("unnecessary key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700376 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)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700390 p_err("duplicate value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700391 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700392 p_err("unnecessary value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700393 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700402 p_err("value smaller than 4B for map in map?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700403 return -1;
404 }
405 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700406 p_err("not enough value arguments for map in map");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700407 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700420 p_err("value smaller than 4B for map of progs?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700421 return -1;
422 }
423 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700424 p_err("not enough value arguments for map of progs");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700425 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700445 p_err("flags specified multiple times: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700446 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
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700460 p_err("expected key or value, got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700461 return -1;
462}
463
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700464static int show_map_close_json(int fd, struct bpf_map_info *info)
465{
466 char *memlock;
467
468 memlock = get_fdinfo(fd, "memlock");
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700469
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");
Jakub Kicinski4b6eca92018-03-23 19:45:12 -0700483 jsonw_printf(json_wtr, "%d", info->map_flags);
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800484
485 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
486
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700487 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
Quentin Monnet99a44bef2018-11-30 16:25:48 +0000495 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
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900519 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
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700531 jsonw_end_object(json_wtr);
532
533 return 0;
534}
535
536static int show_map_close_plain(int fd, struct bpf_map_info *info)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700537{
538 char *memlock;
539
540 memlock = get_fdinfo(fd, "memlock");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700541
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
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800551 printf("flags 0x%x", info->map_flags);
552 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
553 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700554 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
Quentin Monnet99a44bef2018-11-30 16:25:48 +0000561 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700585 printf("\n");
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900586 if (!hash_empty(map_table.table)) {
587 struct pinned_obj *obj;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700588
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900589 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 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700594 return 0;
595}
596
597static 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
Prashant Bholec541b732017-11-08 13:55:49 +0900605 if (show_pinned)
606 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900607
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700608 if (argc == 2) {
609 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
610 if (fd < 0)
611 return -1;
612
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700613 if (json_output)
614 return show_map_close_json(fd, &info);
615 else
616 return show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700617 }
618
619 if (argc)
620 return BAD_ARG();
621
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700622 if (json_output)
623 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700624 while (true) {
625 err = bpf_map_get_next_id(id, &id);
626 if (err) {
627 if (errno == ENOENT)
628 break;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700629 p_err("can't get next map: %s%s", strerror(errno),
630 errno == EINVAL ? " -- kernel too old?" : "");
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800631 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700632 }
633
634 fd = bpf_map_get_fd_by_id(id);
635 if (fd < 0) {
Jakub Kicinski8207c6d2017-12-22 11:36:06 -0800636 if (errno == ENOENT)
637 continue;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700638 p_err("can't get map by id (%u): %s",
639 id, strerror(errno));
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800640 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700641 }
642
643 err = bpf_obj_get_info_by_fd(fd, &info, &len);
644 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700645 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700646 close(fd);
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800647 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700648 }
649
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700650 if (json_output)
651 show_map_close_json(fd, &info);
652 else
653 show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700654 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700655 if (json_output)
656 jsonw_end_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700657
658 return errno == ENOENT ? 0 : -1;
659}
660
Prashant Bhole18a781d2018-10-09 10:04:51 +0900661static 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;
Prashant Bhole8ec92dc2018-10-09 10:04:52 +0900666 int lookup_errno;
Prashant Bhole18a781d2018-10-09 10:04:51 +0900667
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 */
Prashant Bhole8ec92dc2018-10-09 10:04:52 +0900689 lookup_errno = errno;
690
Prashant Bhole18a781d2018-10-09 10:04:51 +0900691 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);
Prashant Bhole8ec92dc2018-10-09 10:04:52 +0900700 jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
Prashant Bhole18a781d2018-10-09 10:04:51 +0900701 jsonw_end_object(json_wtr);
702 } else {
David Ahernbf598a82018-11-08 13:00:07 -0800703 if (errno == ENOENT)
704 print_entry_plain(map_info, key, NULL);
705 else
706 print_entry_error(map_info, key,
707 strerror(lookup_errno));
Prashant Bhole18a781d2018-10-09 10:04:51 +0900708 }
709
710 return 0;
711}
712
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700713static int do_dump(int argc, char **argv)
714{
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700715 struct bpf_map_info info = {};
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700716 void *key, *value, *prev_key;
717 unsigned int num_elems = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700718 __u32 len = sizeof(info);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700719 json_writer_t *btf_wtr;
720 struct btf *btf = NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700721 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700731 key = malloc(info.key_size);
732 value = alloc_value(&info);
733 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700734 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700735 err = -1;
736 goto exit_free;
737 }
738
739 prev_key = NULL;
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700740
Martin KaFai Lau1d2f44c2018-11-23 16:44:32 -0800741 err = btf__get_from_id(info.btf_id, &btf);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700742 if (err) {
743 p_err("failed to get btf");
744 goto exit_free;
745 }
746
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700747 if (json_output)
748 jsonw_start_array(json_wtr);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700749 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700761 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 }
Prashant Bhole18a781d2018-10-09 10:04:51 +0900768 num_elems += dump_map_elem(fd, key, value, &info, btf, btf_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700769 prev_key = key;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700770 }
771
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700772 if (json_output)
773 jsonw_end_array(json_wtr);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700774 else if (btf) {
775 jsonw_end_array(btf_wtr);
776 jsonw_destroy(&btf_wtr);
777 } else {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700778 printf("Found %u element%s\n", num_elems,
779 num_elems != 1 ? "s" : "");
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700780 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700781
782exit_free:
783 free(key);
784 free(value);
785 close(fd);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700786 btf__free(btf);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700787
788 return err;
789}
790
Stanislav Fomichev7d7209c2019-01-16 11:09:59 -0800791static 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700817static 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
Stanislav Fomichev7d7209c2019-01-16 11:09:59 -0800833 err = alloc_key_value(&info, &key, &value);
834 if (err)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700835 goto exit_free;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700836
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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700844 p_err("update failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700845 goto exit_free;
846 }
847
848exit_free:
849 if (value_fd)
850 close(*value_fd);
851 free(key);
852 free(value);
853 close(fd);
854
Quentin Monnet004b45c2017-10-23 09:24:14 -0700855 if (!err && json_output)
856 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700857 return err;
858}
859
860static int do_lookup(int argc, char **argv)
861{
862 struct bpf_map_info info = {};
863 __u32 len = sizeof(info);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700864 json_writer_t *btf_wtr;
865 struct btf *btf = NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700866 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
Stanislav Fomichev8a89fff2019-01-16 11:10:00 -0800877 err = alloc_key_value(&info, &key, &value);
878 if (err)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700879 goto exit_free;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700880
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);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700886 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 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700895 } else {
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700896 p_err("lookup failed: %s", strerror(errno));
897 }
898
899 goto exit_free;
900 }
901
902 /* here means bpf_map_lookup_elem() succeeded */
Martin KaFai Lau1d2f44c2018-11-23 16:44:32 -0800903 err = btf__get_from_id(info.btf_id, &btf);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700904 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);
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700930 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700931 } else {
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700932 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700933 }
934
935exit_free:
936 free(key);
937 free(value);
938 close(fd);
Okash Khawaja2d3feca2018-07-13 21:57:04 -0700939 btf__free(btf);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700940
941 return err;
942}
943
944static 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700962 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700963 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700979 p_err("can't get next key: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700980 goto exit_free;
981 }
982
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700983 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);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700994 } else {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700995 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");
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001005 }
1006
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001007exit_free:
1008 free(nextkey);
1009 free(key);
1010 close(fd);
1011
1012 return err;
1013}
1014
1015static 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) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -07001032 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001033 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)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -07001043 p_err("delete failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001044
1045exit_free:
1046 free(key);
1047 close(fd);
1048
Quentin Monnet004b45c2017-10-23 09:24:14 -07001049 if (!err && json_output)
1050 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001051 return err;
1052}
1053
1054static int do_pin(int argc, char **argv)
1055{
Quentin Monnet004b45c2017-10-23 09:24:14 -07001056 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;
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001062}
1063
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001064static 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
Quentin Monnet8302b9b2018-11-07 12:29:30 +00001134 set_max_rlimit();
1135
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001136 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
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001152static int do_help(int argc, char **argv)
1153{
Quentin Monnet004b45c2017-10-23 09:24:14 -07001154 if (json_output) {
1155 jsonw_null(json_wtr);
1156 return 0;
1157 }
1158
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001159 fprintf(stderr,
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -08001160 "Usage: %s %s { show | list } [MAP]\n"
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001161 " %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"
Jakub Kicinskif412eed2018-05-03 18:37:16 -07001164 " %s %s dump MAP\n"
Stanislav Fomichev7d7209c2019-01-16 11:09:59 -08001165 " %s %s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
Stanislav Fomichev8a89fff2019-01-16 11:10:00 -08001166 " %s %s lookup MAP [key DATA]\n"
Jakub Kicinskif412eed2018-05-03 18:37:16 -07001167 " %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"
Stanislav Fomichev66cf6e02019-01-16 11:10:02 -08001171 " %s %s peek MAP\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001172 " %s %s help\n"
1173 "\n"
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -07001174 " " HELP_SPEC_MAP "\n"
Jakub Kicinskic642ea22018-05-03 18:37:14 -07001175 " DATA := { [hex] BYTES }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001176 " " HELP_SPEC_PROGRAM "\n"
Jakub Kicinskic642ea22018-05-03 18:37:14 -07001177 " VALUE := { DATA | MAP | PROG }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001178 " UPDATE_FLAGS := { any | exist | noexist }\n"
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001179 " TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1180 " percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1181 " lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1182 " devmap | sockmap | cpumap | xskmap | sockhash |\n"
1183 " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -07001184 " " HELP_SPEC_OPTIONS "\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001185 "",
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],
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001188 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
Stanislav Fomichev66cf6e02019-01-16 11:10:02 -08001189 bin_name, argv[-2], bin_name, argv[-2]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001190
1191 return 0;
1192}
1193
1194static const struct cmd cmds[] = {
1195 { "show", do_show },
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -08001196 { "list", do_show },
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001197 { "help", do_help },
1198 { "dump", do_dump },
1199 { "update", do_update },
1200 { "lookup", do_lookup },
1201 { "getnext", do_getnext },
1202 { "delete", do_delete },
1203 { "pin", do_pin },
Jakub Kicinskif412eed2018-05-03 18:37:16 -07001204 { "event_pipe", do_event_pipe },
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -07001205 { "create", do_create },
Stanislav Fomichev66cf6e02019-01-16 11:10:02 -08001206 { "peek", do_lookup },
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001207 { 0 }
1208};
1209
1210int do_map(int argc, char **argv)
1211{
1212 return cmd_select(cmds, argc, argv, do_help);
1213}