aboutsummaryrefslogtreecommitdiffstats
path: root/tools/bpf/bpftool
diff options
context:
space:
mode:
authorJiong Wang <jiong.wang@netronome.com>2018-01-16 19:05:21 -0500
committerDaniel Borkmann <daniel@iogearbox.net>2018-01-17 19:26:15 -0500
commite65935969d0fac9df28d9c49bdbab5d8d8286a20 (patch)
tree173640edb8a446423ab1dd4e00add1c782c17048 /tools/bpf/bpftool
parenteb1d7db927a9653f1402473c777839e0456a7836 (diff)
tools: bpftool: improve architecture detection by using ifindex
The current architecture detection method in bpftool is designed for host case. For offload case, we can't use the architecture of "bpftool" itself. Instead, we could call the existing "ifindex_to_name_ns" to get DEVNAME, then read pci id from /sys/class/dev/DEVNAME/device/vendor, finally we map vendor id to bfd arch name which will finally be used to select bfd backend for the disassembler. Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/bpf/bpftool')
-rw-r--r--tools/bpf/bpftool/common.c72
-rw-r--r--tools/bpf/bpftool/jit_disasm.c16
-rw-r--r--tools/bpf/bpftool/main.h5
-rw-r--r--tools/bpf/bpftool/prog.c12
4 files changed, 102 insertions, 3 deletions
diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index 6601c95a9258..0b482c0070e0 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -34,6 +34,7 @@
34/* Author: Jakub Kicinski <kubakici@wp.pl> */ 34/* Author: Jakub Kicinski <kubakici@wp.pl> */
35 35
36#include <errno.h> 36#include <errno.h>
37#include <fcntl.h>
37#include <fts.h> 38#include <fts.h>
38#include <libgen.h> 39#include <libgen.h>
39#include <mntent.h> 40#include <mntent.h>
@@ -433,6 +434,77 @@ ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
433 return if_indextoname(ifindex, buf); 434 return if_indextoname(ifindex, buf);
434} 435}
435 436
437static int read_sysfs_hex_int(char *path)
438{
439 char vendor_id_buf[8];
440 int len;
441 int fd;
442
443 fd = open(path, O_RDONLY);
444 if (fd < 0) {
445 p_err("Can't open %s: %s", path, strerror(errno));
446 return -1;
447 }
448
449 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
450 close(fd);
451 if (len < 0) {
452 p_err("Can't read %s: %s", path, strerror(errno));
453 return -1;
454 }
455 if (len >= (int)sizeof(vendor_id_buf)) {
456 p_err("Value in %s too long", path);
457 return -1;
458 }
459
460 vendor_id_buf[len] = 0;
461
462 return strtol(vendor_id_buf, NULL, 0);
463}
464
465static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
466{
467 char full_path[64];
468
469 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
470 devname, entry_name);
471
472 return read_sysfs_hex_int(full_path);
473}
474
475const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino)
476{
477 char devname[IF_NAMESIZE];
478 int vendor_id;
479 int device_id;
480
481 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
482 p_err("Can't get net device name for ifindex %d: %s", ifindex,
483 strerror(errno));
484 return NULL;
485 }
486
487 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
488 if (vendor_id < 0) {
489 p_err("Can't get device vendor id for %s", devname);
490 return NULL;
491 }
492
493 switch (vendor_id) {
494 case 0x19ee:
495 device_id = read_sysfs_netdev_hex_int(devname, "device");
496 if (device_id != 0x4000 &&
497 device_id != 0x6000 &&
498 device_id != 0x6003)
499 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
500 return "NFP-6xxx";
501 default:
502 p_err("Can't get bfd arch name for device vendor id 0x%04x",
503 vendor_id);
504 return NULL;
505 }
506}
507
436void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) 508void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
437{ 509{
438 char name[IF_NAMESIZE]; 510 char name[IF_NAMESIZE];
diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c
index 57d32e8a1391..87439320ef70 100644
--- a/tools/bpf/bpftool/jit_disasm.c
+++ b/tools/bpf/bpftool/jit_disasm.c
@@ -76,7 +76,8 @@ static int fprintf_json(void *out, const char *fmt, ...)
76 return 0; 76 return 0;
77} 77}
78 78
79void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes) 79void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
80 const char *arch)
80{ 81{
81 disassembler_ftype disassemble; 82 disassembler_ftype disassemble;
82 struct disassemble_info info; 83 struct disassemble_info info;
@@ -100,6 +101,19 @@ void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
100 else 101 else
101 init_disassemble_info(&info, stdout, 102 init_disassemble_info(&info, stdout,
102 (fprintf_ftype) fprintf); 103 (fprintf_ftype) fprintf);
104
105 /* Update architecture info for offload. */
106 if (arch) {
107 const bfd_arch_info_type *inf = bfd_scan_arch(arch);
108
109 if (inf) {
110 bfdf->arch_info = inf;
111 } else {
112 p_err("No libfd support for %s", arch);
113 return;
114 }
115 }
116
103 info.arch = bfd_get_arch(bfdf); 117 info.arch = bfd_get_arch(bfdf);
104 info.mach = bfd_get_mach(bfdf); 118 info.mach = bfd_get_mach(bfdf);
105 info.buffer = image; 119 info.buffer = image;
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 65b526fe6e7e..b8e9584d6246 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -121,7 +121,10 @@ int do_cgroup(int argc, char **arg);
121 121
122int prog_parse_fd(int *argc, char ***argv); 122int prog_parse_fd(int *argc, char ***argv);
123 123
124void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes); 124void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
125 const char *arch);
125void print_hex_data_json(uint8_t *data, size_t len); 126void print_hex_data_json(uint8_t *data, size_t len);
126 127
128const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino);
129
127#endif 130#endif
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index 099e21cf1b5c..e8e2baaf93c2 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -776,7 +776,17 @@ static int do_dump(int argc, char **argv)
776 } 776 }
777 } else { 777 } else {
778 if (member_len == &info.jited_prog_len) { 778 if (member_len == &info.jited_prog_len) {
779 disasm_print_insn(buf, *member_len, opcodes); 779 const char *name = NULL;
780
781 if (info.ifindex) {
782 name = ifindex_to_bfd_name_ns(info.ifindex,
783 info.netns_dev,
784 info.netns_ino);
785 if (!name)
786 goto err_free;
787 }
788
789 disasm_print_insn(buf, *member_len, opcodes, name);
780 } else { 790 } else {
781 kernel_syms_load(&dd); 791 kernel_syms_load(&dd);
782 if (json_output) 792 if (json_output)