Mininet Python API

2018-10-09  本文已影响157人  顽强的猫尾草

原文地址:https://www.ibm.com/developerworks/cn/cloud/library/1404_luojun_sdnmininet/index.html

本文将联系实际应用,着重描述其中一些最基础的 API。

Intf

link 里面的 Intf 是一个接口函数:

class Intf( object ):
    "Basic interface object that can configure itself."
    def __init__( self, name, node=None, port=None, link=None, **params ):

这个类可以用于定义一个网络接口,如:定义虚拟机某个网卡与mininet中某交换机的某网卡相连,这样我们就可以使用一些打流工具,如 spirent 的 testcenter 给虚拟机的网卡打流,从而引导到 mininet 构建的网络中,达到打流的目的。

默认情况下,port 可以不填,mininet 会自动分配。

绑定 IP 地址

h1 = net.addHost( 'h1', ip='10.0.0.1' )

设置链路带宽等参数

# 10 Mbps, 5ms delay, 10% packet loss
self.addLink(host, switch, bw=10, delay='5ms', loss=10, use_htb=True)

运用示例:

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import irange, dumpNodeConnections
from mininet.log import setLogLevel
from mininet.node import CPULimitedHost
from mininet.link import TCLink

class CustomTopo(Topo):
    "Simple Data Center Topology"

    "linkopts - (1:core, 2:aggregation, 3: edge) parameters"
    "fanout - 每个交换机下的子节点个数, 通过改变这个参数来改变树的规模"
    def __init__(self, linkopts1, linkopts2, linkopts3, fanout=2, **opts):
        # Initialize topology and default options
        Topo.__init__(self, **opts)

        self.fonout = fanout

        # Add core switch
        cs_switch = self.addSwitch('cs%s' % 1)    # 名字中必须包含编号, 否则会报错

        # Add aggregation switches
        for i in irange(1, fanout):
            as_switch = self.addSwitch('as%s' % i)
            self.addLink(as_switch, cs_switch, **linkopts1)
            as_parent_switch = as_switch
 
            # Add edge switches
            for j in irange(1, fanout):
                es_num = i * fanout - 2 + j
                es_switch = self.addSwitch('es%s' % es_num, **linkopts2)
                self.addLink(es_switch, as_parent_switch)
                es_parent_switch = es_switch
 
                # Add hosts
                for k in irange(1, fanout):
                    host_num = es_num * fanout - 2 + k
                    host = self.addHost('h%s' % host_num, cpu=.5/fanout)
                    self.addLink(host, es_parent_switch, **linkopts3)
 
def perTest():
    "Specify performance parameters for the links"
    "bw 带宽, 单位是 Mb/s; delay 延迟, 用 0-100 的数字表示一个百分比; use_htb 使用 htb (Hierarchical Token Bucket) 算法, 大概就是一种流量控制算法"
    # Between core and aggregation switches
    linkopts1 = dict(bw=10, delay='5ms', loss=1, max_queue_size=1000, use_htb=True)
    # Between aggregation and edge switches
    linkopts2 = dict(bw=10, delay='5ms', loss=1, max_queue_size=1000, use_htb=True)
    # Between edge switches and hosts
    linkopts3 = dict(bw=10, delay='5ms', loss=1, max_queue_size=1000, use_htb=True)

    "Create and test a simple network"
    topo = CustomTopo(linkopts1=linkopts1, linkopts2=linkopts2, linkopts3=linkopts3, fanout=2)
    # 采用 CPULimitedHost 来限制主机的性能, TCLink 来设置链路的一些额外属性
    net = Mininet(topo=topo, host=CPULimitedHost, link=TCLink)    # 如果想设置链路参数必须加上 link=TCLink, 否则报错找不到 bw 等
    net.start()

    print "Dumping host connections"
    dumpNodeConnections(net.hosts)

    print "Testing network connectivity"
    net.pingAll()

    print "Testing bandwidth between h1 with h2, h3 and h5"
    h1, h2 = net.get('h1', 'h2')
    net.iperf( ( h1, h2 ) )
    h1, h3 = net.get('h1', 'h3')
    net.iperf( ( h1, h3 ) )
    h1, h5 = net.get('h1', 'h5')
    net.iperf( ( h1, h5 ) )
    h1, h7 = net.get('h1', 'h7')
    net.iperf( ( h1, h7 ) )

    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    perTest()
上一篇下一篇

猜你喜欢

热点阅读