blob: a6cdb640a0d7c309ef3f926a5d0605ee1a0858ef [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35
36#include <assert.h>
37#include <ctype.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <sys/types.h>
46#include <sys/stat.h>
47
48#include <bpf.h>
49
50#include "main.h"
51
52static const char * const map_type_name[] = {
53 [BPF_MAP_TYPE_UNSPEC] = "unspec",
54 [BPF_MAP_TYPE_HASH] = "hash",
55 [BPF_MAP_TYPE_ARRAY] = "array",
56 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
57 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
58 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
59 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
60 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
61 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
62 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
63 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
64 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
65 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
66 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
67 [BPF_MAP_TYPE_DEVMAP] = "devmap",
68 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
Roman Gushchina55aaf62018-01-19 14:17:45 +000069 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
Jakub Kicinski71bb4282017-10-04 20:10:04 -070070};
71
72static unsigned int get_possible_cpus(void)
73{
74 static unsigned int result;
75 char buf[128];
76 long int n;
77 char *ptr;
78 int fd;
79
80 if (result)
81 return result;
82
83 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
84 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -070085 p_err("can't open sysfs possible cpus");
Jakub Kicinski71bb4282017-10-04 20:10:04 -070086 exit(-1);
87 }
88
89 n = read(fd, buf, sizeof(buf));
90 if (n < 2) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -070091 p_err("can't read sysfs possible cpus");
Jakub Kicinski71bb4282017-10-04 20:10:04 -070092 exit(-1);
93 }
94 close(fd);
95
96 if (n == sizeof(buf)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -070097 p_err("read sysfs possible cpus overflow");
Jakub Kicinski71bb4282017-10-04 20:10:04 -070098 exit(-1);
99 }
100
101 ptr = buf;
102 n = 0;
103 while (*ptr && *ptr != '\n') {
104 unsigned int a, b;
105
106 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
107 n += b - a + 1;
108
109 ptr = strchr(ptr, '-') + 1;
110 } else if (sscanf(ptr, "%u", &a) == 1) {
111 n++;
112 } else {
113 assert(0);
114 }
115
116 while (isdigit(*ptr))
117 ptr++;
118 if (*ptr == ',')
119 ptr++;
120 }
121
122 result = n;
123
124 return result;
125}
126
127static bool map_is_per_cpu(__u32 type)
128{
129 return type == BPF_MAP_TYPE_PERCPU_HASH ||
130 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
131 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
132}
133
134static bool map_is_map_of_maps(__u32 type)
135{
136 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
137 type == BPF_MAP_TYPE_HASH_OF_MAPS;
138}
139
140static bool map_is_map_of_progs(__u32 type)
141{
142 return type == BPF_MAP_TYPE_PROG_ARRAY;
143}
144
145static void *alloc_value(struct bpf_map_info *info)
146{
147 if (map_is_per_cpu(info->type))
148 return malloc(info->value_size * get_possible_cpus());
149 else
150 return malloc(info->value_size);
151}
152
153static int map_parse_fd(int *argc, char ***argv)
154{
155 int fd;
156
157 if (is_prefix(**argv, "id")) {
158 unsigned int id;
159 char *endptr;
160
161 NEXT_ARGP();
162
163 id = strtoul(**argv, &endptr, 0);
164 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700165 p_err("can't parse %s as ID", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700166 return -1;
167 }
168 NEXT_ARGP();
169
170 fd = bpf_map_get_fd_by_id(id);
171 if (fd < 0)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700172 p_err("get map by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700173 return fd;
174 } else if (is_prefix(**argv, "pinned")) {
175 char *path;
176
177 NEXT_ARGP();
178
179 path = **argv;
180 NEXT_ARGP();
181
182 return open_obj_pinned_any(path, BPF_OBJ_MAP);
183 }
184
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700185 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700186 return -1;
187}
188
189static int
190map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
191{
192 int err;
193 int fd;
194
195 fd = map_parse_fd(argc, argv);
196 if (fd < 0)
197 return -1;
198
199 err = bpf_obj_get_info_by_fd(fd, info, info_len);
200 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700201 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700202 close(fd);
203 return err;
204 }
205
206 return fd;
207}
208
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700209static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
210 unsigned char *value)
211{
212 jsonw_start_object(json_wtr);
213
214 if (!map_is_per_cpu(info->type)) {
215 jsonw_name(json_wtr, "key");
216 print_hex_data_json(key, info->key_size);
217 jsonw_name(json_wtr, "value");
218 print_hex_data_json(value, info->value_size);
219 } else {
220 unsigned int i, n;
221
222 n = get_possible_cpus();
223
224 jsonw_name(json_wtr, "key");
225 print_hex_data_json(key, info->key_size);
226
227 jsonw_name(json_wtr, "values");
228 jsonw_start_array(json_wtr);
229 for (i = 0; i < n; i++) {
230 jsonw_start_object(json_wtr);
231
232 jsonw_int_field(json_wtr, "cpu", i);
233
234 jsonw_name(json_wtr, "value");
235 print_hex_data_json(value + i * info->value_size,
236 info->value_size);
237
238 jsonw_end_object(json_wtr);
239 }
240 jsonw_end_array(json_wtr);
241 }
242
243 jsonw_end_object(json_wtr);
244}
245
246static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
247 unsigned char *value)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700248{
249 if (!map_is_per_cpu(info->type)) {
250 bool single_line, break_names;
251
252 break_names = info->key_size > 16 || info->value_size > 16;
253 single_line = info->key_size + info->value_size <= 24 &&
254 !break_names;
255
256 printf("key:%c", break_names ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700257 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700258
259 printf(single_line ? " " : "\n");
260
261 printf("value:%c", break_names ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700262 fprint_hex(stdout, value, info->value_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700263
264 printf("\n");
265 } else {
266 unsigned int i, n;
267
268 n = get_possible_cpus();
269
270 printf("key:\n");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700271 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700272 printf("\n");
273 for (i = 0; i < n; i++) {
274 printf("value (CPU %02d):%c",
275 i, info->value_size > 16 ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700276 fprint_hex(stdout, value + i * info->value_size,
277 info->value_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700278 printf("\n");
279 }
280 }
281}
282
283static char **parse_bytes(char **argv, const char *name, unsigned char *val,
284 unsigned int n)
285{
Quentin Monnet0c90f222018-04-17 19:46:34 -0700286 unsigned int i = 0, base = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700287 char *endptr;
288
Quentin Monnet0c90f222018-04-17 19:46:34 -0700289 if (is_prefix(*argv, "hex")) {
290 base = 16;
291 argv++;
292 }
293
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700294 while (i < n && argv[i]) {
Quentin Monnet0c90f222018-04-17 19:46:34 -0700295 val[i] = strtoul(argv[i], &endptr, base);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700296 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700297 p_err("error parsing byte: %s", argv[i]);
Quentin Monnetd9c0b482017-10-19 15:46:23 -0700298 return NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700299 }
300 i++;
301 }
302
303 if (i != n) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700304 p_err("%s expected %d bytes got %d", name, n, i);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700305 return NULL;
306 }
307
308 return argv + i;
309}
310
311static int parse_elem(char **argv, struct bpf_map_info *info,
312 void *key, void *value, __u32 key_size, __u32 value_size,
313 __u32 *flags, __u32 **value_fd)
314{
315 if (!*argv) {
316 if (!key && !value)
317 return 0;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700318 p_err("did not find %s", key ? "key" : "value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700319 return -1;
320 }
321
322 if (is_prefix(*argv, "key")) {
323 if (!key) {
324 if (key_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700325 p_err("duplicate key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700326 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700327 p_err("unnecessary key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700328 return -1;
329 }
330
331 argv = parse_bytes(argv + 1, "key", key, key_size);
332 if (!argv)
333 return -1;
334
335 return parse_elem(argv, info, NULL, value, key_size, value_size,
336 flags, value_fd);
337 } else if (is_prefix(*argv, "value")) {
338 int fd;
339
340 if (!value) {
341 if (value_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700342 p_err("duplicate value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700343 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700344 p_err("unnecessary value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700345 return -1;
346 }
347
348 argv++;
349
350 if (map_is_map_of_maps(info->type)) {
351 int argc = 2;
352
353 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700354 p_err("value smaller than 4B for map in map?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700355 return -1;
356 }
357 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700358 p_err("not enough value arguments for map in map");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700359 return -1;
360 }
361
362 fd = map_parse_fd(&argc, &argv);
363 if (fd < 0)
364 return -1;
365
366 *value_fd = value;
367 **value_fd = fd;
368 } else if (map_is_map_of_progs(info->type)) {
369 int argc = 2;
370
371 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700372 p_err("value smaller than 4B for map of progs?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700373 return -1;
374 }
375 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700376 p_err("not enough value arguments for map of progs");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700377 return -1;
378 }
379
380 fd = prog_parse_fd(&argc, &argv);
381 if (fd < 0)
382 return -1;
383
384 *value_fd = value;
385 **value_fd = fd;
386 } else {
387 argv = parse_bytes(argv, "value", value, value_size);
388 if (!argv)
389 return -1;
390 }
391
392 return parse_elem(argv, info, key, NULL, key_size, value_size,
393 flags, NULL);
394 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
395 is_prefix(*argv, "exist")) {
396 if (!flags) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700397 p_err("flags specified multiple times: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700398 return -1;
399 }
400
401 if (is_prefix(*argv, "any"))
402 *flags = BPF_ANY;
403 else if (is_prefix(*argv, "noexist"))
404 *flags = BPF_NOEXIST;
405 else if (is_prefix(*argv, "exist"))
406 *flags = BPF_EXIST;
407
408 return parse_elem(argv + 1, info, key, value, key_size,
409 value_size, NULL, value_fd);
410 }
411
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700412 p_err("expected key or value, got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700413 return -1;
414}
415
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700416static int show_map_close_json(int fd, struct bpf_map_info *info)
417{
418 char *memlock;
419
420 memlock = get_fdinfo(fd, "memlock");
421 close(fd);
422
423 jsonw_start_object(json_wtr);
424
425 jsonw_uint_field(json_wtr, "id", info->id);
426 if (info->type < ARRAY_SIZE(map_type_name))
427 jsonw_string_field(json_wtr, "type",
428 map_type_name[info->type]);
429 else
430 jsonw_uint_field(json_wtr, "type", info->type);
431
432 if (*info->name)
433 jsonw_string_field(json_wtr, "name", info->name);
434
435 jsonw_name(json_wtr, "flags");
Jakub Kicinski4b6eca92018-03-23 19:45:12 -0700436 jsonw_printf(json_wtr, "%d", info->map_flags);
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800437
438 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
439
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700440 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
441 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
442 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
443
444 if (memlock)
445 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
446 free(memlock);
447
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900448 if (!hash_empty(map_table.table)) {
449 struct pinned_obj *obj;
450
451 jsonw_name(json_wtr, "pinned");
452 jsonw_start_array(json_wtr);
453 hash_for_each_possible(map_table.table, obj, hash, info->id) {
454 if (obj->id == info->id)
455 jsonw_string(json_wtr, obj->path);
456 }
457 jsonw_end_array(json_wtr);
458 }
459
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700460 jsonw_end_object(json_wtr);
461
462 return 0;
463}
464
465static int show_map_close_plain(int fd, struct bpf_map_info *info)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700466{
467 char *memlock;
468
469 memlock = get_fdinfo(fd, "memlock");
470 close(fd);
471
472 printf("%u: ", info->id);
473 if (info->type < ARRAY_SIZE(map_type_name))
474 printf("%s ", map_type_name[info->type]);
475 else
476 printf("type %u ", info->type);
477
478 if (*info->name)
479 printf("name %s ", info->name);
480
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800481 printf("flags 0x%x", info->map_flags);
482 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
483 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700484 printf("\tkey %uB value %uB max_entries %u",
485 info->key_size, info->value_size, info->max_entries);
486
487 if (memlock)
488 printf(" memlock %sB", memlock);
489 free(memlock);
490
491 printf("\n");
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900492 if (!hash_empty(map_table.table)) {
493 struct pinned_obj *obj;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700494
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900495 hash_for_each_possible(map_table.table, obj, hash, info->id) {
496 if (obj->id == info->id)
497 printf("\tpinned %s\n", obj->path);
498 }
499 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700500 return 0;
501}
502
503static int do_show(int argc, char **argv)
504{
505 struct bpf_map_info info = {};
506 __u32 len = sizeof(info);
507 __u32 id = 0;
508 int err;
509 int fd;
510
Prashant Bholec541b732017-11-08 13:55:49 +0900511 if (show_pinned)
512 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900513
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700514 if (argc == 2) {
515 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
516 if (fd < 0)
517 return -1;
518
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700519 if (json_output)
520 return show_map_close_json(fd, &info);
521 else
522 return show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700523 }
524
525 if (argc)
526 return BAD_ARG();
527
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700528 if (json_output)
529 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700530 while (true) {
531 err = bpf_map_get_next_id(id, &id);
532 if (err) {
533 if (errno == ENOENT)
534 break;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700535 p_err("can't get next map: %s%s", strerror(errno),
536 errno == EINVAL ? " -- kernel too old?" : "");
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800537 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700538 }
539
540 fd = bpf_map_get_fd_by_id(id);
541 if (fd < 0) {
Jakub Kicinski8207c6d2017-12-22 11:36:06 -0800542 if (errno == ENOENT)
543 continue;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700544 p_err("can't get map by id (%u): %s",
545 id, strerror(errno));
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800546 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700547 }
548
549 err = bpf_obj_get_info_by_fd(fd, &info, &len);
550 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700551 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700552 close(fd);
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800553 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700554 }
555
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700556 if (json_output)
557 show_map_close_json(fd, &info);
558 else
559 show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700560 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700561 if (json_output)
562 jsonw_end_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700563
564 return errno == ENOENT ? 0 : -1;
565}
566
567static int do_dump(int argc, char **argv)
568{
569 void *key, *value, *prev_key;
570 unsigned int num_elems = 0;
571 struct bpf_map_info info = {};
572 __u32 len = sizeof(info);
573 int err;
574 int fd;
575
576 if (argc != 2)
577 usage();
578
579 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
580 if (fd < 0)
581 return -1;
582
583 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700584 p_err("Dumping maps of maps and program maps not supported");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700585 close(fd);
586 return -1;
587 }
588
589 key = malloc(info.key_size);
590 value = alloc_value(&info);
591 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700592 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700593 err = -1;
594 goto exit_free;
595 }
596
597 prev_key = NULL;
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700598 if (json_output)
599 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700600 while (true) {
601 err = bpf_map_get_next_key(fd, prev_key, key);
602 if (err) {
603 if (errno == ENOENT)
604 err = 0;
605 break;
606 }
607
608 if (!bpf_map_lookup_elem(fd, key, value)) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700609 if (json_output)
610 print_entry_json(&info, key, value);
611 else
612 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700613 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700614 if (json_output) {
615 jsonw_name(json_wtr, "key");
616 print_hex_data_json(key, info.key_size);
617 jsonw_name(json_wtr, "value");
618 jsonw_start_object(json_wtr);
619 jsonw_string_field(json_wtr, "error",
620 "can't lookup element");
621 jsonw_end_object(json_wtr);
622 } else {
623 p_info("can't lookup element with key: ");
624 fprint_hex(stderr, key, info.key_size, " ");
625 fprintf(stderr, "\n");
626 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700627 }
628
629 prev_key = key;
630 num_elems++;
631 }
632
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700633 if (json_output)
634 jsonw_end_array(json_wtr);
635 else
636 printf("Found %u element%s\n", num_elems,
637 num_elems != 1 ? "s" : "");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700638
639exit_free:
640 free(key);
641 free(value);
642 close(fd);
643
644 return err;
645}
646
647static int do_update(int argc, char **argv)
648{
649 struct bpf_map_info info = {};
650 __u32 len = sizeof(info);
651 __u32 *value_fd = NULL;
652 __u32 flags = BPF_ANY;
653 void *key, *value;
654 int fd, err;
655
656 if (argc < 2)
657 usage();
658
659 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
660 if (fd < 0)
661 return -1;
662
663 key = malloc(info.key_size);
664 value = alloc_value(&info);
665 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700666 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700667 err = -1;
668 goto exit_free;
669 }
670
671 err = parse_elem(argv, &info, key, value, info.key_size,
672 info.value_size, &flags, &value_fd);
673 if (err)
674 goto exit_free;
675
676 err = bpf_map_update_elem(fd, key, value, flags);
677 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700678 p_err("update failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700679 goto exit_free;
680 }
681
682exit_free:
683 if (value_fd)
684 close(*value_fd);
685 free(key);
686 free(value);
687 close(fd);
688
Quentin Monnet004b45c2017-10-23 09:24:14 -0700689 if (!err && json_output)
690 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700691 return err;
692}
693
694static int do_lookup(int argc, char **argv)
695{
696 struct bpf_map_info info = {};
697 __u32 len = sizeof(info);
698 void *key, *value;
699 int err;
700 int fd;
701
702 if (argc < 2)
703 usage();
704
705 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
706 if (fd < 0)
707 return -1;
708
709 key = malloc(info.key_size);
710 value = alloc_value(&info);
711 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700712 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700713 err = -1;
714 goto exit_free;
715 }
716
717 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
718 if (err)
719 goto exit_free;
720
721 err = bpf_map_lookup_elem(fd, key, value);
722 if (!err) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700723 if (json_output)
724 print_entry_json(&info, key, value);
725 else
726 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700727 } else if (errno == ENOENT) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700728 if (json_output) {
729 jsonw_null(json_wtr);
730 } else {
731 printf("key:\n");
732 fprint_hex(stdout, key, info.key_size, " ");
733 printf("\n\nNot found\n");
734 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700735 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700736 p_err("lookup failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700737 }
738
739exit_free:
740 free(key);
741 free(value);
742 close(fd);
743
744 return err;
745}
746
747static int do_getnext(int argc, char **argv)
748{
749 struct bpf_map_info info = {};
750 __u32 len = sizeof(info);
751 void *key, *nextkey;
752 int err;
753 int fd;
754
755 if (argc < 2)
756 usage();
757
758 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
759 if (fd < 0)
760 return -1;
761
762 key = malloc(info.key_size);
763 nextkey = malloc(info.key_size);
764 if (!key || !nextkey) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700765 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700766 err = -1;
767 goto exit_free;
768 }
769
770 if (argc) {
771 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
772 NULL, NULL);
773 if (err)
774 goto exit_free;
775 } else {
776 free(key);
777 key = NULL;
778 }
779
780 err = bpf_map_get_next_key(fd, key, nextkey);
781 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700782 p_err("can't get next key: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700783 goto exit_free;
784 }
785
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700786 if (json_output) {
787 jsonw_start_object(json_wtr);
788 if (key) {
789 jsonw_name(json_wtr, "key");
790 print_hex_data_json(key, info.key_size);
791 } else {
792 jsonw_null_field(json_wtr, "key");
793 }
794 jsonw_name(json_wtr, "next_key");
795 print_hex_data_json(nextkey, info.key_size);
796 jsonw_end_object(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700797 } else {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700798 if (key) {
799 printf("key:\n");
800 fprint_hex(stdout, key, info.key_size, " ");
801 printf("\n");
802 } else {
803 printf("key: None\n");
804 }
805 printf("next key:\n");
806 fprint_hex(stdout, nextkey, info.key_size, " ");
807 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700808 }
809
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700810exit_free:
811 free(nextkey);
812 free(key);
813 close(fd);
814
815 return err;
816}
817
818static int do_delete(int argc, char **argv)
819{
820 struct bpf_map_info info = {};
821 __u32 len = sizeof(info);
822 void *key;
823 int err;
824 int fd;
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 key = malloc(info.key_size);
834 if (!key) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700835 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700836 err = -1;
837 goto exit_free;
838 }
839
840 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
841 if (err)
842 goto exit_free;
843
844 err = bpf_map_delete_elem(fd, key);
845 if (err)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700846 p_err("delete failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700847
848exit_free:
849 free(key);
850 close(fd);
851
Quentin Monnet004b45c2017-10-23 09:24:14 -0700852 if (!err && json_output)
853 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700854 return err;
855}
856
857static int do_pin(int argc, char **argv)
858{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700859 int err;
860
861 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
862 if (!err && json_output)
863 jsonw_null(json_wtr);
864 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700865}
866
867static int do_help(int argc, char **argv)
868{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700869 if (json_output) {
870 jsonw_null(json_wtr);
871 return 0;
872 }
873
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700874 fprintf(stderr,
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800875 "Usage: %s %s { show | list } [MAP]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700876 " %s %s dump MAP\n"
Quentin Monnet0c90f222018-04-17 19:46:34 -0700877 " %s %s update MAP key [hex] BYTES value [hex] VALUE [UPDATE_FLAGS]\n"
878 " %s %s lookup MAP key [hex] BYTES\n"
879 " %s %s getnext MAP [key [hex] BYTES]\n"
880 " %s %s delete MAP key [hex] BYTES\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700881 " %s %s pin MAP FILE\n"
882 " %s %s help\n"
883 "\n"
884 " MAP := { id MAP_ID | pinned FILE }\n"
885 " " HELP_SPEC_PROGRAM "\n"
886 " VALUE := { BYTES | MAP | PROG }\n"
887 " UPDATE_FLAGS := { any | exist | noexist }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -0700888 " " HELP_SPEC_OPTIONS "\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700889 "",
890 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
891 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
892 bin_name, argv[-2], bin_name, argv[-2]);
893
894 return 0;
895}
896
897static const struct cmd cmds[] = {
898 { "show", do_show },
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800899 { "list", do_show },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700900 { "help", do_help },
901 { "dump", do_dump },
902 { "update", do_update },
903 { "lookup", do_lookup },
904 { "getnext", do_getnext },
905 { "delete", do_delete },
906 { "pin", do_pin },
907 { 0 }
908};
909
910int do_map(int argc, char **argv)
911{
912 return cmd_select(cmds, argc, argv, do_help);
913}