blob: e298db228eaf4570d8055a3e57f11ac9df565f18 [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
22
23from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
24 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
25 OFPXMT_OFB_VLAN_VID
26from voltha.protos.device_pb2 import Port
27import voltha.core.flow_decomposer as fd
28from voltha.adapters.openolt.protos import openolt_pb2
29from voltha.registry import registry
30
31from common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID
32
33# Flow categories
34HSIA_FLOW = "HSIA_FLOW"
35
36EAP_ETH_TYPE = 0x888e
37LLDP_ETH_TYPE = 0x88cc
38
39IGMP_PROTO = 2
40
41# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
42DEFAULT_MGMT_VLAN = 4091
43
44# Openolt Flow
45UPSTREAM = "upstream"
46DOWNSTREAM = "downstream"
47PACKET_TAG_TYPE = "pkt_tag_type"
48UNTAGGED = "untagged"
49SINGLE_TAG = "single_tag"
50DOUBLE_TAG = "double_tag"
51
52# Classifier
53ETH_TYPE = 'eth_type'
54TPID = 'tpid'
55IP_PROTO = 'ip_proto'
56IN_PORT = 'in_port'
57VLAN_VID = 'vlan_vid'
58VLAN_PCP = 'vlan_pcp'
59UDP_DST = 'udp_dst'
60UDP_SRC = 'udp_src'
61IPV4_DST = 'ipv4_dst'
62IPV4_SRC = 'ipv4_src'
63METADATA = 'metadata'
64OUTPUT = 'output'
65# Action
66POP_VLAN = 'pop_vlan'
67PUSH_VLAN = 'push_vlan'
68TRAP_TO_HOST = 'trap_to_host'
69
70
71class OpenOltFlowMgr(object):
72
73 def __init__(self, adapter_agent, log, stub, device_id, logical_device_id,
74 platform, resource_mgr):
75 self.adapter_agent = adapter_agent
76 self.log = log
77 self.stub = stub
78 self.device_id = device_id
79 self.logical_device_id = logical_device_id
80 self.nni_intf_id = None
81 self.platform = platform
82 self.logical_flows_proxy = registry('core').get_proxy(
83 '/logical_devices/{}/flows'.format(self.logical_device_id))
84 self.flows_proxy = registry('core').get_proxy(
85 '/devices/{}/flows'.format(self.device_id))
86 self.root_proxy = registry('core').get_proxy('/')
87 self.resource_mgr = resource_mgr
88 self.tech_profile = dict()
89 self._populate_tech_profile_per_pon_port()
90 self.retry_add_flow_list = []
91
92 def add_flow(self, flow):
93 self.log.debug('add flow', flow=flow)
94 classifier_info = dict()
95 action_info = dict()
96
97 for field in fd.get_ofb_fields(flow):
98 if field.type == fd.ETH_TYPE:
99 classifier_info[ETH_TYPE] = field.eth_type
100 self.log.debug('field-type-eth-type',
101 eth_type=classifier_info[ETH_TYPE])
102 elif field.type == fd.IP_PROTO:
103 classifier_info[IP_PROTO] = field.ip_proto
104 self.log.debug('field-type-ip-proto',
105 ip_proto=classifier_info[IP_PROTO])
106 elif field.type == fd.IN_PORT:
107 classifier_info[IN_PORT] = field.port
108 self.log.debug('field-type-in-port',
109 in_port=classifier_info[IN_PORT])
110 elif field.type == fd.VLAN_VID:
111 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
112 self.log.debug('field-type-vlan-vid',
113 vlan=classifier_info[VLAN_VID])
114 elif field.type == fd.VLAN_PCP:
115 classifier_info[VLAN_PCP] = field.vlan_pcp
116 self.log.debug('field-type-vlan-pcp',
117 pcp=classifier_info[VLAN_PCP])
118 elif field.type == fd.UDP_DST:
119 classifier_info[UDP_DST] = field.udp_dst
120 self.log.debug('field-type-udp-dst',
121 udp_dst=classifier_info[UDP_DST])
122 elif field.type == fd.UDP_SRC:
123 classifier_info[UDP_SRC] = field.udp_src
124 self.log.debug('field-type-udp-src',
125 udp_src=classifier_info[UDP_SRC])
126 elif field.type == fd.IPV4_DST:
127 classifier_info[IPV4_DST] = field.ipv4_dst
128 self.log.debug('field-type-ipv4-dst',
129 ipv4_dst=classifier_info[IPV4_DST])
130 elif field.type == fd.IPV4_SRC:
131 classifier_info[IPV4_SRC] = field.ipv4_src
132 self.log.debug('field-type-ipv4-src',
133 ipv4_dst=classifier_info[IPV4_SRC])
134 elif field.type == fd.METADATA:
135 classifier_info[METADATA] = field.table_metadata
136 self.log.debug('field-type-metadata',
137 metadata=classifier_info[METADATA])
138 else:
139 raise NotImplementedError('field.type={}'.format(
140 field.type))
141
142 for action in fd.get_actions(flow):
143 if action.type == fd.OUTPUT:
144 action_info[OUTPUT] = action.output.port
145 self.log.debug('action-type-output',
146 output=action_info[OUTPUT],
147 in_port=classifier_info[IN_PORT])
148 elif action.type == fd.POP_VLAN:
149 if fd.get_goto_table_id(flow) is None:
150 self.log.debug('being taken care of by ONU', flow=flow)
151 return
152 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
181 if fd.get_goto_table_id(flow) is not None and POP_VLAN not in action_info:
182 self.log.debug('being taken care of by ONU', flow=flow)
183 return
184
185 if OUTPUT not in action_info and METADATA in classifier_info:
186 # find flow in the next table
187 next_flow = self.find_next_flow(flow)
188 if next_flow is None:
189 return
190 action_info[OUTPUT] = fd.get_out_port(next_flow)
191 for field in fd.get_ofb_fields(next_flow):
192 if field.type == fd.VLAN_VID:
193 classifier_info[METADATA] = field.vlan_vid & 0xfff
194
195 self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT])
196 (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow(
197 classifier_info[IN_PORT], action_info[OUTPUT])
198
199 self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info,
200 action_info, flow)
201
202 def _is_uni_port(self, port_no):
203 try:
204 port = self.adapter_agent.get_logical_port(self.logical_device_id,
205 'uni-{}'.format(port_no))
206 if port is not None:
207 return (not port.root_port), port.device_id
208 else:
209 return False, None
210 except Exception as e:
211 self.log.error("error-retrieving-port", e=e)
212 return False, None
213
214 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
215 uni_port_no = None
216 child_device_id = None
217 if flow_direction == UPSTREAM:
218 for field in fd.get_ofb_fields(flow):
219 if field.type == fd.IN_PORT:
220 is_uni, child_device_id = self._is_uni_port(field.port)
221 if is_uni:
222 uni_port_no = field.port
223 elif flow_direction == DOWNSTREAM:
224 for field in fd.get_ofb_fields(flow):
225 if field.type == fd.METADATA:
226 uni_port = field.table_metadata & 0xFFFFFFFF
227 is_uni, child_device_id = self._is_uni_port(uni_port)
228 if is_uni:
229 uni_port_no = field.port
230
231 if uni_port_no is None:
232 for action in fd.get_actions(flow):
233 if action.type == fd.OUTPUT:
234 is_uni, child_device_id = \
235 self._is_uni_port(action.output.port)
236 if is_uni:
237 uni_port_no = action.output.port
238
239 if child_device_id:
240 child_device = self.adapter_agent.get_device(child_device_id)
241 pon_intf = child_device.proxy_address.channel_id
242 onu_id = child_device.proxy_address.onu_id
243 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
244 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
245 assert (isinstance(flows, list))
246 self.log.debug("retrieved-flows", flows=flows)
247 for idx in range(len(flows)):
248 if flow_direction == flows[idx]['flow_type']:
249 flows.pop(idx)
250 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
251 if len(flows) > 0:
252 # There are still flows referencing the same flow_id.
253 # So the flow should not be freed yet.
254 # For ex: Case of HSIA where same flow is shared
255 # between DS and US.
256 return
257
258 self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id)
259 else:
260 self.log.error("invalid-info", uni_port_no=uni_port_no,
261 child_device_id=child_device_id)
262
263 def retry_add_flow(self, flow):
264 self.log.debug("retry-add-flow")
265 if flow.id in self.retry_add_flow_list:
266 self.retry_add_flow_list.remove(flow.id)
267 self.add_flow(flow)
268
269 def remove_flow(self, flow):
270 self.log.debug('trying to remove flows from logical flow :',
271 logical_flow=flow)
272 device_flows_to_remove = []
273 device_flows = self.flows_proxy.get('/').items
274 for f in device_flows:
275 if f.cookie == flow.id:
276 device_flows_to_remove.append(f)
277
278 for f in device_flows_to_remove:
279 (id, direction) = self.decode_stored_id(f.id)
280 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
281 try:
282 self.stub.FlowRemove(flow_to_remove)
283 except grpc.RpcError as grpc_e:
284 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
285 self.log.debug('This flow does not exist on the switch, '
286 'normal after an OLT reboot',
287 flow=flow_to_remove)
288 else:
289 raise grpc_e
290
291 # once we have successfully deleted the flow on the device
292 # release the flow_id on resource pool and also clear any
293 # data associated with the flow_id on KV store.
294 self._clear_flow_id_from_rm(f, id, direction)
295 self.log.debug('flow removed from device', flow=f,
296 flow_key=flow_to_remove)
297
298 if len(device_flows_to_remove) > 0:
299 new_flows = []
300 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
301 for f in device_flows:
302 if f.id not in flows_ids_to_remove:
303 new_flows.append(f)
304
305 self.flows_proxy.update('/', Flows(items=new_flows))
306 self.log.debug('flows removed from the data store',
307 flow_ids_removed=flows_ids_to_remove,
308 number_of_flows_removed=(len(device_flows) - len(
309 new_flows)), expected_flows_removed=len(
310 device_flows_to_remove))
311 else:
312 self.log.debug('no device flow to remove for this flow (normal '
313 'for multi table flows)', flow=flow)
314
315 def _get_ofp_port_name(self, intf_id, onu_id, uni_id):
316 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
317 child_device = self.adapter_agent.get_child_device(self.device_id,
318 parent_port_no=parent_port_no, onu_id=onu_id)
319 if child_device is None:
320 self.log.error("could-not-find-child-device",
321 parent_port_no=intf_id, onu_id=onu_id)
322 return (None, None)
323 ports = self.adapter_agent.get_ports(child_device.id, Port.ETHERNET_UNI)
324 logical_port = self.adapter_agent.get_logical_port(
325 self.logical_device_id, ports[uni_id].label)
326 ofp_port_name = (logical_port.ofp_port.name, logical_port.ofp_port.port_no)
327 return ofp_port_name
328
329 def get_tp_path(self, intf_id, ofp_port_name):
330 # FIXME Should get Table id form the flow, as of now hardcoded to
331 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
332 # 'tp_path' contains the suffix part of the tech_profile_instance path.
333 # The prefix to the 'tp_path' should be set to \
334 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
335 return self.tech_profile[intf_id]. \
336 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
337 ofp_port_name)
338
339 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id):
340 # Remove the TP instance associated with the ONU
341 ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id)
342 tp_path = self.get_tp_path(intf_id, ofp_port_name)
343 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
344
345 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
346 action, flow):
347
348 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no,
349 classifier=classifier, action=action)
350
351 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
352 flow.table_id)
353 if alloc_id is None or gem_ports is None:
354 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id,
355 gem_ports=gem_ports)
356 return
357
358 self.log.debug('Generated required alloc and gemport ids',
359 alloc_id=alloc_id, gemports=gem_ports)
360
361 # Flows can't be added specific to gemport unless p-bits are received.
362 # Hence adding flows for all gemports
363 for gemport_id in gem_ports:
364 if IP_PROTO in classifier:
365 if classifier[IP_PROTO] == 17:
366 self.log.debug('dhcp flow add')
367 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
368 action, flow, alloc_id, gemport_id)
369 elif classifier[IP_PROTO] == 2:
370 self.log.warn('igmp flow add ignored, not implemented yet')
371 else:
372 self.log.warn("Invalid-Classifier-to-handle",
373 classifier=classifier,
374 action=action)
375 elif ETH_TYPE in classifier:
376 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
377 self.log.debug('eapol flow add')
378 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
379 gemport_id)
380 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
381 if vlan_id is not None:
382 self.add_eapol_flow(
383 intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
384 vlan_id=vlan_id)
385 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
386 onu_device = self.adapter_agent.get_child_device(self.device_id,
387 onu_id=onu_id,
388 parent_port_no=parent_port_no)
389 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
390 if ofp_port_name is None:
391 self.log.error("port-name-not-found")
392 return
393
394 tp_path = self.get_tp_path(intf_id, ofp_port_name)
395
396 self.log.debug('Load-tech-profile-request-to-brcm-handler',
397 tp_path=tp_path)
398 msg = {'proxy_address': onu_device.proxy_address, 'uni_id': uni_id,
399 'event': 'download_tech_profile', 'event_data': tp_path}
400
401 # Send the event message to the ONU adapter
402 self.adapter_agent.publish_inter_adapter_message(onu_device.id,
403 msg)
404
405 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
406 self.log.debug('lldp flow add')
407 nni_intf_id = self.get_nni_intf_id()
408 self.add_lldp_flow(flow, port_no, nni_intf_id)
409
410 elif PUSH_VLAN in action:
411 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
412 action, flow, alloc_id, gemport_id)
413 elif POP_VLAN in action:
414 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
415 action, flow, alloc_id, gemport_id)
416 else:
417 self.log.debug('Invalid-flow-type-to-handle',
418 classifier=classifier,
419 action=action, flow=flow)
420
421 def create_tcont_gemport(self, intf_id, onu_id, uni_id, table_id):
422 alloc_id, gem_port_ids = None, None
423 pon_intf_onu_id = (intf_id, onu_id)
424
425 # If we already have allocated alloc_id and gem_ports earlier, render them
426 alloc_id = \
427 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
428 gem_port_ids = \
429 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
430 if alloc_id is not None and gem_port_ids is not None:
431 return alloc_id, gem_port_ids
432
433 try:
434 (ofp_port_name, ofp_port_no) = self._get_ofp_port_name(intf_id, onu_id, uni_id)
435 if ofp_port_name is None:
436 self.log.error("port-name-not-found")
437 return alloc_id, gem_port_ids
438 # FIXME: If table id is <= 63 using 64 as table id
439 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
440 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
441
442 # Check tech profile instance already exists for derived port name
443 tech_profile_instance = self.tech_profile[intf_id]. \
444 get_tech_profile_instance(table_id, ofp_port_name)
445 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
446
447 if tech_profile_instance is None:
448 # create tech profile instance
449 tech_profile_instance = self.tech_profile[intf_id]. \
450 create_tech_profile_instance(table_id, ofp_port_name,
451 intf_id)
452 if tech_profile_instance is None:
453 raise Exception('Tech-profile-instance-creation-failed')
454 else:
455 self.log.debug(
456 'Tech-profile-instance-already-exist-for-given port-name',
457 ofp_port_name=ofp_port_name)
458
459 # upstream scheduler
460 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
461 tech_profile_instance)
462 # downstream scheduler
463 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
464 tech_profile_instance)
465 # create Tcont
466 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
467 us_scheduler,
468 ds_scheduler)
469
470 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
471 onu_id=onu_id,
472 uni_id=uni_id,
473 port_no=ofp_port_no,
474 tconts=tconts))
475
476 # Fetch alloc id and gemports from tech profile instance
477 alloc_id = tech_profile_instance.us_scheduler.alloc_id
478 gem_port_ids = []
479 for i in range(len(
480 tech_profile_instance.upstream_gem_port_attribute_list)):
481 gem_port_ids.append(
482 tech_profile_instance.upstream_gem_port_attribute_list[i].
483 gemport_id)
484 except BaseException as e:
485 self.log.exception(exception=e)
486
487 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
488 pon_intf_onu_id = (intf_id, onu_id, uni_id)
489 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
490 pon_intf_onu_id,
491 list([alloc_id])
492 )
493 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
494 pon_intf_onu_id,
495 gem_port_ids
496 )
497
498 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
499 gem_port_ids, intf_id, onu_id, uni_id
500 )
501
502 return alloc_id, gem_port_ids
503
504 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
505 uplink_action, logical_flow, alloc_id,
506 gemport_id):
507
508 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
509
510 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
511 uplink_action, UPSTREAM,
512 logical_flow, alloc_id, gemport_id)
513
514 # Secondary EAP on the subscriber vlan
515 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
516 if eap_active:
517 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
518 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
519
520 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
521 downlink_action, flow, alloc_id, gemport_id):
522 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
523 # Needed ???? It should be already there
524 downlink_action[POP_VLAN] = True
525 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
526
527 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
528 downlink_action, DOWNSTREAM,
529 flow, alloc_id, gemport_id)
530
531 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
532 direction, logical_flow, alloc_id, gemport_id):
533
534 flow_store_cookie = self._get_flow_store_cookie(classifier,
535 gemport_id)
536
537 # One of the OLT platform (Broadcom BAL) requires that symmetric
538 # flows require the same flow_id to be used across UL and DL.
539 # Since HSIA flow is the only symmetric flow currently, we need to
540 # re-use the flow_id across both direction. The 'flow_category'
541 # takes priority over flow_cookie to find any available HSIA_FLOW
542 # id for the ONU.
543 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
544 flow_store_cookie,
545 HSIA_FLOW)
546 if flow_id is None:
547 self.log.error("hsia-flow-unavailable")
548 return
549 flow = openolt_pb2.Flow(
550 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
551 flow_type=direction, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
552 gemport_id=gemport_id,
553 classifier=self.mk_classifier(classifier),
554 action=self.mk_action(action),
555 priority=logical_flow.priority,
556 port_no=port_no,
557 cookie=logical_flow.cookie)
558
559 if self.add_flow_to_device(flow, logical_flow):
560 flow_info = self._get_flow_info_as_json_blob(flow,
561 flow_store_cookie,
562 HSIA_FLOW)
563 self.update_flow_info_to_kv_store(flow.access_intf_id,
564 flow.onu_id, flow.uni_id,
565 flow.flow_id, flow_info)
566
567 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
568 alloc_id, gemport_id):
569
570 self.log.debug('add dhcp upstream trap', classifier=classifier,
571 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
572
573 action.clear()
574 action[TRAP_TO_HOST] = True
575 classifier[UDP_SRC] = 68
576 classifier[UDP_DST] = 67
577 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
578 classifier.pop(VLAN_VID, None)
579
580 flow_store_cookie = self._get_flow_store_cookie(classifier,
581 gemport_id)
582
583 flow_id = self.resource_mgr.get_flow_id(
584 intf_id, onu_id, uni_id, flow_store_cookie
585 )
586 dhcp_flow = openolt_pb2.Flow(
587 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
588 access_intf_id=intf_id, gemport_id=gemport_id,
589 alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
590 priority=logical_flow.priority,
591 classifier=self.mk_classifier(classifier),
592 action=self.mk_action(action),
593 port_no=port_no,
594 cookie=logical_flow.cookie)
595
596 if self.add_flow_to_device(dhcp_flow, logical_flow):
597 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
598 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
599 dhcp_flow.onu_id,
600 dhcp_flow.uni_id,
601 dhcp_flow.flow_id,
602 flow_info)
603
604 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
605 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
606
607 uplink_classifier = dict()
608 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
609 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
610 uplink_classifier[VLAN_VID] = vlan_id
611
612 uplink_action = dict()
613 uplink_action[TRAP_TO_HOST] = True
614
615 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
616 gemport_id)
617 # Add Upstream EAPOL Flow.
618 uplink_flow_id = self.resource_mgr.get_flow_id(
619 intf_id, onu_id, uni_id, flow_store_cookie
620 )
621
622 upstream_flow = openolt_pb2.Flow(
623 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=uplink_flow_id,
624 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
625 gemport_id=gemport_id,
626 classifier=self.mk_classifier(uplink_classifier),
627 action=self.mk_action(uplink_action),
628 priority=logical_flow.priority,
629 port_no=port_no,
630 cookie=logical_flow.cookie)
631
632 logical_flow = copy.deepcopy(logical_flow)
633 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
634 vlan_id | 0x1000)]))
635 logical_flow.match.type = OFPMT_OXM
636
637 if self.add_flow_to_device(upstream_flow, logical_flow):
638 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
639 flow_store_cookie)
640 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
641 upstream_flow.onu_id,
642 upstream_flow.uni_id,
643 upstream_flow.flow_id,
644 flow_info)
645
646 if vlan_id == DEFAULT_MGMT_VLAN:
647 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
648 # requirement)
649 # On one of the platforms (Broadcom BAL), when same DL classifier
650 # vlan was used across multiple ONUs, eapol flow re-adds after
651 # flow delete (cases of onu reboot/disable) fails.
652 # In order to generate unique vlan, a combination of intf_id
653 # onu_id and uni_id is used.
654 # uni_id defaults to 0, so add 1 to it.
655 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
656 # Assert that we do not generate invalid vlans under no condition
657 assert (special_vlan_downstream_flow >= 2, 'invalid-vlan-generated')
658
659 downlink_classifier = dict()
660 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
661 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
662
663 downlink_action = dict()
664 downlink_action[PUSH_VLAN] = True
665 downlink_action[VLAN_VID] = vlan_id
666
667
668 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
669 gemport_id)
670
671 downlink_flow_id = self.resource_mgr.get_flow_id(
672 intf_id, onu_id, uni_id, flow_store_cookie
673 )
674
675 downstream_flow = openolt_pb2.Flow(
676 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=downlink_flow_id,
677 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=self.get_nni_intf_id(),
678 gemport_id=gemport_id,
679 classifier=self.mk_classifier(downlink_classifier),
680 action=self.mk_action(downlink_action),
681 priority=logical_flow.priority,
682 port_no=port_no,
683 cookie=logical_flow.cookie)
684
685 downstream_logical_flow = ofp_flow_stats(
686 id=logical_flow.id, cookie=logical_flow.cookie,
687 table_id=logical_flow.table_id, priority=logical_flow.priority,
688 flags=logical_flow.flags)
689
690 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
691 fd.in_port(fd.get_out_port(logical_flow)),
692 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
693 downstream_logical_flow.match.type = OFPMT_OXM
694
695 downstream_logical_flow.instructions.extend(
696 fd.mk_instructions_from_actions([fd.output(
697 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
698
699 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
700 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
701 flow_store_cookie)
702 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
703 downstream_flow.onu_id,
704 downstream_flow.uni_id,
705 downstream_flow.flow_id,
706 flow_info)
707
708 def repush_all_different_flows(self):
709 # Check if the device is supposed to have flows, if so add them
710 # Recover static flows after a reboot
711 logical_flows = self.logical_flows_proxy.get('/').items
712 devices_flows = self.flows_proxy.get('/').items
713 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
714 for logical_flow in logical_flows:
715 try:
716 if logical_flow.id not in logical_flows_ids_provisioned:
717 self.add_flow(logical_flow)
718 except Exception as e:
719 self.log.exception('Problem reading this flow', e=e)
720
721 def reset_flows(self):
722 self.flows_proxy.update('/', Flows())
723
724 """ Add a downstream LLDP trap flow on the NNI interface
725 """
726
727 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
728
729 classifier = dict()
730 classifier[ETH_TYPE] = LLDP_ETH_TYPE
731 classifier[PACKET_TAG_TYPE] = UNTAGGED
732 action = dict()
733 action[TRAP_TO_HOST] = True
734
735 # LLDP flow is installed to trap LLDP packets on the NNI port.
736 # We manage flow_id resource pool on per PON port basis.
737 # Since this situation is tricky, as a hack, we pass the NNI port
738 # index (network_intf_id) as PON port Index for the flow_id resource
739 # pool. Also, there is no ONU Id available for trapping LLDP packets
740 # on NNI port, use onu_id as -1 (invalid)
741 # ****************** CAVEAT *******************
742 # This logic works if the NNI Port Id falls within the same valid
743 # range of PON Port Ids. If this doesn't work for some OLT Vendor
744 # we need to have a re-look at this.
745 # *********************************************
746 onu_id = -1
747 uni_id = -1
748 flow_store_cookie = self._get_flow_store_cookie(classifier)
749 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
750 flow_store_cookie)
751
752 downstream_flow = openolt_pb2.Flow(
753 access_intf_id=-1, # access_intf_id not required
754 onu_id=onu_id, # onu_id not required
755 uni_id=uni_id, # uni_id not used
756 flow_id=flow_id,
757 flow_type=DOWNSTREAM,
758 network_intf_id=network_intf_id,
759 gemport_id=-1, # gemport_id not required
760 classifier=self.mk_classifier(classifier),
761 action=self.mk_action(action),
762 priority=logical_flow.priority,
763 port_no=port_no,
764 cookie=logical_flow.cookie)
765
766 self.log.debug('add lldp downstream trap', classifier=classifier,
767 action=action, flow=downstream_flow, port_no=port_no)
768 if self.add_flow_to_device(downstream_flow, logical_flow):
769 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
770 flow_id, downstream_flow)
771
772 def mk_classifier(self, classifier_info):
773
774 classifier = openolt_pb2.Classifier()
775
776 if ETH_TYPE in classifier_info:
777 classifier.eth_type = classifier_info[ETH_TYPE]
778 if IP_PROTO in classifier_info:
779 classifier.ip_proto = classifier_info[IP_PROTO]
780 if VLAN_VID in classifier_info:
781 classifier.o_vid = classifier_info[VLAN_VID]
782 if METADATA in classifier_info:
783 classifier.i_vid = classifier_info[METADATA]
784 if VLAN_PCP in classifier_info:
785 classifier.o_pbits = classifier_info[VLAN_PCP]
786 if UDP_SRC in classifier_info:
787 classifier.src_port = classifier_info[UDP_SRC]
788 if UDP_DST in classifier_info:
789 classifier.dst_port = classifier_info[UDP_DST]
790 if IPV4_DST in classifier_info:
791 classifier.dst_ip = classifier_info[IPV4_DST]
792 if IPV4_SRC in classifier_info:
793 classifier.src_ip = classifier_info[IPV4_SRC]
794 if PACKET_TAG_TYPE in classifier_info:
795 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
796 classifier.pkt_tag_type = SINGLE_TAG
797 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
798 classifier.pkt_tag_type = DOUBLE_TAG
799 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
800 classifier.pkt_tag_type = UNTAGGED
801 else:
802 classifier.pkt_tag_type = 'none'
803
804 return classifier
805
806 def mk_action(self, action_info):
807 action = openolt_pb2.Action()
808
809 if POP_VLAN in action_info:
810 action.o_vid = action_info[VLAN_VID]
811 action.cmd.remove_outer_tag = True
812 elif PUSH_VLAN in action_info:
813 action.o_vid = action_info[VLAN_VID]
814 action.cmd.add_outer_tag = True
815 elif TRAP_TO_HOST in action_info:
816 action.cmd.trap_to_host = True
817 else:
818 self.log.info('Invalid-action-field', action_info=action_info)
819 return
820 return action
821
822 def is_eap_enabled(self, intf_id, onu_id, uni_id):
823 flows = self.logical_flows_proxy.get('/').items
824
825 for flow in flows:
826 eap_flow = False
827 eap_intf_id = None
828 eap_onu_id = None
829 eap_uni_id = None
830 for field in fd.get_ofb_fields(flow):
831 if field.type == fd.ETH_TYPE:
832 if field.eth_type == EAP_ETH_TYPE:
833 eap_flow = True
834 if field.type == fd.IN_PORT:
835 eap_intf_id = self.platform.intf_id_from_uni_port_num(
836 field.port)
837 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
838 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
839
840 if eap_flow:
841 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
842 intf_id=intf_id, eap_intf_id=eap_intf_id,
843 eap_onu_id=eap_onu_id,
844 eap_uni_id=eap_uni_id)
845 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id:
846 return True, flow
847
848 return False, None
849
850 def get_subscriber_vlan(self, port):
851 self.log.debug('looking from subscriber flow for port', port=port)
852
853 flows = self.logical_flows_proxy.get('/').items
854 for flow in flows:
855 in_port = fd.get_in_port(flow)
856 out_port = fd.get_out_port(flow)
857 if in_port == port and out_port is not None and \
858 self.platform.intf_id_to_port_type_name(out_port) \
859 == Port.ETHERNET_NNI:
860 fields = fd.get_ofb_fields(flow)
861 self.log.debug('subscriber flow found', fields=fields)
862 for field in fields:
863 if field.type == OFPXMT_OFB_VLAN_VID:
864 self.log.debug('subscriber vlan found',
865 vlan_id=field.vlan_vid)
866 return field.vlan_vid & 0x0fff
867 self.log.debug('No subscriber flow found', port=port)
868 return None
869
870 def add_flow_to_device(self, flow, logical_flow):
871 self.log.debug('pushing flow to device', flow=flow)
872 try:
873 self.stub.FlowAdd(flow)
874 except grpc.RpcError as grpc_e:
875 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
876 self.log.warn('flow already exists', e=grpc_e, flow=flow)
877 else:
878 self.log.error('failed to add flow',
879 logical_flow=logical_flow, flow=flow,
880 grpc_error=grpc_e)
881 return False
882 else:
883 self.register_flow(logical_flow, flow)
884 return True
885
886 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
887 self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id,
888 flow_id, flow)
889
890 def register_flow(self, logical_flow, device_flow):
891 self.log.debug('registering flow in device',
892 logical_flow=logical_flow, device_flow=device_flow)
893 stored_flow = copy.deepcopy(logical_flow)
894 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
895 device_flow.flow_type)
896 self.log.debug('generated device flow id', id=stored_flow.id,
897 flow_id=device_flow.flow_id,
898 direction=device_flow.flow_type)
899 stored_flow.cookie = logical_flow.id
900 flows = self.flows_proxy.get('/')
901 flows.items.extend([stored_flow])
902 self.flows_proxy.update('/', flows)
903
904 def find_next_flow(self, flow):
905 table_id = fd.get_goto_table_id(flow)
906 metadata = 0
907 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
908 # 1.13.5 and later, the lower 32-bits is the output port number and the
909 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
910 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
911
912 for field in fd.get_ofb_fields(flow):
913 if field.type == fd.METADATA:
914 metadata = field.table_metadata & 0xFFFFFFFF
915 if table_id is None:
916 return None
917 flows = self.logical_flows_proxy.get('/').items
918 next_flows = []
919 for f in flows:
920 if f.table_id == table_id:
921 # FIXME
922 if fd.get_in_port(f) == fd.get_in_port(flow) and \
923 fd.get_out_port(f) == metadata:
924 next_flows.append(f)
925
926 if len(next_flows) == 0:
927 self.log.warning('no next flow found, it may be a timing issue',
928 flow=flow, number_of_flows=len(flows))
929 if flow.id in self.retry_add_flow_list:
930 self.log.debug('flow is already in retry list', flow_id=flow.id)
931 else:
932 self.retry_add_flow_list.append(flow.id)
933 reactor.callLater(5, self.retry_add_flow, flow)
934 return None
935
936 next_flows.sort(key=lambda f: f.priority, reverse=True)
937
938 return next_flows[0]
939
940 def update_children_flows(self, device_rules_map):
941
942 for device_id, (flows, groups) in device_rules_map.iteritems():
943 if device_id != self.device_id:
944 self.root_proxy.update('/devices/{}/flows'.format(device_id),
945 Flows(items=flows.values()))
946 self.root_proxy.update('/devices/{}/flow_groups'.format(
947 device_id), FlowGroups(items=groups.values()))
948
949 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
950 ofp_port_name = logical_port.ofp_port.name
951 port_no = logical_port.ofp_port.port_no
952 pon_port = child_device.proxy_address.channel_id
953 onu_id = child_device.proxy_address.onu_id
954 uni_id = self.platform.uni_id_from_port_num(logical_port)
955
956 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
957 # is probably to maintain a list of Tech-profile table IDs associated
958 # with the UNI logical_port. This way, when the logical port is deleted,
959 # all the associated tech-profile configuration with the UNI logical_port
960 # can be cleared.
961 tech_profile_instance = self.tech_profile[pon_port]. \
962 get_tech_profile_instance(
963 DEFAULT_TECH_PROFILE_TABLE_ID,
964 ofp_port_name)
965 flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id)
966 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
967 for flow_id in flow_ids:
968 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
969 for flow_info in flow_infos:
970 direction = flow_info['flow_type']
971 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
972 flow_type=direction)
973 try:
974 self.stub.FlowRemove(flow_to_remove)
975 except grpc.RpcError as grpc_e:
976 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
977 self.log.debug('This flow does not exist on the switch, '
978 'normal after an OLT reboot',
979 flow=flow_to_remove)
980 else:
981 raise grpc_e
982
983 self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id)
984
985 try:
986 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
987 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
988 onu_id=onu_id,
989 uni_id=uni_id,
990 port_no=port_no,
991 tconts=tconts))
992 except grpc.RpcError as grpc_e:
993 self.log.error('error-removing-tcont-scheduler-queues',
994 err=grpc_e)
995
996 def generate_stored_id(self, flow_id, direction):
997 if direction == UPSTREAM:
998 self.log.debug('upstream flow, shifting id')
999 return 0x1 << 15 | flow_id
1000 elif direction == DOWNSTREAM:
1001 self.log.debug('downstream flow, not shifting id')
1002 return flow_id
1003 else:
1004 self.log.warn('Unrecognized direction', direction=direction)
1005 return flow_id
1006
1007 def decode_stored_id(self, id):
1008 if id >> 15 == 0x1:
1009 return id & 0x7fff, UPSTREAM
1010 else:
1011 return id, DOWNSTREAM
1012
1013 def _populate_tech_profile_per_pon_port(self):
1014 for arange in self.resource_mgr.device_info.ranges:
1015 for intf_id in arange.intf_ids:
1016 self.tech_profile[intf_id] = \
1017 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1018
1019 # Make sure we have as many tech_profiles as there are pon ports on
1020 # the device
1021 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1022
1023 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1024 flow_category=None):
1025 json_blob = MessageToDict(message=flow,
1026 preserving_proto_field_name=True)
1027 self.log.debug("flow-info", json_blob=json_blob)
1028 json_blob['flow_store_cookie'] = flow_store_cookie
1029 if flow_category is not None:
1030 json_blob['flow_category'] = flow_category
1031 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1032 flow.onu_id, flow.uni_id, flow.flow_id)
1033
1034 if flow_info is None:
1035 flow_info = list()
1036 flow_info.append(json_blob)
1037 else:
1038 assert (isinstance(flow_info, list))
1039 flow_info.append(json_blob)
1040
1041 return flow_info
1042
1043 @staticmethod
1044 def _get_flow_store_cookie(classifier, gem_port=None):
1045 assert isinstance(classifier, dict)
1046 # We need unique flows per gem_port
1047 if gem_port is not None:
1048 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1049 else:
1050 to_hash = dumps(classifier, sort_keys=True)
1051 return hashlib.md5(to_hash).hexdigest()[:12]
1052
1053 def get_nni_intf_id(self):
1054 if self.nni_intf_id is not None:
1055 return self.nni_intf_id
1056
1057 port_list = self.adapter_agent.get_ports(self.device_id, Port.ETHERNET_NNI)
1058 logical_port = self.adapter_agent.get_logical_port(self.logical_device_id,
1059 port_list[0].label)
1060 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(logical_port.ofp_port.port_no)
1061 self.log.debug("nni-intf-d ", nni_intf_id=self.nni_intf_id)
1062 return self.nni_intf_id