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>() {}; |
Maciej Żenczykowski | e6cbd40 | 2019-08-09 18:20:26 -0700 | [diff] [blame] | 49 | |
Maciej Żenczykowski | dfd941f | 2020-01-20 03:18:26 -0800 | [diff] [blame] | 50 | protected: |
| 51 | // flag must be within BPF_OBJ_FLAG_MASK, ie. 0, BPF_F_RDONLY, BPF_F_WRONLY |
| 52 | BpfMap<Key, Value>(const char* pathname, uint32_t flags) { |
| 53 | int map_fd = mapRetrieve(pathname, flags); |
| 54 | if (map_fd >= 0) mMapFd.reset(map_fd); |
| 55 | } |
| 56 | |
| 57 | public: |
| 58 | explicit BpfMap<Key, Value>(const char* pathname) : BpfMap<Key, Value>(pathname, 0) {} |
Maciej Żenczykowski | e6cbd40 | 2019-08-09 18:20:26 -0700 | [diff] [blame] | 59 | |
Hungming Chen | 6161ff2 | 2020-03-05 14:47:53 +0800 | [diff] [blame^] | 60 | BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags = 0) { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 61 | int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags); |
Maciej Żenczykowski | dfd941f | 2020-01-20 03:18:26 -0800 | [diff] [blame] | 62 | if (map_fd >= 0) mMapFd.reset(map_fd); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 65 | base::Result<Key> getFirstKey() const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 66 | Key firstKey; |
| 67 | if (getFirstMapKey(mMapFd, &firstKey)) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 68 | return ErrnoErrorf("Get firstKey map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 69 | } |
| 70 | return firstKey; |
| 71 | } |
| 72 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 73 | base::Result<Key> getNextKey(const Key& key) const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 74 | Key nextKey; |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 75 | if (getNextMapKey(mMapFd, &key, &nextKey)) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 76 | return ErrnoErrorf("Get next key of map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 77 | } |
| 78 | return nextKey; |
| 79 | } |
| 80 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 81 | 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] | 82 | if (writeToMapEntry(mMapFd, &key, &value, flags)) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 83 | return ErrnoErrorf("Write to map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 84 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 85 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 88 | base::Result<Value> readValue(const Key key) const { |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 89 | Value value; |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 90 | if (findMapEntry(mMapFd, &key, &value)) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 91 | return ErrnoErrorf("Read value of map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 92 | } |
| 93 | return value; |
| 94 | } |
| 95 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 96 | base::Result<void> deleteValue(const Key& key) { |
Maciej Żenczykowski | 79365da | 2020-01-17 19:07:37 -0800 | [diff] [blame] | 97 | if (deleteMapEntry(mMapFd, &key)) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 98 | return ErrnoErrorf("Delete entry from map {} failed", mMapFd.get()); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 99 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 100 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Maciej Żenczykowski | 52108bf | 2019-04-01 10:41:13 -0700 | [diff] [blame] | 103 | // Function that tries to get map from a pinned path. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 104 | base::Result<void> init(const char* path); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 105 | |
| 106 | // Iterate through the map and handle each key retrieved based on the filter |
| 107 | // without modification of map content. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 108 | base::Result<void> iterate( |
| 109 | const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>& |
| 110 | filter) const; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 111 | |
| 112 | // Iterate through the map and get each <key, value> pair, handle each <key, |
| 113 | // value> pair based on the filter without modification of map content. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 114 | base::Result<void> iterateWithValue( |
| 115 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 116 | const BpfMap<Key, Value>& map)>& filter) const; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 117 | |
| 118 | // 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] | 119 | base::Result<void> iterate( |
| 120 | const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& |
| 121 | filter); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 122 | |
| 123 | // Iterate through the map and get each <key, value> pair, handle each <key, |
| 124 | // value> pair based on the filter. |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 125 | base::Result<void> iterateWithValue( |
| 126 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 127 | BpfMap<Key, Value>& map)>& filter); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 128 | |
| 129 | const base::unique_fd& getMap() const { return mMapFd; }; |
| 130 | |
Hungming Chen | 4008da9 | 2020-02-27 17:26:40 +0800 | [diff] [blame] | 131 | // Copy assignment operator |
| 132 | void operator=(const BpfMap<Key, Value>& other) { |
| 133 | mMapFd.reset(fcntl(other.mMapFd.get(), F_DUPFD_CLOEXEC, 0)); |
| 134 | } |
| 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); |
Maciej Żenczykowski | 4f65739 | 2020-01-20 15:06:22 -0800 | [diff] [blame] | 139 | other.reset(-1); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Maciej Żenczykowski | 4f65739 | 2020-01-20 15:06:22 -0800 | [diff] [blame] | 142 | void reset(base::unique_fd fd) = delete; |
| 143 | |
| 144 | void reset(int fd) { mMapFd.reset(fd); } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 145 | |
| 146 | bool isValid() const { return mMapFd != -1; } |
| 147 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 148 | base::Result<void> clear() { |
Maciej Żenczykowski | b7176fb | 2020-02-19 10:59:09 -0800 | [diff] [blame] | 149 | while (true) { |
| 150 | auto key = getFirstKey(); |
| 151 | if (!key.ok()) { |
| 152 | if (key.error().code() == ENOENT) return {}; // empty: success |
| 153 | return key.error(); // Anything else is an error |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 154 | } |
Maciej Żenczykowski | b7176fb | 2020-02-19 10:59:09 -0800 | [diff] [blame] | 155 | auto res = deleteValue(key.value()); |
| 156 | if (!res.ok()) { |
| 157 | // Someone else could have deleted the key, so ignore ENOENT |
| 158 | if (res.error().code() == ENOENT) continue; |
| 159 | ALOGE("Failed to delete data %s", strerror(res.error().code())); |
| 160 | return res.error(); |
| 161 | } |
| 162 | } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 165 | base::Result<bool> isEmpty() const { |
Maciej Żenczykowski | b7176fb | 2020-02-19 10:59:09 -0800 | [diff] [blame] | 166 | auto key = getFirstKey(); |
Bernie Innocenti | 43d2538 | 2020-02-10 07:25:16 +0900 | [diff] [blame] | 167 | if (!key.ok()) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 168 | // Return error code ENOENT means the map is empty |
| 169 | if (key.error().code() == ENOENT) return true; |
| 170 | return key.error(); |
| 171 | } |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | base::unique_fd mMapFd; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 180 | base::Result<void> BpfMap<Key, Value>::init(const char* path) { |
Maciej Żenczykowski | 52108bf | 2019-04-01 10:41:13 -0700 | [diff] [blame] | 181 | mMapFd = base::unique_fd(mapRetrieve(path, 0)); |
| 182 | if (mMapFd == -1) { |
Tom Cherry | a7146db | 2020-02-04 15:27:30 -0800 | [diff] [blame] | 183 | return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 184 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 185 | return {}; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 189 | base::Result<void> BpfMap<Key, Value>::iterate( |
| 190 | const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>& |
| 191 | filter) const { |
| 192 | base::Result<Key> curKey = getFirstKey(); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 193 | while (curKey.ok()) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 194 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 195 | base::Result<void> status = filter(curKey.value(), *this); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 196 | if (!status.ok()) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 197 | curKey = nextKey; |
| 198 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 199 | if (curKey.error().code() == ENOENT) return {}; |
| 200 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 204 | base::Result<void> BpfMap<Key, Value>::iterateWithValue( |
| 205 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 206 | const BpfMap<Key, Value>& map)>& filter) const { |
| 207 | base::Result<Key> curKey = getFirstKey(); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 208 | while (curKey.ok()) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 209 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
Maciej Żenczykowski | b7176fb | 2020-02-19 10:59:09 -0800 | [diff] [blame] | 210 | base::Result<Value> curValue = readValue(curKey.value()); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 211 | if (!curValue.ok()) return curValue.error(); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 212 | base::Result<void> status = filter(curKey.value(), curValue.value(), *this); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 213 | if (!status.ok()) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 214 | curKey = nextKey; |
| 215 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 216 | if (curKey.error().code() == ENOENT) return {}; |
| 217 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 221 | base::Result<void> BpfMap<Key, Value>::iterate( |
| 222 | const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& filter) { |
| 223 | base::Result<Key> curKey = getFirstKey(); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 224 | while (curKey.ok()) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 225 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
| 226 | base::Result<void> status = filter(curKey.value(), *this); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 227 | if (!status.ok()) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 228 | curKey = nextKey; |
| 229 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 230 | if (curKey.error().code() == ENOENT) return {}; |
| 231 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | template <class Key, class Value> |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 235 | base::Result<void> BpfMap<Key, Value>::iterateWithValue( |
| 236 | const std::function<base::Result<void>(const Key& key, const Value& value, |
| 237 | BpfMap<Key, Value>& map)>& filter) { |
| 238 | base::Result<Key> curKey = getFirstKey(); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 239 | while (curKey.ok()) { |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 240 | const base::Result<Key>& nextKey = getNextKey(curKey.value()); |
Maciej Żenczykowski | b7176fb | 2020-02-19 10:59:09 -0800 | [diff] [blame] | 241 | base::Result<Value> curValue = readValue(curKey.value()); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 242 | if (!curValue.ok()) return curValue.error(); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 243 | base::Result<void> status = filter(curKey.value(), curValue.value(), *this); |
Bernie Innocenti | 5953dee | 2020-02-06 04:22:41 +0900 | [diff] [blame] | 244 | if (!status.ok()) return status; |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 245 | curKey = nextKey; |
| 246 | } |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 247 | if (curKey.error().code() == ENOENT) return {}; |
| 248 | return curKey.error(); |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Maciej Żenczykowski | dfd941f | 2020-01-20 03:18:26 -0800 | [diff] [blame] | 251 | template <class Key, class Value> |
| 252 | class BpfMapRO : public BpfMap<Key, Value> { |
| 253 | public: |
| 254 | explicit BpfMapRO<Key, Value>(const char* pathname) |
| 255 | : BpfMap<Key, Value>(pathname, BPF_F_RDONLY) {} |
| 256 | }; |
| 257 | |
Chenbo Feng | 75b410b | 2018-10-10 15:01:19 -0700 | [diff] [blame] | 258 | } // namespace bpf |
| 259 | } // namespace android |
| 260 | |
| 261 | #endif |