P4 is a programming language designed to program packet routing rules. Unlike a general purpose language such as C or Python, P4 is a domain-specific language with a number of constructs optimized for network routing.
P4 is an open source language, licensed and supported by a non-profit organization called the P4 Language Consortium. It is also supported by the Open Networking Foundation (ONF) and the Linux Foundation (LF) - the two largest umbrella organizations in open source projects in the field of network technologies.
The language was originally coined in 2013 and described in 2014 in a SIGCOMM CCR document called “Protocol Independent Programming of the Package Routing Processor”.
Since its inception, P4 has grown exponentially and developed, quickly becoming the standard for describing packet transmission by network devices, including network adapters, switches and routers.
')
“SDN has transformed the networking industry, and P4 takes SDN to a new level, providing programmability in the field of Marshutization,” said Guru Parulkar, executive director of the Open Networking Foundation.
The P4 language was originally created by a group of engineers and researchers from Google, Intel, Microsoft Research, Barefoot, Princeton and Stanford. The goal was simple: create an easy-to-use language that a software developer can learn in a day, and use it to accurately describe how packets are sent over the network.
From the outset, the P4 was developed as a target independent (i.e., a program written in P4 could be compiled without modification to be performed on various purposes, such as ASIC, FPGA, CPU, NPU and GPU).
Also, the language is protocol independent (i.e., the P4 program can describe existing standard protocols or be used to specify new custom addressing modes).
In industry, the P4 is used for programming devices. Perhaps in the future, Internet-RFC and IEEE standards will also include the P4 specification.
P4 can be used for both programmable and fixed-function devices. For example, it is used to accurately record the switch pipeline behavior in the switch abstraction interface (SAI) APIs used by the switch OS with the open source SONiC. P4 is also used in the ONF Stratum project to describe switching behavior across multiple stationary and programmable devices.
Describing the behavior of the switch and network adapters for the first time allows you to create an exact executable model of the entire network before deployment. Large cloud providers can fully test and debug the network using software, which significantly reduces the time and cost of testing the interaction in the laboratory without requiring expensive equipment.
Using P4, network equipment providers can rely on a common basic routing behavior across all products, which allows reuse of testing infrastructure, simplifies management software development and, ultimately, ensures compatibility.
Of course, P4 can be used to write programs that describe completely new routing methods. For example, P4 is widely used for telemetry and measurements in data centers, networks of enterprises and service providers.
The research community has also stepped up. Several leading academic research groups on networking technologies have published interesting new applications based on P4 programs, including load balancing, consistent protocols, and key-value caching. A new programming paradigm is being created, innovations are being transferred from hardware to software, which allows many unexpected, new and ingenious ideas to emerge.
The developer community has made significant contributions to code development, including compilers, pipeline programs, behavioral models, APIs, test environments, applications, and more. Devoted developers are in companies like Alibaba, AT & T, Barefoot, Cisco, Fox Networks, Google, Intel, IXIA, Juniper Networks, Mellanox, Microsoft, Netcope, Netronome, VMware, Xilinx and ZTE; from universities including BUPT, Cornell, Harvard, MIT, NCTU, Princeton, Stanford, Technion, Tsinghua, UMass, and USI; and open source projects, including CORD, FD.io, OpenDaylight, ONOS, OvS, SAI and Stratum, underline the fact that P4 is an independent public project.
Typical controller generation for the P4 language:
Application prospects
Since the language is intended for routing applications, the list of requirements and design options is different compared to general-purpose programming languages. The main features of the language are:
- Independence from the target implementation;
- Independence from the protocol (s) used;
- Reconfigurability fields.
Independence from target implementationP4 programs are designed to be implementation-independent, that is, they can be compiled for many different types of execution machines, such as general-purpose processors, FPGAs, on-chip systems, network processors, and ASICs. These different types of machines are known as P4 targets, and a compiler is needed for each target in order to convert the P4 source code into the target switch model. The compiler can be embedded in the target device, external software or even a cloud service. Since many of the original targets for the P4 programs were used for simple packet switching, it is often possible to hear the term “P4 switch”, even if the use of “P4 target” is more true.
Independence from the protocol (s) usedP4 is protocol independent. This means that the language does not have native support for common protocols such as IP, Ethernet, TCP, VxLAN, or MPLS. Instead, the P4 programmer describes the header formats and the field names of the required protocols in the program, which in turn are interpreted and processed by the compiled program and the target device.
Field ConfigurabilityProtocol independence and the abstract language model allow for reconfigurability — P4 goals should be able to modify the processing of packets after the system is deployed. This feature is traditionally associated with routing through general-purpose processors or network processors, rather than integrated circuits with fixed functions.
Although there is no language that could prevent the optimization of a specific set of protocols, these optimizations are invisible to the author of the language and may ultimately reduce the flexibility of the system and the goals and their reconfigurability.
These characteristics of the language were originally laid by its creators with a focus on its widespread use in the network infrastructure.
Already, the language is used in many companies:
1) Hyper-scale data centers;Chinese company Tencent is the largest investment company in the world and one of the largest venture capital companies. Tencent subsidiaries, both in China and in other countries of the world, specialize in various areas of high-tech business, including various Internet services, developments in the field of artificial intelligence and electronic entertainment.
P4 and programmable routing are advanced technologies used in the company's network architecture.
As one of the creators, Google is proud to note the rapid introduction of P4 in the networking industry and, in particular, in the architectural design of the data center.
2) Commercial companies;Using the benefits of working with the open source community and developing common standards and solutions, Goldman Sachs brings innovation to the network infrastructure and provides better solutions for customers.
3) Production;The entire network industry will benefit from a language such as P4, which uniquely identifies redirection behavior. They also consider Cisco, transferring their product lines to the use of this language.
Juniper Networks has incorporated the P4 and P4 Runtime into a range of products, and provides programmatic access to the Juniper embedded processor and its software code.
Ruijie Networks is a strong supporter of P4 and the benefits it brings to networks. With P4, a company can create and deliver best-in-class solutions for a wide range of customers.
4) Telecommunication providers;AT & T was one of the first supporters of P4, one of the first to use P4 to determine the behavior it wanted to see on networks, and to use P4 programmable forwarding devices on its network.
At Deutsche Telekom, the language is used to create prototypes of key network functions within the Access 4.0 program.
5) Semiconductor industry;The language allowed Barefoot to implement a new paradigm of transferring software capabilities to the network routing plane.
Xilinx was one of the founders of P4.org and was actively involved in the development of the P4 language and implemented it in FPGA-based programmable platforms for SmartNIC and NFV equipment, releasing one of the first P4
16 compilers as part of SDNet design.
6) Software.VMware believes that P4 creates tremendous energy, innovation and community, which leads to meaningful and necessary transformations in the network. VMware was originally part of this industry movement, as the new wave of innovation is driven by programmatic approaches that expand the capabilities of the infrastructure and implement it in the latest products.
Thus, P4 is a goal-independent and protocol-independent programming language that is used by industry and the scientific community to uniquely define the behavior of packet routing as a program, which, in turn, can be compiled for several purposes. Today's goals include hardware and software switches, hypervisor switches, NPU, graphics processors, FPGA, SmartNIC, and ASIC.
The main features of the language significantly expand the scope of its application and ensure its rapid implementation in network architectures.
Where to begin
P4 is an open project, all relevant information is on the website
P4.orgLink to the repository
https://github.com/p4lang , where you can get source codes of examples and training materials.
Plugin for Eclipse with P4 support, but we can recommend
P4 Studio from Barefoot.
Let us analyze the main abstractions of the kernel:
Definition of headers - with their help protocol headers are defined.
In the definition of headers is given:
- description of packet formats and header field names
- fixed and variable permitted fields
for example
header Ethernet_h{
bit <48> dstAddr;
bit <48> srcAddr;
bit <16> etherType;
}
header IPv4_h{
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;
varbit <320> options;
}
Parsers - their task is to parse the headers.
The following example of the parser will determine the transition of the final state of the machine from one initial state to one of two final states:
parser MyParser(){
state start{transition parse_ethernet;}
state parse_ethernet{
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType){
TYPE_IPV4: parse_ipv4;
default: accept;
}
}…
}
Tables — contain machine states that associate user keys with actions.
Actions - a description of how the package should be manipulated.
Tables contain states (defined at the management level) for packet forwarding, describe the Match-Action unit of action.
Comparison of packages is made by:
- Exact match
- Longest Match with Prefix (LPM)
- Triple mapping
table ipv4_lpm{
reads {
ipv4.dstAddr: lpm;
} actions {
forward();
}
}
All possible actions should be defined in the tables in advance.
Actions consist of code and data. Data is received at a management level (for example, IP addresses / port numbers). Certain, cyclic-free primitives can be specified directly in action, but the number of commands must be predictable. Therefore, actions cannot contain any cycles or conditional statements.
action ipv4_forward(macAddr_t dstAddr, egressSpec_t port){
standard_metadata.egress_spec = port;
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}
Modules Match-Action - actions to create a search key, search the table, perform actions.
A typical example of a module is shown in the figure:
Control flow - indicates the order of application of the Match-Action modules. This is an imperative program that defines high-level logic and a Match-Action sequence. The control flow connects all objects by specifying the control level.
External objects are specific objects with a clearly defined architecture and APIs. For example, checksum calculation, registers, counters, counters, etc.
extern register{
register(bit<32> size);
void read(out T result, in bit<32> index);
void write(in bit<32> index, in T value);
}
extern Checksum16{
Checksum16(); //constructor
void clear(); //prepare unit for computation
void update(in T data); //add data to checksum
void remove(in T data); /remove data from existing checksum
bit <16> get(); //get the checksum for the data added since last clear
}
Metadata - data structures associated with each package.
There are 2 types of metadata:
Custom metadata (empty structure for all packages)You can put everything you want here.
Available throughout the pipeline
convenient for use for their own purposes, for example, to store the package hash
Internal metadata - provided by architectureInput port, output port are defined here.
The timestamp when the packet was queued, the queue depth
multicast hash / multicast queue
Package priority, package importance
Output port specification (for example, output queue)
P4 compiler
P4 compiler (P4C) generates:
- Data plane runtime
- API to manage the state of the machine in the data plane
An example of a software switch in the P4 language
Source codes can be downloaded from the repository.
p4lang / p4c-bm: creates a JSON configuration for bmv2
p4lang / bmv2: softswitch that understands bmv2 version JSON configurations
The figure shows the project compilation scheme:
Manipulations with tables, registers of reading, counter:
table_set_default <table name> <action name> <action parameters> table_add <table name> <action name> <match fields> => <action
parameters> [priority] table_delete <table name> <entry handle>
table_set_default <table name> <action name> <action parameters> table_add <table name> <action name> <match fields> => <action
parameters> [priority] table_delete <table name> <entry handle>
table_set_default <table name> <action name> <action parameters> table_add <table name> <action name> <match fields> => <action
parameters> [priority] table_delete <table name> <entry handle>
The source code contains the program simple_switch_CLI for convenient use of the software switch API.
This and other examples you can download in the repository.
PS In early summer, Intel signed an agreement to acquire Barefoot Networks, with a view to quickly meeting the needs of Hyperscale Cloud users. According to Navin Shenoy (executive director at the Intel Corporation), this will allow Intel to provide greater workloads and more opportunities for data center customers.
In my personal opinion, we should not forget that Intel is the leader in the production of FPGA chips and it has an excellent Quartus environment. So you can expect that with the arrival at Intel, Barefoot will not only replenish the product line, but also Quartus and P4 Studio will have major updates and line-ups with the Toffino and Toffino 2 line.
The official member of the P4 community is the
Factor Group Company.