默认拓扑结构
#创建默认拓扑,并指定外部控制器
[email protected]$sudo mn --controller remote,ip=<控制器ip>,port=<控制器端口6633、6653>
数据中心拓扑生成
拓扑结构
代码(生成拓扑并自动连接控制器[数据中心四叉树结构])
#encoding: utf-8
#python topo.py命令直接运行即可
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel,info
from mininet.node import CPULimitedHost
from mininet.node import RemoteController
from mininet.link import TCLink
from mininet.cli import CLI
class MyTopo( Topo ):
"Simple topology example."
def __init__( self ):
"Create custom topo."
# Initialize topology
Topo.__init__( self )
L1 = 4
L2 = L1 * 2
L3 = L2
Core = []
Aggregation = []
Edge = []
# add core ovs
for i in range( L1 ):
Switch = self.addSwitch( 'SC{}'.format( i + 1 ) )
Core.append( Switch )
# add aggregation ovs
for i in range( L2 ):
Switch= self.addSwitch( 'SA{}'.format( L1 +i + 1 ) )
Aggregation.append( Switch )
# add edge ovs
for i in range( L3 ):
Switch = self.addSwitch( 'SE{}'.format( L1 +L2 + i + 1 ) )
Edge .append( Switch)
# add links between core and aggregation ovs
for i in range( L1 ):
sw1 = Core[i]
for sw2 in Aggregation [i/2::L1/2]:
self.addLink( sw2, sw1 )
# add links between aggregation and edge ovs
for i in range( 0, L2, 2 ):
for sw1 in Aggregation [i:i+2]:
for sw2 in Edge[i:i+2]:
self.addLink( sw2, sw1 )
#add hosts and its links with edge ovs
count = 1
for sw1 in Edge:
for i in range(2):
host = self.addHost( 'h{}'.format( count ) )
self.addLink( sw1, host )
count += 1
print('[信息]: 数据中心拓扑初始化完成')
if __name__=='__main__':
setLogLevel('info')
topo=MyTopo()
net=Mininet(topo=topo,controller=None)
net.addController('control',controller=RemoteController,ip='10.6.65.121',port=6653) #指定控制器
print('[信息]: 控制器连接成功')
net.start() #启动拓扑
CLI(net) #等待命令
net.stop() #关闭拓扑
控制器中生成拓扑
接入网卡
sudo ifconfig ens38 0.0.0.0 #清除网卡ip
#!/usr/bin/python
#-*- coding:utf-8 -*-
import re
import sys
from mininet.cli import CLI
from mininet.log import setLogLevel, info, error
from mininet.net import Mininet
from mininet.link import Intf
from mininet.util import quietRun
from mininet.node import OVSSwitch, OVSController, Controller, RemoteController
from mininet.topo import Topo
class MyTopo( Topo ):
def __init__( self ):
"拓扑结构初始化"
#初始化拓扑
Topo.__init__( self )
# 添加主机
h1= self.addHost( 'h11' , ip="192.168.199.90/24", mac="00:00:00:00:00:01", defaultRoute="via 192.168.199.1")
h2= self.addHost( 'h12' , ip="192.168.199.91/24", mac="00:00:00:00:00:02", defaultRoute="via 192.168.199.1")
h3 = self.addHost( 'h13' , ip="192.168.199.92/24", mac="00:00:00:00:00:03", defaultRoute="via 192.168.199.1")
h4 = self.addHost( 'h14' , ip="192.168.199.93/24", mac="00:00:00:00:00:04", defaultRoute="via 192.168.199.1")
# 添加交换机
s1 = self.addSwitch( 's1' )
s2 = self.addSwitch( 's2' )
s3 = self.addSwitch( 's3' )
# 创建链路
self.addLink( s1, s2 )
self.addLink( s1, s3 )
self.addLink( s2, h1 )
self.addLink( s2, h2 )
self.addLink( s3, h3 )
self.addLink( s3, h4 )
print('[信息]: 拓扑结构初始化完成')
#检查eth1或者其他指定的网卡资源是不是已经被占用
def checkIntf( intf ):
"确保接口存在且未配置。"
if ( ' %s:' % intf ) not in quietRun( 'ip link show' ):
error( '[错误]:', intf, '不存在!\n' )
exit( 1 )
ips = re.findall( r'\d+\.\d+\.\d+\.\d+', quietRun( 'ifconfig ' + intf ) )
if ips:
error( '[错误]:', intf, '有一个IP地址,并且正在使用!\n' )
exit( 1 )
if __name__ == '__main__':
setLogLevel( 'info' )
# 尝试从命令行获取网络接口;默认情况下,使用ens38
intfName = sys.argv[1] if len( sys.argv ) > 1 else 'ens38'
info( '*** 连接到网络接口: %s' % intfName )
info( '*** 检查', intfName, '\n' )
checkIntf( intfName )
info( '*** 创建网络\n' )
net = Mininet( topo=MyTopo(),controller=None) #关键函数,创建mininet网络,指定拓扑和控制器。这里的控制器在后面添加进去
switch = net.switches[0] #取第一个交换机与网卡桥接
info( '*** 添加硬件接口', intfName, 'to switch', switch.name, '\n' )
_intf = Intf( intfName, node=switch ) #最关键的函数,用作把一个网卡与一个交换机桥接
info( '*** 注意:您可能需要重新配置Mininet主机的接口:\n', net.hosts, '\n' )
net.addController('control',controller=RemoteController,ip='10.6.65.160',port=6653) #指定控制器
print('[信息]: 控制器连接成功')
net.start() #启动拓扑
CLI(net) #等待命令
net.stop() #关闭拓扑
拓扑