Skip to content

Commit ce4f660

Browse files
qmonnetAlexei Starovoitov
authored andcommitted
bpftool: Support setting alternative arch for JIT disasm with LLVM
For offloaded BPF programs, instead of failing to create the LLVM disassembler without even looking for a triple at all, do run the function that attempts to retrieve a valid architecture name for the device. It will still fail for the LLVM disassembler, because currently we have no valid triple to return (NFP disassembly is not supported by LLVM). But failing in that function is more logical than to assume in jit_disasm.c that passing an "arch" name is simply not supported. Suggested-by: Song Liu <song@kernel.org> Signed-off-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/r/20221025150329.97371-8-quentin@isovalent.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent eb9d1ac commit ce4f660

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

tools/bpf/bpftool/common.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,12 +627,11 @@ static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
627627
}
628628

629629
const char *
630-
ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
631-
const char **opt)
630+
ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
632631
{
632+
__maybe_unused int device_id;
633633
char devname[IF_NAMESIZE];
634634
int vendor_id;
635-
int device_id;
636635

637636
if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
638637
p_err("Can't get net device name for ifindex %d: %s", ifindex,
@@ -647,6 +646,7 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
647646
}
648647

649648
switch (vendor_id) {
649+
#ifdef HAVE_LIBBFD_SUPPORT
650650
case 0x19ee:
651651
device_id = read_sysfs_netdev_hex_int(devname, "device");
652652
if (device_id != 0x4000 &&
@@ -655,8 +655,10 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
655655
p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
656656
*opt = "ctx4";
657657
return "NFP-6xxx";
658+
#endif /* HAVE_LIBBFD_SUPPORT */
659+
/* No NFP support in LLVM, we have no valid triple to return. */
658660
default:
659-
p_err("Can't get bfd arch name for device vendor id 0x%04x",
661+
p_err("Can't get arch name for device vendor id 0x%04x",
660662
vendor_id);
661663
return NULL;
662664
}

tools/bpf/bpftool/jit_disasm.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ init_context(disasm_ctx_t *ctx, const char *arch,
8484
{
8585
char *triple;
8686

87-
if (arch) {
88-
p_err("Architecture %s not supported", arch);
89-
return -1;
90-
}
91-
92-
triple = LLVMGetDefaultTargetTriple();
87+
if (arch)
88+
triple = LLVMNormalizeTargetTriple(arch);
89+
else
90+
triple = LLVMGetDefaultTargetTriple();
9391
if (!triple) {
9492
p_err("Failed to retrieve triple");
9593
return -1;
@@ -128,8 +126,9 @@ disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc)
128126

129127
int disasm_init(void)
130128
{
131-
LLVMInitializeNativeTarget();
132-
LLVMInitializeNativeDisassembler();
129+
LLVMInitializeAllTargetInfos();
130+
LLVMInitializeAllTargetMCs();
131+
LLVMInitializeAllDisassemblers();
133132
return 0;
134133
}
135134
#endif /* HAVE_LLVM_SUPPORT */

tools/bpf/bpftool/main.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ void print_hex_data_json(uint8_t *data, size_t len);
203203
unsigned int get_page_size(void);
204204
unsigned int get_possible_cpus(void);
205205
const char *
206-
ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
207-
const char **opt);
206+
ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt);
208207

209208
struct btf_dumper {
210209
const struct btf *btf;

tools/bpf/bpftool/prog.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
764764
const char *name = NULL;
765765

766766
if (info->ifindex) {
767-
name = ifindex_to_bfd_params(info->ifindex,
768-
info->netns_dev,
769-
info->netns_ino,
770-
&disasm_opt);
767+
name = ifindex_to_arch(info->ifindex, info->netns_dev,
768+
info->netns_ino, &disasm_opt);
771769
if (!name)
772770
goto exit_free;
773771
}

0 commit comments

Comments
 (0)