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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

# 1. 导入相关的依赖
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():
# 定义网络,在IP基地址为10.0.0.0/8上
net = Mininet(topo=None, build=False, ipBase='10.0.0.0/8')

info('*** 添加远程控制器\n')
# 因为我们在本地运行ryu-manager充当控制器,故这里为远程控制器
# RemoteController且IP地址为本地IP
c0 = net.addController(name='c0',
controller=RemoteController,
ip='127.0.0.1',
protocol='tcp',
port=6633)

info('*** 添加交换机\n')
# dpid为16位编号
s1 = net.addSwitch('s1', cls=OVSKernelSwitch, dpid='0000000000000001')
s2 = net.addSwitch('s2', cls=OVSKernelSwitch, dpid='0000000000000002')
s3 = net.addSwitch('s3', cls=OVSKernelSwitch, dpid='0000000000000003')

info('*** 添加6台主机\n')
h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
h3 = net.addHost('h3', cls=Host, ip='10.0.0.3', defaultRoute=None)
h4 = net.addHost('h4', cls=Host, ip='10.0.0.4', defaultRoute=None)
h5 = net.addHost('h5', cls=Host, ip='10.0.0.5', defaultRoute=None)
h6 = net.addHost('h6', cls=Host, ip='10.0.0.6', defaultRoute=None)

info('*** 添加链路\n')
net.addLink(s1, h1)
net.addLink(s1, h2)
net.addLink(s2, h3)
net.addLink(s2, h4)
net.addLink(s3, h5)
net.addLink(s3, h6)
# 交换机级联
net.addLink(s3, s2)
net.addLink(s2, s1)

info('*** 启动网络\n')
net.build()

info('*** 启动控制器\n')
for controller in net.controllers:
controller.start()

info('*** 启动交换机\n')
net.get('s1').start([c0])
net.get('s2').start([c0])
net.get('s3').start([c0])

info('*** 显示配置的交换机与主机信息\n')
CLI(net)

# 停止运行
net.stop()

if __name__ == '__main__':
setLogLevel('info')
myNetwork()