Skip to content

Commit

Permalink
Merge pull request iovisor#127 from iovisor/bblanco_dev
Browse files Browse the repository at this point in the history
Changes to support map entry read/write in fuse
  • Loading branch information
4ast committed Aug 12, 2015
2 parents f04003e + 2582ecf commit 8cea2e9
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 131 deletions.
31 changes: 18 additions & 13 deletions src/cc/bpf_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ size_t bpf_num_tables(void *program) {
return mod->num_tables();
}

size_t bpf_table_id(void *program, const char *table_name) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return ~0ull;
return mod->table_id(table_name);
}

int bpf_table_fd(void *program, const char *table_name) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return -1;
Expand Down Expand Up @@ -170,27 +176,26 @@ size_t bpf_table_leaf_size_id(void *program, size_t id) {
return mod->table_leaf_size(id);
}

int bpf_table_update(void *program, const char *table_name, const char *key, const char *leaf) {
int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return 0;
return mod->table_update(table_name, key, leaf);
if (!mod) return -1;
return mod->table_key_printf(id, buf, buflen, key);
}

int bpf_table_update_id(void *program, size_t id, const char *key, const char *leaf) {
int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return 0;
return mod->table_update(id, key, leaf);
if (!mod) return -1;
return mod->table_leaf_printf(id, buf, buflen, leaf);
}

int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key) {
int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return 0;
return mod->table_key_printf(id, buf, buflen, key);
if (!mod) return -1;
return mod->table_key_scanf(id, buf, key);
}
int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf) {
int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return 0;
return mod->table_key_printf(id, buf, buflen, leaf);
if (!mod) return -1;
return mod->table_leaf_scanf(id, buf, leaf);
}

}
7 changes: 3 additions & 4 deletions src/cc/bpf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void * bpf_function_start(void *program, const char *name);
size_t bpf_function_size_id(void *program, size_t id);
size_t bpf_function_size(void *program, const char *name);
size_t bpf_num_tables(void *program);
size_t bpf_table_id(void *program, const char *table_name);
int bpf_table_fd(void *program, const char *table_name);
int bpf_table_fd_id(void *program, size_t id);
const char * bpf_table_name(void *program, size_t id);
Expand All @@ -50,10 +51,8 @@ size_t bpf_table_leaf_size(void *program, const char *table_name);
size_t bpf_table_leaf_size_id(void *program, size_t id);
int bpf_table_key_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *key);
int bpf_table_leaf_snprintf(void *program, size_t id, char *buf, size_t buflen, const void *leaf);
//int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key);
//int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf);
int bpf_table_update(void *program, const char *table_name, const char *key, const char *leaf);
int bpf_table_update_id(void *program, size_t id, const char *key, const char *leaf);
int bpf_table_key_sscanf(void *program, size_t id, const char *buf, void *key);
int bpf_table_leaf_sscanf(void *program, size_t id, const char *buf, void *leaf);

#ifdef __cplusplus
}
Expand Down
192 changes: 96 additions & 96 deletions src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <linux/bpf.h>

#include <llvm/ADT/STLExtras.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/IRReader/IRReader.h>
Expand Down Expand Up @@ -64,6 +63,9 @@ using std::unique_ptr;
using std::vector;
using namespace llvm;

typedef int (* sscanf_fn) (const char *, void *);
typedef int (* snprintf_fn) (char *, size_t, const void *);

const string BPFModule::FN_PREFIX = BPF_FN_PREFIX;

