map.c 19.2 KB
Newer Older
J
Jakub Kicinski 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/*
 * Copyright (C) 2017 Netronome Systems, Inc.
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Author: Jakub Kicinski <kubakici@wp.pl> */

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <bpf.h>

#include "main.h"

static const char * const map_type_name[] = {
	[BPF_MAP_TYPE_UNSPEC]		= "unspec",
	[BPF_MAP_TYPE_HASH]		= "hash",
	[BPF_MAP_TYPE_ARRAY]		= "array",
	[BPF_MAP_TYPE_PROG_ARRAY]	= "prog_array",
	[BPF_MAP_TYPE_PERF_EVENT_ARRAY]	= "perf_event_array",
	[BPF_MAP_TYPE_PERCPU_HASH]	= "percpu_hash",
	[BPF_MAP_TYPE_PERCPU_ARRAY]	= "percpu_array",
	[BPF_MAP_TYPE_STACK_TRACE]	= "stack_trace",
	[BPF_MAP_TYPE_CGROUP_ARRAY]	= "cgroup_array",
	[BPF_MAP_TYPE_LRU_HASH]		= "lru_hash",
	[BPF_MAP_TYPE_LRU_PERCPU_HASH]	= "lru_percpu_hash",
	[BPF_MAP_TYPE_LPM_TRIE]		= "lpm_trie",
	[BPF_MAP_TYPE_ARRAY_OF_MAPS]	= "array_of_maps",
	[BPF_MAP_TYPE_HASH_OF_MAPS]	= "hash_of_maps",
	[BPF_MAP_TYPE_DEVMAP]		= "devmap",
	[BPF_MAP_TYPE_SOCKMAP]		= "sockmap",
69
	[BPF_MAP_TYPE_CPUMAP]		= "cpumap",
J
Jakub Kicinski 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
};

static unsigned int get_possible_cpus(void)
{
	static unsigned int result;
	char buf[128];
	long int n;
	char *ptr;
	int fd;

	if (result)
		return result;

	fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
	if (fd < 0) {
85
		p_err("can't open sysfs possible cpus");
J
Jakub Kicinski 已提交
86 87 88 89 90
		exit(-1);
	}

	n = read(fd, buf, sizeof(buf));
	if (n < 2) {
91
		p_err("can't read sysfs possible cpus");
J
Jakub Kicinski 已提交
92 93 94 95 96
		exit(-1);
	}
	close(fd);

	if (n == sizeof(buf)) {
97
		p_err("read sysfs possible cpus overflow");
J
Jakub Kicinski 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		exit(-1);
	}

	ptr = buf;
	n = 0;
	while (*ptr && *ptr != '\n') {
		unsigned int a, b;

		if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
			n += b - a + 1;

			ptr = strchr(ptr, '-') + 1;
		} else if (sscanf(ptr, "%u", &a) == 1) {
			n++;
		} else {
			assert(0);
		}

		while (isdigit(*ptr))
			ptr++;
		if (*ptr == ',')
			ptr++;
	}

	result = n;

	return result;
}

static bool map_is_per_cpu(__u32 type)
{
	return type == BPF_MAP_TYPE_PERCPU_HASH ||
	       type == BPF_MAP_TYPE_PERCPU_ARRAY ||
	       type == BPF_MAP_TYPE_LRU_PERCPU_HASH;
}

static bool map_is_map_of_maps(__u32 type)
{
	return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
	       type == BPF_MAP_TYPE_HASH_OF_MAPS;
}

static bool map_is_map_of_progs(__u32 type)
{
	return type == BPF_MAP_TYPE_PROG_ARRAY;
}

static void *alloc_value(struct bpf_map_info *info)
{
	if (map_is_per_cpu(info->type))
		return malloc(info->value_size * get_possible_cpus());
	else
		return malloc(info->value_size);
}

static int map_parse_fd(int *argc, char ***argv)
{
	int fd;

	if (is_prefix(**argv, "id")) {
		unsigned int id;
		char *endptr;

		NEXT_ARGP();

		id = strtoul(**argv, &endptr, 0);
		if (*endptr) {
165
			p_err("can't parse %s as ID", **argv);
J
Jakub Kicinski 已提交
166 167 168 169 170 171
			return -1;
		}
		NEXT_ARGP();

		fd = bpf_map_get_fd_by_id(id);
		if (fd < 0)
172
			p_err("get map by id (%u): %s", id, strerror(errno));
J
Jakub Kicinski 已提交
173 174 175 176 177 178 179 180 181 182 183 184
		return fd;
	} else if (is_prefix(**argv, "pinned")) {
		char *path;

		NEXT_ARGP();

		path = **argv;
		NEXT_ARGP();

		return open_obj_pinned_any(path, BPF_OBJ_MAP);
	}

185
	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
J
Jakub Kicinski 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	return -1;
}

