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"]: |
Carmelo Cascone | 6ec8f8f | 2017-11-22 14:27:06 -0800 | [diff] [blame] | 38 | cmd = "/sbin/ethtool --offload %s %s off"\ |
| 39 | % (self.defaultIntf(), off) |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 40 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 46 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 47 | |
| 48 | class ONOSBmv2Switch(Switch): |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 49 | """BMv2 software switch with gRPC server""" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 50 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 51 | deviceId = 0 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 52 | instanceCount = 0 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 53 | |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 54 | def __init__(self, name, json=None, debugger=False, loglevel="warn", elogger=False, |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 55 | persistent=False, grpcPort=None, thriftPort=None, netcfg=True, dryrun=False, |
Carmelo Cascone | 6ec8f8f | 2017-11-22 14:27:06 -0800 | [diff] [blame] | 56 | pipeconfId="", pktdump=False, valgrind=False, netcfgDelay=0, |
| 57 | **kwargs): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 58 | Switch.__init__(self, name, **kwargs) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 59 | self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort |
Carmelo Cascone | aaf6d98 | 2017-07-29 19:52:58 -0400 | [diff] [blame] | 60 | self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 61 | if self.dpid: |
| 62 | 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] | 63 | else: |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 64 | self.deviceId = ONOSBmv2Switch.deviceId |
| 65 | ONOSBmv2Switch.deviceId += 1 |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 66 | self.json = json |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 67 | self.debugger = parseBoolean(debugger) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 68 | self.loglevel = loglevel |
| 69 | self.logfile = '/tmp/bmv2-%d.log' % self.deviceId |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 70 | 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 Cascone | 6ec8f8f | 2017-11-22 14:27:06 -0800 | [diff] [blame] | 76 | self.netcfgDelay = netcfgDelay |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 77 | self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 78 | self.pipeconfId = pipeconfId |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 79 | if persistent: |
| 80 | self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId |
| 81 | self.cmd("touch %s" % self.exectoken) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 82 | # Store thrift port for future uses. |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 83 | 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] | 84 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 85 | 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 Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 97 | @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 Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 104 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 105 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 114 | def getDeviceConfig(self, srcIP): |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 115 | 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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 130 | 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 Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 139 | "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 Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 152 | } |
| 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 Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 165 | cfgData = { |
| 166 | "devices": { |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 167 | self.onosDeviceId: self.getDeviceConfig(srcIP) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 168 | } |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 169 | } |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 170 | with open(self.netcfgfile, 'w') as fp: |
| 171 | json.dump(cfgData, fp, indent=4) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 172 | |
| 173 | if not self.netcfg: |
| 174 | # Do not push config to ONOS. |
| 175 | return |
| 176 | |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 177 | # 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 Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 184 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 191 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 192 | def start(self, controllers): |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 193 | args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)] |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 194 | for port, intf in self.intfs.items(): |
| 195 | if not intf.IP(): |
| 196 | args.append('-i %d@%s' % (port, intf.name)) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 197 | 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 Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 202 | args.append('--log-console') |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 203 | if self.pktdump: |
| 204 | args.append('--pcap --dump-packet-data %d' % PKT_BYTES_TO_DUMP) |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 205 | args.append('-L%s' % self.loglevel) |
Carmelo Cascone | aaf6d98 | 2017-07-29 19:52:58 -0400 | [diff] [blame] | 206 | args.append('--thrift-port %d' % self.thriftPort) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 207 | if not self.json: |
| 208 | args.append('--no-p4') |
| 209 | else: |
| 210 | args.append(self.json) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 211 | |
| 212 | # gRPC target-specific options. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 213 | args.append('--') |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 214 | args.append('--cpu-port %d' % CPU_PORT) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 215 | args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 216 | |
| 217 | bmv2cmd = " ".join(args) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 218 | if self.valgrind: |
| 219 | bmv2cmd = "%s %s" % (VALGRIND_PREFIX, bmv2cmd) |
| 220 | if self.dryrun: |
| 221 | info("\n*** DRY RUN (not executing bmv2)") |
Carmelo Cascone | 6e85404 | 2017-09-11 21:37:53 +0200 | [diff] [blame] | 222 | info("\nStarting BMv2 target: %s\n" % bmv2cmd) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 223 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 224 | if self.persistent: |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 225 | # Bash loop to re-exec the switch if it crashes. |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 226 | bmv2cmd = "(while [ -e {} ]; do {} ; sleep 1; done;)".format(self.exectoken, bmv2cmd) |
| 227 | |
| 228 | cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 229 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 230 | # Starts the switch. |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 231 | if not self.dryrun: |
| 232 | out = self.cmd(cmdStr) |
| 233 | if out: |
| 234 | print out |
Carmelo Cascone | 6ec8f8f | 2017-11-22 14:27:06 -0800 | [diff] [blame] | 235 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 242 | |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 243 | 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 Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 250 | |
| 251 | def stop(self, deleteIntfs=True): |
| 252 | """Terminate switch.""" |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 253 | self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 254 | self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId) |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 255 | self.cmd('kill %' + BMV2_TARGET) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 256 | Switch.stop(self, deleteIntfs) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 257 | |
| 258 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 259 | # Exports for bin/mn |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 260 | switches = {'onosbmv2': ONOSBmv2Switch} |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 261 | hosts = {'onoshost': ONOSHost} |