CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4?...

45
Copyright © 2019 – P4.org CS344 – Lecture 2 P4 Language Overview

Transcript of CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4?...

Page 1: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

CS344 – Lecture 2P4 Language Overview

Page 2: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

What is Data Plane Programming?• Why program the Data Plane?

2

Page 3: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Software Defined Networking

3

•Main contributions◦ Match-Action abstraction◦ Standardized protocol to interact with switch

◦ Logically centralized control via a single entity

• Issues◦ Data-plane protocol evolution requires

changes to standards (12 à 40 OpenFlowmatch-fields)

◦ Limited interoperability between vendors => southbound I/F differences handled at controller(OpenFlow / netconf / JSON / XML variants)

◦ Limited programmability

SDN

Controller

Switch

Southbound I/F

Northbound I/F

Control Function (“App”)

x n

x m

x 1

Page 4: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Switch OS

Run-time APIDriver

“This is how I know toprocess packets”

(i.e. the ASIC datasheetmakes the rules)

Fixed-function ASIC

Status Quo: Bottom-up design

4

?Network Demands

Page 5: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Switch OS

Run-time APIDriver

“This is how I want the network to behave and how to

switch packets…”(the user / controller

makes the rules)P4 Programmable Device

A Better Approach: Top-down design

5

Network Demands

P4

Feedback

Page 6: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

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…

6

Page 7: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Programmable Network Devices

• PISA: Flexible Match+Action ASICs◦ Intel Flexpipe, Cisco Doppler, Cavium (Xpliant), Barefoot Tofino, …

• NPU◦ EZchip, Netronome, …

• CPU◦ Open Vswitch, eBPF, DPDK, VPP…

• FPGA◦ Xilinx, Altera, …

These devices let us tell them how to process packets.

7

Page 8: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

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

8

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

Page 9: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Brief History and Trivia

9

• 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

Page 10: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P4 Data Plane Model

10

Page 11: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

PISA: Protocol-Independent Switch Architecture

11

ProgrammableParser

ProgrammableDeparser

Programmable Match-Action Pipeline

Programmer declares the headers that should be

recognized and their order in the packet

Programmer defines the tables and the exact processing algorithm

Programmer declares how the output packet

will look on the wire

Page 12: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

PISA in Action• Packet is parsed into individual headers (parsed representation)• Headers and intermediate results can be used for matching and actions• Headers can be modified, added or removed• Packet is deparsed (serialized)

12

Match+ActionStage (Unit)

ProgrammableParser

ProgrammableDeparser

Programmable Match-Action Pipeline

Page 13: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Mapping a Simple L3 Data Plane Program on PISA

13

Match+ActionStage (Unit)

ProgrammableParser

ProgrammableDeparser

Programmable Match-Action Pipeline

EthernetMAC

AddressTable

ACLRules

IPv4Address

Table

L2 ACL

IPv4

IPv6Address

Table

IPv6

Page 14: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Mapping a More Complex Data Plane Program on PISA

14

Match+ActionStage (Unit)

ProgrammableParser

ProgrammableDeparser

Programmable Match-Action Pipeline

EthernetMAC

AddressTable

ACLRules

IPv4Address

Table

L2 ACL

IPv4

IPv6Address

Table

IPv4Address

Table

MPLS Table

MPLS

IPv6

Page 15: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

•Define how the P4 programmable elements are composed•Define how to interact with non-programmable elements• P4 programs are written for a specific architecture

15

Custom

P4 Architectures

Page 16: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org 16

FPGAs Programmableswitch ASICs

Software switches

SimpleSumeSwitch ASICArchSWArch

IR IRIR

Community Developed

Vendor Developed

Architecture

Target

P4 Programs

P4 Approach

Page 17: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org 17

FPGAs Software switches

Portable Switch Architecture (PSA)

Programmableswitch ASICs

IR

CommunityDeveloped

VendorDeveloped

P4 Goal

Page 18: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Programming a P4 Target

18

P4 ArchitectureModel

P4 Compiler

Target-specific configuration

binaryData PlaneTables Extern

objectsLoad

TargetVendor supplied

P4 Program

User supplied

Control Plane

Add/removetable entries

CPU port

Packet-in/outExterncontrol

RU

NTI

ME

Page 19: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

SimpleSumeSwitch Architecture

19

Supported on:Ø NetFPGA SUME boardØ BMv2 in Mininet

