forked from Huangmachi/PureSDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fattree8.py
287 lines (255 loc) · 7.67 KB
/
fattree8.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright (C) 2016 Huang MaChi at Chongqing University
# of Posts and Telecommunications, Chongqing, China.
# Copyright (C) 2016 Li Cheng at Beijing University of Posts
# and Telecommunications. www.muzixing.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mininet.net import Mininet
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import Link, Intf, TCLink
from mininet.topo import Topo
from mininet.util import dumpNodeConnections
import logging
import os
class Fattree(Topo):
"""
Class of Fattree Topology.
"""
CoreSwitchList = []
AggSwitchList = []
EdgeSwitchList = []
HostList = []
def __init__(self, k, density):
self.pod = int(k)
self.density = int(density)
self.iCoreLayerSwitch = int((k/2)**2)
self.iAggLayerSwitch = int(k*k/2)
self.iEdgeLayerSwitch = int(k*k/2)
self.iHost = int(self.iEdgeLayerSwitch * density)
# Init Topo
Topo.__init__(self)
def createNodes(self):
self.createCoreLayerSwitch(self.iCoreLayerSwitch)
self.createAggLayerSwitch(self.iAggLayerSwitch)
self.createEdgeLayerSwitch(self.iEdgeLayerSwitch)
self.createHost(self.iHost)
# Create Switch and Host
def _addSwitch(self, number, level, switch_list):
"""
Create switches.
"""
for i in range(1, number+1):
PREFIX = str(level) + "00"
if i >= 10:
PREFIX = str(level) + "0"
switch_list.append(self.addSwitch(PREFIX + str(i)))
def createCoreLayerSwitch(self, NUMBER):
self._addSwitch(NUMBER, 1, self.CoreSwitchList)
def createAggLayerSwitch(self, NUMBER):
self._addSwitch(NUMBER, 2, self.AggSwitchList)
def createEdgeLayerSwitch(self, NUMBER):
self._addSwitch(NUMBER, 3, self.EdgeSwitchList)
def createHost(self, NUMBER):
"""
Create hosts.
"""
for i in range(1, NUMBER+1):
if i >= 100:
PREFIX = "h"
elif i >= 10:
PREFIX = "h0"
else:
PREFIX = "h00"
self.HostList.append(self.addHost(PREFIX + str(i), cpu=1.0/NUMBER))
def createLinks(self, bw_c2a=10, bw_a2e=10, bw_e2h=10):
"""
Add network links.
"""
# Core to Agg
end = int(self.pod/2)
for x in range(0, self.iAggLayerSwitch, end):
for i in range(0, end):
for j in range(0, end):
self.addLink(
self.CoreSwitchList[i*end+j],
self.AggSwitchList[x+i],
bw=bw_c2a, max_queue_size=1000) # use_htb=False
# Agg to Edge
for x in range(0, self.iAggLayerSwitch, end):
for i in range(0, end):
for j in range(0, end):
self.addLink(
self.AggSwitchList[x+i], self.EdgeSwitchList[x+j],
bw=bw_a2e, max_queue_size=1000) # use_htb=False
# Edge to Host
for x in range(0, self.iEdgeLayerSwitch):
for i in range(0, self.density):
self.addLink(
self.EdgeSwitchList[x],
self.HostList[self.density * x + i],
bw=bw_e2h, max_queue_size=1000) # use_htb=False
def set_ovs_protocol_13(self,):
"""
Set the OpenFlow version for switches.
"""
self._set_ovs_protocol_13(self.CoreSwitchList)
self._set_ovs_protocol_13(self.AggSwitchList)
self._set_ovs_protocol_13(self.EdgeSwitchList)
def _set_ovs_protocol_13(self, sw_list):
for sw in sw_list:
cmd = "sudo ovs-vsctl set bridge %s protocols=OpenFlow13" % sw
os.system(cmd)
def set_host_ip(net, topo):
hostlist = []
for k in range(len(topo.HostList)):
hostlist.append(net.get(topo.HostList[k]))
i = 1
j = 1
for host in hostlist:
host.setIP("10.%d.0.%d" % (i, j))
j += 1
if j == topo.density+1:
j = 1
i += 1
def create_subnetList(topo, num):
"""
Create the subnet list of the certain Pod.
"""
subnetList = []
remainder = num % (topo.pod/2)
if topo.pod == 4:
if remainder == 0:
subnetList = [num-1, num]
elif remainder == 1:
subnetList = [num, num+1]
else:
pass
elif topo.pod == 8:
if remainder == 0:
subnetList = [num-3, num-2, num-1, num]
elif remainder == 1:
subnetList = [num, num+1, num+2, num+3]
elif remainder == 2:
subnetList = [num-1, num, num+1, num+2]
elif remainder == 3:
subnetList = [num-2, num-1, num, num+1]
else:
pass
else:
pass
return subnetList
def install_proactive(net, topo):
"""
Install direct flow entries for edge switches.
"""
# Edge Switch
for sw in topo.EdgeSwitchList:
num = int(sw[-2:])
# Downstream.
for i in range(1, topo.density+1):
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \
nw_dst=10.%d.0.%d,actions=output:%d'" % (sw, num, i, topo.pod/2+i)
os.system(cmd)
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \
nw_dst=10.%d.0.%d,actions=output:%d'" % (sw, num, i, topo.pod/2+i)
os.system(cmd)
# Aggregate Switch
# Downstream.
for sw in topo.AggSwitchList:
num = int(sw[-2:])
subnetList = create_subnetList(topo, num)
k = 1
for i in subnetList:
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \
nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, topo.pod/2+k)
os.system(cmd)
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \
nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, topo.pod/2+k)
os.system(cmd)
k += 1
# Core Switch
for sw in topo.CoreSwitchList:
j = 1
k = 1
for i in range(1, len(topo.EdgeSwitchList)+1):
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,arp, \
nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, j)
os.system(cmd)
cmd = "ovs-ofctl add-flow %s -O OpenFlow13 \
'table=0,idle_timeout=0,hard_timeout=0,priority=10,ip, \
nw_dst=10.%d.0.0/16, actions=output:%d'" % (sw, i, j)
os.system(cmd)
k += 1
if k == topo.pod/2 + 1:
j += 1
k = 1
def iperfTest(net, topo):
"""
Start iperf test.
"""
h001, h015, h016 = net.get(
topo.HostList[0], topo.HostList[14], topo.HostList[15])
# iperf Server
h001.popen('iperf -s -u -i 1 > iperf_server_differentPod_result', shell=True)
# iperf Server
h015.popen('iperf -s -u -i 1 > iperf_server_samePod_result', shell=True)
# iperf Client
h016.cmdPrint('iperf -c ' + h001.IP() + ' -u -t 10 -i 1 -b 10m')
h016.cmdPrint('iperf -c ' + h015.IP() + ' -u -t 10 -i 1 -b 10m')
def pingTest(net):
"""
Start ping test.
"""
net.pingAll()
def createTopo(pod, density, ip="192.168.56.104", port=6653, bw_c2a=10, bw_a2e=10, bw_e2h=10):
"""
Create network topology and run the Mininet.
"""
# Create Topo.
topo = Fattree(pod, density)
topo.createNodes()
topo.createLinks(bw_c2a=bw_c2a, bw_a2e=bw_a2e, bw_e2h=bw_e2h)
# Start Mininet.
CONTROLLER_IP = ip
CONTROLLER_PORT = port
net = Mininet(topo=topo, link=TCLink, controller=None, autoSetMacs=True)
net.addController(
'controller', controller=RemoteController,
ip=CONTROLLER_IP, port=CONTROLLER_PORT)
net.start()
# Set OVS's protocol as OF13.
topo.set_ovs_protocol_13()
# Set hosts IP addresses.
set_host_ip(net, topo)
# Install proactive flow entries
install_proactive(net, topo)
# dumpNodeConnections(net.hosts)
# pingTest(net)
# iperfTest(net, topo)
CLI(net)
net.stop()
if __name__ == '__main__':
setLogLevel('info')
if os.getuid() != 0:
logging.debug("You are NOT root")
elif os.getuid() == 0:
# createTopo(4, 2)
createTopo(8, 4)