Skip to content

Commit

Permalink
Add support for multiple parsers / deparsers in bmv2 backend
Browse files Browse the repository at this point in the history
This is useful for PSA, for which there are 2 parsers and 2
deparsers.
The bmv2 PSA backend now uses the following names:
 * ingress_parser
 * egress_parser
 * ingress_deparser
 * egress_deparser
  • Loading branch information
antoninbas committed Feb 20, 2019
1 parent b7664d8 commit 5ae3904
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion backends/bmv2/common/deparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void DeparserConverter::convertDeparserBody(const IR::Vector<IR::StatOrDecl>* bo

Util::IJson* DeparserConverter::convertDeparser(const IR::P4Control* ctrl) {
auto result = new Util::JsonObject();
result->emplace("name", "deparser"); // at least in simple_router this name is hardwired
result->emplace("name", name);
result->emplace("id", nextId("deparser"));
result->emplace_non_null("source_info", ctrl->sourceInfoJsonObj());
auto order = mkArrayField(result, "order");
Expand Down
8 changes: 5 additions & 3 deletions backends/bmv2/common/deparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace BMV2 {

class DeparserConverter : public Inspector {
ConversionContext* ctxt;
cstring name;
P4::P4CoreLibrary& corelib;

protected:
Expand All @@ -36,9 +37,10 @@ class DeparserConverter : public Inspector {
public:
bool preorder(const IR::P4Control* ctrl);

explicit DeparserConverter(ConversionContext* ctxt) :
ctxt(ctxt), corelib(P4::P4CoreLibrary::instance) {
setName("DeparserConverter"); }
explicit DeparserConverter(ConversionContext* ctxt, cstring name = "deparser")
: ctxt(ctxt), name(name), corelib(P4::P4CoreLibrary::instance) {
setName("DeparserConverter");
}
};

} // namespace BMV2
Expand Down
3 changes: 1 addition & 2 deletions backends/bmv2/common/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ ParserConverter::createDefaultTransition() {
}

bool ParserConverter::preorder(const IR::P4Parser* parser) {
// hanw hard-coded parser name assumed by BMv2
auto parser_id = ctxt->json->add_parser("parser");
auto parser_id = ctxt->json->add_parser(name);

for (auto s : parser->parserLocals) {
if (auto inst = s->to<IR::P4ValueSet>()) {
Expand Down
5 changes: 3 additions & 2 deletions backends/bmv2/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class JsonObjects;

class ParserConverter : public Inspector {
ConversionContext* ctxt;
cstring name;
P4::P4CoreLibrary& corelib;

protected:
Expand All @@ -46,8 +47,8 @@ class ParserConverter : public Inspector {

public:
bool preorder(const IR::P4Parser* p) override;
explicit ParserConverter(ConversionContext* ctxt) :
ctxt(ctxt), corelib(P4::P4CoreLibrary::instance) {
explicit ParserConverter(ConversionContext* ctxt, cstring name = "parser")
: ctxt(ctxt), name(name), corelib(P4::P4CoreLibrary::instance) {
setName("ParserConverter");
}
};
Expand Down
27 changes: 19 additions & 8 deletions backends/bmv2/psa_switch/psaSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,15 @@ void PsaProgramStructure::createHeaders(ConversionContext* ctxt) {
}

void PsaProgramStructure::createParsers(ConversionContext* ctxt) {
auto cvt = new ParserConverter(ctxt);
for (auto kv : parsers) {
kv.second->apply(*cvt);
{
auto cvt = new ParserConverter(ctxt, "ingress_parser");
auto ingress = parsers.at("ingress");
ingress->apply(*cvt);
}
{
auto cvt = new ParserConverter(ctxt, "egress_parser");
auto ingress = parsers.at("egress");
ingress->apply(*cvt);
}
}

Expand Down Expand Up @@ -183,11 +189,16 @@ void PsaProgramStructure::createControls(ConversionContext* ctxt) {
}

void PsaProgramStructure::createDeparsers(ConversionContext* ctxt) {
auto cvt = new DeparserConverter(ctxt);
auto ingress = deparsers.at("ingress");
ingress->apply(*cvt);
auto egress = deparsers.at("egress");
egress->apply(*cvt);
{
auto cvt = new DeparserConverter(ctxt, "ingress_deparser");
auto ingress = deparsers.at("ingress");
ingress->apply(*cvt);
}
{
auto cvt = new DeparserConverter(ctxt, "egress_deparser");
auto egress = deparsers.at("egress");
egress->apply(*cvt);
}
}

void PsaProgramStructure::createGlobals() {
Expand Down
2 changes: 1 addition & 1 deletion backends/bmv2/psa_switch/psaSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace BMV2 {
class PsaSwitchExpressionConverter : public ExpressionConverter {
public:
PsaSwitchExpressionConverter(P4::ReferenceMap* refMap, P4::TypeMap* typeMap,
ProgramStructure* structure, cstring scalarsName) :
ProgramStructure* structure, cstring scalarsName) :
BMV2::ExpressionConverter(refMap, typeMap, structure, scalarsName) { }

Util::IJson* convertParam(UNUSED const IR::Parameter* param, cstring fieldName) override {
Expand Down

0 comments on commit 5ae3904

Please sign in to comment.