Skip to content

Commit 1ae7a84

Browse files
mrpreAlexei Starovoitov
authored andcommitted
bpftool: Add support for custom BTF path in prog load/loadall
This patch exposes the btf_custom_path feature to bpftool, allowing users to specify a custom BTF file when loading BPF programs using prog load or prog loadall commands. The argument 'btf_custom_path' in libbpf is used for those kernels that don't have CONFIG_DEBUG_INFO_BTF enabled but still want to perform CO-RE relocations. Suggested-by: Quentin Monnet <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Jiayuan Chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 92de53d commit 1ae7a84

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

tools/bpf/bpftool/Documentation/bpftool-prog.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PROG COMMANDS
3131
| **bpftool** **prog dump xlated** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] [**visual**] }]
3232
| **bpftool** **prog dump jited** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] }]
3333
| **bpftool** **prog pin** *PROG* *FILE*
34-
| **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_dev** } *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
34+
| **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_dev** } *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**] [**kernel_btf** *BTF_FILE*]
3535
| **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
3636
| **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
3737
| **bpftool** **prog tracelog**
@@ -127,7 +127,7 @@ bpftool prog pin *PROG* *FILE*
127127
Note: *FILE* must be located in *bpffs* mount. It must not contain a dot
128128
character ('.'), which is reserved for future extensions of *bpffs*.
129129

130-
bpftool prog { load | loadall } *OBJ* *PATH* [type *TYPE*] [map { idx *IDX* | name *NAME* } *MAP*] [{ offload_dev | xdpmeta_dev } *NAME*] [pinmaps *MAP_DIR*] [autoattach]
130+
bpftool prog { load | loadall } *OBJ* *PATH* [type *TYPE*] [map { idx *IDX* | name *NAME* } *MAP*] [{ offload_dev | xdpmeta_dev } *NAME*] [pinmaps *MAP_DIR*] [autoattach] [kernel_btf *BTF_FILE*]
131131
Load bpf program(s) from binary *OBJ* and pin as *PATH*. **bpftool prog
132132
load** pins only the first program from the *OBJ* as *PATH*. **bpftool prog
133133
loadall** pins all programs from the *OBJ* under *PATH* directory. **type**
@@ -153,6 +153,12 @@ bpftool prog { load | loadall } *OBJ* *PATH* [type *TYPE*] [map { idx *IDX* | na
153153
program does not support autoattach, bpftool falls back to regular pinning
154154
for that program instead.
155155

156+
The **kernel_btf** option allows specifying an external BTF file to replace
157+
the system's own vmlinux BTF file for CO-RE relocations. Note that any
158+
other feature relying on BTF (such as fentry/fexit programs, struct_ops)
159+
requires the BTF file for the actual kernel running on the host, often
160+
exposed at /sys/kernel/btf/vmlinux.
161+
156162
Note: *PATH* must be located in *bpffs* mount. It must not contain a dot
157163
character ('.'), which is reserved for future extensions of *bpffs*.
158164

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,13 @@ _bpftool()
505505
_bpftool_get_map_names
506506
return 0
507507
;;
508-
pinned|pinmaps)
508+
pinned|pinmaps|kernel_btf)
509509
_filedir
510510
return 0
511511
;;
512512
*)
513513
COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
514-
_bpftool_once_attr 'type pinmaps autoattach'
514+
_bpftool_once_attr 'type pinmaps autoattach kernel_btf'
515515
_bpftool_one_of_list 'offload_dev xdpmeta_dev'
516516
return 0
517517
;;

tools/bpf/bpftool/prog.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,17 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
16811681
} else if (is_prefix(*argv, "autoattach")) {
16821682
auto_attach = true;
16831683
NEXT_ARG();
1684+
} else if (is_prefix(*argv, "kernel_btf")) {
1685+
NEXT_ARG();
1686+
1687+
if (!REQ_ARGS(1))
1688+
goto err_free_reuse_maps;
1689+
1690+
open_opts.btf_custom_path = GET_ARG();
16841691
} else {
1685-
p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
1692+
p_err("expected no more arguments, "
1693+
"'type', 'map', 'offload_dev', 'xdpmeta_dev', 'pinmaps', "
1694+
"'autoattach', or 'kernel_btf', got: '%s'?",
16861695
*argv);
16871696
goto err_free_reuse_maps;
16881697
}
@@ -2474,6 +2483,7 @@ static int do_help(int argc, char **argv)
24742483
" [map { idx IDX | name NAME } MAP]\\\n"
24752484
" [pinmaps MAP_DIR]\n"
24762485
" [autoattach]\n"
2486+
" [kernel_btf BTF_FILE]\n"
24772487
" %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
24782488
" %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
24792489
" %1$s %2$s run PROG \\\n"

0 commit comments

Comments
 (0)