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
/
xdpControl.cpp
190 lines (156 loc) · 6.41 KB
/
xdpControl.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
/*
Copyright 2017 VMware, 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 "xdpControl.h"
#include "lib/error.h"
#include "backends/ebpf/ebpfControl.h"
namespace XDP {
XDPSwitch::XDPSwitch(const XDPProgram* program,
const IR::ControlBlock* block,
const IR::Parameter* parserHeaders) :
EBPF::EBPFControl(program, block, parserHeaders),
inputMeta(nullptr), outputMeta(nullptr) {}
bool XDPSwitch::build() {
hitVariable = program->refMap->newName("hit");
auto pl = controlBlock->container->type->applyParams;
if (pl->size() != 3) {
::error(ErrorType::ERR_EXPECTED,
"Expected switch block to have exactly 3 parameters");
return false;
}
auto it = pl->parameters.begin();
headers = *it;
++it;
inputMeta = *it;
++it;
outputMeta = *it;
codeGen = new EBPF::ControlBodyTranslator(this);
codeGen->substitute(headers, parserHeaders);
scanConstants();
return ::errorCount() == 0;
}
//////////////////////////////////////////////////////////////////////////
namespace {
class OutHeaderSize final : public EBPF::CodeGenInspector {
P4::ReferenceMap* refMap;
P4::TypeMap* typeMap;
const XDPProgram* program;
std::map<const IR::Parameter*, const IR::Parameter*> substitution;
bool illegal(const IR::Statement* statement)
{ ::error(ErrorType::ERR_UNSUPPORTED,
"%1%: not supported in deparser", statement); return false; }
public:
OutHeaderSize(P4::ReferenceMap* refMap, P4::TypeMap* typeMap,
const XDPProgram* program):
EBPF::CodeGenInspector(refMap, typeMap), refMap(refMap), typeMap(typeMap),
program(program) {
CHECK_NULL(refMap); CHECK_NULL(typeMap); CHECK_NULL(program);
setName("OutHeaderSize"); }
bool preorder(const IR::PathExpression* expression) override {
auto decl = refMap->getDeclaration(expression->path, true);
auto param = decl->getNode()->to<IR::Parameter>();
if (param != nullptr) {
auto subst = ::get(substitution, param);
if (subst != nullptr) {
builder->append(subst->name);
return false;
}
}
builder->append(expression->path->name);
return false;
}
bool preorder(const IR::SwitchStatement* statement) override
{ return illegal(statement); }
bool preorder(const IR::AssignmentStatement* statement) override
{ return illegal(statement); }
bool preorder(const IR::ReturnStatement* statement) override
{ return illegal(statement); }
bool preorder(const IR::ExitStatement* statement) override
{ return illegal(statement); }
bool preorder(const IR::MethodCallStatement* statement) override {
auto &p4lib = P4::P4CoreLibrary::instance;
auto mi = P4::MethodInstance::resolve(statement->methodCall, refMap, typeMap);
auto method = mi->to<P4::ExternMethod>();
if (method == nullptr)
return illegal(statement);
auto declType = method->originalExternType;
if (declType->name.name != p4lib.packetOut.name ||
method->method->name.name != p4lib.packetOut.emit.name ||
method->expr->arguments->size() != 1) {
return illegal(statement);
}
auto h = method->expr->arguments->at(0);
auto type = typeMap->getType(h);
auto ht = type->to<IR::Type_Header>();
if (ht == nullptr) {
::error(ErrorType::ERR_INVALID, "Cannot emit a non-header type %1%", h);
return false;
}
unsigned width = ht->width_bits();
builder->append("if (");
visit(h);
builder->append(".ebpf_valid) ");
builder->appendFormat("%s += %d;", program->outHeaderLengthVar.c_str(), width);
return false;
}
void substitute(const IR::Parameter* p, const IR::Parameter* with)
{ substitution.emplace(p, with); }
};
} // namespace
XDPDeparser::XDPDeparser(const XDPProgram* program, const IR::ControlBlock* block,
const IR::Parameter* parserHeaders) :
EBPF::EBPFControl(program, block, parserHeaders), packet(nullptr) {}
bool XDPDeparser::build() {
hitVariable = program->refMap->newName("hit");
auto pl = controlBlock->container->type->applyParams;
if (pl->size() != 2) {
::error(ErrorType::ERR_EXPECTED, "Expected switch block to have exactly 3 parameters");
return false;
}
auto it = pl->parameters.begin();
headers = *it;
++it;
packet = *it;
codeGen = new EBPF::ControlBodyTranslator(this);
codeGen->substitute(headers, parserHeaders);
return true;
}
void XDPDeparser::emit(EBPF::CodeBuilder* builder) {
OutHeaderSize ohs(program->refMap, program->typeMap,
static_cast<const XDPProgram*>(program));
ohs.substitute(headers, parserHeaders);
ohs.setBuilder(builder);
builder->emitIndent();
(void)controlBlock->container->body->apply(ohs);
builder->newline();
builder->emitIndent();
builder->appendFormat("bpf_xdp_adjust_head(%s, BYTES(%s) - BYTES(%s));",
program->model.CPacketName.str(),
program->offsetVar.c_str(),
getProgram()->outHeaderLengthVar.c_str());
builder->newline();
builder->emitIndent();
builder->appendFormat("%s = %s;",
program->packetStartVar,
builder->target->dataOffset(program->model.CPacketName.str()));
builder->newline();
builder->emitIndent();
builder->appendFormat("%s = %s;",
program->packetEndVar,
builder->target->dataEnd(program->model.CPacketName.str()));
builder->newline();
builder->emitIndent();
builder->appendFormat("%s = 0;", program->offsetVar.c_str());
builder->newline();
EBPF::EBPFControl::emit(builder);
}
} // namespace XDP