mininet.net.Mininet

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

init

  1. inNamespace=False,
  2. autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,
  3. listenPort=None ):
  4. """Create Mininet object.
  5. topo: Topo (topology) object or None
  6. switch: default Switch class
  7. host: default Host class/constructor
  8. controller: default Controller class/constructor
  9. link: default Link class/constructor
  10. intf: default Intf class/constructor
  11. ipBase: base IP address for hosts,
  12. build: build now from topo?
  13. xterms: if build now, spawn xterms?
  14. cleanup: if build now, cleanup before creating?
  15. inNamespace: spawn switches and controller in net namespaces?
  16. autoSetMacs: set MAC addrs automatically like IP addresses?
  17. autoStaticArp: set all-pairs static MAC addrs?
  18. autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?
  19. listenPort: base listening port to open; will be incremented for
  20. each additional switch in the net if inNamespace=False"""
  21. self.topo = topo
  22. self.switch = switch
  23. self.host = host
  24. self.controller = controller
  25. self.link = link
  26. self.intf = intf
  27. self.ipBase = ipBase
  28. self.ipBaseNum, self.prefixLen = netParse( self.ipBase )
  29. self.nextIP = 1 # start for address allocation
  30. self.inNamespace = inNamespace
  31. self.xterms = xterms
  32. self.cleanup = cleanup
  33. self.autoSetMacs = autoSetMacs
  34. self.autoStaticArp = autoStaticArp
  35. self.autoPinCpus = autoPinCpus
  36. self.numCores = numCores()
  37. self.nextCore = 0 # next core for pinning hosts to CPUs
  38. self.listenPort = listenPort
  39. self.hosts = []
  40. self.switches = []
  41. self.controllers = []
  42. self.nameToNode = {} # name to Node (Host/Switch) objects
  43. self.terms = [] # list of spawned xterm processes
  44. Mininet.init() # Initialize Mininet if necessary
  45. self.built = False
  46. if topo and build:
  47. self.build()

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

buildFromTopo

  1. def buildFromTopo( self, topo=None ):
  2. """Build mininet from a topology object
  3. At the end of this function, everything should be connected
  4. and up."""
  5. # Possibly we should clean up here and/or validate
  6. # the topo
  7. if self.cleanup:
  8. pass
  9. info( '*** Creating network\n' )
  10. if not self.controllers and self.controller:
  11. # Add a default controller
  12. info( '*** Adding controller\n' )
  13. classes = self.controller
  14. if type( classes ) is not list:
  15. classes = [ classes ]
  16. for i, cls in enumerate( classes ):
  17. self.addController( 'c%d' % i, cls )
  18. info( '*** Adding hosts:\n' )
  19. for hostName in topo.hosts():
  20. self.addHost( hostName, **topo.nodeInfo( hostName ) )
  21. info( hostName + ' ' )
  22. info( '\n*** Adding switches:\n' )
  23. for switchName in topo.switches():
  24. self.addSwitch( switchName, **topo.nodeInfo( switchName) )
  25. info( switchName + ' ' )
  26. info( '\n*** Adding links:\n' )
  27. for srcName, dstName in topo.links(sort=True):
  28. src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ]
  29. params = topo.linkInfo( srcName, dstName )
  30. srcPort, dstPort = topo.port( srcName, dstName )
  31. self.addLink( src, dst, srcPort, dstPort, **params )
  32. info( '(%s, %s) ' % ( src.name, dst.name ) )
  33. info( '\n' )