-
Notifications
You must be signed in to change notification settings - Fork 444
/
ebpfProgram.cpp
361 lines (315 loc) · 13.4 KB
/
ebpfProgram.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
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.
*/
#include "ebpfProgram.h"
#include <chrono>
#include <ctime>
#include "ebpfControl.h"
#include "ebpfDeparser.h"
#include "ebpfParser.h"
#include "ebpfTable.h"
#include "ebpfType.h"
#include "frontends/common/options.h"
#include "frontends/p4/coreLibrary.h"
namespace P4::EBPF {
bool EBPFProgram::build() {
auto pack = toplevel->getMain();
if (pack->type->name == "xdp") {
if (pack->getConstructorParameters()->size() != 3) {
::P4::error(ErrorType::ERR_EXPECTED,
"Expected toplevel xdp package %1% to have 3 parameters", pack->type);
return false;
}
model.arch = ModelArchitecture::XdpSwitch;
progTarget = new XdpTarget(options.emitTraceMessages);
} else {
if (pack->type->name != "ebpfFilter")
::P4::warning(ErrorType::WARN_INVALID,
"%1%: the main ebpf package should be called ebpfFilter or xdp"
"; are you using the wrong architecture?",
pack->type->name);
if (pack->getConstructorParameters()->size() != 2) {
::P4::error(ErrorType::ERR_EXPECTED,
"Expected toplevel ebpfFilter package %1% to have 2 parameters",
pack->type);
return false;
}
model.arch = ModelArchitecture::EbpfFilter;
}
auto prsName = (model.arch == ModelArchitecture::XdpSwitch) ? model.xdp.parser.name
: model.filter.parser.name;
auto ctlName = (model.arch == ModelArchitecture::XdpSwitch) ? model.xdp.switch_.name
: model.filter.filter.name;
auto pb = pack->getParameterValue(prsName)->to<IR::ParserBlock>();
BUG_CHECK(pb != nullptr, "No parser block found");
parser = new EBPFParser(this, pb, typeMap);
bool success = parser->build();
if (!success) return success;
auto cb = pack->getParameterValue(ctlName)->to<IR::ControlBlock>();
BUG_CHECK(cb != nullptr, "No control block found");
control = new EBPFControl(this, cb, parser->headers);
success = control->build();
if (!success) return success;
if (model.arch == ModelArchitecture::XdpSwitch) {
auto db = pack->getParameterValue(model.xdp.deparser.name)->to<IR::ControlBlock>();
BUG_CHECK(db != nullptr, "No deparser block found");
deparser = new EBPFDeparser(this, db, parser->headers);
bool success = deparser->build();
if (!success) return success;
}
return true;
}
void EBPFProgram::emitC(CodeBuilder *builder, const std::filesystem::path &header) {
emitGeneratedComment(builder);
// Remove the path from the header
builder->appendFormat("#include \"%s\"", header.filename());
builder->newline();
builder->target->emitIncludes(builder);
emitPreamble(builder);
builder->append("REGISTER_START()\n");
control->emitTableInstances(builder);
parser->emitValueSetInstances(builder);
builder->append("REGISTER_END()\n");
builder->newline();
builder->emitIndent();
// Use different section name for XDP - this is used by the runtime test framework.
if (model.arch == ModelArchitecture::XdpSwitch)
builder->target->emitCodeSection(builder, "xdp"_cs);
else
builder->target->emitCodeSection(builder, "prog"_cs);
builder->emitIndent();
builder->target->emitMain(builder, functionName, model.CPacketName.toString());
builder->blockStart();
emitHeaderInstances(builder);
builder->append(" = ");
parser->headerType->emitInitializer(builder);
builder->endOfStatement(true);
emitLocalVariables(builder);
builder->newline();
parser->emit(builder);
emitPipeline(builder);
builder->emitIndent();
builder->appendFormat("%s:\n", endLabel.c_str());
builder->emitIndent();
if (model.arch == ModelArchitecture::EbpfFilter) {
builder->appendFormat("if (%s)\n", control->accept->name.name.c_str());
builder->increaseIndent();
builder->emitIndent();
builder->appendFormat("return %s;\n", builder->target->forwardReturnCode().c_str());
builder->decreaseIndent();
builder->emitIndent();
builder->appendLine("else");
builder->increaseIndent();
builder->emitIndent();
builder->appendFormat("return %s;\n", builder->target->dropReturnCode().c_str());
builder->decreaseIndent();
} else if (model.arch == ModelArchitecture::XdpSwitch) {
builder->append("return omd.output_action;");
builder->newline();
} else {
BUG("Invalid value for model.arch !");
}
builder->blockEnd(true); // end of function
builder->target->emitLicense(builder, license);
}
void EBPFProgram::emitGeneratedComment(CodeBuilder *builder) {
std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(now);
builder->append("/* Automatically generated by ");
builder->append(options.exe_name);
builder->append(" from ");
builder->append(options.file);
builder->append(" on ");
builder->append(std::ctime(&time));
builder->append(" */");
builder->newline();
}
void EBPFProgram::emitH(CodeBuilder *builder, const std::filesystem::path &) {
emitGeneratedComment(builder);
builder->appendLine("#ifndef _P4_GEN_HEADER_");
builder->appendLine("#define _P4_GEN_HEADER_");
builder->target->emitIncludes(builder);
builder->appendFormat("#define MAP_PATH \"%s\"", builder->target->sysMapPath().c_str());
builder->newline();
emitTypes(builder);
control->emitTableTypes(builder);
parser->emitTypes(builder);
builder->appendLine("#if CONTROL_PLANE");
builder->appendLine("static void init_tables() ");
builder->blockStart();
builder->emitIndent();
builder->appendFormat("u32 %s = 0;", zeroKey.c_str());
builder->newline();
control->emitTableInitializers(builder);
builder->blockEnd(true);
builder->appendLine("#endif");
builder->appendLine("#endif");
}
void EBPFProgram::emitTypes(CodeBuilder *builder) {
for (auto d : program->objects) {
if (d->is<IR::Type>() && !d->is<IR::IContainer>() && !d->is<IR::Type_Extern>() &&
!d->is<IR::Type_Parser>() && !d->is<IR::Type_Control>() && !d->is<IR::Type_Typedef>() &&
!d->is<IR::Type_Error>()) {
auto type = EBPFTypeFactory::instance->create(d->to<IR::Type>());
if (type == nullptr) continue;
if (d->is<IR::Type_Enum>() && d->to<IR::Type_Enum>()->name == "xdp_action")
continue; // already in linux/bpf.h
type->emit(builder);
builder->newline();
}
// TODO: This code is disabled until we fix stability issues in Ubuntu 20.04.
// For an unclear reason we can not use definitions and declarations for eBPF externs there.
// All externs need to be defined as static inline, which clashes with these definitions.
// Context: https://github.com/p4lang/p4c/pull/4644
// if (const auto *method = d->to<IR::Method>()) {
// if (!method->srcInfo.isValid()) {
// continue;
// }
// // Ignore methods originating from core.p4 and ubpf_model.p4 because they are already
// // defined.
// // TODO: Maybe we should still generate declarations for these methods?
// if (isLibraryMethod(method->controlPlaneName())) {
// continue;
// }
// EBPFMethodDeclaration methodInstance(method);
// methodInstance.emit(builder);
// builder->newline();
// }
}
}
namespace {
class ErrorCodesVisitor : public Inspector {
CodeBuilder *builder;
public:
explicit ErrorCodesVisitor(CodeBuilder *builder) : builder(builder) {}
bool preorder(const IR::Type_Error *errors) override {
for (auto m : *errors->getDeclarations()) {
builder->emitIndent();
builder->appendFormat("%s,\n", m->getName().name.c_str());
}
return false;
}
};
} // namespace
void EBPFProgram::emitCommonPreamble(CodeBuilder *builder) {
builder->newline();
builder->appendLine("#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1)");
builder->appendLine("#define BYTES(w) ((w) / 8)");
builder->appendLine(
"#define write_partial(a, s, v) do "
"{ u8 mask = EBPF_MASK(u8, s); "
"*((u8*)a) = ((*((u8*)a)) & ~mask) | (((v) >> (8 - (s))) & mask); "
"} while (0)");
builder->appendLine(
"#define write_partial_ex(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) "
"& ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0)");
builder->appendLine(
"#define write_byte(base, offset, v) do { "
"*(u8*)((base) + (offset)) = (v); "
"} while (0)");
builder->appendLine("#define PTR_DIFF_BYTES(b, o) (ssize_t)((u8*)(b) - (u8*)(o))");
}
void EBPFProgram::emitPreamble(CodeBuilder *builder) {
builder->emitIndent();
builder->appendFormat("enum %s ", errorEnum.c_str());
builder->blockStart();
ErrorCodesVisitor visitor(builder);
program->apply(visitor);
builder->blockEnd(false);
builder->endOfStatement(true);
emitCommonPreamble(builder);
builder->newline();
builder->appendLine("void* memcpy(void* dest, const void* src, size_t num);");
builder->newline();
builder->target->emitPreamble(builder);
}
void EBPFProgram::emitLocalVariables(CodeBuilder *builder) {
builder->emitIndent();
builder->appendFormat("enum %s %s = %s;", errorEnum.c_str(), errorVar.c_str(),
P4::P4CoreLibrary::instance().noError.str());
builder->newline();
builder->emitIndent();
builder->appendFormat("void* %s = %s;", packetStartVar.c_str(),
builder->target->dataOffset(model.CPacketName.toString()).c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("u8* %s = %s;", headerStartVar.c_str(), packetStartVar.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("void* %s = %s;", packetEndVar.c_str(),
builder->target->dataEnd(model.CPacketName.toString()).c_str());
builder->newline();
if (model.arch == ModelArchitecture::EbpfFilter) {
builder->emitIndent();
builder->appendFormat("u8 %s = 0;", control->accept->name.name.c_str());
builder->newline();
} else if (model.arch == ModelArchitecture::XdpSwitch) {
builder->emitIndent();
builder->append("struct xdp_input imd = { .input_port = skb->ingress_ifindex };");
builder->newline();
builder->emitIndent();
builder->append("struct xdp_output omd = { };");
builder->newline();
} else {
BUG("Invalid value for model.arch !");
}
builder->emitIndent();
builder->appendFormat("u32 %s = 0;", zeroKey.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("unsigned char %s;", byteVar.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("u32 %s = %s", lengthVar.c_str(),
builder->target->dataLength(model.CPacketName.toString()).c_str());
builder->endOfStatement(true);
}
void EBPFProgram::emitHeaderInstances(CodeBuilder *builder) {
builder->emitIndent();
parser->headerType->declare(builder, parser->headers->name.name, false);
}
void EBPFProgram::emitPipeline(CodeBuilder *builder) {
builder->emitIndent();
builder->append(IR::ParserState::accept);
builder->append(":");
builder->newline();
builder->emitIndent();
builder->blockStart();
builder->target->emitTraceMessage(builder, "Control: packet processing started");
control->emit(builder);
builder->blockEnd(true);
if (model.arch == ModelArchitecture::XdpSwitch) {
BUG_CHECK(deparser != nullptr, "XDP program can't be missing deparser");
builder->emitIndent();
builder->append("/* deparser */");
builder->newline();
builder->emitIndent();
builder->blockStart();
deparser->emit(builder);
builder->blockEnd(true);
} else {
builder->target->emitTraceMessage(builder, "Control: packet processing finished, pass=%d",
1, control->accept->name.name.c_str());
}
}
bool EBPFProgram::isLibraryMethod(cstring methodName) {
static std::set<cstring> DEFAULT_METHODS = {"static_assert"_cs, "verify"_cs};
if (DEFAULT_METHODS.find(methodName) != DEFAULT_METHODS.end() && options.target != "xdp") {
return true;
}
static std::set<cstring> XDP_METHODS = {
"ebpf_ipv4_checksum"_cs, "csum_replace2"_cs, "csum_replace4"_cs,
"BPF_PERF_EVENT_OUTPUT"_cs, "BPF_KTIME_GET_NS"_cs,
};
return XDP_METHODS.find(methodName) != XDP_METHODS.end();
}
} // namespace P4::EBPF