Merge tag 'tag-chrome-platform-for-v4.21' of git://git.kernel.org/pub/scm/linux/kerne...
[linux-2.6-microblaze.git] / tools / bpf / bpftool / common.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
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>
22
23 #include <bpf.h>
24
25 #include "main.h"
26
27 #ifndef BPF_FS_MAGIC
28 #define BPF_FS_MAGIC            0xcafe4a11
29 #endif
30
31 void __printf(1, 2) p_err(const char *fmt, ...)
32 {
33         va_list ap;
34
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");
45         }
46         va_end(ap);
47 }
48
49 void __printf(1, 2) p_info(const char *fmt, ...)
50 {
51         va_list ap;
52
53         if (json_output)
54                 return;
55
56         va_start(ap, fmt);
57         vfprintf(stderr, fmt, ap);
58         fprintf(stderr, "\n");
59         va_end(ap);
60 }
61
62 static bool is_bpffs(char *path)
63 {
64         struct statfs st_fs;
65
66         if (statfs(path, &st_fs) < 0)
67                 return false;
68
69         return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
70 }
71
72 void set_max_rlimit(void)
73 {
74         struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
75
76         setrlimit(RLIMIT_MEMLOCK, &rinf);
77 }
78
79 static int
80 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
81 {
82         bool bind_done = false;
83
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;
90                 }
91
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;
97                 }
98
99                 bind_done = true;
100         }
101
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;
106         }
107
108         return 0;
109 }
110
111 int mount_tracefs(const char *target)
112 {
113         char err_str[ERR_MAX_LEN];
114         int err;
115
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);
120         }
121
122         return err;
123 }
124
125 int open_obj_pinned(char *path, bool quiet)
126 {
127         int fd;
128
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;
137         }
138
139         return fd;
140 }
141
142 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
143 {
144         enum bpf_obj_type type;
145         int fd;
146
147         fd = open_obj_pinned(path, false);
148         if (fd < 0)
149                 return -1;
150
151         type = get_fd_type(fd);
152         if (type < 0) {
153                 close(fd);
154                 return type;
155         }
156         if (type != exp_type) {
157                 p_err("incorrect object type: %s", get_fd_type_name(type));
158                 close(fd);
159                 return -1;
160         }
161
162         return fd;
163 }
164
165 int mount_bpffs_for_pin(const char *name)
166 {
167         char err_str[ERR_MAX_LEN];
168         char *file;
169         char *dir;
170         int err = 0;
171
172         file = malloc(strlen(name) + 1);
173         strcpy(file, name);
174         dir = dirname(file);
175
176         if (is_bpffs(dir))
177                 /* nothing to do if already mounted */
178                 goto out_free;
179
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;
184         }
185
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);
191         }
192
193 out_free:
194         free(file);
195         return err;
196 }
197
198 int do_pin_fd(int fd, const char *name)
199 {
200         int err;
201
202         err = mount_bpffs_for_pin(name);
203         if (err)
204                 return err;
205
206         return bpf_obj_pin(fd, name);
207 }
208
209 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
210 {
211         unsigned int id;
212         char *endptr;
213         int err;
214         int fd;
215
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;
222         }
223
224         if (!is_prefix(*argv, "id")) {
225                 p_err("expected 'id' got %s", *argv);
226                 return -1;
227         }
228         NEXT_ARG();
229
230         id = strtoul(*argv, &endptr, 0);
231         if (*endptr) {
232                 p_err("can't parse %s as ID", *argv);
233                 return -1;
234         }
235         NEXT_ARG();
236
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;
241         }
242
243         err = do_pin_fd(fd, *argv);
244
245         close(fd);
246         return err;
247 }
248
249 const char *get_fd_type_name(enum bpf_obj_type type)
250 {
251         static const char * const names[] = {
252                 [BPF_OBJ_UNKNOWN]       = "unknown",
253                 [BPF_OBJ_PROG]          = "prog",
254                 [BPF_OBJ_MAP]           = "map",
255         };
256
257         if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
258                 return names[BPF_OBJ_UNKNOWN];
259
260         return names[type];
261 }
262
263 int get_fd_type(int fd)
264 {
265         char path[PATH_MAX];
266         char buf[512];
267         ssize_t n;
268
269         snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
270
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;
275         }
276         if (n == sizeof(path)) {
277                 p_err("can't read link type: path too long!");
278                 return -1;
279         }
280
281         if (strstr(buf, "bpf-map"))
282                 return BPF_OBJ_MAP;
283         else if (strstr(buf, "bpf-prog"))
284                 return BPF_OBJ_PROG;
285
286         return BPF_OBJ_UNKNOWN;
287 }
288
289 char *get_fdinfo(int fd, const char *key)
290 {
291         char path[PATH_MAX];
292         char *line = NULL;
293         size_t line_n = 0;
294         ssize_t n;
295         FILE *fdi;
296
297         snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
298
299         fdi = fopen(path, "r");
300         if (!fdi) {
301                 p_err("can't open fdinfo: %s", strerror(errno));
302                 return NULL;
303         }
304
305         while ((n = getline(&line, &line_n, fdi)) > 0) {
306                 char *value;
307                 int len;
308
309                 if (!strstr(line, key))
310                         continue;
311
312                 fclose(fdi);
313
314                 value = strchr(line, '\t');
315                 if (!value || !value[1]) {
316                         p_err("malformed fdinfo!?");
317                         free(line);
318                         return NULL;
319                 }
320                 value++;
321
322                 len = strlen(value);
323                 memmove(line, value, len);
324                 line[len - 1] = '\0';
325
326                 return line;
327         }
328
329         p_err("key '%s' not found in fdinfo", key);
330         free(line);
331         fclose(fdi);
332         return NULL;
333 }
334
335 void print_data_json(uint8_t *data, size_t len)
336 {
337         unsigned int i;
338
339         jsonw_start_array(json_wtr);
340         for (i = 0; i < len; i++)
341                 jsonw_printf(json_wtr, "%d", data[i]);
342         jsonw_end_array(json_wtr);
343 }
344
345 void print_hex_data_json(uint8_t *data, size_t len)
346 {
347         unsigned int i;
348
349         jsonw_start_array(json_wtr);
350         for (i = 0; i < len; i++)
351                 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
352         jsonw_end_array(json_wtr);
353 }
354
355 int build_pinned_obj_table(struct pinned_obj_table *tab,
356                            enum bpf_obj_type type)
357 {
358         struct bpf_prog_info pinned_info = {};
359         struct pinned_obj *obj_node = NULL;
360         __u32 len = sizeof(pinned_info);
361         struct mntent *mntent = NULL;
362         enum bpf_obj_type objtype;
363         FILE *mntfile = NULL;
364         FTSENT *ftse = NULL;
365         FTS *fts = NULL;
366         int fd, err;
367
368         mntfile = setmntent("/proc/mounts", "r");
369         if (!mntfile)
370                 return -1;
371
372         while ((mntent = getmntent(mntfile))) {
373                 char *path[] = { mntent->mnt_dir, NULL };
374
375                 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
376                         continue;
377
378                 fts = fts_open(path, 0, NULL);
379                 if (!fts)
380                         continue;
381
382                 while ((ftse = fts_read(fts))) {
383                         if (!(ftse->fts_info & FTS_F))
384                                 continue;
385                         fd = open_obj_pinned(ftse->fts_path, true);
386                         if (fd < 0)
387                                 continue;
388
389                         objtype = get_fd_type(fd);
390                         if (objtype != type) {
391                                 close(fd);
392                                 continue;
393                         }
394                         memset(&pinned_info, 0, sizeof(pinned_info));
395                         err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
396                         if (err) {
397                                 close(fd);
398                                 continue;
399                         }
400
401                         obj_node = malloc(sizeof(*obj_node));
402                         if (!obj_node) {
403                                 close(fd);
404                                 fts_close(fts);
405                                 fclose(mntfile);
406                                 return -1;
407                         }
408
409                         memset(obj_node, 0, sizeof(*obj_node));
410                         obj_node->id = pinned_info.id;
411                         obj_node->path = strdup(ftse->fts_path);
412                         hash_add(tab->table, &obj_node->hash, obj_node->id);
413
414                         close(fd);
415                 }
416                 fts_close(fts);
417         }
418         fclose(mntfile);
419         return 0;
420 }
421
422 void delete_pinned_obj_table(struct pinned_obj_table *tab)
423 {
424         struct pinned_obj *obj;
425         struct hlist_node *tmp;
426         unsigned int bkt;
427
428         hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
429                 hash_del(&obj->hash);
430                 free(obj->path);
431                 free(obj);
432         }
433 }
434
435 unsigned int get_page_size(void)
436 {
437         static int result;
438
439         if (!result)
440                 result = getpagesize();
441         return result;
442 }
443
444 unsigned int get_possible_cpus(void)
445 {
446         static unsigned int result;
447         char buf[128];
448         long int n;
449         char *ptr;
450         int fd;
451
452         if (result)
453                 return result;
454
455         fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
456         if (fd < 0) {
457                 p_err("can't open sysfs possible cpus");
458                 exit(-1);
459         }
460
461         n = read(fd, buf, sizeof(buf));
462         if (n < 2) {
463                 p_err("can't read sysfs possible cpus");
464                 exit(-1);
465         }
466         close(fd);
467
468         if (n == sizeof(buf)) {
469                 p_err("read sysfs possible cpus overflow");
470                 exit(-1);
471         }
472
473         ptr = buf;
474         n = 0;
475         while (*ptr && *ptr != '\n') {
476                 unsigned int a, b;
477
478                 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
479                         n += b - a + 1;
480
481                         ptr = strchr(ptr, '-') + 1;
482                 } else if (sscanf(ptr, "%u", &a) == 1) {
483                         n++;
484                 } else {
485                         assert(0);
486                 }
487
488                 while (isdigit(*ptr))
489                         ptr++;
490                 if (*ptr == ',')
491                         ptr++;
492         }
493
494         result = n;
495
496         return result;
497 }
498
499 static char *
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
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
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 *
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
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
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
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 }