Commit 54e377d2 authored by Mauricio Vásquez's avatar Mauricio Vásquez Committed by yonghong-song

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: default avatarMauricio Vasquez B <mauricio.vasquez@polito.it>
parent f138fea5
......@@ -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<std::string>& cflags = {},
const std::vector<USDT>& usdt = {});
......
......@@ -99,12 +99,14 @@ class MyMemoryManager : public SectionMemoryManager {
map<string, tuple<uint8_t *, uintptr_t>> *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<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> 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())
......
......@@ -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<llvm::Type *, std::string> readers_;
std::map<llvm::Type *, std::string> writers_;
std::string id_;
std::string maps_ns_;
std::string mod_src_;
std::map<std::string, std::string> src_dbg_fmap_;
TableStorage *ts_;
......
......@@ -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
......
......@@ -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());
......
......@@ -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;
......
......@@ -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<ebpf::cc::Parser>(proto_filename);
......@@ -61,7 +61,7 @@ int BLoader::parse(llvm::Module *mod, const string &filename, const string &prot
}
codegen_ = ebpf::make_unique<ebpf::cc::CodegenLLVM>(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();
......
......@@ -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_;
......
......@@ -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),
......
......@@ -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<clang::Rewriter> rewriter_;
friend class BTypeVisitor;
std::map<std::string, clang::SourceRange> func_range_;
......
......@@ -107,7 +107,8 @@ std::pair<bool, string> get_kernel_path_info(const string kdir)
int ClangLoader::parse(unique_ptr<llvm::Module> *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<llvm::MemoryBuffer> main_buf;
struct utsname un;
......@@ -200,7 +201,7 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *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<llvm::Module> *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<llvm::Module> *mod, TableStorage &ts,
const std::string &main_path,
const unique_ptr<llvm::MemoryBuffer> &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<const char *> flags_cstr = flags_cstr_in;
......@@ -371,7 +373,7 @@ int ClangLoader::do_compile(unique_ptr<llvm::Module> *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<llvm::MemoryBuffer> out_buf1 = llvm::MemoryBuffer::getMemBuffer(out_str1);
......
......@@ -54,7 +54,7 @@ class ClangLoader {
int parse(std::unique_ptr<llvm::Module> *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<llvm::Module> *mod, TableStorage &ts,
......@@ -63,7 +63,8 @@ class ClangLoader {
const std::string &main_path,
const std::unique_ptr<llvm::MemoryBuffer> &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<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_headers_;
......
......@@ -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)
......
/*
* 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<int> t_ns1_a = bpf_ns1_a.get_array_table<int>("mysharedtable");
ebpf::BPFArrayTable<int> t_ns1_b = bpf_ns1_b.get_array_table<int>("mysharedtable");
ebpf::BPFArrayTable<int> t_ns2_a = bpf_ns2_a.get_array_table<int>("mysharedtable");
ebpf::BPFArrayTable<int> t_ns2_b = bpf_ns2_b.get_array_table<int>("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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment