0%

Mininet自定义topo

mininet自带topo

通过mn -h可以看到Mininet自带的几种topo类型,分别有Linear, minimal, reversed, single, torus和tree类型,但有时候这些类型无法满足需求,需要自定义topo

1
2
3
4
5
6
7
8
9
10
$ mn --help
Usage: mn [options]
(type mn -h for details)

...

--topo=TOPO linear|minimal|reversed|single|torus|tree[,param=value
...] linear=LinearTopo torus=TorusTopo tree=TreeTopo
single=SingleSwitchTopo
reversed=SingleSwitchReversedTopo minimal=MinimalTopo

获取示例

Mininet提供了topo-2sw-2host的示例,可以通过Mininet github的custom目录下获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Custom topology example
Two directly connected switches plus a host for each switch:
host --- switch --- switch --- host
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

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

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

# Initialize topology
Topo.__init__( self )

# Add hosts and switches
leftHost = self.addHost( 'h1' )
rightHost = self.addHost( 'h2' )
leftSwitch = self.addSwitch( 's3' )
rightSwitch = self.addSwitch( 's4' )

# Add links
self.addLink( leftHost, leftSwitch )
self.addLink( leftSwitch, rightSwitch )
self.addLink( rightSwitch, rightHost )


topos = { 'mytopo': ( lambda: MyTopo() ) }

自定义topo

如要创建如下topo

1
2
3
4
5
6
7
8
9
10
11
                         +------------+
+----------+ s3 +---------+
| +------------+ |
| |
+----+----+ +----+----+
+-----+ s1 +-----+ +-----+ s2 +-----+
| +---------+ | | +---------+ |
| | | |
+--+--+ +--+--+ +--+--+ +--+--+
| h1 | | h2 | | h3 | | h4 |
+-----+ +-----+ +-----+ +-----+

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from mininet.topo import Topo

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

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

# Initialize topology
Topo.__init__( self )

# Add hosts and switches
h1 = self.addHost( 'h1' )
h2 = self.addHost( 'h2' )
h3 = self.addHost( 'h3' )
h4 = self.addHost( 'h4' )
s1 = self.addSwitch( 's1' )
s2 = self.addSwitch( 's2' )
s3 = self.addSwitch( 's3' )

# Add links
self.addLink( h1, s1 )
self.addLink( s1, h2 )
self.addLink( s1, s3 )
self.addLink( s3, s2 )
self.addLink( h3, s2 )
self.addLink( s2, h4 )


topos = { 'mytopo': ( lambda: MyTopo() ) }

使用自定义topo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@mininet:~# mn --custom ./testtopo.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6633
*** Creating network
*** Adding controller
*** Adding hosts:
h1 h2 h3 h4
*** Adding switches:
s1 s2 s3
*** Adding links:
(h1, s1) (h3, s2) (s1, h2) (s1, s3) (s2, h4) (s3, s2)
*** Configuring hosts
h1 h2 h3 h4
*** Starting controller
c0
*** Starting 3 switches
s1 s2 s3 ...
*** Starting CLI:
mininet>

查看连接状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mininet> links
h1-eth0<->s1-eth1 (OK OK)
h3-eth0<->s2-eth2 (OK OK)
s1-eth2<->h2-eth0 (OK OK)
s1-eth3<->s3-eth1 (OK OK)
s2-eth3<->h4-eth0 (OK OK)
s3-eth2<->s2-eth1 (OK OK)
mininet> pingall
*** Ping: testing ping reachability
h1 -> h2 h3 h4
h2 -> h1 h3 h4
h3 -> h1 h2 h4
h4 -> h1 h2 h3
*** Results: 0% dropped (12/12 received)
mininet>