blob: edf9028f65be5852f5c38ef51992d1660291d1d8 [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.
284 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
285 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000286
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400287 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000288 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
289
Shad Ansari2dda4f32018-05-17 07:16:07 +0000290 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400291 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000292 access_intf_id=intf_id, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400293 priority=logical_flow.priority,
294 classifier=self.mk_classifier(classifier),
295 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000296
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400297 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000298
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400299 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000300
Shad Ansari47e0c392018-07-17 23:55:07 +0000301 self.log.debug('add dhcp upstream trap', classifier=classifier,
302 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000303
304 action.clear()
305 action['trap_to_host'] = True
306 classifier['pkt_tag_type'] = 'single_tag'
307 classifier.pop('vlan_vid', None)
308
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400309 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000310 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
311
312 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000313 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400314 access_intf_id=intf_id, gemport_id=gemport_id,
315 priority=logical_flow.priority,
316 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000317 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000318
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400319 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000320
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400321
Shad Ansari47e0c392018-07-17 23:55:07 +0000322 # FIXME - ONOS should send explicit upstream and downstream
323 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400324
325 downstream_logical_flow = copy.deepcopy(logical_flow)
326 for oxm_field in downstream_logical_flow.match.oxm_fields:
327 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
328 oxm_field.ofb_field.port = 128
329
Shad Ansari47e0c392018-07-17 23:55:07 +0000330 classifier['udp_src'] = 67
331 classifier['udp_dst'] = 68
332 classifier['pkt_tag_type'] = 'double_tag'
333 action.pop('push_vlan', None)
334
335 flow_id = platform.mk_flow_id(intf_id, onu_id,
336 DHCP_DOWNLINK_FLOW_INDEX)
337
338 downstream_flow = openolt_pb2.Flow(
339 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
340 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400341 priority=logical_flow.priority, classifier=self.mk_classifier(
342 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000343 action=self.mk_action(action))
344
Shad Ansari47e0c392018-07-17 23:55:07 +0000345
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400346 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
347
348 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000349 uplink_eapol_id=EAPOL_FLOW_INDEX,
350 downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
351 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000352
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400353 downlink_classifier = {}
354 downlink_classifier['eth_type'] = EAP_ETH_TYPE
355 downlink_classifier['pkt_tag_type'] = 'single_tag'
356 downlink_classifier['vlan_vid'] = vlan_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000357
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400358 downlink_action = {}
359 downlink_action['push_vlan'] = True
360 downlink_action['vlan_vid'] = vlan_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000361
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400362 uplink_classifier = {}
363 uplink_classifier['eth_type'] = EAP_ETH_TYPE
Shad Ansari2dda4f32018-05-17 07:16:07 +0000364 uplink_classifier['pkt_tag_type'] = 'single_tag'
365 uplink_classifier['vlan_vid'] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400366
367 uplink_action = {}
Shad Ansari2dda4f32018-05-17 07:16:07 +0000368 uplink_action['trap_to_host'] = True
369
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400370 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400371
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400372 # Add Upstream EAPOL Flow.
373
374 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
375
Shad Ansari2dda4f32018-05-17 07:16:07 +0000376 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000377 onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400378 access_intf_id=intf_id, gemport_id=gemport_id,
379 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000380 classifier=self.mk_classifier(uplink_classifier),
381 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000382
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400383 logical_flow = copy.deepcopy(logical_flow)
384 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
385 vlan_id | 0x1000)]))
386 logical_flow.match.type = OFPMT_OXM
387
388 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000389
390 # Add Downstream EAPOL Flow.
Shad Ansarif9d2d102018-06-13 02:15:26 +0000391 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
392 downlink_eapol_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000393
394 downstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000395 onu_id=onu_id, flow_id=downlink_flow_id, flow_type="downstream",
396 access_intf_id=intf_id, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400397 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000398 classifier=self.mk_classifier(downlink_classifier),
399 action=self.mk_action(downlink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000400
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400401 downstream_logical_flow = ofp_flow_stats(id=logical_flow.id,
402 cookie=logical_flow.cookie, table_id=logical_flow.table_id,
403 priority=logical_flow.priority, flags=logical_flow.flags)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000404
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400405 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
406 fd.in_port(fd.get_out_port(logical_flow)),
407 fd.eth_type(EAP_ETH_TYPE), fd.vlan_vid(vlan_id | 0x1000)]))
408 downstream_logical_flow.match.type = OFPMT_OXM
409
410 downstream_logical_flow.instructions.extend(
411 fd.mk_instructions_from_actions([fd.output(
412 platform.mk_uni_port_num(intf_id, onu_id))]))
413
414 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
415
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400416 def repush_all_different_flows(self):
417 # Check if the device is supposed to have flows, if so add them
418 # Recover static flows after a reboot
419 logical_flows = self.logical_flows_proxy.get('/').items
420 devices_flows = self.flows_proxy.get('/').items
421 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
422 for logical_flow in logical_flows:
423 try:
424 if logical_flow.id not in logical_flows_ids_provisioned:
425 self.add_flow(logical_flow)
426 except Exception as e:
427 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400428
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400429 def reset_flows(self):
430 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400431
Shad Ansari2dda4f32018-05-17 07:16:07 +0000432 def mk_classifier(self, classifier_info):
433
434 classifier = openolt_pb2.Classifier()
435
436 if 'eth_type' in classifier_info:
437 classifier.eth_type = classifier_info['eth_type']
438 if 'ip_proto' in classifier_info:
439 classifier.ip_proto = classifier_info['ip_proto']
440 if 'vlan_vid' in classifier_info:
441 classifier.o_vid = classifier_info['vlan_vid']
442 if 'metadata' in classifier_info:
443 classifier.i_vid = classifier_info['metadata']
444 if 'vlan_pcp' in classifier_info:
445 classifier.o_pbits = classifier_info['vlan_pcp']
446 if 'udp_src' in classifier_info:
447 classifier.src_port = classifier_info['udp_src']
448 if 'udp_dst' in classifier_info:
449 classifier.dst_port = classifier_info['udp_dst']
450 if 'ipv4_dst' in classifier_info:
451 classifier.dst_ip = classifier_info['ipv4_dst']
452 if 'ipv4_src' in classifier_info:
453 classifier.src_ip = classifier_info['ipv4_src']
454 if 'pkt_tag_type' in classifier_info:
455 if classifier_info['pkt_tag_type'] == 'single_tag':
456 classifier.pkt_tag_type = 'single_tag'
457 elif classifier_info['pkt_tag_type'] == 'double_tag':
458 classifier.pkt_tag_type = 'double_tag'
459 elif classifier_info['pkt_tag_type'] == 'untagged':
460 classifier.pkt_tag_type = 'untagged'
461 else:
462 classifier.pkt_tag_type = 'none'
463
464 return classifier
465
466 def mk_action(self, action_info):
467 action = openolt_pb2.Action()
468
Shad Ansarif9d2d102018-06-13 02:15:26 +0000469 if 'pop_vlan' in action_info:
470 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000471 action.cmd.remove_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000472 elif 'push_vlan' in action_info:
473 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000474 action.cmd.add_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000475 elif 'trap_to_host' in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000476 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000477 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400478 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000479 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000480 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400481
482 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400483 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400484
485 for flow in flows:
486 eap_flow = False
487 eap_intf_id = None
488 eap_onu_id = None
489 for field in fd.get_ofb_fields(flow):
490 if field.type == fd.ETH_TYPE:
491 if field.eth_type == EAP_ETH_TYPE:
492 eap_flow = True
493 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000494 eap_intf_id = platform.intf_id_from_uni_port_num(
495 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400496 eap_onu_id = platform.onu_id_from_port_num(field.port)
497
498 if eap_flow:
499 self.log.debug('eap flow detected', onu_id=onu_id,
500 intf_id=intf_id, eap_intf_id=eap_intf_id,
501 eap_onu_id=eap_onu_id)
502 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400503 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400504
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400505 return (False, None)
506
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400507 def get_subscriber_vlan(self, port):
508 self.log.debug('looking from subscriber flow for port', port=port)
509
510 flows = self.logical_flows_proxy.get('/').items
511 for flow in flows:
512 in_port = fd.get_in_port(flow)
513 out_port = fd.get_out_port(flow)
514 #FIXME
515 if in_port == port and out_port == 128:
516 fields = fd.get_ofb_fields(flow)
517 self.log.debug('subscriber flow found', fields=fields)
518 for field in fields:
519 if field.type == OFPXMT_OFB_VLAN_VID:
520 self.log.debug('subscriber vlan found',
521 vlan_id=field.vlan_vid)
522 return field.vlan_vid & 0x0fff
523 self.log.debug('No subscriber flow found', port=port)
524 return None
525
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400526 def add_flow_to_device(self, flow, logical_flow):
527 self.log.debug('pushing flow to device', flow=flow)
528 self.stub.FlowAdd(flow)
529 self.register_flow(logical_flow, flow)
530
531 def register_flow(self, logical_flow, device_flow):
532 self.log.debug('registering flow in device',
533 logical_flow=logical_flow, device_flow=device_flow)
534 stored_flow = copy.deepcopy(logical_flow)
535 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
536 device_flow.flow_type)
537 self.log.debug('generated device flow id', id=stored_flow.id,
538 flow_id=device_flow.flow_id,
539 direction=device_flow.flow_type)
540 stored_flow.cookie = logical_flow.id
541 flows = self.flows_proxy.get('/')
542 flows.items.extend([stored_flow])
543 self.flows_proxy.update('/', flows)
544
545
546 def find_next_flow(self, flow):
547 table_id = fd.get_goto_table_id(flow)
548 metadata = 0
549 for field in fd.get_ofb_fields(flow):
550 if field.type == fd.METADATA:
551 metadata = field.table_metadata
552 if table_id is None:
553 return None
554 flows = self.logical_flows_proxy.get('/').items
555 next_flows = []
556 for f in flows:
557 if f.table_id == table_id:
558 #FIXME:
559 if fd.get_in_port(f) == fd.get_in_port(flow) and \
560 fd.get_out_port(f) == metadata:
561
562 next_flows.append(f)
563
564
565 if len(next_flows) == 0:
566 self.log.warning('no next flow found, it may be a timing issue',
567 flow=flow, number_of_flows=len(flows))
568 reactor.callLater(5, self.add_flow, flow)
569 return None
570
571 next_flows.sort(key=lambda f:f.priority, reverse=True)
572
573 return next_flows[0]
574
575 def update_children_flows(self, device_rules_map):
576
577 for device_id, (flows, groups) in device_rules_map.iteritems():
578 if device_id != self.device_id:
579 self.root_proxy.update('/devices/{}/flows'.format(device_id),
580 Flows(items=flows.values()))
581 self.root_proxy.update('/devices/{}/flow_groups'.format(
582 device_id), FlowGroups(items=groups.values()))
583
584 def generate_stored_id(self, flow_id, direction):
585 if direction == 'upstream':
586 self.log.debug('upstream flow, shifting id')
587 return 0x1 << 15 | flow_id
588 elif direction == 'downstream':
589 self.log.debug('downstream flow, not shifting id')
590 return flow_id
591 else:
592 self.log.warn('Unrecognized direction', direction=direction)
593 return flow_id
594
595 def decode_stored_id(self, id):
596 if id >> 15 == 0x1:
597 return (id & 0x7fff, 'upstream')
598 else:
599 return (id, 'downstream')