P4 programming language


    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 nonprofit 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 networking projects.
    The language was originally coined in 2013 and described in 2014 in a SIGCOMM CCR document entitled “Protocol Independent, Packet Routing Processor Programming”.

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

    “SDN has transformed the network industry, and P4 takes SDN to the next level, 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 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 very beginning, P4 was designed to be independent of the goal (i.e. a program written in P4 could be compiled unchanged for 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 can be used to indicate new custom addressing modes).

    In industry, 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 behavior of the 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 using software, which significantly reduces the time and cost of testing lab interactions without requiring expensive equipment.

    Using P4, network equipment vendors can rely on common, basic routing behavior across all products, which allows reuse of the test infrastructure, simplifies management software development, and ultimately ensures interoperability.

    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, enterprise networks and service providers.

    The research community has also intensified. Several leading academic network technology research groups 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 moving from hardware to software, which allows many unexpected, new and brilliant ideas to appear.

    The development community has made significant contributions to code development, including compilers, pipelines, behavioral models, APIs, test environments, 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 P4 controller generation:



    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:

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

    Independence from the target implementation

    P4 programs are designed so that they are not implementation dependent, 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 various types of machines are known as P4 targets, and a compiler is required for each target to convert the P4 source code to 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 goals for P4 programs were used for simple packet switching, you can often hear the term “P4 switch”, even if the use of “target P4” is more correct.

    Independence of 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

    Protocol independence and the abstract language model allow reconfigurability - P4 goals should be able to change the processing of packets after deploying the system. This feature has traditionally been 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 certain set of protocols, these optimizations are invisible to the author of the language and can, ultimately, reduce the flexibility of the system and purpose and their reconfigurability.

    These characteristics of the language were originally laid by its creators with an orientation to its widespread use in the network infrastructure.

    Already, the language is used in many companies:

    1) Hyper-scale data centers;

    The 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 that are used in the company's network architecture.

    As one of the creators, Google is proud to note the rapid introduction of P4 in the network industry and, in particular, in the field of architectural design of data centers.

    2) Commercial companies;

    Goldman Sachs, taking advantage of working with the open source community and developing common standards and solutions, is already bringing innovation to the network infrastructure and providing the best solutions for customers.

    3) Production;

    The entire network industry will benefit from a language like P4, which uniquely defines call diverting behavior. Also consider in Cisco, transferring their product lines to use this language.

    Juniper Networks has included the P4 and P4 Runtime in a number of products, and provides programmatic access to the Juniper embedded processor and its program code.

    Ruijie Networks is an active 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 that it wanted to see on networks, and to use P4 programmable forwarding devices on its network.

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

    5) Semiconductor industry;

    The language made it possible to implement a new paradigm for transferring software capabilities to the network routing plane by Barefoot.

    Xilinx was one of the founders of P4.org and took an active part in developing the P4 language and implemented it in FPGA-based programmable platforms for SmartNIC and NFV equipment, releasing one of the first P4 compilers 16as part of the SDNet design.

    6) Software.

    VMware believes that P4 creates tremendous energy, innovation and a community that leads to meaningful and necessary transformation in the network. VMware was originally part of this industry movement, as a new wave of innovation is driven by software 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 determine 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, NPUs, GPUs, FPGAs, SmartNICs, and ASICs.

    The main features of the language significantly expand the scope of its application and provide its quick implementation in network architecture.

    Where to begin


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

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

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



    We will analyze the main abstractions of the kernel:

    Defining headers - with their help, protocol headers are defined.

    The definition of headers sets:

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

    For example, Parsers are their task to parse headers. The following parser example will determine the transition of the final state of a machine from one initial state to one of two final ones:

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








    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 state of the machine associating user keys with actions. Actions - A description of how the package should be manipulated.

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

    Matching of packets is performed by:

    • Exact match
    • Longest Match with a Prefix (LPM)
    • Triple matching (masking)

    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 arrives at the management level (e.g. IP addresses / port numbers). Certain, loopless primitives can be specified directly in action, but the number of commands must be predictable. Therefore, actions cannot contain any loops or conditional statements. Match-Action modules - actions to create a search key, search in a table, perform actions. A typical module example is shown in the figure:

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








    Control flow - indicates the application order of Match-Action modules. This is an imperative program that defines high-level logic and Match-Action sequence. The control flow links all objects, setting the control level.

    External objects are specific objects with a clearly defined architecture and APIs. For example, checksum calculation, registers, counters, counters, etc. Metadata is the data structure 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.

    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
    }





      


        convenient for use for your own purposes, for example, for storing a packet hash

      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 machine state control in data plane


    An example of a software switch in P4


    Source codes can be downloaded from the repository.

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

    The figure shows the compilation of the project:



    Manipulations with tables, read registers, counter:

    • table_set_default
    • table_add
    • => parameters> [priority]
    • table_delete

    • В исходных кодах приведена программа simple_switch_CLI для удобного использования API программного свитча.

      Этот и другие примеры вы можете скачать в репозитории.



      P.S. В начале лета компания Intel подписала соглашение о приобретении Barefoot Networks, с тем прицелом, чтобы быстро удовлетворять потребности пользователей Hyperscale Cloud. Как заявил Navin Shenoy(executive vice president and general manager of the Data Center Group at Intel Corporation) — это  позволит Intel предоставлять бОльшие рабочие нагрузки и больше возможностей для клиентов ЦОДов.

      По моему личному мнению не стоит забывать, что компания Intel — лидер по производству чипов FPGA и у нее есть отличная среда Quartus. А значит можно ожидать, что с приходом в Intel, у компании Barefoot не только пополнится производственная линейка, но и Quartus, и P4 Studio ждут серьезные обновления и пополнение линейкой Toffino и Toffino 2.

      Официальный участник сообщества P4 — компания Фактор груп.

    Also popular now: