blob: 69e712a4cda370924c0bd5f20862ee78c0a0d112 [file] [log] [blame]
Shad Ansari2dda4f32018-05-17 07:16:07 +00001#
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#
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040016import copy
17from twisted.internet import reactor
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -040018import grpc
Shad Ansari2dda4f32018-05-17 07:16:07 +000019
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040020from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
Nicolas Palpacuer41141352018-08-31 14:11:38 -040021 ofp_flow_stats, ofp_match, OFPMT_OXM, Flows, FlowGroups, \
22 OFPXMT_OFB_IN_PORT, OFPXMT_OFB_VLAN_VID
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040023from voltha.protos.device_pb2 import Port
Shad Ansari2dda4f32018-05-17 07:16:07 +000024import voltha.core.flow_decomposer as fd
25import openolt_platform as platform
26from voltha.adapters.openolt.protos import openolt_pb2
Nicolas Palpacuer61815162018-06-20 18:12:04 -040027from voltha.registry import registry
Shad Ansari2dda4f32018-05-17 07:16:07 +000028
Shad Ansarif9d2d102018-06-13 02:15:26 +000029HSIA_FLOW_INDEX = 0 # FIXME
30DHCP_FLOW_INDEX = 1 # FIXME
Shad Ansari47e0c392018-07-17 23:55:07 +000031DHCP_DOWNLINK_FLOW_INDEX = 6 # FIXME
Shad Ansarif9d2d102018-06-13 02:15:26 +000032EAPOL_FLOW_INDEX = 2 # FIXME
33EAPOL_DOWNLINK_FLOW_INDEX = 3 # FIXME
Nicolas Palpacuer61815162018-06-20 18:12:04 -040034EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX = 4 # FIXME
35EAPOL_UPLINK_SECONDARY_FLOW_INDEX = 5 # FIXME
36
37
38EAP_ETH_TYPE = 0x888e
Shad Ansari2dda4f32018-05-17 07:16:07 +000039
40# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
41DEFAULT_MGMT_VLAN = 4091
42
Shad Ansarif9d2d102018-06-13 02:15:26 +000043
Shad Ansari2dda4f32018-05-17 07:16:07 +000044class OpenOltFlowMgr(object):
45
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040046 def __init__(self, log, stub, device_id, logical_device_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +000047 self.log = log
48 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040049 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040050 self.logical_device_id = logical_device_id
51 self.logical_flows_proxy = registry('core').get_proxy(
52 '/logical_devices/{}/flows'.format(self.logical_device_id))
53 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040054 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040055 self.root_proxy = registry('core').get_proxy('/')
56 self.fd_object = fd.FlowDecomposer()
Shad Ansari2dda4f32018-05-17 07:16:07 +000057
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040058 def add_flow(self, flow):
59 self.log.debug('add flow', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +000060 classifier_info = dict()
61 action_info = dict()
62
Shad Ansari2dda4f32018-05-17 07:16:07 +000063
64 for field in fd.get_ofb_fields(flow):
65 if field.type == fd.ETH_TYPE:
66 classifier_info['eth_type'] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +000067 self.log.debug('field-type-eth-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +000068 eth_type=classifier_info['eth_type'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000069 elif field.type == fd.IP_PROTO:
70 classifier_info['ip_proto'] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +000071 self.log.debug('field-type-ip-proto',
Shad Ansarif9d2d102018-06-13 02:15:26 +000072 ip_proto=classifier_info['ip_proto'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000073 elif field.type == fd.IN_PORT:
74 classifier_info['in_port'] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +000075 self.log.debug('field-type-in-port',
Shad Ansarif9d2d102018-06-13 02:15:26 +000076 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000077 elif field.type == fd.VLAN_VID:
78 classifier_info['vlan_vid'] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +000079 self.log.debug('field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +000080 vlan=classifier_info['vlan_vid'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000081 elif field.type == fd.VLAN_PCP:
82 classifier_info['vlan_pcp'] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +000083 self.log.debug('field-type-vlan-pcp',
Shad Ansarif9d2d102018-06-13 02:15:26 +000084 pcp=classifier_info['vlan_pcp'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000085 elif field.type == fd.UDP_DST:
86 classifier_info['udp_dst'] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000087 self.log.debug('field-type-udp-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000088 udp_dst=classifier_info['udp_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000089 elif field.type == fd.UDP_SRC:
90 classifier_info['udp_src'] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000091 self.log.debug('field-type-udp-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +000092 udp_src=classifier_info['udp_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000093 elif field.type == fd.IPV4_DST:
94 classifier_info['ipv4_dst'] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000095 self.log.debug('field-type-ipv4-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000096 ipv4_dst=classifier_info['ipv4_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000097 elif field.type == fd.IPV4_SRC:
98 classifier_info['ipv4_src'] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000099 self.log.debug('field-type-ipv4-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000100 ipv4_dst=classifier_info['ipv4_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000101 elif field.type == fd.METADATA:
102 classifier_info['metadata'] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +0000103 self.log.debug('field-type-metadata',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000104 metadata=classifier_info['metadata'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000105 else:
106 raise NotImplementedError('field.type={}'.format(
107 field.type))
108
109 for action in fd.get_actions(flow):
110 if action.type == fd.OUTPUT:
111 action_info['output'] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000112 self.log.debug('action-type-output',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000113 output=action_info['output'],
114 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000115 elif action.type == fd.POP_VLAN:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400116 if fd.get_goto_table_id(flow) is None:
117 self.log.debug('being taken care of by ONU', flow=flow)
118 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000119 action_info['pop_vlan'] = True
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400120 self.log.debug('action-type-pop-vlan', in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000121 elif action.type == fd.PUSH_VLAN:
122 action_info['push_vlan'] = True
123 action_info['tpid'] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000124 self.log.debug('action-type-push-vlan',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400125 push_tpid=action_info['tpid'], in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000126 if action.push.ethertype != 0x8100:
127 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000128 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000129 elif action.type == fd.SET_FIELD:
130 # action_info['action_type'] = 'set_field'
131 _field = action.set_field.field.ofb_field
132 assert (action.set_field.field.oxm_class ==
133 OFPXMC_OPENFLOW_BASIC)
Shad Ansarie048aaa2018-05-18 18:27:21 +0000134 self.log.debug('action-type-set-field',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400135 field=_field, in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000136 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000137 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000138 vlan_vid=_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000139 action_info['vlan_vid'] = (_field.vlan_vid & 0xfff)
140 else:
141 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000142 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000143 else:
144 self.log.error('unsupported-action-type',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400145 action_type=action.type, in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000146
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400147 if fd.get_goto_table_id(flow) is not None and not 'pop_vlan' in \
148 action_info:
149 self.log.debug('being taken care of by ONU', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000150
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400151 if not 'output' in action_info and 'metadata' in classifier_info:
152 #find flow in the next table
153 next_flow = self.find_next_flow(flow)
154 if next_flow is None:
155 return
156 action_info['output'] = fd.get_out_port(next_flow)
157 for field in fd.get_ofb_fields(next_flow):
158 if field.type == fd.VLAN_VID:
159 classifier_info['metadata'] = field.vlan_vid & 0xfff
160
161
162 (intf_id, onu_id) = platform.extract_access_from_flow(
163 classifier_info['in_port'], action_info['output'])
164
165
166 self.divide_and_add_flow(intf_id, onu_id, classifier_info,
167 action_info, flow)
168
169 def remove_flow(self, flow):
170 self.log.debug('trying to remove flows from logical flow :',
171 logical_flow=flow)
172 device_flows_to_remove = []
173 device_flows = self.flows_proxy.get('/').items
174 for f in device_flows:
175 if f.cookie == flow.id:
176 device_flows_to_remove.append(f)
177
178
179 for f in device_flows_to_remove:
180 (id, direction) = self.decode_stored_id(f.id)
181 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400182 try:
183 self.stub.FlowRemove(flow_to_remove)
184 except grpc.RpcError as grpc_e:
185 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
186 self.log.debug('This flow does not exist on the switch, '
187 'normal after an OLT reboot', flow=flow_to_remove)
188 else:
189 raise grpc_e
190
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400191 self.log.debug('flow removed from device', flow=f,
192 flow_key=flow_to_remove)
193
194 if len(device_flows_to_remove) > 0:
195 new_flows = []
196 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
197 for f in device_flows:
198 if f.id not in flows_ids_to_remove:
199 new_flows.append(f)
200
201 self.flows_proxy.update('/', Flows(items=new_flows))
202 self.log.debug('flows removed from the data store',
203 flow_ids_removed=flows_ids_to_remove,
204 number_of_flows_removed=(len(device_flows) - len(
205 new_flows)), expected_flows_removed=len(
206 device_flows_to_remove))
207 else:
208 self.log.debug('no device flow to remove for this flow (normal '
209 'for multi table flows)', flow=flow)
210
211
212 def divide_and_add_flow(self, intf_id, onu_id, classifier,
213 action, flow):
214
215 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id,
216 classifier=classifier, action=action)
217
Shad Ansari2dda4f32018-05-17 07:16:07 +0000218 if 'ip_proto' in classifier:
219 if classifier['ip_proto'] == 17:
220 self.log.debug('dhcp flow add')
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400221 self.add_dhcp_trap(intf_id, onu_id, classifier,
222 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000223 elif classifier['ip_proto'] == 2:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400224 self.log.warn('igmp flow add ignored, not implemented yet')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000225 else:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400226 self.log.warn("Invalid-Classifier-to-handle",
Shad Ansarif9d2d102018-06-13 02:15:26 +0000227 classifier=classifier,
228 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000229 elif 'eth_type' in classifier:
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400230 if classifier['eth_type'] == EAP_ETH_TYPE:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000231 self.log.debug('eapol flow add')
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400232 self.add_eapol_flow(intf_id, onu_id, flow)
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400233 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
234 if vlan_id is not None:
235 self.add_eapol_flow(intf_id, onu_id, flow,
236 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
237 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
238 vlan_id=vlan_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400239
Shad Ansari2dda4f32018-05-17 07:16:07 +0000240 elif 'push_vlan' in action:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400241 self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
242 flow)
243 elif 'pop_vlan' in action:
244 self.add_downstream_data_flow(intf_id, onu_id, classifier,
245 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000246 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000247 self.log.debug('Invalid-flow-type-to-handle',
248 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400249 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000250
Shad Ansari2dda4f32018-05-17 07:16:07 +0000251
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400252 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
253 uplink_action, logical_flow):
254
Shad Ansari2dda4f32018-05-17 07:16:07 +0000255
256 uplink_classifier['pkt_tag_type'] = 'single_tag'
257
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400258 self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
259 uplink_action, 'upstream', HSIA_FLOW_INDEX,
260 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000261
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400262 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400263 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400264 if eap_active:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400265 self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400266 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
267 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
268 vlan_id=uplink_classifier['vlan_vid'])
269
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400270
271 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
272 downlink_action, flow):
273 downlink_classifier['pkt_tag_type'] = 'double_tag'
274 # Needed ???? It should be already there
275 downlink_action['pop_vlan'] = True
276 downlink_action['vlan_vid'] = downlink_classifier['vlan_vid']
277
278 self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
279 downlink_action, 'downstream', HSIA_FLOW_INDEX,
280 flow)
281
282 # To-Do right now only one GEM port is supported, so below method
283 # will take care of handling all the p bits.
284 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400285 # Waiting for Technology profile
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400286 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
287 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000288
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400289 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000290 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
291
Shad Ansari2dda4f32018-05-17 07:16:07 +0000292 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400293 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000294 access_intf_id=intf_id, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400295 priority=logical_flow.priority,
296 classifier=self.mk_classifier(classifier),
297 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000298
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400299 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000300
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400301 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000302
Shad Ansari47e0c392018-07-17 23:55:07 +0000303 self.log.debug('add dhcp upstream trap', classifier=classifier,
304 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000305
306 action.clear()
307 action['trap_to_host'] = True
308 classifier['pkt_tag_type'] = 'single_tag'
309 classifier.pop('vlan_vid', None)
310
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400311 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000312 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
313
314 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000315 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400316 access_intf_id=intf_id, gemport_id=gemport_id,
317 priority=logical_flow.priority,
318 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000319 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000320
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400321 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000322
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400323
Shad Ansari47e0c392018-07-17 23:55:07 +0000324 # FIXME - ONOS should send explicit upstream and downstream
325 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400326
327 downstream_logical_flow = copy.deepcopy(logical_flow)
328 for oxm_field in downstream_logical_flow.match.oxm_fields:
329 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400330 oxm_field.ofb_field.port = \
331 platform.intf_id_to_port_no(0, Port.ETHERNET_NNI)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400332
Shad Ansari47e0c392018-07-17 23:55:07 +0000333 classifier['udp_src'] = 67
334 classifier['udp_dst'] = 68
335 classifier['pkt_tag_type'] = 'double_tag'
336 action.pop('push_vlan', None)
337
338 flow_id = platform.mk_flow_id(intf_id, onu_id,
339 DHCP_DOWNLINK_FLOW_INDEX)
340
341 downstream_flow = openolt_pb2.Flow(
342 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
343 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400344 priority=logical_flow.priority, classifier=self.mk_classifier(
345 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000346 action=self.mk_action(action))
347
Shad Ansari47e0c392018-07-17 23:55:07 +0000348
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400349 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
350
351 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000352 uplink_eapol_id=EAPOL_FLOW_INDEX,
353 downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
354 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000355
Shad Ansari2dda4f32018-05-17 07:16:07 +0000356
Shad Ansari2dda4f32018-05-17 07:16:07 +0000357
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400358 uplink_classifier = {}
359 uplink_classifier['eth_type'] = EAP_ETH_TYPE
Shad Ansari2dda4f32018-05-17 07:16:07 +0000360 uplink_classifier['pkt_tag_type'] = 'single_tag'
361 uplink_classifier['vlan_vid'] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400362
363 uplink_action = {}
Shad Ansari2dda4f32018-05-17 07:16:07 +0000364 uplink_action['trap_to_host'] = True
365
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400366 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400367
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400368 # Add Upstream EAPOL Flow.
369
370 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
371
Shad Ansari2dda4f32018-05-17 07:16:07 +0000372 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000373 onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400374 access_intf_id=intf_id, gemport_id=gemport_id,
375 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000376 classifier=self.mk_classifier(uplink_classifier),
377 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000378
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400379 logical_flow = copy.deepcopy(logical_flow)
380 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
381 vlan_id | 0x1000)]))
382 logical_flow.match.type = OFPMT_OXM
383
384 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000385
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400386 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000387
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400388 # Add Downstream EAPOL Flow, Only for first EAP flow
Shad Ansari2dda4f32018-05-17 07:16:07 +0000389
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400390 downlink_classifier = {}
391 downlink_classifier['pkt_tag_type'] = 'single_tag'
392 downlink_classifier['vlan_vid'] = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000393
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400394
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400395 downlink_action = {}
396 downlink_action['push_vlan'] = True
397 downlink_action['vlan_vid'] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400398
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400399 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
400 downlink_eapol_id)
401
402 downstream_flow = openolt_pb2.Flow(
403 onu_id=onu_id, flow_id=downlink_flow_id, flow_type="downstream",
404 access_intf_id=intf_id, gemport_id=gemport_id,
405 priority=logical_flow.priority,
406 classifier=self.mk_classifier(downlink_classifier),
407 action=self.mk_action(downlink_action))
408
409 downstream_logical_flow = ofp_flow_stats(id=logical_flow.id,
410 cookie=logical_flow.cookie, table_id=logical_flow.table_id,
411 priority=logical_flow.priority, flags=logical_flow.flags)
412
413 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
414 fd.in_port(fd.get_out_port(logical_flow)),
415 fd.vlan_vid((4000 - onu_id) | 0x1000)]))
416 downstream_logical_flow.match.type = OFPMT_OXM
417
418 downstream_logical_flow.instructions.extend(
419 fd.mk_instructions_from_actions([fd.output(
420 platform.mk_uni_port_num(intf_id, onu_id))]))
421
422 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400423
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400424 def repush_all_different_flows(self):
425 # Check if the device is supposed to have flows, if so add them
426 # Recover static flows after a reboot
427 logical_flows = self.logical_flows_proxy.get('/').items
428 devices_flows = self.flows_proxy.get('/').items
429 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
430 for logical_flow in logical_flows:
431 try:
432 if logical_flow.id not in logical_flows_ids_provisioned:
433 self.add_flow(logical_flow)
434 except Exception as e:
435 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400436
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400437 def reset_flows(self):
438 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400439
Shad Ansari2dda4f32018-05-17 07:16:07 +0000440 def mk_classifier(self, classifier_info):
441
442 classifier = openolt_pb2.Classifier()
443
444 if 'eth_type' in classifier_info:
445 classifier.eth_type = classifier_info['eth_type']
446 if 'ip_proto' in classifier_info:
447 classifier.ip_proto = classifier_info['ip_proto']
448 if 'vlan_vid' in classifier_info:
449 classifier.o_vid = classifier_info['vlan_vid']
450 if 'metadata' in classifier_info:
451 classifier.i_vid = classifier_info['metadata']
452 if 'vlan_pcp' in classifier_info:
453 classifier.o_pbits = classifier_info['vlan_pcp']
454 if 'udp_src' in classifier_info:
455 classifier.src_port = classifier_info['udp_src']
456 if 'udp_dst' in classifier_info:
457 classifier.dst_port = classifier_info['udp_dst']
458 if 'ipv4_dst' in classifier_info:
459 classifier.dst_ip = classifier_info['ipv4_dst']
460 if 'ipv4_src' in classifier_info:
461 classifier.src_ip = classifier_info['ipv4_src']
462 if 'pkt_tag_type' in classifier_info:
463 if classifier_info['pkt_tag_type'] == 'single_tag':
464 classifier.pkt_tag_type = 'single_tag'
465 elif classifier_info['pkt_tag_type'] == 'double_tag':
466 classifier.pkt_tag_type = 'double_tag'
467 elif classifier_info['pkt_tag_type'] == 'untagged':
468 classifier.pkt_tag_type = 'untagged'
469 else:
470 classifier.pkt_tag_type = 'none'
471
472 return classifier
473
474 def mk_action(self, action_info):
475 action = openolt_pb2.Action()
476
Shad Ansarif9d2d102018-06-13 02:15:26 +0000477 if 'pop_vlan' in action_info:
478 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000479 action.cmd.remove_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000480 elif 'push_vlan' in action_info:
481 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000482 action.cmd.add_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000483 elif 'trap_to_host' in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000484 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000485 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400486 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000487 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000488 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400489
490 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400491 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400492
493 for flow in flows:
494 eap_flow = False
495 eap_intf_id = None
496 eap_onu_id = None
497 for field in fd.get_ofb_fields(flow):
498 if field.type == fd.ETH_TYPE:
499 if field.eth_type == EAP_ETH_TYPE:
500 eap_flow = True
501 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000502 eap_intf_id = platform.intf_id_from_uni_port_num(
503 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400504 eap_onu_id = platform.onu_id_from_port_num(field.port)
505
506 if eap_flow:
507 self.log.debug('eap flow detected', onu_id=onu_id,
508 intf_id=intf_id, eap_intf_id=eap_intf_id,
509 eap_onu_id=eap_onu_id)
510 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400511 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400512
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400513 return (False, None)
514
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400515 def get_subscriber_vlan(self, port):
516 self.log.debug('looking from subscriber flow for port', port=port)
517
518 flows = self.logical_flows_proxy.get('/').items
519 for flow in flows:
520 in_port = fd.get_in_port(flow)
521 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400522
523 if in_port == port and \
524 platform.intf_id_to_port_type_name(out_port) == Port.ETHERNET_NNI:
525
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400526 fields = fd.get_ofb_fields(flow)
527 self.log.debug('subscriber flow found', fields=fields)
528 for field in fields:
529 if field.type == OFPXMT_OFB_VLAN_VID:
530 self.log.debug('subscriber vlan found',
531 vlan_id=field.vlan_vid)
532 return field.vlan_vid & 0x0fff
533 self.log.debug('No subscriber flow found', port=port)
534 return None
535
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400536 def add_flow_to_device(self, flow, logical_flow):
537 self.log.debug('pushing flow to device', flow=flow)
538 self.stub.FlowAdd(flow)
539 self.register_flow(logical_flow, flow)
540
541 def register_flow(self, logical_flow, device_flow):
542 self.log.debug('registering flow in device',
543 logical_flow=logical_flow, device_flow=device_flow)
544 stored_flow = copy.deepcopy(logical_flow)
545 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
546 device_flow.flow_type)
547 self.log.debug('generated device flow id', id=stored_flow.id,
548 flow_id=device_flow.flow_id,
549 direction=device_flow.flow_type)
550 stored_flow.cookie = logical_flow.id
551 flows = self.flows_proxy.get('/')
552 flows.items.extend([stored_flow])
553 self.flows_proxy.update('/', flows)
554
555
556 def find_next_flow(self, flow):
557 table_id = fd.get_goto_table_id(flow)
558 metadata = 0
559 for field in fd.get_ofb_fields(flow):
560 if field.type == fd.METADATA:
561 metadata = field.table_metadata
562 if table_id is None:
563 return None
564 flows = self.logical_flows_proxy.get('/').items
565 next_flows = []
566 for f in flows:
567 if f.table_id == table_id:
568 #FIXME:
569 if fd.get_in_port(f) == fd.get_in_port(flow) and \
570 fd.get_out_port(f) == metadata:
571
572 next_flows.append(f)
573
574
575 if len(next_flows) == 0:
576 self.log.warning('no next flow found, it may be a timing issue',
577 flow=flow, number_of_flows=len(flows))
578 reactor.callLater(5, self.add_flow, flow)
579 return None
580
581 next_flows.sort(key=lambda f:f.priority, reverse=True)
582
583 return next_flows[0]
584
585 def update_children_flows(self, device_rules_map):
586
587 for device_id, (flows, groups) in device_rules_map.iteritems():
588 if device_id != self.device_id:
589 self.root_proxy.update('/devices/{}/flows'.format(device_id),
590 Flows(items=flows.values()))
591 self.root_proxy.update('/devices/{}/flow_groups'.format(
592 device_id), FlowGroups(items=groups.values()))
593
594 def generate_stored_id(self, flow_id, direction):
595 if direction == 'upstream':
596 self.log.debug('upstream flow, shifting id')
597 return 0x1 << 15 | flow_id
598 elif direction == 'downstream':
599 self.log.debug('downstream flow, not shifting id')
600 return flow_id
601 else:
602 self.log.warn('Unrecognized direction', direction=direction)
603 return flow_id
604
605 def decode_stored_id(self, id):
606 if id >> 15 == 0x1:
607 return (id & 0x7fff, 'upstream')
608 else:
609 return (id, 'downstream')