Linux Audio

Check our new training course

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