blob: 651ed1a6cf096ed2616d4365410595240da33b7d [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
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#
16import copy
17from twisted.internet import reactor
18import grpc
19from google.protobuf.json_format import MessageToDict
20import hashlib
21from simplejson import dumps
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040022from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian6f436d02019-02-06 16:25:01 -050023
William Kurkian8b1690c2019-03-04 16:53:22 -050024from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
William Kurkian6f436d02019-02-06 16:25:01 -050025 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
26 OFPXMT_OFB_VLAN_VID
William Kurkian8b1690c2019-03-04 16:53:22 -050027from voltha_protos.device_pb2 import Port
William Kurkian44cd7bb2019-02-11 16:39:12 -050028import pyvoltha.common.openflow.utils as fd
William Kurkian8b1690c2019-03-04 16:53:22 -050029from voltha_protos import openolt_pb2
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040030from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -040031 InterAdapterMessageType, InterAdapterOmciMessage, InterAdapterTechProfileDownloadMessage
William Kurkian6f436d02019-02-06 16:25:01 -050032
William Kurkian44cd7bb2019-02-11 16:39:12 -050033from pyvoltha.common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID
William Kurkian6f436d02019-02-06 16:25:01 -050034
35# Flow categories
36HSIA_FLOW = "HSIA_FLOW"
37
38EAP_ETH_TYPE = 0x888e
39LLDP_ETH_TYPE = 0x88cc
40
41IGMP_PROTO = 2
42
43# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
44DEFAULT_MGMT_VLAN = 4091
45
46# Openolt Flow
47UPSTREAM = "upstream"
48DOWNSTREAM = "downstream"
49PACKET_TAG_TYPE = "pkt_tag_type"
50UNTAGGED = "untagged"
51SINGLE_TAG = "single_tag"
52DOUBLE_TAG = "double_tag"
53
54# Classifier
55ETH_TYPE = 'eth_type'
56TPID = 'tpid'
57IP_PROTO = 'ip_proto'
58IN_PORT = 'in_port'
59VLAN_VID = 'vlan_vid'
60VLAN_PCP = 'vlan_pcp'
61UDP_DST = 'udp_dst'
62UDP_SRC = 'udp_src'
63IPV4_DST = 'ipv4_dst'
64IPV4_SRC = 'ipv4_src'
65METADATA = 'metadata'
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -040066TUNNEL_ID = 'tunnel_id'
William Kurkian6f436d02019-02-06 16:25:01 -050067OUTPUT = 'output'
68# Action
69POP_VLAN = 'pop_vlan'
70PUSH_VLAN = 'push_vlan'
71TRAP_TO_HOST = 'trap_to_host'
72
73
74class OpenOltFlowMgr(object):
75
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040076 def __init__(self, core_proxy, adapter_proxy, log, stub, device_id, logical_device_id,
William Kurkian6f436d02019-02-06 16:25:01 -050077 platform, resource_mgr):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040078 self.core_proxy = core_proxy
79 self.adapter_proxy = adapter_proxy
William Kurkian6f436d02019-02-06 16:25:01 -050080 self.log = log
81 self.stub = stub
82 self.device_id = device_id
83 self.logical_device_id = logical_device_id
84 self.nni_intf_id = None
85 self.platform = platform
William Kurkian6f436d02019-02-06 16:25:01 -050086 self.resource_mgr = resource_mgr
87 self.tech_profile = dict()
88 self._populate_tech_profile_per_pon_port()
89 self.retry_add_flow_list = []
90
91 def add_flow(self, flow):
92 self.log.debug('add flow', flow=flow)
93 classifier_info = dict()
94 action_info = dict()
95
96 for field in fd.get_ofb_fields(flow):
97 if field.type == fd.ETH_TYPE:
98 classifier_info[ETH_TYPE] = field.eth_type
99 self.log.debug('field-type-eth-type',
100 eth_type=classifier_info[ETH_TYPE])
101 elif field.type == fd.IP_PROTO:
102 classifier_info[IP_PROTO] = field.ip_proto
103 self.log.debug('field-type-ip-proto',
104 ip_proto=classifier_info[IP_PROTO])
105 elif field.type == fd.IN_PORT:
106 classifier_info[IN_PORT] = field.port
107 self.log.debug('field-type-in-port',
108 in_port=classifier_info[IN_PORT])
109 elif field.type == fd.VLAN_VID:
110 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
111 self.log.debug('field-type-vlan-vid',
112 vlan=classifier_info[VLAN_VID])
113 elif field.type == fd.VLAN_PCP:
114 classifier_info[VLAN_PCP] = field.vlan_pcp
115 self.log.debug('field-type-vlan-pcp',
116 pcp=classifier_info[VLAN_PCP])
117 elif field.type == fd.UDP_DST:
118 classifier_info[UDP_DST] = field.udp_dst
119 self.log.debug('field-type-udp-dst',
120 udp_dst=classifier_info[UDP_DST])
121 elif field.type == fd.UDP_SRC:
122 classifier_info[UDP_SRC] = field.udp_src
123 self.log.debug('field-type-udp-src',
124 udp_src=classifier_info[UDP_SRC])
125 elif field.type == fd.IPV4_DST:
126 classifier_info[IPV4_DST] = field.ipv4_dst
127 self.log.debug('field-type-ipv4-dst',
128 ipv4_dst=classifier_info[IPV4_DST])
129 elif field.type == fd.IPV4_SRC:
130 classifier_info[IPV4_SRC] = field.ipv4_src
131 self.log.debug('field-type-ipv4-src',
132 ipv4_dst=classifier_info[IPV4_SRC])
133 elif field.type == fd.METADATA:
134 classifier_info[METADATA] = field.table_metadata
135 self.log.debug('field-type-metadata',
136 metadata=classifier_info[METADATA])
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400137 elif field.type == fd.TUNNEL_ID:
138 classifier_info[TUNNEL_ID] = field.tunnel_id
139 self.log.debug('field-type-tunnel-id',
140 tunnel_id=classifier_info[TUNNEL_ID])
William Kurkian6f436d02019-02-06 16:25:01 -0500141 else:
142 raise NotImplementedError('field.type={}'.format(
143 field.type))
144
145 for action in fd.get_actions(flow):
146 if action.type == fd.OUTPUT:
147 action_info[OUTPUT] = action.output.port
148 self.log.debug('action-type-output',
149 output=action_info[OUTPUT],
150 in_port=classifier_info[IN_PORT])
151 elif action.type == fd.POP_VLAN:
William Kurkian6f436d02019-02-06 16:25:01 -0500152 action_info[POP_VLAN] = True
153 self.log.debug('action-type-pop-vlan',
154 in_port=classifier_info[IN_PORT])
155 elif action.type == fd.PUSH_VLAN:
156 action_info[PUSH_VLAN] = True
157 action_info[TPID] = action.push.ethertype
158 self.log.debug('action-type-push-vlan',
159 push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT])
160 if action.push.ethertype != 0x8100:
161 self.log.error('unhandled-tpid',
162 ethertype=action.push.ethertype)
163 elif action.type == fd.SET_FIELD:
164 # action_info['action_type'] = 'set_field'
165 _field = action.set_field.field.ofb_field
166 assert (action.set_field.field.oxm_class ==
167 OFPXMC_OPENFLOW_BASIC)
168 self.log.debug('action-type-set-field',
169 field=_field, in_port=classifier_info[IN_PORT])
170 if _field.type == fd.VLAN_VID:
171 self.log.debug('set-field-type-vlan-vid',
172 vlan_vid=_field.vlan_vid & 0xfff)
173 action_info[VLAN_VID] = (_field.vlan_vid & 0xfff)
174 else:
175 self.log.error('unsupported-action-set-field-type',
176 field_type=_field.type)
177 else:
178 self.log.error('unsupported-action-type',
179 action_type=action.type, in_port=classifier_info[IN_PORT])
180
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400181 # controller bound trap flows.
182 if self.platform.is_controller(action_info[OUTPUT]):
183 # trap from pon port. figure out uni port from tunnel id and set as in port
184 if self.platform.intf_id_to_port_type_name(classifier_info[IN_PORT]) == Port.PON_OLT:
185 if fd.get_child_port_from_tunnelid(flow) is not None:
186 classifier_info[IN_PORT] = fd.get_child_port_from_tunnelid(flow)
187 self.log.debug('pon-to-controller-flow-port-in-tunnelid', new_in_port=classifier_info[IN_PORT])
188 else:
189 self.log.debug('pon-to-controller-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
190 out_port=action_info[OUTPUT])
191 # TODO NEW CORE: trap from nni port.
192 else:
193 self.log.warn('nni-to-controller-trap-unsupported', flow=flow)
194 else:
195 # do not operate on the private decomposer vlan. neither onu nor agg switch adds it.
196 # cannot do anything with this flow
197 if VLAN_VID in classifier_info and classifier_info[VLAN_VID] == 4000:
198 self.log.debug('skipping-private-vlan', in_port=classifier_info[IN_PORT],
199 out_port=action_info[OUTPUT])
William Kurkian6f436d02019-02-06 16:25:01 -0500200 return
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400201
202 # downstream nni port to pon port. figure out uni port from tunnel id and set as out port
203 if self.platform.intf_id_to_port_type_name(action_info[OUTPUT]) == Port.PON_OLT:
204 if fd.get_child_port_from_tunnelid(flow) is not None:
205 action_info[OUTPUT] = fd.get_child_port_from_tunnelid(flow)
206 self.log.debug('downstream-pon-port-flow-port-in-tunnelid', new_out_port=action_info[OUTPUT])
207 else:
208 self.log.debug('downstream-pon-port-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
209 out_port=action_info[OUTPUT])
210 return
211
212 # upstream pon port to nni port. figure out uni port from tunnel id and set as in port
213 if self.platform.intf_id_to_port_type_name(classifier_info[IN_PORT]) == Port.PON_OLT:
214 if fd.get_child_port_from_tunnelid(flow) is not None:
215 classifier_info[IN_PORT] = fd.get_child_port_from_tunnelid(flow)
216 self.log.debug('upstream-pon-port-flow-port-in-tunnelid', new_in_port=classifier_info[IN_PORT])
217 else:
218 self.log.debug('upstream-pon-port-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
219 out_port=action_info[OUTPUT])
220 return
William Kurkian6f436d02019-02-06 16:25:01 -0500221
222 self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT])
223 (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow(
224 classifier_info[IN_PORT], action_info[OUTPUT])
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400225
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400226 self.log.debug('extracted-flow-ports', port_no=port_no, intf_id=intf_id, onu_id=onu_id, uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500227
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400228 # TODO NEW CORE: this needs to be broken into onu type flows that need these ID, and NNI flows that do not
William Kurkian6f436d02019-02-06 16:25:01 -0500229 self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info,
William Kurkian23047b92019-05-01 11:02:35 -0400230 action_info, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500231
232 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
233 uni_port_no = None
234 child_device_id = None
235 if flow_direction == UPSTREAM:
236 for field in fd.get_ofb_fields(flow):
237 if field.type == fd.IN_PORT:
238 is_uni, child_device_id = self._is_uni_port(field.port)
239 if is_uni:
240 uni_port_no = field.port
241 elif flow_direction == DOWNSTREAM:
242 for field in fd.get_ofb_fields(flow):
243 if field.type == fd.METADATA:
244 uni_port = field.table_metadata & 0xFFFFFFFF
245 is_uni, child_device_id = self._is_uni_port(uni_port)
246 if is_uni:
247 uni_port_no = field.port
248
249 if uni_port_no is None:
250 for action in fd.get_actions(flow):
251 if action.type == fd.OUTPUT:
252 is_uni, child_device_id = \
253 self._is_uni_port(action.output.port)
254 if is_uni:
255 uni_port_no = action.output.port
256
257 if child_device_id:
258 child_device = self.adapter_agent.get_device(child_device_id)
259 pon_intf = child_device.proxy_address.channel_id
260 onu_id = child_device.proxy_address.onu_id
261 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
262 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
263 assert (isinstance(flows, list))
264 self.log.debug("retrieved-flows", flows=flows)
265 for idx in range(len(flows)):
266 if flow_direction == flows[idx]['flow_type']:
267 flows.pop(idx)
268 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
269 if len(flows) > 0:
270 # There are still flows referencing the same flow_id.
271 # So the flow should not be freed yet.
272 # For ex: Case of HSIA where same flow is shared
273 # between DS and US.
274 return
275
276 self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id)
277 else:
278 self.log.error("invalid-info", uni_port_no=uni_port_no,
279 child_device_id=child_device_id)
280
281 def retry_add_flow(self, flow):
282 self.log.debug("retry-add-flow")
283 if flow.id in self.retry_add_flow_list:
284 self.retry_add_flow_list.remove(flow.id)
285 self.add_flow(flow)
286
287 def remove_flow(self, flow):
288 self.log.debug('trying to remove flows from logical flow :',
289 logical_flow=flow)
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400290
291 # TODO NEW CORE: Keep track of device flows locally. need new array
William Kurkian6f436d02019-02-06 16:25:01 -0500292 device_flows_to_remove = []
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400293 #device_flows = self.flows_proxy.get('/').items
294 device_flows = []
William Kurkian6f436d02019-02-06 16:25:01 -0500295 for f in device_flows:
296 if f.cookie == flow.id:
297 device_flows_to_remove.append(f)
298
299 for f in device_flows_to_remove:
300 (id, direction) = self.decode_stored_id(f.id)
301 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
302 try:
303 self.stub.FlowRemove(flow_to_remove)
304 except grpc.RpcError as grpc_e:
305 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
306 self.log.debug('This flow does not exist on the switch, '
307 'normal after an OLT reboot',
308 flow=flow_to_remove)
309 else:
310 raise grpc_e
311
312 # once we have successfully deleted the flow on the device
313 # release the flow_id on resource pool and also clear any
314 # data associated with the flow_id on KV store.
315 self._clear_flow_id_from_rm(f, id, direction)
316 self.log.debug('flow removed from device', flow=f,
317 flow_key=flow_to_remove)
318
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400319 def get_tp_path(self, intf_id, uni):
William Kurkian6f436d02019-02-06 16:25:01 -0500320 # FIXME Should get Table id form the flow, as of now hardcoded to
321 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
322 # 'tp_path' contains the suffix part of the tech_profile_instance path.
323 # The prefix to the 'tp_path' should be set to \
324 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
325 return self.tech_profile[intf_id]. \
326 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400327 uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500328
329 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id):
330 # Remove the TP instance associated with the ONU
331 ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id)
332 tp_path = self.get_tp_path(intf_id, ofp_port_name)
333 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
334
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400335 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500336 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
337 action, flow):
338
339 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no,
340 classifier=classifier, action=action)
341
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400342 uni = self.get_uni_port_path(intf_id, onu_id, uni_id)
343
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400344 # TODO: if there is no onu_id then the flows pushed are NNI based and belows flow pushes need to be refactored
345 if (onu_id > 0):
346 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
347 uni, port_no, flow.table_id)
348 if alloc_id is None or gem_ports is None:
349 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id, gem_ports=gem_ports)
350 return
William Kurkian6f436d02019-02-06 16:25:01 -0500351
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400352 self.log.debug('Generated required alloc and gemport ids', alloc_id=alloc_id, gemports=gem_ports)
353 else:
354 alloc_id = -1
355 gem_ports = []
356 self.log.error('cannot-generate-alloc-gem-id-for-flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500357
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400358 # TODO: if there are no usable onu, uni, alloc, or gem id, then the below flows are erroneous. this needs a refactor
William Kurkian6f436d02019-02-06 16:25:01 -0500359 # Flows can't be added specific to gemport unless p-bits are received.
360 # Hence adding flows for all gemports
361 for gemport_id in gem_ports:
362 if IP_PROTO in classifier:
363 if classifier[IP_PROTO] == 17:
364 self.log.debug('dhcp flow add')
365 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
366 action, flow, alloc_id, gemport_id)
367 elif classifier[IP_PROTO] == 2:
368 self.log.warn('igmp flow add ignored, not implemented yet')
369 else:
370 self.log.warn("Invalid-Classifier-to-handle",
371 classifier=classifier,
372 action=action)
373 elif ETH_TYPE in classifier:
374 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
375 self.log.debug('eapol flow add')
376 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
377 gemport_id)
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400378
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400379 vlan_id = yield self.get_subscriber_vlan(fd.get_child_port_from_tunnelid(flow))
380 if vlan_id is not None:
381 self.log.debug('adding-supplimental-eap-flow-vlan', vlan_id=vlan_id)
382 self.add_eapol_flow(
383 intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
384 vlan_id=vlan_id)
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400385 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
Matt Jeanneretaa360912019-04-22 16:23:12 -0400386
387 self.log.debug('get-child-device', intf_id=intf_id, onu_id=onu_id,
388 parent_port_no=parent_port_no, device_id=self.device_id)
389
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400390 onu_device = yield self.core_proxy.get_child_device(self.device_id,
Matt Jeanneretaa360912019-04-22 16:23:12 -0400391 onu_id=int(onu_id),
392 parent_port_no=int(parent_port_no))
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400393 tp_path = self.get_tp_path(intf_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500394
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400395 tech_msg = InterAdapterTechProfileDownloadMessage(uni_id=uni_id, path=tp_path)
William Kurkian6f436d02019-02-06 16:25:01 -0500396
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400397 self.log.debug('Load-tech-profile-request-to-brcm-handler',
398 onu_device=onu_device, tp_path=tp_path, tech_msg=tech_msg)
399
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400400 # Send the tech profile event to the onu adapter
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400401 yield self.adapter_proxy.send_inter_adapter_message(
402 msg=tech_msg,
403 type=InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST,
404 from_adapter="openolt",
405 to_adapter=onu_device.type,
406 to_device_id=onu_device.id
407 )
William Kurkian6f436d02019-02-06 16:25:01 -0500408
409 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
410 self.log.debug('lldp flow add')
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400411 nni_intf_id = yield self.get_nni_intf_id()
William Kurkian6f436d02019-02-06 16:25:01 -0500412 self.add_lldp_flow(flow, port_no, nni_intf_id)
413
414 elif PUSH_VLAN in action:
415 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
416 action, flow, alloc_id, gemport_id)
417 elif POP_VLAN in action:
418 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
419 action, flow, alloc_id, gemport_id)
420 else:
421 self.log.debug('Invalid-flow-type-to-handle',
422 classifier=classifier,
423 action=action, flow=flow)
424
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400425 def get_uni_port_path(self, intf_id, onu_id, uni_id):
426 value = 'pon-{}/onu-{}/uni-{}'.format(intf_id, onu_id, uni_id)
427 return value
428
429 def create_tcont_gemport(self, intf_id, onu_id, uni_id, uni, port_no, table_id):
William Kurkian6f436d02019-02-06 16:25:01 -0500430 alloc_id, gem_port_ids = None, None
431 pon_intf_onu_id = (intf_id, onu_id)
432
433 # If we already have allocated alloc_id and gem_ports earlier, render them
434 alloc_id = \
435 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
436 gem_port_ids = \
437 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
438 if alloc_id is not None and gem_port_ids is not None:
439 return alloc_id, gem_port_ids
440
441 try:
William Kurkian6f436d02019-02-06 16:25:01 -0500442 # FIXME: If table id is <= 63 using 64 as table id
443 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
444 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
445
446 # Check tech profile instance already exists for derived port name
447 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400448 get_tech_profile_instance(table_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500449 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
450
451 if tech_profile_instance is None:
452 # create tech profile instance
453 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400454 create_tech_profile_instance(table_id, uni,
William Kurkian6f436d02019-02-06 16:25:01 -0500455 intf_id)
456 if tech_profile_instance is None:
457 raise Exception('Tech-profile-instance-creation-failed')
458 else:
459 self.log.debug(
460 'Tech-profile-instance-already-exist-for-given port-name',
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400461 table_id=table_id, intf_id=intf_id, uni=uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500462
463 # upstream scheduler
464 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
465 tech_profile_instance)
466 # downstream scheduler
467 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
468 tech_profile_instance)
469 # create Tcont
470 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
471 us_scheduler,
472 ds_scheduler)
473
474 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
475 onu_id=onu_id,
476 uni_id=uni_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400477 port_no=port_no,
William Kurkian6f436d02019-02-06 16:25:01 -0500478 tconts=tconts))
479
480 # Fetch alloc id and gemports from tech profile instance
481 alloc_id = tech_profile_instance.us_scheduler.alloc_id
482 gem_port_ids = []
483 for i in range(len(
484 tech_profile_instance.upstream_gem_port_attribute_list)):
485 gem_port_ids.append(
486 tech_profile_instance.upstream_gem_port_attribute_list[i].
487 gemport_id)
488 except BaseException as e:
489 self.log.exception(exception=e)
490
491 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
492 pon_intf_onu_id = (intf_id, onu_id, uni_id)
493 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
494 pon_intf_onu_id,
495 list([alloc_id])
496 )
497 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
498 pon_intf_onu_id,
499 gem_port_ids
500 )
501
502 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
503 gem_port_ids, intf_id, onu_id, uni_id
504 )
505
506 return alloc_id, gem_port_ids
507
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400508 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500509 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
510 uplink_action, logical_flow, alloc_id,
511 gemport_id):
512
513 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
514
515 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
516 uplink_action, UPSTREAM,
517 logical_flow, alloc_id, gemport_id)
518
519 # Secondary EAP on the subscriber vlan
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400520 eap_logical_flow = yield self.is_eap_enabled(intf_id, onu_id, uni_id)
521 if eap_logical_flow is not None:
522 self.log.debug('adding-supplimental-eap-flow', flow=eap_logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500523 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
524 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
525
526 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
527 downlink_action, flow, alloc_id, gemport_id):
528 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
529 # Needed ???? It should be already there
530 downlink_action[POP_VLAN] = True
531 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
532
533 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
534 downlink_action, DOWNSTREAM,
535 flow, alloc_id, gemport_id)
536
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400537 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500538 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
539 direction, logical_flow, alloc_id, gemport_id):
540
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400541 self.log.debug('add hisa flow', flow=logical_flow, port_no=port_no,
542 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
543 alloc_id=alloc_id)
544
William Kurkian6f436d02019-02-06 16:25:01 -0500545 flow_store_cookie = self._get_flow_store_cookie(classifier,
546 gemport_id)
547
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400548 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
549 action=action)
550
William Kurkian6f436d02019-02-06 16:25:01 -0500551 # One of the OLT platform (Broadcom BAL) requires that symmetric
552 # flows require the same flow_id to be used across UL and DL.
553 # Since HSIA flow is the only symmetric flow currently, we need to
554 # re-use the flow_id across both direction. The 'flow_category'
555 # takes priority over flow_cookie to find any available HSIA_FLOW
556 # id for the ONU.
557 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
558 flow_store_cookie,
559 HSIA_FLOW)
560 if flow_id is None:
561 self.log.error("hsia-flow-unavailable")
562 return
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400563
564 self.log.debug('flow-id', flow_id=flow_id)
565
566 network_intf_id = yield self.get_nni_intf_id()
567
William Kurkian6f436d02019-02-06 16:25:01 -0500568 flow = openolt_pb2.Flow(
569 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400570 flow_type=direction, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500571 gemport_id=gemport_id,
572 classifier=self.mk_classifier(classifier),
573 action=self.mk_action(action),
574 priority=logical_flow.priority,
575 port_no=port_no,
576 cookie=logical_flow.cookie)
577
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400578 self.log.debug('openolt-agent-flow', hsia_flow=flow)
579
William Kurkian6f436d02019-02-06 16:25:01 -0500580 if self.add_flow_to_device(flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400581 self.log.debug('added-hsia-openolt-agent-flow', hsia_flow=flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500582 flow_info = self._get_flow_info_as_json_blob(flow,
583 flow_store_cookie,
584 HSIA_FLOW)
585 self.update_flow_info_to_kv_store(flow.access_intf_id,
586 flow.onu_id, flow.uni_id,
587 flow.flow_id, flow_info)
588
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400589 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500590 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
591 alloc_id, gemport_id):
592
593 self.log.debug('add dhcp upstream trap', classifier=classifier,
594 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
595
596 action.clear()
597 action[TRAP_TO_HOST] = True
598 classifier[UDP_SRC] = 68
599 classifier[UDP_DST] = 67
600 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
601 classifier.pop(VLAN_VID, None)
602
603 flow_store_cookie = self._get_flow_store_cookie(classifier,
604 gemport_id)
605
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400606 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
607 action=action)
608
William Kurkian6f436d02019-02-06 16:25:01 -0500609 flow_id = self.resource_mgr.get_flow_id(
610 intf_id, onu_id, uni_id, flow_store_cookie
611 )
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400612
613 self.log.debug('flow-id', flow_id=flow_id)
614
615 network_intf_id = yield self.get_nni_intf_id()
616
William Kurkian6f436d02019-02-06 16:25:01 -0500617 dhcp_flow = openolt_pb2.Flow(
618 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
619 access_intf_id=intf_id, gemport_id=gemport_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400620 alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500621 priority=logical_flow.priority,
622 classifier=self.mk_classifier(classifier),
623 action=self.mk_action(action),
624 port_no=port_no,
625 cookie=logical_flow.cookie)
626
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400627 self.log.debug('openolt-agent-flow', dhcp_flow=dhcp_flow)
628
William Kurkian6f436d02019-02-06 16:25:01 -0500629 if self.add_flow_to_device(dhcp_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400630 self.log.debug('added-dhcp-openolt-agent-flow', dhcp_flow=dhcp_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500631 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
632 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
633 dhcp_flow.onu_id,
634 dhcp_flow.uni_id,
635 dhcp_flow.flow_id,
636 flow_info)
637
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400638 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500639 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
640 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
641
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400642 self.log.debug('add eapol upstream trap', flow=logical_flow, port_no=port_no,
643 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
644 alloc_id=alloc_id, vlan_id=vlan_id)
645
William Kurkian6f436d02019-02-06 16:25:01 -0500646 uplink_classifier = dict()
647 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
648 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
649 uplink_classifier[VLAN_VID] = vlan_id
650
651 uplink_action = dict()
652 uplink_action[TRAP_TO_HOST] = True
653
654 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
655 gemport_id)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400656
657 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, uplink_classifier=uplink_classifier,
658 uplink_action=uplink_action)
659
William Kurkian6f436d02019-02-06 16:25:01 -0500660 # Add Upstream EAPOL Flow.
661 uplink_flow_id = self.resource_mgr.get_flow_id(
662 intf_id, onu_id, uni_id, flow_store_cookie
663 )
664
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400665 self.log.debug('flow-id', uplink_flow_id=uplink_flow_id)
666
667 network_intf_id = yield self.get_nni_intf_id()
668
William Kurkian6f436d02019-02-06 16:25:01 -0500669 upstream_flow = openolt_pb2.Flow(
670 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400671 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500672 gemport_id=gemport_id,
673 classifier=self.mk_classifier(uplink_classifier),
674 action=self.mk_action(uplink_action),
675 priority=logical_flow.priority,
676 port_no=port_no,
677 cookie=logical_flow.cookie)
678
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400679 self.log.debug('openolt-agent-flow', upstream_flow=upstream_flow)
680
William Kurkian6f436d02019-02-06 16:25:01 -0500681 logical_flow = copy.deepcopy(logical_flow)
682 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
683 vlan_id | 0x1000)]))
684 logical_flow.match.type = OFPMT_OXM
685
686 if self.add_flow_to_device(upstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400687 self.log.debug('added-eapol-openolt-agent-flow', upstream_flow=upstream_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500688 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
689 flow_store_cookie)
690 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
691 upstream_flow.onu_id,
692 upstream_flow.uni_id,
693 upstream_flow.flow_id,
694 flow_info)
695
696 if vlan_id == DEFAULT_MGMT_VLAN:
697 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
698 # requirement)
699 # On one of the platforms (Broadcom BAL), when same DL classifier
700 # vlan was used across multiple ONUs, eapol flow re-adds after
701 # flow delete (cases of onu reboot/disable) fails.
702 # In order to generate unique vlan, a combination of intf_id
703 # onu_id and uni_id is used.
704 # uni_id defaults to 0, so add 1 to it.
705 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
706 # Assert that we do not generate invalid vlans under no condition
Matt Jeanneretaa360912019-04-22 16:23:12 -0400707 assert (special_vlan_downstream_flow >= 2), 'invalid-vlan-generated'
708 self.log.warn('generating-special-downstream-vlan-for-bal', special_vlan_downstream_flow=special_vlan_downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500709
710 downlink_classifier = dict()
711 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
712 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
713
714 downlink_action = dict()
715 downlink_action[PUSH_VLAN] = True
716 downlink_action[VLAN_VID] = vlan_id
717
718
719 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
720 gemport_id)
721
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400722 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, downlink_classifier=downlink_classifier,
723 downlink_action=downlink_action)
724
William Kurkian6f436d02019-02-06 16:25:01 -0500725 downlink_flow_id = self.resource_mgr.get_flow_id(
726 intf_id, onu_id, uni_id, flow_store_cookie
727 )
728
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400729 self.log.debug('flow-id', downlink_flow_id=downlink_flow_id)
730
William Kurkian6f436d02019-02-06 16:25:01 -0500731 downstream_flow = openolt_pb2.Flow(
732 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400733 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500734 gemport_id=gemport_id,
735 classifier=self.mk_classifier(downlink_classifier),
736 action=self.mk_action(downlink_action),
737 priority=logical_flow.priority,
738 port_no=port_no,
739 cookie=logical_flow.cookie)
740
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400741 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
742
Matt Jeanneretaa360912019-04-22 16:23:12 -0400743 try:
744 downstream_logical_flow = ofp_flow_stats(
745 id=logical_flow.id, cookie=logical_flow.cookie,
746 table_id=logical_flow.table_id, priority=logical_flow.priority,
747 flags=logical_flow.flags)
William Kurkian6f436d02019-02-06 16:25:01 -0500748
Matt Jeanneretaa360912019-04-22 16:23:12 -0400749 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
750 fd.in_port(fd.get_out_port(logical_flow)),
751 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
752 downstream_logical_flow.match.type = OFPMT_OXM
William Kurkian6f436d02019-02-06 16:25:01 -0500753
Matt Jeanneretaa360912019-04-22 16:23:12 -0400754 downstream_logical_flow.instructions.extend(
755 fd.mk_instructions_from_actions([fd.output(
756 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
757 except Exception as e:
758 self.log.exception("unexpected-error-building-downstream-logical-flow", intf_id=intf_id, onu_id=onu_id,
759 uni_id=uni_id, e=e, downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500760
761 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400762 self.log.debug('added-eapol-openolt-agent-flow', downstream_flow=downstream_flow,
763 downstream_logical_flow=downstream_logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500764 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
765 flow_store_cookie)
766 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
767 downstream_flow.onu_id,
768 downstream_flow.uni_id,
769 downstream_flow.flow_id,
770 flow_info)
771
772 def repush_all_different_flows(self):
773 # Check if the device is supposed to have flows, if so add them
774 # Recover static flows after a reboot
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400775 # TODO NEW CORE: Keep track of device flows locally. need new array
776 #logical_flows = self.logical_flows_proxy.get('/').items
777 #devices_flows = self.flows_proxy.get('/').items
778 logical_flows = []
779 devices_flows = []
William Kurkian6f436d02019-02-06 16:25:01 -0500780 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
781 for logical_flow in logical_flows:
782 try:
783 if logical_flow.id not in logical_flows_ids_provisioned:
784 self.add_flow(logical_flow)
785 except Exception as e:
786 self.log.exception('Problem reading this flow', e=e)
787
788 def reset_flows(self):
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400789 # TODO NEW CORE: Keep track of device flows locally. need new array. here clear them out
790 #self.flows_proxy.update('/', Flows())
791 pass
William Kurkian6f436d02019-02-06 16:25:01 -0500792
793 """ Add a downstream LLDP trap flow on the NNI interface
794 """
795
796 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
797
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400798 self.log.debug('add lldp trap flow', flow=logical_flow, port_no=port_no,
799 network_intf_id=network_intf_id)
800
William Kurkian6f436d02019-02-06 16:25:01 -0500801 classifier = dict()
802 classifier[ETH_TYPE] = LLDP_ETH_TYPE
803 classifier[PACKET_TAG_TYPE] = UNTAGGED
804 action = dict()
805 action[TRAP_TO_HOST] = True
806
807 # LLDP flow is installed to trap LLDP packets on the NNI port.
808 # We manage flow_id resource pool on per PON port basis.
809 # Since this situation is tricky, as a hack, we pass the NNI port
810 # index (network_intf_id) as PON port Index for the flow_id resource
811 # pool. Also, there is no ONU Id available for trapping LLDP packets
812 # on NNI port, use onu_id as -1 (invalid)
813 # ****************** CAVEAT *******************
814 # This logic works if the NNI Port Id falls within the same valid
815 # range of PON Port Ids. If this doesn't work for some OLT Vendor
816 # we need to have a re-look at this.
817 # *********************************************
818 onu_id = -1
819 uni_id = -1
820 flow_store_cookie = self._get_flow_store_cookie(classifier)
821 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
822 flow_store_cookie)
823
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400824 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
825 action=action)
826
William Kurkian6f436d02019-02-06 16:25:01 -0500827 downstream_flow = openolt_pb2.Flow(
828 access_intf_id=-1, # access_intf_id not required
829 onu_id=onu_id, # onu_id not required
830 uni_id=uni_id, # uni_id not used
831 flow_id=flow_id,
832 flow_type=DOWNSTREAM,
833 network_intf_id=network_intf_id,
834 gemport_id=-1, # gemport_id not required
835 classifier=self.mk_classifier(classifier),
836 action=self.mk_action(action),
837 priority=logical_flow.priority,
838 port_no=port_no,
839 cookie=logical_flow.cookie)
840
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400841 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
842
William Kurkian6f436d02019-02-06 16:25:01 -0500843 if self.add_flow_to_device(downstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400844 self.log.debug('added-lldp-openolt-agent-flow', downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500845 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
846 flow_id, downstream_flow)
847
848 def mk_classifier(self, classifier_info):
849
850 classifier = openolt_pb2.Classifier()
851
852 if ETH_TYPE in classifier_info:
853 classifier.eth_type = classifier_info[ETH_TYPE]
854 if IP_PROTO in classifier_info:
855 classifier.ip_proto = classifier_info[IP_PROTO]
856 if VLAN_VID in classifier_info:
857 classifier.o_vid = classifier_info[VLAN_VID]
858 if METADATA in classifier_info:
859 classifier.i_vid = classifier_info[METADATA]
860 if VLAN_PCP in classifier_info:
861 classifier.o_pbits = classifier_info[VLAN_PCP]
862 if UDP_SRC in classifier_info:
863 classifier.src_port = classifier_info[UDP_SRC]
864 if UDP_DST in classifier_info:
865 classifier.dst_port = classifier_info[UDP_DST]
866 if IPV4_DST in classifier_info:
867 classifier.dst_ip = classifier_info[IPV4_DST]
868 if IPV4_SRC in classifier_info:
869 classifier.src_ip = classifier_info[IPV4_SRC]
870 if PACKET_TAG_TYPE in classifier_info:
871 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
872 classifier.pkt_tag_type = SINGLE_TAG
873 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
874 classifier.pkt_tag_type = DOUBLE_TAG
875 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
876 classifier.pkt_tag_type = UNTAGGED
877 else:
878 classifier.pkt_tag_type = 'none'
879
880 return classifier
881
882 def mk_action(self, action_info):
883 action = openolt_pb2.Action()
884
885 if POP_VLAN in action_info:
886 action.o_vid = action_info[VLAN_VID]
887 action.cmd.remove_outer_tag = True
888 elif PUSH_VLAN in action_info:
889 action.o_vid = action_info[VLAN_VID]
890 action.cmd.add_outer_tag = True
891 elif TRAP_TO_HOST in action_info:
892 action.cmd.trap_to_host = True
893 else:
894 self.log.info('Invalid-action-field', action_info=action_info)
895 return
896 return action
897
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400898 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500899 def is_eap_enabled(self, intf_id, onu_id, uni_id):
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400900 self.log.debug('looking from eap enabled for port', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id)
901
902 # TODO NEW CORE: This is really spendy, likely perfomance implications
903 device = yield self.core_proxy.get_device(self.device_id)
904 flows = device.flows.items
William Kurkian6f436d02019-02-06 16:25:01 -0500905
906 for flow in flows:
907 eap_flow = False
908 eap_intf_id = None
909 eap_onu_id = None
910 eap_uni_id = None
911 for field in fd.get_ofb_fields(flow):
912 if field.type == fd.ETH_TYPE:
913 if field.eth_type == EAP_ETH_TYPE:
914 eap_flow = True
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400915 if field.type == fd.TUNNEL_ID:
916 port = fd.get_child_port_from_tunnelid(flow)
917 eap_intf_id = self.platform.intf_id_from_uni_port_num(port)
918 eap_onu_id = self.platform.onu_id_from_port_num(port)
919 eap_uni_id = self.platform.uni_id_from_port_num(port)
William Kurkian6f436d02019-02-06 16:25:01 -0500920
921 if eap_flow:
922 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
923 intf_id=intf_id, eap_intf_id=eap_intf_id,
924 eap_onu_id=eap_onu_id,
925 eap_uni_id=eap_uni_id)
926 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id:
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400927 returnValue(flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500928
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400929 returnValue(None)
William Kurkian6f436d02019-02-06 16:25:01 -0500930
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400931 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500932 def get_subscriber_vlan(self, port):
933 self.log.debug('looking from subscriber flow for port', port=port)
934
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400935 # TODO NEW CORE: This is really spendy, likely perfomance implications
936 device = yield self.core_proxy.get_device(self.device_id)
937 flows = device.flows.items
938
William Kurkian6f436d02019-02-06 16:25:01 -0500939 for flow in flows:
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400940 in_port = fd.get_child_port_from_tunnelid(flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500941 out_port = fd.get_out_port(flow)
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400942 self.log.debug('looping-flows', in_port=in_port, out_port=out_port)
943
944 if self.platform.is_controller(out_port):
945 self.log.debug('skipping-controller-flow', in_port=in_port, out_port=out_port)
946 continue
947
William Kurkian6f436d02019-02-06 16:25:01 -0500948 if in_port == port and out_port is not None and \
949 self.platform.intf_id_to_port_type_name(out_port) \
950 == Port.ETHERNET_NNI:
951 fields = fd.get_ofb_fields(flow)
952 self.log.debug('subscriber flow found', fields=fields)
953 for field in fields:
954 if field.type == OFPXMT_OFB_VLAN_VID:
955 self.log.debug('subscriber vlan found',
956 vlan_id=field.vlan_vid)
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400957 returnValue(field.vlan_vid & 0x0fff)
William Kurkian6f436d02019-02-06 16:25:01 -0500958 self.log.debug('No subscriber flow found', port=port)
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400959 returnValue(None)
William Kurkian6f436d02019-02-06 16:25:01 -0500960
961 def add_flow_to_device(self, flow, logical_flow):
962 self.log.debug('pushing flow to device', flow=flow)
963 try:
964 self.stub.FlowAdd(flow)
965 except grpc.RpcError as grpc_e:
966 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
967 self.log.warn('flow already exists', e=grpc_e, flow=flow)
968 else:
969 self.log.error('failed to add flow',
970 logical_flow=logical_flow, flow=flow,
971 grpc_error=grpc_e)
972 return False
Matt Jeanneretaa360912019-04-22 16:23:12 -0400973 except Exception as f:
974 self.log.exception("unexpected-openolt-agent-error", flow=flow, logical_flow=logical_flow, f=f)
William Kurkian6f436d02019-02-06 16:25:01 -0500975 else:
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400976 self.register_flow(logical_flow, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500977 return True
978
979 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400980 self.log.debug("update-flow-info", intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow=flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500981 self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id,
982 flow_id, flow)
983
984 def register_flow(self, logical_flow, device_flow):
985 self.log.debug('registering flow in device',
986 logical_flow=logical_flow, device_flow=device_flow)
987 stored_flow = copy.deepcopy(logical_flow)
988 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
989 device_flow.flow_type)
990 self.log.debug('generated device flow id', id=stored_flow.id,
991 flow_id=device_flow.flow_id,
992 direction=device_flow.flow_type)
993 stored_flow.cookie = logical_flow.id
William Kurkian6f436d02019-02-06 16:25:01 -0500994
Matt Jeanneret6fe5f732019-05-03 16:52:49 -0400995 # TODO NEW CORE: Keep track of device flows locally. need new array
996 #flows = self.flows_proxy.get('/')
997 #flows.items.extend([stored_flow])
998 #self.flows_proxy.update('/', flows)
William Kurkian6f436d02019-02-06 16:25:01 -0500999
1000 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
1001 ofp_port_name = logical_port.ofp_port.name
1002 port_no = logical_port.ofp_port.port_no
1003 pon_port = child_device.proxy_address.channel_id
1004 onu_id = child_device.proxy_address.onu_id
1005 uni_id = self.platform.uni_id_from_port_num(logical_port)
1006
1007 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
1008 # is probably to maintain a list of Tech-profile table IDs associated
1009 # with the UNI logical_port. This way, when the logical port is deleted,
1010 # all the associated tech-profile configuration with the UNI logical_port
1011 # can be cleared.
1012 tech_profile_instance = self.tech_profile[pon_port]. \
1013 get_tech_profile_instance(
1014 DEFAULT_TECH_PROFILE_TABLE_ID,
1015 ofp_port_name)
1016 flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id)
1017 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
1018 for flow_id in flow_ids:
1019 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
1020 for flow_info in flow_infos:
1021 direction = flow_info['flow_type']
1022 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
1023 flow_type=direction)
1024 try:
1025 self.stub.FlowRemove(flow_to_remove)
1026 except grpc.RpcError as grpc_e:
1027 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
1028 self.log.debug('This flow does not exist on the switch, '
1029 'normal after an OLT reboot',
1030 flow=flow_to_remove)
1031 else:
1032 raise grpc_e
1033
1034 self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id)
1035
1036 try:
1037 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
1038 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
1039 onu_id=onu_id,
1040 uni_id=uni_id,
1041 port_no=port_no,
1042 tconts=tconts))
1043 except grpc.RpcError as grpc_e:
1044 self.log.error('error-removing-tcont-scheduler-queues',
1045 err=grpc_e)
1046
1047 def generate_stored_id(self, flow_id, direction):
1048 if direction == UPSTREAM:
1049 self.log.debug('upstream flow, shifting id')
1050 return 0x1 << 15 | flow_id
1051 elif direction == DOWNSTREAM:
1052 self.log.debug('downstream flow, not shifting id')
1053 return flow_id
1054 else:
1055 self.log.warn('Unrecognized direction', direction=direction)
1056 return flow_id
1057
1058 def decode_stored_id(self, id):
1059 if id >> 15 == 0x1:
1060 return id & 0x7fff, UPSTREAM
1061 else:
1062 return id, DOWNSTREAM
1063
1064 def _populate_tech_profile_per_pon_port(self):
1065 for arange in self.resource_mgr.device_info.ranges:
1066 for intf_id in arange.intf_ids:
1067 self.tech_profile[intf_id] = \
1068 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1069
1070 # Make sure we have as many tech_profiles as there are pon ports on
1071 # the device
1072 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1073
1074 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1075 flow_category=None):
1076 json_blob = MessageToDict(message=flow,
1077 preserving_proto_field_name=True)
1078 self.log.debug("flow-info", json_blob=json_blob)
1079 json_blob['flow_store_cookie'] = flow_store_cookie
1080 if flow_category is not None:
1081 json_blob['flow_category'] = flow_category
1082 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1083 flow.onu_id, flow.uni_id, flow.flow_id)
1084
1085 if flow_info is None:
1086 flow_info = list()
1087 flow_info.append(json_blob)
1088 else:
1089 assert (isinstance(flow_info, list))
1090 flow_info.append(json_blob)
1091
1092 return flow_info
1093
1094 @staticmethod
1095 def _get_flow_store_cookie(classifier, gem_port=None):
1096 assert isinstance(classifier, dict)
1097 # We need unique flows per gem_port
1098 if gem_port is not None:
1099 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1100 else:
1101 to_hash = dumps(classifier, sort_keys=True)
1102 return hashlib.md5(to_hash).hexdigest()[:12]
1103
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001104 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001105 def get_nni_intf_id(self):
1106 if self.nni_intf_id is not None:
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001107 returnValue(self.nni_intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001108
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001109 port_list = yield self.core_proxy.get_ports(self.device_id, Port.ETHERNET_NNI)
1110 self.log.debug("nni-ports-list", port_list=port_list)
1111
1112 # TODO: Hardcoded only first NNI
1113 port = port_list.items[0]
1114
1115 self.log.debug("nni-port", port=port)
1116 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(port.port_no)
1117
1118 self.log.debug("nni-intf-d ", port=port.port_no, nni_intf_id=self.nni_intf_id)
1119 returnValue(self.nni_intf_id)