Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 1 | import os |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 2 | import socket |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 3 | import re |
| 4 | import json |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 5 | import urllib2 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 6 | |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 7 | import time |
| 8 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 9 | from mininet.log import info, warn, error |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 10 | from mininet.node import Switch, Host |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 11 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 12 | if 'ONOS_ROOT' not in os.environ: |
| 13 | error("ERROR: environment var $ONOS_ROOT not set") |
| 14 | exit() |
| 15 | |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 16 | BMV2_TARGET = 'simple_switch_grpc' |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 17 | ONOS_ROOT = os.environ["ONOS_ROOT"] |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 18 | CPU_PORT = 255 |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 19 | PKT_BYTES_TO_DUMP = 80 |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 20 | VALGRIND_PREFIX = 'valgrind --leak-check=yes' |
| 21 | VALGRIND_SLEEP = 10 # seconds |
| 22 | |
| 23 | |
| 24 | def parseBoolean(value): |
| 25 | if value in ['1', 1, 'true', 'True']: |
| 26 | return True |
| 27 | else: |
| 28 | return False |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 45 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 46 | |
| 47 | class ONOSBmv2Switch(Switch): |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 48 | """BMv2 software switch with gRPC server""" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 49 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 50 | deviceId = 0 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 51 | instanceCount = 0 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 52 | |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 53 | def __init__(self, name, json=None, debugger=False, loglevel="warn", elogger=False, |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 54 | persistent=False, grpcPort=None, thriftPort=None, netcfg=True, dryrun=False, |
| 55 | pipeconfId="", pktdump=False, valgrind=False, **kwargs): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 56 | Switch.__init__(self, name, **kwargs) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 57 | self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort |
Carmelo Cascone | aaf6d98 | 2017-07-29 19:52:58 -0400 | [diff] [blame] | 58 | self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 59 | if self.dpid: |
| 60 | self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 61 | else: |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 62 | self.deviceId = ONOSBmv2Switch.deviceId |
| 63 | ONOSBmv2Switch.deviceId += 1 |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 64 | self.json = json |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 65 | self.debugger = parseBoolean(debugger) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 66 | self.loglevel = loglevel |
| 67 | self.logfile = '/tmp/bmv2-%d.log' % self.deviceId |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 68 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 74 | self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 75 | self.pipeconfId = pipeconfId |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 76 | if persistent: |
| 77 | self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId |
| 78 | self.cmd("touch %s" % self.exectoken) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 79 | # Store thrift port for future uses. |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 80 | self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId)) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 81 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 82 | 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 Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 94 | @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 Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 101 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 102 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 111 | def getDeviceConfig(self, srcIP): |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 112 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 127 | 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 Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 136 | "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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 149 | } |
| 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 Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 162 | cfgData = { |
| 163 | "devices": { |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 164 | self.onosDeviceId: self.getDeviceConfig(srcIP) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 165 | } |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 166 | } |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 167 | with open(self.netcfgfile, 'w') as fp: |
| 168 | json.dump(cfgData, fp, indent=4) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 169 | |
| 170 | if not self.netcfg: |
| 171 | # Do not push config to ONOS. |
| 172 | return |
| 173 | |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 174 | # 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 Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 181 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 188 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 189 | def start(self, controllers): |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 190 | args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)] |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 191 | for port, intf in self.intfs.items(): |
| 192 | if not intf.IP(): |
| 193 | args.append('-i %d@%s' % (port, intf.name)) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 194 | 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 Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 199 | args.append('--log-console') |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 200 | if self.pktdump: |
| 201 | args.append('--pcap --dump-packet-data %d' % PKT_BYTES_TO_DUMP) |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 202 | args.append('-L%s' % self.loglevel) |
Carmelo Cascone | aaf6d98 | 2017-07-29 19:52:58 -0400 | [diff] [blame] | 203 | args.append('--thrift-port %d' % self.thriftPort) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 204 | if not self.json: |
| 205 | args.append('--no-p4') |
| 206 | else: |
| 207 | args.append(self.json) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 208 | |
| 209 | # gRPC target-specific options. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 210 | args.append('--') |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 211 | args.append('--cpu-port %d' % CPU_PORT) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 212 | args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 213 | |
| 214 | bmv2cmd = " ".join(args) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 215 | 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 Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 220 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 221 | if self.persistent: |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 222 | # Bash loop to re-exec the switch if it crashes. |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 223 | bmv2cmd = "(while [ -e {} ]; do {} ; sleep 1; done;)".format(self.exectoken, bmv2cmd) |
| 224 | |
| 225 | cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 226 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 227 | # Starts the switch. |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 228 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 237 | |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 238 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 245 | |
| 246 | def stop(self, deleteIntfs=True): |
| 247 | """Terminate switch.""" |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 248 | self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 249 | self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId) |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 250 | self.cmd('kill %' + BMV2_TARGET) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 251 | Switch.stop(self, deleteIntfs) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 252 | |
| 253 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 254 | # Exports for bin/mn |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 255 | switches = {'onosbmv2': ONOSBmv2Switch} |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 256 | hosts = {'onoshost': ONOSHost} |