blob: 4b941a8c9400f2f8a545468b0bbfd9ae9bb4fe56 [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>() {};
Maciej Żenczykowskie6cbd402019-08-09 18:20:26 -070049
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -080050 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 Żenczykowskie6cbd402019-08-09 18:20:26 -070059
Chenbo Feng75b410b2018-10-10 15:01:19 -070060 BpfMap<Key, Value>(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags) {
61 int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags);
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -080062 if (map_fd >= 0) mMapFd.reset(map_fd);
Chenbo Feng75b410b2018-10-10 15:01:19 -070063 }
64
Steven Morelande7cd2a72020-01-10 17:49:35 -080065 base::Result<Key> getFirstKey() const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070066 Key firstKey;
67 if (getFirstMapKey(mMapFd, &firstKey)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080068 return base::ErrnoErrorf("Get firstKey map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070069 }
70 return firstKey;
71 }
72
Steven Morelande7cd2a72020-01-10 17:49:35 -080073 base::Result<Key> getNextKey(const Key& key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070074 Key nextKey;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080075 if (getNextMapKey(mMapFd, &key, &nextKey)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080076 return base::ErrnoErrorf("Get next key of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070077 }
78 return nextKey;
79 }
80
Steven Morelande7cd2a72020-01-10 17:49:35 -080081 base::Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080082 if (writeToMapEntry(mMapFd, &key, &value, flags)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080083 return base::ErrnoErrorf("Write to map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070084 }
Steven Morelande7cd2a72020-01-10 17:49:35 -080085 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -070086 }
87
Steven Morelande7cd2a72020-01-10 17:49:35 -080088 base::Result<Value> readValue(const Key key) const {
Chenbo Feng75b410b2018-10-10 15:01:19 -070089 Value value;
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080090 if (findMapEntry(mMapFd, &key, &value)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080091 return base::ErrnoErrorf("Read value of map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070092 }
93 return value;
94 }
95
Steven Morelande7cd2a72020-01-10 17:49:35 -080096 base::Result<void> deleteValue(const Key& key) {
Maciej Żenczykowski79365da2020-01-17 19:07:37 -080097 if (deleteMapEntry(mMapFd, &key)) {
Steven Morelande7cd2a72020-01-10 17:49:35 -080098 return base::ErrnoErrorf("Delete entry from map {} failed", mMapFd.get());
Chenbo Feng75b410b2018-10-10 15:01:19 -070099 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800100 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700101 }
102
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700103 // Function that tries to get map from a pinned path.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800104 base::Result<void> init(const char* path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700105
106 // Iterate through the map and handle each key retrieved based on the filter
107 // without modification of map content.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800108 base::Result<void> iterate(
109 const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
110 filter) const;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700111
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 Morelande7cd2a72020-01-10 17:49:35 -0800114 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 Feng75b410b2018-10-10 15:01:19 -0700117
118 // Iterate through the map and handle each key retrieved based on the filter
Steven Morelande7cd2a72020-01-10 17:49:35 -0800119 base::Result<void> iterate(
120 const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>&
121 filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700122
123 // Iterate through the map and get each <key, value> pair, handle each <key,
124 // value> pair based on the filter.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800125 base::Result<void> iterateWithValue(
126 const std::function<base::Result<void>(const Key& key, const Value& value,
127 BpfMap<Key, Value>& map)>& filter);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700128
129 const base::unique_fd& getMap() const { return mMapFd; };
130
Chenbo Feng75b410b2018-10-10 15:01:19 -0700131 // Move constructor
132 void operator=(BpfMap<Key, Value>&& other) noexcept {
133 mMapFd = std::move(other.mMapFd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700134 other.reset();
135 }
136
137 void reset(int fd = -1) {
138 mMapFd.reset(fd);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700139 }
140
141 bool isValid() const { return mMapFd != -1; }
142
143 // It is only safe to call this method if it is guaranteed that nothing will concurrently
144 // iterate over the map in any process.
Steven Morelande7cd2a72020-01-10 17:49:35 -0800145 base::Result<void> clear() {
146 const auto deleteAllEntries = [](const Key& key,
147 BpfMap<Key, Value>& map) -> base::Result<void> {
148 base::Result<void> res = map.deleteValue(key);
149 if (!res && res.error().code() != ENOENT) {
150 ALOGE("Failed to delete data %s", strerror(res.error().code()));
Chenbo Feng75b410b2018-10-10 15:01:19 -0700151 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800152 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700153 };
Steven Morelande7cd2a72020-01-10 17:49:35 -0800154 return iterate(deleteAllEntries);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700155 }
156
Steven Morelande7cd2a72020-01-10 17:49:35 -0800157 base::Result<bool> isEmpty() const {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700158 auto key = this->getFirstKey();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800159 if (!key) {
160 // Return error code ENOENT means the map is empty
161 if (key.error().code() == ENOENT) return true;
162 return key.error();
163 }
Chenbo Feng75b410b2018-10-10 15:01:19 -0700164 return false;
165 }
166
167 private:
168 base::unique_fd mMapFd;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700169};
170
171template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800172base::Result<void> BpfMap<Key, Value>::init(const char* path) {
Maciej Żenczykowski52108bf2019-04-01 10:41:13 -0700173 mMapFd = base::unique_fd(mapRetrieve(path, 0));
174 if (mMapFd == -1) {
175 reset();
Steven Morelande7cd2a72020-01-10 17:49:35 -0800176 return base::ErrnoErrorf("Pinned map not accessible or does not exist: ({})", path);
Chenbo Feng75b410b2018-10-10 15:01:19 -0700177 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800178 return {};
Chenbo Feng75b410b2018-10-10 15:01:19 -0700179}
180
181template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800182base::Result<void> BpfMap<Key, Value>::iterate(
183 const std::function<base::Result<void>(const Key& key, const BpfMap<Key, Value>& map)>&
184 filter) const {
185 base::Result<Key> curKey = getFirstKey();
186 while (curKey) {
187 const base::Result<Key>& nextKey = getNextKey(curKey.value());
188 base::Result<void> status = filter(curKey.value(), *this);
189 if (!status) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700190 curKey = nextKey;
191 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800192 if (curKey.error().code() == ENOENT) return {};
193 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700194}
195
196template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800197base::Result<void> BpfMap<Key, Value>::iterateWithValue(
198 const std::function<base::Result<void>(const Key& key, const Value& value,
199 const BpfMap<Key, Value>& map)>& filter) const {
200 base::Result<Key> curKey = getFirstKey();
201 while (curKey) {
202 const base::Result<Key>& nextKey = getNextKey(curKey.value());
203 base::Result<Value> curValue = this->readValue(curKey.value());
204 if (!curValue) return curValue.error();
205 base::Result<void> status = filter(curKey.value(), curValue.value(), *this);
206 if (!status) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700207 curKey = nextKey;
208 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800209 if (curKey.error().code() == ENOENT) return {};
210 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700211}
212
213template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800214base::Result<void> BpfMap<Key, Value>::iterate(
215 const std::function<base::Result<void>(const Key& key, BpfMap<Key, Value>& map)>& filter) {
216 base::Result<Key> curKey = getFirstKey();
217 while (curKey) {
218 const base::Result<Key>& nextKey = getNextKey(curKey.value());
219 base::Result<void> status = filter(curKey.value(), *this);
220 if (!status) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700221 curKey = nextKey;
222 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800223 if (curKey.error().code() == ENOENT) return {};
224 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700225}
226
227template <class Key, class Value>
Steven Morelande7cd2a72020-01-10 17:49:35 -0800228base::Result<void> BpfMap<Key, Value>::iterateWithValue(
229 const std::function<base::Result<void>(const Key& key, const Value& value,
230 BpfMap<Key, Value>& map)>& filter) {
231 base::Result<Key> curKey = getFirstKey();
232 while (curKey) {
233 const base::Result<Key>& nextKey = getNextKey(curKey.value());
234 base::Result<Value> curValue = this->readValue(curKey.value());
235 if (!curValue) return curValue.error();
236 base::Result<void> status = filter(curKey.value(), curValue.value(), *this);
237 if (!status) return status;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700238 curKey = nextKey;
239 }
Steven Morelande7cd2a72020-01-10 17:49:35 -0800240 if (curKey.error().code() == ENOENT) return {};
241 return curKey.error();
Chenbo Feng75b410b2018-10-10 15:01:19 -0700242}
243
Maciej Żenczykowskidfd941f2020-01-20 03:18:26 -0800244template <class Key, class Value>
245class BpfMapRO : public BpfMap<Key, Value> {
246 public:
247 explicit BpfMapRO<Key, Value>(const char* pathname)
248 : BpfMap<Key, Value>(pathname, BPF_F_RDONLY) {}
249};
250
Chenbo Feng75b410b2018-10-10 15:01:19 -0700251} // namespace bpf
252} // namespace android
253
254#endif