blob: 1b996e7527010d227d7ca13e8e8a507cd5272dea [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
Shad Ansari2dda4f32018-05-17 07:16:07 +000020
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040021from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
Jonathan Hart5b435642018-08-20 08:50:05 -070022 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
23 OFPXMT_OFB_VLAN_VID
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040024from voltha.protos.device_pb2 import Port
Shad Ansari2dda4f32018-05-17 07:16:07 +000025import voltha.core.flow_decomposer as fd
Shad Ansari2dda4f32018-05-17 07:16:07 +000026from voltha.adapters.openolt.protos import openolt_pb2
Nicolas Palpacuer61815162018-06-20 18:12:04 -040027from voltha.registry import registry
Shad Ansari2dda4f32018-05-17 07:16:07 +000028
Girish Gowdruab836e92018-10-25 01:17:57 -070029from common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID
30
31# Flow categories
32HSIA_FLOW = "HSIA_FLOW"
33DHCP_FLOW = "DHCP_FLOW"
34EAPOL_PRIMARY_FLOW = "EAPOL_PRIMARY_FLOW"
35EAPOL_SECONDARY_FLOW = "EAPOL_SECONDARY_FLOW"
36IGMP_FLOW = "IGMP_FLOW"
37LLDP_FLOW = "LLDP_FLOW"
Nicolas Palpacuer61815162018-06-20 18:12:04 -040038
39EAP_ETH_TYPE = 0x888e
Jonathan Hart5b435642018-08-20 08:50:05 -070040LLDP_ETH_TYPE = 0x88cc
Shad Ansari2dda4f32018-05-17 07:16:07 +000041
Girish Gowdruab836e92018-10-25 01:17:57 -070042IGMP_PROTO = 2
43
Shad Ansari2dda4f32018-05-17 07:16:07 +000044# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
45DEFAULT_MGMT_VLAN = 4091
46
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040047# Openolt Flow
Girish Gowdruab836e92018-10-25 01:17:57 -070048UPSTREAM = "upstream"
49DOWNSTREAM = "downstream"
50PACKET_TAG_TYPE = "pkt_tag_type"
51UNTAGGED = "untagged"
52SINGLE_TAG = "single_tag"
53DOUBLE_TAG = "double_tag"
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040054
55# Classifier
56ETH_TYPE = 'eth_type'
57TPID = 'tpid'
58IP_PROTO = 'ip_proto'
59IN_PORT = 'in_port'
60VLAN_VID = 'vlan_vid'
61VLAN_PCP = 'vlan_pcp'
62UDP_DST = 'udp_dst'
63UDP_SRC = 'udp_src'
64IPV4_DST = 'ipv4_dst'
65IPV4_SRC = 'ipv4_src'
66METADATA = 'metadata'
67OUTPUT = 'output'
68# Action
69POP_VLAN = 'pop_vlan'
70PUSH_VLAN = 'push_vlan'
71TRAP_TO_HOST = 'trap_to_host'
72
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040073
74
Shad Ansari2dda4f32018-05-17 07:16:07 +000075class OpenOltFlowMgr(object):
76
Girish Gowdruab836e92018-10-25 01:17:57 -070077 def __init__(self, adapter_agent, log, stub, device_id, logical_device_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -070078 platform, resource_mgr):
Girish Gowdruab836e92018-10-25 01:17:57 -070079 self.adapter_agent = adapter_agent
Shad Ansari2dda4f32018-05-17 07:16:07 +000080 self.log = log
81 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040082 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040083 self.logical_device_id = logical_device_id
Shad Ansaricd20a6d2018-10-02 14:36:33 +000084 self.platform = platform
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040085 self.logical_flows_proxy = registry('core').get_proxy(
86 '/logical_devices/{}/flows'.format(self.logical_device_id))
87 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040088 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040089 self.root_proxy = registry('core').get_proxy('/')
Girish Gowdru1e77ea02018-09-24 09:10:35 -070090 self.resource_mgr = resource_mgr
Girish Gowdruab836e92018-10-25 01:17:57 -070091 self.tech_profile = dict()
92 self._populate_tech_profile_per_pon_port()
Shad Ansari2dda4f32018-05-17 07:16:07 +000093
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040094 def add_flow(self, flow):
95 self.log.debug('add flow', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +000096 classifier_info = dict()
97 action_info = dict()
98
Shad Ansari2dda4f32018-05-17 07:16:07 +000099 for field in fd.get_ofb_fields(flow):
100 if field.type == fd.ETH_TYPE:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400101 classifier_info[ETH_TYPE] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +0000102 self.log.debug('field-type-eth-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400103 eth_type=classifier_info[ETH_TYPE])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000104 elif field.type == fd.IP_PROTO:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400105 classifier_info[IP_PROTO] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +0000106 self.log.debug('field-type-ip-proto',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400107 ip_proto=classifier_info[IP_PROTO])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000108 elif field.type == fd.IN_PORT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400109 classifier_info[IN_PORT] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000110 self.log.debug('field-type-in-port',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400111 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000112 elif field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400113 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +0000114 self.log.debug('field-type-vlan-vid',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400115 vlan=classifier_info[VLAN_VID])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000116 elif field.type == fd.VLAN_PCP:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400117 classifier_info[VLAN_PCP] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +0000118 self.log.debug('field-type-vlan-pcp',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400119 pcp=classifier_info[VLAN_PCP])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000120 elif field.type == fd.UDP_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400121 classifier_info[UDP_DST] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000122 self.log.debug('field-type-udp-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400123 udp_dst=classifier_info[UDP_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000124 elif field.type == fd.UDP_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400125 classifier_info[UDP_SRC] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000126 self.log.debug('field-type-udp-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400127 udp_src=classifier_info[UDP_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000128 elif field.type == fd.IPV4_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400129 classifier_info[IPV4_DST] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000130 self.log.debug('field-type-ipv4-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400131 ipv4_dst=classifier_info[IPV4_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000132 elif field.type == fd.IPV4_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400133 classifier_info[IPV4_SRC] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000134 self.log.debug('field-type-ipv4-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400135 ipv4_dst=classifier_info[IPV4_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000136 elif field.type == fd.METADATA:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400137 classifier_info[METADATA] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +0000138 self.log.debug('field-type-metadata',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400139 metadata=classifier_info[METADATA])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000140 else:
141 raise NotImplementedError('field.type={}'.format(
142 field.type))
143
144 for action in fd.get_actions(flow):
145 if action.type == fd.OUTPUT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400146 action_info[OUTPUT] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000147 self.log.debug('action-type-output',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400148 output=action_info[OUTPUT],
149 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000150 elif action.type == fd.POP_VLAN:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400151 if fd.get_goto_table_id(flow) is None:
152 self.log.debug('being taken care of by ONU', flow=flow)
153 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400154 action_info[POP_VLAN] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700155 self.log.debug('action-type-pop-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400156 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000157 elif action.type == fd.PUSH_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400158 action_info[PUSH_VLAN] = True
159 action_info[TPID] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000160 self.log.debug('action-type-push-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400161 push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000162 if action.push.ethertype != 0x8100:
163 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000164 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000165 elif action.type == fd.SET_FIELD:
166 # action_info['action_type'] = 'set_field'
167 _field = action.set_field.field.ofb_field
168 assert (action.set_field.field.oxm_class ==
169 OFPXMC_OPENFLOW_BASIC)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400170 self.log.debug('action-type-set-field',
171 field=_field, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000172 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000173 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000174 vlan_vid=_field.vlan_vid & 0xfff)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400175 action_info[VLAN_VID] = (_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000176 else:
177 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000178 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000179 else:
180 self.log.error('unsupported-action-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400181 action_type=action.type, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000182
Girish Gowdruab836e92018-10-25 01:17:57 -0700183 if fd.get_goto_table_id(flow) is not None and POP_VLAN not in action_info:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400184 self.log.debug('being taken care of by ONU', flow=flow)
Nicolas Palpacuer856d3af2018-09-12 15:04:51 -0400185 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000186
Girish Gowdruab836e92018-10-25 01:17:57 -0700187 if OUTPUT not in action_info and METADATA in classifier_info:
188 # find flow in the next table
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400189 next_flow = self.find_next_flow(flow)
190 if next_flow is None:
191 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400192 action_info[OUTPUT] = fd.get_out_port(next_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400193 for field in fd.get_ofb_fields(next_flow):
194 if field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400195 classifier_info[METADATA] = field.vlan_vid & 0xfff
196
Craig Lutgenabd9c842018-11-15 23:58:27 +0000197 self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT])
198 (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400199 classifier_info[IN_PORT], action_info[OUTPUT])
200
Craig Lutgenabd9c842018-11-15 23:58:27 +0000201 self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400202 action_info, flow)
203
Girish Gowdruab836e92018-10-25 01:17:57 -0700204 def _is_uni_port(self, port_no):
205 try:
206 port = self.adapter_agent.get_logical_port(self.logical_device_id,
207 'uni-{}'.format(port_no))
208 if port is not None:
209 return (not port.root_port), port.device_id
210 else:
211 return False, None
212 except Exception as e:
213 self.log.error("error-retrieving-port", e=e)
214 return False, None
215
216 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
217 uni_port_no = None
218 flow_category = HSIA_FLOW # default
219 child_device_id = None
220 if flow_direction == UPSTREAM:
221 for field in fd.get_ofb_fields(flow):
222 if field.type == fd.IN_PORT:
223 is_uni, child_device_id = self._is_uni_port(field.port)
224 if is_uni:
225 uni_port_no = field.port
226 elif field.type == fd.IP_PROTO:
227 if field.ip_proto == IGMP_PROTO:
228 flow_category = IGMP_FLOW
229 elif field.type == fd.ETH_TYPE:
230 if field.eth_type == EAP_ETH_TYPE:
231 flow_category = EAPOL_PRIMARY_FLOW
232 elif field.eth_type == LLDP_ETH_TYPE:
233 flow_category = LLDP_FLOW
234
235 elif flow_direction == DOWNSTREAM:
236 for field in fd.get_ofb_fields(flow):
237 if field.type == fd.METADATA:
238 uni_port = field.table_metadata & 0xFFFFFFFF
239 is_uni, child_device_id = self._is_uni_port(uni_port)
240 if is_uni:
241 uni_port_no = field.port
242
243 if uni_port_no is None:
244 for action in fd.get_actions(flow):
245 if action.type == fd.OUTPUT:
246 is_uni, child_device_id = \
247 self._is_uni_port(action.output.port)
248 if is_uni:
249 uni_port_no = action.output.port
250
251 if flow_category and child_device_id:
252 child_device = self.adapter_agent.get_device(child_device_id)
253 pon_intf = child_device.proxy_address.channel_id
254 onu_id = child_device.proxy_address.onu_id
Craig Lutgenabd9c842018-11-15 23:58:27 +0000255 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
256 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700257 assert (isinstance(flows, list))
258 self.log.debug("retrieved-flows", flows=flows)
259 for idx in range(len(flows)):
260 if flow_direction == flows[idx]['flow_type']:
261 flows.pop(idx)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000262 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
Girish Gowdruab836e92018-10-25 01:17:57 -0700263 if len(flows) > 0:
264 # There are still flows referencing the same flow_id.
265 # So the flow should not be freed yet.
266 # For ex: Case of HSIA where same flow is shared
267 # between DS and US.
268 return
269
Craig Lutgenabd9c842018-11-15 23:58:27 +0000270 self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700271 else:
272 self.log.error("invalid-info", uni_port_no=uni_port_no,
273 flow_category=flow_category,
274 child_device_id=child_device_id)
275
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400276 def remove_flow(self, flow):
277 self.log.debug('trying to remove flows from logical flow :',
Jonathan Hart5b435642018-08-20 08:50:05 -0700278 logical_flow=flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400279 device_flows_to_remove = []
280 device_flows = self.flows_proxy.get('/').items
281 for f in device_flows:
282 if f.cookie == flow.id:
283 device_flows_to_remove.append(f)
284
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400285 for f in device_flows_to_remove:
286 (id, direction) = self.decode_stored_id(f.id)
287 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400288 try:
289 self.stub.FlowRemove(flow_to_remove)
290 except grpc.RpcError as grpc_e:
291 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
292 self.log.debug('This flow does not exist on the switch, '
Jonathan Hart5b435642018-08-20 08:50:05 -0700293 'normal after an OLT reboot',
294 flow=flow_to_remove)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400295 else:
296 raise grpc_e
297
Girish Gowdruab836e92018-10-25 01:17:57 -0700298 # once we have successfully deleted the flow on the device
299 # release the flow_id on resource pool and also clear any
300 # data associated with the flow_id on KV store.
301 self._clear_flow_id_from_rm(f, id, direction)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400302 self.log.debug('flow removed from device', flow=f,
303 flow_key=flow_to_remove)
304
305 if len(device_flows_to_remove) > 0:
306 new_flows = []
307 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
308 for f in device_flows:
309 if f.id not in flows_ids_to_remove:
310 new_flows.append(f)
311
312 self.flows_proxy.update('/', Flows(items=new_flows))
313 self.log.debug('flows removed from the data store',
314 flow_ids_removed=flows_ids_to_remove,
315 number_of_flows_removed=(len(device_flows) - len(
316 new_flows)), expected_flows_removed=len(
Girish Gowdruab836e92018-10-25 01:17:57 -0700317 device_flows_to_remove))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400318 else:
319 self.log.debug('no device flow to remove for this flow (normal '
320 'for multi table flows)', flow=flow)
321
Craig Lutgenabd9c842018-11-15 23:58:27 +0000322 def _get_ofp_port_name(self, intf_id, onu_id, uni_id):
Girish Gowdruab836e92018-10-25 01:17:57 -0700323 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
324 child_device = self.adapter_agent.get_child_device(self.device_id,
325 parent_port_no=parent_port_no, onu_id=onu_id)
326 if child_device is None:
327 self.log.error("could-not-find-child-device",
328 parent_port_no=intf_id, onu_id=onu_id)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000329 return (None, None)
Girish Gowdruab836e92018-10-25 01:17:57 -0700330 ports = self.adapter_agent.get_ports(child_device.id, Port.ETHERNET_UNI)
331 logical_port = self.adapter_agent.get_logical_port(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000332 self.logical_device_id, ports[uni_id].label)
333 ofp_port_name = (logical_port.ofp_port.name, logical_port.ofp_port.port_no)
Girish Gowdruab836e92018-10-25 01:17:57 -0700334 return ofp_port_name
335
Craig Lutgenabd9c842018-11-15 23:58:27 +0000336 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400337 action, flow):
338
Craig Lutgenabd9c842018-11-15 23:58:27 +0000339 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 -0400340 classifier=classifier, action=action)
341
Craig Lutgenabd9c842018-11-15 23:58:27 +0000342 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700343 flow.table_id)
344 if alloc_id is None or gem_ports is None:
345 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id,
346 gem_ports=gem_ports)
347 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400348
Girish Gowdruab836e92018-10-25 01:17:57 -0700349 self.log.debug('Generated required alloc and gemport ids',
350 alloc_id=alloc_id, gemports=gem_ports)
351
352 # Flows can't be added specific to gemport unless p-bits are received.
353 # Hence adding flows for all gemports
354 for gemport_id in gem_ports:
355 if IP_PROTO in classifier:
356 if classifier[IP_PROTO] == 17:
357 self.log.debug('dhcp flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000358 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700359 action, flow, alloc_id, gemport_id)
360 elif classifier[IP_PROTO] == 2:
361 self.log.warn('igmp flow add ignored, not implemented yet')
362 else:
363 self.log.warn("Invalid-Classifier-to-handle",
364 classifier=classifier,
365 action=action)
366 elif ETH_TYPE in classifier:
367 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
368 self.log.debug('eapol flow add')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000369 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700370 gemport_id)
371 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
372 if vlan_id is not None:
373 self.add_eapol_flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000374 intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700375 eapol_flow_category=EAPOL_SECONDARY_FLOW,
376 vlan_id=vlan_id)
377 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
378 onu_device = self.adapter_agent.get_child_device(self.device_id,
379 onu_id=onu_id,
380 parent_port_no=parent_port_no)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000381 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700382 if ofp_port_name is None:
383 self.log.error("port-name-not-found")
384 return
385
386 # FIXME Should get Table id form the flow, as of now hardcoded to
387 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
Girish Gowdru36ef0692018-11-27 02:51:51 -0800388 # 'tp_path' contains the suffix part of the tech_profile_instance path.
389 # The prefix to the 'tp_path' should be set to \
390 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
391 tp_path = self.tech_profile[intf_id]. \
Girish Gowdruab836e92018-10-25 01:17:57 -0700392 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
393 ofp_port_name)
394
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')
Craig Lutgenabd9c842018-11-15 23:58:27 +0000406 self.add_lldp_flow(flow, port_no)
Girish Gowdruab836e92018-10-25 01:17:57 -0700407
408 elif PUSH_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000409 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700410 action, flow, alloc_id, gemport_id)
411 elif POP_VLAN in action:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000412 self.add_downstream_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 else:
415 self.log.debug('Invalid-flow-type-to-handle',
416 classifier=classifier,
417 action=action, flow=flow)
418
Craig Lutgenabd9c842018-11-15 23:58:27 +0000419 def create_tcont_gemport(self, intf_id, onu_id, uni_id, table_id):
Girish Gowdruab836e92018-10-25 01:17:57 -0700420 alloc_id, gem_port_ids = None, None
421 try:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000422 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700423 if ofp_port_name is None:
424 self.log.error("port-name-not-found")
425 return alloc_id, gem_port_ids
426 # FIXME: If table id is <= 63 using 64 as table id
427 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
428 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
429
430 # Check tech profile instance already exists for derived port name
431 tech_profile_instance = self.tech_profile[intf_id]. \
432 get_tech_profile_instance(table_id, ofp_port_name)
433 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
434
435 if tech_profile_instance is None:
436 # create tech profile instance
437 tech_profile_instance = self.tech_profile[intf_id]. \
438 create_tech_profile_instance(table_id, ofp_port_name,
439 intf_id)
440 if tech_profile_instance is not None:
441
442 # upstream scheduler
443 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
444 tech_profile_instance)
445 # downstream scheduler
446 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
447 tech_profile_instance)
448 # create Tcont
449 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
450 us_scheduler,
451 ds_scheduler)
452
453 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
454 onu_id=onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000455 uni_id=uni_id,
456 port_no=ofp_port_no,
Girish Gowdruab836e92018-10-25 01:17:57 -0700457 tconts=tconts))
458 else:
459 raise Exception('Tech-profile-instance-creation-failed')
460 else:
461 self.log.debug(
462 'Tech-profile-instance-already-exist-for-given port-name',
463 ofp_port_name=ofp_port_name)
464
465 # Fetch alloc id and gemports from tech profile instance
466 alloc_id = tech_profile_instance.us_scheduler.alloc_id
467 gem_port_ids = []
468 for i in range(len(
469 tech_profile_instance.upstream_gem_port_attribute_list)):
470 gem_port_ids.append(
471 tech_profile_instance.upstream_gem_port_attribute_list[i].
472 gemport_id)
473 except BaseException as e:
474 self.log.exception(exception=e)
475
Craig Lutgenabd9c842018-11-15 23:58:27 +0000476 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
477 pon_intf_onu_id = (intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700478 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
479 pon_intf_onu_id,
480 list([alloc_id])
481 )
482 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
483 pon_intf_onu_id,
484 gem_port_ids
485 )
486
487 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000488 gem_port_ids, intf_id, onu_id, uni_id
Girish Gowdruab836e92018-10-25 01:17:57 -0700489 )
490
491 return alloc_id, gem_port_ids
Shad Ansari2dda4f32018-05-17 07:16:07 +0000492
Craig Lutgenabd9c842018-11-15 23:58:27 +0000493 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700494 uplink_action, logical_flow, alloc_id,
495 gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400496
497 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
498
Craig Lutgenabd9c842018-11-15 23:58:27 +0000499 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700500 uplink_action, UPSTREAM,
501 logical_flow, alloc_id, gemport_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000502
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400503 # Secondary EAP on the subscriber vlan
Craig Lutgenabd9c842018-11-15 23:58:27 +0000504 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400505 if eap_active:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000506 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700507 gemport_id, eapol_flow_category=EAPOL_SECONDARY_FLOW,
508 vlan_id=uplink_classifier[VLAN_VID])
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400509
Craig Lutgenabd9c842018-11-15 23:58:27 +0000510 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700511 downlink_action, flow, alloc_id, gemport_id):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400512 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400513 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400514 downlink_action[POP_VLAN] = True
515 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400516
Craig Lutgenabd9c842018-11-15 23:58:27 +0000517 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
Girish Gowdruab836e92018-10-25 01:17:57 -0700518 downlink_action, DOWNSTREAM,
519 flow, alloc_id, gemport_id)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400520
Craig Lutgenabd9c842018-11-15 23:58:27 +0000521 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
Girish Gowdruab836e92018-10-25 01:17:57 -0700522 direction, logical_flow, alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000523
Craig Lutgenabd9c842018-11-15 23:58:27 +0000524 flow_id = self.resource_mgr.get_hsia_flow_for_uni(intf_id, onu_id, uni_id, gemport_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700525 if flow_id is None:
526 self.log.error("hsia-flow-unavailable")
527 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000528
Shad Ansari2dda4f32018-05-17 07:16:07 +0000529 flow = openolt_pb2.Flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000530 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700531 flow_type=direction, alloc_id=alloc_id, network_intf_id=0,
532 gemport_id=gemport_id,
533 classifier=self.mk_classifier(classifier),
534 action=self.mk_action(action),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000535 priority=logical_flow.priority,
536 port_no=port_no,
537 cookie=logical_flow.cookie)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000538
Girish Gowdruab836e92018-10-25 01:17:57 -0700539 if self.add_flow_to_device(flow, logical_flow):
540 flow_info = self._get_flow_info_as_json_blob(flow, HSIA_FLOW)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000541 self.update_flow_info_to_kv_store(flow.access_intf_id, flow.onu_id, flow.uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700542 flow.flow_id, flow_info)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000543
Craig Lutgenabd9c842018-11-15 23:58:27 +0000544 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
Girish Gowdruab836e92018-10-25 01:17:57 -0700545 alloc_id, gemport_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000546
Shad Ansari47e0c392018-07-17 23:55:07 +0000547 self.log.debug('add dhcp upstream trap', classifier=classifier,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000548 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000549
550 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400551 action[TRAP_TO_HOST] = True
Girish Gowdruab836e92018-10-25 01:17:57 -0700552 classifier[UDP_SRC] = 68
553 classifier[UDP_DST] = 67
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400554 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
555 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000556
Craig Lutgenabd9c842018-11-15 23:58:27 +0000557 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000558
Matteo Scandolodf583282018-11-02 16:18:19 -0700559 dhcp_flow = openolt_pb2.Flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000560 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400561 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700562 alloc_id=alloc_id, network_intf_id=0,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400563 priority=logical_flow.priority,
564 classifier=self.mk_classifier(classifier),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000565 action=self.mk_action(action),
566 port_no=port_no,
567 cookie=logical_flow.cookie)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000568
Girish Gowdruab836e92018-10-25 01:17:57 -0700569 if self.add_flow_to_device(dhcp_flow, logical_flow):
570 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, DHCP_FLOW)
571 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
572 dhcp_flow.onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000573 dhcp_flow.uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700574 dhcp_flow.flow_id,
575 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400576
Craig Lutgenabd9c842018-11-15 23:58:27 +0000577 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700578 gemport_id, eapol_flow_category=EAPOL_PRIMARY_FLOW,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000579 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000580
Girish Gowdruab836e92018-10-25 01:17:57 -0700581 uplink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400582 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
583 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
584 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400585
Girish Gowdruab836e92018-10-25 01:17:57 -0700586 uplink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400587 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000588
Girish Gowdruab836e92018-10-25 01:17:57 -0700589 # Add Upstream EAPOL Flow.
590 if eapol_flow_category == EAPOL_PRIMARY_FLOW:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000591 uplink_flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700592 else:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000593 uplink_flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400594
Shad Ansari2dda4f32018-05-17 07:16:07 +0000595 upstream_flow = openolt_pb2.Flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000596 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700597 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=0,
598 gemport_id=gemport_id,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000599 classifier=self.mk_classifier(uplink_classifier),
Girish Gowdruab836e92018-10-25 01:17:57 -0700600 action=self.mk_action(uplink_action),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000601 priority=logical_flow.priority,
602 port_no=port_no,
603 cookie=logical_flow.cookie)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000604
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400605 logical_flow = copy.deepcopy(logical_flow)
606 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
607 vlan_id | 0x1000)]))
608 logical_flow.match.type = OFPMT_OXM
609
Girish Gowdruab836e92018-10-25 01:17:57 -0700610 if self.add_flow_to_device(upstream_flow, logical_flow):
611 if eapol_flow_category == EAPOL_PRIMARY_FLOW:
612 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
613 EAPOL_PRIMARY_FLOW)
614 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
615 upstream_flow.onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000616 upstream_flow.uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700617 upstream_flow.flow_id,
618 flow_info)
619 else:
620 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
621 EAPOL_SECONDARY_FLOW)
622 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
623 upstream_flow.onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000624 upstream_flow.uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700625 upstream_flow.flow_id,
626 flow_info)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000627
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400628 if vlan_id == DEFAULT_MGMT_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400629 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
630 # requirement)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000631 # FIXME this is actually not used. But, here to be consistent with the upstream tagging from ONU.
632 # It needs refactored to be completely removed
633 special_vlan_downstream_flow = 4091 # 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000634
Girish Gowdruab836e92018-10-25 01:17:57 -0700635 downlink_classifier = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400636 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
637 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400638
Girish Gowdruab836e92018-10-25 01:17:57 -0700639 downlink_action = dict()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400640 downlink_action[PUSH_VLAN] = True
641 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400642
Craig Lutgenabd9c842018-11-15 23:58:27 +0000643 downlink_flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400644
645 downstream_flow = openolt_pb2.Flow(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000646 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700647 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=0,
648 gemport_id=gemport_id,
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400649 classifier=self.mk_classifier(downlink_classifier),
Girish Gowdruab836e92018-10-25 01:17:57 -0700650 action=self.mk_action(downlink_action),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000651 priority=logical_flow.priority,
652 port_no=port_no,
653 cookie=logical_flow.cookie)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400654
Shad Ansarifd0111d2018-09-13 21:33:06 +0000655 downstream_logical_flow = ofp_flow_stats(
656 id=logical_flow.id, cookie=logical_flow.cookie,
657 table_id=logical_flow.table_id, priority=logical_flow.priority,
658 flags=logical_flow.flags)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400659
660 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
661 fd.in_port(fd.get_out_port(logical_flow)),
Girish Gowdruab836e92018-10-25 01:17:57 -0700662 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400663 downstream_logical_flow.match.type = OFPMT_OXM
664
665 downstream_logical_flow.instructions.extend(
666 fd.mk_instructions_from_actions([fd.output(
Craig Lutgenabd9c842018-11-15 23:58:27 +0000667 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400668
Girish Gowdruab836e92018-10-25 01:17:57 -0700669 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
670 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
671 EAPOL_PRIMARY_FLOW)
672 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
673 downstream_flow.onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000674 downstream_flow.uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700675 downstream_flow.flow_id,
676 flow_info)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400677
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400678 def repush_all_different_flows(self):
679 # Check if the device is supposed to have flows, if so add them
680 # Recover static flows after a reboot
681 logical_flows = self.logical_flows_proxy.get('/').items
682 devices_flows = self.flows_proxy.get('/').items
683 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
684 for logical_flow in logical_flows:
685 try:
686 if logical_flow.id not in logical_flows_ids_provisioned:
687 self.add_flow(logical_flow)
688 except Exception as e:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000689 self.log.exception('Problem reading this flow', e=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400690
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400691 def reset_flows(self):
692 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400693
Shad Ansarifd0111d2018-09-13 21:33:06 +0000694 """ Add a downstream LLDP trap flow on the NNI interface
695 """
Girish Gowdruab836e92018-10-25 01:17:57 -0700696
Craig Lutgenabd9c842018-11-15 23:58:27 +0000697 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
Jonathan Hart5b435642018-08-20 08:50:05 -0700698
Girish Gowdruab836e92018-10-25 01:17:57 -0700699 classifier = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000700 classifier[ETH_TYPE] = LLDP_ETH_TYPE
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400701 classifier[PACKET_TAG_TYPE] = UNTAGGED
Girish Gowdruab836e92018-10-25 01:17:57 -0700702 action = dict()
Shad Ansarifd0111d2018-09-13 21:33:06 +0000703 action[TRAP_TO_HOST] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700704
Girish Gowdruab836e92018-10-25 01:17:57 -0700705 # LLDP flow is installed to trap LLDP packets on the NNI port.
706 # We manage flow_id resource pool on per PON port basis.
707 # Since this situation is tricky, as a hack, we pass the NNI port
708 # index (network_intf_id) as PON port Index for the flow_id resource
709 # pool. Also, there is no ONU Id available for trapping LLDP packets
710 # on NNI port, use onu_id as -1 (invalid)
711 # ****************** CAVEAT *******************
712 # This logic works if the NNI Port Id falls within the same valid
713 # range of PON Port Ids. If this doesn't work for some OLT Vendor
714 # we need to have a re-look at this.
715 # *********************************************
716 onu_id = -1
Craig Lutgenabd9c842018-11-15 23:58:27 +0000717 uni_id = -1
718 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id)
Jonathan Hart5b435642018-08-20 08:50:05 -0700719
720 downstream_flow = openolt_pb2.Flow(
Shad Ansarifd0111d2018-09-13 21:33:06 +0000721 access_intf_id=-1, # access_intf_id not required
Craig Lutgenabd9c842018-11-15 23:58:27 +0000722 onu_id=onu_id, # onu_id not required
723 uni_id=uni_id, # uni_id not used
Shad Ansarifd0111d2018-09-13 21:33:06 +0000724 flow_id=flow_id,
725 flow_type=DOWNSTREAM,
Shad Ansarifd0111d2018-09-13 21:33:06 +0000726 network_intf_id=network_intf_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700727 gemport_id=-1, # gemport_id not required
Jonathan Hart5b435642018-08-20 08:50:05 -0700728 classifier=self.mk_classifier(classifier),
Girish Gowdruab836e92018-10-25 01:17:57 -0700729 action=self.mk_action(action),
Craig Lutgenabd9c842018-11-15 23:58:27 +0000730 priority=logical_flow.priority,
731 port_no=port_no,
732 cookie=logical_flow.cookie)
Jonathan Hart5b435642018-08-20 08:50:05 -0700733
Shad Ansarifd0111d2018-09-13 21:33:06 +0000734 self.log.debug('add lldp downstream trap', classifier=classifier,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000735 action=action, flow=downstream_flow, port_no=port_no)
Girish Gowdruab836e92018-10-25 01:17:57 -0700736 if self.add_flow_to_device(downstream_flow, logical_flow):
Craig Lutgenabd9c842018-11-15 23:58:27 +0000737 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700738 flow_id, downstream_flow)
Jonathan Hart5b435642018-08-20 08:50:05 -0700739
Shad Ansari2dda4f32018-05-17 07:16:07 +0000740 def mk_classifier(self, classifier_info):
741
742 classifier = openolt_pb2.Classifier()
743
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400744 if ETH_TYPE in classifier_info:
745 classifier.eth_type = classifier_info[ETH_TYPE]
746 if IP_PROTO in classifier_info:
747 classifier.ip_proto = classifier_info[IP_PROTO]
748 if VLAN_VID in classifier_info:
749 classifier.o_vid = classifier_info[VLAN_VID]
750 if METADATA in classifier_info:
751 classifier.i_vid = classifier_info[METADATA]
752 if VLAN_PCP in classifier_info:
753 classifier.o_pbits = classifier_info[VLAN_PCP]
754 if UDP_SRC in classifier_info:
755 classifier.src_port = classifier_info[UDP_SRC]
756 if UDP_DST in classifier_info:
757 classifier.dst_port = classifier_info[UDP_DST]
758 if IPV4_DST in classifier_info:
759 classifier.dst_ip = classifier_info[IPV4_DST]
760 if IPV4_SRC in classifier_info:
761 classifier.src_ip = classifier_info[IPV4_SRC]
762 if PACKET_TAG_TYPE in classifier_info:
763 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
764 classifier.pkt_tag_type = SINGLE_TAG
765 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
766 classifier.pkt_tag_type = DOUBLE_TAG
767 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
768 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000769 else:
770 classifier.pkt_tag_type = 'none'
771
772 return classifier
773
774 def mk_action(self, action_info):
775 action = openolt_pb2.Action()
776
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400777 if POP_VLAN in action_info:
778 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000779 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400780 elif PUSH_VLAN in action_info:
781 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000782 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400783 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000784 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000785 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400786 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000787 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000788 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400789
Craig Lutgenabd9c842018-11-15 23:58:27 +0000790 def is_eap_enabled(self, intf_id, onu_id, uni_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400791 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400792
793 for flow in flows:
794 eap_flow = False
795 eap_intf_id = None
796 eap_onu_id = None
Craig Lutgenabd9c842018-11-15 23:58:27 +0000797 eap_uni_id = None
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400798 for field in fd.get_ofb_fields(flow):
799 if field.type == fd.ETH_TYPE:
800 if field.eth_type == EAP_ETH_TYPE:
801 eap_flow = True
802 if field.type == fd.IN_PORT:
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000803 eap_intf_id = self.platform.intf_id_from_uni_port_num(
Shad Ansari47e0c392018-07-17 23:55:07 +0000804 field.port)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000805 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000806 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400807
808 if eap_flow:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000809 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400810 intf_id=intf_id, eap_intf_id=eap_intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000811 eap_onu_id=eap_onu_id,
812 eap_uni_id=eap_uni_id)
813 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 -0700814 return True, flow
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400815
Girish Gowdruab836e92018-10-25 01:17:57 -0700816 return False, None
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400817
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400818 def get_subscriber_vlan(self, port):
819 self.log.debug('looking from subscriber flow for port', port=port)
820
821 flows = self.logical_flows_proxy.get('/').items
822 for flow in flows:
823 in_port = fd.get_in_port(flow)
824 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400825 if in_port == port and \
Girish Gowdruab836e92018-10-25 01:17:57 -0700826 self.platform.intf_id_to_port_type_name(out_port) \
Shad Ansarifd0111d2018-09-13 21:33:06 +0000827 == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400828 fields = fd.get_ofb_fields(flow)
829 self.log.debug('subscriber flow found', fields=fields)
830 for field in fields:
831 if field.type == OFPXMT_OFB_VLAN_VID:
832 self.log.debug('subscriber vlan found',
833 vlan_id=field.vlan_vid)
834 return field.vlan_vid & 0x0fff
835 self.log.debug('No subscriber flow found', port=port)
836 return None
837
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400838 def add_flow_to_device(self, flow, logical_flow):
839 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400840 try:
841 self.stub.FlowAdd(flow)
842 except grpc.RpcError as grpc_e:
843 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
844 self.log.warn('flow already exists', e=grpc_e, flow=flow)
845 else:
846 self.log.error('failed to add flow',
847 logical_flow=logical_flow, flow=flow,
848 grpc_error=grpc_e)
Girish Gowdruab836e92018-10-25 01:17:57 -0700849 return False
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400850 else:
851 self.register_flow(logical_flow, flow)
Girish Gowdruab836e92018-10-25 01:17:57 -0700852 return True
853
Craig Lutgenabd9c842018-11-15 23:58:27 +0000854 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
855 self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id,
Girish Gowdruab836e92018-10-25 01:17:57 -0700856 flow_id, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400857
858 def register_flow(self, logical_flow, device_flow):
859 self.log.debug('registering flow in device',
860 logical_flow=logical_flow, device_flow=device_flow)
861 stored_flow = copy.deepcopy(logical_flow)
862 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
863 device_flow.flow_type)
864 self.log.debug('generated device flow id', id=stored_flow.id,
865 flow_id=device_flow.flow_id,
866 direction=device_flow.flow_type)
867 stored_flow.cookie = logical_flow.id
868 flows = self.flows_proxy.get('/')
869 flows.items.extend([stored_flow])
870 self.flows_proxy.update('/', flows)
871
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400872 def find_next_flow(self, flow):
873 table_id = fd.get_goto_table_id(flow)
874 metadata = 0
Chip Boling41f795a2018-10-04 15:45:34 -0500875 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
876 # 1.13.5 and later, the lower 32-bits is the output port number and the
877 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
878 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
879
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400880 for field in fd.get_ofb_fields(flow):
881 if field.type == fd.METADATA:
Chip Boling41f795a2018-10-04 15:45:34 -0500882 metadata = field.table_metadata & 0xFFFFFFFF
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400883 if table_id is None:
884 return None
885 flows = self.logical_flows_proxy.get('/').items
886 next_flows = []
887 for f in flows:
888 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700889 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400890 if fd.get_in_port(f) == fd.get_in_port(flow) and \
891 fd.get_out_port(f) == metadata:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400892 next_flows.append(f)
893
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400894 if len(next_flows) == 0:
895 self.log.warning('no next flow found, it may be a timing issue',
896 flow=flow, number_of_flows=len(flows))
897 reactor.callLater(5, self.add_flow, flow)
898 return None
899
Jonathan Hart5b435642018-08-20 08:50:05 -0700900 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400901
902 return next_flows[0]
903
904 def update_children_flows(self, device_rules_map):
905
906 for device_id, (flows, groups) in device_rules_map.iteritems():
907 if device_id != self.device_id:
908 self.root_proxy.update('/devices/{}/flows'.format(device_id),
909 Flows(items=flows.values()))
910 self.root_proxy.update('/devices/{}/flow_groups'.format(
911 device_id), FlowGroups(items=groups.values()))
912
Girish Gowdruab836e92018-10-25 01:17:57 -0700913 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
914 ofp_port_name = logical_port.ofp_port.name
Craig Lutgenabd9c842018-11-15 23:58:27 +0000915 port_no = logical_port.ofp_port.port_no
Girish Gowdruab836e92018-10-25 01:17:57 -0700916 pon_port = child_device.proxy_address.channel_id
917 onu_id = child_device.proxy_address.onu_id
Craig Lutgenabd9c842018-11-15 23:58:27 +0000918 uni_id = self.platform.uni_id_from_port_num(logical_port)
919
Girish Gowdruab836e92018-10-25 01:17:57 -0700920 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
921 # is probably to maintain a list of Tech-profile table IDs associated
922 # with the UNI logical_port. This way, when the logical port is deleted,
923 # all the associated tech-profile configuration with the UNI logical_port
924 # can be cleared.
925 tech_profile_instance = self.tech_profile[pon_port]. \
926 get_tech_profile_instance(
927 DEFAULT_TECH_PROFILE_TABLE_ID,
928 ofp_port_name)
Craig Lutgenabd9c842018-11-15 23:58:27 +0000929 flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700930 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
931 for flow_id in flow_ids:
Craig Lutgenabd9c842018-11-15 23:58:27 +0000932 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700933 for flow_info in flow_infos:
934 direction = flow_info['flow_type']
935 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
936 flow_type=direction)
937 try:
938 self.stub.FlowRemove(flow_to_remove)
939 except grpc.RpcError as grpc_e:
940 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
941 self.log.debug('This flow does not exist on the switch, '
942 'normal after an OLT reboot',
943 flow=flow_to_remove)
944 else:
945 raise grpc_e
946
Craig Lutgenabd9c842018-11-15 23:58:27 +0000947 self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700948
949 try:
950 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
951 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
952 onu_id=onu_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000953 uni_id=uni_id,
954 port_no=port_no,
Girish Gowdruab836e92018-10-25 01:17:57 -0700955 tconts=tconts))
956 except grpc.RpcError as grpc_e:
957 self.log.error('error-removing-tcont-scheduler-queues',
958 err=grpc_e)
959
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400960 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400961 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400962 self.log.debug('upstream flow, shifting id')
963 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400964 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400965 self.log.debug('downstream flow, not shifting id')
966 return flow_id
967 else:
968 self.log.warn('Unrecognized direction', direction=direction)
969 return flow_id
970
971 def decode_stored_id(self, id):
972 if id >> 15 == 0x1:
Girish Gowdruab836e92018-10-25 01:17:57 -0700973 return id & 0x7fff, UPSTREAM
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400974 else:
Girish Gowdruab836e92018-10-25 01:17:57 -0700975 return id, DOWNSTREAM
976
977 def _populate_tech_profile_per_pon_port(self):
978 for arange in self.resource_mgr.device_info.ranges:
979 for intf_id in arange.intf_ids:
980 self.tech_profile[intf_id] = \
981 self.resource_mgr.resource_mgrs[intf_id].tech_profile
982
983 # Make sure we have as many tech_profiles as there are pon ports on
984 # the device
985 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
986
987 def _get_flow_info_as_json_blob(self, flow, flow_category):
988 json_blob = MessageToDict(message=flow,
989 preserving_proto_field_name=True)
990 self.log.debug("flow-info", json_blob=json_blob)
991 json_blob['flow_category'] = flow_category
992 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
Craig Lutgenabd9c842018-11-15 23:58:27 +0000993 flow.onu_id, flow.uni_id, flow.flow_id)
Girish Gowdruab836e92018-10-25 01:17:57 -0700994
995 if flow_info is None:
996 flow_info = list()
997 flow_info.append(json_blob)
998 else:
999 assert (isinstance(flow_info, list))
1000 flow_info.append(json_blob)
1001
1002 return flow_info