创建拓扑
mininet@mininet-vm:~$ sudo mn --topo single,2 --mac --switch ovsk --controller remote
*** Creating network
*** Adding controller
Unable to contact the remote controller at 127.0.0.1:6653
Unable to contact the remote controller at 127.0.0.1:6633
Setting remote controller to 127.0.0.1:6653
*** Adding hosts:
h1 h2
*** Adding switches:
s1
*** Adding links:
(h1, s1) (h2, s1)
*** Configuring hosts
h1 h2
*** Starting controller
c0
*** Starting 1 switches
s1 ...
*** Starting CLI:
mininet>
主机h1的配置信息
mininet> h1 ifconfig
h1-eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:01
inet addr:10.0.0.1 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:252 (252.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:1324 errors:0 dropped:0 overruns:0 frame:0
TX packets:1324 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:356900 (356.9 KB) TX bytes:356900 (356.9 KB)
主机h2的配置信息
mininet> h2 ifconfig
h2-eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:02
inet addr:10.0.0.2 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:870 errors:0 dropped:0 overruns:0 frame:0
TX packets:870 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:243076 (243.0 KB) TX bytes:243076 (243.0 KB)
OpenFlow交换机s1的配置信息
mininet> s1 ifconfig
eth0 Link encap:Ethernet HWaddr 00:0c:29:30:5c:b4
inet addr:192.168.183.130 Bcast:192.168.183.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:140334 errors:0 dropped:0 overruns:0 frame:0
TX packets:141601 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:20557888 (20.5 MB) TX bytes:99569902 (99.5 MB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:210014 errors:0 dropped:0 overruns:0 frame:0
TX packets:210014 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:67842164 (67.8 MB) TX bytes:67842164 (67.8 MB)
s1 Link encap:Ethernet HWaddr 6e:a8:9d:0b:30:4d
UP BROADCAST RUNNING MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
s1-eth1 Link encap:Ethernet HWaddr 92:6e:67:11:c1:a2
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:252 (252.0 B) TX bytes:0 (0.0 B)
s1-eth2 Link encap:Ethernet HWaddr b6:72:95:2b:d2:a1
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
mininet>
使用dpctl来添加流表。dpctl是一个可以分配、修改和管理流表的功能指令。通过dpctl查看OpenFlow交换机上面的流表,此时是空的
mininet> dpctl dump-flows
*** s1 ------------------------------------------------------------------------
NXST_FLOW reply (xid=0x4):
mininet>
登录主机h1
mininet> xterm h1
在主机h1上面ping测试h2,结果ping不通,因为此时没有流表
root@mininet-vm:~# ifconfig
h1-eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:01
inet addr:10.0.0.1 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:654 errors:0 dropped:0 overruns:0 frame:0
TX packets:654 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:126696 (126.6 KB) TX bytes:126696 (126.6 KB)
root@mininet-vm:~# ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
From 10.0.0.1 icmp_seq=1 Destination Host Unreachable
From 10.0.0.1 icmp_seq=2 Destination Host Unreachable
From 10.0.0.1 icmp_seq=3 Destination Host Unreachable
^C
--- 10.0.0.2 ping statistics ---
5 packets transmitted, 0 received, +3 errors, 100% packet loss, time 401
pipe 3
root@mininet-vm:~# ^C
- 下发流表一:头字段in_port=1, 动作actions=output:2
端口1进来的流量从端口2转发 - 下发流表二:头字段in_port=2, 动作actions=output:1
端口2进来的流量从端口1转发
mininet> dpctl add-flow "idle_timeout=60,in_port=1,actions=output:2"
*** s1 ------------------------------------------------------------------------
mininet> dpctl add-flow "idle_timeout=60,in_port=2,actions=output:1"
*** s1 ------------------------------------------------------------------------
mininet> dpctl dump-flows
*** s1 ------------------------------------------------------------------------
NXST_FLOW reply (xid=0x4):
cookie=0x0, duration=5.425s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, idle_age=5, in_port=1 actions=output:2
cookie=0x0, duration=2.635s, table=0, n_packets=0, n_bytes=0, idle_timeout=60, idle_age=2, in_port=2 actions=output:1
mininet>
再次到主机h1上面ping测试,结果可以ping通
root@mininet-vm:~# ping 10.0.0.2
PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data.
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.655 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from 10.0.0.2: icmp_seq=4 ttl=64 time=0.066 ms
64 bytes from 10.0.0.2: icmp_seq=5 ttl=64 time=0.074 ms
64 bytes from 10.0.0.2: icmp_seq=6 ttl=64 time=0.074 ms
^C
--- 10.0.0.2 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 4999ms
rtt min/avg/max/mdev = 0.066/0.168/0.655/0.217 ms
root@mininet-vm:~#
下发流表的时候,下发了idle_timeout=60,当等待60s没有流量转发的话就删除流表
mininet> dpctl dump-flows
*** s1 ------------------------------------------------------------------------
NXST_FLOW reply (xid=0x4):
cookie=0x0, duration=120.604s, table=0, n_packets=21, n_bytes=1890, idle_timeout=60, idle_age=58, in_port=1 actions=output:2
cookie=0x0, duration=119.108s, table=0, n_packets=21, n_bytes=1890, idle_timeout=60, idle_age=58, in_port=2 actions=output:1
mininet> dpctl dump-flows
*** s1 ------------------------------------------------------------------------
NXST_FLOW reply (xid=0x4):
mininet>