BpfMap: add ERROR_FROM_ERRNO macro helper
Test: TreeHugger Flag: EXEMPT mainline, refactor, logging Signed-off-by: Maciej Żenczykowski <maze@google.com> Change-Id: I496a03a2b590f0c171cb96cb4c64a430929ec3c5
This commit is contained in:
parent
0c239209a7
commit
38304c4550
1 changed files with 9 additions and 18 deletions
|
|
@ -61,6 +61,7 @@ void Abort(int __unused error, const char* __unused fmt, ...) {
|
|||
abort();
|
||||
}
|
||||
|
||||
#define ERROR_FROM_ERRNO(f) ErrnoErrorf("BpfMap::" f "() failed")
|
||||
|
||||
// This is a class wrapper for eBPF maps. The eBPF map is a special in-kernel
|
||||
// data structure that stores data in <Key, Value> pairs. It can be read/write
|
||||
|
|
@ -113,25 +114,19 @@ class BpfMapRO {
|
|||
|
||||
Result<Key> getFirstKey() const {
|
||||
Key firstKey;
|
||||
if (getFirstMapKey(mMapFd, &firstKey)) {
|
||||
return ErrnoErrorf("BpfMap::getFirstKey() failed");
|
||||
}
|
||||
if (getFirstMapKey(mMapFd, &firstKey)) return ERROR_FROM_ERRNO("getFirstKey");
|
||||
return firstKey;
|
||||
}
|
||||
|
||||
Result<Key> getNextKey(const Key& key) const {
|
||||
Key nextKey;
|
||||
if (getNextMapKey(mMapFd, &key, &nextKey)) {
|
||||
return ErrnoErrorf("BpfMap::getNextKey() failed");
|
||||
}
|
||||
if (getNextMapKey(mMapFd, &key, &nextKey)) return ERROR_FROM_ERRNO("getNextKey");
|
||||
return nextKey;
|
||||
}
|
||||
|
||||
Result<Value> readValue(const Key& key) const {
|
||||
Value value;
|
||||
if (findMapEntry(mMapFd, &key, &value)) {
|
||||
return ErrnoErrorf("BpfMap::readValue() failed");
|
||||
}
|
||||
if (findMapEntry(mMapFd, &key, &value)) return ERROR_FROM_ERRNO("readValue");
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
@ -184,7 +179,7 @@ class BpfMapRO {
|
|||
uint32_t count = N; // how many to fetch (and possibly delete)
|
||||
int rv = batchLookupAndMaybeDelete(mMapFd, first ? NULL : &batch, &batch, &keys, &values, &count, del);
|
||||
if (rv && errno == ENOSPC) break; // not enough space for full HASH bucket, go around the *outer* loop
|
||||
if (rv && errno != ENOENT) return ErrnoErrorf("BpfMap::doBulkLookupAndMaybeDelete() failed");
|
||||
if (rv && errno != ENOENT) return ERROR_FROM_ERRNO("doBulkLookupAndMaybeDelete");
|
||||
// count is now how many *were* fetched (and possibly delete)
|
||||
for (unsigned i = 0; i < count; ++i) f(keys[i], values[i]);
|
||||
if (rv) return {}; // ENOENT -> success
|
||||
|
|
@ -329,9 +324,7 @@ class BpfMapRW : public BpfMapRO<Key, Value> {
|
|||
}
|
||||
|
||||
Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) {
|
||||
if (writeToMapEntry(mMapFd, &key, &value, flags)) {
|
||||
return ErrnoErrorf("BpfMap::writeValue() failed");
|
||||
}
|
||||
if (writeToMapEntry(mMapFd, &key, &value, flags)) return ERROR_FROM_ERRNO("writeValue");
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
@ -343,7 +336,7 @@ class BpfMapRW : public BpfMapRO<Key, Value> {
|
|||
if (map_flags & BPF_F_RDONLY) Abort(0, "map_flags is read-only");
|
||||
mMapFd.reset(createMap(map_type, sizeof(Key), sizeof(Value), max_entries,
|
||||
map_flags));
|
||||
if (!mMapFd.ok()) return ErrnoErrorf("BpfMap::resetMap() failed");
|
||||
if (!mMapFd.ok()) return ERROR_FROM_ERRNO("resetMap");
|
||||
abortOnMismatch(/* writable */ true);
|
||||
return {};
|
||||
}
|
||||
|
|
@ -363,9 +356,7 @@ class BpfMap : public BpfMapRW<Key, Value> {
|
|||
using BpfMapRW<Key, Value>::readValue;
|
||||
|
||||
Result<void> deleteValue(const Key& key) {
|
||||
if (deleteMapEntry(mMapFd, &key)) {
|
||||
return ErrnoErrorf("BpfMap::deleteValue() failed");
|
||||
}
|
||||
if (deleteMapEntry(mMapFd, &key)) return ERROR_FROM_ERRNO("deleteValue");
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
@ -373,7 +364,7 @@ class BpfMap : public BpfMapRW<Key, Value> {
|
|||
if (isAtLeastKernelVersion(5, 4, 0)) {
|
||||
Value value;
|
||||
if (!findAndDeleteMapEntry(mMapFd, &key, &value)) return value;
|
||||
if (errno == ENOENT) return ErrnoErrorf("BpfMap::readAndDeleteValue() failed");
|
||||
if (errno == ENOENT) return ERROR_FROM_ERRNO("readAndDeleteValue");
|
||||
};
|
||||
|
||||
// fallback path in case of weird error and for pre-5.4 kernels
|
||||
|
|
|
|||
Loading…
Reference in a new issue