forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bpf_table.cc
210 lines (176 loc) · 5.47 KB
/
test_bpf_table.cc
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
/*
* Copyright (c) 2017 Politecnico di Torino
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/version.h>
#include <unistd.h>
#include <string>
#include "BPF.h"
#include "catch.hpp"
TEST_CASE("test bpf table", "[bpf_table]") {
const std::string BPF_PROGRAM = R"(
BPF_TABLE("hash", int, int, myhash, 128);
)";
ebpf::BPF *bpf(new ebpf::BPF);
ebpf::StatusTuple res(0);
res = bpf->init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
ebpf::BPFTable t = bpf->get_table("myhash");
// update element
std::string value;
res = t.update_value("0x07", "0x42");
REQUIRE(res.code() == 0);
res = t.get_value("0x07", value);
REQUIRE(res.code() == 0);
REQUIRE(value == "0x42");
// update another element
res = t.update_value("0x11", "0x777");
REQUIRE(res.code() == 0);
res = t.get_value("0x11", value);
REQUIRE(res.code() == 0);
REQUIRE(value == "0x777");
// remove value
res = t.remove_value("0x11");
REQUIRE(res.code() == 0);
res = t.get_value("0x11", value);
REQUIRE(res.code() != 0);
// clear table
res = t.update_value("0x15", "0x888");
REQUIRE(res.code() == 0);
auto elements = bpf->get_hash_table<int, int>("myhash").get_table_offline();
REQUIRE(elements.size() == 2);
res = t.clear_table_non_atomic();
REQUIRE(res.code() == 0);
elements = bpf->get_hash_table<int, int>("myhash").get_table_offline();
REQUIRE(elements.size() == 0);
// delete bpf_module, call to key/leaf printf/scanf must fail
delete bpf;
res = t.update_value("0x07", "0x42");
REQUIRE(res.code() != 0);
res = t.get_value("0x07", value);
REQUIRE(res.code() != 0);
res = t.remove_value("0x07");
REQUIRE(res.code() != 0);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
TEST_CASE("test bpf percpu tables", "[bpf_percpu_table]") {
const std::string BPF_PROGRAM = R"(
BPF_TABLE("percpu_hash", int, u64, myhash, 128);
)";
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
ebpf::BPFTable t = bpf.get_table("myhash");
size_t ncpus = ebpf::BPFTable::get_possible_cpu_count();
std::vector<std::string> v1(ncpus);
for (size_t i = 0; i < ncpus; i++) {
v1.at(i) = std::to_string(42 * i);
}
// update element
std::vector<std::string> value;
res = t.update_value("0x07", v1);
REQUIRE(res.code() == 0);
res = t.get_value("0x07", value);
REQUIRE(res.code() == 0);
for (size_t i = 0; i < ncpus; i++) {
REQUIRE(42 * i == std::stoul(value.at(i), nullptr, 16));
}
}
#endif
TEST_CASE("test bpf hash table", "[bpf_hash_table]") {
const std::string BPF_PROGRAM = R"(
BPF_HASH(myhash, int, int, 128);
)";
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
auto t = bpf.get_hash_table<int, int>("myhash");
int key, value;
// updaate element
key = 0x08;
value = 0x43;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(t[key] == value);
// update another element
key = 0x12;
value = 0x778;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
key = 0x31;
value = 0x123;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
key = 0x12;
value = 0;
res = t.get_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(value == 0x778);
// remove value and dump table
key = 0x12;
res = t.remove_value(key);
REQUIRE(res.code() == 0);
auto values = t.get_table_offline();
REQUIRE(values.size() == 2);
// clear table
res = t.clear_table_non_atomic();
REQUIRE(res.code() == 0);
values = t.get_table_offline();
REQUIRE(values.size() == 0);
}
TEST_CASE("test bpf stack table", "[bpf_stack_table]") {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
const std::string BPF_PROGRAM = R"(
BPF_HASH(id, int, int, 1);
BPF_STACK_TRACE(stack_traces, 8);
int on_sys_getuid(void *ctx) {
int stack_id = stack_traces.get_stackid(ctx, BPF_F_REUSE_STACKID);
int zero = 0, *val;
val = id.lookup_or_init(&zero, &stack_id);
(*val) = stack_id;
return 0;
}
)";
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "on_sys_getuid");
REQUIRE(res.code() == 0);
REQUIRE(getuid() >= 0);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.code() == 0);
auto id = bpf.get_hash_table<int, int>("id");
auto stack_traces = bpf.get_stack_table("stack_traces");
int stack_id = id[0];
REQUIRE(stack_id >= 0);
auto addrs = stack_traces.get_stack_addr(stack_id);
auto symbols = stack_traces.get_stack_symbol(stack_id, -1);
REQUIRE(addrs.size() > 0);
REQUIRE(addrs.size() == symbols.size());
bool found = false;
for (const auto &symbol : symbols)
if (symbol.find("sys_getuid") != std::string::npos) {
found = true;
break;
}
REQUIRE(found);
stack_traces.clear_table_non_atomic();
addrs = stack_traces.get_stack_addr(stack_id);
REQUIRE(addrs.size() == 0);
#endif
}