Skip to content

Commit

Permalink
batch: Fix GetValueBatch
Browse files Browse the repository at this point in the history
when dealing with partial deletion, more context on aquasecurity#162

Signed-off-by: Francisco Javier Honduvilla Coto <javierhonduco@gmail.com>
  • Loading branch information
javierhonduco committed Aug 3, 2022
1 parent 60eea00 commit bc33d6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 8 additions & 11 deletions libbpfgo.go
Expand Up @@ -760,19 +760,16 @@ func (b *BPFMap) GetValueBatch(keys unsafe.Pointer, startKey, nextKey unsafe.Poi
Flags: C.BPF_ANY,
}

errC := C.bpf_map_lookup_batch(b.fd, startKey, nextKey, keys, valuesPtr, &countC, bpfMapBatchOptsToC(opts))
if errC != 0 {
sc := syscall.Errno(-errC)
if sc != syscall.EFAULT {
if uint32(countC) != count {
return collectBatchValues(values, uint32(countC), b.ValueSize()),
fmt.Errorf("failed to retrieve ALL elements in map %s, fetched (%d/%d): %w", b.name, uint32(countC), count, sc)
}
}
return nil, fmt.Errorf("failed to batch lookup values %v in map %s: %w", keys, b.name, syscall.Errno(-errC))
ret, errC := C.bpf_map_lookup_batch(b.fd, startKey, nextKey, keys, valuesPtr, &countC, bpfMapBatchOptsToC(opts))
processed := uint32(countC)

if ret != 0 && errC != syscall.ENOENT {
// ret = -1 && errno == syscall.ENOENT indicates a partial read.
return nil, fmt.Errorf("failed to batch get value %v in map %s: ret %d (err: %s)", keys, b.name, ret, errC)
}

return collectBatchValues(values, count, b.ValueSize()), nil
// Either some or all entries were read.
return collectBatchValues(values, processed, b.ValueSize()), nil
}

// GetValueAndDeleteBatch allows for batch lookup and deletion of elements where each element is deleted after being retrieved from the map.
Expand Down
7 changes: 7 additions & 0 deletions selftest/map-batch/main.go
Expand Up @@ -73,6 +73,13 @@ func main() {
}
}

// Test batch get value with more elements than we have.
_, err = testerMap.GetValueBatch(unsafe.Pointer(&batchKeys[0]), prevKey, unsafe.Pointer(&nextKey), uint32(len(batchKeys))+100)
if err != nil {
fmt.Fprintf(os.Stderr, "testerMap.GetValueBatch failed: %v", err)
os.Exit(-1)
}

// Test batch lookup and delete.
deleteKeys := make([]uint32, 2)
nextKey = uint32(0)
Expand Down

0 comments on commit bc33d6d

Please sign in to comment.