-
Notifications
You must be signed in to change notification settings - Fork 2
/
ebpfTable.h
193 lines (159 loc) · 6.98 KB
/
ebpfTable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef BACKENDS_EBPF_EBPFTABLE_H_
#define BACKENDS_EBPF_EBPFTABLE_H_
#include "ebpfObject.h"
#include "ebpfProgram.h"
#include "frontends/p4/methodInstance.h"
namespace P4::EBPF {
class ActionTranslationVisitor : public virtual CodeGenInspector {
protected:
const EBPFProgram *program;
const IR::P4Action *action;
cstring valueName;
public:
ActionTranslationVisitor(cstring valueName, const EBPFProgram *program)
: CodeGenInspector(program->refMap, program->typeMap),
program(program),
action(nullptr),
valueName(valueName) {
CHECK_NULL(program);
}
bool preorder(const IR::PathExpression *expression);
bool preorder(const IR::P4Action *act);
virtual cstring getParamInstanceName(const IR::Expression *expression) const;
bool isActionParameter(const IR::PathExpression *expression) const;
}; // ActionTranslationVisitor
/// Also used to represent counters
class EBPFTableBase : public EBPFObject {
public:
const EBPFProgram *program;
cstring instanceName;
cstring keyTypeName;
cstring valueTypeName;
cstring dataMapName;
CodeGenInspector *codeGen;
protected:
EBPFTableBase(const EBPFProgram *program, cstring instanceName, CodeGenInspector *codeGen)
: program(program), instanceName(instanceName), codeGen(codeGen) {
CHECK_NULL(codeGen);
CHECK_NULL(program);
keyTypeName = instanceName + "_key";
valueTypeName = instanceName + "_value";
dataMapName = instanceName;
}
DECLARE_TYPEINFO(EBPFTableBase, EBPFObject);
};
class EBPFTable : public EBPFTableBase {
const int prefixLenFieldWidth = 32;
void initKey();
public:
bool isLPMTable() const;
bool isTernaryTable() const;
protected:
void emitTernaryInstance(CodeBuilder *builder);
virtual void validateKeys() const;
virtual ActionTranslationVisitor *createActionTranslationVisitor(
cstring valueName, const EBPFProgram *program) const {
return new ActionTranslationVisitor(valueName, program);
}
public:
const IR::Key *keyGenerator;
const IR::ActionList *actionList;
const IR::TableBlock *table;
cstring defaultActionMapName;
std::map<const IR::KeyElement *, cstring> keyFieldNames;
std::map<const IR::KeyElement *, EBPFType *> keyTypes;
/// Use 1024 by default.
/// TODO: make it configurable using compiler options.
size_t size = 1024;
const cstring prefixFieldName = "prefixlen"_cs;
EBPFTable(const EBPFProgram *program, const IR::TableBlock *table, CodeGenInspector *codeGen);
EBPFTable(const EBPFProgram *program, CodeGenInspector *codeGen, cstring name);
cstring p4ActionToActionIDName(const IR::P4Action *action) const;
void emitActionArguments(CodeBuilder *builder, const IR::P4Action *action, cstring name);
void emitKey(CodeBuilder *builder, cstring keyName);
virtual void emitTypes(CodeBuilder *builder);
virtual void emitInstance(CodeBuilder *builder);
virtual void emitKeyType(CodeBuilder *builder);
virtual void emitValueType(CodeBuilder *builder);
virtual void emitValueActionIDNames(CodeBuilder *builder);
virtual void emitValueStructStructure(CodeBuilder *builder);
/// Emits value types used by direct externs.
virtual void emitDirectValueTypes(CodeBuilder *builder) { (void)builder; }
virtual void emitAction(CodeBuilder *builder, cstring valueName, cstring actionRunVariable);
virtual void emitInitializer(CodeBuilder *builder);
virtual void emitLookup(CodeBuilder *builder, cstring key, cstring value);
virtual void emitLookupDefault(CodeBuilder *builder, cstring key, cstring value,
cstring actionRunVariable) {
(void)actionRunVariable;
builder->target->emitTableLookup(builder, defaultActionMapName, key, value);
builder->endOfStatement(true);
}
virtual bool isMatchTypeSupported(const IR::Declaration_ID *matchType) {
return matchType->name.name == P4::P4CoreLibrary::instance().exactMatch.name ||
matchType->name.name == P4::P4CoreLibrary::instance().ternaryMatch.name ||
matchType->name.name == P4::P4CoreLibrary::instance().lpmMatch.name;
}
/// Whether to drop packet if no match entry found.
/// Some table implementations may want to continue processing.
virtual bool dropOnNoMatchingEntryFound() const { return true; }
virtual bool cacheEnabled() { return false; }
virtual void emitCacheLookup(CodeBuilder *builder, cstring key, cstring value) {
(void)builder;
(void)key;
(void)value;
}
virtual void emitCacheUpdate(CodeBuilder *builder, cstring key, cstring value) {
(void)builder;
(void)key;
(void)value;
}
DECLARE_TYPEINFO(EBPFTable, EBPFTableBase);
};
class EBPFCounterTable : public EBPFTableBase {
protected:
size_t size;
bool isHash;
public:
EBPFCounterTable(const EBPFProgram *program, const IR::ExternBlock *block, cstring name,
CodeGenInspector *codeGen);
EBPFCounterTable(const EBPFProgram *program, cstring name, CodeGenInspector *codeGen,
size_t size, bool isHash)
: EBPFTableBase(program, name, codeGen), size(size), isHash(isHash) {}
virtual void emitTypes(CodeBuilder *);
virtual void emitInstance(CodeBuilder *builder);
virtual void emitCounterIncrement(CodeBuilder *builder,
const IR::MethodCallExpression *expression);
virtual void emitCounterAdd(CodeBuilder *builder, const IR::MethodCallExpression *expression);
virtual void emitMethodInvocation(CodeBuilder *builder, const P4::ExternMethod *method);
DECLARE_TYPEINFO(EBPFCounterTable, EBPFTableBase);
};
class EBPFValueSet : public EBPFTableBase {
protected:
size_t size;
const IR::P4ValueSet *pvs;
std::vector<std::pair<cstring, const IR::Type *>> fieldNames;
cstring keyVarName;
public:
EBPFValueSet(const EBPFProgram *program, const IR::P4ValueSet *p4vs, cstring instanceName,
CodeGenInspector *codeGen);
void emitTypes(CodeBuilder *builder);
void emitInstance(CodeBuilder *builder);
void emitKeyInitializer(CodeBuilder *builder, const IR::SelectExpression *expression,
cstring varName);
void emitLookup(CodeBuilder *builder);
DECLARE_TYPEINFO(EBPFValueSet, EBPFTableBase);
};
} // namespace P4::EBPF
#endif /* BACKENDS_EBPF_EBPFTABLE_H_ */