Skip to content

Commit

Permalink
Add simple_l3_varbits example
Browse files Browse the repository at this point in the history
Add example that's based on the simple_l3 example
(https://github.com/p4tc-dev/p4tc-examples-pub/blob/main/simple/simple_l3/simple_l3.p4),
and parser IPv4 options
  • Loading branch information
mouse-mt authored and vbnogueira committed Jun 4, 2024
1 parent d28fb85 commit 3e4fd20
Show file tree
Hide file tree
Showing 6 changed files with 1,065 additions and 0 deletions.
159 changes: 159 additions & 0 deletions testdata/p4tc_samples/simple_l3_varbits.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* -*- 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 {
bit<48> dstAddr;
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;
bit<32> srcAddr;
bit<32> dstAddr;
}

header v4_options_t {
varbit<320> opts;
}

struct my_ingress_headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
v4_options_t opts;
}

/*********************** 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.ihl) {
4w0: reject;
4w1: reject;
4w2: reject;
4w3: reject;
4w4: reject;
5: parse_proto;
default: parse_v4_opts;
}
}
state parse_v4_opts {
// XXX The cast to bit<32> is because pkt.extract demands
// bit<32> for its second arg
pkt.extract(hdr.opts,(bit<32>)(32*(bit<9>)(hdr.ipv4.ihl-5)));
transition parse_proto;
}
state parse_proto {
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;
hdr.ipv4.ttl = 31;
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);
pkt.emit(hdr.opts);
}
}

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

PNA_NIC(
Ingress_Parser(),
ingress(),
Ingress_Deparser()
) main;
63 changes: 63 additions & 0 deletions testdata/p4tc_samples_outputs/simple_l3_varbits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"schema_version" : "1.0.0",
"pipeline_name" : "simple_l3_varbits",
"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_outputs/simple_l3_varbits.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_varbits numtables 1

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

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

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

0 comments on commit 3e4fd20

Please sign in to comment.