Skip to content

Commit 4936ad3

Browse files
chentao-kernelKernel Patches Daemon
authored andcommitted
libbpf: Add libbpf_probe_bpf_kfunc API
Similarly to libbpf_probe_bpf_helper, the libbpf_probe_bpf_kfunc used to test the availability of the different eBPF kfuncs on the current system. Signed-off-by: Tao Chen <[email protected]>
1 parent e3ea742 commit 4936ad3

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

tools/lib/bpf/libbpf.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,23 @@ LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void
16801680
*/
16811681
LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,
16821682
enum bpf_func_id helper_id, const void *opts);
1683-
1683+
/**
1684+
* @brief **libbpf_probe_bpf_kfunc()** detects if host kernel supports the
1685+
* use of a given BPF kfunc from specified BPF program type.
1686+
* @param prog_type BPF program type used to check the support of BPF kfunc
1687+
* @param kfunc_id The btf ID of BPF kfunc to check support for
1688+
* @param btf_fd The module BTF FD, if kfunc is defined in kernel module,
1689+
* btf_fd is used to point to module's BTF, 0 means kfunc defined in vmlinux.
1690+
* @param opts reserved for future extensibility, should be NULL
1691+
* @return 1, if given combination of program type and kfunc is supported; 0,
1692+
* if the combination is not supported; negative error code if feature
1693+
* detection for provided input arguments failed or can't be performed
1694+
*
1695+
* Make sure the process has required set of CAP_* permissions (or runs as
1696+
* root) when performing feature checking.
1697+
*/
1698+
LIBBPF_API int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type,
1699+
int kfunc_id, int btf_fd, const void *opts);
16841700
/**
16851701
* @brief **libbpf_num_possible_cpus()** is a helper function to get the
16861702
* number of possible CPUs that the host kernel supports and expects.

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,5 @@ LIBBPF_1.6.0 {
436436
bpf_linker__add_buf;
437437
bpf_linker__add_fd;
438438
bpf_linker__new_fd;
439+
libbpf_probe_bpf_kfunc;
439440
} LIBBPF_1.5.0;

tools/lib/bpf/libbpf_probes.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,61 @@ static bool can_probe_prog_type(enum bpf_prog_type prog_type)
433433
return true;
434434
}
435435

436+
int libbpf_probe_bpf_kfunc(enum bpf_prog_type prog_type, int kfunc_id, int btf_fd,
437+
const void *opts)
438+
{
439+
struct bpf_insn insns[] = {
440+
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, BPF_PSEUDO_KFUNC_CALL, btf_fd, kfunc_id),
441+
BPF_EXIT_INSN(),
442+
};
443+
const size_t insn_cnt = ARRAY_SIZE(insns);
444+
char buf[4096];
445+
int *fd_array = NULL;
446+
size_t fd_array_cnt = 0, fd_array_cap = fd_array_cnt;
447+
int ret;
448+
449+
if (opts)
450+
return libbpf_err(-EINVAL);
451+
452+
if (!can_probe_prog_type(prog_type))
453+
return -EOPNOTSUPP;
454+
455+
if (btf_fd) {
456+
ret = libbpf_ensure_mem((void **)&fd_array, &fd_array_cap,
457+
sizeof(int), fd_array_cnt + btf_fd);
458+
if (ret)
459+
return ret;
460+
461+
/* In kernel, obtain the btf fd by means of the offset of
462+
* the fd_array, and the offset is the btf fd.
463+
*/
464+
fd_array[btf_fd] = btf_fd;
465+
}
466+
467+
buf[0] = '\0';
468+
ret = probe_prog_load(prog_type, insns, insn_cnt, fd_array,
469+
fd_array_cnt, buf, sizeof(buf));
470+
if (ret < 0) {
471+
free(fd_array);
472+
return libbpf_err(ret);
473+
}
474+
475+
free(fd_array);
476+
/* If BPF verifier recognizes BPF kfunc but it's not supported for
477+
* given BPF program type, it will emit "calling kernel function
478+
* bpf_cpumask_create is not allowed", if the kfunc id is invalid,
479+
* it will emit "kernel btf_id 4294967295 is not a function". If btf fd
480+
* invalid in module btf, it will emit "invalid module BTF fd specified" or
481+
* "negative offset disallowed for kernel module function call"
482+
*/
483+
if (ret == 0 && (strstr(buf, "not allowed") || strstr(buf, "not a function") ||
484+
(strstr(buf, "invalid module BTF fd")) ||
485+
(strstr(buf, "negative offset disallowed"))))
486+
return 0;
487+
488+
return 1; /* assume supported */
489+
}
490+
436491
int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
437492
const void *opts)
438493
{

0 commit comments

Comments
 (0)