treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / bpf / bpftool / common.c
blobb75b8ec5469c207cce1dc4c72f8e21bcea5a7ce9
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/bpf.h>
24 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
26 #include "main.h"
28 #ifndef BPF_FS_MAGIC
29 #define BPF_FS_MAGIC 0xcafe4a11
30 #endif
32 void p_err(const char *fmt, ...)
34 va_list ap;
36 va_start(ap, fmt);
37 if (json_output) {
38 jsonw_start_object(json_wtr);
39 jsonw_name(json_wtr, "error");
40 jsonw_vprintf_enquote(json_wtr, fmt, ap);
41 jsonw_end_object(json_wtr);
42 } else {
43 fprintf(stderr, "Error: ");
44 vfprintf(stderr, fmt, ap);
45 fprintf(stderr, "\n");
47 va_end(ap);
50 void p_info(const char *fmt, ...)
52 va_list ap;
54 if (json_output)
55 return;
57 va_start(ap, fmt);
58 vfprintf(stderr, fmt, ap);
59 fprintf(stderr, "\n");
60 va_end(ap);
63 static bool is_bpffs(char *path)
65 struct statfs st_fs;
67 if (statfs(path, &st_fs) < 0)
68 return false;
70 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
73 void set_max_rlimit(void)
75 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
77 setrlimit(RLIMIT_MEMLOCK, &rinf);
80 static int
81 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
83 bool bind_done = false;
85 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
86 if (errno != EINVAL || bind_done) {
87 snprintf(buff, bufflen,
88 "mount --make-private %s failed: %s",
89 target, strerror(errno));
90 return -1;
93 if (mount(target, target, "none", MS_BIND, NULL)) {
94 snprintf(buff, bufflen,
95 "mount --bind %s %s failed: %s",
96 target, target, strerror(errno));
97 return -1;
100 bind_done = true;
103 if (mount(type, target, type, 0, "mode=0700")) {
104 snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
105 type, type, target, strerror(errno));
106 return -1;
109 return 0;
112 int mount_tracefs(const char *target)
114 char err_str[ERR_MAX_LEN];
115 int err;
117 err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
118 if (err) {
119 err_str[ERR_MAX_LEN - 1] = '\0';
120 p_err("can't mount tracefs: %s", err_str);
123 return err;
126 int open_obj_pinned(char *path, bool quiet)
128 int fd;
130 fd = bpf_obj_get(path);
131 if (fd < 0) {
132 if (!quiet)
133 p_err("bpf obj get (%s): %s", path,
134 errno == EACCES && !is_bpffs(dirname(path)) ?
135 "directory not in bpf file system (bpffs)" :
136 strerror(errno));
137 return -1;
140 return fd;
143 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
145 enum bpf_obj_type type;
146 int fd;
148 fd = open_obj_pinned(path, false);
149 if (fd < 0)
150 return -1;
152 type = get_fd_type(fd);
153 if (type < 0) {
154 close(fd);
155 return type;
157 if (type != exp_type) {
158 p_err("incorrect object type: %s", get_fd_type_name(type));
159 close(fd);
160 return -1;
163 return fd;
166 int mount_bpffs_for_pin(const char *name)
168 char err_str[ERR_MAX_LEN];
169 char *file;
170 char *dir;
171 int err = 0;
173 file = malloc(strlen(name) + 1);
174 strcpy(file, name);
175 dir = dirname(file);
177 if (is_bpffs(dir))
178 /* nothing to do if already mounted */
179 goto out_free;
181 if (block_mount) {
182 p_err("no BPF file system found, not mounting it due to --nomount option");
183 err = -1;
184 goto out_free;
187 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
188 if (err) {
189 err_str[ERR_MAX_LEN - 1] = '\0';
190 p_err("can't mount BPF file system to pin the object (%s): %s",
191 name, err_str);
194 out_free:
195 free(file);
196 return err;
199 int do_pin_fd(int fd, const char *name)
201 int err;
203 err = mount_bpffs_for_pin(name);
204 if (err)
205 return err;
207 err = bpf_obj_pin(fd, name);
208 if (err)
209 p_err("can't pin the object (%s): %s", name, strerror(errno));
211 return err;
214 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
216 unsigned int id;
217 char *endptr;
218 int err;
219 int fd;
221 if (argc < 3) {
222 p_err("too few arguments, id ID and FILE path is required");
223 return -1;
224 } else if (argc > 3) {
225 p_err("too many arguments");
226 return -1;
229 if (!is_prefix(*argv, "id")) {
230 p_err("expected 'id' got %s", *argv);
231 return -1;
233 NEXT_ARG();
235 id = strtoul(*argv, &endptr, 0);
236 if (*endptr) {
237 p_err("can't parse %s as ID", *argv);
238 return -1;
240 NEXT_ARG();
242 fd = get_fd_by_id(id);
243 if (fd < 0) {
244 p_err("can't open object by id (%u): %s", id, strerror(errno));
245 return -1;
248 err = do_pin_fd(fd, *argv);
250 close(fd);
251 return err;
254 const char *get_fd_type_name(enum bpf_obj_type type)
256 static const char * const names[] = {
257 [BPF_OBJ_UNKNOWN] = "unknown",
258 [BPF_OBJ_PROG] = "prog",
259 [BPF_OBJ_MAP] = "map",
262 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
263 return names[BPF_OBJ_UNKNOWN];
265 return names[type];
268 int get_fd_type(int fd)
270 char path[PATH_MAX];
271 char buf[512];
272 ssize_t n;
274 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
276 n = readlink(path, buf, sizeof(buf));
277 if (n < 0) {
278 p_err("can't read link type: %s", strerror(errno));
279 return -1;
281 if (n == sizeof(path)) {
282 p_err("can't read link type: path too long!");
283 return -1;
286 if (strstr(buf, "bpf-map"))
287 return BPF_OBJ_MAP;
288 else if (strstr(buf, "bpf-prog"))
289 return BPF_OBJ_PROG;
291 return BPF_OBJ_UNKNOWN;
294 char *get_fdinfo(int fd, const char *key)
296 char path[PATH_MAX];
297 char *line = NULL;
298 size_t line_n = 0;
299 ssize_t n;
300 FILE *fdi;
302 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
304 fdi = fopen(path, "r");
305 if (!fdi)
306 return NULL;
308 while ((n = getline(&line, &line_n, fdi)) > 0) {
309 char *value;
310 int len;
312 if (!strstr(line, key))
313 continue;
315 fclose(fdi);
317 value = strchr(line, '\t');
318 if (!value || !value[1]) {
319 free(line);
320 return NULL;
322 value++;
324 len = strlen(value);
325 memmove(line, value, len);
326 line[len - 1] = '\0';
328 return line;
331 free(line);
332 fclose(fdi);
333 return NULL;
336 void print_data_json(uint8_t *data, size_t len)
338 unsigned int i;
340 jsonw_start_array(json_wtr);
341 for (i = 0; i < len; i++)
342 jsonw_printf(json_wtr, "%d", data[i]);
343 jsonw_end_array(json_wtr);
346 void print_hex_data_json(uint8_t *data, size_t len)
348 unsigned int i;
350 jsonw_start_array(json_wtr);
351 for (i = 0; i < len; i++)
352 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
353 jsonw_end_array(json_wtr);
356 int build_pinned_obj_table(struct pinned_obj_table *tab,
357 enum bpf_obj_type type)
359 struct bpf_prog_info pinned_info = {};
360 struct pinned_obj *obj_node = NULL;
361 __u32 len = sizeof(pinned_info);
362 struct mntent *mntent = NULL;
363 enum bpf_obj_type objtype;
364 FILE *mntfile = NULL;
365 FTSENT *ftse = NULL;
366 FTS *fts = NULL;
367 int fd, err;
369 mntfile = setmntent("/proc/mounts", "r");
370 if (!mntfile)
371 return -1;
373 while ((mntent = getmntent(mntfile))) {
374 char *path[] = { mntent->mnt_dir, NULL };
376 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
377 continue;
379 fts = fts_open(path, 0, NULL);
380 if (!fts)
381 continue;
383 while ((ftse = fts_read(fts))) {
384 if (!(ftse->fts_info & FTS_F))
385 continue;
386 fd = open_obj_pinned(ftse->fts_path, true);
387 if (fd < 0)
388 continue;
390 objtype = get_fd_type(fd);
391 if (objtype != type) {
392 close(fd);
393 continue;
395 memset(&pinned_info, 0, sizeof(pinned_info));
396 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
397 if (err) {
398 close(fd);
399 continue;
402 obj_node = malloc(sizeof(*obj_node));
403 if (!obj_node) {
404 close(fd);
405 fts_close(fts);
406 fclose(mntfile);
407 return -1;
410 memset(obj_node, 0, sizeof(*obj_node));
411 obj_node->id = pinned_info.id;
412 obj_node->path = strdup(ftse->fts_path);
413 hash_add(tab->table, &obj_node->hash, obj_node->id);
415 close(fd);
417 fts_close(fts);
419 fclose(mntfile);
420 return 0;
423 void delete_pinned_obj_table(struct pinned_obj_table *tab)
425 struct pinned_obj *obj;
426 struct hlist_node *tmp;
427 unsigned int bkt;
429 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
430 hash_del(&obj->hash);
431 free(obj->path);
432 free(obj);
436 unsigned int get_page_size(void)
438 static int result;
440 if (!result)
441 result = getpagesize();
442 return result;
445 unsigned int get_possible_cpus(void)
447 int cpus = libbpf_num_possible_cpus();
449 if (cpus < 0) {
450 p_err("Can't get # of possible cpus: %s", strerror(-cpus));
451 exit(-1);
453 return cpus;
456 static char *
457 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
459 struct stat st;
460 int err;
462 err = stat("/proc/self/ns/net", &st);
463 if (err) {
464 p_err("Can't stat /proc/self: %s", strerror(errno));
465 return NULL;
468 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
469 return NULL;
471 return if_indextoname(ifindex, buf);
474 static int read_sysfs_hex_int(char *path)
476 char vendor_id_buf[8];
477 int len;
478 int fd;
480 fd = open(path, O_RDONLY);
481 if (fd < 0) {
482 p_err("Can't open %s: %s", path, strerror(errno));
483 return -1;
486 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
487 close(fd);
488 if (len < 0) {
489 p_err("Can't read %s: %s", path, strerror(errno));
490 return -1;
492 if (len >= (int)sizeof(vendor_id_buf)) {
493 p_err("Value in %s too long", path);
494 return -1;
497 vendor_id_buf[len] = 0;
499 return strtol(vendor_id_buf, NULL, 0);
502 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
504 char full_path[64];
506 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
507 devname, entry_name);
509 return read_sysfs_hex_int(full_path);
512 const char *
513 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
514 const char **opt)
516 char devname[IF_NAMESIZE];
517 int vendor_id;
518 int device_id;
520 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
521 p_err("Can't get net device name for ifindex %d: %s", ifindex,
522 strerror(errno));
523 return NULL;
526 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
527 if (vendor_id < 0) {
528 p_err("Can't get device vendor id for %s", devname);
529 return NULL;
532 switch (vendor_id) {
533 case 0x19ee:
534 device_id = read_sysfs_netdev_hex_int(devname, "device");
535 if (device_id != 0x4000 &&
536 device_id != 0x6000 &&
537 device_id != 0x6003)
538 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
539 *opt = "ctx4";
540 return "NFP-6xxx";
541 default:
542 p_err("Can't get bfd arch name for device vendor id 0x%04x",
543 vendor_id);
544 return NULL;
548 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
550 char name[IF_NAMESIZE];
552 if (!ifindex)
553 return;
555 printf(" offloaded_to ");
556 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
557 printf("%s", name);
558 else
559 printf("ifindex %u ns_dev %llu ns_ino %llu",
560 ifindex, ns_dev, ns_inode);
563 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
565 char name[IF_NAMESIZE];
567 if (!ifindex)
568 return;
570 jsonw_name(json_wtr, "dev");
571 jsonw_start_object(json_wtr);
572 jsonw_uint_field(json_wtr, "ifindex", ifindex);
573 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
574 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
575 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
576 jsonw_string_field(json_wtr, "ifname", name);
577 jsonw_end_object(json_wtr);
580 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
582 char *endptr;
584 NEXT_ARGP();
586 if (*val) {
587 p_err("%s already specified", what);
588 return -1;
591 *val = strtoul(**argv, &endptr, 0);
592 if (*endptr) {
593 p_err("can't parse %s as %s", **argv, what);
594 return -1;
596 NEXT_ARGP();
598 return 0;