forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstf.cpp
More file actions
250 lines (220 loc) · 9.63 KB
/
stf.cpp
File metadata and controls
250 lines (220 loc) · 9.63 KB
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
#include "backends/p4tools/modules/testgen/targets/pna/backend/stf/stf.h"
#include <fstream>
#include <iomanip>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/detail/et_ops.hpp>
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/traits/explicit_conversion.hpp>
#include <inja/inja.hpp>
#include "backends/p4tools/common/lib/format_int.h"
#include "backends/p4tools/common/lib/util.h"
#include "ir/ir.h"
#include "ir/irutils.h"
#include "lib/exceptions.h"
#include "lib/log.h"
#include "nlohmann/json.hpp"
#include "backends/p4tools/modules/testgen/lib/exceptions.h"
#include "backends/p4tools/modules/testgen/targets/pna/test_spec.h"
namespace P4::P4Tools::P4Testgen::Pna {
STF::STF(const TestBackendConfiguration &testBackendConfiguration)
: TestFramework(testBackendConfiguration) {}
inja::json STF::getSend(const TestSpec *testSpec) {
const auto *iPacket = testSpec->getIngressPacket();
const auto *payload = iPacket->getEvaluatedPayload();
inja::json sendJson;
sendJson["ig_port"] = iPacket->getPort();
sendJson["pkt"] = formatHexExpr(payload, {false, true, false});
sendJson["pkt_size"] = payload->type->width_bits();
return sendJson;
}
inja::json STF::getExpectedPacket(const TestSpec *testSpec) {
inja::json verifyData = inja::json::object();
if (testSpec->getEgressPacket() != std::nullopt) {
const auto &packet = **testSpec->getEgressPacket();
verifyData["eg_port"] = packet.getPort();
const auto *payload = packet.getEvaluatedPayload();
const auto *payloadMask = packet.getEvaluatedPayloadMask();
auto dataStr = formatHexExpr(payload, {false, true, false});
if (payloadMask != nullptr) {
auto maskStr = formatHexExpr(payloadMask, {false, true, false});
std::string packetData;
for (size_t dataPos = 0; dataPos < dataStr.size(); ++dataPos) {
if (maskStr.at(dataPos) != 'F') {
packetData += "*";
} else {
packetData += dataStr[dataPos];
}
}
verifyData["exp_pkt"] = packetData;
} else {
verifyData["exp_pkt"] = dataStr;
}
}
return verifyData;
}
inja::json STF::getControlPlaneForTable(const TableMatchMap &matches,
const std::vector<ActionArg> &args) {
inja::json rulesJson;
rulesJson["matches"] = inja::json::array();
rulesJson["act_args"] = inja::json::array();
rulesJson["needs_priority"] = false;
for (const auto &match : matches) {
const auto fieldName = match.first;
const auto &fieldMatch = match.second;
inja::json j;
j["field_name"] = fieldName;
if (const auto *elem = fieldMatch->to<Exact>()) {
j["value"] = formatHexExpr(elem->getEvaluatedValue());
} else if (const auto *elem = fieldMatch->to<Ternary>()) {
const auto *dataValue = elem->getEvaluatedValue();
const auto *maskField = elem->getEvaluatedMask();
BUG_CHECK(dataValue->type->width_bits() == maskField->type->width_bits(),
"Data value and its mask should have the same bit width.");
auto dataStr = formatBinExpr(dataValue, {false, true, false});
auto maskStr = formatBinExpr(maskField, {false, true, false});
std::string data = "0b";
for (size_t dataPos = 0; dataPos < dataStr.size(); ++dataPos) {
if (maskStr.at(dataPos) == '0') {
data += "*";
} else {
data += dataStr.at(dataPos);
}
}
j["value"] = data;
rulesJson["needs_priority"] = true;
} else if (const auto *elem = fieldMatch->to<LPM>()) {
const auto *dataValue = elem->getEvaluatedValue();
auto prefixLen = elem->getEvaluatedPrefixLength()->asInt();
auto fieldWidth = dataValue->type->width_bits();
auto maxVal = IR::getMaxBvVal(prefixLen);
const auto *maskField =
IR::Constant::get(dataValue->type, maxVal << (fieldWidth - prefixLen));
BUG_CHECK(dataValue->type->width_bits() == maskField->type->width_bits(),
"Data value and its mask should have the same bit width.");
auto dataStr = formatBinExpr(dataValue, {false, true, false});
auto maskStr = formatBinExpr(maskField, {false, true, false});
std::string data = "0b";
for (size_t dataPos = 0; dataPos < dataStr.size(); ++dataPos) {
if (maskStr.at(dataPos) == '0') {
data += "*";
} else {
data += dataStr.at(dataPos);
}
}
j["value"] = data;
rulesJson["needs_priority"] = true;
} else if (const auto *elem = fieldMatch->to<Optional>()) {
j["value"] = formatHexExpr(elem->getEvaluatedValue()).c_str();
rulesJson["needs_priority"] = true;
} else {
TESTGEN_UNIMPLEMENTED("Unsupported table key match type \"%1%\"",
fieldMatch->getObjectName());
}
rulesJson["matches"].push_back(j);
}
for (const auto &actArg : args) {
inja::json j;
j["param"] = actArg.getActionParamName().c_str();
j["value"] = formatHexExpr(actArg.getEvaluatedValue());
rulesJson["act_args"].push_back(j);
}
return rulesJson;
}
inja::json STF::getControlPlane(const TestSpec *testSpec) {
inja::json controlPlaneJson = inja::json::object();
auto tables = testSpec->getTestObjectCategory("tables"_cs);
if (!tables.empty()) {
controlPlaneJson["tables"] = inja::json::array();
}
for (const auto &testObject : tables) {
inja::json tblJson;
tblJson["table_name"] = testObject.first.c_str();
const auto *const tblConfig = testObject.second->checkedTo<TableConfig>();
const auto *tblRules = tblConfig->getRules();
tblJson["rules"] = inja::json::array();
for (const auto &tblRule : *tblRules) {
inja::json rule;
const auto *matches = tblRule.getMatches();
const auto *actionCall = tblRule.getActionCall();
const auto *actionArgs = actionCall->getArgs();
rule["action_name"] = actionCall->getActionName().c_str();
auto j = getControlPlaneForTable(*matches, *actionArgs);
rule["rules"] = std::move(j);
rule["priority"] = tblRule.getPriority();
tblJson["rules"].push_back(rule);
}
checkForDefaultActionOverride(tblJson, tblConfig);
controlPlaneJson["tables"].push_back(tblJson);
}
return controlPlaneJson;
}
std::string STF::getTestCaseTemplate() {
static std::string TEST_CASE(
R"""(# p4testgen seed: {{ default(seed, "none") }}
# Date generated: {{timestamp}}
## if length(selected_branches) > 0
# {{selected_branches}}
## endif
# Current node coverage: {{coverage}}
# Traces
## for trace_item in trace
# {{trace_item}}
##endfor
## if control_plane
## for table in control_plane.tables
# Table {{table.table_name}}
## if existsIn(table, "default_override")
setdefault "{{table.table_name}}" "{{table.default_override.action_name}}"({% for a in table.default_override.act_args %}"{{a.param}}":{{a.value}}{% if not loop.is_last %},{% endif %}{% endfor %})
## else
## for rule in table.rules
add "{{table.table_name}}" {% if rule.rules.needs_priority %}{{rule.priority}} {% endif %}{% for r in rule.rules.matches %}"{{r.field_name}}":{{r.value}} {% endfor %}"{{rule.action_name}}"({% for a in rule.rules.act_args %}"{{a.param}}":{{a.value}}{% if not loop.is_last %},{% endif %}{% endfor %})
## endfor
## endif
## endfor
## endif
packet {{send.ig_port}} {{send.pkt}}
## if verify
expect {{verify.eg_port}} {{verify.exp_pkt}}$
## endif
)""");
return TEST_CASE;
}
void STF::emitTestcase(const TestSpec *testSpec, cstring selectedBranches, size_t testId,
const std::string &testCase, float currentCoverage) {
inja::json dataJson;
if (selectedBranches != nullptr) {
dataJson["selected_branches"] = selectedBranches.c_str();
}
auto optSeed = getTestBackendConfiguration().seed;
if (optSeed.has_value()) {
dataJson["seed"] = optSeed.value();
}
dataJson["test_id"] = testId;
dataJson["trace"] = getTrace(testSpec);
dataJson["control_plane"] = getControlPlane(testSpec);
dataJson["send"] = getSend(testSpec);
dataJson["verify"] = getExpectedPacket(testSpec);
dataJson["timestamp"] = Utils::getTimeStamp();
std::stringstream coverageStr;
coverageStr << std::setprecision(2) << currentCoverage;
dataJson["coverage"] = coverageStr.str();
LOG5("STF test back end: emitting testcase:" << std::setw(4) << dataJson);
auto optBasePath = getTestBackendConfiguration().fileBasePath;
BUG_CHECK(optBasePath.has_value(), "Base path is not set.");
auto incrementedbasePath = optBasePath.value();
incrementedbasePath.concat("_" + std::to_string(testId));
incrementedbasePath.replace_extension(".stf");
auto stfFileStream = std::ofstream(incrementedbasePath);
inja::render_to(stfFileStream, testCase, dataJson);
stfFileStream.flush();
}
void STF::writeTestToFile(const TestSpec *testSpec, cstring selectedBranches, size_t testId,
float currentCoverage) {
std::string testCase = getTestCaseTemplate();
emitTestcase(testSpec, selectedBranches, testId, testCase, currentCoverage);
}
} // namespace P4::P4Tools::P4Testgen::Pna