The P4 Programming Language

The P4 Programming Language
P4 is a programming language designed for programming packet routing rules. Unlike general-purpose languages such as C or Python, P4 is a domain-specific language with a range 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), two of the largest umbrella organizations for open-source networking technology projects.
The language was originally conceived in 2013 and described in 2014 in a SIGCOMM CCR paper titled "Protocol-Independent Programming of Packet Processing."

Since its inception, P4 has grown exponentially, quickly becoming the standard for describing packet forwarding in networking devices, including network adapters, switches, and routers.

"SDN has transformed the networking industry, and P4 is taking SDN to the next level by providing programmability in the field of routing," said Guru Parulkar, Executive Director of the Open Networking Foundation.

The P4 language was originally created by a team of engineers and researchers from Google, Intel, Microsoft Research, Barefoot, Princeton, and Stanford. The objective was simple: to create a user-friendly language that a software developer could master in a day and use to accurately describe how packets are forwarded in a network.

From the very beginning, P4 was designed to be target-independent (i.e., a program written in P4 could be compiled unchanged to run on various targets such as ASICs, FPGAs, CPUs, NPUs, and GPUs).

The language is also protocol-independent (i.e., a P4 program can describe existing standard protocols or be used to specify new customizable addressing modes).

In the industry, P4 is used for programming devices. In the future, Internet RFCs and IEEE standards may also include P4 specifications.

P4 can be used for both programmable and fixed-function devices. For instance, it is utilized to accurately record the behavior of the switch pipeline in the Switch Abstraction Interface (SAI) APIs used by the open-source switch operating system SONiC. P4 is also employed in the ONF Stratum project to describe switching behavior across various stationary and programmable devices.

Describing the behavior of switches and network adapters allows for the creation of an accurate executable model of the entire network before deployment. Major cloud providers can fully test and debug the network using software, significantly reducing time and costs associated with lab testing of interactions without requiring expensive hardware.

By using P4, network equipment providers can rely on consistent base routing behavior across all products, enabling the reuse of testing infrastructure, simplifying management software development, and ultimately ensuring compatibility.

Of course, P4 can be utilized to write programs that describe entirely new routing methods. For example, P4 is widely used for telemetry and measurements in data centers, enterprise networks, and service providers.

The research community has also ramped up its efforts. Several leading academic research groups in networking technologies have published intriguing new applications based on P4 programs, including load balancing, synchronized protocols, and key-value caching. A new programming paradigm is emerging, as innovations shift from hardware to software, giving rise to numerous unexpected, new, and brilliant ideas.

The developer community has made significant contributions to the development of code, including compilers, pipeline programs, behavioral models, APIs, testing environments, applications, and much more. Dedicated developers are found in companies such as 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 like CORD, FD.io, OpenDaylight, ONOS, OvS, SAI, and Stratum highlight the fact that P4 is an independent public project.

Typical controller generation for the P4 language:

The P4 Programming Language

Application prospects

The P4 Programming Language
Since the language is intended for routing applications, the list of requirements and design options differs from general-purpose programming languages. The main features of the language are:

  1. Independence from the target implementation;
  2. Independence from the used protocol(s);
  3. Reconfigurability of fields.

Independence from the target implementation

P4 programs are designed to be independent of implementation, meaning they can be compiled for various types of execution machines, such as general-purpose processors, FPGAs, system-on-chip devices, network processors, and ASICs. These different types of machines are known as P4 targets, and each target requires a compiler to convert the P4 source code into a target switch model. The compiler can be embedded in the target device, external software, or even a cloud service. Since many of the initial targets for P4 programs were used for simple packet switching, the term 'P4 switch' is often heard, even though the use of 'P4 target' is more accurate.

Independence from the used protocol(s)

P4 is independent of protocols. This means that the language does not have built-in support for common protocols such as IP, Ethernet, TCP, VxLAN, or MPLS. Instead, a P4 programmer describes the header formats and field names of the required protocols in the program, which are then interpreted and processed by the compiled program and target device.

Reconfigurability of fields

Independence from the protocol and the abstract language model allow for reconfigurability — P4 goals must enable packet processing modifications post-deployment. This capability has traditionally been associated with routing via general-purpose processors or network processors rather than fixed-function integrated circuits.

While the language does not inherently limit the optimization of a specific set of protocols, such optimizations are invisible to the language author and may ultimately reduce the system's flexibility and the configurability of its goals.

These language characteristics were initially laid down by its creators with a focus on widespread use in network infrastructure.

The language is already in use by many companies:

1) Hyperscale data centers;

Chinese company Tencent is the largest investment company in the world and one of the largest venture companies. Tencent's subsidiaries, both in China and around the world, specialize in various high-tech business areas, including internet services, artificial intelligence developments, and electronic entertainment.

P4 and programmable routing are advanced technologies used in the company's network architecture.

As one of its creators, Google proudly notes the rapid adoption of P4 in the networking industry, particularly in data center architectural design.

2) Commercial companies;

Goldman Sachs is already innovating in network infrastructure and providing the best solutions for clients by leveraging the benefits of working with the open-source community and developing common standards and solutions.

3) Manufacturing;

The entire network industry will benefit from a language like P4, which clearly defines forwarding behavior. Cisco shares this belief as it transitions its product lines to utilize this language.

Juniper Networks has integrated P4 and P4 Runtime into a range of products, providing programmatic access to the built-in Juniper processor and its software.

Ruijie Networks is a strong advocate for P4 and the benefits it brings to networks. With P4, the company can create and deliver best-in-class solutions for a wide range of clients.

