Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef BPF_BPFMAP_H |
| 18 | #define BPF_BPFMAP_H |
| 19 | |
| 20 | #include <linux/bpf.h> |
| 21 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 22 | #include <android-base/result.h> |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android-base/unique_fd.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include "bpf/BpfUtils.h" |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace bpf { |
| 30 | |
| 31 | // This is a class wrapper for eBPF maps. The eBPF map is a special in-kernel |
| 32 | // data structure that stores data in <Key, Value> pairs. It can be read/write |
| 33 | // from userspace by passing syscalls with the map file descriptor. This class |
| 34 | // is used to generalize the procedure of interacting with eBPF maps and hide |
| 35 | // the implementation detail from other process. Besides the basic syscalls |
| 36 | // wrapper, it also provides some useful helper functions as well as an iterator |
| 37 | // nested class to iterate the map more easily. |
| 38 | // |
| 39 | // NOTE: A kernel eBPF map may be accessed by both kernel and userspace |
| 40 | // processes at the same time. Or if the map is pinned as a virtual file, it can |
| 41 | // be obtained by multiple eBPF map class object and accessed concurrently. |
| 42 | // Though the map class object and the underlying kernel map are thread safe, it |
| 43 | // is not safe to iterate over a map while another thread or process is deleting |
| 44 | // from it. In this case the iteration can return duplicate entries. |
| 45 | template <class Key, class Value> |
| 46 | class BpfMap { |
| 47 | public: |
Maciej Żenczykowski | e6cbd40 | 2019-08-09 18:20:26 -0700 | [diff] [blame] | 48 | BpfMap<Key, Value>() {}; |
Chenbo Feng | 4c9e9ec | 2018-10-16 20:31:52 -0700 | [diff] [blame] | 49 | explicit BpfMap<Key, Value>(int fd) : mMapFd(fd){}; |
Maciej Żenczykowski | e6cbd40 | 2019-08-09 18:20:26 -0700 | [diff] [blame] | 50 | |
| 51 | // We could technically implement this constructor either with |
| 52 | // : mMapFd(dup(fd)) {} // fd valid in caller, we have our own local copy |
| 53 | // or |
| 54 | // : mMapFd(fd.release()) {} // fd no longer valid in caller, we 'stole' it |
| 55 | // |
| 56 | // However, I think we're much better off with a compile time failure, since |
| 57 | // it's better for whoever passes in a unique_fd to think twice about whether |
| 58 | // they're trying to pass in ownership or not. |
| 59 | explicit BpfMap<Key, Value>(base::unique_fd fd) = delete; |
| 60 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 61 | BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags) { |
| 62 | int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags); |
| 63 | if (map_fd < 0) { |
| 64 | mMapFd.reset(-1); |
| 65 | } else { |
| 66 | mMapFd.reset(map_fd); |
| 67 | } |
| 68 | } |
| 69 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 70 | base::Result<Key> getFirstKey() const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 71 | Key firstKey; |
| 72 | if (getFirstMapKey(mMapFd, &firstKey)) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 73 | return base::ErrnoErrorf("Get firstKey map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 74 | } |
| 75 | return firstKey; |
| 76 | } |
| 77 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 78 | base::Result<Key> getNextKey(const Key& key) const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 79 | Key nextKey; |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 80 | if (getNextMapKey(mMapFd, &key, &nextKey)) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 81 | return base::ErrnoErrorf("Get next key of map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 82 | } |
| 83 | return nextKey; |
| 84 | } |
| 85 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 86 | base::Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) { |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 87 | if (writeToMapEntry(mMapFd, &key, &value, flags)) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 88 | return base::ErrnoErrorf("Write to map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 89 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 90 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 93 | base::Result<Value> readValue(const Key key) const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 94 | Value value; |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 95 | if (findMapEntry(mMapFd, &key, &value)) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 96 | return base::ErrnoErrorf("Read value of map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 97 | } |
| 98 | return value; |
| 99 | } |
| 100 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 101 | base::Result<void> deleteValue(const Key& key) { |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 102 | if (deleteMapEntry(mMapFd, &key)) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 103 | return base::ErrnoErrorf("Delete entry from map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 104 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 105 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Maciej Żenczykowski | 52108bf | 2019-04-01 10:41:13 -0700 | [diff] [blame] | 108 | // Function that tries to get map from a pinned path. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 109 | base::Result<void> init(const char* path); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 110 | |
| 111 | // Iterate through the map and handle each key retrieved based on the filter |
| 112 | // without modification of map content. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 113 | base::Result<void> iterate( |
| 114 | const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>& |
| 115 | filter) const; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 116 | |
| 117 | // Iterate through the map and get each <key, value> pair, handle each <key, |
| 118 | // value> pair based on the filter without modification of map content. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 119 | base::Result<void> iterateWithValue( |
| 120 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 121 | const BpfMap<Key, Value>& map)>& filter) const; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 122 | |
| 123 | // Iterate through the map and handle each key retrieved based on the filter |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 124 | base::Result<void> iterate( |
| 125 | const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& |
| 126 | filter); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 127 | |
| 128 | // Iterate through the map and get each <key, value> pair, handle each <key, |
| 129 | // value> pair based on the filter. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 130 | base::Result<void> iterateWithValue( |
| 131 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 132 | BpfMap<Key, Value>& map)>& filter); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 133 | |
| 134 | const base::unique_fd& getMap() const { return mMapFd; }; |
| 135 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 136 | // Move constructor |
| 137 | void operator=(BpfMap<Key, Value>&& other) noexcept { |
| 138 | mMapFd = std::move(other.mMapFd); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 139 | other.reset(); |
| 140 | } |
| 141 | |
| 142 | void reset(int fd = -1) { |
| 143 | mMapFd.reset(fd); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool isValid() const { return mMapFd != -1; } |
| 147 | |
| 148 | // It is only safe to call this method if it is guaranteed that nothing will concurrently |
| 149 | // iterate over the map in any process. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 150 | base::Result<void> clear() { |
| 151 | const auto deleteAllEntries = [](const Key& key, |
| 152 | BpfMap<Key, Value>& map) -> base::Result<void> { |
| 153 | base::Result<void> res = map.deleteValue(key); |
| 154 | if (!res && res.error().code() != ENOENT) { |
| 155 | ALOGE("Failed to delete data %s", strerror(res.error().code())); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 156 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 157 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 158 | }; |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 159 | return iterate(deleteAllEntries); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 162 | base::Result<bool> isEmpty() const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 163 | auto key = this->getFirstKey(); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 164 | if (!key) { |
| 165 | // Return error code ENOENT means the map is empty |
| 166 | if (key.error().code() == ENOENT) return true; |
| 167 | return key.error(); |
| 168 | } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | base::unique_fd mMapFd; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 177 | base::Result<void> BpfMap<Key, Value>::init(const char* path) { |
Maciej Żenczykowski | 52108bf | 2019-04-01 10:41:13 -0700 | [diff] [blame] | 178 | mMapFd = base::unique_fd(mapRetrieve(path, 0)); |
| 179 | if (mMapFd == -1) { |
| 180 | reset(); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 181 | return base::ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 182 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 183 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 187 | base::Result<void> BpfMap<Key, Value>::iterate( |
| 188 | const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>& |
| 189 | filter) const { |
| 190 | base::Result<Key> curKey = getFirstKey(); |
| 191 | while (curKey) { |
| 192 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 193 | base::Result<void> status = filter(curKey.value(), *this); |
| 194 | if (!status) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 195 | curKey = nextKey; |
| 196 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 197 | if (curKey.error().code() == ENOENT) return {}; |
| 198 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 202 | base::Result<void> BpfMap<Key, Value>::iterateWithValue( |
| 203 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 204 | const BpfMap<Key, Value>& map)>& filter) const { |
| 205 | base::Result<Key> curKey = getFirstKey(); |
| 206 | while (curKey) { |
| 207 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 208 | base::Result<Value> curValue = this->readValue(curKey.value()); |
| 209 | if (!curValue) return curValue.error(); |
| 210 | base::Result<void> status = filter(curKey.value(), curValue.value(), *this); |
| 211 | if (!status) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 212 | curKey = nextKey; |
| 213 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 214 | if (curKey.error().code() == ENOENT) return {}; |
| 215 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 219 | base::Result<void> BpfMap<Key, Value>::iterate( |
| 220 | const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& filter) { |
| 221 | base::Result<Key> curKey = getFirstKey(); |
| 222 | while (curKey) { |
| 223 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 224 | base::Result<void> status = filter(curKey.value(), *this); |
| 225 | if (!status) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 226 | curKey = nextKey; |
| 227 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 228 | if (curKey.error().code() == ENOENT) return {}; |
| 229 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 233 | base::Result<void> BpfMap<Key, Value>::iterateWithValue( |
| 234 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 235 | BpfMap<Key, Value>& map)>& filter) { |
| 236 | base::Result<Key> curKey = getFirstKey(); |
| 237 | while (curKey) { |
| 238 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 239 | base::Result<Value> curValue = this->readValue(curKey.value()); |
| 240 | if (!curValue) return curValue.error(); |
| 241 | base::Result<void> status = filter(curKey.value(), curValue.value(), *this); |
| 242 | if (!status) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 243 | curKey = nextKey; |
| 244 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 245 | if (curKey.error().code() == ENOENT) return {}; |
| 246 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | } // namespace bpf |
| 250 | } // namespace android |
| 251 | |
| 252 | #endif |