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 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 5 | import argparse |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 6 | |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 7 | if 'ONOS_ROOT' not in os.environ: |
| 8 | print "Environment var $ONOS_ROOT not set" |
| 9 | exit() |
| 10 | else: |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 11 | ONOS_ROOT = os.environ["ONOS_ROOT"] |
| 12 | sys.path.append(ONOS_ROOT + "/tools/dev/mininet") |
| 13 | |
| 14 | from onos import ONOSCluster, ONOSCLI |
| 15 | from bmv2 import ONOSBmv2Switch |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 16 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 17 | from itertools import combinations |
| 18 | from time import sleep |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 19 | from subprocess import call |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 20 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 21 | from mininet.cli import CLI |
| 22 | from mininet.link import TCLink |
| 23 | from mininet.log import setLogLevel |
| 24 | from mininet.net import Mininet |
| 25 | from mininet.node import RemoteController, Host |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 26 | from mininet.topo import Topo, SingleSwitchTopo |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class ClosTopo(Topo): |
| 30 | "2 stage Clos topology" |
| 31 | |
| 32 | def __init__(self, **opts): |
| 33 | # Initialize topology and default options |
| 34 | Topo.__init__(self, **opts) |
| 35 | |
| 36 | bmv2SwitchIds = ["s11", "s12", "s13", "s21", "s22", "s23"] |
| 37 | |
| 38 | bmv2Switches = {} |
| 39 | |
| 40 | tport = 9090 |
| 41 | for switchId in bmv2SwitchIds: |
| 42 | bmv2Switches[switchId] = self.addSwitch(switchId, |
| 43 | cls=ONOSBmv2Switch, |
| 44 | loglevel="warn", |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 45 | deviceId=int(switchId[1:]), |
| 46 | thriftPort=tport) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 47 | tport += 1 |
| 48 | |
| 49 | for i in (1, 2, 3): |
| 50 | for j in (1, 2, 3): |
| 51 | if i == j: |
| 52 | # 2 links |
| 53 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
| 54 | cls=TCLink, bw=50) |
| 55 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
| 56 | cls=TCLink, bw=50) |
| 57 | else: |
| 58 | self.addLink(bmv2Switches["s1%d" % i], bmv2Switches["s2%d" % j], |
| 59 | cls=TCLink, bw=50) |
| 60 | |
| 61 | for hostId in (1, 2, 3): |
| 62 | host = self.addHost("h%d" % hostId, |
| 63 | cls=DemoHost, |
| 64 | ip="10.0.0.%d/24" % hostId, |
| 65 | mac='00:00:00:00:00:%02x' % hostId) |
| 66 | self.addLink(host, bmv2Switches["s1%d" % hostId], cls=TCLink, bw=22) |
| 67 | |
| 68 | |
| 69 | class DemoHost(Host): |
| 70 | "Demo host" |
| 71 | |
| 72 | def __init__(self, name, inNamespace=True, **params): |
| 73 | Host.__init__(self, name, inNamespace=inNamespace, **params) |
| 74 | self.exectoken = "/tmp/mn-exec-token-host-%s" % name |
| 75 | self.cmd("touch %s" % self.exectoken) |
| 76 | |
| 77 | def config(self, **params): |
| 78 | r = super(Host, self).config(**params) |
| 79 | |
| 80 | self.defaultIntf().rename("eth0") |
| 81 | |
| 82 | for off in ["rx", "tx", "sg"]: |
| 83 | cmd = "/sbin/ethtool --offload eth0 %s off" % off |
| 84 | self.cmd(cmd) |
| 85 | |
| 86 | # disable IPv6 |
| 87 | self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1") |
| 88 | self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1") |
| 89 | self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1") |
| 90 | |
| 91 | return r |
| 92 | |
| 93 | def startPingBg(self, h): |
| 94 | self.cmd(self.getInfiniteCmdBg("ping -i0.5 %s" % h.IP())) |
| 95 | self.cmd(self.getInfiniteCmdBg("arping -w5000000 %s" % h.IP())) |
| 96 | |
| 97 | def startIperfServer(self): |
| 98 | self.cmd(self.getInfiniteCmdBg("iperf3 -s")) |
| 99 | |
| 100 | def startIperfClient(self, h, flowBw="512k", numFlows=5, duration=5): |
| 101 | iperfCmd = "iperf3 -c{} -b{} -P{} -t{}".format(h.IP(), flowBw, numFlows, duration) |
| 102 | self.cmd(self.getInfiniteCmdBg(iperfCmd, sleep=0)) |
| 103 | |
| 104 | def stop(self): |
| 105 | self.cmd("killall iperf3") |
| 106 | self.cmd("killall ping") |
| 107 | self.cmd("killall arping") |
| 108 | |
| 109 | def describe(self): |
| 110 | print "**********" |
| 111 | print self.name |
| 112 | print "default interface: %s\t%s\t%s" % ( |
| 113 | self.defaultIntf().name, |
| 114 | self.defaultIntf().IP(), |
| 115 | self.defaultIntf().MAC() |
| 116 | ) |
| 117 | print "**********" |
| 118 | |
| 119 | def getInfiniteCmdBg(self, cmd, logfile="/dev/null", sleep=1): |
| 120 | return "(while [ -e {} ]; " \ |
| 121 | "do {}; " \ |
| 122 | "sleep {}; " \ |
| 123 | "done;) > {} 2>&1 &".format(self.exectoken, cmd, sleep, logfile) |
| 124 | |
| 125 | def getCmdBg(self, cmd, logfile="/dev/null"): |
| 126 | return "{} > {} 2>&1 &".format(cmd, logfile) |
| 127 | |
| 128 | |
| 129 | def main(args): |
| 130 | topo = ClosTopo() |
| 131 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 132 | if not args.onos_ip: |
| 133 | controller = ONOSCluster('c0', 3) |
| 134 | onosIp = controller.nodes()[0].IP() |
| 135 | else: |
| 136 | controller = RemoteController('c0', ip=args.onos_ip, port=args.onos_port) |
| 137 | onosIp = args.onos_ip |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 138 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 139 | net = Mininet(topo=topo, build=False, controller=[controller]) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 140 | |
| 141 | net.build() |
| 142 | net.start() |
| 143 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 144 | print "Network started" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 145 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 146 | # Generate background traffic. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 147 | sleep(3) |
| 148 | for (h1, h2) in combinations(net.hosts, 2): |
| 149 | h1.startPingBg(h2) |
| 150 | h2.startPingBg(h1) |
| 151 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 152 | print "Background ping started" |
| 153 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 154 | for h in net.hosts: |
| 155 | h.startIperfServer() |
| 156 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 157 | print "Iperf servers started" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 158 | |
| 159 | # sleep(4) |
| 160 | # print "Starting traffic from h1 to h3..." |
| 161 | # net.hosts[0].startIperfClient(net.hosts[-1], flowBw="200k", numFlows=100, duration=10) |
| 162 | |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 163 | print "Setting netcfg..." |
| 164 | call(("onos-netcfg", onosIp, |
| 165 | "%s/tools/test/topos/bmv2-demo-cfg.json" % ONOS_ROOT)) |
| 166 | |
| 167 | if not args.onos_ip: |
| 168 | ONOSCLI(net) |
| 169 | else: |
| 170 | CLI(net) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 171 | |
| 172 | net.stop() |
| 173 | |
| 174 | |
| 175 | if __name__ == '__main__': |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 176 | parser = argparse.ArgumentParser( |
| 177 | description='BMv2 mininet demo script (2-stage Clos topology)') |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 178 | parser.add_argument('--onos-ip', help='ONOS-BMv2 controller IP address', |
Carmelo Cascone | 12e4d8d | 2016-07-05 15:55:15 -0700 | [diff] [blame] | 179 | type=str, action="store", required=False) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 180 | parser.add_argument('--onos-port', help='ONOS-BMv2 controller port', |
| 181 | type=int, action="store", default=40123) |
| 182 | args = parser.parse_args() |
| 183 | setLogLevel('info') |
| 184 | main(args) |