0% found this document useful (0 votes)
68 views2 pages

p4 Cheat Sheet

This document defines types, headers, parser states, and actions for a P4 program that implements basic IPv4 and MPLS forwarding. It includes definitions for Ethernet, IPv4, MPLS, and common metadata headers. Parser states are defined to parse the headers in sequence. Actions include push/pop operations on the MPLS label stack, setting the next hop, and basic forwarding operations. Registers and counters are also defined to track state.

Uploaded by

Joe Blow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

p4 Cheat Sheet

This document defines types, headers, parser states, and actions for a P4 program that implements basic IPv4 and MPLS forwarding. It includes definitions for Ethernet, IPv4, MPLS, and common metadata headers. Parser states are defined to parse the headers in sequence. Actions include push/pop operations on the MPLS label stack, setting the next hop, and basic forwarding operations. Registers and counters are also defined to track state.

Uploaded by

Joe Blow
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// typedef: introduces alternate type name // Local metadata declaration, assignment table ipv4_lpm {

typedef bit<48> macAddr_t; bit<16> tmp1; bit<16> tmp2; key = {


typedef bit<32> ip4Addr_t; tmp1 = hdr.ethernet.type; hdr.ipv4.dstAddr : lpm;
// standard match kinds:
// headers: ordered collection of members // bit slicing, concatenation // exact, ternary, lpm
// operations test and set validity bits: tmp2 = tmp1[7:0] ++ tmp1[15:8]; }
// isValid(), setValid(), setInvalid() // actions that can be invoked
header ethernet_t { // addition, subtraction, casts actions = {
macAddr_t dstAddr; tmp2 = tmp1 + tmp1 - (bit<16>)tmp1[7:0]; ipv4_forward;
macAddr_t srcAddr; drop;
bit<16> type; // bitwise operators NoAction;
} tmp2 = (~tmp1 & tmp1) | (tmp1 ^ tmp1); }
tmp2 = tmp1 << 3; // table properties
// variable declaration and member access size = 1024;
ethernet_t ethernet; default_action = NoAction();
macAddr_t src = ethernet.srcAddr; }

// struct: unordered collection of members // Inputs provided by control-plane


struct headers_t { action set_next_hop(bit<32> next_hop) {
ethernet_t ethernet; if (next_hop == 0) {
} metadata.next_hop = hdr.ipv4.dst; apply {
} else { // branch on header validity
metadata.next_hop = next_hop; if (hdr.ipv4.isValid()) {
} ipv4_lpm.apply();
} }
// packet_in: extern for input packet // branch on table hit result
extern packet_in { // Inputs provided by data-plane if (local_ip_table.apply().hit) {
void extract<T>(out T hdr); action swap_mac(inout bit<48> x, send_to_cpu();
void extract<T>(out T hdr,in bit<32> n); inout bit<48> y) { }
T lookahead<T>(); bit<48> tmp = x; // branch on table action invocation
void advance(in bit<32> n); x = y; switch (table1.apply().action_run) {
bit<32> length(); y = tmp; action1: { table2.apply(); }
} } action2: { table3.apply(); }
}
// parser: begins in special "start" state // Inputs provided by control/data-plane }
state start { action forward(in bit<9> p, bit<48> d) {
transition parse_ethernet; standard_metadata.egress_spec = p;
} headers.ethernet.dstAddr = d;
}
// User-defined parser state // packet_out: extern for output packet
state parse_ethernet { // Remove header from packet extern packet_out {
packet.extract(hdr.ethernet); action decap_ip_ip() { void emit<T>(in T hdr);
transition select(hdr.ethernet.type) { hdr.ipv4 = hdr.inner_ipv4; }
0x800: parse_ipv4; hdr.inner_ipv4.setInvalid();
default: accept; } apply {
} // insert headers into pkt if valid
} packet.emit(hdr.ethernet);
}
// header stack declaration // common externs struct standard_metadata_t {
header label_t { extern void truncate(in bit<32> length); bit<9> ingress_port;
bit<20> label; extern void resubmit<T>(in T x); bit<9> egress_spec;
bit bos; extern void recirculate<T>(in T x); bit<9> egress_port;
} enum CloneType { I2E, E2I } bit<32> clone_spec;
struct header_t { extern void clone(in CloneType type, bit<32> instance_type;
label_t[10] labels; in bit<32> session); bit<1> drop;
} bit<16> recirculate_port;
header_t hdr; // v1model pipeline elements bit<32> packet_length;
parser Parser<H, M>( bit<32> enq_timestamp;
// remove from header stack packet_in pkt, bit<19> enq_qdepth;
action pop_label() { out H hdr, bit<32> deq_timedelta;
hdr.labels.pop_front(1); inout M meta, bit<19> deq_qdepth;
} inout standard_metadata_t std_meta bit<48> ingress_global_timestamp;
); bit<48> egress_global_timestamp;
// add to header stack control VerifyChecksum<H, M>( bit<32> lf_field_list;
action push_label(in bit<20> label) { inout H hdr, bit<16> mcast_grp;
hdr.labels.push_front(1); inout M meta bit<32> resubmit_flag;
hdr.labels[0].setValid(); ); bit<16> egress_rid;
hdr.labels[0] = { label, 0}; control Ingress<H, M>( bit<1> checksum_error;
} inout H hdr, bit<32> recirculate_flag;
inout M meta, }
inout standard_metadata_t std_meta
);
control Egress<H, M>(
// common defns for IPv4 and IPv6 inout H hdr,
header ip46_t { inout M meta, // counters
bit<4> version; inout standard_metadata_t std_meta counter(8192, CounterType.packets) c;
bit<4> reserved; );
} control ComputeChecksum<H, M>( action count(bit<32> index) {
inout H hdr, //increment counter at index
// header stack parsing inout M meta c.count(index);
state parse_labels { ); }
packet.extract(hdr.labels.next); control Deparser<H>(
transition select(hdr.labels.last.bos) { packet_out b, in H hdr // registers
0: parse_labels; // create loop ); register<bit<48>>(16384) r;
1: guess_labels_payload;
} // v1model switch action ipg(out bit<48> ival, bit<32> x) {
} package V1Switch<H, M>( bit<48> last;
Parser<H, M> p, bit<48> now;
// lookahead parsing VerifyChecksum<H, M> vr, r.read(x, last);
state guess_labels_payload { Ingress<H, M> ig, now = std_meta.ingress_global_timestamp;
transition select(packet.lookahead< Egress<H, M> eg, ival = now - last;
ip46_t>().version) { ComputeChecksum<H, M> ck, r.write(x, now);
4 : parse_inner_ipv4; Deparser<H> d }
6 : parse_inner_ipv6; );
default : parse_inner_ethernet;
}
}

You might also like