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
40
self.hosts = []
41
self.switches = []
42
self.controllers = []
43
44
self.nameToNode = {} # name to Node (Host/Switch) objects
45
46
self.terms = [] # list of spawned xterm processes
47
48
Mininet.init() # Initialize Mininet if necessary
49
50
self.built = False
51
if topo and build:
52
self.build()
Copied!
该方法主要根据传入参数配置相关的数据结构。如果还通过参数执行了拓扑信息,则执行 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
6
# Possibly we should clean up here and/or validate
7
# the topo
8
if self.cleanup:
9
pass
10
11
info( '*** Creating network\n' )
12
13
if not self.controllers and self.controller:
14
# Add a default controller
15
info( '*** Adding controller\n' )
16
classes = self.controller
17
if type( classes ) is not list:
18
classes = [ classes ]
19
for i, cls in enumerate( classes ):
20
self.addController( 'c%d' % i, cls )
21
22
info( '*** Adding hosts:\n' )
23
for hostName in topo.hosts():
24
self.addHost( hostName, **topo.nodeInfo( hostName ) )
25
info( hostName + ' ' )
26
27
info( '\n*** Adding switches:\n' )
28
for switchName in topo.switches():
29
self.addSwitch( switchName, **topo.nodeInfo( switchName) )
30
info( switchName + ' ' )
31
32
info( '\n*** Adding links:\n' )
33
for srcName, dstName in topo.links(sort=True):
34
src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ]
35
params = topo.linkInfo( srcName, dstName )
36
srcPort, dstPort = topo.port( srcName, dstName )
37
self.addLink( src, dst, srcPort, dstPort, **params )
38
info( '(%s, %s) ' % ( src.name, dst.name ) )
39
40
info( '\n' )
Copied!
Copy link