TrafficManager

DeparserIngress Match-ActionParser

Page 20: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P416 Language Elements

20

Architecture Description

Extern Libraries

Programmable blocks and their interfaces

Support for specialized components

Data Types Bistrings, headers, structures, arrays

Controls Tables, Actions, control flow statements

Parsers

Expressions Basic operations and operators

State machine, bitfield extraction

Page 21: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* standard sume switch metadata */struct sume_metadata_t {

bit<16> dma_q_size;bit<16> nf3_q_size;bit<16> nf2_q_size;bit<16> nf1_q_size;bit<16> nf0_q_size;// send digest_data to CPUbit<8> send_dig_to_cpu;// ports are one-hot encodedbit<8> dst_port;bit<8> src_port;// pkt len is measured in bytesbit<16> pkt_len;

}

Standard Metadata in SimpleSumeSwitch Architecture

21

• src_port/dst_port – one-hot

encoded, easy to do multicast

• *_q_size – size of each output

queue, measured in terms of 32-byte

words, when packet starts being

processed by the P4 program

• send_dig_to_cpu – to send

digest_data to control-plane

Page 22: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P4 Program Template (SimpleSumeSwitch)

22

#include <core.p4>#include <sume_switch.p4>/* HEADERS and METADATA */struct user_data { ... }struct digest_data { ... }struct headers {ethernet_t ethernet;ipv4_t ipv4;

}/* PARSER */parser MyParser(packet_in packet,

out headers hdr,out user_data_t user,out digest_data_t digest,inout sume_metadata_t smeta) {

... }/* MATCH-ACTION PROCESSING */control MyIngress(inout headers hdr,

inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

... }

/* DEPARSER */control MyDeparser(packet_out packet,

in headers hdr,in user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

... }/* SWITCH */SimpleSumeSwitch(MyParser(),MyIngress(),MyDeparser()

) main;

Page 23: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org 23

P4 Hello World (SimpleSumeSwitch)#include <core.p4>#include <sume_switch.p4>/* HEADERS and METADATA */struct user_data { ... }struct digest_data { ... }struct headers { ... }/* PARSER */parser MyParser(packet_in packet,

out headers hdr,out user_data_t user,out digest_data_t digest,inout sume_metadata_t smeta) {

state start { transition accept; }}/* MATCH-ACTION PROCESSING */control MyIngress(inout headers hdr,

inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

apply {if (smeta.src_port == 1) {

smeta.dst_port = 2;} else if (smeta.src_port == 2) {

smeta.dst_port = 1;}

}}

/* DEPARSER */control MyDeparser(packet_out packet,

in headers hdr,in user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

apply {}}/* SWITCH */SimpleSumeSwitch(MyParser(),MyIngress(),MyDeparser()

) main;

Page 24: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org 24

P4 Hello World (SimpleSumeSwitch)#include <core.p4>#include <sume_switch.p4>/* HEADERS and METADATA */struct headers { ... }/* PARSER */parser MyParser(packet_in packet, out headers hdr, ...) {state start { transition accept; }}/* MATCH-ACTION PROCESSING */control MyIngress(..., inout sume_metadata_t smeta) {

action set_dst_port(bit<8> port) {smeta.dst_port = port;

}

table forward {key = { smeta.src_port : exact; }actions = {set_dst_port;NoAction;

}size = 1024;default_action = NoAction();

}

apply { forward.apply(); }}

/* DEPARSER */control MyDeparser(packet_out packet,

in headers hdr,in user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

apply {}}/* SWITCH */SimpleSumeSwitch(MyParser(),MyIngress(),MyDeparser()

) main;

Key Action Name Action Data

1 set_dst_port 2

2 set_dst_port 1

forward table entries:

Page 25: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

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

25

Page 26: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Basic Forwarding: Topology

26

s1

h1(10.0.1.1)

s3

s2

h2(10.0.2.2)

h3(10.0.3.3)

Page 27: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

typedef bit<48> macAddr_t;typedef bit<32> ip4Addr_t;header ethernet_t {macAddr_t dstAddr;macAddr_t srcAddr;bit<16> etherType;

}header ipv4_t {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;ip4Addr_t srcAddr;ip4Addr_t dstAddr;

}

P4 Types (Basic and Header Types)

27

Basic Types• bit<n>: Unsigned integer (bitstring) of size n• bit is the same as bit<1>• int<n>: Signed integer of size n (>=2)• varbit<n>: Variable-length bitstring

