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 | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 5 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 6 | from mininet.log import info, warn, error |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 7 | from mininet.node import Switch |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 8 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 9 | if 'ONOS_ROOT' not in os.environ: |
| 10 | error("ERROR: environment var $ONOS_ROOT not set") |
| 11 | exit() |
| 12 | |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 13 | BMV2_TARGET = 'simple_switch_grpc' |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 14 | ONOS_ROOT = os.environ["ONOS_ROOT"] |
| 15 | INIT_BMV2_JSON = '%s/tools/test/p4src/p4c-out/empty.json' % ONOS_ROOT |
| 16 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 17 | |
| 18 | class ONOSBmv2Switch(Switch): |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 19 | """BMv2 software switch with gRPC server""" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 20 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 21 | deviceId = 0 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 22 | instanceCount = 0 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 23 | |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 24 | def __init__(self, name, debugger=False, loglevel="warn", elogger=False, persistent=False, |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 25 | logflush=False, thriftPort=None, grpcPort=None, netcfg=True, **kwargs): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 26 | Switch.__init__(self, name, **kwargs) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 27 | self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort |
| 28 | self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 29 | if self.dpid: |
| 30 | self.deviceId = int(self.dpid, 0 if 'x' in self.dpid else 16) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 31 | else: |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 32 | self.deviceId = ONOSBmv2Switch.deviceId |
| 33 | ONOSBmv2Switch.deviceId += 1 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 34 | self.debugger = debugger |
| 35 | self.loglevel = loglevel |
| 36 | self.logfile = '/tmp/bmv2-%d.log' % self.deviceId |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 37 | self.elogger = elogger |
| 38 | self.persistent = persistent |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 39 | self.logflush = logflush |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 40 | self.netcfg = netcfg |
| 41 | self.netcfgfile = '/tmp/bmv2-%d-netcfg.json' % self.deviceId |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 42 | if persistent: |
| 43 | self.exectoken = "/tmp/bmv2-%d-exec-token" % self.deviceId |
| 44 | self.cmd("touch %s" % self.exectoken) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 45 | # Store thrift port for future uses. |
| 46 | self.cmd("echo %d > /tmp/bmv2-%d-thrift-port" % (self.thriftPort, self.deviceId)) |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 47 | self.cmd("echo %d > /tmp/bmv2-%d-grpc-port" % (self.grpcPort, self.deviceId)) |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 48 | |
| 49 | @classmethod |
| 50 | def pickUnusedPort(cls): |
| 51 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 52 | s.bind(('localhost', 0)) |
| 53 | addr, port = s.getsockname() |
| 54 | s.close() |
| 55 | return port |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 56 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 57 | def getSourceIp(self, dstIP): |
| 58 | """ |
| 59 | Queries the Linux routing table to get the source IP that can talk with dstIP, and vice |
| 60 | versa. |
| 61 | """ |
| 62 | ipRouteOut = self.cmd('ip route get %s' % dstIP) |
| 63 | r = re.search(r"src (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})", ipRouteOut) |
| 64 | return r.group(1) if r else None |
| 65 | |
| 66 | def doOnosNetcfg(self, controllerIP): |
| 67 | """ |
| 68 | Notifies ONOS about the new device via Netcfg. |
| 69 | """ |
| 70 | srcIP = self.getSourceIp(controllerIP) |
| 71 | if not srcIP: |
| 72 | warn("WARN: unable to get device IP address, won't do onos-netcfg") |
| 73 | return |
| 74 | onosDeviceId = "bmv2:%s:%s#%s" % (srcIP, self.grpcPort, self.deviceId) |
| 75 | cfgData = {"devices": { |
| 76 | onosDeviceId: { |
| 77 | "basic": { |
| 78 | "name": "bmv2:%s" % self.deviceId |
| 79 | } |
| 80 | } |
| 81 | }} |
| 82 | with open(self.netcfgfile, 'w') as fp: |
| 83 | json.dump(cfgData, fp, indent=4) |
| 84 | out = self.cmd("%s/tools/test/bin/onos-netcfg %s %s" |
| 85 | % (ONOS_ROOT, controllerIP, self.netcfgfile)) |
| 86 | if out: |
| 87 | print out |
| 88 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 89 | def start(self, controllers): |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 90 | args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)] |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 91 | for port, intf in self.intfs.items(): |
| 92 | if not intf.IP(): |
| 93 | args.append('-i %d@%s' % (port, intf.name)) |
| 94 | if self.thriftPort: |
| 95 | args.append('--thrift-port %d' % self.thriftPort) |
| 96 | if self.elogger: |
| 97 | nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId |
| 98 | args.append('--nanolog %s' % nanomsg) |
| 99 | if self.debugger: |
| 100 | args.append('--debugger') |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 101 | args.append('--log-file %s -L%s' % (self.logfile, self.loglevel)) |
| 102 | if self.logflush: |
| 103 | args.append('--log-flush') |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 104 | |
| 105 | args.append(INIT_BMV2_JSON) |
| 106 | |
| 107 | # gRPC target-specific options. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 108 | args.append('--') |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 109 | args.append('--enable-swap') |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 110 | args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 111 | |
| 112 | bmv2cmd = " ".join(args) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 113 | info("\nStarting BMv2 target: %s\n" % bmv2cmd) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 114 | if self.persistent: |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 115 | # Bash loop to re-exec the switch if it crashes. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 116 | cmdStr = "(while [ -e {} ]; " \ |
| 117 | "do {} ; " \ |
| 118 | "sleep 1; " \ |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 119 | "done;) &".format(self.exectoken, bmv2cmd) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 120 | else: |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 121 | cmdStr = "{} &".format(bmv2cmd) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 122 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 123 | # Starts the switch. |
| 124 | out = self.cmd(cmdStr) |
| 125 | if out: |
| 126 | print out |
| 127 | |
| 128 | if self.netcfg: |
| 129 | try: # onos.py |
| 130 | clist = controllers[0].nodes() |
| 131 | except AttributeError: |
| 132 | clist = controllers |
| 133 | assert len(clist) > 0 |
| 134 | cip = clist[0].IP() |
| 135 | self.doOnosNetcfg(cip) |
| 136 | |
| 137 | def stop(self, deleteIntfs=True): |
| 138 | """Terminate switch.""" |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 139 | self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId) |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 140 | # wildcard end as BMv2 might create log files with .txt extension |
| 141 | self.cmd("rm -f /tmp/bmv2-%d.log*" % self.deviceId) |
| 142 | self.cmd('kill %' + BMV2_TARGET) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 143 | Switch.stop(self, deleteIntfs) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 144 | |
| 145 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 146 | # Exports for bin/mn |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 147 | switches = {'onosbmv2': ONOSBmv2Switch} |