Added documentation to ebpf.go

This commit is contained in:
Özgür Kesim 2020-01-16 15:36:06 +01:00
parent 3c7f976acc
commit 3672fd455b

View File

@ -69,6 +69,7 @@ import (
"unsafe" "unsafe"
) )
// MapFD is a file descriptor representing a eBPF map
type MapFD int type MapFD int
// CreateMap creates an eBPF map from int->uint64. The file descriptor of the // CreateMap creates an eBPF map from int->uint64. The file descriptor of the
@ -113,18 +114,26 @@ func (mfd MapFD) GetMap() (map[int]uint64, error) {
} }
} }
// 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.
func (mfd MapFD) Add(key int, value uint64) error { func (mfd MapFD) Add(key int, value uint64) error {
return mfd.updateElement(key, value, C.BPF_NOEXIST) return mfd.updateElement(key, value, C.BPF_NOEXIST)
} }
// Change changes the value to an existing key in the eBPF map. It returns an
// error otherwise.
func (mfd MapFD) Change(key int, value uint64) error { func (mfd MapFD) Change(key int, value uint64) error {
return mfd.updateElement(key, value, C.BPF_EXIST) return mfd.updateElement(key, value, C.BPF_EXIST)
} }
// Set puts the (key, value) into the eBPF map. It will create or overwrite an
// existing entry for that key.
func (mfd MapFD) Set(key int, value uint64) error { func (mfd MapFD) Set(key int, value uint64) error {
return mfd.updateElement(key, value, C.BPF_ANY) return mfd.updateElement(key, value, C.BPF_ANY)
} }
// updateElement is the low level wrapper to bpf_update_elem, used from Add(),
// Set() and Change().
func (mfd MapFD) updateElement(key int, value uint64, flag C.uint64_t) error { func (mfd MapFD) updateElement(key int, value uint64, flag C.uint64_t) error {
r, errno := C.bpf_update_elem(C.int(mfd), r, errno := C.bpf_update_elem(C.int(mfd),