static int
map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
{
	int err;
	int fd;

	fd = map_parse_fd(argc, argv);
	if (fd < 0)
		return -1;

	err = bpf_obj_get_info_by_fd(fd, info, info_len);
	if (err) {
201
		p_err("can't get map info: %s", strerror(errno));
J
Jakub Kicinski 已提交
202 203 204 205 206 207 208
		close(fd);
		return err;
	}

	return fd;
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
			     unsigned char *value)
{
	jsonw_start_object(json_wtr);

	if (!map_is_per_cpu(info->type)) {
		jsonw_name(json_wtr, "key");
		print_hex_data_json(key, info->key_size);
		jsonw_name(json_wtr, "value");
		print_hex_data_json(value, info->value_size);
	} else {
		unsigned int i, n;

		n = get_possible_cpus();

		jsonw_name(json_wtr, "key");
		print_hex_data_json(key, info->key_size);

		jsonw_name(json_wtr, "values");
		jsonw_start_array(json_wtr);
		for (i = 0; i < n; i++) {
			jsonw_start_object(json_wtr);

			jsonw_int_field(json_wtr, "cpu", i);

			jsonw_name(json_wtr, "value");
			print_hex_data_json(value + i * info->value_size,
					    info->value_size);

			jsonw_end_object(json_wtr);
		}
		jsonw_end_array(json_wtr);
	}

	jsonw_end_object(json_wtr);
}

static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
			      unsigned char *value)
J
Jakub Kicinski 已提交
248 249 250 251 252 253 254 255 256
{
	if (!map_is_per_cpu(info->type)) {
		bool single_line, break_names;

		break_names = info->key_size > 16 || info->value_size > 16;
		single_line = info->key_size + info->value_size <= 24 &&
			!break_names;

		printf("key:%c", break_names ? '\n' : ' ');
257
		fprint_hex(stdout, key, info->key_size, " ");
J
Jakub Kicinski 已提交
258 259 260 261

		printf(single_line ? "  " : "\n");

		printf("value:%c", break_names ? '\n' : ' ');
262
		fprint_hex(stdout, value, info->value_size, " ");
J
Jakub Kicinski 已提交
263 264 265 266 267 268 269 270

		printf("\n");
	} else {
		unsigned int i, n;

		n = get_possible_cpus();

		printf("key:\n");
271
		fprint_hex(stdout, key, info->key_size, " ");
J
Jakub Kicinski 已提交
272 273 274 275
		printf("\n");
		for (i = 0; i < n; i++) {
			printf("value (CPU %02d):%c",
			       i, info->value_size > 16 ? '\n' : ' ');
276 277
			fprint_hex(stdout, value + i * info->value_size,
				   info->value_size, " ");
J
Jakub Kicinski 已提交
278 279 280 281 282 283 284 285
			printf("\n");
		}
	}
}

static char **parse_bytes(char **argv, const char *name, unsigned char *val,
			  unsigned int n)
{
286
	unsigned int i = 0, base = 0;
J
Jakub Kicinski 已提交
287 288
	char *endptr;

289 290 291 292 293
	if (is_prefix(*argv, "hex")) {
		base = 16;
		argv++;
	}

J
Jakub Kicinski 已提交
294
	while (i < n && argv[i]) {
295
		val[i] = strtoul(argv[i], &endptr, base);
J
Jakub Kicinski 已提交
296
		if (*endptr) {
297
			p_err("error parsing byte: %s", argv[i]);
298
			return NULL;
J
Jakub Kicinski 已提交
299 300 301 302 303
		}
		i++;
	}

	if (i != n) {
304
		p_err("%s expected %d bytes got %d", name, n, i);
J
Jakub Kicinski 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317
		return NULL;
	}

	return argv + i;
}

