forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf.go
354 lines (303 loc) · 8.33 KB
/
bpf.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
// Copyright 2016-2017 Authors of Cilium
//
// 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 bpf
/*
#cgo CFLAGS: -I../../bpf/include
#include <stdint.h>
#include <linux/unistd.h>
#include <linux/bpf.h>
#include <sys/resource.h>
#include <time.h>
static __u64 ptr_to_u64(const void *ptr)
{
return (__u64) (unsigned long) ptr;
}
void create_bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
int max_entries, unsigned long flags, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_type = map_type;
ptr_bpf_attr->key_size = key_size;
ptr_bpf_attr->value_size = value_size;
ptr_bpf_attr->max_entries = max_entries;
ptr_bpf_attr->map_flags = flags;
}
void create_bpf_update_elem(int fd, const void *key, const void *value,
unsigned long long flags, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->value = ptr_to_u64(value);
ptr_bpf_attr->flags = flags;
}
void create_bpf_lookup_elem(int fd, const void *key, void *value, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->value = ptr_to_u64(value);
}
void create_bpf_delete_elem(int fd, const void *key, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
}
void create_bpf_get_next_key(int fd, const void *key, void *next_key, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->map_fd = fd;
ptr_bpf_attr->key = ptr_to_u64(key);
ptr_bpf_attr->next_key = ptr_to_u64(next_key);
}
void create_bpf_obj_pin(int fd, const char *pathname, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->pathname = ptr_to_u64(pathname);
ptr_bpf_attr->bpf_fd = fd;
}
void create_bpf_obj_get(const char *pathname, void *attr)
{
union bpf_attr* ptr_bpf_attr;
ptr_bpf_attr = (union bpf_attr*)attr;
ptr_bpf_attr->pathname = ptr_to_u64(pathname);
}
*/
import "C"
import (
"fmt"
"math"
"os"
"path/filepath"
"unsafe"
"golang.org/x/sys/unix"
)
const (
BPF_MAP_TYPE_HASH = C.BPF_MAP_TYPE_HASH
BPF_MAP_TYPE_LPM_TRIE = C.BPF_MAP_TYPE_LPM_TRIE
BPF_F_NO_PREALLOC = C.BPF_F_NO_PREALLOC
)
// CreateMap creates a Map of type mapType, with key size keySize, a value size of
// valueSize and the maximum amount of entries of maxEntries.
// mapType should be one of the bpf_map_type in "uapi/linux/bpf.h"
func CreateMap(mapType int, keySize, valueSize, maxEntries, flags uint32) (int, error) {
uba := C.union_bpf_attr{}
C.create_bpf_create_map(
C.enum_bpf_map_type(mapType),
C.int(keySize),
C.int(valueSize),
C.int(maxEntries),
C.ulong(flags),
unsafe.Pointer(&uba),
)
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_MAP_CREATE,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 {
return int(ret), nil
}
return 0, fmt.Errorf("Unable to create map: %s", err)
}
// UpdateElement updates the map in fd with the given value in the given key.
// The flags can have the following values (if you include "uapi/linux/bpf.h"):
// C.BPF_ANY to create new element or update existing;
// C.BPF_NOEXIST to create new element if it didn't exist;
// C.BPF_EXIST to update existing element.
func UpdateElement(fd int, key, value unsafe.Pointer, flags uint64) error {
uba := C.union_bpf_attr{}
C.create_bpf_update_elem(
C.int(fd),
key,
value,
C.ulonglong(flags),
unsafe.Pointer(&uba),
)
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_MAP_UPDATE_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to update element: %s", err)
}
return nil
}
// LookupElement looks up for the map value stored in fd with the given key. The value
// is stored in the value unsafe.Pointer.
func LookupElement(fd int, key, value unsafe.Pointer) error {
uba := C.union_bpf_attr{}
C.create_bpf_lookup_elem(
C.int(fd),
key,
value,
unsafe.Pointer(&uba),
)
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_MAP_LOOKUP_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to lookup element: %s", err)
}
return nil
}
// DeleteElement deletes the map element with the given key.
func DeleteElement(fd int, key unsafe.Pointer) error {
uba := C.union_bpf_attr{}
C.create_bpf_delete_elem(
C.int(fd),
key,
unsafe.Pointer(&uba),
)
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_MAP_DELETE_ELEM,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to delete element: %s", err)
}
return nil
}
// GetNextKey stores, in nextKey, the next key after the key of the map in fd.
func GetNextKey(fd int, key, nextKey unsafe.Pointer) error {
uba := C.union_bpf_attr{}
C.create_bpf_get_next_key(
C.int(fd),
key,
nextKey,
unsafe.Pointer(&uba),
)
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_MAP_GET_NEXT_KEY,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to get next key: %s", err)
}
return nil
}
// ObjPin stores the map's fd in pathname.
func ObjPin(fd int, pathname string) error {
pathStr := C.CString(pathname)
uba := C.union_bpf_attr{}
C.create_bpf_obj_pin(C.int(fd), pathStr, unsafe.Pointer(&uba))
ret, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_OBJ_PIN,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to pin object: %s", err)
}
return nil
}
// ObjGet reads the pathname and returns the map's fd read.
func ObjGet(pathname string) (int, error) {
pathStr := C.CString(pathname)
uba := C.union_bpf_attr{}
C.create_bpf_obj_get(pathStr, unsafe.Pointer(&uba))
fd, _, err := unix.Syscall(
C.__NR_bpf,
C.BPF_OBJ_GET,
uintptr(unsafe.Pointer(&uba)),
unsafe.Sizeof(uba),
)
if fd == 0 || err != 0 {
return 0, fmt.Errorf("Unable to get object %s: %s", pathname, err)
}
return int(fd), nil
}
// ObjClose closes the map's fd.
func ObjClose(fd int) error {
if fd > 0 {
return unix.Close(fd)
}
return nil
}
func OpenOrCreateMap(path string, mapType int, keySize, valueSize, maxEntries, flags uint32) (int, bool, error) {
var fd int
isNewMap := false
rl := unix.Rlimit{
Cur: math.MaxUint64,
Max: math.MaxUint64,
}
err := unix.Setrlimit(C.RLIMIT_MEMLOCK, &rl)
if err != nil {
return 0, isNewMap, fmt.Errorf("Unable to increase rlimit: %s", err)
}
if _, err = os.Stat(path); os.IsNotExist(err) {
mapDir := filepath.Dir(path)
if _, err = os.Stat(mapDir); os.IsNotExist(err) {
if err = os.MkdirAll(mapDir, 0755); err != nil {
return 0, isNewMap, fmt.Errorf("Unable create map base directory: %s", err)
}
}
fd, err = CreateMap(
mapType,
keySize,
valueSize,
maxEntries,
flags,
)
defer func() {
if err != nil {
// In case of error, we need to close
// this fd since it was open by CreateMap
ObjClose(fd)
}
}()
isNewMap = true
if err != nil {
return 0, isNewMap, err
}
err = ObjPin(fd, path)
if err != nil {
return 0, isNewMap, err
}
return fd, isNewMap, nil
}
fd, err = ObjGet(path)
return fd, isNewMap, err
}
// GetMtime returns monotonic time that can be used to compare
// values with ktime_get_ns() BPF helper, e.g. needed to check
// the timeout in sec for BPF entries. We return the raw nsec,
// although that is not quite usable for comparison. Go has
// runtime.nanotime() but doesn't expose it as API.
func GetMtime() (uint64, error) {
var ts unix.Timespec
err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts)
if err != nil {
return 0, fmt.Errorf("Unable get time: %s", err)
}
return uint64(unix.TimespecToNsec(ts)), nil
}