Skip to content

Commit

Permalink
tc_may_override annotation implementation
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 committed Mar 6, 2024
1 parent d56f93b commit 4bbf2bb
Show file tree
Hide file tree
Showing 27 changed files with 2,956 additions and 72 deletions.
47 changes: 35 additions & 12 deletions backends/tc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,29 +354,52 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab
if (defaultActionProperty->isConstant) {
tabledef->setDefaultMissConst(true);
}
auto annoList = defaultActionProperty->getAnnotations()->annotations;
bool isTCMayOverride = false;
const IR::Annotation *overrideAnno;
for (auto anno : annoList) {
if (anno->name == ParseTCAnnotations::tcMayOverride) {
overrideAnno = anno;
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(true);
}
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 +421,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
26 changes: 24 additions & 2 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,34 @@ 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 defaultActionName = table->defaultMissAction->getActionName();
defaultActionName = defaultActionName.replace('/', '_');
for (auto param : table->defaultMissActionParams) {
cstring paramName = param->paramDetail->getParamName();
cstring placeholder = 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 @@ -1150,7 +1172,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
112 changes: 66 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,6 +265,9 @@ class TCTable {
void setDefaultMissConst(bool i) {
isDefaultMissConst = i;
}
void setTcMayOverride(bool i) {
isTcMayOverride = i;
}
void addAction(TCAction action, unsigned flag) {
actionList.emplace(action, flag);
}
Expand Down Expand Up @@ -265,6 +302,7 @@ class TCTable {
defaultMissAction = nullptr;
isDefaultHitConst = false;
isDefaultMissConst = false;
isTcMayOverride = false;
}
toString {
std::string tcTable = "";
Expand Down Expand Up @@ -309,9 +347,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 +374,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 +386,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 +408,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
14 changes: 8 additions & 6 deletions backends/tc/tcAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ namespace TC {
class ParseTCAnnotations : public P4::ParseAnnotations {
public:
// TC specific annotations
static const cstring default_hit;
static const cstring default_hit_const;
static const cstring defaultHit;
static const cstring defaultHitConst;
static const cstring tcType;
static const cstring numMask;
static const cstring tcMayOverride;
ParseTCAnnotations()
: P4::ParseAnnotations("TC", true,
{PARSE_EMPTY(default_hit), PARSE_EMPTY(default_hit_const),
PARSE_CONSTANT_OR_STRING_LITERAL(tcType),
PARSE_CONSTANT_OR_STRING_LITERAL(numMask)}) {}
: P4::ParseAnnotations(
"TC", true,
{PARSE_EMPTY(defaultHit), PARSE_EMPTY(defaultHitConst),
PARSE_CONSTANT_OR_STRING_LITERAL(tcType), PARSE_CONSTANT_OR_STRING_LITERAL(numMask),
PARSE_EMPTY(tcMayOverride)}) {}
};

} // namespace TC
Expand Down
Loading

0 comments on commit 4bbf2bb

Please sign in to comment.