Skip to content

Commit

Permalink
tc_may_override annotation implementation (p4lang#4529)
Browse files Browse the repository at this point in the history
* tc_may_override annotation for tc backend

* Added testcases for annotation
  • Loading branch information
Sosutha authored Mar 14, 2024
1 parent 98d166f commit a4e5edb
Show file tree
Hide file tree
Showing 48 changed files with 5,842 additions and 72 deletions.
45 changes: 33 additions & 12 deletions backends/tc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,29 +354,50 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab
if (defaultActionProperty->isConstant) {
tabledef->setDefaultMissConst(true);
}
bool isTCMayOverride = false;
const IR::Annotation *overrideAnno =
defaultActionProperty->getAnnotations()->getSingle(
ParseTCAnnotations::tcMayOverride);
if (overrideAnno) {
isTCMayOverride = true;
}
bool directionParamPresent = false;
auto paramList = actionCall->action->getParameters();
for (auto param : paramList->parameters) {
if (param->direction != IR::Direction::None) directionParamPresent = true;
}
if (!directionParamPresent) {
auto i = 0;
if (isTCMayOverride) {
if (paramList->parameters.empty())
::warning(ErrorType::WARN_INVALID,
"%1% annotation cannot be used with default_action without "
"parameters",
overrideAnno);
else
tabledef->setTcMayOverride();
}
for (auto param : paramList->parameters) {
auto defaultParam = new IR::TCDefaultActionParam();
defaultParam->setParamName(param->name.originalName);
for (auto actionParam : tcAction->actionParams) {
if (actionParam->paramName == param->name.originalName) {
defaultParam->setParamDetail(actionParam);
}
}
auto defaultArg = methodexp->arguments->at(i++);
if (auto constVal = defaultArg->expression->to<IR::Constant>()) {
bool sign;
if (const IR::Type_Bits *tb = constVal->type->to<IR::Type_Bits>()) {
sign = tb->isSigned;
} else {
sign = false;
}
defaultParam->setDefaultValue(
Util::toString(constVal->value, 0, sign, constVal->base));
if (!isTCMayOverride)
defaultParam->setDefaultValue(
Util::toString(constVal->value, 0, true, constVal->base));
tabledef->defaultMissActionParams.push_back(defaultParam);
}
tabledef->defaultMissActionParams.push_back(defaultParam);
}
} else {
if (isTCMayOverride)
::warning(ErrorType::WARN_INVALID,
"%1% annotation cannot be used with default_action with "
"directional parameters",
overrideAnno);
}
}
}
Expand All @@ -398,13 +419,13 @@ void ConvertToBackendIR::updateDefaultHitAction(const IR::P4Table *t, IR::TCTabl
if (anno->name == IR::Annotation::tableOnlyAnnotation) {
isTableOnly = true;
}
if (anno->name == ParseTCAnnotations::default_hit) {
if (anno->name == ParseTCAnnotations::defaultHit) {
isDefaultHit = true;
defaultHit++;
auto adecl = refMap->getDeclaration(action->getPath(), true);
defaultActionName = externalName(adecl);
}
if (anno->name == ParseTCAnnotations::default_hit_const) {
if (anno->name == ParseTCAnnotations::defaultHitConst) {
isDefaultHitConst = true;
defaultHitConst++;
auto adecl = refMap->getDeclaration(action->getPath(), true);
Expand Down
33 changes: 31 additions & 2 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,41 @@ void PNAEbpfGenerator::emitP4TCFilterFields(EBPF::CodeBuilder *builder) const {
builder->emitIndent();
builder->appendFormat("__u16 prio");
builder->endOfStatement(true);

emitP4TCActionParam(builder);
builder->blockEnd(false);
builder->endOfStatement(true);
builder->newline();
}

void PNAEbpfGenerator::emitP4TCActionParam(EBPF::CodeBuilder *builder) const {
std::vector<cstring> actionParamList;
for (auto table : tcIR->tcPipeline->tableDefs) {
if (table->isTcMayOverride) {
cstring tblName = table->getTableName();
cstring defaultActionName = table->defaultMissAction->getActionName();
auto actionNameStr = defaultActionName.c_str();
for (long unsigned int i = 0; i < defaultActionName.size(); i++) {
if (actionNameStr[i] == '/') {
defaultActionName = defaultActionName.substr(i + 1, defaultActionName.size());
break;
}
}
for (auto param : table->defaultMissActionParams) {
cstring paramName = param->paramDetail->getParamName();
cstring placeholder = tblName + "_" + defaultActionName + "_" + paramName;
auto itr = find(actionParamList.begin(), actionParamList.end(), placeholder);
if (itr == actionParamList.end()) {
actionParamList.push_back(placeholder);
cstring typeName = param->paramDetail->getParamType();
builder->emitIndent();
builder->appendFormat("%s %s", typeName, placeholder);
builder->endOfStatement(true);
}
}
}
}
}

void PNAEbpfGenerator::emitPipelineInstances(EBPF::CodeBuilder *builder) const {
pipeline->parser->emitValueSetInstances(builder);
pipeline->deparser->emitDigestInstances(builder);
Expand Down Expand Up @@ -1162,7 +1191,7 @@ const PNAEbpfGenerator *ConvertToEbpfPNA::build(const IR::ToplevelBlock *tlb) {
tlb->getProgram()->apply(*pipeline_converter);
auto tcIngress = pipeline_converter->getEbpfPipeline();

return new PNAArchTC(options, ebpfTypes, xdp, tcIngress);
return new PNAArchTC(options, ebpfTypes, xdp, tcIngress, tcIR);
}

const IR::Node *ConvertToEbpfPNA::preorder(IR::ToplevelBlock *tlb) {
Expand Down
11 changes: 7 additions & 4 deletions backends/tc/ebpfCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ class EBPFPnaParser;
class PNAEbpfGenerator : public EBPF::EbpfCodeGenerator {
public:
EBPF::EBPFPipeline *pipeline;
const ConvertToBackendIR *tcIR;

PNAEbpfGenerator(const EbpfOptions &options, std::vector<EBPF::EBPFType *> &ebpfTypes,
EBPF::EBPFPipeline *pipeline)
: EBPF::EbpfCodeGenerator(options, ebpfTypes), pipeline(pipeline) {}
EBPF::EBPFPipeline *pipeline, const ConvertToBackendIR *tcIR)
: EBPF::EbpfCodeGenerator(options, ebpfTypes), pipeline(pipeline), tcIR(tcIR) {}

virtual void emit(EBPF::CodeBuilder *builder) const = 0;
virtual void emitInstances(EBPF::CodeBuilder *builder) const = 0;
Expand All @@ -46,6 +47,7 @@ class PNAEbpfGenerator : public EBPF::EbpfCodeGenerator {
void emitGlobalHeadersMetadata(EBPF::CodeBuilder *builder) const override;
void emitPipelineInstances(EBPF::CodeBuilder *builder) const override;
void emitP4TCFilterFields(EBPF::CodeBuilder *builder) const;
void emitP4TCActionParam(EBPF::CodeBuilder *builder) const;
cstring getProgramName() const;
};

Expand Down Expand Up @@ -89,8 +91,9 @@ class PNAArchTC : public PNAEbpfGenerator {
EBPF::XDPHelpProgram *xdp;

PNAArchTC(const EbpfOptions &options, std::vector<EBPF::EBPFType *> &ebpfTypes,
EBPF::XDPHelpProgram *xdp, EBPF::EBPFPipeline *pipeline)
: PNAEbpfGenerator(options, ebpfTypes, pipeline), xdp(xdp) {}
EBPF::XDPHelpProgram *xdp, EBPF::EBPFPipeline *pipeline,
const ConvertToBackendIR *tcIR)
: PNAEbpfGenerator(options, ebpfTypes, pipeline, tcIR), xdp(xdp) {}

void emit(EBPF::CodeBuilder *builder) const override;
void emitParser(EBPF::CodeBuilder *builder) const override;
Expand Down
115 changes: 69 additions & 46 deletions backends/tc/tc.def
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,6 @@ class TCKernelMetadata {
dbprint { out << toString(); }
}

class TCDefaultActionParam {
cstring paramName;
cstring defaultValue;
void setParamName(cstring pN) {
paramName = pN;
}
void setDefaultValue(cstring dV) {
defaultValue = dV;
}
TCDefaultActionParam() {
paramName = nullptr;
defaultValue = nullptr;
}
toString {
std::string tcActionParam = "";
tcActionParam += " param ";
tcActionParam += paramName;
tcActionParam += " " + defaultValue;
return tcActionParam;
}
dbprint { out << toString(); }
}

class TCActionParam {
cstring paramName;
unsigned dataType;
Expand All @@ -91,6 +68,36 @@ class TCActionParam {
cstring getName() const {
return "param." + paramName;
}
cstring getParamType() const {
std::string paramType = "";
switch(dataType) {
case TC::BIT_TYPE :
paramType += "__u" + Util::toString(bitSize);
break;
case TC::DEV_TYPE :
paramType += "dev";
break;
case TC::MACADDR_TYPE :
paramType += "macaddr";
break;
case TC::IPV4_TYPE :
paramType += "ipv4";
break;
case TC::IPV6_TYPE :
paramType += "ipv6";
break;
case TC::BE16_TYPE :
paramType += "__be16";
break;
case TC::BE32_TYPE :
paramType += "__be32";
break;
case TC::BE64_TYPE :
paramType += "__be64";
break;
}
return paramType;
}
toString {
std::string tcActionParam = "";
tcActionParam += "\n\tparam ";
Expand Down Expand Up @@ -127,6 +134,29 @@ class TCActionParam {
dbprint { out << toString(); }
}

class TCDefaultActionParam {
TCActionParam paramDetail;
cstring defaultValue;
void setParamDetail(TCActionParam pN) {
paramDetail = pN;
}
void setDefaultValue(cstring dV) {
defaultValue = dV;
}
TCDefaultActionParam() {
paramDetail = nullptr;
defaultValue = nullptr;
}
toString {
std::string tcActionParam = "";
tcActionParam += " " + paramDetail->paramName;
if (defaultValue != nullptr)
tcActionParam += " " + defaultValue;
return tcActionParam;
}
dbprint { out << toString(); }
}

class TCAction {
cstring actionName;
cstring pipelineName;
Expand All @@ -137,6 +167,9 @@ class TCAction {
tcAction += "/" + actionName;
return tcAction;
}
cstring getActionName() const {
return actionName;
}
void setPipelineName(cstring pN) {
pipelineName = pN;
}
Expand Down Expand Up @@ -204,6 +237,7 @@ class TCTable {
TCAction defaultMissAction;
optional safe_vector<TCDefaultActionParam> defaultMissActionParams;
bool isDefaultMissConst;
bool isTcMayOverride;
ordered_map<TCAction, unsigned> actionList;
safe_vector<TCEntry> const_entries;

Expand Down Expand Up @@ -231,12 +265,18 @@ class TCTable {
void setDefaultMissConst(bool i) {
isDefaultMissConst = i;
}
void setTcMayOverride() {
isTcMayOverride = true;
}
void addAction(TCAction action, unsigned flag) {
actionList.emplace(action, flag);
}
void addConstEntries(TCEntry entry) {
const_entries.push_back(entry);
}
cstring getTableName() const {
return tableName;
}
cstring printMatchType(unsigned matchType) const {
cstring matchTypeString = "";
switch(matchType) {
Expand Down Expand Up @@ -265,6 +305,7 @@ class TCTable {
defaultMissAction = nullptr;
isDefaultHitConst = false;
isDefaultMissConst = false;
isTcMayOverride = false;
}
toString {
std::string tcTable = "";
Expand Down Expand Up @@ -309,9 +350,12 @@ class TCTable {
tcTable += " permissions 0x1024";
}
tcTable += " action " + defaultMissAction->getName();
for (auto param : defaultMissActionParams) {
if (!defaultMissActionParams.empty())
tcTable += " param";
for (auto param : defaultMissActionParams)
tcTable += param->toString();
}
if (isTcMayOverride)
tcTable += " flags runtime";
}
if (const_entries.size() != 0) {
for (auto entry : const_entries) {
Expand All @@ -333,8 +377,6 @@ class TCPipeline {
unsigned numTables;
safe_vector<TCAction> actionDefs;
safe_vector<TCTable> tableDefs;
TCAction preaction;
TCAction postaction;
void setPipelineName(cstring pName) {
pipelineName = pName;
}
Expand All @@ -347,19 +389,10 @@ class TCPipeline {
void addTableDefinition(TCTable tableDef) {
tableDefs.push_back(tableDef);
}
void setPipelinePreAction(TCAction action) {
preaction = action;
}
void setPipelinePostAction(TCAction action) {
postaction = action;
}
TCPipeline() {
Util::SourceInfo* srcinfo = new Util::SourceInfo();
Node::srcInfo = *srcinfo;
pipelineName = nullptr;
numTables = 0;
preaction = nullptr;
postaction = nullptr;
}
toString {
std::string tcCode = "#!/bin/bash -x\n";
Expand All @@ -378,16 +411,6 @@ class TCPipeline {
tcCode += "\n" + t->toString();
}
}
if (preaction != nullptr) {
tcCode += "\n" + preaction->toString();
tcCode += "\n$TC p4template update pipeline/" + pipelineName
+ " preactions action " + pipelineName + "/preaction";
}
if (postaction != nullptr) {
tcCode += "\n" + postaction->toString();
tcCode += "\n$TC p4template update pipeline/" + pipelineName
+ " postactions action " + pipelineName + "/postaction";
}
tcCode += "\n$TC p4template update pipeline/" + pipelineName + " state ready";
return tcCode;
}
Expand Down
5 changes: 3 additions & 2 deletions backends/tc/tcAnnotations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ and limitations under the License.

namespace TC {

const cstring ParseTCAnnotations::default_hit = "default_hit";
const cstring ParseTCAnnotations::default_hit_const = "default_hit_const";
const cstring ParseTCAnnotations::defaultHit = "default_hit";
const cstring ParseTCAnnotations::defaultHitConst = "default_hit_const";
const cstring ParseTCAnnotations::tcType = "tc_type";
const cstring ParseTCAnnotations::numMask = "nummask";
const cstring ParseTCAnnotations::tcMayOverride = "tc_may_override";

} // namespace TC
Loading

0 comments on commit a4e5edb

Please sign in to comment.