-
Notifications
You must be signed in to change notification settings - Fork 186
/
gadget.go
513 lines (407 loc) · 14.3 KB
/
gadget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright 2019-2021 The Inspektor Gadget authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package traceloop
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets"
"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/traceloop/types"
tracelooptracer "github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/traceloop/tracer"
gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1"
containercollection "github.com/inspektor-gadget/inspektor-gadget/pkg/container-collection"
)
type containerSlim struct {
mntnsid uint64
detached bool
}
type Trace struct {
client client.Client
helpers gadgets.GadgetHelpers
started bool
containerIDs map[string]*containerSlim
trace *gadgetv1alpha1.Trace
}
type TraceFactory struct {
gadgets.BaseFactory
}
type traceSingleton struct {
sync.Mutex
tracer *tracelooptracer.Tracer
users int
}
var traceUnique traceSingleton
func NewFactory() gadgets.TraceFactory {
return &TraceFactory{
BaseFactory: gadgets.BaseFactory{DeleteTrace: deleteTrace},
}
}
func (f *TraceFactory) Description() string {
return `The traceloop gadget traces system calls in a similar way to strace but with
some differences:
* traceloop uses eBPF instead of ptrace
* traceloop's tracing granularity is the container instead of a process
* traceloop's traces are recorded in a fast, in-memory, overwritable ring
buffer like a flight recorder. The tracing could be permanently enabled and
inspected in case of crash.
`
}
func (f *TraceFactory) OutputModesSupported() map[gadgetv1alpha1.TraceOutputMode]struct{} {
return map[gadgetv1alpha1.TraceOutputMode]struct{}{
gadgetv1alpha1.TraceOutputModeStatus: {},
gadgetv1alpha1.TraceOutputModeStream: {},
}
}
func deleteTrace(name string, t interface{}) {
trace := t.(*Trace)
if trace.started {
traceUnique.Lock()
defer traceUnique.Unlock()
traceUnique.users--
if traceUnique.users == 0 {
trace.helpers.Unsubscribe(genPubSubKey(name))
traceUnique.tracer.Stop()
traceUnique.tracer = nil
}
}
}
func (f *TraceFactory) Operations() map[gadgetv1alpha1.Operation]gadgets.TraceOperation {
n := func() interface{} {
return &Trace{
client: f.Client,
helpers: f.Helpers,
}
}
return map[gadgetv1alpha1.Operation]gadgets.TraceOperation{
gadgetv1alpha1.OperationStart: {
Doc: "Start traceloop",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
f.LookupOrCreate(name, n).(*Trace).Start(trace)
},
},
gadgetv1alpha1.OperationCollect: {
Doc: "Collect traceloop",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
// To overcome Status.Output character limit, we decided to create a
// Stream Trace CRD each time we do the collect operation.
// Nonetheless, this Trace CRD will use a previously created Trace.
// To do so, we use the Parameters["name"] which will contain the name
// of the long lived Trace CRD, thus we will be able to get the Trace
// and so all the mntNsIDs associated to it.
t, err := f.Lookup(trace.Spec.Parameters["name"])
if err != nil {
trace.Status.OperationError = fmt.Sprintf("no global trace with name %q: %s", name, err)
return
}
t.(*Trace).Collect(trace)
},
},
gadgetv1alpha1.OperationDelete: {
Doc: "Delete a perf ring buffer owned by traceloop",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
// To overcome Status.Output character limit, we decided to create a
// Stream Trace CRD each time we do the collect operation.
// Nonetheless, this Trace CRD will use a previously created Trace.
// To do so, we use the Parameters["name"] which will contain the name
// of the long lived Trace CRD, thus we will be able to get the Trace
// and so all the mntNsIDs associated to it.
t, err := f.Lookup(trace.Spec.Parameters["name"])
if err != nil {
trace.Status.OperationError = fmt.Sprintf("no global trace with name %q: %s", name, err)
return
}
t.(*Trace).Delete(trace)
},
},
gadgetv1alpha1.OperationStop: {
Doc: "Stop traceloop",
Operation: func(name string, trace *gadgetv1alpha1.Trace) {
f.LookupOrCreate(name, n).(*Trace).Stop(trace)
},
},
}
}
type pubSubKey string
func genPubSubKey(name string) pubSubKey {
return pubSubKey(fmt.Sprintf("gadget/traceloop/%s", name))
}
func (t *Trace) Start(trace *gadgetv1alpha1.Trace) {
if trace.Spec.OutputMode != gadgetv1alpha1.TraceOutputModeStatus {
trace.Status.OperationError = fmt.Sprintf("\"start\" operation can only be used with %q trace while %q was given", gadgetv1alpha1.TraceOutputModeStatus, trace.Spec.OutputMode)
return
}
if t.started {
trace.Status.State = gadgetv1alpha1.TraceStateStarted
return
}
// Having this backlink is mandatory for delete operation.
t.trace = trace
// The output will contain an array of types.TraceloopInfo.
// So, to avoid problems, we initialize it to be a JSON array.
trace.Status.Output = "[]"
t.containerIDs = make(map[string]*containerSlim, 0)
genKey := func(container *containercollection.Container) string {
return container.K8s.Namespace + "/" + container.K8s.PodName
}
attachContainerFunc := func(container *containercollection.Container) error {
containerID := container.Runtime.ContainerID
mntNsID := container.Mntns
key := genKey(container)
traceUnique.Lock()
err := traceUnique.tracer.Attach(containerID, mntNsID)
traceUnique.Unlock()
if err != nil {
log.Errorf("failed to attach tracer: %s", err)
return err
}
t.containerIDs[containerID] = &containerSlim{
mntnsid: mntNsID,
}
log.Debugf("tracer attached for %q (%d)", key, mntNsID)
var infos []types.TraceloopInfo
err = json.Unmarshal([]byte(trace.Status.Output), &infos)
if err != nil {
log.Errorf("failed to unmarshal output: %s", err)
return err
}
infos = append(infos, types.TraceloopInfo{
Namespace: container.K8s.Namespace,
Podname: container.K8s.PodName,
Containername: container.K8s.ContainerName,
ContainerID: containerID,
})
output, err := json.Marshal(infos)
if err != nil {
log.Errorf("failed to marshal infos: %s", err)
return err
}
traceBeforePatch := trace.DeepCopy()
trace.Status.Output = string(output)
patch := client.MergeFrom(traceBeforePatch)
// The surrounding function can be called from any context.
// So, we need to manually patch the trace CRD to have our modifications be
// taken into account.
err = t.client.Status().Patch(context.TODO(), trace, patch)
if err != nil {
log.Errorf("Failed to patch trace %q output: %s", trace.Name, err)
return err
}
return nil
}
detachContainerFunc := func(container *containercollection.Container) {
mntNsID := container.Mntns
key := genKey(container)
traceUnique.Lock()
err := traceUnique.tracer.Detach(mntNsID)
traceUnique.Unlock()
if err != nil {
log.Errorf("failed to detach tracer: %s", err)
return
}
_, ok := t.containerIDs[container.Runtime.ContainerID]
if ok {
t.containerIDs[container.Runtime.ContainerID].detached = true
} else {
log.Errorf("trace does not know about container with ID %q", container.Runtime.ContainerID)
return
}
log.Debugf("tracer detached for %q (%d)", key, mntNsID)
}
containerEventCallback := func(event containercollection.PubSubEvent) {
switch event.Type {
case containercollection.EventTypeAddContainer:
attachContainerFunc(event.Container)
case containercollection.EventTypeRemoveContainer:
detachContainerFunc(event.Container)
}
}
traceUnique.Lock()
if traceUnique.tracer == nil {
var syscallFilters string
var err error
filters, ok := trace.Spec.Parameters["syscall-filters"]
if ok {
syscallFilters = filters
}
traceUnique.tracer, err = tracelooptracer.NewTracer(t.helpers, strings.Split(syscallFilters, ","))
if err != nil {
traceUnique.Unlock()
trace.Status.OperationError = fmt.Sprintf("Failed to start traceloop tracer: %s", err)
return
}
}
traceUnique.users++
traceUnique.Unlock()
existingContainers := t.helpers.Subscribe(
genPubSubKey(trace.ObjectMeta.Namespace+"/"+trace.ObjectMeta.Name),
*gadgets.ContainerSelectorFromContainerFilter(trace.Spec.Filter),
containerEventCallback,
)
for _, c := range existingContainers {
err := attachContainerFunc(c)
if err != nil {
log.Warnf("couldn't attach BPF program: %s", err)
continue
}
}
t.started = true
trace.Status.State = gadgetv1alpha1.TraceStateStarted
}
func (t *Trace) Collect(trace *gadgetv1alpha1.Trace) {
if trace.Spec.OutputMode != gadgetv1alpha1.TraceOutputModeStream {
trace.Status.OperationError = fmt.Sprintf("\"collect\" operation can only be used with %q trace while %q was given", gadgetv1alpha1.TraceOutputModeStream, trace.Spec.OutputMode)
return
}
traceUnique.Lock()
if traceUnique.tracer == nil {
traceUnique.Unlock()
trace.Status.OperationError = "Traceloop tracer is nil"
return
}
traceUnique.Unlock()
containerID := trace.Spec.Parameters["containerID"]
_, ok := t.containerIDs[containerID]
if !ok {
ids := make([]string, len(t.containerIDs))
i := 0
for id := range t.containerIDs {
ids[i] = id
i++
}
trace.Status.OperationError = fmt.Sprintf("%q is not a valid ID for this trace, valid IDs are: %v", containerID, strings.Join(ids, ","))
return
}
traceUnique.Lock()
events, err := traceUnique.tracer.Read(containerID)
traceUnique.Unlock()
if err != nil {
trace.Status.OperationError = fmt.Sprintf("Failed to read perf buffer: %s", err)
return
}
traceName := gadgets.TraceName(trace.ObjectMeta.Namespace, trace.ObjectMeta.Name)
r, err := json.Marshal(events)
if err != nil {
log.Warnf("Gadget %s: error marshaling event: %s", trace.Spec.Gadget, err)
return
}
// HACK Traceloop is really particular as it cannot use Status output because
// the size is limited.
// Also, if we send each event as a line, we will only be able to get the last
// 100 (or 250) events from the CLI due to this code:
// https://github.com/inspektor-gadget/inspektor-gadget/blob/9c7b6a126d82b54262ffdc5709d7c92480002830/pkg/gadgettracermanager/stream/stream.go#L24
// https://github.com/inspektor-gadget/inspektor-gadget/blob/9c7b6a126d82b54262ffdc5709d7c92480002830/pkg/gadgettracermanager/stream/stream.go#L95-L100
// To overcome this limitation, we just send all events as one big line.
// Then, the CLI receives this big line, parses it and prints each event.
// A proper solution would be to develop a specific "output" (neither status
// nor stream) for traceloop, this is let as TODO.
t.helpers.PublishEvent(traceName, string(r))
trace.Status.State = gadgetv1alpha1.TraceStateCompleted
}
func (t *Trace) Delete(trace *gadgetv1alpha1.Trace) {
if trace.Spec.OutputMode != gadgetv1alpha1.TraceOutputModeStatus {
trace.Status.OperationError = fmt.Sprintf("\"delete\" operation can only be used with %q trace while %q was given", gadgetv1alpha1.TraceOutputModeStatus, trace.Spec.OutputMode)
return
}
containerID := trace.Spec.Parameters["containerID"]
container, ok := t.containerIDs[containerID]
if !ok {
ids := make([]string, len(t.containerIDs))
i := 0
for id := range t.containerIDs {
ids[i] = id
i++
}
trace.Status.OperationError = fmt.Sprintf("%q is not a valid ID for this trace, valid IDs are: %v", containerID, strings.Join(ids, ","))
return
}
traceUnique.Lock()
if traceUnique.tracer == nil {
traceUnique.Unlock()
trace.Status.OperationError = "Traceloop tracer is nil"
return
}
// First, we need to detach the perf buffer.
// We do not check the returned error because if the container was deleted it
// was already detached.
if !container.detached {
_ = traceUnique.tracer.Detach(container.mntnsid)
}
// Then we can remove it.
err := traceUnique.tracer.Delete(containerID)
traceUnique.Unlock()
if err != nil {
trace.Status.OperationError = fmt.Sprintf("Failed to delete perf buffer: %s", err)
return
}
// We can now remove containerID from the map.
delete(t.containerIDs, containerID)
// Finally, we need to update the trace output.
var infos []types.TraceloopInfo
err = json.Unmarshal([]byte(t.trace.Status.Output), &infos)
if err != nil {
trace.Status.OperationError = fmt.Sprintf("failed to unmarshal output: %s", err)
return
}
newInfos := make([]types.TraceloopInfo, len(infos)-1)
i := 0
for _, info := range infos {
// We copy all the current information except the one corresponding to the
// container we removed.
if info.ContainerID == containerID {
continue
}
newInfos[i] = info
i++
}
output, err := json.Marshal(newInfos)
if err != nil {
trace.Status.OperationError = fmt.Sprintf("failed to marshal new infos: %s", err)
return
}
traceBeforePatch := t.trace.DeepCopy()
t.trace.Status.Output = string(output)
patch := client.MergeFrom(traceBeforePatch)
// We also need to manually patch the trace CRD to have our modifications be
// taken into account.
err = t.client.Status().Patch(context.TODO(), t.trace, patch)
if err != nil {
log.Errorf("failed to patch trace %q output: %s", trace.Name, err)
return
}
trace.Status.State = gadgetv1alpha1.TraceStateCompleted
}
func (t *Trace) Stop(trace *gadgetv1alpha1.Trace) {
if trace.Spec.OutputMode != gadgetv1alpha1.TraceOutputModeStatus {
trace.Status.OperationError = fmt.Sprintf("\"stop\" operation can only be used with %q trace while %q was given", gadgetv1alpha1.TraceOutputModeStatus, trace.Spec.OutputMode)
return
}
if !t.started {
trace.Status.OperationError = "Not started"
return
}
t.helpers.Unsubscribe(genPubSubKey(trace.ObjectMeta.Namespace + "/" + trace.ObjectMeta.Name))
traceUnique.Lock()
traceUnique.users--
if traceUnique.users == 0 {
traceUnique.tracer.Stop()
traceUnique.tracer = nil
}
traceUnique.Unlock()
t.started = false
trace.Status.State = gadgetv1alpha1.TraceStateStopped
}