-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbpf.go
58 lines (48 loc) · 1.8 KB
/
bpf.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Package bpf provides access to compiled eBPF ELF objects.
package bpf
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
// EnvBpfPath declares an environment variable to explicitly specify a directory containing compiled eBPF ELF objects.
const EnvBpfPath = "NDNDPDK_BPF_PATH"
// Kind indicates the kind of compiled eBPF ELF object.
type Kind string
// Kind definitions.
const (
Strategy Kind = "strategy"
XDP Kind = "xdp"
)
// Find determines the filesystem path of a compiled eBPF ELF object.
// It first constructs a filename from the kind and short name.
// It then searches the file in the following locations:
// 1. The path specified in NDNDPDK_BPF_PATH environ.
// 2. ../lib/bpf relative to the executable.
// From /usr/local/bin/ndndpdk-svc, this step looks for eBPF objects in /usr/local/lib/bpf.
// 3. build/lib/bpf in the source tree.
// This is used in unit tests, and is skipped if the executable is installed under /usr.
func (kind Kind) Find(name string) (path string, e error) {
filename := "ndndpdk-" + string(kind) + "-" + name + ".o"
elfPaths := []string{}
if env, ok := os.LookupEnv(EnvBpfPath); ok {
elfPaths = append(elfPaths, filepath.Join(env, filename))
}
inUsr := false
if exe, e := os.Executable(); e == nil {
inUsr = strings.HasPrefix(exe, "/usr/")
elfPaths = append(elfPaths, filepath.Join(filepath.Dir(exe), "../lib/bpf", filename))
}
if _, source, _, ok := runtime.Caller(0); !inUsr && ok {
elfPaths = append(elfPaths, filepath.Join(filepath.Dir(source), "../build/lib/bpf", filename))
}
for _, elf := range elfPaths {
if path, e := filepath.EvalSymlinks(elf); e == nil {
return path, nil
}
}
return "", fmt.Errorf("%s ELF '%s' not found in %s; try specifying eBPF path via %s environ",
kind, name, strings.Join(elfPaths, ","), EnvBpfPath)
}