bpftool: Support setting alternative arch for JIT disasm with LLVM
authorQuentin Monnet <quentin@isovalent.com>
Tue, 25 Oct 2022 15:03:28 +0000 (16:03 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 25 Oct 2022 17:11:56 +0000 (10:11 -0700)
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>
tools/bpf/bpftool/common.c
tools/bpf/bpftool/jit_disasm.c
tools/bpf/bpftool/main.h
tools/bpf/bpftool/prog.c

index 4c2e909a2d674f0e2c0183854200a008eef9c0c5..e4d33bc8bbbf0b9b5f0c473030e02bd2ade2416f 100644 (file)
@@ -627,12 +627,11 @@ static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
 }
 
 const char *
-ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
-                     const char **opt)
+ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
 {
+       __maybe_unused int device_id;
        char devname[IF_NAMESIZE];
        int vendor_id;
-       int device_id;
 
        if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
                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,
        }
 
        switch (vendor_id) {
+#ifdef HAVE_LIBBFD_SUPPORT
        case 0x19ee:
                device_id = read_sysfs_netdev_hex_int(devname, "device");
                if (device_id != 0x4000 &&
@@ -655,8 +655,10 @@ ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
                        p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
                *opt = "ctx4";
                return "NFP-6xxx";
+#endif /* HAVE_LIBBFD_SUPPORT */
+       /* No NFP support in LLVM, we have no valid triple to return. */
        default:
-               p_err("Can't get bfd arch name for device vendor id 0x%04x",
+               p_err("Can't get arch name for device vendor id 0x%04x",
                      vendor_id);
                return NULL;
        }
index c28b21f90cb93ff1197f17ca9e42d4eb3cbf0f88..58a5017034a2c8de7414ea53a56fe920da4ed95a 100644 (file)
@@ -84,12 +84,10 @@ init_context(disasm_ctx_t *ctx, const char *arch,
 {
        char *triple;
 
-       if (arch) {
-               p_err("Architecture %s not supported", arch);
-               return -1;
-       }
-
-       triple = LLVMGetDefaultTargetTriple();
+       if (arch)
+               triple = LLVMNormalizeTargetTriple(arch);
+       else
+               triple = LLVMGetDefaultTargetTriple();
        if (!triple) {
                p_err("Failed to retrieve triple");
                return -1;
@@ -128,8 +126,9 @@ disassemble_insn(disasm_ctx_t *ctx, unsigned char *image, ssize_t len, int pc)
 
 int disasm_init(void)
 {
-       LLVMInitializeNativeTarget();
-       LLVMInitializeNativeDisassembler();
+       LLVMInitializeAllTargetInfos();
+       LLVMInitializeAllTargetMCs();
+       LLVMInitializeAllDisassemblers();
        return 0;
 }
 #endif /* HAVE_LLVM_SUPPORT */
index 9a149c67aa5dac29e9d4b5350928533680d1a841..467d8472df0c3541784b06b6695b76eb2525a542 100644 (file)
@@ -203,8 +203,7 @@ void print_hex_data_json(uint8_t *data, size_t len);
 unsigned int get_page_size(void);
 unsigned int get_possible_cpus(void);
 const char *
-ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
-                     const char **opt);
+ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt);
 
 struct btf_dumper {
        const struct btf *btf;
index 93dfb89b85e38e85f5701d18761db736b7cad0f4..a858b907da16e0c539c30eaa25735ceabd91f453 100644 (file)
@@ -764,10 +764,8 @@ prog_dump(struct bpf_prog_info *info, enum dump_mode mode,
                const char *name = NULL;
 
                if (info->ifindex) {
-                       name = ifindex_to_bfd_params(info->ifindex,
-                                                    info->netns_dev,
-                                                    info->netns_ino,
-                                                    &disasm_opt);
+                       name = ifindex_to_arch(info->ifindex, info->netns_dev,
+                                              info->netns_ino, &disasm_opt);
                        if (!name)
                                goto exit_free;
                }