2016 NCTU P4 Workshop

Post on 07-Jan-2017

1.190 views 0 download

Transcript of 2016 NCTU P4 Workshop

NCTU P4 WorkshopTseng Yi

NCTU W2CNLabhttps://takeshi.tw/tag/p4/

Outline• Introduction• Architecture• Header and Parser• Action and Table• Control flow• Register, Metadata, Counter and Meter• Getting start

Introduction

• Programming Protocol-Independent Packet Processors.

• Describe how to handle a packet for a target.

• White box in white box.

Introduction• Protocol Independent

• P4 programs specify how a switch processes packets.

• Target Independent• P4 is suitable for describing everything from

high- performance forwarding ASICs to software switches.

• Field Reconfigurable• P4 allows network engineers to change the way

their switches process packets after they are deployed.

P4 is not• SDN Software Switch• OpenFlow or Protocol• Network abstraction• Won’t compile to OpenFlow or any

southbound message.

P4 canBut OpenFlow Switch can’t

• Parse or modify L5~ header (e.g. inner ethernet header from VXLAN, DNS query data, DHCP header…)

• Define new protocol parser• Stateful switch (need newest version of OvS or

modified OF switch)• Flexible match field and table size of any table.• Define new actions for tables.

ArchitectureHeadersParsers

ControlProgram

TableConfig

PacketInput Parser Tables Tables

Queuesand/orBuffers

Ingress Egress

Deployment host

P4 Target

How to write P4?

1. Define headers and parsers (parser graph)

2. Define actions, match fields for table.

3. Design a control flow for your target.

P4 spec v1.0.2http://p4.org/wp-content/uploads/2015/04/p4-

latest.pdf

Header• Like “struct” from C/C++, but more

flexible.header_type eth_t { fields { dst : 48; src : 48; ethType : 16; }}header eth_t eth;

Parser• Parse(extract) a packet step by step.• Eth ————> IPv4 ———>TCP

parser parser_eth { extract(eth); return select(eth.type) { 0x800: parser_ipv4; default: ingress; }}

parser parser_ipv4 { extract(ipv4); return select(latest.proto) { 6: parser_tcp; default: ingress; }}

type 0x0800 proto 6

Actions

• Like a function(but no return value).• In one function, you can use one or more

P4 API (e.g. modify_field, add_header…)• Can be executed in parallel (depends on

implementation of target)

action set_dst_mac_and_output(new_mac, outport) { modify_field(eth.dst, new_mac); modify_field(standard_metadata.egress_spec, outport)}

• For example, if we want to set destination mac address and output port.

Actions

Table• Every table might contains different match field and actions.• Each table might have different features• Not just P4, Some vendors slice tables for different purpose, for

example: OFDPA from Broadcom

Table definitiontable first_table { reads { ipv4.dst : lpm; // exact, lpm, ternary, range, valid }

actions { drop; set_dst_mac_and_output; } size 1024;}

Add one Flow Entry• Currently, ways to control a P4 target

(bmv2):• Use runtime command line interface• ONOS test app for bmv2

p4cli> table_add first_table set_dst_mac_and_output 10.0.0.0/24 => 00:00:00:00:00:01 1

Control flow• Also like a function, but no argument or return

value• Main control flow: ingress and egress• In control flow, you can:

• apply packet to specific tables• go to other control flows

• When ingress ends, data will be sent to queue or buffer, then handle by egress control flow.

Control flow• Ingress:

• Modify state (register)• Modify packet• Modify metadata• Modify egress_spec (e.g. queue, output port)

• Egress:• Modify packet

Control Flowcontrol ingress { apply(in_port); apply(vlan); apply(termination_mac): if(valid(ipv4)) { apply(l3_flow); } apply(unicast); apply(multicast); apply(bridging); apply(acl);}

Register & MetadataCounter & Meter

Register, Metadata• Register

• Like global variable, store data • Can be use for stateful dataplane design

• Metadata• Like local variable, reset after one control flow

ended.• If we need to use register, we need to load

register to metadata.

Counter• Counter

• Count bytes or packets• Update when table match or action call• Fixed size, will stop counting or reset to

zero (depends on program)

Meter

• Like counter, but it monitoring packet rate, not packet/byte count.

Getting start

Getting start• Basic knowledge:

• Linux shell (network & system commands)

• Linux basic tools (git, tmux…)• GNU compiler toolchain (for bmv2)• Python & C/C++• FSM, data structure, network

Getting start• Setup env:

• bmv2• https://github.com/p4lang/behavioral-model

• p4c-bm• https://github.com/p4lang/p4c-bm

• editor plugins (optional)• https://github.com/TakeshiTseng/atom-language-p4• https://github.com/TakeshiTseng/vim-language-p4

Workflow( bmv2)• Write P4 program• Generate json file by using p4c-bmv2• Use json to start a bmv2 target (e.g.

simple_switch)

Use mininet

• from p4_mininet import P4Switch, P4Host• Setup cls parameter for addSwitch and

addHost.

Use mininet• net.addSwitch('s1', cls=P4Switch,

sw_path=SW_PATH, json_path=JSON_PATH, thrift_port=9091)• sw_path: bmv2 target path• json_path: json file generated by p4c-bm• thrift_port: port number for runtime API

P4 thrift API

• Connect bmv2 target and runtime CLI or Conroller (e.g. ONOS)

• You can use runtime_CLI.py from bmv2 repository.

Quick Demohttps://github.com/TakeshiTseng/2016-nctu-p4-

workshop

Quick Demo

• Goal:• Use new protocol instead of ethernet.• Path routing.• Setup by runtime CLI.

Normal packet With path header

src (16 bit)

dst (16 bit)

payload normal packet

path (16 bit)

preamble (24 bit)

start

Ingress control

pkt[0:24] != 0xc0ffee

pkt[0:24] == 0xc0ffee

my_path_header

my_header

Parser

apply “forward”

apply “path_look_up”

Egress

path is not valid

path header is valid

Demo topologyP4

Switch 1

Host 3 Host 4

P4Switch

2

P4Switch

3

P4Switch

4

Host 1 Host 2

Number of path : P(4, 2) = 12

00 02 02

00 02 02

1

1

1

1

Quick Demo

Software Defined Networking Developer Society

• http://sdnds.tw• https://www.facebook.com/groups/

sdnds.tw

Thanks!Question?