blob: c1ee45855512a2ee8169ab9f09c596e5103b33dc [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
Girish Gowdru141ced82018-09-17 20:19:14 -070019from twisted.internet.defer import inlineCallbacks, returnValue
Shad Ansari2dda4f32018-05-17 07:16:07 +000020
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040021from voltha.protos.openflow_13_pb2 import OFPXMC_OPENFLOW_BASIC, \
Jonathan Hart5b435642018-08-20 08:50:05 -070022 ofp_flow_stats, OFPMT_OXM, Flows, FlowGroups, OFPXMT_OFB_IN_PORT, \
23 OFPXMT_OFB_VLAN_VID
Nicolas Palpacuer5780e152018-09-05 17:25:42 -040024from voltha.protos.device_pb2 import Port
Shad Ansari2dda4f32018-05-17 07:16:07 +000025import voltha.core.flow_decomposer as fd
26import openolt_platform as platform
Girish Gowdru141ced82018-09-17 20:19:14 -070027from common.pon_resource_manager.resource_manager import PONResourceManager
Shad Ansari2dda4f32018-05-17 07:16:07 +000028from voltha.adapters.openolt.protos import openolt_pb2
Nicolas Palpacuer61815162018-06-20 18:12:04 -040029from voltha.registry import registry
Shad Ansari2dda4f32018-05-17 07:16:07 +000030
Shad Ansarif9d2d102018-06-13 02:15:26 +000031HSIA_FLOW_INDEX = 0 # FIXME
32DHCP_FLOW_INDEX = 1 # FIXME
Shad Ansari47e0c392018-07-17 23:55:07 +000033DHCP_DOWNLINK_FLOW_INDEX = 6 # FIXME
Shad Ansarif9d2d102018-06-13 02:15:26 +000034EAPOL_FLOW_INDEX = 2 # FIXME
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040035EAPOL_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
Nicolas Palpacuer2789f042018-09-17 09:10:29 -040045# Openolt Flow
46UPSTREAM = 'upstream'
47DOWNSTREAM = 'downstream'
48PACKET_TAG_TYPE = 'pkt_tag_type'
49UNTAGGED = 'untagged'
50SINGLE_TAG = 'single_tag'
51DOUBLE_TAG = 'double_tag'
52
53# Classifier
54ETH_TYPE = 'eth_type'
55TPID = 'tpid'
56IP_PROTO = 'ip_proto'
57IN_PORT = 'in_port'
58VLAN_VID = 'vlan_vid'
59VLAN_PCP = 'vlan_pcp'
60UDP_DST = 'udp_dst'
61UDP_SRC = 'udp_src'
62IPV4_DST = 'ipv4_dst'
63IPV4_SRC = 'ipv4_src'
64METADATA = 'metadata'
65OUTPUT = 'output'
66# Action
67POP_VLAN = 'pop_vlan'
68PUSH_VLAN = 'push_vlan'
69TRAP_TO_HOST = 'trap_to_host'
70
71
72
73
Shad Ansari2dda4f32018-05-17 07:16:07 +000074class OpenOltFlowMgr(object):
75
Girish Gowdru141ced82018-09-17 20:19:14 -070076 def __init__(self, log, stub, device_id, logical_device_id, resource_mgr):
Shad Ansari2dda4f32018-05-17 07:16:07 +000077 self.log = log
78 self.stub = stub
Nicolas Palpacuer61815162018-06-20 18:12:04 -040079 self.device_id = device_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -040080 self.logical_device_id = logical_device_id
81 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 Gowdru141ced82018-09-17 20:19:14 -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
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400193 (intf_id, onu_id) = 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
Girish Gowdru141ced82018-09-17 20:19:14 -0700242 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400243 def divide_and_add_flow(self, intf_id, onu_id, classifier,
244 action, flow):
245
246 self.log.debug('sorting flow', intf_id=intf_id, onu_id=onu_id,
247 classifier=classifier, action=action)
248
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400249 if IP_PROTO in classifier:
250 if classifier[IP_PROTO] == 17:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000251 self.log.debug('dhcp flow add')
Girish Gowdru141ced82018-09-17 20:19:14 -0700252 yield self.add_dhcp_trap(intf_id, onu_id, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400253 action, flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400254 elif classifier[IP_PROTO] == 2:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400255 self.log.warn('igmp flow add ignored, not implemented yet')
Shad Ansari2dda4f32018-05-17 07:16:07 +0000256 else:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400257 self.log.warn("Invalid-Classifier-to-handle",
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400258 classifier=classifier,
259 action=action)
260 elif ETH_TYPE in classifier:
261 if classifier[ETH_TYPE] == EAP_ETH_TYPE:
Shad Ansarie048aaa2018-05-18 18:27:21 +0000262 self.log.debug('eapol flow add')
Girish Gowdru141ced82018-09-17 20:19:14 -0700263 yield self.add_eapol_flow(intf_id, onu_id, flow)
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400264 vlan_id = self.get_subscriber_vlan(fd.get_in_port(flow))
265 if vlan_id is not None:
Girish Gowdru141ced82018-09-17 20:19:14 -0700266 yield self.add_eapol_flow(
Jonathan Hart5b435642018-08-20 08:50:05 -0700267 intf_id, onu_id, flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400268 eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400269 vlan_id=vlan_id)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400270 if classifier[ETH_TYPE] == LLDP_ETH_TYPE:
Jonathan Hart5b435642018-08-20 08:50:05 -0700271 self.log.debug('lldp flow add')
Girish Gowdru141ced82018-09-17 20:19:14 -0700272 yield self.add_lldp_flow(intf_id, onu_id, flow, classifier,
Jonathan Hart5b435642018-08-20 08:50:05 -0700273 action)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400274
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400275
276 elif PUSH_VLAN in action:
277 yield self.add_upstream_data_flow(intf_id, onu_id, classifier,
278 action,
Girish Gowdru141ced82018-09-17 20:19:14 -0700279 flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400280 elif POP_VLAN in action:
Girish Gowdru141ced82018-09-17 20:19:14 -0700281 yield self.add_downstream_data_flow(intf_id, onu_id, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400282 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000283 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000284 self.log.debug('Invalid-flow-type-to-handle',
285 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400286 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000287
Girish Gowdru141ced82018-09-17 20:19:14 -0700288 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400289 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
Jonathan Hart5b435642018-08-20 08:50:05 -0700290 uplink_action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000291
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400292
293 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
294
Shad Ansari2dda4f32018-05-17 07:16:07 +0000295
Girish Gowdru141ced82018-09-17 20:19:14 -0700296 yield self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400297 uplink_action, UPSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400298 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000299
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400300 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400301 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400302 if eap_active:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400303
Girish Gowdru141ced82018-09-17 20:19:14 -0700304 yield self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400305 eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
306 vlan_id=uplink_classifier[VLAN_VID])
307
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400308
Girish Gowdru141ced82018-09-17 20:19:14 -0700309 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400310 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
311 downlink_action, flow):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400312 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400313 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400314 downlink_action[POP_VLAN] = True
315 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400316
Girish Gowdru141ced82018-09-17 20:19:14 -0700317 yield self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400318 downlink_action, DOWNSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400319 flow)
320
321 # To-Do right now only one GEM port is supported, so below method
322 # will take care of handling all the p bits.
323 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400324 # Waiting for Technology profile
Girish Gowdru141ced82018-09-17 20:19:14 -0700325 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400326 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
327 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000328
Girish Gowdru141ced82018-09-17 20:19:14 -0700329 pon_intf_onu_id = (intf_id, onu_id)
330 gemport_id = yield self.resource_mgr.get_gemport_id(
331 pon_intf_onu_id=pon_intf_onu_id
332 )
333 alloc_id = yield self.resource_mgr.get_alloc_id(
334 pon_intf_onu_id=pon_intf_onu_id
335 )
336
Shad Ansari2dda4f32018-05-17 07:16:07 +0000337 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
338
Shad Ansari2dda4f32018-05-17 07:16:07 +0000339 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400340 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000341 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700342 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400343 priority=logical_flow.priority,
344 classifier=self.mk_classifier(classifier),
345 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000346
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400347 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000348
Girish Gowdru141ced82018-09-17 20:19:14 -0700349 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400350 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000351
Shad Ansari47e0c392018-07-17 23:55:07 +0000352 self.log.debug('add dhcp upstream trap', classifier=classifier,
353 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000354
355 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400356 action[TRAP_TO_HOST] = True
357 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
358 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000359
Girish Gowdru141ced82018-09-17 20:19:14 -0700360 pon_intf_onu_id = (intf_id, onu_id)
361 gemport_id = yield self.resource_mgr.get_gemport_id(
362 pon_intf_onu_id=pon_intf_onu_id
363 )
364 alloc_id = yield self.resource_mgr.get_alloc_id(
365 pon_intf_onu_id=pon_intf_onu_id
366 )
Shad Ansari2dda4f32018-05-17 07:16:07 +0000367 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
368
369 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400370 onu_id=onu_id, flow_id=flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400371 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700372 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400373 priority=logical_flow.priority,
374 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000375 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000376
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400377 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000378
Shad Ansari47e0c392018-07-17 23:55:07 +0000379 # FIXME - ONOS should send explicit upstream and downstream
380 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400381
382 downstream_logical_flow = copy.deepcopy(logical_flow)
383 for oxm_field in downstream_logical_flow.match.oxm_fields:
384 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400385 oxm_field.ofb_field.port = \
386 platform.intf_id_to_port_no(0, Port.ETHERNET_NNI)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400387
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400388 classifier[UDP_SRC] = 67
389 classifier[UDP_DST] = 68
390 classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
391 action.pop(PUSH_VLAN, None)
Shad Ansari47e0c392018-07-17 23:55:07 +0000392
393 flow_id = platform.mk_flow_id(intf_id, onu_id,
394 DHCP_DOWNLINK_FLOW_INDEX)
395
396 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400397 onu_id=onu_id, flow_id=flow_id, flow_type=DOWNSTREAM,
Shad Ansari47e0c392018-07-17 23:55:07 +0000398 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400399 priority=logical_flow.priority, classifier=self.mk_classifier(
400 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000401 action=self.mk_action(action))
402
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400403 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
404
Girish Gowdru141ced82018-09-17 20:19:14 -0700405 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400406 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400407 eapol_id=EAPOL_FLOW_INDEX,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000408 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000409
Shad Ansari2dda4f32018-05-17 07:16:07 +0000410
Shad Ansari2dda4f32018-05-17 07:16:07 +0000411
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400412 uplink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400413 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
414 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
415 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400416
417 uplink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400418 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000419
Girish Gowdru141ced82018-09-17 20:19:14 -0700420 pon_intf_onu_id = (intf_id, onu_id)
421 gemport_id = yield self.resource_mgr.get_gemport_id(
422 pon_intf_onu_id=pon_intf_onu_id
423 )
424 alloc_id = yield self.resource_mgr.get_alloc_id(
425 pon_intf_onu_id=pon_intf_onu_id
426 )
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400427
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400428 # Add Upstream EAPOL Flow.
429
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400430 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, eapol_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400431
Shad Ansari2dda4f32018-05-17 07:16:07 +0000432 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400433 onu_id=onu_id, flow_id=uplink_flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400434 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700435 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400436 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000437 classifier=self.mk_classifier(uplink_classifier),
438 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000439
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400440 logical_flow = copy.deepcopy(logical_flow)
441 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
442 vlan_id | 0x1000)]))
443 logical_flow.match.type = OFPMT_OXM
444
445 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000446
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400447 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000448
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400449 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
450 # requirement)
451 special_vlan_downstream_flow = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000452
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400453 downlink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400454 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
455 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400456
Jonathan Hart5b435642018-08-20 08:50:05 -0700457
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400458 downlink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400459 downlink_action[PUSH_VLAN] = True
460 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400461
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400462 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400463 eapol_id)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400464
465 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400466 onu_id=onu_id, flow_id=downlink_flow_id, flow_type=DOWNSTREAM,
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400467 access_intf_id=intf_id, gemport_id=gemport_id,
468 priority=logical_flow.priority,
469 classifier=self.mk_classifier(downlink_classifier),
470 action=self.mk_action(downlink_action))
471
472 downstream_logical_flow = ofp_flow_stats(id=logical_flow.id,
473 cookie=logical_flow.cookie, table_id=logical_flow.table_id,
474 priority=logical_flow.priority, flags=logical_flow.flags)
475
476 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
477 fd.in_port(fd.get_out_port(logical_flow)),
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400478 fd.vlan_vid((special_vlan_downstream_flow) | 0x1000)]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400479 downstream_logical_flow.match.type = OFPMT_OXM
480
481 downstream_logical_flow.instructions.extend(
482 fd.mk_instructions_from_actions([fd.output(
483 platform.mk_uni_port_num(intf_id, onu_id))]))
484
485 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400486
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400487 def repush_all_different_flows(self):
488 # Check if the device is supposed to have flows, if so add them
489 # Recover static flows after a reboot
490 logical_flows = self.logical_flows_proxy.get('/').items
491 devices_flows = self.flows_proxy.get('/').items
492 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
493 for logical_flow in logical_flows:
494 try:
495 if logical_flow.id not in logical_flows_ids_provisioned:
496 self.add_flow(logical_flow)
497 except Exception as e:
498 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400499
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400500 def reset_flows(self):
501 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400502
Girish Gowdru141ced82018-09-17 20:19:14 -0700503 @inlineCallbacks
Jonathan Hart5b435642018-08-20 08:50:05 -0700504 def add_lldp_flow(self, intf_id, onu_id, logical_flow, classifier, action):
505
506 self.log.debug('add lldp downstream trap', classifier=classifier,
507 action=action)
508
509 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400510 action[TRAP_TO_HOST] = True
511 classifier[PACKET_TAG_TYPE] = UNTAGGED
Jonathan Hart5b435642018-08-20 08:50:05 -0700512
Girish Gowdru141ced82018-09-17 20:19:14 -0700513 pon_intf_onu_id = (intf_id, onu_id)
514 gemport_id = yield self.resource_mgr.get_gemport_id(
515 pon_intf_onu_id=pon_intf_onu_id
516 )
517 alloc_id = yield self.resource_mgr.get_alloc_id(
518 pon_intf_onu_id=pon_intf_onu_id
519 )
520
Jonathan Hart5b435642018-08-20 08:50:05 -0700521 flow_id = platform.mk_flow_id(intf_id, onu_id, LLDP_FLOW_INDEX)
522
523 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400524 onu_id=onu_id, flow_id=flow_id, flow_type=DOWNSTREAM,
Jonathan Hart5b435642018-08-20 08:50:05 -0700525 access_intf_id=3, network_intf_id=0, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700526 alloc_id=alloc_id,
Jonathan Hart5b435642018-08-20 08:50:05 -0700527 priority=logical_flow.priority,
528 classifier=self.mk_classifier(classifier),
529 action=self.mk_action(action))
530
531 self.log.debug('add lldp downstream trap', access_intf_id=intf_id,
532 onu_id=onu_id, flow_id=flow_id)
533 self.stub.FlowAdd(downstream_flow)
534
Shad Ansari2dda4f32018-05-17 07:16:07 +0000535 def mk_classifier(self, classifier_info):
536
537 classifier = openolt_pb2.Classifier()
538
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400539 if ETH_TYPE in classifier_info:
540 classifier.eth_type = classifier_info[ETH_TYPE]
541 if IP_PROTO in classifier_info:
542 classifier.ip_proto = classifier_info[IP_PROTO]
543 if VLAN_VID in classifier_info:
544 classifier.o_vid = classifier_info[VLAN_VID]
545 if METADATA in classifier_info:
546 classifier.i_vid = classifier_info[METADATA]
547 if VLAN_PCP in classifier_info:
548 classifier.o_pbits = classifier_info[VLAN_PCP]
549 if UDP_SRC in classifier_info:
550 classifier.src_port = classifier_info[UDP_SRC]
551 if UDP_DST in classifier_info:
552 classifier.dst_port = classifier_info[UDP_DST]
553 if IPV4_DST in classifier_info:
554 classifier.dst_ip = classifier_info[IPV4_DST]
555 if IPV4_SRC in classifier_info:
556 classifier.src_ip = classifier_info[IPV4_SRC]
557 if PACKET_TAG_TYPE in classifier_info:
558 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
559 classifier.pkt_tag_type = SINGLE_TAG
560 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
561 classifier.pkt_tag_type = DOUBLE_TAG
562 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
563 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000564 else:
565 classifier.pkt_tag_type = 'none'
566
567 return classifier
568
569 def mk_action(self, action_info):
570 action = openolt_pb2.Action()
571
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400572 if POP_VLAN in action_info:
573 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000574 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400575 elif PUSH_VLAN in action_info:
576 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000577 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400578 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000579 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000580 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400581 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000582 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000583 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400584
585 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400586 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400587
588 for flow in flows:
589 eap_flow = False
590 eap_intf_id = None
591 eap_onu_id = None
592 for field in fd.get_ofb_fields(flow):
593 if field.type == fd.ETH_TYPE:
594 if field.eth_type == EAP_ETH_TYPE:
595 eap_flow = True
596 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000597 eap_intf_id = platform.intf_id_from_uni_port_num(
598 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400599 eap_onu_id = platform.onu_id_from_port_num(field.port)
600
601 if eap_flow:
602 self.log.debug('eap flow detected', onu_id=onu_id,
603 intf_id=intf_id, eap_intf_id=eap_intf_id,
604 eap_onu_id=eap_onu_id)
605 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400606 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400607
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400608 return (False, None)
609
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400610 def get_subscriber_vlan(self, port):
611 self.log.debug('looking from subscriber flow for port', port=port)
612
613 flows = self.logical_flows_proxy.get('/').items
614 for flow in flows:
615 in_port = fd.get_in_port(flow)
616 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400617
618 if in_port == port and \
619 platform.intf_id_to_port_type_name(out_port) == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400620 fields = fd.get_ofb_fields(flow)
621 self.log.debug('subscriber flow found', fields=fields)
622 for field in fields:
623 if field.type == OFPXMT_OFB_VLAN_VID:
624 self.log.debug('subscriber vlan found',
625 vlan_id=field.vlan_vid)
626 return field.vlan_vid & 0x0fff
627 self.log.debug('No subscriber flow found', port=port)
628 return None
629
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400630 def add_flow_to_device(self, flow, logical_flow):
631 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400632 try:
633 self.stub.FlowAdd(flow)
634 except grpc.RpcError as grpc_e:
635 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
636 self.log.warn('flow already exists', e=grpc_e, flow=flow)
637 else:
638 self.log.error('failed to add flow',
639 logical_flow=logical_flow, flow=flow,
640 grpc_error=grpc_e)
641 else:
642 self.register_flow(logical_flow, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400643
644 def register_flow(self, logical_flow, device_flow):
645 self.log.debug('registering flow in device',
646 logical_flow=logical_flow, device_flow=device_flow)
647 stored_flow = copy.deepcopy(logical_flow)
648 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
649 device_flow.flow_type)
650 self.log.debug('generated device flow id', id=stored_flow.id,
651 flow_id=device_flow.flow_id,
652 direction=device_flow.flow_type)
653 stored_flow.cookie = logical_flow.id
654 flows = self.flows_proxy.get('/')
655 flows.items.extend([stored_flow])
656 self.flows_proxy.update('/', flows)
657
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400658 def find_next_flow(self, flow):
659 table_id = fd.get_goto_table_id(flow)
660 metadata = 0
661 for field in fd.get_ofb_fields(flow):
662 if field.type == fd.METADATA:
663 metadata = field.table_metadata
664 if table_id is None:
665 return None
666 flows = self.logical_flows_proxy.get('/').items
667 next_flows = []
668 for f in flows:
669 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700670 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400671 if fd.get_in_port(f) == fd.get_in_port(flow) and \
672 fd.get_out_port(f) == metadata:
673
674 next_flows.append(f)
675
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400676 if len(next_flows) == 0:
677 self.log.warning('no next flow found, it may be a timing issue',
678 flow=flow, number_of_flows=len(flows))
679 reactor.callLater(5, self.add_flow, flow)
680 return None
681
Jonathan Hart5b435642018-08-20 08:50:05 -0700682 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400683
684 return next_flows[0]
685
686 def update_children_flows(self, device_rules_map):
687
688 for device_id, (flows, groups) in device_rules_map.iteritems():
689 if device_id != self.device_id:
690 self.root_proxy.update('/devices/{}/flows'.format(device_id),
691 Flows(items=flows.values()))
692 self.root_proxy.update('/devices/{}/flow_groups'.format(
693 device_id), FlowGroups(items=groups.values()))
694
695 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400696 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400697 self.log.debug('upstream flow, shifting id')
698 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400699 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400700 self.log.debug('downstream flow, not shifting id')
701 return flow_id
702 else:
703 self.log.warn('Unrecognized direction', direction=direction)
704 return flow_id
705
706 def decode_stored_id(self, id):
707 if id >> 15 == 0x1:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400708 return (id & 0x7fff, UPSTREAM)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400709 else:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400710 return (id, DOWNSTREAM)