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#ifndef _GNU_SOURCE
   5#define _GNU_SOURCE
   6#endif
   7#include <ctype.h>
   8#include <errno.h>
   9#include <fcntl.h>
  10#include <ftw.h>
  11#include <libgen.h>
  12#include <mntent.h>
  13#include <stdbool.h>
  14#include <stdio.h>
  15#include <stdlib.h>
  16#include <string.h>
  17#include <unistd.h>
 
 
  18#include <net/if.h>
  19#include <sys/mount.h>
  20#include <sys/resource.h>
  21#include <sys/stat.h>
  22#include <sys/vfs.h>
  23
  24#include <linux/filter.h>
  25#include <linux/limits.h>
  26#include <linux/magic.h>
  27#include <linux/unistd.h>
  28
  29#include <bpf/bpf.h>
  30#include <bpf/hashmap.h>
  31#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
  32#include <bpf/btf.h>
  33
  34#include "main.h"
  35
  36#ifndef BPF_FS_MAGIC
  37#define BPF_FS_MAGIC		0xcafe4a11
  38#endif
  39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  40void p_err(const char *fmt, ...)
  41{
  42	va_list ap;
  43
  44	va_start(ap, fmt);
  45	if (json_output) {
  46		jsonw_start_object(json_wtr);
  47		jsonw_name(json_wtr, "error");
  48		jsonw_vprintf_enquote(json_wtr, fmt, ap);
  49		jsonw_end_object(json_wtr);
  50	} else {
  51		fprintf(stderr, "Error: ");
  52		vfprintf(stderr, fmt, ap);
  53		fprintf(stderr, "\n");
  54	}
  55	va_end(ap);
  56}
  57
  58void p_info(const char *fmt, ...)
  59{
  60	va_list ap;
  61
  62	if (json_output)
  63		return;
  64
  65	va_start(ap, fmt);
  66	vfprintf(stderr, fmt, ap);
  67	fprintf(stderr, "\n");
  68	va_end(ap);
  69}
  70
  71static bool is_bpffs(char *path)
  72{
  73	struct statfs st_fs;
  74
  75	if (statfs(path, &st_fs) < 0)
  76		return false;
  77
  78	return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
  79}
  80
  81/* Probe whether kernel switched from memlock-based (RLIMIT_MEMLOCK) to
  82 * memcg-based memory accounting for BPF maps and programs. This was done in
  83 * commit 97306be45fbe ("Merge branch 'switch to memcg-based memory
  84 * accounting'"), in Linux 5.11.
  85 *
  86 * Libbpf also offers to probe for memcg-based accounting vs rlimit, but does
  87 * so by checking for the availability of a given BPF helper and this has
  88 * failed on some kernels with backports in the past, see commit 6b4384ff1088
  89 * ("Revert "bpftool: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK"").
  90 * Instead, we can probe by lowering the process-based rlimit to 0, trying to
  91 * load a BPF object, and resetting the rlimit. If the load succeeds then
  92 * memcg-based accounting is supported.
  93 *
  94 * This would be too dangerous to do in the library, because multithreaded
  95 * applications might attempt to load items while the rlimit is at 0. Given
  96 * that bpftool is single-threaded, this is fine to do here.
  97 */
  98static bool known_to_need_rlimit(void)
  99{
 100	struct rlimit rlim_init, rlim_cur_zero = {};
 101	struct bpf_insn insns[] = {
 102		BPF_MOV64_IMM(BPF_REG_0, 0),
 103		BPF_EXIT_INSN(),
 104	};
 105	size_t insn_cnt = ARRAY_SIZE(insns);
 106	union bpf_attr attr;
 107	int prog_fd, err;
 108
 109	memset(&attr, 0, sizeof(attr));
 110	attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
 111	attr.insns = ptr_to_u64(insns);
 112	attr.insn_cnt = insn_cnt;
 113	attr.license = ptr_to_u64("GPL");
 114
 115	if (getrlimit(RLIMIT_MEMLOCK, &rlim_init))
 116		return false;
 117
 118	/* Drop the soft limit to zero. We maintain the hard limit to its
 119	 * current value, because lowering it would be a permanent operation
 120	 * for unprivileged users.
 121	 */
 122	rlim_cur_zero.rlim_max = rlim_init.rlim_max;
 123	if (setrlimit(RLIMIT_MEMLOCK, &rlim_cur_zero))
 124		return false;
 125
 126	/* Do not use bpf_prog_load() from libbpf here, because it calls
 127	 * bump_rlimit_memlock(), interfering with the current probe.
 128	 */
 129	prog_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
 130	err = errno;
 131
 132	/* reset soft rlimit to its initial value */
 133	setrlimit(RLIMIT_MEMLOCK, &rlim_init);
 134
 135	if (prog_fd < 0)
 136		return err == EPERM;
 137
 138	close(prog_fd);
 139	return false;
 140}
 141
 142void set_max_rlimit(void)
 143{
 144	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
 145
 146	if (known_to_need_rlimit())
 147		setrlimit(RLIMIT_MEMLOCK, &rinf);
 148}
 149
 150static int
 151mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
 152{
 153	bool bind_done = false;
 154
 155	while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
 156		if (errno != EINVAL || bind_done) {
 157			snprintf(buff, bufflen,
 158				 "mount --make-private %s failed: %s",
 159				 target, strerror(errno));
 160			return -1;
 161		}
 162
 163		if (mount(target, target, "none", MS_BIND, NULL)) {
 164			snprintf(buff, bufflen,
 165				 "mount --bind %s %s failed: %s",
 166				 target, target, strerror(errno));
 167			return -1;
 168		}
 169
 170		bind_done = true;
 171	}
 172
 173	if (mount(type, target, type, 0, "mode=0700")) {
 174		snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
 175			 type, type, target, strerror(errno));
 176		return -1;
 177	}
 178
 179	return 0;
 180}
 181
 182int mount_tracefs(const char *target)
 183{
 184	char err_str[ERR_MAX_LEN];
 185	int err;
 186
 187	err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
 188	if (err) {
 189		err_str[ERR_MAX_LEN - 1] = '\0';
 190		p_err("can't mount tracefs: %s", err_str);
 191	}
 192
 193	return err;
 194}
 195
 196int open_obj_pinned(const char *path, bool quiet)
 197{
 198	char *pname;
 199	int fd = -1;
 200
 201	pname = strdup(path);
 202	if (!pname) {
 203		if (!quiet)
 204			p_err("mem alloc failed");
 205		goto out_ret;
 206	}
 207
 208	fd = bpf_obj_get(pname);
 209	if (fd < 0) {
 210		if (!quiet)
 211			p_err("bpf obj get (%s): %s", pname,
 212			      errno == EACCES && !is_bpffs(dirname(pname)) ?
 213			    "directory not in bpf file system (bpffs)" :
 214			    strerror(errno));
 215		goto out_free;
 216	}
 217
 218out_free:
 219	free(pname);
 220out_ret:
 221	return fd;
 222}
 223
 224int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
 225{
 226	enum bpf_obj_type type;
 227	int fd;
 228
 229	fd = open_obj_pinned(path, false);
 230	if (fd < 0)
 231		return -1;
 232
 233	type = get_fd_type(fd);
 234	if (type < 0) {
 235		close(fd);
 236		return type;
 237	}
 238	if (type != exp_type) {
 239		p_err("incorrect object type: %s", get_fd_type_name(type));
 240		close(fd);
 241		return -1;
 242	}
 243
 244	return fd;
 245}
 246
 247int mount_bpffs_for_pin(const char *name)
 248{
 249	char err_str[ERR_MAX_LEN];
 250	char *file;
 251	char *dir;
 252	int err = 0;
 253
 254	file = malloc(strlen(name) + 1);
 255	if (!file) {
 256		p_err("mem alloc failed");
 257		return -1;
 258	}
 259
 260	strcpy(file, name);
 261	dir = dirname(file);
 262
 263	if (is_bpffs(dir))
 264		/* nothing to do if already mounted */
 265		goto out_free;
 266
 267	if (block_mount) {
 268		p_err("no BPF file system found, not mounting it due to --nomount option");
 269		err = -1;
 270		goto out_free;
 271	}
 272
 273	err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
 274	if (err) {
 275		err_str[ERR_MAX_LEN - 1] = '\0';
 276		p_err("can't mount BPF file system to pin the object (%s): %s",
 277		      name, err_str);
 278	}
 279
 280out_free:
 281	free(file);
 282	return err;
 283}
 284
 285int do_pin_fd(int fd, const char *name)
 286{
 287	int err;
 288
 289	err = mount_bpffs_for_pin(name);
 290	if (err)
 291		return err;
 292
 293	err = bpf_obj_pin(fd, name);
 294	if (err)
 295		p_err("can't pin the object (%s): %s", name, strerror(errno));
 296
 297	return err;
 298}
 299
 300int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
 301{
 302	int err;
 303	int fd;
 304
 305	if (!REQ_ARGS(3))
 306		return -EINVAL;
 307
 308	fd = get_fd(&argc, &argv);
 309	if (fd < 0)
 310		return fd;
 311
 312	err = do_pin_fd(fd, *argv);
 313
 314	close(fd);
 315	return err;
 316}
 317
 318const char *get_fd_type_name(enum bpf_obj_type type)
 319{
 320	static const char * const names[] = {
 321		[BPF_OBJ_UNKNOWN]	= "unknown",
 322		[BPF_OBJ_PROG]		= "prog",
 323		[BPF_OBJ_MAP]		= "map",
 324		[BPF_OBJ_LINK]		= "link",
 325	};
 326
 327	if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
 328		return names[BPF_OBJ_UNKNOWN];
 329
 330	return names[type];
 331}
 332
 333void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
 334			char *name_buff, size_t buff_len)
 335{
 336	const char *prog_name = prog_info->name;
 337	const struct btf_type *func_type;
 338	const struct bpf_func_info finfo = {};
 339	struct bpf_prog_info info = {};
 340	__u32 info_len = sizeof(info);
 341	struct btf *prog_btf = NULL;
 342
 343	if (buff_len <= BPF_OBJ_NAME_LEN ||
 344	    strlen(prog_info->name) < BPF_OBJ_NAME_LEN - 1)
 345		goto copy_name;
 346
 347	if (!prog_info->btf_id || prog_info->nr_func_info == 0)
 348		goto copy_name;
 349
 350	info.nr_func_info = 1;
 351	info.func_info_rec_size = prog_info->func_info_rec_size;
 352	if (info.func_info_rec_size > sizeof(finfo))
 353		info.func_info_rec_size = sizeof(finfo);
 354	info.func_info = ptr_to_u64(&finfo);
 355
 356	if (bpf_obj_get_info_by_fd(prog_fd, &info, &info_len))
 357		goto copy_name;
 358
 359	prog_btf = btf__load_from_kernel_by_id(info.btf_id);
 360	if (!prog_btf)
 361		goto copy_name;
 362
 363	func_type = btf__type_by_id(prog_btf, finfo.type_id);
 364	if (!func_type || !btf_is_func(func_type))
 365		goto copy_name;
 366
 367	prog_name = btf__name_by_offset(prog_btf, func_type->name_off);
 368
 369copy_name:
 370	snprintf(name_buff, buff_len, "%s", prog_name);
 371
 372	if (prog_btf)
 373		btf__free(prog_btf);
 374}
 375
 376int get_fd_type(int fd)
 377{
 378	char path[PATH_MAX];
 379	char buf[512];
 380	ssize_t n;
 381
 382	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
 383
 384	n = readlink(path, buf, sizeof(buf));
 385	if (n < 0) {
 386		p_err("can't read link type: %s", strerror(errno));
 387		return -1;
 388	}
 389	if (n == sizeof(path)) {
 390		p_err("can't read link type: path too long!");
 391		return -1;
 392	}
 393
 394	if (strstr(buf, "bpf-map"))
 395		return BPF_OBJ_MAP;
 396	else if (strstr(buf, "bpf-prog"))
 397		return BPF_OBJ_PROG;
 398	else if (strstr(buf, "bpf-link"))
 399		return BPF_OBJ_LINK;
 400
 401	return BPF_OBJ_UNKNOWN;
 402}
 403
 404char *get_fdinfo(int fd, const char *key)
 405{
 406	char path[PATH_MAX];
 407	char *line = NULL;
 408	size_t line_n = 0;
 409	ssize_t n;
 410	FILE *fdi;
 411
 412	snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
 413
 414	fdi = fopen(path, "r");
 415	if (!fdi)
 416		return NULL;
 417
 418	while ((n = getline(&line, &line_n, fdi)) > 0) {
 419		char *value;
 420		int len;
 421
 422		if (!strstr(line, key))
 423			continue;
 424
 425		fclose(fdi);
 426
 427		value = strchr(line, '\t');
 428		if (!value || !value[1]) {
 429			free(line);
 430			return NULL;
 431		}
 432		value++;
 433
 434		len = strlen(value);
 435		memmove(line, value, len);
 436		line[len - 1] = '\0';
 437
 438		return line;
 439	}
 440
 441	free(line);
 442	fclose(fdi);
 443	return NULL;
 444}
 445
 446void print_data_json(uint8_t *data, size_t len)
 447{
 448	unsigned int i;
 449
 450	jsonw_start_array(json_wtr);
 451	for (i = 0; i < len; i++)
 452		jsonw_printf(json_wtr, "%d", data[i]);
 453	jsonw_end_array(json_wtr);
 454}
 455
 456void print_hex_data_json(uint8_t *data, size_t len)
 457{
 458	unsigned int i;
 459
 460	jsonw_start_array(json_wtr);
 461	for (i = 0; i < len; i++)
 462		jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
 463	jsonw_end_array(json_wtr);
 464}
 465
 466/* extra params for nftw cb */
 467static struct hashmap *build_fn_table;
 468static enum bpf_obj_type build_fn_type;
 469
 470static int do_build_table_cb(const char *fpath, const struct stat *sb,
 471			     int typeflag, struct FTW *ftwbuf)
 472{
 473	struct bpf_prog_info pinned_info;
 474	__u32 len = sizeof(pinned_info);
 
 475	enum bpf_obj_type objtype;
 476	int fd, err = 0;
 477	char *path;
 478
 479	if (typeflag != FTW_F)
 480		goto out_ret;
 481
 482	fd = open_obj_pinned(fpath, true);
 483	if (fd < 0)
 484		goto out_ret;
 485
 486	objtype = get_fd_type(fd);
 487	if (objtype != build_fn_type)
 488		goto out_close;
 489
 490	memset(&pinned_info, 0, sizeof(pinned_info));
 491	if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
 492		goto out_close;
 493
 494	path = strdup(fpath);
 495	if (!path) {
 496		err = -1;
 497		goto out_close;
 498	}
 499
 500	err = hashmap__append(build_fn_table, pinned_info.id, path);
 501	if (err) {
 502		p_err("failed to append entry to hashmap for ID %u, path '%s': %s",
 503		      pinned_info.id, path, strerror(errno));
 504		free(path);
 505		goto out_close;
 506	}
 507
 
 508out_close:
 509	close(fd);
 510out_ret:
 511	return err;
 512}
 513
 514int build_pinned_obj_table(struct hashmap *tab,
 515			   enum bpf_obj_type type)
 516{
 517	struct mntent *mntent = NULL;
 518	FILE *mntfile = NULL;
 519	int flags = FTW_PHYS;
 520	int nopenfd = 16;
 521	int err = 0;
 522
 523	mntfile = setmntent("/proc/mounts", "r");
 524	if (!mntfile)
 525		return -1;
 526
 527	build_fn_table = tab;
 528	build_fn_type = type;
 529
 530	while ((mntent = getmntent(mntfile))) {
 531		char *path = mntent->mnt_dir;
 532
 533		if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
 534			continue;
 535		err = nftw(path, do_build_table_cb, nopenfd, flags);
 536		if (err)
 537			break;
 538	}
 539	fclose(mntfile);
 540	return err;
 541}
 542
 543void delete_pinned_obj_table(struct hashmap *map)
 544{
 545	struct hashmap_entry *entry;
 546	size_t bkt;
 547
 548	if (!map)
 549		return;
 550
 551	hashmap__for_each_entry(map, entry, bkt)
 552		free(entry->pvalue);
 553
 554	hashmap__free(map);
 555}
 556
 557unsigned int get_page_size(void)
 558{
 559	static int result;
 560
 561	if (!result)
 562		result = getpagesize();
 563	return result;
 564}
 565
 566unsigned int get_possible_cpus(void)
 567{
 568	int cpus = libbpf_num_possible_cpus();
 569
 570	if (cpus < 0) {
 571		p_err("Can't get # of possible cpus: %s", strerror(-cpus));
 572		exit(-1);
 573	}
 574	return cpus;
 575}
 576
 577static char *
 578ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
 579{
 580	struct stat st;
 581	int err;
 582
 583	err = stat("/proc/self/ns/net", &st);
 584	if (err) {
 585		p_err("Can't stat /proc/self: %s", strerror(errno));
 586		return NULL;
 587	}
 588
 589	if (st.st_dev != ns_dev || st.st_ino != ns_ino)
 590		return NULL;
 591
 592	return if_indextoname(ifindex, buf);
 593}
 594
 595static int read_sysfs_hex_int(char *path)
 596{
 597	char vendor_id_buf[8];
 598	int len;
 599	int fd;
 600
 601	fd = open(path, O_RDONLY);
 602	if (fd < 0) {
 603		p_err("Can't open %s: %s", path, strerror(errno));
 604		return -1;
 605	}
 606
 607	len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
 608	close(fd);
 609	if (len < 0) {
 610		p_err("Can't read %s: %s", path, strerror(errno));
 611		return -1;
 612	}
 613	if (len >= (int)sizeof(vendor_id_buf)) {
 614		p_err("Value in %s too long", path);
 615		return -1;
 616	}
 617
 618	vendor_id_buf[len] = 0;
 619
 620	return strtol(vendor_id_buf, NULL, 0);
 621}
 622
 623static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
 624{
 625	char full_path[64];
 626
 627	snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
 628		 devname, entry_name);
 629
 630	return read_sysfs_hex_int(full_path);
 631}
 632
 633const char *
 634ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
 
 635{
 636	__maybe_unused int device_id;
 637	char devname[IF_NAMESIZE];
 638	int vendor_id;
 
 639
 640	if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
 641		p_err("Can't get net device name for ifindex %d: %s", ifindex,
 642		      strerror(errno));
 643		return NULL;
 644	}
 645
 646	vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
 647	if (vendor_id < 0) {
 648		p_err("Can't get device vendor id for %s", devname);
 649		return NULL;
 650	}
 651
 652	switch (vendor_id) {
 653#ifdef HAVE_LIBBFD_SUPPORT
 654	case 0x19ee:
 655		device_id = read_sysfs_netdev_hex_int(devname, "device");
 656		if (device_id != 0x4000 &&
 657		    device_id != 0x6000 &&
 658		    device_id != 0x6003)
 659			p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
 660		*opt = "ctx4";
 661		return "NFP-6xxx";
 662#endif /* HAVE_LIBBFD_SUPPORT */
 663	/* No NFP support in LLVM, we have no valid triple to return. */
 664	default:
 665		p_err("Can't get arch name for device vendor id 0x%04x",
 666		      vendor_id);
 667		return NULL;
 668	}
 669}
 670
 671void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
 672{
 673	char name[IF_NAMESIZE];
 674
 675	if (!ifindex)
 676		return;
 677
 678	printf("  offloaded_to ");
 679	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
 680		printf("%s", name);
 681	else
 682		printf("ifindex %u ns_dev %llu ns_ino %llu",
 683		       ifindex, ns_dev, ns_inode);
 684}
 685
 686void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
 687{
 688	char name[IF_NAMESIZE];
 689
 690	if (!ifindex)
 691		return;
 692
 693	jsonw_name(json_wtr, "dev");
 694	jsonw_start_object(json_wtr);
 695	jsonw_uint_field(json_wtr, "ifindex", ifindex);
 696	jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
 697	jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
 698	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
 699		jsonw_string_field(json_wtr, "ifname", name);
 700	jsonw_end_object(json_wtr);
 701}
 702
 703int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
 704{
 705	char *endptr;
 706
 707	NEXT_ARGP();
 708
 709	if (*val) {
 710		p_err("%s already specified", what);
 711		return -1;
 712	}
 713
 714	*val = strtoul(**argv, &endptr, 0);
 715	if (*endptr) {
 716		p_err("can't parse %s as %s", **argv, what);
 717		return -1;
 718	}
 719	NEXT_ARGP();
 720
 721	return 0;
 722}
 723
 724int __printf(2, 0)
 725print_all_levels(__maybe_unused enum libbpf_print_level level,
 726		 const char *format, va_list args)
 727{
 728	return vfprintf(stderr, format, args);
 729}
 730
 731static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
 732{
 733	char prog_name[MAX_PROG_FULL_NAME];
 734	unsigned int id = 0;
 735	int fd, nb_fds = 0;
 736	void *tmp;
 737	int err;
 738
 739	while (true) {
 740		struct bpf_prog_info info = {};
 741		__u32 len = sizeof(info);
 742
 743		err = bpf_prog_get_next_id(id, &id);
 744		if (err) {
 745			if (errno != ENOENT) {
 746				p_err("%s", strerror(errno));
 747				goto err_close_fds;
 748			}
 749			return nb_fds;
 750		}
 751
 752		fd = bpf_prog_get_fd_by_id(id);
 753		if (fd < 0) {
 754			p_err("can't get prog by id (%u): %s",
 755			      id, strerror(errno));
 756			goto err_close_fds;
 757		}
 758
 759		err = bpf_obj_get_info_by_fd(fd, &info, &len);
 760		if (err) {
 761			p_err("can't get prog info (%u): %s",
 762			      id, strerror(errno));
 763			goto err_close_fd;
 764		}
 765
 766		if (tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) {
 
 767			close(fd);
 768			continue;
 769		}
 770
 771		if (!tag) {
 772			get_prog_full_name(&info, fd, prog_name,
 773					   sizeof(prog_name));
 774			if (strncmp(nametag, prog_name, sizeof(prog_name))) {
 775				close(fd);
 776				continue;
 777			}
 778		}
 779
 780		if (nb_fds > 0) {
 781			tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
 782			if (!tmp) {
 783				p_err("failed to realloc");
 784				goto err_close_fd;
 785			}
 786			*fds = tmp;
 787		}
 788		(*fds)[nb_fds++] = fd;
 789	}
 790
 791err_close_fd:
 792	close(fd);
 793err_close_fds:
 794	while (--nb_fds >= 0)
 795		close((*fds)[nb_fds]);
 796	return -1;
 797}
 798
 799int prog_parse_fds(int *argc, char ***argv, int **fds)
 800{
 801	if (is_prefix(**argv, "id")) {
 802		unsigned int id;
 803		char *endptr;
 804
 805		NEXT_ARGP();
 806
 807		id = strtoul(**argv, &endptr, 0);
 808		if (*endptr) {
 809			p_err("can't parse %s as ID", **argv);
 810			return -1;
 811		}
 812		NEXT_ARGP();
 813
 814		(*fds)[0] = bpf_prog_get_fd_by_id(id);
 815		if ((*fds)[0] < 0) {
 816			p_err("get by id (%u): %s", id, strerror(errno));
 817			return -1;
 818		}
 819		return 1;
 820	} else if (is_prefix(**argv, "tag")) {
 821		unsigned char tag[BPF_TAG_SIZE];
 822
 823		NEXT_ARGP();
 824
 825		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
 826			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
 827		    != BPF_TAG_SIZE) {
 828			p_err("can't parse tag");
 829			return -1;
 830		}
 831		NEXT_ARGP();
 832
 833		return prog_fd_by_nametag(tag, fds, true);
 834	} else if (is_prefix(**argv, "name")) {
 835		char *name;
 836
 837		NEXT_ARGP();
 838
 839		name = **argv;
 840		if (strlen(name) > MAX_PROG_FULL_NAME - 1) {
 841			p_err("can't parse name");
 842			return -1;
 843		}
 844		NEXT_ARGP();
 845
 846		return prog_fd_by_nametag(name, fds, false);
 847	} else if (is_prefix(**argv, "pinned")) {
 848		char *path;
 849
 850		NEXT_ARGP();
 851
 852		path = **argv;
 853		NEXT_ARGP();
 854
 855		(*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
 856		if ((*fds)[0] < 0)
 857			return -1;
 858		return 1;
 859	}
 860
 861	p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv);
 862	return -1;
 863}
 864
 865int prog_parse_fd(int *argc, char ***argv)
 866{
 867	int *fds = NULL;
 868	int nb_fds, fd;
 869
 870	fds = malloc(sizeof(int));
 871	if (!fds) {
 872		p_err("mem alloc failed");
 873		return -1;
 874	}
 875	nb_fds = prog_parse_fds(argc, argv, &fds);
 876	if (nb_fds != 1) {
 877		if (nb_fds > 1) {
 878			p_err("several programs match this handle");
 879			while (nb_fds--)
 880				close(fds[nb_fds]);
 881		}
 882		fd = -1;
 883		goto exit_free;
 884	}
 885
 886	fd = fds[0];
 887exit_free:
 888	free(fds);
 889	return fd;
 890}
 891
 892static int map_fd_by_name(char *name, int **fds)
 893{
 894	unsigned int id = 0;
 895	int fd, nb_fds = 0;
 896	void *tmp;
 897	int err;
 898
 899	while (true) {
 900		struct bpf_map_info info = {};
 901		__u32 len = sizeof(info);
 902
 903		err = bpf_map_get_next_id(id, &id);
 904		if (err) {
 905			if (errno != ENOENT) {
 906				p_err("%s", strerror(errno));
 907				goto err_close_fds;
 908			}
 909			return nb_fds;
 910		}
 911
 912		fd = bpf_map_get_fd_by_id(id);
 913		if (fd < 0) {
 914			p_err("can't get map by id (%u): %s",
 915			      id, strerror(errno));
 916			goto err_close_fds;
 917		}
 918
 919		err = bpf_obj_get_info_by_fd(fd, &info, &len);
 920		if (err) {
 921			p_err("can't get map info (%u): %s",
 922			      id, strerror(errno));
 923			goto err_close_fd;
 924		}
 925
 926		if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) {
 927			close(fd);
 928			continue;
 929		}
 930
 931		if (nb_fds > 0) {
 932			tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
 933			if (!tmp) {
 934				p_err("failed to realloc");
 935				goto err_close_fd;
 936			}
 937			*fds = tmp;
 938		}
 939		(*fds)[nb_fds++] = fd;
 940	}
 941
 942err_close_fd:
 943	close(fd);
 944err_close_fds:
 945	while (--nb_fds >= 0)
 946		close((*fds)[nb_fds]);
 947	return -1;
 948}
 949
 950int map_parse_fds(int *argc, char ***argv, int **fds)
 951{
 952	if (is_prefix(**argv, "id")) {
 953		unsigned int id;
 954		char *endptr;
 955
 956		NEXT_ARGP();
 957
 958		id = strtoul(**argv, &endptr, 0);
 959		if (*endptr) {
 960			p_err("can't parse %s as ID", **argv);
 961			return -1;
 962		}
 963		NEXT_ARGP();
 964
 965		(*fds)[0] = bpf_map_get_fd_by_id(id);
 966		if ((*fds)[0] < 0) {
 967			p_err("get map by id (%u): %s", id, strerror(errno));
 968			return -1;
 969		}
 970		return 1;
 971	} else if (is_prefix(**argv, "name")) {
 972		char *name;
 973
 974		NEXT_ARGP();
 975
 976		name = **argv;
 977		if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
 978			p_err("can't parse name");
 979			return -1;
 980		}
 981		NEXT_ARGP();
 982
 983		return map_fd_by_name(name, fds);
 984	} else if (is_prefix(**argv, "pinned")) {
 985		char *path;
 986
 987		NEXT_ARGP();
 988
 989		path = **argv;
 990		NEXT_ARGP();
 991
 992		(*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP);
 993		if ((*fds)[0] < 0)
 994			return -1;
 995		return 1;
 996	}
 997
 998	p_err("expected 'id', 'name' or 'pinned', got: '%s'?", **argv);
 999	return -1;
