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