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