-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.go
171 lines (143 loc) · 3.91 KB
/
module.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
package ebpftracer
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
"github.com/castai/kvisor/pkg/logging"
"github.com/castai/kvisor/pkg/proc"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sys/unix"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type global_config_t -no-global-types -cc clang-14 -strip=llvm-strip -target arm64 tracer ./c/tracee.bpf.c -- -I./c/headers -Wno-address-of-packed-member -O2
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type global_config_t -no-global-types -cc clang-14 -strip=llvm-strip -target amd64 tracer ./c/tracee.bpf.c -- -I./c/headers -Wno-address-of-packed-member -O2
type moduleConfig struct {
BTFObjPath string
}
func newModule(log *logging.Logger, cfg moduleConfig) *module {
return &module{
log: log,
cfg: cfg,
loaded: &atomic.Bool{},
attachedProbes: map[handle]struct{}{},
}
}
// module is responsible for loading ebpf objects (programs and maps).
type module struct {
log *logging.Logger
objects *tracerObjects
cfg moduleConfig
loaded *atomic.Bool
probes map[handle]probe
attachedProbes map[handle]struct{}
probesMu sync.Mutex
}
func (m *module) load(targetPIDNSID proc.NamespaceID, flowSampleSubmitIntervalSeconds uint64) error {
if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &unix.Rlimit{
Cur: 4096,
Max: 4096,
}); err != nil {
return fmt.Errorf("setting temporary rlimit: %w", err)
}
if err := rlimit.RemoveMemlock(); err != nil {
return err
}
objs := tracerObjects{}
spec, err := loadTracer()
if err != nil {
return err
}
var kernelTypes *btf.Spec
if m.cfg.BTFObjPath != "" {
kernelTypes, err = btf.LoadSpec(m.cfg.BTFObjPath)
if err != nil {
return fmt.Errorf("loading custom btf: %w", err)
}
}
if err := spec.RewriteConstants(map[string]interface{}{
"global_config": tracerGlobalConfigT{
PidNsId: targetPIDNSID,
FlowSampleSubmitIntervalSeconds: flowSampleSubmitIntervalSeconds,
},
}); err != nil {
return err
}
if err := spec.LoadAndAssign(&objs, &ebpf.CollectionOptions{
Maps: ebpf.MapOptions{},
Programs: ebpf.ProgramOptions{
LogLevel: 0,
LogSize: 200_000_000,
LogDisabled: false,
KernelTypes: kernelTypes,
},
MapReplacements: nil,
}); err != nil {
return err
}
m.objects = &objs
// TODO(Kvisord): Mount cgroupv2 if not mounted.
cgroupPath, err := detectCgroupPath()
if err != nil {
cgroupPath = "/cgroupv2"
m.log.Debugf("mounting cgroupv2 to path %s", cgroupPath)
if err := mountCgroup2(cgroupPath); err != nil {
return fmt.Errorf("mounting cgroupv2: %w", err)
}
}
m.probes = newProbes(m.objects, cgroupPath)
m.loaded.Store(true)
return nil
}
func (m *module) close() error {
if !m.loaded.Load() {
return nil
}
m.probesMu.Lock()
defer m.probesMu.Unlock()
// Close bpf probes.
for handle := range m.attachedProbes {
probe := m.probes[handle]
if err := probe.detach(); err != nil {
return fmt.Errorf("detach probe: %s: %w", probe.String(), err)
}
}
// Close programs.
return m.objects.Close()
}
func (m *module) attachProbe(handle handle) error {
m.probesMu.Lock()
defer m.probesMu.Unlock()
if _, found := m.attachedProbes[handle]; found {
return nil
}
probe, found := m.probes[handle]
if !found {
return fmt.Errorf("probe %d not registered", handle)
}
m.log.Debugf("attaching probe: %s", probe.String())
if err := probe.attach(); err != nil {
return err
}
m.attachedProbes[handle] = struct{}{}
return nil
}
func detectCgroupPath() (string, error) {
f, err := os.Open("/proc/mounts")
if err != nil {
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Split(scanner.Text(), " ")
if len(fields) >= 3 && fields[2] == "cgroup2" {
return fields[1], nil
}
}
return "", errors.New("cgroupv2 not found, need to mount")
}