Header Types: Ordered collection of members• Can contain bit<n>, int<n>, and varbit<n>• Byte-aligned• Can be valid or invalid• Provides several operations to test and set validity bit:

isValid(), setValid(), and setInvalid()

Typedef: Alternative name for a type

Page 28: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* Architecture */struct sume_metadata_t {

bit<16> dma_q_size;bit<16> nf3_q_size;bit<16> nf2_q_size;bit<16> nf1_q_size;bit<16> nf0_q_size;bit<8> send_dig_to_cpu;bit<8> dst_port;bit<8> src_port;bit<16> pkt_len;

}

/* User program */struct user_metadata {...

}struct headers {ethernet_t ethernet;ipv4_t ipv4;

}

P4 Types (Other Types)

28

Other useful types• Struct: Unordered collection of members (with no

alignment restrictions)

• Header Stack: array of headers

• Header Union: one of several headers

Page 29: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P4 Parsers

• Parsers are functions that map packets into headers and metadata, written in a state machine style

• Every parser has three predefined states◦ start◦ accept◦ reject

• Other states may be defined by the programmer

• In each state, execute zero or more statements, and then transition to another state (loops are OK)

29

start

accept reject

Page 30: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* From core.p4 */extern packet_in {void extract<T>(out T hdr);void extract<T>(out T variableSizeHeader,

in bit<32> variableFieldSizeInBits);T lookahead<T>();void advance(in bit<32> sizeInBits);bit<32> length();

}/* User Program */parser MyParser(packet_in packet,

out headers hdr,out user_data_t user,out digest_data_t digest,inout sume_metadata_t smeta) {

state start {packet.extract(hdr.ethernet);transition accept;

}

}

Parsers (SimpleSumeSwitch)

30

MyParser

sume_metadata

hdr

digest

packet_in

user

Page 31: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

state start {transition parse_ethernet;

}

state parse_ethernet {packet.extract(hdr.ethernet);transition select(hdr.ethernet.etherType) {

0x800: parse_ipv4;default: accept;

}}

Select Statement

31

P416 has a select statement that can be used to branch in a parser

Similar to case statements in C or Java, but without “fall-through behavior”—i.e., break statements are not needed

In parsers it is often necessary to branch based on some of the bits just parsed

For example, etherType determines the format of the rest of the packet

Match patterns can either be literals or simple computations such as masks

Page 32: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Coding Break

32

Page 33: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P4 Controls

• Similar to C functions (without loops)

• Can declare variables, create tables, instantiate externs, etc.

• Functionality specified by code in apply statement

• Represent all kinds of processing that are expressible as DAG:

◦ Match-Action Pipelines◦ Deparsers◦ Additional forms of packet processing (updating checksums)

• Interfaces with other blocks are governed by user- and

architecture-specified types (typically headers and metadata)

33

Page 34: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

control MyIngress(inout headers hdr,inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

bit<48> tmp;apply {tmp = hdr.ethernet.dstAddr;hdr.ethernet.dstAddr = hdr.ethernet.srcAddr;hdr.ethernet.srcAddr = tmp;smeta.dst_port = smeta.src_port;

}}

Example: Reflector (V1Model)Desired Behavior:

• Swap source and destination MAC addresses

• Bounce the packet back out on the physical port that it came into the switch on

Page 35: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

control MyIngress(inout headers hdr,inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

action swap_mac(inout bit<48> src, inout bit<48> dst) {

bit<48> tmp = src;src = dst;dst = tmp;

}

apply {swap_mac(hdr.ethernet.srcAddr,

hdr.ethernet.dstAddr);smeta.dst_port = smeta.src_port;

}}

Example: Simple Actions• Very similar to C functions• Can be declared inside a control or

globally• Parameters have type and direction• Variables can be instantiated inside• Many standard arithmetic and

logical operations are supported◦ +, -, *◦ ~, &, |, ^, >>, <<◦ ==, !=, >, >=, <, <=◦ No division/modulo

• Non-standard operations:◦ Bit-slicing: [m:l] (works as l-value too)◦ Bit Concatenation: ++

Page 36: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

P4 Tables

• The fundamental unit of a Match-Action Pipeline◦ Specifies what data to match on and match kind◦ Specifies a list of possible actions◦ Optionally specifies a number of table properties

■ Size■ Default action■ Static entries■ etc.