static int parse_elem(char **argv, struct bpf_map_info *info,
		      void *key, void *value, __u32 key_size, __u32 value_size,
		      __u32 *flags, __u32 **value_fd)
{
	if (!*argv) {
		if (!key && !value)
			return 0;
318
		p_err("did not find %s", key ? "key" : "value");
J
Jakub Kicinski 已提交
319 320 321 322 323 324
		return -1;
	}

	if (is_prefix(*argv, "key")) {
		if (!key) {
			if (key_size)
325
				p_err("duplicate key");
J
Jakub Kicinski 已提交
326
			else
327
				p_err("unnecessary key");
J
Jakub Kicinski 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341
			return -1;
		}

		argv = parse_bytes(argv + 1, "key", key, key_size);
		if (!argv)
			return -1;

		return parse_elem(argv, info, NULL, value, key_size, value_size,
				  flags, value_fd);
	} else if (is_prefix(*argv, "value")) {
		int fd;

		if (!value) {
			if (value_size)
342
				p_err("duplicate value");
J
Jakub Kicinski 已提交
343
			else
344
				p_err("unnecessary value");
J
Jakub Kicinski 已提交
345 346 347 348 349 350 351 352 353
			return -1;
		}

		argv++;

		if (map_is_map_of_maps(info->type)) {
			int argc = 2;

			if (value_size != 4) {
354
				p_err("value smaller than 4B for map in map?");
J
Jakub Kicinski 已提交
355 356 357
				return -1;
			}
			if (!argv[0] || !argv[1]) {
358
				p_err("not enough value arguments for map in map");
J
Jakub Kicinski 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371
				return -1;
			}

			fd = map_parse_fd(&argc, &argv);
			if (fd < 0)
				return -1;

			*value_fd = value;
			**value_fd = fd;
		} else if (map_is_map_of_progs(info->type)) {
			int argc = 2;

			if (value_size != 4) {
372
				p_err("value smaller than 4B for map of progs?");
J
Jakub Kicinski 已提交
373 374 375
				return -1;
			}
			if (!argv[0] || !argv[1]) {
376
				p_err("not enough value arguments for map of progs");
J
Jakub Kicinski 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
				return -1;
			}

			fd = prog_parse_fd(&argc, &argv);
			if (fd < 0)
				return -1;

			*value_fd = value;
			**value_fd = fd;
		} else {
			argv = parse_bytes(argv, "value", value, value_size);
			if (!argv)
				return -1;
		}

		return parse_elem(argv, info, key, NULL, key_size, value_size,
				  flags, NULL);
	} else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
		   is_prefix(*argv, "exist")) {
		if (!flags) {
397
			p_err("flags specified multiple times: %s", *argv);
J
Jakub Kicinski 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411
			return -1;
		}

		if (is_prefix(*argv, "any"))
			*flags = BPF_ANY;
		else if (is_prefix(*argv, "noexist"))
			*flags = BPF_NOEXIST;
		else if (is_prefix(*argv, "exist"))
			*flags = BPF_EXIST;

		return parse_elem(argv + 1, info, key, value, key_size,
				  value_size, NULL, value_fd);
	}

412
	p_err("expected key or value, got: %s", *argv);
J
Jakub Kicinski 已提交
413 414 415
	return -1;
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
static int show_map_close_json(int fd, struct bpf_map_info *info)
{
	char *memlock;

	memlock = get_fdinfo(fd, "memlock");
	close(fd);

	jsonw_start_object(json_wtr);

	jsonw_uint_field(json_wtr, "id", info->id);
	if (info->type < ARRAY_SIZE(map_type_name))
		jsonw_string_field(json_wtr, "type",
				   map_type_name[info->type]);
	else
		jsonw_uint_field(json_wtr, "type", info->type);

	if (*info->name)
		jsonw_string_field(json_wtr, "name", info->name);

	jsonw_name(json_wtr, "flags");
436
	jsonw_printf(json_wtr, "%d", info->map_flags);
437 438 439

	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);

440 441 442 443 444 445 446 447
	jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
	jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
	jsonw_uint_field(json_wtr, "max_entries", info->max_entries);

	if (memlock)
		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
	free(memlock);

448 449 450 451 452 453 454 455 456 457 458 459
	if (!hash_empty(map_table.table)) {
		struct pinned_obj *obj;

		jsonw_name(json_wtr, "pinned");
		jsonw_start_array(json_wtr);
		hash_for_each_possible(map_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
				jsonw_string(json_wtr, obj->path);
		}
		jsonw_end_array(json_wtr);
	}

