From 38304c455042ea49c34ead657bb94a1561e73f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=BBenczykowski?= Date: Thu, 28 Aug 2025 11:11:03 -0700 Subject: [PATCH] BpfMap: add ERROR_FROM_ERRNO macro helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test: TreeHugger Flag: EXEMPT mainline, refactor, logging Signed-off-by: Maciej Żenczykowski Change-Id: I496a03a2b590f0c171cb96cb4c64a430929ec3c5 --- bpf/headers/include/bpf/BpfMap.h | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/bpf/headers/include/bpf/BpfMap.h b/bpf/headers/include/bpf/BpfMap.h index 841e5a3e43..ea0def4158 100644 --- a/bpf/headers/include/bpf/BpfMap.h +++ b/bpf/headers/include/bpf/BpfMap.h @@ -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 pairs. It can be read/write @@ -113,25 +114,19 @@ class BpfMapRO { Result 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 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 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 { } Result 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 { 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 { using BpfMapRW::readValue; Result 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 { 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