Skip to content

Commit

Permalink
chore: call bpf_program__attach_iter from go (#304)
Browse files Browse the repository at this point in the history
This changes libbpfgo to call bpf_program__attach_iter directly from
go instead of doing this via a C wrapper. What this means is that
we maintained opts creation in C, but the actual libbpf attach call is
done in go.

This creates two new functions in libbpfgo.h as helpers:
- bpf_iter_attach_opts_new()
- bpf_iter_attach_opts_free()
  • Loading branch information
geyslan authored Mar 23, 2023
1 parent f591a2c commit 5af8a62
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
9 changes: 7 additions & 2 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1613,11 +1613,16 @@ func (p *BPFProg) AttachIter(opts IterOpts) (*BPFLink, error) {
tid := C.uint(opts.Tid)
pid := C.uint(opts.Pid)
pidFd := C.uint(opts.PidFd)
link, errno := C.bpf_prog_attach_iter(p.prog, mapFd, cgroupIterOrder, cgroupFd, cgroupId, tid, pid, pidFd)
cOpts, errno := C.bpf_iter_attach_opts_new(mapFd, cgroupIterOrder, cgroupFd, cgroupId, tid, pid, pidFd)
if cOpts == nil {
return nil, fmt.Errorf("failed to create iter_attach_opts to program %s: %w", p.name, errno)
}
defer C.bpf_iter_attach_opts_free(cOpts)

link, errno := C.bpf_program__attach_iter(p.prog, cOpts)
if link == nil {
return nil, fmt.Errorf("failed to attach iter to program %s: %w", p.name, errno)
}

eventName := fmt.Sprintf("iter-%s-%d", p.name, opts.MapFd)
bpfLink := &BPFLink{
link: link,
Expand Down
55 changes: 37 additions & 18 deletions libbpfgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,43 @@ int bpf_prog_detach_cgroup_legacy(
return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
}

struct bpf_link *bpf_prog_attach_iter(struct bpf_program *prog, __u32 map_fd,
enum bpf_cgroup_iter_order order,
__u32 cgroup_fd, __u64 cgroup_id,
__u32 tid, __u32 pid, __u32 pid_fd) {
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
union bpf_iter_link_info linfo;
memset(&linfo, 0, sizeof(linfo));
linfo.map.map_fd = map_fd;
linfo.cgroup.order = order;
linfo.cgroup.cgroup_fd = cgroup_fd;
linfo.cgroup.cgroup_id = cgroup_id;
linfo.task.tid = tid;
linfo.task.pid = pid;
linfo.task.pid_fd = pid_fd;
opts.link_info = &linfo;
opts.link_info_len = sizeof(linfo);

return bpf_program__attach_iter(prog, &opts);
struct bpf_iter_attach_opts *
bpf_iter_attach_opts_new(__u32 map_fd, enum bpf_cgroup_iter_order order,
__u32 cgroup_fd, __u64 cgroup_id, __u32 tid, __u32 pid,
__u32 pid_fd) {
union bpf_iter_link_info *linfo;
linfo = calloc(1, sizeof(*linfo));
if (!linfo)
return NULL;

linfo->map.map_fd = map_fd;
linfo->cgroup.order = order;
linfo->cgroup.cgroup_fd = cgroup_fd;
linfo->cgroup.cgroup_id = cgroup_id;
linfo->task.tid = tid;
linfo->task.pid = pid;
linfo->task.pid_fd = pid_fd;

struct bpf_iter_attach_opts *opts;
opts = calloc(1, sizeof(*opts));
if (!opts) {
free(linfo);
return NULL;
}

opts->sz = sizeof(*opts);
opts->link_info_len = sizeof(*linfo);
opts->link_info = linfo;

return opts;
}

void bpf_iter_attach_opts_free(struct bpf_iter_attach_opts *opts) {
if (!opts)
return;

free(opts->link_info);
free(opts);
}

struct bpf_object *open_bpf_object(char *btf_file_path, char *kconfig_path,
Expand Down

0 comments on commit 5af8a62

Please sign in to comment.