blob: c42801aa4f276a4e109bf9239e405cc4d21a9ad3 [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
Maciej Żenczykowski23c436f2021-01-21 14:24:15 -080017#pragma once
Chenbo Feng75b410b2018-10-10 15:01:19 -070018
19#include <linux/bpf.h>
20
Steven Morelande7cd2a72020-01-10 17:49:35 -080021#include <android-base/result.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070022#include <android-base/stringprintf.h>
23#include <android-base/unique_fd.h>
24#include <utils/Log.h>
25#include "bpf/BpfUtils.h"
Chenbo Feng75b410b2018-10-10 15:01:19 -070026
27namespace android {
28namespace bpf {
29
30// This is a class wrapper for eBPF maps. The eBPF map is a special in-kernel
31// data structure that stores data in <Key, Value> pairs. It can be read/write
32// from userspace by passing syscalls with the map file descriptor. This class
33// is used to generalize the procedure of interacting with eBPF maps and hide
34// the implementation detail from other process. Besides the basic syscalls
35// wrapper, it also provides some useful helper functions as well as an iterator
36// nested class to iterate the map more easily.
37//
38// NOTE: A kernel eBPF map may be accessed by both kernel and userspace
39// processes at the same time. Or if the map is pinned as a virtual file, it can
40// be obtained by multiple eBPF map class object and accessed concurrently.
41// Though the map class object and the underlying kernel map are thread safe, it
42// is not safe to iterate over a map while another thread or process is deleting
43// from it. In this case the iteration can return duplicate entries.
44template <class Key, class Value>
45class BpfMap {
46 public:
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070047 BpfMap<Key, Value>() {};
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070048
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -080049 protected:
50 // flag must be within BPF_OBJ_FLAG_MASK, ie. 0, BPF_F_RDONLY, BPF_F_WRONLY
51 BpfMap<Key, Value>(const char* pathname, uint32_t flags) {
52 int map_fd = mapRetrieve(pathname, flags);
53 if (map_fd >= 0) mMapFd.reset(map_fd);
54 }
55
56 public:
57 explicit BpfMap<Key, Value>(const char* pathname) : BpfMap<Key, Value>(pathname, 0) {}
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070058
Hungming Chen6161ff22020-03-05 14:47:53 +080059 BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags = 0) {
Chenbo Feng75b410b2018-10-10 15:01:19 -070060 int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags);
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -080061 if (map_fd >= 0) mMapFd.reset(map_fd);
Chenbo Feng75b410b2018-10-10 15:01:19 -070062 }
63
Steven Morelande7cd2a72020-01-10 17:49:35 -080064 base::Result<Key> getFirstKey() const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070065 Key firstKey;
66 if (getFirstMapKey(mMapFd, &firstKey)) {
Tom Cherrya7146db2020-02-04 15:27:30 -080067 return ErrnoErrorf("Get firstKey map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070068 }
69 return firstKey;
70 }
71
Steven Morelande7cd2a72020-01-10 17:49:35 -080072 base::Result<Key> getNextKey(const Key& key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070073 Key nextKey;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080074 if (getNextMapKey(mMapFd, &key, &nextKey)) {
Tom Cherrya7146db2020-02-04 15:27:30 -080075 return ErrnoErrorf("Get next key of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070076 }
77 return nextKey;
78 }
79
Steven Morelande7cd2a72020-01-10 17:49:35 -080080 base::Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080081 if (writeToMapEntry(mMapFd, &key, &value, flags)) {
Tom Cherrya7146db2020-02-04 15:27:30 -080082 return ErrnoErrorf("Write to map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070083 }
Steven Morelande7cd2a72020-01-10 17:49:35 -080084 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -070085 }
86
Steven Morelande7cd2a72020-01-10 17:49:35 -080087 base::Result<Value> readValue(const Key key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070088 Value value;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080089 if (findMapEntry(mMapFd, &key, &value)) {
Tom Cherrya7146db2020-02-04 15:27:30 -080090 return ErrnoErrorf("Read value of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070091 }
92 return value;
93 }
94
Steven Morelande7cd2a72020-01-10 17:49:35 -080095 base::Result<void> deleteValue(const Key& key) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080096 if (deleteMapEntry(mMapFd, &key)) {
Tom Cherrya7146db2020-02-04 15:27:30 -080097 return ErrnoErrorf("Delete entry from map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070098 }
Steven Morelande7cd2a72020-01-10 17:49:35 -080099 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700100 }
101
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700102 // Function that tries to get map from a pinned path.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800103 base::Result<void> init(const char* path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700104
105 // Iterate through the map and handle each key retrieved based on the filter
106 // without modification of map content.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800107 base::Result<void> iterate(
108 const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
109 filter) const;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700110
111 // Iterate through the map and get each <key, value> pair, handle each <key,
112 // value> pair based on the filter without modification of map content.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800113 base::Result<void> iterateWithValue(
114 const std::function<base::Result<void>(const Key& key, const Value& value,
115 const BpfMap<Key, Value>& map)>& filter) const;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700116
117 // Iterate through the map and handle each key retrieved based on the filter
Steven Morelande7cd2a72020-01-10 17:49:35 -0800118 base::Result<void> iterate(
119 const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>&
120 filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700121
122 // Iterate through the map and get each <key, value> pair, handle each <key,
123 // value> pair based on the filter.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800124 base::Result<void> iterateWithValue(
125 const std::function<base::Result<void>(const Key& key, const Value& value,
126 BpfMap<Key, Value>& map)>& filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700127
128 const base::unique_fd& getMap() const { return mMapFd; };
129
Hungming Chen4008da92020-02-27 17:26:40 +0800130 // Copy assignment operator
Maciej Żenczykowski54a7e762020-04-24 08:02:21 -0700131 BpfMap<Key, Value>& operator=(const BpfMap<Key, Value>& other) {
132 if (this != &other) mMapFd.reset(fcntl(other.mMapFd.get(), F_DUPFD_CLOEXEC, 0));
133 return *this;
Hungming Chen4008da92020-02-27 17:26:40 +0800134 }
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);
Maciej Żenczykowski4f657392020-01-20 15:06:22 -0800139 other.reset(-1);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700140 }
141
Maciej Żenczykowski4f657392020-01-20 15:06:22 -0800142 void reset(base::unique_fd fd) = delete;
143
144 void reset(int fd) { mMapFd.reset(fd); }
Chenbo Feng75b410b2018-10-10 15:01:19 -0700145
146 bool isValid() const { return mMapFd != -1; }
147
Steven Morelande7cd2a72020-01-10 17:49:35 -0800148 base::Result<void> clear() {
Maciej Żenczykowskib7176fb2020-02-19 10:59:09 -0800149 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 Feng75b410b2018-10-10 15:01:19 -0700154 }
Maciej Żenczykowskib7176fb2020-02-19 10:59:09 -0800155 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 Feng75b410b2018-10-10 15:01:19 -0700163 }
164
Steven Morelande7cd2a72020-01-10 17:49:35 -0800165 base::Result<bool> isEmpty() const {
Maciej Żenczykowskib7176fb2020-02-19 10:59:09 -0800166 auto key = getFirstKey();
Bernie Innocenti43d25382020-02-10 07:25:16 +0900167 if (!key.ok()) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800168 // Return error code ENOENT means the map is empty
169 if (key.error().code() == ENOENT) return true;
170 return key.error();
171 }
Chenbo Feng75b410b2018-10-10 15:01:19 -0700172 return false;
173 }
174
175 private:
176 base::unique_fd mMapFd;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700177};
178
179template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800180base::Result<void> BpfMap<Key, Value>::init(const char* path) {
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700181 mMapFd = base::unique_fd(mapRetrieveRW(path));
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700182 if (mMapFd == -1) {
Tom Cherrya7146db2020-02-04 15:27:30 -0800183 return ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700184 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800185 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700186}
187
188template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800189base::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 Innocenti5953dee2020-02-06 04:22:41 +0900193 while (curKey.ok()) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800194 const base::Result<Key>& nextKey = getNextKey(curKey.value());
195 base::Result<void> status = filter(curKey.value(), *this);
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900196 if (!status.ok()) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700197 curKey = nextKey;
198 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800199 if (curKey.error().code() == ENOENT) return {};
200 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700201}
202
203template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800204base::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 Innocenti5953dee2020-02-06 04:22:41 +0900208 while (curKey.ok()) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800209 const base::Result<Key>& nextKey = getNextKey(curKey.value());
Maciej Żenczykowskib7176fb2020-02-19 10:59:09 -0800210 base::Result<Value> curValue = readValue(curKey.value());
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900211 if (!curValue.ok()) return curValue.error();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800212 base::Result<void> status = filter(curKey.value(), curValue.value(), *this);
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900213 if (!status.ok()) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700214 curKey = nextKey;
215 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800216 if (curKey.error().code() == ENOENT) return {};
217 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700218}
219
220template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800221base::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 Innocenti5953dee2020-02-06 04:22:41 +0900224 while (curKey.ok()) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800225 const base::Result<Key>& nextKey = getNextKey(curKey.value());
226 base::Result<void> status = filter(curKey.value(), *this);
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900227 if (!status.ok()) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700228 curKey = nextKey;
229 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800230 if (curKey.error().code() == ENOENT) return {};
231 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700232}
233
234template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800235base::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 Innocenti5953dee2020-02-06 04:22:41 +0900239 while (curKey.ok()) {
Steven Morelande7cd2a72020-01-10 17:49:35 -0800240 const base::Result<Key>& nextKey = getNextKey(curKey.value());
Maciej Żenczykowskib7176fb2020-02-19 10:59:09 -0800241 base::Result<Value> curValue = readValue(curKey.value());
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900242 if (!curValue.ok()) return curValue.error();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800243 base::Result<void> status = filter(curKey.value(), curValue.value(), *this);
Bernie Innocenti5953dee2020-02-06 04:22:41 +0900244 if (!status.ok()) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700245 curKey = nextKey;
246 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800247 if (curKey.error().code() == ENOENT) return {};
248 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700249}
250
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -0800251template <class Key, class Value>
252class 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 Feng75b410b2018-10-10 15:01:19 -0700258} // namespace bpf
259} // namespace android