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
|
package internal
import "C"
import (
"bytes"
"encoding/binary"
"fmt"
. "ioriotng/internal/generated/types"
bpf "github.com/aquasecurity/libbpfgo"
)
func eventLoop(bpfModule *bpf.Module, ch <-chan []byte) {
enterOpen := make(map[uint32]*OpenEnterEvent)
enterFd := make(map[uint32]*FdEvent)
// To do this, extract the PID from the TID (pid_tid >> 32)
// openFiles := make(map[
for raw := range ch {
switch OpId(raw[0]) {
case OPENAT_ENTER_OP_ID:
fallthrough
case OPEN_ENTER_OP_ID:
ev := readRaw(raw, NewOpenEnterEvent())
enterOpen[ev.PidTgid] = ev
case OPENAT_EXIT_OP_ID:
fallthrough
case OPEN_EXIT_OP_ID:
ev := readRaw(raw, NewFdEvent())
enterEv, ok := enterOpen[ev.PidTgid]
if !ok {
fmt.Println("Dropping", ev)
RecycleFdEvent(ev)
continue
}
duration := float64(ev.Time-enterEv.Time) / float64(1_000_000)
fmt.Println(duration, "ms", enterEv, ev)
delete(enterOpen, ev.PidTgid)
RecycleFdEvent(ev)
RecycleOpenEnterEvent(enterEv)
case CLOSE_ENTER_OP_ID:
fallthrough
case WRITE_ENTER_OP_ID:
fallthrough
case WRITEV_ENTER_OP_ID:
ev := readRaw(raw, NewFdEvent())
enterFd[ev.PidTgid] = ev
case CLOSE_EXIT_OP_ID:
fallthrough
case WRITE_EXIT_OP_ID:
fallthrough
case WRITEV_EXIT_OP_ID:
ev := readRaw(raw, NewNullEvent())
enterEv, ok := enterFd[ev.PidTgid]
if !ok {
fmt.Println("Dropping", ev)
RecycleNullEvent(ev)
continue
}
duration := float64(ev.Time-enterEv.Time) / float64(1_000_000)
fmt.Println(duration, "ms", enterEv, ev)
delete(enterFd, ev.PidTgid)
RecycleNullEvent(ev)
RecycleFdEvent(enterEv)
default:
panic(fmt.Sprintf("UNKNOWN Ringbuf data received len:%d raw:%v", len(raw), raw))
}
}
fmt.Println("Good bye")
}
func readRaw[T any](raw []byte, ev *T) *T {
if err := binary.Read(bytes.NewReader(raw), binary.LittleEndian, ev); err != nil {
fmt.Println(ev, raw, len(raw), err)
panic(raw)
}
return ev
}
|