Mininet 应用与源码剖析
  • 前言
  • 概述
    • 主要特性
    • 安装
  • 基本使用
    • 创建网络
    • 查看信息
    • 节点操作
  • 常用操作
    • 快捷测试
    • 自定义拓扑
    • 使用友好的 MAC 编号
    • 使用 XTerm
    • 链路操作
    • 指定交换机跟控制器类型
    • 名字空间
    • 启动参数总结
    • 常用命令总结
    • 其他操作
  • 高级功能
    • 使用 dpctl
    • 控制器
    • 交换机与控制器交互
    • 使用 NOX
    • 外部读取配置命令
  • 代码结构
    • 运行相关
    • 安装相关
    • 核心代码
    • 说明文件
    • 其它
    • 整体逻辑功能
  • mininet.link 模块
    • mininet.link.Intf
    • mininet.link.Link
    • mininet.link.TCIntf
    • mininet.link.TCLink
  • mininet.node 模块
    • 基类
    • 主机类
    • 控制器类
    • 交换机类
  • mininet.net 模块
    • mininet.net.Mininet
    • mininet.net.MininetWithControlNet
  • mininet.topo 模块
    • mininet.topo.MultiGraph
    • mininet.topo.Topo
    • mininet.topo.LinearTopo
    • mininet.topo.SingleSwitchTopo
    • mininet.topo.SingleSwitchReversedTopo
  • 其它模块
    • mininet.cli 模块
    • mininet.clean 模块
    • mininet.log 模块
    • mininet.moduledeps 模块
    • mininet.term 模块
    • mininet.util 模块
  • 运行代码和示例
    • mn
    • 示例程序
Powered by GitBook
On this page
  • init
  • buildFromTopo

Was this helpful?

  1. mininet.net 模块

mininet.net.Mininet

模拟一个 Mininet 中的网络,包括拓扑、交换机、主机、控制器、链路、接口等。 其中最主要的部分是 build() 函数,依次执行:根据拓扑创建网络,配置网络名字空间,配置主机的 IP、MAC 等信息,检查是否启动 xterm,是否配置自动静态 arp 等。

init

          inNamespace=False,
                  autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,
                  listenPort=None ):
        """Create Mininet object.
           topo: Topo (topology) object or None
           switch: default Switch class
           host: default Host class/constructor
           controller: default Controller class/constructor
           link: default Link class/constructor
           intf: default Intf class/constructor
           ipBase: base IP address for hosts,
           build: build now from topo?
           xterms: if build now, spawn xterms?
           cleanup: if build now, cleanup before creating?
           inNamespace: spawn switches and controller in net namespaces?
           autoSetMacs: set MAC addrs automatically like IP addresses?
           autoStaticArp: set all-pairs static MAC addrs?
           autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?
           listenPort: base listening port to open; will be incremented for
               each additional switch in the net if inNamespace=False"""
        self.topo = topo
        self.switch = switch
        self.host = host
        self.controller = controller
        self.link = link
        self.intf = intf
        self.ipBase = ipBase
        self.ipBaseNum, self.prefixLen = netParse( self.ipBase )
        self.nextIP = 1  # start for address allocation
        self.inNamespace = inNamespace
        self.xterms = xterms
        self.cleanup = cleanup
        self.autoSetMacs = autoSetMacs
        self.autoStaticArp = autoStaticArp
        self.autoPinCpus = autoPinCpus
        self.numCores = numCores()
        self.nextCore = 0  # next core for pinning hosts to CPUs
        self.listenPort = listenPort

        self.hosts = []
        self.switches = []
        self.controllers = []

        self.nameToNode = {}  # name to Node (Host/Switch) objects

        self.terms = []  # list of spawned xterm processes

        Mininet.init()  # Initialize Mininet if necessary

        self.built = False
        if topo and build:
            self.build()

该方法主要根据传入参数配置相关的数据结构。如果还通过参数执行了拓扑信息,则执行 build 方法,调用 buildFromTopo 方法来根据拓扑信息创建节点和相关的连接等。

buildFromTopo

def buildFromTopo( self, topo=None ):
        """Build mininet from a topology object
           At the end of this function, everything should be connected
           and up."""

        # Possibly we should clean up here and/or validate
        # the topo
        if self.cleanup:
            pass

        info( '*** Creating network\n' )

        if not self.controllers and self.controller:
            # Add a default controller
            info( '*** Adding controller\n' )
            classes = self.controller
            if type( classes ) is not list:
                classes = [ classes ]
            for i, cls in enumerate( classes ):
                self.addController( 'c%d' % i, cls )

        info( '*** Adding hosts:\n' )
        for hostName in topo.hosts():
            self.addHost( hostName, **topo.nodeInfo( hostName ) )
            info( hostName + ' ' )

        info( '\n*** Adding switches:\n' )
        for switchName in topo.switches():
            self.addSwitch( switchName, **topo.nodeInfo( switchName) )
            info( switchName + ' ' )

        info( '\n*** Adding links:\n' )
        for srcName, dstName in topo.links(sort=True):
            src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ]
            params = topo.linkInfo( srcName, dstName )
            srcPort, dstPort = topo.port( srcName, dstName )
            self.addLink( src, dst, srcPort, dstPort, **params )
            info( '(%s, %s) ' % ( src.name, dst.name ) )

        info( '\n' )
Previousmininet.net 模块Nextmininet.net.MininetWithControlNet

Last updated 5 years ago

Was this helpful?