Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P4TC - Support add_on_miss, add entry externs #4522

Merged
merged 15 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implemented add_entry extern
  • Loading branch information
komaljai committed Dec 27, 2023
commit c1c144863e81ed032723d0ef8530cf15e3487c65
72 changes: 71 additions & 1 deletion backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,8 +1315,78 @@ void ControlBodyTranslatorPNA::processFunction(const P4::ExternFunction *functio
builder->emitIndent();
builder->appendLine("};");
builder->emitIndent();
builder->append(
"bpf_p4tc_entry_update(skb, &update_params, &key, sizeof(key), act_bpf)");
}
return;
} else if (function->expr->toString() == "add_entry") {
auto act = findContext<IR::P4Action>();
BUG_CHECK(act != nullptr, "%1% add_entry extern can only be used in an action", function);
BUG_CHECK(function->expr->arguments->size() == 3,
"%1%: expected 3 arguments in add_entry extern", function);

auto action = function->expr->arguments->at(0);
auto actionName = action->expression->to<IR::StringLiteral>()->value;
auto param = function->expr->arguments->at(1)->expression;
auto expire_time_profile_id = function->expr->arguments->at(2);
auto controlName = control->controlBlock->getName().originalName;

if (table) {
auto actID = 0;
for (auto a : table->actionList->actionList) {
auto adecl = control->program->refMap->getDeclaration(a->getPath(), true);
auto action = adecl->getNode()->to<IR::P4Action>();
if (a->toString() == actionName) {
komaljai marked this conversation as resolved.
Show resolved Hide resolved
cstring name = tcIR->externalName(action);
actID = tcIR->getActionId(name);
BUG_CHECK(actID != 0, "Action ID not found");
break;
}
}

builder->emitIndent();
builder->appendLine("struct p4tc_table_entry_act_bpf update_act_bpf = {");
builder->emitIndent();
builder->appendFormat(" .act_id = %d,", actID);
builder->newline();
builder->emitIndent();
builder->append(" .params = ");
if (auto struct_params = param->to<IR::StructExpression>()) {
builder->append("{");
auto components = struct_params->components;
for (size_t index = 0; index < components.size(); index++) {
visit(components.at(index)->expression);
if (index < components.size() - 1) builder->append(", ");
}
builder->append("}");
}
builder->newline();
builder->emitIndent();
builder->appendLine("};");

builder->emitIndent();
builder->appendLine("/* construct key */");
builder->emitIndent();
builder->appendLine(
"bpf_p4tc_entry_update(skb, &update_params, &key, sizeof(key), act_bpf);");
"struct p4tc_table_entry_create_bpf_params__local update_params = {");
builder->emitIndent();
builder->appendLine(" .pipeid = 1,");
builder->emitIndent();
auto tableName = table->instanceName.substr(controlName.size() + 1);
auto tblId = tcIR->getTableId(tableName);
BUG_CHECK(tblId != 0, "Table ID not found");
builder->appendFormat(" .tblid = %d,", tblId);
builder->newline();
builder->emitIndent();
builder->append(" .aging_ms = ");
visit(expire_time_profile_id);
builder->newline();
builder->emitIndent();
builder->appendLine("};");
builder->emitIndent();
builder->append(
"bpf_p4tc_entry_create_on_miss(skb, &update_params, &key, sizeof(key), "
"&update_act_bpf)");
}
return;
}
Expand Down
138 changes: 138 additions & 0 deletions testdata/p4tc_samples/add_entry_example.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <core.p4>
#include <tc/pna.p4>

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;
}

const ExpireTimeProfileId_t EXPIRE_TIME_PROFILE_NOW = (ExpireTimeProfileId_t) 2;

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() {
add_entry(action_name = "default_route_drop", // name of action
action_params = {}, expire_time_profile_id = EXPIRE_TIME_PROFILE_NOW);

}
action default_route_drop() {
drop_packet();
}
action drop() {
drop_packet();
}

table ipv4_tbl_1 {
key = {
hdr.ipv4.dstAddr : exact;
istd.input_port : exact;
}
actions = {
next_hop;
default_route_drop;
}
default_action = default_route_drop;
}
table ipv4_tbl_2 {
key = {
hdr.ipv4.dstAddr : exact;
hdr.ipv4.srcAddr : exact;
komaljai marked this conversation as resolved.
Show resolved Hide resolved
hdr.ipv4.protocol : exact;
}
actions = {
next_hop;
drop;
}
default_action = drop;
}

apply {
if (hdr.ipv4.isValid()) {
ipv4_tbl_1.apply();
ipv4_tbl_2.apply();
}
}
}

control MainDeparserImpl(
packet_out pkt,
in 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
100 changes: 100 additions & 0 deletions testdata/p4tc_samples_outputs/add_entry_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"schema_version" : "1.0.0",
"pipeline_name" : "add_entry_example",
"id" : 1,
"tables" : [
{
"name" : "MainControlImpl/ipv4_tbl_1",
"id" : 1,
"tentries" : 2048,
"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" : [],
"default_hit_action" : false,
"default_miss_action" : false
},
{
"id" : 2,
"name" : "MainControlImpl/default_route_drop",
"action_scope" : "TableAndDefault",
"annotations" : [],
"params" : [],
"default_hit_action" : false,
"default_miss_action" : true
}
]
},
{
"name" : "MainControlImpl/ipv4_tbl_2",
"id" : 2,
"tentries" : 2048,
"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" : [],
"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
}
]
}
]
}
Empty file.
32 changes: 32 additions & 0 deletions testdata/p4tc_samples_outputs/add_entry_example.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash -x

set -e

TC="tc"
$TC p4template create pipeline/add_entry_example pipeid 1 numtables 2

$TC p4template create action/add_entry_example/MainControlImpl/next_hop actid 1
$TC p4template update action/add_entry_example/MainControlImpl/next_hop state active

$TC p4template create action/add_entry_example/MainControlImpl/default_route_drop actid 2
$TC p4template update action/add_entry_example/MainControlImpl/default_route_drop state active

$TC p4template create action/add_entry_example/MainControlImpl/drop actid 3
$TC p4template update action/add_entry_example/MainControlImpl/drop state active

$TC p4template create table/add_entry_example/MainControlImpl/ipv4_tbl_1 \
tblid 1 \
type exact \
keysz 64 nummasks 8 tentries 2048 \
table_acts act name add_entry_example/MainControlImpl/next_hop \
act name add_entry_example/MainControlImpl/default_route_drop
$TC p4template update table/add_entry_example/MainControlImpl/ipv4_tbl_1 default_miss_action action add_entry_example/MainControlImpl/default_route_drop

$TC p4template create table/add_entry_example/MainControlImpl/ipv4_tbl_2 \
tblid 2 \
type exact \
keysz 72 nummasks 8 tentries 2048 \
table_acts act name add_entry_example/MainControlImpl/next_hop \
act name add_entry_example/MainControlImpl/drop
$TC p4template update table/add_entry_example/MainControlImpl/ipv4_tbl_2 default_miss_action action add_entry_example/MainControlImpl/drop
$TC p4template update pipeline/add_entry_example state ready
Loading