This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
xdpProgram.cpp
345 lines (292 loc) · 11.8 KB
/
xdpProgram.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
/*
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 "backends/ebpf/ebpfType.h"
#include "backends/ebpf/ebpfControl.h"
#include "backends/ebpf/ebpfParser.h"
#include "backends/ebpf/ebpfTable.h"
#include "frontends/p4/coreLibrary.h"
#include "xdpProgram.h"
#include "xdpControl.h"
namespace XDP {
bool XDPProgram::build() {
auto pack = toplevel->getMain();
unsigned paramCount = pack->getConstructorParameters()->size();
cstring parserParamName;
if (paramCount == 2) {
parserParamName = model.filter.parser.name;
} else if (paramCount == 3) {
parserParamName = xdp_model.xdp.parser.name;
} else {
::error(ErrorType::ERR_EXPECTED,
"%1%: Expected 2 or 3 package parameters", pack);
}
auto pb = pack->getParameterValue(parserParamName)
->to<IR::ParserBlock>();
BUG_CHECK(pb != nullptr, "No parser block found");
parser = new EBPF::EBPFParser(this, pb, typeMap);
bool success = parser->build();
if (!success)
return success;
if (paramCount == 2) {
cstring controlParamName = model.filter.filter.name;
auto cb = pack->getParameterValue(controlParamName)
->to<IR::ControlBlock>();
BUG_CHECK(cb != nullptr, "No control block found");
control = new EBPF::EBPFControl(this, cb, parser->headers);
success = control->build();
if (!success)
return success;
} else {
cstring controlParamName = xdp_model.xdp.swtch.name;
auto cb = pack->getParameterValue(controlParamName)
->to<IR::ControlBlock>();
BUG_CHECK(cb != nullptr, "No control block found");
control = new XDPSwitch(this, cb, parser->headers);
success = control->build();
if (!success)
return success;
}
if (paramCount == 3) {
auto db = pack->getParameterValue(xdp_model.xdp.deparser.name)
->to<IR::ControlBlock>();
BUG_CHECK(db != nullptr, "No deparser block found");
deparser = new XDPDeparser(this, db, parser->headers);
success = deparser->build();
if (!success)
return success;
}
return true;
}
void XDPProgram::emitTypes(EBPF::CodeBuilder* builder) {
for (auto d : program->objects) {
if (!d->is<IR::Type>()) continue;
if (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>())
continue;
if (d->is<IR::Type_Enum>()) {
if (d->to<IR::Type_Enum>()->name == XDPModel::instance.action_enum.name)
continue;
}
auto type = EBPF::EBPFTypeFactory::instance->create(d->to<IR::Type>());
if (type == nullptr)
continue;
type->emit(builder);
builder->newline();
}
}
void XDPProgram::emitC(EBPF::CodeBuilder* builder, cstring headerFile) {
emitGeneratedComment(builder);
if (!switchTarget()) {
EBPF::EBPFProgram::emitC(builder, headerFile);
return;
}
if (builder->target->name != "XDP") {
::error(ErrorType::ERR_EXPECTED,
"This program must be compiled with --target xdp");
return;
}
builder->appendFormat("#include \"%s\"", headerFile);
builder->newline();
builder->target->emitIncludes(builder);
emitPreamble(builder);
control->emitTableInstances(builder);
builder->appendLine(
"inline u16 ebpf_ipv4_checksum(u8 version, u8 ihl, u8 diffserv,\n"
" u16 totalLen, u16 identification, u8 flags,\n"
" u16 fragOffset, u8 ttl, u8 protocol,\n"
" u32 srcAddr, u32 dstAddr) {\n"
" u32 checksum = __bpf_htons(((u16)version << 12) | ((u16)ihl << 8) | (u16)diffserv);\n"
" checksum += __bpf_htons(totalLen);\n"
" checksum += __bpf_htons(identification);\n"
" checksum += __bpf_htons(((u16)flags << 13) | fragOffset);\n"
" checksum += __bpf_htons(((u16)ttl << 8) | (u16)protocol);\n"
" srcAddr = __bpf_ntohl(srcAddr);\n"
" dstAddr = __bpf_ntohl(dstAddr);\n"
" checksum += (srcAddr >> 16) + (u16)srcAddr;\n"
" checksum += (dstAddr >> 16) + (u16)dstAddr;\n"
" // Fields in 'struct Headers' are host byte order.\n"
" // Deparser converts to network byte-order\n"
" return bpf_ntohs(~((checksum & 0xFFFF) + (checksum >> 16)));\n"
"}");
builder->appendLine(
"inline u16 csum16_add(u16 csum, u16 addend) {\n"
" u16 res = csum;\n"
" res += addend;\n"
" return (res + (res < addend));\n"
"}\n"
"inline u16 csum16_sub(u16 csum, u16 addend) {\n"
" return csum16_add(csum, ~addend);\n"
"}\n"
"inline u16 csum_replace2(u16 csum, u16 old, u16 new) {\n"
" return (~csum16_add(csum16_sub(~csum, old), new));\n"
"}\n");
builder->appendLine(
"inline u16 csum_fold(u32 csum) {\n"
" u32 r = csum << 16 | csum >> 16;\n"
" csum = ~csum;\n"
" csum -= r;\n"
" return (u16)(csum >> 16);\n"
"}\n"
"inline u32 csum_unfold(u16 csum) {\n"
" return (u32)csum;\n"
"}\n"
"inline u32 csum32_add(u32 csum, u32 addend) {\n"
" u32 res = csum;\n"
" res += addend;\n"
" return (res + (res < addend));\n"
"}\n"
"inline u32 csum32_sub(u32 csum, u32 addend) {\n"
" return csum32_add(csum, ~addend);\n"
"}\n"
"inline u16 csum_replace4(u16 csum, u32 from, u32 to) {\n"
" u32 tmp = csum32_sub(~csum_unfold(csum), from);\n"
" return csum_fold(csum32_add(tmp, to));\n"
"}\n");
builder->appendLine(
"struct bpf_elf_map SEC(\"maps\") perf_event = {\n"
" .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,\n"
" .size_key = sizeof(u32),\n"
" .size_value = sizeof(u32),\n"
" .pinning = 1,\n"
" .max_elem = 2,\n"
"};\n"
"#define BPF_PERF_EVENT_OUTPUT() do {\\\n"
" int pktsize = (int)(skb->data_end - skb->data);\\\n"
" bpf_perf_event_output(skb, &perf_event, ((u64)pktsize << 32), &pktsize, 4);\\\n"
"} while(0);\n");
builder->appendLine(
"#define BPF_KTIME_GET_NS() ({\\\n"
" u32 ___ts = (u32)bpf_ktime_get_ns(); ___ts; })\\\n");
// The table used for forwarding: we write the output in it
// TODO: this should use target->emitTableDecl().
// We can't do it today because it has a different map type PERCPU_ARRAY
builder->emitIndent();
builder->appendFormat("struct bpf_elf_map SEC(\"maps\") %s = ", outTableName.c_str());
builder->blockStart();
builder->emitIndent();
builder->append(".type = ");
builder->appendLine("BPF_MAP_TYPE_PERCPU_ARRAY,");
builder->emitIndent();
builder->append(".size_key = sizeof(u32),");
builder->newline();
builder->emitIndent();
builder->appendFormat(".size_value = sizeof(u32),");
builder->newline();
builder->emitIndent();
builder->appendFormat(".pinning = 2, /* PIN_OBJECT_NS */");
builder->newline();
builder->emitIndent();
builder->appendFormat(".max_elem = 1 /* No multicast support */");
builder->newline();
builder->blockEnd(false);
builder->endOfStatement(true);
builder->newline();
builder->emitIndent();
builder->target->emitCodeSection(builder, "prog");
builder->emitIndent();
builder->target->emitMain(builder, functionName, model.CPacketName.str());
builder->blockStart();
emitHeaderInstances(builder);
builder->append(" = ");
parser->headerType->emitInitializer(builder);
builder->endOfStatement(true);
emitLocalVariables(builder);
builder->newline();
builder->emitIndent();
builder->appendFormat("goto %s;", IR::ParserState::start.c_str());
builder->newline();
parser->emit(builder);
emitPipeline(builder);
builder->emitIndent();
builder->append(endLabel);
builder->appendLine(":");
// write output port to a table
builder->emitIndent();
builder->appendFormat("bpf_map_update_elem(&%s, &%s, &%s.%s, BPF_ANY)",
outTableName.c_str(), zeroKey.c_str(),
getSwitch()->outputMeta->name.name,
XDPModel::instance.outputMetadataModel.outputPort.str());
builder->endOfStatement(true);
builder->emitIndent();
builder->appendFormat("return %s.%s",
getSwitch()->outputMeta->name.name,
XDPModel::instance.outputMetadataModel.output_action.str());
builder->endOfStatement(true);
builder->blockEnd(true); // end of function
builder->target->emitLicense(builder, license);
}
void XDPProgram::emitPipeline(EBPF::CodeBuilder* builder) {
builder->emitIndent();
builder->append(IR::ParserState::accept);
builder->append(":");
builder->newline();
builder->emitIndent();
builder->blockStart();
control->emit(builder);
builder->blockEnd(true);
if (switchTarget()) {
builder->emitIndent();
builder->append("/* deparser */");
builder->newline();
builder->emitIndent();
builder->blockStart();
deparser->emit(builder);
builder->blockEnd(true);
}
}
void XDPProgram::emitLocalVariables(EBPF::CodeBuilder* builder) {
if (!switchTarget()) {
EBPF::EBPFProgram::emitLocalVariables(builder);
return;
}
builder->emitIndent();
builder->appendFormat("unsigned %s = 0;", offsetVar);
builder->newline();
builder->emitIndent();
builder->appendFormat("enum %s %s = %s;", errorEnum, errorVar,
P4::P4CoreLibrary::instance.noError.str());
builder->newline();
builder->emitIndent();
builder->appendFormat("void* %s = %s;",
packetStartVar, builder->target->dataOffset(model.CPacketName.str()));
builder->newline();
builder->emitIndent();
builder->appendFormat("void* %s = %s;",
packetEndVar, builder->target->dataEnd(model.CPacketName.str()));
builder->newline();
builder->emitIndent();
builder->appendFormat("u32 %s = 0;", zeroKey);
builder->newline();
builder->emitIndent();
builder->appendFormat("u8 %s = 0;", byteVar);
builder->newline();
builder->emitIndent();
builder->appendFormat("u32 %s = 0;", outHeaderLengthVar);
builder->newline();
builder->emitIndent();
builder->appendFormat("struct %s %s;", xdp_model.outputMetadataModel.name,
getSwitch()->outputMeta->name.name);
builder->newline();
builder->emitIndent();
builder->appendFormat("/* TODO: this should be initialized by the environment. HOW? */");
builder->newline();
builder->emitIndent();
builder->appendFormat("struct %s %s;", xdp_model.inputMetadataModel.name,
getSwitch()->inputMeta->name.name);
builder->newline();
}
XDPSwitch* XDPProgram::getSwitch() const {
return dynamic_cast<XDPSwitch*>(control);
}
} // namespace XDP