460 461 462 463 464 465
	jsonw_end_object(json_wtr);

	return 0;
}

static int show_map_close_plain(int fd, struct bpf_map_info *info)
J
Jakub Kicinski 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
{
	char *memlock;

	memlock = get_fdinfo(fd, "memlock");
	close(fd);

	printf("%u: ", info->id);
	if (info->type < ARRAY_SIZE(map_type_name))
		printf("%s  ", map_type_name[info->type]);
	else
		printf("type %u  ", info->type);

	if (*info->name)
		printf("name %s  ", info->name);

481 482 483
	printf("flags 0x%x", info->map_flags);
	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
	printf("\n");
J
Jakub Kicinski 已提交
484 485 486 487 488 489 490 491
	printf("\tkey %uB  value %uB  max_entries %u",
	       info->key_size, info->value_size, info->max_entries);

	if (memlock)
		printf("  memlock %sB", memlock);
	free(memlock);

	printf("\n");
492 493
	if (!hash_empty(map_table.table)) {
		struct pinned_obj *obj;
J
Jakub Kicinski 已提交
494

495 496 497 498 499
		hash_for_each_possible(map_table.table, obj, hash, info->id) {
			if (obj->id == info->id)
				printf("\tpinned %s\n", obj->path);
		}
	}
J
Jakub Kicinski 已提交
500 501 502 503 504 505 506 507 508 509 510
	return 0;
}

static int do_show(int argc, char **argv)
{
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	__u32 id = 0;
	int err;
	int fd;

511 512
	if (show_pinned)
		build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
513

J
Jakub Kicinski 已提交
514 515 516 517 518
	if (argc == 2) {
		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
		if (fd < 0)
			return -1;

519 520 521 522
		if (json_output)
			return show_map_close_json(fd, &info);
		else
			return show_map_close_plain(fd, &info);
J
Jakub Kicinski 已提交
523 524 525 526 527
	}

	if (argc)
		return BAD_ARG();

528 529
	if (json_output)
		jsonw_start_array(json_wtr);
J
Jakub Kicinski 已提交
530 531 532 533 534
	while (true) {
		err = bpf_map_get_next_id(id, &id);
		if (err) {
			if (errno == ENOENT)
				break;
535 536
			p_err("can't get next map: %s%s", strerror(errno),
			      errno == EINVAL ? " -- kernel too old?" : "");
537
			break;
J
Jakub Kicinski 已提交
538 539 540 541
		}

		fd = bpf_map_get_fd_by_id(id);
		if (fd < 0) {
542 543
			if (errno == ENOENT)
				continue;
544 545
			p_err("can't get map by id (%u): %s",
			      id, strerror(errno));
546
			break;
J
Jakub Kicinski 已提交
547 548 549 550
		}

		err = bpf_obj_get_info_by_fd(fd, &info, &len);
		if (err) {
551
			p_err("can't get map info: %s", strerror(errno));
J
Jakub Kicinski 已提交
552
			close(fd);
553
			break;
J
Jakub Kicinski 已提交
554 555
		}

556 557 558 559
		if (json_output)
			show_map_close_json(fd, &info);
		else
			show_map_close_plain(fd, &info);
J
Jakub Kicinski 已提交
560
	}
561 562
	if (json_output)
		jsonw_end_array(json_wtr);
J
Jakub Kicinski 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

	return errno == ENOENT ? 0 : -1;
}

