Download - 2016 NCTU P4 Workshop
![Page 1: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/1.jpg)
NCTU P4 WorkshopTseng Yi
NCTU W2CNLabhttps://takeshi.tw/tag/p4/
![Page 2: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/2.jpg)
Outline• Introduction• Architecture• Header and Parser• Action and Table• Control flow• Register, Metadata, Counter and Meter• Getting start
![Page 3: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/3.jpg)
Introduction
• Programming Protocol-Independent Packet Processors.
• Describe how to handle a packet for a target.
• White box in white box.
![Page 4: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/4.jpg)
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.
![Page 5: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/5.jpg)
P4 is not• SDN Software Switch• OpenFlow or Protocol• Network abstraction• Won’t compile to OpenFlow or any
southbound message.
![Page 6: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/6.jpg)
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.
![Page 7: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/7.jpg)
ArchitectureHeadersParsers
ControlProgram
TableConfig
PacketInput Parser Tables Tables
Queuesand/orBuffers
Ingress Egress
Deployment host
P4 Target
![Page 8: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/8.jpg)
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.
![Page 9: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/9.jpg)
P4 spec v1.0.2http://p4.org/wp-content/uploads/2015/04/p4-
latest.pdf
![Page 10: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/10.jpg)
Header• Like “struct” from C/C++, but more
flexible.header_type eth_t { fields { dst : 48; src : 48; ethType : 16; }}header eth_t eth;
![Page 11: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/11.jpg)
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
![Page 12: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/12.jpg)
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)
![Page 13: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/13.jpg)
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
![Page 14: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/14.jpg)
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
![Page 15: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/15.jpg)
Table definitiontable first_table { reads { ipv4.dst : lpm; // exact, lpm, ternary, range, valid }
actions { drop; set_dst_mac_and_output; } size 1024;}
![Page 16: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/16.jpg)
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
![Page 17: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/17.jpg)
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.
![Page 18: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/18.jpg)
Control flow• Ingress:
• Modify state (register)• Modify packet• Modify metadata• Modify egress_spec (e.g. queue, output port)
• Egress:• Modify packet
![Page 19: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/19.jpg)
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);}
![Page 20: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/20.jpg)
Register & MetadataCounter & Meter
![Page 21: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/21.jpg)
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.
![Page 22: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/22.jpg)
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)
![Page 23: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/23.jpg)
Meter
• Like counter, but it monitoring packet rate, not packet/byte count.
![Page 24: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/24.jpg)
Getting start
![Page 25: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/25.jpg)
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
![Page 26: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/26.jpg)
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
![Page 27: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/27.jpg)
Workflow( bmv2)• Write P4 program• Generate json file by using p4c-bmv2• Use json to start a bmv2 target (e.g.
simple_switch)
![Page 28: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/28.jpg)
Use mininet
• from p4_mininet import P4Switch, P4Host• Setup cls parameter for addSwitch and
addHost.
![Page 29: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/29.jpg)
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
![Page 30: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/30.jpg)
P4 thrift API
• Connect bmv2 target and runtime CLI or Conroller (e.g. ONOS)
• You can use runtime_CLI.py from bmv2 repository.
![Page 31: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/31.jpg)
Quick Demohttps://github.com/TakeshiTseng/2016-nctu-p4-
workshop
![Page 32: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/32.jpg)
Quick Demo
• Goal:• Use new protocol instead of ethernet.• Path routing.• Setup by runtime CLI.
![Page 33: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/33.jpg)
Normal packet With path header
src (16 bit)
dst (16 bit)
payload normal packet
path (16 bit)
preamble (24 bit)
![Page 34: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/34.jpg)
start
Ingress control
pkt[0:24] != 0xc0ffee
pkt[0:24] == 0xc0ffee
my_path_header
my_header
![Page 35: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/35.jpg)
Parser
apply “forward”
apply “path_look_up”
Egress
path is not valid
path header is valid
![Page 36: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/36.jpg)
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
![Page 37: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/37.jpg)
Quick Demo
![Page 38: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/38.jpg)
Software Defined Networking Developer Society
• http://sdnds.tw• https://www.facebook.com/groups/
sdnds.tw
![Page 39: 2016 NCTU P4 Workshop](https://reader034.vdocuments.mx/reader034/viewer/2022050806/586fe7c51a28ab92198b4639/html5/thumbnails/39.jpg)
Thanks!Question?