20210412 Openvswitch ( 一 )

課堂資料

課堂練習

Openvswitch

mkdir ovs-test
cd ovs-test

lab1

mkdir lab1
cd lab1
gedit lab1.py &
mn
h1 ping h2 -c 5
exit
mn --controller=remote
h1 ping h2
h1 arp -n
h1 ping h2 -c 5
h1 arp -n
h2 arp -n
h1 ping h2 -c 5
python lab1.py

Ruler S1

ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=10.0.0.1,arp_tpa=10.0.0.2,actions=output:2
ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,actions=output:1
ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=10.0.0.1,arp_tpa=10.0.0.2,actions=output:2
ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,actions=output:1
ovs-ofctl add-flow s1 icmp,nw_src=10.0.0.1,nw_dst=10.0.0.2,icmp_type=8,icmp_code=0,actions=output:3
ovs-ofctl add-flow s1 icmp,nw_src=10.0.0.2,nw_dst=10.0.0.1,icmp_type=0,icmp_code=0,actions=output:1
ovs-ofctl dump-flows s1

Ruler S2

ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=10.0.0.1,arp_tpa=10.0.0.2,actions=output:3
ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,actions=output:1
ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=10.0.0.1,arp_tpa=10.0.0.2,actions=output:3
ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=10.0.0.2,arp_tpa=10.0.0.1,actions=output:1
ovs-ofctl add-flow s2 icmp,nw_src=10.0.0.1,nw_dst=10.0.0.2,icmp_type=8,icmp_code=0,actions=output:3
ovs-ofctl add-flow s2 icmp,nw_src=10.0.0.2,nw_dst=10.0.0.1,icmp_type=0,icmp_code=0,actions=output:2
ovs-ofctl dump-flows s2

Ruler S3

ovs-ofctl add-flow s3 icmp,nw_src=10.0.0.1,nw_dst=10.0.0.2,icmp_type=8,icmp_code=0,actions=output:2
ovs-ofctl dump-flows s3
xterm s3 s2
wireshark

s3-eth1

h1 ping h2 -c 5
h1 ping s2
h1 ping h2 -c 5
ovs-ofctl dump-flows s2
ovs-ofctl del-flows s2 icmp,nw_src=10.0.0.2,nw_dst=10.0.0.1,icmp_type=0,icmp_code=0
ovs-ofctl add-flow s2 icmp,nw_src=10.0.0.2,nw_dst=10.0.0.1,icmp_type=0,icmp_code=0,actions=output:1
ovs-ofctl dump-flows s2
h1 ping h2 -c 5
cd ..
mkdir lab2
cd lab2

lab2

gedit lab2.py

lab2.py

#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink,Intf
from mininet.node import Controller,RemoteController
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1', ip="10.0.0.1/24", mac="00:00:00:00:00:01")
  h2 = net.addHost('h2', ip="10.0.0.2/24", mac="00:00:00:00:00:02")
  h3 = net.addHost('h3', ip="10.0.0.3/24", mac="00:00:00:00:00:03")
  s1 = net.addSwitch('s1')
  c0 = net.addController('c0', controller=RemoteController)
  net.addLink(h1, s1)
  net.addLink(h2, s1)
  net.addLink(h3, s1)
  net.build()
  c0.start()
  s1.start([c0])
  h1.cmd("arp -s 10.0.0.2 00:00:00:00:00:02")
  h1.cmd("arp -s 10.0.0.3 00:00:00:00:00:03")
  h2.cmd("arp -s 10.0.0.1 00:00:00:00:00:01")
  h2.cmd("arp -s 10.0.0.3 00:00:00:00:00:03")
  h3.cmd("arp -s 10.0.0.1 00:00:00:00:00:01")
  h3.cmd("arp -s 10.0.0.2 00:00:00:00:00:02")
  #rules for s1
  #ovs-ofctl add-flow s1 priority=1,ip,nw_dst=10.0.0.1,actions=output:1
  #ovs-ofctl add-flow s1 priority=1,ip,nw_dst=10.0.0.2,actions=output:2
  #ovs-ofctl add-flow s1 priority=1,ip,nw_dst=10.0.0.3,actions=output:3
  #ovs-ofctl add-flow s1 priority=10,udp,nw_dst=10.0.0.2,actions=output:2,mod_dl_dst=00:00:00:00:00:03,mod_nw_dst=10.0.0.3,output:3
  CLI(net)
  net.stop()
python lab2.py
xterm h1 h2 h3
iperf -s -i 1 -u
pingall
ovs-ofctl dump-flows s1
ovs-ofctl show s1
ovs-ofctl add-flow s1 priority=10,ip,nw_src=10.0.0.1,nw_dst=10.0.0.2,actions=output:2,mod_nw_dst=10.0.0.3,output:3
ovs-ofctl dump-flows s1
ovs-ofctl add-flow s1 priority=100,ip,nw_src=10.0.0.1,nw_dst=10.0.0.2,actions=output:2,mod_nw_dst=10.0.0.3,mod_dl_dst=00:00:00:03,output:3
iperf -s -i 1
iperf -s -i 1

期中考題練習

ICPM Request / ICPM Reply

mkdir exam
cd exam
gedit switch.py

switch.py

#!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink,Intf
from mininet.node import Controller,RemoteController
 
if '__main__' == __name__:
  net = Mininet(link=TCLink)
  h1 = net.addHost('h1', ip="192.168.10.1")
  h2 = net.addHost('h2', ip="192.168.20.1")
  s1 = net.addSwitch('s1')
  s2 = net.addSwitch('s2')
  s3 = net.addSwitch('s3')
  s4 = net.addSwitch('s4')
  c0 = net.addController('c0', controller=RemoteController)
  net.addLink(h1, s1)
  net.addLink(s1, s2)
  net.addLink(s1, s3)
  net.addLink(s2, s4)
  net.addLink(s3, s4)
  net.addLink(s4, h2)
  net.build()
  c0.start()
  s1.start([c0])
  s2.start([c0])
  s3.start([c0])
  s4.start([c0])
  # rules for s1
  # h1 ping h2
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:2")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")
  s1.cmd("ovs-ofctl add-flow s1 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:3")

  # rules for s2
  # h1 ping h2
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s2.cmd("ovs-ofctl add-flow s2 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:2")
  s2.cmd("ovs-ofctl add-flow s2 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")

  # rules for s3
  s3.cmd("ovs-ofctl add-flow s3 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:2")
  s3.cmd("ovs-ofctl add-flow s3 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:1")

  # rules for s4
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=1,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=1,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=2,arp_spa=192.168.10.1,arp_tpa=192.168.20.1,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 arp,arp_op=2,arp_spa=192.168.20.1,arp_tpa=192.168.10.1,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=8,icmp_code=0,actions=output:3")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=0,icmp_code=0,actions=output:2")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.20.1,nw_dst=192.168.10.1,icmp_type=8,icmp_code=0,actions=output:1")
  s4.cmd("ovs-ofctl add-flow s4 icmp,nw_src=192.168.10.1,nw_dst=192.168.20.1,icmp_type=0,icmp_code=0,actions=output:3")


  CLI(net)
  net.stop()

Last updated

Was this helpful?