static int do_dump(int argc, char **argv)
{
	void *key, *value, *prev_key;
	unsigned int num_elems = 0;
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	int err;
	int fd;

	if (argc != 2)
		usage();

	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
	if (fd < 0)
		return -1;

	if (map_is_map_of_maps(info.type) || map_is_map_of_progs(info.type)) {
584
		p_err("Dumping maps of maps and program maps not supported");
J
Jakub Kicinski 已提交
585 586 587 588 589 590 591
		close(fd);
		return -1;
	}

	key = malloc(info.key_size);
	value = alloc_value(&info);
	if (!key || !value) {
592
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
593 594 595 596 597
		err = -1;
		goto exit_free;
	}

	prev_key = NULL;
598 599
	if (json_output)
		jsonw_start_array(json_wtr);
J
Jakub Kicinski 已提交
600 601 602 603 604 605 606 607 608
	while (true) {
		err = bpf_map_get_next_key(fd, prev_key, key);
		if (err) {
			if (errno == ENOENT)
				err = 0;
			break;
		}

		if (!bpf_map_lookup_elem(fd, key, value)) {
609 610 611 612
			if (json_output)
				print_entry_json(&info, key, value);
			else
				print_entry_plain(&info, key, value);
J
Jakub Kicinski 已提交
613
		} else {
614 615 616 617 618 619 620 621 622 623 624 625 626
			if (json_output) {
				jsonw_name(json_wtr, "key");
				print_hex_data_json(key, info.key_size);
				jsonw_name(json_wtr, "value");
				jsonw_start_object(json_wtr);
				jsonw_string_field(json_wtr, "error",
						   "can't lookup element");
				jsonw_end_object(json_wtr);
			} else {
				p_info("can't lookup element with key: ");
				fprint_hex(stderr, key, info.key_size, " ");
				fprintf(stderr, "\n");
			}
J
Jakub Kicinski 已提交
627 628 629 630 631 632
		}

		prev_key = key;
		num_elems++;
	}

633 634 635 636 637
	if (json_output)
		jsonw_end_array(json_wtr);
	else
		printf("Found %u element%s\n", num_elems,
		       num_elems != 1 ? "s" : "");
J
Jakub Kicinski 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

exit_free:
	free(key);
	free(value);
	close(fd);

	return err;
}

static int do_update(int argc, char **argv)
{
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	__u32 *value_fd = NULL;
	__u32 flags = BPF_ANY;
	void *key, *value;
	int fd, err;

	if (argc < 2)
		usage();

	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
	if (fd < 0)
		return -1;

	key = malloc(info.key_size);
	value = alloc_value(&info);
	if (!key || !value) {
666
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
667 668 669 670 671 672 673 674 675 676 677
		err = -1;
		goto exit_free;
	}

	err = parse_elem(argv, &info, key, value, info.key_size,
			 info.value_size, &flags, &value_fd);
	if (err)
		goto exit_free;

	err = bpf_map_update_elem(fd, key, value, flags);
	if (err) {
678
		p_err("update failed: %s", strerror(errno));
J
Jakub Kicinski 已提交
679 680 681 682 683 684 685 686 687 688
		goto exit_free;
	}

exit_free:
	if (value_fd)
		close(*value_fd);
	free(key);
	free(value);
	close(fd);

689 690
	if (!err && json_output)
		jsonw_null(json_wtr);
J
Jakub Kicinski 已提交
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	return err;
}

static int do_lookup(int argc, char **argv)
{
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	void *key, *value;
	int err;
	int fd;

	if (argc < 2)
		usage();

	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
	if (fd < 0)
		return -1;

	key = malloc(info.key_size);
	value = alloc_value(&info);
	if (!key || !value) {
712
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
713 714 715 716 717 718 719 720 721 722
		err = -1;
		goto exit_free;
	}

	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
	if (err)
		goto exit_free;

	err = bpf_map_lookup_elem(fd, key, value);
	if (!err) {
723 724 725 726
		if (json_output)
			print_entry_json(&info, key, value);
		else
			print_entry_plain(&info, key, value);
J
Jakub Kicinski 已提交
727
	} else if (errno == ENOENT) {
728 729 730 731 732 733 734
		if (json_output) {
			jsonw_null(json_wtr);
		} else {
			printf("key:\n");
			fprint_hex(stdout, key, info.key_size, " ");
			printf("\n\nNot found\n");
		}
J
Jakub Kicinski 已提交
735
	} else {
736
		p_err("lookup failed: %s", strerror(errno));
J
Jakub Kicinski 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	}

exit_free:
	free(key);
	free(value);
	close(fd);

	return err;
}

static int do_getnext(int argc, char **argv)
{
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	void *key, *nextkey;
	int err;
	int fd;

	if (argc < 2)
		usage();

	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
	if (fd < 0)
		return -1;

	key = malloc(info.key_size);
	nextkey = malloc(info.key_size);
	if (!key || !nextkey) {
765
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
		err = -1;
		goto exit_free;
	}

	if (argc) {
		err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
				 NULL, NULL);
		if (err)
			goto exit_free;
	} else {
		free(key);
		key = NULL;
	}

	err = bpf_map_get_next_key(fd, key, nextkey);
	if (err) {
782
		p_err("can't get next key: %s", strerror(errno));
J
Jakub Kicinski 已提交
783 784 785
		goto exit_free;
	}

