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

API functions for pinning bpf programs #96

Merged
merged 1 commit into from
Dec 13, 2021
Merged
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
39 changes: 36 additions & 3 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ type BPFMap struct {
}

type BPFProg struct {
name string
prog *C.struct_bpf_program
module *Module
name string
prog *C.struct_bpf_program
module *Module
pinnedPath string
}

type LinkType int
Expand Down Expand Up @@ -690,6 +691,34 @@ func (p *BPFProg) GetFd() int {
return int(C.bpf_program__fd(p.prog))
}

func (p *BPFProg) Pin(path string) error {

absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("invalid path: %s: %v", path, err)
}

cs := C.CString(absPath)
cErr := C.bpf_program__pin(p.prog, cs)
C.free(unsafe.Pointer(cs))
if cErr != 0 {
return fmt.Errorf("failed to pin program %s to %s", p.name, path)
}
p.pinnedPath = absPath
return nil
}

func (p *BPFProg) Unpin(path string) error {
cs := C.CString(path)
err := C.bpf_program__unpin(p.prog, cs)
C.free(unsafe.Pointer(cs))
if err != 0 {
return fmt.Errorf("failed to unpin program %s to %s", p.name, path)
}
p.pinnedPath = ""
return nil
}

func (p *BPFProg) GetModule() *Module {
return p.module
}
Expand All @@ -698,6 +727,10 @@ func (p *BPFProg) GetName() string {
return p.name
}

func (p *BPFProg) GetPinPath() string {
return p.pinnedPath
}

// BPFProgType is an enum as defined in https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/bpf.h
type BPFProgType uint32

Expand Down