blob: d09806570689d999a02930585593f959b80ece98 [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
Shad Ansari2dda4f32018-05-17 07:16:07 +000025from 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
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040032EAPOL_SECONDARY_FLOW_INDEX = 5 # FIXME
Nicolas Palpacuer636ac762018-09-19 11:19:10 -040033DOWNSTREAM_FLOW_FOR_PACKET_OUT = 7
Shad Ansarifd0111d2018-09-13 21:33:06 +000034LLDP_FLOW_ID = 0x3FF8 # FIXME (16376)
Nicolas Palpacuer61815162018-06-20 18:12:04 -040035
36EAP_ETH_TYPE = 0x888e
Jonathan Hart5b435642018-08-20 08:50:05 -070037LLDP_ETH_TYPE = 0x88cc
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
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040043# Openolt Flow
44UPSTREAM = 'upstream'
45DOWNSTREAM = 'downstream'
46PACKET_TAG_TYPE = 'pkt_tag_type'
47UNTAGGED = 'untagged'
48SINGLE_TAG = 'single_tag'
49DOUBLE_TAG = 'double_tag'
50
51# Classifier
52ETH_TYPE = 'eth_type'
53TPID = 'tpid'
54IP_PROTO = 'ip_proto'
55IN_PORT = 'in_port'
56VLAN_VID = 'vlan_vid'
57VLAN_PCP = 'vlan_pcp'
58UDP_DST = 'udp_dst'
59UDP_SRC = 'udp_src'
60IPV4_DST = 'ipv4_dst'
61IPV4_SRC = 'ipv4_src'
62METADATA = 'metadata'
63OUTPUT = 'output'
64# Action
65POP_VLAN = 'pop_vlan'
66PUSH_VLAN = 'push_vlan'
67TRAP_TO_HOST = 'trap_to_host'
68
69
70
71
Shad Ansari2dda4f32018-05-17 07:16:07 +000072class OpenOltFlowMgr(object):
73
Shad Ansaricd20a6d2018-10-02 14:36:33 +000074 def __init__(self, log, stub, device_id, logical_device_id,
Girish Gowdru1e77ea02018-09-24 09:10:35 -070075 platform, resource_mgr):
Shad Ansari2dda4f32018-05-17 07:16:07 +000076 self.log = log
77 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040078 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040079 self.logical_device_id = logical_device_id
Shad Ansaricd20a6d2018-10-02 14:36:33 +000080 self.platform = platform
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040081 self.logical_flows_proxy = registry('core').get_proxy(
82 '/logical_devices/{}/flows'.format(self.logical_device_id))
83 self.flows_proxy = registry('core').get_proxy(
Nicolas Palpacuer61815162018-06-20 18:12:04 -040084 '/devices/{}/flows'.format(self.device_id))
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040085 self.root_proxy = registry('core').get_proxy('/')
Girish Gowdru1e77ea02018-09-24 09:10:35 -070086 self.resource_mgr = resource_mgr
Shad Ansari2dda4f32018-05-17 07:16:07 +000087
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040088 def add_flow(self, flow):
89 self.log.debug('add flow', flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +000090 classifier_info = dict()
91 action_info = dict()
92
Shad Ansari2dda4f32018-05-17 07:16:07 +000093 for field in fd.get_ofb_fields(flow):
94 if field.type == fd.ETH_TYPE:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040095 classifier_info[ETH_TYPE] = field.eth_type
Shad Ansarie048aaa2018-05-18 18:27:21 +000096 self.log.debug('field-type-eth-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040097 eth_type=classifier_info[ETH_TYPE])
Shad Ansari2dda4f32018-05-17 07:16:07 +000098 elif field.type == fd.IP_PROTO:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040099 classifier_info[IP_PROTO] = field.ip_proto
Shad Ansarie048aaa2018-05-18 18:27:21 +0000100 self.log.debug('field-type-ip-proto',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400101 ip_proto=classifier_info[IP_PROTO])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000102 elif field.type == fd.IN_PORT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400103 classifier_info[IN_PORT] = field.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000104 self.log.debug('field-type-in-port',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400105 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000106 elif field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400107 classifier_info[VLAN_VID] = field.vlan_vid & 0xfff
Shad Ansarie048aaa2018-05-18 18:27:21 +0000108 self.log.debug('field-type-vlan-vid',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400109 vlan=classifier_info[VLAN_VID])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000110 elif field.type == fd.VLAN_PCP:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400111 classifier_info[VLAN_PCP] = field.vlan_pcp
Shad Ansarie048aaa2018-05-18 18:27:21 +0000112 self.log.debug('field-type-vlan-pcp',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400113 pcp=classifier_info[VLAN_PCP])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000114 elif field.type == fd.UDP_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400115 classifier_info[UDP_DST] = field.udp_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000116 self.log.debug('field-type-udp-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400117 udp_dst=classifier_info[UDP_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000118 elif field.type == fd.UDP_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400119 classifier_info[UDP_SRC] = field.udp_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000120 self.log.debug('field-type-udp-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400121 udp_src=classifier_info[UDP_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000122 elif field.type == fd.IPV4_DST:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400123 classifier_info[IPV4_DST] = field.ipv4_dst
Shad Ansarie048aaa2018-05-18 18:27:21 +0000124 self.log.debug('field-type-ipv4-dst',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400125 ipv4_dst=classifier_info[IPV4_DST])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000126 elif field.type == fd.IPV4_SRC:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400127 classifier_info[IPV4_SRC] = field.ipv4_src
Shad Ansarie048aaa2018-05-18 18:27:21 +0000128 self.log.debug('field-type-ipv4-src',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400129 ipv4_dst=classifier_info[IPV4_SRC])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000130 elif field.type == fd.METADATA:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400131 classifier_info[METADATA] = field.table_metadata
Shad Ansarie048aaa2018-05-18 18:27:21 +0000132 self.log.debug('field-type-metadata',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400133 metadata=classifier_info[METADATA])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000134 else:
135 raise NotImplementedError('field.type={}'.format(
136 field.type))
137
138 for action in fd.get_actions(flow):
139 if action.type == fd.OUTPUT:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400140 action_info[OUTPUT] = action.output.port
Shad Ansarie048aaa2018-05-18 18:27:21 +0000141 self.log.debug('action-type-output',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400142 output=action_info[OUTPUT],
143 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000144 elif action.type == fd.POP_VLAN:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400145 if fd.get_goto_table_id(flow) is None:
146 self.log.debug('being taken care of by ONU', flow=flow)
147 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400148 action_info[POP_VLAN] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700149 self.log.debug('action-type-pop-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400150 in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000151 elif action.type == fd.PUSH_VLAN:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400152 action_info[PUSH_VLAN] = True
153 action_info[TPID] = action.push.ethertype
Shad Ansarie048aaa2018-05-18 18:27:21 +0000154 self.log.debug('action-type-push-vlan',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400155 push_tpid=action_info[TPID], in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000156 if action.push.ethertype != 0x8100:
157 self.log.error('unhandled-tpid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000158 ethertype=action.push.ethertype)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000159 elif action.type == fd.SET_FIELD:
160 # action_info['action_type'] = 'set_field'
161 _field = action.set_field.field.ofb_field
162 assert (action.set_field.field.oxm_class ==
163 OFPXMC_OPENFLOW_BASIC)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400164 self.log.debug('action-type-set-field',
165 field=_field, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000166 if _field.type == fd.VLAN_VID:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000167 self.log.debug('set-field-type-vlan-vid',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000168 vlan_vid=_field.vlan_vid & 0xfff)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400169 action_info[VLAN_VID] = (_field.vlan_vid & 0xfff)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000170 else:
171 self.log.error('unsupported-action-set-field-type',
Shad Ansarif9d2d102018-06-13 02:15:26 +0000172 field_type=_field.type)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000173 else:
174 self.log.error('unsupported-action-type',
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400175 action_type=action.type, in_port=classifier_info[IN_PORT])
Shad Ansari2dda4f32018-05-17 07:16:07 +0000176
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400177 if fd.get_goto_table_id(flow) is not None and not POP_VLAN in \
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400178 action_info:
179 self.log.debug('being taken care of by ONU', flow=flow)
Nicolas Palpacuer856d3af2018-09-12 15:04:51 -0400180 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000181
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400182 if not OUTPUT in action_info and METADATA in classifier_info:
183 #find flow in the next table
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400184 next_flow = self.find_next_flow(flow)
185 if next_flow is None:
186 return
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400187 action_info[OUTPUT] = fd.get_out_port(next_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400188 for field in fd.get_ofb_fields(next_flow):
189 if field.type == fd.VLAN_VID:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400190 classifier_info[METADATA] = field.vlan_vid & 0xfff
191
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400192
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000193 (intf_id, onu_id) = self.platform.extract_access_from_flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400194 classifier_info[IN_PORT], action_info[OUTPUT])
195
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400196
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400197 self.divide_and_add_flow(intf_id, onu_id, classifier_info,
198 action_info, flow)
199
200 def remove_flow(self, flow):
201 self.log.debug('trying to remove flows from logical flow :',
Jonathan Hart5b435642018-08-20 08:50:05 -0700202 logical_flow=flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400203 device_flows_to_remove = []
204 device_flows = self.flows_proxy.get('/').items
205 for f in device_flows:
206 if f.cookie == flow.id:
207 device_flows_to_remove.append(f)
208
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400209 for f in device_flows_to_remove:
210 (id, direction) = self.decode_stored_id(f.id)
211 flow_to_remove = openolt_pb2.Flow(flow_id=id, flow_type=direction)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400212 try:
213 self.stub.FlowRemove(flow_to_remove)
214 except grpc.RpcError as grpc_e:
215 if grpc_e.code() == grpc.StatusCode.NOT_FOUND:
216 self.log.debug('This flow does not exist on the switch, '
Jonathan Hart5b435642018-08-20 08:50:05 -0700217 'normal after an OLT reboot',
218 flow=flow_to_remove)
Nicolas Palpacuer3d0878d2018-08-17 11:29:42 -0400219 else:
220 raise grpc_e
221
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400222 self.log.debug('flow removed from device', flow=f,
223 flow_key=flow_to_remove)
224
225 if len(device_flows_to_remove) > 0:
226 new_flows = []
227 flows_ids_to_remove = [f.id for f in device_flows_to_remove]
228 for f in device_flows:
229 if f.id not in flows_ids_to_remove:
230 new_flows.append(f)
231
232 self.flows_proxy.update('/', Flows(items=new_flows))
233 self.log.debug('flows removed from the data store',
234 flow_ids_removed=flows_ids_to_remove,
235 number_of_flows_removed=(len(device_flows) - len(
236 new_flows)), expected_flows_removed=len(
237 device_flows_to_remove))
238 else:
239 self.log.debug('no device flow to remove for this flow (normal '
240 'for multi table flows)', flow=flow)
241
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400242 def divide_and_add_flow(self, intf_id, onu_id, classifier,
243 action, flow):
244
245 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id,
246 classifier=classifier, action=action)
247
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400248 if IP_PROTO in classifier:
249 if classifier[IP_PROTO] == 17:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000250 self.log.debug('dhcp flow add')
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700251 self.add_dhcp_trap(intf_id, onu_id, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400252 action, flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400253 elif classifier[IP_PROTO] == 2:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400254 self.log.warn('igmp flow add ignored, not implemented yet')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000255 else:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400256 self.log.warn("Invalid-Classifier-to-handle",
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400257 classifier=classifier,
258 action=action)
259 elif ETH_TYPE in classifier:
260 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000261 self.log.debug('eapol flow add')
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700262 self.add_eapol_flow(intf_id, onu_id, flow)
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400263 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
264 if vlan_id is not None:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700265 self.add_eapol_flow(
Jonathan Hart5b435642018-08-20 08:50:05 -0700266 intf_id, onu_id, flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400267 eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400268 vlan_id=vlan_id)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400269 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
Jonathan Hart5b435642018-08-20 08:50:05 -0700270 self.log.debug('lldp flow add')
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700271 self.add_lldp_flow(flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400272
273 elif PUSH_VLAN in action:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700274 self.add_upstream_data_flow(intf_id, onu_id, classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400275 action,
Girish Gowdru141ced82018-09-17 20:19:14 -0700276 flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400277 elif POP_VLAN in action:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700278 self.add_downstream_data_flow(intf_id, onu_id, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400279 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000280 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000281 self.log.debug('Invalid-flow-type-to-handle',
282 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400283 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000284
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400285 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
Jonathan Hart5b435642018-08-20 08:50:05 -0700286 uplink_action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000287
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400288
289 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
290
Shad Ansari2dda4f32018-05-17 07:16:07 +0000291
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700292 self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400293 uplink_action, UPSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400294 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000295
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400296 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400297 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400298 if eap_active:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700299 self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400300 eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
301 vlan_id=uplink_classifier[VLAN_VID])
302
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400303
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400304 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
305 downlink_action, flow):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400306 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400307 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400308 downlink_action[POP_VLAN] = True
309 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400310
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700311 self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400312 downlink_action, DOWNSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400313 flow)
314
315 # To-Do right now only one GEM port is supported, so below method
316 # will take care of handling all the p bits.
317 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400318 # Waiting for Technology profile
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400319 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
320 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000321
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700322 pon_intf_onu_id = (intf_id, onu_id)
323 gemport_id = self.resource_mgr.get_gemport_id(
324 pon_intf_onu_id=pon_intf_onu_id
325 )
326 alloc_id = self.resource_mgr.get_alloc_id(
327 pon_intf_onu_id=pon_intf_onu_id
328 )
329
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000330 flow_id = self.platform.mk_flow_id(intf_id, onu_id, hsia_id)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000331
Shad Ansari2dda4f32018-05-17 07:16:07 +0000332 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400333 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000334 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700335 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400336 priority=logical_flow.priority,
337 classifier=self.mk_classifier(classifier),
338 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000339
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400340 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000341
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400342 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000343
Shad Ansari47e0c392018-07-17 23:55:07 +0000344 self.log.debug('add dhcp upstream trap', classifier=classifier,
Shad Ansari9712dc92018-09-26 17:46:00 +0000345 intf_id=intf_id, onu_id=onu_id, action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000346
347 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400348 action[TRAP_TO_HOST] = True
349 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
350 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000351
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700352 pon_intf_onu_id = (intf_id, onu_id)
353 gemport_id = self.resource_mgr.get_gemport_id(
354 pon_intf_onu_id=pon_intf_onu_id
355 )
356 alloc_id = self.resource_mgr.get_alloc_id(
357 pon_intf_onu_id=pon_intf_onu_id
358 )
359
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000360 flow_id = self.platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000361
362 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400363 onu_id=onu_id, flow_id=flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400364 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700365 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400366 priority=logical_flow.priority,
367 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000368 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000369
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400370 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000371
Shad Ansari47e0c392018-07-17 23:55:07 +0000372 # FIXME - ONOS should send explicit upstream and downstream
373 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400374
375 downstream_logical_flow = copy.deepcopy(logical_flow)
376 for oxm_field in downstream_logical_flow.match.oxm_fields:
377 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400378 oxm_field.ofb_field.port = \
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000379 self.platform.intf_id_to_port_no(0, Port.ETHERNET_NNI)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400380
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400381 classifier[UDP_SRC] = 67
382 classifier[UDP_DST] = 68
383 classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
384 action.pop(PUSH_VLAN, None)
Shad Ansari47e0c392018-07-17 23:55:07 +0000385
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000386 flow_id = self.platform.mk_flow_id(intf_id, onu_id,
387 DHCP_DOWNLINK_FLOW_INDEX)
Shad Ansari47e0c392018-07-17 23:55:07 +0000388
389 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400390 onu_id=onu_id, flow_id=flow_id, flow_type=DOWNSTREAM,
Shad Ansari47e0c392018-07-17 23:55:07 +0000391 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400392 priority=logical_flow.priority, classifier=self.mk_classifier(
393 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000394 action=self.mk_action(action))
395
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400396 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
397
398 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400399 eapol_id=EAPOL_FLOW_INDEX,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000400 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000401
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400402 uplink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400403 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
404 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
405 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400406
407 uplink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400408 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000409
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400410 # Add Upstream EAPOL Flow.
411
Girish Gowdru1e77ea02018-09-24 09:10:35 -0700412 pon_intf_onu_id = (intf_id, onu_id)
413 gemport_id = self.resource_mgr.get_gemport_id(
414 pon_intf_onu_id=pon_intf_onu_id
415 )
416 alloc_id = self.resource_mgr.get_alloc_id(
417 pon_intf_onu_id=pon_intf_onu_id
418 )
419
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000420 uplink_flow_id = self.platform.mk_flow_id(intf_id, onu_id, eapol_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400421
Shad Ansari2dda4f32018-05-17 07:16:07 +0000422 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400423 onu_id=onu_id, flow_id=uplink_flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400424 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700425 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400426 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000427 classifier=self.mk_classifier(uplink_classifier),
428 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000429
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400430 logical_flow = copy.deepcopy(logical_flow)
431 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
432 vlan_id | 0x1000)]))
433 logical_flow.match.type = OFPMT_OXM
434
435 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000436
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400437 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000438
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400439 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
440 # requirement)
441 special_vlan_downstream_flow = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000442
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400443 downlink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400444 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
445 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400446
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400447 downlink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400448 downlink_action[PUSH_VLAN] = True
449 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400450
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000451 downlink_flow_id = self.platform.mk_flow_id(
452 intf_id, onu_id, DOWNSTREAM_FLOW_FOR_PACKET_OUT)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400453
454 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400455 onu_id=onu_id, flow_id=downlink_flow_id, flow_type=DOWNSTREAM,
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400456 access_intf_id=intf_id, gemport_id=gemport_id,
457 priority=logical_flow.priority,
458 classifier=self.mk_classifier(downlink_classifier),
459 action=self.mk_action(downlink_action))
460
Shad Ansarifd0111d2018-09-13 21:33:06 +0000461 downstream_logical_flow = ofp_flow_stats(
462 id=logical_flow.id, cookie=logical_flow.cookie,
463 table_id=logical_flow.table_id, priority=logical_flow.priority,
464 flags=logical_flow.flags)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400465
466 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
467 fd.in_port(fd.get_out_port(logical_flow)),
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400468 fd.vlan_vid((special_vlan_downstream_flow) | 0x1000)]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400469 downstream_logical_flow.match.type = OFPMT_OXM
470
471 downstream_logical_flow.instructions.extend(
472 fd.mk_instructions_from_actions([fd.output(
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000473 self.platform.mk_uni_port_num(intf_id, onu_id))]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400474
475 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400476
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400477 def repush_all_different_flows(self):
478 # Check if the device is supposed to have flows, if so add them
479 # Recover static flows after a reboot
480 logical_flows = self.logical_flows_proxy.get('/').items
481 devices_flows = self.flows_proxy.get('/').items
482 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
483 for logical_flow in logical_flows:
484 try:
485 if logical_flow.id not in logical_flows_ids_provisioned:
486 self.add_flow(logical_flow)
487 except Exception as e:
488 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400489
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400490 def reset_flows(self):
491 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400492
Shad Ansarifd0111d2018-09-13 21:33:06 +0000493
494 """ Add a downstream LLDP trap flow on the NNI interface
495 """
Shad Ansarifd0111d2018-09-13 21:33:06 +0000496 def add_lldp_flow(self, logical_flow, network_intf_id=0):
Jonathan Hart5b435642018-08-20 08:50:05 -0700497
Shad Ansarifd0111d2018-09-13 21:33:06 +0000498 classifier = {}
499 classifier[ETH_TYPE] = LLDP_ETH_TYPE
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400500 classifier[PACKET_TAG_TYPE] = UNTAGGED
Shad Ansarifd0111d2018-09-13 21:33:06 +0000501 action = {}
502 action[TRAP_TO_HOST] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700503
Shad Ansarifd0111d2018-09-13 21:33:06 +0000504 flow_id = LLDP_FLOW_ID # FIXME
Jonathan Hart5b435642018-08-20 08:50:05 -0700505
506 downstream_flow = openolt_pb2.Flow(
Shad Ansarifd0111d2018-09-13 21:33:06 +0000507 onu_id=-1, # onu_id not required
508 gemport_id=-1, # gemport_id not required
509 access_intf_id=-1, # access_intf_id not required
510 flow_id=flow_id,
511 flow_type=DOWNSTREAM,
Jonathan Hart5b435642018-08-20 08:50:05 -0700512 priority=logical_flow.priority,
Shad Ansarifd0111d2018-09-13 21:33:06 +0000513 network_intf_id=network_intf_id,
Jonathan Hart5b435642018-08-20 08:50:05 -0700514 classifier=self.mk_classifier(classifier),
515 action=self.mk_action(action))
516
Shad Ansarifd0111d2018-09-13 21:33:06 +0000517 self.log.debug('add lldp downstream trap', classifier=classifier,
518 action=action, flow=downstream_flow)
519 self.add_flow_to_device(downstream_flow, logical_flow)
Jonathan Hart5b435642018-08-20 08:50:05 -0700520
Shad Ansari2dda4f32018-05-17 07:16:07 +0000521 def mk_classifier(self, classifier_info):
522
523 classifier = openolt_pb2.Classifier()
524
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400525 if ETH_TYPE in classifier_info:
526 classifier.eth_type = classifier_info[ETH_TYPE]
527 if IP_PROTO in classifier_info:
528 classifier.ip_proto = classifier_info[IP_PROTO]
529 if VLAN_VID in classifier_info:
530 classifier.o_vid = classifier_info[VLAN_VID]
531 if METADATA in classifier_info:
532 classifier.i_vid = classifier_info[METADATA]
533 if VLAN_PCP in classifier_info:
534 classifier.o_pbits = classifier_info[VLAN_PCP]
535 if UDP_SRC in classifier_info:
536 classifier.src_port = classifier_info[UDP_SRC]
537 if UDP_DST in classifier_info:
538 classifier.dst_port = classifier_info[UDP_DST]
539 if IPV4_DST in classifier_info:
540 classifier.dst_ip = classifier_info[IPV4_DST]
541 if IPV4_SRC in classifier_info:
542 classifier.src_ip = classifier_info[IPV4_SRC]
543 if PACKET_TAG_TYPE in classifier_info:
544 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
545 classifier.pkt_tag_type = SINGLE_TAG
546 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
547 classifier.pkt_tag_type = DOUBLE_TAG
548 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
549 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000550 else:
551 classifier.pkt_tag_type = 'none'
552
553 return classifier
554
555 def mk_action(self, action_info):
556 action = openolt_pb2.Action()
557
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400558 if POP_VLAN in action_info:
559 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000560 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400561 elif PUSH_VLAN in action_info:
562 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000563 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400564 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000565 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000566 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400567 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000568 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000569 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400570
571 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400572 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400573
574 for flow in flows:
575 eap_flow = False
576 eap_intf_id = None
577 eap_onu_id = None
578 for field in fd.get_ofb_fields(flow):
579 if field.type == fd.ETH_TYPE:
580 if field.eth_type == EAP_ETH_TYPE:
581 eap_flow = True
582 if field.type == fd.IN_PORT:
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000583 eap_intf_id = self.platform.intf_id_from_uni_port_num(
Shad Ansari47e0c392018-07-17 23:55:07 +0000584 field.port)
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000585 eap_onu_id = self.platform.onu_id_from_port_num(field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400586
587 if eap_flow:
588 self.log.debug('eap flow detected', onu_id=onu_id,
589 intf_id=intf_id, eap_intf_id=eap_intf_id,
590 eap_onu_id=eap_onu_id)
591 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400592 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400593
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400594 return (False, None)
595
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400596 def get_subscriber_vlan(self, port):
597 self.log.debug('looking from subscriber flow for port', port=port)
598
599 flows = self.logical_flows_proxy.get('/').items
600 for flow in flows:
601 in_port = fd.get_in_port(flow)
602 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400603 if in_port == port and \
Shad Ansaricd20a6d2018-10-02 14:36:33 +0000604 self.platform.intf_id_to_port_type_name(out_port) \
Shad Ansarifd0111d2018-09-13 21:33:06 +0000605 == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400606 fields = fd.get_ofb_fields(flow)
607 self.log.debug('subscriber flow found', fields=fields)
608 for field in fields:
609 if field.type == OFPXMT_OFB_VLAN_VID:
610 self.log.debug('subscriber vlan found',
611 vlan_id=field.vlan_vid)
612 return field.vlan_vid & 0x0fff
613 self.log.debug('No subscriber flow found', port=port)
614 return None
615
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400616 def add_flow_to_device(self, flow, logical_flow):
617 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400618 try:
619 self.stub.FlowAdd(flow)
620 except grpc.RpcError as grpc_e:
621 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
622 self.log.warn('flow already exists', e=grpc_e, flow=flow)
623 else:
624 self.log.error('failed to add flow',
625 logical_flow=logical_flow, flow=flow,
626 grpc_error=grpc_e)
627 else:
628 self.register_flow(logical_flow, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400629
630 def register_flow(self, logical_flow, device_flow):
631 self.log.debug('registering flow in device',
632 logical_flow=logical_flow, device_flow=device_flow)
633 stored_flow = copy.deepcopy(logical_flow)
634 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
635 device_flow.flow_type)
636 self.log.debug('generated device flow id', id=stored_flow.id,
637 flow_id=device_flow.flow_id,
638 direction=device_flow.flow_type)
639 stored_flow.cookie = logical_flow.id
640 flows = self.flows_proxy.get('/')
641 flows.items.extend([stored_flow])
642 self.flows_proxy.update('/', flows)
643
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400644 def find_next_flow(self, flow):
645 table_id = fd.get_goto_table_id(flow)
646 metadata = 0
647 for field in fd.get_ofb_fields(flow):
648 if field.type == fd.METADATA:
649 metadata = field.table_metadata
650 if table_id is None:
651 return None
652 flows = self.logical_flows_proxy.get('/').items
653 next_flows = []
654 for f in flows:
655 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700656 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400657 if fd.get_in_port(f) == fd.get_in_port(flow) and \
658 fd.get_out_port(f) == metadata:
659
660 next_flows.append(f)
661
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400662 if len(next_flows) == 0:
663 self.log.warning('no next flow found, it may be a timing issue',
664 flow=flow, number_of_flows=len(flows))
665 reactor.callLater(5, self.add_flow, flow)
666 return None
667
Jonathan Hart5b435642018-08-20 08:50:05 -0700668 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400669
670 return next_flows[0]
671
672 def update_children_flows(self, device_rules_map):
673
674 for device_id, (flows, groups) in device_rules_map.iteritems():
675 if device_id != self.device_id:
676 self.root_proxy.update('/devices/{}/flows'.format(device_id),
677 Flows(items=flows.values()))
678 self.root_proxy.update('/devices/{}/flow_groups'.format(
679 device_id), FlowGroups(items=groups.values()))
680
681 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400682 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400683 self.log.debug('upstream flow, shifting id')
684 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400685 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400686 self.log.debug('downstream flow, not shifting id')
687 return flow_id
688 else:
689 self.log.warn('Unrecognized direction', direction=direction)
690 return flow_id
691
692 def decode_stored_id(self, id):
693 if id >> 15 == 0x1:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400694 return (id & 0x7fff, UPSTREAM)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400695 else:
Girish Gowdru1a3b7042018-09-19 07:08:48 -0700696 return (id, DOWNSTREAM)