blob: 410c86a675c56dd5365df5ed0ad5c65cb1840248 [file] [log] [blame]
Shad Ansari2dda4f32018-05-17 07:16:07 +00001#
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#
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040016import copy
17from twisted.internet import reactor
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -040018import grpc
Girish Gowdruab836e92018-10-25 01:17:57 -070019from google.protobuf.json_format import MessageToDict
Girish Gowdrub761bc12018-11-29 02:22:18 -080020import hashlib
21from simplejson import dumps
Shad Ansari2dda4f32018-05-17 07:16:07 +000022
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040023from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
Jonathan Hart5b435642018-08-20 08:50:05 -070024 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
25 OFPXMT_OFB_VLAN_VID
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040026from voltha.protos.device_pb2 import Port
Shad Ansari2dda4f32018-05-17 07:16:07 +000027import voltha.core.flow_decomposer as fd
Shad Ansari2dda4f32018-05-17 07:16:07 +000028from voltha.adapters.openolt.protos import openolt_pb2
Nicolas Palpacuer61815162018-06-20 18:12:04 -040029from voltha.registry import registry
Shad Ansari2dda4f32018-05-17 07:16:07 +000030
Girish Gowdruab836e92018-10-25 01:17:57 -070031from common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID
32
33# Flow categories
34HSIA_FLOW = "HSIA_FLOW"
Nicolas Palpacuer61815162018-06-20 18:12:04 -040035
36EAP_ETH_TYPE = 0x888e
Jonathan Hart5b435642018-08-20 08:50:05 -070037LLDP_ETH_TYPE = 0x88cc
Shad Ansari2dda4f32018-05-17 07:16:07 +000038
Girish Gowdruab836e92018-10-25 01:17:57 -070039IGMP_PROTO = 2
40
Shad Ansari2dda4f32018-05-17 07:16:07 +000041# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
42DEFAULT_MGMT_VLAN = 4091
Thiyagarajan Subramani3b62d6c2019-02-15 08:48:01 -080043RESERVED_VLAN = 4095
Shad Ansari2dda4f32018-05-17 07:16:07 +000044
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040045# Openolt Flow
Girish Gowdruab836e92018-10-25 01:17:57 -070046UPSTREAM = "upstream"
47DOWNSTREAM = "downstream"
48PACKET_TAG_TYPE = "pkt_tag_type"
49UNTAGGED = "untagged"
50SINGLE_TAG = "single_tag"
51DOUBLE_TAG = "double_tag"
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040052
53# Classifier
54ETH_TYPE = 'eth_type'
55TPID = 'tpid'
56IP_PROTO = 'ip_proto'
57IN_PORT = 'in_port'
58VLAN_VID = 'vlan_vid'
59VLAN_PCP = 'vlan_pcp'
60UDP_DST = 'udp_dst'
61UDP_SRC = 'udp_src'
62IPV4_DST = 'ipv4_dst'
63IPV4_SRC = 'ipv4_src'
64METADATA = 'metadata'
65OUTPUT = 'output'
66# Action
67POP_VLAN = 'pop_vlan'
68PUSH_VLAN = 'push_vlan'
69TRAP_TO_HOST = 'trap_to_host'
70
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040071
Shad Ansari2dda4f32018-05-17 07:16:07 +000072class OpenOltFlowMgr(object):
73
Girish Gowdruab836e92018-10-25 01:17:57 -070074 def __init__(self, adapter_agent, log, stub, device_id, logical_device_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -070075 platform, resource_mgr):
Girish Gowdruab836e92018-10-25 01:17:57 -070076 self.adapter_agent = adapter_agent
Shad Ansari2dda4f32018-05-17 07:16:07 +000077 self.log = log
78 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040079 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040080 self.logical_device_id = logical_device_id
Girish Gowdrube328a02019-01-26 21:44:40 -080081 self.nni_intf_id = None
Shad Ansaricd20a6d2018-10-02 14:36:33 +000082 self.platform = platform
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040083 self.logical_flows_proxy = registry('core').get_proxy(
84 '/logical_devices/{}/flows'.format(self.logical_device_id))
85 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040086 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040087 self.root_proxy = registry('core').get_proxy('/')
Girish Gowdru1e77ea02018-09-24 09:10:35 -070088 self.resource_mgr = resource_mgr
Girish Gowdruab836e92018-10-25 01:17:57 -070089 self.tech_profile = dict()
90 self._populate_tech_profile_per_pon_port()
Scott Bakerbe5a9ea2018-11-19 19:24:21 -080091 self.retry_add_flow_list = []
Shad Ansari2dda4f32018-05-17 07:16:07 +000092
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040093 def add_flow(self, flow):
94 self.log.debug('add flow', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +000095 classifier_info = dict()
96 action_info = dict()
97
Shad Ansari2dda4f32018-05-17 07:16:07 +000098 for field in fd.get_ofb_fields(flow):
99 if field.type == fd.ETH_TYPE:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400100 classifier_info[ETH_TYPE] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +0000101 self.log.debug('field-type-eth-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400102 eth_type=classifier_info[ETH_TYPE])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000103 elif field.type == fd.IP_PROTO:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400104 classifier_info[IP_PROTO] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +0000105 self.log.debug('field-type-ip-proto',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400106 ip_proto=classifier_info[IP_PROTO])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000107 elif field.type == fd.IN_PORT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400108 classifier_info[IN_PORT] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000109 self.log.debug('field-type-in-port',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400110 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000111 elif field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400112 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +0000113 self.log.debug('field-type-vlan-vid',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400114 vlan=classifier_info[VLAN_VID])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000115 elif field.type == fd.VLAN_PCP:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400116 classifier_info[VLAN_PCP] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +0000117 self.log.debug('field-type-vlan-pcp',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400118 pcp=classifier_info[VLAN_PCP])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000119 elif field.type == fd.UDP_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400120 classifier_info[UDP_DST] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000121 self.log.debug('field-type-udp-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400122 udp_dst=classifier_info[UDP_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000123 elif field.type == fd.UDP_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400124 classifier_info[UDP_SRC] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000125 self.log.debug('field-type-udp-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400126 udp_src=classifier_info[UDP_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000127 elif field.type == fd.IPV4_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400128 classifier_info[IPV4_DST] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000129 self.log.debug('field-type-ipv4-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400130 ipv4_dst=classifier_info[IPV4_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000131 elif field.type == fd.IPV4_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400132 classifier_info[IPV4_SRC] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000133 self.log.debug('field-type-ipv4-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400134 ipv4_dst=classifier_info[IPV4_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000135 elif field.type == fd.METADATA:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400136 classifier_info[METADATA] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +0000137 self.log.debug('field-type-metadata',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400138 metadata=classifier_info[METADATA])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000139 else:
140 raise NotImplementedError('field.type={}'.format(
141 field.type))
142
143 for action in fd.get_actions(flow):
144 if action.type == fd.OUTPUT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400145 action_info[OUTPUT] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000146 self.log.debug('action-type-output',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400147 output=action_info[OUTPUT],
148 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000149 elif action.type == fd.POP_VLAN:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400150 if fd.get_goto_table_id(flow) is None:
151 self.log.debug('being taken care of by ONU', flow=flow)
152 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400153 action_info[POP_VLAN] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700154 self.log.debug('action-type-pop-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400155 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000156 elif action.type == fd.PUSH_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400157 action_info[PUSH_VLAN] = True
158 action_info[TPID] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000159 self.log.debug('action-type-push-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400160 push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000161 if action.push.ethertype != 0x8100:
162 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000163 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000164 elif action.type == fd.SET_FIELD:
165 # action_info['action_type'] = 'set_field'
166 _field = action.set_field.field.ofb_field
167 assert (action.set_field.field.oxm_class ==
168 OFPXMC_OPENFLOW_BASIC)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400169 self.log.debug('action-type-set-field',
170 field=_field, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000171 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000172 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000173 vlan_vid=_field.vlan_vid & 0xfff)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400174 action_info[VLAN_VID] = (_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000175 else:
176 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000177 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000178 else:
179 self.log.error('unsupported-action-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400180 action_type=action.type, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000181
Girish Gowdruab836e92018-10-25 01:17:57 -0700182 if fd.get_goto_table_id(flow) is not None and POP_VLAN not in action_info:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400183 self.log.debug('being taken care of by ONU', flow=flow)
Nicolas Palpacuer856d3af2018-09-12 15:04:51 -0400184 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000185
Girish Gowdruab836e92018-10-25 01:17:57 -0700186 if OUTPUT not in action_info and METADATA in classifier_info:
187 # find flow in the next table
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400188 next_flow = self.find_next_flow(flow)
189 if next_flow is None:
190 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400191 action_info[OUTPUT] = fd.get_out_port(next_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400192 for field in fd.get_ofb_fields(next_flow):
193 if field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400194 classifier_info[METADATA] = field.vlan_vid & 0xfff
195
Craig Lutgenabd9c842018-11-15 23:58:27 +0000196 self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT])
197 (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400198 classifier_info[IN_PORT], action_info[OUTPUT])
199
Craig Lutgenabd9c842018-11-15 23:58:27 +0000200 self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400201 action_info, flow)
202
Girish Gowdruab836e92018-10-25 01:17:57 -0700203 def _is_uni_port(self, port_no):
204 try:
205 port = self.adapter_agent.get_logical_port(self.logical_device_id,
206 'uni-{}'.format(port_no))
207 if port is not None:
208 return (not port.root_port), port.device_id
209 else:
210 return False, None
211 except Exception as e:
212 self.log.error("error-retrieving-port", e=e)
213 return False, None
214
215 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
216 uni_port_no = None
Girish Gowdruab836e92018-10-25 01:17:57 -0700217 child_device_id = None
218 if flow_direction == UPSTREAM:
219 for field in fd.get_ofb_fields(flow):
220 if field.type == fd.IN_PORT:
221 is_uni, child_device_id = self._is_uni_port(field.port)
222 if is_uni:
223 uni_port_no = field.port
Girish Gowdruab836e92018-10-25 01:17:57 -0700224 elif flow_direction == DOWNSTREAM:
225 for field in fd.get_ofb_fields(flow):
226 if field.type == fd.METADATA:
227 uni_port = field.table_metadata & 0xFFFFFFFF
228 is_uni, child_device_id = self._is_uni_port(uni_port)
229 if is_uni:
230 uni_port_no = field.port
231
232 if uni_port_no is None:
233 for action in fd.get_actions(flow):
234 if action.type == fd.OUTPUT:
235 is_uni, child_device_id = \
236 self._is_uni_port(action.output.port)
237 if is_uni:
238 uni_port_no = action.output.port
239
Girish Gowdrub761bc12018-11-29 02:22:18 -0800240 if child_device_id:
Girish Gowdruab836e92018-10-25 01:17:57 -0700241 child_device = self.adapter_agent.get_device(child_device_id)
242 pon_intf = child_device.proxy_address.channel_id
243 onu_id = child_device.proxy_address.onu_id
Craig Lutgenabd9c842018-11-15 23:58:27 +0000244 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
245 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700246 assert (isinstance(flows, list))
247 self.log.debug("retrieved-flows", flows=flows)
248 for idx in range(len(flows)):
249 if flow_direction == flows[idx]['flow_type']:
250 flows.pop(idx)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000251 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
Girish Gowdruab836e92018-10-25 01:17:57 -0700252 if len(flows) > 0:
253 # There are still flows referencing the same flow_id.
254 # So the flow should not be freed yet.
255 # For ex: Case of HSIA where same flow is shared
256 # between DS and US.
257 return
258
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800259 self.resource_mgr.free_flow_id(pon_intf, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700260 else:
261 self.log.error("invalid-info", uni_port_no=uni_port_no,
Girish Gowdruab836e92018-10-25 01:17:57 -0700262 child_device_id=child_device_id)
263
Scott Bakerbe5a9ea2018-11-19 19:24:21 -0800264 def retry_add_flow(self, flow):
265 self.log.debug("retry-add-flow")
266 if flow.id in self.retry_add_flow_list:
267 self.retry_add_flow_list.remove(flow.id)
268 self.add_flow(flow)
269
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400270 def remove_flow(self, flow):
271 self.log.debug('trying to remove flows from logical flow :',
Jonathan Hart5b435642018-08-20 08:50:05 -0700272 logical_flow=flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400273 device_flows_to_remove = []
274 device_flows = self.flows_proxy.get('/').items
275 for f in device_flows:
276 if f.cookie == flow.id:
277 device_flows_to_remove.append(f)
278
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400279 for f in device_flows_to_remove:
280 (id, direction) = self.decode_stored_id(f.id)
281 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400282 try:
283 self.stub.FlowRemove(flow_to_remove)
284 except grpc.RpcError as grpc_e:
285 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
286 self.log.debug('This flow does not exist on the switch, '
Jonathan Hart5b435642018-08-20 08:50:05 -0700287 'normal after an OLT reboot',
288 flow=flow_to_remove)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400289 else:
290 raise grpc_e
291
Girish Gowdruab836e92018-10-25 01:17:57 -0700292 # once we have successfully deleted the flow on the device
293 # release the flow_id on resource pool and also clear any
294 # data associated with the flow_id on KV store.
295 self._clear_flow_id_from_rm(f, id, direction)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400296 self.log.debug('flow removed from device', flow=f,
297 flow_key=flow_to_remove)
298
299 if len(device_flows_to_remove) > 0:
300 new_flows = []
301 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
302 for f in device_flows:
303 if f.id not in flows_ids_to_remove:
304 new_flows.append(f)
305
306 self.flows_proxy.update('/', Flows(items=new_flows))
307 self.log.debug('flows removed from the data store',
308 flow_ids_removed=flows_ids_to_remove,
309 number_of_flows_removed=(len(device_flows) - len(
310 new_flows)), expected_flows_removed=len(
Girish Gowdruab836e92018-10-25 01:17:57 -0700311 device_flows_to_remove))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400312 else:
313 self.log.debug('no device flow to remove for this flow (normal '
314 'for multi table flows)', flow=flow)
315
Craig Lutgenabd9c842018-11-15 23:58:27 +0000316 def _get_ofp_port_name(self, intf_id, onu_id, uni_id):
Girish Gowdruab836e92018-10-25 01:17:57 -0700317 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
318 child_device = self.adapter_agent.get_child_device(self.device_id,
319 parent_port_no=parent_port_no, onu_id=onu_id)
320 if child_device is None:
321 self.log.error("could-not-find-child-device",
322 parent_port_no=intf_id, onu_id=onu_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000323 return (None, None)
Girish Gowdruab836e92018-10-25 01:17:57 -0700324 ports = self.adapter_agent.get_ports(child_device.id, Port.ETHERNET_UNI)
325 logical_port = self.adapter_agent.get_logical_port(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000326 self.logical_device_id, ports[uni_id].label)
327 ofp_port_name = (logical_port.ofp_port.name, logical_port.ofp_port.port_no)
Girish Gowdruab836e92018-10-25 01:17:57 -0700328 return ofp_port_name
329
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800330 def get_tp_path(self, intf_id, ofp_port_name, techprofile_id):
Girish Gowdrub761bc12018-11-29 02:22:18 -0800331 return self.tech_profile[intf_id]. \
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800332 get_tp_path(techprofile_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -0800333 ofp_port_name)
334
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800335 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id, ofp_port_name):
Girish Gowdrub761bc12018-11-29 02:22:18 -0800336 # Remove the TP instance associated with the ONU
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800337 if ofp_port_name is None:
338 ofp_port_name, ofp_port_no = self._get_ofp_port_name(intf_id, onu_id, uni_id)
339 tp_id = self.resource_mgr.get_tech_profile_id_for_onu(intf_id, onu_id, uni_id)
340 tp_path = self.get_tp_path(intf_id, ofp_port_name, tp_id)
341 self.log.debug(" tp-path-in-delete",tp_path=tp_path)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800342 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
343
Craig Lutgenabd9c842018-11-15 23:58:27 +0000344 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400345 action, flow):
346
Craig Lutgenabd9c842018-11-15 23:58:27 +0000347 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400348 classifier=classifier, action=action)
349
Craig Lutgenabd9c842018-11-15 23:58:27 +0000350 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700351 flow.table_id)
352 if alloc_id is None or gem_ports is None:
353 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id,
354 gem_ports=gem_ports)
355 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400356
Girish Gowdruab836e92018-10-25 01:17:57 -0700357 self.log.debug('Generated required alloc and gemport ids',
358 alloc_id=alloc_id, gemports=gem_ports)
359
360 # Flows can't be added specific to gemport unless p-bits are received.
361 # Hence adding flows for all gemports
362 for gemport_id in gem_ports:
363 if IP_PROTO in classifier:
364 if classifier[IP_PROTO] == 17:
365 self.log.debug('dhcp flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000366 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700367 action, flow, alloc_id, gemport_id)
368 elif classifier[IP_PROTO] == 2:
369 self.log.warn('igmp flow add ignored, not implemented yet')
370 else:
371 self.log.warn("Invalid-Classifier-to-handle",
372 classifier=classifier,
373 action=action)
374 elif ETH_TYPE in classifier:
375 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
376 self.log.debug('eapol flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000377 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700378 gemport_id)
379 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
380 if vlan_id is not None:
381 self.add_eapol_flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000382 intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700383 vlan_id=vlan_id)
384 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
385 onu_device = self.adapter_agent.get_child_device(self.device_id,
386 onu_id=onu_id,
387 parent_port_no=parent_port_no)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000388 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700389 if ofp_port_name is None:
390 self.log.error("port-name-not-found")
391 return
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800392 tp_id = self.resource_mgr.get_tech_profile_id_for_onu(intf_id, onu_id, uni_id)
393 tp_path = self.get_tp_path(intf_id, ofp_port_name, tp_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700394
395 self.log.debug('Load-tech-profile-request-to-brcm-handler',
396 tp_path=tp_path)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000397 msg = {'proxy_address': onu_device.proxy_address, 'uni_id': uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700398 'event': 'download_tech_profile', 'event_data': tp_path}
399
400 # Send the event message to the ONU adapter
401 self.adapter_agent.publish_inter_adapter_message(onu_device.id,
402 msg)
403
404 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
405 self.log.debug('lldp flow add')
Girish Gowdrube328a02019-01-26 21:44:40 -0800406 nni_intf_id = self.get_nni_intf_id()
407 self.add_lldp_flow(flow, port_no, nni_intf_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700408
409 elif PUSH_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000410 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700411 action, flow, alloc_id, gemport_id)
412 elif POP_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000413 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700414 action, flow, alloc_id, gemport_id)
415 else:
416 self.log.debug('Invalid-flow-type-to-handle',
417 classifier=classifier,
418 action=action, flow=flow)
419
Craig Lutgenabd9c842018-11-15 23:58:27 +0000420 def create_tcont_gemport(self, intf_id, onu_id, uni_id, table_id):
Girish Gowdruab836e92018-10-25 01:17:57 -0700421 alloc_id, gem_port_ids = None, None
Girish Gowdrub761bc12018-11-29 02:22:18 -0800422 pon_intf_onu_id = (intf_id, onu_id)
423
424 # If we already have allocated alloc_id and gem_ports earlier, render them
425 alloc_id = \
426 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
427 gem_port_ids = \
428 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
429 if alloc_id is not None and gem_port_ids is not None:
430 return alloc_id, gem_port_ids
431
Girish Gowdruab836e92018-10-25 01:17:57 -0700432 try:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000433 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700434 if ofp_port_name is None:
435 self.log.error("port-name-not-found")
436 return alloc_id, gem_port_ids
437 # FIXME: If table id is <= 63 using 64 as table id
438 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
439 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
440
441 # Check tech profile instance already exists for derived port name
442 tech_profile_instance = self.tech_profile[intf_id]. \
443 get_tech_profile_instance(table_id, ofp_port_name)
444 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
445
446 if tech_profile_instance is None:
447 # create tech profile instance
448 tech_profile_instance = self.tech_profile[intf_id]. \
449 create_tech_profile_instance(table_id, ofp_port_name,
450 intf_id)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800451 if tech_profile_instance is None:
Girish Gowdruab836e92018-10-25 01:17:57 -0700452 raise Exception('Tech-profile-instance-creation-failed')
453 else:
454 self.log.debug(
455 'Tech-profile-instance-already-exist-for-given port-name',
456 ofp_port_name=ofp_port_name)
457
Girish Gowdrub761bc12018-11-29 02:22:18 -0800458 # upstream scheduler
459 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
460 tech_profile_instance)
461 # downstream scheduler
462 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
463 tech_profile_instance)
464 # create Tcont
465 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
466 us_scheduler,
467 ds_scheduler)
468
469 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
470 onu_id=onu_id,
471 uni_id=uni_id,
472 port_no=ofp_port_no,
473 tconts=tconts))
474
Girish Gowdruab836e92018-10-25 01:17:57 -0700475 # Fetch alloc id and gemports from tech profile instance
476 alloc_id = tech_profile_instance.us_scheduler.alloc_id
477 gem_port_ids = []
478 for i in range(len(
479 tech_profile_instance.upstream_gem_port_attribute_list)):
480 gem_port_ids.append(
481 tech_profile_instance.upstream_gem_port_attribute_list[i].
Girish Gowdrub761bc12018-11-29 02:22:18 -0800482 gemport_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700483 except BaseException as e:
484 self.log.exception(exception=e)
485
Craig Lutgenabd9c842018-11-15 23:58:27 +0000486 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
487 pon_intf_onu_id = (intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700488 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
489 pon_intf_onu_id,
490 list([alloc_id])
491 )
492 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
493 pon_intf_onu_id,
494 gem_port_ids
495 )
496
497 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000498 gem_port_ids, intf_id, onu_id, uni_id
Girish Gowdruab836e92018-10-25 01:17:57 -0700499 )
500
501 return alloc_id, gem_port_ids
Shad Ansari2dda4f32018-05-17 07:16:07 +0000502
Craig Lutgenabd9c842018-11-15 23:58:27 +0000503 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700504 uplink_action, logical_flow, alloc_id,
505 gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400506
507 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
508
Craig Lutgenabd9c842018-11-15 23:58:27 +0000509 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700510 uplink_action, UPSTREAM,
511 logical_flow, alloc_id, gemport_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000512
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400513 # Secondary EAP on the subscriber vlan
Craig Lutgenabd9c842018-11-15 23:58:27 +0000514 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400515 if eap_active:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000516 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -0800517 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400518
Craig Lutgenabd9c842018-11-15 23:58:27 +0000519 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700520 downlink_action, flow, alloc_id, gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400521 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400522 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400523 downlink_action[POP_VLAN] = True
524 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400525
Craig Lutgenabd9c842018-11-15 23:58:27 +0000526 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700527 downlink_action, DOWNSTREAM,
528 flow, alloc_id, gemport_id)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400529
Craig Lutgenabd9c842018-11-15 23:58:27 +0000530 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
Girish Gowdruab836e92018-10-25 01:17:57 -0700531 direction, logical_flow, alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000532
Girish Gowdrub761bc12018-11-29 02:22:18 -0800533 flow_store_cookie = self._get_flow_store_cookie(classifier,
534 gemport_id)
535
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800536 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
537 flow_store_cookie):
538 self.log.debug('flow-exists--not-re-adding')
539 else:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000540
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800541 # One of the OLT platform (Broadcom BAL) requires that symmetric
542 # flows require the same flow_id to be used across UL and DL.
543 # Since HSIA flow is the only symmetric flow currently, we need to
544 # re-use the flow_id across both direction. The 'flow_category'
545 # takes priority over flow_cookie to find any available HSIA_FLOW
546 # id for the ONU.
547 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
548 flow_store_cookie,
549 HSIA_FLOW)
550 if flow_id is None:
551 self.log.error("hsia-flow-unavailable")
552 return
553
554 flow = openolt_pb2.Flow(
555 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
556 flow_type=direction, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
557 gemport_id=gemport_id,
558 classifier=self.mk_classifier(classifier),
559 action=self.mk_action(action),
560 priority=logical_flow.priority,
561 port_no=port_no,
562 cookie=logical_flow.cookie)
563
564 if self.add_flow_to_device(flow, logical_flow):
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)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000571
Craig Lutgenabd9c842018-11-15 23:58:27 +0000572 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
Girish Gowdruab836e92018-10-25 01:17:57 -0700573 alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000574
Shad Ansari47e0c392018-07-17 23:55:07 +0000575 self.log.debug('add dhcp upstream trap', classifier=classifier,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000576 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000577
578 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400579 action[TRAP_TO_HOST] = True
Girish Gowdruab836e92018-10-25 01:17:57 -0700580 classifier[UDP_SRC] = 68
581 classifier[UDP_DST] = 67
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400582 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
583 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000584
Girish Gowdrub761bc12018-11-29 02:22:18 -0800585 flow_store_cookie = self._get_flow_store_cookie(classifier,
586 gemport_id)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800587 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
588 flow_store_cookie):
589 self.log.debug('flow-exists--not-re-adding')
590 else:
591 flow_id = self.resource_mgr.get_flow_id(
592 intf_id, onu_id, uni_id, flow_store_cookie
593 )
Girish Gowdrub761bc12018-11-29 02:22:18 -0800594
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800595 dhcp_flow = openolt_pb2.Flow(
596 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
597 access_intf_id=intf_id, gemport_id=gemport_id,
598 alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
599 priority=logical_flow.priority,
600 classifier=self.mk_classifier(classifier),
601 action=self.mk_action(action),
602 port_no=port_no,
603 cookie=logical_flow.cookie)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000604
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800605 if self.add_flow_to_device(dhcp_flow, logical_flow):
606 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
607 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
608 dhcp_flow.onu_id,
609 dhcp_flow.uni_id,
610 dhcp_flow.flow_id,
611 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400612
Craig Lutgenabd9c842018-11-15 23:58:27 +0000613 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -0800614 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000615
Girish Gowdruab836e92018-10-25 01:17:57 -0700616 uplink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400617 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
618 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
619 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400620
Girish Gowdruab836e92018-10-25 01:17:57 -0700621 uplink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400622 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000623
Girish Gowdrub761bc12018-11-29 02:22:18 -0800624 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
625 gemport_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400626
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800627 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
628 flow_store_cookie):
629 self.log.debug('flow-exists--not-re-adding')
630 else:
631 # Add Upstream EAPOL Flow.
632 uplink_flow_id = self.resource_mgr.get_flow_id(
633 intf_id, onu_id, uni_id, flow_store_cookie
634 )
Shad Ansari2dda4f32018-05-17 07:16:07 +0000635
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800636 upstream_flow = openolt_pb2.Flow(
637 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id,
638 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
639 gemport_id=gemport_id,
640 classifier=self.mk_classifier(uplink_classifier),
641 action=self.mk_action(uplink_action),
642 priority=logical_flow.priority,
643 port_no=port_no,
644 cookie=logical_flow.cookie)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400645
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800646 logical_flow = copy.deepcopy(logical_flow)
647 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
648 vlan_id | 0x1000)]))
649 logical_flow.match.type = OFPMT_OXM
650
651 if self.add_flow_to_device(upstream_flow, logical_flow):
652 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
653 flow_store_cookie)
654 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
655 upstream_flow.onu_id,
656 upstream_flow.uni_id,
657 upstream_flow.flow_id,
658 flow_info)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000659
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400660 if vlan_id == DEFAULT_MGMT_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400661 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
662 # requirement)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800663 # On one of the platforms (Broadcom BAL), when same DL classifier
664 # vlan was used across multiple ONUs, eapol flow re-adds after
665 # flow delete (cases of onu reboot/disable) fails.
666 # In order to generate unique vlan, a combination of intf_id
667 # onu_id and uni_id is used.
668 # uni_id defaults to 0, so add 1 to it.
669 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
670 # Assert that we do not generate invalid vlans under no condition
671 assert (special_vlan_downstream_flow >= 2, 'invalid-vlan-generated')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000672
Girish Gowdruab836e92018-10-25 01:17:57 -0700673 downlink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400674 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
675 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400676
Girish Gowdruab836e92018-10-25 01:17:57 -0700677 downlink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400678 downlink_action[PUSH_VLAN] = True
679 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400680
Girish Gowdrub761bc12018-11-29 02:22:18 -0800681
682 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
683 gemport_id)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800684 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
685 flow_store_cookie):
686 self.log.debug('flow-exists--not-re-adding')
687 else:
Girish Gowdrub761bc12018-11-29 02:22:18 -0800688
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800689 downlink_flow_id = self.resource_mgr.get_flow_id(
690 intf_id, onu_id, uni_id, flow_store_cookie
691 )
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400692
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800693 downstream_flow = openolt_pb2.Flow(
694 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id,
695 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
696 gemport_id=gemport_id,
697 classifier=self.mk_classifier(downlink_classifier),
698 action=self.mk_action(downlink_action),
699 priority=logical_flow.priority,
700 port_no=port_no,
701 cookie=logical_flow.cookie)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400702
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800703 downstream_logical_flow = ofp_flow_stats(
704 id=logical_flow.id, cookie=logical_flow.cookie,
705 table_id=logical_flow.table_id, priority=logical_flow.priority,
706 flags=logical_flow.flags)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400707
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800708 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
709 fd.in_port(fd.get_out_port(logical_flow)),
710 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
711 downstream_logical_flow.match.type = OFPMT_OXM
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400712
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800713 downstream_logical_flow.instructions.extend(
714 fd.mk_instructions_from_actions([fd.output(
715 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400716
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800717 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
718 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
719 flow_store_cookie)
720 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
721 downstream_flow.onu_id,
722 downstream_flow.uni_id,
723 downstream_flow.flow_id,
724 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400725
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400726 def repush_all_different_flows(self):
727 # Check if the device is supposed to have flows, if so add them
728 # Recover static flows after a reboot
729 logical_flows = self.logical_flows_proxy.get('/').items
730 devices_flows = self.flows_proxy.get('/').items
731 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
732 for logical_flow in logical_flows:
733 try:
734 if logical_flow.id not in logical_flows_ids_provisioned:
735 self.add_flow(logical_flow)
736 except Exception as e:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000737 self.log.exception('Problem reading this flow', e=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400738
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400739 def reset_flows(self):
740 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400741
Shad Ansarifd0111d2018-09-13 21:33:06 +0000742 """ Add a downstream LLDP trap flow on the NNI interface
743 """
Girish Gowdruab836e92018-10-25 01:17:57 -0700744
Craig Lutgenabd9c842018-11-15 23:58:27 +0000745 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
Jonathan Hart5b435642018-08-20 08:50:05 -0700746
Girish Gowdruab836e92018-10-25 01:17:57 -0700747 classifier = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000748 classifier[ETH_TYPE] = LLDP_ETH_TYPE
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400749 classifier[PACKET_TAG_TYPE] = UNTAGGED
Girish Gowdruab836e92018-10-25 01:17:57 -0700750 action = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000751 action[TRAP_TO_HOST] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700752
Girish Gowdruab836e92018-10-25 01:17:57 -0700753 # LLDP flow is installed to trap LLDP packets on the NNI port.
754 # We manage flow_id resource pool on per PON port basis.
755 # Since this situation is tricky, as a hack, we pass the NNI port
756 # index (network_intf_id) as PON port Index for the flow_id resource
757 # pool. Also, there is no ONU Id available for trapping LLDP packets
758 # on NNI port, use onu_id as -1 (invalid)
759 # ****************** CAVEAT *******************
760 # This logic works if the NNI Port Id falls within the same valid
761 # range of PON Port Ids. If this doesn't work for some OLT Vendor
762 # we need to have a re-look at this.
763 # *********************************************
764 onu_id = -1
Craig Lutgenabd9c842018-11-15 23:58:27 +0000765 uni_id = -1
Girish Gowdrub761bc12018-11-29 02:22:18 -0800766 flow_store_cookie = self._get_flow_store_cookie(classifier)
Jonathan Hart5b435642018-08-20 08:50:05 -0700767
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800768 if self.resource_mgr.is_flow_cookie_on_kv_store(network_intf_id, onu_id, uni_id,
769 flow_store_cookie):
770 self.log.debug('flow-exists--not-re-adding')
771 else:
772 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
773 flow_store_cookie)
Jonathan Hart5b435642018-08-20 08:50:05 -0700774
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800775 downstream_flow = openolt_pb2.Flow(
776 access_intf_id=-1, # access_intf_id not required
777 onu_id=onu_id, # onu_id not required
778 uni_id=uni_id, # uni_id not used
779 flow_id=flow_id,
780 flow_type=DOWNSTREAM,
781 network_intf_id=network_intf_id,
782 gemport_id=-1, # gemport_id not required
783 classifier=self.mk_classifier(classifier),
784 action=self.mk_action(action),
785 priority=logical_flow.priority,
786 port_no=port_no,
787 cookie=logical_flow.cookie)
788
789 self.log.debug('add lldp downstream trap', classifier=classifier,
790 action=action, flow=downstream_flow, port_no=port_no)
791 if self.add_flow_to_device(downstream_flow, logical_flow):
792 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
793 flow_store_cookie)
794 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
795 flow_id, flow_info)
Jonathan Hart5b435642018-08-20 08:50:05 -0700796
Shad Ansari2dda4f32018-05-17 07:16:07 +0000797 def mk_classifier(self, classifier_info):
798
799 classifier = openolt_pb2.Classifier()
800
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400801 if ETH_TYPE in classifier_info:
802 classifier.eth_type = classifier_info[ETH_TYPE]
803 if IP_PROTO in classifier_info:
804 classifier.ip_proto = classifier_info[IP_PROTO]
Thiyagarajan Subramani3b62d6c2019-02-15 08:48:01 -0800805 if VLAN_VID in classifier_info and \
806 classifier_info[VLAN_VID] != RESERVED_VLAN:
807 classifier.o_vid = classifier_info[VLAN_VID]
808 if METADATA in classifier_info and \
809 classifier_info[METADATA] != RESERVED_VLAN:
810 classifier.i_vid = classifier_info[METADATA]
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400811 if VLAN_PCP in classifier_info:
812 classifier.o_pbits = classifier_info[VLAN_PCP]
813 if UDP_SRC in classifier_info:
814 classifier.src_port = classifier_info[UDP_SRC]
815 if UDP_DST in classifier_info:
816 classifier.dst_port = classifier_info[UDP_DST]
817 if IPV4_DST in classifier_info:
818 classifier.dst_ip = classifier_info[IPV4_DST]
819 if IPV4_SRC in classifier_info:
820 classifier.src_ip = classifier_info[IPV4_SRC]
821 if PACKET_TAG_TYPE in classifier_info:
822 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
823 classifier.pkt_tag_type = SINGLE_TAG
824 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
825 classifier.pkt_tag_type = DOUBLE_TAG
826 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
827 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000828 else:
829 classifier.pkt_tag_type = 'none'
830
831 return classifier
832
833 def mk_action(self, action_info):
834 action = openolt_pb2.Action()
835
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400836 if POP_VLAN in action_info:
837 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000838 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400839 elif PUSH_VLAN in action_info:
840 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000841 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400842 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000843 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000844 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400845 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000846 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000847 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400848
Craig Lutgenabd9c842018-11-15 23:58:27 +0000849 def is_eap_enabled(self, intf_id, onu_id, uni_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400850 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400851
852 for flow in flows:
853 eap_flow = False
854 eap_intf_id = None
855 eap_onu_id = None
Craig Lutgenabd9c842018-11-15 23:58:27 +0000856 eap_uni_id = None
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400857 for field in fd.get_ofb_fields(flow):
858 if field.type == fd.ETH_TYPE:
859 if field.eth_type == EAP_ETH_TYPE:
860 eap_flow = True
861 if field.type == fd.IN_PORT:
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000862 eap_intf_id = self.platform.intf_id_from_uni_port_num(
Shad Ansari47e0c392018-07-17 23:55:07 +0000863 field.port)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000864 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000865 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400866
867 if eap_flow:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000868 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400869 intf_id=intf_id, eap_intf_id=eap_intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000870 eap_onu_id=eap_onu_id,
871 eap_uni_id=eap_uni_id)
872 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id:
Girish Gowdruab836e92018-10-25 01:17:57 -0700873 return True, flow
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400874
Girish Gowdruab836e92018-10-25 01:17:57 -0700875 return False, None
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400876
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400877 def get_subscriber_vlan(self, port):
878 self.log.debug('looking from subscriber flow for port', port=port)
879
880 flows = self.logical_flows_proxy.get('/').items
881 for flow in flows:
882 in_port = fd.get_in_port(flow)
883 out_port = fd.get_out_port(flow)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800884 if in_port == port and out_port is not None and \
Girish Gowdruab836e92018-10-25 01:17:57 -0700885 self.platform.intf_id_to_port_type_name(out_port) \
Shad Ansarifd0111d2018-09-13 21:33:06 +0000886 == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400887 fields = fd.get_ofb_fields(flow)
888 self.log.debug('subscriber flow found', fields=fields)
889 for field in fields:
890 if field.type == OFPXMT_OFB_VLAN_VID:
891 self.log.debug('subscriber vlan found',
892 vlan_id=field.vlan_vid)
893 return field.vlan_vid & 0x0fff
894 self.log.debug('No subscriber flow found', port=port)
895 return None
896
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400897 def add_flow_to_device(self, flow, logical_flow):
898 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400899 try:
900 self.stub.FlowAdd(flow)
901 except grpc.RpcError as grpc_e:
902 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
903 self.log.warn('flow already exists', e=grpc_e, flow=flow)
904 else:
905 self.log.error('failed to add flow',
906 logical_flow=logical_flow, flow=flow,
907 grpc_error=grpc_e)
Girish Gowdruab836e92018-10-25 01:17:57 -0700908 return False
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400909 else:
910 self.register_flow(logical_flow, flow)
Girish Gowdruab836e92018-10-25 01:17:57 -0700911 return True
912
Craig Lutgenabd9c842018-11-15 23:58:27 +0000913 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800914 self.resource_mgr.update_flow_id_info(intf_id, onu_id, uni_id,
915 flow_id, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400916
917 def register_flow(self, logical_flow, device_flow):
918 self.log.debug('registering flow in device',
919 logical_flow=logical_flow, device_flow=device_flow)
920 stored_flow = copy.deepcopy(logical_flow)
921 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
922 device_flow.flow_type)
923 self.log.debug('generated device flow id', id=stored_flow.id,
924 flow_id=device_flow.flow_id,
925 direction=device_flow.flow_type)
926 stored_flow.cookie = logical_flow.id
927 flows = self.flows_proxy.get('/')
928 flows.items.extend([stored_flow])
929 self.flows_proxy.update('/', flows)
930
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400931 def find_next_flow(self, flow):
932 table_id = fd.get_goto_table_id(flow)
933 metadata = 0
Chip Boling41f795a2018-10-04 15:45:34 -0500934 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
935 # 1.13.5 and later, the lower 32-bits is the output port number and the
936 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
937 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
938
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400939 for field in fd.get_ofb_fields(flow):
940 if field.type == fd.METADATA:
Chip Boling41f795a2018-10-04 15:45:34 -0500941 metadata = field.table_metadata & 0xFFFFFFFF
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400942 if table_id is None:
943 return None
944 flows = self.logical_flows_proxy.get('/').items
945 next_flows = []
946 for f in flows:
947 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700948 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400949 if fd.get_in_port(f) == fd.get_in_port(flow) and \
950 fd.get_out_port(f) == metadata:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400951 next_flows.append(f)
952
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400953 if len(next_flows) == 0:
954 self.log.warning('no next flow found, it may be a timing issue',
955 flow=flow, number_of_flows=len(flows))
Scott Bakerbe5a9ea2018-11-19 19:24:21 -0800956 if flow.id in self.retry_add_flow_list:
957 self.log.debug('flow is already in retry list', flow_id=flow.id)
958 else:
959 self.retry_add_flow_list.append(flow.id)
960 reactor.callLater(5, self.retry_add_flow, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400961 return None
962
Jonathan Hart5b435642018-08-20 08:50:05 -0700963 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400964
965 return next_flows[0]
966
967 def update_children_flows(self, device_rules_map):
968
969 for device_id, (flows, groups) in device_rules_map.iteritems():
970 if device_id != self.device_id:
971 self.root_proxy.update('/devices/{}/flows'.format(device_id),
972 Flows(items=flows.values()))
973 self.root_proxy.update('/devices/{}/flow_groups'.format(
974 device_id), FlowGroups(items=groups.values()))
975
Girish Gowdruab836e92018-10-25 01:17:57 -0700976 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
977 ofp_port_name = logical_port.ofp_port.name
Craig Lutgenabd9c842018-11-15 23:58:27 +0000978 port_no = logical_port.ofp_port.port_no
Girish Gowdruab836e92018-10-25 01:17:57 -0700979 pon_port = child_device.proxy_address.channel_id
980 onu_id = child_device.proxy_address.onu_id
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800981 uni_id = self.platform.uni_id_from_port_num(port_no)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000982
Girish Gowdruab836e92018-10-25 01:17:57 -0700983 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
984 # is probably to maintain a list of Tech-profile table IDs associated
985 # with the UNI logical_port. This way, when the logical port is deleted,
986 # all the associated tech-profile configuration with the UNI logical_port
987 # can be cleared.
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800988 tp_id = self.resource_mgr.get_tech_profile_id_for_onu(pon_port, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700989 tech_profile_instance = self.tech_profile[pon_port]. \
990 get_tech_profile_instance(
Thiyagarajan Subramani353af122019-02-20 23:14:24 -0800991 tp_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700992 ofp_port_name)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800993 flow_ids = self.resource_mgr.get_current_flow_ids(pon_port, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700994 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
995 for flow_id in flow_ids:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000996 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700997 for flow_info in flow_infos:
998 direction = flow_info['flow_type']
999 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
1000 flow_type=direction)
1001 try:
1002 self.stub.FlowRemove(flow_to_remove)
1003 except grpc.RpcError as grpc_e:
1004 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
1005 self.log.debug('This flow does not exist on the switch, '
1006 'normal after an OLT reboot',
1007 flow=flow_to_remove)
1008 else:
1009 raise grpc_e
1010
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001011 self.resource_mgr.free_flow_id(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -07001012
1013 try:
1014 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
1015 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
1016 onu_id=onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +00001017 uni_id=uni_id,
1018 port_no=port_no,
Girish Gowdruab836e92018-10-25 01:17:57 -07001019 tconts=tconts))
1020 except grpc.RpcError as grpc_e:
1021 self.log.error('error-removing-tcont-scheduler-queues',
1022 err=grpc_e)
1023
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001024 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -04001025 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001026 self.log.debug('upstream flow, shifting id')
1027 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -04001028 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001029 self.log.debug('downstream flow, not shifting id')
1030 return flow_id
1031 else:
1032 self.log.warn('Unrecognized direction', direction=direction)
1033 return flow_id
1034
1035 def decode_stored_id(self, id):
1036 if id >> 15 == 0x1:
Girish Gowdruab836e92018-10-25 01:17:57 -07001037 return id & 0x7fff, UPSTREAM
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001038 else:
Girish Gowdruab836e92018-10-25 01:17:57 -07001039 return id, DOWNSTREAM
1040
1041 def _populate_tech_profile_per_pon_port(self):
1042 for arange in self.resource_mgr.device_info.ranges:
1043 for intf_id in arange.intf_ids:
1044 self.tech_profile[intf_id] = \
1045 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1046
1047 # Make sure we have as many tech_profiles as there are pon ports on
1048 # the device
1049 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1050
Girish Gowdrub761bc12018-11-29 02:22:18 -08001051 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1052 flow_category=None):
Girish Gowdruab836e92018-10-25 01:17:57 -07001053 json_blob = MessageToDict(message=flow,
1054 preserving_proto_field_name=True)
1055 self.log.debug("flow-info", json_blob=json_blob)
Girish Gowdrub761bc12018-11-29 02:22:18 -08001056 json_blob['flow_store_cookie'] = flow_store_cookie
1057 if flow_category is not None:
1058 json_blob['flow_category'] = flow_category
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001059
1060 # For flows which trap out of the NNI, the access_intf_id is invalid (set to -1).
1061 # In such cases, we need to refer to the network_intf_id.
1062 if flow.access_intf_id != -1:
1063 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1064 flow.onu_id, flow.uni_id,
1065 flow.flow_id)
1066 else:
1067 # Case of LLDP trap flow from the NNI. We can't use flow.access_intf_id
1068 # in that case, as it is invalid. We use flow.network_intf_id.
1069 flow_info = self.resource_mgr.get_flow_id_info(flow.network_intf_id,
1070 flow.onu_id, flow.uni_id,
1071 flow.flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -07001072
1073 if flow_info is None:
1074 flow_info = list()
1075 flow_info.append(json_blob)
1076 else:
1077 assert (isinstance(flow_info, list))
1078 flow_info.append(json_blob)
1079
1080 return flow_info
Girish Gowdrub761bc12018-11-29 02:22:18 -08001081
1082 @staticmethod
1083 def _get_flow_store_cookie(classifier, gem_port=None):
1084 assert isinstance(classifier, dict)
1085 # We need unique flows per gem_port
1086 if gem_port is not None:
1087 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1088 else:
1089 to_hash = dumps(classifier, sort_keys=True)
1090 return hashlib.md5(to_hash).hexdigest()[:12]
Girish Gowdrube328a02019-01-26 21:44:40 -08001091
1092 def get_nni_intf_id(self):
1093 if self.nni_intf_id is not None:
1094 return self.nni_intf_id
1095
1096 port_list = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI)
1097 logical_port = self.adapter_agent.get_logical_port(self.logical_device_id,
1098 port_list[0].label)
1099 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(logical_port.ofp_port.port_no)
1100 self.log.debug("nni-intf-d ", nni_intf_id=self.nni_intf_id)
1101 return self.nni_intf_id
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001102