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