This document provides an overview of the P4 programming language. P4 allows defining the data plane functionality of programmable forwarding elements like software switches, NICs, routers, and network appliances in a protocol and target independent way. The key components of P4 include data declarations to define packet headers and metadata, parser programming to parse packets, packet processing programming using match-action tables, and control functions to define the communication between the data plane and control plane. A P4 program defines the packet parsing and processing logic, while the P4 compiler generates target-specific code that implements this logic on the actual hardware or software target.
14. Handson
● Goto --> ... /tutorialsmaster/exercises/
●
Create new folder and named it as myip_forward
●
Open any editor and create new p4 program
Example : myip_forward.p4
●
Include core.p4 header file
●
It defines some standard datatypes and error codes.
●
For example: declarations of the predefined packet_in and packet_out
extern objects.
● #include <core.4>
●
What is extern in C ?
15. <Include Header Files>
● Goto --> ... /tutorialsmaster/exercises/
●
Create new folder and named it as myipforward
●
Open any editor and create new p4 program
Example : ip_forward.p4
●
Include core header file
●
It defines some standard datatypes and error codes.
●
For example: declarations of the predefined packet_in and packet_out
extern objects.
●
What is extern in C ?
#include<core.4>
●
Include v1model header file
●
For Example:
#include<v1model.p4>
29. Code Block
START
ETHERNET
ACCEPT
IPv4
MyParser
packet_in
meta meta
hdr
standard_meta standard_meta
IN OUT
parser MyParser (packet_in packet ,
out headers hdr,
inout metadata meta ,
inout standard_metadata_t standard_metadata )
Parser Declaration
state start {
transition parse_ethernet;
}
●
Each state has a name and body.
●
It consists of a sequence of statements that describe the processing
performed when the parser transitions to that state including:
●
Local variable declarations
●
Assignment statements
●
Method calls, which serve following purpose:
●
Invoking function
●
Invoking methods
●
Transitions to other states
30. Code Block
START
ETHERNET
ACCEPT
IPv4
MyParser
packet_in
meta meta
hdr
standard_meta standard_meta
IN OUT
parser MyParser (packet_in packet ,
out headers hdr,
inout metadata meta ,
inout standard_metadata_t standard_metadata )
Parser Declaration
state start {
transition parse_ethernet;
}
parser_ethernet
●
Extract ethernet header
●
Check EtherType field
DEST MAC SRC MAC EtherType
0x0800
0x0806
0x86DD
. . .
. . .
31. Code Block
START
ETHERNET
ACCEPT
IPv4
MyParser
packet_in
meta meta
hdr
standard_meta standard_meta
IN OUT
parser MyParser (packet_in packet ,
out headers hdr,
inout metadata meta ,
inout standard_metadata_t standard_metadata )
Parser Declaration
state start {
transition parse_ethernet;
}
parser_ethernet
●
Extract ethernet header
●
Check EtherType field
DEST MAC SRC MAC EtherType
0x0800 : IPv4
0x0806 : ARP
0x86DD : IPv6
. . .
. . .
32. Code Block
START
ETHERNET
ACCEPT
IPv4
MyParser
packet_in
meta meta
hdr
standard_meta standard_meta
IN OUT
parser MyParser (packet_in packet ,
out headers hdr,
inout metadata meta ,
inout standard_metadata_t standard_metadata )
Parser Declaration
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV4: parse_ipv4;
default: accept;
}
}
33. Code Block
START
ETHERNET
ACCEPT
IPv4
MyParser
packet_in
meta meta
hdr
standard_meta standard_meta
IN OUT
parser MyParser (packet_in packet ,
out headers hdr,
inout metadata meta ,
inout standard_metadata_t standard_metadata )
Parser Declaration
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV4: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition accept;
}
}