From d48848ca9e894a089351498d539ef146af035277 Mon Sep 17 00:00:00 2001 From: Sosutha Sethuramapandian Date: Tue, 2 Apr 2024 02:29:15 -0700 Subject: [PATCH] Added changes for default hit actions for tc backend * Updated tc_may_override hint to be used with default_hit actions * Added testcases for tc_may_override hint with default_hit action --- backends/tc/backend.cpp | 68 ++- backends/tc/backend.h | 1 + backends/tc/ebpfCodeGen.cpp | 35 +- backends/tc/tc.def | 33 +- backends/tc/tc_defines.h | 5 + .../tc_may_override_example_01.p4 | 2 +- .../tc_may_override_example_07.p4 | 135 ++++++ .../tc_may_override_example_08.p4 | 135 ++++++ .../tc_may_override_example_09.p4 | 135 ++++++ .../tc_may_override_example_01.json | 4 +- .../tc_may_override_example_01.template | 1 + ...c_may_override_example_01_control_blocks.c | 13 +- .../tc_may_override_example_01_parser.h | 1 + .../tc_may_override_example_07.json | 125 ++++++ .../tc_may_override_example_07.p4-stderr | 0 .../tc_may_override_example_07.template | 35 ++ ...c_may_override_example_07_control_blocks.c | 396 ++++++++++++++++++ .../tc_may_override_example_07_parser.c | 136 ++++++ .../tc_may_override_example_07_parser.h | 166 ++++++++ .../tc_may_override_example_08.json | 125 ++++++ .../tc_may_override_example_08.p4-stderr | 0 .../tc_may_override_example_08.template | 35 ++ ...c_may_override_example_08_control_blocks.c | 396 ++++++++++++++++++ .../tc_may_override_example_08_parser.c | 136 ++++++ .../tc_may_override_example_08_parser.h | 166 ++++++++ .../tc_may_override_example_09.json | 125 ++++++ .../tc_may_override_example_09.p4-stderr | 1 + .../tc_may_override_example_09.template | 34 ++ ...c_may_override_example_09_control_blocks.c | 387 +++++++++++++++++ .../tc_may_override_example_09_parser.c | 136 ++++++ .../tc_may_override_example_09_parser.h | 165 ++++++++ 31 files changed, 3110 insertions(+), 22 deletions(-) create mode 100644 testdata/p4tc_samples/tc_may_override_example_07.p4 create mode 100644 testdata/p4tc_samples/tc_may_override_example_08.p4 create mode 100644 testdata/p4tc_samples/tc_may_override_example_09.p4 create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_07.json create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_07.p4-stderr create mode 100755 testdata/p4tc_samples_outputs/tc_may_override_example_07.template create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_07_control_blocks.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.h create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_08.json create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_08.p4-stderr create mode 100755 testdata/p4tc_samples_outputs/tc_may_override_example_08.template create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_08_control_blocks.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.h create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_09.json create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_09.p4-stderr create mode 100755 testdata/p4tc_samples_outputs/tc_may_override_example_09.template create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_09_control_blocks.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.c create mode 100644 testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.h diff --git a/backends/tc/backend.cpp b/backends/tc/backend.cpp index 6da44e52d74..662f3eae0f4 100644 --- a/backends/tc/backend.cpp +++ b/backends/tc/backend.cpp @@ -271,6 +271,16 @@ void ConvertToBackendIR::postorder(const IR::P4Action *action) { "tc_type annotation cannot have '%1%' as value", expr); } } + auto direction = param->direction; + if (direction == IR::Direction::InOut) { + tcActionParam->setDirection(TC::INOUT); + } else if (direction == IR::Direction::In) { + tcActionParam->setDirection(TC::IN); + } else if (direction == IR::Direction::Out) { + tcActionParam->setDirection(TC::OUT); + } else { + tcActionParam->setDirection(TC::NONE); + } tcAction->addActionParams(tcActionParam); } } @@ -381,12 +391,12 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab if (defaultActionProperty->isConstant) { tabledef->setDefaultMissConst(true); } - bool isTCMayOverride = false; + bool isTCMayOverrideMiss = false; const IR::Annotation *overrideAnno = defaultActionProperty->getAnnotations()->getSingle( ParseTCAnnotations::tcMayOverride); if (overrideAnno) { - isTCMayOverride = true; + isTCMayOverrideMiss = true; } bool directionParamPresent = false; auto paramList = actionCall->action->getParameters(); @@ -395,14 +405,14 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab } if (!directionParamPresent) { auto i = 0; - if (isTCMayOverride) { + if (isTCMayOverrideMiss) { if (paramList->parameters.empty()) ::warning(ErrorType::WARN_INVALID, "%1% annotation cannot be used with default_action without " "parameters", overrideAnno); else - tabledef->setTcMayOverride(); + tabledef->setTcMayOverrideMiss(); } for (auto param : paramList->parameters) { auto defaultParam = new IR::TCDefaultActionParam(); @@ -413,14 +423,14 @@ void ConvertToBackendIR::updateDefaultMissAction(const IR::P4Table *t, IR::TCTab } auto defaultArg = methodexp->arguments->at(i++); if (auto constVal = defaultArg->expression->to()) { - if (!isTCMayOverride) + if (!isTCMayOverrideMiss) defaultParam->setDefaultValue( Util::toString(constVal->value, 0, true, constVal->base)); tabledef->defaultMissActionParams.push_back(defaultParam); } } } else { - if (isTCMayOverride) + if (isTCMayOverrideMiss) ::warning(ErrorType::WARN_INVALID, "%1% annotation cannot be used with default_action with " "directional parameters", @@ -437,11 +447,13 @@ void ConvertToBackendIR::updateDefaultHitAction(const IR::P4Table *t, IR::TCTabl unsigned int defaultHit = 0; unsigned int defaultHitConst = 0; cstring defaultActionName = nullptr; + bool isTcMayOverrideHitAction = false; for (auto action : actionlist->actionList) { auto annoList = action->getAnnotations()->annotations; bool isTableOnly = false; bool isDefaultHit = false; bool isDefaultHitConst = false; + bool isTcMayOverrideHit = false; for (auto anno : annoList) { if (anno->name == IR::Annotation::tableOnlyAnnotation) { isTableOnly = true; @@ -458,6 +470,9 @@ void ConvertToBackendIR::updateDefaultHitAction(const IR::P4Table *t, IR::TCTabl auto adecl = refMap->getDeclaration(action->getPath(), true); defaultActionName = externalName(adecl); } + if (anno->name == ParseTCAnnotations::tcMayOverride) { + isTcMayOverrideHit = true; + } } if (isTableOnly && isDefaultHit && isDefaultHitConst) { ::error(ErrorType::ERR_INVALID, @@ -483,6 +498,26 @@ void ConvertToBackendIR::updateDefaultHitAction(const IR::P4Table *t, IR::TCTabl "annotated with '@default_hit' and '@default_hit_const'", t->name.originalName, action->getName().originalName); break; + } else if (isTcMayOverrideHit) { + auto adecl = refMap->getDeclaration(action->getPath(), true); + auto p4Action = adecl->getNode()->checkedTo(); + if (!isDefaultHit && !isDefaultHitConst) { + ::warning(ErrorType::WARN_INVALID, + "Table '%1%' has an action reference '%2%' which is " + "annotated with '@tc_may_override' without '@default_hit' or " + "'@default_hit_const'", + t->name.originalName, action->getName().originalName); + isTcMayOverrideHit = false; + break; + } else if (p4Action->getParameters()->parameters.empty()) { + ::warning(ErrorType::WARN_INVALID, + " '@tc_may_override' cannot be used for %1% action " + " without parameters", + action->getName().originalName); + isTcMayOverrideHit = false; + break; + } + isTcMayOverrideHitAction = true; } } if (::errorCount() > 0) { @@ -512,6 +547,16 @@ void ConvertToBackendIR::updateDefaultHitAction(const IR::P4Table *t, IR::TCTabl if (defaultHitConst == 1) { tabledef->setDefaultHitConst(true); } + if (isTcMayOverrideHitAction) { + if (!checkParameterDirection(tcAction)) { + tabledef->setTcMayOverrideHit(); + for (auto param : tcAction->actionParams) { + auto defaultParam = new IR::TCDefaultActionParam(); + defaultParam->setParamDetail(param); + tabledef->defaultHitActionParams.push_back(defaultParam); + } + } + } } } } @@ -1101,4 +1146,15 @@ void ConvertToBackendIR::updateMatchType(const IR::P4Table *t, IR::TCTable *tabl tabledef->setMatchType(tableMatchType); } +bool ConvertToBackendIR::checkParameterDirection(const IR::TCAction *tcAction) { + bool dirParam = false; + for (auto actionParam : tcAction->actionParams) { + if (actionParam->getDirection() != TC::NONE) { + dirParam = true; + break; + } + } + return dirParam; +} + } // namespace TC diff --git a/backends/tc/backend.h b/backends/tc/backend.h index 71892084d37..09722983f00 100644 --- a/backends/tc/backend.h +++ b/backends/tc/backend.h @@ -123,6 +123,7 @@ class ConvertToBackendIR : public Inspector { cstring HandleTableAccessPermission(const IR::P4Table *t); std::pair *GetAnnotatedAccessPath(const IR::Annotation *anno); void updateAddOnMissTable(const IR::P4Table *t); + bool checkParameterDirection(const IR::TCAction *tcAction); }; class Extern { diff --git a/backends/tc/ebpfCodeGen.cpp b/backends/tc/ebpfCodeGen.cpp index c7cc5dd79a1..1a62b310a75 100644 --- a/backends/tc/ebpfCodeGen.cpp +++ b/backends/tc/ebpfCodeGen.cpp @@ -133,7 +133,7 @@ void PNAEbpfGenerator::emitP4TCFilterFields(EBPF::CodeBuilder *builder) const { void PNAEbpfGenerator::emitP4TCActionParam(EBPF::CodeBuilder *builder) const { for (auto table : tcIR->tcPipeline->tableDefs) { - if (table->isTcMayOverride) { + if (table->isTcMayOverrideMiss) { cstring tblName = table->getTableName(); cstring defaultActionName = table->defaultMissAction->getActionName(); defaultActionName = defaultActionName.substr( @@ -148,6 +148,21 @@ void PNAEbpfGenerator::emitP4TCActionParam(EBPF::CodeBuilder *builder) const { builder->endOfStatement(true); } } + if (table->isTcMayOverrideHit) { + cstring tblName = table->getTableName(); + cstring defaultActionName = table->defaultHitAction->getActionName(); + defaultActionName = defaultActionName.substr( + defaultActionName.find('/') - defaultActionName.begin() + 1, + defaultActionName.size()); + for (auto param : table->defaultHitActionParams) { + cstring paramName = param->paramDetail->getParamName(); + cstring placeholder = tblName + "_" + defaultActionName + "_" + paramName; + cstring typeName = param->paramDetail->getParamType(); + builder->emitIndent(); + builder->appendFormat("%s %s", typeName, placeholder); + builder->endOfStatement(true); + } + } } } @@ -968,9 +983,10 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName, } } bool generateDefaultMissCode = false; + bool generateDefaultHitCode = false; for (auto tbl : tcIR->tcPipeline->tableDefs) { if (tbl->getTableName() == table->container->name.originalName) { - if (tbl->isTcMayOverride) { + if (tbl->isTcMayOverrideMiss) { cstring defaultActionName = tbl->defaultMissAction->getActionName(); defaultActionName = defaultActionName.substr( defaultActionName.find('/') - defaultActionName.begin() + 1, @@ -978,16 +994,27 @@ void EBPFTablePNA::emitAction(EBPF::CodeBuilder *builder, cstring valueName, if (defaultActionName == action->name.originalName) generateDefaultMissCode = true; } + if (tbl->isTcMayOverrideHit) { + cstring defaultActionName = tbl->defaultHitAction->getActionName(); + defaultActionName = defaultActionName.substr( + defaultActionName.find('/') - defaultActionName.begin() + 1, + defaultActionName.size()); + if (defaultActionName == action->name.originalName) + generateDefaultHitCode = true; + } } } - if (generateDefaultMissCode) { + if (generateDefaultMissCode || generateDefaultHitCode) { builder->emitIndent(); builder->appendFormat("{"); builder->newline(); builder->increaseIndent(); builder->emitIndent(); - builder->appendFormat("if (%s->is_default_miss_act) ", valueName.c_str()); + if (generateDefaultMissCode) + builder->appendFormat("if (%s->is_default_miss_act) ", valueName.c_str()); + else if (generateDefaultHitCode) + builder->appendFormat("if (%s->is_default_hit_act) ", valueName.c_str()); builder->newline(); builder->emitIndent(); diff --git a/backends/tc/tc.def b/backends/tc/tc.def index 60fd6015705..b0d48960438 100644 --- a/backends/tc/tc.def +++ b/backends/tc/tc.def @@ -49,6 +49,7 @@ class TCActionParam { cstring paramName; unsigned dataType; unsigned bitSize; + unsigned direction; void setParamName(cstring pN) { paramName = pN; } @@ -58,6 +59,9 @@ class TCActionParam { void setDataType(unsigned d) { dataType = d; } + void setDirection(unsigned d){ + direction = d; + } TCActionParam() { paramName = nullptr; bitSize = 0; @@ -68,6 +72,9 @@ class TCActionParam { cstring getName() const { return "param." + paramName; } + unsigned getDirection() const { + return direction; + } cstring getParamType() const { std::string paramType = ""; switch(dataType) { @@ -240,11 +247,13 @@ class TCTable { unsigned defaultTimerProfiles = 4; TCAction defaultHitAction; bool isDefaultHitConst; + bool isTcMayOverrideHit; + optional safe_vector defaultHitActionParams; TCAction defaultMissAction; - optional safe_vector defaultMissActionParams; bool isDefaultMissConst; + bool isTcMayOverrideMiss; + optional safe_vector defaultMissActionParams; bool isTableAddOnMiss; - bool isTcMayOverride; ordered_map actionList; safe_vector const_entries; @@ -275,12 +284,15 @@ class TCTable { void setDefaultMissConst(bool i) { isDefaultMissConst = i; } + void setTcMayOverrideHit() { + isTcMayOverrideHit = true; + } + void setTcMayOverrideMiss() { + isTcMayOverrideMiss = true; + } void setTableAddOnMiss() { isTableAddOnMiss = true; } - void setTcMayOverride() { - isTcMayOverride = true; - } void addAction(TCAction action, unsigned flag) { actionList.emplace(action, flag); } @@ -322,8 +334,9 @@ class TCTable { defaultMissAction = nullptr; isDefaultHitConst = false; isDefaultMissConst = false; + isTcMayOverrideHit = false; + isTcMayOverrideMiss = false; isTableAddOnMiss = false; - isTcMayOverride = false; } toString { std::string tcTable = ""; @@ -363,6 +376,12 @@ class TCTable { tcTable += " permissions 0x1024"; } tcTable += " action " + defaultHitAction->getName(); + if (!defaultHitActionParams.empty()) + tcTable += " param"; + for (auto param : defaultHitActionParams) + tcTable += param->toString(); + if (isTcMayOverrideHit) + tcTable += " flags runtime"; } if (defaultMissAction != nullptr) { tcTable += "\n$TC p4template update table/" + pipelineName @@ -376,7 +395,7 @@ class TCTable { tcTable += " param"; for (auto param : defaultMissActionParams) tcTable += param->toString(); - if (isTcMayOverride) + if (isTcMayOverrideMiss) tcTable += " flags runtime"; } if (const_entries.size() != 0) { diff --git a/backends/tc/tc_defines.h b/backends/tc/tc_defines.h index 5a315c33980..8c448b06574 100644 --- a/backends/tc/tc_defines.h +++ b/backends/tc/tc_defines.h @@ -86,6 +86,11 @@ inline constexpr auto DEFAULTONLY = 2; inline constexpr auto EXACT_TYPE = 0; inline constexpr auto LPM_TYPE = 1; inline constexpr auto TERNARY_TYPE = 2; + +inline constexpr auto NONE = 0; +inline constexpr auto IN = 1; +inline constexpr auto OUT = 2; +inline constexpr auto INOUT = 3; } // namespace TC #endif /* BACKENDS_TC_TC_DEFINES_H_ */ diff --git a/testdata/p4tc_samples/tc_may_override_example_01.p4 b/testdata/p4tc_samples/tc_may_override_example_01.p4 index 8a4a37a22cb..11b0e10b0a7 100644 --- a/testdata/p4tc_samples/tc_may_override_example_01.p4 +++ b/testdata/p4tc_samples/tc_may_override_example_01.p4 @@ -99,7 +99,7 @@ control MainControlImpl( hdr.ipv4.protocol : exact; } actions = { - next_hop; + @tc_may_override @default_hit_const next_hop; drop; } default_action = drop; diff --git a/testdata/p4tc_samples/tc_may_override_example_07.p4 b/testdata/p4tc_samples/tc_may_override_example_07.p4 new file mode 100644 index 00000000000..743addc1af0 --- /dev/null +++ b/testdata/p4tc_samples/tc_may_override_example_07.p4 @@ -0,0 +1,135 @@ +#include +#include + +typedef bit<48> EthernetAddress; + +header ethernet_t { + EthernetAddress dstAddr; + EthernetAddress srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + @tc_type ("ipv4") bit<32> srcAddr; + @tc_type ("ipv4") bit<32> dstAddr; +} + +////////////////////////////////////////////////////////////////////// +// Struct types for holding user-defined collections of headers and +// metadata in the P4 developer's program. +// +// Note: The names of these struct types are completely up to the P4 +// developer, as are their member fields, with the only restriction +// being that the structs intended to contain headers should only +// contain members whose types are header, header stack, or +// header_union. +////////////////////////////////////////////////////////////////////// + +struct main_metadata_t { + // empty for this skeleton +} + +// User-defined struct containing all of those headers parsed in the +// main parser. +struct headers_t { + ethernet_t ethernet; + ipv4_t ipv4; +} + +parser MainParserImpl( + packet_in pkt, + out headers_t hdr, + inout main_metadata_t main_meta, + in pna_main_parser_input_metadata_t istd) +{ + state start { + pkt.extract(hdr.ethernet); + transition select(hdr.ethernet.etherType) { + 0x0800 : parse_ipv4; + default : accept; + } + } + state parse_ipv4 { + pkt.extract(hdr.ipv4); + transition accept; + } +} + +control MainControlImpl( + inout headers_t hdr, // from main parser + inout main_metadata_t user_meta, // from main parser, to "next block" + in pna_main_input_metadata_t istd, + inout pna_main_output_metadata_t ostd) +{ + action next_hop(PortId_t vport) { + send_to_port(vport); + } + action dflt_route_drop() { + drop_packet(); + } + action drop() { + drop_packet(); + } + + table ipv4_tbl_1 { + key = { + hdr.ipv4.dstAddr : exact; + istd.input_port : exact; + } + actions = { + next_hop; + dflt_route_drop; + } + @tc_may_override default_action = next_hop((PortId_t)123); + } + table ipv4_tbl_2 { + key = { + hdr.ipv4.dstAddr : exact; + hdr.ipv4.srcAddr : exact; + hdr.ipv4.protocol : exact; + } + actions = { + @default_hit @tc_may_override next_hop; + dflt_route_drop; + drop; + } + default_action = drop; + } + + apply { + if (hdr.ipv4.isValid()) { + ipv4_tbl_1.apply(); + ipv4_tbl_2.apply(); + } + } +} + +control MainDeparserImpl( + packet_out pkt, + inout headers_t hdr, // from main control + in main_metadata_t user_meta, // from main control + in pna_main_output_metadata_t ostd) +{ + apply { + pkt.emit(hdr.ethernet); + pkt.emit(hdr.ipv4); + } +} + +// BEGIN:Package_Instantiation_Example +PNA_NIC( + MainParserImpl(), + MainControlImpl(), + MainDeparserImpl() + ) main; +// END:Package_Instantiation_Example diff --git a/testdata/p4tc_samples/tc_may_override_example_08.p4 b/testdata/p4tc_samples/tc_may_override_example_08.p4 new file mode 100644 index 00000000000..0103d53ebc3 --- /dev/null +++ b/testdata/p4tc_samples/tc_may_override_example_08.p4 @@ -0,0 +1,135 @@ +#include +#include + +typedef bit<48> EthernetAddress; + +header ethernet_t { + EthernetAddress dstAddr; + EthernetAddress srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + @tc_type ("ipv4") bit<32> srcAddr; + @tc_type ("ipv4") bit<32> dstAddr; +} + +////////////////////////////////////////////////////////////////////// +// Struct types for holding user-defined collections of headers and +// metadata in the P4 developer's program. +// +// Note: The names of these struct types are completely up to the P4 +// developer, as are their member fields, with the only restriction +// being that the structs intended to contain headers should only +// contain members whose types are header, header stack, or +// header_union. +////////////////////////////////////////////////////////////////////// + +struct main_metadata_t { + // empty for this skeleton +} + +// User-defined struct containing all of those headers parsed in the +// main parser. +struct headers_t { + ethernet_t ethernet; + ipv4_t ipv4; +} + +parser MainParserImpl( + packet_in pkt, + out headers_t hdr, + inout main_metadata_t main_meta, + in pna_main_parser_input_metadata_t istd) +{ + state start { + pkt.extract(hdr.ethernet); + transition select(hdr.ethernet.etherType) { + 0x0800 : parse_ipv4; + default : accept; + } + } + state parse_ipv4 { + pkt.extract(hdr.ipv4); + transition accept; + } +} + +control MainControlImpl( + inout headers_t hdr, // from main parser + inout main_metadata_t user_meta, // from main parser, to "next block" + in pna_main_input_metadata_t istd, + inout pna_main_output_metadata_t ostd) +{ + action next_hop(PortId_t vport) { + send_to_port(vport); + } + action dflt_route_drop() { + drop_packet(); + } + action drop() { + drop_packet(); + } + + table ipv4_tbl_1 { + key = { + hdr.ipv4.dstAddr : exact; + istd.input_port : exact; + } + actions = { + next_hop; + dflt_route_drop; + } + @tc_may_override default_action = next_hop((PortId_t)123); + } + table ipv4_tbl_2 { + key = { + hdr.ipv4.dstAddr : exact; + hdr.ipv4.srcAddr : exact; + hdr.ipv4.protocol : exact; + } + actions = { + @default_hit_const @tc_may_override next_hop; + dflt_route_drop; + drop; + } + default_action = drop; + } + + apply { + if (hdr.ipv4.isValid()) { + ipv4_tbl_1.apply(); + ipv4_tbl_2.apply(); + } + } +} + +control MainDeparserImpl( + packet_out pkt, + inout headers_t hdr, // from main control + in main_metadata_t user_meta, // from main control + in pna_main_output_metadata_t ostd) +{ + apply { + pkt.emit(hdr.ethernet); + pkt.emit(hdr.ipv4); + } +} + +// BEGIN:Package_Instantiation_Example +PNA_NIC( + MainParserImpl(), + MainControlImpl(), + MainDeparserImpl() + ) main; +// END:Package_Instantiation_Example diff --git a/testdata/p4tc_samples/tc_may_override_example_09.p4 b/testdata/p4tc_samples/tc_may_override_example_09.p4 new file mode 100644 index 00000000000..94db8ac351e --- /dev/null +++ b/testdata/p4tc_samples/tc_may_override_example_09.p4 @@ -0,0 +1,135 @@ +#include +#include + +typedef bit<48> EthernetAddress; + +header ethernet_t { + EthernetAddress dstAddr; + EthernetAddress srcAddr; + bit<16> etherType; +} + +header ipv4_t { + bit<4> version; + bit<4> ihl; + bit<8> diffserv; + bit<16> totalLen; + bit<16> identification; + bit<3> flags; + bit<13> fragOffset; + bit<8> ttl; + bit<8> protocol; + bit<16> hdrChecksum; + @tc_type ("ipv4") bit<32> srcAddr; + @tc_type ("ipv4") bit<32> dstAddr; +} + +////////////////////////////////////////////////////////////////////// +// Struct types for holding user-defined collections of headers and +// metadata in the P4 developer's program. +// +// Note: The names of these struct types are completely up to the P4 +// developer, as are their member fields, with the only restriction +// being that the structs intended to contain headers should only +// contain members whose types are header, header stack, or +// header_union. +////////////////////////////////////////////////////////////////////// + +struct main_metadata_t { + // empty for this skeleton +} + +// User-defined struct containing all of those headers parsed in the +// main parser. +struct headers_t { + ethernet_t ethernet; + ipv4_t ipv4; +} + +parser MainParserImpl( + packet_in pkt, + out headers_t hdr, + inout main_metadata_t main_meta, + in pna_main_parser_input_metadata_t istd) +{ + state start { + pkt.extract(hdr.ethernet); + transition select(hdr.ethernet.etherType) { + 0x0800 : parse_ipv4; + default : accept; + } + } + state parse_ipv4 { + pkt.extract(hdr.ipv4); + transition accept; + } +} + +control MainControlImpl( + inout headers_t hdr, // from main parser + inout main_metadata_t user_meta, // from main parser, to "next block" + in pna_main_input_metadata_t istd, + inout pna_main_output_metadata_t ostd) +{ + action next_hop(PortId_t vport) { + send_to_port(vport); + } + action dflt_route_drop() { + drop_packet(); + } + action drop() { + drop_packet(); + } + + table ipv4_tbl_1 { + key = { + hdr.ipv4.dstAddr : exact; + istd.input_port : exact; + } + actions = { + next_hop; + dflt_route_drop; + } + @tc_may_override default_action = next_hop((PortId_t)123); + } + table ipv4_tbl_2 { + key = { + hdr.ipv4.dstAddr : exact; + hdr.ipv4.srcAddr : exact; + hdr.ipv4.protocol : exact; + } + actions = { + @tc_may_override next_hop; + dflt_route_drop; + drop; + } + default_action = drop; + } + + apply { + if (hdr.ipv4.isValid()) { + ipv4_tbl_1.apply(); + ipv4_tbl_2.apply(); + } + } +} + +control MainDeparserImpl( + packet_out pkt, + inout headers_t hdr, // from main control + in main_metadata_t user_meta, // from main control + in pna_main_output_metadata_t ostd) +{ + apply { + pkt.emit(hdr.ethernet); + pkt.emit(hdr.ipv4); + } +} + +// BEGIN:Package_Instantiation_Example +PNA_NIC( + MainParserImpl(), + MainControlImpl(), + MainDeparserImpl() + ) main; +// END:Package_Instantiation_Example diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_01.json b/testdata/p4tc_samples_outputs/tc_may_override_example_01.json index 2c269c3b168..4dbb0a8e4d1 100644 --- a/testdata/p4tc_samples_outputs/tc_may_override_example_01.json +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_01.json @@ -89,7 +89,7 @@ "id" : 1, "name" : "MainControlImpl/next_hop", "action_scope" : "TableAndDefault", - "annotations" : [], + "annotations" : ["tc_may_override", "default_hit_const"], "params" : [ { "id" : 1, @@ -98,7 +98,7 @@ "bitwidth" : 32 } ], - "default_hit_action" : false, + "default_hit_action" : true, "default_miss_action" : false }, { diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_01.template b/testdata/p4tc_samples_outputs/tc_may_override_example_01.template index 7766296cd10..dbb65a1d0d6 100755 --- a/testdata/p4tc_samples_outputs/tc_may_override_example_01.template +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_01.template @@ -29,5 +29,6 @@ $TC p4template create table/tc_may_override_example_01/MainControlImpl/ipv4_tbl_ keysz 72 nummasks 8 permissions 0x3da4 tentries 2048 \ table_acts act name tc_may_override_example_01/MainControlImpl/next_hop \ act name tc_may_override_example_01/MainControlImpl/drop +$TC p4template update table/tc_may_override_example_01/MainControlImpl/ipv4_tbl_2 default_hit_action permissions 0x1024 action tc_may_override_example_01/MainControlImpl/next_hop param vport flags runtime $TC p4template update table/tc_may_override_example_01/MainControlImpl/ipv4_tbl_2 default_miss_action action tc_may_override_example_01/MainControlImpl/drop $TC p4template update pipeline/tc_may_override_example_01 state ready \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_01_control_blocks.c b/testdata/p4tc_samples_outputs/tc_may_override_example_01_control_blocks.c index 2464b93a6ea..f7417585397 100644 --- a/testdata/p4tc_samples_outputs/tc_may_override_example_01_control_blocks.c +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_01_control_blocks.c @@ -170,9 +170,18 @@ if (/* hdr->ipv4.isValid() */ switch (value->action) { case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP: { + if (value->is_default_hit_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: diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_01_parser.h b/testdata/p4tc_samples_outputs/tc_may_override_example_01_parser.h index 780f9fb5632..6af1f29ed1d 100644 --- a/testdata/p4tc_samples_outputs/tc_may_override_example_01_parser.h +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_01_parser.h @@ -56,6 +56,7 @@ struct p4tc_filter_fields { __be16 proto; __u16 prio; __u32 ipv4_tbl_1_next_hop_vport; + __u32 ipv4_tbl_2_next_hop_vport; }; REGISTER_START() diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07.json b/testdata/p4tc_samples_outputs/tc_may_override_example_07.json new file mode 100644 index 00000000000..246bb0e402a --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_07.json @@ -0,0 +1,125 @@ +{ + "schema_version" : "1.0.0", + "pipeline_name" : "tc_may_override_example_07", + "externs" : [], + "tables" : [ + { + "name" : "MainControlImpl/ipv4_tbl_1", + "id" : 1, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 64, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "istd.input_port", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : false, + "default_miss_action" : true + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + } + ] + }, + { + "name" : "MainControlImpl/ipv4_tbl_2", + "id" : 2, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 72, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "hdr.ipv4.srcAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 3, + "name" : "hdr.ipv4.protocol", + "type" : "bit8", + "match_type" : "exact", + "bitwidth" : 8 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : ["default_hit", "tc_may_override"], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : true, + "default_miss_action" : false + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + }, + { + "id" : 3, + "name" : "MainControlImpl/drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : true + } + ] + } + ] +} \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07.p4-stderr b/testdata/p4tc_samples_outputs/tc_may_override_example_07.p4-stderr new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07.template b/testdata/p4tc_samples_outputs/tc_may_override_example_07.template new file mode 100755 index 00000000000..26b1ebc29c4 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_07.template @@ -0,0 +1,35 @@ +#!/bin/bash -x + +set -e + +TC="tc" +$TC p4template create pipeline/tc_may_override_example_07 numtables 2 + +$TC p4template create action/tc_may_override_example_07/MainControlImpl/next_hop actid 1 \ + param vport type bit32 +$TC p4template update action/tc_may_override_example_07/MainControlImpl/next_hop state active + +$TC p4template create action/tc_may_override_example_07/MainControlImpl/dflt_route_drop actid 2 +$TC p4template update action/tc_may_override_example_07/MainControlImpl/dflt_route_drop state active + +$TC p4template create action/tc_may_override_example_07/MainControlImpl/drop actid 3 +$TC p4template update action/tc_may_override_example_07/MainControlImpl/drop state active + +$TC p4template create table/tc_may_override_example_07/MainControlImpl/ipv4_tbl_1 \ + tblid 1 \ + type exact \ + keysz 64 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_07/MainControlImpl/next_hop \ + act name tc_may_override_example_07/MainControlImpl/dflt_route_drop +$TC p4template update table/tc_may_override_example_07/MainControlImpl/ipv4_tbl_1 default_miss_action action tc_may_override_example_07/MainControlImpl/next_hop param vport flags runtime + +$TC p4template create table/tc_may_override_example_07/MainControlImpl/ipv4_tbl_2 \ + tblid 2 \ + type exact \ + keysz 72 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_07/MainControlImpl/next_hop \ + act name tc_may_override_example_07/MainControlImpl/dflt_route_drop \ + act name tc_may_override_example_07/MainControlImpl/drop +$TC p4template update table/tc_may_override_example_07/MainControlImpl/ipv4_tbl_2 default_hit_action action tc_may_override_example_07/MainControlImpl/next_hop param vport flags runtime +$TC p4template update table/tc_may_override_example_07/MainControlImpl/ipv4_tbl_2 default_miss_action action tc_may_override_example_07/MainControlImpl/drop +$TC p4template update pipeline/tc_may_override_example_07 state ready \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07_control_blocks.c b/testdata/p4tc_samples_outputs/tc_may_override_example_07_control_blocks.c new file mode 100644 index 00000000000..4acc091c622 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_07_control_blocks.c @@ -0,0 +1,396 @@ +#include "tc_may_override_example_07_parser.h" +struct p4tc_filter_fields p4tc_filter_fields; + +struct internal_metadata { + __u16 pkt_ether_type; +} __attribute__((aligned(4))); + +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* istd.input_port */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0 +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value { + unsigned int action; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + } u; +}; +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* hdr.ipv4.srcAddr */ + u8 field2; /* hdr.ipv4.protocol */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#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; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + struct { + } MainControlImpl_drop; + } u; +}; + +static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits; + hdr_start = pkt + BYTES(ebpf_packetOffsetInBits); + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); +{ + u8 hit; + { +if (/* hdr->ipv4.isValid() */ + hdr->ipv4.ebpf_valid) { +/* ipv4_tbl.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 1 + }; + struct MainControlImpl_ipv4_tbl_1_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 64; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = skb->ifindex; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_1_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_1_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + 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); + } + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + /* ipv4_tbl_0.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 2 + }; + struct MainControlImpl_ipv4_tbl_2_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 72; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = hdr->ipv4.srcAddr; + key.field2 = hdr->ipv4.protocol; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_2_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_2_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + switch (value->action) { + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP: + { + if (value->is_default_hit_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); + } + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + } + } + } + { +{ +; + ; + } + + if (compiler_meta__->drop) { + return TC_ACT_SHOT; + } + int outHeaderLength = 0; + if (hdr->ethernet.ebpf_valid) { + outHeaderLength += 112; + } +; if (hdr->ipv4.ebpf_valid) { + outHeaderLength += 160; + } +; + int outHeaderOffset = BYTES(outHeaderLength) - (hdr_start - (u8*)pkt); + if (outHeaderOffset != 0) { + int returnCode = 0; + returnCode = bpf_skb_adjust_room(skb, outHeaderOffset, 1, 0); + if (returnCode) { + return TC_ACT_SHOT; + } + } + pkt = ((void*)(long)skb->data); + ebpf_packetEnd = ((void*)(long)skb->data_end); + ebpf_packetOffsetInBits = 0; + if (hdr->ethernet.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 112)) { + return TC_ACT_SHOT; + } + + hdr->ethernet.dstAddr = htonll(hdr->ethernet.dstAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = htonll(hdr->ethernet.srcAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = bpf_htons(hdr->ethernet.etherType); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + } +; if (hdr->ipv4.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 160)) { + return TC_ACT_SHOT; + } + + ebpf_byte = ((char*)(&hdr->ipv4.version))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 4, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.ihl))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 0, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.diffserv))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = bpf_htons(hdr->ipv4.totalLen); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = bpf_htons(hdr->ipv4.identification); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.flags))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 3, 5, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = bpf_htons(hdr->ipv4.fragOffset << 3); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 5, 0, (ebpf_byte >> 3)); + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0 + 1, 3, 5, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[1]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 1, 5, 0, (ebpf_byte >> 3)); + ebpf_packetOffsetInBits += 13; + + ebpf_byte = ((char*)(&hdr->ipv4.ttl))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + ebpf_byte = ((char*)(&hdr->ipv4.protocol))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = bpf_htons(hdr->ipv4.hdrChecksum); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + } +; + } + return -1; +} +SEC("p4tc/main") +int tc_ingress_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + if (compiler_meta__->pass_to_kernel == true) return TC_ACT_OK; + if (!compiler_meta__->recirculated) { + compiler_meta__->mark = 153; + struct internal_metadata *md = (struct internal_metadata *)(unsigned long)skb->data_meta; + if ((void *) ((struct internal_metadata *) md + 1) <= (void *)(long)skb->data) { + __u16 *ether_type = (__u16 *) ((void *) (long)skb->data + 12); + if ((void *) ((__u16 *) ether_type + 1) > (void *) (long) skb->data_end) { + return TC_ACT_SHOT; + } + *ether_type = md->pkt_ether_type; + } + } + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = process(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + if (!compiler_meta__->drop && compiler_meta__->egress_port == 0) { + compiler_meta__->pass_to_kernel = true; + return bpf_redirect(skb->ifindex, BPF_F_INGRESS); + } + return bpf_redirect(compiler_meta__->egress_port, 0); +} +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.c b/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.c new file mode 100644 index 00000000000..aafa7c3dedc --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.c @@ -0,0 +1,136 @@ +#include "tc_may_override_example_07_parser.h" + +struct p4tc_filter_fields p4tc_filter_fields; + +static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + __builtin_memset(hdrMd, 0, sizeof(struct hdr_md)); + + unsigned ebpf_packetOffsetInBits = 0; + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); + { + goto start; + parse_ipv4: { +/* extract(hdr->ipv4) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(160 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ipv4.version = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 4) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.ihl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13)); + ebpf_packetOffsetInBits += 13; + + hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + __builtin_memcpy(&hdr->ipv4.srcAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + __builtin_memcpy(&hdr->ipv4.dstAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + + hdr->ipv4.ebpf_valid = 1; + hdr_start += BYTES(160); + +; + goto accept; + } + start: { +/* extract(hdr->ethernet) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(112 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ethernet.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + + hdr->ethernet.ebpf_valid = 1; + hdr_start += BYTES(112); + +; + u16 select_0; + select_0 = hdr->ethernet.etherType; + if (select_0 == 0x800)goto parse_ipv4; + if ((select_0 & 0x0) == (0x0 & 0x0))goto accept; + else goto reject; + } + + reject: { + if (ebpf_errorCode == 0) { + return TC_ACT_SHOT; + } + compiler_meta__->parser_error = ebpf_errorCode; + goto accept; + } + + } + + accept: + hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits; + return -1; +} + +SEC("p4tc/parse") +int tc_parse_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = run_parser(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + return TC_ACT_PIPE; + } +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.h b/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.h new file mode 100644 index 00000000000..6af1f29ed1d --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_07_parser.h @@ -0,0 +1,166 @@ +#include "ebpf_kernel.h" + +#include +#include +#include +#include "pna.h" + +#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1) +#define BYTES(w) ((w) / 8) +#define write_partial(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) & ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0) +#define write_byte(base, offset, v) do { *(u8*)((base) + (offset)) = (v); } while (0) +#define bpf_trace_message(fmt, ...) + + +struct ethernet_t { + u64 dstAddr; /* EthernetAddress */ + u64 srcAddr; /* EthernetAddress */ + u16 etherType; /* bit<16> */ + u8 ebpf_valid; +}; +struct ipv4_t { + u8 version; /* bit<4> */ + u8 ihl; /* bit<4> */ + u8 diffserv; /* bit<8> */ + u16 totalLen; /* bit<16> */ + u16 identification; /* bit<16> */ + u8 flags; /* bit<3> */ + u16 fragOffset; /* bit<13> */ + u8 ttl; /* bit<8> */ + u8 protocol; /* bit<8> */ + u16 hdrChecksum; /* bit<16> */ + u32 srcAddr; /* bit<32> */ + u32 dstAddr; /* bit<32> */ + u8 ebpf_valid; +}; +struct main_metadata_t { +}; +struct headers_t { + struct ethernet_t ethernet; /* ethernet_t */ + struct ipv4_t ipv4; /* ipv4_t */ +}; + +struct hdr_md { + struct headers_t cpumap_hdr; + struct main_metadata_t cpumap_usermeta; + unsigned ebpf_packetOffsetInBits; + __u8 __hook; +}; + +struct p4tc_filter_fields { + __u32 pipeid; + __u32 handle; + __u32 classid; + __u32 chain; + __u32 blockid; + __be16 proto; + __u16 prio; + __u32 ipv4_tbl_1_next_hop_vport; + __u32 ipv4_tbl_2_next_hop_vport; +}; + +REGISTER_START() +REGISTER_TABLE(hdr_md_cpumap, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct hdr_md, 2) +BPF_ANNOTATE_KV_PAIR(hdr_md_cpumap, u32, struct hdr_md) +REGISTER_END() + +static __always_inline +void crc16_update(u16 * reg, const u8 * data, u16 data_size, const u16 poly) { + if (data_size <= 8) + data += data_size - 1; + #pragma clang loop unroll(full) + for (u16 i = 0; i < data_size; i++) { + bpf_trace_message("CRC16: data byte: %x\n", *data); + *reg ^= *data; + for (u8 bit = 0; bit < 8; bit++) { + *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1; + } + if (data_size <= 8) + data--; + else + data++; + } +} +static __always_inline u16 crc16_finalize(u16 reg) { + return reg; +} +static __always_inline +void crc32_update(u32 * reg, const u8 * data, u16 data_size, const u32 poly) { + u32* current = (u32*) data; + u32 index = 0; + u32 lookup_key = 0; + u32 lookup_value = 0; + u32 lookup_value1 = 0; + u32 lookup_value2 = 0; + u32 lookup_value3 = 0; + u32 lookup_value4 = 0; + u32 lookup_value5 = 0; + u32 lookup_value6 = 0; + u32 lookup_value7 = 0; + u32 lookup_value8 = 0; + u16 tmp = 0; + if (crc32_table != NULL) { + for (u16 i = data_size; i >= 8; i -= 8) { + /* Vars one and two will have swapped byte order if data_size == 8 */ + if (data_size == 8) current = (u32 *)(data + 4); + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 one = (data_size == 8 ? __builtin_bswap32(*current--) : *current++) ^ *reg; + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 two = (data_size == 8 ? __builtin_bswap32(*current--) : *current++); + lookup_key = (one & 0x000000FF); + lookup_value8 = crc32_table[(u16)(1792 + (u8)lookup_key)]; + lookup_key = (one >> 8) & 0x000000FF; + lookup_value7 = crc32_table[(u16)(1536 + (u8)lookup_key)]; + lookup_key = (one >> 16) & 0x000000FF; + lookup_value6 = crc32_table[(u16)(1280 + (u8)lookup_key)]; + lookup_key = one >> 24; + lookup_value5 = crc32_table[(u16)(1024 + (u8)(lookup_key))]; + lookup_key = (two & 0x000000FF); + lookup_value4 = crc32_table[(u16)(768 + (u8)lookup_key)]; + lookup_key = (two >> 8) & 0x000000FF; + lookup_value3 = crc32_table[(u16)(512 + (u8)lookup_key)]; + lookup_key = (two >> 16) & 0x000000FF; + lookup_value2 = crc32_table[(u16)(256 + (u8)lookup_key)]; + lookup_key = two >> 24; + lookup_value1 = crc32_table[(u8)(lookup_key)]; + *reg = lookup_value8 ^ lookup_value7 ^ lookup_value6 ^ lookup_value5 ^ + lookup_value4 ^ lookup_value3 ^ lookup_value2 ^ lookup_value1; + tmp += 8; + } + volatile int std_algo_lookup_key = 0; + if (data_size < 8) { + unsigned char *currentChar = (unsigned char *) current; + currentChar += data_size - 1; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar--); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } else { + /* Consume data not processed by slice-by-8 algorithm above, these data are in network byte order */ + unsigned char *currentChar = (unsigned char *) current; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar++); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } + } +} +static __always_inline u32 crc32_finalize(u32 reg) { + return reg ^ 0xFFFFFFFF; +} +inline u16 csum16_add(u16 csum, u16 addend) { + u16 res = csum; + res += addend; + return (res + (res < addend)); +} +inline u16 csum16_sub(u16 csum, u16 addend) { + return csum16_add(csum, ~addend); +} diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08.json b/testdata/p4tc_samples_outputs/tc_may_override_example_08.json new file mode 100644 index 00000000000..158b27498ec --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_08.json @@ -0,0 +1,125 @@ +{ + "schema_version" : "1.0.0", + "pipeline_name" : "tc_may_override_example_08", + "externs" : [], + "tables" : [ + { + "name" : "MainControlImpl/ipv4_tbl_1", + "id" : 1, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 64, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "istd.input_port", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : false, + "default_miss_action" : true + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + } + ] + }, + { + "name" : "MainControlImpl/ipv4_tbl_2", + "id" : 2, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 72, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "hdr.ipv4.srcAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 3, + "name" : "hdr.ipv4.protocol", + "type" : "bit8", + "match_type" : "exact", + "bitwidth" : 8 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : ["default_hit_const", "tc_may_override"], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : true, + "default_miss_action" : false + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + }, + { + "id" : 3, + "name" : "MainControlImpl/drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : true + } + ] + } + ] +} \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08.p4-stderr b/testdata/p4tc_samples_outputs/tc_may_override_example_08.p4-stderr new file mode 100644 index 00000000000..e69de29bb2d diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08.template b/testdata/p4tc_samples_outputs/tc_may_override_example_08.template new file mode 100755 index 00000000000..4b238f93f88 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_08.template @@ -0,0 +1,35 @@ +#!/bin/bash -x + +set -e + +TC="tc" +$TC p4template create pipeline/tc_may_override_example_08 numtables 2 + +$TC p4template create action/tc_may_override_example_08/MainControlImpl/next_hop actid 1 \ + param vport type bit32 +$TC p4template update action/tc_may_override_example_08/MainControlImpl/next_hop state active + +$TC p4template create action/tc_may_override_example_08/MainControlImpl/dflt_route_drop actid 2 +$TC p4template update action/tc_may_override_example_08/MainControlImpl/dflt_route_drop state active + +$TC p4template create action/tc_may_override_example_08/MainControlImpl/drop actid 3 +$TC p4template update action/tc_may_override_example_08/MainControlImpl/drop state active + +$TC p4template create table/tc_may_override_example_08/MainControlImpl/ipv4_tbl_1 \ + tblid 1 \ + type exact \ + keysz 64 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_08/MainControlImpl/next_hop \ + act name tc_may_override_example_08/MainControlImpl/dflt_route_drop +$TC p4template update table/tc_may_override_example_08/MainControlImpl/ipv4_tbl_1 default_miss_action action tc_may_override_example_08/MainControlImpl/next_hop param vport flags runtime + +$TC p4template create table/tc_may_override_example_08/MainControlImpl/ipv4_tbl_2 \ + tblid 2 \ + type exact \ + keysz 72 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_08/MainControlImpl/next_hop \ + act name tc_may_override_example_08/MainControlImpl/dflt_route_drop \ + act name tc_may_override_example_08/MainControlImpl/drop +$TC p4template update table/tc_may_override_example_08/MainControlImpl/ipv4_tbl_2 default_hit_action permissions 0x1024 action tc_may_override_example_08/MainControlImpl/next_hop param vport flags runtime +$TC p4template update table/tc_may_override_example_08/MainControlImpl/ipv4_tbl_2 default_miss_action action tc_may_override_example_08/MainControlImpl/drop +$TC p4template update pipeline/tc_may_override_example_08 state ready \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08_control_blocks.c b/testdata/p4tc_samples_outputs/tc_may_override_example_08_control_blocks.c new file mode 100644 index 00000000000..fb3a152279d --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_08_control_blocks.c @@ -0,0 +1,396 @@ +#include "tc_may_override_example_08_parser.h" +struct p4tc_filter_fields p4tc_filter_fields; + +struct internal_metadata { + __u16 pkt_ether_type; +} __attribute__((aligned(4))); + +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* istd.input_port */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0 +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value { + unsigned int action; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + } u; +}; +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* hdr.ipv4.srcAddr */ + u8 field2; /* hdr.ipv4.protocol */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#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; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + struct { + } MainControlImpl_drop; + } u; +}; + +static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits; + hdr_start = pkt + BYTES(ebpf_packetOffsetInBits); + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); +{ + u8 hit; + { +if (/* hdr->ipv4.isValid() */ + hdr->ipv4.ebpf_valid) { +/* ipv4_tbl.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 1 + }; + struct MainControlImpl_ipv4_tbl_1_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 64; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = skb->ifindex; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_1_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_1_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + 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); + } + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + /* ipv4_tbl_0.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 2 + }; + struct MainControlImpl_ipv4_tbl_2_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 72; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = hdr->ipv4.srcAddr; + key.field2 = hdr->ipv4.protocol; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_2_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_2_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + switch (value->action) { + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP: + { + if (value->is_default_hit_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); + } + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + } + } + } + { +{ +; + ; + } + + if (compiler_meta__->drop) { + return TC_ACT_SHOT; + } + int outHeaderLength = 0; + if (hdr->ethernet.ebpf_valid) { + outHeaderLength += 112; + } +; if (hdr->ipv4.ebpf_valid) { + outHeaderLength += 160; + } +; + int outHeaderOffset = BYTES(outHeaderLength) - (hdr_start - (u8*)pkt); + if (outHeaderOffset != 0) { + int returnCode = 0; + returnCode = bpf_skb_adjust_room(skb, outHeaderOffset, 1, 0); + if (returnCode) { + return TC_ACT_SHOT; + } + } + pkt = ((void*)(long)skb->data); + ebpf_packetEnd = ((void*)(long)skb->data_end); + ebpf_packetOffsetInBits = 0; + if (hdr->ethernet.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 112)) { + return TC_ACT_SHOT; + } + + hdr->ethernet.dstAddr = htonll(hdr->ethernet.dstAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = htonll(hdr->ethernet.srcAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = bpf_htons(hdr->ethernet.etherType); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + } +; if (hdr->ipv4.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 160)) { + return TC_ACT_SHOT; + } + + ebpf_byte = ((char*)(&hdr->ipv4.version))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 4, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.ihl))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 0, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.diffserv))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = bpf_htons(hdr->ipv4.totalLen); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = bpf_htons(hdr->ipv4.identification); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.flags))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 3, 5, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = bpf_htons(hdr->ipv4.fragOffset << 3); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 5, 0, (ebpf_byte >> 3)); + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0 + 1, 3, 5, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[1]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 1, 5, 0, (ebpf_byte >> 3)); + ebpf_packetOffsetInBits += 13; + + ebpf_byte = ((char*)(&hdr->ipv4.ttl))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + ebpf_byte = ((char*)(&hdr->ipv4.protocol))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = bpf_htons(hdr->ipv4.hdrChecksum); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + } +; + } + return -1; +} +SEC("p4tc/main") +int tc_ingress_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + if (compiler_meta__->pass_to_kernel == true) return TC_ACT_OK; + if (!compiler_meta__->recirculated) { + compiler_meta__->mark = 153; + struct internal_metadata *md = (struct internal_metadata *)(unsigned long)skb->data_meta; + if ((void *) ((struct internal_metadata *) md + 1) <= (void *)(long)skb->data) { + __u16 *ether_type = (__u16 *) ((void *) (long)skb->data + 12); + if ((void *) ((__u16 *) ether_type + 1) > (void *) (long) skb->data_end) { + return TC_ACT_SHOT; + } + *ether_type = md->pkt_ether_type; + } + } + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = process(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + if (!compiler_meta__->drop && compiler_meta__->egress_port == 0) { + compiler_meta__->pass_to_kernel = true; + return bpf_redirect(skb->ifindex, BPF_F_INGRESS); + } + return bpf_redirect(compiler_meta__->egress_port, 0); +} +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.c b/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.c new file mode 100644 index 00000000000..dcb5a694879 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.c @@ -0,0 +1,136 @@ +#include "tc_may_override_example_08_parser.h" + +struct p4tc_filter_fields p4tc_filter_fields; + +static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + __builtin_memset(hdrMd, 0, sizeof(struct hdr_md)); + + unsigned ebpf_packetOffsetInBits = 0; + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); + { + goto start; + parse_ipv4: { +/* extract(hdr->ipv4) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(160 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ipv4.version = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 4) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.ihl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13)); + ebpf_packetOffsetInBits += 13; + + hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + __builtin_memcpy(&hdr->ipv4.srcAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + __builtin_memcpy(&hdr->ipv4.dstAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + + hdr->ipv4.ebpf_valid = 1; + hdr_start += BYTES(160); + +; + goto accept; + } + start: { +/* extract(hdr->ethernet) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(112 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ethernet.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + + hdr->ethernet.ebpf_valid = 1; + hdr_start += BYTES(112); + +; + u16 select_0; + select_0 = hdr->ethernet.etherType; + if (select_0 == 0x800)goto parse_ipv4; + if ((select_0 & 0x0) == (0x0 & 0x0))goto accept; + else goto reject; + } + + reject: { + if (ebpf_errorCode == 0) { + return TC_ACT_SHOT; + } + compiler_meta__->parser_error = ebpf_errorCode; + goto accept; + } + + } + + accept: + hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits; + return -1; +} + +SEC("p4tc/parse") +int tc_parse_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = run_parser(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + return TC_ACT_PIPE; + } +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.h b/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.h new file mode 100644 index 00000000000..6af1f29ed1d --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_08_parser.h @@ -0,0 +1,166 @@ +#include "ebpf_kernel.h" + +#include +#include +#include +#include "pna.h" + +#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1) +#define BYTES(w) ((w) / 8) +#define write_partial(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) & ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0) +#define write_byte(base, offset, v) do { *(u8*)((base) + (offset)) = (v); } while (0) +#define bpf_trace_message(fmt, ...) + + +struct ethernet_t { + u64 dstAddr; /* EthernetAddress */ + u64 srcAddr; /* EthernetAddress */ + u16 etherType; /* bit<16> */ + u8 ebpf_valid; +}; +struct ipv4_t { + u8 version; /* bit<4> */ + u8 ihl; /* bit<4> */ + u8 diffserv; /* bit<8> */ + u16 totalLen; /* bit<16> */ + u16 identification; /* bit<16> */ + u8 flags; /* bit<3> */ + u16 fragOffset; /* bit<13> */ + u8 ttl; /* bit<8> */ + u8 protocol; /* bit<8> */ + u16 hdrChecksum; /* bit<16> */ + u32 srcAddr; /* bit<32> */ + u32 dstAddr; /* bit<32> */ + u8 ebpf_valid; +}; +struct main_metadata_t { +}; +struct headers_t { + struct ethernet_t ethernet; /* ethernet_t */ + struct ipv4_t ipv4; /* ipv4_t */ +}; + +struct hdr_md { + struct headers_t cpumap_hdr; + struct main_metadata_t cpumap_usermeta; + unsigned ebpf_packetOffsetInBits; + __u8 __hook; +}; + +struct p4tc_filter_fields { + __u32 pipeid; + __u32 handle; + __u32 classid; + __u32 chain; + __u32 blockid; + __be16 proto; + __u16 prio; + __u32 ipv4_tbl_1_next_hop_vport; + __u32 ipv4_tbl_2_next_hop_vport; +}; + +REGISTER_START() +REGISTER_TABLE(hdr_md_cpumap, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct hdr_md, 2) +BPF_ANNOTATE_KV_PAIR(hdr_md_cpumap, u32, struct hdr_md) +REGISTER_END() + +static __always_inline +void crc16_update(u16 * reg, const u8 * data, u16 data_size, const u16 poly) { + if (data_size <= 8) + data += data_size - 1; + #pragma clang loop unroll(full) + for (u16 i = 0; i < data_size; i++) { + bpf_trace_message("CRC16: data byte: %x\n", *data); + *reg ^= *data; + for (u8 bit = 0; bit < 8; bit++) { + *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1; + } + if (data_size <= 8) + data--; + else + data++; + } +} +static __always_inline u16 crc16_finalize(u16 reg) { + return reg; +} +static __always_inline +void crc32_update(u32 * reg, const u8 * data, u16 data_size, const u32 poly) { + u32* current = (u32*) data; + u32 index = 0; + u32 lookup_key = 0; + u32 lookup_value = 0; + u32 lookup_value1 = 0; + u32 lookup_value2 = 0; + u32 lookup_value3 = 0; + u32 lookup_value4 = 0; + u32 lookup_value5 = 0; + u32 lookup_value6 = 0; + u32 lookup_value7 = 0; + u32 lookup_value8 = 0; + u16 tmp = 0; + if (crc32_table != NULL) { + for (u16 i = data_size; i >= 8; i -= 8) { + /* Vars one and two will have swapped byte order if data_size == 8 */ + if (data_size == 8) current = (u32 *)(data + 4); + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 one = (data_size == 8 ? __builtin_bswap32(*current--) : *current++) ^ *reg; + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 two = (data_size == 8 ? __builtin_bswap32(*current--) : *current++); + lookup_key = (one & 0x000000FF); + lookup_value8 = crc32_table[(u16)(1792 + (u8)lookup_key)]; + lookup_key = (one >> 8) & 0x000000FF; + lookup_value7 = crc32_table[(u16)(1536 + (u8)lookup_key)]; + lookup_key = (one >> 16) & 0x000000FF; + lookup_value6 = crc32_table[(u16)(1280 + (u8)lookup_key)]; + lookup_key = one >> 24; + lookup_value5 = crc32_table[(u16)(1024 + (u8)(lookup_key))]; + lookup_key = (two & 0x000000FF); + lookup_value4 = crc32_table[(u16)(768 + (u8)lookup_key)]; + lookup_key = (two >> 8) & 0x000000FF; + lookup_value3 = crc32_table[(u16)(512 + (u8)lookup_key)]; + lookup_key = (two >> 16) & 0x000000FF; + lookup_value2 = crc32_table[(u16)(256 + (u8)lookup_key)]; + lookup_key = two >> 24; + lookup_value1 = crc32_table[(u8)(lookup_key)]; + *reg = lookup_value8 ^ lookup_value7 ^ lookup_value6 ^ lookup_value5 ^ + lookup_value4 ^ lookup_value3 ^ lookup_value2 ^ lookup_value1; + tmp += 8; + } + volatile int std_algo_lookup_key = 0; + if (data_size < 8) { + unsigned char *currentChar = (unsigned char *) current; + currentChar += data_size - 1; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar--); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } else { + /* Consume data not processed by slice-by-8 algorithm above, these data are in network byte order */ + unsigned char *currentChar = (unsigned char *) current; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar++); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } + } +} +static __always_inline u32 crc32_finalize(u32 reg) { + return reg ^ 0xFFFFFFFF; +} +inline u16 csum16_add(u16 csum, u16 addend) { + u16 res = csum; + res += addend; + return (res + (res < addend)); +} +inline u16 csum16_sub(u16 csum, u16 addend) { + return csum16_add(csum, ~addend); +} diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09.json b/testdata/p4tc_samples_outputs/tc_may_override_example_09.json new file mode 100644 index 00000000000..4bbe6d7c75b --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09.json @@ -0,0 +1,125 @@ +{ + "schema_version" : "1.0.0", + "pipeline_name" : "tc_may_override_example_09", + "externs" : [], + "tables" : [ + { + "name" : "MainControlImpl/ipv4_tbl_1", + "id" : 1, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 64, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "istd.input_port", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : false, + "default_miss_action" : true + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + } + ] + }, + { + "name" : "MainControlImpl/ipv4_tbl_2", + "id" : 2, + "tentries" : 2048, + "permissions" : "0x3da4", + "nummask" : 8, + "keysize" : 72, + "keyfields" : [ + { + "id" : 1, + "name" : "hdr.ipv4.dstAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 2, + "name" : "hdr.ipv4.srcAddr", + "type" : "bit32", + "match_type" : "exact", + "bitwidth" : 32 + }, + { + "id" : 3, + "name" : "hdr.ipv4.protocol", + "type" : "bit8", + "match_type" : "exact", + "bitwidth" : 8 + } + ], + "actions" : [ + { + "id" : 1, + "name" : "MainControlImpl/next_hop", + "action_scope" : "TableAndDefault", + "annotations" : ["tc_may_override"], + "params" : [ + { + "id" : 1, + "name" : "vport", + "type" : "bit32", + "bitwidth" : 32 + } + ], + "default_hit_action" : false, + "default_miss_action" : false + }, + { + "id" : 2, + "name" : "MainControlImpl/dflt_route_drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : false + }, + { + "id" : 3, + "name" : "MainControlImpl/drop", + "action_scope" : "TableAndDefault", + "annotations" : [], + "params" : [], + "default_hit_action" : false, + "default_miss_action" : true + } + ] + } + ] +} \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09.p4-stderr b/testdata/p4tc_samples_outputs/tc_may_override_example_09.p4-stderr new file mode 100644 index 00000000000..f6e8a35e993 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09.p4-stderr @@ -0,0 +1 @@ +[--Wwarn=invalid] warning: Table 'ipv4_tbl_2' has an action reference 'next_hop' which is annotated with '@tc_may_override' without '@default_hit' or '@default_hit_const' diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09.template b/testdata/p4tc_samples_outputs/tc_may_override_example_09.template new file mode 100755 index 00000000000..1c1509d3820 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09.template @@ -0,0 +1,34 @@ +#!/bin/bash -x + +set -e + +TC="tc" +$TC p4template create pipeline/tc_may_override_example_09 numtables 2 + +$TC p4template create action/tc_may_override_example_09/MainControlImpl/next_hop actid 1 \ + param vport type bit32 +$TC p4template update action/tc_may_override_example_09/MainControlImpl/next_hop state active + +$TC p4template create action/tc_may_override_example_09/MainControlImpl/dflt_route_drop actid 2 +$TC p4template update action/tc_may_override_example_09/MainControlImpl/dflt_route_drop state active + +$TC p4template create action/tc_may_override_example_09/MainControlImpl/drop actid 3 +$TC p4template update action/tc_may_override_example_09/MainControlImpl/drop state active + +$TC p4template create table/tc_may_override_example_09/MainControlImpl/ipv4_tbl_1 \ + tblid 1 \ + type exact \ + keysz 64 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_09/MainControlImpl/next_hop \ + act name tc_may_override_example_09/MainControlImpl/dflt_route_drop +$TC p4template update table/tc_may_override_example_09/MainControlImpl/ipv4_tbl_1 default_miss_action action tc_may_override_example_09/MainControlImpl/next_hop param vport flags runtime + +$TC p4template create table/tc_may_override_example_09/MainControlImpl/ipv4_tbl_2 \ + tblid 2 \ + type exact \ + keysz 72 nummasks 8 permissions 0x3da4 tentries 2048 \ + table_acts act name tc_may_override_example_09/MainControlImpl/next_hop \ + act name tc_may_override_example_09/MainControlImpl/dflt_route_drop \ + act name tc_may_override_example_09/MainControlImpl/drop +$TC p4template update table/tc_may_override_example_09/MainControlImpl/ipv4_tbl_2 default_miss_action action tc_may_override_example_09/MainControlImpl/drop +$TC p4template update pipeline/tc_may_override_example_09 state ready \ No newline at end of file diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09_control_blocks.c b/testdata/p4tc_samples_outputs/tc_may_override_example_09_control_blocks.c new file mode 100644 index 00000000000..6ae03156f5c --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09_control_blocks.c @@ -0,0 +1,387 @@ +#include "tc_may_override_example_09_parser.h" +struct p4tc_filter_fields p4tc_filter_fields; + +struct internal_metadata { + __u16 pkt_ether_type; +} __attribute__((aligned(4))); + +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* istd.input_port */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#define MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION 0 +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_1_value { + unsigned int action; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + } u; +}; +struct __attribute__((__packed__)) MainControlImpl_ipv4_tbl_2_key { + u32 keysz; + u32 maskid; + u32 field0; /* hdr.ipv4.dstAddr */ + u32 field1; /* hdr.ipv4.srcAddr */ + u8 field2; /* hdr.ipv4.protocol */ +} __attribute__((aligned(8))); +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP 1 +#define MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP 2 +#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; + u32 hit:1, + is_default_miss_act:1, + is_default_hit_act:1; + union { + struct { + } _NoAction; + struct __attribute__((__packed__)) { + u32 vport; + } MainControlImpl_next_hop; + struct { + } MainControlImpl_dflt_route_drop; + struct { + } MainControlImpl_drop; + } u; +}; + +static __always_inline int process(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + unsigned ebpf_packetOffsetInBits = hdrMd->ebpf_packetOffsetInBits; + hdr_start = pkt + BYTES(ebpf_packetOffsetInBits); + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); +{ + u8 hit; + { +if (/* hdr->ipv4.isValid() */ + hdr->ipv4.ebpf_valid) { +/* ipv4_tbl.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 1 + }; + struct MainControlImpl_ipv4_tbl_1_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 64; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = skb->ifindex; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_1_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_1_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + 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); + } + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_MAINCONTROLIMPL_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_1_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + /* ipv4_tbl_0.apply() */ + { + /* construct key */ + struct p4tc_table_entry_act_bpf_params__local params = { + .pipeid = p4tc_filter_fields.pipeid, + .tblid = 2 + }; + struct MainControlImpl_ipv4_tbl_2_key key; + __builtin_memset(&key, 0, sizeof(key)); + key.keysz = 72; + key.field0 = hdr->ipv4.dstAddr; + key.field1 = hdr->ipv4.srcAddr; + key.field2 = hdr->ipv4.protocol; + struct p4tc_table_entry_act_bpf *act_bpf; + /* value */ + struct MainControlImpl_ipv4_tbl_2_value *value = NULL; + /* perform lookup */ + act_bpf = bpf_p4tc_tbl_read(skb, ¶ms, sizeof(params), &key, sizeof(key)); + value = (struct MainControlImpl_ipv4_tbl_2_value *)act_bpf; + if (value == NULL) { + /* miss; find default action */ + hit = 0; + } else { + hit = value->hit; + } + if (value != NULL) { + /* run action */ + switch (value->action) { + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_NEXT_HOP: + { +/* 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_DFLT_ROUTE_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_MAINCONTROLIMPL_DROP: + { +/* drop_packet() */ + drop_packet(); + } + break; + case MAINCONTROLIMPL_IPV4_TBL_2_ACT_NOACTION: + { + } + break; + } + } else { + } + } +; + } + } + } + { +{ +; + ; + } + + if (compiler_meta__->drop) { + return TC_ACT_SHOT; + } + int outHeaderLength = 0; + if (hdr->ethernet.ebpf_valid) { + outHeaderLength += 112; + } +; if (hdr->ipv4.ebpf_valid) { + outHeaderLength += 160; + } +; + int outHeaderOffset = BYTES(outHeaderLength) - (hdr_start - (u8*)pkt); + if (outHeaderOffset != 0) { + int returnCode = 0; + returnCode = bpf_skb_adjust_room(skb, outHeaderOffset, 1, 0); + if (returnCode) { + return TC_ACT_SHOT; + } + } + pkt = ((void*)(long)skb->data); + ebpf_packetEnd = ((void*)(long)skb->data_end); + ebpf_packetOffsetInBits = 0; + if (hdr->ethernet.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 112)) { + return TC_ACT_SHOT; + } + + hdr->ethernet.dstAddr = htonll(hdr->ethernet.dstAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.dstAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = htonll(hdr->ethernet.srcAddr << 16); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[4]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 4, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.srcAddr))[5]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 5, (ebpf_byte)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = bpf_htons(hdr->ethernet.etherType); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ethernet.etherType))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + } +; if (hdr->ipv4.ebpf_valid) { + if (ebpf_packetEnd < pkt + BYTES(ebpf_packetOffsetInBits + 160)) { + return TC_ACT_SHOT; + } + + ebpf_byte = ((char*)(&hdr->ipv4.version))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 4, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.ihl))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 4, 0, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 4; + + ebpf_byte = ((char*)(&hdr->ipv4.diffserv))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = bpf_htons(hdr->ipv4.totalLen); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.totalLen))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = bpf_htons(hdr->ipv4.identification); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.identification))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.flags))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 3, 5, (ebpf_byte >> 0)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = bpf_htons(hdr->ipv4.fragOffset << 3); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[0]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0, 5, 0, (ebpf_byte >> 3)); + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 0 + 1, 3, 5, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.fragOffset))[1]; + write_partial(pkt + BYTES(ebpf_packetOffsetInBits) + 1, 5, 0, (ebpf_byte >> 3)); + ebpf_packetOffsetInBits += 13; + + ebpf_byte = ((char*)(&hdr->ipv4.ttl))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + ebpf_byte = ((char*)(&hdr->ipv4.protocol))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = bpf_htons(hdr->ipv4.hdrChecksum); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.hdrChecksum))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_packetOffsetInBits += 16; + + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.srcAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[0]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 0, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[1]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 1, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[2]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 2, (ebpf_byte)); + ebpf_byte = ((char*)(&hdr->ipv4.dstAddr))[3]; + write_byte(pkt, BYTES(ebpf_packetOffsetInBits) + 3, (ebpf_byte)); + ebpf_packetOffsetInBits += 32; + + } +; + } + return -1; +} +SEC("p4tc/main") +int tc_ingress_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + if (compiler_meta__->pass_to_kernel == true) return TC_ACT_OK; + if (!compiler_meta__->recirculated) { + compiler_meta__->mark = 153; + struct internal_metadata *md = (struct internal_metadata *)(unsigned long)skb->data_meta; + if ((void *) ((struct internal_metadata *) md + 1) <= (void *)(long)skb->data) { + __u16 *ether_type = (__u16 *) ((void *) (long)skb->data + 12); + if ((void *) ((__u16 *) ether_type + 1) > (void *) (long) skb->data_end) { + return TC_ACT_SHOT; + } + *ether_type = md->pkt_ether_type; + } + } + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = process(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + if (!compiler_meta__->drop && compiler_meta__->egress_port == 0) { + compiler_meta__->pass_to_kernel = true; + return bpf_redirect(skb->ifindex, BPF_F_INGRESS); + } + return bpf_redirect(compiler_meta__->egress_port, 0); +} +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.c b/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.c new file mode 100644 index 00000000000..6ee7f4bae87 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.c @@ -0,0 +1,136 @@ +#include "tc_may_override_example_09_parser.h" + +struct p4tc_filter_fields p4tc_filter_fields; + +static __always_inline int run_parser(struct __sk_buff *skb, struct headers_t *hdr, struct pna_global_metadata *compiler_meta__) +{ + struct hdr_md *hdrMd; + + unsigned ebpf_packetOffsetInBits_save = 0; + ParserError_t ebpf_errorCode = NoError; + void* pkt = ((void*)(long)skb->data); + u8* hdr_start = pkt; + void* ebpf_packetEnd = ((void*)(long)skb->data_end); + u32 ebpf_zero = 0; + u32 ebpf_one = 1; + unsigned char ebpf_byte; + u32 pkt_len = skb->len; + + struct main_metadata_t *user_meta; + + hdrMd = BPF_MAP_LOOKUP_ELEM(hdr_md_cpumap, &ebpf_zero); + if (!hdrMd) + return TC_ACT_SHOT; + __builtin_memset(hdrMd, 0, sizeof(struct hdr_md)); + + unsigned ebpf_packetOffsetInBits = 0; + hdr = &(hdrMd->cpumap_hdr); + user_meta = &(hdrMd->cpumap_usermeta); + { + goto start; + parse_ipv4: { +/* extract(hdr->ipv4) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(160 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ipv4.version = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 4) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.ihl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u8, 4)); + ebpf_packetOffsetInBits += 4; + + hdr->ipv4.diffserv = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.totalLen = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.identification = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + hdr->ipv4.flags = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)) >> 5) & EBPF_MASK(u8, 3)); + ebpf_packetOffsetInBits += 3; + + hdr->ipv4.fragOffset = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits))) & EBPF_MASK(u16, 13)); + ebpf_packetOffsetInBits += 13; + + hdr->ipv4.ttl = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.protocol = (u8)((load_byte(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 8; + + hdr->ipv4.hdrChecksum = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + __builtin_memcpy(&hdr->ipv4.srcAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + __builtin_memcpy(&hdr->ipv4.dstAddr, pkt + BYTES(ebpf_packetOffsetInBits), 4); + ebpf_packetOffsetInBits += 32; + + + hdr->ipv4.ebpf_valid = 1; + hdr_start += BYTES(160); + +; + goto accept; + } + start: { +/* extract(hdr->ethernet) */ + if ((u8*)ebpf_packetEnd < hdr_start + BYTES(112 + 0)) { + ebpf_errorCode = PacketTooShort; + goto reject; + } + + hdr->ethernet.dstAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.srcAddr = (u64)((load_dword(pkt, BYTES(ebpf_packetOffsetInBits)) >> 16) & EBPF_MASK(u64, 48)); + ebpf_packetOffsetInBits += 48; + + hdr->ethernet.etherType = (u16)((load_half(pkt, BYTES(ebpf_packetOffsetInBits)))); + ebpf_packetOffsetInBits += 16; + + + hdr->ethernet.ebpf_valid = 1; + hdr_start += BYTES(112); + +; + u16 select_0; + select_0 = hdr->ethernet.etherType; + if (select_0 == 0x800)goto parse_ipv4; + if ((select_0 & 0x0) == (0x0 & 0x0))goto accept; + else goto reject; + } + + reject: { + if (ebpf_errorCode == 0) { + return TC_ACT_SHOT; + } + compiler_meta__->parser_error = ebpf_errorCode; + goto accept; + } + + } + + accept: + hdrMd->ebpf_packetOffsetInBits = ebpf_packetOffsetInBits; + return -1; +} + +SEC("p4tc/parse") +int tc_parse_func(struct __sk_buff *skb) { + struct pna_global_metadata *compiler_meta__ = (struct pna_global_metadata *) skb->cb; + struct hdr_md *hdrMd; + struct headers_t *hdr; + int ret = -1; + ret = run_parser(skb, (struct headers_t *) hdr, compiler_meta__); + if (ret != -1) { + return ret; + } + return TC_ACT_PIPE; + } +char _license[] SEC("license") = "GPL"; diff --git a/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.h b/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.h new file mode 100644 index 00000000000..780f9fb5632 --- /dev/null +++ b/testdata/p4tc_samples_outputs/tc_may_override_example_09_parser.h @@ -0,0 +1,165 @@ +#include "ebpf_kernel.h" + +#include +#include +#include +#include "pna.h" + +#define EBPF_MASK(t, w) ((((t)(1)) << (w)) - (t)1) +#define BYTES(w) ((w) / 8) +#define write_partial(a, w, s, v) do { *((u8*)a) = ((*((u8*)a)) & ~(EBPF_MASK(u8, w) << s)) | (v << s) ; } while (0) +#define write_byte(base, offset, v) do { *(u8*)((base) + (offset)) = (v); } while (0) +#define bpf_trace_message(fmt, ...) + + +struct ethernet_t { + u64 dstAddr; /* EthernetAddress */ + u64 srcAddr; /* EthernetAddress */ + u16 etherType; /* bit<16> */ + u8 ebpf_valid; +}; +struct ipv4_t { + u8 version; /* bit<4> */ + u8 ihl; /* bit<4> */ + u8 diffserv; /* bit<8> */ + u16 totalLen; /* bit<16> */ + u16 identification; /* bit<16> */ + u8 flags; /* bit<3> */ + u16 fragOffset; /* bit<13> */ + u8 ttl; /* bit<8> */ + u8 protocol; /* bit<8> */ + u16 hdrChecksum; /* bit<16> */ + u32 srcAddr; /* bit<32> */ + u32 dstAddr; /* bit<32> */ + u8 ebpf_valid; +}; +struct main_metadata_t { +}; +struct headers_t { + struct ethernet_t ethernet; /* ethernet_t */ + struct ipv4_t ipv4; /* ipv4_t */ +}; + +struct hdr_md { + struct headers_t cpumap_hdr; + struct main_metadata_t cpumap_usermeta; + unsigned ebpf_packetOffsetInBits; + __u8 __hook; +}; + +struct p4tc_filter_fields { + __u32 pipeid; + __u32 handle; + __u32 classid; + __u32 chain; + __u32 blockid; + __be16 proto; + __u16 prio; + __u32 ipv4_tbl_1_next_hop_vport; +}; + +REGISTER_START() +REGISTER_TABLE(hdr_md_cpumap, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct hdr_md, 2) +BPF_ANNOTATE_KV_PAIR(hdr_md_cpumap, u32, struct hdr_md) +REGISTER_END() + +static __always_inline +void crc16_update(u16 * reg, const u8 * data, u16 data_size, const u16 poly) { + if (data_size <= 8) + data += data_size - 1; + #pragma clang loop unroll(full) + for (u16 i = 0; i < data_size; i++) { + bpf_trace_message("CRC16: data byte: %x\n", *data); + *reg ^= *data; + for (u8 bit = 0; bit < 8; bit++) { + *reg = (*reg) & 1 ? ((*reg) >> 1) ^ poly : (*reg) >> 1; + } + if (data_size <= 8) + data--; + else + data++; + } +} +static __always_inline u16 crc16_finalize(u16 reg) { + return reg; +} +static __always_inline +void crc32_update(u32 * reg, const u8 * data, u16 data_size, const u32 poly) { + u32* current = (u32*) data; + u32 index = 0; + u32 lookup_key = 0; + u32 lookup_value = 0; + u32 lookup_value1 = 0; + u32 lookup_value2 = 0; + u32 lookup_value3 = 0; + u32 lookup_value4 = 0; + u32 lookup_value5 = 0; + u32 lookup_value6 = 0; + u32 lookup_value7 = 0; + u32 lookup_value8 = 0; + u16 tmp = 0; + if (crc32_table != NULL) { + for (u16 i = data_size; i >= 8; i -= 8) { + /* Vars one and two will have swapped byte order if data_size == 8 */ + if (data_size == 8) current = (u32 *)(data + 4); + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 one = (data_size == 8 ? __builtin_bswap32(*current--) : *current++) ^ *reg; + bpf_trace_message("CRC32: data dword: %x\n", *current); + u32 two = (data_size == 8 ? __builtin_bswap32(*current--) : *current++); + lookup_key = (one & 0x000000FF); + lookup_value8 = crc32_table[(u16)(1792 + (u8)lookup_key)]; + lookup_key = (one >> 8) & 0x000000FF; + lookup_value7 = crc32_table[(u16)(1536 + (u8)lookup_key)]; + lookup_key = (one >> 16) & 0x000000FF; + lookup_value6 = crc32_table[(u16)(1280 + (u8)lookup_key)]; + lookup_key = one >> 24; + lookup_value5 = crc32_table[(u16)(1024 + (u8)(lookup_key))]; + lookup_key = (two & 0x000000FF); + lookup_value4 = crc32_table[(u16)(768 + (u8)lookup_key)]; + lookup_key = (two >> 8) & 0x000000FF; + lookup_value3 = crc32_table[(u16)(512 + (u8)lookup_key)]; + lookup_key = (two >> 16) & 0x000000FF; + lookup_value2 = crc32_table[(u16)(256 + (u8)lookup_key)]; + lookup_key = two >> 24; + lookup_value1 = crc32_table[(u8)(lookup_key)]; + *reg = lookup_value8 ^ lookup_value7 ^ lookup_value6 ^ lookup_value5 ^ + lookup_value4 ^ lookup_value3 ^ lookup_value2 ^ lookup_value1; + tmp += 8; + } + volatile int std_algo_lookup_key = 0; + if (data_size < 8) { + unsigned char *currentChar = (unsigned char *) current; + currentChar += data_size - 1; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar--); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } else { + /* Consume data not processed by slice-by-8 algorithm above, these data are in network byte order */ + unsigned char *currentChar = (unsigned char *) current; + for (u16 i = tmp; i < data_size; i++) { + bpf_trace_message("CRC32: data byte: %x\n", *currentChar); + std_algo_lookup_key = (u32)(((*reg) & 0xFF) ^ *currentChar++); + if (std_algo_lookup_key >= 0) { + lookup_value = crc32_table[(u8)(std_algo_lookup_key & 255)]; + } + *reg = ((*reg) >> 8) ^ lookup_value; + } + } + } +} +static __always_inline u32 crc32_finalize(u32 reg) { + return reg ^ 0xFFFFFFFF; +} +inline u16 csum16_add(u16 csum, u16 addend) { + u16 res = csum; + res += addend; + return (res + (res < addend)); +} +inline u16 csum16_sub(u16 csum, u16 addend) { + return csum16_add(csum, ~addend); +}