diff options
| author | Maciej Żenczykowski <maze@google.com> | 2022-06-13 17:38:12 -0700 |
|---|---|---|
| committer | Maciej Żenczykowski <maze@google.com> | 2022-06-21 03:19:55 +0000 |
| commit | 5d99d148f170cae5aa442c19cb0b6b5f6a7a955a (patch) | |
| tree | 20e11893ee4c1038033d319efa3f98e44fee93b0 | |
| parent | 6025f4716cb9457f55f1d68fa9837771043984d6 (diff) | |
BpfMap - add BpfMapRO.init() support
Really we need to fix the inheritance to make
BpfMapRO the parent class of BpfMap:
but that's a far more difficult thing to do,
so in the short term we punt like this.
This makes BpfMapRO a little bit more usable,
and allows a slow transition across the codebase...
Bug: 218408035
Test: TreeHugger
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: I1c5112db70e9e523c113cba536fbe19422b4d3f3
(cherry picked from commit b54659706758641a0ff01770644cb189e3959b29)
Merged-In: I1c5112db70e9e523c113cba536fbe19422b4d3f3
| -rw-r--r-- | common/native/bpf_headers/include/bpf/BpfMap.h | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/common/native/bpf_headers/include/bpf/BpfMap.h b/common/native/bpf_headers/include/bpf/BpfMap.h index a7720ea..2bee2ee 100644 --- a/common/native/bpf_headers/include/bpf/BpfMap.h +++ b/common/native/bpf_headers/include/bpf/BpfMap.h @@ -103,8 +103,29 @@ class BpfMap { return {}; } + protected: + [[clang::reinitializes]] base::Result<void> init(const char* path, int fd) { + mMapFd.reset(fd); + if (mMapFd == -1) { + return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); + } + if (isAtLeastKernelVersion(4, 14, 0)) { + // Normally we should return an error here instead of calling abort, + // but this cannot happen at runtime without a massive code bug (K/V type mismatch) + // and as such it's better to just blow the system up and let the developer fix it. + // Crashes are much more likely to be noticed than logs and missing functionality. + if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort(); + if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) abort(); + } + return {}; + } + + public: // Function that tries to get map from a pinned path. - [[clang::reinitializes]] base::Result<void> init(const char* path); + [[clang::reinitializes]] base::Result<void> init(const char* path) { + return init(path, mapRetrieveRW(path)); + } + #ifdef TEST_BPF_MAP // due to Android SELinux limitations which prevent map creation by anyone besides the bpfloader @@ -210,23 +231,6 @@ class BpfMap { }; template <class Key, class Value> -base::Result<void> BpfMap<Key, Value>::init(const char* path) { - mMapFd.reset(mapRetrieveRW(path)); - if (mMapFd == -1) { - return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); - } - if (isAtLeastKernelVersion(4, 14, 0)) { - // Normally we should return an error here instead of calling abort, - // but this cannot happen at runtime without a massive code bug (K/V type mismatch) - // and as such it's better to just blow the system up and let the developer fix it. - // Crashes are much more likely to be noticed than logs and missing functionality. - if (bpfGetFdKeySize(mMapFd) != sizeof(Key)) abort(); - if (bpfGetFdValueSize(mMapFd) != sizeof(Value)) abort(); - } - return {}; -} - -template <class Key, class Value> base::Result<void> BpfMap<Key, Value>::iterate( const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>& filter) const { @@ -292,8 +296,15 @@ base::Result<void> BpfMap<Key, Value>::iterateWithValue( template <class Key, class Value> class BpfMapRO : public BpfMap<Key, Value> { public: + BpfMapRO<Key, Value>() {}; + explicit BpfMapRO<Key, Value>(const char* pathname) : BpfMap<Key, Value>(pathname, BPF_F_RDONLY) {} + + // Function that tries to get map from a pinned path. + [[clang::reinitializes]] base::Result<void> init(const char* path) { + return BpfMap<Key,Value>::init(path, mapRetrieveRO(path)); + } }; } // namespace bpf |
