Skip to content

Commit 0d6b1a9

Browse files
committed
struct_ops: fix to remove unused struct members
`wID` should be resolved in createMap() Signed-off-by: shun159 <[email protected]>
1 parent 2fba3a5 commit 0d6b1a9

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ func (spec *MapSpec) createMap(inner *sys.FD) (_ *Map, err error) {
575575
}
576576

577577
userStructType, s, modBtfObjId, err := findStructByNameWithPrefix(s, userStType)
578+
if err != nil {
579+
return nil, fmt.Errorf("lookup struct type: %w", err)
580+
}
581+
578582
btfValueTypeId, err := s.TypeID(userStructType)
579583
if err != nil {
580584
return nil, fmt.Errorf("lookup type_id: %w", err)

prog.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,14 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, c *btf.Cache)
397397

398398
if spec.Type == StructOps {
399399
if meta, ok := spec.Instructions[0].Metadata.Get(structOpsProgMetaKey{}).(*structOpsProgMeta); ok {
400-
attr.AttachBtfId = sys.TypeID(meta.AttachBtfId)
401-
attr.ExpectedAttachType = meta.AttachType
400+
attr.AttachBtfId = sys.TypeID(meta.attachBtfId)
401+
attr.ExpectedAttachType = meta.attachType
402402

403403
var modH *btf.Handle
404-
if meta.ModBtfObjID != 0 {
405-
h, err := btf.NewHandleFromID(btf.ID(meta.ModBtfObjID))
404+
if meta.modBtfObjID != 0 {
405+
h, err := btf.NewHandleFromID(btf.ID(meta.modBtfObjID))
406406
if err != nil {
407-
return nil, fmt.Errorf("open module BTF handle (id=%d): %w", meta.ModBtfObjID, err)
407+
return nil, fmt.Errorf("open module BTF handle (id=%d): %w", meta.modBtfObjID, err)
408408
}
409409
modH = h
410410
attr.AttachBtfObjFd = uint32(h.FD())

struct_ops.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ const structOpsValuePrefix = "bpf_struct_ops_"
1414
// TODO: Doc
1515
type structOpsProgMetaKey struct{}
1616
type structOpsProgMeta struct {
17-
AttachBtfId btf.TypeID
18-
AttachType sys.AttachType
19-
ModBtfObjID uint32
17+
attachBtfId btf.TypeID
18+
attachType sys.AttachType
19+
modBtfObjID uint32
2020
}
2121

2222
// structOpsKernTypes holds information about kernel types related to struct_ops
@@ -28,8 +28,6 @@ type structOpsKernTypes struct {
2828
typeID btf.TypeID
2929
// The wrapper struct type that contains the target struct
3030
valueType *btf.Struct
31-
// The BTF type ID of the wrapper struct
32-
valueTypeID btf.TypeID
3331
// The member within ValueType that holds the target struct
3432
dataMember *btf.Member
3533
// mod_btf
@@ -69,8 +67,7 @@ type structOpsMeta struct {
6967
funcs []structOpsFunc
7068
// used for represent a data for the user struct
7169
// e.g. struct tcp_congestion_ops in bpf_prog's btf format */
72-
data []byte
73-
modBtfObjId btf.ID
70+
data []byte
7471
}
7572

7673
// extractStructOpsMeta returns the *structops.Meta embedded in a MapSpec’s Contents
@@ -221,18 +218,11 @@ func findStructOpsKernTypes(userStructType *btf.Struct) (*structOpsKernTypes, er
221218
return nil, fmt.Errorf("type ID of %s: %w", kType.TypeName(), err)
222219
}
223220

224-
// 5. type-ID of wrapper
225-
wID, err := s.TypeID(wType)
226-
if err != nil {
227-
return nil, fmt.Errorf("type ID of %s: %w", wType.TypeName(), err)
228-
}
229-
230221
return &structOpsKernTypes{
231222
spec: s,
232223
typ: kType,
233224
typeID: kID,
234225
valueType: wType,
235-
valueTypeID: wID,
236226
dataMember: dataMem,
237227
modBtfObjId: uint32(modID),
238228
}, nil
@@ -416,9 +406,9 @@ func (sl *structOpsLoader) copyDataMember(
416406
sl.stOpsProgsToMap[ps.Name] = ms.Name
417407

418408
ps.Instructions[0].Metadata.Set(structOpsProgMetaKey{}, &structOpsProgMeta{
419-
AttachBtfId: kern.typeID,
420-
AttachType: attachType,
421-
ModBtfObjID: kern.modBtfObjId,
409+
attachBtfId: kern.typeID,
410+
attachType: attachType,
411+
modBtfObjID: kern.modBtfObjId,
422412
})
423413
}
424414

0 commit comments

Comments
 (0)