P4 programming language

P4 programming language
P4 is a programming language for programming 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 maintained 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 projects.
The language was originally coined in 2013 and described in 2014 in a SIGCOMM CCR document titled "Protocol Independent Packet Routing Processor Programming".

Since its inception, P4 has grown and evolved exponentially, quickly becoming the standard for describing the transmission of packets by network devices, including network adapters, switches, and routers.

β€œSDN has transformed the networking industry, and P4 takes SDN to the next level by enabling programmability in routing,” 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: to create an easy-to-use language that a software developer can learn in a day and use to describe exactly how packets are sent over the network.

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

Also the language is protocol independent (ie the P4 program can describe existing standard protocols or be used to specify new custom addressing modes).

In industry, P4 is used for device programming. Perhaps in the future, Internet-RFCs and the 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 capture the behavior of a switch pipeline in the Switch Abstraction Interface (SAI) APIs used by the open source SONiC Switch OS. P4 is also used in the ONF Stratum project to describe switching behavior across a variety of fixed and programmable devices.

Describing the behavior of the switch and network adapters for the first time allows you to create an accurate executable model of the entire network before deployment. Large cloud providers can fully test and debug the network through software, greatly reducing the time and cost of interoperability testing in the lab without requiring expensive hardware.

With P4, network equipment vendors can rely on a common underlying routing behavior across all products, allowing for reuse of test infrastructure, simplifies development of management software, and ultimately ensures interoperability.

Of course, P4 can be used to write programs that describe completely new ways of routing. For example, P4 is widely used for telemetry and measurements in data centers, enterprise and service provider networks.

The research community has also become active. Several leading academic networking research groups have published interesting new applications based on P4 programs, including load balancing, negotiated protocols, and key-value caching. A new programming paradigm is being created, innovation is moving from hardware to software, allowing for many unexpected, new and brilliant ideas.

The developer community has made significant contributions to code development, including compilers, pipelines, behavioral models, APIs, test frameworks, applications, and more. There are dedicated developers at 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 including CORD, FD.io, OpenDaylight, ONOS, OvS, SAI and Stratum highlight the fact that P4 is an independent community project.

Typical generation of controllers for the P4 language:

P4 programming language

Perspectives of application

P4 programming language
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:

  1. Independence from the target implementation;
  2. Independence from the protocol(s) used;
  3. Field reconfigurability.

Independence from target implementation

P4 programs are designed to be implementation independent, meaning they can be compiled for many different types of execution machines such as general purpose processors, FPGAs, systems on a chip, 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 built into the target device, external software, or even a cloud service. Because many of the original targets for P4 programs were used for simple packet switching, it is very common to hear the term "P4 switch" even though "P4 target" is more accurate.

Independence from the protocol(s) used

P4 is protocol independent. This means that the language does not have built-in support for common protocols such as IP, Ethernet, TCP, VxLAN, or MPLS. Instead, the P4 programmer describes the header formats and 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 reconfigurability

The protocol independence and abstract language model allows for reconfigurabilityβ€”P4 targets should be able to change how packets are handled after the system is deployed. This capability has traditionally been associated with routing through general purpose processors or network processors rather than fixed function integrated circuits.

Although there is nothing in the language that would prevent the optimization of a certain set of protocols, these optimizations are invisible to the author of the language and may ultimately reduce the flexibility of the system and goals and their reconfigurability.

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

Already, the language is used in many companies:

1) Hyperscale data centers;

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

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

As one of the creators, Google is proud to celebrate the rapid adoption of P4 in the networking industry and in particular in the field of data center architecture.

2) Commercial companies;

Taking advantage of the open source community and the development of common standards and solutions, Goldman Sachs is already bringing innovation to network infrastructure and delivering the best solutions for customers.

3) Production;

The entire networking industry would benefit from a language like P4 that unambiguously defines forwarding behavior. They also consider Cisco, transferring their product lines to use this language.

