Skip to content

Commit

Permalink
Merge pull request iovisor#11 from s41m0n/s41m0n/queue_support
Browse files Browse the repository at this point in the history
Queue and Stack data structure support
  • Loading branch information
frisso authored May 20, 2020
2 parents b564d48 + e7b926e commit 679c5e9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 38 deletions.
114 changes: 77 additions & 37 deletions src/cc/export/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,22 @@ R"********(
#define BCC_SEC(NAME) __attribute__((section(NAME), used))

// Associate map with its key/value types
#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
struct ____btf_map_##name { \
type_key key; \
type_val value; \
}; \
struct ____btf_map_##name \
__attribute__ ((section(".maps." #name), used)) \
#define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val) \
struct ____btf_map_##name { \
type_key key; \
type_val value; \
}; \
struct ____btf_map_##name \
__attribute__ ((section(".maps." #name), used)) \
____btf_map_##name = { }

// Associate map with its key/value types for QUEUE/STACK map types
#define BPF_ANNOTATE_KV_PAIR_QUEUESTACK(name, type_val) \
struct ____btf_map_##name { \
type_val value; \
}; \
struct ____btf_map_##name \
__attribute__ ((section(".maps." #name), used)) \
____btf_map_##name = { }

// Changes to the macro require changes in BFrontendAction classes
Expand All @@ -100,6 +109,37 @@ __attribute__((section("maps/" _table_type))) \
struct _name##_table_t _name = { .flags = (_flags), .max_entries = (_max_entries) }; \
BPF_ANNOTATE_KV_PAIR(_name, _key_type, _leaf_type)

// Changes to the macro require changes in BFrontendAction classes
#define BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags) \
struct _name##_table_t { \
_leaf_type leaf; \
int * (*peek) (_leaf_type *); \
int * (*pop) (_leaf_type *); \
int * (*push) (_leaf_type *, u64); \
u32 max_entries; \
int flags; \
}; \
__attribute__((section("maps/" _table_type))) \
struct _name##_table_t _name = { .flags = (_flags), .max_entries = (_max_entries) }; \
BPF_ANNOTATE_KV_PAIR_QUEUESTACK(_name, _leaf_type)

// define queue with 3 parameters (_type=queue/stack automatically) and default flags to 0
#define BPF_QUEUE_STACK3(_type, _name, _leaf_type, _max_entries) \
BPF_QUEUESTACK(_type, _name, _leaf_type, _max_entries, 0)

// define queue with 4 parameters (_type=queue/stack automatically)
#define BPF_QUEUE_STACK4(_type, _name, _leaf_type, _max_entries, _flags) \
BPF_QUEUESTACK(_type, _name, _leaf_type, _max_entries, _flags)

// helper for default-variable macro function
#define BPF_QUEUE_STACKX(_1, _2, _3, _4, NAME, ...) NAME

#define BPF_QUEUE(...) \
BPF_QUEUE_STACKX(__VA_ARGS__, BPF_QUEUE_STACK4, BPF_QUEUE_STACK3)("queue", __VA_ARGS__)

#define BPF_STACK(...) \
BPF_QUEUE_STACKX(__VA_ARGS__, BPF_QUEUE_STACK4, BPF_QUEUE_STACK3)("stack", __VA_ARGS__)

#define BPF_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries) \
BPF_F_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries, 0)

Expand Down Expand Up @@ -915,15 +955,15 @@ int bpf_usdt_readarg_p(int argc, struct pt_regs *ctx, void *buf, u64 len) asm("l
#endif

#if defined(bpf_target_powerpc)
#define PT_REGS_PARM1(ctx) ((ctx)->gpr[3])
#define PT_REGS_PARM2(ctx) ((ctx)->gpr[4])
#define PT_REGS_PARM3(ctx) ((ctx)->gpr[5])
#define PT_REGS_PARM4(ctx) ((ctx)->gpr[6])
#define PT_REGS_PARM5(ctx) ((ctx)->gpr[7])
#define PT_REGS_PARM6(ctx) ((ctx)->gpr[8])
#define PT_REGS_RC(ctx) ((ctx)->gpr[3])
#define PT_REGS_IP(ctx) ((ctx)->nip)
#define PT_REGS_SP(ctx) ((ctx)->gpr[1])
#define PT_REGS_PARM1(ctx) ((ctx)->gpr[3])
#define PT_REGS_PARM2(ctx) ((ctx)->gpr[4])
#define PT_REGS_PARM3(ctx) ((ctx)->gpr[5])
#define PT_REGS_PARM4(ctx) ((ctx)->gpr[6])
#define PT_REGS_PARM5(ctx) ((ctx)->gpr[7])
#define PT_REGS_PARM6(ctx) ((ctx)->gpr[8])
#define PT_REGS_RC(ctx) ((ctx)->gpr[3])
#define PT_REGS_IP(ctx) ((ctx)->nip)
#define PT_REGS_SP(ctx) ((ctx)->gpr[1])
#elif defined(bpf_target_s930x)
#define PT_REGS_PARM1(x) ((x)->gprs[2])
#define PT_REGS_PARM2(x) ((x)->gprs[3])
Expand All @@ -936,29 +976,29 @@ int bpf_usdt_readarg_p(int argc, struct pt_regs *ctx, void *buf, u64 len) asm("l
#define PT_REGS_SP(x) ((x)->gprs[15])
#define PT_REGS_IP(x) ((x)->psw.addr)
#elif defined(bpf_target_x86)
#define PT_REGS_PARM1(ctx) ((ctx)->di)
#define PT_REGS_PARM2(ctx) ((ctx)->si)
#define PT_REGS_PARM3(ctx) ((ctx)->dx)
#define PT_REGS_PARM4(ctx) ((ctx)->cx)
#define PT_REGS_PARM5(ctx) ((ctx)->r8)
#define PT_REGS_PARM6(ctx) ((ctx)->r9)
#define PT_REGS_RET(ctx) ((ctx)->sp)
#define PT_REGS_PARM1(ctx) ((ctx)->di)
#define PT_REGS_PARM2(ctx) ((ctx)->si)
#define PT_REGS_PARM3(ctx) ((ctx)->dx)
#define PT_REGS_PARM4(ctx) ((ctx)->cx)
#define PT_REGS_PARM5(ctx) ((ctx)->r8)
#define PT_REGS_PARM6(ctx) ((ctx)->r9)
#define PT_REGS_RET(ctx) ((ctx)->sp)
#define PT_REGS_FP(ctx) ((ctx)->bp) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(ctx) ((ctx)->ax)
#define PT_REGS_IP(ctx) ((ctx)->ip)
#define PT_REGS_SP(ctx) ((ctx)->sp)
#define PT_REGS_RC(ctx) ((ctx)->ax)
#define PT_REGS_IP(ctx) ((ctx)->ip)
#define PT_REGS_SP(ctx) ((ctx)->sp)
#elif defined(bpf_target_arm64)
#define PT_REGS_PARM1(x) ((x)->regs[0])
#define PT_REGS_PARM2(x) ((x)->regs[1])
#define PT_REGS_PARM3(x) ((x)->regs[2])
#define PT_REGS_PARM4(x) ((x)->regs[3])
#define PT_REGS_PARM5(x) ((x)->regs[4])
#define PT_REGS_PARM6(x) ((x)->regs[5])
#define PT_REGS_RET(x) ((x)->regs[30])
#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(x) ((x)->regs[0])
#define PT_REGS_SP(x) ((x)->sp)
#define PT_REGS_IP(x) ((x)->pc)
#define PT_REGS_PARM1(x) ((x)->regs[0])
#define PT_REGS_PARM2(x) ((x)->regs[1])
#define PT_REGS_PARM3(x) ((x)->regs[2])
#define PT_REGS_PARM4(x) ((x)->regs[3])
#define PT_REGS_PARM5(x) ((x)->regs[4])
#define PT_REGS_PARM6(x) ((x)->regs[5])
#define PT_REGS_RET(x) ((x)->regs[30])
#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
#define PT_REGS_RC(x) ((x)->regs[0])
#define PT_REGS_SP(x) ((x)->sp)
#define PT_REGS_IP(x) ((x)->pc)
#else
#error "bcc does not support this platform yet"
#endif
Expand Down
17 changes: 16 additions & 1 deletion src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,16 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
} else if (memb_name == "get_local_storage") {
prefix = "bpf_get_local_storage";
suffix = ")";
} else {
} else if (memb_name == "push") {
prefix = "bpf_map_push_elem";
suffix = ")";
} else if (memb_name == "pop") {
prefix = "bpf_map_pop_elem";
suffix = ")";
} else if (memb_name == "peek") {
prefix = "bpf_map_peek_elem";
suffix = ")";
} else {
error(GET_BEGINLOC(Call), "invalid bpf_table operation %0") << memb_name;
return false;
}
Expand Down Expand Up @@ -1276,6 +1285,12 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
table.max_entries = numcpu;
} else if (section_attr == "maps/perf_array") {
map_type = BPF_MAP_TYPE_PERF_EVENT_ARRAY;
} else if (section_attr == "maps/queue") {
table.key_size = 0;
map_type = BPF_MAP_TYPE_QUEUE;
} else if (section_attr == "maps/stack") {
table.key_size = 0;
map_type = BPF_MAP_TYPE_STACK;
} else if (section_attr == "maps/cgroup_array") {
map_type = BPF_MAP_TYPE_CGROUP_ARRAY;
} else if (section_attr == "maps/stacktrace") {
Expand Down

0 comments on commit 679c5e9

Please sign in to comment.