William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2018 the original author or authors. |
| 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 | import copy |
| 17 | from twisted.internet import reactor |
| 18 | import grpc |
| 19 | from google.protobuf.json_format import MessageToDict |
| 20 | import hashlib |
| 21 | from simplejson import dumps |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 22 | from twisted.internet.defer import inlineCallbacks, returnValue |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 23 | |
William Kurkian | 8b1690c | 2019-03-04 16:53:22 -0500 | [diff] [blame] | 24 | from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \ |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 25 | ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \ |
| 26 | OFPXMT_OFB_VLAN_VID |
William Kurkian | 8b1690c | 2019-03-04 16:53:22 -0500 | [diff] [blame] | 27 | from voltha_protos.device_pb2 import Port |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 28 | import pyvoltha.common.openflow.utils as fd |
William Kurkian | 8b1690c | 2019-03-04 16:53:22 -0500 | [diff] [blame] | 29 | from voltha_protos import openolt_pb2 |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 30 | from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \ |
| 31 | InterAdapterMessageType, InterAdapterOmciMessage |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 32 | |
William Kurkian | 44cd7bb | 2019-02-11 16:39:12 -0500 | [diff] [blame] | 33 | from pyvoltha.common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 34 | |
| 35 | # Flow categories |
| 36 | HSIA_FLOW = "HSIA_FLOW" |
| 37 | |
| 38 | EAP_ETH_TYPE = 0x888e |
| 39 | LLDP_ETH_TYPE = 0x88cc |
| 40 | |
| 41 | IGMP_PROTO = 2 |
| 42 | |
| 43 | # FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py |
| 44 | DEFAULT_MGMT_VLAN = 4091 |
| 45 | |
| 46 | # Openolt Flow |
| 47 | UPSTREAM = "upstream" |
| 48 | DOWNSTREAM = "downstream" |
| 49 | PACKET_TAG_TYPE = "pkt_tag_type" |
| 50 | UNTAGGED = "untagged" |
| 51 | SINGLE_TAG = "single_tag" |
| 52 | DOUBLE_TAG = "double_tag" |
| 53 | |
| 54 | # Classifier |
| 55 | ETH_TYPE = 'eth_type' |
| 56 | TPID = 'tpid' |
| 57 | IP_PROTO = 'ip_proto' |
| 58 | IN_PORT = 'in_port' |
| 59 | VLAN_VID = 'vlan_vid' |
| 60 | VLAN_PCP = 'vlan_pcp' |
| 61 | UDP_DST = 'udp_dst' |
| 62 | UDP_SRC = 'udp_src' |
| 63 | IPV4_DST = 'ipv4_dst' |
| 64 | IPV4_SRC = 'ipv4_src' |
| 65 | METADATA = 'metadata' |
| 66 | OUTPUT = 'output' |
| 67 | # Action |
| 68 | POP_VLAN = 'pop_vlan' |
| 69 | PUSH_VLAN = 'push_vlan' |
| 70 | TRAP_TO_HOST = 'trap_to_host' |
| 71 | |
| 72 | |
| 73 | class OpenOltFlowMgr(object): |
| 74 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 75 | def __init__(self, core_proxy, adapter_proxy, log, stub, device_id, logical_device_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 76 | platform, resource_mgr): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 77 | self.core_proxy = core_proxy |
| 78 | self.adapter_proxy = adapter_proxy |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 79 | self.log = log |
| 80 | self.stub = stub |
| 81 | self.device_id = device_id |
| 82 | self.logical_device_id = logical_device_id |
| 83 | self.nni_intf_id = None |
| 84 | self.platform = platform |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 85 | self.resource_mgr = resource_mgr |
| 86 | self.tech_profile = dict() |
| 87 | self._populate_tech_profile_per_pon_port() |
| 88 | self.retry_add_flow_list = [] |
| 89 | |
| 90 | def add_flow(self, flow): |
| 91 | self.log.debug('add flow', flow=flow) |
| 92 | classifier_info = dict() |
| 93 | action_info = dict() |
| 94 | |
| 95 | for field in fd.get_ofb_fields(flow): |
| 96 | if field.type == fd.ETH_TYPE: |
| 97 | classifier_info[ETH_TYPE] = field.eth_type |
| 98 | self.log.debug('field-type-eth-type', |
| 99 | eth_type=classifier_info[ETH_TYPE]) |
| 100 | elif field.type == fd.IP_PROTO: |
| 101 | classifier_info[IP_PROTO] = field.ip_proto |
| 102 | self.log.debug('field-type-ip-proto', |
| 103 | ip_proto=classifier_info[IP_PROTO]) |
| 104 | elif field.type == fd.IN_PORT: |
| 105 | classifier_info[IN_PORT] = field.port |
| 106 | self.log.debug('field-type-in-port', |
| 107 | in_port=classifier_info[IN_PORT]) |
| 108 | elif field.type == fd.VLAN_VID: |
| 109 | classifier_info[VLAN_VID] = field.vlan_vid & 0xfff |
| 110 | self.log.debug('field-type-vlan-vid', |
| 111 | vlan=classifier_info[VLAN_VID]) |
| 112 | elif field.type == fd.VLAN_PCP: |
| 113 | classifier_info[VLAN_PCP] = field.vlan_pcp |
| 114 | self.log.debug('field-type-vlan-pcp', |
| 115 | pcp=classifier_info[VLAN_PCP]) |
| 116 | elif field.type == fd.UDP_DST: |
| 117 | classifier_info[UDP_DST] = field.udp_dst |
| 118 | self.log.debug('field-type-udp-dst', |
| 119 | udp_dst=classifier_info[UDP_DST]) |
| 120 | elif field.type == fd.UDP_SRC: |
| 121 | classifier_info[UDP_SRC] = field.udp_src |
| 122 | self.log.debug('field-type-udp-src', |
| 123 | udp_src=classifier_info[UDP_SRC]) |
| 124 | elif field.type == fd.IPV4_DST: |
| 125 | classifier_info[IPV4_DST] = field.ipv4_dst |
| 126 | self.log.debug('field-type-ipv4-dst', |
| 127 | ipv4_dst=classifier_info[IPV4_DST]) |
| 128 | elif field.type == fd.IPV4_SRC: |
| 129 | classifier_info[IPV4_SRC] = field.ipv4_src |
| 130 | self.log.debug('field-type-ipv4-src', |
| 131 | ipv4_dst=classifier_info[IPV4_SRC]) |
| 132 | elif field.type == fd.METADATA: |
| 133 | classifier_info[METADATA] = field.table_metadata |
| 134 | self.log.debug('field-type-metadata', |
| 135 | metadata=classifier_info[METADATA]) |
| 136 | else: |
| 137 | raise NotImplementedError('field.type={}'.format( |
| 138 | field.type)) |
| 139 | |
| 140 | for action in fd.get_actions(flow): |
| 141 | if action.type == fd.OUTPUT: |
| 142 | action_info[OUTPUT] = action.output.port |
| 143 | self.log.debug('action-type-output', |
| 144 | output=action_info[OUTPUT], |
| 145 | in_port=classifier_info[IN_PORT]) |
| 146 | elif action.type == fd.POP_VLAN: |
| 147 | if fd.get_goto_table_id(flow) is None: |
| 148 | self.log.debug('being taken care of by ONU', flow=flow) |
| 149 | return |
| 150 | action_info[POP_VLAN] = True |
| 151 | self.log.debug('action-type-pop-vlan', |
| 152 | in_port=classifier_info[IN_PORT]) |
| 153 | elif action.type == fd.PUSH_VLAN: |
| 154 | action_info[PUSH_VLAN] = True |
| 155 | action_info[TPID] = action.push.ethertype |
| 156 | self.log.debug('action-type-push-vlan', |
| 157 | push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT]) |
| 158 | if action.push.ethertype != 0x8100: |
| 159 | self.log.error('unhandled-tpid', |
| 160 | ethertype=action.push.ethertype) |
| 161 | elif action.type == fd.SET_FIELD: |
| 162 | # action_info['action_type'] = 'set_field' |
| 163 | _field = action.set_field.field.ofb_field |
| 164 | assert (action.set_field.field.oxm_class == |
| 165 | OFPXMC_OPENFLOW_BASIC) |
| 166 | self.log.debug('action-type-set-field', |
| 167 | field=_field, in_port=classifier_info[IN_PORT]) |
| 168 | if _field.type == fd.VLAN_VID: |
| 169 | self.log.debug('set-field-type-vlan-vid', |
| 170 | vlan_vid=_field.vlan_vid & 0xfff) |
| 171 | action_info[VLAN_VID] = (_field.vlan_vid & 0xfff) |
| 172 | else: |
| 173 | self.log.error('unsupported-action-set-field-type', |
| 174 | field_type=_field.type) |
| 175 | else: |
| 176 | self.log.error('unsupported-action-type', |
| 177 | action_type=action.type, in_port=classifier_info[IN_PORT]) |
| 178 | |
| 179 | if fd.get_goto_table_id(flow) is not None and POP_VLAN not in action_info: |
| 180 | self.log.debug('being taken care of by ONU', flow=flow) |
| 181 | return |
| 182 | |
| 183 | if OUTPUT not in action_info and METADATA in classifier_info: |
| 184 | # find flow in the next table |
| 185 | next_flow = self.find_next_flow(flow) |
| 186 | if next_flow is None: |
| 187 | return |
| 188 | action_info[OUTPUT] = fd.get_out_port(next_flow) |
| 189 | for field in fd.get_ofb_fields(next_flow): |
| 190 | if field.type == fd.VLAN_VID: |
| 191 | classifier_info[METADATA] = field.vlan_vid & 0xfff |
| 192 | |
| 193 | self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT]) |
| 194 | (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow( |
| 195 | classifier_info[IN_PORT], action_info[OUTPUT]) |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 196 | self.log.debug('extracted-flow-ports', port_no=port_no, intf_id=intf_id, onu_id=onu_id, uni_id=uni_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 197 | |
| 198 | self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info, |
| 199 | action_info, flow) |
| 200 | |
| 201 | def _is_uni_port(self, port_no): |
| 202 | try: |
| 203 | port = self.adapter_agent.get_logical_port(self.logical_device_id, |
| 204 | 'uni-{}'.format(port_no)) |
| 205 | if port is not None: |
| 206 | return (not port.root_port), port.device_id |
| 207 | else: |
| 208 | return False, None |
| 209 | except Exception as e: |
| 210 | self.log.error("error-retrieving-port", e=e) |
| 211 | return False, None |
| 212 | |
| 213 | def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction): |
| 214 | uni_port_no = None |
| 215 | child_device_id = None |
| 216 | if flow_direction == UPSTREAM: |
| 217 | for field in fd.get_ofb_fields(flow): |
| 218 | if field.type == fd.IN_PORT: |
| 219 | is_uni, child_device_id = self._is_uni_port(field.port) |
| 220 | if is_uni: |
| 221 | uni_port_no = field.port |
| 222 | elif flow_direction == DOWNSTREAM: |
| 223 | for field in fd.get_ofb_fields(flow): |
| 224 | if field.type == fd.METADATA: |
| 225 | uni_port = field.table_metadata & 0xFFFFFFFF |
| 226 | is_uni, child_device_id = self._is_uni_port(uni_port) |
| 227 | if is_uni: |
| 228 | uni_port_no = field.port |
| 229 | |
| 230 | if uni_port_no is None: |
| 231 | for action in fd.get_actions(flow): |
| 232 | if action.type == fd.OUTPUT: |
| 233 | is_uni, child_device_id = \ |
| 234 | self._is_uni_port(action.output.port) |
| 235 | if is_uni: |
| 236 | uni_port_no = action.output.port |
| 237 | |
| 238 | if child_device_id: |
| 239 | child_device = self.adapter_agent.get_device(child_device_id) |
| 240 | pon_intf = child_device.proxy_address.channel_id |
| 241 | onu_id = child_device.proxy_address.onu_id |
| 242 | uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None |
| 243 | flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id) |
| 244 | assert (isinstance(flows, list)) |
| 245 | self.log.debug("retrieved-flows", flows=flows) |
| 246 | for idx in range(len(flows)): |
| 247 | if flow_direction == flows[idx]['flow_type']: |
| 248 | flows.pop(idx) |
| 249 | self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows) |
| 250 | if len(flows) > 0: |
| 251 | # There are still flows referencing the same flow_id. |
| 252 | # So the flow should not be freed yet. |
| 253 | # For ex: Case of HSIA where same flow is shared |
| 254 | # between DS and US. |
| 255 | return |
| 256 | |
| 257 | self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id) |
| 258 | else: |
| 259 | self.log.error("invalid-info", uni_port_no=uni_port_no, |
| 260 | child_device_id=child_device_id) |
| 261 | |
| 262 | def retry_add_flow(self, flow): |
| 263 | self.log.debug("retry-add-flow") |
| 264 | if flow.id in self.retry_add_flow_list: |
| 265 | self.retry_add_flow_list.remove(flow.id) |
| 266 | self.add_flow(flow) |
| 267 | |
| 268 | def remove_flow(self, flow): |
| 269 | self.log.debug('trying to remove flows from logical flow :', |
| 270 | logical_flow=flow) |
| 271 | device_flows_to_remove = [] |
| 272 | device_flows = self.flows_proxy.get('/').items |
| 273 | for f in device_flows: |
| 274 | if f.cookie == flow.id: |
| 275 | device_flows_to_remove.append(f) |
| 276 | |
| 277 | for f in device_flows_to_remove: |
| 278 | (id, direction) = self.decode_stored_id(f.id) |
| 279 | flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction) |
| 280 | try: |
| 281 | self.stub.FlowRemove(flow_to_remove) |
| 282 | except grpc.RpcError as grpc_e: |
| 283 | if grpc_e.code() == grpc.StatusCode.NOT_FOUND: |
| 284 | self.log.debug('This flow does not exist on the switch, ' |
| 285 | 'normal after an OLT reboot', |
| 286 | flow=flow_to_remove) |
| 287 | else: |
| 288 | raise grpc_e |
| 289 | |
| 290 | # once we have successfully deleted the flow on the device |
| 291 | # release the flow_id on resource pool and also clear any |
| 292 | # data associated with the flow_id on KV store. |
| 293 | self._clear_flow_id_from_rm(f, id, direction) |
| 294 | self.log.debug('flow removed from device', flow=f, |
| 295 | flow_key=flow_to_remove) |
| 296 | |
| 297 | if len(device_flows_to_remove) > 0: |
| 298 | new_flows = [] |
| 299 | flows_ids_to_remove = [f.id for f in device_flows_to_remove] |
| 300 | for f in device_flows: |
| 301 | if f.id not in flows_ids_to_remove: |
| 302 | new_flows.append(f) |
| 303 | |
| 304 | self.flows_proxy.update('/', Flows(items=new_flows)) |
| 305 | self.log.debug('flows removed from the data store', |
| 306 | flow_ids_removed=flows_ids_to_remove, |
| 307 | number_of_flows_removed=(len(device_flows) - len( |
| 308 | new_flows)), expected_flows_removed=len( |
| 309 | device_flows_to_remove)) |
| 310 | else: |
| 311 | self.log.debug('no device flow to remove for this flow (normal ' |
| 312 | 'for multi table flows)', flow=flow) |
| 313 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 314 | def get_tp_path(self, intf_id, uni): |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 315 | # FIXME Should get Table id form the flow, as of now hardcoded to |
| 316 | # DEFAULT_TECH_PROFILE_TABLE_ID (64) |
| 317 | # 'tp_path' contains the suffix part of the tech_profile_instance path. |
| 318 | # The prefix to the 'tp_path' should be set to \ |
| 319 | # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter. |
| 320 | return self.tech_profile[intf_id]. \ |
| 321 | get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 322 | uni) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 323 | |
| 324 | def delete_tech_profile_instance(self, intf_id, onu_id, uni_id): |
| 325 | # Remove the TP instance associated with the ONU |
| 326 | ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id) |
| 327 | tp_path = self.get_tp_path(intf_id, ofp_port_name) |
| 328 | return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path) |
| 329 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 330 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 331 | def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier, |
| 332 | action, flow): |
| 333 | |
| 334 | self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no, |
| 335 | classifier=classifier, action=action) |
| 336 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 337 | uni = self.get_uni_port_path(intf_id, onu_id, uni_id) |
| 338 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 339 | alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 340 | uni, port_no, flow.table_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 341 | if alloc_id is None or gem_ports is None: |
| 342 | self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id, |
| 343 | gem_ports=gem_ports) |
| 344 | return |
| 345 | |
| 346 | self.log.debug('Generated required alloc and gemport ids', |
| 347 | alloc_id=alloc_id, gemports=gem_ports) |
| 348 | |
| 349 | # Flows can't be added specific to gemport unless p-bits are received. |
| 350 | # Hence adding flows for all gemports |
| 351 | for gemport_id in gem_ports: |
| 352 | if IP_PROTO in classifier: |
| 353 | if classifier[IP_PROTO] == 17: |
| 354 | self.log.debug('dhcp flow add') |
| 355 | self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier, |
| 356 | action, flow, alloc_id, gemport_id) |
| 357 | elif classifier[IP_PROTO] == 2: |
| 358 | self.log.warn('igmp flow add ignored, not implemented yet') |
| 359 | else: |
| 360 | self.log.warn("Invalid-Classifier-to-handle", |
| 361 | classifier=classifier, |
| 362 | action=action) |
| 363 | elif ETH_TYPE in classifier: |
| 364 | if classifier[ETH_TYPE] == EAP_ETH_TYPE: |
| 365 | self.log.debug('eapol flow add') |
| 366 | self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id, |
| 367 | gemport_id) |
| 368 | vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow)) |
| 369 | if vlan_id is not None: |
| 370 | self.add_eapol_flow( |
| 371 | intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id, |
| 372 | vlan_id=vlan_id) |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 373 | parent_port_no = self.platform.intf_id_to_port_no(port_no, Port.PON_OLT) |
| 374 | onu_device = yield self.core_proxy.get_child_device(self.device_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 375 | onu_id=onu_id, |
| 376 | parent_port_no=parent_port_no) |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 377 | tp_path = self.get_tp_path(intf_id, uni) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 378 | |
| 379 | self.log.debug('Load-tech-profile-request-to-brcm-handler', |
| 380 | tp_path=tp_path) |
| 381 | msg = {'proxy_address': onu_device.proxy_address, 'uni_id': uni_id, |
| 382 | 'event': 'download_tech_profile', 'event_data': tp_path} |
| 383 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 384 | # TODO NEW CORE: Create a new interadapter message type for tech profile startup for the onu |
| 385 | # Send the tech profile event to the onu adapter |
| 386 | #yield self.adapter_proxy.send_inter_adapter_message( |
| 387 | # msg=msg, |
| 388 | # type=InterAdapterMessageType.TECH_IND_REQUEST, |
| 389 | # from_adapter="openolt", |
| 390 | # to_adapter=onu_device.type, |
| 391 | # to_device_id=onu_device.id |
| 392 | #) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 393 | |
| 394 | if classifier[ETH_TYPE] == LLDP_ETH_TYPE: |
| 395 | self.log.debug('lldp flow add') |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 396 | nni_intf_id = yield self.get_nni_intf_id() |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 397 | self.add_lldp_flow(flow, port_no, nni_intf_id) |
| 398 | |
| 399 | elif PUSH_VLAN in action: |
| 400 | self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier, |
| 401 | action, flow, alloc_id, gemport_id) |
| 402 | elif POP_VLAN in action: |
| 403 | self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier, |
| 404 | action, flow, alloc_id, gemport_id) |
| 405 | else: |
| 406 | self.log.debug('Invalid-flow-type-to-handle', |
| 407 | classifier=classifier, |
| 408 | action=action, flow=flow) |
| 409 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 410 | def get_uni_port_path(self, intf_id, onu_id, uni_id): |
| 411 | value = 'pon-{}/onu-{}/uni-{}'.format(intf_id, onu_id, uni_id) |
| 412 | return value |
| 413 | |
| 414 | def create_tcont_gemport(self, intf_id, onu_id, uni_id, uni, port_no, table_id): |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 415 | alloc_id, gem_port_ids = None, None |
| 416 | pon_intf_onu_id = (intf_id, onu_id) |
| 417 | |
| 418 | # If we already have allocated alloc_id and gem_ports earlier, render them |
| 419 | alloc_id = \ |
| 420 | self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id) |
| 421 | gem_port_ids = \ |
| 422 | self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id) |
| 423 | if alloc_id is not None and gem_port_ids is not None: |
| 424 | return alloc_id, gem_port_ids |
| 425 | |
| 426 | try: |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 427 | # FIXME: If table id is <= 63 using 64 as table id |
| 428 | if table_id < DEFAULT_TECH_PROFILE_TABLE_ID: |
| 429 | table_id = DEFAULT_TECH_PROFILE_TABLE_ID |
| 430 | |
| 431 | # Check tech profile instance already exists for derived port name |
| 432 | tech_profile_instance = self.tech_profile[intf_id]. \ |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 433 | get_tech_profile_instance(table_id, uni) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 434 | self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance) |
| 435 | |
| 436 | if tech_profile_instance is None: |
| 437 | # create tech profile instance |
| 438 | tech_profile_instance = self.tech_profile[intf_id]. \ |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 439 | create_tech_profile_instance(table_id, uni, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 440 | intf_id) |
| 441 | if tech_profile_instance is None: |
| 442 | raise Exception('Tech-profile-instance-creation-failed') |
| 443 | else: |
| 444 | self.log.debug( |
| 445 | 'Tech-profile-instance-already-exist-for-given port-name', |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 446 | table_id=table_id, intf_id=intf_id, uni=uni) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 447 | |
| 448 | # upstream scheduler |
| 449 | us_scheduler = self.tech_profile[intf_id].get_us_scheduler( |
| 450 | tech_profile_instance) |
| 451 | # downstream scheduler |
| 452 | ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler( |
| 453 | tech_profile_instance) |
| 454 | # create Tcont |
| 455 | tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance, |
| 456 | us_scheduler, |
| 457 | ds_scheduler) |
| 458 | |
| 459 | self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id, |
| 460 | onu_id=onu_id, |
| 461 | uni_id=uni_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 462 | port_no=port_no, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 463 | tconts=tconts)) |
| 464 | |
| 465 | # Fetch alloc id and gemports from tech profile instance |
| 466 | alloc_id = tech_profile_instance.us_scheduler.alloc_id |
| 467 | gem_port_ids = [] |
| 468 | for i in range(len( |
| 469 | tech_profile_instance.upstream_gem_port_attribute_list)): |
| 470 | gem_port_ids.append( |
| 471 | tech_profile_instance.upstream_gem_port_attribute_list[i]. |
| 472 | gemport_id) |
| 473 | except BaseException as e: |
| 474 | self.log.exception(exception=e) |
| 475 | |
| 476 | # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store |
| 477 | pon_intf_onu_id = (intf_id, onu_id, uni_id) |
| 478 | self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu( |
| 479 | pon_intf_onu_id, |
| 480 | list([alloc_id]) |
| 481 | ) |
| 482 | self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu( |
| 483 | pon_intf_onu_id, |
| 484 | gem_port_ids |
| 485 | ) |
| 486 | |
| 487 | self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store( |
| 488 | gem_port_ids, intf_id, onu_id, uni_id |
| 489 | ) |
| 490 | |
| 491 | return alloc_id, gem_port_ids |
| 492 | |
| 493 | def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier, |
| 494 | uplink_action, logical_flow, alloc_id, |
| 495 | gemport_id): |
| 496 | |
| 497 | uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 498 | |
| 499 | self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier, |
| 500 | uplink_action, UPSTREAM, |
| 501 | logical_flow, alloc_id, gemport_id) |
| 502 | |
| 503 | # Secondary EAP on the subscriber vlan |
| 504 | (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id) |
| 505 | if eap_active: |
| 506 | self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id, |
| 507 | gemport_id, vlan_id=uplink_classifier[VLAN_VID]) |
| 508 | |
| 509 | def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier, |
| 510 | downlink_action, flow, alloc_id, gemport_id): |
| 511 | downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG |
| 512 | # Needed ???? It should be already there |
| 513 | downlink_action[POP_VLAN] = True |
| 514 | downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID] |
| 515 | |
| 516 | self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier, |
| 517 | downlink_action, DOWNSTREAM, |
| 518 | flow, alloc_id, gemport_id) |
| 519 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 520 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 521 | def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action, |
| 522 | direction, logical_flow, alloc_id, gemport_id): |
| 523 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 524 | self.log.debug('add hisa flow', flow=logical_flow, port_no=port_no, |
| 525 | intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id, |
| 526 | alloc_id=alloc_id) |
| 527 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 528 | flow_store_cookie = self._get_flow_store_cookie(classifier, |
| 529 | gemport_id) |
| 530 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 531 | self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier, |
| 532 | action=action) |
| 533 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 534 | # One of the OLT platform (Broadcom BAL) requires that symmetric |
| 535 | # flows require the same flow_id to be used across UL and DL. |
| 536 | # Since HSIA flow is the only symmetric flow currently, we need to |
| 537 | # re-use the flow_id across both direction. The 'flow_category' |
| 538 | # takes priority over flow_cookie to find any available HSIA_FLOW |
| 539 | # id for the ONU. |
| 540 | flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id, |
| 541 | flow_store_cookie, |
| 542 | HSIA_FLOW) |
| 543 | if flow_id is None: |
| 544 | self.log.error("hsia-flow-unavailable") |
| 545 | return |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 546 | |
| 547 | self.log.debug('flow-id', flow_id=flow_id) |
| 548 | |
| 549 | network_intf_id = yield self.get_nni_intf_id() |
| 550 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 551 | flow = openolt_pb2.Flow( |
| 552 | access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 553 | flow_type=direction, alloc_id=alloc_id, network_intf_id=network_intf_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 554 | gemport_id=gemport_id, |
| 555 | classifier=self.mk_classifier(classifier), |
| 556 | action=self.mk_action(action), |
| 557 | priority=logical_flow.priority, |
| 558 | port_no=port_no, |
| 559 | cookie=logical_flow.cookie) |
| 560 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 561 | self.log.debug('openolt-agent-flow', hsia_flow=flow) |
| 562 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 563 | if self.add_flow_to_device(flow, logical_flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 564 | self.log.debug('added-hsia-openolt-agent-flow', hsia_flow=flow, logical_flow=logical_flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 565 | flow_info = self._get_flow_info_as_json_blob(flow, |
| 566 | flow_store_cookie, |
| 567 | HSIA_FLOW) |
| 568 | self.update_flow_info_to_kv_store(flow.access_intf_id, |
| 569 | flow.onu_id, flow.uni_id, |
| 570 | flow.flow_id, flow_info) |
| 571 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 572 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 573 | def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow, |
| 574 | alloc_id, gemport_id): |
| 575 | |
| 576 | self.log.debug('add dhcp upstream trap', classifier=classifier, |
| 577 | intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action) |
| 578 | |
| 579 | action.clear() |
| 580 | action[TRAP_TO_HOST] = True |
| 581 | classifier[UDP_SRC] = 68 |
| 582 | classifier[UDP_DST] = 67 |
| 583 | classifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 584 | classifier.pop(VLAN_VID, None) |
| 585 | |
| 586 | flow_store_cookie = self._get_flow_store_cookie(classifier, |
| 587 | gemport_id) |
| 588 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 589 | self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier, |
| 590 | action=action) |
| 591 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 592 | flow_id = self.resource_mgr.get_flow_id( |
| 593 | intf_id, onu_id, uni_id, flow_store_cookie |
| 594 | ) |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 595 | |
| 596 | self.log.debug('flow-id', flow_id=flow_id) |
| 597 | |
| 598 | network_intf_id = yield self.get_nni_intf_id() |
| 599 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 600 | dhcp_flow = openolt_pb2.Flow( |
| 601 | onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM, |
| 602 | access_intf_id=intf_id, gemport_id=gemport_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 603 | alloc_id=alloc_id, network_intf_id=network_intf_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 604 | priority=logical_flow.priority, |
| 605 | classifier=self.mk_classifier(classifier), |
| 606 | action=self.mk_action(action), |
| 607 | port_no=port_no, |
| 608 | cookie=logical_flow.cookie) |
| 609 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 610 | self.log.debug('openolt-agent-flow', dhcp_flow=dhcp_flow) |
| 611 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 612 | if self.add_flow_to_device(dhcp_flow, logical_flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 613 | self.log.debug('added-dhcp-openolt-agent-flow', dhcp_flow=dhcp_flow, logical_flow=logical_flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 614 | flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie) |
| 615 | self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id, |
| 616 | dhcp_flow.onu_id, |
| 617 | dhcp_flow.uni_id, |
| 618 | dhcp_flow.flow_id, |
| 619 | flow_info) |
| 620 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 621 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 622 | def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id, |
| 623 | gemport_id, vlan_id=DEFAULT_MGMT_VLAN): |
| 624 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 625 | self.log.debug('add eapol upstream trap', flow=logical_flow, port_no=port_no, |
| 626 | intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id, |
| 627 | alloc_id=alloc_id, vlan_id=vlan_id) |
| 628 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 629 | uplink_classifier = dict() |
| 630 | uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE |
| 631 | uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 632 | uplink_classifier[VLAN_VID] = vlan_id |
| 633 | |
| 634 | uplink_action = dict() |
| 635 | uplink_action[TRAP_TO_HOST] = True |
| 636 | |
| 637 | flow_store_cookie = self._get_flow_store_cookie(uplink_classifier, |
| 638 | gemport_id) |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 639 | |
| 640 | self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, uplink_classifier=uplink_classifier, |
| 641 | uplink_action=uplink_action) |
| 642 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 643 | # Add Upstream EAPOL Flow. |
| 644 | uplink_flow_id = self.resource_mgr.get_flow_id( |
| 645 | intf_id, onu_id, uni_id, flow_store_cookie |
| 646 | ) |
| 647 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 648 | self.log.debug('flow-id', uplink_flow_id=uplink_flow_id) |
| 649 | |
| 650 | network_intf_id = yield self.get_nni_intf_id() |
| 651 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 652 | upstream_flow = openolt_pb2.Flow( |
| 653 | access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 654 | flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 655 | gemport_id=gemport_id, |
| 656 | classifier=self.mk_classifier(uplink_classifier), |
| 657 | action=self.mk_action(uplink_action), |
| 658 | priority=logical_flow.priority, |
| 659 | port_no=port_no, |
| 660 | cookie=logical_flow.cookie) |
| 661 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 662 | self.log.debug('openolt-agent-flow', upstream_flow=upstream_flow) |
| 663 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 664 | logical_flow = copy.deepcopy(logical_flow) |
| 665 | logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid( |
| 666 | vlan_id | 0x1000)])) |
| 667 | logical_flow.match.type = OFPMT_OXM |
| 668 | |
| 669 | if self.add_flow_to_device(upstream_flow, logical_flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 670 | self.log.debug('added-eapol-openolt-agent-flow', upstream_flow=upstream_flow, logical_flow=logical_flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 671 | flow_info = self._get_flow_info_as_json_blob(upstream_flow, |
| 672 | flow_store_cookie) |
| 673 | self.update_flow_info_to_kv_store(upstream_flow.access_intf_id, |
| 674 | upstream_flow.onu_id, |
| 675 | upstream_flow.uni_id, |
| 676 | upstream_flow.flow_id, |
| 677 | flow_info) |
| 678 | |
| 679 | if vlan_id == DEFAULT_MGMT_VLAN: |
| 680 | # Add Downstream EAPOL Flow, Only for first EAP flow (BAL |
| 681 | # requirement) |
| 682 | # On one of the platforms (Broadcom BAL), when same DL classifier |
| 683 | # vlan was used across multiple ONUs, eapol flow re-adds after |
| 684 | # flow delete (cases of onu reboot/disable) fails. |
| 685 | # In order to generate unique vlan, a combination of intf_id |
| 686 | # onu_id and uni_id is used. |
| 687 | # uni_id defaults to 0, so add 1 to it. |
| 688 | special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1) |
| 689 | # Assert that we do not generate invalid vlans under no condition |
| 690 | assert (special_vlan_downstream_flow >= 2, 'invalid-vlan-generated') |
| 691 | |
| 692 | downlink_classifier = dict() |
| 693 | downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 694 | downlink_classifier[VLAN_VID] = special_vlan_downstream_flow |
| 695 | |
| 696 | downlink_action = dict() |
| 697 | downlink_action[PUSH_VLAN] = True |
| 698 | downlink_action[VLAN_VID] = vlan_id |
| 699 | |
| 700 | |
| 701 | flow_store_cookie = self._get_flow_store_cookie(downlink_classifier, |
| 702 | gemport_id) |
| 703 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 704 | self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, downlink_classifier=downlink_classifier, |
| 705 | downlink_action=downlink_action) |
| 706 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 707 | downlink_flow_id = self.resource_mgr.get_flow_id( |
| 708 | intf_id, onu_id, uni_id, flow_store_cookie |
| 709 | ) |
| 710 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 711 | self.log.debug('flow-id', downlink_flow_id=downlink_flow_id) |
| 712 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 713 | downstream_flow = openolt_pb2.Flow( |
| 714 | access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id, |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 715 | flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id, |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 716 | gemport_id=gemport_id, |
| 717 | classifier=self.mk_classifier(downlink_classifier), |
| 718 | action=self.mk_action(downlink_action), |
| 719 | priority=logical_flow.priority, |
| 720 | port_no=port_no, |
| 721 | cookie=logical_flow.cookie) |
| 722 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 723 | self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow) |
| 724 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 725 | downstream_logical_flow = ofp_flow_stats( |
| 726 | id=logical_flow.id, cookie=logical_flow.cookie, |
| 727 | table_id=logical_flow.table_id, priority=logical_flow.priority, |
| 728 | flags=logical_flow.flags) |
| 729 | |
| 730 | downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([ |
| 731 | fd.in_port(fd.get_out_port(logical_flow)), |
| 732 | fd.vlan_vid(special_vlan_downstream_flow | 0x1000)])) |
| 733 | downstream_logical_flow.match.type = OFPMT_OXM |
| 734 | |
| 735 | downstream_logical_flow.instructions.extend( |
| 736 | fd.mk_instructions_from_actions([fd.output( |
| 737 | self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))])) |
| 738 | |
| 739 | if self.add_flow_to_device(downstream_flow, downstream_logical_flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 740 | self.log.debug('added-eapol-openolt-agent-flow', downstream_flow=downstream_flow, |
| 741 | downstream_logical_flow=downstream_logical_flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 742 | flow_info = self._get_flow_info_as_json_blob(downstream_flow, |
| 743 | flow_store_cookie) |
| 744 | self.update_flow_info_to_kv_store(downstream_flow.access_intf_id, |
| 745 | downstream_flow.onu_id, |
| 746 | downstream_flow.uni_id, |
| 747 | downstream_flow.flow_id, |
| 748 | flow_info) |
| 749 | |
| 750 | def repush_all_different_flows(self): |
| 751 | # Check if the device is supposed to have flows, if so add them |
| 752 | # Recover static flows after a reboot |
| 753 | logical_flows = self.logical_flows_proxy.get('/').items |
| 754 | devices_flows = self.flows_proxy.get('/').items |
| 755 | logical_flows_ids_provisioned = [f.cookie for f in devices_flows] |
| 756 | for logical_flow in logical_flows: |
| 757 | try: |
| 758 | if logical_flow.id not in logical_flows_ids_provisioned: |
| 759 | self.add_flow(logical_flow) |
| 760 | except Exception as e: |
| 761 | self.log.exception('Problem reading this flow', e=e) |
| 762 | |
| 763 | def reset_flows(self): |
| 764 | self.flows_proxy.update('/', Flows()) |
| 765 | |
| 766 | """ Add a downstream LLDP trap flow on the NNI interface |
| 767 | """ |
| 768 | |
| 769 | def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0): |
| 770 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 771 | self.log.debug('add lldp trap flow', flow=logical_flow, port_no=port_no, |
| 772 | network_intf_id=network_intf_id) |
| 773 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 774 | classifier = dict() |
| 775 | classifier[ETH_TYPE] = LLDP_ETH_TYPE |
| 776 | classifier[PACKET_TAG_TYPE] = UNTAGGED |
| 777 | action = dict() |
| 778 | action[TRAP_TO_HOST] = True |
| 779 | |
| 780 | # LLDP flow is installed to trap LLDP packets on the NNI port. |
| 781 | # We manage flow_id resource pool on per PON port basis. |
| 782 | # Since this situation is tricky, as a hack, we pass the NNI port |
| 783 | # index (network_intf_id) as PON port Index for the flow_id resource |
| 784 | # pool. Also, there is no ONU Id available for trapping LLDP packets |
| 785 | # on NNI port, use onu_id as -1 (invalid) |
| 786 | # ****************** CAVEAT ******************* |
| 787 | # This logic works if the NNI Port Id falls within the same valid |
| 788 | # range of PON Port Ids. If this doesn't work for some OLT Vendor |
| 789 | # we need to have a re-look at this. |
| 790 | # ********************************************* |
| 791 | onu_id = -1 |
| 792 | uni_id = -1 |
| 793 | flow_store_cookie = self._get_flow_store_cookie(classifier) |
| 794 | flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id, |
| 795 | flow_store_cookie) |
| 796 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 797 | self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, classifier=classifier, |
| 798 | action=action) |
| 799 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 800 | downstream_flow = openolt_pb2.Flow( |
| 801 | access_intf_id=-1, # access_intf_id not required |
| 802 | onu_id=onu_id, # onu_id not required |
| 803 | uni_id=uni_id, # uni_id not used |
| 804 | flow_id=flow_id, |
| 805 | flow_type=DOWNSTREAM, |
| 806 | network_intf_id=network_intf_id, |
| 807 | gemport_id=-1, # gemport_id not required |
| 808 | classifier=self.mk_classifier(classifier), |
| 809 | action=self.mk_action(action), |
| 810 | priority=logical_flow.priority, |
| 811 | port_no=port_no, |
| 812 | cookie=logical_flow.cookie) |
| 813 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 814 | self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow) |
| 815 | |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 816 | if self.add_flow_to_device(downstream_flow, logical_flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 817 | self.log.debug('added-lldp-openolt-agent-flow', downstream_flow=downstream_flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 818 | self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id, |
| 819 | flow_id, downstream_flow) |
| 820 | |
| 821 | def mk_classifier(self, classifier_info): |
| 822 | |
| 823 | classifier = openolt_pb2.Classifier() |
| 824 | |
| 825 | if ETH_TYPE in classifier_info: |
| 826 | classifier.eth_type = classifier_info[ETH_TYPE] |
| 827 | if IP_PROTO in classifier_info: |
| 828 | classifier.ip_proto = classifier_info[IP_PROTO] |
| 829 | if VLAN_VID in classifier_info: |
| 830 | classifier.o_vid = classifier_info[VLAN_VID] |
| 831 | if METADATA in classifier_info: |
| 832 | classifier.i_vid = classifier_info[METADATA] |
| 833 | if VLAN_PCP in classifier_info: |
| 834 | classifier.o_pbits = classifier_info[VLAN_PCP] |
| 835 | if UDP_SRC in classifier_info: |
| 836 | classifier.src_port = classifier_info[UDP_SRC] |
| 837 | if UDP_DST in classifier_info: |
| 838 | classifier.dst_port = classifier_info[UDP_DST] |
| 839 | if IPV4_DST in classifier_info: |
| 840 | classifier.dst_ip = classifier_info[IPV4_DST] |
| 841 | if IPV4_SRC in classifier_info: |
| 842 | classifier.src_ip = classifier_info[IPV4_SRC] |
| 843 | if PACKET_TAG_TYPE in classifier_info: |
| 844 | if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG: |
| 845 | classifier.pkt_tag_type = SINGLE_TAG |
| 846 | elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG: |
| 847 | classifier.pkt_tag_type = DOUBLE_TAG |
| 848 | elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED: |
| 849 | classifier.pkt_tag_type = UNTAGGED |
| 850 | else: |
| 851 | classifier.pkt_tag_type = 'none' |
| 852 | |
| 853 | return classifier |
| 854 | |
| 855 | def mk_action(self, action_info): |
| 856 | action = openolt_pb2.Action() |
| 857 | |
| 858 | if POP_VLAN in action_info: |
| 859 | action.o_vid = action_info[VLAN_VID] |
| 860 | action.cmd.remove_outer_tag = True |
| 861 | elif PUSH_VLAN in action_info: |
| 862 | action.o_vid = action_info[VLAN_VID] |
| 863 | action.cmd.add_outer_tag = True |
| 864 | elif TRAP_TO_HOST in action_info: |
| 865 | action.cmd.trap_to_host = True |
| 866 | else: |
| 867 | self.log.info('Invalid-action-field', action_info=action_info) |
| 868 | return |
| 869 | return action |
| 870 | |
| 871 | def is_eap_enabled(self, intf_id, onu_id, uni_id): |
| 872 | flows = self.logical_flows_proxy.get('/').items |
| 873 | |
| 874 | for flow in flows: |
| 875 | eap_flow = False |
| 876 | eap_intf_id = None |
| 877 | eap_onu_id = None |
| 878 | eap_uni_id = None |
| 879 | for field in fd.get_ofb_fields(flow): |
| 880 | if field.type == fd.ETH_TYPE: |
| 881 | if field.eth_type == EAP_ETH_TYPE: |
| 882 | eap_flow = True |
| 883 | if field.type == fd.IN_PORT: |
| 884 | eap_intf_id = self.platform.intf_id_from_uni_port_num( |
| 885 | field.port) |
| 886 | eap_onu_id = self.platform.onu_id_from_port_num(field.port) |
| 887 | eap_uni_id = self.platform.uni_id_from_port_num(field.port) |
| 888 | |
| 889 | if eap_flow: |
| 890 | self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id, |
| 891 | intf_id=intf_id, eap_intf_id=eap_intf_id, |
| 892 | eap_onu_id=eap_onu_id, |
| 893 | eap_uni_id=eap_uni_id) |
| 894 | if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id: |
| 895 | return True, flow |
| 896 | |
| 897 | return False, None |
| 898 | |
| 899 | def get_subscriber_vlan(self, port): |
| 900 | self.log.debug('looking from subscriber flow for port', port=port) |
| 901 | |
| 902 | flows = self.logical_flows_proxy.get('/').items |
| 903 | for flow in flows: |
| 904 | in_port = fd.get_in_port(flow) |
| 905 | out_port = fd.get_out_port(flow) |
| 906 | if in_port == port and out_port is not None and \ |
| 907 | self.platform.intf_id_to_port_type_name(out_port) \ |
| 908 | == Port.ETHERNET_NNI: |
| 909 | fields = fd.get_ofb_fields(flow) |
| 910 | self.log.debug('subscriber flow found', fields=fields) |
| 911 | for field in fields: |
| 912 | if field.type == OFPXMT_OFB_VLAN_VID: |
| 913 | self.log.debug('subscriber vlan found', |
| 914 | vlan_id=field.vlan_vid) |
| 915 | return field.vlan_vid & 0x0fff |
| 916 | self.log.debug('No subscriber flow found', port=port) |
| 917 | return None |
| 918 | |
| 919 | def add_flow_to_device(self, flow, logical_flow): |
| 920 | self.log.debug('pushing flow to device', flow=flow) |
| 921 | try: |
| 922 | self.stub.FlowAdd(flow) |
| 923 | except grpc.RpcError as grpc_e: |
| 924 | if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS: |
| 925 | self.log.warn('flow already exists', e=grpc_e, flow=flow) |
| 926 | else: |
| 927 | self.log.error('failed to add flow', |
| 928 | logical_flow=logical_flow, flow=flow, |
| 929 | grpc_error=grpc_e) |
| 930 | return False |
| 931 | else: |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 932 | # TODO NEW CORE: Should not need. Core keeps track of logical flows. no need to keep track. verify, especially olt reboot! |
| 933 | # self.register_flow(logical_flow, flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 934 | return True |
| 935 | |
| 936 | def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow): |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 937 | self.log.debug("update-flow-info", intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow=flow) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 938 | self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id, |
| 939 | flow_id, flow) |
| 940 | |
| 941 | def register_flow(self, logical_flow, device_flow): |
| 942 | self.log.debug('registering flow in device', |
| 943 | logical_flow=logical_flow, device_flow=device_flow) |
| 944 | stored_flow = copy.deepcopy(logical_flow) |
| 945 | stored_flow.id = self.generate_stored_id(device_flow.flow_id, |
| 946 | device_flow.flow_type) |
| 947 | self.log.debug('generated device flow id', id=stored_flow.id, |
| 948 | flow_id=device_flow.flow_id, |
| 949 | direction=device_flow.flow_type) |
| 950 | stored_flow.cookie = logical_flow.id |
| 951 | flows = self.flows_proxy.get('/') |
| 952 | flows.items.extend([stored_flow]) |
| 953 | self.flows_proxy.update('/', flows) |
| 954 | |
| 955 | def find_next_flow(self, flow): |
| 956 | table_id = fd.get_goto_table_id(flow) |
| 957 | metadata = 0 |
| 958 | # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In |
| 959 | # 1.13.5 and later, the lower 32-bits is the output port number and the |
| 960 | # upper 32-bits is the inner-vid we are looking for. Use just the lower 32 |
| 961 | # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline |
| 962 | |
| 963 | for field in fd.get_ofb_fields(flow): |
| 964 | if field.type == fd.METADATA: |
| 965 | metadata = field.table_metadata & 0xFFFFFFFF |
| 966 | if table_id is None: |
| 967 | return None |
| 968 | flows = self.logical_flows_proxy.get('/').items |
| 969 | next_flows = [] |
| 970 | for f in flows: |
| 971 | if f.table_id == table_id: |
| 972 | # FIXME |
| 973 | if fd.get_in_port(f) == fd.get_in_port(flow) and \ |
| 974 | fd.get_out_port(f) == metadata: |
| 975 | next_flows.append(f) |
| 976 | |
| 977 | if len(next_flows) == 0: |
| 978 | self.log.warning('no next flow found, it may be a timing issue', |
| 979 | flow=flow, number_of_flows=len(flows)) |
| 980 | if flow.id in self.retry_add_flow_list: |
| 981 | self.log.debug('flow is already in retry list', flow_id=flow.id) |
| 982 | else: |
| 983 | self.retry_add_flow_list.append(flow.id) |
| 984 | reactor.callLater(5, self.retry_add_flow, flow) |
| 985 | return None |
| 986 | |
| 987 | next_flows.sort(key=lambda f: f.priority, reverse=True) |
| 988 | |
| 989 | return next_flows[0] |
| 990 | |
| 991 | def update_children_flows(self, device_rules_map): |
| 992 | |
| 993 | for device_id, (flows, groups) in device_rules_map.iteritems(): |
| 994 | if device_id != self.device_id: |
| 995 | self.root_proxy.update('/devices/{}/flows'.format(device_id), |
| 996 | Flows(items=flows.values())) |
| 997 | self.root_proxy.update('/devices/{}/flow_groups'.format( |
| 998 | device_id), FlowGroups(items=groups.values())) |
| 999 | |
| 1000 | def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port): |
| 1001 | ofp_port_name = logical_port.ofp_port.name |
| 1002 | port_no = logical_port.ofp_port.port_no |
| 1003 | pon_port = child_device.proxy_address.channel_id |
| 1004 | onu_id = child_device.proxy_address.onu_id |
| 1005 | uni_id = self.platform.uni_id_from_port_num(logical_port) |
| 1006 | |
| 1007 | # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do, |
| 1008 | # is probably to maintain a list of Tech-profile table IDs associated |
| 1009 | # with the UNI logical_port. This way, when the logical port is deleted, |
| 1010 | # all the associated tech-profile configuration with the UNI logical_port |
| 1011 | # can be cleared. |
| 1012 | tech_profile_instance = self.tech_profile[pon_port]. \ |
| 1013 | get_tech_profile_instance( |
| 1014 | DEFAULT_TECH_PROFILE_TABLE_ID, |
| 1015 | ofp_port_name) |
| 1016 | flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id) |
| 1017 | self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids) |
| 1018 | for flow_id in flow_ids: |
| 1019 | flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id) |
| 1020 | for flow_info in flow_infos: |
| 1021 | direction = flow_info['flow_type'] |
| 1022 | flow_to_remove = openolt_pb2.Flow(flow_id=flow_id, |
| 1023 | flow_type=direction) |
| 1024 | try: |
| 1025 | self.stub.FlowRemove(flow_to_remove) |
| 1026 | except grpc.RpcError as grpc_e: |
| 1027 | if grpc_e.code() == grpc.StatusCode.NOT_FOUND: |
| 1028 | self.log.debug('This flow does not exist on the switch, ' |
| 1029 | 'normal after an OLT reboot', |
| 1030 | flow=flow_to_remove) |
| 1031 | else: |
| 1032 | raise grpc_e |
| 1033 | |
| 1034 | self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id) |
| 1035 | |
| 1036 | try: |
| 1037 | tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance) |
| 1038 | self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port, |
| 1039 | onu_id=onu_id, |
| 1040 | uni_id=uni_id, |
| 1041 | port_no=port_no, |
| 1042 | tconts=tconts)) |
| 1043 | except grpc.RpcError as grpc_e: |
| 1044 | self.log.error('error-removing-tcont-scheduler-queues', |
| 1045 | err=grpc_e) |
| 1046 | |
| 1047 | def generate_stored_id(self, flow_id, direction): |
| 1048 | if direction == UPSTREAM: |
| 1049 | self.log.debug('upstream flow, shifting id') |
| 1050 | return 0x1 << 15 | flow_id |
| 1051 | elif direction == DOWNSTREAM: |
| 1052 | self.log.debug('downstream flow, not shifting id') |
| 1053 | return flow_id |
| 1054 | else: |
| 1055 | self.log.warn('Unrecognized direction', direction=direction) |
| 1056 | return flow_id |
| 1057 | |
| 1058 | def decode_stored_id(self, id): |
| 1059 | if id >> 15 == 0x1: |
| 1060 | return id & 0x7fff, UPSTREAM |
| 1061 | else: |
| 1062 | return id, DOWNSTREAM |
| 1063 | |
| 1064 | def _populate_tech_profile_per_pon_port(self): |
| 1065 | for arange in self.resource_mgr.device_info.ranges: |
| 1066 | for intf_id in arange.intf_ids: |
| 1067 | self.tech_profile[intf_id] = \ |
| 1068 | self.resource_mgr.resource_mgrs[intf_id].tech_profile |
| 1069 | |
| 1070 | # Make sure we have as many tech_profiles as there are pon ports on |
| 1071 | # the device |
| 1072 | assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports |
| 1073 | |
| 1074 | def _get_flow_info_as_json_blob(self, flow, flow_store_cookie, |
| 1075 | flow_category=None): |
| 1076 | json_blob = MessageToDict(message=flow, |
| 1077 | preserving_proto_field_name=True) |
| 1078 | self.log.debug("flow-info", json_blob=json_blob) |
| 1079 | json_blob['flow_store_cookie'] = flow_store_cookie |
| 1080 | if flow_category is not None: |
| 1081 | json_blob['flow_category'] = flow_category |
| 1082 | flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id, |
| 1083 | flow.onu_id, flow.uni_id, flow.flow_id) |
| 1084 | |
| 1085 | if flow_info is None: |
| 1086 | flow_info = list() |
| 1087 | flow_info.append(json_blob) |
| 1088 | else: |
| 1089 | assert (isinstance(flow_info, list)) |
| 1090 | flow_info.append(json_blob) |
| 1091 | |
| 1092 | return flow_info |
| 1093 | |
| 1094 | @staticmethod |
| 1095 | def _get_flow_store_cookie(classifier, gem_port=None): |
| 1096 | assert isinstance(classifier, dict) |
| 1097 | # We need unique flows per gem_port |
| 1098 | if gem_port is not None: |
| 1099 | to_hash = dumps(classifier, sort_keys=True) + str(gem_port) |
| 1100 | else: |
| 1101 | to_hash = dumps(classifier, sort_keys=True) |
| 1102 | return hashlib.md5(to_hash).hexdigest()[:12] |
| 1103 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 1104 | @inlineCallbacks |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1105 | def get_nni_intf_id(self): |
| 1106 | if self.nni_intf_id is not None: |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 1107 | returnValue(self.nni_intf_id) |
William Kurkian | 6f436d0 | 2019-02-06 16:25:01 -0500 | [diff] [blame] | 1108 | |
Matt Jeanneret | 9dbce8c | 2019-03-23 14:35:00 -0400 | [diff] [blame] | 1109 | port_list = yield self.core_proxy.get_ports(self.device_id, Port.ETHERNET_NNI) |
| 1110 | self.log.debug("nni-ports-list", port_list=port_list) |
| 1111 | |
| 1112 | # TODO: Hardcoded only first NNI |
| 1113 | port = port_list.items[0] |
| 1114 | |
| 1115 | self.log.debug("nni-port", port=port) |
| 1116 | self.nni_intf_id = self.platform.intf_id_from_nni_port_num(port.port_no) |
| 1117 | |
| 1118 | self.log.debug("nni-intf-d ", port=port.port_no, nni_intf_id=self.nni_intf_id) |
| 1119 | returnValue(self.nni_intf_id) |