Skip to content

Commit

Permalink
Fix for default_action
Browse files Browse the repository at this point in the history
 * default miss action code updation
 * template code change for default miss action
 * NoAction case generation in c code
 * default case deletion in c code
 * testcases output updated
  • Loading branch information
Sosutha committed Feb 16, 2024
1 parent 12dafee commit 491c85a
Show file tree
Hide file tree
Showing 30 changed files with 272 additions and 262 deletions.
15 changes: 15 additions & 0 deletions backends/tc/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,21 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab
if (defaultActionProperty->isConstant) {
tabledef->setDefaultMissConst(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;
for (auto param : paramList->parameters) {
auto defaultParam = new IR::TCDefaultActionParam();
defaultParam->setParamName(param->name.originalName);
auto defaultArg = methodexp->arguments->at(i++);
defaultParam->setDefaultValue(defaultArg->toString());
tabledef->defaultMissActionParams.push_back(defaultParam);
}
}
}
}
}
Expand Down
80 changes: 34 additions & 46 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,13 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName,
builder->emitIndent();
builder->appendFormat("switch (%s->action) ", valueName.c_str());
builder->blockStart();
bool noActionGenerated = false;

for (auto a : actionList->actionList) {
auto adecl = program->refMap->getDeclaration(a->getPath(), true);
auto action = adecl->getNode()->checkedTo<IR::P4Action>();
if (action->name.originalName == P4::P4CoreLibrary::instance().noAction.name)
noActionGenerated = true;
cstring name = EBPF::EBPFObject::externalName(action), msgStr, convStr;
builder->emitIndent();
cstring actionName = p4ActionToActionIDName(action);
Expand Down Expand Up @@ -867,16 +870,22 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName,
builder->appendLine("break;");
builder->decreaseIndent();
}

builder->emitIndent();
builder->appendLine("default:");
builder->increaseIndent();
builder->target->emitTraceMessage(builder, "Control: Invalid action type, aborting");

builder->emitIndent();
builder->appendFormat("return %s", builder->target->abortReturnCode().c_str());
builder->endOfStatement(true);
builder->decreaseIndent();
if (!noActionGenerated) {
cstring noActionName = P4::P4CoreLibrary::instance().noAction.name;
cstring tableInstance = dataMapName;
cstring actionName =
Util::printf_format("%s_ACT_%s", tableInstance.toUpper(), noActionName.toUpper());
builder->emitIndent();
builder->appendFormat("case %s: ", actionName);
builder->newline();
builder->increaseIndent();
builder->emitIndent();
builder->blockStart();
builder->blockEnd(true);
builder->emitIndent();
builder->appendLine("break;");
builder->decreaseIndent();
}

builder->blockEnd(true);

Expand All @@ -888,13 +897,8 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName,

builder->blockEnd(false);
builder->appendFormat(" else ");
if (dropOnNoMatchingEntryFound()) {
builder->target->emitTraceMessage(builder, "Control: Entry not found, aborting");
emitDefaultAction(builder, valueName);
} else {
builder->target->emitTraceMessage(builder,
"Control: Entry not found, executing implicit NoAction");
}
builder->blockStart();
builder->blockEnd(true);
}

void EBPFTablePNA::emitInitializer(EBPF::CodeBuilder *builder) {
Expand All @@ -917,46 +921,30 @@ void EBPFTablePNA::emitDefaultActionStruct(EBPF::CodeBuilder *builder) {
}
}

void EBPFTablePNA::emitDefaultAction(EBPF::CodeBuilder *builder, cstring valueName) {
const IR::P4Table *t = table->container;
const IR::Expression *default_action = t->getDefaultAction();
bool visitDefaultAction = false;
if (default_action) {
if (auto mc = default_action->to<IR::MethodCallExpression>()) {
default_action = mc->method;
}
auto path = default_action->to<IR::PathExpression>();
BUG_CHECK(path, "Default action path %1% cannot be found", default_action);
if (auto defaultActionDecl =
program->refMap->getDeclaration(path->path)->to<IR::P4Action>()) {
if (defaultActionDecl->name.originalName != "NoAction") {
visitDefaultAction = true;
auto visitor = createActionTranslationVisitor(valueName, program);
visitor->setBuilder(builder);
visitor->copySubstitutions(codeGen);
visitor->copyPointerVariables(codeGen);
defaultActionDecl->apply(*visitor);
builder->newline();
}
}
}
if (visitDefaultAction == false) {
builder->blockStart();
builder->blockEnd(true);
}
}