786 787 788 789 790 791 792 793 794 795 796
	if (json_output) {
		jsonw_start_object(json_wtr);
		if (key) {
			jsonw_name(json_wtr, "key");
			print_hex_data_json(key, info.key_size);
		} else {
			jsonw_null_field(json_wtr, "key");
		}
		jsonw_name(json_wtr, "next_key");
		print_hex_data_json(nextkey, info.key_size);
		jsonw_end_object(json_wtr);
J
Jakub Kicinski 已提交
797
	} else {
798 799 800 801 802 803 804 805 806 807
		if (key) {
			printf("key:\n");
			fprint_hex(stdout, key, info.key_size, " ");
			printf("\n");
		} else {
			printf("key: None\n");
		}
		printf("next key:\n");
		fprint_hex(stdout, nextkey, info.key_size, " ");
		printf("\n");
J
Jakub Kicinski 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	}

exit_free:
	free(nextkey);
	free(key);
	close(fd);

	return err;
}

static int do_delete(int argc, char **argv)
{
	struct bpf_map_info info = {};
	__u32 len = sizeof(info);
	void *key;
	int err;
	int fd;

	if (argc < 2)
		usage();

	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
	if (fd < 0)
		return -1;

	key = malloc(info.key_size);
	if (!key) {
835
		p_err("mem alloc failed");
J
Jakub Kicinski 已提交
836 837 838 839 840 841 842 843 844 845
		err = -1;
		goto exit_free;
	}

	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
	if (err)
		goto exit_free;

	err = bpf_map_delete_elem(fd, key);
	if (err)
846
		p_err("delete failed: %s", strerror(errno));
J
Jakub Kicinski 已提交
847 848 849 850 851

exit_free:
	free(key);
	close(fd);

852 853
	if (!err && json_output)
		jsonw_null(json_wtr);
J
Jakub Kicinski 已提交
854 855 856 857 858
	return err;
}

static int do_pin(int argc, char **argv)
{
859 860 861 862 863 864
	int err;

	err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
	if (!err && json_output)
		jsonw_null(json_wtr);
	return err;
J
Jakub Kicinski 已提交
865 866 867 868
}

static int do_help(int argc, char **argv)
{
869 870 871 872 873
	if (json_output) {
		jsonw_null(json_wtr);
		return 0;
	}

J
Jakub Kicinski 已提交
874
	fprintf(stderr,
875
		"Usage: %s %s { show | list }   [MAP]\n"
J
Jakub Kicinski 已提交
876
		"       %s %s dump    MAP\n"
877 878 879 880
		"       %s %s update  MAP  key [hex] BYTES value [hex] VALUE [UPDATE_FLAGS]\n"
		"       %s %s lookup  MAP  key [hex] BYTES\n"
		"       %s %s getnext MAP [key [hex] BYTES]\n"
		"       %s %s delete  MAP  key [hex] BYTES\n"
J
Jakub Kicinski 已提交
881 882 883 884 885 886 887
		"       %s %s pin     MAP  FILE\n"
		"       %s %s help\n"
		"\n"
		"       MAP := { id MAP_ID | pinned FILE }\n"
		"       " HELP_SPEC_PROGRAM "\n"
		"       VALUE := { BYTES | MAP | PROG }\n"
		"       UPDATE_FLAGS := { any | exist | noexist }\n"
888
		"       " HELP_SPEC_OPTIONS "\n"
J
Jakub Kicinski 已提交
889 890 891 892 893 894 895 896 897 898
		"",
		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
		bin_name, argv[-2], bin_name, argv[-2]);

	return 0;
}

static const struct cmd cmds[] = {
	{ "show",	do_show },
899
	{ "list",	do_show },
J
Jakub Kicinski 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913
	{ "help",	do_help },
	{ "dump",	do_dump },
	{ "update",	do_update },
	{ "lookup",	do_lookup },
	{ "getnext",	do_getnext },
	{ "delete",	do_delete },
	{ "pin",	do_pin },
	{ 0 }
};

int do_map(int argc, char **argv)
{
	return cmd_select(cmds, argc, argv, do_help);
}