blob: 2864dca8fb73b6aed09d0af2ab012c1c0943b3ff [file] [log] [blame]
Carmelo Cascone785fada2016-06-16 18:34:16 -07001#!/usr/bin/python
2
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07003import os
4import sys
Yi Tseng7875cb72017-08-08 10:15:58 -07005import json
Carmelo Cascone785fada2016-06-16 18:34:16 -07006import argparse
Carmelo Cascone34433252017-08-25 20:27:18 +02007from collections import OrderedDict
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07008
Yi Tseng7875cb72017-08-08 10:15:58 -07009TEMP_NETCFG_FILE = '/tmp/bmv2-demo-cfg.json'
10BASE_LONGITUDE = -115
11SWITCH_BASE_LATITUDE = 25
12HOST_BASE_LATITUDE = 28
13BASE_SHIFT = 8
14VLAN_NONE = -1
15DEFAULT_SW_BW = 50
16DEFAULT_HOST_BW = 25
17
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070018if 'ONOS_ROOT' not in os.environ:
19 print "Environment var $ONOS_ROOT not set"
20 exit()
21else:
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070022 ONOS_ROOT = os.environ["ONOS_ROOT"]
23 sys.path.append(ONOS_ROOT + "/tools/dev/mininet")
Devin Lim0d944e22017-06-23 15:17:53 -070024if 'RUN_PACK_PATH' not in os.environ:
25 print "Environment var $RUN_PACK_PATH not set"
26 exit()
27else:
28 RUN_PACK_PATH = os.environ["RUN_PACK_PATH"]
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070029
30from onos import ONOSCluster, ONOSCLI
Carmelo Cascone34433252017-08-25 20:27:18 +020031from bmv2 import ONOSBmv2Switch, ONOSHost
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070032
Carmelo Cascone785fada2016-06-16 18:34:16 -070033from itertools import combinations
34from time import sleep
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070035from subprocess import call
Carmelo Cascone785fada2016-06-16 18:34:16 -070036
Carmelo Cascone785fada2016-06-16 18:34:16 -070037from mininet.cli import CLI
38from mininet.link import TCLink
39from mininet.log import setLogLevel
40from mininet.net import Mininet
41from mininet.node import RemoteController, Host
Yi Tseng7875cb72017-08-08 10:15:58 -070042from mininet.topo import Topo
Carmelo Cascone785fada2016-06-16 18:34:16 -070043
Carmelo Cascone73f6b6d2017-08-24 13:17:55 +020044
Carmelo Cascone785fada2016-06-16 18:34:16 -070045class ClosTopo(Topo):
46 "2 stage Clos topology"
47
Carmelo Cascone34433252017-08-25 20:27:18 +020048 def __init__(self, args, **opts):
Carmelo Cascone785fada2016-06-16 18:34:16 -070049 # Initialize topology and default options
50 Topo.__init__(self, **opts)
51
Carmelo Cascone34433252017-08-25 20:27:18 +020052 bmv2SwitchIds = []
53 for row in (1, 2):
54 for col in range(1, args.size + 1):
55 bmv2SwitchIds.append("s%d%d" % (row, col))
56
Carmelo Cascone785fada2016-06-16 18:34:16 -070057 bmv2Switches = {}
58
Carmelo Cascone785fada2016-06-16 18:34:16 -070059 for switchId in bmv2SwitchIds:
Carmelo Cascone73f6b6d2017-08-24 13:17:55 +020060 deviceId = int(switchId[1:])
Yi Tseng7875cb72017-08-08 10:15:58 -070061 # Use first number in device id to calculate latitude (row number)
62 latitude = SWITCH_BASE_LATITUDE + (deviceId // 10) * BASE_SHIFT
63
64 # Use second number in device id to calculate longitude (column number)
65 longitude = BASE_LONGITUDE + (deviceId % 10) * BASE_SHIFT
Carmelo Cascone785fada2016-06-16 18:34:16 -070066 bmv2Switches[switchId] = self.addSwitch(switchId,
67 cls=ONOSBmv2Switch,
68 loglevel="warn",
Yi Tseng7875cb72017-08-08 10:15:58 -070069 deviceId=deviceId,
70 netcfg=False,
71 longitude=longitude,
72 latitude=latitude,
Carmelo Cascone8ba7b212017-09-01 16:05:57 +020073 pipeconfId=args.pipeconf_id,
74 valgrind=True,
75 netcfgSleep=0,
76 leglevel="debug")
Carmelo Cascone785fada2016-06-16 18:34:16 -070077
Carmelo Cascone34433252017-08-25 20:27:18 +020078 for i in range(1, args.size + 1):
79 for j in range(1, args.size + 1):
Carmelo Cascone785fada2016-06-16 18:34:16 -070080 if i == j:
81 # 2 links
82 self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j],
Yi Tseng7875cb72017-08-08 10:15:58 -070083 cls=TCLink, bw=DEFAULT_SW_BW)
Carmelo Cascone34433252017-08-25 20:27:18 +020084 if args.with_imbalanced_striping:
85 self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j],
86 cls=TCLink, bw=DEFAULT_SW_BW)
Carmelo Cascone785fada2016-06-16 18:34:16 -070087 else:
88 self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j],
Yi Tseng7875cb72017-08-08 10:15:58 -070089 cls=TCLink, bw=DEFAULT_SW_BW)
Carmelo Cascone785fada2016-06-16 18:34:16 -070090
Carmelo Cascone34433252017-08-25 20:27:18 +020091 for hostId in range(1, args.size + 1):
Carmelo Cascone785fada2016-06-16 18:34:16 -070092 host = self.addHost("h%d" % hostId,
93 cls=DemoHost,
94 ip="10.0.0.%d/24" % hostId,
95 mac='00:00:00:00:00:%02x' % hostId)
Yi Tseng7875cb72017-08-08 10:15:58 -070096 self.addLink(host, bmv2Switches["s1%d" % hostId], cls=TCLink, bw=DEFAULT_HOST_BW)
Carmelo Cascone785fada2016-06-16 18:34:16 -070097
98
Carmelo Cascone34433252017-08-25 20:27:18 +020099class DemoHost(ONOSHost):
Carmelo Cascone785fada2016-06-16 18:34:16 -0700100 "Demo host"
101
Carmelo Cascone34433252017-08-25 20:27:18 +0200102 def __init__(self, name, **params):
103 ONOSHost.__init__(self, name, **params)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700104 self.exectoken = "/tmp/mn-exec-token-host-%s" % name
105 self.cmd("touch %s" % self.exectoken)
106
Carmelo Cascone785fada2016-06-16 18:34:16 -0700107 def startPingBg(self, h):
108 self.cmd(self.getInfiniteCmdBg("ping -i0.5 %s" % h.IP()))
109 self.cmd(self.getInfiniteCmdBg("arping -w5000000 %s" % h.IP()))
110
111 def startIperfServer(self):
112 self.cmd(self.getInfiniteCmdBg("iperf3 -s"))
113
114 def startIperfClient(self, h, flowBw="512k", numFlows=5, duration=5):
115 iperfCmd = "iperf3 -c{} -b{} -P{} -t{}".format(h.IP(), flowBw, numFlows, duration)
116 self.cmd(self.getInfiniteCmdBg(iperfCmd, sleep=0))
117
118 def stop(self):
119 self.cmd("killall iperf3")
120 self.cmd("killall ping")
121 self.cmd("killall arping")
122
123 def describe(self):
124 print "**********"
125 print self.name
126 print "default interface: %s\t%s\t%s" % (
127 self.defaultIntf().name,
128 self.defaultIntf().IP(),
129 self.defaultIntf().MAC()
130 )
131 print "**********"
132
133 def getInfiniteCmdBg(self, cmd, logfile="/dev/null", sleep=1):
134 return "(while [ -e {} ]; " \
135 "do {}; " \
136 "sleep {}; " \
137 "done;) > {} 2>&1 &".format(self.exectoken, cmd, sleep, logfile)
138
139 def getCmdBg(self, cmd, logfile="/dev/null"):
140 return "{} > {} 2>&1 &".format(cmd, logfile)
141
Carmelo Cascone73f6b6d2017-08-24 13:17:55 +0200142
143def generateNetcfg(onosIp, net, args):
Carmelo Cascone34433252017-08-25 20:27:18 +0200144 netcfg = OrderedDict()
145 netcfg['devices'] = {}
146 netcfg['links'] = {}
147 netcfg['hosts'] = {}
Yi Tseng7875cb72017-08-08 10:15:58 -0700148 # Device configs
149 for sw in net.switches:
150 srcIp = sw.getSourceIp(onosIp)
151 netcfg['devices'][sw.onosDeviceId] = sw.getDeviceConfig(srcIp)
152
153 hostLocations = {}
154 # Link configs
155 for link in net.links:
156 switchPort = link.intf1.name.split('-')
Carmelo Cascone73f6b6d2017-08-24 13:17:55 +0200157 sw1Name = switchPort[0] # s11
158 port1Name = switchPort[1] # eth0
Yi Tseng7875cb72017-08-08 10:15:58 -0700159 port1 = port1Name[3:]
160 switchPort = link.intf2.name.split('-')
161 sw2Name = switchPort[0]
162 port2Name = switchPort[1]
163 port2 = port2Name[3:]
164 sw1 = net[sw1Name]
165 sw2 = net[sw2Name]
166 if isinstance(sw1, Host):
167 # record host location and ignore it
168 # e.g. {'h1': 'device:bmv2:11'}
169 hostLocations[sw1.name] = '%s/%s' % (sw2.onosDeviceId, port2)
170 continue
171
172 if isinstance(sw2, Host):
173 # record host location and ignore it
174 # e.g. {'h1': 'device:bmv2:11'}
175 hostLocations[sw2.name] = '%s/%s' % (sw1.onosDeviceId, port1)
176 continue
177
Carmelo Cascone34433252017-08-25 20:27:18 +0200178 for linkId in ('%s/%s-%s/%s' % (sw1.onosDeviceId, port1, sw2.onosDeviceId, port2),
179 '%s/%s-%s/%s' % (sw2.onosDeviceId, port2, sw1.onosDeviceId, port1)):
180 netcfg['links'][linkId] = {
181 'basic': {
182 'type': 'DIRECT',
183 'bandwidth': DEFAULT_SW_BW
184 }
Yi Tseng7875cb72017-08-08 10:15:58 -0700185 }
Yi Tseng7875cb72017-08-08 10:15:58 -0700186
187 # Host configs
188 longitude = BASE_LONGITUDE
189 for host in net.hosts:
190 longitude = longitude + BASE_SHIFT
191 hostDefaultIntf = host.defaultIntf()
192 hostMac = host.MAC(hostDefaultIntf)
193 hostIp = host.IP(hostDefaultIntf)
194 hostId = '%s/%d' % (hostMac, VLAN_NONE)
195 location = hostLocations[host.name]
196
197 # use host Id to generate host location
198 hostConfig = {
199 'basic': {
200 'locations': [location],
201 'ips': [hostIp],
202 'name': host.name,
203 'latitude': HOST_BASE_LATITUDE,
204 'longitude': longitude
205 }
206 }
207 netcfg['hosts'][hostId] = hostConfig
208
Carmelo Cascone34433252017-08-25 20:27:18 +0200209 netcfg["apps"] = {
210 "org.onosproject.core": {
211 "core": {
212 "linkDiscoveryMode": "STRICT"
213 }
214 }
215 }
216
Yi Tseng7875cb72017-08-08 10:15:58 -0700217 print "Writing network config to %s" % TEMP_NETCFG_FILE
218 with open(TEMP_NETCFG_FILE, 'w') as tempFile:
Carmelo Cascone73f6b6d2017-08-24 13:17:55 +0200219 json.dump(netcfg, tempFile, indent=4)
220
Carmelo Cascone785fada2016-06-16 18:34:16 -0700221
222def main(args):
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700223 if not args.onos_ip:
224 controller = ONOSCluster('c0', 3)
225 onosIp = controller.nodes()[0].IP()
226 else:
Yi Tseng7875cb72017-08-08 10:15:58 -0700227 controller = RemoteController('c0', ip=args.onos_ip)
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700228 onosIp = args.onos_ip
Carmelo Cascone785fada2016-06-16 18:34:16 -0700229
Carmelo Cascone34433252017-08-25 20:27:18 +0200230 topo = ClosTopo(args)
Yi Tseng7875cb72017-08-08 10:15:58 -0700231
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700232 net = Mininet(topo=topo, build=False, controller=[controller])
Carmelo Cascone785fada2016-06-16 18:34:16 -0700233
234 net.build()
235 net.start()
236
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700237 print "Network started"
Carmelo Cascone785fada2016-06-16 18:34:16 -0700238
Carmelo Cascone8ba7b212017-09-01 16:05:57 +0200239 generateNetcfg(onosIp, net, args)
240
241 sleep(30)
242 print "Uploading netcfg..."
243 call(("%s/onos-netcfg" % RUN_PACK_PATH, onosIp, TEMP_NETCFG_FILE))
244
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700245 # Generate background traffic.
Carmelo Cascone8ba7b212017-09-01 16:05:57 +0200246 sleep(5)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700247 for (h1, h2) in combinations(net.hosts, 2):
248 h1.startPingBg(h2)
249 h2.startPingBg(h1)
250
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700251 print "Background ping started"
252
Carmelo Cascone785fada2016-06-16 18:34:16 -0700253 for h in net.hosts:
254 h.startIperfServer()
255
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700256 print "Iperf servers started"
Carmelo Cascone785fada2016-06-16 18:34:16 -0700257
258 # sleep(4)
259 # print "Starting traffic from h1 to h3..."
260 # net.hosts[0].startIperfClient(net.hosts[-1], flowBw="200k", numFlows=100, duration=10)
261
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700262 if not args.onos_ip:
263 ONOSCLI(net)
264 else:
265 CLI(net)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700266
267 net.stop()
Yi Tseng7875cb72017-08-08 10:15:58 -0700268 call(("rm", "-f", TEMP_NETCFG_FILE))
Carmelo Cascone785fada2016-06-16 18:34:16 -0700269
270
271if __name__ == '__main__':
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700272 parser = argparse.ArgumentParser(
273 description='BMv2 mininet demo script (2-stage Clos topology)')
Carmelo Cascone785fada2016-06-16 18:34:16 -0700274 parser.add_argument('--onos-ip', help='ONOS-BMv2 controller IP address',
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700275 type=str, action="store", required=False)
Carmelo Cascone34433252017-08-25 20:27:18 +0200276 parser.add_argument('--size', help='Number of leaf/spine switches',
277 type=int, action="store", required=False, default=2)
278 parser.add_argument('--with-imbalanced-striping', help='Topology with imbalanced striping',
279 type=bool, action="store", required=False, default=False)
Yi Tseng7875cb72017-08-08 10:15:58 -0700280 parser.add_argument('--pipeconf-id', help='Pipeconf ID for switches',
281 type=str, action="store", required=False, default='')
Carmelo Cascone785fada2016-06-16 18:34:16 -0700282 args = parser.parse_args()
283 setLogLevel('info')
284 main(args)