void EBPFTablePNA::emitValueActionIDNames(EBPF::CodeBuilder *builder) {
// create type definition for action
builder->emitIndent();
bool noActionGenerated = false;
for (auto a : actionList->actionList) {
auto adecl = program->refMap->getDeclaration(a->getPath(), true);
auto action = adecl->getNode()->checkedTo<IR::P4Action>();
if (action->name.originalName == P4::P4CoreLibrary::instance().noAction.name)
noActionGenerated = true;
unsigned int action_idx = tcIR->getActionId(tcIR->externalName(action));
builder->emitIndent();
builder->appendFormat("#define %s %d", p4ActionToActionIDName(action), action_idx);
builder->newline();
}
if (!noActionGenerated) {
cstring noActionName = P4::P4CoreLibrary::instance().noAction.name;
cstring tableInstance = dataMapName;
cstring actionName =
Util::printf_format("%s_ACT_%s", tableInstance.toUpper(), noActionName.toUpper());
unsigned int action_idx = tcIR->getActionId(noActionName);
builder->emitIndent();
builder->appendFormat("#define %s %d", actionName, action_idx);
builder->newline();
}
builder->emitIndent();
}

Expand Down
1 change: 0 additions & 1 deletion backends/tc/ebpfCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ class EBPFTablePNA : public EBPF::EBPFTablePSA {
void emitAction(EBPF::CodeBuilder *builder, cstring valueName,
cstring actionRunVariable) override;
void emitValueActionIDNames(EBPF::CodeBuilder *builder) override;
void emitDefaultAction(EBPF::CodeBuilder *builder, cstring valueName);
cstring p4ActionToActionIDName(const IR::P4Action *action) const;
};

