实验1:SDN拓扑实践

a) 第1步Mininet运行结果截图



b) 第2步的执行结果截图!


c) 第3步提交修改过的“学号.py”代码、Mininet运行结果

点击查看代码
#!/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 controller\n' )
    c0=net.addController(name='c0',
                      controller=Controller,
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1', cls=OVSKernelSwitch, dpid='0000000000000001')
    s2 = net.addSwitch('s2', cls=OVSKernelSwitch, dpid='0000000000000001')

    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None, cpu=50)
    h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, s1, bw=10, delay='5ms', max_queue_siza=1000, loss=10, use_htb=True)
    net.addLink(h3, s2)
    net.addLink(s2, h4)
    net.addLink(s1, s2)
    net.addLink(s1, h2)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')
    net.get('s1').start([c0])
    net.get('s2').start([c0])

    info( '*** Post configure switches and hosts\n')

    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()



进阶:

点击查看代码
#!/usr/bin/python
#创建网络拓扑
"""Custom topology example
Adding the 'topos' dict with a key/value pair to generate our newly defined
topology enables one to pass in '--topo=mytopo' from the command line.
"""

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import RemoteController,CPULimitedHost
from mininet.link import TCLink
from mininet.util import dumpNodeConnections

class MyTopo( Topo ):
    "Simple topology example."

    def __init__( self ):
        "Create custom topo."

        # Initialize topology
        Topo.__init__( self )
        L1 = 2
        L2 = L1 * 2
        L3 = L2 * 2
        c = []
        a = []
        e = []

        # add core ovs  
        for i in range( L1 ):
                sw = self.addSwitch( 's{}'.format( i + 1 ) )
                c.append( sw )

        # add aggregation ovs
        for i in range( L2 ):
                sw = self.addSwitch( 's{}'.format( L1 + i + 1 ) )
                a.append( sw )

        # add edge ovs
        for i in range( L3 ):
                sw = self.addSwitch( 's{}'.format( L1 + L2 + i + 1 ) )
                e.append( sw )
# add links between core and aggregation ovs
        for i in range( L1 ):
                sw1 = c[i]
                for sw2 in a[i//2::L1//2]:
                # self.addLink(sw2, sw1, bw=10, delay='5ms', loss=10, max_queue_size=1000, use_htb=True)
                        self.addLink( sw2, sw1 )

        # add links between aggregation and edge ovs
        for i in range( 0, L2, 2 ):
                for sw1 in a[i:i+2]:
                    for sw2 in e[i:i+2]:
                            self.addLink( sw2, sw1 )

        #add hosts and its links with edge ovs
        count = 1
        for sw1 in e:
                for i in range(2):
                    host = self.addHost( 'h{}'.format( count ) )
                    self.addLink( sw1, host )
                    count += 1
topos = { 'mytopo': ( lambda: MyTopo() ) }


个人感想: 本次实验是基于第一次SDN环境安装的实验的基础上的,所以要是在第一个实验有未完成的,可能会对本次实验有所影响。在做本次普通实验时,由于PDF讲述的很清晰,加上第一次实验也比较成功,所以本次实验比较顺利,值得注意的一点是想启动miniedit就要先进入root模式。在进阶实验中,当参照案例完成了代码的编写,想用python脚本启动mininet构建拓扑时出现了问题,启动失败,一开始查阅以为是mininet安装的问题,捣鼓了一会,根据报错提示才发现是代码的问题,由于对Python语言的不太熟练,问题出自于在首行缩进使用了tab和空格混合的情况,还有就是在代码中的运算出现除号时,应将/改为//,最终成功构建拓扑,参照案例修改代码也只需要根据可视化构建的拓扑结构来进行简单修改即可
posted @ 2022-09-18 18:08  212002125王昊  阅读(22)  评论(0编辑  收藏  举报