P4 FAQ

 

What is P4 about and what is P4 not about?

P4 is a high-level programming language for expressing how packets are processed by the data plane of any programmable packet processing device like switches, network interface cards(NICs), network processing units(NPUs), field programmable gate arrays(FPGAs), software switches, etc.

P4 programs describe the data plane behavior only. In addition to that, they allow the programmer to generate the application programming interfaces (APIs) that the control plane can use to communicate with or configure the data plane.

How does a P4-programmable switch differs from a traditional switch?

A P4-programmable switch differs from a traditional switch in two ways:

First, the switch’s data plane is no longer fixed. P4 programs describe the data plane functionality. The data plane is configured at P4-to-target compile time. P4 itself is protocol independent; it allows programmers to express a rich set of data plane behaviors.

Second, the control plane continues to interact with the data plane using the same channels; however, the set of tables and other objects driving the behavior of the data plane is no longer fixed, since those are derived from a specific P4 program. The P4 compiler generates the API that the control plane uses to communicate with the data plane from the P4 program.

What is the difference between P4 and OpenFlow?

Here is a blog post written by Nick McKeown and Jen Rexford explaining the differences between P4 and OpenFlow. P4 addresses a much more general problem than OpenFlow.

OpenFlow is designed for SDN networks in which we separate the control plane from the forwarding plane. It assumes the switches have a fixed, well-known behavior, typically described in the data sheet of a switch ASIC. OpenFlow gives us a way to populate a set of well-known tables.

P4 is designed to program the behavior of any switch or router, whether it’s controlled locally from a switch operating system, or remotely by an SDN controller. P4 let us control switches “top-down” by first specifying their forwarding behavior, then populating the tables we’ve defined. In P4, OpenFlow is one of many possible programs to describe what the forwarding plane does.

Why doesn’t P4 support loops, pointers, and conditionals in an action?

P4 is a domain-specific language to describe packet-forwarding logic. It is designed to be implementable on a variety of platforms including programmable network cards, FPGA switches, software switches and programmable ASICs. As such the language is restricted to constructs that are efficiently implementable on all these platforms.

P4 is not a general-purpose programming language. All P4 program blocks should provably execute a constant number of operations provided that cost of table lookup cost is constant. These guarantees are necessary for enabling fast packet processing across a variety of targets.

Pointers are absent because P4 memory model is fundamentally different. Direct access happens either via a key or via an integer index.

Can P4 call external library functions?

P4 is not designed to be able to implement complex computing functions such as encryption, decryption or string manipulation in P4. However, P4 programmers can consume complex computing functions if they are already made available in the processing pipeline of the target, by using the extern type. P4 target providers could expose a few library constructs (e.g., hardwired checksum unit on chip or C-library functions in software switch) at the P4 level as extern types. Such extern types will most likely be supported only on those special types of targets and not programmable using P4.

Is there a quick intro to all the repositories in github.com/p4lang?

Core Software Components

  • behavioral-model is an architecture-independent user-space software switch to emulate P4 dataplane, nicknamed bmv2. It can be used to implement different P4-programmable targets. The repository comes with three example targets. The most important one is simple_switch, which tries to implement the abstract switch model described in the original P4 spec.
  • p4c-bm is the compiler for the behavioral model (bmv2). it takes P4 program and outputs a JSON configuration which can be loaded by the behavioral model. The compiler can also generate program-dependent APIs that the control plane can use to manipulate tables and other P4 objects defined in the program.
  • p4-hlir is the frontend compiler that translates P4 code into High-Level Intermediate Representation (HLIR), currently represented as a hierarchy of Python objects. It frees backend compiler developers from the burden of syntax analysis and target-independent semantic checks.

Reference Code

  • switch repository contains the switch.p4 sample P4 program along with all the library repos to manipulate the switch using SAI, SwitchAPI and switchlink. The switch.p4 program describes a data plane of an L2/L3 switch with many supported features.

Development and Test Infrastructure

  • p4factory repository maintains a sample set of the P4 programs and allows building P4 for the behavioral model.
  • ntf is a Network Test Framework that contains sample mininet and docker infrastructure required to perform network-wide tests of various applications on the behavioral model.
  • ptf is a Python based dataplane test framework based on unittest.

Forks of other projects

  • scapy-vxlan : Scapy with support for additional packet headers.
  • mininet : Mininet with p4.org enhancements

Tutorials

  • tutorials contains a set of example P4 programs.

How can one implement a simple L2 learning switch in P4?

MAC Learning requires interaction with the control plane and flooding involves the packet replication engine in the target. bmv2 has a simple example implementation of L2 learning switch. l2_switch.cpp implements the switch target, l2_switch.p4 implements the data plane, and learn_client.cpp implements the control plane.

Are there any P4 syntax highlighters for popular editors?

Several developers have shared different P4 syntax highlighters for VIM[1, 2], EMACS[1], sublime[1] and atom[1] on GitHub.

Where can I get more resources to learn P4?

P4 Languate Consortium’s official website p4.org and youtube channel have many useful materials.

Where can I get more help?

Please post on the P4 Developers mailing list. We’ll be glad to help!