blob: 96afa0fb674831931243f57f74398c744383a27f [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
Shad Ansari2dda4f32018-05-17 07:16:07 +000023import voltha.core.flow_decomposer as fd
24import openolt_platform as platform
25from voltha.adapters.openolt.protos import openolt_pb2
Nicolas Palpacuer61815162018-06-20 18:12:04 -040026from voltha.registry import registry
Shad Ansari2dda4f32018-05-17 07:16:07 +000027
Shad Ansarif9d2d102018-06-13 02:15:26 +000028HSIA_FLOW_INDEX = 0 # FIXME
29DHCP_FLOW_INDEX = 1 # FIXME
Shad Ansari47e0c392018-07-17 23:55:07 +000030DHCP_DOWNLINK_FLOW_INDEX = 6 # FIXME
Shad Ansarif9d2d102018-06-13 02:15:26 +000031EAPOL_FLOW_INDEX = 2 # FIXME
32EAPOL_DOWNLINK_FLOW_INDEX = 3 # FIXME
Nicolas Palpacuer61815162018-06-20 18:12:04 -040033EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX = 4 # FIXME
34EAPOL_UPLINK_SECONDARY_FLOW_INDEX = 5 # FIXME
35
36
37EAP_ETH_TYPE = 0x888e
Shad Ansari2dda4f32018-05-17 07:16:07 +000038
39# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
40DEFAULT_MGMT_VLAN = 4091
41
Shad Ansarif9d2d102018-06-13 02:15:26 +000042
Shad Ansari2dda4f32018-05-17 07:16:07 +000043class OpenOltFlowMgr(object):
44
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040045 def __init__(self, log, stub, device_id, logical_device_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +000046 self.log = log
47 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040048 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040049 self.logical_device_id = logical_device_id
50 self.logical_flows_proxy = registry('core').get_proxy(
51 '/logical_devices/{}/flows'.format(self.logical_device_id))
52 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040053 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040054 self.root_proxy = registry('core').get_proxy('/')
55 self.fd_object = fd.FlowDecomposer()
Shad Ansari2dda4f32018-05-17 07:16:07 +000056
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040057 def add_flow(self, flow):
58 self.log.debug('add flow', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +000059 classifier_info = dict()
60 action_info = dict()
61
Shad Ansari2dda4f32018-05-17 07:16:07 +000062
63 for field in fd.get_ofb_fields(flow):
64 if field.type == fd.ETH_TYPE:
65 classifier_info['eth_type'] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +000066 self.log.debug('field-type-eth-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +000067 eth_type=classifier_info['eth_type'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000068 elif field.type == fd.IP_PROTO:
69 classifier_info['ip_proto'] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +000070 self.log.debug('field-type-ip-proto',
Shad Ansarif9d2d102018-06-13 02:15:26 +000071 ip_proto=classifier_info['ip_proto'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000072 elif field.type == fd.IN_PORT:
73 classifier_info['in_port'] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +000074 self.log.debug('field-type-in-port',
Shad Ansarif9d2d102018-06-13 02:15:26 +000075 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000076 elif field.type == fd.VLAN_VID:
77 classifier_info['vlan_vid'] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +000078 self.log.debug('field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +000079 vlan=classifier_info['vlan_vid'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000080 elif field.type == fd.VLAN_PCP:
81 classifier_info['vlan_pcp'] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +000082 self.log.debug('field-type-vlan-pcp',
Shad Ansarif9d2d102018-06-13 02:15:26 +000083 pcp=classifier_info['vlan_pcp'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000084 elif field.type == fd.UDP_DST:
85 classifier_info['udp_dst'] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000086 self.log.debug('field-type-udp-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000087 udp_dst=classifier_info['udp_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000088 elif field.type == fd.UDP_SRC:
89 classifier_info['udp_src'] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000090 self.log.debug('field-type-udp-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +000091 udp_src=classifier_info['udp_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000092 elif field.type == fd.IPV4_DST:
93 classifier_info['ipv4_dst'] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000094 self.log.debug('field-type-ipv4-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000095 ipv4_dst=classifier_info['ipv4_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000096 elif field.type == fd.IPV4_SRC:
97 classifier_info['ipv4_src'] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000098 self.log.debug('field-type-ipv4-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +000099 ipv4_dst=classifier_info['ipv4_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000100 elif field.type == fd.METADATA:
101 classifier_info['metadata'] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +0000102 self.log.debug('field-type-metadata',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000103 metadata=classifier_info['metadata'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000104 else:
105 raise NotImplementedError('field.type={}'.format(
106 field.type))
107
108 for action in fd.get_actions(flow):
109 if action.type == fd.OUTPUT:
110 action_info['output'] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000111 self.log.debug('action-type-output',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000112 output=action_info['output'],
113 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000114 elif action.type == fd.POP_VLAN:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400115 if fd.get_goto_table_id(flow) is None:
116 self.log.debug('being taken care of by ONU', flow=flow)
117 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000118 action_info['pop_vlan'] = True
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400119 self.log.debug('action-type-pop-vlan', in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000120 elif action.type == fd.PUSH_VLAN:
121 action_info['push_vlan'] = True
122 action_info['tpid'] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000123 self.log.debug('action-type-push-vlan',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400124 push_tpid=action_info['tpid'], in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000125 if action.push.ethertype != 0x8100:
126 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000127 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000128 elif action.type == fd.SET_FIELD:
129 # action_info['action_type'] = 'set_field'
130 _field = action.set_field.field.ofb_field
131 assert (action.set_field.field.oxm_class ==
132 OFPXMC_OPENFLOW_BASIC)
Shad Ansarie048aaa2018-05-18 18:27:21 +0000133 self.log.debug('action-type-set-field',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400134 field=_field, in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000135 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000136 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000137 vlan_vid=_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000138 action_info['vlan_vid'] = (_field.vlan_vid & 0xfff)
139 else:
140 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000141 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000142 else:
143 self.log.error('unsupported-action-type',
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400144 action_type=action.type, in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000145
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400146 if fd.get_goto_table_id(flow) is not None and not 'pop_vlan' in \
147 action_info:
148 self.log.debug('being taken care of by ONU', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000149
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400150 if not 'output' in action_info and 'metadata' in classifier_info:
151 #find flow in the next table
152 next_flow = self.find_next_flow(flow)
153 if next_flow is None:
154 return
155 action_info['output'] = fd.get_out_port(next_flow)
156 for field in fd.get_ofb_fields(next_flow):
157 if field.type == fd.VLAN_VID:
158 classifier_info['metadata'] = field.vlan_vid & 0xfff
159
160
161 (intf_id, onu_id) = platform.extract_access_from_flow(
162 classifier_info['in_port'], action_info['output'])
163
164
165 self.divide_and_add_flow(intf_id, onu_id, classifier_info,
166 action_info, flow)
167
168 def remove_flow(self, flow):
169 self.log.debug('trying to remove flows from logical flow :',
170 logical_flow=flow)
171 device_flows_to_remove = []
172 device_flows = self.flows_proxy.get('/').items
173 for f in device_flows:
174 if f.cookie == flow.id:
175 device_flows_to_remove.append(f)
176
177
178 for f in device_flows_to_remove:
179 (id, direction) = self.decode_stored_id(f.id)
180 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400181 try:
182 self.stub.FlowRemove(flow_to_remove)
183 except grpc.RpcError as grpc_e:
184 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
185 self.log.debug('This flow does not exist on the switch, '
186 'normal after an OLT reboot', flow=flow_to_remove)
187 else:
188 raise grpc_e
189
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400190 self.log.debug('flow removed from device', flow=f,
191 flow_key=flow_to_remove)
192
193 if len(device_flows_to_remove) > 0:
194 new_flows = []
195 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
196 for f in device_flows:
197 if f.id not in flows_ids_to_remove:
198 new_flows.append(f)
199
200 self.flows_proxy.update('/', Flows(items=new_flows))
201 self.log.debug('flows removed from the data store',
202 flow_ids_removed=flows_ids_to_remove,
203 number_of_flows_removed=(len(device_flows) - len(
204 new_flows)), expected_flows_removed=len(
205 device_flows_to_remove))
206 else:
207 self.log.debug('no device flow to remove for this flow (normal '
208 'for multi table flows)', flow=flow)
209
210
211 def divide_and_add_flow(self, intf_id, onu_id, classifier,
212 action, flow):
213
214 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id,
215 classifier=classifier, action=action)
216
Shad Ansari2dda4f32018-05-17 07:16:07 +0000217 if 'ip_proto' in classifier:
218 if classifier['ip_proto'] == 17:
219 self.log.debug('dhcp flow add')
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400220 self.add_dhcp_trap(intf_id, onu_id, classifier,
221 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000222 elif classifier['ip_proto'] == 2:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400223 self.log.warn('igmp flow add ignored, not implemented yet')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000224 else:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400225 self.log.warn("Invalid-Classifier-to-handle",
Shad Ansarif9d2d102018-06-13 02:15:26 +0000226 classifier=classifier,
227 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000228 elif 'eth_type' in classifier:
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400229 if classifier['eth_type'] == EAP_ETH_TYPE:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000230 self.log.debug('eapol flow add')
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400231 self.add_eapol_flow(intf_id, onu_id, flow)
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400232 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
233 if vlan_id is not None:
234 self.add_eapol_flow(intf_id, onu_id, flow,
235 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
236 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
237 vlan_id=vlan_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400238
Shad Ansari2dda4f32018-05-17 07:16:07 +0000239 elif 'push_vlan' in action:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400240 self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
241 flow)
242 elif 'pop_vlan' in action:
243 self.add_downstream_data_flow(intf_id, onu_id, classifier,
244 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000245 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000246 self.log.debug('Invalid-flow-type-to-handle',
247 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400248 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000249
Shad Ansari2dda4f32018-05-17 07:16:07 +0000250
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400251 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
252 uplink_action, logical_flow):
253
Shad Ansari2dda4f32018-05-17 07:16:07 +0000254
255 uplink_classifier['pkt_tag_type'] = 'single_tag'
256
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400257 self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
258 uplink_action, 'upstream', HSIA_FLOW_INDEX,
259 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000260
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400261 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400262 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400263 if eap_active:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400264 self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400265 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
266 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
267 vlan_id=uplink_classifier['vlan_vid'])
268
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400269
270 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
271 downlink_action, flow):
272 downlink_classifier['pkt_tag_type'] = 'double_tag'
273 # Needed ???? It should be already there
274 downlink_action['pop_vlan'] = True
275 downlink_action['vlan_vid'] = downlink_classifier['vlan_vid']
276
277 self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
278 downlink_action, 'downstream', HSIA_FLOW_INDEX,
279 flow)
280
281 # To-Do right now only one GEM port is supported, so below method
282 # will take care of handling all the p bits.
283 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400284 # Waiting for Technology profile
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400285 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
286 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000287
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400288 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000289 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
290
Shad Ansari2dda4f32018-05-17 07:16:07 +0000291 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400292 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000293 access_intf_id=intf_id, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400294 priority=logical_flow.priority,
295 classifier=self.mk_classifier(classifier),
296 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000297
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400298 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000299
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400300 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000301
Shad Ansari47e0c392018-07-17 23:55:07 +0000302 self.log.debug('add dhcp upstream trap', classifier=classifier,
303 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000304
305 action.clear()
306 action['trap_to_host'] = True
307 classifier['pkt_tag_type'] = 'single_tag'
308 classifier.pop('vlan_vid', None)
309
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400310 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000311 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
312
313 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000314 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400315 access_intf_id=intf_id, gemport_id=gemport_id,
316 priority=logical_flow.priority,
317 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000318 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000319
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400320 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000321
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400322
Shad Ansari47e0c392018-07-17 23:55:07 +0000323 # FIXME - ONOS should send explicit upstream and downstream
324 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400325
326 downstream_logical_flow = copy.deepcopy(logical_flow)
327 for oxm_field in downstream_logical_flow.match.oxm_fields:
328 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
329 oxm_field.ofb_field.port = 128
330
Shad Ansari47e0c392018-07-17 23:55:07 +0000331 classifier['udp_src'] = 67
332 classifier['udp_dst'] = 68
333 classifier['pkt_tag_type'] = 'double_tag'
334 action.pop('push_vlan', None)
335
336 flow_id = platform.mk_flow_id(intf_id, onu_id,
337 DHCP_DOWNLINK_FLOW_INDEX)
338
339 downstream_flow = openolt_pb2.Flow(
340 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
341 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400342 priority=logical_flow.priority, classifier=self.mk_classifier(
343 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000344 action=self.mk_action(action))
345
Shad Ansari47e0c392018-07-17 23:55:07 +0000346
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400347 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
348
349 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000350 uplink_eapol_id=EAPOL_FLOW_INDEX,
351 downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
352 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000353
Shad Ansari2dda4f32018-05-17 07:16:07 +0000354
Shad Ansari2dda4f32018-05-17 07:16:07 +0000355
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400356 uplink_classifier = {}
357 uplink_classifier['eth_type'] = EAP_ETH_TYPE
Shad Ansari2dda4f32018-05-17 07:16:07 +0000358 uplink_classifier['pkt_tag_type'] = 'single_tag'
359 uplink_classifier['vlan_vid'] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400360
361 uplink_action = {}
Shad Ansari2dda4f32018-05-17 07:16:07 +0000362 uplink_action['trap_to_host'] = True
363
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400364 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400365
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400366 # Add Upstream EAPOL Flow.
367
368 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
369
Shad Ansari2dda4f32018-05-17 07:16:07 +0000370 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000371 onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400372 access_intf_id=intf_id, gemport_id=gemport_id,
373 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000374 classifier=self.mk_classifier(uplink_classifier),
375 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000376
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400377 logical_flow = copy.deepcopy(logical_flow)
378 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
379 vlan_id | 0x1000)]))
380 logical_flow.match.type = OFPMT_OXM
381
382 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000383
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400384 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000385
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400386 # Add Downstream EAPOL Flow, Only for first EAP flow
Shad Ansari2dda4f32018-05-17 07:16:07 +0000387
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400388 downlink_classifier = {}
389 downlink_classifier['pkt_tag_type'] = 'single_tag'
390 downlink_classifier['vlan_vid'] = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000391
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400392
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400393 downlink_action = {}
394 downlink_action['push_vlan'] = True
395 downlink_action['vlan_vid'] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400396
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400397 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
398 downlink_eapol_id)
399
400 downstream_flow = openolt_pb2.Flow(
401 onu_id=onu_id, flow_id=downlink_flow_id, flow_type="downstream",
402 access_intf_id=intf_id, gemport_id=gemport_id,
403 priority=logical_flow.priority,
404 classifier=self.mk_classifier(downlink_classifier),
405 action=self.mk_action(downlink_action))
406
407 downstream_logical_flow = ofp_flow_stats(id=logical_flow.id,
408 cookie=logical_flow.cookie, table_id=logical_flow.table_id,
409 priority=logical_flow.priority, flags=logical_flow.flags)
410
411 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
412 fd.in_port(fd.get_out_port(logical_flow)),
413 fd.vlan_vid((4000 - onu_id) | 0x1000)]))
414 downstream_logical_flow.match.type = OFPMT_OXM
415
416 downstream_logical_flow.instructions.extend(
417 fd.mk_instructions_from_actions([fd.output(
418 platform.mk_uni_port_num(intf_id, onu_id))]))
419
420 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400421
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400422 def repush_all_different_flows(self):
423 # Check if the device is supposed to have flows, if so add them
424 # Recover static flows after a reboot
425 logical_flows = self.logical_flows_proxy.get('/').items
426 devices_flows = self.flows_proxy.get('/').items
427 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
428 for logical_flow in logical_flows:
429 try:
430 if logical_flow.id not in logical_flows_ids_provisioned:
431 self.add_flow(logical_flow)
432 except Exception as e:
433 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400434
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400435 def reset_flows(self):
436 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400437
Shad Ansari2dda4f32018-05-17 07:16:07 +0000438 def mk_classifier(self, classifier_info):
439
440 classifier = openolt_pb2.Classifier()
441
442 if 'eth_type' in classifier_info:
443 classifier.eth_type = classifier_info['eth_type']
444 if 'ip_proto' in classifier_info:
445 classifier.ip_proto = classifier_info['ip_proto']
446 if 'vlan_vid' in classifier_info:
447 classifier.o_vid = classifier_info['vlan_vid']
448 if 'metadata' in classifier_info:
449 classifier.i_vid = classifier_info['metadata']
450 if 'vlan_pcp' in classifier_info:
451 classifier.o_pbits = classifier_info['vlan_pcp']
452 if 'udp_src' in classifier_info:
453 classifier.src_port = classifier_info['udp_src']
454 if 'udp_dst' in classifier_info:
455 classifier.dst_port = classifier_info['udp_dst']
456 if 'ipv4_dst' in classifier_info:
457 classifier.dst_ip = classifier_info['ipv4_dst']
458 if 'ipv4_src' in classifier_info:
459 classifier.src_ip = classifier_info['ipv4_src']
460 if 'pkt_tag_type' in classifier_info:
461 if classifier_info['pkt_tag_type'] == 'single_tag':
462 classifier.pkt_tag_type = 'single_tag'
463 elif classifier_info['pkt_tag_type'] == 'double_tag':
464 classifier.pkt_tag_type = 'double_tag'
465 elif classifier_info['pkt_tag_type'] == 'untagged':
466 classifier.pkt_tag_type = 'untagged'
467 else:
468 classifier.pkt_tag_type = 'none'
469
470 return classifier
471
472 def mk_action(self, action_info):
473 action = openolt_pb2.Action()
474
Shad Ansarif9d2d102018-06-13 02:15:26 +0000475 if 'pop_vlan' in action_info:
476 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000477 action.cmd.remove_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000478 elif 'push_vlan' in action_info:
479 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000480 action.cmd.add_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000481 elif 'trap_to_host' in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000482 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000483 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400484 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000485 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000486 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400487
488 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400489 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400490
491 for flow in flows:
492 eap_flow = False
493 eap_intf_id = None
494 eap_onu_id = None
495 for field in fd.get_ofb_fields(flow):
496 if field.type == fd.ETH_TYPE:
497 if field.eth_type == EAP_ETH_TYPE:
498 eap_flow = True
499 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000500 eap_intf_id = platform.intf_id_from_uni_port_num(
501 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400502 eap_onu_id = platform.onu_id_from_port_num(field.port)
503
504 if eap_flow:
505 self.log.debug('eap flow detected', onu_id=onu_id,
506 intf_id=intf_id, eap_intf_id=eap_intf_id,
507 eap_onu_id=eap_onu_id)
508 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400509 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400510
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400511 return (False, None)
512
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400513 def get_subscriber_vlan(self, port):
514 self.log.debug('looking from subscriber flow for port', port=port)
515
516 flows = self.logical_flows_proxy.get('/').items
517 for flow in flows:
518 in_port = fd.get_in_port(flow)
519 out_port = fd.get_out_port(flow)
520 #FIXME
521 if in_port == port and out_port == 128:
522 fields = fd.get_ofb_fields(flow)
523 self.log.debug('subscriber flow found', fields=fields)
524 for field in fields:
525 if field.type == OFPXMT_OFB_VLAN_VID:
526 self.log.debug('subscriber vlan found',
527 vlan_id=field.vlan_vid)
528 return field.vlan_vid & 0x0fff
529 self.log.debug('No subscriber flow found', port=port)
530 return None
531
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400532 def add_flow_to_device(self, flow, logical_flow):
533 self.log.debug('pushing flow to device', flow=flow)
534 self.stub.FlowAdd(flow)
535 self.register_flow(logical_flow, flow)
536
537 def register_flow(self, logical_flow, device_flow):
538 self.log.debug('registering flow in device',
539 logical_flow=logical_flow, device_flow=device_flow)
540 stored_flow = copy.deepcopy(logical_flow)
541 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
542 device_flow.flow_type)
543 self.log.debug('generated device flow id', id=stored_flow.id,
544 flow_id=device_flow.flow_id,
545 direction=device_flow.flow_type)
546 stored_flow.cookie = logical_flow.id
547 flows = self.flows_proxy.get('/')
548 flows.items.extend([stored_flow])
549 self.flows_proxy.update('/', flows)
550
551
552 def find_next_flow(self, flow):
553 table_id = fd.get_goto_table_id(flow)
554 metadata = 0
555 for field in fd.get_ofb_fields(flow):
556 if field.type == fd.METADATA:
557 metadata = field.table_metadata
558 if table_id is None:
559 return None
560 flows = self.logical_flows_proxy.get('/').items
561 next_flows = []
562 for f in flows:
563 if f.table_id == table_id:
564 #FIXME:
565 if fd.get_in_port(f) == fd.get_in_port(flow) and \
566 fd.get_out_port(f) == metadata:
567
568 next_flows.append(f)
569
570
571 if len(next_flows) == 0:
572 self.log.warning('no next flow found, it may be a timing issue',
573 flow=flow, number_of_flows=len(flows))
574 reactor.callLater(5, self.add_flow, flow)
575 return None
576
577 next_flows.sort(key=lambda f:f.priority, reverse=True)
578
579 return next_flows[0]
580
581 def update_children_flows(self, device_rules_map):
582
583 for device_id, (flows, groups) in device_rules_map.iteritems():
584 if device_id != self.device_id:
585 self.root_proxy.update('/devices/{}/flows'.format(device_id),
586 Flows(items=flows.values()))
587 self.root_proxy.update('/devices/{}/flow_groups'.format(
588 device_id), FlowGroups(items=groups.values()))
589
590 def generate_stored_id(self, flow_id, direction):
591 if direction == 'upstream':
592 self.log.debug('upstream flow, shifting id')
593 return 0x1 << 15 | flow_id
594 elif direction == 'downstream':
595 self.log.debug('downstream flow, not shifting id')
596 return flow_id
597 else:
598 self.log.warn('Unrecognized direction', direction=direction)
599 return flow_id
600
601 def decode_stored_id(self, id):
602 if id >> 15 == 0x1:
603 return (id & 0x7fff, 'upstream')
604 else:
605 return (id, 'downstream')