-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemulator_linux_map_stack_queue.go
216 lines (172 loc) · 5.59 KB
/
emulator_linux_map_stack_queue.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
package mimic
import (
"fmt"
"sync"
"syscall"
"github.com/cilium/ebpf"
)
var (
_ LinuxMap = (*LinuxQueueMap)(nil)
_ LinuxMapPopper = (*LinuxQueueMap)(nil)
_ LinuxMapPusher = (*LinuxQueueMap)(nil)
)
// LinuxQueueMap is the emulated version of ebpf.Queue / BPF_MAP_TYPE_QUEUE.
// This map type has no keys, value sizes are fixed, but is configurable. This map type is a FIFO queue.
type LinuxQueueMap struct {
Spec *ebpf.MapSpec
emulator *LinuxEmulator
buffer *ringBuffer
}
// Init initializes the map, part of the LinuxMap implementation
func (m *LinuxQueueMap) Init(emulator *LinuxEmulator) error {
size := (1 + m.Spec.MaxEntries) * m.Spec.ValueSize
m.emulator = emulator
m.buffer = &ringBuffer{}
err := m.buffer.Init(&emulator.vm.MemoryController, int(size), fmt.Sprintf("%s-values", m.Spec.Name))
if err != nil {
return fmt.Errorf("buf init: %w", err)
}
// The the map itself to the memory controller
_, err = emulator.vm.MemoryController.AddEntry(m, 8, m.Spec.Name)
if err != nil {
return fmt.Errorf("add map to memory controller: %w", err)
}
return nil
}
// GetSpec returns the specification of the map, part of the LinuxMap implementation
func (m *LinuxQueueMap) GetSpec() ebpf.MapSpec {
return *m.Spec
}
// Indices returns the amount of per-cpu indexes.
func (m *LinuxQueueMap) Indices() int {
return 1
}
// Keys returns a byte slice which contains all keys in the map, keys are packed, the user is expected to calculate
// the proper window into the slice based on the size of m.Spec.KeySize.
func (m *LinuxQueueMap) Keys(cpuid int) []byte {
values := m.buffer.used() / m.Spec.ValueSize
b := make([]byte, 4*values)
bo := GetNativeEndianness()
for i := uint32(0); i < values; i++ {
bo.PutUint32(b[i*4:(i+1)*4], i)
}
return b
}
// Lookup returns the virtual memory offset to the map value or 0 if no value can be found for the given key.
func (m *LinuxQueueMap) Lookup(key []byte, cpuid int) (uint32, error) {
// Map is empty
if m.buffer.Used() < m.Spec.ValueSize {
return 0, nil
}
index := GetNativeEndianness().Uint32(key)
return m.buffer.PeekAddr(m.Spec.ValueSize * index)
}
// Push pushes a new value into the ring buffer
func (m *LinuxQueueMap) Push(value []byte, cpuid int) error {
if len(value) != int(m.Spec.ValueSize) {
return fmt.Errorf("value doesn't match the value size of the map")
}
return m.buffer.Write(value)
}
// Pop pops a value from the ring buffer
func (m *LinuxQueueMap) Pop(cpuid int) (uint32, error) {
addr, err := m.buffer.ReadAddr(m.Spec.ValueSize)
if err != nil {
return 0, fmt.Errorf("buf read value: %w", err)
}
return addr, nil
}
var (
_ LinuxMap = (*LinuxStackMap)(nil)
_ LinuxMapPopper = (*LinuxStackMap)(nil)
_ LinuxMapPusher = (*LinuxStackMap)(nil)
)
// LinuxStackMap is the emulated version of ebpf.PerfEventArray / BPF_MAP_TYPE_PERF_EVENT_ARRAY.
// Array maps have 4 byte integer keys from 0 to Spec.MaxEntries with arbitrary values
type LinuxStackMap struct {
Spec *ebpf.MapSpec
emulator *LinuxEmulator
mu sync.Mutex
stack *PlainMemory
topOff uint32
addr uint32
}
// Init initializes the map, part of the LinuxMap implementation
func (m *LinuxStackMap) Init(emulator *LinuxEmulator) error {
m.emulator = emulator
size := m.Spec.MaxEntries * m.Spec.ValueSize
m.stack = &PlainMemory{
Backing: make([]byte, size),
}
m.topOff = size
entry, err := emulator.vm.MemoryController.AddEntry(m.stack, size, fmt.Sprintf("%s-values", m.Spec.Name))
if err != nil {
return fmt.Errorf("add map values to memory controller: %w", err)
}
m.addr = entry.Addr
// The the map itself to the memory controller
_, err = emulator.vm.MemoryController.AddEntry(m, 8, m.Spec.Name)
if err != nil {
return fmt.Errorf("add map to memory controller: %w", err)
}
return nil
}
// GetSpec returns the specification of the map, part of the LinuxMap implementation
func (m *LinuxStackMap) GetSpec() ebpf.MapSpec {
return *m.Spec
}
// Indices returns the amount of per-cpu indexes.
func (m *LinuxStackMap) Indices() int {
return 1
}
// Keys returns a byte slice which contains all keys in the map, keys are packed, the user is expected to calculate
// the proper window into the slice based on the size of m.Spec.KeySize.
func (m *LinuxStackMap) Keys(cpuid int) []byte {
m.mu.Lock()
defer m.mu.Unlock()
values := (uint32(len(m.stack.Backing)) - m.topOff) / m.Spec.ValueSize
fmt.Println(values)
b := make([]byte, 4*values)
bo := GetNativeEndianness()
for i := uint32(0); i < values; i++ {
bo.PutUint32(b[i*4:(i+1)*4], i)
}
return b
}
// Lookup returns the virtual memory offset to the map value or 0 if no value can be found for the given key.
func (m *LinuxStackMap) Lookup(key []byte, cpuid int) (uint32, error) {
m.mu.Lock()
defer m.mu.Unlock()
// Map is empty
if int(m.topOff) == len(m.stack.Backing) {
return 0, nil
}
index := GetNativeEndianness().Uint32(key)
return m.addr + m.topOff + (m.Spec.ValueSize * index), nil
}
// Push pushes a new value into the ring buffer
func (m *LinuxStackMap) Push(value []byte, cpuid int) error {
m.mu.Lock()
defer m.mu.Unlock()
if len(value) != int(m.Spec.ValueSize) {
return fmt.Errorf("value doesn't match the value size of the map")
}
// If stack is full
if int(m.topOff)-int(m.Spec.ValueSize) < 0 {
return syscall.E2BIG
}
m.topOff -= m.Spec.ValueSize
return m.stack.Write(m.topOff, value)
}
// Pop pops a value from the ring buffer
func (m *LinuxStackMap) Pop(cpuid int) (uint32, error) {
m.mu.Lock()
defer m.mu.Unlock()
// Map is empty
if int(m.topOff)+int(m.Spec.ValueSize) > len(m.stack.Backing) {
return 0, nil
}
off := m.topOff
m.topOff += m.Spec.ValueSize
return m.addr + off, nil
}