Skip to content

Commit

Permalink
Add helpers for passing flags (#154)
Browse files Browse the repository at this point in the history
* Add helpers for passing flags

Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Jun 24, 2022
1 parent beff12a commit 0046dd6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions libbpfgo.go
Expand Up @@ -182,6 +182,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 @@ -679,6 +688,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 @@ -902,9 +922,13 @@ func (b *BPFMap) DeleteKey(key unsafe.Pointer) error {
// bpfmap.Update(keyPtr, valuePtr)
//
func (b *BPFMap) Update(key, value unsafe.Pointer) error {
ret, errC := C.bpf_map_update_elem(b.fd, key, value, C.BPF_ANY)
if ret != 0 {
return fmt.Errorf("failed to update map %s: %w", b.name, errC)
return b.UpdateValueFlags(key, value, MapFlagUpdateAny)
}

func (b *BPFMap) UpdateValueFlags(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
}
Expand Down

0 comments on commit 0046dd6

Please sign in to comment.