Skip to content

Commit

Permalink
fix get_table_offline for percpu hash table
Browse files Browse the repository at this point in the history
Use get_value() instead of lookup() as the value as in the case of
percpu tables the ValueType is an std::vector that has to be resized
before performing the lookup function.

Add also some testing for it.

Solves iovisor#1860

Signed-off-by: Mauricio Vasquez B <[email protected]>
  • Loading branch information
mauriciovasquezbernal committed Jul 11, 2018
1 parent 1cf67ae commit b5cadaf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cc/api/BPFTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@ class BPFHashTable : public BPFTableBase<KeyType, ValueType> {
KeyType cur;
ValueType value;

StatusTuple r(0);

if (!this->first(&cur))
return res;

while (true) {
if (!this->lookup(&cur, &value))
r = get_value(cur, value);
if (r.code() != 0)
break;
res.emplace_back(cur, value);
if (!this->next(&cur, &cur))
Expand Down
33 changes: 33 additions & 0 deletions tests/cc/test_hash_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ TEST_CASE("test hash table", "[hash_table]") {
REQUIRE(pair.first % 3 == 0);
REQUIRE(pair.first / 3 == pair.second);
}

// clear table
t.clear_table_non_atomic();
REQUIRE(t.get_table_offline().size() == 0);
}
}

Expand Down Expand Up @@ -156,5 +160,34 @@ TEST_CASE("percpu hash table", "[percpu_hash_table]") {
res = t.get_value(k, v2);
REQUIRE(res.code() != 0);
}

SECTION("walk table") {
std::vector<uint64_t> v(ncpus);

for (int k = 3; k <= 30; k+=3) {
for (size_t cpu = 0; cpu < ncpus; cpu++) {
v[cpu] = k * cpu;
}
res = t.update_value(k, v);
REQUIRE(res.code() == 0);
}

// get whole table
auto offline = t.get_table_offline();
REQUIRE(offline.size() == 10);
for (int i = 0; i < 10; i++) {
// check the key
REQUIRE(offline.at(i).first % 3 == 0);

// check value
for (size_t cpu = 0; cpu < ncpus; cpu++) {
REQUIRE(offline.at(i).second.at(cpu) == cpu * offline.at(i).first);
}
}

// clear table
t.clear_table_non_atomic();
REQUIRE(t.get_table_offline().size() == 0);
}
}
#endif

0 comments on commit b5cadaf

Please sign in to comment.