Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: call bpf_object__open_mem from go #314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,26 +382,32 @@ func NewModuleFromBufferArgs(args NewModuleArgs) (*Module, error) {
args.BTFObjPath = "/sys/kernel/btf/vmlinux"
}

CBTFFilePath := C.CString(args.BTFObjPath)
CKconfigPath := C.CString(args.KConfigFilePath)
CBPFObjName := C.CString(args.BPFObjName)
CBPFBuff := unsafe.Pointer(C.CBytes(args.BPFObjBuff))
CBPFBuffSize := C.size_t(len(args.BPFObjBuff))
cBTFFilePath := C.CString(args.BTFObjPath)
defer C.free(unsafe.Pointer(cBTFFilePath))
cKconfigPath := C.CString(args.KConfigFilePath)
defer C.free(unsafe.Pointer(cKconfigPath))
cBPFObjName := C.CString(args.BPFObjName)
defer C.free(unsafe.Pointer(cBPFObjName))
cBPFBuff := unsafe.Pointer(C.CBytes(args.BPFObjBuff))
defer C.free(cBPFBuff)
cBPFBuffSize := C.size_t(len(args.BPFObjBuff))

if len(args.KConfigFilePath) <= 2 {
C.free(unsafe.Pointer(CKconfigPath))
CKconfigPath = nil
C.free(unsafe.Pointer(cKconfigPath))
cKconfigPath = nil
}

obj, errno := C.open_bpf_object(CBTFFilePath, CKconfigPath, CBPFObjName, CBPFBuff, CBPFBuffSize)
cOpts, errno := C.bpf_object_open_opts_new(cBTFFilePath, cKconfigPath, cBPFObjName)
if cOpts == nil {
return nil, fmt.Errorf("failed to create bpf_object_open_opts to %s: %w", args.BPFObjName, errno)
}
defer C.bpf_object_open_opts_free(cOpts)

obj, errno := C.bpf_object__open_mem(cBPFBuff, cBPFBuffSize, cOpts)
if obj == nil {
return nil, fmt.Errorf("failed to open BPF object %s: %w", args.BPFObjName, errno)
}

C.free(CBPFBuff)
C.free(unsafe.Pointer(CBPFObjName))
C.free(unsafe.Pointer(CBTFFilePath))

return &Module{
obj: obj,
elf: f,
Expand Down
34 changes: 16 additions & 18 deletions libbpfgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,25 @@ void bpf_iter_attach_opts_free(struct bpf_iter_attach_opts *opts)
free(opts);
}

struct bpf_object *open_bpf_object(char *btf_file_path,
char *kconfig_path,
char *bpf_obj_name,
const void *obj_buf,
size_t obj_buf_size)
struct bpf_object_open_opts *
bpf_object_open_opts_new(char *btf_file_path, char *kconfig_path, char *bpf_obj_name)
{
struct bpf_object_open_opts opts = {};
opts.btf_custom_path = btf_file_path;
opts.kconfig = kconfig_path;
opts.object_name = bpf_obj_name;
opts.sz = sizeof(opts);

struct bpf_object *obj = bpf_object__open_mem(obj_buf, obj_buf_size, &opts);
if (obj == NULL) {
int saved_errno = errno;
fprintf(stderr, "Failed to open bpf object: %s\n", strerror(errno));
errno = saved_errno;
struct bpf_object_open_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts)
return NULL;
}

return obj;
opts->sz = sizeof(*opts);
opts->btf_custom_path = btf_file_path;
opts->kconfig = kconfig_path;
opts->object_name = bpf_obj_name;

return opts;
}

void bpf_object_open_opts_free(struct bpf_object_open_opts *opts)
{
free(opts);
}

#endif