Skip to content

Commit

Permalink
Add NewModuleArgs's KernelLogLevel arg option (#412)
Browse files Browse the repository at this point in the history
Optionally enable kernel logging using KernelLogLevel arg option.
  • Loading branch information
umanwizard committed Apr 2, 2024
1 parent 3c5cd66 commit 721aff6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion libbpfgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion libbpfgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type NewModuleArgs struct {
BPFObjPath string
BPFObjBuff []byte
SkipMemlockBump bool
KernelLogLevel uint32
}

func NewModuleFromFile(bpfObjPath string) (*Module, error) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 721aff6

Please sign in to comment.