1000}
1001
1002int map_parse_fd(int *argc, char ***argv)
1003{
1004	int *fds = NULL;
1005	int nb_fds, fd;
1006
1007	fds = malloc(sizeof(int));
1008	if (!fds) {
1009		p_err("mem alloc failed");
1010		return -1;
1011	}
1012	nb_fds = map_parse_fds(argc, argv, &fds);
1013	if (nb_fds != 1) {
1014		if (nb_fds > 1) {
1015			p_err("several maps match this handle");
1016			while (nb_fds--)
1017				close(fds[nb_fds]);
1018		}
1019		fd = -1;
1020		goto exit_free;
1021	}
1022
1023	fd = fds[0];
1024exit_free:
1025	free(fds);
1026	return fd;
1027}
1028
1029int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
1030{
1031	int err;
1032	int fd;
1033
1034	fd = map_parse_fd(argc, argv);
1035	if (fd < 0)
1036		return -1;
1037
1038	err = bpf_obj_get_info_by_fd(fd, info, info_len);
1039	if (err) {
1040		p_err("can't get map info: %s", strerror(errno));
1041		close(fd);
1042		return err;
1043	}
1044
1045	return fd;
1046}
1047
1048size_t hash_fn_for_key_as_id(long key, void *ctx)
1049{
1050	return key;
1051}
1052
1053bool equal_fn_for_key_as_id(long k1, long k2, void *ctx)
1054{
1055	return k1 == k2;
1056}
1057
1058const char *bpf_attach_type_input_str(enum bpf_attach_type t)
1059{
1060	switch (t) {
1061	case BPF_CGROUP_INET_INGRESS:		return "ingress";
1062	case BPF_CGROUP_INET_EGRESS:		return "egress";
1063	case BPF_CGROUP_INET_SOCK_CREATE:	return "sock_create";
1064	case BPF_CGROUP_INET_SOCK_RELEASE:	return "sock_release";
1065	case BPF_CGROUP_SOCK_OPS:		return "sock_ops";
1066	case BPF_CGROUP_DEVICE:			return "device";
1067	case BPF_CGROUP_INET4_BIND:		return "bind4";
1068	case BPF_CGROUP_INET6_BIND:		return "bind6";
1069	case BPF_CGROUP_INET4_CONNECT:		return "connect4";
1070	case BPF_CGROUP_INET6_CONNECT:		return "connect6";
1071	case BPF_CGROUP_INET4_POST_BIND:	return "post_bind4";
1072	case BPF_CGROUP_INET6_POST_BIND:	return "post_bind6";
1073	case BPF_CGROUP_INET4_GETPEERNAME:	return "getpeername4";
1074	case BPF_CGROUP_INET6_GETPEERNAME:	return "getpeername6";
1075	case BPF_CGROUP_INET4_GETSOCKNAME:	return "getsockname4";
1076	case BPF_CGROUP_INET6_GETSOCKNAME:	return "getsockname6";
1077	case BPF_CGROUP_UDP4_SENDMSG:		return "sendmsg4";
1078	case BPF_CGROUP_UDP6_SENDMSG:		return "sendmsg6";
1079	case BPF_CGROUP_SYSCTL:			return "sysctl";
1080	case BPF_CGROUP_UDP4_RECVMSG:		return "recvmsg4";
1081	case BPF_CGROUP_UDP6_RECVMSG:		return "recvmsg6";
1082	case BPF_CGROUP_GETSOCKOPT:		return "getsockopt";
1083	case BPF_CGROUP_SETSOCKOPT:		return "setsockopt";
1084	case BPF_TRACE_RAW_TP:			return "raw_tp";
1085	case BPF_TRACE_FENTRY:			return "fentry";
1086	case BPF_TRACE_FEXIT:			return "fexit";
1087	case BPF_MODIFY_RETURN:			return "mod_ret";
1088	case BPF_SK_REUSEPORT_SELECT:		return "sk_skb_reuseport_select";
1089	case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:	return "sk_skb_reuseport_select_or_migrate";
1090	default:	return libbpf_bpf_attach_type_str(t);
1091	}
1092}
v5.9
  1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
  3
 
  4#define _GNU_SOURCE
 
  5#include <ctype.h>
  6#include <errno.h>
  7#include <fcntl.h>
  8#include <ftw.h>
  9#include <libgen.h>
 10#include <mntent.h>
 11#include <stdbool.h>
 12#include <stdio.h>
 13#include <stdlib.h>
 14#include <string.h>
 15#include <unistd.h>
 16#include <linux/limits.h>
 17#include <linux/magic.h>
 18#include <net/if.h>
 19#include <sys/mount.h>
 20#include <sys/resource.h>
 21#include <sys/stat.h>
 22#include <sys/vfs.h>
 23
 
 
 
 
 
 24#include <bpf/bpf.h>
 
 25#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
 
 26
 27#include "main.h"
 28
 29#ifndef BPF_FS_MAGIC
 30#define BPF_FS_MAGIC		0xcafe4a11
 31#endif
 32
 33const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = {
 34	[BPF_CGROUP_INET_INGRESS]	= "ingress",
 35	[BPF_CGROUP_INET_EGRESS]	= "egress",
 36	[BPF_CGROUP_INET_SOCK_CREATE]	= "sock_create",
 37	[BPF_CGROUP_INET_SOCK_RELEASE]	= "sock_release",
 38	[BPF_CGROUP_SOCK_OPS]		= "sock_ops",
 39	[BPF_CGROUP_DEVICE]		= "device",
 40	[BPF_CGROUP_INET4_BIND]		= "bind4",
 41	[BPF_CGROUP_INET6_BIND]		= "bind6",
 42	[BPF_CGROUP_INET4_CONNECT]	= "connect4",
 43	[BPF_CGROUP_INET6_CONNECT]	= "connect6",
 44	[BPF_CGROUP_INET4_POST_BIND]	= "post_bind4",
 45	[BPF_CGROUP_INET6_POST_BIND]	= "post_bind6",
 46	[BPF_CGROUP_INET4_GETPEERNAME]	= "getpeername4",
 47	[BPF_CGROUP_INET6_GETPEERNAME]	= "getpeername6",
 48	[BPF_CGROUP_INET4_GETSOCKNAME]	= "getsockname4",
 49	[BPF_CGROUP_INET6_GETSOCKNAME]	= "getsockname6",
 50	[BPF_CGROUP_UDP4_SENDMSG]	= "sendmsg4",
 51	[BPF_CGROUP_UDP6_SENDMSG]	= "sendmsg6",
 52	[BPF_CGROUP_SYSCTL]		= "sysctl",
 53	[BPF_CGROUP_UDP4_RECVMSG]	= "recvmsg4",
 54	[BPF_CGROUP_UDP6_RECVMSG]	= "recvmsg6",
 55	[BPF_CGROUP_GETSOCKOPT]		= "getsockopt",
 56	[BPF_CGROUP_SETSOCKOPT]		= "setsockopt",
 57
 58	[BPF_SK_SKB_STREAM_PARSER]	= "sk_skb_stream_parser",
 59	[BPF_SK_SKB_STREAM_VERDICT]	= "sk_skb_stream_verdict",
 60	[BPF_SK_MSG_VERDICT]		= "sk_msg_verdict",
 61	[BPF_LIRC_MODE2]		= "lirc_mode2",
 62	[BPF_FLOW_DISSECTOR]		= "flow_dissector",
 63	[BPF_TRACE_RAW_TP]		= "raw_tp",
 64	[BPF_TRACE_FENTRY]		= "fentry",
 65	[BPF_TRACE_FEXIT]		= "fexit",
 66	[BPF_MODIFY_RETURN]		= "mod_ret",
 67	[BPF_LSM_MAC]			= "lsm_mac",
 68	[BPF_SK_LOOKUP]			= "sk_lookup",
 69};
 70
 71void p_err(const char *fmt, ...)
 72{
 73	va_list ap;
 74
 75	va_start(ap, fmt);
 76	if (json_output) {
 77		jsonw_start_object(json_wtr);
 78		jsonw_name(json_wtr, "error");
 79		jsonw_vprintf_enquote(json_wtr, fmt, ap);
 80		jsonw_end_object(json_wtr);
 81	} else {
 82		fprintf(stderr, "Error: ");
 83		vfprintf(stderr, fmt, ap);
 84		fprintf(stderr, "\n");
 85	}
 86	va_end(ap);
 87}
 88
 89void p_info(const char *fmt, ...)
 90{
 91	va_list ap;
 92
 93	if (json_output)
 94		return;
 95
 96	va_start(ap, fmt);
 97	vfprintf(stderr, fmt, ap);
 98	fprintf(stderr, "\n");
 99	va_end(ap);
100}
101
102static bool is_bpffs(char *path)
103{
104	struct statfs st_fs;
105
106	if (statfs(path, &st_fs) < 0)
107		return false;
108
109	return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
110}
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112void set_max_rlimit(void)
113{
114	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
115
116	setrlimit(RLIMIT_MEMLOCK, &rinf);
 
117}
118
119static int
120mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
121{
122	bool bind_done = false;
123
124	while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
125		if (errno != EINVAL || bind_done) {
126			snprintf(buff, bufflen,
127				 "mount --make-private %s failed: %s",
128				 target, strerror(errno));
129			return -1;
130		}
131
132		if (mount(target, target, "none", MS_BIND, NULL)) {
133			snprintf(buff, bufflen,
134				 "mount --bind %s %s failed: %s",
135				 target, target, strerror(errno));
136			return -1;
137		}
138
139		bind_done = true;
140	}
141
142	if (mount(type, target, type, 0, "mode=0700")) {
143		snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
144			 type, type, target, strerror(errno));
145		return -1;
146	}
147
148	return 0;
149}
150
151int mount_tracefs(const char *target)
152{
153	char err_str[ERR_MAX_LEN];
154	int err;
155
156	err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
157	if (err) {
158		err_str[ERR_MAX_LEN - 1] = '\0';
159		p_err("can't mount tracefs: %s", err_str);
160	}
161
162	return err;
163}
164
165int open_obj_pinned(const char *path, bool quiet)
166{
167	char *pname;
168	int fd = -1;
169
170	pname = strdup(path);
171	if (!pname) {
172		if (!quiet)
173			p_err("mem alloc failed");
174		goto out_ret;
175	}
176
177	fd = bpf_obj_get(pname);
178	if (fd < 0) {
179		if (!quiet)
180			p_err("bpf obj get (%s): %s", pname,
181			      errno == EACCES && !is_bpffs(dirname(pname)) ?
182			    "directory not in bpf file system (bpffs)" :
183			    strerror(errno));
184		goto out_free;
185	}
186
187out_free:
188	free(pname);
189out_ret:
190	return fd;
191}
192
193int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
194{
195	enum bpf_obj_type type;
196	int fd;
197
198	fd = open_obj_pinned(path, false);
199	if (fd < 0)
200		return -1;
201
202	type = get_fd_type(fd);
203	if (type < 0) {
204		close(fd);
205		return type;
206	}
207	if (type != exp_type) {
208		p_err("incorrect object type: %s", get_fd_type_name(type));
209		close(fd);
210		return -1;
211	}
212
213	return fd;
214}
215
216int mount_bpffs_for_pin(const char *name)
217{
218	char err_str[ERR_MAX_LEN];
219	char *file;
220	char *dir;
221	int err = 0;
222
223	file = malloc(strlen(name) + 1);
 
 
 
 
 
224	strcpy(file, name);
225	dir = dirname(file);
226
227	if (is_bpffs(dir))
228		/* nothing to do if already mounted */
229		goto out_free;
230
231	if (block_mount) {
232		p_err("no BPF file system found, not mounting it due to --nomount option");
233		err = -1;
234		goto out_free;
235	}
236
237	err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
238	if (err) {
239		err_str[ERR_MAX_LEN - 1] = '\0';
240		p_err("can't mount BPF file system to pin the object (%s): %s",
241		      name, err_str);
242	}
243
244out_free:
245	free(file);
246	return err;
247}
248
249int do_pin_fd(int fd, const char *name)
250{
251	int err;
252
253	err = mount_bpffs_for_pin(name);
254	if (err)
255		return err;
256
257	err = bpf_obj_pin(fd, name);
258	if (err)
259		p_err("can't pin the object (%s): %s", name, strerror(errno));
260
261	return err;
262}
263
264int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
265{
266	int err;
267	int fd;
268
 
 
 
269	fd = get_fd(&argc, &argv);
270	if (fd < 0)
271		return fd;
272
273	err = do_pin_fd(fd, *argv);
274
275	close(fd);
276	return err;
277}
278
279const char *get_fd_type_name(enum bpf_obj_type type)
280{
281	static const char * const names[] = {
282		[BPF_OBJ_UNKNOWN]	= "unknown",
283		[BPF_OBJ_PROG]		= "prog",
284		[BPF_OBJ_MAP]		= "map",
 
285	};
286
287	if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
288		return names[BPF_OBJ_UNKNOWN];
289
290	return names[type];
291}
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293int get_fd_type(int fd)
294{
295	char path[PATH_MAX];
296	char buf[512];
297	ssize_t n;
298
299	snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
300
301	n = readlink(path, buf, sizeof(buf));
302	if (n < 0) {
303		p_err("can't read link type: %s", strerror(errno));
304		return -1;
305	}
306	if (n == sizeof(path)) {
307		p_err("can't read link type: path too long!");
308		return -1;
309	}
310
311	if (strstr(buf, "bpf-map"))
312		return BPF_OBJ_MAP;
313	else if (strstr(buf, "bpf-prog"))
314		return BPF_OBJ_PROG;
315	else if (strstr(buf, "bpf-link"))
316		return BPF_OBJ_LINK;
317
318	return BPF_OBJ_UNKNOWN;
319}
320
321char *get_fdinfo(int fd, const char *key)
322{
323	char path[PATH_MAX];
324	char *line = NULL;
325	size_t line_n = 0;
326	ssize_t n;
327	FILE *fdi;
328
329	snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
330
331	fdi = fopen(path, "r");
332	if (!fdi)
333		return NULL;
334
335	while ((n = getline(&line, &line_n, fdi)) > 0) {
336		char *value;
337		int len;
338
339		if (!strstr(line, key))
340			continue;
341
342		fclose(fdi);
343
344		value = strchr(line, '\t');
345		if (!value || !value[1]) {
346			free(line);
347			return NULL;
348		}
349		value++;
350
351		len = strlen(value);
352		memmove(line, value, len);
353		line[len - 1] = '\0';
354
355		return line;
356	}
357
358	free(line);
359	fclose(fdi);
360	return NULL;
361}
362
363void print_data_json(uint8_t *data, size_t len)
364{
365	unsigned int i;
366
367	jsonw_start_array(json_wtr);
368	for (i = 0; i < len; i++)
369		jsonw_printf(json_wtr, "%d", data[i]);
370	jsonw_end_array(json_wtr);
371}
372
373void print_hex_data_json(uint8_t *data, size_t len)
374{
375	unsigned int i;
376
377	jsonw_start_array(json_wtr);
378	for (i = 0; i < len; i++)
379		jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
380	jsonw_end_array(json_wtr);
381}
382
383/* extra params for nftw cb */
384static struct pinned_obj_table *build_fn_table;
385static enum bpf_obj_type build_fn_type;
386
387static int do_build_table_cb(const char *fpath, const struct stat *sb,
388			     int typeflag, struct FTW *ftwbuf)
389{
390	struct bpf_prog_info pinned_info;
391	__u32 len = sizeof(pinned_info);
392	struct pinned_obj *obj_node;
393	enum bpf_obj_type objtype;
394	int fd, err = 0;
 
395
396	if (typeflag != FTW_F)
397		goto out_ret;
398
399	fd = open_obj_pinned(fpath, true);
400	if (fd < 0)
401		goto out_ret;
402
403	objtype = get_fd_type(fd);
404	if (objtype != build_fn_type)
405		goto out_close;
406
407	memset(&pinned_info, 0, sizeof(pinned_info));
408	if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
409		goto out_close;
410
411	obj_node = calloc(1, sizeof(*obj_node));
412	if (!obj_node) {
413		err = -1;
414		goto out_close;
415	}
416
417	obj_node->id = pinned_info.id;
418	obj_node->path = strdup(fpath);
419	if (!obj_node->path) {
420		err = -1;
421		free(obj_node);
422		goto out_close;
423	}
424
425	hash_add(build_fn_table->table, &obj_node->hash, obj_node->id);
426out_close:
427	close(fd);
428out_ret:
429	return err;
430}
431
432int build_pinned_obj_table(struct pinned_obj_table *tab,
433			   enum bpf_obj_type type)
434{
435	struct mntent *mntent = NULL;
436	FILE *mntfile = NULL;
437	int flags = FTW_PHYS;
438	int nopenfd = 16;
439	int err = 0;
440
441	mntfile = setmntent("/proc/mounts", "r");
442	if (!mntfile)
443		return -1;
444
445	build_fn_table = tab;
446	build_fn_type = type;
447
448	while ((mntent = getmntent(mntfile))) {
449		char *path = mntent->mnt_dir;
450
451		if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
452			continue;
453		err = nftw(path, do_build_table_cb, nopenfd, flags);
454		if (err)
455			break;
456	}
457	fclose(mntfile);
458	return err;
459}
460
461void delete_pinned_obj_table(struct pinned_obj_table *tab)
462{
463	struct pinned_obj *obj;
464	struct hlist_node *tmp;
465	unsigned int bkt;
466
467	hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
468		hash_del(&obj->hash);
469		free(obj->path);
470		free(obj);
471	}
 
