/
processor.go
447 lines (371 loc) · 11.4 KB
/
processor.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
package linuxmonitor
import (
"encoding/json"
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/collector"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/rpc/events"
"github.com/aporeto-inc/trireme-lib/rpc/processor"
"github.com/aporeto-inc/trireme-lib/utils/cgnetcls"
"github.com/aporeto-inc/trireme-lib/utils/contextstore"
"github.com/aporeto-inc/trireme-lib/utils/portspec"
)
// StoredContext is the information stored to retrieve the context in case of restart.
type StoredContext struct {
EventInfo *events.EventInfo
Tags *policy.TagStore `json:"Tags,omitempty"`
}
// linuxProcessor captures all the monitor processor information
// It implements the EventProcessor interface of the rpc monitor
type linuxProcessor struct {
host bool
config *processor.Config
metadataExtractor events.EventMetadataExtractor
netcls cgnetcls.Cgroupnetcls
contextStore contextstore.ContextStore
regStart *regexp.Regexp
regStop *regexp.Regexp
storePath string
}
func baseName(name, separator string) string {
lastseparator := strings.LastIndex(name, separator)
if len(name) <= lastseparator {
return ""
}
return name[lastseparator+1:]
}
// RemapData Remaps the contextstore data from an old format to the newer format.
func (l *linuxProcessor) RemapData(data string, fixedData interface{}) error {
event := &events.EventInfo{}
if err := json.Unmarshal([]byte(data), event); err != nil {
return fmt.Errorf("Received error %s while remapping data", err)
}
// Convert the eventInfo data to new format
for index, s := range event.Services {
if s.Port != 0 {
s.Ports = &portspec.PortSpec{
Min: s.Port,
Max: s.Port,
}
}
event.Services[index].Ports = s.Ports
}
sc, ok := fixedData.(*StoredContext)
if !ok {
return fmt.Errorf("Invalid data type")
}
if sc.Tags == nil {
sc.Tags = policy.NewTagStore()
}
sc.EventInfo = event
return nil
}
// Create handles create events
func (l *linuxProcessor) Create(eventInfo *events.EventInfo) error {
if !l.regStart.Match([]byte(eventInfo.PUID)) {
return fmt.Errorf("invalid pu id: %s", eventInfo.PUID)
}
return l.config.PUHandler.HandlePUEvent(eventInfo.PUID, events.EventCreate)
}
// startInternal is called while starting and reacquiring.
func (l *linuxProcessor) startInternal(runtimeInfo *policy.PURuntime, eventInfo *events.EventInfo) (err error) {
// Validate the PUID format
if !l.regStart.Match([]byte(eventInfo.PUID)) {
return fmt.Errorf("invalid pu id: %s", eventInfo.PUID)
}
// Setup the run time
if err = l.config.PUHandler.CreatePURuntime(eventInfo.PUID, runtimeInfo); err != nil {
return fmt.Errorf("create runtime failed: %s", err)
}
if err = l.config.PUHandler.HandlePUEvent(eventInfo.PUID, events.EventStart); err != nil {
return fmt.Errorf("handle pu failed: %s", err)
}
if eventInfo.HostService {
err = l.processHostServiceStart(eventInfo, runtimeInfo)
} else {
err = l.processLinuxServiceStart(eventInfo, runtimeInfo)
}
if err != nil {
return fmt.Errorf("start pu failed: %s", err)
}
l.config.Collector.CollectContainerEvent(&collector.ContainerRecord{
ContextID: eventInfo.PUID,
IPAddress: runtimeInfo.IPAddresses(),
Tags: runtimeInfo.Tags(),
Event: collector.ContainerStart,
})
// Store the state in the context store for future access
return l.contextStore.Store(eventInfo.PUID, &StoredContext{
EventInfo: eventInfo,
Tags: runtimeInfo.Tags(),
})
}
// Start handles start events
func (l *linuxProcessor) Start(eventInfo *events.EventInfo) error {
// Extract the metadata
runtimeInfo, err := l.metadataExtractor(eventInfo)
if err != nil {
return err
}
return l.startInternal(runtimeInfo, eventInfo)
}
// Stop handles a stop event
func (l *linuxProcessor) Stop(eventInfo *events.EventInfo) error {
contextID, err := l.generateContextID(eventInfo)
if err != nil {
return err
}
if contextID == "/trireme" {
return nil
}
contextID = baseName(contextID, "/")
return l.config.PUHandler.HandlePUEvent(contextID, events.EventStop)
}
// Destroy handles a destroy event
func (l *linuxProcessor) Destroy(eventInfo *events.EventInfo) error {
contextID, err := l.generateContextID(eventInfo)
if err != nil {
return err
}
if contextID == "/trireme" {
contextID = strings.TrimLeft(contextID, "/")
l.netcls.Deletebasepath(contextID)
return nil
}
contextID = baseName(contextID, "/")
// Send the event upstream
if err := l.config.PUHandler.HandlePUEvent(contextID, events.EventDestroy); err != nil {
zap.L().Warn("Unable to clean trireme ",
zap.String("contextID", contextID),
zap.Error(err),
)
}
if eventInfo.HostService {
if err := ioutil.WriteFile("/sys/fs/cgroup/net_cls,net_prio/net_cls.classid", []byte("0"), 0644); err != nil {
return fmt.Errorf("unable to write to net_cls.classid file for new cgroup: %s", err)
}
}
//let us remove the cgroup files now
if err := l.netcls.DeleteCgroup(contextID); err != nil {
zap.L().Warn("Failed to clean netcls group",
zap.String("contextID", contextID),
zap.Error(err),
)
}
if err := l.contextStore.Remove(contextID); err != nil {
zap.L().Error("Failed to clean cache while destroying process",
zap.String("contextID", contextID),
zap.Error(err),
)
}
return nil
}
// Pause handles a pause event
func (l *linuxProcessor) Pause(eventInfo *events.EventInfo) error {
contextID, err := l.generateContextID(eventInfo)
if err != nil {
return fmt.Errorf("unable to generate context id: %s", err)
}
return l.config.PUHandler.HandlePUEvent(contextID, events.EventPause)
}
// ReSync resyncs with all the existing services that were there before we start
func (l *linuxProcessor) ReSync(e *events.EventInfo) error {
deleted := []string{}
reacquired := []string{}
retrieveFailed := 0
metadataExtractionFailed := 0
syncFailed := 0
puStartFailed := 0
invalidContextWithNoTags := 0
newPUCreated := 0
defer func() {
if retrieveFailed == 0 &&
metadataExtractionFailed == 0 &&
syncFailed == 0 &&
puStartFailed == 0 &&
invalidContextWithNoTags == 0 &&
newPUCreated == 0 {
zap.L().Debug("Linux process resync completed",
zap.Bool("host", l.host),
zap.Strings("deleted", deleted),
zap.Strings("reacquired", reacquired),
)
} else {
zap.L().Warn("Linux process resync completed with failures",
zap.Bool("host", l.host),
zap.Strings("deleted", deleted),
zap.Strings("reacquired", reacquired),
zap.Int("retrieve-failed", retrieveFailed),
zap.Int("metadata-extraction-failed", metadataExtractionFailed),
zap.Int("sync-failed", syncFailed),
zap.Int("start-failed", puStartFailed),
zap.Int("invalidContextWithNoTags", invalidContextWithNoTags),
zap.Int("newPUCreated", newPUCreated),
)
}
}()
walker, err := l.contextStore.Walk()
if err != nil {
return fmt.Errorf("unable to walk context store: %s", err)
}
for {
contextID := <-walker
if contextID == "" {
break
}
// Get contexts, runtime, eventinfo, etc ..
storedContext := StoredContext{}
if err := l.contextStore.Retrieve("/"+contextID, &storedContext); err != nil {
retrieveFailed++
continue
}
if storedContext.Tags == nil {
invalidContextWithNoTags++
continue
}
// Add specific tags
eventInfo := storedContext.EventInfo
for _, t := range l.config.MergeTags {
if val, ok := storedContext.Tags.Get(t); ok {
eventInfo.Tags = append(eventInfo.Tags, t+"="+val)
}
}
runtimeInfo, err := l.metadataExtractor(eventInfo)
if err != nil {
metadataExtractionFailed++
return err
}
t := runtimeInfo.Tags()
if t != nil && storedContext.Tags != nil {
t.Merge(storedContext.Tags)
runtimeInfo.SetTags(t)
}
if !eventInfo.HostService {
processlist, err := cgnetcls.ListCgroupProcesses(eventInfo.PUID)
if err != nil {
deleted = append(deleted, eventInfo.PUID)
if err := l.contextStore.Remove(eventInfo.PUID); err != nil {
zap.L().Warn("Failed to remove state from store handler",
zap.String("puID", eventInfo.PUID),
zap.Error(err))
}
continue
}
if len(processlist) <= 0 {
deleted = append(deleted, eventInfo.PUID)
// We have an empty cgroup. Remove the cgroup and context store file
if err := l.netcls.DeleteCgroup(eventInfo.PUID); err != nil {
zap.L().Warn("Failed to deleted cgroup",
zap.String("puID", eventInfo.PUID),
zap.Error(err),
)
}
if err := l.contextStore.Remove(eventInfo.PUID); err != nil {
zap.L().Warn("Failed to deleted context",
zap.String("puID", eventInfo.PUID),
zap.Error(err),
)
}
continue
}
}
reacquired = append(reacquired, eventInfo.PUID)
// Synchronize
if storedContext.Tags.IsEmpty() {
newPUCreated++
} else {
if l.config.SyncHandler != nil {
if err := l.config.SyncHandler.HandleSynchronization(
contextID,
events.StateStarted,
runtimeInfo,
processor.SynchronizationTypeInitial,
); err != nil {
zap.L().Debug("Failed to sync", zap.Error(err))
syncFailed++
continue
}
}
}
if err := l.startInternal(runtimeInfo, eventInfo); err != nil {
zap.L().Debug("Failed to start", zap.Error(err))
puStartFailed++
}
}
return nil
}
// generateContextID creates the contextID from the event information
func (l *linuxProcessor) generateContextID(eventInfo *events.EventInfo) (string, error) {
contextID := eventInfo.PUID
if eventInfo.Cgroup == "" {
return contextID, nil
}
if !l.regStop.Match([]byte(eventInfo.Cgroup)) {
return "", fmt.Errorf("invalid pu id: %s", eventInfo.Cgroup)
}
contextID = baseName(eventInfo.Cgroup, "/")
return contextID, nil
}
func (l *linuxProcessor) processLinuxServiceStart(event *events.EventInfo, runtimeInfo *policy.PURuntime) error {
list, err := cgnetcls.ListCgroupProcesses(event.PUID)
if err == nil {
//cgroup exists and pid might be a member
isrestart := func() bool {
for _, element := range list {
if element == event.PID {
//pid is already there it is restart
return true
}
}
return false
}()
if !isrestart {
pid, _ := strconv.Atoi(event.PID)
l.netcls.AddProcess(event.PUID, pid) // nolint
return nil
}
}
//It is okay to launch this so let us create a cgroup for it
err = l.netcls.Creategroup(event.PUID)
if err != nil {
return err
}
markval := runtimeInfo.Options().CgroupMark
if markval == "" {
if derr := l.netcls.DeleteCgroup(event.PUID); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return fmt.Errorf("mark value %s not found", markval)
}
mark, _ := strconv.ParseUint(markval, 10, 32)
err = l.netcls.AssignMark(event.PUID, mark)
if err != nil {
if derr := l.netcls.DeleteCgroup(event.PUID); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return err
}
pid, _ := strconv.Atoi(event.PID)
err = l.netcls.AddProcess(event.PUID, pid)
if err != nil {
if derr := l.netcls.DeleteCgroup(event.PUID); derr != nil {
zap.L().Warn("Failed to clean cgroup", zap.Error(derr))
}
return err
}
return nil
}
func (l *linuxProcessor) processHostServiceStart(event *events.EventInfo, runtimeInfo *policy.PURuntime) error {
if event.NetworkOnlyTraffic {
return nil
}
markval := runtimeInfo.Options().CgroupMark
mark, _ := strconv.ParseUint(markval, 10, 32)
hexmark := "0x" + (strconv.FormatUint(mark, 16))
return ioutil.WriteFile("/sys/fs/cgroup/net_cls,net_prio/net_cls.classid", []byte(hexmark), 0644)
}