aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMauricio Vásquez <mauriciovasquezbernal@gmail.com>2018-09-28 10:40:49 -0500
committeryonghong-song <ys114321@gmail.com>2018-09-28 08:40:49 -0700
commit54e377d29b1e09968b8278c039fc3e146da9d962 (patch)
tree15c460e2182140d02d8f235d61675b8bebd27f5d /src
parentf138fea5a9ab279b7347fa6acfd2f53777068b27 (diff)
downloadbcc-54e377d29b1e09968b8278c039fc3e146da9d962.tar.gz
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 <mauricio.vasquez@polito.it>
Diffstat (limited to 'src')
-rw-r--r--src/cc/api/BPF.h5
-rw-r--r--src/cc/bpf_module.cc11
-rw-r--r--src/cc/bpf_module.h5
-rwxr-xr-xsrc/cc/export/helpers.h6
-rw-r--r--src/cc/frontends/b/codegen_llvm.cc3
-rw-r--r--src/cc/frontends/b/codegen_llvm.h3
-rw-r--r--src/cc/frontends/b/loader.cc4
-rw-r--r--src/cc/frontends/b/loader.h2
-rw-r--r--src/cc/frontends/clang/b_frontend_action.cc24
-rw-r--r--src/cc/frontends/clang/b_frontend_action.h5
-rwxr-xr-xsrc/cc/frontends/clang/loader.cc12
-rw-r--r--src/cc/frontends/clang/loader.h5
12 files changed, 61 insertions, 24 deletions
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<std::string>& cflags = {},
const std::vector<USDT>& 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<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())
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<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_;
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<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();
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<clang::Rewriter> rewriter_;
friend class BTypeVisitor;
std::map<std::string, clang::SourceRange> 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<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);
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<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_;