Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPDK Backend: Add support for tdi.json #3440

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion backends/dpdk/control-plane/bfruntime_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,25 @@ const Util::JsonObject*
BFRuntimeSchemaGenerator::genSchema() const {
auto* json = new Util::JsonObject();

json->emplace("schema_version", cstring("1.0.0"));
if (isTDI) {
cstring progName = options.file;
auto fileName = progName.findlast('/');
// Handle the case when input file is in the current working directory.
// fileName would be null in that case, hence progName should remain unchanged.
if (fileName)
progName = fileName;
auto fileext = progName.find(".");
progName = progName.replace(fileext, "");
progName = progName.trim("/\t\n\r");
json->emplace("program_name", progName);
json->emplace("build_date", cstring(options.getBuildDate()));
json->emplace("compile_command", cstring(options.getCompileCommand()));
json->emplace("compiler_version", cstring(options.compilerVersion));
json->emplace("schema_version", cstring("0.1"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a constant instead of a literal?

json->emplace("target", cstring("DPDK"));
} else {
json->emplace("schema_version", cstring("1.0.0"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version strings can have more than one decimal point. So I think a string literal should be ok here.
Do you mean instead of string literal, we should use a const string type variable to hold the version string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a const string, in one place.

}

auto* tablesJson = new Util::JsonArray();
json->emplace("tables", tablesJson);
Expand Down
9 changes: 6 additions & 3 deletions backends/dpdk/control-plane/bfruntime_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ limitations under the License.
#ifndef DPDK_CONTROL_PLANE_BFRUNTIME_EXT_H_
#define DPDK_CONTROL_PLANE_BFRUNTIME_EXT_H_

#include "backends/dpdk/options.h"
#include "control-plane/bfruntime.h"
#include "p4/config/dpdk/p4info.pb.h"

namespace P4 {

namespace BFRT {
Expand All @@ -27,13 +27,16 @@ namespace BFRT {
/// the context of P4Runtime to the BF-RT info JSON used by the BF-RT API.
class BFRuntimeSchemaGenerator : public BFRuntimeGenerator {
public:
explicit BFRuntimeSchemaGenerator(const p4configv1::P4Info& p4info)
: BFRuntimeGenerator(p4info) { }
BFRuntimeSchemaGenerator(const p4configv1::P4Info& p4info, bool isTDI,
DPDK::DpdkOptions &options)
: BFRuntimeGenerator(p4info), isTDI(isTDI), options(options) { }

/// Generates the schema as a Json object for the provided P4Info instance.
const Util::JsonObject* genSchema() const override;

private:
bool isTDI;
DPDK::DpdkOptions &options;
// TODO(antonin): these values may need to be available to the BF-RT
// implementation as well, if they want to expose them as enums.

Expand Down
44 changes: 28 additions & 16 deletions backends/dpdk/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ limitations under the License.
#include "lib/log.h"
#include "lib/nullstream.h"

void generateTDIBfrtJson(bool isTDI, const IR::P4Program *program, DPDK::DpdkOptions &options) {
auto p4RuntimeSerializer = P4::P4RuntimeSerializer::get();
if (options.arch == "psa")
p4RuntimeSerializer->registerArch("psa",
new P4::ControlPlaneAPI::Standard::PSAArchHandlerBuilderForDPDK());
if (options.arch == "pna")
p4RuntimeSerializer->registerArch("pna",
new P4::ControlPlaneAPI::Standard::PNAArchHandlerBuilderForDPDK());
auto p4Runtime = P4::generateP4Runtime(program, options.arch);

cstring filename = isTDI ? options.tdiFile : options.bfRtSchema;
auto p4rt = new P4::BFRT::BFRuntimeSchemaGenerator(*p4Runtime.p4Info, isTDI, options);
std::ostream* out = openFile(filename, false);
if (!out) {
::error(ErrorType::ERR_IO,
"Could not open file: %1%", filename);
return;
}
p4rt->serializeBFRuntimeSchema(out);
}

int main(int argc, char *const argv[]) {
setup_gc_logging();

Expand Down Expand Up @@ -99,23 +120,14 @@ int main(int argc, char *const argv[]) {
return 1;

if (!options.bfRtSchema.isNullOrEmpty()) {
auto p4RuntimeSerializer = P4::P4RuntimeSerializer::get();
if (options.arch == "psa")
p4RuntimeSerializer->registerArch("psa",
new P4::ControlPlaneAPI::Standard::PSAArchHandlerBuilderForDPDK());
if (options.arch == "pna")
p4RuntimeSerializer->registerArch("pna",
new P4::ControlPlaneAPI::Standard::PNAArchHandlerBuilderForDPDK());
auto p4Runtime = P4::generateP4Runtime(program, options.arch);
auto p4rt = new P4::BFRT::BFRuntimeSchemaGenerator(*p4Runtime.p4Info);
std::ostream* out = openFile(options.bfRtSchema, false);
if (!out) {
::error(ErrorType::ERR_IO,
"Could not open BF-RT schema file: %1%", options.bfRtSchema);
return 1;
}
p4rt->serializeBFRuntimeSchema(out);
generateTDIBfrtJson(false, program, options);
}
if (!options.tdiFile.isNullOrEmpty()) {
generateTDIBfrtJson(true, program, options);
}

if (::errorCount() > 0)
return 1;

DPDK::DpdkMidEnd midEnd(options);
midEnd.addDebugHook(hook);
Expand Down
5 changes: 5 additions & 0 deletions backends/dpdk/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class DpdkOptions : public CompilerOptions {
cstring bfRtSchema = "";
// file to output to
cstring outputFile = nullptr;
// file to ouput TDI Json to
cstring tdiFile = "";
// file to ouput context Json to
cstring ctxtFile = "";
// read from json
Expand Down Expand Up @@ -57,6 +59,9 @@ class DpdkOptions : public CompilerOptions {
registerOption("-o", "outfile",
[this](const char* arg) { outputFile = arg; return true; },
"Write output to outfile");
registerOption("--tdi", "file",
[this](const char *arg) { tdiFile = arg; return true; },
"Generate and write TDI JSON to the specified file");
registerOption("--context", "file",
[this](const char *arg) { ctxtFile = arg; return true; },
"Generate and write context JSON to the specified file");
Expand Down