Skip to content

Commit

Permalink
Added default action code for tc backend (p4lang#4561)
Browse files Browse the repository at this point in the history
* Added default action code in control block for tc backend

*  Updated testcase outputs
  • Loading branch information
Sosutha authored Mar 27, 2024
1 parent 1de63e6 commit 277c389
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 47 deletions.
111 changes: 80 additions & 31 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,20 @@ void PNAEbpfGenerator::emitP4TCFilterFields(EBPF::CodeBuilder *builder) const {
}

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;
}
}
defaultActionName = defaultActionName.substr(
defaultActionName.find('/') - defaultActionName.begin() + 1,
defaultActionName.size());
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);
}
cstring typeName = param->paramDetail->getParamType();
builder->emitIndent();
builder->appendFormat("%s %s", typeName, placeholder);
builder->endOfStatement(true);
}
}
}
Expand Down Expand Up @@ -965,16 +956,63 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName,
builder->target->emitTraceMessage(builder, msgStr.c_str());
}
}
bool generateDefaultMissCode = false;
for (auto tbl : tcIR->tcPipeline->tableDefs) {
if (tbl->getTableName() == table->container->name.originalName) {
if (tbl->isTcMayOverride) {
cstring defaultActionName = tbl->defaultMissAction->getActionName();
defaultActionName = defaultActionName.substr(
defaultActionName.find('/') - defaultActionName.begin() + 1,
defaultActionName.size());
if (defaultActionName == action->name.originalName)
generateDefaultMissCode = true;
}
}
}
if (generateDefaultMissCode) {
builder->emitIndent();
builder->appendFormat("{");
builder->newline();
builder->increaseIndent();

builder->emitIndent();
builder->emitIndent();
builder->appendFormat("if (%s->is_default_miss_act) ", valueName.c_str());
builder->newline();

auto visitor = createActionTranslationVisitor(valueName, program);
visitor->setBuilder(builder);
visitor->copySubstitutions(codeGen);
visitor->copyPointerVariables(codeGen);
builder->emitIndent();
auto defaultVisitor = createActionTranslationVisitor(valueName, program, action, true);
defaultVisitor->setBuilder(builder);
defaultVisitor->copySubstitutions(codeGen);
defaultVisitor->copyPointerVariables(codeGen);
action->apply(*defaultVisitor);
builder->newline();

builder->emitIndent();
builder->appendFormat("else");
builder->newline();

builder->emitIndent();
auto visitor = createActionTranslationVisitor(valueName, program, action, false);
visitor->setBuilder(builder);
visitor->copySubstitutions(codeGen);
visitor->copyPointerVariables(codeGen);
action->apply(*visitor);
builder->newline();

builder->decreaseIndent();
builder->emitIndent();
builder->appendFormat("}");
builder->newline();
} else {
builder->emitIndent();
auto visitor = createActionTranslationVisitor(valueName, program, action, false);
visitor->setBuilder(builder);
visitor->copySubstitutions(codeGen);
visitor->copyPointerVariables(codeGen);
action->apply(*visitor);
builder->newline();
}

action->apply(*visitor);
builder->newline();
builder->emitIndent();
builder->appendLine("break;");
builder->decreaseIndent();
Expand Down Expand Up @@ -1866,14 +1904,16 @@ bool ControlBodyTranslatorPNA::preorder(const IR::AssignmentStatement *a) {
return EBPF::CodeGenInspector::preorder(a);
}
// =====================ActionTranslationVisitorPNA=============================
ActionTranslationVisitorPNA::ActionTranslationVisitorPNA(const EBPF::EBPFProgram *program,
cstring valueName,
const EBPF::EBPFTablePSA *table,
const ConvertToBackendIR *tcIR)
ActionTranslationVisitorPNA::ActionTranslationVisitorPNA(
const EBPF::EBPFProgram *program, cstring valueName, const EBPF::EBPFTablePSA *table,
const ConvertToBackendIR *tcIR, const IR::P4Action *action, bool isDefaultAction)
: EBPF::CodeGenInspector(program->refMap, program->typeMap),
EBPF::ActionTranslationVisitor(valueName, program),
ControlBodyTranslatorPNA(program->as<EBPF::EBPFPipeline>().control, tcIR, table),
table(table) {}
table(table),
isDefaultAction(isDefaultAction) {
action = action;
}

