Repository navigation
Expand file tree
/
Copy pathbpfmap.cpp
More file actions
302 lines (266 loc) · 7.79 KB
/
Copy pathbpfmap.cpp
File metadata and controls
302 lines (266 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <sstream>
#include <unordered_map>
#include "bpfmap.h"
#include "util/stats.h"
#include "util/tseries.h"
namespace bpftrace {
char BpfMapError::ID = 0;
const std::unordered_map<std::string, bpf_map_type> BPF_MAP_TYPES = {
{ "hash", BPF_MAP_TYPE_HASH },
{ "lruhash", BPF_MAP_TYPE_LRU_HASH },
{ "percpuhash", BPF_MAP_TYPE_PERCPU_HASH },
{ "percpulruhash", BPF_MAP_TYPE_LRU_PERCPU_HASH }
};
int BpfMap::fd() const
{
return bpf_map__fd(bpf_map_);
}
bpf_map_type BpfMap::type() const
{
return type_;
}
const std::string &BpfMap::bpf_name() const
{
return name_;
}
std::string BpfMap::name() const
{
return bpftrace_map_name(bpf_name());
}
uint32_t BpfMap::max_entries() const
{
return max_entries_;
}
bool BpfMap::is_stack_map() const
{
return name().starts_with("stack_");
}
bool BpfMap::is_per_cpu_type() const
{
return type() == BPF_MAP_TYPE_PERCPU_HASH ||
type() == BPF_MAP_TYPE_LRU_PERCPU_HASH;
}
bool BpfMap::is_printable() const
{
// Internal maps are not printable
return bpf_name().starts_with("AT_");
}
std::vector<OpaqueValue> BpfMap::collect_keys() const
{
const void *last_key = nullptr;
std::vector<OpaqueValue> keys;
while (true) {
int rc = 0;
auto key = OpaqueValue::alloc(key_size_, [&](void *data) {
rc = bpf_map_get_next_key(fd(), last_key, data);
});
if (rc != 0) {
break;
}
last_key = keys.emplace_back(std::move(key)).data();
}
return keys;
}
Result<> BpfMap::zero_out(int nvalues) const
{
auto keys = collect_keys();
auto value_size = static_cast<size_t>(value_size_) *
static_cast<size_t>(nvalues);
auto zero = OpaqueValue::alloc(value_size, [&](void *data) {
memset(data, 0, value_size);
});
for (auto &k : keys) {
int err = bpf_map_update_elem(fd(), k.data(), zero.data(), BPF_EXIST);
if (err && err != -ENOENT) {
return make_error<BpfMapError>(name_, "zero", err);
}
}
return OK();
}
Result<> BpfMap::clear() const
{
auto keys = collect_keys();
for (auto &k : keys) {
int err = bpf_map_delete_elem(fd(), k.data());
if (err && err != -ENOENT) {
return make_error<BpfMapError>(name_, "clear", err);
}
}
return OK();
}
Result<> BpfMap::update_elem(const void *key, const void *value) const
{
auto err = bpf_map_update_elem(fd(), key, value, BPF_ANY);
if (err != 0) {
return make_error<BpfMapError>(name_, "update", err);
}
return OK();
}
Result<> BpfMap::lookup_elem(const void *key, void *value) const
{
auto err = bpf_map_lookup_elem(fd(), key, value);
if (err != 0) {
return make_error<BpfMapError>(name_, "lookup", err);
}
return OK();
}
Result<> BpfMap::resize(uint32_t new_size) const
{
auto err = bpf_map__set_max_entries(bpf_map_, new_size);
if (err != 0) {
return make_error<BpfMapError>(name_, "resize", err);
}
return OK();
}
Result<MapElements> BpfMap::collect_elements(int nvalues) const
{
auto keys = collect_keys();
MapElements values_by_key;
for (auto &key : keys) {
int err = 0;
auto value = OpaqueValue::alloc(
static_cast<size_t>(value_size_) * static_cast<size_t>(nvalues),
[&](void *data) { err = bpf_map_lookup_elem(fd(), key.data(), data); });
if (err == -ENOENT) {
// key was removed by the eBPF program during bpf_map_get_next_key() and
// bpf_map_lookup_elem(), let's skip this key.
continue;
} else if (err) {
return make_error<BpfMapError>(name_, "lookup", err);
}
values_by_key.emplace_back(std::move(key), std::move(value));
}
return values_by_key;
}
Result<HistogramMap> BpfMap::collect_histogram_data(const MapInfo &map_info,
int nvalues) const
{
auto keys = collect_keys();
HistogramMap values_by_key;
for (auto &key : keys) {
int err = 0;
auto value = OpaqueValue::alloc(
static_cast<size_t>(value_size_) * static_cast<size_t>(nvalues),
[&](void *data) { err = bpf_map_lookup_elem(fd(), key.data(), data); });
if (err == -ENOENT) {
// key was removed by the eBPF program during bpf_map_get_next_key() and
// bpf_map_lookup_elem(), let's skip this key
continue;
} else if (err) {
return make_error<BpfMapError>(name_, "lookup", err);
}
auto prefix = key.slice(0, map_info.key_type.GetSize());
auto bucket = key.slice(map_info.key_type.GetSize(), sizeof(uint64_t));
if (!values_by_key.contains(prefix)) {
// New key - create a list of buckets for it
if (map_info.value_type.IsHistTy())
values_by_key[prefix].resize(65 * 32, 0);
else
values_by_key[prefix].resize(1002, 0);
}
auto idx = bucket.bitcast<uint64_t>();
values_by_key[prefix].at(idx) = util::reduce_value<uint64_t>(value);
}
return values_by_key;
}
Result<TSeriesMap> BpfMap::collect_tseries_data(const MapInfo &map_info,
int nvalues) const
{
auto keys = collect_keys();
TSeriesMap values_by_key;
const auto &tseries_args = std::get<TSeriesArgs>(map_info.detail);
for (auto &key : keys) {
int err = 0;
auto value = OpaqueValue::alloc(
static_cast<size_t>(value_size_) * static_cast<size_t>(nvalues),
[&](void *data) { err = bpf_map_lookup_elem(fd(), key.data(), data); });
if (err == -ENOENT) {
// key was removed by the eBPF program during bpf_map_get_next_key() and
// bpf_map_lookup_elem(), let's skip this key
continue;
} else if (err) {
return make_error<BpfMapError>(name_, "lookup", err);
}
auto prefix = key.slice(0, map_info.key_type.GetSize());
auto bucket = key.slice(map_info.key_type.GetSize(), sizeof(uint64_t));
auto tseries = values_by_key.try_emplace(prefix).first;
auto [epoch, v] = util::reduce_tseries_value(value,
tseries_args.value_type,
tseries_args.agg);
tseries->second.emplace(epoch, std::move(v));
}
return values_by_key;
}
std::string to_string(MapType t)
{
switch (t) {
case MapType::PerfEvent:
return "perf_event";
case MapType::Elapsed:
return "elapsed";
case MapType::Ringbuf:
return "ringbuf";
case MapType::EventLossCounter:
return "event_loss_counter";
case MapType::RecursionPrevention:
return "recursion_prevention";
}
return {}; // unreached
}
bpf_map_type get_bpf_map_type(const SizedType &val_type)
{
if (val_type.NeedsPercpuMap()) {
return BPF_MAP_TYPE_PERCPU_HASH;
} else {
return BPF_MAP_TYPE_HASH;
}
}
std::optional<bpf_map_type> get_bpf_map_type(const std::string &name)
{
auto found = BPF_MAP_TYPES.find(name);
if (found == BPF_MAP_TYPES.end()) {
return std::nullopt;
}
return found->second;
}
std::string get_bpf_map_type_str(bpf_map_type map_type)
{
for (const auto &pair : BPF_MAP_TYPES) {
if (pair.second == map_type) {
return pair.first;
}
}
return "unknown";
}
void add_bpf_map_types_hint(std::stringstream &hint)
{
hint << "Valid map types: ";
int num_types = BPF_MAP_TYPES.size();
for (const auto &bpf_type : BPF_MAP_TYPES) {
hint << bpf_type.first;
num_types--;
if (num_types != 0) {
hint << ", ";
}
}
}
bool bpf_map_types_compatible(const SizedType &val_type, bpf_map_type kind)
{
auto kind_from_stype = get_bpf_map_type(val_type);
if (kind_from_stype == kind) {
return true;
}
if ((kind_from_stype == BPF_MAP_TYPE_HASH ||
kind_from_stype == BPF_MAP_TYPE_LRU_HASH) &&
(kind == BPF_MAP_TYPE_HASH || kind == BPF_MAP_TYPE_LRU_HASH)) {
return true;
}
if ((kind_from_stype == BPF_MAP_TYPE_PERCPU_HASH ||
kind_from_stype == BPF_MAP_TYPE_LRU_PERCPU_HASH) &&
(kind == BPF_MAP_TYPE_PERCPU_HASH ||
kind == BPF_MAP_TYPE_LRU_PERCPU_HASH)) {
return true;
}
return false;
}
} // namespace bpftrace