472}
473
474unsigned int get_page_size(void)
475{
476	static int result;
477
478	if (!result)
479		result = getpagesize();
480	return result;
481}
482
483unsigned int get_possible_cpus(void)
484{
485	int cpus = libbpf_num_possible_cpus();
486
487	if (cpus < 0) {
488		p_err("Can't get # of possible cpus: %s", strerror(-cpus));
489		exit(-1);
490	}
491	return cpus;
492}
493
494static char *
495ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
496{
497	struct stat st;
498	int err;
499
500	err = stat("/proc/self/ns/net", &st);
501	if (err) {
502		p_err("Can't stat /proc/self: %s", strerror(errno));
503		return NULL;
504	}
505
506	if (st.st_dev != ns_dev || st.st_ino != ns_ino)
507		return NULL;
508
509	return if_indextoname(ifindex, buf);
510}
511
512static int read_sysfs_hex_int(char *path)
513{
514	char vendor_id_buf[8];
515	int len;
516	int fd;
517
518	fd = open(path, O_RDONLY);
519	if (fd < 0) {
520		p_err("Can't open %s: %s", path, strerror(errno));
521		return -1;
522	}
523
524	len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
525	close(fd);
526	if (len < 0) {
527		p_err("Can't read %s: %s", path, strerror(errno));
528		return -1;
529	}
530	if (len >= (int)sizeof(vendor_id_buf)) {
531		p_err("Value in %s too long", path);
532		return -1;
533	}
534
535	vendor_id_buf[len] = 0;
536
537	return strtol(vendor_id_buf, NULL, 0);
538}
539
540static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
541{
542	char full_path[64];
543
544	snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
545		 devname, entry_name);
546
547	return read_sysfs_hex_int(full_path);
548}
549
550const char *
551ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
552		      const char **opt)
553{
 
554	char devname[IF_NAMESIZE];
555	int vendor_id;
556	int device_id;
557
558	if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
559		p_err("Can't get net device name for ifindex %d: %s", ifindex,
560		      strerror(errno));
561		return NULL;
562	}
563
564	vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
565	if (vendor_id < 0) {
566		p_err("Can't get device vendor id for %s", devname);
567		return NULL;
568	}
569
570	switch (vendor_id) {
 
571	case 0x19ee:
572		device_id = read_sysfs_netdev_hex_int(devname, "device");
573		if (device_id != 0x4000 &&
574		    device_id != 0x6000 &&
575		    device_id != 0x6003)
576			p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
577		*opt = "ctx4";
578		return "NFP-6xxx";
 
 
579	default:
580		p_err("Can't get bfd arch name for device vendor id 0x%04x",
581		      vendor_id);
582		return NULL;
583	}
584}
585
586void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
587{
588	char name[IF_NAMESIZE];
589
590	if (!ifindex)
591		return;
592
593	printf("  offloaded_to ");
594	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
595		printf("%s", name);
596	else
597		printf("ifindex %u ns_dev %llu ns_ino %llu",
598		       ifindex, ns_dev, ns_inode);
599}
600
601void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
602{
603	char name[IF_NAMESIZE];
604
605	if (!ifindex)
606		return;
607
608	jsonw_name(json_wtr, "dev");
609	jsonw_start_object(json_wtr);
610	jsonw_uint_field(json_wtr, "ifindex", ifindex);
611	jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
612	jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
613	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
614		jsonw_string_field(json_wtr, "ifname", name);
615	jsonw_end_object(json_wtr);
616}
617
618int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
619{
620	char *endptr;
621
622	NEXT_ARGP();
623
624	if (*val) {
625		p_err("%s already specified", what);
626		return -1;
627	}
628
629	*val = strtoul(**argv, &endptr, 0);
630	if (*endptr) {
631		p_err("can't parse %s as %s", **argv, what);
632		return -1;
633	}
634	NEXT_ARGP();
635
636	return 0;
637}
638
639int __printf(2, 0)
640print_all_levels(__maybe_unused enum libbpf_print_level level,
641		 const char *format, va_list args)
642{
643	return vfprintf(stderr, format, args);
644}
645
646static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
647{
 
648	unsigned int id = 0;
649	int fd, nb_fds = 0;
650	void *tmp;
651	int err;
652
653	while (true) {
654		struct bpf_prog_info info = {};
655		__u32 len = sizeof(info);
656
657		err = bpf_prog_get_next_id(id, &id);
658		if (err) {
659			if (errno != ENOENT) {
660				p_err("%s", strerror(errno));
661				goto err_close_fds;
662			}
663			return nb_fds;
664		}
665
666		fd = bpf_prog_get_fd_by_id(id);
667		if (fd < 0) {
668			p_err("can't get prog by id (%u): %s",
669			      id, strerror(errno));
670			goto err_close_fds;
671		}
672
673		err = bpf_obj_get_info_by_fd(fd, &info, &len);
674		if (err) {
675			p_err("can't get prog info (%u): %s",
676			      id, strerror(errno));
677			goto err_close_fd;
678		}
679
680		if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) ||
681		    (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) {
682			close(fd);
683			continue;
684		}
685
 
 
 
 
 
 
 
 
 
686		if (nb_fds > 0) {
687			tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
688			if (!tmp) {
689				p_err("failed to realloc");
690				goto err_close_fd;
691			}
692			*fds = tmp;
693		}
694		(*fds)[nb_fds++] = fd;
695	}
696
697err_close_fd:
698	close(fd);
699err_close_fds:
700	while (--nb_fds >= 0)
701		close((*fds)[nb_fds]);
702	return -1;
703}
704
705int prog_parse_fds(int *argc, char ***argv, int **fds)
706{
707	if (is_prefix(**argv, "id")) {
708		unsigned int id;
709		char *endptr;
710
711		NEXT_ARGP();
712
713		id = strtoul(**argv, &endptr, 0);
714		if (*endptr) {
715			p_err("can't parse %s as ID", **argv);
716			return -1;
717		}
718		NEXT_ARGP();
719
720		(*fds)[0] = bpf_prog_get_fd_by_id(id);
721		if ((*fds)[0] < 0) {
722			p_err("get by id (%u): %s", id, strerror(errno));
723			return -1;
724		}
725		return 1;
726	} else if (is_prefix(**argv, "tag")) {
727		unsigned char tag[BPF_TAG_SIZE];
728
729		NEXT_ARGP();
730
731		if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
732			   tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
733		    != BPF_TAG_SIZE) {
734			p_err("can't parse tag");
735			return -1;
736		}
737		NEXT_ARGP();
738
739		return prog_fd_by_nametag(tag, fds, true);
740	} else if (is_prefix(**argv, "name")) {
741		char *name;
742
743		NEXT_ARGP();
744
745		name = **argv;
746		if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
747			p_err("can't parse name");
748			return -1;
749		}
750		NEXT_ARGP();
751
752		return prog_fd_by_nametag(name, fds, false);
753	} else if (is_prefix(**argv, "pinned")) {
754		char *path;
755
756		NEXT_ARGP();
757
758		path = **argv;
759		NEXT_ARGP();
760
761		(*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
762		if ((*fds)[0] < 0)
763			return -1;
764		return 1;
765	}
766
767	p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv);
768	return -1;
769}
770
771int prog_parse_fd(int *argc, char ***argv)
772{
773	int *fds = NULL;
774	int nb_fds, fd;
775
776	fds = malloc(sizeof(int));
777	if (!fds) {
778		p_err("mem alloc failed");
779		return -1;
780	}
781	nb_fds = prog_parse_fds(argc, argv, &fds);
782	if (nb_fds != 1) {
783		if (nb_fds > 1) {
784			p_err("several programs match this handle");
785			while (nb_fds--)
786				close(fds[nb_fds]);
787		}
788		fd = -1;
789		goto exit_free;
790	}
791
792	fd = fds[0];
793exit_free:
794	free(fds);
795	return fd;
796}
797
798static int map_fd_by_name(char *name, int **fds)
799{
800	unsigned int id = 0;
801	int fd, nb_fds = 0;
802	void *tmp;
803	int err;
804
805	while (true) {
806		struct bpf_map_info info = {};
807		__u32 len = sizeof(info);
808
809		err = bpf_map_get_next_id(id, &id);
810		if (err) {
811			if (errno != ENOENT) {
812				p_err("%s", strerror(errno));
813				goto err_close_fds;
814			}
815			return nb_fds;
816		}
817
818		fd = bpf_map_get_fd_by_id(id);
819		if (fd < 0) {
820			p_err("can't get map by id (%u): %s",
821			      id, strerror(errno));
822			goto err_close_fds;
823		}
824
825		err = bpf_obj_get_info_by_fd(fd, &info, &len);
826		if (err) {
827			p_err("can't get map info (%u): %s",
828			      id, strerror(errno));
829			goto err_close_fd;
830		}
831
832		if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) {
833			close(fd);
834			continue;
835		}
836
837		if (nb_fds > 0) {
838			tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
839			if (!tmp) {
840				p_err("failed to realloc");
841				goto err_close_fd;
842			}
843			*fds = tmp;
844		}
845		(*fds)[nb_fds++] = fd;
846	}
847
848err_close_fd:
849	close(fd);
850err_close_fds:
851	while (--nb_fds >= 0)
852		close((*fds)[nb_fds]);
853	return -1;
854}
855
856int map_parse_fds(int *argc, char ***argv, int **fds)
857{
858	if (is_prefix(**argv, "id")) {
859		unsigned int id;
860		char *endptr;
861
862		NEXT_ARGP();
863
864		id = strtoul(**argv, &endptr, 0);
865		if (*endptr) {
866			p_err("can't parse %s as ID", **argv);
867			return -1;
868		}
869		NEXT_ARGP();
870
871		(*fds)[0] = bpf_map_get_fd_by_id(id);
872		if ((*fds)[0] < 0) {
873			p_err("get map by id (%u): %s", id, strerror(errno));
874			return -1;
875		}
876		return 1;
877	} else if (is_prefix(**argv, "name")) {
878		char *name;
879
880		NEXT_ARGP();
881
882		name = **argv;
883		if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
884			p_err("can't parse name");
885			return -1;
886		}
887		NEXT_ARGP();
888
889		return map_fd_by_name(name, fds);
890	} else if (is_prefix(**argv, "pinned")) {
891		char *path;
892
893		NEXT_ARGP();
894
895		path = **argv;
896		NEXT_ARGP();
897
898		(*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP);
899		if ((*fds)[0] < 0)
900			return -1;
901		return 1;
902	}
903
904	p_err("expected 'id', 'name' or 'pinned', got: '%s'?", **argv);
905	return -1;
906}
907
908int map_parse_fd(int *argc, char ***argv)
909{
910	int *fds = NULL;
911	int nb_fds, fd;
912
913	fds = malloc(sizeof(int));
914	if (!fds) {
915		p_err("mem alloc failed");
916		return -1;
917	}
918	nb_fds = map_parse_fds(argc, argv, &fds);
919	if (nb_fds != 1) {
920		if (nb_fds > 1) {
921			p_err("several maps match this handle");
922			while (nb_fds--)
923				close(fds[nb_fds]);
924		}
925		fd = -1;
926		goto exit_free;
927	}
928
929	fd = fds[0];
930exit_free:
931	free(fds);
932	return fd;
933}
934
935int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
936{
937	int err;
938	int fd;
939
940	fd = map_parse_fd(argc, argv);
941	if (fd < 0)
942		return -1;
943
944	err = bpf_obj_get_info_by_fd(fd, info, info_len);
945	if (err) {
946		p_err("can't get map info: %s", strerror(errno));
947		close(fd);
948		return err;
949	}
950
951	return fd;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
952}