bool ActionTranslationVisitorPNA::preorder(const IR::PathExpression *pe) {
if (isActionParameter(pe)) {
Expand All @@ -1894,7 +1934,15 @@ bool ActionTranslationVisitorPNA::isActionParameter(const IR::Expression *expres
cstring ActionTranslationVisitorPNA::getParamInstanceName(const IR::Expression *expression) const {
if (auto cast = expression->to<IR::Cast>()) expression = cast->expr;

return EBPF::ActionTranslationVisitor::getParamInstanceName(expression);
if (isDefaultAction) {
cstring actionName = action->name.originalName;
auto paramStr = Util::printf_format("p4tc_filter_fields.%s_%s_%s",
table->table->container->name.originalName, actionName,
expression->toString());
return paramStr;
} else {
return EBPF::ActionTranslationVisitor::getParamInstanceName(expression);
}
}

cstring ActionTranslationVisitorPNA::getParamName(const IR::PathExpression *expr) {
Expand All @@ -1905,9 +1953,10 @@ cstring ActionTranslationVisitorPNA::getParamName(const IR::PathExpression *expr
}

EBPF::ActionTranslationVisitor *EBPFTablePNA::createActionTranslationVisitor(
cstring valueName, const EBPF::EBPFProgram *program) const {
cstring valueName, const EBPF::EBPFProgram *program, const IR::P4Action *action,
bool isDefault) const {
return new ActionTranslationVisitorPNA(program->checkedTo<EBPF::EBPFPipeline>(), valueName,
this, tcIR);
this, tcIR, action, isDefault);
}

void EBPFTablePNA::validateKeys() const {
Expand Down
11 changes: 7 additions & 4 deletions backends/tc/ebpfCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ class EBPFPnaParser : public EBPF::EBPFPsaParser {

class EBPFTablePNA : public EBPF::EBPFTablePSA {
protected:
EBPF::ActionTranslationVisitor *createActionTranslationVisitor(
cstring valueName, const EBPF::EBPFProgram *program) const override;
EBPF::ActionTranslationVisitor *createActionTranslationVisitor(cstring valueName,
const EBPF::EBPFProgram *program,
const IR::P4Action *action,
bool isDefaultAction) const;
void validateKeys() const override;
const ConvertToBackendIR *tcIR;

Expand Down Expand Up @@ -333,12 +335,13 @@ class ActionTranslationVisitorPNA : public EBPF::ActionTranslationVisitor,
public ControlBodyTranslatorPNA {
protected:
const EBPF::EBPFTablePSA *table;
bool isDefaultAction;

public:
const ConvertToBackendIR *tcIR;
ActionTranslationVisitorPNA(const EBPF::EBPFProgram *program, cstring valueName,
const EBPF::EBPFTablePSA *table, const ConvertToBackendIR *tcIR);

const EBPF::EBPFTablePSA *table, const ConvertToBackendIR *tcIR,
const IR::P4Action *action, bool isDefaultAction);
bool preorder(const IR::PathExpression *pe) override;
bool isActionParameter(const IR::Expression *expression) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,18 @@ if (/* hdr->ipv4.isValid() */
switch (value->action) {
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP:
{
if (value->is_default_miss_act)
{
/* send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport) */
compiler_meta__->drop = false;
send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport);
}
else
{
/* send_to_port(value->u.MainControlImpl_next_hop.vport) */
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
}
}
break;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,20 @@ if (/* hdr->ipv4.isValid() */
switch (value->action) {
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP:
{
hdr->ipv4.dstAddr = value->u.MainControlImpl_next_hop.ipv4addr;
/* send_to_port(8) */
compiler_meta__->drop = false;
send_to_port(8);
if (value->is_default_miss_act)
{
hdr->ipv4.dstAddr = p4tc_filter_fields.ipv4_tbl_1_next_hop_ipv4addr;
/* send_to_port(8) */
compiler_meta__->drop = false;
send_to_port(8);
}
else
{
hdr->ipv4.dstAddr = value->u.MainControlImpl_next_hop.ipv4addr;
/* send_to_port(8) */
compiler_meta__->drop = false;
send_to_port(8);
}
}
break;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,18 @@ if (/* hdr->ipv4.isValid() */
switch (value->action) {
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP:
{
if (value->is_default_miss_act)
{
/* send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport) */
compiler_meta__->drop = false;
send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport);
}
else
{
/* send_to_port(value->u.MainControlImpl_next_hop.vport) */
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
}
}
break;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,18 @@ if (/* hdr->ipv4.isValid() */
switch (value->action) {
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP:
{
if (value->is_default_miss_act)
{
/* send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport) */
compiler_meta__->drop = false;
send_to_port(p4tc_filter_fields.ipv4_tbl_1_next_hop_vport);
}
else
{
/* send_to_port(value->u.MainControlImpl_next_hop.vport) */
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
}
}
break;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP:
Expand Down Expand Up @@ -161,9 +170,18 @@ if (/* hdr->ipv4.isValid() */
switch (value->action) {
case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP:
{
if (value->is_default_miss_act)
{
/* send_to_port(p4tc_filter_fields.ipv4_tbl_2_next_hop_vport) */
compiler_meta__->drop = false;
send_to_port(p4tc_filter_fields.ipv4_tbl_2_next_hop_vport);
}
else
{
/* send_to_port(value->u.MainControlImpl_next_hop.vport) */
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
compiler_meta__->drop = false;
send_to_port(value->u.MainControlImpl_next_hop.vport);
}
}
break;
case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DROP:
Expand Down

0 comments on commit 277c389

Please sign in to comment.