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