blob: d1aa5799f088d1fa28fd53bdbc10ec4d89f78cb8 [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
Carmelo Cascone785fada2016-06-16 18:34:16 -07005import argparse
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07006
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07007if 'ONOS_ROOT' not in os.environ:
8 print "Environment var $ONOS_ROOT not set"
9 exit()
10else:
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070011 ONOS_ROOT = os.environ["ONOS_ROOT"]
12 sys.path.append(ONOS_ROOT + "/tools/dev/mininet")
13
14from onos import ONOSCluster, ONOSCLI
15from bmv2 import ONOSBmv2Switch
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070016
Carmelo Cascone785fada2016-06-16 18:34:16 -070017from itertools import combinations
18from time import sleep
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070019from subprocess import call
Carmelo Cascone785fada2016-06-16 18:34:16 -070020
Carmelo Cascone785fada2016-06-16 18:34:16 -070021from mininet.cli import CLI
22from mininet.link import TCLink
23from mininet.log import setLogLevel
24from mininet.net import Mininet
25from mininet.node import RemoteController, Host
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -070026from mininet.topo import Topo, SingleSwitchTopo
Carmelo Cascone785fada2016-06-16 18:34:16 -070027
28
29class 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 Cascone977ae3f2016-06-23 19:28:28 -070045 deviceId=int(switchId[1:]),
46 thriftPort=tport)
Carmelo Cascone785fada2016-06-16 18:34:16 -070047 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
69class 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
129def main(args):
130 topo = ClosTopo()
131
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700132 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 Cascone785fada2016-06-16 18:34:16 -0700138
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700139 net = Mininet(topo=topo, build=False, controller=[controller])
Carmelo Cascone785fada2016-06-16 18:34:16 -0700140
141 net.build()
142 net.start()
143
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700144 print "Network started"
Carmelo Cascone785fada2016-06-16 18:34:16 -0700145
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700146 # Generate background traffic.
Carmelo Cascone785fada2016-06-16 18:34:16 -0700147 sleep(3)
148 for (h1, h2) in combinations(net.hosts, 2):
149 h1.startPingBg(h2)
150 h2.startPingBg(h1)
151
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700152 print "Background ping started"
153
Carmelo Cascone785fada2016-06-16 18:34:16 -0700154 for h in net.hosts:
155 h.startIperfServer()
156
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700157 print "Iperf servers started"
Carmelo Cascone785fada2016-06-16 18:34:16 -0700158
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 Cascone12e4d8d2016-07-05 15:55:15 -0700163 print "Setting netcfg..."
tjian33917d32017-02-27 15:46:30 +0800164 call(("%s/tools/test/bin/onos-netcfg" % ONOS_ROOT, onosIp,
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700165 "%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 Cascone785fada2016-06-16 18:34:16 -0700171
172 net.stop()
173
174
175if __name__ == '__main__':
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700176 parser = argparse.ArgumentParser(
177 description='BMv2 mininet demo script (2-stage Clos topology)')
Carmelo Cascone785fada2016-06-16 18:34:16 -0700178 parser.add_argument('--onos-ip', help='ONOS-BMv2 controller IP address',
Carmelo Cascone12e4d8d2016-07-05 15:55:15 -0700179 type=str, action="store", required=False)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700180 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)