Linux 5.1.15
[linux/fpc-iii.git] / tools / bpf / bpftool / common.c
blobf7261fad45c19cd7859d8d68d87aa26eb71ff128
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
4 #include <ctype.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <fts.h>
8 #include <libgen.h>
9 #include <mntent.h>
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <linux/limits.h>
16 #include <linux/magic.h>
17 #include <net/if.h>
18 #include <sys/mount.h>
19 #include <sys/resource.h>
20 #include <sys/stat.h>
21 #include <sys/vfs.h>
23 #include <bpf.h>
25 #include "main.h"
27 #ifndef BPF_FS_MAGIC
28 #define BPF_FS_MAGIC 0xcafe4a11
29 #endif
31 void __printf(1, 2) p_err(const char *fmt, ...)
33 va_list ap;
35 va_start(ap, fmt);
36 if (json_output) {
37 jsonw_start_object(json_wtr);
38 jsonw_name(json_wtr, "error");
39 jsonw_vprintf_enquote(json_wtr, fmt, ap);
40 jsonw_end_object(json_wtr);
41 } else {
42 fprintf(stderr, "Error: ");
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
46 va_end(ap);
49 void __printf(1, 2) p_info(const char *fmt, ...)
51 va_list ap;
53 if (json_output)
54 return;
56 va_start(ap, fmt);
57 vfprintf(stderr, fmt, ap);
58 fprintf(stderr, "\n");
59 va_end(ap);
62 static bool is_bpffs(char *path)
64 struct statfs st_fs;
66 if (statfs(path, &st_fs) < 0)
67 return false;
69 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
72 void set_max_rlimit(void)
74 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
76 setrlimit(RLIMIT_MEMLOCK, &rinf);
79 static int
80 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
82 bool bind_done = false;
84 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
85 if (errno != EINVAL || bind_done) {
86 snprintf(buff, bufflen,
87 "mount --make-private %s failed: %s",
88 target, strerror(errno));
89 return -1;
92 if (mount(target, target, "none", MS_BIND, NULL)) {
93 snprintf(buff, bufflen,
94 "mount --bind %s %s failed: %s",
95 target, target, strerror(errno));
96 return -1;
99 bind_done = true;
102 if (mount(type, target, type, 0, "mode=0700")) {
103 snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
104 type, type, target, strerror(errno));
105 return -1;
108 return 0;
111 int mount_tracefs(const char *target)
113 char err_str[ERR_MAX_LEN];
114 int err;
116 err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
117 if (err) {
118 err_str[ERR_MAX_LEN - 1] = '\0';
119 p_err("can't mount tracefs: %s", err_str);
122 return err;
125 int open_obj_pinned(char *path, bool quiet)
127 int fd;
129 fd = bpf_obj_get(path);
130 if (fd < 0) {
131 if (!quiet)
132 p_err("bpf obj get (%s): %s", path,
133 errno == EACCES && !is_bpffs(dirname(path)) ?
134 "directory not in bpf file system (bpffs)" :
135 strerror(errno));
136 return -1;
139 return fd;
142 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
144 enum bpf_obj_type type;
145 int fd;
147 fd = open_obj_pinned(path, false);
148 if (fd < 0)
149 return -1;
151 type = get_fd_type(fd);
152 if (type < 0) {
153 close(fd);
154 return type;
156 if (type != exp_type) {
157 p_err("incorrect object type: %s", get_fd_type_name(type));
158 close(fd);
159 return -1;
162 return fd;
165 int mount_bpffs_for_pin(const char *name)
167 char err_str[ERR_MAX_LEN];
168 char *file;
169 char *dir;
170 int err = 0;
172 file = malloc(strlen(name) + 1);
173 strcpy(file, name);
174 dir = dirname(file);
176 if (is_bpffs(dir))
177 /* nothing to do if already mounted */
178 goto out_free;
180 if (block_mount) {
181 p_err("no BPF file system found, not mounting it due to --nomount option");
182 err = -1;
183 goto out_free;
186 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
187 if (err) {
188 err_str[ERR_MAX_LEN - 1] = '\0';
189 p_err("can't mount BPF file system to pin the object (%s): %s",
190 name, err_str);
193 out_free:
194 free(file);
195 return err;
198 int do_pin_fd(int fd, const char *name)
200 int err;
202 err = mount_bpffs_for_pin(name);
203 if (err)
204 return err;
206 return bpf_obj_pin(fd, name);
209 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
211 unsigned int id;
212 char *endptr;
213 int err;
214 int fd;
216 if (argc < 3) {
217 p_err("too few arguments, id ID and FILE path is required");
218 return -1;
219 } else if (argc > 3) {
220 p_err("too many arguments");
221 return -1;
224 if (!is_prefix(*argv, "id")) {
225 p_err("expected 'id' got %s", *argv);
226 return -1;
228 NEXT_ARG();
230 id = strtoul(*argv, &endptr, 0);
231 if (*endptr) {
232 p_err("can't parse %s as ID", *argv);
233 return -1;
235 NEXT_ARG();
237 fd = get_fd_by_id(id);
238 if (fd < 0) {
239 p_err("can't get prog by id (%u): %s", id, strerror(errno));
240 return -1;
243 err = do_pin_fd(fd, *argv);
245 close(fd);
246 return err;
249 const char *get_fd_type_name(enum bpf_obj_type type)
251 static const char * const names[] = {
252 [BPF_OBJ_UNKNOWN] = "unknown",
253 [BPF_OBJ_PROG] = "prog",
254 [BPF_OBJ_MAP] = "map",
257 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
258 return names[BPF_OBJ_UNKNOWN];
260 return names[type];
263 int get_fd_type(int fd)
265 char path[PATH_MAX];
266 char buf[512];
267 ssize_t n;
269 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
271 n = readlink(path, buf, sizeof(buf));
272 if (n < 0) {
273 p_err("can't read link type: %s", strerror(errno));
274 return -1;
276 if (n == sizeof(path)) {
277 p_err("can't read link type: path too long!");
278 return -1;
281 if (strstr(buf, "bpf-map"))
282 return BPF_OBJ_MAP;
283 else if (strstr(buf, "bpf-prog"))
284 return BPF_OBJ_PROG;
286 return BPF_OBJ_UNKNOWN;
289 char *get_fdinfo(int fd, const char *key)
291 char path[PATH_MAX];
292 char *line = NULL;
293 size_t line_n = 0;
294 ssize_t n;
295 FILE *fdi;
297 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
299 fdi = fopen(path, "r");
300 if (!fdi)
301 return NULL;
303 while ((n = getline(&line, &line_n, fdi)) > 0) {
304 char *value;
305 int len;
307 if (!strstr(line, key))
308 continue;
310 fclose(fdi);
312 value = strchr(line, '\t');
313 if (!value || !value[1]) {
314 free(line);
315 return NULL;
317 value++;
319 len = strlen(value);
320 memmove(line, value, len);
321 line[len - 1] = '\0';
323 return line;
326 free(line);
327 fclose(fdi);
328 return NULL;
331 void print_data_json(uint8_t *data, size_t len)
333 unsigned int i;
335 jsonw_start_array(json_wtr);
336 for (i = 0; i < len; i++)
337 jsonw_printf(json_wtr, "%d", data[i]);
338 jsonw_end_array(json_wtr);
341 void print_hex_data_json(uint8_t *data, size_t len)
343 unsigned int i;
345 jsonw_start_array(json_wtr);
346 for (i = 0; i < len; i++)
347 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
348 jsonw_end_array(json_wtr);
351 int build_pinned_obj_table(struct pinned_obj_table *tab,
352 enum bpf_obj_type type)
354 struct bpf_prog_info pinned_info = {};
355 struct pinned_obj *obj_node = NULL;
356 __u32 len = sizeof(pinned_info);
357 struct mntent *mntent = NULL;
358 enum bpf_obj_type objtype;
359 FILE *mntfile = NULL;
360 FTSENT *ftse = NULL;
361 FTS *fts = NULL;
362 int fd, err;
364 mntfile = setmntent("/proc/mounts", "r");
365 if (!mntfile)
366 return -1;
368 while ((mntent = getmntent(mntfile))) {
369 char *path[] = { mntent->mnt_dir, NULL };
371 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
372 continue;
374 fts = fts_open(path, 0, NULL);
375 if (!fts)
376 continue;
378 while ((ftse = fts_read(fts))) {
379 if (!(ftse->fts_info & FTS_F))
380 continue;
381 fd = open_obj_pinned(ftse->fts_path, true);
382 if (fd < 0)
383 continue;
385 objtype = get_fd_type(fd);
386 if (objtype != type) {
387 close(fd);
388 continue;
390 memset(&pinned_info, 0, sizeof(pinned_info));
391 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
392 if (err) {
393 close(fd);
394 continue;
397 obj_node = malloc(sizeof(*obj_node));
398 if (!obj_node) {
399 close(fd);
400 fts_close(fts);
401 fclose(mntfile);
402 return -1;
405 memset(obj_node, 0, sizeof(*obj_node));
406 obj_node->id = pinned_info.id;
407 obj_node->path = strdup(ftse->fts_path);
408 hash_add(tab->table, &obj_node->hash, obj_node->id);
410 close(fd);
412 fts_close(fts);
414 fclose(mntfile);
415 return 0;
418 void delete_pinned_obj_table(struct pinned_obj_table *tab)
420 struct pinned_obj *obj;
421 struct hlist_node *tmp;
422 unsigned int bkt;
424 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
425 hash_del(&obj->hash);
426 free(obj->path);
427 free(obj);
431 unsigned int get_page_size(void)
433 static int result;
435 if (!result)
436 result = getpagesize();
437 return result;
440 unsigned int get_possible_cpus(void)
442 static unsigned int result;
443 char buf[128];
444 long int n;
445 char *ptr;
446 int fd;
448 if (result)
449 return result;
451 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
452 if (fd < 0) {
453 p_err("can't open sysfs possible cpus");
454 exit(-1);
457 n = read(fd, buf, sizeof(buf));
458 if (n < 2) {
459 p_err("can't read sysfs possible cpus");
460 exit(-1);
462 close(fd);
464 if (n == sizeof(buf)) {
465 p_err("read sysfs possible cpus overflow");
466 exit(-1);
469 ptr = buf;
470 n = 0;
471 while (*ptr && *ptr != '\n') {
472 unsigned int a, b;
474 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
475 n += b - a + 1;
477 ptr = strchr(ptr, '-') + 1;
478 } else if (sscanf(ptr, "%u", &a) == 1) {
479 n++;
480 } else {
481 assert(0);
484 while (isdigit(*ptr))
485 ptr++;
486 if (*ptr == ',')
487 ptr++;
490 result = n;
492 return result;
495 static char *
496 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
498 struct stat st;
499 int err;
501 err = stat("/proc/self/ns/net", &st);
502 if (err) {
503 p_err("Can't stat /proc/self: %s", strerror(errno));
504 return NULL;
507 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
508 return NULL;
510 return if_indextoname(ifindex, buf);
513 static int read_sysfs_hex_int(char *path)
515 char vendor_id_buf[8];
516 int len;
517 int fd;
519 fd = open(path, O_RDONLY);
520 if (fd < 0) {
521 p_err("Can't open %s: %s", path, strerror(errno));
522 return -1;
525 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
526 close(fd);
527 if (len < 0) {
528 p_err("Can't read %s: %s", path, strerror(errno));
529 return -1;
531 if (len >= (int)sizeof(vendor_id_buf)) {
532 p_err("Value in %s too long", path);
533 return -1;
536 vendor_id_buf[len] = 0;
538 return strtol(vendor_id_buf, NULL, 0);
541 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
543 char full_path[64];
545 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
546 devname, entry_name);
548 return read_sysfs_hex_int(full_path);
551 const char *
552 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
553 const char **opt)
555 char devname[IF_NAMESIZE];
556 int vendor_id;
557 int device_id;
559 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
560 p_err("Can't get net device name for ifindex %d: %s", ifindex,
561 strerror(errno));
562 return NULL;
565 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
566 if (vendor_id < 0) {
567 p_err("Can't get device vendor id for %s", devname);
568 return NULL;
571 switch (vendor_id) {
572 case 0x19ee:
573 device_id = read_sysfs_netdev_hex_int(devname, "device");
574 if (device_id != 0x4000 &&
575 device_id != 0x6000 &&
576 device_id != 0x6003)
577 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
578 *opt = "ctx4";
579 return "NFP-6xxx";
580 default:
581 p_err("Can't get bfd arch name for device vendor id 0x%04x",
582 vendor_id);
583 return NULL;
587 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
589 char name[IF_NAMESIZE];
591 if (!ifindex)
592 return;
594 printf(" offloaded_to ");
595 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
596 printf("%s", name);
597 else
598 printf("ifindex %u ns_dev %llu ns_ino %llu",
599 ifindex, ns_dev, ns_inode);
602 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
604 char name[IF_NAMESIZE];
606 if (!ifindex)
607 return;
609 jsonw_name(json_wtr, "dev");
610 jsonw_start_object(json_wtr);
611 jsonw_uint_field(json_wtr, "ifindex", ifindex);
612 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
613 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
614 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
615 jsonw_string_field(json_wtr, "ifname", name);
616 jsonw_end_object(json_wtr);
619 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
621 char *endptr;
623 NEXT_ARGP();
625 if (*val) {
626 p_err("%s already specified", what);
627 return -1;
630 *val = strtoul(**argv, &endptr, 0);
631 if (*endptr) {
632 p_err("can't parse %s as %s", **argv, what);
633 return -1;
635 NEXT_ARGP();
637 return 0;