File: load.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (42 lines) | stat: -rw-r--r-- 1,041 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package pin

import (
	"fmt"
	"io"

	"github.com/cilium/ebpf"
	"github.com/cilium/ebpf/internal/sys"
	"github.com/cilium/ebpf/link"
)

// Pinner is an interface implemented by all eBPF objects that support pinning
// to a bpf virtual filesystem.
type Pinner interface {
	io.Closer
	Pin(string) error
}

// Load retrieves a pinned object from a bpf virtual filesystem. It returns one
// of [ebpf.Map], [ebpf.Program], or [link.Link].
//
// Trying to open anything other than a bpf object is an error.
func Load(path string, opts *ebpf.LoadPinOptions) (Pinner, error) {
	fd, typ, err := sys.ObjGetTyped(&sys.ObjGetAttr{
		Pathname:  sys.NewStringPointer(path),
		FileFlags: opts.Marshal(),
	})
	if err != nil {
		return nil, fmt.Errorf("opening pin %s: %w", path, err)
	}

	switch typ {
	case sys.BPF_TYPE_MAP:
		return ebpf.NewMapFromFD(fd.Disown())
	case sys.BPF_TYPE_PROG:
		return ebpf.NewProgramFromFD(fd.Disown())
	case sys.BPF_TYPE_LINK:
		return link.NewFromFD(fd.Disown())
	}

	return nil, fmt.Errorf("unknown object type %d", typ)
}