blob: 7dc00c8391e2e670ad40000b309764b17f4de4f3 [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import copy
17from twisted.internet import reactor
18import grpc
19from google.protobuf.json_format import MessageToDict
20import hashlib
21from simplejson import dumps
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040022from twisted.internet.defer import inlineCallbacks, returnValue
William Kurkian6f436d02019-02-06 16:25:01 -050023
William Kurkian8b1690c2019-03-04 16:53:22 -050024from voltha_protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
William Kurkian6f436d02019-02-06 16:25:01 -050025 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
26 OFPXMT_OFB_VLAN_VID
William Kurkian8b1690c2019-03-04 16:53:22 -050027from voltha_protos.device_pb2 import Port
William Kurkian44cd7bb2019-02-11 16:39:12 -050028import pyvoltha.common.openflow.utils as fd
William Kurkian8b1690c2019-03-04 16:53:22 -050029from voltha_protos import openolt_pb2
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040030from voltha_protos.inter_container_pb2 import SwitchCapability, PortCapability, \
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -040031 InterAdapterMessageType, InterAdapterOmciMessage, InterAdapterTechProfileDownloadMessage
William Kurkian6f436d02019-02-06 16:25:01 -050032
William Kurkian44cd7bb2019-02-11 16:39:12 -050033from pyvoltha.common.tech_profile.tech_profile import DEFAULT_TECH_PROFILE_TABLE_ID
William Kurkian6f436d02019-02-06 16:25:01 -050034
35# Flow categories
36HSIA_FLOW = "HSIA_FLOW"
37
38EAP_ETH_TYPE = 0x888e
39LLDP_ETH_TYPE = 0x88cc
40
41IGMP_PROTO = 2
42
43# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
44DEFAULT_MGMT_VLAN = 4091
45
46# Openolt Flow
47UPSTREAM = "upstream"
48DOWNSTREAM = "downstream"
49PACKET_TAG_TYPE = "pkt_tag_type"
50UNTAGGED = "untagged"
51SINGLE_TAG = "single_tag"
52DOUBLE_TAG = "double_tag"
53
54# Classifier
55ETH_TYPE = 'eth_type'
56TPID = 'tpid'
57IP_PROTO = 'ip_proto'
58IN_PORT = 'in_port'
59VLAN_VID = 'vlan_vid'
60VLAN_PCP = 'vlan_pcp'
61UDP_DST = 'udp_dst'
62UDP_SRC = 'udp_src'
63IPV4_DST = 'ipv4_dst'
64IPV4_SRC = 'ipv4_src'
65METADATA = 'metadata'
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -040066TUNNEL_ID = 'tunnel_id'
William Kurkian6f436d02019-02-06 16:25:01 -050067OUTPUT = 'output'
68# Action
69POP_VLAN = 'pop_vlan'
70PUSH_VLAN = 'push_vlan'
71TRAP_TO_HOST = 'trap_to_host'
72
73
74class OpenOltFlowMgr(object):
75
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040076 def __init__(self, core_proxy, adapter_proxy, log, stub, device_id, logical_device_id,
William Kurkian6f436d02019-02-06 16:25:01 -050077 platform, resource_mgr):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -040078 self.core_proxy = core_proxy
79 self.adapter_proxy = adapter_proxy
William Kurkian6f436d02019-02-06 16:25:01 -050080 self.log = log
81 self.stub = stub
82 self.device_id = device_id
83 self.logical_device_id = logical_device_id
84 self.nni_intf_id = None
85 self.platform = platform
William Kurkian6f436d02019-02-06 16:25:01 -050086 self.resource_mgr = resource_mgr
87 self.tech_profile = dict()
88 self._populate_tech_profile_per_pon_port()
89 self.retry_add_flow_list = []
90
91 def add_flow(self, flow):
92 self.log.debug('add flow', flow=flow)
93 classifier_info = dict()
94 action_info = dict()
95
96 for field in fd.get_ofb_fields(flow):
97 if field.type == fd.ETH_TYPE:
98 classifier_info[ETH_TYPE] = field.eth_type
99 self.log.debug('field-type-eth-type',
100 eth_type=classifier_info[ETH_TYPE])
101 elif field.type == fd.IP_PROTO:
102 classifier_info[IP_PROTO] = field.ip_proto
103 self.log.debug('field-type-ip-proto',
104 ip_proto=classifier_info[IP_PROTO])
105 elif field.type == fd.IN_PORT:
106 classifier_info[IN_PORT] = field.port
107 self.log.debug('field-type-in-port',
108 in_port=classifier_info[IN_PORT])
109 elif field.type == fd.VLAN_VID:
110 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
111 self.log.debug('field-type-vlan-vid',
112 vlan=classifier_info[VLAN_VID])
113 elif field.type == fd.VLAN_PCP:
114 classifier_info[VLAN_PCP] = field.vlan_pcp
115 self.log.debug('field-type-vlan-pcp',
116 pcp=classifier_info[VLAN_PCP])
117 elif field.type == fd.UDP_DST:
118 classifier_info[UDP_DST] = field.udp_dst
119 self.log.debug('field-type-udp-dst',
120 udp_dst=classifier_info[UDP_DST])
121 elif field.type == fd.UDP_SRC:
122 classifier_info[UDP_SRC] = field.udp_src
123 self.log.debug('field-type-udp-src',
124 udp_src=classifier_info[UDP_SRC])
125 elif field.type == fd.IPV4_DST:
126 classifier_info[IPV4_DST] = field.ipv4_dst
127 self.log.debug('field-type-ipv4-dst',
128 ipv4_dst=classifier_info[IPV4_DST])
129 elif field.type == fd.IPV4_SRC:
130 classifier_info[IPV4_SRC] = field.ipv4_src
131 self.log.debug('field-type-ipv4-src',
132 ipv4_dst=classifier_info[IPV4_SRC])
133 elif field.type == fd.METADATA:
134 classifier_info[METADATA] = field.table_metadata
135 self.log.debug('field-type-metadata',
136 metadata=classifier_info[METADATA])
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400137 elif field.type == fd.TUNNEL_ID:
138 classifier_info[TUNNEL_ID] = field.tunnel_id
139 self.log.debug('field-type-tunnel-id',
140 tunnel_id=classifier_info[TUNNEL_ID])
William Kurkian6f436d02019-02-06 16:25:01 -0500141 else:
142 raise NotImplementedError('field.type={}'.format(
143 field.type))
144
145 for action in fd.get_actions(flow):
146 if action.type == fd.OUTPUT:
147 action_info[OUTPUT] = action.output.port
148 self.log.debug('action-type-output',
149 output=action_info[OUTPUT],
150 in_port=classifier_info[IN_PORT])
151 elif action.type == fd.POP_VLAN:
William Kurkian6f436d02019-02-06 16:25:01 -0500152 action_info[POP_VLAN] = True
153 self.log.debug('action-type-pop-vlan',
154 in_port=classifier_info[IN_PORT])
155 elif action.type == fd.PUSH_VLAN:
156 action_info[PUSH_VLAN] = True
157 action_info[TPID] = action.push.ethertype
158 self.log.debug('action-type-push-vlan',
159 push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT])
160 if action.push.ethertype != 0x8100:
161 self.log.error('unhandled-tpid',
162 ethertype=action.push.ethertype)
163 elif action.type == fd.SET_FIELD:
164 # action_info['action_type'] = 'set_field'
165 _field = action.set_field.field.ofb_field
166 assert (action.set_field.field.oxm_class ==
167 OFPXMC_OPENFLOW_BASIC)
168 self.log.debug('action-type-set-field',
169 field=_field, in_port=classifier_info[IN_PORT])
170 if _field.type == fd.VLAN_VID:
171 self.log.debug('set-field-type-vlan-vid',
172 vlan_vid=_field.vlan_vid & 0xfff)
173 action_info[VLAN_VID] = (_field.vlan_vid & 0xfff)
174 else:
175 self.log.error('unsupported-action-set-field-type',
176 field_type=_field.type)
177 else:
178 self.log.error('unsupported-action-type',
179 action_type=action.type, in_port=classifier_info[IN_PORT])
180
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400181 # controller bound trap flows.
182 if self.platform.is_controller(action_info[OUTPUT]):
183 # trap from pon port. figure out uni port from tunnel id and set as in port
184 if self.platform.intf_id_to_port_type_name(classifier_info[IN_PORT]) == Port.PON_OLT:
185 if fd.get_child_port_from_tunnelid(flow) is not None:
186 classifier_info[IN_PORT] = fd.get_child_port_from_tunnelid(flow)
187 self.log.debug('pon-to-controller-flow-port-in-tunnelid', new_in_port=classifier_info[IN_PORT])
188 else:
189 self.log.debug('pon-to-controller-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
190 out_port=action_info[OUTPUT])
191 # TODO NEW CORE: trap from nni port.
192 else:
193 self.log.warn('nni-to-controller-trap-unsupported', flow=flow)
194 else:
195 # do not operate on the private decomposer vlan. neither onu nor agg switch adds it.
196 # cannot do anything with this flow
197 if VLAN_VID in classifier_info and classifier_info[VLAN_VID] == 4000:
198 self.log.debug('skipping-private-vlan', in_port=classifier_info[IN_PORT],
199 out_port=action_info[OUTPUT])
William Kurkian6f436d02019-02-06 16:25:01 -0500200 return
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400201
202 # downstream nni port to pon port. figure out uni port from tunnel id and set as out port
203 if self.platform.intf_id_to_port_type_name(action_info[OUTPUT]) == Port.PON_OLT:
204 if fd.get_child_port_from_tunnelid(flow) is not None:
205 action_info[OUTPUT] = fd.get_child_port_from_tunnelid(flow)
206 self.log.debug('downstream-pon-port-flow-port-in-tunnelid', new_out_port=action_info[OUTPUT])
207 else:
208 self.log.debug('downstream-pon-port-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
209 out_port=action_info[OUTPUT])
210 return
211
212 # upstream pon port to nni port. figure out uni port from tunnel id and set as in port
213 if self.platform.intf_id_to_port_type_name(classifier_info[IN_PORT]) == Port.PON_OLT:
214 if fd.get_child_port_from_tunnelid(flow) is not None:
215 classifier_info[IN_PORT] = fd.get_child_port_from_tunnelid(flow)
216 self.log.debug('upstream-pon-port-flow-port-in-tunnelid', new_in_port=classifier_info[IN_PORT])
217 else:
218 self.log.debug('upstream-pon-port-flow-NO-PORT-in-tunnelid', in_port=classifier_info[IN_PORT],
219 out_port=action_info[OUTPUT])
220 return
William Kurkian6f436d02019-02-06 16:25:01 -0500221
222 self.log.debug('flow-ports', classifier_inport=classifier_info[IN_PORT], action_output=action_info[OUTPUT])
223 (port_no, intf_id, onu_id, uni_id) = self.platform.extract_access_from_flow(
224 classifier_info[IN_PORT], action_info[OUTPUT])
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400225
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400226 self.log.debug('extracted-flow-ports', port_no=port_no, intf_id=intf_id, onu_id=onu_id, uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500227
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400228 # TODO NEW CORE: this needs to be broken into onu type flows that need these ID, and NNI flows that do not
William Kurkian6f436d02019-02-06 16:25:01 -0500229 self.divide_and_add_flow(intf_id, onu_id, uni_id, port_no, classifier_info,
William Kurkian23047b92019-05-01 11:02:35 -0400230 action_info, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500231
232 def _clear_flow_id_from_rm(self, flow, flow_id, flow_direction):
233 uni_port_no = None
234 child_device_id = None
235 if flow_direction == UPSTREAM:
236 for field in fd.get_ofb_fields(flow):
237 if field.type == fd.IN_PORT:
238 is_uni, child_device_id = self._is_uni_port(field.port)
239 if is_uni:
240 uni_port_no = field.port
241 elif flow_direction == DOWNSTREAM:
242 for field in fd.get_ofb_fields(flow):
243 if field.type == fd.METADATA:
244 uni_port = field.table_metadata & 0xFFFFFFFF
245 is_uni, child_device_id = self._is_uni_port(uni_port)
246 if is_uni:
247 uni_port_no = field.port
248
249 if uni_port_no is None:
250 for action in fd.get_actions(flow):
251 if action.type == fd.OUTPUT:
252 is_uni, child_device_id = \
253 self._is_uni_port(action.output.port)
254 if is_uni:
255 uni_port_no = action.output.port
256
257 if child_device_id:
258 child_device = self.adapter_agent.get_device(child_device_id)
259 pon_intf = child_device.proxy_address.channel_id
260 onu_id = child_device.proxy_address.onu_id
261 uni_id = self.platform.uni_id_from_port_num(uni_port_no) if uni_port_no is not None else None
262 flows = self.resource_mgr.get_flow_id_info(pon_intf, onu_id, uni_id, flow_id)
263 assert (isinstance(flows, list))
264 self.log.debug("retrieved-flows", flows=flows)
265 for idx in range(len(flows)):
266 if flow_direction == flows[idx]['flow_type']:
267 flows.pop(idx)
268 self.update_flow_info_to_kv_store(pon_intf, onu_id, uni_id, flow_id, flows)
269 if len(flows) > 0:
270 # There are still flows referencing the same flow_id.
271 # So the flow should not be freed yet.
272 # For ex: Case of HSIA where same flow is shared
273 # between DS and US.
274 return
275
276 self.resource_mgr.free_flow_id_for_uni(pon_intf, onu_id, uni_id, flow_id)
277 else:
278 self.log.error("invalid-info", uni_port_no=uni_port_no,
279 child_device_id=child_device_id)
280
281 def retry_add_flow(self, flow):
282 self.log.debug("retry-add-flow")
283 if flow.id in self.retry_add_flow_list:
284 self.retry_add_flow_list.remove(flow.id)
285 self.add_flow(flow)
286
287 def remove_flow(self, flow):
288 self.log.debug('trying to remove flows from logical flow :',
289 logical_flow=flow)
290 device_flows_to_remove = []
291 device_flows = self.flows_proxy.get('/').items
292 for f in device_flows:
293 if f.cookie == flow.id:
294 device_flows_to_remove.append(f)
295
296 for f in device_flows_to_remove:
297 (id, direction) = self.decode_stored_id(f.id)
298 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
299 try:
300 self.stub.FlowRemove(flow_to_remove)
301 except grpc.RpcError as grpc_e:
302 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
303 self.log.debug('This flow does not exist on the switch, '
304 'normal after an OLT reboot',
305 flow=flow_to_remove)
306 else:
307 raise grpc_e
308
309 # once we have successfully deleted the flow on the device
310 # release the flow_id on resource pool and also clear any
311 # data associated with the flow_id on KV store.
312 self._clear_flow_id_from_rm(f, id, direction)
313 self.log.debug('flow removed from device', flow=f,
314 flow_key=flow_to_remove)
315
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400316 def get_tp_path(self, intf_id, uni):
William Kurkian6f436d02019-02-06 16:25:01 -0500317 # FIXME Should get Table id form the flow, as of now hardcoded to
318 # DEFAULT_TECH_PROFILE_TABLE_ID (64)
319 # 'tp_path' contains the suffix part of the tech_profile_instance path.
320 # The prefix to the 'tp_path' should be set to \
321 # TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
322 return self.tech_profile[intf_id]. \
323 get_tp_path(DEFAULT_TECH_PROFILE_TABLE_ID,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400324 uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500325
326 def delete_tech_profile_instance(self, intf_id, onu_id, uni_id):
327 # Remove the TP instance associated with the ONU
328 ofp_port_name = self._get_ofp_port_name(intf_id, onu_id, uni_id)
329 tp_path = self.get_tp_path(intf_id, ofp_port_name)
330 return self.tech_profile[intf_id].delete_tech_profile_instance(tp_path)
331
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400332 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500333 def divide_and_add_flow(self, intf_id, onu_id, uni_id, port_no, classifier,
334 action, flow):
335
336 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, port_no=port_no,
337 classifier=classifier, action=action)
338
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400339 uni = self.get_uni_port_path(intf_id, onu_id, uni_id)
340
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400341 # TODO: if there is no onu_id then the flows pushed are NNI based and belows flow pushes need to be refactored
342 if (onu_id > 0):
343 alloc_id, gem_ports = self.create_tcont_gemport(intf_id, onu_id, uni_id,
344 uni, port_no, flow.table_id)
345 if alloc_id is None or gem_ports is None:
346 self.log.error("alloc-id-gem-ports-unavailable", alloc_id=alloc_id, gem_ports=gem_ports)
347 return
William Kurkian6f436d02019-02-06 16:25:01 -0500348
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400349 self.log.debug('Generated required alloc and gemport ids', alloc_id=alloc_id, gemports=gem_ports)
350 else:
351 alloc_id = -1
352 gem_ports = []
353 self.log.error('cannot-generate-alloc-gem-id-for-flow', intf_id=intf_id, onu_id=onu_id, uni_id=uni_id)
William Kurkian6f436d02019-02-06 16:25:01 -0500354
Matt Jeanneret90e0a7a2019-04-14 11:33:38 -0400355 # TODO: if there are no usable onu, uni, alloc, or gem id, then the below flows are erroneous. this needs a refactor
William Kurkian6f436d02019-02-06 16:25:01 -0500356 # Flows can't be added specific to gemport unless p-bits are received.
357 # Hence adding flows for all gemports
358 for gemport_id in gem_ports:
359 if IP_PROTO in classifier:
360 if classifier[IP_PROTO] == 17:
361 self.log.debug('dhcp flow add')
362 self.add_dhcp_trap(intf_id, onu_id, uni_id, port_no, classifier,
363 action, flow, alloc_id, gemport_id)
364 elif classifier[IP_PROTO] == 2:
365 self.log.warn('igmp flow add ignored, not implemented yet')
366 else:
367 self.log.warn("Invalid-Classifier-to-handle",
368 classifier=classifier,
369 action=action)
370 elif ETH_TYPE in classifier:
371 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
372 self.log.debug('eapol flow add')
373 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, flow, alloc_id,
374 gemport_id)
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400375
376 # TODO NEW CORE: Skip trying to add subsequent eap capture for subscriber vlan
377 # (later attempts at re-eap)
378 #vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
379 #if vlan_id is not None:
380 # self.add_eapol_flow(
381 # intf_id, onu_id, uni_id, port_no, flow, alloc_id, gemport_id,
382 # vlan_id=vlan_id)
383 parent_port_no = self.platform.intf_id_to_port_no(intf_id, Port.PON_OLT)
Matt Jeanneretaa360912019-04-22 16:23:12 -0400384
385 self.log.debug('get-child-device', intf_id=intf_id, onu_id=onu_id,
386 parent_port_no=parent_port_no, device_id=self.device_id)
387
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400388 onu_device = yield self.core_proxy.get_child_device(self.device_id,
Matt Jeanneretaa360912019-04-22 16:23:12 -0400389 onu_id=int(onu_id),
390 parent_port_no=int(parent_port_no))
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400391 tp_path = self.get_tp_path(intf_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500392
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400393 tech_msg = InterAdapterTechProfileDownloadMessage(uni_id=uni_id, path=tp_path)
William Kurkian6f436d02019-02-06 16:25:01 -0500394
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400395 self.log.debug('Load-tech-profile-request-to-brcm-handler',
396 onu_device=onu_device, tp_path=tp_path, tech_msg=tech_msg)
397
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400398 # Send the tech profile event to the onu adapter
Matt Jeanneretdc28e8d2019-04-12 19:25:00 -0400399 yield self.adapter_proxy.send_inter_adapter_message(
400 msg=tech_msg,
401 type=InterAdapterMessageType.TECH_PROFILE_DOWNLOAD_REQUEST,
402 from_adapter="openolt",
403 to_adapter=onu_device.type,
404 to_device_id=onu_device.id
405 )
William Kurkian6f436d02019-02-06 16:25:01 -0500406
407 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
408 self.log.debug('lldp flow add')
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400409 nni_intf_id = yield self.get_nni_intf_id()
William Kurkian6f436d02019-02-06 16:25:01 -0500410 self.add_lldp_flow(flow, port_no, nni_intf_id)
411
412 elif PUSH_VLAN in action:
413 self.add_upstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
414 action, flow, alloc_id, gemport_id)
415 elif POP_VLAN in action:
416 self.add_downstream_data_flow(intf_id, onu_id, uni_id, port_no, classifier,
417 action, flow, alloc_id, gemport_id)
418 else:
419 self.log.debug('Invalid-flow-type-to-handle',
420 classifier=classifier,
421 action=action, flow=flow)
422
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400423 def get_uni_port_path(self, intf_id, onu_id, uni_id):
424 value = 'pon-{}/onu-{}/uni-{}'.format(intf_id, onu_id, uni_id)
425 return value
426
427 def create_tcont_gemport(self, intf_id, onu_id, uni_id, uni, port_no, table_id):
William Kurkian6f436d02019-02-06 16:25:01 -0500428 alloc_id, gem_port_ids = None, None
429 pon_intf_onu_id = (intf_id, onu_id)
430
431 # If we already have allocated alloc_id and gem_ports earlier, render them
432 alloc_id = \
433 self.resource_mgr.get_current_alloc_ids_for_onu(pon_intf_onu_id)
434 gem_port_ids = \
435 self.resource_mgr.get_current_gemport_ids_for_onu(pon_intf_onu_id)
436 if alloc_id is not None and gem_port_ids is not None:
437 return alloc_id, gem_port_ids
438
439 try:
William Kurkian6f436d02019-02-06 16:25:01 -0500440 # FIXME: If table id is <= 63 using 64 as table id
441 if table_id < DEFAULT_TECH_PROFILE_TABLE_ID:
442 table_id = DEFAULT_TECH_PROFILE_TABLE_ID
443
444 # Check tech profile instance already exists for derived port name
445 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400446 get_tech_profile_instance(table_id, uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500447 self.log.debug('Get-tech-profile-instance-status', tech_profile_instance=tech_profile_instance)
448
449 if tech_profile_instance is None:
450 # create tech profile instance
451 tech_profile_instance = self.tech_profile[intf_id]. \
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400452 create_tech_profile_instance(table_id, uni,
William Kurkian6f436d02019-02-06 16:25:01 -0500453 intf_id)
454 if tech_profile_instance is None:
455 raise Exception('Tech-profile-instance-creation-failed')
456 else:
457 self.log.debug(
458 'Tech-profile-instance-already-exist-for-given port-name',
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400459 table_id=table_id, intf_id=intf_id, uni=uni)
William Kurkian6f436d02019-02-06 16:25:01 -0500460
461 # upstream scheduler
462 us_scheduler = self.tech_profile[intf_id].get_us_scheduler(
463 tech_profile_instance)
464 # downstream scheduler
465 ds_scheduler = self.tech_profile[intf_id].get_ds_scheduler(
466 tech_profile_instance)
467 # create Tcont
468 tconts = self.tech_profile[intf_id].get_tconts(tech_profile_instance,
469 us_scheduler,
470 ds_scheduler)
471
472 self.stub.CreateTconts(openolt_pb2.Tconts(intf_id=intf_id,
473 onu_id=onu_id,
474 uni_id=uni_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400475 port_no=port_no,
William Kurkian6f436d02019-02-06 16:25:01 -0500476 tconts=tconts))
477
478 # Fetch alloc id and gemports from tech profile instance
479 alloc_id = tech_profile_instance.us_scheduler.alloc_id
480 gem_port_ids = []
481 for i in range(len(
482 tech_profile_instance.upstream_gem_port_attribute_list)):
483 gem_port_ids.append(
484 tech_profile_instance.upstream_gem_port_attribute_list[i].
485 gemport_id)
486 except BaseException as e:
487 self.log.exception(exception=e)
488
489 # Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store
490 pon_intf_onu_id = (intf_id, onu_id, uni_id)
491 self.resource_mgr.resource_mgrs[intf_id].update_alloc_ids_for_onu(
492 pon_intf_onu_id,
493 list([alloc_id])
494 )
495 self.resource_mgr.resource_mgrs[intf_id].update_gemport_ids_for_onu(
496 pon_intf_onu_id,
497 gem_port_ids
498 )
499
500 self.resource_mgr.update_gemports_ponport_to_onu_map_on_kv_store(
501 gem_port_ids, intf_id, onu_id, uni_id
502 )
503
504 return alloc_id, gem_port_ids
505
506 def add_upstream_data_flow(self, intf_id, onu_id, uni_id, port_no, uplink_classifier,
507 uplink_action, logical_flow, alloc_id,
508 gemport_id):
509
510 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
511
512 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, uplink_classifier,
513 uplink_action, UPSTREAM,
514 logical_flow, alloc_id, gemport_id)
515
516 # Secondary EAP on the subscriber vlan
517 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id, uni_id)
518 if eap_active:
519 self.add_eapol_flow(intf_id, onu_id, uni_id, port_no, eap_logical_flow, alloc_id,
520 gemport_id, vlan_id=uplink_classifier[VLAN_VID])
521
522 def add_downstream_data_flow(self, intf_id, onu_id, uni_id, port_no, downlink_classifier,
523 downlink_action, flow, alloc_id, gemport_id):
524 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
525 # Needed ???? It should be already there
526 downlink_action[POP_VLAN] = True
527 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
528
529 self.add_hsia_flow(intf_id, onu_id, uni_id, port_no, downlink_classifier,
530 downlink_action, DOWNSTREAM,
531 flow, alloc_id, gemport_id)
532
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400533 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500534 def add_hsia_flow(self, intf_id, onu_id, uni_id, port_no, classifier, action,
535 direction, logical_flow, alloc_id, gemport_id):
536
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400537 self.log.debug('add hisa flow', flow=logical_flow, port_no=port_no,
538 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
539 alloc_id=alloc_id)
540
William Kurkian6f436d02019-02-06 16:25:01 -0500541 flow_store_cookie = self._get_flow_store_cookie(classifier,
542 gemport_id)
543
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400544 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
545 action=action)
546
William Kurkian6f436d02019-02-06 16:25:01 -0500547 # One of the OLT platform (Broadcom BAL) requires that symmetric
548 # flows require the same flow_id to be used across UL and DL.
549 # Since HSIA flow is the only symmetric flow currently, we need to
550 # re-use the flow_id across both direction. The 'flow_category'
551 # takes priority over flow_cookie to find any available HSIA_FLOW
552 # id for the ONU.
553 flow_id = self.resource_mgr.get_flow_id(intf_id, onu_id, uni_id,
554 flow_store_cookie,
555 HSIA_FLOW)
556 if flow_id is None:
557 self.log.error("hsia-flow-unavailable")
558 return
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400559
560 self.log.debug('flow-id', flow_id=flow_id)
561
562 network_intf_id = yield self.get_nni_intf_id()
563
William Kurkian6f436d02019-02-06 16:25:01 -0500564 flow = openolt_pb2.Flow(
565 access_intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, flow_id=flow_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400566 flow_type=direction, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500567 gemport_id=gemport_id,
568 classifier=self.mk_classifier(classifier),
569 action=self.mk_action(action),
570 priority=logical_flow.priority,
571 port_no=port_no,
572 cookie=logical_flow.cookie)
573
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400574 self.log.debug('openolt-agent-flow', hsia_flow=flow)
575
William Kurkian6f436d02019-02-06 16:25:01 -0500576 if self.add_flow_to_device(flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400577 self.log.debug('added-hsia-openolt-agent-flow', hsia_flow=flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500578 flow_info = self._get_flow_info_as_json_blob(flow,
579 flow_store_cookie,
580 HSIA_FLOW)
581 self.update_flow_info_to_kv_store(flow.access_intf_id,
582 flow.onu_id, flow.uni_id,
583 flow.flow_id, flow_info)
584
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400585 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500586 def add_dhcp_trap(self, intf_id, onu_id, uni_id, port_no, classifier, action, logical_flow,
587 alloc_id, gemport_id):
588
589 self.log.debug('add dhcp upstream trap', classifier=classifier,
590 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, action=action)
591
592 action.clear()
593 action[TRAP_TO_HOST] = True
594 classifier[UDP_SRC] = 68
595 classifier[UDP_DST] = 67
596 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
597 classifier.pop(VLAN_VID, None)
598
599 flow_store_cookie = self._get_flow_store_cookie(classifier,
600 gemport_id)
601
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400602 self.log.debug('flow-store-cookie-classifer-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
603 action=action)
604
William Kurkian6f436d02019-02-06 16:25:01 -0500605 flow_id = self.resource_mgr.get_flow_id(
606 intf_id, onu_id, uni_id, flow_store_cookie
607 )
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400608
609 self.log.debug('flow-id', flow_id=flow_id)
610
611 network_intf_id = yield self.get_nni_intf_id()
612
William Kurkian6f436d02019-02-06 16:25:01 -0500613 dhcp_flow = openolt_pb2.Flow(
614 onu_id=onu_id, uni_id=uni_id, flow_id=flow_id, flow_type=UPSTREAM,
615 access_intf_id=intf_id, gemport_id=gemport_id,
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400616 alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500617 priority=logical_flow.priority,
618 classifier=self.mk_classifier(classifier),
619 action=self.mk_action(action),
620 port_no=port_no,
621 cookie=logical_flow.cookie)
622
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400623 self.log.debug('openolt-agent-flow', dhcp_flow=dhcp_flow)
624
William Kurkian6f436d02019-02-06 16:25:01 -0500625 if self.add_flow_to_device(dhcp_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400626 self.log.debug('added-dhcp-openolt-agent-flow', dhcp_flow=dhcp_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500627 flow_info = self._get_flow_info_as_json_blob(dhcp_flow, flow_store_cookie)
628 self.update_flow_info_to_kv_store(dhcp_flow.access_intf_id,
629 dhcp_flow.onu_id,
630 dhcp_flow.uni_id,
631 dhcp_flow.flow_id,
632 flow_info)
633
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400634 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -0500635 def add_eapol_flow(self, intf_id, onu_id, uni_id, port_no, logical_flow, alloc_id,
636 gemport_id, vlan_id=DEFAULT_MGMT_VLAN):
637
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400638 self.log.debug('add eapol upstream trap', flow=logical_flow, port_no=port_no,
639 intf_id=intf_id, onu_id=onu_id, uni_id=uni_id, gemport_id=gemport_id,
640 alloc_id=alloc_id, vlan_id=vlan_id)
641
William Kurkian6f436d02019-02-06 16:25:01 -0500642 uplink_classifier = dict()
643 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
644 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
645 uplink_classifier[VLAN_VID] = vlan_id
646
647 uplink_action = dict()
648 uplink_action[TRAP_TO_HOST] = True
649
650 flow_store_cookie = self._get_flow_store_cookie(uplink_classifier,
651 gemport_id)
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400652
653 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, uplink_classifier=uplink_classifier,
654 uplink_action=uplink_action)
655
William Kurkian6f436d02019-02-06 16:25:01 -0500656 # Add Upstream EAPOL Flow.
657 uplink_flow_id = self.resource_mgr.get_flow_id(
658 intf_id, onu_id, uni_id, flow_store_cookie
659 )
660
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400661 self.log.debug('flow-id', uplink_flow_id=uplink_flow_id)
662
663 network_intf_id = yield self.get_nni_intf_id()
664
William Kurkian6f436d02019-02-06 16:25:01 -0500665 upstream_flow = openolt_pb2.Flow(
666 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 -0400667 flow_type=UPSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500668 gemport_id=gemport_id,
669 classifier=self.mk_classifier(uplink_classifier),
670 action=self.mk_action(uplink_action),
671 priority=logical_flow.priority,
672 port_no=port_no,
673 cookie=logical_flow.cookie)
674
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400675 self.log.debug('openolt-agent-flow', upstream_flow=upstream_flow)
676
William Kurkian6f436d02019-02-06 16:25:01 -0500677 logical_flow = copy.deepcopy(logical_flow)
678 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
679 vlan_id | 0x1000)]))
680 logical_flow.match.type = OFPMT_OXM
681
682 if self.add_flow_to_device(upstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400683 self.log.debug('added-eapol-openolt-agent-flow', upstream_flow=upstream_flow, logical_flow=logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500684 flow_info = self._get_flow_info_as_json_blob(upstream_flow,
685 flow_store_cookie)
686 self.update_flow_info_to_kv_store(upstream_flow.access_intf_id,
687 upstream_flow.onu_id,
688 upstream_flow.uni_id,
689 upstream_flow.flow_id,
690 flow_info)
691
692 if vlan_id == DEFAULT_MGMT_VLAN:
693 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
694 # requirement)
695 # On one of the platforms (Broadcom BAL), when same DL classifier
696 # vlan was used across multiple ONUs, eapol flow re-adds after
697 # flow delete (cases of onu reboot/disable) fails.
698 # In order to generate unique vlan, a combination of intf_id
699 # onu_id and uni_id is used.
700 # uni_id defaults to 0, so add 1 to it.
701 special_vlan_downstream_flow = 4090 - intf_id * onu_id * (uni_id+1)
702 # Assert that we do not generate invalid vlans under no condition
Matt Jeanneretaa360912019-04-22 16:23:12 -0400703 assert (special_vlan_downstream_flow >= 2), 'invalid-vlan-generated'
704 self.log.warn('generating-special-downstream-vlan-for-bal', special_vlan_downstream_flow=special_vlan_downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500705
706 downlink_classifier = dict()
707 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
708 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
709
710 downlink_action = dict()
711 downlink_action[PUSH_VLAN] = True
712 downlink_action[VLAN_VID] = vlan_id
713
714
715 flow_store_cookie = self._get_flow_store_cookie(downlink_classifier,
716 gemport_id)
717
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400718 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, downlink_classifier=downlink_classifier,
719 downlink_action=downlink_action)
720
William Kurkian6f436d02019-02-06 16:25:01 -0500721 downlink_flow_id = self.resource_mgr.get_flow_id(
722 intf_id, onu_id, uni_id, flow_store_cookie
723 )
724
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400725 self.log.debug('flow-id', downlink_flow_id=downlink_flow_id)
726
William Kurkian6f436d02019-02-06 16:25:01 -0500727 downstream_flow = openolt_pb2.Flow(
728 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 -0400729 flow_type=DOWNSTREAM, alloc_id=alloc_id, network_intf_id=network_intf_id,
William Kurkian6f436d02019-02-06 16:25:01 -0500730 gemport_id=gemport_id,
731 classifier=self.mk_classifier(downlink_classifier),
732 action=self.mk_action(downlink_action),
733 priority=logical_flow.priority,
734 port_no=port_no,
735 cookie=logical_flow.cookie)
736
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400737 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
738
Matt Jeanneretaa360912019-04-22 16:23:12 -0400739 try:
740 downstream_logical_flow = ofp_flow_stats(
741 id=logical_flow.id, cookie=logical_flow.cookie,
742 table_id=logical_flow.table_id, priority=logical_flow.priority,
743 flags=logical_flow.flags)
William Kurkian6f436d02019-02-06 16:25:01 -0500744
Matt Jeanneretaa360912019-04-22 16:23:12 -0400745 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
746 fd.in_port(fd.get_out_port(logical_flow)),
747 fd.vlan_vid(special_vlan_downstream_flow | 0x1000)]))
748 downstream_logical_flow.match.type = OFPMT_OXM
William Kurkian6f436d02019-02-06 16:25:01 -0500749
Matt Jeanneretaa360912019-04-22 16:23:12 -0400750 downstream_logical_flow.instructions.extend(
751 fd.mk_instructions_from_actions([fd.output(
752 self.platform.mk_uni_port_num(intf_id, onu_id, uni_id))]))
753 except Exception as e:
754 self.log.exception("unexpected-error-building-downstream-logical-flow", intf_id=intf_id, onu_id=onu_id,
755 uni_id=uni_id, e=e, downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500756
757 if self.add_flow_to_device(downstream_flow, downstream_logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400758 self.log.debug('added-eapol-openolt-agent-flow', downstream_flow=downstream_flow,
759 downstream_logical_flow=downstream_logical_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500760 flow_info = self._get_flow_info_as_json_blob(downstream_flow,
761 flow_store_cookie)
762 self.update_flow_info_to_kv_store(downstream_flow.access_intf_id,
763 downstream_flow.onu_id,
764 downstream_flow.uni_id,
765 downstream_flow.flow_id,
766 flow_info)
767
768 def repush_all_different_flows(self):
769 # Check if the device is supposed to have flows, if so add them
770 # Recover static flows after a reboot
771 logical_flows = self.logical_flows_proxy.get('/').items
772 devices_flows = self.flows_proxy.get('/').items
773 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
774 for logical_flow in logical_flows:
775 try:
776 if logical_flow.id not in logical_flows_ids_provisioned:
777 self.add_flow(logical_flow)
778 except Exception as e:
779 self.log.exception('Problem reading this flow', e=e)
780
781 def reset_flows(self):
782 self.flows_proxy.update('/', Flows())
783
784 """ Add a downstream LLDP trap flow on the NNI interface
785 """
786
787 def add_lldp_flow(self, logical_flow, port_no, network_intf_id=0):
788
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400789 self.log.debug('add lldp trap flow', flow=logical_flow, port_no=port_no,
790 network_intf_id=network_intf_id)
791
William Kurkian6f436d02019-02-06 16:25:01 -0500792 classifier = dict()
793 classifier[ETH_TYPE] = LLDP_ETH_TYPE
794 classifier[PACKET_TAG_TYPE] = UNTAGGED
795 action = dict()
796 action[TRAP_TO_HOST] = True
797
798 # LLDP flow is installed to trap LLDP packets on the NNI port.
799 # We manage flow_id resource pool on per PON port basis.
800 # Since this situation is tricky, as a hack, we pass the NNI port
801 # index (network_intf_id) as PON port Index for the flow_id resource
802 # pool. Also, there is no ONU Id available for trapping LLDP packets
803 # on NNI port, use onu_id as -1 (invalid)
804 # ****************** CAVEAT *******************
805 # This logic works if the NNI Port Id falls within the same valid
806 # range of PON Port Ids. If this doesn't work for some OLT Vendor
807 # we need to have a re-look at this.
808 # *********************************************
809 onu_id = -1
810 uni_id = -1
811 flow_store_cookie = self._get_flow_store_cookie(classifier)
812 flow_id = self.resource_mgr.get_flow_id(network_intf_id, onu_id, uni_id,
813 flow_store_cookie)
814
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400815 self.log.debug('flow-store-cookie-classifier-action', flow_store_cookie=flow_store_cookie, classifier=classifier,
816 action=action)
817
William Kurkian6f436d02019-02-06 16:25:01 -0500818 downstream_flow = openolt_pb2.Flow(
819 access_intf_id=-1, # access_intf_id not required
820 onu_id=onu_id, # onu_id not required
821 uni_id=uni_id, # uni_id not used
822 flow_id=flow_id,
823 flow_type=DOWNSTREAM,
824 network_intf_id=network_intf_id,
825 gemport_id=-1, # gemport_id not required
826 classifier=self.mk_classifier(classifier),
827 action=self.mk_action(action),
828 priority=logical_flow.priority,
829 port_no=port_no,
830 cookie=logical_flow.cookie)
831
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400832 self.log.debug('openolt-agent-flow', downstream_flow=downstream_flow)
833
William Kurkian6f436d02019-02-06 16:25:01 -0500834 if self.add_flow_to_device(downstream_flow, logical_flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400835 self.log.debug('added-lldp-openolt-agent-flow', downstream_flow=downstream_flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500836 self.update_flow_info_to_kv_store(network_intf_id, onu_id, uni_id,
837 flow_id, downstream_flow)
838
839 def mk_classifier(self, classifier_info):
840
841 classifier = openolt_pb2.Classifier()
842
843 if ETH_TYPE in classifier_info:
844 classifier.eth_type = classifier_info[ETH_TYPE]
845 if IP_PROTO in classifier_info:
846 classifier.ip_proto = classifier_info[IP_PROTO]
847 if VLAN_VID in classifier_info:
848 classifier.o_vid = classifier_info[VLAN_VID]
849 if METADATA in classifier_info:
850 classifier.i_vid = classifier_info[METADATA]
851 if VLAN_PCP in classifier_info:
852 classifier.o_pbits = classifier_info[VLAN_PCP]
853 if UDP_SRC in classifier_info:
854 classifier.src_port = classifier_info[UDP_SRC]
855 if UDP_DST in classifier_info:
856 classifier.dst_port = classifier_info[UDP_DST]
857 if IPV4_DST in classifier_info:
858 classifier.dst_ip = classifier_info[IPV4_DST]
859 if IPV4_SRC in classifier_info:
860 classifier.src_ip = classifier_info[IPV4_SRC]
861 if PACKET_TAG_TYPE in classifier_info:
862 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
863 classifier.pkt_tag_type = SINGLE_TAG
864 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
865 classifier.pkt_tag_type = DOUBLE_TAG
866 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
867 classifier.pkt_tag_type = UNTAGGED
868 else:
869 classifier.pkt_tag_type = 'none'
870
871 return classifier
872
873 def mk_action(self, action_info):
874 action = openolt_pb2.Action()
875
876 if POP_VLAN in action_info:
877 action.o_vid = action_info[VLAN_VID]
878 action.cmd.remove_outer_tag = True
879 elif PUSH_VLAN in action_info:
880 action.o_vid = action_info[VLAN_VID]
881 action.cmd.add_outer_tag = True
882 elif TRAP_TO_HOST in action_info:
883 action.cmd.trap_to_host = True
884 else:
885 self.log.info('Invalid-action-field', action_info=action_info)
886 return
887 return action
888
889 def is_eap_enabled(self, intf_id, onu_id, uni_id):
890 flows = self.logical_flows_proxy.get('/').items
891
892 for flow in flows:
893 eap_flow = False
894 eap_intf_id = None
895 eap_onu_id = None
896 eap_uni_id = None
897 for field in fd.get_ofb_fields(flow):
898 if field.type == fd.ETH_TYPE:
899 if field.eth_type == EAP_ETH_TYPE:
900 eap_flow = True
901 if field.type == fd.IN_PORT:
902 eap_intf_id = self.platform.intf_id_from_uni_port_num(
903 field.port)
904 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
905 eap_uni_id = self.platform.uni_id_from_port_num(field.port)
906
907 if eap_flow:
908 self.log.debug('eap flow detected', onu_id=onu_id, uni_id=uni_id,
909 intf_id=intf_id, eap_intf_id=eap_intf_id,
910 eap_onu_id=eap_onu_id,
911 eap_uni_id=eap_uni_id)
912 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id and uni_id == eap_uni_id:
913 return True, flow
914
915 return False, None
916
917 def get_subscriber_vlan(self, port):
918 self.log.debug('looking from subscriber flow for port', port=port)
919
920 flows = self.logical_flows_proxy.get('/').items
921 for flow in flows:
922 in_port = fd.get_in_port(flow)
923 out_port = fd.get_out_port(flow)
924 if in_port == port and out_port is not None and \
925 self.platform.intf_id_to_port_type_name(out_port) \
926 == Port.ETHERNET_NNI:
927 fields = fd.get_ofb_fields(flow)
928 self.log.debug('subscriber flow found', fields=fields)
929 for field in fields:
930 if field.type == OFPXMT_OFB_VLAN_VID:
931 self.log.debug('subscriber vlan found',
932 vlan_id=field.vlan_vid)
933 return field.vlan_vid & 0x0fff
934 self.log.debug('No subscriber flow found', port=port)
935 return None
936
937 def add_flow_to_device(self, flow, logical_flow):
938 self.log.debug('pushing flow to device', flow=flow)
939 try:
940 self.stub.FlowAdd(flow)
941 except grpc.RpcError as grpc_e:
942 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
943 self.log.warn('flow already exists', e=grpc_e, flow=flow)
944 else:
945 self.log.error('failed to add flow',
946 logical_flow=logical_flow, flow=flow,
947 grpc_error=grpc_e)
948 return False
Matt Jeanneretaa360912019-04-22 16:23:12 -0400949 except Exception as f:
950 self.log.exception("unexpected-openolt-agent-error", flow=flow, logical_flow=logical_flow, f=f)
William Kurkian6f436d02019-02-06 16:25:01 -0500951 else:
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400952 # TODO NEW CORE: Should not need. Core keeps track of logical flows. no need to keep track. verify, especially olt reboot!
953 # self.register_flow(logical_flow, flow)
William Kurkian6f436d02019-02-06 16:25:01 -0500954 return True
955
956 def update_flow_info_to_kv_store(self, intf_id, onu_id, uni_id, flow_id, flow):
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -0400957 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 -0500958 self.resource_mgr.update_flow_id_info_for_uni(intf_id, onu_id, uni_id,
959 flow_id, flow)
960
961 def register_flow(self, logical_flow, device_flow):
962 self.log.debug('registering flow in device',
963 logical_flow=logical_flow, device_flow=device_flow)
964 stored_flow = copy.deepcopy(logical_flow)
965 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
966 device_flow.flow_type)
967 self.log.debug('generated device flow id', id=stored_flow.id,
968 flow_id=device_flow.flow_id,
969 direction=device_flow.flow_type)
970 stored_flow.cookie = logical_flow.id
971 flows = self.flows_proxy.get('/')
972 flows.items.extend([stored_flow])
973 self.flows_proxy.update('/', flows)
974
975 def find_next_flow(self, flow):
976 table_id = fd.get_goto_table_id(flow)
977 metadata = 0
978 # Prior to ONOS 1.13.5, Metadata contained the UNI output port number. In
979 # 1.13.5 and later, the lower 32-bits is the output port number and the
980 # upper 32-bits is the inner-vid we are looking for. Use just the lower 32
981 # bits. Allows this code to work with pre- and post-1.13.5 ONOS OltPipeline
982
983 for field in fd.get_ofb_fields(flow):
984 if field.type == fd.METADATA:
985 metadata = field.table_metadata & 0xFFFFFFFF
986 if table_id is None:
987 return None
988 flows = self.logical_flows_proxy.get('/').items
989 next_flows = []
990 for f in flows:
991 if f.table_id == table_id:
992 # FIXME
993 if fd.get_in_port(f) == fd.get_in_port(flow) and \
994 fd.get_out_port(f) == metadata:
995 next_flows.append(f)
996
997 if len(next_flows) == 0:
998 self.log.warning('no next flow found, it may be a timing issue',
999 flow=flow, number_of_flows=len(flows))
1000 if flow.id in self.retry_add_flow_list:
1001 self.log.debug('flow is already in retry list', flow_id=flow.id)
1002 else:
1003 self.retry_add_flow_list.append(flow.id)
1004 reactor.callLater(5, self.retry_add_flow, flow)
1005 return None
1006
1007 next_flows.sort(key=lambda f: f.priority, reverse=True)
1008
1009 return next_flows[0]
1010
1011 def update_children_flows(self, device_rules_map):
1012
1013 for device_id, (flows, groups) in device_rules_map.iteritems():
1014 if device_id != self.device_id:
1015 self.root_proxy.update('/devices/{}/flows'.format(device_id),
1016 Flows(items=flows.values()))
1017 self.root_proxy.update('/devices/{}/flow_groups'.format(
1018 device_id), FlowGroups(items=groups.values()))
1019
1020 def clear_flows_and_scheduler_for_logical_port(self, child_device, logical_port):
1021 ofp_port_name = logical_port.ofp_port.name
1022 port_no = logical_port.ofp_port.port_no
1023 pon_port = child_device.proxy_address.channel_id
1024 onu_id = child_device.proxy_address.onu_id
1025 uni_id = self.platform.uni_id_from_port_num(logical_port)
1026
1027 # TODO: The DEFAULT_TECH_PROFILE_ID is assumed. Right way to do,
1028 # is probably to maintain a list of Tech-profile table IDs associated
1029 # with the UNI logical_port. This way, when the logical port is deleted,
1030 # all the associated tech-profile configuration with the UNI logical_port
1031 # can be cleared.
1032 tech_profile_instance = self.tech_profile[pon_port]. \
1033 get_tech_profile_instance(
1034 DEFAULT_TECH_PROFILE_TABLE_ID,
1035 ofp_port_name)
1036 flow_ids = self.resource_mgr.get_current_flow_ids_for_uni(pon_port, onu_id, uni_id)
1037 self.log.debug("outstanding-flows-to-be-cleared", flow_ids=flow_ids)
1038 for flow_id in flow_ids:
1039 flow_infos = self.resource_mgr.get_flow_id_info(pon_port, onu_id, uni_id, flow_id)
1040 for flow_info in flow_infos:
1041 direction = flow_info['flow_type']
1042 flow_to_remove = openolt_pb2.Flow(flow_id=flow_id,
1043 flow_type=direction)
1044 try:
1045 self.stub.FlowRemove(flow_to_remove)
1046 except grpc.RpcError as grpc_e:
1047 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
1048 self.log.debug('This flow does not exist on the switch, '
1049 'normal after an OLT reboot',
1050 flow=flow_to_remove)
1051 else:
1052 raise grpc_e
1053
1054 self.resource_mgr.free_flow_id_for_uni(pon_port, onu_id, uni_id, flow_id)
1055
1056 try:
1057 tconts = self.tech_profile[pon_port].get_tconts(tech_profile_instance)
1058 self.stub.RemoveTconts(openolt_pb2.Tconts(intf_id=pon_port,
1059 onu_id=onu_id,
1060 uni_id=uni_id,
1061 port_no=port_no,
1062 tconts=tconts))
1063 except grpc.RpcError as grpc_e:
1064 self.log.error('error-removing-tcont-scheduler-queues',
1065 err=grpc_e)
1066
1067 def generate_stored_id(self, flow_id, direction):
1068 if direction == UPSTREAM:
1069 self.log.debug('upstream flow, shifting id')
1070 return 0x1 << 15 | flow_id
1071 elif direction == DOWNSTREAM:
1072 self.log.debug('downstream flow, not shifting id')
1073 return flow_id
1074 else:
1075 self.log.warn('Unrecognized direction', direction=direction)
1076 return flow_id
1077
1078 def decode_stored_id(self, id):
1079 if id >> 15 == 0x1:
1080 return id & 0x7fff, UPSTREAM
1081 else:
1082 return id, DOWNSTREAM
1083
1084 def _populate_tech_profile_per_pon_port(self):
1085 for arange in self.resource_mgr.device_info.ranges:
1086 for intf_id in arange.intf_ids:
1087 self.tech_profile[intf_id] = \
1088 self.resource_mgr.resource_mgrs[intf_id].tech_profile
1089
1090 # Make sure we have as many tech_profiles as there are pon ports on
1091 # the device
1092 assert len(self.tech_profile) == self.resource_mgr.device_info.pon_ports
1093
1094 def _get_flow_info_as_json_blob(self, flow, flow_store_cookie,
1095 flow_category=None):
1096 json_blob = MessageToDict(message=flow,
1097 preserving_proto_field_name=True)
1098 self.log.debug("flow-info", json_blob=json_blob)
1099 json_blob['flow_store_cookie'] = flow_store_cookie
1100 if flow_category is not None:
1101 json_blob['flow_category'] = flow_category
1102 flow_info = self.resource_mgr.get_flow_id_info(flow.access_intf_id,
1103 flow.onu_id, flow.uni_id, flow.flow_id)
1104
1105 if flow_info is None:
1106 flow_info = list()
1107 flow_info.append(json_blob)
1108 else:
1109 assert (isinstance(flow_info, list))
1110 flow_info.append(json_blob)
1111
1112 return flow_info
1113
1114 @staticmethod
1115 def _get_flow_store_cookie(classifier, gem_port=None):
1116 assert isinstance(classifier, dict)
1117 # We need unique flows per gem_port
1118 if gem_port is not None:
1119 to_hash = dumps(classifier, sort_keys=True) + str(gem_port)
1120 else:
1121 to_hash = dumps(classifier, sort_keys=True)
1122 return hashlib.md5(to_hash).hexdigest()[:12]
1123
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001124 @inlineCallbacks
William Kurkian6f436d02019-02-06 16:25:01 -05001125 def get_nni_intf_id(self):
1126 if self.nni_intf_id is not None:
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001127 returnValue(self.nni_intf_id)
William Kurkian6f436d02019-02-06 16:25:01 -05001128
Matt Jeanneret9dbce8c2019-03-23 14:35:00 -04001129 port_list = yield self.core_proxy.get_ports(self.device_id, Port.ETHERNET_NNI)
1130 self.log.debug("nni-ports-list", port_list=port_list)
1131
1132 # TODO: Hardcoded only first NNI
1133 port = port_list.items[0]
1134
1135 self.log.debug("nni-port", port=port)
1136 self.nni_intf_id = self.platform.intf_id_from_nni_port_num(port.port_no)
1137
1138 self.log.debug("nni-intf-d ", port=port.port_no, nni_intf_id=self.nni_intf_id)
1139 returnValue(self.nni_intf_id)