Skip to content

Commit

Permalink
Add helpers for passing flags
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Apr 21, 2022
1 parent 64458ba commit ec44380
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion libbpfgo.go
Expand Up @@ -172,6 +172,15 @@ const (
MapTypeBloomFilter
)

type MapFlag uint32

const (
MapFlagUpdateAny MapFlag = iota // create new element or update existing
MapFlagUpdateNoExist // create new element if it didn't exist
MapFlagUpdateExist // update existing element
MapFlagFLock // spin_lock-ed map_lookup/map_update
)

func (m MapType) String() string {
x := map[MapType]string{
MapTypeUnspec: "BPF_MAP_TYPE_UNSPEC",
Expand Down Expand Up @@ -630,6 +639,17 @@ func (b *BPFMap) GetValue(key unsafe.Pointer) ([]byte, error) {
return value, nil
}

func (b *BPFMap) GetValueFlags(key unsafe.Pointer, flags MapFlag) ([]byte, error) {
value := make([]byte, b.ValueSize())
valuePtr := unsafe.Pointer(&value[0])

errC := C.bpf_map_lookup_elem_flags(b.fd, key, valuePtr, C.ulonglong(flags))
if errC != 0 {
return nil, fmt.Errorf("failed to lookup value %v in map %s: %w", key, b.name, syscall.Errno(-errC))
}
return value, nil
}

// BPFMapBatchOpts mirrors the C structure bpf_map_batch_opts.
type BPFMapBatchOpts struct {
Sz uint64
Expand Down Expand Up @@ -787,7 +807,7 @@ func (b *BPFMap) DeleteKeyBatch(keys unsafe.Pointer, count uint32) error {
ElemFlags: C.BPF_ANY,
Flags: C.BPF_ANY,
}

errC := C.bpf_map_delete_batch(b.fd, keys, &countC, bpfMapBatchOptsToC(opts))
if errC != 0 {
sc := syscall.Errno(-errC)
Expand Down Expand Up @@ -841,6 +861,14 @@ func (b *BPFMap) Update(key, value unsafe.Pointer) error {
return nil
}

func (b *BPFMap) UpdateFlags(key, value unsafe.Pointer, flags MapFlag) error {
errC := C.bpf_map_update_elem(b.fd, key, value, C.ulonglong(flags))
if errC != 0 {
return fmt.Errorf("failed to update map %s: %w", b.name, syscall.Errno(-errC))
}
return nil
}

type BPFMapIterator struct {
b *BPFMap
err error
Expand Down

0 comments on commit ec44380

Please sign in to comment.