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 |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 5 | import urllib2 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 6 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 7 | from mininet.log import info, warn, error |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 8 | from mininet.node import Switch |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 9 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 10 | if 'ONOS_ROOT' not in os.environ: |
| 11 | error("ERROR: environment var $ONOS_ROOT not set") |
| 12 | exit() |
| 13 | |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 14 | BMV2_TARGET = 'simple_switch_grpc' |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 15 | ONOS_ROOT = os.environ["ONOS_ROOT"] |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 16 | CPU_PORT = 255 |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 17 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 18 | |
| 19 | class ONOSBmv2Switch(Switch): |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 20 | """BMv2 software switch with gRPC server""" |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 21 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 22 | deviceId = 0 |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 23 | instanceCount = 0 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 24 | |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 25 | def __init__(self, name, debugger=False, loglevel="warn", elogger=False, persistent=False, |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 26 | thriftPort=None, grpcPort=None, netcfg=True, **kwargs): |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 27 | Switch.__init__(self, name, **kwargs) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 28 | self.thriftPort = ONOSBmv2Switch.pickUnusedPort() if not thriftPort else thriftPort |
| 29 | self.grpcPort = ONOSBmv2Switch.pickUnusedPort() if not grpcPort else grpcPort |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 30 | if self.dpid: |
| 31 | 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] | 32 | else: |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 33 | self.deviceId = ONOSBmv2Switch.deviceId |
| 34 | ONOSBmv2Switch.deviceId += 1 |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 35 | self.debugger = debugger |
| 36 | self.loglevel = loglevel |
| 37 | self.logfile = '/tmp/bmv2-%d.log' % self.deviceId |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 38 | self.elogger = elogger |
| 39 | self.persistent = persistent |
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 |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 74 | onosDeviceId = "bmv2:%s" % self.deviceId |
| 75 | cfgData = { |
| 76 | "devices": { |
| 77 | "device:%s" % onosDeviceId: { |
| 78 | "generalprovider": { |
| 79 | "p4runtime": { |
| 80 | "ip": srcIP, |
| 81 | "port": self.grpcPort, |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 82 | "deviceId": self.deviceId, |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 83 | "deviceKeyId": "p4runtime:%s" % onosDeviceId |
| 84 | } |
| 85 | }, |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 86 | "piPipeconf": { |
| 87 | "piPipeconfId": "" |
| 88 | }, |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 89 | "basic": { |
| 90 | "driver": "bmv2" |
| 91 | } |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 92 | } |
| 93 | } |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 94 | } |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 95 | with open(self.netcfgfile, 'w') as fp: |
| 96 | json.dump(cfgData, fp, indent=4) |
Brian O'Connor | 71167f9 | 2017-06-16 14:55:00 -0700 | [diff] [blame] | 97 | # Build netcfg URL |
| 98 | url = 'http://%s:8181/onos/v1/network/configuration/' % controllerIP |
| 99 | # Instantiate password manager for HTTP auth |
| 100 | pm = urllib2.HTTPPasswordMgrWithDefaultRealm() |
| 101 | pm.add_password(None, url, os.environ['ONOS_WEB_USER'], os.environ['ONOS_WEB_PASS']) |
| 102 | urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(pm))) |
| 103 | # Push config data to controller |
Carmelo Cascone | a11279b | 2017-06-22 04:30:08 -0400 | [diff] [blame] | 104 | req = urllib2.Request(url, json.dumps(cfgData), {'Content-Type': 'application/json'}) |
| 105 | try: |
| 106 | f = urllib2.urlopen(req) |
| 107 | print f.read() |
| 108 | f.close() |
| 109 | except urllib2.URLError as e: |
| 110 | warn("WARN: unable to push config to ONOS (%s)" % e.reason) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 111 | |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 112 | def start(self, controllers): |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 113 | args = [BMV2_TARGET, '--device-id %s' % str(self.deviceId)] |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 114 | for port, intf in self.intfs.items(): |
| 115 | if not intf.IP(): |
| 116 | args.append('-i %d@%s' % (port, intf.name)) |
| 117 | if self.thriftPort: |
| 118 | args.append('--thrift-port %d' % self.thriftPort) |
| 119 | if self.elogger: |
| 120 | nanomsg = 'ipc:///tmp/bmv2-%d-log.ipc' % self.deviceId |
| 121 | args.append('--nanolog %s' % nanomsg) |
| 122 | if self.debugger: |
| 123 | args.append('--debugger') |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 124 | args.append('-L%s' % self.loglevel) |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 125 | args.append('--no-p4') |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 126 | |
| 127 | # gRPC target-specific options. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 128 | args.append('--') |
Carmelo Cascone | 569d4ad | 2017-07-10 16:09:00 -0400 | [diff] [blame] | 129 | args.append('--cpu-port %d' % CPU_PORT) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 130 | args.append('--grpc-server-addr 0.0.0.0:%d' % self.grpcPort) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 131 | |
| 132 | bmv2cmd = " ".join(args) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 133 | info("\nStarting BMv2 target: %s\n" % bmv2cmd) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 134 | if self.persistent: |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 135 | # Bash loop to re-exec the switch if it crashes. |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 136 | cmdStr = "(while [ -e {} ]; " \ |
| 137 | "do {} ; " \ |
| 138 | "sleep 1; " \ |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 139 | "done;) > {} 2>&1 &".format(self.exectoken, bmv2cmd, self.logfile) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 140 | else: |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 141 | cmdStr = "{} > {} 2>&1 &".format(bmv2cmd, self.logfile) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 142 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 143 | # Starts the switch. |
| 144 | out = self.cmd(cmdStr) |
| 145 | if out: |
| 146 | print out |
| 147 | |
| 148 | if self.netcfg: |
| 149 | try: # onos.py |
| 150 | clist = controllers[0].nodes() |
| 151 | except AttributeError: |
| 152 | clist = controllers |
| 153 | assert len(clist) > 0 |
| 154 | cip = clist[0].IP() |
| 155 | self.doOnosNetcfg(cip) |
| 156 | |
| 157 | def stop(self, deleteIntfs=True): |
| 158 | """Terminate switch.""" |
Carmelo Cascone | 977ae3f | 2016-06-23 19:28:28 -0700 | [diff] [blame] | 159 | self.cmd("rm -f /tmp/bmv2-%d-*" % self.deviceId) |
Carmelo Cascone | 9e6621f | 2017-06-27 16:06:33 -0400 | [diff] [blame] | 160 | self.cmd("rm -f /tmp/bmv2-%d.log" % self.deviceId) |
Carmelo Cascone | 75e9799 | 2017-06-05 02:32:47 -0400 | [diff] [blame] | 161 | self.cmd('kill %' + BMV2_TARGET) |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 162 | Switch.stop(self, deleteIntfs) |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 163 | |
| 164 | |
Carmelo Cascone | b752427 | 2017-06-05 16:53:13 -0400 | [diff] [blame] | 165 | # Exports for bin/mn |
Carmelo Cascone | 785fada | 2016-06-16 18:34:16 -0700 | [diff] [blame] | 166 | switches = {'onosbmv2': ONOSBmv2Switch} |