forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebpfParser.cpp
444 lines (391 loc) · 15.3 KB
/
ebpfParser.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
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 "ebpfModel.h"
#include "ebpfParser.h"
#include "ebpfType.h"
#include "frontends/p4/coreLibrary.h"
#include "frontends/p4/methodInstance.h"
namespace EBPF {
namespace {
class StateTranslationVisitor : public CodeGenInspector {
bool hasDefault;
P4::P4CoreLibrary& p4lib;
const EBPFParserState* state;
void compileExtractField(const IR::Expression* expr, cstring name,
unsigned alignment, EBPFType* type);
void compileExtract(const IR::Expression* destination);
void compileLookahead(const IR::Expression* destination);
public:
explicit StateTranslationVisitor(const EBPFParserState* state) :
CodeGenInspector(state->parser->program->refMap, state->parser->program->typeMap),
hasDefault(false), p4lib(P4::P4CoreLibrary::instance), state(state) {}
bool preorder(const IR::ParserState* state) override;
bool preorder(const IR::SelectCase* selectCase) override;
bool preorder(const IR::SelectExpression* expression) override;
bool preorder(const IR::Member* expression) override;
bool preorder(const IR::MethodCallExpression* expression) override;
bool preorder(const IR::MethodCallStatement* stat) override
{ visit(stat->methodCall); return false; }
bool preorder(const IR::AssignmentStatement* stat) override;
};
} // namespace
void
StateTranslationVisitor::compileLookahead(const IR::Expression* destination) {
builder->emitIndent();
builder->blockStart();
builder->emitIndent();
builder->appendFormat("%s_save = %s",
state->parser->program->offsetVar.c_str(),
state->parser->program->offsetVar.c_str());
builder->endOfStatement(true);
compileExtract(destination);
builder->emitIndent();
builder->appendFormat("%s = %s_save",
state->parser->program->offsetVar.c_str(),
state->parser->program->offsetVar.c_str());
builder->endOfStatement(true);
builder->blockEnd(true);
}
bool StateTranslationVisitor::preorder(const IR::AssignmentStatement* statement) {
if (auto mce = statement->right->to<IR::MethodCallExpression>()) {
auto mi = P4::MethodInstance::resolve(mce,
state->parser->program->refMap,
state->parser->program->typeMap);
auto extMethod = mi->to<P4::ExternMethod>();
if (extMethod == nullptr)
BUG("Unhandled method %1%", mce);
auto decl = extMethod->object;
if (decl == state->parser->packet) {
if (extMethod->method->name.name == p4lib.packetIn.lookahead.name) {
compileLookahead(statement->left);
return false;
}
}
::error(ErrorType::ERR_UNEXPECTED,
"Unexpected method call in parser %1%", statement->right);
}
return CodeGenInspector::preorder(statement);
}
bool StateTranslationVisitor::preorder(const IR::ParserState* parserState) {
if (parserState->isBuiltin()) return false;
builder->emitIndent();
builder->append(parserState->name.name);
builder->append(":");
builder->spc();
builder->blockStart();
visit(parserState->components, "components");
if (parserState->selectExpression == nullptr) {
builder->emitIndent();
builder->append("goto ");
builder->append(IR::ParserState::reject);
builder->endOfStatement(true);
} else if (parserState->selectExpression->is<IR::SelectExpression>()) {
visit(parserState->selectExpression);
} else {
// must be a PathExpression which is a state name
if (!parserState->selectExpression->is<IR::PathExpression>())
BUG("Expected a PathExpression, got a %1%", parserState->selectExpression);
builder->emitIndent();
builder->append("goto ");
visit(parserState->selectExpression);
builder->endOfStatement(true);
}
builder->blockEnd(true);
return false;
}
bool StateTranslationVisitor::preorder(const IR::SelectExpression* expression) {
hasDefault = false;
BUG_CHECK(expression->select->components.size() == 1,
"%1%: tuple not eliminated in select",
expression->select);
builder->emitIndent();
builder->append("switch (");
visit(expression->select);
builder->append(") ");
builder->blockStart();
for (auto e : expression->selectCases)
visit(e);
if (!hasDefault) {
builder->emitIndent();
builder->appendFormat("default: goto %s;", IR::ParserState::reject.c_str());
builder->newline();
}
builder->blockEnd(true);
return false;
}
bool StateTranslationVisitor::preorder(const IR::SelectCase* selectCase) {
builder->emitIndent();
if (selectCase->keyset->is<IR::DefaultExpression>()) {
hasDefault = true;
builder->append("default: ");
} else {
builder->append("case ");
visit(selectCase->keyset);
builder->append(": ");
}
builder->append("goto ");
visit(selectCase->state);
builder->endOfStatement(true);
return false;
}
void
StateTranslationVisitor::compileExtractField(
const IR::Expression* expr, cstring field, unsigned alignment, EBPFType* type) {
unsigned widthToExtract = dynamic_cast<IHasWidth*>(type)->widthInBits();
auto program = state->parser->program;
if (widthToExtract <= 64) {
unsigned lastBitIndex = widthToExtract + alignment - 1;
unsigned lastWordIndex = lastBitIndex / 8;
unsigned wordsToRead = lastWordIndex + 1;
unsigned loadSize;
const char* helper = nullptr;
if (wordsToRead <= 1) {
helper = "load_byte";
loadSize = 8;
} else if (wordsToRead <= 2) {
helper = "load_half";
loadSize = 16;
} else if (wordsToRead <= 4) {
helper = "load_word";
loadSize = 32;
} else {
// TODO: this is wrong, since a 60-bit unaligned read may require 9 words.
if (wordsToRead > 64) BUG("Unexpected width %d", widthToExtract);
helper = "load_dword";
loadSize = 64;
}
unsigned shift = loadSize - alignment - widthToExtract;
builder->emitIndent();
visit(expr);
builder->appendFormat(".%s = (", field.c_str());
type->emit(builder);
builder->appendFormat(")((%s(%s, BYTES(%s))",
helper,
program->packetStartVar.c_str(),
program->offsetVar.c_str());
if (shift != 0)
builder->appendFormat(" >> %d", shift);
builder->append(")");
if (widthToExtract != loadSize) {
builder->append(" & EBPF_MASK(");
type->emit(builder);
builder->appendFormat(", %d)", widthToExtract);
}
builder->append(")");
builder->endOfStatement(true);
} else {
// wide values; read all bytes one by one.
unsigned shift;
if (alignment == 0)
shift = 0;
else
shift = 8 - alignment;
const char* helper;
if (shift == 0)
helper = "load_byte";
else
helper = "load_half";
auto bt = EBPFTypeFactory::instance->create(IR::Type_Bits::get(8));
unsigned bytes = ROUNDUP(widthToExtract, 8);
for (unsigned i=0; i < bytes; i++) {
builder->emitIndent();
visit(expr);
builder->appendFormat(".%s[%d] = (", field.c_str(), i);
bt->emit(builder);
builder->appendFormat(")((%s(%s, BYTES(%s) + %d) >> %d)",
helper,
program->packetStartVar.c_str(),
program->offsetVar.c_str(), i, shift);
if ((i == bytes - 1) && (widthToExtract % 8 != 0)) {
builder->append(" & EBPF_MASK(");
bt->emit(builder);
builder->appendFormat(", %d)", widthToExtract % 8);
}
builder->append(")");
builder->endOfStatement(true);
}
}
builder->emitIndent();
builder->appendFormat("%s += %d", program->offsetVar.c_str(), widthToExtract);
builder->endOfStatement(true);
builder->newline();
}
void
StateTranslationVisitor::compileExtract(const IR::Expression* destination) {
auto type = state->parser->typeMap->getType(destination);
auto ht = type->to<IR::Type_StructLike>();
if (ht == nullptr) {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"Cannot extract to a non-struct type %1%", destination);
return;
}
unsigned width = ht->width_bits();
auto program = state->parser->program;
builder->emitIndent();
builder->appendFormat("if (%s < %s + BYTES(%s + %d)) ",
program->packetEndVar.c_str(),
program->packetStartVar.c_str(),
program->offsetVar.c_str(), width);
builder->blockStart();
builder->emitIndent();
builder->appendFormat("%s = %s;", program->errorVar.c_str(),
p4lib.packetTooShort.str());
builder->newline();
builder->emitIndent();
builder->appendFormat("goto %s;", IR::ParserState::reject.c_str());
builder->newline();
builder->blockEnd(true);
unsigned alignment = 0;
for (auto f : ht->fields) {
auto ftype = state->parser->typeMap->getType(f);
auto etype = EBPFTypeFactory::instance->create(ftype);
auto et = dynamic_cast<IHasWidth*>(etype);
if (et == nullptr) {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"Only headers with fixed widths supported %1%", f);
return;
}
compileExtractField(destination, f->name, alignment, etype);
alignment += et->widthInBits();
alignment %= 8;
}
if (ht->is<IR::Type_Header>()) {
builder->emitIndent();
visit(destination);
builder->appendLine(".ebpf_valid = 1;");
}
}
bool StateTranslationVisitor::preorder(const IR::MethodCallExpression* expression) {
builder->append("/* ");
visit(expression->method);
builder->append("(");
bool first = true;
for (auto a : *expression->arguments) {
if (!first)
builder->append(", ");
first = false;
visit(a);
}
builder->append(")");
builder->append("*/");
builder->newline();
auto mi = P4::MethodInstance::resolve(expression,
state->parser->program->refMap,
state->parser->program->typeMap);
if (auto extMethod = mi->to<P4::ExternMethod>()) {
auto decl = extMethod->object;
if (decl == state->parser->packet) {
if (extMethod->method->name.name == p4lib.packetIn.extract.name) {
if (expression->arguments->size() != 1) {
::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"Variable-sized header fields not yet supported %1%", expression);
return false;
}
compileExtract(expression->arguments->at(0)->expression);
return false;
}
BUG("Unhandled packet method %1%", expression->method);
return false;
}
} else if (auto bim = mi->to<P4::BuiltInMethod>()) {
builder->emitIndent();
if (bim->name == IR::Type_Header::isValid) {
visit(bim->appliedTo);
builder->append(".ebpf_valid");
return false;
} else if (bim->name == IR::Type_Header::setValid) {
visit(bim->appliedTo);
builder->append(".ebpf_valid = true");
return false;
} else if (bim->name == IR::Type_Header::setInvalid) {
visit(bim->appliedTo);
builder->append(".ebpf_valid = false");
return false;
}
}
::error(ErrorType::ERR_UNEXPECTED,
"Unexpected method call in parser %1%", expression);
return false;
}
bool StateTranslationVisitor::preorder(const IR::Member* expression) {
if (expression->expr->is<IR::PathExpression>()) {
auto pe = expression->expr->to<IR::PathExpression>();
auto decl = state->parser->program->refMap->getDeclaration(pe->path, true);
if (decl == state->parser->packet) {
builder->append(expression->member);
return false;
}
}
visit(expression->expr);
builder->append(".");
builder->append(expression->member);
return false;
}
//////////////////////////////////////////////////////////////////
void EBPFParserState::emit(CodeBuilder* builder) {
StateTranslationVisitor visitor(this);
visitor.setBuilder(builder);
state->apply(visitor);
}
EBPFParser::EBPFParser(const EBPFProgram* program, const IR::ParserBlock* block,
const P4::TypeMap* typeMap) :
program(program), typeMap(typeMap), parserBlock(block),
packet(nullptr), headers(nullptr), headerType(nullptr) {}
void EBPFParser::emitDeclaration(CodeBuilder* builder, const IR::Declaration* decl) {
if (decl->is<IR::Declaration_Variable>()) {
auto vd = decl->to<IR::Declaration_Variable>();
auto etype = EBPFTypeFactory::instance->create(vd->type);
builder->emitIndent();
etype->declare(builder, vd->name, false);
builder->endOfStatement(true);
BUG_CHECK(vd->initializer == nullptr,
"%1%: declarations with initializers not supported", decl);
return;
}
BUG("%1%: not yet handled", decl);
}
void EBPFParser::emit(CodeBuilder* builder) {
for (auto l : parserBlock->container->parserLocals)
emitDeclaration(builder, l);
for (auto s : states)
s->emit(builder);
builder->newline();
// Create a synthetic reject state
builder->emitIndent();
builder->appendFormat("%s: { return %s; }",
IR::ParserState::reject.c_str(),
builder->target->abortReturnCode().c_str());
builder->newline();
builder->newline();
}
bool EBPFParser::build() {
auto pl = parserBlock->container->type->applyParams;
if (pl->size() != 2) {
::error(ErrorType::ERR_EXPECTED,
"Expected parser to have exactly 2 parameters");
return false;
}
auto it = pl->parameters.begin();
packet = *it; ++it;
headers = *it;
for (auto state : parserBlock->container->states) {
auto ps = new EBPFParserState(state, this);
states.push_back(ps);
}
auto ht = typeMap->getType(headers);
if (ht == nullptr)
return false;
headerType = EBPFTypeFactory::instance->create(ht);
return true;
}
} // namespace EBPF