-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtsmith.cpp
238 lines (203 loc) · 8.96 KB
/
rtsmith.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
#include "backends/p4tools/modules/rtsmith/rtsmith.h"
#include <google/protobuf/text_format.h>
#include <cstddef>
#include <cstdlib>
#include <filesystem>
#include "backends/p4tools/common/compiler/compiler_result.h"
#include "backends/p4tools/common/lib/logging.h"
#include "backends/p4tools/common/lib/util.h"
#include "backends/p4tools/modules/rtsmith/core/target.h"
#include "backends/p4tools/modules/rtsmith/core/util.h"
#include "backends/p4tools/modules/rtsmith/register.h"
#include "backends/p4tools/modules/rtsmith/toolname.h"
#include "control-plane/p4RuntimeSerializer.h"
#include "lib/error.h"
#include "lib/nullstream.h"
namespace P4::P4Tools::RtSmith {
void RtSmith::registerTarget() {
// Register all available compiler targets.
// These are discovered by CMAKE, which fills out the register.h.in file.
registerRtSmithTargets();
}
namespace {
std::optional<RtSmithResult> runRtSmith(const CompilerResult &rtSmithResult,
const RtSmithOptions &rtSmithOptions) {
const auto *programInfo = RtSmithTarget::produceProgramInfo(rtSmithResult, rtSmithOptions);
if (programInfo == nullptr) {
error("Program not supported by target device and architecture.");
return std::nullopt;
}
if (errorCount() > 0) {
error("P4RuntimeSmith: Encountered errors during preprocessing. Exiting");
return std::nullopt;
}
auto p4RuntimeApi = programInfo->getP4RuntimeApi();
// printInfo("Inferred API:\n%1%", p4RuntimeApi.p4Info->DebugString());
if (rtSmithOptions.p4InfoFilePath().has_value()) {
auto *outputFile = openFile(rtSmithOptions.p4InfoFilePath().value(), true);
if (outputFile == nullptr) {
return std::nullopt;
}
p4RuntimeApi.serializeP4InfoTo(outputFile, P4::P4RuntimeFormat::TEXT_PROTOBUF);
}
auto &fuzzer = RtSmithTarget::getFuzzer(*programInfo);
auto initialConfig = fuzzer.produceInitialConfig();
auto timeSeriesUpdates = fuzzer.produceUpdateTimeSeries();
if (rtSmithOptions.printToStdout()) {
printInfo("Generated initial configuration:");
for (const auto &writeRequest : initialConfig) {
printInfo("%1%", writeRequest->DebugString());
}
printInfo("Time series updates:");
for (const auto &[time, writeRequest] : timeSeriesUpdates) {
printInfo("Time %1%:\n%2%", writeRequest->DebugString());
}
}
auto dirPath = rtSmithOptions.outputDir();
if (!dirPath.empty()) {
if (!std::filesystem::exists(dirPath)) {
if (!std::filesystem::create_directory(dirPath)) {
error("P4RuntimeSmith: Failed to create output directory. Exiting");
return std::nullopt;
}
}
auto initialConfigPath = dirPath;
if (rtSmithOptions.configName().has_value()) {
initialConfigPath = initialConfigPath / rtSmithOptions.configName().value();
initialConfigPath = initialConfigPath.replace_extension("initial_config");
} else {
initialConfigPath = initialConfigPath / "initial_config";
}
initialConfigPath = initialConfigPath.replace_extension(".txtpb");
auto *outputFile = openFile(initialConfigPath, true);
if (outputFile == nullptr) {
error("P4RuntimeSmith: Config file path doesn't exist. Exiting");
return std::nullopt;
}
for (const auto &writeRequest : initialConfig) {
std::string output;
google::protobuf::TextFormat::Printer textPrinter;
textPrinter.SetExpandAny(true);
if (!textPrinter.PrintToString(*writeRequest, &output)) {
error(ErrorType::ERR_IO, "Failed to serialize protobuf message to text");
return std::nullopt;
}
*outputFile << output;
if (!outputFile->good()) {
error(ErrorType::ERR_IO, "Failed to write text protobuf message to the output");
return std::nullopt;
}
printInfo("Wrote initial configuration to %1%", initialConfigPath);
outputFile->flush();
}
for (size_t idx = 0; idx < timeSeriesUpdates.size(); ++idx) {
const auto &[microseconds, writeRequest] = timeSeriesUpdates.at(idx);
std::string output;
google::protobuf::TextFormat::Printer textPrinter;
textPrinter.SetExpandAny(true);
if (!textPrinter.PrintToString(*writeRequest, &output)) {
error(ErrorType::ERR_IO, "Failed to serialize protobuf message to text");
return std::nullopt;
}
auto updatePath = initialConfigPath;
updatePath.replace_filename("update_" + std::to_string(idx + 1));
updatePath.replace_extension(".txtpb");
auto *updateFile = openFile(updatePath, true);
if (updateFile == nullptr) {
error("P4RuntimeSmith: Update file path doesn't exist. Exiting");
return std::nullopt;
}
*updateFile << output;
printInfo("Wrote update to %1%", updatePath);
if (!updateFile->good()) {
error(ErrorType::ERR_IO, "Failed to write text protobuf message to the output");
return std::nullopt;
}
updateFile->flush();
}
}
return {{std::move(initialConfig), std::move(timeSeriesUpdates)}};
}
} // namespace
int RtSmith::mainImpl(const CompilerResult &compilerResult) {
// Register all available P4RuntimeSmith targets.
// These are discovered by CMAKE, which fills out the register.h.in file.
registerRtSmithTargets();
const auto &rtSmithOptions = RtSmithOptions::get();
auto result = runRtSmith(compilerResult, rtSmithOptions);
return (result.has_value() && errorCount() == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
std::optional<RtSmithResult> generateConfigImpl(
std::optional<std::reference_wrapper<const std::string>> program,
const RtSmithOptions &rtSmithOptions) {
// Register supported P4RTSmith targets.
registerRtSmithTargets();
P4Tools::Target::init(rtSmithOptions.target.c_str(), rtSmithOptions.arch.c_str());
CompilerResultOrError compilerResult;
if (program.has_value()) {
// Run the compiler to get an IR and invoke the tool.
ASSIGN_OR_RETURN(
compilerResult,
P4Tools::CompilerTarget::runCompiler(rtSmithOptions, TOOL_NAME, program->get()),
std::nullopt);
} else {
RETURN_IF_FALSE_WITH_MESSAGE(!rtSmithOptions.file.empty(), std::nullopt,
error("Expected a file input."));
// Run the compiler to get an IR and invoke the tool.
ASSIGN_OR_RETURN(compilerResult,
P4Tools::CompilerTarget::runCompiler(rtSmithOptions, TOOL_NAME),
std::nullopt);
}
return runRtSmith(compilerResult.value(), rtSmithOptions);
}
std::optional<RtSmithResult> RtSmith::generateConfig(const std::string &program,
const RtSmithOptions &rtSmithOptions) {
try {
return generateConfigImpl(program, rtSmithOptions);
} catch (const std::exception &e) {
std::cerr << "Internal error: " << e.what() << "\n";
return std::nullopt;
} catch (...) {
return std::nullopt;
}
return std::nullopt;
}
std::optional<RtSmithResult> RtSmith::generateConfig(const RtSmithOptions &rtSmithOptions) {
try {
return generateConfigImpl(std::nullopt, rtSmithOptions);
} catch (const std::exception &e) {
std::cerr << "Internal error: " << e.what() << "\n";
return std::nullopt;
} catch (...) {
return std::nullopt;
}
return std::nullopt;
}
std::optional<const P4::P4Tools::CompilerResult> RtSmith::generateCompilerResult(
std::optional<std::reference_wrapper<const std::string>> program,
const RtSmithOptions &rtSmithOptions) {
// Register supported P4RTSmith targets.
registerRtSmithTargets();
P4Tools::Target::init(rtSmithOptions.target.c_str(), rtSmithOptions.arch.c_str());
CompilerResultOrError compilerResult;
if (program.has_value()) {
// Run the compiler to get an IR and invoke the tool.
ASSIGN_OR_RETURN(
compilerResult,
P4Tools::CompilerTarget::runCompiler(rtSmithOptions, TOOL_NAME, program->get()),
std::nullopt);
} else {
RETURN_IF_FALSE_WITH_MESSAGE(!rtSmithOptions.file.empty(), std::nullopt,
error("Expected a file input."));
// Run the compiler to get an IR and invoke the tool.
ASSIGN_OR_RETURN(compilerResult,
P4Tools::CompilerTarget::runCompiler(rtSmithOptions, TOOL_NAME),
std::nullopt);
}
if (compilerResult.has_value()) {
return compilerResult.value();
} else {
return std::nullopt;
}
}
} // namespace P4::P4Tools::RtSmith