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

(一)基本要求

(1)/home/用户名/学号/lab2/目录下执行ovs-vsctl show命令、以及p0和p1连通性测试的执行结果截图


(2) /home/用户名/学号/lab2/目录下开启Mininet CLI并执行pingall命令的截图


上述目录下查看OVS流表的命令结果截图,wireshark抓包截图



(二)进阶要求

from mininet.net import Mininet
from mininet.node import Node
from mininet.link import Link
from mininet.log import  setLogLevel, info
 
def myNet():
    "Create network from scratch using Open vSwitch."
 
    info( "*** Creating nodes\n" )
    s1 = Node( 's1', inNamespace=False)
    s2 = Node( 's2', inNamespace=False)
 
    h1 = Node( 'h1' )
    h2 = Node( 'h2' )
    h3 = Node( 'h3' )
    h4 = Node( 'h4' )
 
    info( "*** Creating links\n" )
    Link( h1, s1, 1, 1)
    Link( h2, s1, 1, 2)
    Link( h3, s2, 1, 1)
    Link( h4, s2, 1, 2)
    Link( s1, s2, 3, 3)
 
    info( "*** Configuring hosts\n" )
    h1.setIP( '10.0.0.1/24' )
    h2.setIP( '10.0.0.2/24' )
    h3.setIP( '10.0.0.3/24' )
    h4.setIP( '10.0.0.4/24' )
       
    info( "*** Starting network using Open vSwitch\n" )
    s1.cmd( 'ovs-vsctl del-br dp1' )
    s1.cmd( 'ovs-vsctl add-br dp1' )
    s2.cmd( 'ovs-vsctl del-br dp2' )
    s2.cmd( 'ovs-vsctl add-br dp2' )
    
    for intf in s1.intfs.values():
        print(intf)
        print(s1.cmd( 'ovs-vsctl add-port dp1 %s' % intf ))
 
    for intf in s2.intfs.values():
        print(intf)
        print(s2.cmd( 'ovs-vsctl add-port dp2 %s' % intf ))


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

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

    info( "*** Running test\n" )
    h1.cmdPrint( 'ping -c 3 ' + h3.IP() )
    h1.cmdPrint( 'ping -c 3 ' + h4.IP() )
    h2.cmdPrint( 'ping -c 3 ' + h3.IP() )
    h2.cmdPrint( 'ping -c 3 ' + h4.IP() )
    h3.cmdPrint( 'ping -c 3 ' + h1.IP() )
    h3.cmdPrint( 'ping -c 3 ' + h2.IP() )
    h4.cmdPrint( 'ping -c 3 ' + h1.IP() )
    h4.cmdPrint( 'ping -c 3 ' + h2.IP() )
    
    info( "*** Stopping network\n" )
    s1.cmd( 'ovs-vsctl del-br dp1' )
    s1.deleteIntfs()
    info( '\n' )
    s2.cmd( 'ovs-vsctl del-br dp2' )
    s2.deleteIntfs()
    info( '\n' )   
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )
    Mininet.init()
    myNet()


个人总结

1.本次实验前我先浏览了老师所给的pdf文档,对接下来需要做的内容有了一个大概的了解,在还没上机前我对这些命令都只是一知半解。在真正上机开始实验后,即使是对照着老师所给的代码修改,也因为一些细节上实施的不同而导致了出错。在查错过程中,我对Openflow协议有了一定的了解、对OVS命令的使用有了初步的掌握,也对VLAN、流表等计算机网络的概念有了更深刻的体会。
2.添加流表时出现报错,正确做法是在添加流表的时候要同时运行创建拓扑的python文件。
3.wireshark抓包时,要在终端执行pingall操作,才可以抓包成功。

posted @ 2022-09-21 20:31  piqigoo  阅读(22)  评论(0编辑  收藏  举报