Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 5 | import json |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 6 | import argparse |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 7 | from collections import OrderedDict |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 8 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 9 | TEMP_NETCFG_FILE = '/tmp/bmv2-demo-cfg.json' |
| 10 | BASE_LONGITUDE = -115 |
| 11 | SWITCH_BASE_LATITUDE = 25 |
| 12 | HOST_BASE_LATITUDE = 28 |
| 13 | BASE_SHIFT = 8 |
| 14 | VLAN_NONE = -1 |
| 15 | DEFAULT_SW_BW = 50 |
| 16 | DEFAULT_HOST_BW = 25 |
| 17 | |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 18 | if 'ONOS_ROOT' not in os.environ: |
| 19 | print "Environment var $ONOS_ROOT not set" |
| 20 | exit() |
| 21 | else: |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 22 | ONOS_ROOT = os.environ["ONOS_ROOT"] |
| 23 | sys.path.append(ONOS_ROOT + "/tools/dev/mininet") |
Devin Lim | 0d944e2 | 2017-06-23 15:17:53 -0700 | [diff] [blame] | 24 | if 'RUN_PACK_PATH' not in os.environ: |
| 25 | print "Environment var $RUN_PACK_PATH not set" |
| 26 | exit() |
| 27 | else: |
| 28 | RUN_PACK_PATH = os.environ["RUN_PACK_PATH"] |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 29 | |
| 30 | from onos import ONOSCluster, ONOSCLI |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 31 | from bmv2 import ONOSBmv2Switch, ONOSHost |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 32 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 33 | from itertools import combinations |
| 34 | from time import sleep |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 35 | from subprocess import call |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 36 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 37 | from mininet.cli import CLI |
| 38 | from mininet.link import TCLink |
| 39 | from mininet.log import setLogLevel |
| 40 | from mininet.net import Mininet |
| 41 | from mininet.node import RemoteController, Host |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 42 | from mininet.topo import Topo |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 43 | |
Carmelo Cascone | 73f6b6d | 2017-08-24 13:17:55 +0200 | [diff] [blame] | 44 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 45 | class ClosTopo(Topo): |
| 46 | "2 stage Clos topology" |
| 47 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 48 | def __init__(self, args, **opts): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 49 | # Initialize topology and default options |
| 50 | Topo.__init__(self, **opts) |
| 51 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 52 | 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 Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 57 | bmv2Switches = {} |
| 58 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 59 | for switchId in bmv2SwitchIds: |
Carmelo Cascone | 73f6b6d | 2017-08-24 13:17:55 +0200 | [diff] [blame] | 60 | deviceId = int(switchId[1:]) |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 61 | # 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 Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 66 | bmv2Switches[switchId] = self.addSwitch(switchId, |
| 67 | cls=ONOSBmv2Switch, |
| 68 | loglevel="warn", |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 69 | deviceId=deviceId, |
| 70 | netcfg=False, |
| 71 | longitude=longitude, |
| 72 | latitude=latitude, |
Carmelo Cascone | 8ba7b21 | 2017-09-01 16:05:57 +0200 | [diff] [blame^] | 73 | pipeconfId=args.pipeconf_id, |
| 74 | valgrind=True, |
| 75 | netcfgSleep=0, |
| 76 | leglevel="debug") |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 77 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 78 | for i in range(1, args.size + 1): |
| 79 | for j in range(1, args.size + 1): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 80 | if i == j: |
| 81 | # 2 links |
| 82 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 83 | cls=TCLink, bw=DEFAULT_SW_BW) |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 84 | if args.with_imbalanced_striping: |
| 85 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
| 86 | cls=TCLink, bw=DEFAULT_SW_BW) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 87 | else: |
| 88 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 89 | cls=TCLink, bw=DEFAULT_SW_BW) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 90 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 91 | for hostId in range(1, args.size + 1): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 92 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 96 | self.addLink(host, bmv2Switches["s1%d" % hostId], cls=TCLink, bw=DEFAULT_HOST_BW) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 97 | |
| 98 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 99 | class DemoHost(ONOSHost): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 100 | "Demo host" |
| 101 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 102 | def __init__(self, name, **params): |
| 103 | ONOSHost.__init__(self, name, **params) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 104 | self.exectoken = "/tmp/mn-exec-token-host-%s" % name |
| 105 | self.cmd("touch %s" % self.exectoken) |
| 106 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 107 | 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 Cascone | 73f6b6d | 2017-08-24 13:17:55 +0200 | [diff] [blame] | 142 | |
| 143 | def generateNetcfg(onosIp, net, args): |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 144 | netcfg = OrderedDict() |
| 145 | netcfg['devices'] = {} |
| 146 | netcfg['links'] = {} |
| 147 | netcfg['hosts'] = {} |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 148 | # 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 Cascone | 73f6b6d | 2017-08-24 13:17:55 +0200 | [diff] [blame] | 157 | sw1Name = switchPort[0] # s11 |
| 158 | port1Name = switchPort[1] # eth0 |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 159 | 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 Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 178 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 185 | } |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 186 | |
| 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 Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 209 | netcfg["apps"] = { |
| 210 | "org.onosproject.core": { |
| 211 | "core": { |
| 212 | "linkDiscoveryMode": "STRICT" |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 217 | print "Writing network config to %s" % TEMP_NETCFG_FILE |
| 218 | with open(TEMP_NETCFG_FILE, 'w') as tempFile: |
Carmelo Cascone | 73f6b6d | 2017-08-24 13:17:55 +0200 | [diff] [blame] | 219 | json.dump(netcfg, tempFile, indent=4) |
| 220 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 221 | |
| 222 | def main(args): |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 223 | if not args.onos_ip: |
| 224 | controller = ONOSCluster('c0', 3) |
| 225 | onosIp = controller.nodes()[0].IP() |
| 226 | else: |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 227 | controller = RemoteController('c0', ip=args.onos_ip) |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 228 | onosIp = args.onos_ip |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 229 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 230 | topo = ClosTopo(args) |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 231 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 232 | net = Mininet(topo=topo, build=False, controller=[controller]) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 233 | |
| 234 | net.build() |
| 235 | net.start() |
| 236 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 237 | print "Network started" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 238 | |
Carmelo Cascone | 8ba7b21 | 2017-09-01 16:05:57 +0200 | [diff] [blame^] | 239 | 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 Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 245 | # Generate background traffic. |
Carmelo Cascone | 8ba7b21 | 2017-09-01 16:05:57 +0200 | [diff] [blame^] | 246 | sleep(5) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 247 | for (h1, h2) in combinations(net.hosts, 2): |
| 248 | h1.startPingBg(h2) |
| 249 | h2.startPingBg(h1) |
| 250 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 251 | print "Background ping started" |
| 252 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 253 | for h in net.hosts: |
| 254 | h.startIperfServer() |
| 255 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 256 | print "Iperf servers started" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 257 | |
| 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 Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 262 | if not args.onos_ip: |
| 263 | ONOSCLI(net) |
| 264 | else: |
| 265 | CLI(net) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 266 | |
| 267 | net.stop() |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 268 | call(("rm", "-f", TEMP_NETCFG_FILE)) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 269 | |
| 270 | |
| 271 | if __name__ == '__main__': |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 272 | parser = argparse.ArgumentParser( |
| 273 | description='BMv2 mininet demo script (2-stage Clos topology)') |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 274 | parser.add_argument('--onos-ip', help='ONOS-BMv2 controller IP address', |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 275 | type=str, action="store", required=False) |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 276 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 280 | parser.add_argument('--pipeconf-id', help='Pipeconf ID for switches', |
| 281 | type=str, action="store", required=False, default='') |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 282 | args = parser.parse_args() |
| 283 | setLogLevel('info') |
| 284 | main(args) |