// Snooping class to remember the sections as the JIT creates them
Expand Down Expand Up @@ -147,9 +149,9 @@ static void parse_type(IRBuilder<> &B, vector<Value *> *args, string *fmt,
else if (it->getBitWidth() <= 16)
*fmt += "%h";
else if (it->getBitWidth() <= 32)
*fmt += "%l";
*fmt += "%";
else
*fmt += "%ll";
*fmt += "%l";
if (is_writer)
*fmt += "x";
else
Expand All @@ -171,15 +173,11 @@ Function * BPFModule::make_reader(Module *mod, Type *type) {

IRBuilder<> B(*ctx_);

// The JIT currently supports a limited number of function prototypes, use the
// int (*) (int, char **, const char **) version
vector<Type *> fn_args({B.getInt32Ty(), B.getInt8PtrTy(), PointerType::getUnqual(type)});
vector<Type *> fn_args({B.getInt8PtrTy(), PointerType::getUnqual(type)});
FunctionType *fn_type = FunctionType::get(B.getInt32Ty(), fn_args, /*isVarArg=*/false);
Function *fn = Function::Create(fn_type, GlobalValue::ExternalLinkage,
"reader" + std::to_string(readers_.size()), mod);
auto arg_it = fn->arg_begin();
Argument *arg_argc = arg_it++;
arg_argc->setName("argc");
Argument *arg_in = arg_it++;
arg_in->setName("in");
Argument *arg_out = arg_it++;
Expand All @@ -197,6 +195,9 @@ Function * BPFModule::make_reader(Module *mod, Type *type) {

args[1] = B.CreateInBoundsGEP(fmt_gvar, vector<Value *>({B.getInt64(0), B.getInt64(0)}));

if (0)
debug_printf(mod, B, "%p %p\n", vector<Value *>({arg_in, arg_out}));

vector<Type *> sscanf_fn_args({B.getInt8PtrTy(), B.getInt8PtrTy()});
FunctionType *sscanf_fn_type = FunctionType::get(B.getInt32Ty(), sscanf_fn_args, /*isVarArg=*/true);
Function *sscanf_fn = mod->getFunction("sscanf");
Expand Down Expand Up @@ -234,17 +235,15 @@ Function * BPFModule::make_writer(Module *mod, Type *type) {

IRBuilder<> B(*ctx_);

// The JIT currently supports a limited number of function prototypes, use the
// int (*) (int, char **, const char **) version
vector<Type *> fn_args({B.getInt32Ty(), B.getInt8PtrTy(), PointerType::getUnqual(type)});
vector<Type *> fn_args({B.getInt8PtrTy(), B.getInt64Ty(), PointerType::getUnqual(type)});
FunctionType *fn_type = FunctionType::get(B.getInt32Ty(), fn_args, /*isVarArg=*/false);
Function *fn = Function::Create(fn_type, GlobalValue::ExternalLinkage,
"writer" + std::to_string(writers_.size()), mod);
auto arg_it = fn->arg_begin();
Argument *arg_len = arg_it++;
arg_len->setName("len");
Argument *arg_out = arg_it++;
arg_out->setName("out");
Argument *arg_len = arg_it++;
arg_len->setName("len");
Argument *arg_in = arg_it++;
arg_in->setName("in");

Expand Down Expand Up @@ -331,18 +330,18 @@ int BPFModule::annotate() {
if (st->getNumElements() < 2) continue;
Type *key_type = st->elements()[0];
Type *leaf_type = st->elements()[1];
table.key_reader = make_reader(&*m, key_type);
if (!table.key_reader)
errs() << "Failed to compile reader for " << *key_type << "\n";
table.leaf_reader = make_reader(&*m, leaf_type);
if (!table.leaf_reader)
errs() << "Failed to compile reader for " << *leaf_type << "\n";
table.key_writer = make_writer(&*m, key_type);
if (!table.key_writer)
errs() << "Failed to compile writer for " << *key_type << "\n";
table.leaf_writer = make_writer(&*m, leaf_type);
if (!table.leaf_writer)
errs() << "Failed to compile writer for " << *leaf_type << "\n";
table.key_sscanf = make_reader(&*m, key_type);
if (!table.key_sscanf)
errs() << "Failed to compile sscanf for " << *key_type << "\n";
table.leaf_sscanf = make_reader(&*m, leaf_type);
if (!table.leaf_sscanf)
errs() << "Failed to compile sscanf for " << *leaf_type << "\n";
table.key_snprintf = make_writer(&*m, key_type);
if (!table.key_snprintf)
errs() << "Failed to compile snprintf for " << *key_type << "\n";
table.leaf_snprintf = make_writer(&*m, leaf_type);
if (!table.leaf_snprintf)
errs() << "Failed to compile snprintf for " << *leaf_type << "\n";
}
}
}
Expand Down Expand Up @@ -474,10 +473,14 @@ size_t BPFModule::num_tables() const {
return tables_->size();
}

int BPFModule::table_fd(const string &name) const {
size_t BPFModule::table_id(const string &name) const {
auto it = table_names_.find(name);
if (it == table_names_.end()) return -1;
return table_fd(it->second);
if (it == table_names_.end()) return ~0ull;
return it->second;
}

int BPFModule::table_fd(const string &name) const {
return table_fd(table_id(name));
}

int BPFModule::table_fd(size_t id) const {
Expand All @@ -497,9 +500,7 @@ const char * BPFModule::table_key_desc(size_t id) const {
}

const char * BPFModule::table_key_desc(const string &name) const {
auto it = table_names_.find(name);
if (it == table_names_.end()) return nullptr;
return table_key_desc(it->second);
return table_key_desc(table_id(name));
}

const char * BPFModule::table_leaf_desc(size_t id) const {
Expand All @@ -509,61 +510,22 @@ const char * BPFModule::table_leaf_desc(size_t id) const {
}

const char * BPFModule::table_leaf_desc(const string &name) const {
auto it = table_names_.find(name);
if (it == table_names_.end()) return nullptr;
return table_leaf_desc(it->second);
return table_leaf_desc(table_id(name));
}
size_t BPFModule::table_key_size(size_t id) const {
if (id >= tables_->size()) return 0;
return (*tables_)[id].key_size;
}
size_t BPFModule::table_key_size(const string &name) const {
auto it = table_names_.find(name);
if (it == table_names_.end()) return 0;
return table_key_size(it->second);
return table_key_size(table_id(name));
}

size_t BPFModule::table_leaf_size(size_t id) const {
if (id >= tables_->size()) return 0;
return (*tables_)[id].leaf_size;
}
size_t BPFModule::table_leaf_size(const string &name) const {
auto it = table_names_.find(name);
if (it == table_names_.end()) return 0;
return table_leaf_size(it->second);
}

int BPFModule::table_update(const string &name, const char *key_str, const char *leaf_str) {
auto it = table_names_.find(name);
if (it == table_names_.end()) return 0;
return table_update(it->second, key_str, leaf_str);
}

int BPFModule::table_update(size_t id, const char *key_str, const char *leaf_str) {
if (id >= tables_->size()) return -1;

const TableDesc &desc = (*tables_)[id];
if (desc.fd < 0) return -1;

if (!rw_engine_ || !desc.key_reader || !desc.leaf_reader) {
fprintf(stderr, "Table sscanf not available\n");
return -1;
}

unique_ptr<uint8_t[]> key(new uint8_t[desc.key_size]);
unique_ptr<uint8_t[]> leaf(new uint8_t[desc.leaf_size]);
GenericValue rc;
rc = rw_engine_->runFunction(desc.key_reader, vector<GenericValue>({GenericValue(),
GenericValue((void *)key_str),
GenericValue((void *)key.get())}));
if (rc.IntVal != 0)
return -1;
rc = rw_engine_->runFunction(desc.leaf_reader, vector<GenericValue>({GenericValue(),
GenericValue((void *)leaf_str),
GenericValue((void *)leaf.get())}));
if (rc.IntVal != 0)
return -1;
return bpf_update_elem(desc.fd, key.get(), leaf.get(), 0);
return table_leaf_size(table_id(name));
}

struct TableIterator {
Expand All @@ -576,52 +538,90 @@ struct TableIterator {
};

int BPFModule::table_key_printf(size_t id, char *buf, size_t buflen, const void *key) {
if (id >= tables_->size()) {
fprintf(stderr, "table id %zu out of range\n", id);
if (id >= tables_->size()) return -1;
const TableDesc &desc = (*tables_)[id];
if (!desc.key_snprintf) {
fprintf(stderr, "Key snprintf not available\n");
return -1;
}
snprintf_fn fn = (snprintf_fn)rw_engine_->getPointerToFunction(desc.key_snprintf);
if (!fn) {
fprintf(stderr, "Key snprintf not available in JIT Engine\n");
return -1;
}
int rc = (*fn)(buf, buflen, key);
if (rc < 0) {
perror("snprintf");
return -1;
}
if ((size_t)rc >= buflen) {
fprintf(stderr, "snprintf ran out of buffer space\n");
return -1;
}
return 0;
}

int BPFModule::table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf) {
if (id >= tables_->size()) return -1;
const TableDesc &desc = (*tables_)[id];
if (!desc.key_writer) {
fprintf(stderr, "table snprintf not implemented for %s key\n", desc.name.c_str());
if (!desc.leaf_snprintf) {
fprintf(stderr, "Key snprintf not available\n");
return -1;
}
snprintf_fn fn = (snprintf_fn)rw_engine_->getPointerToFunction(desc.leaf_snprintf);
if (!fn) {
fprintf(stderr, "Leaf snprintf not available in JIT Engine\n");
return -1;
}
GenericValue gv_buflen;
gv_buflen.IntVal = APInt(32, buflen, true);
vector<GenericValue> args({gv_buflen, GenericValue((void *)buf), GenericValue((void *)key)});
GenericValue rc = rw_engine_->runFunction(desc.key_writer, args);
if (rc.IntVal.isNegative()) {
int rc = (*fn)(buf, buflen, leaf);
if (rc < 0) {
perror("snprintf");
return -1;
}
if (rc.IntVal.sge(buflen)) {
if ((size_t)rc >= buflen) {
fprintf(stderr, "snprintf ran out of buffer space\n");
return -1;
}
return 0;
}

int BPFModule::table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf) {
if (id >= tables_->size()) {
fprintf(stderr, "table id %zu out of range\n", id);
int BPFModule::table_key_scanf(size_t id, const char *key_str, void *key) {
if (id >= tables_->size()) return -1;
const TableDesc &desc = (*tables_)[id];
if (!desc.key_sscanf) {
fprintf(stderr, "Key sscanf not available\n");
return -1;
}

sscanf_fn fn = (sscanf_fn)rw_engine_->getPointerToFunction(desc.key_sscanf);
if (!fn) {
fprintf(stderr, "Key sscanf not available in JIT Engine\n");
return -1;
}
int rc = (*fn)(key_str, key);
if (rc != 0) {
perror("sscanf");
return -1;
}
return 0;
}

int BPFModule::table_leaf_scanf(size_t id, const char *leaf_str, void *leaf) {
if (id >= tables_->size()) return -1;
const TableDesc &desc = (*tables_)[id];
if (!desc.leaf_writer) {
fprintf(stderr, "table snprintf not implemented for %s leaf\n", desc.name.c_str());
if (!desc.leaf_sscanf) {
fprintf(stderr, "Key sscanf not available\n");
return -1;
}
GenericValue gv_buflen;
gv_buflen.IntVal = buflen;
vector<GenericValue> args({gv_buflen, GenericValue((void *)buf), GenericValue((void *)leaf)});
GenericValue rc = rw_engine_->runFunction(desc.leaf_writer, args);
if (rc.IntVal.isNegative()) {
perror("snprintf");

sscanf_fn fn = (sscanf_fn)rw_engine_->getPointerToFunction(desc.leaf_sscanf);
if (!fn) {
fprintf(stderr, "Leaf sscanf not available in JIT Engine\n");
return -1;
}
if (rc.IntVal.sge(buflen)) {
fprintf(stderr, "snprintf ran out of buffer space\n");
int rc = (*fn)(leaf_str, leaf);
if (rc != 0) {
perror("sscanf");
return -1;
}
return 0;
Expand Down
Loading

0 comments on commit 8cea2e9

Please sign in to comment.