blob: f95fa67bb498f3f701e100d3be14ec32ac5fb658 [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{
286 unsigned int i = 0;
287 char *endptr;
288
289 while (i < n && argv[i]) {
290 val[i] = strtoul(argv[i], &endptr, 0);
291 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700292 p_err("error parsing byte: %s", argv[i]);
Quentin Monnetd9c0b482017-10-19 15:46:23 -0700293 return NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700294 }
295 i++;
296 }
297
298 if (i != n) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700299 p_err("%s expected %d bytes got %d", name, n, i);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700300 return NULL;
301 }
302
303 return argv + i;
304}
305
306static int parse_elem(char **argv, struct bpf_map_info *info,
307 void *key, void *value, __u32 key_size, __u32 value_size,
308 __u32 *flags, __u32 **value_fd)
309{
310 if (!*argv) {
311 if (!key && !value)
312 return 0;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700313 p_err("did not find %s", key ? "key" : "value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700314 return -1;
315 }
316
317 if (is_prefix(*argv, "key")) {
318 if (!key) {
319 if (key_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700320 p_err("duplicate key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700321 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700322 p_err("unnecessary key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700323 return -1;
324 }
325
326 argv = parse_bytes(argv + 1, "key", key, key_size);
327 if (!argv)
328 return -1;
329
330 return parse_elem(argv, info, NULL, value, key_size, value_size,
331 flags, value_fd);
332 } else if (is_prefix(*argv, "value")) {
333 int fd;
334
335 if (!value) {
336 if (value_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700337 p_err("duplicate value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700338 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700339 p_err("unnecessary value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700340 return -1;
341 }
342
343 argv++;
344
345 if (map_is_map_of_maps(info->type)) {
346 int argc = 2;
347
348 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700349 p_err("value smaller than 4B for map in map?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700350 return -1;
351 }
352 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700353 p_err("not enough value arguments for map in map");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700354 return -1;
355 }
356
357 fd = map_parse_fd(&argc, &argv);
358 if (fd < 0)
359 return -1;
360
361 *value_fd = value;
362 **value_fd = fd;
363 } else if (map_is_map_of_progs(info->type)) {
364 int argc = 2;
365
366 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700367 p_err("value smaller than 4B for map of progs?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700368 return -1;
369 }
370 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700371 p_err("not enough value arguments for map of progs");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700372 return -1;
373 }
374
375 fd = prog_parse_fd(&argc, &argv);
376 if (fd < 0)
377 return -1;
378
379 *value_fd = value;
380 **value_fd = fd;
381 } else {
382 argv = parse_bytes(argv, "value", value, value_size);
383 if (!argv)
384 return -1;
385 }
386
387 return parse_elem(argv, info, key, NULL, key_size, value_size,
388 flags, NULL);
389 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
390 is_prefix(*argv, "exist")) {
391 if (!flags) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700392 p_err("flags specified multiple times: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700393 return -1;
394 }
395
396 if (is_prefix(*argv, "any"))
397 *flags = BPF_ANY;
398 else if (is_prefix(*argv, "noexist"))
399 *flags = BPF_NOEXIST;
400 else if (is_prefix(*argv, "exist"))
401 *flags = BPF_EXIST;
402
403 return parse_elem(argv + 1, info, key, value, key_size,
404 value_size, NULL, value_fd);
405 }
406
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700407 p_err("expected key or value, got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700408 return -1;
409}
410
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700411static int show_map_close_json(int fd, struct bpf_map_info *info)
412{
413 char *memlock;
414
415 memlock = get_fdinfo(fd, "memlock");
416 close(fd);
417
418 jsonw_start_object(json_wtr);
419
420 jsonw_uint_field(json_wtr, "id", info->id);
421 if (info->type < ARRAY_SIZE(map_type_name))
422 jsonw_string_field(json_wtr, "type",
423 map_type_name[info->type]);
424 else
425 jsonw_uint_field(json_wtr, "type", info->type);
426
427 if (*info->name)
428 jsonw_string_field(json_wtr, "name", info->name);
429
430 jsonw_name(json_wtr, "flags");
431 jsonw_printf(json_wtr, "%#x", info->map_flags);
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800432
433 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
434
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700435 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
436 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
437 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
438
439 if (memlock)
440 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
441 free(memlock);
442
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900443 if (!hash_empty(map_table.table)) {
444 struct pinned_obj *obj;
445
446 jsonw_name(json_wtr, "pinned");
447 jsonw_start_array(json_wtr);
448 hash_for_each_possible(map_table.table, obj, hash, info->id) {
449 if (obj->id == info->id)
450 jsonw_string(json_wtr, obj->path);
451 }
452 jsonw_end_array(json_wtr);
453 }
454
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700455 jsonw_end_object(json_wtr);
456
457 return 0;
458}
459
460static int show_map_close_plain(int fd, struct bpf_map_info *info)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700461{
462 char *memlock;
463
464 memlock = get_fdinfo(fd, "memlock");
465 close(fd);
466
467 printf("%u: ", info->id);
468 if (info->type < ARRAY_SIZE(map_type_name))
469 printf("%s ", map_type_name[info->type]);
470 else
471 printf("type %u ", info->type);
472
473 if (*info->name)
474 printf("name %s ", info->name);
475
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800476 printf("flags 0x%x", info->map_flags);
477 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
478 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700479 printf("\tkey %uB value %uB max_entries %u",
480 info->key_size, info->value_size, info->max_entries);
481
482 if (memlock)
483 printf(" memlock %sB", memlock);
484 free(memlock);
485
486 printf("\n");
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900487 if (!hash_empty(map_table.table)) {
488 struct pinned_obj *obj;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700489
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900490 hash_for_each_possible(map_table.table, obj, hash, info->id) {
491 if (obj->id == info->id)
492 printf("\tpinned %s\n", obj->path);
493 }
494 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700495 return 0;
496}
497
498static int do_show(int argc, char **argv)
499{
500 struct bpf_map_info info = {};
501 __u32 len = sizeof(info);
502 __u32 id = 0;
503 int err;
504 int fd;
505
Prashant Bholec541b732017-11-08 13:55:49 +0900506 if (show_pinned)
507 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900508
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700509 if (argc == 2) {
510 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
511 if (fd < 0)
512 return -1;
513
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700514 if (json_output)
515 return show_map_close_json(fd, &info);
516 else
517 return show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700518 }
519
520 if (argc)
521 return BAD_ARG();
522
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700523 if (json_output)
524 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700525 while (true) {
526 err = bpf_map_get_next_id(id, &id);
527 if (err) {
528 if (errno == ENOENT)
529 break;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700530 p_err("can't get next map: %s%s", strerror(errno),
531 errno == EINVAL ? " -- kernel too old?" : "");
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800532 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700533 }
534
535 fd = bpf_map_get_fd_by_id(id);
536 if (fd < 0) {
Jakub Kicinski8207c6d2017-12-22 11:36:06 -0800537 if (errno == ENOENT)
538 continue;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700539 p_err("can't get map by id (%u): %s",
540 id, strerror(errno));
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800541 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700542 }
543
544 err = bpf_obj_get_info_by_fd(fd, &info, &len);
545 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700546 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700547 close(fd);
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800548 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700549 }
550
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700551 if (json_output)
552 show_map_close_json(fd, &info);
553 else
554 show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700555 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700556 if (json_output)
557 jsonw_end_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700558
559 return errno == ENOENT ? 0 : -1;
560}
561
562static int do_dump(int argc, char **argv)
563{
564 void *key, *value, *prev_key;
565 unsigned int num_elems = 0;
566 struct bpf_map_info info = {};
567 __u32 len = sizeof(info);
568 int err;
569 int fd;
570
571 if (argc != 2)
572 usage();
573
574 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
575 if (fd < 0)
576 return -1;
577
578 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700579 p_err("Dumping maps of maps and program maps not supported");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700580 close(fd);
581 return -1;
582 }
583
584 key = malloc(info.key_size);
585 value = alloc_value(&info);
586 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700587 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700588 err = -1;
589 goto exit_free;
590 }
591
592 prev_key = NULL;
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700593 if (json_output)
594 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700595 while (true) {
596 err = bpf_map_get_next_key(fd, prev_key, key);
597 if (err) {
598 if (errno == ENOENT)
599 err = 0;
600 break;
601 }
602
603 if (!bpf_map_lookup_elem(fd, key, value)) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700604 if (json_output)
605 print_entry_json(&info, key, value);
606 else
607 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700608 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700609 if (json_output) {
610 jsonw_name(json_wtr, "key");
611 print_hex_data_json(key, info.key_size);
612 jsonw_name(json_wtr, "value");
613 jsonw_start_object(json_wtr);
614 jsonw_string_field(json_wtr, "error",
615 "can't lookup element");
616 jsonw_end_object(json_wtr);
617 } else {
618 p_info("can't lookup element with key: ");
619 fprint_hex(stderr, key, info.key_size, " ");
620 fprintf(stderr, "\n");
621 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700622 }
623
624 prev_key = key;
625 num_elems++;
626 }
627
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700628 if (json_output)
629 jsonw_end_array(json_wtr);
630 else
631 printf("Found %u element%s\n", num_elems,
632 num_elems != 1 ? "s" : "");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700633
634exit_free:
635 free(key);
636 free(value);
637 close(fd);
638
639 return err;
640}
641
642static int do_update(int argc, char **argv)
643{
644 struct bpf_map_info info = {};
645 __u32 len = sizeof(info);
646 __u32 *value_fd = NULL;
647 __u32 flags = BPF_ANY;
648 void *key, *value;
649 int fd, err;
650
651 if (argc < 2)
652 usage();
653
654 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
655 if (fd < 0)
656 return -1;
657
658 key = malloc(info.key_size);
659 value = alloc_value(&info);
660 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700661 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700662 err = -1;
663 goto exit_free;
664 }
665
666 err = parse_elem(argv, &info, key, value, info.key_size,
667 info.value_size, &flags, &value_fd);
668 if (err)
669 goto exit_free;
670
671 err = bpf_map_update_elem(fd, key, value, flags);
672 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700673 p_err("update failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700674 goto exit_free;
675 }
676
677exit_free:
678 if (value_fd)
679 close(*value_fd);
680 free(key);
681 free(value);
682 close(fd);
683
Quentin Monnet004b45c2017-10-23 09:24:14 -0700684 if (!err && json_output)
685 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700686 return err;
687}
688
689static int do_lookup(int argc, char **argv)
690{
691 struct bpf_map_info info = {};
692 __u32 len = sizeof(info);
693 void *key, *value;
694 int err;
695 int fd;
696
697 if (argc < 2)
698 usage();
699
700 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
701 if (fd < 0)
702 return -1;
703
704 key = malloc(info.key_size);
705 value = alloc_value(&info);
706 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700707 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700708 err = -1;
709 goto exit_free;
710 }
711
712 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
713 if (err)
714 goto exit_free;
715
716 err = bpf_map_lookup_elem(fd, key, value);
717 if (!err) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700718 if (json_output)
719 print_entry_json(&info, key, value);
720 else
721 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700722 } else if (errno == ENOENT) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700723 if (json_output) {
724 jsonw_null(json_wtr);
725 } else {
726 printf("key:\n");
727 fprint_hex(stdout, key, info.key_size, " ");
728 printf("\n\nNot found\n");
729 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700730 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700731 p_err("lookup failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700732 }
733
734exit_free:
735 free(key);
736 free(value);
737 close(fd);
738
739 return err;
740}
741
742static int do_getnext(int argc, char **argv)
743{
744 struct bpf_map_info info = {};
745 __u32 len = sizeof(info);
746 void *key, *nextkey;
747 int err;
748 int fd;
749
750 if (argc < 2)
751 usage();
752
753 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
754 if (fd < 0)
755 return -1;
756
757 key = malloc(info.key_size);
758 nextkey = malloc(info.key_size);
759 if (!key || !nextkey) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700760 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700761 err = -1;
762 goto exit_free;
763 }
764
765 if (argc) {
766 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
767 NULL, NULL);
768 if (err)
769 goto exit_free;
770 } else {
771 free(key);
772 key = NULL;
773 }
774
775 err = bpf_map_get_next_key(fd, key, nextkey);
776 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700777 p_err("can't get next key: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700778 goto exit_free;
779 }
780
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700781 if (json_output) {
782 jsonw_start_object(json_wtr);
783 if (key) {
784 jsonw_name(json_wtr, "key");
785 print_hex_data_json(key, info.key_size);
786 } else {
787 jsonw_null_field(json_wtr, "key");
788 }
789 jsonw_name(json_wtr, "next_key");
790 print_hex_data_json(nextkey, info.key_size);
791 jsonw_end_object(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700792 } else {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700793 if (key) {
794 printf("key:\n");
795 fprint_hex(stdout, key, info.key_size, " ");
796 printf("\n");
797 } else {
798 printf("key: None\n");
799 }
800 printf("next key:\n");
801 fprint_hex(stdout, nextkey, info.key_size, " ");
802 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700803 }
804
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700805exit_free:
806 free(nextkey);
807 free(key);
808 close(fd);
809
810 return err;
811}
812
813static int do_delete(int argc, char **argv)
814{
815 struct bpf_map_info info = {};
816 __u32 len = sizeof(info);
817 void *key;
818 int err;
819 int fd;
820
821 if (argc < 2)
822 usage();
823
824 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
825 if (fd < 0)
826 return -1;
827
828 key = malloc(info.key_size);
829 if (!key) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700830 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700831 err = -1;
832 goto exit_free;
833 }
834
835 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
836 if (err)
837 goto exit_free;
838
839 err = bpf_map_delete_elem(fd, key);
840 if (err)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700841 p_err("delete failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700842
843exit_free:
844 free(key);
845 close(fd);
846
Quentin Monnet004b45c2017-10-23 09:24:14 -0700847 if (!err && json_output)
848 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700849 return err;
850}
851
852static int do_pin(int argc, char **argv)
853{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700854 int err;
855
856 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
857 if (!err && json_output)
858 jsonw_null(json_wtr);
859 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700860}
861
862static int do_help(int argc, char **argv)
863{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700864 if (json_output) {
865 jsonw_null(json_wtr);
866 return 0;
867 }
868
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700869 fprintf(stderr,
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800870 "Usage: %s %s { show | list } [MAP]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700871 " %s %s dump MAP\n"
872 " %s %s update MAP key BYTES value VALUE [UPDATE_FLAGS]\n"
873 " %s %s lookup MAP key BYTES\n"
874 " %s %s getnext MAP [key BYTES]\n"
875 " %s %s delete MAP key BYTES\n"
876 " %s %s pin MAP FILE\n"
877 " %s %s help\n"
878 "\n"
879 " MAP := { id MAP_ID | pinned FILE }\n"
880 " " HELP_SPEC_PROGRAM "\n"
881 " VALUE := { BYTES | MAP | PROG }\n"
882 " UPDATE_FLAGS := { any | exist | noexist }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -0700883 " " HELP_SPEC_OPTIONS "\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700884 "",
885 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
886 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
887 bin_name, argv[-2], bin_name, argv[-2]);
888
889 return 0;
890}
891
892static const struct cmd cmds[] = {
893 { "show", do_show },
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800894 { "list", do_show },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700895 { "help", do_help },
896 { "dump", do_dump },
897 { "update", do_update },
898 { "lookup", do_lookup },
899 { "getnext", do_getnext },
900 { "delete", do_delete },
901 { "pin", do_pin },
902 { 0 }
903};
904
905int do_map(int argc, char **argv)
906{
907 return cmd_select(cmds, argc, argv, do_help);
908}