Skip to content

Commit

Permalink
Introducing Queue/Stack helpers and clang frontend
Browse files Browse the repository at this point in the history
This commit aims to introduce helpers to declare Queue/Stack maps. I have supported also
the creation of shared/public/pinned ones, as for the "traditional" tables.
In clang frontend I have added both declaration of maps type/queue, type/stack and all the operations
supported so far by these new maps (push/pop/peek).

Possible declarations introduced:

* BPF_QUEUESTACK(<"queue"/"stack">, <name>, <leaf_type>, <max_entries>, <flags>)
* BPF_QUEUESTACK_SHARED(...)
* BPF_QUEUESTACK_PINNED(...)
* BPF_QUEUESTACK_PUBLIC(...)
* BPF_QUEUE(<name>, <leaf_type>, <max_entries>)
* BPF_QUEUE(<name>, <leaf_type>, <max_entries>, <flags>)
* BPF_STACK(<name>, <leaf_type>, <max_entries>)
* BPF_STACK(<name>, <leaf_type>, <max_entries>, <flags>)

Signed-off-by: Simone Magnani <[email protected]>
Co-authored-by: Sebastiano Miano <[email protected]>
  • Loading branch information
2 people authored and yonghong-song committed Jun 29, 2020
1 parent fe730f2 commit fbde62b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
54 changes: 54 additions & 0 deletions src/cc/export/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ R"********(
__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
#define BPF_F_TABLE(_table_type, _key_type, _leaf_type, _name, _max_entries, _flags) \
struct _name##_table_t { \
Expand All @@ -100,6 +109,51 @@ __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_QUEUESTACK_PINNED(_table_type, _name, _leaf_type, _max_entries, _flags, _pinned) \
BPF_QUEUESTACK(_table_type ":" _pinned, _name, _leaf_type, _max_entries, _flags)

#define BPF_QUEUESTACK_PUBLIC(_table_type, _name, _leaf_type, _max_entries, _flags) \
BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags); \
__attribute__((section("maps/export"))) \
struct _name##_table_t __##_name

#define BPF_QUEUESTACK_SHARED(_table_type, _name, _leaf_type, _max_entries, _flags) \
BPF_QUEUESTACK(_table_type, _name, _leaf_type, _max_entries, _flags); \
__attribute__((section("maps/shared"))) \
struct _name##_table_t __##_name

#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
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 @@ -1036,7 +1036,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 @@ -1419,6 +1428,12 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
table.leaf_size = 0;
} 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 fbde62b

Please sign in to comment.