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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*
* Copyright (c) 2019 Facebook, Inc.
*
* 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://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"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
TEST_CASE("test hash of maps", "[hash_of_maps]") {
{
const std::string BPF_PROGRAM = R"(
BPF_ARRAY(cntl, int, 1);
BPF_ARRAY(ex1, int, 1024);
BPF_ARRAY(ex2, int, 1024);
BPF_ARRAY(ex3, u64, 1024);
BPF_HASH_OF_MAPS(maps_hash, int, "ex1", 10);
int syscall__getuid(void *ctx) {
int key = 0, data, *val, cntl_val;
void *inner_map;
val = cntl.lookup(&key);
if (!val || *val == 0)
return 0;
// cntl_val == 1 : lookup and update
cntl_val = *val;
inner_map = maps_hash.lookup(&key);
if (!inner_map)
return 0;
if (cntl_val == 1) {
val = bpf_map_lookup_elem(inner_map, &key);
if (val) {
data = 1;
bpf_map_update_elem(inner_map, &key, &data, 0);
}
}
return 0;
}
)";
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.ok());
auto t = bpf.get_map_in_map_table<int>("maps_hash");
auto ex1_table = bpf.get_array_table<int>("ex1");
auto ex2_table = bpf.get_array_table<int>("ex2");
auto ex3_table = bpf.get_array_table<unsigned long long>("ex3");
int ex1_fd = ex1_table.get_fd();
int ex2_fd = ex2_table.get_fd();
int ex3_fd = ex3_table.get_fd();
int key = 0, value = 0;
res = t.update_value(key, ex1_fd);
REQUIRE(res.ok());
// updating already-occupied slot will succeed.
res = t.update_value(key, ex2_fd);
REQUIRE(res.ok());
res = t.update_value(key, ex1_fd);
REQUIRE(res.ok());
// an in-compatible map
key = 1;
res = t.update_value(key, ex3_fd);
REQUIRE(res.code() == -1);
// hash table, any valid key should work as long
// as hash table is not full.
key = 10;
res = t.update_value(key, ex2_fd);
REQUIRE(res.ok());
res = t.remove_value(key);
REQUIRE(res.ok());
// test effectiveness of map-in-map
key = 0;
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "syscall__getuid");
REQUIRE(res.ok());
auto cntl_table = bpf.get_array_table<int>("cntl");
cntl_table.update_value(0, 1);
REQUIRE(getuid() >= 0);
res = ex1_table.get_value(key, value);
REQUIRE(res.ok());
REQUIRE(value > 0);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.ok());
res = t.remove_value(key);
REQUIRE(res.ok());
}
}
TEST_CASE("test hash of maps using custom key", "[hash_of_maps_custom_key]") {
{
const std::string BPF_PROGRAM = R"(
struct custom_key {
int value_1;
int value_2;
};
BPF_ARRAY(cntl, int, 1);
BPF_TABLE("hash", int, int, ex1, 1024);
BPF_TABLE("hash", int, int, ex2, 1024);
BPF_HASH_OF_MAPS(maps_hash, struct custom_key, "ex1", 10);
int syscall__getuid(void *ctx) {
struct custom_key hash_key = {1, 0};
int key = 0, data, *val, cntl_val;
void *inner_map;
val = cntl.lookup(&key);
if (!val || *val == 0)
return 0;
hash_key.value_2 = *val;
inner_map = maps_hash.lookup(&hash_key);
if (!inner_map)
return 0;
val = bpf_map_lookup_elem(inner_map, &key);
if (!val) {
data = 1;
bpf_map_update_elem(inner_map, &key, &data, 0);
} else {
data = 1 + *val;
bpf_map_update_elem(inner_map, &key, &data, 0);
}
return 0;
}
)";
struct custom_key {
int value_1;
int value_2;
};
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.ok());
auto t = bpf.get_map_in_map_table<struct custom_key>("maps_hash");
auto ex1_table = bpf.get_hash_table<int, int>("ex1");
auto ex2_table = bpf.get_hash_table<int, int>("ex2");
auto cntl_table = bpf.get_array_table<int>("cntl");
int ex1_fd = ex1_table.get_fd();
int ex2_fd = ex2_table.get_fd();
// test effectiveness of map-in-map
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "syscall__getuid");
REQUIRE(res.ok());
struct custom_key hash_key = {1, 1};
res = t.update_value(hash_key, ex1_fd);
REQUIRE(res.ok());
struct custom_key hash_key2 = {1, 2};
res = t.update_value(hash_key2, ex2_fd);
REQUIRE(res.ok());
int key = 0, value = 0, value2 = 0;
// Can't get value when value didn't set.
res = ex1_table.get_value(key, value);
REQUIRE(!res.ok());
REQUIRE(value == 0);
// Call syscall__getuid, then set value to ex1_table
res = cntl_table.update_value(key, 1);
REQUIRE(res.ok());
REQUIRE(getuid() >= 0);
// Now we can get value from ex1_table
res = ex1_table.get_value(key, value);
REQUIRE(res.ok());
REQUIRE(value >= 1);
// Can't get value when value didn't set.
res = ex2_table.get_value(key, value2);
REQUIRE(!res.ok());
REQUIRE(value2 == 0);
// Call syscall__getuid, then set value to ex2_table
res = cntl_table.update_value(key, 2);
REQUIRE(res.ok());
REQUIRE(getuid() >= 0);
// Now we can get value from ex2_table
res = ex2_table.get_value(key, value2);
REQUIRE(res.ok());
REQUIRE(value > 0);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.ok());
res = t.remove_value(hash_key);
REQUIRE(res.ok());
res = t.remove_value(hash_key2);
REQUIRE(res.ok());
}
}
TEST_CASE("test array of maps", "[array_of_maps]") {
{
const std::string BPF_PROGRAM = R"(
BPF_ARRAY(cntl, int, 1);
BPF_TABLE("hash", int, int, ex1, 1024);
BPF_TABLE("hash", int, int, ex2, 1024);
BPF_TABLE("hash", u64, u64, ex3, 1024);
BPF_ARRAY_OF_MAPS(maps_array, "ex1", 10);
int syscall__getuid(void *ctx) {
int key = 0, data, *val, cntl_val;
void *inner_map;
val = cntl.lookup(&key);
if (!val || *val == 0)
return 0;
// cntl_val == 1 : lookup and update
// cntl_val == 2 : delete
cntl_val = *val;
inner_map = maps_array.lookup(&key);
if (!inner_map)
return 0;
if (cntl_val == 1) {
val = bpf_map_lookup_elem(inner_map, &key);
if (!val) {
data = 1;
bpf_map_update_elem(inner_map, &key, &data, 0);
}
} else if (cntl_val == 2) {
bpf_map_delete_elem(inner_map, &key);
}
return 0;
}
)";
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.ok());
auto t = bpf.get_map_in_map_table<int>("maps_array");
auto ex1_table = bpf.get_hash_table<int, int>("ex1");
auto ex2_table = bpf.get_hash_table<int, int>("ex2");
auto ex3_table =
bpf.get_hash_table<unsigned long long, unsigned long long>("ex3");
int ex1_fd = ex1_table.get_fd();
int ex2_fd = ex2_table.get_fd();
int ex3_fd = ex3_table.get_fd();
int key = 0, value = 0;
res = t.update_value(key, ex1_fd);
REQUIRE(res.ok());
// updating already-occupied slot will succeed.
res = t.update_value(key, ex2_fd);
REQUIRE(res.ok());
res = t.update_value(key, ex1_fd);
REQUIRE(res.ok());
// an in-compatible map
key = 1;
res = t.update_value(key, ex3_fd);
REQUIRE(res.code() == -1);
// array table, out of bound access
key = 10;
res = t.update_value(key, ex2_fd);
REQUIRE(res.code() == -1);
// test effectiveness of map-in-map
key = 0;
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "syscall__getuid");
REQUIRE(res.ok());
auto cntl_table = bpf.get_array_table<int>("cntl");
cntl_table.update_value(0, 1);
REQUIRE(getuid() >= 0);
res = ex1_table.get_value(key, value);
REQUIRE(res.ok());
REQUIRE(value == 1);
cntl_table.update_value(0, 2);
REQUIRE(getuid() >= 0);
res = ex1_table.get_value(key, value);
REQUIRE(res.code() == -1);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.ok());
res = t.remove_value(key);
REQUIRE(res.ok());
}
}
#endif
|