Skip to content

Commit

Permalink
Add simple_l3 example
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Nogueira <victor@mojatatu.com>
  • Loading branch information
vbnogueira committed Dec 10, 2024
1 parent 5e7f9cf commit 8b06b32
Show file tree
Hide file tree
Showing 8 changed files with 728 additions and 0 deletions.
1 change: 1 addition & 0 deletions backends/tc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,4 @@ endmacro(p4tc_add_test_with_args)

p4c_add_tests("p4tc" ${P4TC_COMPILER_DRIVER} "${P4_16_SUITES}" "")
p4tc_add_test_with_args("p4tc" ${P4TC_COMPILER_DRIVER} FALSE "testdata/p4tc_samples_stf/arp_respond.p4" "testdata/p4tc_samples_stf/arp_respond.p4" "-tf ${P4C_SOURCE_DIR}/testdata/p4tc_samples_stf/arp_respond.stf" "")
p4tc_add_test_with_args("p4tc" ${P4TC_COMPILER_DRIVER} FALSE "testdata/p4tc_samples_stf/simple_l3.p4" "testdata/p4tc_samples_stf/simple_l3.p4" "-tf ${P4C_SOURCE_DIR}/testdata/p4tc_samples_stf/simple_l3.stf" "")
135 changes: 135 additions & 0 deletions testdata/p4tc_samples_stf/simple_l3.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* -*- P4_16 -*- */

#include <core.p4>
#include <tc/pna.p4>

struct my_ingress_metadata_t {
}

struct empty_metadata_t {
}

/*
* CONST VALUES FOR TYPES
*/
const bit<8> IP_PROTO_TCP = 0x06;
const bit<16> ETHERTYPE_IPV4 = 0x0800;

/*
* Standard ethernet header
*/
header ethernet_t {
@tc_type("macaddr") bit<48> dstAddr;
@tc_type("macaddr") bit<48> 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 my_ingress_headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
}

/*********************** P A R S E R **************************/
parser Ingress_Parser(
packet_in pkt,
out my_ingress_headers_t hdr,
inout my_ingress_metadata_t meta,
in pna_main_parser_input_metadata_t istd)
{

state start {
transition parse_ethernet;
}

state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
ETHERTYPE_IPV4: parse_ipv4;
default: reject;
}
}
state parse_ipv4 {
pkt.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
IP_PROTO_TCP : accept;
default: reject;
}
}
}

#define L3_TABLE_SIZE 2048

/***************** M A T C H - A C T I O N *********************/

control ingress(
inout my_ingress_headers_t hdr,
inout my_ingress_metadata_t meta,
in pna_main_input_metadata_t istd,
inout pna_main_output_metadata_t ostd
)
{
action send_nh(@tc_type("dev") PortId_t port, @tc_type("macaddr") bit<48> srcMac, @tc_type("macaddr") bit<48> dstMac) {
hdr.ethernet.srcAddr = srcMac;
hdr.ethernet.dstAddr = dstMac;
send_to_port(port);
}

action drop() {
drop_packet();
}

table nh_table {
key = {
hdr.ipv4.dstAddr : exact @tc_type("ipv4") @name("dstAddr");
}
actions = {
send_nh;
drop;
}
size = L3_TABLE_SIZE;
const default_action = drop;
}

apply {
if (hdr.ipv4.isValid() && hdr.ipv4.protocol == IP_PROTO_TCP) {
nh_table.apply();
}
}
}

/********************* D E P A R S E R ************************/

control Ingress_Deparser(
packet_out pkt,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
apply {
pkt.emit(hdr.ethernet);
pkt.emit(hdr.ipv4);
}
}

/************ F I N A L P A C K A G E ******************************/

PNA_NIC(
Ingress_Parser(),
ingress(),
Ingress_Deparser()
) main;
5 changes: 5 additions & 0 deletions testdata/p4tc_samples_stf/simple_l3.stf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add ingress.nh_table dstAddr:0x0a000002 ingress.send_nh(port:0x1, srcMac:0x000102030405, dstMac:0xAABBCCDDEEFF)

packet 0 10000002 aabb1000 0001aabb 08004500 0028613d 00004006 05910a00 00010a0 0000209f e0000364 95b780fb 14bc6500 00200a2a b0000

expect 1 aabbccdd eeff0001 02030405 08004500 0028613d 00004006 05910a00 00010a0 0000209f e0000364 95b780fb 14bc6500 00200a2a b0000
63 changes: 63 additions & 0 deletions testdata/p4tc_samples_stf_outputs/simple_l3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"schema_version" : "1.0.0",
"pipeline_name" : "simple_l3",
"externs" : [],
"tables" : [
{
"name" : "ingress/nh_table",
"id" : 1,
"tentries" : 2048,
"permissions" : "0x3da4",
"nummask" : 8,
"keysize" : 32,
"keyfields" : [
{
"id" : 1,
"name" : "dstAddr",
"type" : "ipv4",
"match_type" : "exact",
"bitwidth" : 32
}
],
"actions" : [
{
"id" : 1,
"name" : "ingress/send_nh",
"action_scope" : "TableAndDefault",
"annotations" : [],
"params" : [
{
"id" : 1,
"name" : "port",
"type" : "dev",
"bitwidth" : 32
},
{
"id" : 2,
"name" : "srcMac",
"type" : "macaddr",
"bitwidth" : 48
},
{
"id" : 3,
"name" : "dstMac",
"type" : "macaddr",
"bitwidth" : 48
}
],
"default_hit_action" : false,
"default_miss_action" : false
},
{
"id" : 2,
"name" : "ingress/drop",
"action_scope" : "TableAndDefault",
"annotations" : [],
"params" : [],
"default_hit_action" : false,
"default_miss_action" : true
}
]
}
]
}
24 changes: 24 additions & 0 deletions testdata/p4tc_samples_stf_outputs/simple_l3.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash -x

set -e

: "${TC:="tc"}"
$TC p4template create pipeline/simple_l3 numtables 1

$TC p4template create action/simple_l3/ingress/send_nh actid 1 \
param port type dev \
param srcMac type macaddr \
param dstMac type macaddr
$TC p4template update action/simple_l3/ingress/send_nh state active

$TC p4template create action/simple_l3/ingress/drop actid 2
$TC p4template update action/simple_l3/ingress/drop state active

$TC p4template create table/simple_l3/ingress/nh_table \
tblid 1 \
type exact \
keysz 32 permissions 0x3da4 tentries 2048 nummasks 1 \
table_acts act name simple_l3/ingress/send_nh \
act name simple_l3/ingress/drop
$TC p4template update table/simple_l3/ingress/nh_table default_miss_action permissions 0x1024 action simple_l3/ingress/drop
$TC p4template update pipeline/simple_l3 state ready
Loading

0 comments on commit 8b06b32

Please sign in to comment.