Skip to content

Commit 0e096cc

Browse files
tetragon/windows: Support multiple programs from a single collection
This PR changes the way programs are loaded on Windows in order to support loading multiple programs from a single collection. Earlier implementation assumed one program per collection. In Windows, a collection can be loaded only once per process. In that light, if a sensor had two programs from the same sys file, the loading of the second program would fail, as loading the collection second time around fails. In the PR, we cache loaded collections by path and check the cache before loading a new collection. The second run will use the cached collection object and load the passed in program. Signed-off-by: Anadi Anadi<[email protected]>
1 parent 3eff025 commit 0e096cc

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

pkg/bpf/coll_windows.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,40 @@ import (
99
"github.com/cilium/ebpf"
1010
)
1111

12-
type CollectionStore struct {
13-
collMap map[string]*ebpf.Collection
14-
}
15-
1612
var (
17-
store CollectionStore
18-
collstoreMutex sync.RWMutex
13+
collectionByName map[string]*ebpf.Collection
14+
collectionByPath map[string]*ebpf.Collection
15+
collstoreMutex sync.RWMutex
1916
)
2017

21-
func SetCollection(name string, coll *ebpf.Collection) {
18+
func SetCollection(name string, path string, coll *ebpf.Collection) {
2219
collstoreMutex.Lock()
23-
store.collMap[name] = coll
24-
collstoreMutex.Unlock()
20+
defer collstoreMutex.Unlock()
21+
collectionByName[name] = coll
22+
collectionByPath[path] = coll
23+
}
24+
25+
func GetCollectionByPath(path string) (*ebpf.Collection, error) {
26+
collstoreMutex.RLock()
27+
defer collstoreMutex.RUnlock()
28+
coll, ok := collectionByPath[path]
29+
if ok {
30+
return coll, nil
31+
}
32+
return nil, errors.New("collection object not found by path")
2533
}
2634

2735
func GetCollection(name string) (*ebpf.Collection, error) {
2836
collstoreMutex.RLock()
29-
coll, ok := store.collMap[name]
30-
collstoreMutex.RUnlock()
37+
defer collstoreMutex.RUnlock()
38+
coll, ok := collectionByName[name]
3139
if ok {
3240
return coll, nil
3341
}
3442
return nil, errors.New("collection object not found")
3543
}
3644

3745
func init() {
38-
store.collMap = make(map[string]*ebpf.Collection)
46+
collectionByName = make(map[string]*ebpf.Collection)
47+
collectionByPath = make(map[string]*ebpf.Collection)
3948
}

pkg/sensors/program/loader_windows.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ func doLoadProgram(
123123
_ int,
124124
) (*LoadedCollection, error) {
125125

126-
coll, err := ebpf.LoadCollection(load.Name)
126+
coll, err := bpf.GetCollectionByPath(load.Name)
127127
if err != nil {
128-
logger.GetLogger().WithError(err).WithField("Error ", err.Error()).Warn(" Failed to load Native Windows Collection ")
129-
return nil, err
128+
coll, err = ebpf.LoadCollection(load.Name)
129+
if err != nil {
130+
logger.GetLogger().WithError(err).WithField("Error ", err.Error()).Warn(" Failed to load Native Windows Collection ")
131+
return nil, err
132+
}
133+
bpf.SetCollection(load.Label, load.Name, coll)
130134
}
131-
bpf.SetCollection(load.Label, coll)
132135

133136
collMaps := map[ebpf.MapID]*ebpf.Map{}
134137
// we need a mapping by ID
@@ -165,7 +168,6 @@ func doLoadProgram(
165168

166169
var prog *ebpf.Program
167170
for _, p := range coll.Programs {
168-
169171
i, err := p.Info()
170172
if i.Name == load.Label {
171173
prog = p

0 commit comments

Comments
 (0)