blob: e2baec1122fbc3b8e9ee66aed7ed156e90cdcad5 [file] [log] [blame]
Jakub Kicinski71bb4282017-10-04 20:10:04 -07001/*
Jakub Kicinskic642ea22018-05-03 18:37:14 -07002 * Copyright (C) 2017-2018 Netronome Systems, Inc.
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003 *
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
Jakub Kicinski71bb4282017-10-04 20:10:04 -070034#include <assert.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070035#include <errno.h>
36#include <fcntl.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44
45#include <bpf.h>
46
47#include "main.h"
48
49static const char * const map_type_name[] = {
50 [BPF_MAP_TYPE_UNSPEC] = "unspec",
51 [BPF_MAP_TYPE_HASH] = "hash",
52 [BPF_MAP_TYPE_ARRAY] = "array",
53 [BPF_MAP_TYPE_PROG_ARRAY] = "prog_array",
54 [BPF_MAP_TYPE_PERF_EVENT_ARRAY] = "perf_event_array",
55 [BPF_MAP_TYPE_PERCPU_HASH] = "percpu_hash",
56 [BPF_MAP_TYPE_PERCPU_ARRAY] = "percpu_array",
57 [BPF_MAP_TYPE_STACK_TRACE] = "stack_trace",
58 [BPF_MAP_TYPE_CGROUP_ARRAY] = "cgroup_array",
59 [BPF_MAP_TYPE_LRU_HASH] = "lru_hash",
60 [BPF_MAP_TYPE_LRU_PERCPU_HASH] = "lru_percpu_hash",
61 [BPF_MAP_TYPE_LPM_TRIE] = "lpm_trie",
62 [BPF_MAP_TYPE_ARRAY_OF_MAPS] = "array_of_maps",
63 [BPF_MAP_TYPE_HASH_OF_MAPS] = "hash_of_maps",
64 [BPF_MAP_TYPE_DEVMAP] = "devmap",
65 [BPF_MAP_TYPE_SOCKMAP] = "sockmap",
Roman Gushchina55aaf62018-01-19 14:17:45 +000066 [BPF_MAP_TYPE_CPUMAP] = "cpumap",
John Fastabend62c52d12018-05-14 10:00:19 -070067 [BPF_MAP_TYPE_SOCKHASH] = "sockhash",
Jakub Kicinski71bb4282017-10-04 20:10:04 -070068};
69
Jakub Kicinski71bb4282017-10-04 20:10:04 -070070static bool map_is_per_cpu(__u32 type)
71{
72 return type == BPF_MAP_TYPE_PERCPU_HASH ||
73 type == BPF_MAP_TYPE_PERCPU_ARRAY ||
74 type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
75}
76
77static bool map_is_map_of_maps(__u32 type)
78{
79 return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
80 type == BPF_MAP_TYPE_HASH_OF_MAPS;
81}
82
83static bool map_is_map_of_progs(__u32 type)
84{
85 return type == BPF_MAP_TYPE_PROG_ARRAY;
86}
87
88static void *alloc_value(struct bpf_map_info *info)
89{
90 if (map_is_per_cpu(info->type))
91 return malloc(info->value_size * get_possible_cpus());
92 else
93 return malloc(info->value_size);
94}
95
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -070096int map_parse_fd(int *argc, char ***argv)
Jakub Kicinski71bb4282017-10-04 20:10:04 -070097{
98 int fd;
99
100 if (is_prefix(**argv, "id")) {
101 unsigned int id;
102 char *endptr;
103
104 NEXT_ARGP();
105
106 id = strtoul(**argv, &endptr, 0);
107 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700108 p_err("can't parse %s as ID", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700109 return -1;
110 }
111 NEXT_ARGP();
112
113 fd = bpf_map_get_fd_by_id(id);
114 if (fd < 0)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700115 p_err("get map by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700116 return fd;
117 } else if (is_prefix(**argv, "pinned")) {
118 char *path;
119
120 NEXT_ARGP();
121
122 path = **argv;
123 NEXT_ARGP();
124
125 return open_obj_pinned_any(path, BPF_OBJ_MAP);
126 }
127
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700128 p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700129 return -1;
130}
131
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700132int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700133{
134 int err;
135 int fd;
136
137 fd = map_parse_fd(argc, argv);
138 if (fd < 0)
139 return -1;
140
141 err = bpf_obj_get_info_by_fd(fd, info, info_len);
142 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700143 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700144 close(fd);
145 return err;
146 }
147
148 return fd;
149}
150
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700151static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
152 unsigned char *value)
153{
154 jsonw_start_object(json_wtr);
155
156 if (!map_is_per_cpu(info->type)) {
157 jsonw_name(json_wtr, "key");
158 print_hex_data_json(key, info->key_size);
159 jsonw_name(json_wtr, "value");
160 print_hex_data_json(value, info->value_size);
161 } else {
162 unsigned int i, n;
163
164 n = get_possible_cpus();
165
166 jsonw_name(json_wtr, "key");
167 print_hex_data_json(key, info->key_size);
168
169 jsonw_name(json_wtr, "values");
170 jsonw_start_array(json_wtr);
171 for (i = 0; i < n; i++) {
172 jsonw_start_object(json_wtr);
173
174 jsonw_int_field(json_wtr, "cpu", i);
175
176 jsonw_name(json_wtr, "value");
177 print_hex_data_json(value + i * info->value_size,
178 info->value_size);
179
180 jsonw_end_object(json_wtr);
181 }
182 jsonw_end_array(json_wtr);
183 }
184
185 jsonw_end_object(json_wtr);
186}
187
188static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
189 unsigned char *value)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700190{
191 if (!map_is_per_cpu(info->type)) {
192 bool single_line, break_names;
193
194 break_names = info->key_size > 16 || info->value_size > 16;
195 single_line = info->key_size + info->value_size <= 24 &&
196 !break_names;
197
198 printf("key:%c", break_names ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700199 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700200
201 printf(single_line ? " " : "\n");
202
203 printf("value:%c", break_names ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700204 fprint_hex(stdout, value, info->value_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700205
206 printf("\n");
207 } else {
208 unsigned int i, n;
209
210 n = get_possible_cpus();
211
212 printf("key:\n");
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700213 fprint_hex(stdout, key, info->key_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700214 printf("\n");
215 for (i = 0; i < n; i++) {
216 printf("value (CPU %02d):%c",
217 i, info->value_size > 16 ? '\n' : ' ');
Quentin Monnet9cbe1f582017-10-19 15:46:19 -0700218 fprint_hex(stdout, value + i * info->value_size,
219 info->value_size, " ");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700220 printf("\n");
221 }
222 }
223}
224
225static char **parse_bytes(char **argv, const char *name, unsigned char *val,
226 unsigned int n)
227{
Quentin Monnet0c90f222018-04-17 19:46:34 -0700228 unsigned int i = 0, base = 0;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700229 char *endptr;
230
Quentin Monnet0c90f222018-04-17 19:46:34 -0700231 if (is_prefix(*argv, "hex")) {
232 base = 16;
233 argv++;
234 }
235
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700236 while (i < n && argv[i]) {
Quentin Monnet0c90f222018-04-17 19:46:34 -0700237 val[i] = strtoul(argv[i], &endptr, base);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700238 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700239 p_err("error parsing byte: %s", argv[i]);
Quentin Monnetd9c0b482017-10-19 15:46:23 -0700240 return NULL;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700241 }
242 i++;
243 }
244
245 if (i != n) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700246 p_err("%s expected %d bytes got %d", name, n, i);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700247 return NULL;
248 }
249
250 return argv + i;
251}
252
253static int parse_elem(char **argv, struct bpf_map_info *info,
254 void *key, void *value, __u32 key_size, __u32 value_size,
255 __u32 *flags, __u32 **value_fd)
256{
257 if (!*argv) {
258 if (!key && !value)
259 return 0;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700260 p_err("did not find %s", key ? "key" : "value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700261 return -1;
262 }
263
264 if (is_prefix(*argv, "key")) {
265 if (!key) {
266 if (key_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700267 p_err("duplicate key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700268 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700269 p_err("unnecessary key");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700270 return -1;
271 }
272
273 argv = parse_bytes(argv + 1, "key", key, key_size);
274 if (!argv)
275 return -1;
276
277 return parse_elem(argv, info, NULL, value, key_size, value_size,
278 flags, value_fd);
279 } else if (is_prefix(*argv, "value")) {
280 int fd;
281
282 if (!value) {
283 if (value_size)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700284 p_err("duplicate value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700285 else
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700286 p_err("unnecessary value");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700287 return -1;
288 }
289
290 argv++;
291
292 if (map_is_map_of_maps(info->type)) {
293 int argc = 2;
294
295 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700296 p_err("value smaller than 4B for map in map?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700297 return -1;
298 }
299 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700300 p_err("not enough value arguments for map in map");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700301 return -1;
302 }
303
304 fd = map_parse_fd(&argc, &argv);
305 if (fd < 0)
306 return -1;
307
308 *value_fd = value;
309 **value_fd = fd;
310 } else if (map_is_map_of_progs(info->type)) {
311 int argc = 2;
312
313 if (value_size != 4) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700314 p_err("value smaller than 4B for map of progs?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700315 return -1;
316 }
317 if (!argv[0] || !argv[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700318 p_err("not enough value arguments for map of progs");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700319 return -1;
320 }
321
322 fd = prog_parse_fd(&argc, &argv);
323 if (fd < 0)
324 return -1;
325
326 *value_fd = value;
327 **value_fd = fd;
328 } else {
329 argv = parse_bytes(argv, "value", value, value_size);
330 if (!argv)
331 return -1;
332 }
333
334 return parse_elem(argv, info, key, NULL, key_size, value_size,
335 flags, NULL);
336 } else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
337 is_prefix(*argv, "exist")) {
338 if (!flags) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700339 p_err("flags specified multiple times: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700340 return -1;
341 }
342
343 if (is_prefix(*argv, "any"))
344 *flags = BPF_ANY;
345 else if (is_prefix(*argv, "noexist"))
346 *flags = BPF_NOEXIST;
347 else if (is_prefix(*argv, "exist"))
348 *flags = BPF_EXIST;
349
350 return parse_elem(argv + 1, info, key, value, key_size,
351 value_size, NULL, value_fd);
352 }
353
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700354 p_err("expected key or value, got: %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700355 return -1;
356}
357
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700358static int show_map_close_json(int fd, struct bpf_map_info *info)
359{
360 char *memlock;
361
362 memlock = get_fdinfo(fd, "memlock");
363 close(fd);
364
365 jsonw_start_object(json_wtr);
366
367 jsonw_uint_field(json_wtr, "id", info->id);
368 if (info->type < ARRAY_SIZE(map_type_name))
369 jsonw_string_field(json_wtr, "type",
370 map_type_name[info->type]);
371 else
372 jsonw_uint_field(json_wtr, "type", info->type);
373
374 if (*info->name)
375 jsonw_string_field(json_wtr, "name", info->name);
376
377 jsonw_name(json_wtr, "flags");
Jakub Kicinski4b6eca92018-03-23 19:45:12 -0700378 jsonw_printf(json_wtr, "%d", info->map_flags);
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800379
380 print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
381
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700382 jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
383 jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
384 jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
385
386 if (memlock)
387 jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
388 free(memlock);
389
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900390 if (!hash_empty(map_table.table)) {
391 struct pinned_obj *obj;
392
393 jsonw_name(json_wtr, "pinned");
394 jsonw_start_array(json_wtr);
395 hash_for_each_possible(map_table.table, obj, hash, info->id) {
396 if (obj->id == info->id)
397 jsonw_string(json_wtr, obj->path);
398 }
399 jsonw_end_array(json_wtr);
400 }
401
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700402 jsonw_end_object(json_wtr);
403
404 return 0;
405}
406
407static int show_map_close_plain(int fd, struct bpf_map_info *info)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700408{
409 char *memlock;
410
411 memlock = get_fdinfo(fd, "memlock");
412 close(fd);
413
414 printf("%u: ", info->id);
415 if (info->type < ARRAY_SIZE(map_type_name))
416 printf("%s ", map_type_name[info->type]);
417 else
418 printf("type %u ", info->type);
419
420 if (*info->name)
421 printf("name %s ", info->name);
422
Jakub Kicinski064a07c2018-01-17 19:13:29 -0800423 printf("flags 0x%x", info->map_flags);
424 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
425 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700426 printf("\tkey %uB value %uB max_entries %u",
427 info->key_size, info->value_size, info->max_entries);
428
429 if (memlock)
430 printf(" memlock %sB", memlock);
431 free(memlock);
432
433 printf("\n");
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900434 if (!hash_empty(map_table.table)) {
435 struct pinned_obj *obj;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700436
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900437 hash_for_each_possible(map_table.table, obj, hash, info->id) {
438 if (obj->id == info->id)
439 printf("\tpinned %s\n", obj->path);
440 }
441 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700442 return 0;
443}
444
445static int do_show(int argc, char **argv)
446{
447 struct bpf_map_info info = {};
448 __u32 len = sizeof(info);
449 __u32 id = 0;
450 int err;
451 int fd;
452
Prashant Bholec541b732017-11-08 13:55:49 +0900453 if (show_pinned)
454 build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900455
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700456 if (argc == 2) {
457 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
458 if (fd < 0)
459 return -1;
460
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700461 if (json_output)
462 return show_map_close_json(fd, &info);
463 else
464 return show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700465 }
466
467 if (argc)
468 return BAD_ARG();
469
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700470 if (json_output)
471 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700472 while (true) {
473 err = bpf_map_get_next_id(id, &id);
474 if (err) {
475 if (errno == ENOENT)
476 break;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700477 p_err("can't get next map: %s%s", strerror(errno),
478 errno == EINVAL ? " -- kernel too old?" : "");
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800479 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700480 }
481
482 fd = bpf_map_get_fd_by_id(id);
483 if (fd < 0) {
Jakub Kicinski8207c6d2017-12-22 11:36:06 -0800484 if (errno == ENOENT)
485 continue;
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700486 p_err("can't get map by id (%u): %s",
487 id, strerror(errno));
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800488 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700489 }
490
491 err = bpf_obj_get_info_by_fd(fd, &info, &len);
492 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700493 p_err("can't get map info: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700494 close(fd);
Jakub Kicinskib3b1b652017-12-22 11:36:05 -0800495 break;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700496 }
497
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700498 if (json_output)
499 show_map_close_json(fd, &info);
500 else
501 show_map_close_plain(fd, &info);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700502 }
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700503 if (json_output)
504 jsonw_end_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700505
506 return errno == ENOENT ? 0 : -1;
507}
508
509static int do_dump(int argc, char **argv)
510{
511 void *key, *value, *prev_key;
512 unsigned int num_elems = 0;
513 struct bpf_map_info info = {};
514 __u32 len = sizeof(info);
515 int err;
516 int fd;
517
518 if (argc != 2)
519 usage();
520
521 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
522 if (fd < 0)
523 return -1;
524
525 if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700526 p_err("Dumping maps of maps and program maps not supported");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700527 close(fd);
528 return -1;
529 }
530
531 key = malloc(info.key_size);
532 value = alloc_value(&info);
533 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700534 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700535 err = -1;
536 goto exit_free;
537 }
538
539 prev_key = NULL;
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700540 if (json_output)
541 jsonw_start_array(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700542 while (true) {
543 err = bpf_map_get_next_key(fd, prev_key, key);
544 if (err) {
545 if (errno == ENOENT)
546 err = 0;
547 break;
548 }
549
550 if (!bpf_map_lookup_elem(fd, key, value)) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700551 if (json_output)
552 print_entry_json(&info, key, value);
553 else
554 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700555 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700556 if (json_output) {
557 jsonw_name(json_wtr, "key");
558 print_hex_data_json(key, info.key_size);
559 jsonw_name(json_wtr, "value");
560 jsonw_start_object(json_wtr);
561 jsonw_string_field(json_wtr, "error",
562 "can't lookup element");
563 jsonw_end_object(json_wtr);
564 } else {
565 p_info("can't lookup element with key: ");
566 fprint_hex(stderr, key, info.key_size, " ");
567 fprintf(stderr, "\n");
568 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700569 }
570
571 prev_key = key;
572 num_elems++;
573 }
574
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700575 if (json_output)
576 jsonw_end_array(json_wtr);
577 else
578 printf("Found %u element%s\n", num_elems,
579 num_elems != 1 ? "s" : "");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700580
581exit_free:
582 free(key);
583 free(value);
584 close(fd);
585
586 return err;
587}
588
589static int do_update(int argc, char **argv)
590{
591 struct bpf_map_info info = {};
592 __u32 len = sizeof(info);
593 __u32 *value_fd = NULL;
594 __u32 flags = BPF_ANY;
595 void *key, *value;
596 int fd, err;
597
598 if (argc < 2)
599 usage();
600
601 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
602 if (fd < 0)
603 return -1;
604
605 key = malloc(info.key_size);
606 value = alloc_value(&info);
607 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700608 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700609 err = -1;
610 goto exit_free;
611 }
612
613 err = parse_elem(argv, &info, key, value, info.key_size,
614 info.value_size, &flags, &value_fd);
615 if (err)
616 goto exit_free;
617
618 err = bpf_map_update_elem(fd, key, value, flags);
619 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700620 p_err("update failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700621 goto exit_free;
622 }
623
624exit_free:
625 if (value_fd)
626 close(*value_fd);
627 free(key);
628 free(value);
629 close(fd);
630
Quentin Monnet004b45c2017-10-23 09:24:14 -0700631 if (!err && json_output)
632 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700633 return err;
634}
635
636static int do_lookup(int argc, char **argv)
637{
638 struct bpf_map_info info = {};
639 __u32 len = sizeof(info);
640 void *key, *value;
641 int err;
642 int fd;
643
644 if (argc < 2)
645 usage();
646
647 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
648 if (fd < 0)
649 return -1;
650
651 key = malloc(info.key_size);
652 value = alloc_value(&info);
653 if (!key || !value) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700654 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700655 err = -1;
656 goto exit_free;
657 }
658
659 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
660 if (err)
661 goto exit_free;
662
663 err = bpf_map_lookup_elem(fd, key, value);
664 if (!err) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700665 if (json_output)
666 print_entry_json(&info, key, value);
667 else
668 print_entry_plain(&info, key, value);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700669 } else if (errno == ENOENT) {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700670 if (json_output) {
671 jsonw_null(json_wtr);
672 } else {
673 printf("key:\n");
674 fprint_hex(stdout, key, info.key_size, " ");
675 printf("\n\nNot found\n");
676 }
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700677 } else {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700678 p_err("lookup failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700679 }
680
681exit_free:
682 free(key);
683 free(value);
684 close(fd);
685
686 return err;
687}
688
689static int do_getnext(int argc, char **argv)
690{
691 struct bpf_map_info info = {};
692 __u32 len = sizeof(info);
693 void *key, *nextkey;
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 nextkey = malloc(info.key_size);
706 if (!key || !nextkey) {
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 if (argc) {
713 err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
714 NULL, NULL);
715 if (err)
716 goto exit_free;
717 } else {
718 free(key);
719 key = NULL;
720 }
721
722 err = bpf_map_get_next_key(fd, key, nextkey);
723 if (err) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700724 p_err("can't get next key: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700725 goto exit_free;
726 }
727
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700728 if (json_output) {
729 jsonw_start_object(json_wtr);
730 if (key) {
731 jsonw_name(json_wtr, "key");
732 print_hex_data_json(key, info.key_size);
733 } else {
734 jsonw_null_field(json_wtr, "key");
735 }
736 jsonw_name(json_wtr, "next_key");
737 print_hex_data_json(nextkey, info.key_size);
738 jsonw_end_object(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700739 } else {
Quentin Monnet831a0aa2017-10-23 09:24:11 -0700740 if (key) {
741 printf("key:\n");
742 fprint_hex(stdout, key, info.key_size, " ");
743 printf("\n");
744 } else {
745 printf("key: None\n");
746 }
747 printf("next key:\n");
748 fprint_hex(stdout, nextkey, info.key_size, " ");
749 printf("\n");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700750 }
751
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700752exit_free:
753 free(nextkey);
754 free(key);
755 close(fd);
756
757 return err;
758}
759
760static int do_delete(int argc, char **argv)
761{
762 struct bpf_map_info info = {};
763 __u32 len = sizeof(info);
764 void *key;
765 int err;
766 int fd;
767
768 if (argc < 2)
769 usage();
770
771 fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
772 if (fd < 0)
773 return -1;
774
775 key = malloc(info.key_size);
776 if (!key) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700777 p_err("mem alloc failed");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700778 err = -1;
779 goto exit_free;
780 }
781
782 err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
783 if (err)
784 goto exit_free;
785
786 err = bpf_map_delete_elem(fd, key);
787 if (err)
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700788 p_err("delete failed: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700789
790exit_free:
791 free(key);
792 close(fd);
793
Quentin Monnet004b45c2017-10-23 09:24:14 -0700794 if (!err && json_output)
795 jsonw_null(json_wtr);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700796 return err;
797}
798
799static int do_pin(int argc, char **argv)
800{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700801 int err;
802
803 err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
804 if (!err && json_output)
805 jsonw_null(json_wtr);
806 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700807}
808
809static int do_help(int argc, char **argv)
810{
Quentin Monnet004b45c2017-10-23 09:24:14 -0700811 if (json_output) {
812 jsonw_null(json_wtr);
813 return 0;
814 }
815
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700816 fprintf(stderr,
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800817 "Usage: %s %s { show | list } [MAP]\n"
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700818 " %s %s dump MAP\n"
819 " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n"
820 " %s %s lookup MAP key DATA\n"
821 " %s %s getnext MAP [key DATA]\n"
822 " %s %s delete MAP key DATA\n"
823 " %s %s pin MAP FILE\n"
824 " %s %s event_pipe MAP [cpu N index M]\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700825 " %s %s help\n"
826 "\n"
Jakub Kicinski3ff5a4d2018-07-10 14:43:07 -0700827 " " HELP_SPEC_MAP "\n"
Jakub Kicinskic642ea22018-05-03 18:37:14 -0700828 " DATA := { [hex] BYTES }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700829 " " HELP_SPEC_PROGRAM "\n"
Jakub Kicinskic642ea22018-05-03 18:37:14 -0700830 " VALUE := { DATA | MAP | PROG }\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700831 " UPDATE_FLAGS := { any | exist | noexist }\n"
Quentin Monnet0641c3c2017-10-23 09:24:16 -0700832 " " HELP_SPEC_OPTIONS "\n"
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700833 "",
834 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
835 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700836 bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700837
838 return 0;
839}
840
841static const struct cmd cmds[] = {
842 { "show", do_show },
Jakub Kicinski6ebe6db2018-01-02 14:48:36 -0800843 { "list", do_show },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700844 { "help", do_help },
845 { "dump", do_dump },
846 { "update", do_update },
847 { "lookup", do_lookup },
848 { "getnext", do_getnext },
849 { "delete", do_delete },
850 { "pin", do_pin },
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700851 { "event_pipe", do_event_pipe },
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700852 { 0 }
853};
854
855int do_map(int argc, char **argv)
856{
857 return cmd_select(cmds, argc, argv, do_help);
858}