blob: 4c409101c63ab4002692fc78ab6adcd983bb48db [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, \
Jonathan Hart5b435642018-08-20 08:50:05 -070021 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
22 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
Jonathan Hart5b435642018-08-20 08:50:05 -070036LLDP_FLOW_INDEX = 7 # FIXME
Nicolas Palpacuer61815162018-06-20 18:12:04 -040037
38EAP_ETH_TYPE = 0x888e
Jonathan Hart5b435642018-08-20 08:50:05 -070039LLDP_ETH_TYPE = 0x88cc
Shad Ansari2dda4f32018-05-17 07:16:07 +000040
41# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
42DEFAULT_MGMT_VLAN = 4091
43
Shad Ansarif9d2d102018-06-13 02:15:26 +000044
Shad Ansari2dda4f32018-05-17 07:16:07 +000045class OpenOltFlowMgr(object):
46
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040047 def __init__(self, log, stub, device_id, logical_device_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +000048 self.log = log
49 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040050 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040051 self.logical_device_id = logical_device_id
52 self.logical_flows_proxy = registry('core').get_proxy(
53 '/logical_devices/{}/flows'.format(self.logical_device_id))
54 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040055 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040056 self.root_proxy = registry('core').get_proxy('/')
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 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
Jonathan Hart5b435642018-08-20 08:50:05 -0700119 self.log.debug('action-type-pop-vlan',
120 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',
Jonathan Hart5b435642018-08-20 08:50:05 -0700125 push_tpid=action_info['tpid'],
126 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000127 if action.push.ethertype != 0x8100:
128 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000129 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000130 elif action.type == fd.SET_FIELD:
131 # action_info['action_type'] = 'set_field'
132 _field = action.set_field.field.ofb_field
133 assert (action.set_field.field.oxm_class ==
134 OFPXMC_OPENFLOW_BASIC)
Jonathan Hart5b435642018-08-20 08:50:05 -0700135 self.log.debug('action-type-set-field', field=_field,
136 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000137 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000138 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000139 vlan_vid=_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000140 action_info['vlan_vid'] = (_field.vlan_vid & 0xfff)
141 else:
142 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000143 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000144 else:
145 self.log.error('unsupported-action-type',
Jonathan Hart5b435642018-08-20 08:50:05 -0700146 action_type=action.type,
147 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000148
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400149 if fd.get_goto_table_id(flow) is not None and not 'pop_vlan' in \
150 action_info:
151 self.log.debug('being taken care of by ONU', flow=flow)
Nicolas Palpacuer856d3af2018-09-12 15:04:51 -0400152 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000153
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400154 if not 'output' in action_info and 'metadata' in classifier_info:
Jonathan Hart5b435642018-08-20 08:50:05 -0700155 # find flow in the next table
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400156 next_flow = self.find_next_flow(flow)
157 if next_flow is None:
158 return
159 action_info['output'] = fd.get_out_port(next_flow)
160 for field in fd.get_ofb_fields(next_flow):
161 if field.type == fd.VLAN_VID:
162 classifier_info['metadata'] = field.vlan_vid & 0xfff
163
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400164 (intf_id, onu_id) = platform.extract_access_from_flow(
165 classifier_info['in_port'], action_info['output'])
166
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400167 self.divide_and_add_flow(intf_id, onu_id, classifier_info,
168 action_info, flow)
169
170 def remove_flow(self, flow):
171 self.log.debug('trying to remove flows from logical flow :',
Jonathan Hart5b435642018-08-20 08:50:05 -0700172 logical_flow=flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400173 device_flows_to_remove = []
174 device_flows = self.flows_proxy.get('/').items
175 for f in device_flows:
176 if f.cookie == flow.id:
177 device_flows_to_remove.append(f)
178
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400179 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, '
Jonathan Hart5b435642018-08-20 08:50:05 -0700187 'normal after an OLT reboot',
188 flow=flow_to_remove)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400189 else:
190 raise grpc_e
191
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400192 self.log.debug('flow removed from device', flow=f,
193 flow_key=flow_to_remove)
194
195 if len(device_flows_to_remove) > 0:
196 new_flows = []
197 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
198 for f in device_flows:
199 if f.id not in flows_ids_to_remove:
200 new_flows.append(f)
201
202 self.flows_proxy.update('/', Flows(items=new_flows))
203 self.log.debug('flows removed from the data store',
204 flow_ids_removed=flows_ids_to_remove,
205 number_of_flows_removed=(len(device_flows) - len(
206 new_flows)), expected_flows_removed=len(
207 device_flows_to_remove))
208 else:
209 self.log.debug('no device flow to remove for this flow (normal '
210 'for multi table flows)', flow=flow)
211
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400212 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",
Jonathan Hart5b435642018-08-20 08:50:05 -0700227 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:
Jonathan Hart5b435642018-08-20 08:50:05 -0700235 self.add_eapol_flow(
236 intf_id, onu_id, flow,
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400237 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
238 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
239 vlan_id=vlan_id)
Jonathan Hart5b435642018-08-20 08:50:05 -0700240 if classifier['eth_type'] == LLDP_ETH_TYPE:
241 self.log.debug('lldp flow add')
242 self.add_lldp_flow(intf_id, onu_id, flow, classifier,
243 action)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400244
Shad Ansari2dda4f32018-05-17 07:16:07 +0000245 elif 'push_vlan' in action:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400246 self.add_upstream_data_flow(intf_id, onu_id, classifier, action,
Jonathan Hart5b435642018-08-20 08:50:05 -0700247 flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400248 elif 'pop_vlan' in action:
249 self.add_downstream_data_flow(intf_id, onu_id, classifier,
250 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000251 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000252 self.log.debug('Invalid-flow-type-to-handle',
253 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400254 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000255
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400256 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
Jonathan Hart5b435642018-08-20 08:50:05 -0700257 uplink_action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000258
259 uplink_classifier['pkt_tag_type'] = 'single_tag'
260
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400261 self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
262 uplink_action, 'upstream', HSIA_FLOW_INDEX,
263 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000264
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400265 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400266 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400267 if eap_active:
Jonathan Hart5b435642018-08-20 08:50:05 -0700268 self.add_eapol_flow(
269 intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400270 uplink_eapol_id=EAPOL_UPLINK_SECONDARY_FLOW_INDEX,
271 downlink_eapol_id=EAPOL_DOWNLINK_SECONDARY_FLOW_INDEX,
272 vlan_id=uplink_classifier['vlan_vid'])
273
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400274 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
275 downlink_action, flow):
276 downlink_classifier['pkt_tag_type'] = 'double_tag'
277 # Needed ???? It should be already there
278 downlink_action['pop_vlan'] = True
279 downlink_action['vlan_vid'] = downlink_classifier['vlan_vid']
280
281 self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
282 downlink_action, 'downstream', HSIA_FLOW_INDEX,
283 flow)
284
285 # To-Do right now only one GEM port is supported, so below method
286 # will take care of handling all the p bits.
287 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400288 # Waiting for Technology profile
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400289 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
290 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000291
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400292 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000293 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
294
Shad Ansari2dda4f32018-05-17 07:16:07 +0000295 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400296 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000297 access_intf_id=intf_id, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400298 priority=logical_flow.priority,
299 classifier=self.mk_classifier(classifier),
300 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000301
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400302 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000303
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400304 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000305
Shad Ansari47e0c392018-07-17 23:55:07 +0000306 self.log.debug('add dhcp upstream trap', classifier=classifier,
307 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000308
309 action.clear()
310 action['trap_to_host'] = True
311 classifier['pkt_tag_type'] = 'single_tag'
312 classifier.pop('vlan_vid', None)
313
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400314 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000315 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
316
317 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000318 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400319 access_intf_id=intf_id, gemport_id=gemport_id,
320 priority=logical_flow.priority,
321 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000322 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000323
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400324 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000325
Shad Ansari47e0c392018-07-17 23:55:07 +0000326 # FIXME - ONOS should send explicit upstream and downstream
327 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400328
329 downstream_logical_flow = copy.deepcopy(logical_flow)
330 for oxm_field in downstream_logical_flow.match.oxm_fields:
331 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400332 oxm_field.ofb_field.port = \
333 platform.intf_id_to_port_no(0, Port.ETHERNET_NNI)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400334
Shad Ansari47e0c392018-07-17 23:55:07 +0000335 classifier['udp_src'] = 67
336 classifier['udp_dst'] = 68
337 classifier['pkt_tag_type'] = 'double_tag'
338 action.pop('push_vlan', None)
339
340 flow_id = platform.mk_flow_id(intf_id, onu_id,
341 DHCP_DOWNLINK_FLOW_INDEX)
342
343 downstream_flow = openolt_pb2.Flow(
344 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
345 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400346 priority=logical_flow.priority, classifier=self.mk_classifier(
347 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000348 action=self.mk_action(action))
349
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400350 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
351
352 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000353 uplink_eapol_id=EAPOL_FLOW_INDEX,
354 downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
355 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000356
Shad Ansari2dda4f32018-05-17 07:16:07 +0000357
Shad Ansari2dda4f32018-05-17 07:16:07 +0000358
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400359 uplink_classifier = {}
360 uplink_classifier['eth_type'] = EAP_ETH_TYPE
Shad Ansari2dda4f32018-05-17 07:16:07 +0000361 uplink_classifier['pkt_tag_type'] = 'single_tag'
362 uplink_classifier['vlan_vid'] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400363
364 uplink_action = {}
Shad Ansari2dda4f32018-05-17 07:16:07 +0000365 uplink_action['trap_to_host'] = True
366
Nicolas Palpacuere9bf83c2018-08-16 14:53:14 -0400367 gemport_id = platform.mk_gemport_id(intf_id, onu_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400368
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400369 # Add Upstream EAPOL Flow.
370
371 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
372
Shad Ansari2dda4f32018-05-17 07:16:07 +0000373 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000374 onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400375 access_intf_id=intf_id, gemport_id=gemport_id,
376 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000377 classifier=self.mk_classifier(uplink_classifier),
378 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000379
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400380 logical_flow = copy.deepcopy(logical_flow)
381 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
382 vlan_id | 0x1000)]))
383 logical_flow.match.type = OFPMT_OXM
384
385 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000386
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400387 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000388
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400389 # Add Downstream EAPOL Flow, Only for first EAP flow
Shad Ansari2dda4f32018-05-17 07:16:07 +0000390
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400391 downlink_classifier = {}
392 downlink_classifier['pkt_tag_type'] = 'single_tag'
393 downlink_classifier['vlan_vid'] = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000394
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400395
Jonathan Hart5b435642018-08-20 08:50:05 -0700396
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400397 downlink_action = {}
398 downlink_action['push_vlan'] = True
399 downlink_action['vlan_vid'] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400400
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400401 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
402 downlink_eapol_id)
403
404 downstream_flow = openolt_pb2.Flow(
405 onu_id=onu_id, flow_id=downlink_flow_id, flow_type="downstream",
406 access_intf_id=intf_id, gemport_id=gemport_id,
407 priority=logical_flow.priority,
408 classifier=self.mk_classifier(downlink_classifier),
409 action=self.mk_action(downlink_action))
410
411 downstream_logical_flow = ofp_flow_stats(id=logical_flow.id,
412 cookie=logical_flow.cookie, table_id=logical_flow.table_id,
413 priority=logical_flow.priority, flags=logical_flow.flags)
414
415 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
416 fd.in_port(fd.get_out_port(logical_flow)),
417 fd.vlan_vid((4000 - onu_id) | 0x1000)]))
418 downstream_logical_flow.match.type = OFPMT_OXM
419
420 downstream_logical_flow.instructions.extend(
421 fd.mk_instructions_from_actions([fd.output(
422 platform.mk_uni_port_num(intf_id, onu_id))]))
423
424 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400425
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400426 def repush_all_different_flows(self):
427 # Check if the device is supposed to have flows, if so add them
428 # Recover static flows after a reboot
429 logical_flows = self.logical_flows_proxy.get('/').items
430 devices_flows = self.flows_proxy.get('/').items
431 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
432 for logical_flow in logical_flows:
433 try:
434 if logical_flow.id not in logical_flows_ids_provisioned:
435 self.add_flow(logical_flow)
436 except Exception as e:
437 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400438
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400439 def reset_flows(self):
440 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400441
Jonathan Hart5b435642018-08-20 08:50:05 -0700442 def add_lldp_flow(self, intf_id, onu_id, logical_flow, classifier, action):
443
444 self.log.debug('add lldp downstream trap', classifier=classifier,
445 action=action)
446
447 action.clear()
448 action['trap_to_host'] = True
449 classifier['pkt_tag_type'] = 'untagged'
450
451 gemport_id = platform.mk_gemport_id(onu_id)
452 flow_id = platform.mk_flow_id(intf_id, onu_id, LLDP_FLOW_INDEX)
453
454 downstream_flow = openolt_pb2.Flow(
455 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
456 access_intf_id=3, network_intf_id=0, gemport_id=gemport_id,
457 priority=logical_flow.priority,
458 classifier=self.mk_classifier(classifier),
459 action=self.mk_action(action))
460
461 self.log.debug('add lldp downstream trap', access_intf_id=intf_id,
462 onu_id=onu_id, flow_id=flow_id)
463 self.stub.FlowAdd(downstream_flow)
464
Shad Ansari2dda4f32018-05-17 07:16:07 +0000465 def mk_classifier(self, classifier_info):
466
467 classifier = openolt_pb2.Classifier()
468
469 if 'eth_type' in classifier_info:
470 classifier.eth_type = classifier_info['eth_type']
471 if 'ip_proto' in classifier_info:
472 classifier.ip_proto = classifier_info['ip_proto']
473 if 'vlan_vid' in classifier_info:
474 classifier.o_vid = classifier_info['vlan_vid']
475 if 'metadata' in classifier_info:
476 classifier.i_vid = classifier_info['metadata']
477 if 'vlan_pcp' in classifier_info:
478 classifier.o_pbits = classifier_info['vlan_pcp']
479 if 'udp_src' in classifier_info:
480 classifier.src_port = classifier_info['udp_src']
481 if 'udp_dst' in classifier_info:
482 classifier.dst_port = classifier_info['udp_dst']
483 if 'ipv4_dst' in classifier_info:
484 classifier.dst_ip = classifier_info['ipv4_dst']
485 if 'ipv4_src' in classifier_info:
486 classifier.src_ip = classifier_info['ipv4_src']
487 if 'pkt_tag_type' in classifier_info:
488 if classifier_info['pkt_tag_type'] == 'single_tag':
489 classifier.pkt_tag_type = 'single_tag'
490 elif classifier_info['pkt_tag_type'] == 'double_tag':
491 classifier.pkt_tag_type = 'double_tag'
492 elif classifier_info['pkt_tag_type'] == 'untagged':
493 classifier.pkt_tag_type = 'untagged'
494 else:
495 classifier.pkt_tag_type = 'none'
496
497 return classifier
498
499 def mk_action(self, action_info):
500 action = openolt_pb2.Action()
501
Shad Ansarif9d2d102018-06-13 02:15:26 +0000502 if 'pop_vlan' in action_info:
503 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000504 action.cmd.remove_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000505 elif 'push_vlan' in action_info:
506 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000507 action.cmd.add_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000508 elif 'trap_to_host' in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000509 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000510 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400511 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000512 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000513 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400514
515 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400516 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400517
518 for flow in flows:
519 eap_flow = False
520 eap_intf_id = None
521 eap_onu_id = None
522 for field in fd.get_ofb_fields(flow):
523 if field.type == fd.ETH_TYPE:
524 if field.eth_type == EAP_ETH_TYPE:
525 eap_flow = True
526 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000527 eap_intf_id = platform.intf_id_from_uni_port_num(
528 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400529 eap_onu_id = platform.onu_id_from_port_num(field.port)
530
531 if eap_flow:
532 self.log.debug('eap flow detected', onu_id=onu_id,
533 intf_id=intf_id, eap_intf_id=eap_intf_id,
534 eap_onu_id=eap_onu_id)
535 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400536 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400537
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400538 return (False, None)
539
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400540 def get_subscriber_vlan(self, port):
541 self.log.debug('looking from subscriber flow for port', port=port)
542
543 flows = self.logical_flows_proxy.get('/').items
544 for flow in flows:
545 in_port = fd.get_in_port(flow)
546 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400547
548 if in_port == port and \
549 platform.intf_id_to_port_type_name(out_port) == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400550 fields = fd.get_ofb_fields(flow)
551 self.log.debug('subscriber flow found', fields=fields)
552 for field in fields:
553 if field.type == OFPXMT_OFB_VLAN_VID:
554 self.log.debug('subscriber vlan found',
555 vlan_id=field.vlan_vid)
556 return field.vlan_vid & 0x0fff
557 self.log.debug('No subscriber flow found', port=port)
558 return None
559
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400560 def add_flow_to_device(self, flow, logical_flow):
561 self.log.debug('pushing flow to device', flow=flow)
562 self.stub.FlowAdd(flow)
563 self.register_flow(logical_flow, flow)
564
565 def register_flow(self, logical_flow, device_flow):
566 self.log.debug('registering flow in device',
567 logical_flow=logical_flow, device_flow=device_flow)
568 stored_flow = copy.deepcopy(logical_flow)
569 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
570 device_flow.flow_type)
571 self.log.debug('generated device flow id', id=stored_flow.id,
572 flow_id=device_flow.flow_id,
573 direction=device_flow.flow_type)
574 stored_flow.cookie = logical_flow.id
575 flows = self.flows_proxy.get('/')
576 flows.items.extend([stored_flow])
577 self.flows_proxy.update('/', flows)
578
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400579 def find_next_flow(self, flow):
580 table_id = fd.get_goto_table_id(flow)
581 metadata = 0
582 for field in fd.get_ofb_fields(flow):
583 if field.type == fd.METADATA:
584 metadata = field.table_metadata
585 if table_id is None:
586 return None
587 flows = self.logical_flows_proxy.get('/').items
588 next_flows = []
589 for f in flows:
590 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700591 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400592 if fd.get_in_port(f) == fd.get_in_port(flow) and \
593 fd.get_out_port(f) == metadata:
594
595 next_flows.append(f)
596
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400597 if len(next_flows) == 0:
598 self.log.warning('no next flow found, it may be a timing issue',
599 flow=flow, number_of_flows=len(flows))
600 reactor.callLater(5, self.add_flow, flow)
601 return None
602
Jonathan Hart5b435642018-08-20 08:50:05 -0700603 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400604
605 return next_flows[0]
606
607 def update_children_flows(self, device_rules_map):
608
609 for device_id, (flows, groups) in device_rules_map.iteritems():
610 if device_id != self.device_id:
611 self.root_proxy.update('/devices/{}/flows'.format(device_id),
612 Flows(items=flows.values()))
613 self.root_proxy.update('/devices/{}/flow_groups'.format(
614 device_id), FlowGroups(items=groups.values()))
615
616 def generate_stored_id(self, flow_id, direction):
617 if direction == 'upstream':
618 self.log.debug('upstream flow, shifting id')
619 return 0x1 << 15 | flow_id
620 elif direction == 'downstream':
621 self.log.debug('downstream flow, not shifting id')
622 return flow_id
623 else:
624 self.log.warn('Unrecognized direction', direction=direction)
625 return flow_id
626
627 def decode_stored_id(self, id):
628 if id >> 15 == 0x1:
629 return (id & 0x7fff, 'upstream')
630 else:
Jonathan Hart5b435642018-08-20 08:50:05 -0700631 return (id, 'downstream')