forked from dropbox/goebpf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
697 lines (604 loc) · 19.7 KB
/
map.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright (c) 2019 Dropbox, Inc.
// Full license can be found in the LICENSE file.
package goebpf
/*
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "bpf.h"
#include "bpf_helpers.h"
// Since eBPF mock package is optional and have definition of "__maps_head" symbol
// it may cause link error, so defining weak symbol here as well
struct __create_map_def maps_head;
__attribute__((weak)) struct __maps_head_def *__maps_head = (struct __maps_head_def*) &maps_head;
static int ebpf_map_create(const char *name, __u32 map_type, __u32 key_size, __u32 value_size,
__u32 max_entries, __u32 flags, __u32 inner_fd, void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.map_type = map_type;
attr.key_size = key_size;
attr.value_size = value_size;
attr.max_entries = max_entries;
attr.map_flags = flags;
attr.inner_map_fd = inner_fd;
strncpy((char*)&attr.map_name, name, BPF_OBJ_NAME_LEN - 1);
int res = syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
static int ebpf_map_update_elem(__u32 fd, const void *key, const void *value,
__u64 flags, void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
attr.flags = flags;
int res = syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
static int ebpf_map_lookup_elem(__u32 fd, const void *key, void *value,
void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
attr.value = ptr_to_u64(value);
int res = syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
static int ebpf_map_delete_elem(__u32 fd, const void *key,
void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.map_fd = fd;
attr.key = ptr_to_u64(key);
int res = syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
static int ebpf_obj_get(const char *pathname,
void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.pathname = ptr_to_u64(pathname);
int res = syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
static int ebpf_map_get_fd_by_id(__u32 id,
void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.map_id = id;
int fd = syscall(__NR_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return fd;
}
static int ebpf_obj_get_info_by_fd(__u32 fd, void *info, __u32 info_len,
void *log_buf, size_t log_size)
{
union bpf_attr attr = {};
attr.info.bpf_fd = fd;
attr.info.info = ptr_to_u64(info);
attr.info.info_len = info_len;
int res = syscall(__NR_bpf, BPF_OBJ_GET_INFO_BY_FD, &attr, sizeof(attr));
strncpy(log_buf, strerror(errno), log_size);
return res;
}
*/
import "C"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"strings"
"unsafe"
)
// MapType is eBPF map type enum
type MapType int
// Supported eBPF map types.
const (
MapTypeHash MapType = C.BPF_MAP_TYPE_HASH
MapTypeArray MapType = C.BPF_MAP_TYPE_ARRAY
MapTypeProgArray MapType = C.BPF_MAP_TYPE_PROG_ARRAY
MapTypePerfEventArray MapType = C.BPF_MAP_TYPE_PERF_EVENT_ARRAY
MapTypePerCPUHash MapType = C.BPF_MAP_TYPE_PERCPU_HASH
MapTypePerCPUArray MapType = C.BPF_MAP_TYPE_PERCPU_ARRAY
MapTypeStackTrace MapType = C.BPF_MAP_TYPE_STACK_TRACE
MapTypeCgroupArray MapType = C.BPF_MAP_TYPE_CGROUP_ARRAY
MapTypeLRUHash MapType = C.BPF_MAP_TYPE_LRU_HASH
MapTypeLRUPerCPUHash MapType = C.BPF_MAP_TYPE_LRU_PERCPU_HASH
MapTypeLPMTrie MapType = C.BPF_MAP_TYPE_LPM_TRIE
MapTypeArrayOfMaps MapType = C.BPF_MAP_TYPE_ARRAY_OF_MAPS
MapTypeHashOfMaps MapType = C.BPF_MAP_TYPE_HASH_OF_MAPS
MapTypeDevMap MapType = C.BPF_MAP_TYPE_DEVMAP
MapTypeSockMap MapType = C.BPF_MAP_TYPE_SOCKMAP
MapTypeCPUMap MapType = C.BPF_MAP_TYPE_CPUMAP
MapTypeXSKMap MapType = C.BPF_MAP_TYPE_XSKMAP
MapTypeSockHash MapType = C.BPF_MAP_TYPE_SOCKHASH
MapTypeCGroupStorage MapType = C.BPF_MAP_TYPE_CGROUP_STORAGE
MapTypeReusePortSockArray MapType = C.BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
MapTypePerCpuCGroupStorage MapType = C.BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
MapTypeQueue MapType = C.BPF_MAP_TYPE_QUEUE
MapTypeStack MapType = C.BPF_MAP_TYPE_STACK
MapTypeSKStorage MapType = C.BPF_MAP_TYPE_SK_STORAGE
)
// Optional flags for ebpf_map_create()
const (
bpfNoPrealloc = 1
bpfNoCommonLRU = 2
bpfNumaNode = 4
bpfReadOnly = 8
bpfWriteOnly = 16
bpfStackBuildId = 32
bpfZeroSeed = 64
bpfReadOnlyProgram = 128
bpfWriteOnlyProgram = 256
)
// Optional flags for ebpf_map_update_elem()
const (
bpfAny = C.BPF_ANY // create new element or update existing
bpfNoexist = C.BPF_NOEXIST // create new element if it didn't exist
bpfExist = C.BPF_EXIST // update existing element
bpfFLock = C.BPF_F_LOCK // spin_lock-ed map_lookup/map_update
)
// Returns user friendly name for MapType
func (t MapType) String() string {
switch t {
case MapTypeHash:
return "Hash"
case MapTypeArray:
return "Array"
case MapTypeProgArray:
return "Array of programs"
case MapTypePerfEventArray:
return "Event array"
case MapTypePerCPUHash:
return "Per-CPU hash"
case MapTypePerCPUArray:
return "Per-CPU array"
case MapTypeStackTrace:
return "Stack trace"
case MapTypeCgroupArray:
return "Cgroup array"
case MapTypeLRUHash:
return "LRU hash"
case MapTypeLRUPerCPUHash:
return "LRU per-CPU hash"
case MapTypeLPMTrie:
return "Longest prefix match trie"
case MapTypeArrayOfMaps:
return "Array of maps"
case MapTypeHashOfMaps:
return "Hash of maps"
case MapTypeDevMap:
return "Device map"
case MapTypeSockMap:
return "Socket map"
case MapTypeCPUMap:
return "CPU map"
case MapTypeXSKMap:
return "AF_XDP socket map"
case MapTypeSockHash:
return "Socket hash"
case MapTypeCGroupStorage:
return "CGroup storage"
case MapTypeReusePortSockArray:
return "Reuseport socket array"
case MapTypePerCpuCGroupStorage:
return "per-CPU CGroup storage"
case MapTypeQueue:
return "Queue"
case MapTypeStack:
return "Stack"
case MapTypeSKStorage:
return "Socket storage"
}
return "Unknown"
}
// EbpfMap is structure to define eBPF map
type EbpfMap struct {
fd int
// Map name, picked up automatically by loader from ELF section
Name string
Type MapType
KeySize int
ValueSize int
MaxEntries int
Flags int
// Name of eBPF map used as template for all inner maps. Only for array/hash of maps
InnerMapName string
InnerMapFd int
// Persistent eBPF map use case: contains path to special file in filesystem.
// WARNING: filesystem must be mounted as BPF
PersistentPath string
// In case of Per-CPU maps bpf_lookup call expects buffer equal to valueSize * nCPUs
// which will be populated with data from all possible CPUs
valueRealSize int
}
// CreateLPMtrieKey converts string representation of CIDR into net.IPNet
// in order to support special eBPF map type: LPMtrie ("Longest Prefix Match Trie")
// Can be used to match single IPv4/6 address with multiple CIDRs, like
// m.Insert(CreateLPMtrieKey("192.168.0.0/16"), "value16")
// m.Insert(CreateLPMtrieKey("192.168.0.0/24"), "value24")
//
// value, err := m.LookupString(CreateLPMtrieKey("192.168.0.10"))
//
// IP 192.168.0.10 matches both CIDRs, however lookup value will be "value24" since /24 prefix is smaller than /16
func CreateLPMtrieKey(s string) *net.IPNet {
var ipnet *net.IPNet
// Check if given address is CIDR
if strings.Contains(s, "/") {
_, ipnet, _ = net.ParseCIDR(s)
} else {
if strings.Contains(s, ":") {
// IPv6
_, ipnet, _ = net.ParseCIDR(s + "/128")
} else {
// IPv4
_, ipnet, _ = net.ParseCIDR(s + "/32")
}
}
return ipnet
}
// Creates map from ELF section definition
// Helper to create BPF map from binary representation stored
// in ELF section, defined in BPF program itself.
// Refer to bpf_helpers.h, struct bpf_map_def
const (
mapDefinitionSize = C.BPF_MAP_DEF_SIZE
mapDefinitionPersistentOffset = C.BPF_MAP_OFFSET_PERSISTENT
mapDefinitionInnerMapOffset = C.BPF_MAP_OFFSET_INNER_MAP
)
// Create EbpfMap binary data stored in ELF section
func newMapFromElfSection(data []byte) (*EbpfMap, error) {
if len(data) < mapDefinitionSize {
return nil, errors.New("Invalid binary representation of BPF map")
}
return &EbpfMap{
Type: MapType(binary.LittleEndian.Uint32(data[:4])),
KeySize: int(binary.LittleEndian.Uint32(data[4:])),
ValueSize: int(binary.LittleEndian.Uint32(data[8:])),
MaxEntries: int(binary.LittleEndian.Uint32(data[12:])),
Flags: int(binary.LittleEndian.Uint32(data[16:])),
}, nil
}
// NewMapFromExistingMapByFd creates eBPF map from already existing map by fd
// available to current process (i.e. created by it).
// In other words it will work only on maps created by current process.
func NewMapFromExistingMapByFd(fd int) (*EbpfMap, error) {
var logBuf [errCodeBufferSize]byte
var infoBuf [1024]byte
// Get map information
res := C.ebpf_obj_get_info_by_fd(C.__u32(fd),
unsafe.Pointer(&infoBuf[0]), C.__u32(len(infoBuf)),
unsafe.Pointer(&logBuf[0]), C.size_t(unsafe.Sizeof(logBuf)))
if res == -1 {
return nil, fmt.Errorf("ebpf_obj_get_info_by_fd() failed: %v",
NullTerminatedStringToString(logBuf[:]))
}
// Read definition
var rawInfo struct {
Type uint32
Id uint32
KeySize uint32
ValueSize uint32
MaxEntries uint32
Flags uint32
Name [C.BPF_OBJ_NAME_LEN]byte
}
reader := bytes.NewReader(infoBuf[:])
if err := binary.Read(reader, binary.LittleEndian, &rawInfo); err != nil {
return nil, err
}
return &EbpfMap{
fd: fd,
Name: NullTerminatedStringToString(rawInfo.Name[:]),
Type: MapType(rawInfo.Type),
KeySize: int(rawInfo.KeySize),
ValueSize: int(rawInfo.ValueSize),
MaxEntries: int(rawInfo.MaxEntries),
Flags: int(rawInfo.Flags),
}, nil
}
// NewMapFromExistingMapById creates eBPF map from BPF object ID.
// BPF object ID is a kernel mechanism to let non owner process to use BPF objects.
// Common use case - tooling for troubleshoot / inspect existing BPF objects in the kernel.
func NewMapFromExistingMapById(id int) (*EbpfMap, error) {
var logBuf [errCodeBufferSize]byte
// Resolve object FD from ID
fd := C.ebpf_map_get_fd_by_id(C.__u32(id),
unsafe.Pointer(&logBuf[0]), C.size_t(unsafe.Sizeof(logBuf)))
if fd == -1 {
return nil, fmt.Errorf("ebpf_map_get_fd_by_id() failed: %v",
NullTerminatedStringToString(logBuf[:]))
}
return NewMapFromExistingMapByFd(int(fd))
}
// If map type is Per-CPU based
func (m *EbpfMap) isPerCpu() bool {
return m.Type == MapTypePerCPUArray ||
m.Type == MapTypePerCPUHash ||
m.Type == MapTypeLRUPerCPUHash ||
m.Type == MapTypePerCpuCGroupStorage
}
// Map elements part: lookup, update / delete / etc
// Create creates map in kernel
func (m *EbpfMap) Create() error {
var logBuf [errCodeBufferSize]byte
// These special map types always have 4 byte value
if m.Type == MapTypeArrayOfMaps || m.Type == MapTypeHashOfMaps ||
m.Type == MapTypeProgArray {
m.ValueSize = 4
}
// Array's key must always be 4 bytes
if m.Type == MapTypeArray || m.Type == MapTypePerCPUArray ||
m.Type == MapTypeArrayOfMaps || m.Type == MapTypeProgArray {
if m.KeySize > 4 {
return fmt.Errorf("Invalid map '%s' key size(%d), must be 4 bytes", m.Name, m.KeySize)
}
// Allow to omit key size
if m.KeySize == 0 {
m.KeySize = 4
}
}
// LPM-Trie maps require BPF_F_NO_PREALLOC flag
if m.Type == MapTypeLPMTrie {
m.Flags |= bpfNoPrealloc
}
// Perform few sanity checks
if len(m.Name) >= C.BPF_OBJ_NAME_LEN {
return fmt.Errorf("Map name '%s' is too long", m.Name)
}
if m.KeySize < 1 {
return fmt.Errorf("Invalid map '%s' key size(%d)", m.Name, m.KeySize)
}
if m.ValueSize < 1 {
return fmt.Errorf("Invalid map '%s' value size(%d)", m.Name, m.ValueSize)
}
// Per-CPU maps require extra space to store values from ALL possible CPUs
if m.isPerCpu() {
numCpus, err := GetNumOfPossibleCpus()
if err != nil {
return err
}
m.valueRealSize = m.ValueSize * numCpus
} else {
m.valueRealSize = m.ValueSize
}
// Don't re-create map if it has fd assigned (NewMapFromExisting use case)
if m.fd != 0 {
return nil
}
// Make C string from map name
name := C.CString(m.Name)
defer C.free(unsafe.Pointer(name))
// Map can be defined as either process only or system wide ("object pinning")
// If PersistentPath is set - it indicates that eBPF program wants to
// make this map system wide accessible via PersistentPath (it is just filename)
persistentPathStr := C.CString(m.PersistentPath)
defer C.free(unsafe.Pointer(persistentPathStr))
if m.PersistentPath != "" {
// Try to locate map in the system on
// given path (i.e. map has been already created before)
objFd := int(C.ebpf_obj_get(
persistentPathStr,
unsafe.Pointer(&logBuf[0]),
C.size_t(unsafe.Sizeof(logBuf)),
))
if objFd != -1 {
// Successful, retrieved map fd from given location
m.fd = objFd
return nil
}
// No map at given location present yet, create it!
}
newFd := int(C.ebpf_map_create(
name,
C.__u32(m.Type),
C.__u32(m.KeySize),
C.__u32(m.ValueSize),
C.__u32(m.MaxEntries),
C.__u32(m.Flags),
C.__u32(m.InnerMapFd),
unsafe.Pointer(&logBuf[0]),
C.size_t(unsafe.Sizeof(logBuf)),
))
if newFd == -1 {
return fmt.Errorf("ebpf_create_map() failed: %s",
NullTerminatedStringToString(logBuf[:]))
}
m.fd = newFd
// If eBPF program decides to make this map system wide - pin it to given location
if m.PersistentPath != "" {
err := ebpfObjPin(m.fd, m.PersistentPath)
if err != nil {
// Destroy just created map
cerr := m.Close()
if cerr != nil {
return fmt.Errorf("%v, also close() failed: %v", err, cerr)
}
return err
}
}
return nil
}
// Close destroy eBPF map (removes it from kernel)
func (m *EbpfMap) Close() error {
if m.fd == 0 {
return errors.New("Already closed / not created")
}
err := closeFd(m.fd)
if err != nil {
return err
}
m.fd = 0
return nil
}
// CloneTemplate creates new instance of eBPF map using current map parameters.
// Main use case is work with array/hash of maps:
//
// // Create new map based on template
// newItem := templateMap.CloneTemplate()
// newItem.Create()
// // Insert item into array of maps
// superMap.Insert(1, newItem)
func (m *EbpfMap) CloneTemplate() Map {
res := *m
res.fd = 0
return &res
}
// Lookup performs lookup and returns array of bytes
// WARNING: For Per-CPU array/hash map return value will contain
// data from all CPUs, i.e. length = valueSize * nCPU
func (m *EbpfMap) Lookup(ikey interface{}) ([]byte, error) {
// Convert key into bytes
key, err := KeyValueToBytes(ikey, int(m.KeySize))
if err != nil {
return nil, err
}
var val = make([]byte, m.valueRealSize)
var logBuf [errCodeBufferSize]byte
res := int(C.ebpf_map_lookup_elem(
C.__u32(m.fd),
unsafe.Pointer(&key[0]),
unsafe.Pointer(&val[0]),
unsafe.Pointer(&logBuf[0]),
C.size_t(unsafe.Sizeof(logBuf))))
if res == -1 {
return nil, fmt.Errorf("ebpf_map_lookup_elem() failed: %s",
NullTerminatedStringToString(logBuf[:]))
}
return val, nil
}
// LookupString perform lookup and returns GO string from NULL terminated C string
// WARNING: Does NOT work for Per-CPU maps (not an real use case?).
func (m *EbpfMap) LookupString(ikey interface{}) (string, error) {
if m.isPerCpu() {
return "", fmt.Errorf("LookupString is not supported for %v", m.Type)
}
val, err := m.Lookup(ikey)
if err != nil {
return "", err
}
return NullTerminatedStringToString(val), nil
}
// Helper to read flexible integer value from bytes
// with support for per-cpu maps (multiple values joined into one buffer)
func (m *EbpfMap) parseFlexibleMultiInteger(rawVal []byte) uint64 {
// Per-CPU map types have slightly different behavior:
// lookup returns all values in single call, e.g. assuming for uint32 and 2 CPUs:
// {0x01, 0x00, 0x00, 0x00, 0x0ff, 0x00, 0x00, 0x00}
// ^^^^ CPU0 value ^^^^ ^^^^ CPU1 value^^^^^
// = 0x01 + 0xff = 256
//
// First value (for all map types)
val := ParseFlexibleIntegerLittleEndian(rawVal[:m.ValueSize])
if m.isPerCpu() {
// Sum up values from all CPUs in case of Per-CPU maps,
// starting from second item
for i := m.ValueSize; i < m.valueRealSize; i += m.ValueSize {
val += ParseFlexibleIntegerLittleEndian(rawVal[i : i+m.ValueSize])
}
}
return val
}
// LookupInt performs lookup and returns integer
// WARNING: For Per-CPU array/hash returns sum of values from all CPUs
func (m *EbpfMap) LookupInt(ikey interface{}) (int, error) {
val, err := m.LookupUint64(ikey)
return int(val), err
}
// LookupUint64 performs lookup and returns uint64
// WARNING: For Per-CPU array/hash returns sum of values from all CPUs
func (m *EbpfMap) LookupUint64(ikey interface{}) (uint64, error) {
if m.ValueSize > 8 {
return 0, errors.New("Value is too large to fit int")
}
rawVal, err := m.Lookup(ikey)
if err != nil {
return 0, err
}
return m.parseFlexibleMultiInteger(rawVal), nil
}
// Actual implementation for Insert / Update methods
func (m *EbpfMap) updateImpl(ikey interface{}, ivalue interface{}, op int) error {
// ArrayOfMaps/ProgArray requires BPF_ANY in order to update item for some reason... :(
if m.Type == MapTypeArrayOfMaps || m.Type == MapTypeProgArray {
op = bpfAny
}
// Convert key/value into bytes
key, err := KeyValueToBytes(ikey, int(m.KeySize))
if err != nil {
return err
}
val, err := KeyValueToBytes(ivalue, int(m.ValueSize))
if err != nil {
return err
}
var logBuf [errCodeBufferSize]byte
res := int(C.ebpf_map_update_elem(
C.__u32(m.fd),
unsafe.Pointer(&key[0]),
unsafe.Pointer(&val[0]),
C.__u64(op),
unsafe.Pointer(&logBuf[0]),
C.size_t(unsafe.Sizeof(logBuf))))
if res == -1 {
return fmt.Errorf("ebpf_map_update_elem() failed: %s",
NullTerminatedStringToString(logBuf[:]))
}
return nil
}
// Insert inserts value into eBPF map at given ikey.
// Supported key/value types are: int, uint8, uint16, uint32, int32, uint64, string, []byte, net.IPNet
func (m *EbpfMap) Insert(ikey interface{}, ivalue interface{}) error {
return m.updateImpl(ikey, ivalue, bpfNoexist)
}
// Update updates (replaces) element at given ikey.
// Supported ivalue types are: int, uint8, uint16, uint32, int32, uint64, string, []byte, net.IPNet
//
// Element must be inserted before for non array types (map, hash)
func (m *EbpfMap) Update(ikey interface{}, ivalue interface{}) error {
return m.updateImpl(ikey, ivalue, bpfExist)
}
// Upsert updates (replaces) or inserts element at given ikey.
// Supported ivalue types are: int, uint8, uint16, uint32, int32, uint64, string, []byte, net.IPNet
//
func (m *EbpfMap) Upsert(ikey interface{}, ivalue interface{}) error {
return m.updateImpl(ikey, ivalue, bpfAny)
}
// Delete deletes element by given ikey.
// Array based types are not supported.
func (m *EbpfMap) Delete(ikey interface{}) error {
// Convert key into bytes
key, err := KeyValueToBytes(ikey, int(m.KeySize))
if err != nil {
return err
}
var logBuf [errCodeBufferSize]byte
res := int(C.ebpf_map_delete_elem(
C.__u32(m.fd),
unsafe.Pointer(&key[0]),
unsafe.Pointer(&logBuf[0]),
C.size_t(unsafe.Sizeof(logBuf))))
if res == -1 {
return fmt.Errorf("ebpf_map_delete_elem() failed: %s",
NullTerminatedStringToString(logBuf[:]))
}
return nil
}
// GetFd returns fd (file descriptor) of eBPF map
func (m *EbpfMap) GetFd() int {
return m.fd
}
// GetName returns map name
func (m *EbpfMap) GetName() string {
return m.Name
}