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

Export package btf #665

Merged
merged 3 commits into from
May 11, 2022
Merged
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
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ TARGETS := \
testdata/map_spin_lock \
testdata/subprog_reloc \
testdata/fwd_decl \
internal/btf/testdata/relocs \
internal/btf/testdata/relocs_read \
internal/btf/testdata/relocs_read_tgt
btf/testdata/relocs \
btf/testdata/relocs_read \
btf/testdata/relocs_read_tgt

.PHONY: all clean container-all container-shell generate

Expand All @@ -60,7 +60,7 @@ container-shell:

clean:
-$(RM) testdata/*.elf
-$(RM) internal/btf/testdata/*.elf
-$(RM) btf/testdata/*.elf

format:
find . -type f -name "*.c" | xargs clang-format -i
Expand Down Expand Up @@ -95,6 +95,6 @@ testdata/loader-%-eb.elf: testdata/loader.c

# Usage: make VMLINUX=/path/to/vmlinux vmlinux-btf
.PHONY: vmlinux-btf
vmlinux-btf: internal/btf/testdata/vmlinux-btf.gz
internal/btf/testdata/vmlinux-btf.gz: $(VMLINUX)
vmlinux-btf: btf/testdata/vmlinux-btf.gz
btf/testdata/vmlinux-btf.gz: $(VMLINUX)
objcopy --dump-section .BTF=/dev/stdout "$<" /dev/null | gzip > "$@"
11 changes: 11 additions & 0 deletions internal/btf/btf.go → btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ func (h *btfHeader) stringStart() int64 {
return int64(h.HdrLen + h.StringOff)
}

// LoadSpec opens file and calls LoadSpecFromReader on it.
func LoadSpec(file string) (*Spec, error) {
fh, err := os.Open(file)
if err != nil {
return nil, err
}
defer fh.Close()

return LoadSpecFromReader(fh)
}

// LoadSpecFromReader reads from an ELF or a raw BTF blob.
//
// Returns ErrNotFound if reading from an ELF which contains no BTF. ExtInfos
Expand Down
12 changes: 4 additions & 8 deletions internal/btf/btf_test.go → btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ func readVMLinux(tb testing.TB) *bytes.Reader {
}

func parseELFBTF(tb testing.TB, file string) *Spec {
fh, err := os.Open(file)
if err != nil {
tb.Fatal(err)
}
defer fh.Close()
tb.Helper()

spec, err := LoadSpecFromReader(fh)
spec, err := LoadSpec(file)
if err != nil {
tb.Fatal("Can't load BTF:", err)
}
Expand Down Expand Up @@ -229,7 +225,7 @@ func TestFindVMLinux(t *testing.T) {
}

func TestLoadSpecFromElf(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "../../testdata/loader-e*.elf"), func(t *testing.T, file string) {
testutils.Files(t, testutils.Glob(t, "../testdata/loader-e*.elf"), func(t *testing.T, file string) {
spec := parseELFBTF(t, file)

vt, err := spec.TypeByID(0)
Expand Down Expand Up @@ -302,7 +298,7 @@ func TestGuessBTFByteOrder(t *testing.T) {
}

func TestSpecCopy(t *testing.T) {
spec := parseELFBTF(t, "../../testdata/loader-el.elf")
spec := parseELFBTF(t, "../testdata/loader-el.elf")

if len(spec.types) < 1 {
t.Fatal("Not enough types")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 6 additions & 15 deletions internal/btf/core_reloc_test.go → btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestCORERelocationLoad(t *testing.T) {
}

prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
TargetBTF: fh,
KernelTypes: progSpec.BTF,
})

if strings.HasPrefix(progSpec.Name, "err_") {
Expand Down Expand Up @@ -69,13 +70,7 @@ func TestCORERelocationLoad(t *testing.T) {

func TestCORERelocationRead(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/relocs_read-*.elf"), func(t *testing.T, file string) {
fh, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer fh.Close()

spec, err := ebpf.LoadCollectionSpecFromReader(fh)
spec, err := ebpf.LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}
Expand All @@ -84,20 +79,16 @@ func TestCORERelocationRead(t *testing.T) {
return
}

tgt, err := os.Open(fmt.Sprintf("testdata/relocs_read_tgt-%s.elf", internal.ClangEndian))
targetFile := fmt.Sprintf("testdata/relocs_read_tgt-%s.elf", internal.ClangEndian)
targetSpec, err := btf.LoadSpec(targetFile)
if err != nil {
t.Fatal(err)
}
defer tgt.Close()

for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
if _, err := tgt.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}

prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
TargetBTF: tgt,
KernelTypes: targetSpec,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand Down
File renamed without changes.
3 changes: 0 additions & 3 deletions internal/btf/doc.go → btf/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
//
// The canonical documentation lives in the Linux kernel repository and is
// available at https://www.kernel.org/doc/html/latest/bpf/btf.html
//
// The API is very much unstable. You should only use this via the main
// ebpf library.
package btf
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added btf/testdata/relocs-eb.elf
Binary file not shown.
Binary file added btf/testdata/relocs-el.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/btf/testdata/relocs.c → btf/testdata/relocs.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../../../testdata/common.h"
#include "../../testdata/common.h"
#include "bpf_core_read.h"

enum e {
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../../../testdata/common.h"
#include "../../testdata/common.h"
#include "bpf_core_read.h"

#define core_access __builtin_preserve_access_index
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"text/template"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
)

const ebpfModule = "github.com/cilium/ebpf"
Expand Down
2 changes: 1 addition & 1 deletion cmd/bpf2go/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"testing"

"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/btf"
qt "github.com/frankban/quicktest"
)

Expand Down
19 changes: 1 addition & 18 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"strings"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/btf"
)

// CollectionOptions control loading a collection into the kernel.
Expand Down Expand Up @@ -347,13 +346,11 @@ func NewCollectionWithOptions(spec *CollectionSpec, opts CollectionOptions) (*Co

type handleCache struct {
btfHandles map[*btf.Spec]*btf.Handle
btfSpecs map[io.ReaderAt]*btf.Spec
}

func newHandleCache() *handleCache {
return &handleCache{
btfHandles: make(map[*btf.Spec]*btf.Handle),
btfSpecs: make(map[io.ReaderAt]*btf.Spec),
}
}

Expand All @@ -371,20 +368,6 @@ func (hc handleCache) btfHandle(spec *btf.Spec) (*btf.Handle, error) {
return handle, nil
}

func (hc handleCache) btfSpec(rd io.ReaderAt) (*btf.Spec, error) {
if hc.btfSpecs[rd] != nil {
return hc.btfSpecs[rd], nil
}

spec, err := btf.LoadSpecFromReader(rd)
if err != nil {
return nil, err
}

hc.btfSpecs[rd] = spec
return spec, nil
}

func (hc handleCache) close() {
for _, handle := range hc.btfHandles {
handle.Close()
Expand Down
2 changes: 1 addition & 1 deletion collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"testing"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/testutils"
)

Expand Down
2 changes: 1 addition & 1 deletion elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"strings"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/unix"
)

Expand Down
9 changes: 7 additions & 2 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"syscall"
"testing"

"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"

Expand Down Expand Up @@ -761,8 +761,13 @@ func TestLibBPFCompat(t *testing.T) {
}
defer fh.Close()

btfSpec, err := btf.LoadSpec(coreFile)
if err != nil {
t.Fatal(err)
}

opts := opts // copy
opts.Programs.TargetBTF = fh
opts.Programs.KernelTypes = btfSpec
load(t, spec, opts, valid)
})
}
Expand Down
2 changes: 1 addition & 1 deletion info.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"unsafe"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/unix"
)
Expand Down
Binary file removed internal/btf/testdata/relocs-eb.elf
Binary file not shown.
Binary file removed internal/btf/testdata/relocs-el.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/cmd/gentypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sort"
"strings"

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

Expand Down
2 changes: 1 addition & 1 deletion internal/sys/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package sys

// Regenerate types.go by invoking go generate in the current directory.

//go:generate go run github.com/cilium/ebpf/internal/cmd/gentypes ../btf/testdata/vmlinux-btf.gz
//go:generate go run github.com/cilium/ebpf/internal/cmd/gentypes ../../btf/testdata/vmlinux-btf.gz
2 changes: 1 addition & 1 deletion link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"

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

Expand Down
2 changes: 1 addition & 1 deletion link/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

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

Expand Down
2 changes: 1 addition & 1 deletion linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/btf"
)

// splitSymbols splits insns into subsections delimited by Symbol Instructions.
Expand Down
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"
"unsafe"

"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/unix"
)
Expand Down
2 changes: 1 addition & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"unsafe"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"
Expand Down
Loading