forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget.h
321 lines (291 loc) · 15.1 KB
/
target.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
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_TARGET_H_
#define BACKENDS_EBPF_TARGET_H_
#include "frontends/p4/typeMap.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/error.h"
#include "lib/exceptions.h"
#include "lib/sourceCodeBuilder.h"
/// We are prepared to support code generation using multiple styles
/// (e.g., using BCC or using CLANG).
namespace EBPF {
using namespace P4::literals;
enum TableKind {
TableHash,
TableArray,
TablePerCPUArray,
TableProgArray,
TableLPMTrie, // Longest prefix match trie.
TableHashLRU,
TableDevmap
};
class Target {
protected:
explicit Target(cstring name) : name(name) {}
Target() = delete;
virtual ~Target() {}
public:
const cstring name;
virtual void emitLicense(Util::SourceCodeBuilder *builder, cstring license) const = 0;
virtual void emitCodeSection(Util::SourceCodeBuilder *builder, cstring sectionName) const = 0;
virtual void emitIncludes(Util::SourceCodeBuilder *builder) const = 0;
virtual void emitResizeBuffer(Util::SourceCodeBuilder *builder, cstring buffer,
cstring offsetVar) const = 0;
virtual void emitTableLookup(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const = 0;
virtual void emitTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const = 0;
virtual void emitUserTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const = 0;
virtual void emitTableDecl(Util::SourceCodeBuilder *builder, cstring tblName,
TableKind tableKind, cstring keyType, cstring valueType,
unsigned size) const = 0;
virtual void emitTableDeclSpinlock(Util::SourceCodeBuilder *builder, cstring tblName,
TableKind tableKind, cstring keyType, cstring valueType,
unsigned size) const {
(void)builder;
(void)tblName;
(void)tableKind;
(void)keyType;
(void)valueType;
(void)size;
::error(ErrorType::ERR_UNSUPPORTED, "emitTableDeclSpinlock is not supported on %1% target",
name);
}
/// map-in-map requires declaration of both inner and outer map,
/// thus we define them together in a single method.
virtual void emitMapInMapDecl(Util::SourceCodeBuilder *builder, cstring innerName,
TableKind innerTableKind, cstring innerKeyType,
cstring innerValueType, unsigned innerSize, cstring outerName,
TableKind outerTableKind, cstring outerKeyType,
unsigned outerSize) const {
(void)builder;
(void)innerName;
(void)innerTableKind;
(void)innerKeyType;
(void)innerValueType;
(void)innerSize;
(void)outerName;
(void)outerTableKind;
(void)outerKeyType;
(void)outerSize;
::error(ErrorType::ERR_UNSUPPORTED, "emitMapInMapDecl is not supported on %1% target",
name);
}
virtual void emitMain(Util::SourceCodeBuilder *builder, cstring functionName,
cstring argName) const = 0;
virtual cstring dataOffset(cstring base) const = 0;
virtual cstring dataEnd(cstring base) const = 0;
virtual cstring dataLength(cstring base) const = 0;
virtual cstring forwardReturnCode() const = 0;
virtual cstring dropReturnCode() const = 0;
virtual cstring abortReturnCode() const = 0;
// Path on /sys filesystem where maps are stored
virtual cstring sysMapPath() const = 0;
virtual cstring packetDescriptorType() const = 0;
virtual void emitPreamble(Util::SourceCodeBuilder *builder) const;
/// Emit trace message which will be printed during packet processing (if enabled).
/// @param builder Actual source code builder.
/// @param format Format string, interpreted by `printk`-like function. For more
/// information see documentation for `bpf_trace_printk`.
/// @param argc Number of variadic arguments. Up to 3 arguments can be passed
/// due to limitation of eBPF.
/// @param ... Arguments to the format string, they must be C string and valid code in C.
///
/// To print variable value: `emitTraceMessage(builder, "var=%u", 1, "var_name")`
/// To print expression value: `emitTraceMessage(builder, "diff=%d", 1, "var1 - var2")`
/// To print just message: `emitTraceMessage(builder, "Here")`
virtual void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format, int argc,
...) const;
virtual void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format) const;
};
/// Represents a target that is compiled within the kernel.
/// source tree samples folder and which attaches to a socket.
class KernelSamplesTarget : public Target {
private:
mutable unsigned int innerMapIndex;
cstring getBPFMapType(TableKind kind) const {
if (kind == TableHash) {
return "BPF_MAP_TYPE_HASH"_cs;
} else if (kind == TableArray) {
return "BPF_MAP_TYPE_ARRAY"_cs;
} else if (kind == TablePerCPUArray) {
return "BPF_MAP_TYPE_PERCPU_ARRAY"_cs;
} else if (kind == TableLPMTrie) {
return "BPF_MAP_TYPE_LPM_TRIE"_cs;
} else if (kind == TableHashLRU) {
return "BPF_MAP_TYPE_LRU_HASH"_cs;
} else if (kind == TableProgArray) {
return "BPF_MAP_TYPE_PROG_ARRAY"_cs;
} else if (kind == TableDevmap) {
return "BPF_MAP_TYPE_DEVMAP"_cs;
}
BUG("Unknown table kind");
}
protected:
bool emitTraceMessages;
public:
explicit KernelSamplesTarget(bool emitTrace = false, cstring name = "Linux kernel"_cs)
: Target(name), innerMapIndex(0), emitTraceMessages(emitTrace) {}
void emitLicense(Util::SourceCodeBuilder *builder, cstring license) const override;
void emitCodeSection(Util::SourceCodeBuilder *builder, cstring sectionName) const override;
void emitIncludes(Util::SourceCodeBuilder *builder) const override;
void emitResizeBuffer(Util::SourceCodeBuilder *builder, cstring buffer,
cstring offsetVar) const override;
void emitTableLookup(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitUserTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitTableDecl(Util::SourceCodeBuilder *builder, cstring tblName, TableKind tableKind,
cstring keyType, cstring valueType, unsigned size) const override;
void emitTableDeclSpinlock(Util::SourceCodeBuilder *builder, cstring tblName,
TableKind tableKind, cstring keyType, cstring valueType,
unsigned size) const override;
void emitMapInMapDecl(Util::SourceCodeBuilder *builder, cstring innerName,
TableKind innerTableKind, cstring innerKeyType, cstring innerValueType,
unsigned innerSize, cstring outerName, TableKind outerTableKind,
cstring outerKeyType, unsigned outerSize) const override;
void emitMain(Util::SourceCodeBuilder *builder, cstring functionName,
cstring argName) const override;
void emitPreamble(Util::SourceCodeBuilder *builder) const override;
void emitTraceMessage(Util::SourceCodeBuilder *builder, const char *format, int argc = 0,
...) const override;
cstring dataOffset(cstring base) const override {
return cstring("((void*)(long)") + base + "->data)";
}
cstring dataEnd(cstring base) const override {
return cstring("((void*)(long)") + base + "->data_end)";
}
cstring dataLength(cstring base) const override { return cstring(base) + "->len"; }
cstring forwardReturnCode() const override { return "TC_ACT_OK"_cs; }
cstring dropReturnCode() const override { return "TC_ACT_SHOT"_cs; }
cstring abortReturnCode() const override { return "TC_ACT_SHOT"_cs; }
cstring sysMapPath() const override { return "/sys/fs/bpf/tc/globals"_cs; }
cstring packetDescriptorType() const override { return "struct __sk_buff"_cs; }
void annotateTableWithBTF(Util::SourceCodeBuilder *builder, cstring name, cstring keyType,
cstring valueType) const;
};
class P4TCTarget : public KernelSamplesTarget {
public:
explicit P4TCTarget(bool emitTrace) : KernelSamplesTarget(emitTrace, "P4TC"_cs) {}
cstring getByteOrderFromAnnotation(const IR::Vector<IR::Annotation> annotations) const {
for (auto anno : annotations) {
if (anno->name != "tc_type") continue;
for (auto annoVal : anno->body) {
if (annoVal->text == "macaddr" || annoVal->text == "ipv4" ||
annoVal->text == "ipv6" || annoVal->text == "be16" || annoVal->text == "be32" ||
annoVal->text == "be64") {
return "NETWORK"_cs;
}
}
}
return "HOST"_cs;
}
cstring getByteOrder(P4::TypeMap *typeMap, const IR::P4Action *action,
const IR::Expression *exp) const {
if (auto mem = exp->to<IR::Member>()) {
auto type = typeMap->getType(mem->expr, true);
if (type->is<IR::Type_StructLike>()) {
auto field = type->to<IR::Type_StructLike>()->getField(mem->member);
return getByteOrderFromAnnotation(field->getAnnotations()->annotations);
}
} else if (action) {
auto paramList = action->getParameters();
if (paramList != nullptr && !paramList->empty()) {
for (auto param : paramList->parameters) {
if (param->name.originalName == exp->toString()) {
return getByteOrderFromAnnotation(param->getAnnotations()->annotations);
}
}
}
}
return "HOST"_cs;
}
};
/// Target XDP.
class XdpTarget : public KernelSamplesTarget {
public:
explicit XdpTarget(bool emitTrace) : KernelSamplesTarget(emitTrace, "XDP"_cs) {}
cstring forwardReturnCode() const override { return "XDP_PASS"_cs; }
cstring dropReturnCode() const override { return "XDP_DROP"_cs; }
cstring abortReturnCode() const override { return "XDP_ABORTED"_cs; }
cstring redirectReturnCode() const { return "XDP_REDIRECT"_cs; }
cstring sysMapPath() const override { return "/sys/fs/bpf/xdp/globals"_cs; }
cstring packetDescriptorType() const override { return "struct xdp_md"_cs; }
cstring dataLength(cstring base) const override {
return cstring("(") + base + "->data_end - " + base + "->data)";
}
void emitResizeBuffer(Util::SourceCodeBuilder *builder, cstring buffer,
cstring offsetVar) const override;
void emitMain(Util::SourceCodeBuilder *builder, cstring functionName,
cstring argName) const override {
builder->appendFormat("int %s(%s *%s)", functionName.c_str(), packetDescriptorType(),
argName.c_str());
}
};
/// Represents a target compiled by bcc that uses the TC.
class BccTarget : public Target {
public:
BccTarget() : Target("BCC"_cs) {}
void emitLicense(Util::SourceCodeBuilder *, cstring) const override {}
void emitCodeSection(Util::SourceCodeBuilder *, cstring) const override {}
void emitIncludes(Util::SourceCodeBuilder *builder) const override;
void emitResizeBuffer(Util::SourceCodeBuilder *, cstring, cstring) const override {}
void emitTableLookup(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitUserTableUpdate(Util::SourceCodeBuilder *builder, cstring tblName, cstring key,
cstring value) const override;
void emitTableDecl(Util::SourceCodeBuilder *builder, cstring tblName, TableKind tableKind,
cstring keyType, cstring valueType, unsigned size) const override;
void emitMain(Util::SourceCodeBuilder *builder, cstring functionName,
cstring argName) const override;
cstring dataOffset(cstring base) const override { return base; }
cstring dataEnd(cstring base) const override {
return cstring("(") + base + " + " + base + "->len)";
}
cstring dataLength(cstring base) const override { return cstring(base) + "->len"; }
cstring forwardReturnCode() const override { return "0"_cs; }
cstring dropReturnCode() const override { return "1"_cs; }
cstring abortReturnCode() const override { return "1"_cs; }
cstring sysMapPath() const override { return "/sys/fs/bpf"_cs; }
cstring packetDescriptorType() const override { return "struct __sk_buff"_cs; }
};
/// A userspace test version with functionality equivalent to the kernel.
/// Compiles with GCC.
class TestTarget : public EBPF::KernelSamplesTarget {
public:
TestTarget() : KernelSamplesTarget(false, "Userspace Test"_cs) {}
void emitResizeBuffer(Util::SourceCodeBuilder *, cstring, cstring) const override {}
void emitIncludes(Util::SourceCodeBuilder *builder) const override;
void emitTableDecl(Util::SourceCodeBuilder *builder, cstring tblName, TableKind tableKind,
cstring keyType, cstring valueType, unsigned size) const override;
cstring dataOffset(cstring base) const override {
return cstring("((void*)(long)") + base + "->data)";
}
cstring dataEnd(cstring base) const override {
return cstring("((void*)(long)(") + base + "->data + " + base + "->len))";
}
cstring forwardReturnCode() const override { return "true"_cs; }
cstring dropReturnCode() const override { return "false"_cs; }
cstring abortReturnCode() const override { return "false"_cs; }
cstring sysMapPath() const override { return "/sys/fs/bpf"_cs; }
cstring packetDescriptorType() const override { return "struct __sk_buff"_cs; }
};
} // namespace EBPF
#endif /* BACKENDS_EBPF_TARGET_H_ */