@@ -11,7 +11,6 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"path"
14
- "sync"
15
14
16
15
"github.com/cilium/ebpf"
17
16
"github.com/cilium/tetragon/pkg/api/ops"
@@ -48,10 +47,7 @@ const (
48
47
)
49
48
50
49
var (
51
- // Tracepoint information (genericTracepoint) is needed at load time
52
- // and at the time we process the perf event from bpf-side. We keep
53
- // this information on a table index by a (unique) tracepoint id.
54
- genericTracepointTable = tracepointTable {}
50
+ genericTracepointTable idtable.Table
55
51
56
52
tracepointLog logrus.FieldLogger
57
53
)
@@ -70,15 +66,14 @@ func init() {
70
66
71
67
// genericTracepoint is the internal representation of a tracepoint
72
68
type genericTracepoint struct {
69
+ tableId idtable.EntryID
70
+
73
71
Info * tracepoint.Tracepoint
74
72
args []genericTracepointArg
75
73
76
74
Spec * v1alpha1.TracepointSpec
77
75
policyID policyfilter.PolicyID
78
76
79
- // index to access this on genericTracepointTable
80
- tableIdx int
81
-
82
77
// for tracepoints that have a GetUrl or DnsLookup action, we store the table of arguments.
83
78
actionArgs idtable.Table
84
79
@@ -103,6 +98,10 @@ type genericTracepoint struct {
103
98
raw bool
104
99
}
105
100
101
+ func (tp * genericTracepoint ) SetID (id idtable.EntryID ) {
102
+ tp .tableId = id
103
+ }
104
+
106
105
// genericTracepointArg is the internal representation of an output value of a
107
106
// generic tracepoint.
108
107
type genericTracepointArg struct {
@@ -138,30 +137,16 @@ type genericTracepointArg struct {
138
137
btf [tracingapi .MaxBTFArgDepth ]tracingapi.ConfigBTFArg
139
138
}
140
139
141
- // tracepointTable is, for now, an array.
142
- type tracepointTable struct {
143
- mu sync.Mutex
144
- arr []* genericTracepoint
145
- }
146
-
147
- // addTracepoint adds a tracepoint to the table, and sets its .tableIdx field
148
- // to be the index to retrieve it from the table.
149
- func (t * tracepointTable ) addTracepoint (tp * genericTracepoint ) {
150
- t .mu .Lock ()
151
- defer t .mu .Unlock ()
152
- idx := len (t .arr )
153
- t .arr = append (t .arr , tp )
154
- tp .tableIdx = idx
155
- }
156
-
157
- // getTracepoint retrieves a tracepoint from the table using its id
158
- func (t * tracepointTable ) getTracepoint (idx int ) (* genericTracepoint , error ) {
159
- t .mu .Lock ()
160
- defer t .mu .Unlock ()
161
- if idx < len (t .arr ) {
162
- return t .arr [idx ], nil
140
+ func genericTracepointTableGet (id idtable.EntryID ) (* genericTracepoint , error ) {
141
+ entry , err := genericTracepointTable .GetEntry (id )
142
+ if err != nil {
143
+ return nil , fmt .Errorf ("getting entry from genericTracepointTable failed with: %w" , err )
163
144
}
164
- return nil , fmt .Errorf ("tracepoint table: invalid id:%d (len=%d)" , idx , len (t .arr ))
145
+ val , ok := entry .(* genericTracepoint )
146
+ if ! ok {
147
+ return nil , fmt .Errorf ("getting entry from genericTracepointTable failed with: got invalid type: %T (%v)" , entry , entry )
148
+ }
149
+ return val , nil
165
150
}
166
151
167
152
func (out * genericTracepointArg ) String () string {
@@ -408,6 +393,7 @@ func createGenericTracepoint(
408
393
}
409
394
410
395
ret := & genericTracepoint {
396
+ tableId : idtable .UninitializedEntryID ,
411
397
Info : & tp ,
412
398
Spec : conf ,
413
399
args : tpArgs ,
@@ -419,8 +405,8 @@ func createGenericTracepoint(
419
405
raw : conf .Raw ,
420
406
}
421
407
422
- genericTracepointTable .addTracepoint (ret )
423
- ret .pinPathPrefix = sensors .PathJoin (sensorName , fmt .Sprintf ("gtp-%d" , ret .tableIdx ))
408
+ genericTracepointTable .AddEntry (ret )
409
+ ret .pinPathPrefix = sensors .PathJoin (sensorName , fmt .Sprintf ("gtp-%d" , ret .tableId . ID ))
424
410
return ret , nil
425
411
}
426
412
@@ -563,7 +549,7 @@ func createGenericTracepointSensor(
563
549
564
550
has .fdInstall = selectorsHaveFDInstall (tp .Spec .Selectors )
565
551
566
- prog0 .LoaderData = tp .tableIdx
552
+ prog0 .LoaderData = tp .tableId
567
553
progs = append (progs , prog0 )
568
554
569
555
fdinstall := program .MapBuilderSensor ("fdinstall_map" , prog0 )
@@ -664,6 +650,19 @@ func createGenericTracepointSensor(
664
650
665
651
ret .Progs = progs
666
652
ret .Maps = maps
653
+
654
+ ret .DestroyHook = func () error {
655
+ var errs error
656
+
657
+ for _ , tp := range tracepoints {
658
+ _ , err := genericTracepointTable .RemoveEntry (tp .tableId )
659
+ if err != nil {
660
+ errs = errors .Join (errs , err )
661
+ }
662
+ }
663
+ return errs
664
+ }
665
+
667
666
return ret , nil
668
667
}
669
668
@@ -719,7 +718,7 @@ func (tp *genericTracepoint) EventConfig() (*tracingapi.EventConfig, error) {
719
718
720
719
config := tracingapi.EventConfig {}
721
720
config .PolicyID = uint32 (tp .policyID )
722
- config .FuncId = uint32 (tp .tableIdx )
721
+ config .FuncId = uint32 (tp .tableId . ID )
723
722
724
723
if tp .raw {
725
724
return tp .eventConfigRaw (& config )
@@ -769,12 +768,12 @@ func LoadGenericTracepointSensor(bpfDir string, load *program.Program, maps []*p
769
768
770
769
tracepointLog = logger .GetLogger ()
771
770
772
- tpIdx , ok := load .LoaderData .(int )
771
+ id , ok := load .LoaderData .(idtable. EntryID )
773
772
if ! ok {
774
773
return fmt .Errorf ("loaderData for genericTracepoint %s is %T (%v) (not an int)" , load .Name , load .LoaderData , load .LoaderData )
775
774
}
776
775
777
- tp , err := genericTracepointTable . getTracepoint ( tpIdx )
776
+ tp , err := genericTracepointTableGet ( id )
778
777
if err != nil {
779
778
return fmt .Errorf ("could not find generic tracepoint information for %s: %w" , load .Attach , err )
780
779
}
@@ -820,7 +819,7 @@ func handleGenericTracepoint(r *bytes.Reader) ([]observer.Event, error) {
820
819
Event : "UNKNOWN" ,
821
820
}
822
821
823
- tp , err := genericTracepointTable . getTracepoint ( int (m .FuncId ))
822
+ tp , err := genericTracepointTableGet (idtable. EntryID { ID : int (m .FuncId )} )
824
823
if err != nil {
825
824
logger .GetLogger ().WithField ("id" , m .FuncId ).WithError (err ).Warnf ("genericTracepoint info not found" )
826
825
return []observer.Event {unix }, nil
0 commit comments