Skip to content

Commit 7888a06

Browse files
committed
struct_ops: moved postLoad() logic into populateDeferredMaps()
Signed-off-by: shun159 <[email protected]>
1 parent 0597fa9 commit 7888a06

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

collection.go

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,6 @@ func (cs *CollectionSpec) LoadAndAssign(to interface{}, opts *CollectionOptions)
309309
return err
310310
}
311311

312-
// load programs to struct_ops maps
313-
if err := loader.populateAndPutStructOpsProgFds(); err != nil {
314-
return err
315-
}
316-
317312
// Evaluate the loader's objects after all (lazy)loading has taken place.
318313
for n, m := range loader.maps {
319314
if m.typ.canStoreProgram() {
@@ -725,6 +720,34 @@ func (cl *collectionLoader) populateDeferredMaps() error {
725720
}
726721
}
727722

723+
if mapSpec.Type == StructOpsMap {
724+
structOps, ok := cl.stOpsSpecs[mapName]
725+
if !ok {
726+
return fmt.Errorf("struct_ops Map: %s is not initialized", mapName)
727+
}
728+
729+
for idx, progName := range structOps.programName {
730+
if progName == "" {
731+
continue // unlikely to happen
732+
}
733+
// loadProgram is idempotent and could return an existing Program.
734+
prog, err := cl.loadProgram(progName)
735+
if err != nil {
736+
return fmt.Errorf("loading program %s is not loaded, for map %s: %w",
737+
progName, mapName, err)
738+
}
739+
defer prog.Close()
740+
741+
// populate ProgFDs as Values
742+
off := structOps.kernFuncOff[idx]
743+
ptr := unsafe.Pointer(&structOps.kernVData[0])
744+
*(*uint64)(unsafe.Pointer(uintptr(ptr) + uintptr(off))) = uint64(prog.FD())
745+
}
746+
747+
mapSpec.Contents[0] =
748+
MapKV{Key: uint32(0), Value: structOps.kernVData}
749+
}
750+
728751
// Populate and freeze the map if specified.
729752
if err := m.finalize(mapSpec); err != nil {
730753
return fmt.Errorf("populating map %s: %w", mapName, err)
@@ -888,49 +911,6 @@ func (cl *collectionLoader) initKernStructOps(cs *CollectionSpec) error {
888911
return nil
889912
}
890913

891-
// TODO: should be RENAMED AND MOVED!!!!
892-
893-
// populateStructOpsProgFds runs after all maps and programs have been loaded.
894-
// It writes program FDs into struct_ops.KernVData and updates the map entry.
895-
func (cl *collectionLoader) populateAndPutStructOpsProgFds() error {
896-
for mapName, m := range cl.maps {
897-
if m.Type() != StructOpsMap {
898-
continue
899-
}
900-
901-
structOps, ok := cl.stOpsSpecs[mapName]
902-
if !ok {
903-
return fmt.Errorf("struct_ops Map: %s is not initialized", mapName)
904-
}
905-
906-
for idx, progName := range structOps.programName {
907-
if progName == "" {
908-
continue
909-
}
910-
911-
prog, ok := cl.programs[progName]
912-
if !ok {
913-
return fmt.Errorf("program %s should be loaded", progName)
914-
}
915-
defer prog.Close()
916-
917-
off := structOps.kernFuncOff[idx]
918-
ptr := unsafe.Pointer(&structOps.kernVData[0])
919-
*(*uint64)(unsafe.Pointer(uintptr(ptr) + uintptr(off))) = uint64(prog.FD())
920-
}
921-
922-
m, ok := cl.maps[mapName]
923-
if !ok {
924-
return fmt.Errorf("map %s should be loaded", mapName)
925-
}
926-
927-
if err := m.Put(uint32(0), structOps.kernVData); err != nil {
928-
return err
929-
}
930-
}
931-
return nil
932-
}
933-
934914
// resolveKconfig resolves all variables declared in .kconfig and populates
935915
// m.Contents. Does nothing if the given m.Contents is non-empty.
936916
func resolveKconfig(m *MapSpec) error {

types.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (mt MapType) hasPerCPUValue() bool {
144144
// canStoreMapOrProgram returns true if the Map stores references to another Map
145145
// or Program.
146146
func (mt MapType) canStoreMapOrProgram() bool {
147-
return mt.canStoreMap() || mt.canStoreProgram()
147+
return mt.canStoreMap() || mt.canStoreProgram() || mt.canStoreSturct()
148148
}
149149

150150
// canStoreMap returns true if the map type accepts a map fd
@@ -159,6 +159,12 @@ func (mt MapType) canStoreProgram() bool {
159159
return mt == ProgramArray || mt == WindowsProgramArray
160160
}
161161

162+
// canStoreSturct returns true if the map type accepts a struct bytes with prog FDs
163+
// for update and returns a map id for lookup.
164+
func (mt MapType) canStoreSturct() bool {
165+
return mt == StructOpsMap
166+
}
167+
162168
// canHaveValueSize returns true if the map type supports setting a value size.
163169
func (mt MapType) canHaveValueSize() bool {
164170
switch mt {

0 commit comments

Comments
 (0)