blob: 31efd588445d8eb514792cba7cea018714c62702 [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
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 Morelande7cd2a72020-01-10 17:49:35 -080022#include <android-base/result.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070023#include <android-base/stringprintf.h>
24#include <android-base/unique_fd.h>
25#include <utils/Log.h>
26#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070027
28namespace android {
29namespace 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.
45template <class Key, class Value>
46class BpfMap {
47 public:
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070048 BpfMap<Key, Value>() {};
Chenbo Feng4c9e9ec2018-10-16 20:31:52 -070049 explicit BpfMap<Key, Value>(int fd) : mMapFd(fd){};
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070050
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 Feng75b410b2018-10-10 15:01:19 -070061 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 Morelande7cd2a72020-01-10 17:49:35 -080070 base::Result<Key> getFirstKey() const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070071 Key firstKey;
72 if (getFirstMapKey(mMapFd, &firstKey)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080073 return base::ErrnoErrorf("Get firstKey map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070074 }
75 return firstKey;
76 }
77
Steven Morelande7cd2a72020-01-10 17:49:35 -080078 base::Result<Key> getNextKey(const Key& key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070079 Key nextKey;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080080 if (getNextMapKey(mMapFd, &key, &nextKey)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080081 return base::ErrnoErrorf("Get next key of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070082 }
83 return nextKey;
84 }
85
Steven Morelande7cd2a72020-01-10 17:49:35 -080086 base::Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080087 if (writeToMapEntry(mMapFd, &key, &value, flags)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080088 return base::ErrnoErrorf("Write to map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070089 }
Steven Morelande7cd2a72020-01-10 17:49:35 -080090 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -070091 }
92
Steven Morelande7cd2a72020-01-10 17:49:35 -080093 base::Result<Value> readValue(const Key key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070094 Value value;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080095 if (findMapEntry(mMapFd, &key, &value)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080096 return base::ErrnoErrorf("Read value of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070097 }
98 return value;
99 }
100
Steven Morelande7cd2a72020-01-10 17:49:35 -0800101 base::Result<void> deleteValue(const Key& key) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -0800102 if (deleteMapEntry(mMapFd, &key)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800103 return base::ErrnoErrorf("Delete entry from map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -0700104 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800105 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700106 }
107
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700108 // Function that tries to get map from a pinned path.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800109 base::Result<void> init(const char* path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700110
111 // Iterate through the map and handle each key retrieved based on the filter
112 // without modification of map content.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800113 base::Result<void> iterate(
114 const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
115 filter) const;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700116
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 Morelande7cd2a72020-01-10 17:49:35 -0800119 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 Feng75b410b2018-10-10 15:01:19 -0700122
123 // Iterate through the map and handle each key retrieved based on the filter
Steven Morelande7cd2a72020-01-10 17:49:35 -0800124 base::Result<void> iterate(
125 const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>&
126 filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700127
128 // Iterate through the map and get each <key, value> pair, handle each <key,
129 // value> pair based on the filter.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800130 base::Result<void> iterateWithValue(
131 const std::function<base::Result<void>(const Key& key, const Value& value,
132 BpfMap<Key, Value>& map)>& filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700133
134 const base::unique_fd& getMap() const { return mMapFd; };
135
Chenbo Feng75b410b2018-10-10 15:01:19 -0700136 // Move constructor
137 void operator=(BpfMap<Key, Value>&& other) noexcept {
138 mMapFd = std::move(other.mMapFd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700139 other.reset();
140 }
141
142 void reset(int fd = -1) {
143 mMapFd.reset(fd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700144 }
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 Morelande7cd2a72020-01-10 17:49:35 -0800150 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 Feng75b410b2018-10-10 15:01:19 -0700156 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800157 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700158 };
Steven Morelande7cd2a72020-01-10 17:49:35 -0800159 return iterate(deleteAllEntries);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700160 }
161
Steven Morelande7cd2a72020-01-10 17:49:35 -0800162 base::Result<bool> isEmpty() const {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700163 auto key = this->getFirstKey();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800164 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 Feng75b410b2018-10-10 15:01:19 -0700169 return false;
170 }
171
172 private:
173 base::unique_fd mMapFd;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700174};
175
176template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800177base::Result<void> BpfMap<Key, Value>::init(const char* path) {
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700178 mMapFd = base::unique_fd(mapRetrieve(path, 0));
179 if (mMapFd == -1) {
180 reset();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800181 return base::ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700182 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800183 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700184}
185
186template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800187base::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 Feng75b410b2018-10-10 15:01:19 -0700195 curKey = nextKey;
196 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800197 if (curKey.error().code() == ENOENT) return {};
198 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700199}
200
201template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800202base::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 Feng75b410b2018-10-10 15:01:19 -0700212 curKey = nextKey;
213 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800214 if (curKey.error().code() == ENOENT) return {};
215 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700216}
217
218template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800219base::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 Feng75b410b2018-10-10 15:01:19 -0700226 curKey = nextKey;
227 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800228 if (curKey.error().code() == ENOENT) return {};
229 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700230}
231
232template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800233base::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 Feng75b410b2018-10-10 15:01:19 -0700243 curKey = nextKey;
244 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800245 if (curKey.error().code() == ENOENT) return {};
246 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700247}
248
249} // namespace bpf
250} // namespace android
251
252#endif