libbpf: Allow passing user log setting through bpf_object_open_opts
Allow users to provide their own custom log_buf, log_size, and log_level at bpf_object level through bpf_object_open_opts. This log_buf will be used during BTF loading. Subsequent patch will use same log_buf during BPF program loading, unless overriden at per-bpf_program level. When such custom log_buf is provided, libbpf won't be attempting retrying loading of BTF to try to provide its own log buffer to capture kernel's error log output. User is responsible to provide big enough buffer, otherwise they run a risk of getting -ENOSPC error from the bpf() syscall. See also comments in bpf_object_open_opts regarding log_level and log_buf interactions. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20211209193840.1248570-5-andrii@kernel.org
This commit is contained in:
parent
1a190d1e8e
commit
e0e3ea888c
|
@ -195,8 +195,9 @@ struct bpf_load_program_attr {
|
|||
/* Flags to direct loading requirements */
|
||||
#define MAPS_RELAX_COMPAT 0x01
|
||||
|
||||
/* Recommend log buffer size */
|
||||
/* Recommended log buffer size */
|
||||
#define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
|
||||
|
||||
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_prog_load() instead")
|
||||
LIBBPF_API int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
|
||||
char *log_buf, size_t log_buf_sz);
|
||||
|
|
|
@ -573,6 +573,11 @@ struct bpf_object {
|
|||
size_t btf_module_cnt;
|
||||
size_t btf_module_cap;
|
||||
|
||||
/* optional log settings passed to BPF_BTF_LOAD and BPF_PROG_LOAD commands */
|
||||
char *log_buf;
|
||||
size_t log_size;
|
||||
__u32 log_level;
|
||||
|
||||
void *priv;
|
||||
bpf_object_clear_priv_t clear_priv;
|
||||
|
||||
|
@ -3017,7 +3022,9 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
|
|||
*/
|
||||
btf__set_fd(kern_btf, 0);
|
||||
} else {
|
||||
err = btf__load_into_kernel(kern_btf);
|
||||
/* currently BPF_BTF_LOAD only supports log_level 1 */
|
||||
err = btf_load_into_kernel(kern_btf, obj->log_buf, obj->log_size,
|
||||
obj->log_level ? 1 : 0);
|
||||
}
|
||||
if (sanitize) {
|
||||
if (!err) {
|
||||
|
@ -6932,6 +6939,9 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
|
|||
struct bpf_object *obj;
|
||||
char tmp_name[64];
|
||||
int err;
|
||||
char *log_buf;
|
||||
size_t log_size;
|
||||
__u32 log_level;
|
||||
|
||||
if (elf_version(EV_CURRENT) == EV_NONE) {
|
||||
pr_warn("failed to init libelf for %s\n",
|
||||
|
@ -6954,10 +6964,22 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
|
|||
pr_debug("loading object '%s' from buffer\n", obj_name);
|
||||
}
|
||||
|
||||
log_buf = OPTS_GET(opts, kernel_log_buf, NULL);
|
||||
log_size = OPTS_GET(opts, kernel_log_size, 0);
|
||||
log_level = OPTS_GET(opts, kernel_log_level, 0);
|
||||
if (log_size > UINT_MAX)
|
||||
return ERR_PTR(-EINVAL);
|
||||
if (log_size && !log_buf)
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
||||
obj = bpf_object__new(path, obj_buf, obj_buf_sz, obj_name);
|
||||
if (IS_ERR(obj))
|
||||
return obj;
|
||||
|
||||
obj->log_buf = log_buf;
|
||||
obj->log_size = log_size;
|
||||
obj->log_level = log_level;
|
||||
|
||||
btf_tmp_path = OPTS_GET(opts, btf_custom_path, NULL);
|
||||
if (btf_tmp_path) {
|
||||
if (strlen(btf_tmp_path) >= PATH_MAX) {
|
||||
|
|
|
@ -108,8 +108,47 @@ struct bpf_object_open_opts {
|
|||
* struct_ops, etc) will need actual kernel BTF at /sys/kernel/btf/vmlinux.
|
||||
*/
|
||||
const char *btf_custom_path;
|
||||
/* Pointer to a buffer for storing kernel logs for applicable BPF
|
||||
* commands. Valid kernel_log_size has to be specified as well and are
|
||||
* passed-through to bpf() syscall. Keep in mind that kernel might
|
||||
* fail operation with -ENOSPC error if provided buffer is too small
|
||||
* to contain entire log output.
|
||||
* See the comment below for kernel_log_level for interaction between
|
||||
* log_buf and log_level settings.
|
||||
*
|
||||
* If specified, this log buffer will be passed for:
|
||||
* - each BPF progral load (BPF_PROG_LOAD) attempt, unless overriden
|
||||
* with bpf_program__set_log() on per-program level, to get
|
||||
* BPF verifier log output.
|
||||
* - during BPF object's BTF load into kernel (BPF_BTF_LOAD) to get
|
||||
* BTF sanity checking log.
|
||||
*
|
||||
* Each BPF command (BPF_BTF_LOAD or BPF_PROG_LOAD) will overwrite
|
||||
* previous contents, so if you need more fine-grained control, set
|
||||
* per-program buffer with bpf_program__set_log_buf() to preserve each
|
||||
* individual program's verification log. Keep using kernel_log_buf
|
||||
* for BTF verification log, if necessary.
|
||||
*/
|
||||
char *kernel_log_buf;
|
||||
size_t kernel_log_size;
|
||||
/*
|
||||
* Log level can be set independently from log buffer. Log_level=0
|
||||
* means that libbpf will attempt loading BTF or program without any
|
||||
* logging requested, but will retry with either its own or custom log
|
||||
* buffer, if provided, and log_level=1 on any error.
|
||||
* And vice versa, setting log_level>0 will request BTF or prog
|
||||
* loading with verbose log from the first attempt (and as such also
|
||||
* for successfully loaded BTF or program), and the actual log buffer
|
||||
* could be either libbpf's own auto-allocated log buffer, if
|
||||
* kernel_log_buffer is NULL, or user-provided custom kernel_log_buf.
|
||||
* If user didn't provide custom log buffer, libbpf will emit captured
|
||||
* logs through its print callback.
|
||||
*/
|
||||
__u32 kernel_log_level;
|
||||
|
||||
size_t :0;
|
||||
};
|
||||
#define bpf_object_open_opts__last_field btf_custom_path
|
||||
#define bpf_object_open_opts__last_field kernel_log_level
|
||||
|
||||
LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
|
||||
|
||||
|
|
Loading…
Reference in New Issue