Skip to content

Commit

Permalink
Add example to illustrate accessing parser metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Nogueira <[email protected]>
  • Loading branch information
vbnogueira authored and komaljai committed Oct 17, 2024
1 parent b17c9f0 commit f705d86
Show file tree
Hide file tree
Showing 7 changed files with 877 additions and 0 deletions.
144 changes: 144 additions & 0 deletions testdata/p4tc_samples/digest_parser_meta.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* -*- P4_16 -*- */

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

struct my_ingress_metadata_t {
@tc_type("dev") PortId_t ingress_port;
}

/*
* 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);
meta.ingress_port = istd.input_port;
transition accept;
}
}

#define L3_TABLE_SIZE 2048

struct ipv4_learn_digest_t {
@tc_type("ipv4") bit<32> srcAddr;
@tc_type("dev") PortId_t ingress_port;
};

/***************** 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()) {
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)
{
Digest<ipv4_learn_digest_t>() digest_inst;

apply {
ipv4_learn_digest_t ipv4_learn_digest;

pkt.emit(hdr.ethernet);
pkt.emit(hdr.ipv4);

ipv4_learn_digest.srcAddr = hdr.ipv4.srcAddr;
ipv4_learn_digest.ingress_port = meta.ingress_port;
digest_inst.pack(ipv4_learn_digest);
}
}

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

PNA_NIC(
Ingress_Parser(),
ingress(),
Ingress_Deparser()
) main;
98 changes: 98 additions & 0 deletions testdata/p4tc_samples_outputs/digest_parser_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"schema_version" : "1.0.0",
"pipeline_name" : "digest_parser_meta",
"externs" : [
{
"name" : "Digest",
"id" : "0x05000000",
"permissions" : "0x19b6",
"instances" : [
{
"inst_name" : "Ingress_Deparser.digest_inst",
"inst_id" : 1,
"params" : [
{
"id" : 1,
"name" : "index",
"type" : "bit32",
"attr" : "tc_key",
"bitwidth" : 32
},
{
"id" : 2,
"name" : "srcAddr",
"type" : "ipv4",
"attr" : "param",
"bitwidth" : 32
},
{
"id" : 3,
"name" : "ingress_port",
"type" : "dev",
"attr" : "param",
"bitwidth" : 32
}
]
}
]
}
],
"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
}
]
}
]
}
Empty file.
30 changes: 30 additions & 0 deletions testdata/p4tc_samples_outputs/digest_parser_meta.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash -x

set -e

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

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

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

$TC p4template create extern/root/Digest extid 0x05000000 numinstances 1 tc_acl 0x19b6 has_exec_method

$TC p4template create extern_inst/digest_parser_meta/Digest/Ingress_Deparser.digest_inst instid 1 \
tc_numel 0 \
control_path tc_key index ptype bit32 id 1 param srcAddr ptype ipv4 id 2 param ingress_port ptype dev id 3

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

0 comments on commit f705d86

Please sign in to comment.