Commit db410bf3 authored by Gary Lin's avatar Gary Lin

Add two new map types: DEVMAP and CPUMAP

Those two map types are necessary to support bpf_redirect_map() in XDP.

v2:
  Use ArrayBase as the base class of DevMap and CpuMap
Signed-off-by: default avatarGary Lin <glin@suse.com>
parent 3ae15d60
......@@ -201,6 +201,23 @@ struct bpf_stacktrace {
#define BPF_PROG_ARRAY(_name, _max_entries) \
BPF_TABLE("prog", u32, u32, _name, _max_entries)
#define BPF_XDP_REDIRECT_MAP(_table_type, _leaf_type, _name, _max_entries) \
struct _name##_table_t { \
u32 key; \
_leaf_type leaf; \
/* xdp_act = map.redirect_map(index, flag) */ \
u64 (*redirect_map) (int, int); \
u32 max_entries; \
}; \
__attribute__((section("maps/"_table_type))) \
struct _name##_table_t _name = { .max_entries = (_max_entries) }
#define BPF_DEVMAP(_name, _max_entries) \
BPF_XDP_REDIRECT_MAP("devmap", int, _name, _max_entries)
#define BPF_CPUMAP(_name, _max_entries) \
BPF_XDP_REDIRECT_MAP("cpumap", u32, _name, _max_entries)
// packet parsing state machine helpers
#define cursor_advance(_cursor, _len) \
({ void *_tmp = _cursor; _cursor += _len; _tmp; })
......
......@@ -609,6 +609,9 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
} else if (memb_name == "check_current_task") {
prefix = "bpf_current_task_under_cgroup";
suffix = ")";
} else if (memb_name == "redirect_map") {
prefix = "bpf_redirect_map";
suffix = ")";
} else {
error(Call->getLocStart(), "invalid bpf_table operation %0") << memb_name;
return false;
......@@ -920,6 +923,10 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
map_type = BPF_MAP_TYPE_CGROUP_ARRAY;
} else if (A->getName() == "maps/stacktrace") {
map_type = BPF_MAP_TYPE_STACK_TRACE;
} else if (A->getName() == "maps/devmap") {
map_type = BPF_MAP_TYPE_DEVMAP;
} 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(Decl->getLocStart(), "reference to undefined table");
......
......@@ -36,6 +36,13 @@ BPF_MAP_TYPE_CGROUP_ARRAY = 8
BPF_MAP_TYPE_LRU_HASH = 9
BPF_MAP_TYPE_LRU_PERCPU_HASH = 10
BPF_MAP_TYPE_LPM_TRIE = 11
BPF_MAP_TYPE_ARRAY_OF_MAPS = 12
BPF_MAP_TYPE_HASH_OF_MAPS = 13
BPF_MAP_TYPE_DEVMAP = 14
BPF_MAP_TYPE_SOCKMAP = 15
BPF_MAP_TYPE_CPUMAP = 16
BPF_MAP_TYPE_XSKMAP = 17
BPF_MAP_TYPE_SOCKHASH = 18
stars_max = 40
log2_index_max = 65
......@@ -144,6 +151,10 @@ def Table(bpf, map_id, map_fd, keytype, leaftype, **kwargs):
t = LruPerCpuHash(bpf, map_id, map_fd, keytype, leaftype)
elif ttype == BPF_MAP_TYPE_CGROUP_ARRAY:
t = CgroupArray(bpf, map_id, map_fd, keytype, leaftype)
elif ttype == BPF_MAP_TYPE_DEVMAP:
t = DevMap(bpf, map_id, map_fd, keytype, leaftype)
elif ttype == BPF_MAP_TYPE_CPUMAP:
t = CpuMap(bpf, map_id, map_fd, keytype, leaftype)
if t == None:
raise Exception("Unknown table type %d" % ttype)
return t
......@@ -766,3 +777,11 @@ class StackTrace(TableBase):
def clear(self):
pass
class DevMap(ArrayBase):
def __init__(self, *args, **kwargs):
super(DevMap, self).__init__(*args, **kwargs)
class CpuMap(ArrayBase):
def __init__(self, *args, **kwargs):
super(CpuMap, self).__init__(*args, **kwargs)
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