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