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
33 const 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
p_err(const char * fmt,...)71 void 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
p_info(const char * fmt,...)89 void 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
is_bpffs(char * path)102 static 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
set_max_rlimit(void)112 void set_max_rlimit(void)
113 {
114 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
115
116 setrlimit(RLIMIT_MEMLOCK, &rinf);
117 }
118
119 static int
mnt_fs(const char * target,const char * type,char * buff,size_t bufflen)120 mnt_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
mount_tracefs(const char * target)151 int 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
open_obj_pinned(const char * path,bool quiet)165 int 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
187 out_free:
188 free(pname);
189 out_ret:
190 return fd;
191 }
192
open_obj_pinned_any(const char * path,enum bpf_obj_type exp_type)193 int 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
mount_bpffs_for_pin(const char * name)216 int 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 if (!file) {
225 p_err("mem alloc failed");
226 return -1;
227 }
228
229 strcpy(file, name);
230 dir = dirname(file);
231
232 if (is_bpffs(dir))
233 /* nothing to do if already mounted */
234 goto out_free;
235
236 if (block_mount) {
237 p_err("no BPF file system found, not mounting it due to --nomount option");
238 err = -1;
239 goto out_free;
240 }
241
242 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
243 if (err) {
244 err_str[ERR_MAX_LEN - 1] = '\0';
245 p_err("can't mount BPF file system to pin the object (%s): %s",
246 name, err_str);
247 }
248
249 out_free:
250 free(file);
251 return err;
252 }
253
do_pin_fd(int fd,const char * name)254 int do_pin_fd(int fd, const char *name)
255 {
256 int err;
257
258 err = mount_bpffs_for_pin(name);
259 if (err)
260 return err;
261
262 err = bpf_obj_pin(fd, name);
263 if (err)
264 p_err("can't pin the object (%s): %s", name, strerror(errno));
265
266 return err;
267 }
268
do_pin_any(int argc,char ** argv,int (* get_fd)(int *,char ***))269 int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
270 {
271 int err;
272 int fd;
273
274 fd = get_fd(&argc, &argv);
275 if (fd < 0)
276 return fd;
277
278 err = do_pin_fd(fd, *argv);
279
280 close(fd);
281 return err;
282 }
283
get_fd_type_name(enum bpf_obj_type type)284 const char *get_fd_type_name(enum bpf_obj_type type)
285 {
286 static const char * const names[] = {
287 [BPF_OBJ_UNKNOWN] = "unknown",
288 [BPF_OBJ_PROG] = "prog",
289 [BPF_OBJ_MAP] = "map",
290 };
291
292 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
293 return names[BPF_OBJ_UNKNOWN];
294
295 return names[type];
296 }
297
get_fd_type(int fd)298 int get_fd_type(int fd)
299 {
300 char path[PATH_MAX];
301 char buf[512];
302 ssize_t n;
303
304 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
305
306 n = readlink(path, buf, sizeof(buf));
307 if (n < 0) {
308 p_err("can't read link type: %s", strerror(errno));
309 return -1;
310 }
311 if (n == sizeof(path)) {
312 p_err("can't read link type: path too long!");
313 return -1;
314 }
315
316 if (strstr(buf, "bpf-map"))
317 return BPF_OBJ_MAP;
318 else if (strstr(buf, "bpf-prog"))
319 return BPF_OBJ_PROG;
320 else if (strstr(buf, "bpf-link"))
321 return BPF_OBJ_LINK;
322
323 return BPF_OBJ_UNKNOWN;
324 }
325
get_fdinfo(int fd,const char * key)326 char *get_fdinfo(int fd, const char *key)
327 {
328 char path[PATH_MAX];
329 char *line = NULL;
330 size_t line_n = 0;
331 ssize_t n;
332 FILE *fdi;
333
334 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
335
336 fdi = fopen(path, "r");
337 if (!fdi)
338 return NULL;
339
340 while ((n = getline(&line, &line_n, fdi)) > 0) {
341 char *value;
342 int len;
343
344 if (!strstr(line, key))
345 continue;
346
347 fclose(fdi);
348
349 value = strchr(line, '\t');
350 if (!value || !value[1]) {
351 free(line);
352 return NULL;
353 }
354 value++;
355
356 len = strlen(value);
357 memmove(line, value, len);
358 line[len - 1] = '\0';
359
360 return line;
361 }
362
363 free(line);
364 fclose(fdi);
365 return NULL;
366 }
367
print_data_json(uint8_t * data,size_t len)368 void print_data_json(uint8_t *data, size_t len)
369 {
370 unsigned int i;
371
372 jsonw_start_array(json_wtr);
373 for (i = 0; i < len; i++)
374 jsonw_printf(json_wtr, "%d", data[i]);
375 jsonw_end_array(json_wtr);
376 }
377
print_hex_data_json(uint8_t * data,size_t len)378 void print_hex_data_json(uint8_t *data, size_t len)
379 {
380 unsigned int i;
381
382 jsonw_start_array(json_wtr);
383 for (i = 0; i < len; i++)
384 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
385 jsonw_end_array(json_wtr);
386 }
387
388 /* extra params for nftw cb */
389 static struct pinned_obj_table *build_fn_table;
390 static enum bpf_obj_type build_fn_type;
391
do_build_table_cb(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)392 static int do_build_table_cb(const char *fpath, const struct stat *sb,
393 int typeflag, struct FTW *ftwbuf)
394 {
395 struct bpf_prog_info pinned_info;
396 __u32 len = sizeof(pinned_info);
397 struct pinned_obj *obj_node;
398 enum bpf_obj_type objtype;
399 int fd, err = 0;
400
401 if (typeflag != FTW_F)
402 goto out_ret;
403
404 fd = open_obj_pinned(fpath, true);
405 if (fd < 0)
406 goto out_ret;
407
408 objtype = get_fd_type(fd);
409 if (objtype != build_fn_type)
410 goto out_close;
411
412 memset(&pinned_info, 0, sizeof(pinned_info));
413 if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
414 goto out_close;
415
416 obj_node = calloc(1, sizeof(*obj_node));
417 if (!obj_node) {
418 err = -1;
419 goto out_close;
420 }
421
422 obj_node->id = pinned_info.id;
423 obj_node->path = strdup(fpath);
424 if (!obj_node->path) {
425 err = -1;
426 free(obj_node);
427 goto out_close;
428 }
429
430 hash_add(build_fn_table->table, &obj_node->hash, obj_node->id);
431 out_close:
432 close(fd);
433 out_ret:
434 return err;
435 }
436
build_pinned_obj_table(struct pinned_obj_table * tab,enum bpf_obj_type type)437 int build_pinned_obj_table(struct pinned_obj_table *tab,
438 enum bpf_obj_type type)
439 {
440 struct mntent *mntent = NULL;
441 FILE *mntfile = NULL;
442 int flags = FTW_PHYS;
443 int nopenfd = 16;
444 int err = 0;
445
446 mntfile = setmntent("/proc/mounts", "r");
447 if (!mntfile)
448 return -1;
449
450 build_fn_table = tab;
451 build_fn_type = type;
452
453 while ((mntent = getmntent(mntfile))) {
454 char *path = mntent->mnt_dir;
455
456 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
457 continue;
458 err = nftw(path, do_build_table_cb, nopenfd, flags);
459 if (err)
460 break;
461 }
462 fclose(mntfile);
463 return err;
464 }
465
delete_pinned_obj_table(struct pinned_obj_table * tab)466 void delete_pinned_obj_table(struct pinned_obj_table *tab)
467 {
468 struct pinned_obj *obj;
469 struct hlist_node *tmp;
470 unsigned int bkt;
471
472 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
473 hash_del(&obj->hash);
474 free(obj->path);
475 free(obj);
476 }
477 }
478
get_page_size(void)479 unsigned int get_page_size(void)
480 {
481 static int result;
482
483 if (!result)
484 result = getpagesize();
485 return result;
486 }
487
get_possible_cpus(void)488 unsigned int get_possible_cpus(void)
489 {
490 int cpus = libbpf_num_possible_cpus();
491
492 if (cpus < 0) {
493 p_err("Can't get # of possible cpus: %s", strerror(-cpus));
494 exit(-1);
495 }
496 return cpus;
497 }
498
499 static char *
ifindex_to_name_ns(__u32 ifindex,__u32 ns_dev,__u32 ns_ino,char * buf)500 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
501 {
502 struct stat st;
503 int err;
504
505 err = stat("/proc/self/ns/net", &st);
506 if (err) {
507 p_err("Can't stat /proc/self: %s", strerror(errno));
508 return NULL;
509 }
510
511 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
512 return NULL;
513
514 return if_indextoname(ifindex, buf);
515 }
516
read_sysfs_hex_int(char * path)517 static int read_sysfs_hex_int(char *path)
518 {
519 char vendor_id_buf[8];
520 int len;
521 int fd;
522
523 fd = open(path, O_RDONLY);
524 if (fd < 0) {
525 p_err("Can't open %s: %s", path, strerror(errno));
526 return -1;
527 }
528
529 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
530 close(fd);
531 if (len < 0) {
532 p_err("Can't read %s: %s", path, strerror(errno));
533 return -1;
534 }
535 if (len >= (int)sizeof(vendor_id_buf)) {
536 p_err("Value in %s too long", path);
537 return -1;
538 }
539
540 vendor_id_buf[len] = 0;
541
542 return strtol(vendor_id_buf, NULL, 0);
543 }
544
read_sysfs_netdev_hex_int(char * devname,const char * entry_name)545 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
546 {
547 char full_path[64];
548
549 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
550 devname, entry_name);
551
552 return read_sysfs_hex_int(full_path);
553 }
554
555 const char *
ifindex_to_bfd_params(__u32 ifindex,__u64 ns_dev,__u64 ns_ino,const char ** opt)556 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
557 const char **opt)
558 {
559 char devname[IF_NAMESIZE];
560 int vendor_id;
561 int device_id;
562
563 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
564 p_err("Can't get net device name for ifindex %d: %s", ifindex,
565 strerror(errno));
566 return NULL;
567 }
568
569 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
570 if (vendor_id < 0) {
571 p_err("Can't get device vendor id for %s", devname);
572 return NULL;
573 }
574
575 switch (vendor_id) {
576 case 0x19ee:
577 device_id = read_sysfs_netdev_hex_int(devname, "device");
578 if (device_id != 0x4000 &&
579 device_id != 0x6000 &&
580 device_id != 0x6003)
581 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
582 *opt = "ctx4";
583 return "NFP-6xxx";
584 default:
585 p_err("Can't get bfd arch name for device vendor id 0x%04x",
586 vendor_id);
587 return NULL;
588 }
589 }
590
print_dev_plain(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)591 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
592 {
593 char name[IF_NAMESIZE];
594
595 if (!ifindex)
596 return;
597
598 printf(" offloaded_to ");
599 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
600 printf("%s", name);
601 else
602 printf("ifindex %u ns_dev %llu ns_ino %llu",
603 ifindex, ns_dev, ns_inode);
604 }
605
print_dev_json(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)606 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
607 {
608 char name[IF_NAMESIZE];
609
610 if (!ifindex)
611 return;
612
613 jsonw_name(json_wtr, "dev");
614 jsonw_start_object(json_wtr);
615 jsonw_uint_field(json_wtr, "ifindex", ifindex);
616 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
617 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
618 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
619 jsonw_string_field(json_wtr, "ifname", name);
620 jsonw_end_object(json_wtr);
621 }
622
parse_u32_arg(int * argc,char *** argv,__u32 * val,const char * what)623 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
624 {
625 char *endptr;
626
627 NEXT_ARGP();
628
629 if (*val) {
630 p_err("%s already specified", what);
631 return -1;
632 }
633
634 *val = strtoul(**argv, &endptr, 0);
635 if (*endptr) {
636 p_err("can't parse %s as %s", **argv, what);
637 return -1;
638 }
639 NEXT_ARGP();
640
641 return 0;
642 }
643
644 int __printf(2, 0)
print_all_levels(__maybe_unused enum libbpf_print_level level,const char * format,va_list args)645 print_all_levels(__maybe_unused enum libbpf_print_level level,
646 const char *format, va_list args)
647 {
648 return vfprintf(stderr, format, args);
649 }
650
prog_fd_by_nametag(void * nametag,int ** fds,bool tag)651 static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
652 {
653 unsigned int id = 0;
654 int fd, nb_fds = 0;
655 void *tmp;
656 int err;
657
658 while (true) {
659 struct bpf_prog_info info = {};
660 __u32 len = sizeof(info);
661
662 err = bpf_prog_get_next_id(id, &id);
663 if (err) {
664 if (errno != ENOENT) {
665 p_err("%s", strerror(errno));
666 goto err_close_fds;
667 }
668 return nb_fds;
669 }
670
671 fd = bpf_prog_get_fd_by_id(id);
672 if (fd < 0) {
673 p_err("can't get prog by id (%u): %s",
674 id, strerror(errno));
675 goto err_close_fds;
676 }
677
678 err = bpf_obj_get_info_by_fd(fd, &info, &len);
679 if (err) {
680 p_err("can't get prog info (%u): %s",
681 id, strerror(errno));
682 goto err_close_fd;
683 }
684
685 if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) ||
686 (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) {
687 close(fd);
688 continue;
689 }
690
691 if (nb_fds > 0) {
692 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
693 if (!tmp) {
694 p_err("failed to realloc");
695 goto err_close_fd;
696 }
697 *fds = tmp;
698 }
699 (*fds)[nb_fds++] = fd;
700 }
701
702 err_close_fd:
703 close(fd);
704 err_close_fds:
705 while (--nb_fds >= 0)
706 close((*fds)[nb_fds]);
707 return -1;
708 }
709
prog_parse_fds(int * argc,char *** argv,int ** fds)710 int prog_parse_fds(int *argc, char ***argv, int **fds)
711 {
712 if (is_prefix(**argv, "id")) {
713 unsigned int id;
714 char *endptr;
715
716 NEXT_ARGP();
717
718 id = strtoul(**argv, &endptr, 0);
719 if (*endptr) {
720 p_err("can't parse %s as ID", **argv);
721 return -1;
722 }
723 NEXT_ARGP();
724
725 (*fds)[0] = bpf_prog_get_fd_by_id(id);
726 if ((*fds)[0] < 0) {
727 p_err("get by id (%u): %s", id, strerror(errno));
728 return -1;
729 }
730 return 1;
731 } else if (is_prefix(**argv, "tag")) {
732 unsigned char tag[BPF_TAG_SIZE];
733
734 NEXT_ARGP();
735
736 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
737 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
738 != BPF_TAG_SIZE) {
739 p_err("can't parse tag");
740 return -1;
741 }
742 NEXT_ARGP();
743
744 return prog_fd_by_nametag(tag, fds, true);
745 } else if (is_prefix(**argv, "name")) {
746 char *name;
747
748 NEXT_ARGP();
749
750 name = **argv;
751 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
752 p_err("can't parse name");
753 return -1;
754 }
755 NEXT_ARGP();
756
757 return prog_fd_by_nametag(name, fds, false);
758 } else if (is_prefix(**argv, "pinned")) {
759 char *path;
760
761 NEXT_ARGP();
762
763 path = **argv;
764 NEXT_ARGP();
765
766 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
767 if ((*fds)[0] < 0)
768 return -1;
769 return 1;
770 }
771
772 p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv);
773 return -1;
774 }
775
prog_parse_fd(int * argc,char *** argv)776 int prog_parse_fd(int *argc, char ***argv)
777 {
778 int *fds = NULL;
779 int nb_fds, fd;
780
781 fds = malloc(sizeof(int));
782 if (!fds) {
783 p_err("mem alloc failed");
784 return -1;
785 }
786 nb_fds = prog_parse_fds(argc, argv, &fds);
787 if (nb_fds != 1) {
788 if (nb_fds > 1) {
789 p_err("several programs match this handle");
790 while (nb_fds--)
791 close(fds[nb_fds]);
792 }
793 fd = -1;
794 goto exit_free;
795 }
796
797 fd = fds[0];
798 exit_free:
799 free(fds);
800 return fd;
801 }
802
map_fd_by_name(char * name,int ** fds)803 static int map_fd_by_name(char *name, int **fds)
804 {
805 unsigned int id = 0;
806 int fd, nb_fds = 0;
807 void *tmp;
808 int err;
809
810 while (true) {
811 struct bpf_map_info info = {};
812 __u32 len = sizeof(info);
813
814 err = bpf_map_get_next_id(id, &id);
815 if (err) {
816 if (errno != ENOENT) {
817 p_err("%s", strerror(errno));
818 goto err_close_fds;
819 }
820 return nb_fds;
821 }
822
823 fd = bpf_map_get_fd_by_id(id);
824 if (fd < 0) {
825 p_err("can't get map by id (%u): %s",
826 id, strerror(errno));
827 goto err_close_fds;
828 }
829
830 err = bpf_obj_get_info_by_fd(fd, &info, &len);
831 if (err) {
832 p_err("can't get map info (%u): %s",
833 id, strerror(errno));
834 goto err_close_fd;
835 }
836
837 if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) {
838 close(fd);
839 continue;
840 }
841
842 if (nb_fds > 0) {
843 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
844 if (!tmp) {
845 p_err("failed to realloc");
846 goto err_close_fd;
847 }
848 *fds = tmp;
849 }
850 (*fds)[nb_fds++] = fd;
851 }
852
853 err_close_fd:
854 close(fd);
855 err_close_fds:
856 while (--nb_fds >= 0)
857 close((*fds)[nb_fds]);
858 return -1;
859 }
860
map_parse_fds(int * argc,char *** argv,int ** fds)861 int map_parse_fds(int *argc, char ***argv, int **fds)
862 {
863 if (is_prefix(**argv, "id")) {
864 unsigned int id;
865 char *endptr;
866
867 NEXT_ARGP();
868
869 id = strtoul(**argv, &endptr, 0);
870 if (*endptr) {
871 p_err("can't parse %s as ID", **argv);
872 return -1;
873 }
874 NEXT_ARGP();
875
876 (*fds)[0] = bpf_map_get_fd_by_id(id);
877 if ((*fds)[0] < 0) {
878 p_err("get map by id (%u): %s", id, strerror(errno));
879 return -1;
880 }
881 return 1;
882 } else if (is_prefix(**argv, "name")) {
883 char *name;
884
885 NEXT_ARGP();
886
887 name = **argv;
888 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
889 p_err("can't parse name");
890 return -1;
891 }
892 NEXT_ARGP();
893
894 return map_fd_by_name(name, fds);
895 } else if (is_prefix(**argv, "pinned")) {
896 char *path;
897
898 NEXT_ARGP();
899
900 path = **argv;
901 NEXT_ARGP();
902
903 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP);
904 if ((*fds)[0] < 0)
905 return -1;
906 return 1;
907 }
908
909 p_err("expected 'id', 'name' or 'pinned', got: '%s'?", **argv);
910 return -1;
911 }
912
map_parse_fd(int * argc,char *** argv)913 int map_parse_fd(int *argc, char ***argv)
914 {
915 int *fds = NULL;
916 int nb_fds, fd;
917
918 fds = malloc(sizeof(int));
919 if (!fds) {
920 p_err("mem alloc failed");
921 return -1;
922 }
923 nb_fds = map_parse_fds(argc, argv, &fds);
924 if (nb_fds != 1) {
925 if (nb_fds > 1) {
926 p_err("several maps match this handle");
927 while (nb_fds--)
928 close(fds[nb_fds]);
929 }
930 fd = -1;
931 goto exit_free;
932 }
933
934 fd = fds[0];
935 exit_free:
936 free(fds);
937 return fd;
938 }
939
map_parse_fd_and_info(int * argc,char *** argv,void * info,__u32 * info_len)940 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
941 {
942 int err;
943 int fd;
944
945 fd = map_parse_fd(argc, argv);
946 if (fd < 0)
947 return -1;
948
949 err = bpf_obj_get_info_by_fd(fd, info, info_len);
950 if (err) {
951 p_err("can't get map info: %s", strerror(errno));
952 close(fd);
953 return err;
954 }
955
956 return fd;
957 }
958