示例,要创建一个3个交换机8个主机的拓扑,如下图:
可以用如下python代码创建上述拓扑,并指定ip:
from mininet.topo import Topo class MyTopo( Topo ): def __init__( self ): "Create custom topo." # Initialize topology Topo.__init__( self ) # Add hosts and switches leftHost1 = self.addHost( 'h1', ip='10.0.0.1' ) leftHost2 = self.addHost( 'h2', ip='10.0.0.2' ) leftHost3 = self.addHost( 'h3', ip='10.0.1.1' ) leftHost4 = self.addHost( 'h4', ip='10.0.1.2' ) leftSwitch = self.addSwitch( 's1' ) middleSwitch = self.addSwitch( 's2' ) rightSwitch = self.addSwitch( 's3' ) rightHost1 = self.addHost( 'h5', ip='10.0.11.1' ) rightHost2 = self.addHost( 'h6', ip='10.0.11.2' ) rightHost3 = self.addHost( 'h7', ip='10.0.12.1' ) rightHost4 = self.addHost( 'h8', ip='10.0.12.2' ) # Add links self.addLink( leftHost1, leftSwitch ) self.addLink( leftHost2, leftSwitch ) self.addLink( leftHost3, leftSwitch ) self.addLink( leftHost4, leftSwitch ) self.addLink( leftSwitch, middleSwitch ) self.addLink( middleSwitch, rightSwitch ) self.addLink( rightSwitch, rightHost1 ) self.addLink( rightSwitch, rightHost2 ) self.addLink( rightSwitch, rightHost3 ) self.addLink( rightSwitch, rightHost4 ) topos = { 'mytopo': ( lambda: MyTopo() ) }
创建topo之后用dump命令验证,可见它们的ip确实如代码中指定的一样。
mininet@mininet-vm:~/mininet/custom$ sudo mn --custom topo-3sw-8host.py --topo mytopo --controller=remote,ip=192.168.56.1 --mac added a switch! added a switch! added a switch! *** Creating network *** Adding controller *** Adding hosts: h1 h2 h3 h4 h5 h6 h7 h8 *** Adding switches: s1 s2 s3 *** Adding links: (h1, s1) (h2, s1) (h3, s1) (h4, s1) (h5, s3) (h6, s3) (h7, s3) (h8, s3) (s1, s2) (s2, s3) *** Configuring hosts h1 h2 h3 h4 h5 h6 h7 h8 *** Starting controller *** Starting 3 switches s1 s2 s3 *** Starting CLI: mininet> dump <RemoteController c0: 192.168.56.1:6633 pid=6887> <OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None,s1-eth4:None,s1-eth5:None pid=6904> <OVSSwitch s2: lo:127.0.0.1,s2-eth1:None,s2-eth2:None pid=6909> <OVSSwitch s3: lo:127.0.0.1,s3-eth1:None,s3-eth2:None,s3-eth3:None,s3-eth4:None,s3-eth5:None pid=6914> <Host h1: h1-eth0:10.0.0.1 pid=6894> <Host h2: h2-eth0:10.0.0.2 pid=6895> <Host h3: h3-eth0:10.0.1.1 pid=6896> <Host h4: h4-eth0:10.0.1.2 pid=6897> <Host h5: h5-eth0:10.0.11.1 pid=6898> <Host h6: h6-eth0:10.0.11.2 pid=6899> <Host h7: h7-eth0:10.0.12.1 pid=6900> <Host h8: h8-eth0:10.0.12.2 pid=6901> mininet>