[RFC,bpf-next,2/2] tools: bpftool: improve architecture detection by using offload arch info

Message ID 20180112220624.14424-2-jakub.kicinski@netronome.com
State RFC
Delegated to: BPF Maintainers
Headers show
Series
  • [RFC,bpf-next,1/2] nfp: bpf: set new jit info fields
Related show

Commit Message

Jakub Kicinski Jan. 12, 2018, 10:06 p.m.
From: Jiong Wang <jiong.wang@netronome.com>

The current architecture detection method in bpftool is designed for host
case.

For offload case, we can't use the architecture of "bpftool" itself. We
use the new arch name field in bpf_prog_info and use bfd_scan_arch to
return the correct bfd arch.

Signed-off-by: Jiong Wang <jiong.wang@netronome.com>
---
 tools/bpf/bpftool/jit_disasm.c | 16 +++++++++++++++-
 tools/bpf/bpftool/main.h       |  3 ++-
 tools/bpf/bpftool/prog.c       |  7 ++++++-
 3 files changed, 23 insertions(+), 3 deletions(-)

Patch

diff --git a/tools/bpf/bpftool/jit_disasm.c b/tools/bpf/bpftool/jit_disasm.c
index 57d32e8a1391..a54fc0695a50 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, ...)
 	return 0;
 }
 
-void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
+void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
+		       char *arch)
 {
 	disassembler_ftype disassemble;
 	struct disassemble_info info;
@@ -100,6 +101,19 @@  void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes)
 	else
 		init_disassemble_info(&info, stdout,
 				      (fprintf_ftype) fprintf);
+
+	/* Update architecture info for offload. */
+	if (arch) {
+		const bfd_arch_info_type *inf = bfd_scan_arch(arch);
+
+		if (inf) {
+			bfdf->arch_info = inf;
+		} else {
+			p_err("No libfd support for %s", arch);
+			return;
+		}
+	}
+
 	info.arch = bfd_get_arch(bfdf);
 	info.mach = bfd_get_mach(bfdf);
 	info.buffer = image;
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 65b526fe6e7e..0c2898a68340 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -121,7 +121,8 @@  int do_cgroup(int argc, char **arg);
 
 int prog_parse_fd(int *argc, char ***argv);
 
-void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes);
+void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
+		       char *arch);
 void print_hex_data_json(uint8_t *data, size_t len);
 
 #endif
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index c6a28be4665c..97c2649f71f8 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -775,7 +775,12 @@  static int do_dump(int argc, char **argv)
 		}
 	} else {
 		if (member_len == &info.jited_prog_len) {
-			disasm_print_insn(buf, *member_len, opcodes);
+			if (info.ifindex)
+				disasm_print_insn(buf, *member_len, opcodes,
+						  info.offload_arch_name);
+			else
+				disasm_print_insn(buf, *member_len, opcodes,
+						  NULL);
 		} else {
 			kernel_syms_load(&dd);
 			if (json_output)