Skip to content

Commit

Permalink
Fix for default_action
Browse files Browse the repository at this point in the history
 * default miss action updation
  • Loading branch information
Sosutha committed Feb 12, 2024
1 parent 12dafee commit 9dc0bfb
Show file tree
Hide file tree
Showing 27 changed files with 238 additions and 237 deletions.
88 changes: 43 additions & 45 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,32 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName,
builder->appendLine("break;");
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->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();
// 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();

builder->blockEnd(true);

Expand All @@ -888,13 +907,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 +931,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_key {
} __attribute__((aligned(4)));
#define MAINCONTROLIMPL_IPV4_TBL_ACT_MAINCONTROLIMPL_NEXT_HOP 1
#define MAINCONTROLIMPL_IPV4_TBL_ACT_MAINCONTROLIMPL_DEFAULT_ROUTE_DROP 2
#define MAINCONTROLIMPL_IPV4_TBL_ACT_NOACTION 0
struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_value {
unsigned int action;
union {
Expand Down Expand Up @@ -87,12 +88,12 @@ if (/* hdr->ipv4.isValid() */
drop_packet();
}
break;
default:
return TC_ACT_SHOT;
case MAINCONTROLIMPL_IPV4_TBL_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__)) ingress_nh_table2_key {
} __attribute__((aligned(4)));
#define INGRESS_NH_TABLE2_ACT_INGRESS_SEND_NH 2
#define INGRESS_NH_TABLE2_ACT_INGRESS_DROP 3
#define INGRESS_NH_TABLE2_ACT_NOACTION 0
struct __attribute__((__packed__)) ingress_nh_table2_value {
unsigned int action;
union {
Expand All @@ -30,6 +31,7 @@ struct __attribute__((__packed__)) ingress_nh_table_key {
} __attribute__((aligned(4)));
#define INGRESS_NH_TABLE_ACT__SEND_NH 1
#define INGRESS_NH_TABLE_ACT_INGRESS_DROP 3
#define INGRESS_NH_TABLE_ACT_NOACTION 0
struct __attribute__((__packed__)) ingress_nh_table_value {
unsigned int action;
union {
Expand Down Expand Up @@ -109,12 +111,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 Expand Up @@ -157,12 +159,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_TABLE2_ACT_NOACTION:
{
}
break;
}
} else {
/* drop_packet() */
drop_packet();
}
}
;
Expand Down
Loading

0 comments on commit 9dc0bfb

Please sign in to comment.