Free tech learning for everyone, from basics to pro

SDNEmerging Technologies and SecurityAdvanced

Programmable data planes and the P4 language

In short: P4 is a language for telling a programmable switch or network card how to process packets: which headers to parse, which tables to match and what actions to take. It goes further than OpenFlow, which only fills fixed tables. P4 enables custom protocols, detailed telemetry and fast in-network security checks.

From fixed-function to programmable

Most switch chips are fixed-function: the vendor decides which headers they understand and what they can do. If you need support for a new protocol, you wait for new hardware. SDN with OpenFlow moved the decisions to a controller, but the switch still only understood a fixed list of header fields. A programmable data plane goes one step further: the forwarding behaviour itself is software. P4 (Programming Protocol-independent Packet Processors) is the open language most widely used to describe that behaviour. It is maintained as an open-source project hosted by the Linux Foundation.

P4 pipeline (PISA)ParserextractheadersIngressmatch-actiontablesTrafficmanagerqueues,buffersEgressmatch-actiontablesDeparserrebuild packet
Figure 1. In a programmable pipeline, the parser, tables and deparser are all defined by your P4 program.

How a P4 program is structured

P4 targets a model often called PISA (Protocol Independent Switch Architecture). A program defines:

  • Headers: the layout of each header you care about, such as Ethernet, IPv4 or a custom tag.
  • Parser: a small state machine that reads the packet and extracts those headers.
  • Match-action tables: each table matches on fields (exact, longest prefix or ternary) and runs an action such as forward, drop, rewrite or count.
  • Control blocks: the order in which tables are applied in ingress and egress.
  • Deparser: puts the possibly modified headers back on the packet.

A simplified IPv4 forwarding table in P4_16 looks like this:

table ipv4_lpm {
    key = { hdr.ipv4.dstAddr : lpm; }
    actions = { ipv4_forward; drop; }
    size = 1024;
    default_action = drop();
}

The program defines the table; the entries (which prefixes go where) are added at runtime by a controller through P4Runtime, a gRPC-based API. So P4 still fits the SDN model, with a cleaner split between what the switch can do and what it is told to do.

P4 compared with OpenFlow

OpenFlow versus P4OpenFlowFixed set of headersController fills tablesSwitch defines behaviourRuntime onlyP4Any headers you defineProgram defines tablesYou define behaviourCompile time and runtime
Figure 2. OpenFlow programs the contents of fixed tables; P4 programs what the tables and parser are.
QuestionOpenFlowP4
Can you add a new header?Only if the standard and the switch support itYes, you define it in the program
Who defines the tables?The switch vendorThe P4 programmer
Runtime APIOpenFlow messagesP4Runtime (gRPC)
TargetsOpenFlow switches, Open vSwitchProgrammable ASICs, SmartNICs, FPGAs, software switches

The two can work together: you could write a P4 program that behaves like an OpenFlow switch. P4 targets include the open-source BMv2 software switch for learning, some SmartNICs and Data Processing Units (DPUs), and FPGAs. The market for programmable switch chips has changed over time, so check current vendor support before planning a production design.

Uses in operations and security

  • In-band Network Telemetry (INT): switches add queue depth and timing data to packets as they pass, giving per-packet visibility that sampling tools miss.
  • Line-rate filtering: drop traffic that matches an attack signature, or rate-limit a source, without sending packets to a server.
  • DDoS detection: count flows in data plane registers and flag sudden spikes.
  • Custom protocols: research and cloud providers use P4 to test new encapsulations and load balancing ideas.

Programmable data planes also bring new risks. A bug in the P4 program can mis-forward or leak traffic, so programs need code review, testing and version control just like any other software. P4Runtime access must be authenticated and encrypted, because it can rewrite every table.

Tip: to learn P4 safely, use the official P4 tutorials with BMv2 and Mininet in a virtual machine. Everything runs in software on your own laptop.

Practice

What part of a P4 program extracts headers from a packet?
The parser, which is a state machine that reads the packet and fills in header structures.
How do table entries get into a P4 switch at runtime?
A controller installs them through P4Runtime, a gRPC-based API; the P4 program only defines the tables and actions.
Give one security use of programmable data planes.
Examples: dropping or rate-limiting attack traffic at line rate, counting flows to detect DDoS, or adding in-band telemetry for visibility.

Key takeaways

  • P4 describes how a programmable target parses and processes packets.
  • A program defines headers, parser, match-action tables, control flow and deparser.
  • Table entries are installed at runtime through P4Runtime.
  • OpenFlow fills fixed tables; P4 defines the tables themselves.
  • Uses include in-band telemetry and line-rate filtering; treat P4 code as critical software.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.