From 54e377d29b1e09968b8278c039fc3e146da9d962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Fri, 28 Sep 2018 10:40:49 -0500 Subject: Add support for shared tables (#1988) Currently tables can be created in two modes: 1. private to the ebpf program 2. shared among all the ebpf programs created by the same user-space process This commit extends bcc allowing to create a table that is shared among a set of specific ebpf programs. This is useful when creating network functions that are composed by more than 1 ebpf program, a map is shared among all the programs of the network function but isolated from different instances of that function. Signed-off-by: Mauricio Vasquez B --- src/cc/api/BPF.h | 5 +- src/cc/bpf_module.cc | 11 +++-- src/cc/bpf_module.h | 5 +- src/cc/export/helpers.h | 6 +++ src/cc/frontends/b/codegen_llvm.cc | 3 +- src/cc/frontends/b/codegen_llvm.h | 3 +- src/cc/frontends/b/loader.cc | 4 +- src/cc/frontends/b/loader.h | 2 +- src/cc/frontends/clang/b_frontend_action.cc | 24 +++++++-- src/cc/frontends/clang/b_frontend_action.h | 5 +- src/cc/frontends/clang/loader.cc | 12 +++-- src/cc/frontends/clang/loader.h | 5 +- tests/cc/CMakeLists.txt | 1 + tests/cc/test_shared_table.cc | 75 +++++++++++++++++++++++++++++ 14 files changed, 137 insertions(+), 24 deletions(-) create mode 100644 tests/cc/test_shared_table.cc diff --git a/src/cc/api/BPF.h b/src/cc/api/BPF.h index 8dd5f842..21fb42dd 100644 --- a/src/cc/api/BPF.h +++ b/src/cc/api/BPF.h @@ -47,8 +47,9 @@ class BPF { static const int BPF_MAX_STACK_DEPTH = 127; explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr, - bool rw_engine_enabled = true) - : flag_(flag), bpf_module_(new BPFModule(flag, ts, rw_engine_enabled)) {} + bool rw_engine_enabled = true, const std::string &maps_ns = "") + : flag_(flag), + bpf_module_(new BPFModule(flag, ts, rw_engine_enabled, maps_ns)) {} StatusTuple init(const std::string& bpf_program, const std::vector& cflags = {}, const std::vector& usdt = {}); diff --git a/src/cc/bpf_module.cc b/src/cc/bpf_module.cc index 93443409..a8174be7 100644 --- a/src/cc/bpf_module.cc +++ b/src/cc/bpf_module.cc @@ -99,12 +99,14 @@ class MyMemoryManager : public SectionMemoryManager { map> *sections_; }; -BPFModule::BPFModule(unsigned flags, TableStorage *ts, bool rw_engine_enabled) +BPFModule::BPFModule(unsigned flags, TableStorage *ts, bool rw_engine_enabled, + const std::string &maps_ns) : flags_(flags), rw_engine_enabled_(rw_engine_enabled), used_b_loader_(false), ctx_(new LLVMContext), id_(std::to_string((uintptr_t)this)), + maps_ns_(maps_ns), ts_(ts) { InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); @@ -473,7 +475,7 @@ unique_ptr BPFModule::finalize_rw(unique_ptr m) { int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) { ClangLoader clang_loader(&*ctx_, flags_); if (clang_loader.parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_, - *func_src_, mod_src_)) + *func_src_, mod_src_, maps_ns_)) return -1; return 0; } @@ -486,7 +488,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags int BPFModule::load_includes(const string &text) { ClangLoader clang_loader(&*ctx_, flags_); if (clang_loader.parse(&mod_, *ts_, text, true, nullptr, 0, "", *func_src_, - mod_src_)) + mod_src_, "")) return -1; return 0; } @@ -979,7 +981,8 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) { BLoader b_loader(flags_); used_b_loader_ = true; - if (int rc = b_loader.parse(&*mod_, filename, proto_filename, *ts_, id_)) + if (int rc = b_loader.parse(&*mod_, filename, proto_filename, *ts_, id_, + maps_ns_)) return rc; if (rw_engine_enabled_) { if (int rc = annotate()) diff --git a/src/cc/bpf_module.h b/src/cc/bpf_module.h index 184871d7..ff237a50 100644 --- a/src/cc/bpf_module.h +++ b/src/cc/bpf_module.h @@ -76,12 +76,14 @@ class BPFModule { const void *val); public: - BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true); + BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true, + const std::string &maps_ns = ""); ~BPFModule(); int load_b(const std::string &filename, const std::string &proto_filename); int load_c(const std::string &filename, const char *cflags[], int ncflags); int load_string(const std::string &text, const char *cflags[], int ncflags); std::string id() const { return id_; } + std::string maps_ns() const { return maps_ns_; } size_t num_functions() const; uint8_t * function_start(size_t id) const; uint8_t * function_start(const std::string &name) const; @@ -137,6 +139,7 @@ class BPFModule { std::map readers_; std::map writers_; std::string id_; + std::string maps_ns_; std::string mod_src_; std::map src_dbg_fmap_; TableStorage *ts_; diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h index 778a13f4..bcca4c9e 100755 --- a/src/cc/export/helpers.h +++ b/src/cc/export/helpers.h @@ -66,6 +66,12 @@ BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries); \ __attribute__((section("maps/export"))) \ struct _name##_table_t __##_name +// define a table that is shared accross the programs in the same namespace +#define BPF_TABLE_SHARED(_table_type, _key_type, _leaf_type, _name, _max_entries) \ +BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries); \ +__attribute__((section("maps/shared"))) \ +struct _name##_table_t __##_name + // Identifier for current CPU used in perf_submit and perf_read // Prefer BPF_F_CURRENT_CPU flag, falls back to call helper for older kernel // Can be overridden from BCC diff --git a/src/cc/frontends/b/codegen_llvm.cc b/src/cc/frontends/b/codegen_llvm.cc index 54645846..dc9651bf 100644 --- a/src/cc/frontends/b/codegen_llvm.cc +++ b/src/cc/frontends/b/codegen_llvm.cc @@ -1230,7 +1230,8 @@ StatusTuple CodegenLLVM::visit_func_decl_stmt_node(FuncDeclStmtNode *n) { return StatusTuple(0); } -StatusTuple CodegenLLVM::visit(Node *root, TableStorage &ts, const string &id) { +StatusTuple CodegenLLVM::visit(Node *root, TableStorage &ts, const string &id, + const string &maps_ns) { scopes_->set_current(scopes_->top_state()); scopes_->set_current(scopes_->top_var()); diff --git a/src/cc/frontends/b/codegen_llvm.h b/src/cc/frontends/b/codegen_llvm.h index 1e7d4300..c2947f74 100644 --- a/src/cc/frontends/b/codegen_llvm.h +++ b/src/cc/frontends/b/codegen_llvm.h @@ -65,7 +65,8 @@ class CodegenLLVM : public Visitor { EXPAND_NODES(VISIT) #undef VISIT - STATUS_RETURN visit(Node *n, TableStorage &ts, const std::string &id); + STATUS_RETURN visit(Node *n, TableStorage &ts, const std::string &id, + const std::string &maps_ns); int get_table_fd(const std::string &name) const; diff --git a/src/cc/frontends/b/loader.cc b/src/cc/frontends/b/loader.cc index 79332242..8d7f8a2f 100644 --- a/src/cc/frontends/b/loader.cc +++ b/src/cc/frontends/b/loader.cc @@ -33,7 +33,7 @@ BLoader::~BLoader() { } int BLoader::parse(llvm::Module *mod, const string &filename, const string &proto_filename, - TableStorage &ts, const string &id) { + TableStorage &ts, const string &id, const std::string &maps_ns) { int rc; proto_parser_ = make_unique(proto_filename); @@ -61,7 +61,7 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot } codegen_ = ebpf::make_unique(mod, parser_->scopes_.get(), proto_parser_->scopes_.get()); - ret = codegen_->visit(parser_->root_node_, ts, id); + ret = codegen_->visit(parser_->root_node_, ts, id, maps_ns); if (ret.code() != 0 || ret.msg().size()) { fprintf(stderr, "Codegen error @line=%d: %s\n", ret.code(), ret.msg().c_str()); return ret.code(); diff --git a/src/cc/frontends/b/loader.h b/src/cc/frontends/b/loader.h index 93ca77d2..6330d5c2 100644 --- a/src/cc/frontends/b/loader.h +++ b/src/cc/frontends/b/loader.h @@ -38,7 +38,7 @@ class BLoader { explicit BLoader(unsigned flags); ~BLoader(); int parse(llvm::Module *mod, const std::string &filename, const std::string &proto_filename, - TableStorage &ts, const std::string &id); + TableStorage &ts, const std::string &id, const std::string &maps_ns); private: unsigned flags_; diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc index 59ce9fac..12095e62 100644 --- a/src/cc/frontends/clang/b_frontend_action.cc +++ b/src/cc/frontends/clang/b_frontend_action.cc @@ -1097,6 +1097,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) { TableStorage::iterator table_it; table.name = Decl->getName(); Path local_path({fe_.id(), table.name}); + Path maps_ns_path({"ns", fe_.maps_ns(), table.name}); Path global_path({table.name}); QualType key_type, leaf_type; @@ -1182,9 +1183,11 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) { } else if (A->getName() == "maps/cpumap") { map_type = BPF_MAP_TYPE_CPUMAP; } else if (A->getName() == "maps/extern") { - if (!fe_.table_storage().Find(global_path, table_it)) { - error(GET_BEGINLOC(Decl), "reference to undefined table"); - return false; + if (!fe_.table_storage().Find(maps_ns_path, table_it)) { + if (!fe_.table_storage().Find(global_path, table_it)) { + error(GET_BEGINLOC(Decl), "reference to undefined table"); + return false; + } } table = table_it->second.dup(); table.is_extern = true; @@ -1199,6 +1202,17 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) { } fe_.table_storage().Insert(global_path, table_it->second.dup()); return true; + } else if(A->getName() == "maps/shared") { + if (table.name.substr(0, 2) == "__") + table.name = table.name.substr(2); + Path local_path({fe_.id(), table.name}); + Path maps_ns_path({"ns", fe_.maps_ns(), table.name}); + if (!fe_.table_storage().Find(local_path, table_it)) { + error(GET_BEGINLOC(Decl), "reference to undefined table"); + return false; + } + fe_.table_storage().Insert(maps_ns_path, table_it->second.dup()); + return true; } if (!table.is_extern) { @@ -1328,11 +1342,13 @@ void BTypeConsumer::HandleTranslationUnit(ASTContext &Context) { BFrontendAction::BFrontendAction(llvm::raw_ostream &os, unsigned flags, TableStorage &ts, const std::string &id, const std::string &main_path, - FuncSource &func_src, std::string &mod_src) + FuncSource &func_src, std::string &mod_src, + const std::string &maps_ns) : os_(os), flags_(flags), ts_(ts), id_(id), + maps_ns_(maps_ns), rewriter_(new Rewriter), main_path_(main_path), func_src_(func_src), diff --git a/src/cc/frontends/clang/b_frontend_action.h b/src/cc/frontends/clang/b_frontend_action.h index 703f80da..4559d114 100644 --- a/src/cc/frontends/clang/b_frontend_action.h +++ b/src/cc/frontends/clang/b_frontend_action.h @@ -152,7 +152,8 @@ class BFrontendAction : public clang::ASTFrontendAction { // should be written. BFrontendAction(llvm::raw_ostream &os, unsigned flags, TableStorage &ts, const std::string &id, const std::string &main_path, - FuncSource &func_src, std::string &mod_src); + FuncSource &func_src, std::string &mod_src, + const std::string &maps_ns); // Called by clang when the AST has been completed, here the output stream // will be flushed. @@ -164,6 +165,7 @@ class BFrontendAction : public clang::ASTFrontendAction { clang::Rewriter &rewriter() const { return *rewriter_; } TableStorage &table_storage() const { return ts_; } std::string id() const { return id_; } + std::string maps_ns() const { return maps_ns_; } bool is_rewritable_ext_func(clang::FunctionDecl *D); void DoMiscWorkAround(); @@ -172,6 +174,7 @@ class BFrontendAction : public clang::ASTFrontendAction { unsigned flags_; TableStorage &ts_; std::string id_; + std::string maps_ns_; std::unique_ptr rewriter_; friend class BTypeVisitor; std::map func_range_; diff --git a/src/cc/frontends/clang/loader.cc b/src/cc/frontends/clang/loader.cc index f67642d1..3596bd64 100755 --- a/src/cc/frontends/clang/loader.cc +++ b/src/cc/frontends/clang/loader.cc @@ -107,7 +107,8 @@ std::pair get_kernel_path_info(const string kdir) int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, const string &file, bool in_memory, const char *cflags[], int ncflags, const std::string &id, FuncSource &func_src, - std::string &mod_src) { + std::string &mod_src, + const std::string &maps_ns) { string main_path = "/virtual/main.c"; unique_ptr main_buf; struct utsname un; @@ -200,7 +201,7 @@ int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, #endif if (do_compile(mod, ts, in_memory, flags_cstr, flags_cstr_rem, main_path, - main_buf, id, func_src, mod_src, true)) { + main_buf, id, func_src, mod_src, true, maps_ns)) { #if BCC_BACKUP_COMPILE != 1 return -1; #else @@ -211,7 +212,7 @@ int ClangLoader::parse(unique_ptr *mod, TableStorage &ts, func_src.clear(); mod_src.clear(); if (do_compile(mod, ts, in_memory, flags_cstr, flags_cstr_rem, main_path, - main_buf, id, func_src, mod_src, false)) + main_buf, id, func_src, mod_src, false, maps_ns)) return -1; #endif } @@ -257,7 +258,8 @@ int ClangLoader::do_compile(unique_ptr *mod, TableStorage &ts, const std::string &main_path, const unique_ptr &main_buf, const std::string &id, FuncSource &func_src, - std::string &mod_src, bool use_internal_bpfh) { + std::string &mod_src, bool use_internal_bpfh, + const std::string &maps_ns) { using namespace clang; vector flags_cstr = flags_cstr_in; @@ -371,7 +373,7 @@ int ClangLoader::do_compile(unique_ptr *mod, TableStorage &ts, // capture the rewritten c file string out_str1; llvm::raw_string_ostream os1(out_str1); - BFrontendAction bact(os1, flags_, ts, id, main_path, func_src, mod_src); + BFrontendAction bact(os1, flags_, ts, id, main_path, func_src, mod_src, maps_ns); if (!compiler1.ExecuteAction(bact)) return -1; unique_ptr out_buf1 = llvm::MemoryBuffer::getMemBuffer(out_str1); diff --git a/src/cc/frontends/clang/loader.h b/src/cc/frontends/clang/loader.h index b348d6bb..1aeb6523 100644 --- a/src/cc/frontends/clang/loader.h +++ b/src/cc/frontends/clang/loader.h @@ -54,7 +54,7 @@ class ClangLoader { int parse(std::unique_ptr *mod, TableStorage &ts, const std::string &file, bool in_memory, const char *cflags[], int ncflags, const std::string &id, FuncSource &func_src, - std::string &mod_src); + std::string &mod_src, const std::string &maps_ns); private: int do_compile(std::unique_ptr *mod, TableStorage &ts, @@ -63,7 +63,8 @@ class ClangLoader { const std::string &main_path, const std::unique_ptr &main_buf, const std::string &id, FuncSource &func_src, - std::string &mod_src, bool use_internal_bpfh); + std::string &mod_src, bool use_internal_bpfh, + const std::string &maps_ns); private: std::map> remapped_headers_; diff --git a/tests/cc/CMakeLists.txt b/tests/cc/CMakeLists.txt index 89ee55f8..335b428d 100644 --- a/tests/cc/CMakeLists.txt +++ b/tests/cc/CMakeLists.txt @@ -18,6 +18,7 @@ add_executable(test_libbcc test_hash_table.cc test_perf_event.cc test_prog_table.cc + test_shared_table.cc test_usdt_args.cc test_usdt_probes.cc utils.cc) diff --git a/tests/cc/test_shared_table.cc b/tests/cc/test_shared_table.cc new file mode 100644 index 00000000..a638cb50 --- /dev/null +++ b/tests/cc/test_shared_table.cc @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2018 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://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" + +const std::string BPF_PROGRAM1 = R"( +BPF_TABLE_SHARED("array", int, int, mysharedtable, 1024); +)"; + +const std::string BPF_PROGRAM2 = R"( +BPF_TABLE("extern", int, int, mysharedtable, 1024); +)"; + +TEST_CASE("test shared table", "[shared_table]") { + // deploy 4 ebpf programs: _ns1_a and _ns1_b are in ns1, _ns2_a and _ns2_b in ns2 + ebpf::BPF bpf_ns1_a(0, nullptr, false, "ns1"); + ebpf::BPF bpf_ns1_b(0, nullptr, false, "ns1"); + ebpf::BPF bpf_ns2_a(0, nullptr, false, "ns2"); + ebpf::BPF bpf_ns2_b(0, nullptr, false, "ns2"); + + ebpf::StatusTuple res(0); + + res = bpf_ns1_a.init(BPF_PROGRAM1); + REQUIRE(res.code() == 0); + + res = bpf_ns1_b.init(BPF_PROGRAM2); + REQUIRE(res.code() == 0); + + res = bpf_ns2_a.init(BPF_PROGRAM1); + REQUIRE(res.code() == 0); + + res = bpf_ns2_b.init(BPF_PROGRAM2); + REQUIRE(res.code() == 0); + + // get references to all tables + ebpf::BPFArrayTable t_ns1_a = bpf_ns1_a.get_array_table("mysharedtable"); + ebpf::BPFArrayTable t_ns1_b = bpf_ns1_b.get_array_table("mysharedtable"); + ebpf::BPFArrayTable t_ns2_a = bpf_ns2_a.get_array_table("mysharedtable"); + ebpf::BPFArrayTable t_ns2_b = bpf_ns2_b.get_array_table("mysharedtable"); + + // test that tables within the same ns are shared + int v1, v2, v3; + res = t_ns1_a.update_value(13, 42); + REQUIRE(res.code() == 0); + + res = t_ns1_b.get_value(13, v1); + REQUIRE(res.code() == 0); + REQUIRE(v1 == 42); + + // test that tables are isolated within different ns + res = t_ns2_a.update_value(13, 69); + REQUIRE(res.code() == 0); + + res = t_ns2_b.get_value(13, v2); + REQUIRE(res.code() == 0); + REQUIRE(v2 == 69); + + res = t_ns1_b.get_value(13, v3); + REQUIRE(res.code() == 0); + REQUIRE(v3 == 42); // value should still be 42 +} -- cgit v1.2.3