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