Skip to content

Commit

Permalink
btf: remove Map
Browse files Browse the repository at this point in the history
btf.Program is gone, so let's get rid of Map as well.
  • Loading branch information
lmb committed Mar 28, 2022
1 parent ef3d333 commit 661a625
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
12 changes: 4 additions & 8 deletions cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,12 @@ func collectCTypes(types *btf.Spec, names []string) ([]btf.Type, error) {
func collectMapTypes(maps map[string]*ebpf.MapSpec) []btf.Type {
var result []btf.Type
for _, m := range maps {
if m.BTF == nil {
continue
}

if m.BTF.Key != nil && m.BTF.Key.TypeName() != "" {
result = append(result, m.BTF.Key)
if m.Key != nil && m.Key.TypeName() != "" {
result = append(result, m.Key)
}

if m.BTF.Value != nil && m.BTF.Value.TypeName() != "" {
result = append(result, m.BTF.Value)
if m.Value != nil && m.Value.TypeName() != "" {
result = append(result, m.Value)
}
}
return result
Expand Down
4 changes: 2 additions & 2 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (cs *CollectionSpec) RewriteConstants(consts map[string]interface{}) error
buf := make([]byte, len(value))
copy(buf, value)

err := patchValue(buf, rodata.BTF.Value, consts)
err := patchValue(buf, rodata.Value, consts)
if err != nil {
return err
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func (cl *collectionLoader) loadMap(mapName string) (*Map, error) {
return nil, fmt.Errorf("missing map %s", mapName)
}

if mapSpec.BTF != nil && cl.coll.Types != mapSpec.BTF.Spec {
if mapSpec.BTF != nil && cl.coll.Types != mapSpec.BTF {
return nil, fmt.Errorf("map %s: BTF doesn't match collection", mapName)
}

Expand Down
8 changes: 6 additions & 2 deletions elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ func mapSpecFromBTF(es *elfSection, vs *btf.VarSecinfo, def *btf.Struct, spec *b
ValueSize: valueSize,
MaxEntries: maxEntries,
Flags: flags,
BTF: &btf.Map{Spec: spec, Key: key, Value: value},
Key: key,
Value: value,
BTF: spec,
Pinning: pinType,
InnerMap: innerMapSpec,
Contents: contents,
Expand Down Expand Up @@ -1026,7 +1028,9 @@ func (ec *elfCode) loadDataSections(maps map[string]*MapSpec) error {
ValueSize: uint32(len(data)),
MaxEntries: 1,
Contents: []MapKV{{uint32(0), data}},
BTF: &btf.Map{Spec: ec.btf, Key: &btf.Void{}, Value: datasec},
Key: &btf.Void{},
Value: datasec,
BTF: ec.btf,
}

switch sec.Name {
Expand Down
3 changes: 2 additions & 1 deletion elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ func TestLoadCollectionSpec(t *testing.T) {
}
return false
}),
cmpopts.IgnoreTypes(new(btf.Map), new(btf.Spec)),
cmpopts.IgnoreTypes(new(btf.Spec)),
cmpopts.IgnoreFields(CollectionSpec{}, "ByteOrder", "Types"),
cmpopts.IgnoreFields(ProgramSpec{}, "Instructions", "ByteOrder"),
cmpopts.IgnoreFields(MapSpec{}, "Key", "Value"),
cmpopts.IgnoreUnexported(ProgramSpec{}),
cmpopts.IgnoreMapEntries(func(key string, _ *MapSpec) bool {
switch key {
Expand Down
6 changes: 0 additions & 6 deletions internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,6 @@ func (h *Handle) FD() int {
return h.fd.Int()
}

// Map is the BTF for a map.
type Map struct {
Spec *Spec
Key, Value Type
}

func marshalBTF(types interface{}, strings []byte, bo binary.ByteOrder) []byte {
const minHeaderLength = 24

Expand Down
11 changes: 7 additions & 4 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ type MapSpec struct {
// Must be nil or empty before instantiating the MapSpec into a Map.
Extra *bytes.Reader

// The key and value type of this map. May be nil.
Key, Value btf.Type

// The BTF associated with this map.
BTF *btf.Map
BTF *btf.Spec
}

func (ms *MapSpec) String() string {
Expand Down Expand Up @@ -398,15 +401,15 @@ func (spec *MapSpec) createMap(inner *sys.FD, opts MapOptions, handles *handleCa
}

if spec.hasBTF() {
handle, err := handles.btfHandle(spec.BTF.Spec)
handle, err := handles.btfHandle(spec.BTF)
if err != nil && !errors.Is(err, btf.ErrNotSupported) {
return nil, fmt.Errorf("load BTF: %w", err)
}

if handle != nil {
attr.BtfFd = uint32(handle.FD())
attr.BtfKeyTypeId = uint32(spec.BTF.Key.ID())
attr.BtfValueTypeId = uint32(spec.BTF.Value.ID())
attr.BtfKeyTypeId = uint32(spec.Key.ID())
attr.BtfValueTypeId = uint32(spec.Value.ID())
}
}

Expand Down
2 changes: 1 addition & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ func TestMapPinning(t *testing.T) {
// This is a terrible hack: if loading a pinned map tries to load BTF,
// it will get a nil *btf.Spec from this *btf.Map. This is turn will make
// btf.NewHandle fail.
spec.BTF = new(btf.Map)
spec.BTF = new(btf.Spec)

m2, err := NewMapWithOptions(spec, MapOptions{PinPath: tmp})
testutils.SkipIfNotSupported(t, err)
Expand Down

0 comments on commit 661a625

Please sign in to comment.