Skip to content

Commit 5b12ed2

Browse files
committed
struct_ops: use value type as MapSpec.Value and drop unused helpers (2)
Signed-off-by: shun159 <[email protected]>
1 parent 280b68a commit 5b12ed2

File tree

5 files changed

+23
-61
lines changed

5 files changed

+23
-61
lines changed

collection.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ func (cl *collectionLoader) loadVariable(varName string) (*Variable, error) {
675675
// populateDeferredMaps iterates maps holding programs or other maps and loads
676676
// any dependencies. Populates all maps in cl and freezes them if specified.
677677
func (cl *collectionLoader) populateDeferredMaps() error {
678-
fmt.Println("hogehogehogehoge")
679678
for mapName, m := range cl.maps {
680679
mapSpec, ok := cl.coll.Maps[mapName]
681680
if !ok {
@@ -721,7 +720,7 @@ func (cl *collectionLoader) populateDeferredMaps() error {
721720
}
722721

723722
if mapSpec.Type == StructOpsMap {
724-
vType, ok := btf.As[*btf.Struct](mapSpec.Value)
723+
valueType, ok := btf.As[*btf.Struct](mapSpec.Value)
725724
if !ok {
726725
return fmt.Errorf("value should be a *Struct")
727726
}
@@ -736,17 +735,17 @@ func (cl *collectionLoader) populateDeferredMaps() error {
736735
return fmt.Errorf("load vmlinux BTF: %w", err)
737736
}
738737

739-
kernType, _, _, err := findStructTypeByName(s, vType.Name)
738+
vType, _, _, err := findStructTypeByName(s, valueType.Name)
740739
if err != nil {
741740
return fmt.Errorf("find value type: %w", err)
742741
}
743742

744-
kernVData, err := translateStructData(vType, userData, kernType)
743+
kernVData, err := translateStructData(valueType, userData, vType)
745744
if err != nil {
746745
return err
747746
}
748747

749-
if err := populateFuncPtr(kernType, kernVData, cl.programs); err != nil {
748+
if err := populateFuncPtr(vType, kernVData, cl.programs); err != nil {
750749
return err
751750
}
752751

@@ -773,7 +772,6 @@ func translateStructData(from *btf.Struct, fromData []byte, to *btf.Struct) ([]b
773772

774773
for _, m := range to.Members {
775774
st, ok := btf.As[*btf.Struct](btf.UnderlyingType(m.Type))
776-
fmt.Println(m.Name, st.Name)
777775
if ok && st.Name == innerName {
778776
inner = st
779777
innerOff = int(m.Offset / 8)
@@ -869,22 +867,21 @@ func (cl *collectionLoader) initKernStructOps() error {
869867
continue
870868
}
871869

872-
kernSt := ms.Value
873-
if kernSt == nil {
870+
valueSt := ms.Value
871+
if valueSt == nil {
874872
return fmt.Errorf("user struct type should be specified as Value")
875873
}
876874

877-
kType, ok := btf.As[*btf.Struct](kernSt)
875+
vType, ok := btf.As[*btf.Struct](valueSt)
878876
if !ok {
879877
return fmt.Errorf("user struct type should be a Struct")
880878
}
881879

882880
// resolve kernel-side types (target + value type)
883-
kernTypes, err := findStructOpsKernTypes(kType)
881+
kernTypes, err := findStructOpsKernTypes(vType)
884882
if err != nil {
885883
return fmt.Errorf("find kern_type: %w", err)
886884
}
887-
ms.Value = kernTypes.typ
888885

889886
// allocate per-map state (names, offsets, kernel buffer, attach types, typeID)
890887
structOps := &structOpsSpec{

collection_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,13 +790,13 @@ func TestStructOpsMapSpecSimpleLoadAndAssign(t *testing.T) {
790790
},
791791
Maps: map[string]*MapSpec{
792792
"testmod_ops": {
793-
Name: "bpf_struct_ops_testmod_ops",
793+
Name: "testmod_ops",
794794
Type: StructOpsMap,
795795
Flags: sys.BPF_F_LINK,
796796
KeySize: 4,
797797
ValueSize: 448,
798798
MaxEntries: 1,
799-
Value: &btf.Struct{Name: "bpf_testmod_ops"},
799+
Value: &btf.Struct{Name: "bpf_struct_ops_bpf_testmod_ops"},
800800
Contents: []MapKV{
801801
{
802802
Key: uint32(0),

map.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,19 +569,20 @@ func (spec *MapSpec) createMap(inner *sys.FD) (_ *Map, err error) {
569569
return nil, fmt.Errorf("struct type is not specified as Value")
570570
}
571571

572-
vType, ok := btf.As[*btf.Struct](spec.Value)
572+
valueType, ok := btf.As[*btf.Struct](spec.Value)
573573
if !ok {
574574
return nil, fmt.Errorf("value must be Struct type")
575575
}
576576

577577
// struct_ops: resolve value type ("bpf_struct_ops_<name>") and
578578
// record kernel-specific BTF IDs / FDs needed for map creation.
579-
kType, s, modBtfObjId, err := findStructTypeByName(s, vType.Name)
579+
vType, s, modBtfObjId, err := findStructTypeByName(s, valueType.Name)
580580
if err != nil {
581-
return nil, fmt.Errorf("lookup struct type: %w", err)
581+
return nil, fmt.Errorf("lookup value type %q: %w", valueType.Name, err)
582582
}
583583

584-
btfValueTypeId, err := s.TypeID(kType)
584+
btfValueTypeId, err := s.TypeID(vType)
585+
fmt.Println(vType, btfValueTypeId)
585586
if err != nil {
586587
return nil, fmt.Errorf("lookup type_id: %w", err)
587588
}

struct_ops.go

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ebpf
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/cilium/ebpf/btf"
89
"github.com/cilium/ebpf/internal/sys"
@@ -19,8 +20,6 @@ type structOpsKernTypes struct {
1920
typeID btf.TypeID
2021
// value struct "bpf_struct_ops_<name>" that contains typ.
2122
valueType *btf.Struct
22-
// The *btf.Member within valueType that embeds typ.
23-
dataMember *btf.Member
2423
// The BTF object ID of the module where the type was found
2524
// 0 if resolved in vmlinux.
2625
modBtfObjId uint32
@@ -64,34 +63,6 @@ type structOpsSpec struct {
6463
progAttachBtfID btf.TypeID
6564
}
6665

67-
// findByTypeFromStruct searches for the first member of a struct whose
68-
// resolved BTF type ID matches the given typ.
69-
//
70-
// It resolves the BTF type ID of typ and compares it against each
71-
// member’s TypeID in st.Members. If a match is found, the corresponding
72-
// *btf.Member is returned.
73-
//
74-
// Returns an error if typ cannot be resolved, if any member type
75-
// resolution fails, or if no member with the requested type exists.
76-
func findByTypeFromStruct(spec *btf.Spec, st *btf.Struct, typ btf.Type) (*btf.Member, error) {
77-
typeId, err := spec.TypeID(typ)
78-
if err != nil {
79-
return nil, fmt.Errorf("failed to resolve typeId for %s: %w", (typ).TypeName(), err)
80-
}
81-
82-
for _, member := range st.Members {
83-
memberTypeId, err := spec.TypeID(member.Type)
84-
if err != nil {
85-
return nil, fmt.Errorf("failed to resolve typeId for %s: %w", member.Name, err)
86-
}
87-
if memberTypeId == typeId {
88-
return &member, nil
89-
}
90-
}
91-
92-
return nil, fmt.Errorf("member of type %s not found in %s", typ.TypeName(), st.Name)
93-
}
94-
9566
// findStructTypeByName looks up a struct type with the exact name in the
9667
// provided base BTF spec, and falls back to scanning all loaded module BTFs
9768
// if it is not present in vmlinux.
@@ -168,32 +139,26 @@ func findStructTypeByNameFromModule(base *btf.Spec, name string) (*btf.Struct, *
168139
// findStructOpsKernTypes discovers all kernel-side BTF artifacts that belong to
169140
// a given struct_ops, identified by the user-visible base struct name
170141
// (e.g., "tcp_congestion_ops").
171-
func findStructOpsKernTypes(kType *btf.Struct) (*structOpsKernTypes, error) {
142+
func findStructOpsKernTypes(valueType *btf.Struct) (*structOpsKernTypes, error) {
172143
spec, err := btf.LoadKernelSpec()
173144
if err != nil {
174145
return nil, fmt.Errorf("load vmlinux BTF: %w", err)
175146
}
176147

177148
// 1. kernel target struct (e.g. tcp_congestion_ops)
178-
kType, s, modID, err := findStructTypeByName(spec, kType.Name)
149+
kTypeName := strings.TrimPrefix(valueType.Name, structOpsValuePrefix)
150+
kType, s, modID, err := findStructTypeByName(spec, kTypeName)
179151
if err != nil {
180152
return nil, fmt.Errorf("struct type: %s %w", kType.TypeName(), err)
181153
}
182154

183155
// 2. value struct (bpf_struct_ops_<name>)
184-
vTypeName := structOpsValuePrefix + kType.Name
185-
vType, _, _, err := findStructTypeByName(s, vTypeName)
156+
vType, _, _, err := findStructTypeByName(s, valueType.Name)
186157
if err != nil {
187158
return nil, fmt.Errorf("kern struct type for %s %w", kType.TypeName(), err)
188159
}
189160

190-
// 3. member “data” that embeds the real ops
191-
dataMem, err := findByTypeFromStruct(s, vType, kType)
192-
if err != nil {
193-
return nil, err
194-
}
195-
196-
// 4. type-ID of kernel target
161+
// 3. type-ID of kernel target
197162
kID, err := s.TypeID(kType)
198163
if err != nil {
199164
return nil, fmt.Errorf("type ID of %s: %w", kType.TypeName(), err)
@@ -204,7 +169,6 @@ func findStructOpsKernTypes(kType *btf.Struct) (*structOpsKernTypes, error) {
204169
typ: kType,
205170
typeID: kID,
206171
valueType: vType,
207-
dataMember: dataMem,
208172
modBtfObjId: uint32(modID),
209173
}, nil
210174
}

struct_ops_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestCreateStructOpsMapSpecSimple(t *testing.T) {
1919
KeySize: 4,
2020
ValueSize: 448,
2121
MaxEntries: 1,
22-
Value: &btf.Struct{Name: "bpf_struct_ops_testmod_ops"},
22+
Value: &btf.Struct{Name: "bpf_struct_ops_bpf_testmod_ops"},
2323
Contents: []MapKV{
2424
{
2525
Key: uint32(0),
@@ -33,7 +33,7 @@ func TestCreateStructOpsMapSpecSimple(t *testing.T) {
3333
t.Fatal(err)
3434
}
3535

36-
_, _, _, err = findStructTypeByName(s, "bpf_struct_ops_testmod_ops")
36+
_, _, _, err = findStructTypeByName(s, "bpf_struct_ops_bpf_testmod_ops")
3737
if errors.Is(err, btf.ErrNotFound) {
3838
t.Skip("bpf_testmod_ops not loaded")
3939
}

0 commit comments

Comments
 (0)