• Each table contains one or more entries (rules)• An entry contains:◦ A specific key to match on◦ A single action that is executed when a packet matches the entry◦ Action data (possibly empty)

36

Page 37: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Directional

(DataPlane)

Parameters

Dire

ctio

nles

s (A

ctio

n D

ata)

Pa

ram

eter

s

Action Code

Action Execution

Tables: Match-Action Processing

Lookup Key

Key Action ID Action Data

Default Action ID

Default Action Data

Hit

Control Plane

Data

Headers and Metadata

(Input)

Headers and Metadata(Output)

Action ID

ID

Hit/M

iss Selector

Headers and Metadata

Page 38: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Example: IPv4_LPM Table

• Data Plane (P4) Program◦ Defines the format of the table

■ Key Fields■ Actions■ Action Data

◦ Performs the lookup◦ Executes the chosen action

• Control Plane (IP stack, Routing protocols)◦ Populates table entries with

specific information■ Based on the configuration■ Based on automatic discovery■ Based on protocol calculations

38

Key Action Action Data10.0.1.1/32 ipv4_forward dstAddr=00:00:00:00:01:01

port=110.0.1.2/32 drop*` NoAction

10.0.1.1 10.0.1.2

Page 39: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

table ipv4_lpm {key = {

hdr.ipv4.dstAddr: lpm;}actions = {

ipv4_forward;drop;NoAction;

}size = 1024;default_action = NoAction();

}

IPv4_LPM Table

39

Page 40: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* core.p4 */match_kind {

exact, ternary,lpm

}

/* v1model.p4 */match_kind {

range,selector

}

/* Some other architecture */match_kind {

regexp,fuzzy

}

Match Kinds

40

• The type match_kind is special in P4

• The standard library (core.p4) defines three standard match kinds◦ Exact match◦ Ternary match◦ LPM match

• The architecture (v1model.p4) defines two additional match kinds:◦ range◦ selector

• Other architectures may define (and provide implementation for) additional match kinds

Page 41: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* core.p4 */action NoAction() { }

/* basic.p4 */action drop() {

mark_to_drop();}

/* basic.p4 */action ipv4_forward(macAddr_t dstAddr,

bit<9> port) {...

}

Defining Actions for L3 forwarding

41

• Actions can have two different types of parameters◦ Directional (from the Data Plane)◦ Directionless (from the Control

Plane)• Actions that are called directly:

◦ Only use directional parameters• Actions used in tables:

◦ Typically use directionless parameters

◦ May sometimes use directional parameters too

Directional

(DataPlane)

Parameters

Dire

ctio

nles

s (A

ctio

n D

ata)

Pa

ram

eter

s

Action Code

Action Execution

Page 42: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

control MyIngress(inout headers hdr,inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta) {

table ipv4_lpm { ...

}apply {

...ipv4_lpm.apply();...

}}

Applying Tables in Controls

42

Page 43: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

/* From core.p4 */

extern packet_out {

void emit<T>(in T hdr);

}

/* User Program */

control MyDeparser(packet_out packet,

in headers hdr,

in user_data_t user,

inout digest_data_t digest,inout sume_metadata_t smeta)

{

apply {

...

packet.emit(hdr.ethernet);

...

}

}

P4 Deparsing

43

•Assembles the headers back into a well-formed packet

•Expressed as a control function◦ No need for another construct!

•packet_out extern is defined in core.p4: emit(hdr): serializes header if it is valid

•Advantages:• Makes deparsing explicit...

...but decouples from parsing

Page 44: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

// special SUME hash functionextern void sume_hash(in bit<64> data,

out bit<8> result);

control MyIngress(inout headers hdr,inout user_data_t user,inout digest_data_t digest,inout sume_metadata_t smeta)

{

apply {bit<8> flowID;sume_hash(hdr.ip.src++hdr.ip.dst, flowID);...

}}

P4 Externs

44

• Black boxes for P4 programs

• Functionality is not described in P4

• Used to perform device/vendor specific functionality

• Can be stateless of stateful

• Can be accessed by the control-plane

• Set of supported externs is defined by architecture

Page 45: CS344 –Lecture 2 P4 Language Overview · Copyright © 2019 –P4.org What can you do with P4? •Layer 4 Load Balancer –SilkRoad[1] •Low Latency Congestion Control –NDP[2]

Copyright © 2019 – P4.org

Fin!

45