blob: 063101b03e3f851e753e6a655e8505c168e79605 [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#
16
Shad Ansari2dda4f32018-05-17 07:16:07 +000017from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC
18import voltha.core.flow_decomposer as fd
19import openolt_platform as platform
20from voltha.adapters.openolt.protos import openolt_pb2
21
Shad Ansarif9d2d102018-06-13 02:15:26 +000022HSIA_FLOW_INDEX = 0 # FIXME
23DHCP_FLOW_INDEX = 1 # FIXME
24EAPOL_FLOW_INDEX = 2 # FIXME
25EAPOL_DOWNLINK_FLOW_INDEX = 3 # FIXME
Shad Ansari2dda4f32018-05-17 07:16:07 +000026
27# FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py
28DEFAULT_MGMT_VLAN = 4091
29
Shad Ansarif9d2d102018-06-13 02:15:26 +000030
Shad Ansari2dda4f32018-05-17 07:16:07 +000031class OpenOltFlowMgr(object):
32
33 def __init__(self, log, stub):
34 self.log = log
35 self.stub = stub
36
37 def add_flow(self, flow, is_down_stream):
Shad Ansarie048aaa2018-05-18 18:27:21 +000038 self.log.debug('add flow', flow=flow, is_down_stream=is_down_stream)
Shad Ansari2dda4f32018-05-17 07:16:07 +000039 classifier_info = dict()
40 action_info = dict()
41
42 in_port = fd.get_in_port(flow)
43 assert in_port is not None
44
45 for field in fd.get_ofb_fields(flow):
46 if field.type == fd.ETH_TYPE:
47 classifier_info['eth_type'] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +000048 self.log.debug('field-type-eth-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +000049 eth_type=classifier_info['eth_type'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000050 elif field.type == fd.IP_PROTO:
51 classifier_info['ip_proto'] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +000052 self.log.debug('field-type-ip-proto',
Shad Ansarif9d2d102018-06-13 02:15:26 +000053 ip_proto=classifier_info['ip_proto'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000054 elif field.type == fd.IN_PORT:
55 classifier_info['in_port'] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +000056 self.log.debug('field-type-in-port',
Shad Ansarif9d2d102018-06-13 02:15:26 +000057 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000058 elif field.type == fd.VLAN_VID:
59 classifier_info['vlan_vid'] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +000060 self.log.debug('field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +000061 vlan=classifier_info['vlan_vid'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000062 elif field.type == fd.VLAN_PCP:
63 classifier_info['vlan_pcp'] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +000064 self.log.debug('field-type-vlan-pcp',
Shad Ansarif9d2d102018-06-13 02:15:26 +000065 pcp=classifier_info['vlan_pcp'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000066 elif field.type == fd.UDP_DST:
67 classifier_info['udp_dst'] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000068 self.log.debug('field-type-udp-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000069 udp_dst=classifier_info['udp_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000070 elif field.type == fd.UDP_SRC:
71 classifier_info['udp_src'] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000072 self.log.debug('field-type-udp-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +000073 udp_src=classifier_info['udp_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000074 elif field.type == fd.IPV4_DST:
75 classifier_info['ipv4_dst'] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +000076 self.log.debug('field-type-ipv4-dst',
Shad Ansarif9d2d102018-06-13 02:15:26 +000077 ipv4_dst=classifier_info['ipv4_dst'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000078 elif field.type == fd.IPV4_SRC:
79 classifier_info['ipv4_src'] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +000080 self.log.debug('field-type-ipv4-src',
Shad Ansarif9d2d102018-06-13 02:15:26 +000081 ipv4_dst=classifier_info['ipv4_src'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000082 elif field.type == fd.METADATA:
83 classifier_info['metadata'] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +000084 self.log.debug('field-type-metadata',
Shad Ansarif9d2d102018-06-13 02:15:26 +000085 metadata=classifier_info['metadata'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000086 else:
87 raise NotImplementedError('field.type={}'.format(
88 field.type))
89
90 for action in fd.get_actions(flow):
91 if action.type == fd.OUTPUT:
92 action_info['output'] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +000093 self.log.debug('action-type-output',
Shad Ansarif9d2d102018-06-13 02:15:26 +000094 output=action_info['output'],
95 in_port=classifier_info['in_port'])
Shad Ansari2dda4f32018-05-17 07:16:07 +000096 elif action.type == fd.POP_VLAN:
97 action_info['pop_vlan'] = True
Shad Ansarie048aaa2018-05-18 18:27:21 +000098 self.log.debug('action-type-pop-vlan', in_port=in_port)
Shad Ansari2dda4f32018-05-17 07:16:07 +000099 elif action.type == fd.PUSH_VLAN:
100 action_info['push_vlan'] = True
101 action_info['tpid'] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000102 self.log.debug('action-type-push-vlan',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000103 push_tpid=action_info['tpid'], in_port=in_port)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000104 if action.push.ethertype != 0x8100:
105 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000106 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000107 elif action.type == fd.SET_FIELD:
108 # action_info['action_type'] = 'set_field'
109 _field = action.set_field.field.ofb_field
110 assert (action.set_field.field.oxm_class ==
111 OFPXMC_OPENFLOW_BASIC)
Shad Ansarie048aaa2018-05-18 18:27:21 +0000112 self.log.debug('action-type-set-field',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000113 field=_field, in_port=in_port)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000114 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000115 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000116 vlan_vid=_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000117 action_info['vlan_vid'] = (_field.vlan_vid & 0xfff)
118 else:
119 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000120 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000121 else:
122 self.log.error('unsupported-action-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000123 action_type=action.type, in_port=in_port)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000124
125 # FIXME - Why ignore downstream flows?
126 if is_down_stream is False:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000127 intf_id = platform.intf_id_from_uni_port_num(
128 classifier_info['in_port'])
129 onu_id = platform.onu_id_from_port_num(
130 classifier_info['in_port'])
131 self.divide_and_add_flow(intf_id, onu_id, classifier_info,
132 action_info)
133 # else:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000134 # self.log.info('ignore downstream flow', flow=flow,
135 # classifier_info=classifier_info,
136 # action_info=action_info)
137
138 # FIXME - No need for divide_and_add_flow if
139 # both upstream and downstream flows
140 # are acted upon (not just upstream flows).
141 def divide_and_add_flow(self, intf_id, onu_id, classifier, action):
142 if 'ip_proto' in classifier:
143 if classifier['ip_proto'] == 17:
144 self.log.debug('dhcp flow add')
145 self.add_dhcp_trap(intf_id, onu_id, classifier, action)
146 elif classifier['ip_proto'] == 2:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000147 self.log.debug('igmp flow add ignored')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000148 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000149 self.log.debug("Invalid-Classifier-to-handle",
150 classifier=classifier,
151 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000152 elif 'eth_type' in classifier:
153 if classifier['eth_type'] == 0x888e:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000154 self.log.debug('eapol flow add')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000155 self.add_eapol_flow(intf_id, onu_id, classifier, action)
156 elif 'push_vlan' in action:
157 self.add_data_flow(intf_id, onu_id, classifier, action)
158 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000159 self.log.debug('Invalid-flow-type-to-handle',
160 classifier=classifier,
161 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000162
163 def add_data_flow(self, intf_id, onu_id, uplink_classifier, uplink_action):
164
165 downlink_classifier = dict(uplink_classifier)
166 downlink_action = dict(uplink_action)
167
168 uplink_classifier['pkt_tag_type'] = 'single_tag'
169
170 downlink_classifier['pkt_tag_type'] = 'double_tag'
171 downlink_classifier['vlan_vid'] = uplink_action['vlan_vid']
172 downlink_classifier['metadata'] = uplink_classifier['vlan_vid']
173 del downlink_action['push_vlan']
174 downlink_action['pop_vlan'] = True
175
176 # To-Do right now only one GEM port is supported, so below method
177 # will take care of handling all the p bits.
178 # We need to revisit when mulitple gem port per p bits is needed.
179 self.add_hsia_flow(intf_id, onu_id, uplink_classifier, uplink_action,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000180 downlink_classifier, downlink_action,
181 HSIA_FLOW_INDEX)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000182
183 def add_hsia_flow(self, intf_id, onu_id, uplink_classifier, uplink_action,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000184 downlink_classifier, downlink_action, hsia_id):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000185
186 gemport_id = platform.mk_gemport_id(onu_id)
187 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
188
Shad Ansarif9d2d102018-06-13 02:15:26 +0000189 self.log.debug('add upstream flow', onu_id=onu_id,
190 classifier=uplink_classifier, action=uplink_action,
191 gemport_id=gemport_id, flow_id=flow_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000192
193 flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000194 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
195 access_intf_id=intf_id, gemport_id=gemport_id,
196 classifier=self.mk_classifier(uplink_classifier),
197 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000198
199 self.stub.FlowAdd(flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000200
Shad Ansarie048aaa2018-05-18 18:27:21 +0000201 self.log.debug('add downstream flow', classifier=downlink_classifier,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000202 action=downlink_action, gemport_id=gemport_id,
203 flow_id=flow_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000204
205 flow = openolt_pb2.Flow(
206 onu_id=onu_id, flow_id=flow_id, flow_type="downstream",
207 access_intf_id=intf_id, gemport_id=gemport_id,
208 classifier=self.mk_classifier(downlink_classifier),
209 action=self.mk_action(downlink_action))
210
211 self.stub.FlowAdd(flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000212
213 def add_dhcp_trap(self, intf_id, onu_id, classifier, action):
214
Shad Ansarie048aaa2018-05-18 18:27:21 +0000215 self.log.debug('add dhcp trap', classifier=classifier, action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000216
217 action.clear()
218 action['trap_to_host'] = True
219 classifier['pkt_tag_type'] = 'single_tag'
220 classifier.pop('vlan_vid', None)
221
222 gemport_id = platform.mk_gemport_id(onu_id)
223 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
224
225 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000226 onu_id=onu_id, flow_id=flow_id, flow_type="upstream",
227 access_intf_id=intf_id, gemport_id=gemport_id,
228 classifier=self.mk_classifier(classifier),
229 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000230
231 self.stub.FlowAdd(upstream_flow)
232
233 def add_eapol_flow(self, intf_id, onu_id, uplink_classifier, uplink_action,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000234 uplink_eapol_id=EAPOL_FLOW_INDEX,
235 downlink_eapol_id=EAPOL_DOWNLINK_FLOW_INDEX,
236 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000237
Shad Ansarif9d2d102018-06-13 02:15:26 +0000238 self.log.debug('add eapol flow', classifier=uplink_classifier,
239 action=uplink_action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000240
241 downlink_classifier = dict(uplink_classifier)
242 downlink_action = dict(uplink_action)
243
244 gemport_id = platform.mk_gemport_id(onu_id)
245 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, uplink_eapol_id)
246
247 # Add Upstream EAPOL Flow.
248 uplink_classifier['pkt_tag_type'] = 'single_tag'
249 uplink_classifier['vlan_vid'] = vlan_id
250 uplink_action.clear()
251 uplink_action['trap_to_host'] = True
252
253 upstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000254 onu_id=onu_id, flow_id=uplink_flow_id, flow_type="upstream",
255 access_intf_id=intf_id, gemport_id=gemport_id,
256 classifier=self.mk_classifier(uplink_classifier),
257 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000258
259 self.stub.FlowAdd(upstream_flow)
260
261 # Add Downstream EAPOL Flow.
Shad Ansarif9d2d102018-06-13 02:15:26 +0000262 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
263 downlink_eapol_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000264 downlink_classifier['pkt_tag_type'] = 'single_tag'
265 downlink_classifier['vlan_vid'] = vlan_id
266
267 downstream_flow = openolt_pb2.Flow(
Shad Ansarif9d2d102018-06-13 02:15:26 +0000268 onu_id=onu_id, flow_id=downlink_flow_id, flow_type="downstream",
269 access_intf_id=intf_id, gemport_id=gemport_id,
270 classifier=self.mk_classifier(downlink_classifier),
271 action=self.mk_action(downlink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000272
273 self.stub.FlowAdd(downstream_flow)
274
275 def mk_classifier(self, classifier_info):
276
277 classifier = openolt_pb2.Classifier()
278
279 if 'eth_type' in classifier_info:
280 classifier.eth_type = classifier_info['eth_type']
281 if 'ip_proto' in classifier_info:
282 classifier.ip_proto = classifier_info['ip_proto']
283 if 'vlan_vid' in classifier_info:
284 classifier.o_vid = classifier_info['vlan_vid']
285 if 'metadata' in classifier_info:
286 classifier.i_vid = classifier_info['metadata']
287 if 'vlan_pcp' in classifier_info:
288 classifier.o_pbits = classifier_info['vlan_pcp']
289 if 'udp_src' in classifier_info:
290 classifier.src_port = classifier_info['udp_src']
291 if 'udp_dst' in classifier_info:
292 classifier.dst_port = classifier_info['udp_dst']
293 if 'ipv4_dst' in classifier_info:
294 classifier.dst_ip = classifier_info['ipv4_dst']
295 if 'ipv4_src' in classifier_info:
296 classifier.src_ip = classifier_info['ipv4_src']
297 if 'pkt_tag_type' in classifier_info:
298 if classifier_info['pkt_tag_type'] == 'single_tag':
299 classifier.pkt_tag_type = 'single_tag'
300 elif classifier_info['pkt_tag_type'] == 'double_tag':
301 classifier.pkt_tag_type = 'double_tag'
302 elif classifier_info['pkt_tag_type'] == 'untagged':
303 classifier.pkt_tag_type = 'untagged'
304 else:
305 classifier.pkt_tag_type = 'none'
306
307 return classifier
308
309 def mk_action(self, action_info):
310 action = openolt_pb2.Action()
311
Shad Ansarif9d2d102018-06-13 02:15:26 +0000312 if 'pop_vlan' in action_info:
313 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000314 action.cmd.remove_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000315 elif 'push_vlan' in action_info:
316 action.o_vid = action_info['vlan_vid']
Shad Ansari2dda4f32018-05-17 07:16:07 +0000317 action.cmd.add_outer_tag = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000318 elif 'trap_to_host' in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000319 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000320 else:
321 self.log.info('Invalid-action-field')
322 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000323 return action