# 網路模擬與分析(5/10): P4 ###### tags: `Mininet`、`P4` --- **--------------目前為純 code 及 result,詳解部分之後更新--------------** --- ## Physical Layer Forwarding ![](https://i.imgur.com/S9RmQSr.png) p4-utils 框架需要: `basic.p4`、`p4app.json`和`cmd.txt` ``` /* -*- P4_16 -*- */ #include <core.p4> #include <v1model.p4> /************************************************************************* *********************** H E A D E R S *********************************** *************************************************************************/ struct metadata { /* empty */ } struct headers { } /************************************************************************* *********************** P A R S E R *********************************** *************************************************************************/ parser MyParser(packet_in packet, out headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { state start { transition accept; } } /************************************************************************* ************ C H E C K S U M V E R I F I C A T I O N ************* *************************************************************************/ control MyVerifyChecksum(inout headers hdr, inout metadata meta) { apply { } } /************************************************************************* ************** I N G R E S S P R O C E S S I N G ******************* *************************************************************************/ control MyIngress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { action drop() { mark_to_drop(standard_metadata); } action forward(bit<9> port) { standard_metadata.egress_spec = port; } # 定義table table phy_forward { key = { standard_metadata.ingress_port: exact; } actions = { forward; drop; } size = 1024; default_action = drop(); } apply { phy_forward.apply(); } } /************************************************************************* **************** E G R E S S P R O C E S S I N G ******************* *************************************************************************/ control MyEgress(inout headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { apply { } } /************************************************************************* ************* C H E C K S U M C O M P U T A T I O N ************** *************************************************************************/ control MyComputeChecksum(inout headers hdr, inout metadata meta) { apply { } } /************************************************************************* *********************** D E P A R S E R ******************************* *************************************************************************/ control MyDeparser(packet_out packet, in headers hdr) { apply { } } /************************************************************************* *********************** S W I T C H ******************************* *************************************************************************/ V1Switch( MyParser(), MyVerifyChecksum(), MyIngress(), MyEgress(), MyComputeChecksum(), MyDeparser() ) main; ``` ``` { "program": "basic.p4", "switch": "simple_switch", "compiler": "p4c", "options": "--target bmv2 --arch v1model --std p4-16", "switch_cli": "simple_switch_CLI", "cli": true, "pcap_dump": true, "enable_log": true, "topo_module": { "file_path": "", "module_name": "p4utils.mininetlib.apptopo", "object_name": "AppTopoStrategies" }, "controller_module": null, "topodb_module": { "file_path": "", "module_name": "p4utils.utils.topology", "object_name": "Topology" }, "mininet_module": { "file_path": "", "module_name": "p4utils.mininetlib.p4net", "object_name": "P4Mininet" }, "topology": { "links": [["h1", "s1"], ["h2", "s1"]], "hosts": { "h1": { }, "h2": { } }, "switches": { "s1": { "cli_input": "cmd.txt", "program": "basic.p4" } } } } ``` ``` # 執行動作/表格名稱/執行動作/匹配 => 執行動作 table_add phy_forward forward 1 => 2 table_add phy_forward forward 2 => 1 ``` 執行`p4run` h1 ping h2 ![](https://i.imgur.com/sAaxx2L.png) 開啟另一台終端機使用`simple_switch_CLI --thrift-port 9090` 進入後使用`table_info phy_forward`去查看所有資訊 使用`table_dump phy_forward`查看規則表 --- ### Physical Forwaring 延伸 `gedit p4app.json basic.p4` ``` { "program": "basic.p4", "switch": "simple_switch", "compiler": "p4c", "options": "--target bmv2 --arch v1model --std p4-16", "switch_cli": "simple_switch_CLI", "cli": true, "pcap_dump": true, "enable_log": true, "topo_module": { "file_path": "", "module_name": "p4utils.mininetlib.apptopo", "object_name": "AppTopoStrategies" }, "controller_module": null, "topodb_module": { "file_path": "", "module_name": "p4utils.utils.topology", "object_name": "Topology" }, "mininet_module": { "file_path": "", "module_name": "p4utils.mininetlib.p4net", "object_name": "P4Mininet" }, "topology": { "assignment_strategy": "l2", "links": [["h1", "s1"], ["h2", "s2"], ["s1", "s2"]], "hosts": { "h1": { }, "h2": { } }, "switches": { "s1": { "cli_input":"cmd1.txt", "program": "basic.p4" }, "s2": { "cli_input":"cmd2.txt", "program": "basic.p4" } } } } ``` 使用`p4run` 進入後使用`h1 ping h2 -c 5` --- ## Reference 1. https://kknews.cc/zh-tw/tech/jkxkk4q.html 2. https://dl.acm.org/doi/10.1145/2656877.2656890 3. https://www.youtube.com/watch?v=euDS8MHaW3I&feature=emb_title 4. https://www.youtube.com/watch?v=QbTboYkIBjE 5. https://github.com/nsg-ethz/p4-utils 6. http://csie.nqu.edu.tw/smallko/sdn/p4utils-basic.htm 7. http://csie.nqu.edu.tw/smallko/sdn/p4_forwarding.htm