Juniper Networks has included P4 and P4 Runtime in a range of products, and provides programmatic access to Juniper's embedded processor and its program 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 an early proponent of P4, one of the first to use P4 to define the behavior it wanted to see on networks, and to use P4 programmable forwarders on its network.

Deutsche Telekom uses the language to prototype key network functions as part of the Access 4.0 program.

5) Semiconductor industry;

The language allowed for a new paradigm of transferring software capabilities to the network routing plane by Barefoot.

Xilinx was one of the founders of P4.org and was actively involved in the development of the P4 language and introduced it into FPGA based programmable platforms for SmartNIC and NFV hardware, releasing one of the first P416 compilers as part of the SDNet design.

6) Software.

VMware believes that P4 generates tremendous energy, innovation, and a community that is driving meaningful and necessary network transformation. VMware has been a part of this industry movement since the beginning, as a new wave of innovation is driven by software-based approaches that empower infrastructure and implement it in the latest products.

Thus, P4 is a target-independent and protocol-independent programming language that is used by industry and the scientific community to unambiguously define the behavior of packet routing as a program that can in turn be compiled for multiple targets. Targets today include hardware and software switches, hypervisor switches, NPUs, GPUs, FPGAs, SmartNICs, and ASICs.

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.org

Link to the repository https://github.com/p4langwhere you can get the source code for the examples and tutorials.

plugin for Eclipse with P4 support, but we can recommend P4 Studio from Barefoot.

P4 programming language

Let's analyze the main abstractions of the kernel:

Definition of headers - with their help protocol headers are defined.

The definition of headers specifies:

  • description of packet formats and header field names
  • fixed and variable allowed 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 headlines.

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

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 machine states that associate user keys with actions. Actions β€” a description of how the package should be manipulated.

The tables contain the states (defined at the management level) for packet forwarding, describe the Match-Action unit of action

Packages are matched by:

  • Exact match
  • Longest Prefixed Match (LPM)
  • Triple matching (masking)

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

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

Actions are made up of code and data. The data comes in at the management level (eg IP addresses/port numbers). Certain, loopless primitives can be specified directly in the action, but the number of instructions must be predictable. Therefore, actions cannot contain any loops 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;
}

Match Action Modules - actions to create a search key, search in the table, perform actions.

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

P4 programming language

Control flow Specifies the order in which Match-Action modules are applied. This is an imperative program that defines high-level logic and the Match-Action sequence. The flow of control links all objects by specifying the level of control.

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 - the data structures associated with each package.

There are 2 types of metadata:

  Custom metadata (empty structure for all packages)
    You can put whatever you want here
    Available throughout the pipeline
    convenient to use for your own purposes, for example, to store the hash of the package

  Internal metadata - provided by the 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)

Compiler P4

P4 compiler (P4C) generates:

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

P4 programming language

An example of a software switch in P4 language

Source codes can be downloaded from the repository.

p4lang/p4c-bm: generates JSON configuration for bmv2
p4lang/bmv2: softswitch that understands bmv2 version JSON configurations

The figure shows the project compilation scheme:

P4 programming language

Manipulations with tables, read registers, 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>


The source code contains the simple_switch_CLI program for convenient use of the software switch API.

You can download this and other examples in the repository.

P4 programming language

PS At the beginning of the summer, Intel signed an agreement to acquire Barefoot Networks to quickly meet the needs of Hyperscale Cloud users. According to Navin Shenoy (executive vice president and general manager of the Data Center Group at Intel Corporation), this will allow Intel to provide more workloads and more opportunities for data center customers.

In my personal opinion, do not forget that Intel is the leader in the production of FPGA chips and they have an excellent Quartus environment. This means that with the arrival at Intel, Barefoot will not only expand its product line, but also Quartus and P4 Studio will have major updates and replenishment with the Toffino and Toffino 2 line.

Official member of the P4 community - company factor group.

Source: habr.com

Add a comment