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