From 721aff69cb12d56b672f240c136ce147ebe67cc3 Mon Sep 17 00:00:00 2001 From: umanwizard Date: Tue, 2 Apr 2024 08:08:07 -0400 Subject: [PATCH] Add NewModuleArgs's KernelLogLevel arg option (#412) Optionally enable kernel logging using KernelLogLevel arg option. --- libbpfgo.c | 4 +++- libbpfgo.h | 3 ++- module.go | 8 ++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libbpfgo.c b/libbpfgo.c index e7dd5e1c..053711e1 100644 --- a/libbpfgo.c +++ b/libbpfgo.c @@ -167,7 +167,8 @@ void cgo_bpf_iter_attach_opts_free(struct bpf_iter_attach_opts *opts) struct bpf_object_open_opts *cgo_bpf_object_open_opts_new(const char *btf_file_path, const char *kconfig_path, - const char *bpf_obj_name) + const char *bpf_obj_name, + __u32 kernel_log_level) { struct bpf_object_open_opts *opts; opts = calloc(1, sizeof(*opts)); @@ -178,6 +179,7 @@ struct bpf_object_open_opts *cgo_bpf_object_open_opts_new(const char *btf_file_p opts->btf_custom_path = btf_file_path; opts->kconfig = kconfig_path; opts->object_name = bpf_obj_name; + opts->kernel_log_level = kernel_log_level; return opts; } diff --git a/libbpfgo.h b/libbpfgo.h index 44e22b2e..de936ec4 100644 --- a/libbpfgo.h +++ b/libbpfgo.h @@ -43,7 +43,8 @@ void cgo_bpf_iter_attach_opts_free(struct bpf_iter_attach_opts *opts); struct bpf_object_open_opts *cgo_bpf_object_open_opts_new(const char *btf_file_path, const char *kconfig_path, - const char *bpf_obj_name); + const char *bpf_obj_name, + __u32 kernel_log_level); void cgo_bpf_object_open_opts_free(struct bpf_object_open_opts *opts); struct bpf_map_create_opts *cgo_bpf_map_create_opts_new(__u32 btf_fd, diff --git a/module.go b/module.go index c454cfd8..35ae6509 100644 --- a/module.go +++ b/module.go @@ -40,6 +40,7 @@ type NewModuleArgs struct { BPFObjPath string BPFObjBuff []byte SkipMemlockBump bool + KernelLogLevel uint32 } func NewModuleFromFile(bpfObjPath string) (*Module, error) { @@ -77,7 +78,9 @@ func NewModuleFromFileArgs(args NewModuleArgs) (*Module, error) { defer C.free(unsafe.Pointer(kconfigPathC)) } - optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kconfigPathC, nil) + kernelLogLevelC := C.uint(args.KernelLogLevel) + + optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kconfigPathC, nil, kernelLogLevelC) if optsC == nil { return nil, fmt.Errorf("failed to create bpf_object_open_opts: %w", errno) } @@ -129,12 +132,13 @@ func NewModuleFromBufferArgs(args NewModuleArgs) (*Module, error) { bpfBuffC := unsafe.Pointer(C.CBytes(args.BPFObjBuff)) defer C.free(bpfBuffC) bpfBuffSizeC := C.size_t(len(args.BPFObjBuff)) + kernelLogLevelC := C.uint(args.KernelLogLevel) if len(args.KConfigFilePath) <= 2 { kConfigPathC = nil } - optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kConfigPathC, bpfObjNameC) + optsC, errno := C.cgo_bpf_object_open_opts_new(btfFilePathC, kConfigPathC, bpfObjNameC, kernelLogLevelC) if optsC == nil { return nil, fmt.Errorf("failed to create bpf_object_open_opts: %w", errno) }