Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | Tibit OLT device adapter |
| 19 | """ |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 20 | import json |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 21 | from uuid import uuid4 |
| 22 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 23 | import structlog |
| 24 | from scapy.fields import StrField |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 25 | from scapy.layers.l2 import Ether, Dot1Q |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 26 | from scapy.packet import Packet, bind_layers |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 27 | from twisted.internet import reactor |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 28 | from twisted.internet.defer import DeferredQueue, inlineCallbacks |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 29 | from zope.interface import implementer |
| 30 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 31 | from common.frameio.frameio import BpfProgramFilter, hexify |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 32 | from voltha.adapters.interface import IAdapterInterface |
Nathan Knuth | 31c3696 | 2016-12-27 10:04:49 -0800 | [diff] [blame] | 33 | from voltha.extensions.eoam.EOAM import EOAMPayload, DPoEOpcode_SetRequest |
| 34 | from voltha.extensions.eoam.EOAM_TLV import DOLTObject, \ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 35 | PortIngressRuleClauseMatchLength02, PortIngressRuleResultForward, \ |
| 36 | PortIngressRuleResultSet, PortIngressRuleResultInsert, \ |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 37 | PortIngressRuleTerminator, AddPortIngressRule, CablelabsOUI, PonPortObject |
Nathan Knuth | 31c3696 | 2016-12-27 10:04:49 -0800 | [diff] [blame] | 38 | from voltha.extensions.eoam.EOAM_TLV import PortIngressRuleHeader |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 39 | from voltha.extensions.eoam.EOAM_TLV import ClauseSubtypeEnum |
| 40 | from voltha.extensions.eoam.EOAM_TLV import RuleOperatorEnum |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 41 | from voltha.core.flow_decomposer import * |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 42 | from voltha.core.logical_device_agent import mac_str_to_tuple |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 43 | from voltha.protos.adapter_pb2 import Adapter, AdapterConfig |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 44 | from voltha.protos.common_pb2 import LogLevel, ConnectStatus |
| 45 | from voltha.protos.common_pb2 import OperStatus, AdminState |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 46 | from voltha.protos.device_pb2 import Device, Port |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 47 | from voltha.protos.device_pb2 import DeviceType, DeviceTypes |
| 48 | from voltha.protos.health_pb2 import HealthStatus |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 49 | from voltha.protos.logical_device_pb2 import LogicalDevice, LogicalPort |
| 50 | from voltha.protos.openflow_13_pb2 import ofp_desc, ofp_port, OFPPF_10GB_FD, \ |
| 51 | OFPPF_FIBER, OFPPS_LIVE, ofp_switch_features, OFPC_PORT_STATS, \ |
| 52 | OFPC_GROUP_STATS, OFPC_TABLE_STATS, OFPC_FLOW_STATS |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 53 | from voltha.registry import registry |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 54 | log = structlog.get_logger() |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 55 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 56 | # Match on the MGMT VLAN, Priority 7 |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 57 | TIBIT_MGMT_VLAN = 4090 |
| 58 | TIBIT_MGMT_PRIORITY = 7 |
| 59 | frame_match_case1 = 'ether[14:2] = 0x{:01x}{:03x}'.format( |
| 60 | TIBIT_MGMT_PRIORITY << 1, TIBIT_MGMT_VLAN) |
| 61 | |
| 62 | TIBIT_PACKET_IN_VLAN = 4000 |
| 63 | frame_match_case2 = '(ether[14:2] & 0xfff) = 0x{:03x}'.format( |
| 64 | TIBIT_PACKET_IN_VLAN) |
| 65 | |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 66 | TIBIT_PACKET_OUT_VLAN = 4000 |
| 67 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 68 | is_tibit_frame = BpfProgramFilter('{} or {}'.format( |
| 69 | frame_match_case1, frame_match_case2)) |
| 70 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 71 | #is_tibit_frame = lambda x: True |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 72 | |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 73 | # Extract OLT MAC address: This is a good |
| 74 | # example of getting the OLT mac address |
| 75 | |
| 76 | #for mac, device in self.device_ids.iteritems(): |
| 77 | # if device == dev_id: |
| 78 | # olt_mac_address = mac |
| 79 | # log.info('packet-out', olt_mac_address=olt_mac_address) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 80 | |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 81 | # To be removed in favor of OAM |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 82 | class TBJSON(Packet): |
| 83 | """ TBJSON 'packet' layer. """ |
| 84 | name = "TBJSON" |
| 85 | fields_desc = [StrField("data", default="")] |
| 86 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 87 | bind_layers(Ether, TBJSON, type=0x9001) |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 88 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 89 | |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 90 | @implementer(IAdapterInterface) |
| 91 | class TibitOltAdapter(object): |
| 92 | |
| 93 | name = 'tibit_olt' |
| 94 | |
| 95 | supported_device_types = [ |
| 96 | DeviceType( |
| 97 | id='tibit_olt', |
| 98 | adapter=name, |
| 99 | accepts_bulk_flow_update=True |
| 100 | ) |
| 101 | ] |
| 102 | |
| 103 | def __init__(self, adapter_agent, config): |
| 104 | self.adapter_agent = adapter_agent |
| 105 | self.config = config |
| 106 | self.descriptor = Adapter( |
| 107 | id=self.name, |
| 108 | vendor='Tibit Communications Inc.', |
| 109 | version='0.1', |
| 110 | config=AdapterConfig(log_level=LogLevel.INFO) |
| 111 | ) |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 112 | self.interface = registry('main').get_args().interface |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 113 | self.io_port = None |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 114 | self.incoming_queues = {} # OLT mac_address -> DeferredQueue() |
| 115 | self.device_ids = {} # OLT mac_address -> device_id |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 116 | self.vlan_to_device_ids = {} # c-vid -> (device_id, logical_device_id) |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 117 | |
| 118 | def start(self): |
Zsolt Haraszti | a17f3ec | 2016-12-08 14:55:49 -0800 | [diff] [blame] | 119 | log.debug('starting', interface=self.interface) |
| 120 | log.info('started', interface=self.interface) |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 121 | |
| 122 | def stop(self): |
| 123 | log.debug('stopping') |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 124 | if self.io_port is not None: |
| 125 | registry('frameio').del_interface(self.interface) |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 126 | log.info('stopped') |
| 127 | |
| 128 | def adapter_descriptor(self): |
| 129 | return self.descriptor |
| 130 | |
| 131 | def device_types(self): |
| 132 | return DeviceTypes(items=self.supported_device_types) |
| 133 | |
| 134 | def health(self): |
| 135 | return HealthStatus(state=HealthStatus.HealthState.HEALTHY) |
| 136 | |
| 137 | def change_master_state(self, master): |
| 138 | raise NotImplementedError() |
| 139 | |
| 140 | def adopt_device(self, device): |
| 141 | log.info('adopt-device', device=device) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 142 | self._activate_io_port() |
| 143 | reactor.callLater(0, self._launch_device_activation, device) |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 144 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 145 | def _activate_io_port(self): |
| 146 | if self.io_port is None: |
| 147 | self.io_port = registry('frameio').add_interface( |
| 148 | self.interface, self._rcv_io, is_tibit_frame) |
| 149 | |
| 150 | @inlineCallbacks |
| 151 | def _launch_device_activation(self, device): |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 152 | try: |
| 153 | log.debug('launch_dev_activation') |
| 154 | # prepare receive queue |
| 155 | self.incoming_queues[device.mac_address] = DeferredQueue(size=100) |
| 156 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 157 | # add mac_address to device_ids table |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 158 | olt_mac = device.mac_address |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 159 | self.device_ids[olt_mac] = device.id |
| 160 | |
| 161 | # send out ping to OLT device |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 162 | ping_frame = self._make_ping_frame(mac_address=olt_mac) |
| 163 | self.io_port.send(ping_frame) |
| 164 | |
| 165 | # wait till we receive a response |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 166 | ## TODO add timeout mechanism so we can signal if we cannot reach |
| 167 | ##device |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 168 | while True: |
| 169 | response = yield self.incoming_queues[olt_mac].get() |
| 170 | # verify response and if not the expected response |
| 171 | if 1: # TODO check if it is really what we expect, and wait if not |
| 172 | break |
| 173 | |
| 174 | except Exception, e: |
| 175 | log.exception('launch device failed', e=e) |
| 176 | |
| 177 | # if we got response, we can fill out the device info, mark the device |
| 178 | # reachable |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 179 | # jdev = json.loads(response.data[5:]) |
| 180 | jdev = json.loads(response.payload.payload.body.load) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 181 | device.root = True |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 182 | device.vendor = 'Tibit Communications, Inc.' |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 183 | device.model = jdev.get('results', {}).get('device', 'DEVICE_UNKNOWN') |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 184 | device.hardware_version = jdev['results']['datecode'] |
| 185 | device.firmware_version = jdev['results']['firmware'] |
| 186 | device.software_version = jdev['results']['modelversion'] |
| 187 | device.serial_number = jdev['results']['manufacturer'] |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 188 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 189 | device.connect_status = ConnectStatus.REACHABLE |
| 190 | self.adapter_agent.update_device(device) |
| 191 | |
Zsolt Haraszti | aa4626e | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 192 | # then shortly after we create some ports for the device |
| 193 | log.info('create-port') |
| 194 | nni_port = Port( |
| 195 | port_no=2, |
| 196 | label='NNI facing Ethernet port', |
| 197 | type=Port.ETHERNET_NNI, |
| 198 | admin_state=AdminState.ENABLED, |
| 199 | oper_status=OperStatus.ACTIVE |
| 200 | ) |
| 201 | self.adapter_agent.add_port(device.id, nni_port) |
| 202 | self.adapter_agent.add_port(device.id, Port( |
| 203 | port_no=1, |
| 204 | label='PON port', |
| 205 | type=Port.PON_OLT, |
| 206 | admin_state=AdminState.ENABLED, |
| 207 | oper_status=OperStatus.ACTIVE |
| 208 | )) |
| 209 | |
| 210 | log.info('create-logical-device') |
| 211 | # then shortly after we create the logical device with one port |
| 212 | # that will correspond to the NNI port |
| 213 | logical_device_id = uuid4().hex[:12] |
| 214 | ld = LogicalDevice( |
| 215 | id=logical_device_id, |
| 216 | datapath_id=int('0x' + logical_device_id[:8], 16), # from id |
| 217 | desc=ofp_desc( |
| 218 | mfr_desc=device.vendor, |
| 219 | hw_desc=jdev['results']['device'], |
| 220 | sw_desc=jdev['results']['firmware'], |
| 221 | serial_num=uuid4().hex, |
| 222 | dp_desc='n/a' |
| 223 | ), |
| 224 | switch_features=ofp_switch_features( |
| 225 | n_buffers=256, # TODO fake for now |
| 226 | n_tables=2, # TODO ditto |
| 227 | capabilities=( # TODO and ditto |
| 228 | OFPC_FLOW_STATS |
| 229 | | OFPC_TABLE_STATS |
| 230 | | OFPC_PORT_STATS |
| 231 | | OFPC_GROUP_STATS |
| 232 | ) |
| 233 | ), |
| 234 | root_device_id=device.id |
| 235 | ) |
| 236 | self.adapter_agent.create_logical_device(ld) |
| 237 | cap = OFPPF_10GB_FD | OFPPF_FIBER |
| 238 | self.adapter_agent.add_logical_port(ld.id, LogicalPort( |
| 239 | id='nni', |
| 240 | ofp_port=ofp_port( |
| 241 | port_no=129, |
| 242 | hw_addr=mac_str_to_tuple(device.mac_address), |
| 243 | name='nni', |
| 244 | config=0, |
| 245 | state=OFPPS_LIVE, |
| 246 | curr=cap, |
| 247 | advertised=cap, |
| 248 | peer=cap, |
| 249 | curr_speed=OFPPF_10GB_FD, |
| 250 | max_speed=OFPPF_10GB_FD |
| 251 | ), |
| 252 | device_id=device.id, |
| 253 | device_port_no=nni_port.port_no, |
| 254 | root_port=True |
| 255 | )) |
| 256 | |
| 257 | # and finally update to active |
| 258 | device = self.adapter_agent.get_device(device.id) |
| 259 | device.parent_id = ld.id |
| 260 | device.oper_status = OperStatus.ACTIVE |
| 261 | self.adapter_agent.update_device(device) |
| 262 | |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 263 | # Just transitioned to ACTIVE, wait a tenth of second |
| 264 | # before checking for ONUs |
| 265 | reactor.callLater(0.1, self._detect_onus, device) |
| 266 | |
| 267 | @inlineCallbacks |
| 268 | def _detect_onus(self, device): |
| 269 | # send out get 'links' to the OLT device |
| 270 | olt_mac = device.mac_address |
| 271 | links_frame = self._make_links_frame(mac_address=olt_mac) |
| 272 | self.io_port.send(links_frame) |
| 273 | while True: |
| 274 | response = yield self.incoming_queues[olt_mac].get() |
| 275 | # verify response and if not the expected response |
| 276 | if 1: # TODO check if it is really what we expect, and wait if not |
| 277 | break |
| 278 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 279 | jdev = json.loads(response.payload.payload.body.load) |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 280 | tibit_mac = '' |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 281 | for macid in jdev['results']: |
| 282 | if macid['macid'] is None: |
| 283 | log.info('MAC ID is NONE %s' % str(macid['macid'])) |
| 284 | else: |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 285 | tibit_mac = '000c' + macid.get('macid', 'e2000000')[4:] |
| 286 | log.info('activate-olt-for-onu-%s' % tibit_mac) |
| 287 | |
| 288 | # Convert from string to colon separated form |
| 289 | tibit_mac = ':'.join(s.encode('hex') for s in tibit_mac.decode('hex')) |
| 290 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 291 | vlan_id = self._olt_side_onu_activation(int(macid['macid'][-4:-2], 16)) |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 292 | self.adapter_agent.child_device_detected( |
| 293 | parent_device_id=device.id, |
| 294 | parent_port_no=1, |
| 295 | child_device_type='tibit_onu', |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 296 | mac_address = tibit_mac, |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 297 | proxy_address=Device.ProxyAddress( |
| 298 | device_id=device.id, |
| 299 | channel_id=vlan_id |
| 300 | ), |
| 301 | vlan=vlan_id |
| 302 | ) |
| 303 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 304 | # also record the vlan_id -> (device_id, logical_device_id) for |
| 305 | # later use |
| 306 | self.vlan_to_device_ids[vlan_id] = (device.id, device.parent_id) |
| 307 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 308 | def _olt_side_onu_activation(self, serial): |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 309 | """ |
| 310 | This is where if this was a real OLT, the OLT-side activation for |
| 311 | the new ONU should be performed. By the time we return, the OLT shall |
| 312 | be able to provide tunneled (proxy) communication to the given ONU, |
| 313 | using the returned information. |
| 314 | """ |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 315 | vlan_id = serial + 200 |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 316 | return vlan_id |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 317 | |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 318 | def _rcv_io(self, port, frame): |
| 319 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 320 | log.info('frame-received', frame=hexify(frame)) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 321 | |
Zsolt Haraszti | 4ef0a9a | 2016-12-20 01:35:48 -0800 | [diff] [blame] | 322 | # make into frame to extract source mac |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 323 | response = Ether(frame) |
| 324 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 325 | if response.haslayer(Dot1Q): |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 326 | |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 327 | # All OAM responses from the OLT should have a TIBIT_MGMT_VLAN. |
| 328 | # Responses from the ONUs should have a TIBIT_MGMT_VLAN followed by a ONU CTAG |
| 329 | # All packet-in frames will have the TIBIT_PACKET_IN_VLAN. |
| 330 | if response.getlayer(Dot1Q).type == 0x8100: |
| 331 | |
| 332 | if response.getlayer(Dot1Q).vlan == TIBIT_PACKET_IN_VLAN: |
| 333 | |
| 334 | inner_tag_and_rest = response.payload.payload |
| 335 | |
| 336 | inner_tag_and_rest.show() # TODO remove this soon |
| 337 | |
| 338 | if isinstance(inner_tag_and_rest, Dot1Q): |
| 339 | |
| 340 | cvid = inner_tag_and_rest.vlan |
| 341 | |
| 342 | frame = Ether(src=response.src, |
| 343 | dst=response.dst, |
| 344 | type=inner_tag_and_rest.type) /\ |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 345 | inner_tag_and_rest.payload |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 346 | |
| 347 | _, logical_device_id = self.vlan_to_device_ids.get(cvid) |
| 348 | if logical_device_id is None: |
| 349 | log.error('invalid-cvid', cvid=cvid) |
| 350 | else: |
| 351 | self.adapter_agent.send_packet_in( |
| 352 | logical_device_id=logical_device_id, |
| 353 | logical_port_no=cvid, # C-VID encodes port no |
| 354 | packet=str(frame)) |
| 355 | |
| 356 | else: |
| 357 | log.error('packet-in-single-tagged', |
| 358 | frame=hexify(response)) |
| 359 | |
| 360 | else: |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 361 | ## Mgmt responses received from the ONU |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 362 | ## Since the type of the first layer is 0x8100, |
| 363 | ## then the frame must have an inner tag layer |
| 364 | olt_mac = response.src |
| 365 | device_id = self.device_ids[olt_mac] |
| 366 | channel_id = response[Dot1Q:2].vlan |
| 367 | log.info('received_channel_id', channel_id=channel_id, |
| 368 | device_id=device_id) |
| 369 | |
| 370 | proxy_address=Device.ProxyAddress( |
| 371 | device_id=device_id, |
| 372 | channel_id=channel_id |
| 373 | ) |
| 374 | # pop dot1q header(s) |
| 375 | msg = response.payload.payload |
| 376 | self.adapter_agent.receive_proxied_message(proxy_address, msg) |
| 377 | |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 378 | else: |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 379 | ## Mgmt responses received from the OLT |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 380 | ## enqueue incoming parsed frame to right device |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 381 | log.info('received-dot1q-not-8100') |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 382 | self.incoming_queues[response.src].put(response) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 383 | |
| 384 | def _make_ping_frame(self, mac_address): |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 385 | # Create a json packet |
| 386 | json_operation_str = '{\"operation\":\"version\"}' |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 387 | frame = Ether(dst=mac_address)/Dot1Q(vlan=TIBIT_MGMT_VLAN, prio=TIBIT_MGMT_PRIORITY)/TBJSON(data='json %s' % json_operation_str) |
Zsolt Haraszti | 89a2730 | 2016-12-08 16:53:06 -0800 | [diff] [blame] | 388 | return str(frame) |
| 389 | |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 390 | def _make_links_frame(self, mac_address): |
| 391 | # Create a json packet |
| 392 | json_operation_str = '{\"operation\":\"links\"}' |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 393 | frame = Ether(dst=mac_address)/Dot1Q(vlan=TIBIT_MGMT_VLAN, prio=TIBIT_MGMT_PRIORITY)/TBJSON(data='json %s' % json_operation_str) |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 394 | return str(frame) |
| 395 | |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 396 | def abandon_device(self, device): |
| 397 | raise NotImplementedError(0 |
| 398 | ) |
| 399 | def deactivate_device(self, device): |
| 400 | raise NotImplementedError() |
| 401 | |
| 402 | def update_flows_bulk(self, device, flows, groups): |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 403 | log.info('########################################') |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 404 | log.info('bulk-flow-update', device_id=device.id, |
| 405 | flows=flows, groups=groups) |
| 406 | |
| 407 | assert len(groups.items) == 0, "Cannot yet deal with groups" |
| 408 | |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 409 | Clause = {v: k for k, v in ClauseSubtypeEnum.iteritems()} |
| 410 | Operator = {v: k for k, v in RuleOperatorEnum.iteritems()} |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 411 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 412 | for flow in flows.items: |
| 413 | in_port = get_in_port(flow) |
| 414 | assert in_port is not None |
| 415 | |
| 416 | precedence = 255 - min(flow.priority / 256, 255) |
| 417 | |
| 418 | if in_port == 2: |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 419 | log.info('#### Downstream Rule ####') |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 420 | dn_req = PonPortObject() |
| 421 | dn_req /= PortIngressRuleHeader(precedence=precedence) |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 422 | |
| 423 | for field in get_ofb_fields(flow): |
| 424 | |
| 425 | if field.type == ETH_TYPE: |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 426 | _type = field.eth_type |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 427 | log.info('#### field.type == ETH_TYPE ####') |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 428 | dn_req /= PortIngressRuleClauseMatchLength02( |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 429 | fieldcode=Clause['L2 Type/Len'], |
| 430 | operator=Operator['=='], |
| 431 | match=_type) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 432 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 433 | elif field.type == IP_PROTO: |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 434 | _proto = field.ip_proto |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 435 | log.info('#### field.type == IP_PROTO ####') |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 436 | pass # construct ip_proto based condition here |
| 437 | |
| 438 | elif field.type == IN_PORT: |
| 439 | _port = field.port |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 440 | log.info('#### field.type == IN_PORT ####') |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 441 | pass # construct in_port based condition here |
| 442 | |
| 443 | elif field.type == VLAN_VID: |
| 444 | _vlan_vid = field.vlan_vid |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 445 | log.info('#### field.type == VLAN_VID ####') |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 446 | pass # construct VLAN ID based filter condition here |
| 447 | |
| 448 | elif field.type == VLAN_PCP: |
| 449 | _vlan_pcp = field.vlan_pcp |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 450 | log.info('#### field.type == VLAN_PCP ####') |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 451 | pass # construct VLAN PCP based filter condition here |
| 452 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 453 | elif field.type == UDP_SRC: |
| 454 | _udp_src = field.udp_src |
| 455 | log.info('#### field.type == UDP_SRC ####') |
| 456 | pass # construct UDP SRC based filter here |
| 457 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 458 | elif field.type == UDP_DST: |
| 459 | _udp_dst = field.udp_dst |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 460 | log.info('#### field.type == UDP_DST ####') |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 461 | pass # construct UDP DST based filter here |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 462 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 463 | elif field.type == METADATA: |
| 464 | pass # safe to ignore |
| 465 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 466 | else: |
| 467 | raise NotImplementedError('field.type={}'.format( |
| 468 | field.type)) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 469 | |
| 470 | for action in get_actions(flow): |
| 471 | |
| 472 | if action.type == OUTPUT: |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 473 | dn_req /= PortIngressRuleResultForward() |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 474 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 475 | elif action.type == POP_VLAN: |
| 476 | pass # construct vlan pop command here |
| 477 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 478 | elif action.type == PUSH_VLAN: |
| 479 | if action.push.ethertype != 0x8100: |
| 480 | log.error('unhandled-ether-type', |
| 481 | ethertype=action.push.ethertype) |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 482 | dn_req /= PortIngressRuleResultInsert(fieldcode=Clause['C-VLAN Tag']) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 483 | |
| 484 | elif action.type == SET_FIELD: |
| 485 | assert (action.set_field.field.oxm_class == |
| 486 | ofp.OFPXMC_OPENFLOW_BASIC) |
| 487 | field = action.set_field.field.ofb_field |
| 488 | if field.type == VLAN_VID: |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 489 | dn_req /= PortIngressRuleResultSet( |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 490 | fieldcode=Clause['C-VLAN Tag'], value=field.vlan_vid & 0xfff) |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 491 | else: |
| 492 | log.error('unsupported-action-set-field-type', |
| 493 | field_type=field.type) |
| 494 | else: |
| 495 | log.error('unsupported-action-type', |
| 496 | action_type=action.type) |
| 497 | |
| 498 | # dn_req /= PortIngressRuleTerminator() |
| 499 | # dn_req /= AddPortIngressRule() |
| 500 | |
| 501 | # msg = ( |
| 502 | # Ether(dst=device.mac_address) / |
| 503 | # Dot1Q(vlan=TIBIT_MGMT_VLAN, prio=TIBIT_MGMT_PRIORITY) / |
| 504 | # EOAMPayload( |
| 505 | # body=CablelabsOUI() / DPoEOpcode_SetRequest() / dn_req) |
| 506 | # ) |
| 507 | |
| 508 | # self.io_port.send(str(msg)) |
| 509 | |
| 510 | elif in_port == 1: |
| 511 | # Upstream rule |
| 512 | log.info('#### Upstream Rule ####') |
| 513 | up_req = PonPortObject() |
| 514 | up_req /= PortIngressRuleHeader(precedence=precedence) |
| 515 | |
| 516 | for field in get_ofb_fields(flow): |
| 517 | |
| 518 | if field.type == ETH_TYPE: |
| 519 | _type = field.eth_type |
| 520 | up_req /= PortIngressRuleClauseMatchLength02( |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 521 | fieldcode=Clause['L2 Type/Len'], |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 522 | operator=1, |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 523 | match=_type) |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 524 | |
| 525 | elif field.type == IP_PROTO: |
| 526 | _proto = field.ip_proto |
| 527 | log.info('#### field.type == IP_PROTO ####') |
| 528 | pass # construct ip_proto based condition here |
| 529 | |
| 530 | elif field.type == IN_PORT: |
| 531 | _port = field.port |
| 532 | log.info('#### field.type == IN_PORT ####') |
| 533 | pass # construct in_port based condition here |
| 534 | |
| 535 | elif field.type == VLAN_VID: |
| 536 | _vlan_vid = field.vlan_vid |
| 537 | log.info('#### field.type == VLAN_VID ####') |
| 538 | pass # construct VLAN ID based filter condition here |
| 539 | |
| 540 | elif field.type == VLAN_PCP: |
| 541 | _vlan_pcp = field.vlan_pcp |
| 542 | log.info('#### field.type == VLAN_PCP ####') |
| 543 | pass # construct VLAN PCP based filter condition here |
| 544 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 545 | elif field.type == UDP_SRC: |
| 546 | _udp_src = field.udp_src |
| 547 | log.info('#### field.type == UDP_SRC ####') |
| 548 | pass # construct UDP SRC based filter here |
| 549 | |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 550 | elif field.type == UDP_DST: |
| 551 | _udp_dst = field.udp_dst |
| 552 | log.info('#### field.type == UDP_DST ####') |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 553 | pass # construct UDP DST based filter here |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 554 | |
| 555 | else: |
| 556 | raise NotImplementedError('field.type={}'.format( |
| 557 | field.type)) |
| 558 | |
| 559 | for action in get_actions(flow): |
| 560 | |
| 561 | if action.type == OUTPUT: |
| 562 | up_req /= PortIngressRuleResultForward() |
| 563 | |
| 564 | elif action.type == POP_VLAN: |
| 565 | pass # construct vlan pop command here |
| 566 | |
| 567 | elif action.type == PUSH_VLAN: |
| 568 | if action.push.ethertype != 0x8100: |
| 569 | log.error('unhandled-ether-type', |
| 570 | ethertype=action.push.ethertype) |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 571 | up_req /= PortIngressRuleResultInsert(fieldcode=Clause['C-VLAN Tag']) |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 572 | |
| 573 | elif action.type == SET_FIELD: |
| 574 | assert (action.set_field.field.oxm_class == |
| 575 | ofp.OFPXMC_OPENFLOW_BASIC) |
| 576 | field = action.set_field.field.ofb_field |
| 577 | if field.type == VLAN_VID: |
| 578 | up_req /= PortIngressRuleResultSet( |
Nathan Knuth | d390ceb | 2017-01-07 15:38:58 -0800 | [diff] [blame] | 579 | fieldcode=Clause['C-VLAN Tag'], value=field.vlan_vid & 0xfff) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 580 | else: |
| 581 | log.error('unsupported-action-set-field-type', |
| 582 | field_type=field.type) |
| 583 | |
| 584 | else: |
| 585 | log.error('unsupported-action-type', |
| 586 | action_type=action.type) |
| 587 | |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 588 | up_req /= PortIngressRuleTerminator() |
| 589 | up_req /= AddPortIngressRule() |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 590 | |
| 591 | msg = ( |
| 592 | Ether(dst=device.mac_address) / |
| 593 | Dot1Q(vlan=TIBIT_MGMT_VLAN, prio=TIBIT_MGMT_PRIORITY) / |
| 594 | EOAMPayload( |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 595 | body=CablelabsOUI() / DPoEOpcode_SetRequest() / up_req) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 596 | ) |
| 597 | |
| 598 | self.io_port.send(str(msg)) |
| 599 | |
| 600 | else: |
| 601 | raise Exception('Port should be 1 or 2 by our convention') |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 602 | |
| 603 | def update_flows_incrementally(self, device, flow_changes, group_changes): |
| 604 | raise NotImplementedError() |
| 605 | |
| 606 | def send_proxied_message(self, proxy_address, msg): |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 607 | log.info('send-proxied-message', proxy_address=proxy_address) |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 608 | device = self.adapter_agent.get_device(proxy_address.device_id) |
Nathan Knuth | e69ceb1 | 2017-01-04 21:13:39 -0800 | [diff] [blame] | 609 | frame = Ether(dst=device.mac_address) / \ |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 610 | Dot1Q(vlan=TIBIT_MGMT_VLAN, prio=TIBIT_MGMT_PRIORITY) / \ |
| 611 | Dot1Q(vlan=proxy_address.channel_id, prio=TIBIT_MGMT_PRIORITY) / \ |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 612 | msg |
Nathan Knuth | 6e57f33 | 2016-12-22 15:49:20 -0800 | [diff] [blame] | 613 | |
Zsolt Haraszti | 348d193 | 2016-12-10 01:10:07 -0800 | [diff] [blame] | 614 | self.io_port.send(str(frame)) |
Zsolt Haraszti | ed09160 | 2016-12-08 13:36:38 -0800 | [diff] [blame] | 615 | |
| 616 | def receive_proxied_message(self, proxy_address, msg): |
| 617 | raise NotImplementedError() |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 618 | |
| 619 | def receive_packet_out(self, logical_device_id, egress_port_no, msg): |
| 620 | log.info('packet-out', logical_device_id=logical_device_id, |
| 621 | egress_port_no=egress_port_no, msg_len=len(msg)) |
Nathan Knuth | fe2b2e0 | 2017-01-06 07:29:02 -0800 | [diff] [blame] | 622 | |
| 623 | dev_id, logical_dev_id = self.vlan_to_device_ids[egress_port_no] |
| 624 | if logical_dev_id != logical_device_id: |
| 625 | raise Exception('Internal table mismatch') |
| 626 | |
| 627 | tmp = Ether(msg) |
| 628 | |
| 629 | frame = Ether(dst=tmp.dst, src=tmp.src) / \ |
| 630 | Dot1Q(vlan=TIBIT_PACKET_OUT_VLAN) / \ |
| 631 | Dot1Q(vlan=egress_port_no) / \ |
| 632 | tmp.payload |
| 633 | |
| 634 | self.io_port.send(str(frame)) |