Expand Down
50 changes: 27 additions & 23 deletions backends/tc/tc.def
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ 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 Down Expand Up @@ -176,11 +199,10 @@ class TCTable {
unsigned tableEntriesCount;
unsigned numMask;
unsigned matchType;
TCAction preaction;
TCAction postaction;
TCAction defaultHitAction;
bool isDefaultHitConst;
TCAction defaultMissAction;
optional safe_vector<TCDefaultActionParam> defaultMissActionParams;
bool isDefaultMissConst;
ordered_map<TCAction, unsigned> actionList;
safe_vector<TCEntry> const_entries;
Expand All @@ -197,12 +219,6 @@ class TCTable {
void setMatchType(unsigned m) {
matchType = m;
}
void setPreaction(TCAction p) {
preaction = p;
}
void setPostaction(TCAction p) {
postaction = p;
}
void setDefaultHitAction(TCAction d) {
defaultHitAction = d;
}
Expand Down Expand Up @@ -245,8 +261,6 @@ class TCTable {
tableEntriesCount = TC::DEFAULT_TABLE_ENTRIES;
numMask = TC::DEFAULT_KEY_MASK;
matchType = TC::EXACT_TYPE;
preaction = nullptr;
postaction = nullptr;
defaultHitAction = nullptr;
defaultMissAction = nullptr;
isDefaultHitConst = false;
Expand Down Expand Up @@ -278,19 +292,6 @@ class TCTable {
}
}
}

if (preaction != nullptr) {
tcTable += "\n" + preaction->toString();
tcTable += "\n$TC p4template update table/" + pipelineName
+ "/" + controlName + "/" + tableName
+ " preactions action " + preaction->getName();
}
if (postaction != nullptr) {
tcTable += "\n" + postaction->toString();
tcTable += "\n$TC p4template update table/" + pipelineName
+ "/" + controlName + "/" + tableName
+ " postactions action " + postaction->getName();
}
if (defaultHitAction != nullptr) {
tcTable += "\n$TC p4template update table/" + pipelineName
+ "/" + controlName + "/" + tableName
Expand All @@ -308,6 +309,9 @@ class TCTable {
tcTable += " permissions 0x1024";
}
tcTable += " action " + defaultMissAction->getName();
for (auto param : defaultMissActionParams) {
tcTable += param->toString();
}
}
if (const_entries.size() != 0) {
for (auto entry : const_entries) {
Expand Down
9 changes: 5 additions & 4 deletions testdata/p4tc_samples_outputs/calculator_control_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct __attribute__((__packed__)) MainControlImpl_calculate_key {
#define MAINCONTROLIMPL_CALCULATE_ACT_MAINCONTROLIMPL_OPERATION_OR 4
#define MAINCONTROLIMPL_CALCULATE_ACT_MAINCONTROLIMPL_OPERATION_XOR 5
#define MAINCONTROLIMPL_CALCULATE_ACT_MAINCONTROLIMPL_OPERATION_DROP 6
#define MAINCONTROLIMPL_CALCULATE_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_calculate_value {
unsigned int action;
union {
Expand Down Expand Up @@ -147,12 +148,12 @@ if (/* hdr->p4calc.isValid() */
drop_packet();
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_CALCULATE_ACT_NOACTION:
{
}
break;
}
} else {
/* drop_packet() */
drop_packet();
}
}
; }
Expand Down
9 changes: 5 additions & 4 deletions testdata/p4tc_samples_outputs/checksum_control_blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct __attribute__((__packed__)) ingress_nh_table_key {
} __attribute__((aligned(4)));
#define INGRESS_NH_TABLE_ACT_INGRESS_SEND_NH 1
#define INGRESS_NH_TABLE_ACT_INGRESS_DROP 2
#define INGRESS_NH_TABLE_ACT_NOACTION 0
struct __attribute__((__packed__)) ingress_nh_table_value {
unsigned int action;
union {
Expand Down Expand Up @@ -89,12 +90,12 @@ static __always_inline int process(struct __sk_buff *skb, struct my_ingress_head
drop_packet();
}
break;
default:
return TC_ACT_SHOT;
case INGRESS_NH_TABLE_ACT_NOACTION:
{
}
break;
}
} else {
/* drop_packet() */
drop_packet();
}
}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct __attribute__((__packed__)) MainControlImpl_t_range_key {
} __attribute__((aligned(4)));
#define MAINCONTROLIMPL_T_RANGE_ACT_MAINCONTROLIMPL_A 1
#define MAINCONTROLIMPL_T_RANGE_ACT_MAINCONTROLIMPL_A_WITH_CONTROL_PARAMS 2
#define MAINCONTROLIMPL_T_RANGE_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_t_range_value {
unsigned int action;
union {
Expand Down Expand Up @@ -82,11 +83,12 @@ static __always_inline int process(struct __sk_buff *skb, struct Header_t *h, st
h->h.t = value->u.MainControlImpl_a_with_control_params.x;
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_T_RANGE_ACT_NOACTION:
{
}
break;
}
} else {
h->h.e = 0;
}
}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key {
} __attribute__((aligned(4)));
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP 1
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP 2
#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value {
unsigned int action;
union {
Expand All @@ -32,6 +33,7 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key {
} __attribute__((aligned(4)));
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP 1
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DROP 3
#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_value {
unsigned int action;
union {
Expand Down Expand Up @@ -110,12 +112,12 @@ if (/* hdr->ipv4.isValid() */
drop_packet();
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION:
{
}
break;
}
} else {
/* drop_packet() */
drop_packet();
}
}
;
Expand Down Expand Up @@ -159,12 +161,12 @@ if (/* hdr->ipv4.isValid() */
drop_packet();
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION:
{
}
break;
}
} else {
/* drop_packet() */
drop_packet();
}
}
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct MainControlImpl_set_ct_options_key_mask {
#define MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_MAINCONTROLIMPL_TCP_SYN_PACKET 1
#define MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_MAINCONTROLIMPL_TCP_FIN_OR_RST_PACKET 2
#define MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_MAINCONTROLIMPL_TCP_OTHER_PACKETS 3
#define MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_set_ct_options_value {
unsigned int action;
__u32 priority;
Expand Down Expand Up @@ -94,8 +95,10 @@ if (/* hdr->ipv4.isValid() */
{
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_SET_CT_OPTIONS_ACT_NOACTION:
{
}
break;
}
} else {
}
Expand Down
Loading

0 comments on commit 491c85a

Please sign in to comment.