4. 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.
5. P4 is not
• SDN Software Switch
• OpenFlow or Protocol
• Network abstraction
• Won’t compile to OpenFlow or any southbound
message.
5
6. P4 can
But 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.
6
8. 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.
8
10. Header
• Like “struct” from C/C++, but more flexible.
header_type eth_t {
fields {
dst : 48;
src : 48;
ethType : 16;
}
}
header eth_t eth;
10
11. 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;
}
}
11
type 0x0800 proto 6
12. 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)
12
13. 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
13
14. 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
16. 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
17. 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.
21. 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.
22. 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)
27. Workflow(bmv2)
• Write P4 program
• Generate json file by using p4c-bmv2
• Use json to start a bmv2 target (e.g.
simple_switch)
28. Use mininet
• from p4_mininet import P4Switch, P4Host
• Setup cls parameter for addSwitch and addHost.
29. 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
30. P4 thrift API
• Connect bmv2 target and runtime CLI or
Conroller (e.g. ONOS)
• You can use runtime_CLI.py from bmv2
repository.