# 網路模擬-多台Switch設定
###### tags: `P4`、`Mininet`
----------------------------------------
Question
===
- In this environment, we need to h1、h2 can ping successfully, 2 hosts between 2 swtiches (s1、s2), what should we do that h1 can ping h2?
Environment
===

Codes
===
this is a simply physical forwarding codes, including basic.p4、cmd.txt and p4app.json :
> basic.p4 ↓
```
/* -*- 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 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;
```
> cmd.txt ↓
```
table_add phy_forward forward 1 => 2
table_add phy_forward forward 2 => 1
```
> p4app.json ↓
```
{
"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"
}
}
}
}
```
this code is use simple physical forward to connect , and this environment is only have h1、h2、s1 , so we need to add some code , like p4app.json. now this is a new version code by p4app.json. this version code is modify to 2 hosts and 2 switches.
> p4app.json (new version) ↓
```
{
"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": {
"program": "basic.p4"
},
"s2": {
"program": "basic.p4"
}
}
}
}
```
> As we can see, the code change one rule by "topology", and add 1 switch by "links" and "switches".
> "l2"(layer 2) means when we do ping test, it would setting same LAN in this environment.
Test
===
> use "p4run" to connect the p4 program, and ping h1、h2 first, we can see 2 hosts not working.

>
>
> we open 2 terminal one by s1, and use "simple_switch_CLI --thrift-port 9090" to connect s1 port.

> another is for s2, is the same way to connect,but s2 port number is 9091

> we can check this information to know s1、s2 port number.

> in s1、s2, if we don't know the command, use "help" to check information.

> use "table_add phy_forward forward 1 => 2" and "table_add phy_forward forward 2 => 1", this command is add rule to s1、s2.
>
> if you don't understand why this command can ping work, use "help [table name], like help table_add", it would show detail information for you.

> now you can do this command "h1 ping h2" on p4 and check hosts can work.

we also can write a shell to do this test.


is work!
Reference
===
[p4-basic](http://csie.nqu.edu.tw/smallko/sdn/p4utils-basic.htm)
---
###### 2020-05-25
###### written by **yi-hao tu**