4) Telecommunications providers;

AT&T was one of the first supporters of P4, being among the first to use P4 to define the behavior it wanted to see in networks and utilizing P4 programmable forwarding devices in its network.

In Deutsche Telekom, the language is used to prototype key network functions within the Access 4.0 program.

5) Semiconductor industry;

The language has enabled a new paradigm of delivering software capabilities into the network routing layer by Barefoot.

Xilinx was one of the founders of P4.org and actively participated in the development of the P4 language, implementing it in FPGA-based programmable platforms for SmartNIC and NFV equipment, releasing one of the first P416 compilers as part of the SDNet design.

6) Software.

At VMware, they believe that P4 generates tremendous energy, innovation, and a community that leads to significant and necessary transformations in networking. VMware was initially part of this industry movement, as the new wave of innovation is driven by software approaches that extend infrastructure capabilities and implement them in the latest products.

Thus, P4 is a target-independent and protocol-independent programming language utilized by the industry and the academic community to unambiguously define packet routing behavior as a program, which in turn can be compiled for multiple targets. Today, these targets include hardware and software switches, hypervisor switches, NPUs, GPUs, FPGAs, SmartNICs, and ASICs.

The main features of the language significantly expand its application areas and facilitate rapid implementation in network architectures.

Getting Started

P4 is an open project, all current information is available on the website P4.org

Link to the repository https://github.com/p4lang, where you can access the source codes of examples and educational materials.

Plugin for Eclipse with P4 support, but we can recommend P4 Studio by Barefoot.

The P4 Programming Language

Let's discuss the core abstractions:

Header Definition — they are used to define protocol headers.

In the header definition, the following is specified:

  • description of packet formats and header field names
  • fixed and variable allowed fields

For example

header Ethernet_h{
    bitdstAddr;
    bitsrcAddr;
    bitetherType;
}

header IPv4_h{
    bitversion;
    bitihl;
    bitdiffserv;
    bittotalLen;
    bitidentification;
    bitflags;
    bitfragOffset;
    bitttl;
    bitprotocol;
    bithdrChecksum;
    bitsrcAddr;
    bitdstAddr;
    varbitoptions;
}

Parsers — their task is to parse the headers.

The following parser example will determine the transition of the finite state machine from one initial state to one of two final states:

The P4 Programming Language

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 the states of the machine linking user keys with actions. Actions — description of how to manipulate the packet.

Tables contain states (defined at the management level) for forwarding packets, describe the Match-Action unit of action

Packet matching is performed by:

  • Exact match
  • Longest Prefix Match (LPM)
  • Triple match (masking)

table ipv4_lpm{
    reads{
        ipv4.dstAddr: lpm;
    } maxActions {
        forward();
    }
}

All possible actions must be defined in the tables in advance.

Actions consist of code and data. Data comes from the management level (e.g., IP addresses / port numbers). Defined, loop-free primitives can be specified directly in the action, but the number of commands must be predictable. Therefore, actions cannot contain any loops or conditional operators.

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;
}

Match-Action Modules — actions for creating a search key, searching in the table, executing actions.

A typical example of a module is shown in the figure:

The P4 Programming Language

Control Flow — specifies the order of applying Match-Action modules. This is an imperative program that defines high-level logic and the sequence of Match-Action. The control flow ties all objects together, setting the control level.

External Objects — these are specific objects with clearly defined architecture and API interfaces. For example, checksum calculation, registers, counters, etc.

extern register{
    register(bit size);
    void read(out T result, in bit index);
    void write(in bit 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
  bitget(); //get the checksum for the data added since last clear
}

Metadata — data structures associated with each packet.

There are 2 types of metadata:

  User Metadata (an empty structure for all packets)
    You can put anything you want here
    Available throughout the pipeline
    convenient for use for your purposes, for example, to store the packet hash

  Internal Metadata — provided by architecture
    Input port, output port are defined here
    Timestamp when the packet was queued, queue depth
    multicast hash / multicast queue
    Packet priority, packet importance
    Output port specification (e.g., output queue)

P4 compiler

P4 compiler (P4C) generates:

  1. Data plane runtime
  2. API for managing the state of the machine in the data plane

The P4 Programming Language

Example of a software switch in P4

The source code can be downloaded from the repository.

p4lang/p4c-bm: creates JSON configuration for bmv2
p4lang/bmv2: software switch that understands JSON configurations of version bmv2

The diagram illustrates the project's compilation scheme:

The P4 Programming Language

Manipulations with tables, read registers, counters:

  • set_default_table <table name> <action name> <action parameters>
  • add table <table name> <action name> <match fields> => &lt;action
    parameters> [priority]
  • delete table <table name> <entry handle>


The source codes include the simple_switch_CLI program for convenient use of the software switch API.

You can download this and other examples from the repository.

The P4 Programming Language

P.S. At the beginning of summer, Intel signed an agreement to acquire Barefoot Networks, aiming to quickly meet the needs of Hyperscale Cloud users. As stated by Navin Shenoy (executive vice president and general manager of the Data Center Group at Intel Corporation) — this will allow Intel to provide larger workloads and more capabilities for data center clients.

In my personal opinion, we should not forget that Intel is a leader in FPGA chip production and has an excellent Quartus environment. Therefore, it can be expected that with Barefoot joining Intel, not only will the production line be expanded, but also Quartus and P4 Studio will see significant updates and the addition of the Toffino and Toffino 2 product lines.

Official member of the P4 community — the company Factor group.

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster