aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMauricio Vásquez <mauriciovasquezbernal@gmail.com>2018-09-21 10:59:24 -0500
committeryonghong-song <ys114321@gmail.com>2018-09-21 08:59:24 -0700
commitc7ccd5b701959013d6565c8b6b5fbfe20ba46567 (patch)
tree95643458c5c43241f4c1ba46f4cb5bbe23f52f16 /src
parent6ce918bd7030241f0598ee6b1107940bf8480085 (diff)
downloadbcc-c7ccd5b701959013d6565c8b6b5fbfe20ba46567.tar.gz
cc: add BPFTable::get_offline_table (#1978)
get_offline_table is missing in the BPFTable class. Add it and some test cases. Signed-off-by: Mauricio Vasquez B <mauricio.vasquez@polito.it>
Diffstat (limited to 'src')
-rw-r--r--src/cc/api/BPFTable.cc66
-rw-r--r--src/cc/api/BPFTable.h1
2 files changed, 67 insertions, 0 deletions
diff --git a/src/cc/api/BPFTable.cc b/src/cc/api/BPFTable.cc
index 5a3d77e9..64fe77ca 100644
--- a/src/cc/api/BPFTable.cc
+++ b/src/cc/api/BPFTable.cc
@@ -180,6 +180,72 @@ StatusTuple BPFTable::clear_table_non_atomic() {
return StatusTuple(0);
}
+StatusTuple BPFTable::get_table_offline(
+ std::vector<std::pair<std::string, std::string>> &res) {
+ StatusTuple r(0);
+ int err;
+
+ auto key = std::unique_ptr<void, decltype(::free)*>(::malloc(desc.key_size),
+ ::free);
+ auto value = std::unique_ptr<void, decltype(::free)*>(::malloc(desc.leaf_size),
+ ::free);
+ std::string key_str;
+ std::string value_str;
+
+ if (desc.type == BPF_MAP_TYPE_ARRAY ||
+ desc.type == BPF_MAP_TYPE_PROG_ARRAY ||
+ desc.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
+ desc.type == BPF_MAP_TYPE_PERCPU_ARRAY ||
+ desc.type == BPF_MAP_TYPE_CGROUP_ARRAY ||
+ desc.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
+ desc.type == BPF_MAP_TYPE_DEVMAP ||
+ desc.type == BPF_MAP_TYPE_CPUMAP ||
+ desc.type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
+ // For arrays, just iterate over all indices
+ for (size_t i = 0; i < desc.max_entries; i++) {
+ err = bpf_lookup_elem(desc.fd, &i, value.get());
+ if (err < 0 && errno == ENOENT) {
+ // Element is not present, skip it
+ continue;
+ } else if (err < 0) {
+ // Other error, abort
+ return StatusTuple(-1, "Error looking up value: %s", std::strerror(errno));
+ }
+
+ r = key_to_string(&i, key_str);
+ if (r.code() != 0)
+ return r;
+
+ r = leaf_to_string(value.get(), value_str);
+ if (r.code() != 0)
+ return r;
+ res.emplace_back(key_str, value_str);
+ }
+ } else {
+ res.clear();
+ // For other maps, try to use the first() and next() interfaces
+ if (!this->first(key.get()))
+ return StatusTuple(0);
+
+ while (true) {
+ if (!this->lookup(key.get(), value.get()))
+ break;
+ r = key_to_string(key.get(), key_str);
+ if (r.code() != 0)
+ return r;
+
+ r = leaf_to_string(value.get(), value_str);
+ if (r.code() != 0)
+ return r;
+ res.emplace_back(key_str, value_str);
+ if (!this->next(key.get(), key.get()))
+ break;
+ }
+ }
+
+ return StatusTuple(0);
+}
+
size_t BPFTable::get_possible_cpu_count() { return get_possible_cpus().size(); }
BPFStackTable::BPFStackTable(const TableDesc& desc, bool use_debug_file,
diff --git a/src/cc/api/BPFTable.h b/src/cc/api/BPFTable.h
index ad3651b7..3a183f4f 100644
--- a/src/cc/api/BPFTable.h
+++ b/src/cc/api/BPFTable.h
@@ -104,6 +104,7 @@ class BPFTable : public BPFTableBase<void, void> {
StatusTuple remove_value(const std::string& key_str);
StatusTuple clear_table_non_atomic();
+ StatusTuple get_table_offline(std::vector<std::pair<std::string, std::string>> &res);
static size_t get_possible_cpu_count();
};