blob: e9426469dbeee65d5c3fae8ee1c92b332674b632 [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"]:
38 cmd = "/sbin/ethtool --offload %s %s off" % (self.defaultIntf(), off)
39 self.cmd(cmd)
40 # disable IPv6
41 self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
42 self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
43 self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1")
44 return r
Carmelo Casconeb7524272017-06-05 16:53:13 -040045
Carmelo Cascone785fada2016-06-16 18:34:16 -070046
47class ONOSBmv2Switch(Switch):
Carmelo Casconeb7524272017-06-05 16:53:13 -040048 """BMv2 software switch with gRPC server"""
Carmelo Cascone785fada2016-06-16 18:34:16 -070049
Carmelo Cascone785fada2016-06-16 18:34:16 -070050 deviceId = 0
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070051 instanceCount = 0
Carmelo Cascone785fada2016-06-16 18:34:16 -070052
Carmelo Casconefb76b042017-07-17 19:42:00 -040053 def __init__(self, name, json=None, debugger=False, loglevel="warn", elogger=False,
Carmelo Cascone46d360b2017-08-29 20:20:32 +020054 persistent=False, grpcPort=None, thriftPort=None, netcfg=True, dryrun=False,
55 pipeconfId="", pktdump=False, valgrind=False, **kwargs):
Carmelo Cascone785fada2016-06-16 18:34:16 -070056 Switch.__init__(self, name, **kwargs)
Carmelo Casconeb7524272017-06-05 16:53:13 -040057 self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort
Carmelo Casconeaaf6d982017-07-29 19:52:58 -040058 self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort
Carmelo Cascone75e97992017-06-05 02:32:47 -040059 if self.dpid:
60 self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16)
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070061 else:
Carmelo Cascone75e97992017-06-05 02:32:47 -040062 self.deviceId = ONOSBmv2Switch.deviceId
63 ONOSBmv2Switch.deviceId += 1
Carmelo Casconefb76b042017-07-17 19:42:00 -040064 self.json = json
Carmelo Cascone46d360b2017-08-29 20:20:32 +020065 self.debugger = parseBoolean(debugger)
Carmelo Cascone785fada2016-06-16 18:34:16 -070066 self.loglevel = loglevel
67 self.logfile = '/tmp/bmv2-%d.log' % self.deviceId
Carmelo Cascone46d360b2017-08-29 20:20:32 +020068 self.elogger = parseBoolean(elogger)
69 self.pktdump = parseBoolean(pktdump)
70 self.persistent = parseBoolean(persistent)
71 self.netcfg = parseBoolean(netcfg)
72 self.dryrun = parseBoolean(dryrun)
73 self.valgrind = parseBoolean(valgrind)
Carmelo Casconeb7524272017-06-05 16:53:13 -040074 self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId
Yi Tseng7875cb72017-08-08 10:15:58 -070075 self.pipeconfId = pipeconfId
Carmelo Cascone785fada2016-06-16 18:34:16 -070076 if persistent:
77 self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId
78 self.cmd("touch %s" % self.exectoken)
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070079 # Store thrift port for future uses.
Carmelo Cascone75e97992017-06-05 02:32:47 -040080 self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId))
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070081
Yi Tseng7875cb72017-08-08 10:15:58 -070082 if 'longitude' in kwargs:
83 self.longitude = kwargs['longitude']
84 else:
85 self.longitude = None
86
87 if 'latitude' in kwargs:
88 self.latitude = kwargs['latitude']
89 else:
90 self.latitude = None
91
92 self.onosDeviceId = "device:bmv2:%d" % self.deviceId
93
Carmelo Cascone977ae3f2016-06-23 19:28:28 -070094 @classmethod
95 def pickUnusedPort(cls):
96 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97 s.bind(('localhost', 0))
98 addr, port = s.getsockname()
99 s.close()
100 return port
Carmelo Cascone785fada2016-06-16 18:34:16 -0700101
Carmelo Casconeb7524272017-06-05 16:53:13 -0400102 def getSourceIp(self, dstIP):
103 """
104 Queries the Linux routing table to get the source IP that can talk with dstIP, and vice
105 versa.
106 """
107 ipRouteOut = self.cmd('ip route get %s' % dstIP)
108 r = re.search(r"src (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", ipRouteOut)
109 return r.group(1) if r else None
110
Yi Tseng7875cb72017-08-08 10:15:58 -0700111 def getDeviceConfig(self, srcIP):
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200112 portData = {}
113 portId = 1
114 for intfName in self.intfNames():
115 if intfName == 'lo':
116 continue
117 portData[str(portId)] = {
118 "number": portId,
119 "name": intfName,
120 "enabled": True,
121 "removed": False,
122 "type": "copper",
123 "speed": 10000
124 }
125 portId += 1
126
Yi Tseng7875cb72017-08-08 10:15:58 -0700127 basicCfg = {
128 "driver": "bmv2"
129 }
130
131 if self.longitude and self.latitude:
132 basicCfg["longitude"] = self.longitude
133 basicCfg["latitude"] = self.latitude
134
135 cfgData = {
Carmelo Cascone34433252017-08-25 20:27:18 +0200136 "generalprovider": {
137 "p4runtime": {
138 "ip": srcIP,
139 "port": self.grpcPort,
140 "deviceId": self.deviceId,
141 "deviceKeyId": "p4runtime:%s" % self.onosDeviceId
142 }
143 },
144 "piPipeconf": {
145 "piPipeconfId": self.pipeconfId
146 },
147 "basic": basicCfg,
148 "ports": portData
Yi Tseng7875cb72017-08-08 10:15:58 -0700149 }
150
151 return cfgData
152
153 def doOnosNetcfg(self, controllerIP):
154 """
155 Notifies ONOS about the new device via Netcfg.
156 """
157 srcIP = self.getSourceIp(controllerIP)
158 if not srcIP:
159 warn("WARN: unable to get device IP address, won't do onos-netcfg")
160 return
161
Carmelo Casconea11279b2017-06-22 04:30:08 -0400162 cfgData = {
163 "devices": {
Yi Tseng7875cb72017-08-08 10:15:58 -0700164 self.onosDeviceId: self.getDeviceConfig(srcIP)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400165 }
Carmelo Casconea11279b2017-06-22 04:30:08 -0400166 }
Carmelo Casconeb7524272017-06-05 16:53:13 -0400167 with open(self.netcfgfile, 'w') as fp:
168 json.dump(cfgData, fp, indent=4)
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200169
170 if not self.netcfg:
171 # Do not push config to ONOS.
172 return
173
Brian O'Connor71167f92017-06-16 14:55:00 -0700174 # Build netcfg URL
175 url = 'http://%s:8181/onos/v1/network/configuration/' % controllerIP
176 # Instantiate password manager for HTTP auth
177 pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
178 pm.add_password(None, url, os.environ['ONOS_WEB_USER'], os.environ['ONOS_WEB_PASS'])
179 urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(pm)))
180 # Push config data to controller
Carmelo Casconea11279b2017-06-22 04:30:08 -0400181 req = urllib2.Request(url, json.dumps(cfgData), {'Content-Type': 'application/json'})
182 try:
183 f = urllib2.urlopen(req)
184 print f.read()
185 f.close()
186 except urllib2.URLError as e:
187 warn("WARN: unable to push config to ONOS (%s)" % e.reason)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400188
Carmelo Cascone785fada2016-06-16 18:34:16 -0700189 def start(self, controllers):
Carmelo Cascone75e97992017-06-05 02:32:47 -0400190 args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)]
Carmelo Cascone785fada2016-06-16 18:34:16 -0700191 for port, intf in self.intfs.items():
192 if not intf.IP():
193 args.append('-i %d@%s' % (port, intf.name))
Carmelo Cascone785fada2016-06-16 18:34:16 -0700194 if self.elogger:
195 nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId
196 args.append('--nanolog %s' % nanomsg)
197 if self.debugger:
198 args.append('--debugger')
Carmelo Casconefb76b042017-07-17 19:42:00 -0400199 args.append('--log-console')
Carmelo Cascone34433252017-08-25 20:27:18 +0200200 if self.pktdump:
201 args.append('--pcap --dump-packet-data %d' % PKT_BYTES_TO_DUMP)
Carmelo Cascone9e6621f2017-06-27 16:06:33 -0400202 args.append('-L%s' % self.loglevel)
Carmelo Casconeaaf6d982017-07-29 19:52:58 -0400203 args.append('--thrift-port %d' % self.thriftPort)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400204 if not self.json:
205 args.append('--no-p4')
206 else:
207 args.append(self.json)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400208
209 # gRPC target-specific options.
Carmelo Cascone785fada2016-06-16 18:34:16 -0700210 args.append('--')
Carmelo Cascone569d4ad2017-07-10 16:09:00 -0400211 args.append('--cpu-port %d' % CPU_PORT)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400212 args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700213
214 bmv2cmd = " ".join(args)
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200215 if self.valgrind:
216 bmv2cmd = "%s %s" % (VALGRIND_PREFIX, bmv2cmd)
217 if self.dryrun:
218 info("\n*** DRY RUN (not executing bmv2)")
219 info("\nStarting BMv2 target: %s" % bmv2cmd)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400220
Carmelo Cascone785fada2016-06-16 18:34:16 -0700221 if self.persistent:
Carmelo Casconeb7524272017-06-05 16:53:13 -0400222 # Bash loop to re-exec the switch if it crashes.
Carmelo Casconefb76b042017-07-17 19:42:00 -0400223 bmv2cmd = "(while [ -e {} ]; do {} ; sleep 1; done;)".format(self.exectoken, bmv2cmd)
224
225 cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700226
Carmelo Casconeb7524272017-06-05 16:53:13 -0400227 # Starts the switch.
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200228 if not self.dryrun:
229 out = self.cmd(cmdStr)
230 if out:
231 print out
232 if self.valgrind:
233 # With valgrind, it takes some time before the gRPC server is available.
234 # Wait before pushing the netcfg.
235 info("\n*** Waiting %d seconds before pushing the config to ONOS...\n" % VALGRIND_SLEEP)
236 time.sleep(VALGRIND_SLEEP)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400237
Carmelo Cascone46d360b2017-08-29 20:20:32 +0200238 try: # onos.py
239 clist = controllers[0].nodes()
240 except AttributeError:
241 clist = controllers
242 assert len(clist) > 0
243 cip = clist[0].IP()
244 self.doOnosNetcfg(cip)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400245
246 def stop(self, deleteIntfs=True):
247 """Terminate switch."""
Carmelo Cascone977ae3f2016-06-23 19:28:28 -0700248 self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId)
Carmelo Casconefb76b042017-07-17 19:42:00 -0400249 self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId)
Carmelo Cascone75e97992017-06-05 02:32:47 -0400250 self.cmd('kill %' + BMV2_TARGET)
Carmelo Casconeb7524272017-06-05 16:53:13 -0400251 Switch.stop(self, deleteIntfs)
Carmelo Cascone785fada2016-06-16 18:34:16 -0700252
253
Carmelo Casconeb7524272017-06-05 16:53:13 -0400254# Exports for bin/mn
Carmelo Cascone785fada2016-06-16 18:34:16 -0700255switches = {'onosbmv2': ONOSBmv2Switch}
Carmelo Cascone34433252017-08-25 20:27:18 +0200256hosts = {'onoshost': ONOSHost}