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