Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 1 | import json |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 2 | import multiprocessing |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 3 | import os |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 4 | import random |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 5 | import re |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 6 | import socket |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 7 | import threading |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 8 | import time |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 9 | import urllib2 |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 10 | from contextlib import closing |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 11 | from mininet.log import info, warn |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 12 | from mininet.node import Switch, Host |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 13 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 14 | SIMPLE_SWITCH_GRPC = 'simple_switch_grpc' |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 15 | PKT_BYTES_TO_DUMP = 80 |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 16 | VALGRIND_PREFIX = 'valgrind --leak-check=yes' |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 17 | SWITCH_START_TIMEOUT = 5 # seconds |
| 18 | BMV2_LOG_LINES = 5 |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 19 | BMV2_DEFAULT_DEVICE_ID = 0 |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 20 | |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 21 | |
| 22 | def parseBoolean(value): |
| 23 | if value in ['1', 1, 'true', 'True']: |
| 24 | return True |
| 25 | else: |
| 26 | return False |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 27 | |
| 28 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 29 | def pickUnusedPort(): |
| 30 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 31 | s.bind(('localhost', 0)) |
| 32 | addr, port = s.getsockname() |
| 33 | s.close() |
| 34 | return port |
| 35 | |
| 36 | |
| 37 | def writeToFile(path, value): |
| 38 | with open(path, "w") as f: |
| 39 | f.write(str(value)) |
| 40 | |
| 41 | |
| 42 | def watchDog(sw): |
| 43 | while True: |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 44 | if ONOSBmv2Switch.mininet_exception == 1: |
| 45 | sw.killBmv2(log=False) |
| 46 | return |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 47 | if sw.stopped: |
| 48 | return |
| 49 | with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: |
| 50 | if s.connect_ex(('127.0.0.1', sw.grpcPort)) == 0: |
| 51 | time.sleep(1) |
| 52 | else: |
Carmelo Cascone | df32bea | 2018-06-19 17:45:54 +0200 | [diff] [blame] | 53 | warn("\n*** WARN: BMv2 instance %s died!\n" % sw.name) |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 54 | sw.printBmv2Log() |
| 55 | print ("-" * 80) + "\n" |
| 56 | return |
| 57 | |
| 58 | |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 59 | class ONOSHost(Host): |
| 60 | def __init__(self, name, inNamespace=True, **params): |
| 61 | Host.__init__(self, name, inNamespace=inNamespace, **params) |
| 62 | |
| 63 | def config(self, **params): |
| 64 | r = super(Host, self).config(**params) |
| 65 | for off in ["rx", "tx", "sg"]: |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 66 | cmd = "/sbin/ethtool --offload %s %s off" \ |
Carmelo Cascone | 6ec8f8f | 2017-11-22 14:27:06 -0800 | [diff] [blame] | 67 | % (self.defaultIntf(), off) |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 68 | self.cmd(cmd) |
| 69 | # disable IPv6 |
| 70 | self.cmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1") |
| 71 | self.cmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1") |
| 72 | self.cmd("sysctl -w net.ipv6.conf.lo.disable_ipv6=1") |
| 73 | return r |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 74 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 75 | |
| 76 | class ONOSBmv2Switch(Switch): |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 77 | """BMv2 software switch with gRPC server""" |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 78 | # Shared value used to notify to all instances of this class that a Mininet |
| 79 | # exception occurred. Mininet exception handling doesn't call the stop() |
| 80 | # method, so the mn process would hang after clean-up since Bmv2 would still |
| 81 | # be running. |
| 82 | mininet_exception = multiprocessing.Value('i', 0) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 83 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 84 | def __init__(self, name, json=None, debugger=False, loglevel="warn", |
Carmelo Cascone | eaa8b1d | 2018-04-11 14:12:17 -0700 | [diff] [blame] | 85 | elogger=False, grpcport=None, cpuport=255, |
| 86 | thriftport=None, netcfg=True, dryrun=False, pipeconf="", |
| 87 | pktdump=False, valgrind=False, gnmi=False, |
Carmelo Cascone | 55965c6 | 2018-05-17 18:13:16 -0700 | [diff] [blame] | 88 | portcfg=True, onosdevid=None, **kwargs): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 89 | Switch.__init__(self, name, **kwargs) |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 90 | self.grpcPort = grpcport |
| 91 | self.thriftPort = thriftport |
Carmelo Cascone | eaa8b1d | 2018-04-11 14:12:17 -0700 | [diff] [blame] | 92 | self.cpuPort = cpuport |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 93 | self.json = json |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 94 | self.debugger = parseBoolean(debugger) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 95 | self.loglevel = loglevel |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 96 | # Important: Mininet removes all /tmp/*.log files in case of exceptions. |
| 97 | # We want to be able to see the bmv2 log if anything goes wrong, hence |
| 98 | # avoid the .log extension. |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 99 | self.logfile = '/tmp/bmv2-%s-log' % self.name |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 100 | self.elogger = parseBoolean(elogger) |
| 101 | self.pktdump = parseBoolean(pktdump) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 102 | self.netcfg = parseBoolean(netcfg) |
| 103 | self.dryrun = parseBoolean(dryrun) |
| 104 | self.valgrind = parseBoolean(valgrind) |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 105 | self.netcfgfile = '/tmp/bmv2-%s-netcfg.json' % self.name |
Carmelo Cascone | eaa8b1d | 2018-04-11 14:12:17 -0700 | [diff] [blame] | 106 | self.pipeconfId = pipeconf |
| 107 | self.injectPorts = parseBoolean(portcfg) |
| 108 | self.withGnmi = parseBoolean(gnmi) |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 109 | self.longitude = kwargs['longitude'] if 'longitude' in kwargs else None |
| 110 | self.latitude = kwargs['latitude'] if 'latitude' in kwargs else None |
Carmelo Cascone | 55965c6 | 2018-05-17 18:13:16 -0700 | [diff] [blame] | 111 | if onosdevid is not None and len(onosdevid) > 0: |
| 112 | self.onosDeviceId = onosdevid |
| 113 | else: |
| 114 | self.onosDeviceId = "device:bmv2:%s" % self.name |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 115 | self.logfd = None |
| 116 | self.bmv2popen = None |
| 117 | self.stopped = False |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 118 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 119 | # Remove files from previous executions |
| 120 | self.cleanupTmpFiles() |
| 121 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 122 | def getSourceIp(self, dstIP): |
| 123 | """ |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 124 | Queries the Linux routing table to get the source IP that can talk with |
| 125 | dstIP, and vice versa. |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 126 | """ |
| 127 | ipRouteOut = self.cmd('ip route get %s' % dstIP) |
| 128 | r = re.search(r"src (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", ipRouteOut) |
| 129 | return r.group(1) if r else None |
| 130 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 131 | def getDeviceConfig(self, srcIP): |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 132 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 133 | basicCfg = { |
| 134 | "driver": "bmv2" |
| 135 | } |
| 136 | |
| 137 | if self.longitude and self.latitude: |
| 138 | basicCfg["longitude"] = self.longitude |
| 139 | basicCfg["latitude"] = self.latitude |
| 140 | |
| 141 | cfgData = { |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 142 | "generalprovider": { |
| 143 | "p4runtime": { |
| 144 | "ip": srcIP, |
| 145 | "port": self.grpcPort, |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 146 | "deviceId": BMV2_DEFAULT_DEVICE_ID, |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 147 | "deviceKeyId": "p4runtime:%s" % self.onosDeviceId |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 148 | }, |
| 149 | "bmv2-thrift": { |
| 150 | "ip": srcIP, |
| 151 | "port": self.thriftPort |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 152 | } |
| 153 | }, |
| 154 | "piPipeconf": { |
| 155 | "piPipeconfId": self.pipeconfId |
| 156 | }, |
Andrea Campanella | bf9e5ce | 2017-12-06 14:26:36 +0100 | [diff] [blame] | 157 | "basic": basicCfg |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Carmelo Cascone | 4f985cd | 2018-02-11 17:36:42 -0800 | [diff] [blame] | 160 | if self.withGnmi: |
| 161 | cfgData["generalprovider"]["gnmi"] = { |
| 162 | "ip": srcIP, |
| 163 | "port": self.grpcPort |
| 164 | } |
| 165 | |
| 166 | if self.injectPorts: |
Andrea Campanella | bf9e5ce | 2017-12-06 14:26:36 +0100 | [diff] [blame] | 167 | portData = {} |
| 168 | portId = 1 |
| 169 | for intfName in self.intfNames(): |
| 170 | if intfName == 'lo': |
| 171 | continue |
| 172 | portData[str(portId)] = { |
| 173 | "number": portId, |
| 174 | "name": intfName, |
| 175 | "enabled": True, |
| 176 | "removed": False, |
| 177 | "type": "copper", |
| 178 | "speed": 10000 |
| 179 | } |
| 180 | portId += 1 |
| 181 | |
| 182 | cfgData['ports'] = portData |
| 183 | |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 184 | return cfgData |
| 185 | |
| 186 | def doOnosNetcfg(self, controllerIP): |
| 187 | """ |
| 188 | Notifies ONOS about the new device via Netcfg. |
| 189 | """ |
| 190 | srcIP = self.getSourceIp(controllerIP) |
| 191 | if not srcIP: |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 192 | warn("*** WARN: unable to get switch IP address, won't do netcfg\n") |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 193 | return |
| 194 | |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 195 | cfgData = { |
| 196 | "devices": { |
Yi Tseng | 7875cb7 | 2017-08-08 10:15:58 -0700 | [diff] [blame] | 197 | self.onosDeviceId: self.getDeviceConfig(srcIP) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 198 | } |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 199 | } |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 200 | with open(self.netcfgfile, 'w') as fp: |
| 201 | json.dump(cfgData, fp, indent=4) |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 202 | |
| 203 | if not self.netcfg: |
| 204 | # Do not push config to ONOS. |
| 205 | return |
| 206 | |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 207 | # Build netcfg URL |
| 208 | url = 'http://%s:8181/onos/v1/network/configuration/' % controllerIP |
| 209 | # Instantiate password manager for HTTP auth |
| 210 | pm = urllib2.HTTPPasswordMgrWithDefaultRealm() |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 211 | pm.add_password(None, url, |
| 212 | os.environ['ONOS_WEB_USER'], |
| 213 | os.environ['ONOS_WEB_PASS']) |
| 214 | urllib2.install_opener(urllib2.build_opener( |
| 215 | urllib2.HTTPBasicAuthHandler(pm))) |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 216 | # Push config data to controller |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 217 | req = urllib2.Request(url, json.dumps(cfgData), |
| 218 | {'Content-Type': 'application/json'}) |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 219 | try: |
| 220 | f = urllib2.urlopen(req) |
| 221 | print f.read() |
| 222 | f.close() |
| 223 | except urllib2.URLError as e: |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 224 | warn("*** WARN: unable to push config to ONOS (%s)\n" % e.reason) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 225 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 226 | def start(self, controllers): |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 227 | bmv2Args = [SIMPLE_SWITCH_GRPC] + self.grpcTargetArgs() |
| 228 | if self.valgrind: |
| 229 | bmv2Args = VALGRIND_PREFIX.split() + bmv2Args |
| 230 | |
| 231 | cmdString = " ".join(bmv2Args) |
| 232 | |
| 233 | if self.dryrun: |
| 234 | info("\n*** DRY RUN (not executing bmv2)") |
| 235 | |
| 236 | info("\nStarting BMv2 target: %s\n" % cmdString) |
| 237 | |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 238 | writeToFile("/tmp/bmv2-%s-grpc-port" % self.name, self.grpcPort) |
| 239 | writeToFile("/tmp/bmv2-%s-thrift-port" % self.name, self.thriftPort) |
| 240 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 241 | try: |
| 242 | if not self.dryrun: |
| 243 | # Start the switch |
| 244 | self.logfd = open(self.logfile, "w") |
| 245 | self.bmv2popen = self.popen(cmdString, |
| 246 | stdout=self.logfd, |
| 247 | stderr=self.logfd) |
| 248 | self.waitBmv2Start() |
| 249 | # We want to be notified if BMv2 dies... |
| 250 | threading.Thread(target=watchDog, args=[self]).start() |
| 251 | |
| 252 | self.doOnosNetcfg(self.controllerIp(controllers)) |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 253 | except Exception: |
| 254 | ONOSBmv2Switch.mininet_exception = 1 |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 255 | self.killBmv2() |
| 256 | self.printBmv2Log() |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 257 | raise |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 258 | |
| 259 | def grpcTargetArgs(self): |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 260 | if self.grpcPort is None: |
| 261 | self.grpcPort = pickUnusedPort() |
| 262 | if self.thriftPort is None: |
| 263 | self.thriftPort = pickUnusedPort() |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 264 | args = ['--device-id %s' % str(BMV2_DEFAULT_DEVICE_ID)] |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 265 | for port, intf in self.intfs.items(): |
| 266 | if not intf.IP(): |
| 267 | args.append('-i %d@%s' % (port, intf.name)) |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 268 | args.append('--thrift-port %s' % self.thriftPort) |
| 269 | ntfaddr = 'ipc:///tmp/bmv2-%s-notifications.ipc' % self.name |
| 270 | args.append('--notifications-addr %s' % ntfaddr) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 271 | if self.elogger: |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 272 | nanologaddr = 'ipc:///tmp/bmv2-%s-nanolog.ipc' % self.name |
| 273 | args.append('--nanolog %s' % nanologaddr) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 274 | if self.debugger: |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 275 | dbgaddr = 'ipc:///tmp/bmv2-%s-debug.ipc' % self.name |
| 276 | args.append('--debugger-addr %s' % dbgaddr) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 277 | args.append('--log-console') |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 278 | if self.pktdump: |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 279 | args.append('--pcap --dump-packet-data %s' % PKT_BYTES_TO_DUMP) |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 280 | args.append('-L%s' % self.loglevel) |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 281 | if not self.json: |
| 282 | args.append('--no-p4') |
| 283 | else: |
| 284 | args.append(self.json) |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 285 | # gRPC target-specific options |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 286 | args.append('--') |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 287 | args.append('--cpu-port %s' % self.cpuPort) |
| 288 | args.append('--grpc-server-addr 0.0.0.0:%s' % self.grpcPort) |
| 289 | return args |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 290 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 291 | def waitBmv2Start(self): |
| 292 | # Wait for switch to open gRPC port, before sending ONOS the netcfg. |
| 293 | # Include time-out just in case something hangs. |
| 294 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 295 | endtime = time.time() + SWITCH_START_TIMEOUT |
| 296 | while True: |
| 297 | result = sock.connect_ex(('127.0.0.1', self.grpcPort)) |
| 298 | if result == 0: |
| 299 | # The port is open. Let's go! (Close socket first) |
| 300 | sock.close() |
| 301 | break |
| 302 | # Port is not open yet. If there is time, we wait a bit. |
| 303 | if endtime > time.time(): |
Carmelo Cascone | 3c216fa | 2018-06-22 14:52:15 +0200 | [diff] [blame] | 304 | time.sleep(0.1) |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 305 | else: |
| 306 | # Time's up. |
| 307 | raise Exception("Switch did not start before timeout") |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 308 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 309 | def printBmv2Log(self): |
| 310 | if os.path.isfile(self.logfile): |
| 311 | print "-" * 80 |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 312 | print "%s log (from %s):" % (self.name, self.logfile) |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 313 | with open(self.logfile, 'r') as f: |
| 314 | lines = f.readlines() |
| 315 | if len(lines) > BMV2_LOG_LINES: |
| 316 | print "..." |
| 317 | for line in lines[-BMV2_LOG_LINES:]: |
| 318 | print line.rstrip() |
Carmelo Cascone | fb76b04 | 2017-07-17 19:42:00 -0400 | [diff] [blame] | 319 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 320 | @staticmethod |
| 321 | def controllerIp(controllers): |
| 322 | try: |
| 323 | # onos.py |
Carmelo Cascone | 46d360b | 2017-08-29 20:20:32 +0200 | [diff] [blame] | 324 | clist = controllers[0].nodes() |
| 325 | except AttributeError: |
| 326 | clist = controllers |
| 327 | assert len(clist) > 0 |
Carmelo Cascone | 44448a5 | 2018-06-25 23:36:57 +0200 | [diff] [blame] | 328 | return random.choice(clist).IP() |
Keesjan Karsten | 8539f08 | 2018-01-04 17:03:31 +0100 | [diff] [blame] | 329 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 330 | def killBmv2(self, log=False): |
| 331 | if self.bmv2popen is not None: |
| 332 | self.bmv2popen.kill() |
| 333 | if self.logfd is not None: |
| 334 | if log: |
| 335 | self.logfd.write("*** PROCESS TERMINATED BY MININET ***\n") |
| 336 | self.logfd.close() |
Keesjan Karsten | 8539f08 | 2018-01-04 17:03:31 +0100 | [diff] [blame] | 337 | |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 338 | def cleanupTmpFiles(self): |
Carmelo Cascone | c282133 | 2018-05-14 18:15:33 -0700 | [diff] [blame] | 339 | self.cmd("rm -f /tmp/bmv2-%s-*" % self.name) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 340 | |
| 341 | def stop(self, deleteIntfs=True): |
| 342 | """Terminate switch.""" |
Carmelo Cascone | f11513d | 2018-01-16 00:31:14 -0800 | [diff] [blame] | 343 | self.stopped = True |
| 344 | self.killBmv2(log=True) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 345 | Switch.stop(self, deleteIntfs) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 346 | |
| 347 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 348 | # Exports for bin/mn |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 349 | switches = {'onosbmv2': ONOSBmv2Switch} |
Carmelo Cascone | 3443325 | 2017-08-25 20:27:18 +0200 | [diff] [blame] | 350 | hosts = {'onoshost': ONOSHost} |