blob: 1bad6602a2825c6a22055edf092dc6b74db64428 [file] [log] [blame]
Jakub Kicinski02ff58d2018-12-12 19:59:25 -08001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
Jakub Kicinski71bb4282017-10-04 20:10:04 -07003
Jakub Kicinskie64d5252018-05-03 18:37:15 -07004#include <ctype.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -07005#include <errno.h>
Jiong Wange6593592018-01-16 16:05:21 -08006#include <fcntl.h>
Prashant Bhole4990f1f2017-11-08 13:55:48 +09007#include <fts.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -07008#include <libgen.h>
Prashant Bhole4990f1f2017-11-08 13:55:48 +09009#include <mntent.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070010#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>
Jakub Kicinski52262212017-12-27 18:39:10 -080017#include <net/if.h>
Quentin Monnet3fc27b72017-10-24 20:11:28 -070018#include <sys/mount.h>
Quentin Monnet8302b9b2018-11-07 12:29:30 +000019#include <sys/resource.h>
Jakub Kicinski52262212017-12-27 18:39:10 -080020#include <sys/stat.h>
Jakub Kicinski71bb4282017-10-04 20:10:04 -070021#include <sys/vfs.h>
22
23#include <bpf.h>
24
25#include "main.h"
26
Jiri Benc20d5de52018-03-06 14:50:10 +010027#ifndef BPF_FS_MAGIC
28#define BPF_FS_MAGIC 0xcafe4a11
29#endif
30
Quentin Monnetc1011892018-12-14 13:56:01 +000031void __printf(1, 2) p_err(const char *fmt, ...)
Quentin Monnet0b1c27d2017-11-03 13:59:07 -070032{
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
Quentin Monnetc1011892018-12-14 13:56:01 +000049void __printf(1, 2) p_info(const char *fmt, ...)
Quentin Monnet0b1c27d2017-11-03 13:59:07 -070050{
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
Jakub Kicinski71bb4282017-10-04 20:10:04 -070062static 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
Quentin Monnet8302b9b2018-11-07 12:29:30 +000072void set_max_rlimit(void)
73{
74 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
75
76 setrlimit(RLIMIT_MEMLOCK, &rinf);
77}
78
Quentin Monnetbe3245e2018-12-18 10:13:18 +000079static int
80mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
Quentin Monnet3fc27b72017-10-24 20:11:28 -070081{
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
Quentin Monnetbe3245e2018-12-18 10:13:18 +0000102 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));
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700105 return -1;
106 }
107
108 return 0;
109}
110
Quentin Monnetbe3245e2018-12-18 10:13:18 +0000111int 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
Quentin Monnetf1209192018-11-08 11:52:27 +0000125int open_obj_pinned(char *path, bool quiet)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700126{
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700127 int fd;
128
129 fd = bpf_obj_get(path);
130 if (fd < 0) {
Quentin Monnetf1209192018-11-08 11:52:27 +0000131 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));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700136 return -1;
137 }
138
Prashant Bhole18527192017-11-08 13:55:47 +0900139 return fd;
140}
141
142int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
143{
144 enum bpf_obj_type type;
145 int fd;
146
Quentin Monnetf1209192018-11-08 11:52:27 +0000147 fd = open_obj_pinned(path, false);
Prashant Bhole18527192017-11-08 13:55:47 +0900148 if (fd < 0)
149 return -1;
150
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700151 type = get_fd_type(fd);
152 if (type < 0) {
153 close(fd);
154 return type;
155 }
156 if (type != exp_type) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700157 p_err("incorrect object type: %s", get_fd_type_name(type));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700158 close(fd);
159 return -1;
160 }
161
162 return fd;
163}
164
Stanislav Fomichev77380992018-11-09 08:21:44 -0800165int mount_bpffs_for_pin(const char *name)
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700166{
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700167 char err_str[ERR_MAX_LEN];
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700168 char *file;
169 char *dir;
Roman Gushchin49a086c2017-12-13 15:18:53 +0000170 int err = 0;
171
Roman Gushchin49a086c2017-12-13 15:18:53 +0000172 file = malloc(strlen(name) + 1);
173 strcpy(file, name);
174 dir = dirname(file);
175
Stanislav Fomichev77380992018-11-09 08:21:44 -0800176 if (is_bpffs(dir))
177 /* nothing to do if already mounted */
Roman Gushchin49a086c2017-12-13 15:18:53 +0000178 goto out_free;
Roman Gushchin49a086c2017-12-13 15:18:53 +0000179
Quentin Monnetbe3245e2018-12-18 10:13:18 +0000180 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
Stanislav Fomichev77380992018-11-09 08:21:44 -0800181 if (err) {
Roman Gushchin49a086c2017-12-13 15:18:53 +0000182 err_str[ERR_MAX_LEN - 1] = '\0';
183 p_err("can't mount BPF file system to pin the object (%s): %s",
184 name, err_str);
185 }
186
187out_free:
188 free(file);
Roman Gushchin49a086c2017-12-13 15:18:53 +0000189 return err;
190}
191
Stanislav Fomichev77380992018-11-09 08:21:44 -0800192int do_pin_fd(int fd, const char *name)
193{
194 int err;
195
196 err = mount_bpffs_for_pin(name);
197 if (err)
198 return err;
199
200 return bpf_obj_pin(fd, name);
201}
202
Roman Gushchin49a086c2017-12-13 15:18:53 +0000203int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
204{
205 unsigned int id;
206 char *endptr;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700207 int err;
208 int fd;
209
Taeung Song759b94a2018-07-19 21:10:05 +0900210 if (argc < 3) {
211 p_err("too few arguments, id ID and FILE path is required");
212 return -1;
213 } else if (argc > 3) {
214 p_err("too many arguments");
215 return -1;
216 }
217
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700218 if (!is_prefix(*argv, "id")) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700219 p_err("expected 'id' got %s", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700220 return -1;
221 }
222 NEXT_ARG();
223
224 id = strtoul(*argv, &endptr, 0);
225 if (*endptr) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700226 p_err("can't parse %s as ID", *argv);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700227 return -1;
228 }
229 NEXT_ARG();
230
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700231 fd = get_fd_by_id(id);
232 if (fd < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700233 p_err("can't get prog by id (%u): %s", id, strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700234 return -1;
235 }
236
Roman Gushchin49a086c2017-12-13 15:18:53 +0000237 err = do_pin_fd(fd, *argv);
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700238
Quentin Monnet3fc27b72017-10-24 20:11:28 -0700239 close(fd);
240 return err;
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700241}
242
243const char *get_fd_type_name(enum bpf_obj_type type)
244{
245 static const char * const names[] = {
246 [BPF_OBJ_UNKNOWN] = "unknown",
247 [BPF_OBJ_PROG] = "prog",
248 [BPF_OBJ_MAP] = "map",
249 };
250
251 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
252 return names[BPF_OBJ_UNKNOWN];
253
254 return names[type];
255}
256
257int get_fd_type(int fd)
258{
259 char path[PATH_MAX];
260 char buf[512];
261 ssize_t n;
262
Quentin Monnet327e5da2018-11-30 16:25:44 +0000263 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700264
265 n = readlink(path, buf, sizeof(buf));
266 if (n < 0) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700267 p_err("can't read link type: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700268 return -1;
269 }
270 if (n == sizeof(path)) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700271 p_err("can't read link type: path too long!");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700272 return -1;
273 }
274
275 if (strstr(buf, "bpf-map"))
276 return BPF_OBJ_MAP;
277 else if (strstr(buf, "bpf-prog"))
278 return BPF_OBJ_PROG;
279
280 return BPF_OBJ_UNKNOWN;
281}
282
283char *get_fdinfo(int fd, const char *key)
284{
285 char path[PATH_MAX];
286 char *line = NULL;
287 size_t line_n = 0;
288 ssize_t n;
289 FILE *fdi;
290
Quentin Monnet327e5da2018-11-30 16:25:44 +0000291 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700292
293 fdi = fopen(path, "r");
294 if (!fdi) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700295 p_err("can't open fdinfo: %s", strerror(errno));
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700296 return NULL;
297 }
298
Quentin Monnet53909032018-11-08 11:52:25 +0000299 while ((n = getline(&line, &line_n, fdi)) > 0) {
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700300 char *value;
301 int len;
302
303 if (!strstr(line, key))
304 continue;
305
306 fclose(fdi);
307
308 value = strchr(line, '\t');
309 if (!value || !value[1]) {
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700310 p_err("malformed fdinfo!?");
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700311 free(line);
312 return NULL;
313 }
314 value++;
315
316 len = strlen(value);
317 memmove(line, value, len);
318 line[len - 1] = '\0';
319
320 return line;
321 }
322
Quentin Monnet9a5ab8b2017-10-23 09:24:13 -0700323 p_err("key '%s' not found in fdinfo", key);
Jakub Kicinski71bb4282017-10-04 20:10:04 -0700324 free(line);
325 fclose(fdi);
326 return NULL;
327}
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700328
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700329void print_data_json(uint8_t *data, size_t len)
330{
331 unsigned int i;
332
333 jsonw_start_array(json_wtr);
334 for (i = 0; i < len; i++)
335 jsonw_printf(json_wtr, "%d", data[i]);
336 jsonw_end_array(json_wtr);
337}
338
Quentin Monnetf05e2c32017-10-23 09:24:10 -0700339void print_hex_data_json(uint8_t *data, size_t len)
340{
341 unsigned int i;
342
343 jsonw_start_array(json_wtr);
344 for (i = 0; i < len; i++)
345 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
346 jsonw_end_array(json_wtr);
347}
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900348
349int build_pinned_obj_table(struct pinned_obj_table *tab,
350 enum bpf_obj_type type)
351{
352 struct bpf_prog_info pinned_info = {};
353 struct pinned_obj *obj_node = NULL;
354 __u32 len = sizeof(pinned_info);
355 struct mntent *mntent = NULL;
356 enum bpf_obj_type objtype;
357 FILE *mntfile = NULL;
358 FTSENT *ftse = NULL;
359 FTS *fts = NULL;
360 int fd, err;
361
362 mntfile = setmntent("/proc/mounts", "r");
363 if (!mntfile)
364 return -1;
365
366 while ((mntent = getmntent(mntfile))) {
367 char *path[] = { mntent->mnt_dir, NULL };
368
369 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
370 continue;
371
372 fts = fts_open(path, 0, NULL);
373 if (!fts)
374 continue;
375
376 while ((ftse = fts_read(fts))) {
377 if (!(ftse->fts_info & FTS_F))
378 continue;
Quentin Monnetf1209192018-11-08 11:52:27 +0000379 fd = open_obj_pinned(ftse->fts_path, true);
Prashant Bhole4990f1f2017-11-08 13:55:48 +0900380 if (fd < 0)
381 continue;
382
383 objtype = get_fd_type(fd);
384 if (objtype != type) {
385 close(fd);
386 continue;
387 }
388 memset(&pinned_info, 0, sizeof(pinned_info));
389 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
390 if (err) {
391 close(fd);
392 continue;
393 }
394
395 obj_node = malloc(sizeof(*obj_node));
396 if (!obj_node) {
397 close(fd);
398 fts_close(fts);
399 fclose(mntfile);
400 return -1;
401 }
402
403 memset(obj_node, 0, sizeof(*obj_node));
404 obj_node->id = pinned_info.id;
405 obj_node->path = strdup(ftse->fts_path);
406 hash_add(tab->table, &obj_node->hash, obj_node->id);
407
408 close(fd);
409 }
410 fts_close(fts);
411 }
412 fclose(mntfile);
413 return 0;
414}
415
416void delete_pinned_obj_table(struct pinned_obj_table *tab)
417{
418 struct pinned_obj *obj;
419 struct hlist_node *tmp;
420 unsigned int bkt;
421
422 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
423 hash_del(&obj->hash);
424 free(obj->path);
425 free(obj);
426 }
427}
Jakub Kicinski52262212017-12-27 18:39:10 -0800428
Jakub Kicinskif412eed2018-05-03 18:37:16 -0700429unsigned int get_page_size(void)
430{
431 static int result;
432
433 if (!result)
434 result = getpagesize();
435 return result;
436}
437
Jakub Kicinskie64d5252018-05-03 18:37:15 -0700438unsigned int get_possible_cpus(void)
439{
440 static unsigned int result;
441 char buf[128];
442 long int n;
443 char *ptr;
444 int fd;
445
446 if (result)
447 return result;
448
449 fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
450 if (fd < 0) {
451 p_err("can't open sysfs possible cpus");
452 exit(-1);
453 }
454
455 n = read(fd, buf, sizeof(buf));
456 if (n < 2) {
457 p_err("can't read sysfs possible cpus");
458 exit(-1);
459 }
460 close(fd);
461
462 if (n == sizeof(buf)) {
463 p_err("read sysfs possible cpus overflow");
464 exit(-1);
465 }
466
467 ptr = buf;
468 n = 0;
469 while (*ptr && *ptr != '\n') {
470 unsigned int a, b;
471
472 if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
473 n += b - a + 1;
474
475 ptr = strchr(ptr, '-') + 1;
476 } else if (sscanf(ptr, "%u", &a) == 1) {
477 n++;
478 } else {
479 assert(0);
480 }
481
482 while (isdigit(*ptr))
483 ptr++;
484 if (*ptr == ',')
485 ptr++;
486 }
487
488 result = n;
489
490 return result;
491}
492
Jakub Kicinski52262212017-12-27 18:39:10 -0800493static char *
494ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
495{
496 struct stat st;
497 int err;
498
499 err = stat("/proc/self/ns/net", &st);
500 if (err) {
501 p_err("Can't stat /proc/self: %s", strerror(errno));
502 return NULL;
503 }
504
505 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
506 return NULL;
507
508 return if_indextoname(ifindex, buf);
509}
510
Jiong Wange6593592018-01-16 16:05:21 -0800511static int read_sysfs_hex_int(char *path)
512{
513 char vendor_id_buf[8];
514 int len;
515 int fd;
516
517 fd = open(path, O_RDONLY);
518 if (fd < 0) {
519 p_err("Can't open %s: %s", path, strerror(errno));
520 return -1;
521 }
522
523 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
524 close(fd);
525 if (len < 0) {
526 p_err("Can't read %s: %s", path, strerror(errno));
527 return -1;
528 }
529 if (len >= (int)sizeof(vendor_id_buf)) {
530 p_err("Value in %s too long", path);
531 return -1;
532 }
533
534 vendor_id_buf[len] = 0;
535
536 return strtol(vendor_id_buf, NULL, 0);
537}
538
539static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
540{
541 char full_path[64];
542
543 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
544 devname, entry_name);
545
546 return read_sysfs_hex_int(full_path);
547}
548
Jakub Kicinski3ddeac62018-10-18 11:34:55 -0700549const char *
550ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
551 const char **opt)
Jiong Wange6593592018-01-16 16:05:21 -0800552{
553 char devname[IF_NAMESIZE];
554 int vendor_id;
555 int device_id;
556
557 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
558 p_err("Can't get net device name for ifindex %d: %s", ifindex,
559 strerror(errno));
560 return NULL;
561 }
562
563 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
564 if (vendor_id < 0) {
565 p_err("Can't get device vendor id for %s", devname);
566 return NULL;
567 }
568
569 switch (vendor_id) {
570 case 0x19ee:
571 device_id = read_sysfs_netdev_hex_int(devname, "device");
572 if (device_id != 0x4000 &&
573 device_id != 0x6000 &&
574 device_id != 0x6003)
575 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
Jakub Kicinski3ddeac62018-10-18 11:34:55 -0700576 *opt = "ctx4";
Jiong Wange6593592018-01-16 16:05:21 -0800577 return "NFP-6xxx";
578 default:
579 p_err("Can't get bfd arch name for device vendor id 0x%04x",
580 vendor_id);
581 return NULL;
582 }
583}
584
Jakub Kicinski52262212017-12-27 18:39:10 -0800585void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
586{
587 char name[IF_NAMESIZE];
588
589 if (!ifindex)
590 return;
591
Quentin Monnet73f0b9d2018-11-30 16:25:47 +0000592 printf(" offloaded_to ");
Jakub Kicinski52262212017-12-27 18:39:10 -0800593 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
594 printf("%s", name);
595 else
596 printf("ifindex %u ns_dev %llu ns_ino %llu",
597 ifindex, ns_dev, ns_inode);
598}
599
600void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
601{
602 char name[IF_NAMESIZE];
603
604 if (!ifindex)
605 return;
606
607 jsonw_name(json_wtr, "dev");
608 jsonw_start_object(json_wtr);
609 jsonw_uint_field(json_wtr, "ifindex", ifindex);
610 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
611 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
612 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
613 jsonw_string_field(json_wtr, "ifname", name);
614 jsonw_end_object(json_wtr);
615}
Jakub Kicinski0b592b5a2018-10-15 16:30:36 -0700616
617int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
618{
619 char *endptr;
620
621 NEXT_ARGP();
622
623 if (*val) {
624 p_err("%s already specified", what);
625 return -1;
626 }
627
628 *val = strtoul(**argv, &endptr, 0);
629 if (*endptr) {
630 p_err("can't parse %s as %s", **argv, what);
631 return -1;
632 }
633 NEXT_ARGP();
634
635 return 0;
636}