blob: 99d387c18d3a7e5e4d7617499c7af00bd8210d8a [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
Girish Gowdrub761bc12018-11-29 02:22:18 -0800330 def get_tp_path(self, intf_id, ofp_port_name):
331 # FIXME Should get Table id form the flow, as of now hardcoded to
332 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
333 # 'tp_path' contains the suffix part of the tech_profile_instance path.
334 # The prefix to the 'tp_path' should be set to \
335 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
336 return self.tech_profile[intf_id]. \
337 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
338 ofp_port_name)
339
340 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id):
341 # Remove the TP instance associated with the ONU
342 ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id)
343 tp_path = self.get_tp_path(intf_id, ofp_port_name)
344 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
345
Craig Lutgenabd9c842018-11-15 23:58:27 +0000346 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400347 action, flow):
348
Craig Lutgenabd9c842018-11-15 23:58:27 +0000349 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 -0400350 classifier=classifier, action=action)
351
Craig Lutgenabd9c842018-11-15 23:58:27 +0000352 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700353 flow.table_id)
354 if alloc_id is None or gem_ports is None:
355 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id,
356 gem_ports=gem_ports)
357 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400358
Girish Gowdruab836e92018-10-25 01:17:57 -0700359 self.log.debug('Generated required alloc and gemport ids',
360 alloc_id=alloc_id, gemports=gem_ports)
361
362 # Flows can't be added specific to gemport unless p-bits are received.
363 # Hence adding flows for all gemports
364 for gemport_id in gem_ports:
365 if IP_PROTO in classifier:
366 if classifier[IP_PROTO] == 17:
367 self.log.debug('dhcp flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000368 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700369 action, flow, alloc_id, gemport_id)
370 elif classifier[IP_PROTO] == 2:
371 self.log.warn('igmp flow add ignored, not implemented yet')
372 else:
373 self.log.warn("Invalid-Classifier-to-handle",
374 classifier=classifier,
375 action=action)
376 elif ETH_TYPE in classifier:
377 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
378 self.log.debug('eapol flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000379 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700380 gemport_id)
381 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
382 if vlan_id is not None:
383 self.add_eapol_flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000384 intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700385 vlan_id=vlan_id)
386 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
387 onu_device = self.adapter_agent.get_child_device(self.device_id,
388 onu_id=onu_id,
389 parent_port_no=parent_port_no)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000390 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700391 if ofp_port_name is None:
392 self.log.error("port-name-not-found")
393 return
394
Girish Gowdrub761bc12018-11-29 02:22:18 -0800395 tp_path = self.get_tp_path(intf_id, ofp_port_name)
Girish Gowdruab836e92018-10-25 01:17:57 -0700396
397 self.log.debug('Load-tech-profile-request-to-brcm-handler',
398 tp_path=tp_path)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000399 msg = {'proxy_address': onu_device.proxy_address, 'uni_id': uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700400 'event': 'download_tech_profile', 'event_data': tp_path}
401
402 # Send the event message to the ONU adapter
403 self.adapter_agent.publish_inter_adapter_message(onu_device.id,
404 msg)
405
406 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
407 self.log.debug('lldp flow add')
Girish Gowdrube328a02019-01-26 21:44:40 -0800408 nni_intf_id = self.get_nni_intf_id()
409 self.add_lldp_flow(flow, port_no, nni_intf_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700410
411 elif PUSH_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000412 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700413 action, flow, alloc_id, gemport_id)
414 elif POP_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000415 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700416 action, flow, alloc_id, gemport_id)
417 else:
418 self.log.debug('Invalid-flow-type-to-handle',
419 classifier=classifier,
420 action=action, flow=flow)
421
Craig Lutgenabd9c842018-11-15 23:58:27 +0000422 def create_tcont_gemport(self, intf_id, onu_id, uni_id, table_id):
Girish Gowdruab836e92018-10-25 01:17:57 -0700423 alloc_id, gem_port_ids = None, None
Girish Gowdrub761bc12018-11-29 02:22:18 -0800424 pon_intf_onu_id = (intf_id, onu_id)
425
426 # If we already have allocated alloc_id and gem_ports earlier, render them
427 alloc_id = \
428 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
429 gem_port_ids = \
430 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
431 if alloc_id is not None and gem_port_ids is not None:
432 return alloc_id, gem_port_ids
433
Girish Gowdruab836e92018-10-25 01:17:57 -0700434 try:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000435 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700436 if ofp_port_name is None:
437 self.log.error("port-name-not-found")
438 return alloc_id, gem_port_ids
439 # FIXME: If table id is <= 63 using 64 as table id
440 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
441 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
442
443 # Check tech profile instance already exists for derived port name
444 tech_profile_instance = self.tech_profile[intf_id]. \
445 get_tech_profile_instance(table_id, ofp_port_name)
446 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
447
448 if tech_profile_instance is None:
449 # create tech profile instance
450 tech_profile_instance = self.tech_profile[intf_id]. \
451 create_tech_profile_instance(table_id, ofp_port_name,
452 intf_id)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800453 if tech_profile_instance is None:
Girish Gowdruab836e92018-10-25 01:17:57 -0700454 raise Exception('Tech-profile-instance-creation-failed')
455 else:
456 self.log.debug(
457 'Tech-profile-instance-already-exist-for-given port-name',
458 ofp_port_name=ofp_port_name)
459
Girish Gowdrub761bc12018-11-29 02:22:18 -0800460 # upstream scheduler
461 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
462 tech_profile_instance)
463 # downstream scheduler
464 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
465 tech_profile_instance)
466 # create Tcont
467 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
468 us_scheduler,
469 ds_scheduler)
470
471 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
472 onu_id=onu_id,
473 uni_id=uni_id,
474 port_no=ofp_port_no,
475 tconts=tconts))
476
Girish Gowdruab836e92018-10-25 01:17:57 -0700477 # Fetch alloc id and gemports from tech profile instance
478 alloc_id = tech_profile_instance.us_scheduler.alloc_id
479 gem_port_ids = []
480 for i in range(len(
481 tech_profile_instance.upstream_gem_port_attribute_list)):
482 gem_port_ids.append(
483 tech_profile_instance.upstream_gem_port_attribute_list[i].
Girish Gowdrub761bc12018-11-29 02:22:18 -0800484 gemport_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700485 except BaseException as e:
486 self.log.exception(exception=e)
487
Craig Lutgenabd9c842018-11-15 23:58:27 +0000488 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
489 pon_intf_onu_id = (intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700490 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
491 pon_intf_onu_id,
492 list([alloc_id])
493 )
494 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
495 pon_intf_onu_id,
496 gem_port_ids
497 )
498
499 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000500 gem_port_ids, intf_id, onu_id, uni_id
Girish Gowdruab836e92018-10-25 01:17:57 -0700501 )
502
503 return alloc_id, gem_port_ids
Shad Ansari2dda4f32018-05-17 07:16:07 +0000504
Craig Lutgenabd9c842018-11-15 23:58:27 +0000505 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700506 uplink_action, logical_flow, alloc_id,
507 gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400508
509 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
510
Craig Lutgenabd9c842018-11-15 23:58:27 +0000511 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700512 uplink_action, UPSTREAM,
513 logical_flow, alloc_id, gemport_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000514
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400515 # Secondary EAP on the subscriber vlan
Craig Lutgenabd9c842018-11-15 23:58:27 +0000516 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400517 if eap_active:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000518 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -0800519 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400520
Craig Lutgenabd9c842018-11-15 23:58:27 +0000521 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700522 downlink_action, flow, alloc_id, gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400523 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400524 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400525 downlink_action[POP_VLAN] = True
526 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400527
Craig Lutgenabd9c842018-11-15 23:58:27 +0000528 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700529 downlink_action, DOWNSTREAM,
530 flow, alloc_id, gemport_id)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400531
Craig Lutgenabd9c842018-11-15 23:58:27 +0000532 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
Girish Gowdruab836e92018-10-25 01:17:57 -0700533 direction, logical_flow, alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000534
Girish Gowdrub761bc12018-11-29 02:22:18 -0800535 flow_store_cookie = self._get_flow_store_cookie(classifier,
536 gemport_id)
537
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800538 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
539 flow_store_cookie):
540 self.log.debug('flow-exists--not-re-adding')
541 else:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000542
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800543 # One of the OLT platform (Broadcom BAL) requires that symmetric
544 # flows require the same flow_id to be used across UL and DL.
545 # Since HSIA flow is the only symmetric flow currently, we need to
546 # re-use the flow_id across both direction. The 'flow_category'
547 # takes priority over flow_cookie to find any available HSIA_FLOW
548 # id for the ONU.
549 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
550 flow_store_cookie,
551 HSIA_FLOW)
552 if flow_id is None:
553 self.log.error("hsia-flow-unavailable")
554 return
555
556 flow = openolt_pb2.Flow(
557 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
558 flow_type=direction, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
559 gemport_id=gemport_id,
560 classifier=self.mk_classifier(classifier),
561 action=self.mk_action(action),
562 priority=logical_flow.priority,
563 port_no=port_no,
564 cookie=logical_flow.cookie)
565
566 if self.add_flow_to_device(flow, logical_flow):
567 flow_info = self._get_flow_info_as_json_blob(flow,
568 flow_store_cookie,
569 HSIA_FLOW)
570 self.update_flow_info_to_kv_store(flow.access_intf_id,
571 flow.onu_id, flow.uni_id,
572 flow.flow_id, flow_info)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000573
Craig Lutgenabd9c842018-11-15 23:58:27 +0000574 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
Girish Gowdruab836e92018-10-25 01:17:57 -0700575 alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000576
Shad Ansari47e0c392018-07-17 23:55:07 +0000577 self.log.debug('add dhcp upstream trap', classifier=classifier,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000578 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000579
580 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400581 action[TRAP_TO_HOST] = True
Girish Gowdruab836e92018-10-25 01:17:57 -0700582 classifier[UDP_SRC] = 68
583 classifier[UDP_DST] = 67
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400584 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
585 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000586
Girish Gowdrub761bc12018-11-29 02:22:18 -0800587 flow_store_cookie = self._get_flow_store_cookie(classifier,
588 gemport_id)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800589 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
590 flow_store_cookie):
591 self.log.debug('flow-exists--not-re-adding')
592 else:
593 flow_id = self.resource_mgr.get_flow_id(
594 intf_id, onu_id, uni_id, flow_store_cookie
595 )
Girish Gowdrub761bc12018-11-29 02:22:18 -0800596
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800597 dhcp_flow = openolt_pb2.Flow(
598 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
599 access_intf_id=intf_id, gemport_id=gemport_id,
600 alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
601 priority=logical_flow.priority,
602 classifier=self.mk_classifier(classifier),
603 action=self.mk_action(action),
604 port_no=port_no,
605 cookie=logical_flow.cookie)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000606
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800607 if self.add_flow_to_device(dhcp_flow, logical_flow):
608 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
609 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
610 dhcp_flow.onu_id,
611 dhcp_flow.uni_id,
612 dhcp_flow.flow_id,
613 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400614
Craig Lutgenabd9c842018-11-15 23:58:27 +0000615 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
Girish Gowdrub761bc12018-11-29 02:22:18 -0800616 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000617
Girish Gowdruab836e92018-10-25 01:17:57 -0700618 uplink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400619 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
620 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
621 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400622
Girish Gowdruab836e92018-10-25 01:17:57 -0700623 uplink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400624 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000625
Girish Gowdrub761bc12018-11-29 02:22:18 -0800626 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
627 gemport_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400628
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800629 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
630 flow_store_cookie):
631 self.log.debug('flow-exists--not-re-adding')
632 else:
633 # Add Upstream EAPOL Flow.
634 uplink_flow_id = self.resource_mgr.get_flow_id(
635 intf_id, onu_id, uni_id, flow_store_cookie
636 )
Shad Ansari2dda4f32018-05-17 07:16:07 +0000637
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800638 upstream_flow = openolt_pb2.Flow(
639 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id,
640 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
641 gemport_id=gemport_id,
642 classifier=self.mk_classifier(uplink_classifier),
643 action=self.mk_action(uplink_action),
644 priority=logical_flow.priority,
645 port_no=port_no,
646 cookie=logical_flow.cookie)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400647
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800648 logical_flow = copy.deepcopy(logical_flow)
649 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
650 vlan_id | 0x1000)]))
651 logical_flow.match.type = OFPMT_OXM
652
653 if self.add_flow_to_device(upstream_flow, logical_flow):
654 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
655 flow_store_cookie)
656 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
657 upstream_flow.onu_id,
658 upstream_flow.uni_id,
659 upstream_flow.flow_id,
660 flow_info)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000661
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400662 if vlan_id == DEFAULT_MGMT_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400663 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
664 # requirement)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800665 # On one of the platforms (Broadcom BAL), when same DL classifier
666 # vlan was used across multiple ONUs, eapol flow re-adds after
667 # flow delete (cases of onu reboot/disable) fails.
668 # In order to generate unique vlan, a combination of intf_id
669 # onu_id and uni_id is used.
670 # uni_id defaults to 0, so add 1 to it.
671 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
672 # Assert that we do not generate invalid vlans under no condition
673 assert (special_vlan_downstream_flow >= 2, 'invalid-vlan-generated')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000674
Girish Gowdruab836e92018-10-25 01:17:57 -0700675 downlink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400676 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
677 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400678
Girish Gowdruab836e92018-10-25 01:17:57 -0700679 downlink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400680 downlink_action[PUSH_VLAN] = True
681 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400682
Girish Gowdrub761bc12018-11-29 02:22:18 -0800683
684 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
685 gemport_id)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800686 if self.resource_mgr.is_flow_cookie_on_kv_store(intf_id, onu_id, uni_id,
687 flow_store_cookie):
688 self.log.debug('flow-exists--not-re-adding')
689 else:
Girish Gowdrub761bc12018-11-29 02:22:18 -0800690
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800691 downlink_flow_id = self.resource_mgr.get_flow_id(
692 intf_id, onu_id, uni_id, flow_store_cookie
693 )
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400694
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800695 downstream_flow = openolt_pb2.Flow(
696 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id,
697 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
698 gemport_id=gemport_id,
699 classifier=self.mk_classifier(downlink_classifier),
700 action=self.mk_action(downlink_action),
701 priority=logical_flow.priority,
702 port_no=port_no,
703 cookie=logical_flow.cookie)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400704
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800705 downstream_logical_flow = ofp_flow_stats(
706 id=logical_flow.id, cookie=logical_flow.cookie,
707 table_id=logical_flow.table_id, priority=logical_flow.priority,
708 flags=logical_flow.flags)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400709
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800710 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
711 fd.in_port(fd.get_out_port(logical_flow)),
712 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
713 downstream_logical_flow.match.type = OFPMT_OXM
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400714
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800715 downstream_logical_flow.instructions.extend(
716 fd.mk_instructions_from_actions([fd.output(
717 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400718
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800719 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
720 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
721 flow_store_cookie)
722 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
723 downstream_flow.onu_id,
724 downstream_flow.uni_id,
725 downstream_flow.flow_id,
726 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400727
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400728 def repush_all_different_flows(self):
729 # Check if the device is supposed to have flows, if so add them
730 # Recover static flows after a reboot
731 logical_flows = self.logical_flows_proxy.get('/').items
732 devices_flows = self.flows_proxy.get('/').items
733 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
734 for logical_flow in logical_flows:
735 try:
736 if logical_flow.id not in logical_flows_ids_provisioned:
737 self.add_flow(logical_flow)
738 except Exception as e:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000739 self.log.exception('Problem reading this flow', e=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400740
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400741 def reset_flows(self):
742 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400743
Shad Ansarifd0111d2018-09-13 21:33:06 +0000744 """ Add a downstream LLDP trap flow on the NNI interface
745 """
Girish Gowdruab836e92018-10-25 01:17:57 -0700746
Craig Lutgenabd9c842018-11-15 23:58:27 +0000747 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
Jonathan Hart5b435642018-08-20 08:50:05 -0700748
Girish Gowdruab836e92018-10-25 01:17:57 -0700749 classifier = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000750 classifier[ETH_TYPE] = LLDP_ETH_TYPE
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400751 classifier[PACKET_TAG_TYPE] = UNTAGGED
Girish Gowdruab836e92018-10-25 01:17:57 -0700752 action = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000753 action[TRAP_TO_HOST] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700754
Girish Gowdruab836e92018-10-25 01:17:57 -0700755 # LLDP flow is installed to trap LLDP packets on the NNI port.
756 # We manage flow_id resource pool on per PON port basis.
757 # Since this situation is tricky, as a hack, we pass the NNI port
758 # index (network_intf_id) as PON port Index for the flow_id resource
759 # pool. Also, there is no ONU Id available for trapping LLDP packets
760 # on NNI port, use onu_id as -1 (invalid)
761 # ****************** CAVEAT *******************
762 # This logic works if the NNI Port Id falls within the same valid
763 # range of PON Port Ids. If this doesn't work for some OLT Vendor
764 # we need to have a re-look at this.
765 # *********************************************
766 onu_id = -1
Craig Lutgenabd9c842018-11-15 23:58:27 +0000767 uni_id = -1
Girish Gowdrub761bc12018-11-29 02:22:18 -0800768 flow_store_cookie = self._get_flow_store_cookie(classifier)
Jonathan Hart5b435642018-08-20 08:50:05 -0700769
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800770 if self.resource_mgr.is_flow_cookie_on_kv_store(network_intf_id, onu_id, uni_id,
771 flow_store_cookie):
772 self.log.debug('flow-exists--not-re-adding')
773 else:
774 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
775 flow_store_cookie)
Jonathan Hart5b435642018-08-20 08:50:05 -0700776
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800777 downstream_flow = openolt_pb2.Flow(
778 access_intf_id=-1, # access_intf_id not required
779 onu_id=onu_id, # onu_id not required
780 uni_id=uni_id, # uni_id not used
781 flow_id=flow_id,
782 flow_type=DOWNSTREAM,
783 network_intf_id=network_intf_id,
784 gemport_id=-1, # gemport_id not required
785 classifier=self.mk_classifier(classifier),
786 action=self.mk_action(action),
787 priority=logical_flow.priority,
788 port_no=port_no,
789 cookie=logical_flow.cookie)
790
791 self.log.debug('add lldp downstream trap', classifier=classifier,
792 action=action, flow=downstream_flow, port_no=port_no)
793 if self.add_flow_to_device(downstream_flow, logical_flow):
794 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
795 flow_store_cookie)
796 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
797 flow_id, flow_info)
Jonathan Hart5b435642018-08-20 08:50:05 -0700798
Shad Ansari2dda4f32018-05-17 07:16:07 +0000799 def mk_classifier(self, classifier_info):
800
801 classifier = openolt_pb2.Classifier()
802
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400803 if ETH_TYPE in classifier_info:
804 classifier.eth_type = classifier_info[ETH_TYPE]
805 if IP_PROTO in classifier_info:
806 classifier.ip_proto = classifier_info[IP_PROTO]
Thiyagarajan Subramani3b62d6c2019-02-15 08:48:01 -0800807 if VLAN_VID in classifier_info and \
808 classifier_info[VLAN_VID] != RESERVED_VLAN:
809 classifier.o_vid = classifier_info[VLAN_VID]
810 if METADATA in classifier_info and \
811 classifier_info[METADATA] != RESERVED_VLAN:
812 classifier.i_vid = classifier_info[METADATA]
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400813 if VLAN_PCP in classifier_info:
814 classifier.o_pbits = classifier_info[VLAN_PCP]
815 if UDP_SRC in classifier_info:
816 classifier.src_port = classifier_info[UDP_SRC]
817 if UDP_DST in classifier_info:
818 classifier.dst_port = classifier_info[UDP_DST]
819 if IPV4_DST in classifier_info:
820 classifier.dst_ip = classifier_info[IPV4_DST]
821 if IPV4_SRC in classifier_info:
822 classifier.src_ip = classifier_info[IPV4_SRC]
823 if PACKET_TAG_TYPE in classifier_info:
824 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
825 classifier.pkt_tag_type = SINGLE_TAG
826 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
827 classifier.pkt_tag_type = DOUBLE_TAG
828 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
829 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000830 else:
831 classifier.pkt_tag_type = 'none'
832
833 return classifier
834
835 def mk_action(self, action_info):
836 action = openolt_pb2.Action()
837
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400838 if POP_VLAN in action_info:
839 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000840 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400841 elif PUSH_VLAN in action_info:
842 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000843 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400844 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000845 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000846 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400847 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000848 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000849 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400850
Craig Lutgenabd9c842018-11-15 23:58:27 +0000851 def is_eap_enabled(self, intf_id, onu_id, uni_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400852 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400853
854 for flow in flows:
855 eap_flow = False
856 eap_intf_id = None
857 eap_onu_id = None
Craig Lutgenabd9c842018-11-15 23:58:27 +0000858 eap_uni_id = None
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400859 for field in fd.get_ofb_fields(flow):
860 if field.type == fd.ETH_TYPE:
861 if field.eth_type == EAP_ETH_TYPE:
862 eap_flow = True
863 if field.type == fd.IN_PORT:
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000864 eap_intf_id = self.platform.intf_id_from_uni_port_num(
Shad Ansari47e0c392018-07-17 23:55:07 +0000865 field.port)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000866 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000867 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400868
869 if eap_flow:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000870 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400871 intf_id=intf_id, eap_intf_id=eap_intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000872 eap_onu_id=eap_onu_id,
873 eap_uni_id=eap_uni_id)
874 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 -0700875 return True, flow
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400876
Girish Gowdruab836e92018-10-25 01:17:57 -0700877 return False, None
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400878
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400879 def get_subscriber_vlan(self, port):
880 self.log.debug('looking from subscriber flow for port', port=port)
881
882 flows = self.logical_flows_proxy.get('/').items
883 for flow in flows:
884 in_port = fd.get_in_port(flow)
885 out_port = fd.get_out_port(flow)
Girish Gowdrub761bc12018-11-29 02:22:18 -0800886 if in_port == port and out_port is not None and \
Girish Gowdruab836e92018-10-25 01:17:57 -0700887 self.platform.intf_id_to_port_type_name(out_port) \
Shad Ansarifd0111d2018-09-13 21:33:06 +0000888 == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400889 fields = fd.get_ofb_fields(flow)
890 self.log.debug('subscriber flow found', fields=fields)
891 for field in fields:
892 if field.type == OFPXMT_OFB_VLAN_VID:
893 self.log.debug('subscriber vlan found',
894 vlan_id=field.vlan_vid)
895 return field.vlan_vid & 0x0fff
896 self.log.debug('No subscriber flow found', port=port)
897 return None
898
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400899 def add_flow_to_device(self, flow, logical_flow):
900 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400901 try:
902 self.stub.FlowAdd(flow)
903 except grpc.RpcError as grpc_e:
904 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
905 self.log.warn('flow already exists', e=grpc_e, flow=flow)
906 else:
907 self.log.error('failed to add flow',
908 logical_flow=logical_flow, flow=flow,
909 grpc_error=grpc_e)
Girish Gowdruab836e92018-10-25 01:17:57 -0700910 return False
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400911 else:
912 self.register_flow(logical_flow, flow)
Girish Gowdruab836e92018-10-25 01:17:57 -0700913 return True
914
Craig Lutgenabd9c842018-11-15 23:58:27 +0000915 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800916 self.resource_mgr.update_flow_id_info(intf_id, onu_id, uni_id,
917 flow_id, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400918
919 def register_flow(self, logical_flow, device_flow):
920 self.log.debug('registering flow in device',
921 logical_flow=logical_flow, device_flow=device_flow)
922 stored_flow = copy.deepcopy(logical_flow)
923 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
924 device_flow.flow_type)
925 self.log.debug('generated device flow id', id=stored_flow.id,
926 flow_id=device_flow.flow_id,
927 direction=device_flow.flow_type)
928 stored_flow.cookie = logical_flow.id
929 flows = self.flows_proxy.get('/')
930 flows.items.extend([stored_flow])
931 self.flows_proxy.update('/', flows)
932
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400933 def find_next_flow(self, flow):
934 table_id = fd.get_goto_table_id(flow)
935 metadata = 0
Chip Boling41f795a2018-10-04 15:45:34 -0500936 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
937 # 1.13.5 and later, the lower 32-bits is the output port number and the
938 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
939 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
940
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400941 for field in fd.get_ofb_fields(flow):
942 if field.type == fd.METADATA:
Chip Boling41f795a2018-10-04 15:45:34 -0500943 metadata = field.table_metadata & 0xFFFFFFFF
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400944 if table_id is None:
945 return None
946 flows = self.logical_flows_proxy.get('/').items
947 next_flows = []
948 for f in flows:
949 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700950 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400951 if fd.get_in_port(f) == fd.get_in_port(flow) and \
952 fd.get_out_port(f) == metadata:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400953 next_flows.append(f)
954
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400955 if len(next_flows) == 0:
956 self.log.warning('no next flow found, it may be a timing issue',
957 flow=flow, number_of_flows=len(flows))
Scott Bakerbe5a9ea2018-11-19 19:24:21 -0800958 if flow.id in self.retry_add_flow_list:
959 self.log.debug('flow is already in retry list', flow_id=flow.id)
960 else:
961 self.retry_add_flow_list.append(flow.id)
962 reactor.callLater(5, self.retry_add_flow, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400963 return None
964
Jonathan Hart5b435642018-08-20 08:50:05 -0700965 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400966
967 return next_flows[0]
968
969 def update_children_flows(self, device_rules_map):
970
971 for device_id, (flows, groups) in device_rules_map.iteritems():
972 if device_id != self.device_id:
973 self.root_proxy.update('/devices/{}/flows'.format(device_id),
974 Flows(items=flows.values()))
975 self.root_proxy.update('/devices/{}/flow_groups'.format(
976 device_id), FlowGroups(items=groups.values()))
977
Girish Gowdruab836e92018-10-25 01:17:57 -0700978 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
979 ofp_port_name = logical_port.ofp_port.name
Craig Lutgenabd9c842018-11-15 23:58:27 +0000980 port_no = logical_port.ofp_port.port_no
Girish Gowdruab836e92018-10-25 01:17:57 -0700981 pon_port = child_device.proxy_address.channel_id
982 onu_id = child_device.proxy_address.onu_id
Craig Lutgenabd9c842018-11-15 23:58:27 +0000983 uni_id = self.platform.uni_id_from_port_num(logical_port)
984
Girish Gowdruab836e92018-10-25 01:17:57 -0700985 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
986 # is probably to maintain a list of Tech-profile table IDs associated
987 # with the UNI logical_port. This way, when the logical port is deleted,
988 # all the associated tech-profile configuration with the UNI logical_port
989 # can be cleared.
990 tech_profile_instance = self.tech_profile[pon_port]. \
991 get_tech_profile_instance(
992 DEFAULT_TECH_PROFILE_TABLE_ID,
993 ofp_port_name)
Girish Gowdru50f62fb2019-02-04 22:16:15 -0800994 flow_ids = self.resource_mgr.get_current_flow_ids(pon_port, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700995 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
996 for flow_id in flow_ids:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000997 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700998 for flow_info in flow_infos:
999 direction = flow_info['flow_type']
1000 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
1001 flow_type=direction)
1002 try:
1003 self.stub.FlowRemove(flow_to_remove)
1004 except grpc.RpcError as grpc_e:
1005 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
1006 self.log.debug('This flow does not exist on the switch, '
1007 'normal after an OLT reboot',
1008 flow=flow_to_remove)
1009 else:
1010 raise grpc_e
1011
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001012 self.resource_mgr.free_flow_id(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -07001013
1014 try:
1015 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
1016 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
1017 onu_id=onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +00001018 uni_id=uni_id,
1019 port_no=port_no,
Girish Gowdruab836e92018-10-25 01:17:57 -07001020 tconts=tconts))
1021 except grpc.RpcError as grpc_e:
1022 self.log.error('error-removing-tcont-scheduler-queues',
1023 err=grpc_e)
1024
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001025 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -04001026 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001027 self.log.debug('upstream flow, shifting id')
1028 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -04001029 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001030 self.log.debug('downstream flow, not shifting id')
1031 return flow_id
1032 else:
1033 self.log.warn('Unrecognized direction', direction=direction)
1034 return flow_id
1035
1036 def decode_stored_id(self, id):
1037 if id >> 15 == 0x1:
Girish Gowdruab836e92018-10-25 01:17:57 -07001038 return id & 0x7fff, UPSTREAM
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -04001039 else:
Girish Gowdruab836e92018-10-25 01:17:57 -07001040 return id, DOWNSTREAM
1041
1042 def _populate_tech_profile_per_pon_port(self):
1043 for arange in self.resource_mgr.device_info.ranges:
1044 for intf_id in arange.intf_ids:
1045 self.tech_profile[intf_id] = \
1046 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1047
1048 # Make sure we have as many tech_profiles as there are pon ports on
1049 # the device
1050 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1051
Girish Gowdrub761bc12018-11-29 02:22:18 -08001052 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1053 flow_category=None):
Girish Gowdruab836e92018-10-25 01:17:57 -07001054 json_blob = MessageToDict(message=flow,
1055 preserving_proto_field_name=True)
1056 self.log.debug("flow-info", json_blob=json_blob)
Girish Gowdrub761bc12018-11-29 02:22:18 -08001057 json_blob['flow_store_cookie'] = flow_store_cookie
1058 if flow_category is not None:
1059 json_blob['flow_category'] = flow_category
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001060
1061 # For flows which trap out of the NNI, the access_intf_id is invalid (set to -1).
1062 # In such cases, we need to refer to the network_intf_id.
1063 if flow.access_intf_id != -1:
1064 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1065 flow.onu_id, flow.uni_id,
1066 flow.flow_id)
1067 else:
1068 # Case of LLDP trap flow from the NNI. We can't use flow.access_intf_id
1069 # in that case, as it is invalid. We use flow.network_intf_id.
1070 flow_info = self.resource_mgr.get_flow_id_info(flow.network_intf_id,
1071 flow.onu_id, flow.uni_id,
1072 flow.flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -07001073
1074 if flow_info is None:
1075 flow_info = list()
1076 flow_info.append(json_blob)
1077 else:
1078 assert (isinstance(flow_info, list))
1079 flow_info.append(json_blob)
1080
1081 return flow_info
Girish Gowdrub761bc12018-11-29 02:22:18 -08001082
1083 @staticmethod
1084 def _get_flow_store_cookie(classifier, gem_port=None):
1085 assert isinstance(classifier, dict)
1086 # We need unique flows per gem_port
1087 if gem_port is not None:
1088 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1089 else:
1090 to_hash = dumps(classifier, sort_keys=True)
1091 return hashlib.md5(to_hash).hexdigest()[:12]
Girish Gowdrube328a02019-01-26 21:44:40 -08001092
1093 def get_nni_intf_id(self):
1094 if self.nni_intf_id is not None:
1095 return self.nni_intf_id
1096
1097 port_list = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI)
1098 logical_port = self.adapter_agent.get_logical_port(self.logical_device_id,
1099 port_list[0].label)
1100 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(logical_port.ofp_port.port_no)
1101 self.log.debug("nni-intf-d ", nni_intf_id=self.nni_intf_id)
1102 return self.nni_intf_id
Girish Gowdru50f62fb2019-02-04 22:16:15 -08001103