blob: 7066f61ea75f9a15c2a18a8954bac9ce4694f6e7 [file] [log] [blame]
Carmelo Casconeb7524272017-06-05 16:53:13 -04001import os
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07002import socket
Carmelo Casconeb7524272017-06-05 16:53:13 -04003import re
4import json
Brian O'Connor71167f92017-06-16 14:55:00 -07005import urllib2
Carmelo Cascone977ae3f2016-06-23 19:28:28 -07006
Carmelo Cascone46d360b2017-08-29 20:20:32 +02007import time
8
Carmelo Casconeb7524272017-06-05 16:53:13 -04009from mininet.log import info, warn, error
Carmelo Cascone34433252017-08-25 20:27:18 +020010from mininet.node import Switch, Host
Carmelo Cascone785fada2016-06-16 18:34:16 -070011
Carmelo Casconeb7524272017-06-05 16:53:13 -040012if 'ONOS_ROOT' not in os.environ:
13 error("ERROR: environment var $ONOS_ROOT not set")
14 exit()
15
Carmelo Cascone75e97992017-06-05 02:32:47 -040016BMV2_TARGET = 'simple_switch_grpc'
Carmelo Casconeb7524272017-06-05 16:53:13 -040017ONOS_ROOT = os.environ["ONOS_ROOT"]
Carmelo Cascone569d4ad2017-07-10 16:09:00 -040018CPU_PORT = 255
Carmelo Cascone34433252017-08-25 20:27:18 +020019PKT_BYTES_TO_DUMP = 80
Carmelo Cascone46d360b2017-08-29 20:20:32 +020020VALGRIND_PREFIX = 'valgrind --leak-check=yes'
21VALGRIND_SLEEP = 10 # seconds
22
23
24def parseBoolean(value):
25 if value in ['1', 1, 'true', 'True']:
26 return True
27 else:
28 return False
Carmelo Cascone34433252017-08-25 20:27:18 +020029
30
31class ONOSHost(Host):
32 def __init__(self, name, inNamespace=True, **params):
33 Host.__init__(self, name, inNamespace=inNamespace, **params)
34
35 def config(self, **params):
36 r = super(Host, self).config(**params)
37 for off in ["rx", "tx", "sg"]:
Carmelo Cascone6ec8f8f2017-11-22 14:27:06 -080038 cmd = "/sbin/ethtool --offload %s %s off"\
39 % (self.defaultIntf(), off)
Carmelo Cascone34433252017-08-25 20:27:18 +020040 self.cmd(cmd)
41 # disable IPv6
42 self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
43 self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
44 self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1")
45 return r
Carmelo Casconeb7524272017-06-05 16:53:13 -040046
Carmelo Cascone785fada2016-06-16 18:34:16 -070047
48class ONOSBmv2Switch(Switch):
Carmelo Casconeb7524272017-06-05 16:53:13 -040049 """BMv2 software switch with gRPC server"""
Carmelo Cascone785fada2016-06-16 18:34:16 -070050
Carmelo Cascone785fada2016-06-16 18:34:16 -070051 deviceId = 0
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070052 instanceCount = 0
Carmelo Cascone785fada2016-06-16 18:34:16 -070053
Carmelo Casconefb76b042017-07-17 19:42:00 -040054 def __init__(self, name, json=None, debugger=False, loglevel="warn", elogger=False,
Carmelo Cascone46d360b2017-08-29 20:20:32 +020055 persistent=False, grpcPort=None, thriftPort=None, netcfg=True, dryrun=False,
Carmelo Cascone6ec8f8f2017-11-22 14:27:06 -080056 pipeconfId="", pktdump=False, valgrind=False, netcfgDelay=0,
57 **kwargs):
Carmelo Cascone785fada2016-06-16 18:34:16 -070058 Switch.__init__(self, name, **kwargs)
Carmelo Casconeb7524272017-06-05 16:53:13 -040059 self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort
Carmelo Casconeaaf6d982017-07-29 19:52:58 -040060 self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort
Carmelo Cascone75e97992017-06-05 02:32:47 -040061 if self.dpid:
62 self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070063 else:
Carmelo Cascone75e97992017-06-05 02:32:47 -040064 self.deviceId = ONOSBmv2Switch.deviceId
65 ONOSBmv2Switch.deviceId += 1
Carmelo Casconefb76b042017-07-17 19:42:00 -040066 self.json = json
Carmelo Cascone46d360b2017-08-29 20:20:32 +020067 self.debugger = parseBoolean(debugger)
Carmelo Cascone785fada2016-06-16 18:34:16 -070068 self.loglevel = loglevel
69 self.logfile = '/tmp/bmv2-%d.log' % self.deviceId
Carmelo Cascone46d360b2017-08-29 20:20:32 +020070 self.elogger = parseBoolean(elogger)
71 self.pktdump = parseBoolean(pktdump)
72 self.persistent = parseBoolean(persistent)
73 self.netcfg = parseBoolean(netcfg)
74 self.dryrun = parseBoolean(dryrun)
75 self.valgrind = parseBoolean(valgrind)
Carmelo Cascone6ec8f8f2017-11-22 14:27:06 -080076 self.netcfgDelay = netcfgDelay
Carmelo Casconeb7524272017-06-05 16:53:13 -040077 self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId
Yi Tseng7875cb72017-08-08 10:15:58 -070078 self.pipeconfId = pipeconfId
Carmelo Cascone785fada2016-06-16 18:34:16 -070079 if persistent:
80 self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId
81 self.cmd("touch %s" % self.exectoken)
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070082 # Store thrift port for future uses.
Carmelo Cascone75e97992017-06-05 02:32:47 -040083 self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId))
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070084
Yi Tseng7875cb72017-08-08 10:15:58 -070085 if 'longitude' in kwargs:
86 self.longitude = kwargs['longitude']
87 else:
88 self.longitude = None
89
90 if 'latitude' in kwargs:
91 self.latitude = kwargs['latitude']
92 else:
93 self.latitude = None
94
95 self.onosDeviceId = "device:bmv2:%d" % self.deviceId
96
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070097 @classmethod
98 def pickUnusedPort(cls):
99 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
100 s.bind(('localhost', 0))
101 addr, port = s.getsockname()
102 s.close()
103 return port
Carmelo Cascone785fada2016-06-16 18:34:16 -0700104
Carmelo Casconeb7524272017-06-05 16:53:13 -0400105 def getSourceIp(self, dstIP):
106 """
107 Queries the Linux routing table to get the source IP that can talk with dstIP, and vice
108 versa.
109 """
110 ipRouteOut = self.cmd('ip route get %s' % dstIP)
111 r = re.search(r"src (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", ipRouteOut)
112 return r.group(1) if r else None
113
Yi Tseng7875cb72017-08-08 10:15:58 -0700114 def getDeviceConfig(self, srcIP):
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200115 portData = {}
116 portId = 1
117 for intfName in self.intfNames():
118 if intfName == 'lo':
119 continue
120 portData[str(portId)] = {
121 "number": portId,
122 "name": intfName,
123 "enabled": True,
124 "removed": False,
125 "type": "copper",
126 "speed": 10000
127 }
128 portId += 1
129
Yi Tseng7875cb72017-08-08 10:15:58 -0700130 basicCfg = {
131 "driver": "bmv2"
132 }
133
134 if self.longitude and self.latitude:
135 basicCfg["longitude"] = self.longitude
136 basicCfg["latitude"] = self.latitude
137
138 cfgData = {
Carmelo Cascone34433252017-08-25 20:27:18 +0200139 "generalprovider": {
140 "p4runtime": {
141 "ip": srcIP,
142 "port": self.grpcPort,
143 "deviceId": self.deviceId,
144 "deviceKeyId": "p4runtime:%s" % self.onosDeviceId
145 }
146 },
147 "piPipeconf": {
148 "piPipeconfId": self.pipeconfId
149 },
150 "basic": basicCfg,
151 "ports": portData
Yi Tseng7875cb72017-08-08 10:15:58 -0700152 }
153
154 return cfgData
155
156 def doOnosNetcfg(self, controllerIP):
157 """
158 Notifies ONOS about the new device via Netcfg.
159 """
160 srcIP = self.getSourceIp(controllerIP)
161 if not srcIP:
162 warn("WARN: unable to get device IP address, won't do onos-netcfg")
163 return
164
Carmelo Casconea11279b2017-06-22 04:30:08 -0400165 cfgData = {
166 "devices": {
Yi Tseng7875cb72017-08-08 10:15:58 -0700167 self.onosDeviceId: self.getDeviceConfig(srcIP)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400168 }
Carmelo Casconea11279b2017-06-22 04:30:08 -0400169 }
Carmelo Casconeb7524272017-06-05 16:53:13 -0400170 with open(self.netcfgfile, 'w') as fp:
171 json.dump(cfgData, fp, indent=4)
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200172
173 if not self.netcfg:
174 # Do not push config to ONOS.
175 return
176
Brian O'Connor71167f92017-06-16 14:55:00 -0700177 # Build netcfg URL
178 url = 'http://%s:8181/onos/v1/network/configuration/' % controllerIP
179 # Instantiate password manager for HTTP auth
180 pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
181 pm.add_password(None, url, os.environ['ONOS_WEB_USER'], os.environ['ONOS_WEB_PASS'])
182 urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(pm)))
183 # Push config data to controller
Carmelo Casconea11279b2017-06-22 04:30:08 -0400184 req = urllib2.Request(url, json.dumps(cfgData), {'Content-Type': 'application/json'})
185 try:
186 f = urllib2.urlopen(req)
187 print f.read()
188 f.close()
189 except urllib2.URLError as e:
190 warn("WARN: unable to push config to ONOS (%s)" % e.reason)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400191
Carmelo Cascone785fada2016-06-16 18:34:16 -0700192 def start(self, controllers):
Carmelo Cascone75e97992017-06-05 02:32:47 -0400193 args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)]
Carmelo Cascone785fada2016-06-16 18:34:16 -0700194 for port, intf in self.intfs.items():
195 if not intf.IP():
196 args.append('-i %d@%s' % (port, intf.name))
Carmelo Cascone785fada2016-06-16 18:34:16 -0700197 if self.elogger:
198 nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId
199 args.append('--nanolog %s' % nanomsg)
200 if self.debugger:
201 args.append('--debugger')
Carmelo Casconefb76b042017-07-17 19:42:00 -0400202 args.append('--log-console')
Carmelo Cascone34433252017-08-25 20:27:18 +0200203 if self.pktdump:
204 args.append('--pcap --dump-packet-data %d' % PKT_BYTES_TO_DUMP)
Carmelo Cascone9e6621f2017-06-27 16:06:33 -0400205 args.append('-L%s' % self.loglevel)
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400206 args.append('--thrift-port %d' % self.thriftPort)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400207 if not self.json:
208 args.append('--no-p4')
209 else:
210 args.append(self.json)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400211
212 # gRPC target-specific options.
Carmelo Cascone785fada2016-06-16 18:34:16 -0700213 args.append('--')
Carmelo Cascone569d4ad2017-07-10 16:09:00 -0400214 args.append('--cpu-port %d' % CPU_PORT)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400215 args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700216
217 bmv2cmd = " ".join(args)
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200218 if self.valgrind:
219 bmv2cmd = "%s %s" % (VALGRIND_PREFIX, bmv2cmd)
220 if self.dryrun:
221 info("\n*** DRY RUN (not executing bmv2)")
Carmelo Cascone6e854042017-09-11 21:37:53 +0200222 info("\nStarting BMv2 target: %s\n" % bmv2cmd)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400223
Carmelo Cascone785fada2016-06-16 18:34:16 -0700224 if self.persistent:
Carmelo Casconeb7524272017-06-05 16:53:13 -0400225 # Bash loop to re-exec the switch if it crashes.
Carmelo Casconefb76b042017-07-17 19:42:00 -0400226 bmv2cmd = "(while [ -e {} ]; do {} ; sleep 1; done;)".format(self.exectoken, bmv2cmd)
227
228 cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700229
Carmelo Casconeb7524272017-06-05 16:53:13 -0400230 # Starts the switch.
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200231 if not self.dryrun:
232 out = self.cmd(cmdStr)
233 if out:
234 print out
Carmelo Cascone6ec8f8f2017-11-22 14:27:06 -0800235 if self.netcfg:
236 if self.valgrind:
237 # With valgrind, it takes some time before the gRPC server is available.
238 # Wait before pushing the netcfg.
239 info("\n*** Waiting %d seconds before pushing the config to ONOS...\n" % VALGRIND_SLEEP)
240 time.sleep(VALGRIND_SLEEP)
241 time.sleep(self.netcfgDelay)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400242
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200243 try: # onos.py
244 clist = controllers[0].nodes()
245 except AttributeError:
246 clist = controllers
247 assert len(clist) > 0
248 cip = clist[0].IP()
249 self.doOnosNetcfg(cip)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400250
251 def stop(self, deleteIntfs=True):
252 """Terminate switch."""
Carmelo Cascone977ae3f2016-06-23 19:28:28 -0700253 self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400254 self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId)
Carmelo Cascone75e97992017-06-05 02:32:47 -0400255 self.cmd('kill %' + BMV2_TARGET)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400256 Switch.stop(self, deleteIntfs)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700257
258
Carmelo Casconeb7524272017-06-05 16:53:13 -0400259# Exports for bin/mn
Carmelo Cascone785fada2016-06-16 18:34:16 -0700260switches = {'onosbmv2': ONOSBmv2Switch}
Carmelo Cascone34433252017-08-25 20:27:18 +0200261hosts = {'onoshost': ONOSHost}