2020-01-14 15:32:06 +01:00
|
|
|
package ebpf
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
|
|
|
|
// See the bpf man page for details on the following functions
|
|
|
|
|
|
|
|
static int bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size)
|
|
|
|
{
|
|
|
|
return syscall(__NR_bpf, cmd, attr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bpf_create_map(
|
|
|
|
enum bpf_map_type map_type, unsigned int key_size, unsigned int value_size,
|
|
|
|
unsigned int max_entries)
|
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_type = map_type,
|
2020-01-15 19:04:56 +01:00
|
|
|
.key_size = key_size,
|
|
|
|
.value_size = value_size,
|
2020-01-14 15:32:06 +01:00
|
|
|
.max_entries = max_entries};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf_get_next_key(int fd, const void *key, void *next_key)
|
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_fd = fd,
|
2020-01-15 19:04:56 +01:00
|
|
|
.key = (__u64) (unsigned long) key,
|
|
|
|
.next_key = (__u64) (unsigned long) next_key};
|
2020-01-14 15:32:06 +01:00
|
|
|
|
|
|
|
return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bpf_lookup_elem(int fd, const void *key, void *value)
|
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
2020-01-15 19:04:56 +01:00
|
|
|
.map_fd = fd,
|
|
|
|
.key = (__u64) (unsigned long) key,
|
2020-01-14 15:32:06 +01:00
|
|
|
.value = (__u64) (unsigned long) value,
|
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
|
|
|
|
}
|
|
|
|
|
2020-01-15 19:04:56 +01:00
|
|
|
int bpf_update_elem(int fd, const void *key, const void *value, uint64_t flags)
|
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_fd = fd,
|
|
|
|
.key = (__u64) (unsigned long) key,
|
|
|
|
.value = (__u64) (unsigned long) value,
|
|
|
|
.flags = flags,
|
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
|
|
|
|
}
|
2020-01-15 12:48:36 +01:00
|
|
|
|
2020-01-14 15:32:06 +01:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2020-01-16 15:36:06 +01:00
|
|
|
// MapFD is a file descriptor representing a eBPF map
|
2020-01-15 19:04:56 +01:00
|
|
|
type MapFD int
|
|
|
|
|
2020-01-14 15:32:06 +01:00
|
|
|
// CreateMap creates an eBPF map from int->uint64. The file descriptor of the
|
|
|
|
// created map is returned.
|
2020-01-15 19:04:56 +01:00
|
|
|
func CreateMap() (MapFD, error) {
|
|
|
|
|
|
|
|
r, errno := C.bpf_create_map(C.BPF_MAP_TYPE_HASH, 4, 8, 64)
|
|
|
|
|
|
|
|
if r == -1 {
|
|
|
|
return MapFD(r), fmt.Errorf("bpf_create_map with errno %d", errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
return MapFD(r), nil
|
2020-01-14 15:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMap gets the key/value pairs from the specified eBPF map as a Go map
|
2020-01-15 19:04:56 +01:00
|
|
|
func (mfd MapFD) GetMap() (map[int]uint64, error) {
|
2020-01-14 15:32:06 +01:00
|
|
|
retMap := make(map[int]uint64)
|
|
|
|
|
2020-01-15 19:04:56 +01:00
|
|
|
mapFDC := C.int(mfd)
|
2020-01-14 15:32:06 +01:00
|
|
|
keyC := C.int(0)
|
|
|
|
nextKeyC := C.int(0)
|
|
|
|
valC := C.uint64_t(0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
r, errno := C.bpf_get_next_key(mapFDC, unsafe.Pointer(&keyC), unsafe.Pointer(&nextKeyC))
|
|
|
|
if r == -1 {
|
|
|
|
if errno == syscall.ENOENT {
|
|
|
|
// The provided key was the last element. We're done iterating.
|
|
|
|
return retMap, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("bpf_get_next_key failed with errno %d", errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = C.bpf_lookup_elem(mapFDC, unsafe.Pointer(&nextKeyC), unsafe.Pointer(&valC))
|
|
|
|
if r == -1 {
|
|
|
|
return nil, fmt.Errorf("bpf_lookup_elem failed")
|
|
|
|
}
|
|
|
|
retMap[int(nextKeyC)] = uint64(valC)
|
|
|
|
|
|
|
|
keyC = nextKeyC
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 19:04:56 +01:00
|
|
|
|
2020-01-16 15:36:06 +01:00
|
|
|
// Add puts the (key, value) into the eBPF map, only if the key does not exist
|
|
|
|
// yet in the map. It returns an error otherwise.
|
2020-01-15 19:04:56 +01:00
|
|
|
func (mfd MapFD) Add(key int, value uint64) error {
|
|
|
|
return mfd.updateElement(key, value, C.BPF_NOEXIST)
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:36:06 +01:00
|
|
|
// Change changes the value to an existing key in the eBPF map. It returns an
|
|
|
|
// error otherwise.
|
2020-01-15 19:04:56 +01:00
|
|
|
func (mfd MapFD) Change(key int, value uint64) error {
|
|
|
|
return mfd.updateElement(key, value, C.BPF_EXIST)
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:36:06 +01:00
|
|
|
// Set puts the (key, value) into the eBPF map. It will create or overwrite an
|
|
|
|
// existing entry for that key.
|
2020-01-15 19:04:56 +01:00
|
|
|
func (mfd MapFD) Set(key int, value uint64) error {
|
|
|
|
return mfd.updateElement(key, value, C.BPF_ANY)
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:36:06 +01:00
|
|
|
// updateElement is the low level wrapper to bpf_update_elem, used from Add(),
|
|
|
|
// Set() and Change().
|
2020-01-15 19:04:56 +01:00
|
|
|
func (mfd MapFD) updateElement(key int, value uint64, flag C.uint64_t) error {
|
|
|
|
|
|
|
|
r, errno := C.bpf_update_elem(C.int(mfd),
|
|
|
|
unsafe.Pointer(&key),
|
|
|
|
unsafe.Pointer(&value),
|
|
|
|
flag)
|
|
|
|
|
|
|
|
if r == -1 {
|
|
|
|
return fmt.Errorf("bpf_update_elem failed with errno %d", errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|