blob: 264056a6cbc4cde70350e7b38b3c35cf7d7188d0 [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,
William Kurkian23047b92019-05-01 11:02:35 -0400199 action_info, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500200
201 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
202 uni_port_no = None
203 child_device_id = None
204 if flow_direction == UPSTREAM:
205 for field in fd.get_ofb_fields(flow):
206 if field.type == fd.IN_PORT:
207 is_uni, child_device_id = self._is_uni_port(field.port)
208 if is_uni:
209 uni_port_no = field.port
210 elif flow_direction == DOWNSTREAM:
211 for field in fd.get_ofb_fields(flow):
212 if field.type == fd.METADATA:
213 uni_port = field.table_metadata & 0xFFFFFFFF
214 is_uni, child_device_id = self._is_uni_port(uni_port)
215 if is_uni:
216 uni_port_no = field.port
217
218 if uni_port_no is None:
219 for action in fd.get_actions(flow):
220 if action.type == fd.OUTPUT:
221 is_uni, child_device_id = \
222 self._is_uni_port(action.output.port)
223 if is_uni:
224 uni_port_no = action.output.port
225
226 if child_device_id:
227 child_device = self.adapter_agent.get_device(child_device_id)
228 pon_intf = child_device.proxy_address.channel_id
229 onu_id = child_device.proxy_address.onu_id
230 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
231 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
232 assert (isinstance(flows, list))
233 self.log.debug("retrieved-flows", flows=flows)
234 for idx in range(len(flows)):
235 if flow_direction == flows[idx]['flow_type']:
236 flows.pop(idx)
237 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
238 if len(flows) > 0:
239 # There are still flows referencing the same flow_id.
240 # So the flow should not be freed yet.
241 # For ex: Case of HSIA where same flow is shared
242 # between DS and US.
243 return
244
245 self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id)
246 else:
247 self.log.error("invalid-info", uni_port_no=uni_port_no,
248 child_device_id=child_device_id)
249
250 def retry_add_flow(self, flow):
251 self.log.debug("retry-add-flow")
252 if flow.id in self.retry_add_flow_list:
253 self.retry_add_flow_list.remove(flow.id)
254 self.add_flow(flow)
255
256 def remove_flow(self, flow):
257 self.log.debug('trying to remove flows from logical flow :',
258 logical_flow=flow)
259 device_flows_to_remove = []
260 device_flows = self.flows_proxy.get('/').items
261 for f in device_flows:
262 if f.cookie == flow.id:
263 device_flows_to_remove.append(f)
264
265 for f in device_flows_to_remove:
266 (id, direction) = self.decode_stored_id(f.id)
267 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
268 try:
269 self.stub.FlowRemove(flow_to_remove)
270 except grpc.RpcError as grpc_e:
271 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
272 self.log.debug('This flow does not exist on the switch, '
273 'normal after an OLT reboot',
274 flow=flow_to_remove)
275 else:
276 raise grpc_e
277
278 # once we have successfully deleted the flow on the device
279 # release the flow_id on resource pool and also clear any
280 # data associated with the flow_id on KV store.
281 self._clear_flow_id_from_rm(f, id, direction)
282 self.log.debug('flow removed from device', flow=f,
283 flow_key=flow_to_remove)
284
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400285 def get_tp_path(self, intf_id, uni):
William Kurkian6f436d02019-02-06 16:25:01 -0500286 # FIXME Should get Table id form the flow, as of now hardcoded to
287 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
288 # 'tp_path' contains the suffix part of the tech_profile_instance path.
289 # The prefix to the 'tp_path' should be set to \
290 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
291 return self.tech_profile[intf_id]. \
292 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400293 uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500294
295 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id):
296 # Remove the TP instance associated with the ONU
297 ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id)
298 tp_path = self.get_tp_path(intf_id, ofp_port_name)
299 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
300
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400301 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500302 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
303 action, flow):
304
305 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no,
306 classifier=classifier, action=action)
307
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400308 uni = self.get_uni_port_path(intf_id, onu_id, uni_id)
309
William Kurkian6f436d02019-02-06 16:25:01 -0500310 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400311 uni, port_no, flow.table_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500312 if alloc_id is None or gem_ports is None:
313 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id,
314 gem_ports=gem_ports)
315 return
316
317 self.log.debug('Generated required alloc and gemport ids',
318 alloc_id=alloc_id, gemports=gem_ports)
319
320 # Flows can't be added specific to gemport unless p-bits are received.
321 # Hence adding flows for all gemports
322 for gemport_id in gem_ports:
323 if IP_PROTO in classifier:
324 if classifier[IP_PROTO] == 17:
325 self.log.debug('dhcp flow add')
326 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
327 action, flow, alloc_id, gemport_id)
328 elif classifier[IP_PROTO] == 2:
329 self.log.warn('igmp flow add ignored, not implemented yet')
330 else:
331 self.log.warn("Invalid-Classifier-to-handle",
332 classifier=classifier,
333 action=action)
334 elif ETH_TYPE in classifier:
335 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
336 self.log.debug('eapol flow add')
337 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
338 gemport_id)
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400339
340 # TODO NEW CORE: Skip trying to add subsequent eap capture for subscriber vlan
341 # (later attempts at re-eap)
342 #vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
343 #if vlan_id is not None:
344 # self.add_eapol_flow(
345 # intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
346 # vlan_id=vlan_id)
347 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
Matt Jeanneretaa360912019-04-22 16:23:12 -0400348
349 self.log.debug('get-child-device', intf_id=intf_id, onu_id=onu_id,
350 parent_port_no=parent_port_no, device_id=self.device_id)
351
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400352 onu_device = yield self.core_proxy.get_child_device(self.device_id,
Matt Jeanneretaa360912019-04-22 16:23:12 -0400353 onu_id=int(onu_id),
354 parent_port_no=int(parent_port_no))
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400355 tp_path = self.get_tp_path(intf_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500356
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400357 tech_msg = InterAdapterTechProfileDownloadMessage(uni_id=uni_id, path=tp_path)
William Kurkian6f436d02019-02-06 16:25:01 -0500358
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400359 self.log.debug('Load-tech-profile-request-to-brcm-handler',
360 onu_device=onu_device, tp_path=tp_path, tech_msg=tech_msg)
361
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400362 # Send the tech profile event to the onu adapter
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400363 yield self.adapter_proxy.send_inter_adapter_message(
364 msg=tech_msg,
365 type=InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST,
366 from_adapter="openolt",
367 to_adapter=onu_device.type,
368 to_device_id=onu_device.id
369 )
William Kurkian6f436d02019-02-06 16:25:01 -0500370
371 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
372 self.log.debug('lldp flow add')
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400373 nni_intf_id = yield self.get_nni_intf_id()
William Kurkian6f436d02019-02-06 16:25:01 -0500374 self.add_lldp_flow(flow, port_no, nni_intf_id)
375
376 elif PUSH_VLAN in action:
377 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
378 action, flow, alloc_id, gemport_id)
379 elif POP_VLAN in action:
380 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
381 action, flow, alloc_id, gemport_id)
382 else:
383 self.log.debug('Invalid-flow-type-to-handle',
384 classifier=classifier,
385 action=action, flow=flow)
386
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400387 def get_uni_port_path(self, intf_id, onu_id, uni_id):
388 value = 'pon-{}/onu-{}/uni-{}'.format(intf_id, onu_id, uni_id)
389 return value
390
391 def create_tcont_gemport(self, intf_id, onu_id, uni_id, uni, port_no, table_id):
William Kurkian6f436d02019-02-06 16:25:01 -0500392 alloc_id, gem_port_ids = None, None
393 pon_intf_onu_id = (intf_id, onu_id)
394
395 # If we already have allocated alloc_id and gem_ports earlier, render them
396 alloc_id = \
397 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
398 gem_port_ids = \
399 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
400 if alloc_id is not None and gem_port_ids is not None:
401 return alloc_id, gem_port_ids
402
403 try:
William Kurkian6f436d02019-02-06 16:25:01 -0500404 # FIXME: If table id is <= 63 using 64 as table id
405 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
406 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
407
408 # Check tech profile instance already exists for derived port name
409 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400410 get_tech_profile_instance(table_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500411 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
412
413 if tech_profile_instance is None:
414 # create tech profile instance
415 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400416 create_tech_profile_instance(table_id, uni,
William Kurkian6f436d02019-02-06 16:25:01 -0500417 intf_id)
418 if tech_profile_instance is None:
419 raise Exception('Tech-profile-instance-creation-failed')
420 else:
421 self.log.debug(
422 'Tech-profile-instance-already-exist-for-given port-name',
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400423 table_id=table_id, intf_id=intf_id, uni=uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500424
425 # upstream scheduler
426 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
427 tech_profile_instance)
428 # downstream scheduler
429 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
430 tech_profile_instance)
431 # create Tcont
432 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
433 us_scheduler,
434 ds_scheduler)
435
436 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
437 onu_id=onu_id,
438 uni_id=uni_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400439 port_no=port_no,
William Kurkian6f436d02019-02-06 16:25:01 -0500440 tconts=tconts))
441
442 # Fetch alloc id and gemports from tech profile instance
443 alloc_id = tech_profile_instance.us_scheduler.alloc_id
444 gem_port_ids = []
445 for i in range(len(
446 tech_profile_instance.upstream_gem_port_attribute_list)):
447 gem_port_ids.append(
448 tech_profile_instance.upstream_gem_port_attribute_list[i].
449 gemport_id)
450 except BaseException as e:
451 self.log.exception(exception=e)
452
453 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
454 pon_intf_onu_id = (intf_id, onu_id, uni_id)
455 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
456 pon_intf_onu_id,
457 list([alloc_id])
458 )
459 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
460 pon_intf_onu_id,
461 gem_port_ids
462 )
463
464 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
465 gem_port_ids, intf_id, onu_id, uni_id
466 )
467
468 return alloc_id, gem_port_ids
469
470 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
471 uplink_action, logical_flow, alloc_id,
472 gemport_id):
473
474 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
475
476 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
477 uplink_action, UPSTREAM,
478 logical_flow, alloc_id, gemport_id)
479
480 # Secondary EAP on the subscriber vlan
481 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
482 if eap_active:
483 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
484 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
485
486 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
487 downlink_action, flow, alloc_id, gemport_id):
488 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
489 # Needed ???? It should be already there
490 downlink_action[POP_VLAN] = True
491 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
492
493 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
494 downlink_action, DOWNSTREAM,
495 flow, alloc_id, gemport_id)
496
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400497 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500498 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
499 direction, logical_flow, alloc_id, gemport_id):
500
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400501 self.log.debug('add hisa flow', flow=logical_flow, port_no=port_no,
502 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
503 alloc_id=alloc_id)
504
William Kurkian6f436d02019-02-06 16:25:01 -0500505 flow_store_cookie = self._get_flow_store_cookie(classifier,
506 gemport_id)
507
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400508 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
509 action=action)
510
William Kurkian6f436d02019-02-06 16:25:01 -0500511 # One of the OLT platform (Broadcom BAL) requires that symmetric
512 # flows require the same flow_id to be used across UL and DL.
513 # Since HSIA flow is the only symmetric flow currently, we need to
514 # re-use the flow_id across both direction. The 'flow_category'
515 # takes priority over flow_cookie to find any available HSIA_FLOW
516 # id for the ONU.
517 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
518 flow_store_cookie,
519 HSIA_FLOW)
520 if flow_id is None:
521 self.log.error("hsia-flow-unavailable")
522 return
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400523
524 self.log.debug('flow-id', flow_id=flow_id)
525
526 network_intf_id = yield self.get_nni_intf_id()
527
William Kurkian6f436d02019-02-06 16:25:01 -0500528 flow = openolt_pb2.Flow(
529 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400530 flow_type=direction, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500531 gemport_id=gemport_id,
532 classifier=self.mk_classifier(classifier),
533 action=self.mk_action(action),
534 priority=logical_flow.priority,
535 port_no=port_no,
536 cookie=logical_flow.cookie)
537
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400538 self.log.debug('openolt-agent-flow', hsia_flow=flow)
539
William Kurkian6f436d02019-02-06 16:25:01 -0500540 if self.add_flow_to_device(flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400541 self.log.debug('added-hsia-openolt-agent-flow', hsia_flow=flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500542 flow_info = self._get_flow_info_as_json_blob(flow,
543 flow_store_cookie,
544 HSIA_FLOW)
545 self.update_flow_info_to_kv_store(flow.access_intf_id,
546 flow.onu_id, flow.uni_id,
547 flow.flow_id, flow_info)
548
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400549 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500550 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
551 alloc_id, gemport_id):
552
553 self.log.debug('add dhcp upstream trap', classifier=classifier,
554 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
555
556 action.clear()
557 action[TRAP_TO_HOST] = True
558 classifier[UDP_SRC] = 68
559 classifier[UDP_DST] = 67
560 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
561 classifier.pop(VLAN_VID, None)
562
563 flow_store_cookie = self._get_flow_store_cookie(classifier,
564 gemport_id)
565
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400566 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
567 action=action)
568
William Kurkian6f436d02019-02-06 16:25:01 -0500569 flow_id = self.resource_mgr.get_flow_id(
570 intf_id, onu_id, uni_id, flow_store_cookie
571 )
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400572
573 self.log.debug('flow-id', flow_id=flow_id)
574
575 network_intf_id = yield self.get_nni_intf_id()
576
William Kurkian6f436d02019-02-06 16:25:01 -0500577 dhcp_flow = openolt_pb2.Flow(
578 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
579 access_intf_id=intf_id, gemport_id=gemport_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400580 alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500581 priority=logical_flow.priority,
582 classifier=self.mk_classifier(classifier),
583 action=self.mk_action(action),
584 port_no=port_no,
585 cookie=logical_flow.cookie)
586
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400587 self.log.debug('openolt-agent-flow', dhcp_flow=dhcp_flow)
588
William Kurkian6f436d02019-02-06 16:25:01 -0500589 if self.add_flow_to_device(dhcp_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400590 self.log.debug('added-dhcp-openolt-agent-flow', dhcp_flow=dhcp_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500591 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
592 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
593 dhcp_flow.onu_id,
594 dhcp_flow.uni_id,
595 dhcp_flow.flow_id,
596 flow_info)
597
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400598 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500599 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
600 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
601
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400602 self.log.debug('add eapol upstream trap', flow=logical_flow, port_no=port_no,
603 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
604 alloc_id=alloc_id, vlan_id=vlan_id)
605
William Kurkian6f436d02019-02-06 16:25:01 -0500606 uplink_classifier = dict()
607 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
608 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
609 uplink_classifier[VLAN_VID] = vlan_id
610
611 uplink_action = dict()
612 uplink_action[TRAP_TO_HOST] = True
613
614 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
615 gemport_id)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400616
617 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, uplink_classifier=uplink_classifier,
618 uplink_action=uplink_action)
619
William Kurkian6f436d02019-02-06 16:25:01 -0500620 # Add Upstream EAPOL Flow.
621 uplink_flow_id = self.resource_mgr.get_flow_id(
622 intf_id, onu_id, uni_id, flow_store_cookie
623 )
624
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400625 self.log.debug('flow-id', uplink_flow_id=uplink_flow_id)
626
627 network_intf_id = yield self.get_nni_intf_id()
628
William Kurkian6f436d02019-02-06 16:25:01 -0500629 upstream_flow = openolt_pb2.Flow(
630 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 -0400631 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500632 gemport_id=gemport_id,
633 classifier=self.mk_classifier(uplink_classifier),
634 action=self.mk_action(uplink_action),
635 priority=logical_flow.priority,
636 port_no=port_no,
637 cookie=logical_flow.cookie)
638
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400639 self.log.debug('openolt-agent-flow', upstream_flow=upstream_flow)
640
William Kurkian6f436d02019-02-06 16:25:01 -0500641 logical_flow = copy.deepcopy(logical_flow)
642 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
643 vlan_id | 0x1000)]))
644 logical_flow.match.type = OFPMT_OXM
645
646 if self.add_flow_to_device(upstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400647 self.log.debug('added-eapol-openolt-agent-flow', upstream_flow=upstream_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500648 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
649 flow_store_cookie)
650 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
651 upstream_flow.onu_id,
652 upstream_flow.uni_id,
653 upstream_flow.flow_id,
654 flow_info)
655
656 if vlan_id == DEFAULT_MGMT_VLAN:
657 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
658 # requirement)
659 # On one of the platforms (Broadcom BAL), when same DL classifier
660 # vlan was used across multiple ONUs, eapol flow re-adds after
661 # flow delete (cases of onu reboot/disable) fails.
662 # In order to generate unique vlan, a combination of intf_id
663 # onu_id and uni_id is used.
664 # uni_id defaults to 0, so add 1 to it.
665 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
666 # Assert that we do not generate invalid vlans under no condition
Matt Jeanneretaa360912019-04-22 16:23:12 -0400667 assert (special_vlan_downstream_flow >= 2), 'invalid-vlan-generated'
668 self.log.warn('generating-special-downstream-vlan-for-bal', special_vlan_downstream_flow=special_vlan_downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500669
670 downlink_classifier = dict()
671 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
672 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
673
674 downlink_action = dict()
675 downlink_action[PUSH_VLAN] = True
676 downlink_action[VLAN_VID] = vlan_id
677
678
679 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
680 gemport_id)
681
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400682 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, downlink_classifier=downlink_classifier,
683 downlink_action=downlink_action)
684
William Kurkian6f436d02019-02-06 16:25:01 -0500685 downlink_flow_id = self.resource_mgr.get_flow_id(
686 intf_id, onu_id, uni_id, flow_store_cookie
687 )
688
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400689 self.log.debug('flow-id', downlink_flow_id=downlink_flow_id)
690
William Kurkian6f436d02019-02-06 16:25:01 -0500691 downstream_flow = openolt_pb2.Flow(
692 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 -0400693 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500694 gemport_id=gemport_id,
695 classifier=self.mk_classifier(downlink_classifier),
696 action=self.mk_action(downlink_action),
697 priority=logical_flow.priority,
698 port_no=port_no,
699 cookie=logical_flow.cookie)
700
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400701 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
702
Matt Jeanneretaa360912019-04-22 16:23:12 -0400703 try:
704 downstream_logical_flow = ofp_flow_stats(
705 id=logical_flow.id, cookie=logical_flow.cookie,
706 table_id=logical_flow.table_id, priority=logical_flow.priority,
707 flags=logical_flow.flags)
William Kurkian6f436d02019-02-06 16:25:01 -0500708
Matt Jeanneretaa360912019-04-22 16:23:12 -0400709 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
710 fd.in_port(fd.get_out_port(logical_flow)),
711 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
712 downstream_logical_flow.match.type = OFPMT_OXM
William Kurkian6f436d02019-02-06 16:25:01 -0500713
Matt Jeanneretaa360912019-04-22 16:23:12 -0400714 downstream_logical_flow.instructions.extend(
715 fd.mk_instructions_from_actions([fd.output(
716 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
717 except Exception as e:
718 self.log.exception("unexpected-error-building-downstream-logical-flow", intf_id=intf_id, onu_id=onu_id,
719 uni_id=uni_id, e=e, downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500720
721 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400722 self.log.debug('added-eapol-openolt-agent-flow', downstream_flow=downstream_flow,
723 downstream_logical_flow=downstream_logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500724 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
725 flow_store_cookie)
726 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
727 downstream_flow.onu_id,
728 downstream_flow.uni_id,
729 downstream_flow.flow_id,
730 flow_info)
731
732 def repush_all_different_flows(self):
733 # Check if the device is supposed to have flows, if so add them
734 # Recover static flows after a reboot
735 logical_flows = self.logical_flows_proxy.get('/').items
736 devices_flows = self.flows_proxy.get('/').items
737 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
738 for logical_flow in logical_flows:
739 try:
740 if logical_flow.id not in logical_flows_ids_provisioned:
741 self.add_flow(logical_flow)
742 except Exception as e:
743 self.log.exception('Problem reading this flow', e=e)
744
745 def reset_flows(self):
746 self.flows_proxy.update('/', Flows())
747
748 """ Add a downstream LLDP trap flow on the NNI interface
749 """
750
751 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
752
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400753 self.log.debug('add lldp trap flow', flow=logical_flow, port_no=port_no,
754 network_intf_id=network_intf_id)
755
William Kurkian6f436d02019-02-06 16:25:01 -0500756 classifier = dict()
757 classifier[ETH_TYPE] = LLDP_ETH_TYPE
758 classifier[PACKET_TAG_TYPE] = UNTAGGED
759 action = dict()
760 action[TRAP_TO_HOST] = True
761
762 # LLDP flow is installed to trap LLDP packets on the NNI port.
763 # We manage flow_id resource pool on per PON port basis.
764 # Since this situation is tricky, as a hack, we pass the NNI port
765 # index (network_intf_id) as PON port Index for the flow_id resource
766 # pool. Also, there is no ONU Id available for trapping LLDP packets
767 # on NNI port, use onu_id as -1 (invalid)
768 # ****************** CAVEAT *******************
769 # This logic works if the NNI Port Id falls within the same valid
770 # range of PON Port Ids. If this doesn't work for some OLT Vendor
771 # we need to have a re-look at this.
772 # *********************************************
773 onu_id = -1
774 uni_id = -1
775 flow_store_cookie = self._get_flow_store_cookie(classifier)
776 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
777 flow_store_cookie)
778
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400779 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
780 action=action)
781
William Kurkian6f436d02019-02-06 16:25:01 -0500782 downstream_flow = openolt_pb2.Flow(
783 access_intf_id=-1, # access_intf_id not required
784 onu_id=onu_id, # onu_id not required
785 uni_id=uni_id, # uni_id not used
786 flow_id=flow_id,
787 flow_type=DOWNSTREAM,
788 network_intf_id=network_intf_id,
789 gemport_id=-1, # gemport_id not required
790 classifier=self.mk_classifier(classifier),
791 action=self.mk_action(action),
792 priority=logical_flow.priority,
793 port_no=port_no,
794 cookie=logical_flow.cookie)
795
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400796 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
797
William Kurkian6f436d02019-02-06 16:25:01 -0500798 if self.add_flow_to_device(downstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400799 self.log.debug('added-lldp-openolt-agent-flow', downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500800 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
801 flow_id, downstream_flow)
802
803 def mk_classifier(self, classifier_info):
804
805 classifier = openolt_pb2.Classifier()
806
807 if ETH_TYPE in classifier_info:
808 classifier.eth_type = classifier_info[ETH_TYPE]
809 if IP_PROTO in classifier_info:
810 classifier.ip_proto = classifier_info[IP_PROTO]
811 if VLAN_VID in classifier_info:
812 classifier.o_vid = classifier_info[VLAN_VID]
813 if METADATA in classifier_info:
814 classifier.i_vid = classifier_info[METADATA]
815 if VLAN_PCP in classifier_info:
816 classifier.o_pbits = classifier_info[VLAN_PCP]
817 if UDP_SRC in classifier_info:
818 classifier.src_port = classifier_info[UDP_SRC]
819 if UDP_DST in classifier_info:
820 classifier.dst_port = classifier_info[UDP_DST]
821 if IPV4_DST in classifier_info:
822 classifier.dst_ip = classifier_info[IPV4_DST]
823 if IPV4_SRC in classifier_info:
824 classifier.src_ip = classifier_info[IPV4_SRC]
825 if PACKET_TAG_TYPE in classifier_info:
826 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
827 classifier.pkt_tag_type = SINGLE_TAG
828 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
829 classifier.pkt_tag_type = DOUBLE_TAG
830 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
831 classifier.pkt_tag_type = UNTAGGED
832 else:
833 classifier.pkt_tag_type = 'none'
834
835 return classifier
836
837 def mk_action(self, action_info):
838 action = openolt_pb2.Action()
839
840 if POP_VLAN in action_info:
841 action.o_vid = action_info[VLAN_VID]
842 action.cmd.remove_outer_tag = True
843 elif PUSH_VLAN in action_info:
844 action.o_vid = action_info[VLAN_VID]
845 action.cmd.add_outer_tag = True
846 elif TRAP_TO_HOST in action_info:
847 action.cmd.trap_to_host = True
848 else:
849 self.log.info('Invalid-action-field', action_info=action_info)
850 return
851 return action
852
853 def is_eap_enabled(self, intf_id, onu_id, uni_id):
854 flows = self.logical_flows_proxy.get('/').items
855
856 for flow in flows:
857 eap_flow = False
858 eap_intf_id = None
859 eap_onu_id = None
860 eap_uni_id = None
861 for field in fd.get_ofb_fields(flow):
862 if field.type == fd.ETH_TYPE:
863 if field.eth_type == EAP_ETH_TYPE:
864 eap_flow = True
865 if field.type == fd.IN_PORT:
866 eap_intf_id = self.platform.intf_id_from_uni_port_num(
867 field.port)
868 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
869 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
870
871 if eap_flow:
872 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
873 intf_id=intf_id, eap_intf_id=eap_intf_id,
874 eap_onu_id=eap_onu_id,
875 eap_uni_id=eap_uni_id)
876 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id:
877 return True, flow
878
879 return False, None
880
881 def get_subscriber_vlan(self, port):
882 self.log.debug('looking from subscriber flow for port', port=port)
883
884 flows = self.logical_flows_proxy.get('/').items
885 for flow in flows:
886 in_port = fd.get_in_port(flow)
887 out_port = fd.get_out_port(flow)
888 if in_port == port and out_port is not None and \
889 self.platform.intf_id_to_port_type_name(out_port) \
890 == Port.ETHERNET_NNI:
891 fields = fd.get_ofb_fields(flow)
892 self.log.debug('subscriber flow found', fields=fields)
893 for field in fields:
894 if field.type == OFPXMT_OFB_VLAN_VID:
895 self.log.debug('subscriber vlan found',
896 vlan_id=field.vlan_vid)
897 return field.vlan_vid & 0x0fff
898 self.log.debug('No subscriber flow found', port=port)
899 return None
900
901 def add_flow_to_device(self, flow, logical_flow):
902 self.log.debug('pushing flow to device', flow=flow)
903 try:
904 self.stub.FlowAdd(flow)
905 except grpc.RpcError as grpc_e:
906 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
907 self.log.warn('flow already exists', e=grpc_e, flow=flow)
908 else:
909 self.log.error('failed to add flow',
910 logical_flow=logical_flow, flow=flow,
911 grpc_error=grpc_e)
912 return False
Matt Jeanneretaa360912019-04-22 16:23:12 -0400913 except Exception as f:
914 self.log.exception("unexpected-openolt-agent-error", flow=flow, logical_flow=logical_flow, f=f)
William Kurkian6f436d02019-02-06 16:25:01 -0500915 else:
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400916 # TODO NEW CORE: Should not need. Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
917 # self.register_flow(logical_flow, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500918 return True
919
920 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400921 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 -0500922 self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id,
923 flow_id, flow)
924
925 def register_flow(self, logical_flow, device_flow):
926 self.log.debug('registering flow in device',
927 logical_flow=logical_flow, device_flow=device_flow)
928 stored_flow = copy.deepcopy(logical_flow)
929 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
930 device_flow.flow_type)
931 self.log.debug('generated device flow id', id=stored_flow.id,
932 flow_id=device_flow.flow_id,
933 direction=device_flow.flow_type)
934 stored_flow.cookie = logical_flow.id
935 flows = self.flows_proxy.get('/')
936 flows.items.extend([stored_flow])
937 self.flows_proxy.update('/', flows)
938
939 def find_next_flow(self, flow):
940 table_id = fd.get_goto_table_id(flow)
941 metadata = 0
942 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
943 # 1.13.5 and later, the lower 32-bits is the output port number and the
944 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
945 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
946
947 for field in fd.get_ofb_fields(flow):
948 if field.type == fd.METADATA:
949 metadata = field.table_metadata & 0xFFFFFFFF
950 if table_id is None:
951 return None
952 flows = self.logical_flows_proxy.get('/').items
953 next_flows = []
954 for f in flows:
955 if f.table_id == table_id:
956 # FIXME
957 if fd.get_in_port(f) == fd.get_in_port(flow) and \
958 fd.get_out_port(f) == metadata:
959 next_flows.append(f)
960
961 if len(next_flows) == 0:
962 self.log.warning('no next flow found, it may be a timing issue',
963 flow=flow, number_of_flows=len(flows))
964 if flow.id in self.retry_add_flow_list:
965 self.log.debug('flow is already in retry list', flow_id=flow.id)
966 else:
967 self.retry_add_flow_list.append(flow.id)
968 reactor.callLater(5, self.retry_add_flow, flow)
969 return None
970
971 next_flows.sort(key=lambda f: f.priority, reverse=True)
972
973 return next_flows[0]
974
975 def update_children_flows(self, device_rules_map):
976
977 for device_id, (flows, groups) in device_rules_map.iteritems():
978 if device_id != self.device_id:
979 self.root_proxy.update('/devices/{}/flows'.format(device_id),
980 Flows(items=flows.values()))
981 self.root_proxy.update('/devices/{}/flow_groups'.format(
982 device_id), FlowGroups(items=groups.values()))
983
984 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
985 ofp_port_name = logical_port.ofp_port.name
986 port_no = logical_port.ofp_port.port_no
987 pon_port = child_device.proxy_address.channel_id
988 onu_id = child_device.proxy_address.onu_id
989 uni_id = self.platform.uni_id_from_port_num(logical_port)
990
991 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
992 # is probably to maintain a list of Tech-profile table IDs associated
993 # with the UNI logical_port. This way, when the logical port is deleted,
994 # all the associated tech-profile configuration with the UNI logical_port
995 # can be cleared.
996 tech_profile_instance = self.tech_profile[pon_port]. \
997 get_tech_profile_instance(
998 DEFAULT_TECH_PROFILE_TABLE_ID,
999 ofp_port_name)
1000 flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id)
1001 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
1002 for flow_id in flow_ids:
1003 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
1004 for flow_info in flow_infos:
1005 direction = flow_info['flow_type']
1006 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
1007 flow_type=direction)
1008 try:
1009 self.stub.FlowRemove(flow_to_remove)
1010 except grpc.RpcError as grpc_e:
1011 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
1012 self.log.debug('This flow does not exist on the switch, '
1013 'normal after an OLT reboot',
1014 flow=flow_to_remove)
1015 else:
1016 raise grpc_e
1017
1018 self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id)
1019
1020 try:
1021 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
1022 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
1023 onu_id=onu_id,
1024 uni_id=uni_id,
1025 port_no=port_no,
1026 tconts=tconts))
1027 except grpc.RpcError as grpc_e:
1028 self.log.error('error-removing-tcont-scheduler-queues',
1029 err=grpc_e)
1030
1031 def generate_stored_id(self, flow_id, direction):
1032 if direction == UPSTREAM:
1033 self.log.debug('upstream flow, shifting id')
1034 return 0x1 << 15 | flow_id
1035 elif direction == DOWNSTREAM:
1036 self.log.debug('downstream flow, not shifting id')
1037 return flow_id
1038 else:
1039 self.log.warn('Unrecognized direction', direction=direction)
1040 return flow_id
1041
1042 def decode_stored_id(self, id):
1043 if id >> 15 == 0x1:
1044 return id & 0x7fff, UPSTREAM
1045 else:
1046 return id, DOWNSTREAM
1047
1048 def _populate_tech_profile_per_pon_port(self):
1049 for arange in self.resource_mgr.device_info.ranges:
1050 for intf_id in arange.intf_ids:
1051 self.tech_profile[intf_id] = \
1052 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1053
1054 # Make sure we have as many tech_profiles as there are pon ports on
1055 # the device
1056 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1057
1058 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1059 flow_category=None):
1060 json_blob = MessageToDict(message=flow,
1061 preserving_proto_field_name=True)
1062 self.log.debug("flow-info", json_blob=json_blob)
1063 json_blob['flow_store_cookie'] = flow_store_cookie
1064 if flow_category is not None:
1065 json_blob['flow_category'] = flow_category
1066 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1067 flow.onu_id, flow.uni_id, flow.flow_id)
1068
1069 if flow_info is None:
1070 flow_info = list()
1071 flow_info.append(json_blob)
1072 else:
1073 assert (isinstance(flow_info, list))
1074 flow_info.append(json_blob)
1075
1076 return flow_info
1077
1078 @staticmethod
1079 def _get_flow_store_cookie(classifier, gem_port=None):
1080 assert isinstance(classifier, dict)
1081 # We need unique flows per gem_port
1082 if gem_port is not None:
1083 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1084 else:
1085 to_hash = dumps(classifier, sort_keys=True)
1086 return hashlib.md5(to_hash).hexdigest()[:12]
1087
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001088 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001089 def get_nni_intf_id(self):
1090 if self.nni_intf_id is not None:
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001091 returnValue(self.nni_intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001092
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001093 port_list = yield self.core_proxy.get_ports(self.device_id, Port.ETHERNET_NNI)
1094 self.log.debug("nni-ports-list", port_list=port_list)
1095
1096 # TODO: Hardcoded only first NNI
1097 port = port_list.items[0]
1098
1099 self.log.debug("nni-port", port=port)
1100 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(port.port_no)
1101
1102 self.log.debug("nni-intf-d ", port=port.port_no, nni_intf_id=self.nni_intf_id)
1103 returnValue(self.nni_intf_id)