Linux Audio

Check our new training course

Loading...
v5.14.15
   1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
   2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   3
   4#include <assert.h>
 
   5#include <errno.h>
   6#include <fcntl.h>
   7#include <linux/err.h>
   8#include <linux/kernel.h>
   9#include <net/if.h>
  10#include <stdbool.h>
  11#include <stdio.h>
  12#include <stdlib.h>
  13#include <string.h>
  14#include <unistd.h>
  15#include <sys/types.h>
  16#include <sys/stat.h>
  17
  18#include <bpf/bpf.h>
  19#include <bpf/btf.h>
  20
  21#include "json_writer.h"
  22#include "main.h"
  23
  24const char * const map_type_name[] = {
  25	[BPF_MAP_TYPE_UNSPEC]			= "unspec",
  26	[BPF_MAP_TYPE_HASH]			= "hash",
  27	[BPF_MAP_TYPE_ARRAY]			= "array",
  28	[BPF_MAP_TYPE_PROG_ARRAY]		= "prog_array",
  29	[BPF_MAP_TYPE_PERF_EVENT_ARRAY]		= "perf_event_array",
  30	[BPF_MAP_TYPE_PERCPU_HASH]		= "percpu_hash",
  31	[BPF_MAP_TYPE_PERCPU_ARRAY]		= "percpu_array",
  32	[BPF_MAP_TYPE_STACK_TRACE]		= "stack_trace",
  33	[BPF_MAP_TYPE_CGROUP_ARRAY]		= "cgroup_array",
  34	[BPF_MAP_TYPE_LRU_HASH]			= "lru_hash",
  35	[BPF_MAP_TYPE_LRU_PERCPU_HASH]		= "lru_percpu_hash",
  36	[BPF_MAP_TYPE_LPM_TRIE]			= "lpm_trie",
  37	[BPF_MAP_TYPE_ARRAY_OF_MAPS]		= "array_of_maps",
  38	[BPF_MAP_TYPE_HASH_OF_MAPS]		= "hash_of_maps",
  39	[BPF_MAP_TYPE_DEVMAP]			= "devmap",
  40	[BPF_MAP_TYPE_DEVMAP_HASH]		= "devmap_hash",
  41	[BPF_MAP_TYPE_SOCKMAP]			= "sockmap",
  42	[BPF_MAP_TYPE_CPUMAP]			= "cpumap",
  43	[BPF_MAP_TYPE_XSKMAP]			= "xskmap",
  44	[BPF_MAP_TYPE_SOCKHASH]			= "sockhash",
  45	[BPF_MAP_TYPE_CGROUP_STORAGE]		= "cgroup_storage",
  46	[BPF_MAP_TYPE_REUSEPORT_SOCKARRAY]	= "reuseport_sockarray",
  47	[BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE]	= "percpu_cgroup_storage",
  48	[BPF_MAP_TYPE_QUEUE]			= "queue",
  49	[BPF_MAP_TYPE_STACK]			= "stack",
  50	[BPF_MAP_TYPE_SK_STORAGE]		= "sk_storage",
  51	[BPF_MAP_TYPE_STRUCT_OPS]		= "struct_ops",
  52	[BPF_MAP_TYPE_RINGBUF]			= "ringbuf",
  53	[BPF_MAP_TYPE_INODE_STORAGE]		= "inode_storage",
  54	[BPF_MAP_TYPE_TASK_STORAGE]		= "task_storage",
  55};
  56
  57const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  58
  59static bool map_is_per_cpu(__u32 type)
  60{
  61	return type == BPF_MAP_TYPE_PERCPU_HASH ||
  62	       type == BPF_MAP_TYPE_PERCPU_ARRAY ||
  63	       type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
  64	       type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE;
  65}
  66
  67static bool map_is_map_of_maps(__u32 type)
  68{
  69	return type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
  70	       type == BPF_MAP_TYPE_HASH_OF_MAPS;
  71}
  72
  73static bool map_is_map_of_progs(__u32 type)
  74{
  75	return type == BPF_MAP_TYPE_PROG_ARRAY;
  76}
  77
  78static int map_type_from_str(const char *type)
  79{
  80	unsigned int i;
  81
  82	for (i = 0; i < ARRAY_SIZE(map_type_name); i++)
  83		/* Don't allow prefixing in case of possible future shadowing */
  84		if (map_type_name[i] && !strcmp(map_type_name[i], type))
  85			return i;
  86	return -1;
  87}
  88
  89static void *alloc_value(struct bpf_map_info *info)
  90{
  91	if (map_is_per_cpu(info->type))
  92		return malloc(round_up(info->value_size, 8) *
  93			      get_possible_cpus());
  94	else
  95		return malloc(info->value_size);
  96}
  97
  98static int do_dump_btf(const struct btf_dumper *d,
  99		       struct bpf_map_info *map_info, void *key,
 100		       void *value)
 101{
 102	__u32 value_id;
 103	int ret = 0;
 104
 105	/* start of key-value pair */
 106	jsonw_start_object(d->jw);
 
 107
 108	if (map_info->btf_key_type_id) {
 109		jsonw_name(d->jw, "key");
 110
 111		ret = btf_dumper_type(d, map_info->btf_key_type_id, key);
 112		if (ret)
 113			goto err_end_obj;
 114	}
 
 
 115
 116	value_id = map_info->btf_vmlinux_value_type_id ?
 117		: map_info->btf_value_type_id;
 
 
 
 
 118
 119	if (!map_is_per_cpu(map_info->type)) {
 120		jsonw_name(d->jw, "value");
 121		ret = btf_dumper_type(d, value_id, value);
 122	} else {
 123		unsigned int i, n, step;
 124
 125		jsonw_name(d->jw, "values");
 126		jsonw_start_array(d->jw);
 127		n = get_possible_cpus();
 128		step = round_up(map_info->value_size, 8);
 129		for (i = 0; i < n; i++) {
 130			jsonw_start_object(d->jw);
 131			jsonw_int_field(d->jw, "cpu", i);
 132			jsonw_name(d->jw, "value");
 133			ret = btf_dumper_type(d, value_id, value + i * step);
 134			jsonw_end_object(d->jw);
 135			if (ret)
 136				break;
 137		}
 138		jsonw_end_array(d->jw);
 139	}
 140
 141err_end_obj:
 142	/* end of key-value pair */
 143	jsonw_end_object(d->jw);
 144
 145	return ret;
 
 146}
 147
 148static json_writer_t *get_btf_writer(void)
 
 149{
 150	json_writer_t *jw = jsonw_new(stdout);
 
 151
 152	if (!jw)
 153		return NULL;
 154	jsonw_pretty(jw, true);
 155
 156	return jw;
 
 
 
 
 
 
 
 157}
 158
 159static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
 160			     unsigned char *value, struct btf *btf)
 161{
 162	jsonw_start_object(json_wtr);
 163
 164	if (!map_is_per_cpu(info->type)) {
 165		jsonw_name(json_wtr, "key");
 166		print_hex_data_json(key, info->key_size);
 167		jsonw_name(json_wtr, "value");
 168		print_hex_data_json(value, info->value_size);
 169		if (btf) {
 170			struct btf_dumper d = {
 171				.btf = btf,
 172				.jw = json_wtr,
 173				.is_plain_text = false,
 174			};
 175
 176			jsonw_name(json_wtr, "formatted");
 177			do_dump_btf(&d, info, key, value);
 178		}
 179	} else {
 180		unsigned int i, n, step;
 181
 182		n = get_possible_cpus();
 183		step = round_up(info->value_size, 8);
 184
 185		jsonw_name(json_wtr, "key");
 186		print_hex_data_json(key, info->key_size);
 187
 188		jsonw_name(json_wtr, "values");
 189		jsonw_start_array(json_wtr);
 190		for (i = 0; i < n; i++) {
 191			jsonw_start_object(json_wtr);
 192
 193			jsonw_int_field(json_wtr, "cpu", i);
 194
 195			jsonw_name(json_wtr, "value");
 196			print_hex_data_json(value + i * step,
 197					    info->value_size);
 198
 199			jsonw_end_object(json_wtr);
 200		}
 201		jsonw_end_array(json_wtr);
 202		if (btf) {
 203			struct btf_dumper d = {
 204				.btf = btf,
 205				.jw = json_wtr,
 206				.is_plain_text = false,
 207			};
 208
 209			jsonw_name(json_wtr, "formatted");
 210			do_dump_btf(&d, info, key, value);
 211		}
 212	}
 213
 214	jsonw_end_object(json_wtr);
 215}
 216
 217static void
 218print_entry_error_msg(struct bpf_map_info *info, unsigned char *key,
 219		      const char *error_msg)
 220{
 221	int msg_size = strlen(error_msg);
 222	bool single_line, break_names;
 223
 224	break_names = info->key_size > 16 || msg_size > 16;
 225	single_line = info->key_size + msg_size <= 24 && !break_names;
 226
 227	printf("key:%c", break_names ? '\n' : ' ');
 228	fprint_hex(stdout, key, info->key_size, " ");
 229
 230	printf(single_line ? "  " : "\n");
 231
 232	printf("value:%c%s", break_names ? '\n' : ' ', error_msg);
 233
 234	printf("\n");
 235}
 236
 237static void
 238print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno)
 239{
 240	/* For prog_array maps or arrays of maps, failure to lookup the value
 241	 * means there is no entry for that key. Do not print an error message
 242	 * in that case.
 243	 */
 244	if ((map_is_map_of_maps(map_info->type) ||
 245	     map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT)
 246		return;
 247
 248	if (json_output) {
 249		jsonw_start_object(json_wtr);	/* entry */
 250		jsonw_name(json_wtr, "key");
 251		print_hex_data_json(key, map_info->key_size);
 252		jsonw_name(json_wtr, "value");
 253		jsonw_start_object(json_wtr);	/* error */
 254		jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
 255		jsonw_end_object(json_wtr);	/* error */
 256		jsonw_end_object(json_wtr);	/* entry */
 257	} else {
 258		const char *msg = NULL;
 259
 260		if (lookup_errno == ENOENT)
 261			msg = "<no entry>";
 262		else if (lookup_errno == ENOSPC &&
 263			 map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
 264			msg = "<cannot read>";
 265
 266		print_entry_error_msg(map_info, key,
 267				      msg ? : strerror(lookup_errno));
 268	}
 269}
 270
 271static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
 272			      unsigned char *value)
 273{
 274	if (!map_is_per_cpu(info->type)) {
 275		bool single_line, break_names;
 276
 277		break_names = info->key_size > 16 || info->value_size > 16;
 278		single_line = info->key_size + info->value_size <= 24 &&
 279			!break_names;
 280
 281		if (info->key_size) {
 282			printf("key:%c", break_names ? '\n' : ' ');
 283			fprint_hex(stdout, key, info->key_size, " ");
 284
 285			printf(single_line ? "  " : "\n");
 286		}
 287
 288		if (info->value_size) {
 289			printf("value:%c", break_names ? '\n' : ' ');
 290			fprint_hex(stdout, value, info->value_size, " ");
 291		}
 292
 293		printf("\n");
 294	} else {
 295		unsigned int i, n, step;
 296
 297		n = get_possible_cpus();
 298		step = round_up(info->value_size, 8);
 299
 300		if (info->key_size) {
 301			printf("key:\n");
 302			fprint_hex(stdout, key, info->key_size, " ");
 
 
 
 
 
 303			printf("\n");
 304		}
 305		if (info->value_size) {
 306			for (i = 0; i < n; i++) {
 307				printf("value (CPU %02d):%c",
 308				       i, info->value_size > 16 ? '\n' : ' ');
 309				fprint_hex(stdout, value + i * step,
 310					   info->value_size, " ");
 311				printf("\n");
 312			}
 313		}
 314	}
 315}
 316
 317static char **parse_bytes(char **argv, const char *name, unsigned char *val,
 318			  unsigned int n)
 319{
 320	unsigned int i = 0, base = 0;
 321	char *endptr;
 322
 323	if (is_prefix(*argv, "hex")) {
 324		base = 16;
 325		argv++;
 326	}
 327
 328	while (i < n && argv[i]) {
 329		val[i] = strtoul(argv[i], &endptr, base);
 330		if (*endptr) {
 331			p_err("error parsing byte: %s", argv[i]);
 332			return NULL;
 333		}
 334		i++;
 335	}
 336
 337	if (i != n) {
 338		p_err("%s expected %d bytes got %d", name, n, i);
 339		return NULL;
 340	}
 341
 342	return argv + i;
 343}
 344
 345/* on per cpu maps we must copy the provided value on all value instances */
 346static void fill_per_cpu_value(struct bpf_map_info *info, void *value)
 347{
 348	unsigned int i, n, step;
 349
 350	if (!map_is_per_cpu(info->type))
 351		return;
 352
 353	n = get_possible_cpus();
 354	step = round_up(info->value_size, 8);
 355	for (i = 1; i < n; i++)
 356		memcpy(value + i * step, value, info->value_size);
 357}
 358
 359static int parse_elem(char **argv, struct bpf_map_info *info,
 360		      void *key, void *value, __u32 key_size, __u32 value_size,
 361		      __u32 *flags, __u32 **value_fd)
 362{
 363	if (!*argv) {
 364		if (!key && !value)
 365			return 0;
 366		p_err("did not find %s", key ? "key" : "value");
 367		return -1;
 368	}
 369
 370	if (is_prefix(*argv, "key")) {
 371		if (!key) {
 372			if (key_size)
 373				p_err("duplicate key");
 374			else
 375				p_err("unnecessary key");
 376			return -1;
 377		}
 378
 379		argv = parse_bytes(argv + 1, "key", key, key_size);
 380		if (!argv)
 381			return -1;
 382
 383		return parse_elem(argv, info, NULL, value, key_size, value_size,
 384				  flags, value_fd);
 385	} else if (is_prefix(*argv, "value")) {
 386		int fd;
 387
 388		if (!value) {
 389			if (value_size)
 390				p_err("duplicate value");
 391			else
 392				p_err("unnecessary value");
 393			return -1;
 394		}
 395
 396		argv++;
 397
 398		if (map_is_map_of_maps(info->type)) {
 399			int argc = 2;
 400
 401			if (value_size != 4) {
 402				p_err("value smaller than 4B for map in map?");
 403				return -1;
 404			}
 405			if (!argv[0] || !argv[1]) {
 406				p_err("not enough value arguments for map in map");
 407				return -1;
 408			}
 409
 410			fd = map_parse_fd(&argc, &argv);
 411			if (fd < 0)
 412				return -1;
 413
 414			*value_fd = value;
 415			**value_fd = fd;
 416		} else if (map_is_map_of_progs(info->type)) {
 417			int argc = 2;
 418
 419			if (value_size != 4) {
 420				p_err("value smaller than 4B for map of progs?");
 421				return -1;
 422			}
 423			if (!argv[0] || !argv[1]) {
 424				p_err("not enough value arguments for map of progs");
 425				return -1;
 426			}
 427			if (is_prefix(*argv, "id"))
 428				p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n"
 429				       "         by some process or pinned otherwise update will be lost");
 430
 431			fd = prog_parse_fd(&argc, &argv);
 432			if (fd < 0)
 433				return -1;
 434
 435			*value_fd = value;
 436			**value_fd = fd;
 437		} else {
 438			argv = parse_bytes(argv, "value", value, value_size);
 439			if (!argv)
 440				return -1;
 441
 442			fill_per_cpu_value(info, value);
 443		}
 444
 445		return parse_elem(argv, info, key, NULL, key_size, value_size,
 446				  flags, NULL);
 447	} else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") ||
 448		   is_prefix(*argv, "exist")) {
 449		if (!flags) {
 450			p_err("flags specified multiple times: %s", *argv);
 451			return -1;
 452		}
 453
 454		if (is_prefix(*argv, "any"))
 455			*flags = BPF_ANY;
 456		else if (is_prefix(*argv, "noexist"))
 457			*flags = BPF_NOEXIST;
 458		else if (is_prefix(*argv, "exist"))
 459			*flags = BPF_EXIST;
 460
 461		return parse_elem(argv + 1, info, key, value, key_size,
 462				  value_size, NULL, value_fd);
 463	}
 464
 465	p_err("expected key or value, got: %s", *argv);
 466	return -1;
 467}
 468
 469static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr)
 470{
 471	jsonw_uint_field(wtr, "id", info->id);
 472	if (info->type < ARRAY_SIZE(map_type_name))
 473		jsonw_string_field(wtr, "type", map_type_name[info->type]);
 474	else
 475		jsonw_uint_field(wtr, "type", info->type);
 476
 477	if (*info->name)
 478		jsonw_string_field(wtr, "name", info->name);
 479
 480	jsonw_name(wtr, "flags");
 481	jsonw_printf(wtr, "%d", info->map_flags);
 482}
 483
 484static int show_map_close_json(int fd, struct bpf_map_info *info)
 485{
 486	char *memlock, *frozen_str;
 487	int frozen = 0;
 488
 489	memlock = get_fdinfo(fd, "memlock");
 490	frozen_str = get_fdinfo(fd, "frozen");
 491
 492	jsonw_start_object(json_wtr);
 493
 494	show_map_header_json(info, json_wtr);
 
 
 
 
 
 
 
 
 
 
 
 495
 496	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
 497
 498	jsonw_uint_field(json_wtr, "bytes_key", info->key_size);
 499	jsonw_uint_field(json_wtr, "bytes_value", info->value_size);
 500	jsonw_uint_field(json_wtr, "max_entries", info->max_entries);
 501
 502	if (memlock)
 503		jsonw_int_field(json_wtr, "bytes_memlock", atoi(memlock));
 504	free(memlock);
 505
 506	if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
 507		char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
 508		char *owner_jited = get_fdinfo(fd, "owner_jited");
 509
 510		if (owner_prog_type) {
 511			unsigned int prog_type = atoi(owner_prog_type);
 512
 513			if (prog_type < prog_type_name_size)
 514				jsonw_string_field(json_wtr, "owner_prog_type",
 515						   prog_type_name[prog_type]);
 516			else
 517				jsonw_uint_field(json_wtr, "owner_prog_type",
 518						 prog_type);
 519		}
 520		if (owner_jited)
 521			jsonw_bool_field(json_wtr, "owner_jited",
 522					 !!atoi(owner_jited));
 523
 524		free(owner_prog_type);
 525		free(owner_jited);
 526	}
 527	close(fd);
 528
 529	if (frozen_str) {
 530		frozen = atoi(frozen_str);
 531		free(frozen_str);
 532	}
 533	jsonw_int_field(json_wtr, "frozen", frozen);
 534
 535	if (info->btf_id)
 536		jsonw_int_field(json_wtr, "btf_id", info->btf_id);
 537
 538	if (!hash_empty(map_table.table)) {
 539		struct pinned_obj *obj;
 540
 541		jsonw_name(json_wtr, "pinned");
 542		jsonw_start_array(json_wtr);
 543		hash_for_each_possible(map_table.table, obj, hash, info->id) {
 544			if (obj->id == info->id)
 545				jsonw_string(json_wtr, obj->path);
 546		}
 547		jsonw_end_array(json_wtr);
 548	}
 549
 550	emit_obj_refs_json(&refs_table, info->id, json_wtr);
 551
 552	jsonw_end_object(json_wtr);
 553
 554	return 0;
 555}
 556
 557static void show_map_header_plain(struct bpf_map_info *info)
 558{
 
 
 
 
 
 559	printf("%u: ", info->id);
 560	if (info->type < ARRAY_SIZE(map_type_name))
 561		printf("%s  ", map_type_name[info->type]);
 562	else
 563		printf("type %u  ", info->type);
 564
 565	if (*info->name)
 566		printf("name %s  ", info->name);
 567
 568	printf("flags 0x%x", info->map_flags);
 569	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
 570	printf("\n");
 571}
 572
 573static int show_map_close_plain(int fd, struct bpf_map_info *info)
 574{
 575	char *memlock, *frozen_str;
 576	int frozen = 0;
 577
 578	memlock = get_fdinfo(fd, "memlock");
 579	frozen_str = get_fdinfo(fd, "frozen");
 580
 581	show_map_header_plain(info);
 582	printf("\tkey %uB  value %uB  max_entries %u",
 583	       info->key_size, info->value_size, info->max_entries);
 584
 585	if (memlock)
 586		printf("  memlock %sB", memlock);
 587	free(memlock);
 588
 589	if (info->type == BPF_MAP_TYPE_PROG_ARRAY) {
 590		char *owner_prog_type = get_fdinfo(fd, "owner_prog_type");
 591		char *owner_jited = get_fdinfo(fd, "owner_jited");
 592
 593		if (owner_prog_type || owner_jited)
 594			printf("\n\t");
 595		if (owner_prog_type) {
 596			unsigned int prog_type = atoi(owner_prog_type);
 597
 598			if (prog_type < prog_type_name_size)
 599				printf("owner_prog_type %s  ",
 600				       prog_type_name[prog_type]);
 601			else
 602				printf("owner_prog_type %d  ", prog_type);
 603		}
 604		if (owner_jited)
 605			printf("owner%s jited",
 606			       atoi(owner_jited) ? "" : " not");
 607
 608		free(owner_prog_type);
 609		free(owner_jited);
 610	}
 611	close(fd);
 612
 613	if (!hash_empty(map_table.table)) {
 614		struct pinned_obj *obj;
 615
 616		hash_for_each_possible(map_table.table, obj, hash, info->id) {
 617			if (obj->id == info->id)
 618				printf("\n\tpinned %s", obj->path);
 619		}
 620	}
 621	printf("\n");
 622
 623	if (frozen_str) {
 624		frozen = atoi(frozen_str);
 625		free(frozen_str);
 626	}
 627
 628	if (!info->btf_id && !frozen)
 629		return 0;
 630
 631	printf("\t");
 632
 633	if (info->btf_id)
 634		printf("btf_id %d", info->btf_id);
 635
 636	if (frozen)
 637		printf("%sfrozen", info->btf_id ? "  " : "");
 638
 639	emit_obj_refs_plain(&refs_table, info->id, "\n\tpids ");
 640
 641	printf("\n");
 642	return 0;
 643}
 644
 645static int do_show_subset(int argc, char **argv)
 646{
 647	struct bpf_map_info info = {};
 648	__u32 len = sizeof(info);
 649	int *fds = NULL;
 650	int nb_fds, i;
 651	int err = -1;
 652
 653	fds = malloc(sizeof(int));
 654	if (!fds) {
 655		p_err("mem alloc failed");
 656		return -1;
 657	}
 658	nb_fds = map_parse_fds(&argc, &argv, &fds);
 659	if (nb_fds < 1)
 660		goto exit_free;
 661
 662	if (json_output && nb_fds > 1)
 663		jsonw_start_array(json_wtr);	/* root array */
 664	for (i = 0; i < nb_fds; i++) {
 665		err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
 666		if (err) {
 667			p_err("can't get map info: %s",
 668			      strerror(errno));
 669			for (; i < nb_fds; i++)
 670				close(fds[i]);
 671			break;
 672		}
 673
 674		if (json_output)
 675			show_map_close_json(fds[i], &info);
 676		else
 677			show_map_close_plain(fds[i], &info);
 678
 679		close(fds[i]);
 680	}
 681	if (json_output && nb_fds > 1)
 682		jsonw_end_array(json_wtr);	/* root array */
 683
 684exit_free:
 685	free(fds);
 686	return err;
 687}
 688
 689static int do_show(int argc, char **argv)
 690{
 691	struct bpf_map_info info = {};
 692	__u32 len = sizeof(info);
 693	__u32 id = 0;
 694	int err;
 695	int fd;
 696
 697	if (show_pinned)
 698		build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
 699	build_obj_refs_table(&refs_table, BPF_OBJ_MAP);
 700
 701	if (argc == 2)
 702		return do_show_subset(argc, argv);
 
 
 
 
 
 
 
 
 703
 704	if (argc)
 705		return BAD_ARG();
 706
 707	if (json_output)
 708		jsonw_start_array(json_wtr);
 709	while (true) {
 710		err = bpf_map_get_next_id(id, &id);
 711		if (err) {
 712			if (errno == ENOENT)
 713				break;
 714			p_err("can't get next map: %s%s", strerror(errno),
 715			      errno == EINVAL ? " -- kernel too old?" : "");
 716			break;
 717		}
 718
 719		fd = bpf_map_get_fd_by_id(id);
 720		if (fd < 0) {
 721			if (errno == ENOENT)
 722				continue;
 723			p_err("can't get map by id (%u): %s",
 724			      id, strerror(errno));
 725			break;
 726		}
 727
 728		err = bpf_obj_get_info_by_fd(fd, &info, &len);
 729		if (err) {
 730			p_err("can't get map info: %s", strerror(errno));
 731			close(fd);
 732			break;
 733		}
 734
 735		if (json_output)
 736			show_map_close_json(fd, &info);
 737		else
 738			show_map_close_plain(fd, &info);
 739	}
 740	if (json_output)
 741		jsonw_end_array(json_wtr);
 742
 743	delete_obj_refs_table(&refs_table);
 744
 745	return errno == ENOENT ? 0 : -1;
 746}
 747
 748static int dump_map_elem(int fd, void *key, void *value,
 749			 struct bpf_map_info *map_info, struct btf *btf,
 750			 json_writer_t *btf_wtr)
 751{
 752	if (bpf_map_lookup_elem(fd, key, value)) {
 753		print_entry_error(map_info, key, errno);
 754		return -1;
 755	}
 756
 757	if (json_output) {
 758		print_entry_json(map_info, key, value, btf);
 759	} else if (btf) {
 760		struct btf_dumper d = {
 761			.btf = btf,
 762			.jw = btf_wtr,
 763			.is_plain_text = true,
 764		};
 765
 766		do_dump_btf(&d, map_info, key, value);
 767	} else {
 768		print_entry_plain(map_info, key, value);
 769	}
 770
 771	return 0;
 772}
 773
 774static int maps_have_btf(int *fds, int nb_fds)
 775{
 
 
 776	struct bpf_map_info info = {};
 777	__u32 len = sizeof(info);
 778	int err, i;
 779
 780	for (i = 0; i < nb_fds; i++) {
 781		err = bpf_obj_get_info_by_fd(fds[i], &info, &len);
 782		if (err) {
 783			p_err("can't get map info: %s", strerror(errno));
 784			return -1;
 785		}
 786
 787		if (!info.btf_id)
 788			return 0;
 789	}
 790
 791	return 1;
 792}
 793
 794static struct btf *btf_vmlinux;
 
 795
 796static struct btf *get_map_kv_btf(const struct bpf_map_info *info)
 797{
 798	struct btf *btf = NULL;
 799
 800	if (info->btf_vmlinux_value_type_id) {
 801		if (!btf_vmlinux) {
 802			btf_vmlinux = libbpf_find_kernel_btf();
 803			if (IS_ERR(btf_vmlinux))
 804				p_err("failed to get kernel btf");
 805		}
 806		return btf_vmlinux;
 807	} else if (info->btf_value_type_id) {
 808		int err;
 809
 810		err = btf__get_from_id(info->btf_id, &btf);
 811		if (err || !btf) {
 812			p_err("failed to get btf");
 813			btf = err ? ERR_PTR(err) : ERR_PTR(-ESRCH);
 814		}
 815	}
 816
 817	return btf;
 818}
 819
 820static void free_map_kv_btf(struct btf *btf)
 821{
 822	if (!IS_ERR(btf) && btf != btf_vmlinux)
 823		btf__free(btf);
 824}
 825
 826static void free_btf_vmlinux(void)
 827{
 828	if (!IS_ERR(btf_vmlinux))
 829		btf__free(btf_vmlinux);
 830}
 831
 832static int
 833map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr,
 834	 bool show_header)
 835{
 836	void *key, *value, *prev_key;
 837	unsigned int num_elems = 0;
 838	struct btf *btf = NULL;
 839	int err;
 840
 841	key = malloc(info->key_size);
 842	value = alloc_value(info);
 843	if (!key || !value) {
 844		p_err("mem alloc failed");
 845		err = -1;
 846		goto exit_free;
 847	}
 848
 849	prev_key = NULL;
 850
 851	if (wtr) {
 852		btf = get_map_kv_btf(info);
 853		if (IS_ERR(btf)) {
 854			err = PTR_ERR(btf);
 855			goto exit_free;
 856		}
 857
 858		if (show_header) {
 859			jsonw_start_object(wtr);	/* map object */
 860			show_map_header_json(info, wtr);
 861			jsonw_name(wtr, "elements");
 862		}
 863		jsonw_start_array(wtr);		/* elements */
 864	} else if (show_header) {
 865		show_map_header_plain(info);
 866	}
 867
 868	if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
 869	    info->value_size != 8)
 870		p_info("Warning: cannot read values from %s map with value_size != 8",
 871		       map_type_name[info->type]);
 872	while (true) {
 873		err = bpf_map_get_next_key(fd, prev_key, key);
 874		if (err) {
 875			if (errno == ENOENT)
 876				err = 0;
 877			break;
 878		}
 879		if (!dump_map_elem(fd, key, value, info, btf, wtr))
 880			num_elems++;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 881		prev_key = key;
 
 882	}
 883
 884	if (wtr) {
 885		jsonw_end_array(wtr);	/* elements */
 886		if (show_header)
 887			jsonw_end_object(wtr);	/* map object */
 888	} else {
 889		printf("Found %u element%s\n", num_elems,
 890		       num_elems != 1 ? "s" : "");
 891	}
 892
 893exit_free:
 894	free(key);
 895	free(value);
 896	close(fd);
 897	free_map_kv_btf(btf);
 898
 899	return err;
 900}
 901
 902static int do_dump(int argc, char **argv)
 903{
 904	json_writer_t *wtr = NULL, *btf_wtr = NULL;
 905	struct bpf_map_info info = {};
 906	int nb_fds, i = 0;
 907	__u32 len = sizeof(info);
 908	int *fds = NULL;
 909	int err = -1;
 910
 911	if (argc != 2)
 912		usage();
 913
 914	fds = malloc(sizeof(int));
 915	if (!fds) {
 916		p_err("mem alloc failed");
 917		return -1;
 918	}
 919	nb_fds = map_parse_fds(&argc, &argv, &fds);
 920	if (nb_fds < 1)
 921		goto exit_free;
 922
 923	if (json_output) {
 924		wtr = json_wtr;
 925	} else {
 926		int do_plain_btf;
 927
 928		do_plain_btf = maps_have_btf(fds, nb_fds);
 929		if (do_plain_btf < 0)
 930			goto exit_close;
 931
 932		if (do_plain_btf) {
 933			btf_wtr = get_btf_writer();
 934			wtr = btf_wtr;
 935			if (!btf_wtr)
 936				p_info("failed to create json writer for btf. falling back to plain output");
 937		}
 938	}
 939
 940	if (wtr && nb_fds > 1)
 941		jsonw_start_array(wtr);	/* root array */
 942	for (i = 0; i < nb_fds; i++) {
 943		if (bpf_obj_get_info_by_fd(fds[i], &info, &len)) {
 944			p_err("can't get map info: %s", strerror(errno));
 945			break;
 946		}
 947		err = map_dump(fds[i], &info, wtr, nb_fds > 1);
 948		if (!wtr && i != nb_fds - 1)
 949			printf("\n");
 950
 951		if (err)
 952			break;
 953		close(fds[i]);
 954	}
 955	if (wtr && nb_fds > 1)
 956		jsonw_end_array(wtr);	/* root array */
 957
 958	if (btf_wtr)
 959		jsonw_destroy(&btf_wtr);
 960exit_close:
 961	for (; i < nb_fds; i++)
 962		close(fds[i]);
 963exit_free:
 964	free(fds);
 965	free_btf_vmlinux();
 966	return err;
 967}
 968
 969static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
 970{
 971	*key = NULL;
 972	*value = NULL;
 973
 974	if (info->key_size) {
 975		*key = malloc(info->key_size);
 976		if (!*key) {
 977			p_err("key mem alloc failed");
 978			return -1;
 979		}
 980	}
 981
 982	if (info->value_size) {
 983		*value = alloc_value(info);
 984		if (!*value) {
 985			p_err("value mem alloc failed");
 986			free(*key);
 987			*key = NULL;
 988			return -1;
 989		}
 990	}
 991
 992	return 0;
 993}
 994
 995static int do_update(int argc, char **argv)
 996{
 997	struct bpf_map_info info = {};
 998	__u32 len = sizeof(info);
 999	__u32 *value_fd = NULL;
1000	__u32 flags = BPF_ANY;
1001	void *key, *value;
1002	int fd, err;
1003
1004	if (argc < 2)
1005		usage();
1006
1007	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1008	if (fd < 0)
1009		return -1;
1010
1011	err = alloc_key_value(&info, &key, &value);
1012	if (err)
 
 
 
1013		goto exit_free;
 
1014
1015	err = parse_elem(argv, &info, key, value, info.key_size,
1016			 info.value_size, &flags, &value_fd);
1017	if (err)
1018		goto exit_free;
1019
1020	err = bpf_map_update_elem(fd, key, value, flags);
1021	if (err) {
1022		p_err("update failed: %s", strerror(errno));
1023		goto exit_free;
1024	}
1025
1026exit_free:
1027	if (value_fd)
1028		close(*value_fd);
1029	free(key);
1030	free(value);
1031	close(fd);
1032
1033	if (!err && json_output)
1034		jsonw_null(json_wtr);
1035	return err;
1036}
1037
1038static void print_key_value(struct bpf_map_info *info, void *key,
1039			    void *value)
1040{
1041	json_writer_t *btf_wtr;
1042	struct btf *btf = NULL;
1043	int err;
1044
1045	err = btf__get_from_id(info->btf_id, &btf);
1046	if (err) {
1047		p_err("failed to get btf");
1048		return;
1049	}
1050
1051	if (json_output) {
1052		print_entry_json(info, key, value, btf);
1053	} else if (btf) {
1054		/* if here json_wtr wouldn't have been initialised,
1055		 * so let's create separate writer for btf
1056		 */
1057		btf_wtr = get_btf_writer();
1058		if (!btf_wtr) {
1059			p_info("failed to create json writer for btf. falling back to plain output");
1060			btf__free(btf);
1061			btf = NULL;
1062			print_entry_plain(info, key, value);
1063		} else {
1064			struct btf_dumper d = {
1065				.btf = btf,
1066				.jw = btf_wtr,
1067				.is_plain_text = true,
1068			};
1069
1070			do_dump_btf(&d, info, key, value);
1071			jsonw_destroy(&btf_wtr);
1072		}
1073	} else {
1074		print_entry_plain(info, key, value);
1075	}
1076	btf__free(btf);
1077}
1078
1079static int do_lookup(int argc, char **argv)
1080{
1081	struct bpf_map_info info = {};
1082	__u32 len = sizeof(info);
1083	void *key, *value;
1084	int err;
1085	int fd;
1086
1087	if (argc < 2)
1088		usage();
1089
1090	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1091	if (fd < 0)
1092		return -1;
1093
1094	err = alloc_key_value(&info, &key, &value);
1095	if (err)
 
 
 
1096		goto exit_free;
 
1097
1098	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1099	if (err)
1100		goto exit_free;
1101
1102	err = bpf_map_lookup_elem(fd, key, value);
1103	if (err) {
1104		if (errno == ENOENT) {
1105			if (json_output) {
1106				jsonw_null(json_wtr);
1107			} else {
1108				printf("key:\n");
1109				fprint_hex(stdout, key, info.key_size, " ");
1110				printf("\n\nNot found\n");
1111			}
1112		} else {
1113			p_err("lookup failed: %s", strerror(errno));
 
 
1114		}
1115
1116		goto exit_free;
1117	}
1118
1119	/* here means bpf_map_lookup_elem() succeeded */
1120	print_key_value(&info, key, value);
1121
1122exit_free:
1123	free(key);
1124	free(value);
1125	close(fd);
1126
1127	return err;
1128}
1129
1130static int do_getnext(int argc, char **argv)
1131{
1132	struct bpf_map_info info = {};
1133	__u32 len = sizeof(info);
1134	void *key, *nextkey;
1135	int err;
1136	int fd;
1137
1138	if (argc < 2)
1139		usage();
1140
1141	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1142	if (fd < 0)
1143		return -1;
1144
1145	key = malloc(info.key_size);
1146	nextkey = malloc(info.key_size);
1147	if (!key || !nextkey) {
1148		p_err("mem alloc failed");
1149		err = -1;
1150		goto exit_free;
1151	}
1152
1153	if (argc) {
1154		err = parse_elem(argv, &info, key, NULL, info.key_size, 0,
1155				 NULL, NULL);
1156		if (err)
1157			goto exit_free;
1158	} else {
1159		free(key);
1160		key = NULL;
1161	}
1162
1163	err = bpf_map_get_next_key(fd, key, nextkey);
1164	if (err) {
1165		p_err("can't get next key: %s", strerror(errno));
1166		goto exit_free;
1167	}
1168
1169	if (json_output) {
1170		jsonw_start_object(json_wtr);
1171		if (key) {
1172			jsonw_name(json_wtr, "key");
1173			print_hex_data_json(key, info.key_size);
1174		} else {
1175			jsonw_null_field(json_wtr, "key");
1176		}
1177		jsonw_name(json_wtr, "next_key");
1178		print_hex_data_json(nextkey, info.key_size);
1179		jsonw_end_object(json_wtr);
1180	} else {
1181		if (key) {
1182			printf("key:\n");
1183			fprint_hex(stdout, key, info.key_size, " ");
1184			printf("\n");
1185		} else {
1186			printf("key: None\n");
1187		}
1188		printf("next key:\n");
1189		fprint_hex(stdout, nextkey, info.key_size, " ");
1190		printf("\n");
1191	}
1192
1193exit_free:
1194	free(nextkey);
1195	free(key);
1196	close(fd);
1197
1198	return err;
1199}
1200
1201static int do_delete(int argc, char **argv)
1202{
1203	struct bpf_map_info info = {};
1204	__u32 len = sizeof(info);
1205	void *key;
1206	int err;
1207	int fd;
1208
1209	if (argc < 2)
1210		usage();
1211
1212	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1213	if (fd < 0)
1214		return -1;
1215
1216	key = malloc(info.key_size);
1217	if (!key) {
1218		p_err("mem alloc failed");
1219		err = -1;
1220		goto exit_free;
1221	}
1222
1223	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
1224	if (err)
1225		goto exit_free;
1226
1227	err = bpf_map_delete_elem(fd, key);
1228	if (err)
1229		p_err("delete failed: %s", strerror(errno));
1230
1231exit_free:
1232	free(key);
1233	close(fd);
1234
1235	if (!err && json_output)
1236		jsonw_null(json_wtr);
1237	return err;
1238}
1239
1240static int do_pin(int argc, char **argv)
1241{
1242	int err;
1243
1244	err = do_pin_any(argc, argv, map_parse_fd);
1245	if (!err && json_output)
1246		jsonw_null(json_wtr);
1247	return err;
1248}
1249
1250static int do_create(int argc, char **argv)
1251{
1252	struct bpf_create_map_attr attr = { NULL, };
1253	const char *pinfile;
1254	int err = -1, fd;
1255
1256	if (!REQ_ARGS(7))
1257		return -1;
1258	pinfile = GET_ARG();
1259
1260	while (argc) {
1261		if (!REQ_ARGS(2))
1262			return -1;
1263
1264		if (is_prefix(*argv, "type")) {
1265			NEXT_ARG();
1266
1267			if (attr.map_type) {
1268				p_err("map type already specified");
1269				goto exit;
1270			}
1271
1272			attr.map_type = map_type_from_str(*argv);
1273			if ((int)attr.map_type < 0) {
1274				p_err("unrecognized map type: %s", *argv);
1275				goto exit;
1276			}
1277			NEXT_ARG();
1278		} else if (is_prefix(*argv, "name")) {
1279			NEXT_ARG();
1280			attr.name = GET_ARG();
1281		} else if (is_prefix(*argv, "key")) {
1282			if (parse_u32_arg(&argc, &argv, &attr.key_size,
1283					  "key size"))
1284				goto exit;
1285		} else if (is_prefix(*argv, "value")) {
1286			if (parse_u32_arg(&argc, &argv, &attr.value_size,
1287					  "value size"))
1288				goto exit;
1289		} else if (is_prefix(*argv, "entries")) {
1290			if (parse_u32_arg(&argc, &argv, &attr.max_entries,
1291					  "max entries"))
1292				goto exit;
1293		} else if (is_prefix(*argv, "flags")) {
1294			if (parse_u32_arg(&argc, &argv, &attr.map_flags,
1295					  "flags"))
1296				goto exit;
1297		} else if (is_prefix(*argv, "dev")) {
1298			NEXT_ARG();
1299
1300			if (attr.map_ifindex) {
1301				p_err("offload device already specified");
1302				goto exit;
1303			}
1304
1305			attr.map_ifindex = if_nametoindex(*argv);
1306			if (!attr.map_ifindex) {
1307				p_err("unrecognized netdevice '%s': %s",
1308				      *argv, strerror(errno));
1309				goto exit;
1310			}
1311			NEXT_ARG();
1312		} else if (is_prefix(*argv, "inner_map")) {
1313			struct bpf_map_info info = {};
1314			__u32 len = sizeof(info);
1315			int inner_map_fd;
1316
1317			NEXT_ARG();
1318			if (!REQ_ARGS(2))
1319				usage();
1320			inner_map_fd = map_parse_fd_and_info(&argc, &argv,
1321							     &info, &len);
1322			if (inner_map_fd < 0)
1323				return -1;
1324			attr.inner_map_fd = inner_map_fd;
1325		} else {
1326			p_err("unknown arg %s", *argv);
1327			goto exit;
1328		}
1329	}
1330
1331	if (!attr.name) {
1332		p_err("map name not specified");
1333		goto exit;
1334	}
1335
1336	set_max_rlimit();
1337
1338	fd = bpf_create_map_xattr(&attr);
1339	if (fd < 0) {
1340		p_err("map create failed: %s", strerror(errno));
1341		goto exit;
1342	}
1343
1344	err = do_pin_fd(fd, pinfile);
1345	close(fd);
1346	if (err)
1347		goto exit;
1348
1349	if (json_output)
1350		jsonw_null(json_wtr);
1351
1352exit:
1353	if (attr.inner_map_fd > 0)
1354		close(attr.inner_map_fd);
1355
1356	return err;
1357}
1358
1359static int do_pop_dequeue(int argc, char **argv)
1360{
1361	struct bpf_map_info info = {};
1362	__u32 len = sizeof(info);
1363	void *key, *value;
1364	int err;
1365	int fd;
1366
1367	if (argc < 2)
1368		usage();
1369
1370	fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
1371	if (fd < 0)
1372		return -1;
1373
1374	err = alloc_key_value(&info, &key, &value);
1375	if (err)
1376		goto exit_free;
1377
1378	err = bpf_map_lookup_and_delete_elem(fd, key, value);
1379	if (err) {
1380		if (errno == ENOENT) {
1381			if (json_output)
1382				jsonw_null(json_wtr);
1383			else
1384				printf("Error: empty map\n");
1385		} else {
1386			p_err("pop failed: %s", strerror(errno));
1387		}
1388
1389		goto exit_free;
1390	}
1391
1392	print_key_value(&info, key, value);
1393
1394exit_free:
1395	free(key);
1396	free(value);
1397	close(fd);
1398
1399	return err;
1400}
1401
1402static int do_freeze(int argc, char **argv)
1403{
1404	int err, fd;
1405
1406	if (!REQ_ARGS(2))
1407		return -1;
1408
1409	fd = map_parse_fd(&argc, &argv);
1410	if (fd < 0)
1411		return -1;
1412
1413	if (argc) {
1414		close(fd);
1415		return BAD_ARG();
1416	}
1417
1418	err = bpf_map_freeze(fd);
1419	close(fd);
1420	if (err) {
1421		p_err("failed to freeze map: %s", strerror(errno));
1422		return err;
1423	}
1424
1425	if (json_output)
1426		jsonw_null(json_wtr);
1427
1428	return 0;
1429}
1430
1431static int do_help(int argc, char **argv)
1432{
1433	if (json_output) {
1434		jsonw_null(json_wtr);
1435		return 0;
1436	}
1437
1438	fprintf(stderr,
1439		"Usage: %1$s %2$s { show | list }   [MAP]\n"
1440		"       %1$s %2$s create     FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
1441		"                                  entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1442		"                                  [inner_map MAP] [dev NAME]\n"
1443		"       %1$s %2$s dump       MAP\n"
1444		"       %1$s %2$s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
1445		"       %1$s %2$s lookup     MAP [key DATA]\n"
1446		"       %1$s %2$s getnext    MAP [key DATA]\n"
1447		"       %1$s %2$s delete     MAP  key DATA\n"
1448		"       %1$s %2$s pin        MAP  FILE\n"
1449		"       %1$s %2$s event_pipe MAP [cpu N index M]\n"
1450		"       %1$s %2$s peek       MAP\n"
1451		"       %1$s %2$s push       MAP value VALUE\n"
1452		"       %1$s %2$s pop        MAP\n"
1453		"       %1$s %2$s enqueue    MAP value VALUE\n"
1454		"       %1$s %2$s dequeue    MAP\n"
1455		"       %1$s %2$s freeze     MAP\n"
1456		"       %1$s %2$s help\n"
1457		"\n"
1458		"       " HELP_SPEC_MAP "\n"
1459		"       DATA := { [hex] BYTES }\n"
1460		"       " HELP_SPEC_PROGRAM "\n"
1461		"       VALUE := { DATA | MAP | PROG }\n"
1462		"       UPDATE_FLAGS := { any | exist | noexist }\n"
1463		"       TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n"
1464		"                 percpu_array | stack_trace | cgroup_array | lru_hash |\n"
1465		"                 lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n"
1466		"                 devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n"
1467		"                 cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n"
1468		"                 queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n"
1469		"		  task_storage }\n"
1470		"       " HELP_SPEC_OPTIONS "\n"
1471		"",
1472		bin_name, argv[-2]);
 
 
1473
1474	return 0;
1475}
1476
1477static const struct cmd cmds[] = {
1478	{ "show",	do_show },
1479	{ "list",	do_show },
1480	{ "help",	do_help },
1481	{ "dump",	do_dump },
1482	{ "update",	do_update },
1483	{ "lookup",	do_lookup },
1484	{ "getnext",	do_getnext },
1485	{ "delete",	do_delete },
1486	{ "pin",	do_pin },
1487	{ "event_pipe",	do_event_pipe },
1488	{ "create",	do_create },
1489	{ "peek",	do_lookup },
1490	{ "push",	do_update },
1491	{ "enqueue",	do_update },
1492	{ "pop",	do_pop_dequeue },
1493	{ "dequeue",	do_pop_dequeue },
1494	{ "freeze",	do_freeze },
1495	{ 0 }
1496};
1497
1498int do_map(int argc, char **argv)
1499{
1500	return cmd_select(cmds, argc, argv, do_help);
1501}
v4.17
  1/*
  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",
 69	[BPF_MAP_TYPE_CPUMAP]		= "cpumap",
 
 
 
 
 
 
 
 
 
 
 
 
 
 70};
 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) {
 85		p_err("can't open sysfs possible cpus");
 86		exit(-1);
 87	}
 88
 89	n = read(fd, buf, sizeof(buf));
 90	if (n < 2) {
 91		p_err("can't read sysfs possible cpus");
 92		exit(-1);
 93	}
 94	close(fd);
 95
 96	if (n == sizeof(buf)) {
 97		p_err("read sysfs possible cpus overflow");
 98		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) {
165			p_err("can't parse %s as ID", **argv);
166			return -1;
167		}
168		NEXT_ARGP();
169
170		fd = bpf_map_get_fd_by_id(id);
171		if (fd < 0)
172			p_err("get map by id (%u): %s", id, strerror(errno));
173		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
185	p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
186	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) {
201		p_err("can't get map info: %s", strerror(errno));
202		close(fd);
203		return err;
204	}
205
206	return fd;
207}
208
209static 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)
248{
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' : ' ');
257		fprint_hex(stdout, key, info->key_size, " ");
 
258
259		printf(single_line ? "  " : "\n");
 
260
261		printf("value:%c", break_names ? '\n' : ' ');
262		fprint_hex(stdout, value, info->value_size, " ");
 
 
263
264		printf("\n");
265	} else {
266		unsigned int i, n;
267
268		n = get_possible_cpus();
 
269
270		printf("key:\n");
271		fprint_hex(stdout, key, info->key_size, " ");
272		printf("\n");
273		for (i = 0; i < n; i++) {
274			printf("value (CPU %02d):%c",
275			       i, info->value_size > 16 ? '\n' : ' ');
276			fprint_hex(stdout, value + i * info->value_size,
277				   info->value_size, " ");
278			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) {
292			p_err("error parsing byte: %s", argv[i]);
293			return NULL;
294		}
295		i++;
296	}
297
298	if (i != n) {
299		p_err("%s expected %d bytes got %d", name, n, i);
300		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;
313		p_err("did not find %s", key ? "key" : "value");
314		return -1;
315	}
316
317	if (is_prefix(*argv, "key")) {
318		if (!key) {
319			if (key_size)
320				p_err("duplicate key");
321			else
322				p_err("unnecessary key");
323			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)
337				p_err("duplicate value");
338			else
339				p_err("unnecessary value");
340			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) {
349				p_err("value smaller than 4B for map in map?");
350				return -1;
351			}
352			if (!argv[0] || !argv[1]) {
353				p_err("not enough value arguments for map in map");
354				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) {
367				p_err("value smaller than 4B for map of progs?");
368				return -1;
369			}
370			if (!argv[0] || !argv[1]) {
371				p_err("not enough value arguments for map of progs");
372				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) {
392			p_err("flags specified multiple times: %s", *argv);
393			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
407	p_err("expected key or value, got: %s", *argv);
408	return -1;
409}
410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411static 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, "%d", info->map_flags);
432
433	print_dev_json(info->ifindex, info->netns_dev, info->netns_ino);
434
435	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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443	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
 
 
455	jsonw_end_object(json_wtr);
456
457	return 0;
458}
459
460static int show_map_close_plain(int fd, struct bpf_map_info *info)
461{
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
476	printf("flags 0x%x", info->map_flags);
477	print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino);
478	printf("\n");
 
 
 
 
 
 
 
 
 
 
 
479	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");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487	if (!hash_empty(map_table.table)) {
488		struct pinned_obj *obj;
489
490		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	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
495	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
506	if (show_pinned)
507		build_pinned_obj_table(&map_table, BPF_OBJ_MAP);
 
508
509	if (argc == 2) {
510		fd = map_parse_fd_and_info(&argc, &argv, &info, &len);
511		if (fd < 0)
512			return -1;
513
514		if (json_output)
515			return show_map_close_json(fd, &info);
516		else
517			return show_map_close_plain(fd, &info);
518	}
519
520	if (argc)
521		return BAD_ARG();
522
523	if (json_output)
524		jsonw_start_array(json_wtr);
525	while (true) {
526		err = bpf_map_get_next_id(id, &id);
527		if (err) {
528			if (errno == ENOENT)
529				break;
530			p_err("can't get next map: %s%s", strerror(errno),
531			      errno == EINVAL ? " -- kernel too old?" : "");
532			break;
533		}
534
535		fd = bpf_map_get_fd_by_id(id);
536		if (fd < 0) {
537			if (errno == ENOENT)
538				continue;
539			p_err("can't get map by id (%u): %s",
540			      id, strerror(errno));
541			break;
542		}
543
544		err = bpf_obj_get_info_by_fd(fd, &info, &len);
545		if (err) {
546			p_err("can't get map info: %s", strerror(errno));
547			close(fd);
548			break;
549		}
550
551		if (json_output)
552			show_map_close_json(fd, &info);
553		else
554			show_map_close_plain(fd, &info);
555	}
556	if (json_output)
557		jsonw_end_array(json_wtr);
558
 
 
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)) {
579		p_err("Dumping maps of maps and program maps not supported");
580		close(fd);
581		return -1;
 
 
 
 
 
 
 
 
 
 
 
582	}
583
584	key = malloc(info.key_size);
585	value = alloc_value(&info);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586	if (!key || !value) {
587		p_err("mem alloc failed");
588		err = -1;
589		goto exit_free;
590	}
591
592	prev_key = NULL;
593	if (json_output)
594		jsonw_start_array(json_wtr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
595	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)) {
604			if (json_output)
605				print_entry_json(&info, key, value);
606			else
607				print_entry_plain(&info, key, value);
608		} else {
609			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			}
622		}
623
624		prev_key = key;
625		num_elems++;
626	}
627
628	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" : "");
 
633
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) {
661		p_err("mem alloc failed");
662		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) {
673		p_err("update failed: %s", strerror(errno));
674		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
684	if (!err && json_output)
685		jsonw_null(json_wtr);
686	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) {
707		p_err("mem alloc failed");
708		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) {
718		if (json_output)
719			print_entry_json(&info, key, value);
720		else
721			print_entry_plain(&info, key, value);
722	} else if (errno == ENOENT) {
723		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		}
730	} else {
731		p_err("lookup failed: %s", strerror(errno));
732	}
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) {
760		p_err("mem alloc failed");
761		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) {
777		p_err("can't get next key: %s", strerror(errno));
778		goto exit_free;
779	}
780
781	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);
792	} else {
793		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");
803	}
804
805exit_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) {
830		p_err("mem alloc failed");
831		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)
841		p_err("delete failed: %s", strerror(errno));
842
843exit_free:
844	free(key);
845	close(fd);
846
847	if (!err && json_output)
848		jsonw_null(json_wtr);
849	return err;
850}
851
852static int do_pin(int argc, char **argv)
853{
854	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;
860}
861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
862static int do_help(int argc, char **argv)
863{
864	if (json_output) {
865		jsonw_null(json_wtr);
866		return 0;
867	}
868
869	fprintf(stderr,
870		"Usage: %s %s { show | list }   [MAP]\n"
871		"       %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"
 
 
 
 
 
 
 
883		"       " HELP_SPEC_OPTIONS "\n"
884		"",
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 },
894	{ "list",	do_show },
895	{ "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}