Skip to content

Commit

Permalink
cc: add support for array table
Browse files Browse the repository at this point in the history
Before this commit there was not a specify way to handle a map of type
array. A workaround was to use it as a hash table with int as key type.

This commit creates a new class (BPFArrayTable) that allows to get and
update the value of an element.

Signed-off-by: Mauricio Vasquez B <[email protected]>
  • Loading branch information
mauriciovasquezbernal committed Apr 4, 2017
1 parent 089ad4c commit 0f3787f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cc/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class BPF {
int group_fd = -1);
StatusTuple detach_perf_event(uint32_t ev_type, uint32_t ev_config);

template <class ValueType>
BPFArrayTable<ValueType> get_array_table(const std::string& name) {
TableStorage::iterator it;
if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it))
return BPFArrayTable<ValueType>(it->second);
return BPFArrayTable<ValueType>({});
}

template <class KeyType, class ValueType>
BPFHashTable<KeyType, ValueType> get_hash_table(const std::string& name) {
TableStorage::iterator it;
Expand Down
39 changes: 39 additions & 0 deletions src/cc/BPFTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,45 @@ class BPFTableBase {
size_t capacity_;
};

template <class ValueType>
class BPFArrayTable : protected BPFTableBase<int, ValueType> {
public:
BPFArrayTable(const TableDesc& desc)
: BPFTableBase<int, ValueType>(desc) {
if (desc.type != BPF_MAP_TYPE_ARRAY &&
desc.type != BPF_MAP_TYPE_PERCPU_ARRAY)
throw std::invalid_argument("Table '" + desc.name + "' is not an array table");
}

StatusTuple get_value(const int& index, ValueType& value) {
if (!this->lookup(const_cast<int*>(&index), &value))
return StatusTuple(-1, "Error getting value: %s", std::strerror(errno));
return StatusTuple(0);
}

StatusTuple update_value(const int& index, const ValueType& value) {
if (!this->update(const_cast<int*>(&index), const_cast<ValueType*>(&value)))
return StatusTuple(-1, "Error updating value: %s", std::strerror(errno));
return StatusTuple(0);
}

ValueType operator[](const int& key) {
ValueType value;
get_value(key, value);
return value;
}

std::vector<ValueType> get_table_offline() {
std::vector<ValueType> res(this->capacity());

for (int i = 0; i < (int) this->capacity(); i++) {
get_value(i, res[i]);
}

return res;
}
};

template <class KeyType, class ValueType>
class BPFHashTable : protected BPFTableBase<KeyType, ValueType> {
public:
Expand Down
1 change: 1 addition & 0 deletions tests/cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_test(NAME c_test_static COMMAND ${TEST_WRAPPER} c_test_static sudo ${CMAKE_C
add_executable(test_libbcc
test_libbcc.cc
test_c_api.cc
test_array_table.cc
test_hash_table.cc
test_usdt_args.cc
test_usdt_probes.cc)
Expand Down
93 changes: 93 additions & 0 deletions tests/cc/test_array_table.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 "BPF.h"

#include "catch.hpp"

#include <random>
#include <iostream>

TEST_CASE("test array table", "[array_table]") {
const std::string BPF_PROGRAM = R"(
BPF_TABLE("hash", int, int, myhash, 128);
BPF_TABLE("array", int, int, myarray, 128);
)";

ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);

ebpf::BPFArrayTable<int> t = bpf.get_array_table<int>("myarray");

SECTION("bad table type") {
// try to get table of wrong type
auto f1 = [&](){
bpf.get_array_table<int>("myhash");
};

REQUIRE_THROWS(f1());
}

SECTION("standard methods") {
int i, v1, v2;
i = 1;
v1 = 42;
// update element
res = t.update_value(i, v1);
REQUIRE(res.code() == 0);
res = t.get_value(i, v2);
REQUIRE(res.code() == 0);
REQUIRE(v2 == 42);

// update another element
i = 2;
v1 = 69;
res = t.update_value(i, v1);
REQUIRE(res.code() == 0);
res = t.get_value(i, v2);
REQUIRE(res.code() == 0);
REQUIRE(v2 == 69);

// get non existing element
i = 1024;
res = t.get_value(i, v2);
REQUIRE(res.code() != 0);
}

SECTION("full table") {
// random number generator
std::mt19937 rng;
rng.seed(std::random_device()());
std::uniform_int_distribution<int> dist;

std::vector<int> localtable(128);

for(int i = 0; i < 128; i++) {
int v = dist(rng);

res = t.update_value(i, v);
REQUIRE(res.code() == 0);

// save it in the local table to compare later on
localtable[i] = v;
}

std::vector<int> offlinetable = t.get_table_offline();
REQUIRE(localtable == offlinetable);
}
}

0 comments on commit 0f3787f

Please sign in to comment.