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