• 欢迎光临~

实验2

开发技术 开发技术 2022-09-29 次浏览

a:实验2
b:实验2
实验2
实验2
实验2
进阶:
实验2

点击查看代码
#!/usr/bin/env python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controllern' )
    c0=net.addController(name='c0',
                      controller=Controller,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switchesn')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch)
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch)

    info( '*** Add hostsn')
    h1 = net.addHost('h1', ip='10.0.0.1')
    h2 = net.addHost('h2', ip='10.0.0.2')
    h3 = net.addHost('h3', ip='10.0.0.3')
    h4 = net.addHost('h4', ip='10.0.0.4')

    info( '*** Add linksn')
    net.addLink(s1, h1, 1, 1)
    net.addLink(s1, h2, 2, 1)
    net.addLink(s2, h3, 1, 1)
    net.addLink(s2, h4, 2, 1)
    net.addLink(s1, s2, 3, 3)

    info( '*** Starting networkn')
    net.build()
    info( '*** Starting controllersn')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switchesn')
    net.get('s1').start([c0])
    net.get('s2').start([c0])

    info( '*** Post configure switches and hostsn')

    s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096->vlan_vid,output:3')
    s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097->vlan_vid,output:3')
    s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
    s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
    s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096->vlan_vid,output:3')
    s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097->vlan_vid,output:3')
    s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
    s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
 
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()
个人心得:下发流表时要注意命令里的端口号是否与建立拓扑时的端口号一致,使用命令行要注意数值的配置,且很多操作失误后无法彻底删除上一步操作产生的冗余文件,所以要在键入命令时多检查几次。
程序员灯塔
转载请注明原文链接:实验2
喜欢 (0)