第二次SDN实验

实验2:Open vSwitch虚拟交换机实践

1.基本要求

创建OVS交换机并配置

点击查看代码
sudo ovs-vsctl add-br ovs-switch026

sudo ovs-vsctl add-port ovs-switch026 p0
sudo ovs-vsctl set Interface p0 ofport_request=100 type=internal

sudo ovs-vsctl add-port ovs-switch026 p1
sudo ovs-vsctl set Interface p1 ofport_request=101 type=internal

sudo ethtool -i p0
sudo ethtool -i p1

sudo ip netns add ns0
sudo ip link set p0 netns ns0
sudo ip netns exec ns0 ip addr add 192.168.0.100/24 dev p0
sudo ip netns exec ns0 ifconfig p0 promisc up

sudo ip netns add ns1
sudo ip link set p1 netns ns1
sudo ip netns exec ns1 ip addr add 192.168.0.101/24 dev p1
sudo ip netns exec ns1 ifconfig p1 promisc up

sudo ip netns exec ns0 ping 192.168.0.101

sudo ovs-vsctl show

输入sudo ovs-vsctl show查看网络状态

进行pingall命令

查看OVS流表

wireshark抓包结果:查看s1的1号端口

2.进阶要求

代码如下:

点击查看代码
from mininet.net import Mininet
from mininet.node import Node
from mininet.link import TCLink, Link
from mininet.log import  setLogLevel, info
 
def myNet():
    "Create network from scratch using Open vSwitch."
 
    info( "*** Creating nodes\n" )
    switch1 = Node( 's1', inNamespace=False )
    switch2 = Node( 's2', inNamespace=False )

    h1 = Node( 'h1' )
    h2 = Node( 'h2' )
    h3 = Node( 'h3' )
    h4 = Node( 'h4' )
 
    info( "*** Creating links\n" )
    Link( h1, switch1 )
    Link( h2, switch1 )
    Link( h3, switch2 )
    Link( h4, switch2 )
    Link( switch1, switch2 )
 
    info( "*** Configuring hosts\n" )
    h1.setIP( '192.168.123.1/24' )
    h2.setIP( '192.168.123.2/24' )
    h3.setIP( '192.168.123.3/24' )
    h4.setIP( '192.168.123.4/24' )
    info( str( h1 ) + '\n' )
    info( str( h2 ) + '\n' )
    info( str( h3 ) + '\n' )
    info( str( h4 ) + '\n' )
       
    info( "*** Starting network using Open vSwitch\n" )
    switch1.cmd( 'ovs-vsctl del-br dp0' )
    switch1.cmd( 'ovs-vsctl add-br dp0' )
    switch2.cmd( 'ovs-vsctl del-br dp1' )
    switch2.cmd( 'ovs-vsctl add-br dp1' )
 
    for intf in switch1.intfs.values():
        print(intf)
        print(switch1.cmd( 'ovs-vsctl add-port dp0 %s' % intf ))
 
    for intf in switch2.intfs.values():
        print(intf)
        print(switch2.cmd( 'ovs-vsctl add-port dp1 %s' % intf ))


    switch1.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3' )
    switch1.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3' )
    switch1.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=0,actions=pop_vlan,output=1' )
    switch1.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=1,actions=pop_vlan,output=2' )

    switch2.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3' )
    switch2.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3' )
    switch2.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output=1' )
    switch2.cmd( 'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=1,actions=pop_vlan,output=2' )

    info( "*** Running test\n" )
    info( "\nh1 ping h2\n" )
    h1.cmdPrint( 'ping -Q 0x30 -c 3 ' + h2.IP() )
    info( "\nh1 ping h3\n" )
    h1.cmdPrint( 'ping -Q 0x30 -c 3 ' + h3.IP() )
    info( "\nh1 ping h4\n" )
    h1.cmdPrint( 'ping -Q 0x30 -c 3 ' + h4.IP() )
    info( "\nh2 ping h3\n" )
    h2.cmdPrint( 'ping -Q 0x30 -c 3 ' + h3.IP() )
    info( "\nh2 ping h4\n" )
    h2.cmdPrint( 'ping -Q 0x30 -c 3 ' + h4.IP() )
    info( "\nh3 ping h4\n" )
    h3.cmdPrint( 'ping -Q 0x30 -c 3 ' + h4.IP() )

    info( "*** print the table\n" )
    info( switch1.cmd( 'ovs-ofctl dump-flows dp0 -O OpenFlow13' ) )
    info( switch2.cmd( 'ovs-ofctl dump-flows dp1 -O OpenFlow13' ) )

 
    info( "*** Stopping network\n" )
    switch1.cmd( 'ovs-vsctl del-br dp0' )
    switch1.deleteIntfs()
    switch2.cmd( 'ovs-vsctl del-br dp1' )
    switch2.deleteIntfs()
    info( '\n' )
 
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )
    Mininet.init()
    myNet()



posted @ 2023-09-19 22:15  Q·K  阅读(11)  评论(0编辑  收藏  举报