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

Make some bpf2go output types/funcs public #1448

Closed
Closed
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
44 changes: 22 additions & 22 deletions cmd/bpf2go/output.go → bpf2go/output.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package bpf2go

import (
"bytes"
Expand Down Expand Up @@ -71,38 +71,38 @@ func (n templateName) CloseHelper() string {
return "_" + toUpperFirst(string(n)) + "Close"
}

type outputArgs struct {
type OutputArgs struct {
// Package of the resulting file.
pkg string
Pkg string
// The prefix of all names declared at the top-level.
stem string
// Build constraints included in the resulting file.
constraints constraint.Expr
Stem string
// Build Constraints included in the resulting file.
Constraints constraint.Expr
// Maps to be emitted.
maps []string
Maps []string
// Programs to be emitted.
programs []string
Programs []string
// Types to be emitted.
types []btf.Type
Types []btf.Type
// Filename of the ELF object to embed.
obj string
out io.Writer
Obj string
Out io.Writer
}

func output(args outputArgs) error {
func Output(args OutputArgs) error {
maps := make(map[string]string)
for _, name := range args.maps {
for _, name := range args.Maps {
maps[name] = internal.Identifier(name)
}

programs := make(map[string]string)
for _, name := range args.programs {
for _, name := range args.Programs {
programs[name] = internal.Identifier(name)
}

typeNames := make(map[btf.Type]string)
for _, typ := range args.types {
typeNames[typ] = args.stem + internal.Identifier(typ.TypeName())
for _, typ := range args.Types {
typeNames[typ] = args.Stem + internal.Identifier(typ.TypeName())
}

// Ensure we don't have conflicting names and generate a sorted list of
Expand Down Expand Up @@ -133,25 +133,25 @@ func output(args outputArgs) error {
}{
gf,
module,
args.pkg,
args.constraints,
templateName(args.stem),
args.Pkg,
args.Constraints,
templateName(args.Stem),
maps,
programs,
types,
typeNames,
args.obj,
args.Obj,
}

var buf bytes.Buffer
if err := commonTemplate.Execute(&buf, &ctx); err != nil {
return fmt.Errorf("can't generate types: %s", err)
}

return internal.WriteFormatted(buf.Bytes(), args.out)
return internal.WriteFormatted(buf.Bytes(), args.Out)
}

func collectFromSpec(spec *ebpf.CollectionSpec, cTypes []string, skipGlobalTypes bool) (maps, programs []string, types []btf.Type, _ error) {
func CollectFromSpec(spec *ebpf.CollectionSpec, cTypes []string, skipGlobalTypes bool) (maps, programs []string, types []btf.Type, _ error) {
for name := range spec.Maps {
// Skip .rodata, .data, .bss, etc. sections
if !strings.HasPrefix(name, ".") {
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions cmd/bpf2go/output_test.go → bpf2go/output_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package bpf2go

import (
"testing"
Expand Down Expand Up @@ -75,21 +75,21 @@ func TestCollectFromSpec(t *testing.T) {

map1 := spec.Maps["map1"]

maps, programs, types, err := collectFromSpec(spec, nil, false)
maps, programs, types, err := CollectFromSpec(spec, nil, false)
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.ContentEquals(maps, []string{"map1"}))
qt.Assert(t, qt.ContentEquals(programs, []string{"filter"}))
qt.Assert(t, qt.CmpEquals(types, []btf.Type{map1.Key, map1.Value}, typesEqualComparer))

_, _, types, err = collectFromSpec(spec, nil, true)
_, _, types, err = CollectFromSpec(spec, nil, true)
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.CmpEquals[[]btf.Type](types, nil, typesEqualComparer))

_, _, types, err = collectFromSpec(spec, []string{"barfoo"}, true)
_, _, types, err = CollectFromSpec(spec, []string{"barfoo"}, true)
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/bpf2go/tools.go → bpf2go/tools.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package bpf2go

import (
"errors"
Expand All @@ -9,7 +9,7 @@ import (
"unicode/utf8"
)

func splitCFlagsFromArgs(in []string) (args, cflags []string) {
func SplitCFlagsFromArgs(in []string) (args, cflags []string) {
for i, arg := range in {
if arg == "--" {
return in[:i], in[i+1:]
Expand All @@ -19,7 +19,7 @@ func splitCFlagsFromArgs(in []string) (args, cflags []string) {
return in, nil
}

func splitArguments(in string) ([]string, error) {
func SplitArguments(in string) ([]string, error) {
var (
result []string
builder strings.Builder
Expand Down Expand Up @@ -85,7 +85,7 @@ func toUpperFirst(str string) string {

func currentModule() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
if !ok || bi.Main.Path == "" {
// Fall back to constant since bazel doesn't support BuildInfo.
return "github.com/cilium/ebpf"
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/bpf2go/tools_test.go → bpf2go/tools_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package bpf2go

import (
"reflect"
Expand Down Expand Up @@ -26,7 +26,7 @@ func TestSplitArguments(t *testing.T) {
}

for _, testcase := range testcases {
have, err := splitArguments(testcase.in)
have, err := SplitArguments(testcase.in)
if testcase.out == nil {
if err == nil {
t.Errorf("Test should fail for: %s", testcase.in)
Expand Down
25 changes: 13 additions & 12 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"strings"

"github.com/cilium/ebpf"
bpf2golib "github.com/cilium/ebpf/bpf2go"
)

const helpText = `Usage: %[1]s [options] <ident> <source file> [-- <C flags>]
Expand Down Expand Up @@ -164,10 +165,10 @@ func newB2G(stdout io.Writer, args []string) (*bpf2go, error) {
return nil, errors.New("no compiler specified")
}

args, cFlags := splitCFlagsFromArgs(fs.Args())
args, cFlags := bpf2golib.SplitCFlagsFromArgs(fs.Args())

if *flagCFlags != "" {
splitCFlags, err := splitArguments(*flagCFlags)
splitCFlags, err := bpf2golib.SplitArguments(*flagCFlags)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -389,7 +390,7 @@ func (b2g *bpf2go) convert(tgt target, goarches []goarch) (err error) {
return fmt.Errorf("can't load BPF from ELF: %s", err)
}

maps, programs, types, err := collectFromSpec(spec, b2g.cTypes, b2g.skipGlobalTypes)
maps, programs, types, err := bpf2golib.CollectFromSpec(spec, b2g.cTypes, b2g.skipGlobalTypes)
if err != nil {
return err
}
Expand All @@ -402,15 +403,15 @@ func (b2g *bpf2go) convert(tgt target, goarches []goarch) (err error) {
}
defer removeOnError(goFile)

err = output(outputArgs{
pkg: b2g.pkg,
stem: b2g.identStem,
constraints: constraints,
maps: maps,
programs: programs,
types: types,
obj: filepath.Base(objFileName),
out: goFile,
err = bpf2golib.Output(bpf2golib.OutputArgs{
Pkg: b2g.pkg,
Stem: b2g.identStem,
Constraints: constraints,
Maps: maps,
Programs: programs,
Types: types,
Obj: filepath.Base(objFileName),
Out: goFile,
})
if err != nil {
return fmt.Errorf("can't write %s: %s", goFileName, err)
Expand Down