CS344 – Lecture 2
P4 Language Overview
Copyright © 2019 – P4.org
What is Data Plane Programming?
• Why program the Data Plane?
Copyright © 2019 – P4.org 2
Software Defined Networking
• Main contributions Control Function (“App”) xm
◦ Match-Action abstraction
◦ Standardized protocol to interact with switch Northbound I/F
◦ Logically centralized control via a single entity
• Issues SDN
◦ Data-plane protocol evolution requires x1
Controller
changes to standards (12 à 40 OpenFlow
match-fields)
Southbound I/F
◦ Limited interoperability between vendors =>
southbound I/F differences handled at
controller
(OpenFlow / netconf / JSON / XML variants) xn
◦ Limited programmability
Copyright © 2019 – P4.org
Switch 3
A Better Approach: Top-down design
Switch OS
Network Demands
Feedback
Run-time API
Driver
P4
“This is how I want the
network to behave and how to
switch packets…”
(the user / controller
makes the rules)
P4 Programmable Device
Copyright © 2019 – P4.org 5
Benefits of Data Plane Programmability
• New Features – Add new protocols
• Reduce complexity – Remove unused protocols
• Efficient use of resources – flexible use of tables
• Greater visibility – New diagnostic techniques, telemetry, etc.
• SW style development – rapid design cycle, fast innovation, fix data
plane bugs in the field
• You keep your own ideas
Think programming rather than protocols…
Copyright © 2019 – P4.org 6
What can you do with P4?
• Layer 4 Load Balancer – SilkRoad[1]
• Low Latency Congestion Control – NDP[2]
• In-band Network Telemetry – INT[3]
• Fast In-Network cache for key-value stores – NetCache[4]
• Consensus at network speed – NetPaxos[5]
• Aggregation for MapReduce Applications [6]
• … and much more
[1] Miao, Rui, et al. "SilkRoad: Making Stateful Layer-4 Load Balancing Fast and Cheap Using Switching ASICs." SIGCOMM, 2017.
[2] Handley, Mark, et al. "Re-architecting datacenter networks and stacks for low latency and high performance.” SIGCOMM, 2017.
[3] Kim, Changhoon, et al. "In-band network telemetry via programmable dataplanes.” SIGCOMM. 2015.
[4] Xin Jin et al. “NetCache: Balancing Key-Value Stores with Fast In-Network Caching.” To appear at SOSP 2017
[5] Dang, Huynh Tu, et al. "NetPaxos: Consensus at network speed.” SIGCOMM, 2015.
[6] Sapio, Amedeo, et al. "In-Network Computation is a Dumb Idea Whose Time Has Come." Hot Topics in Networks. ACM, 2017.
Copyright © 2019 – P4.org 8
Brief History and Trivia
• May 2013: Initial idea and the name “P4”
• July 2014: First paper (SIGCOMM CCR)
• Aug 2014: First P414 Draft Specification (v0.9.8)
• Sep 2014: P414 Specification released (v1.0.0)
• Jan 2015: P414 v1.0.1
• Mar 2015: P414 v1.0.2
• Nov 2016: P414 v1.0.3
• May 2017: P414 v1.0.4
• Apr 2016: P416 – first commits
• Dec 2016: First P416 Draft Specification
• May 2017: P416 Specification released
Copyright © 2019 – P4.org 9
Standard Metadata in SimpleSumeSwitch Architecture
/* standard sume switch metadata */ • src_port/dst_port – one-hot
struct sume_metadata_t {
bit<16> dma_q_size; encoded, easy to do multicast
bit<16> nf3_q_size; • *_q_size – size of each output
bit<16> nf2_q_size;
bit<16> nf1_q_size; queue, measured in terms of 32-byte
bit<16> nf0_q_size; words, when packet starts being
// send digest_data to CPU
processed by the P4 program
bit<8> send_dig_to_cpu;
// ports are one-hot encoded • send_dig_to_cpu – to send
bit<8> dst_port; digest_data to control-plane
bit<8> src_port;
// pkt len is measured in bytes
bit<16> pkt_len;
}
Copyright © 2019 – P4.org 21
P4 Hello World (SimpleSumeSwitch)
#include <core.p4> /* DEPARSER */
#include <sume_switch.p4> control MyDeparser(packet_out packet,
/* HEADERS and METADATA */ in headers hdr,
struct headers { ... } in user_data_t user,
/* PARSER */ inout digest_data_t digest,
parser MyParser(packet_in packet, out headers hdr, ...) { inout sume_metadata_t smeta) {
state start { transition accept; } apply {}
} }
/* MATCH-ACTION PROCESSING */ /* SWITCH */
control MyIngress(..., inout sume_metadata_t smeta) { SimpleSumeSwitch(
MyParser(),
action set_dst_port(bit<8> port) { MyIngress(),
smeta.dst_port = port; MyDeparser()
} ) main;
table forward {
key = { smeta.src_port : exact; } forward table entries:
actions = {
set_dst_port; Key Action Name Action Data
NoAction;
} 1 set_dst_port 2
size = 1024;
default_action = NoAction(); 2 set_dst_port 1
}
apply { forward.apply(); }
}
Copyright © 2019 – P4.org 24
Running Example: Basic Forwarding
•We’ll use a simple application as a running example—a basic
router—to illustrate the main features of P416
•Basic router functionality:
◦Parse Ethernet and IPv4 headers from packet
◦Find destination in IPv4 routing table
◦Update source / destination MAC addresses
◦Decrement time-to-live (TTL) field & update IP checksum
◦Set the egress port
◦Deparse headers back into a packet
•We’ve written some starter code for you (basic.p4) and
implemented a static control plane
Copyright © 2019 – P4.org 25
P4 Types (Other Types)
/* Architecture */
struct sume_metadata_t { Other useful types
bit<16> dma_q_size;
bit<16> nf3_q_size; • Struct: Unordered collection of members (with no
bit<16> nf2_q_size;
bit<16> nf1_q_size;
bit<16> nf0_q_size;
alignment restrictions)
bit<8> send_dig_to_cpu;
bit<8> dst_port; • Header Stack: array of headers
bit<8> src_port;
bit<16> pkt_len; • Header Union: one of several headers
}
/* User program */
struct user_metadata {
...
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
}
Copyright © 2019 – P4.org 28