blob: 8b57c6f7a3f9f6c2dacd00acb5240611dd588080 [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
Shad Ansarifd0111d2018-09-13 21:33:06 +000036LLDP_FLOW_ID = 0x3FF8 # FIXME (16376)
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')
Shad Ansarifd0111d2018-09-13 21:33:06 +0000272 yield self.add_lldp_flow(flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400273
274 elif PUSH_VLAN in action:
275 yield self.add_upstream_data_flow(intf_id, onu_id, classifier,
276 action,
Girish Gowdru141ced82018-09-17 20:19:14 -0700277 flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400278 elif POP_VLAN in action:
Girish Gowdru141ced82018-09-17 20:19:14 -0700279 yield self.add_downstream_data_flow(intf_id, onu_id, classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400280 action, flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000281 else:
Shad Ansarif9d2d102018-06-13 02:15:26 +0000282 self.log.debug('Invalid-flow-type-to-handle',
283 classifier=classifier,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400284 action=action, flow=flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000285
Girish Gowdru141ced82018-09-17 20:19:14 -0700286 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400287 def add_upstream_data_flow(self, intf_id, onu_id, uplink_classifier,
Jonathan Hart5b435642018-08-20 08:50:05 -0700288 uplink_action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000289
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400290
291 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
292
Shad Ansari2dda4f32018-05-17 07:16:07 +0000293
Girish Gowdru141ced82018-09-17 20:19:14 -0700294 yield self.add_hsia_flow(intf_id, onu_id, uplink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400295 uplink_action, UPSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400296 logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000297
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400298 # Secondary EAP on the subscriber vlan
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400299 (eap_active, eap_logical_flow) = self.is_eap_enabled(intf_id, onu_id)
Nicolas Palpacuer2e0fa582018-07-16 16:04:12 -0400300 if eap_active:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400301
Girish Gowdru141ced82018-09-17 20:19:14 -0700302 yield self.add_eapol_flow(intf_id, onu_id, eap_logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400303 eapol_id=EAPOL_SECONDARY_FLOW_INDEX,
304 vlan_id=uplink_classifier[VLAN_VID])
305
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400306
Girish Gowdru141ced82018-09-17 20:19:14 -0700307 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400308 def add_downstream_data_flow(self, intf_id, onu_id, downlink_classifier,
309 downlink_action, flow):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400310 downlink_classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400311 # Needed ???? It should be already there
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400312 downlink_action[POP_VLAN] = True
313 downlink_action[VLAN_VID] = downlink_classifier[VLAN_VID]
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400314
Girish Gowdru141ced82018-09-17 20:19:14 -0700315 yield self.add_hsia_flow(intf_id, onu_id, downlink_classifier,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400316 downlink_action, DOWNSTREAM, HSIA_FLOW_INDEX,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400317 flow)
318
319 # To-Do right now only one GEM port is supported, so below method
320 # will take care of handling all the p bits.
321 # We need to revisit when mulitple gem port per p bits is needed.
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400322 # Waiting for Technology profile
Girish Gowdru141ced82018-09-17 20:19:14 -0700323 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400324 def add_hsia_flow(self, intf_id, onu_id, classifier, action,
325 direction, hsia_id, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000326
Girish Gowdru141ced82018-09-17 20:19:14 -0700327 pon_intf_onu_id = (intf_id, onu_id)
328 gemport_id = yield self.resource_mgr.get_gemport_id(
329 pon_intf_onu_id=pon_intf_onu_id
330 )
331 alloc_id = yield self.resource_mgr.get_alloc_id(
332 pon_intf_onu_id=pon_intf_onu_id
333 )
334
Shad Ansari2dda4f32018-05-17 07:16:07 +0000335 flow_id = platform.mk_flow_id(intf_id, onu_id, hsia_id)
336
Shad Ansari2dda4f32018-05-17 07:16:07 +0000337 flow = openolt_pb2.Flow(
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400338 onu_id=onu_id, flow_id=flow_id, flow_type=direction,
Shad Ansari2dda4f32018-05-17 07:16:07 +0000339 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700340 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400341 priority=logical_flow.priority,
342 classifier=self.mk_classifier(classifier),
343 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000344
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400345 self.add_flow_to_device(flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000346
Girish Gowdru141ced82018-09-17 20:19:14 -0700347 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400348 def add_dhcp_trap(self, intf_id, onu_id, classifier, action, logical_flow):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000349
Shad Ansari47e0c392018-07-17 23:55:07 +0000350 self.log.debug('add dhcp upstream trap', classifier=classifier,
351 action=action)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000352
353 action.clear()
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400354 action[TRAP_TO_HOST] = True
355 classifier[PACKET_TAG_TYPE] = SINGLE_TAG
356 classifier.pop(VLAN_VID, None)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000357
Girish Gowdru141ced82018-09-17 20:19:14 -0700358 pon_intf_onu_id = (intf_id, onu_id)
359 gemport_id = yield self.resource_mgr.get_gemport_id(
360 pon_intf_onu_id=pon_intf_onu_id
361 )
362 alloc_id = yield self.resource_mgr.get_alloc_id(
363 pon_intf_onu_id=pon_intf_onu_id
364 )
Shad Ansari2dda4f32018-05-17 07:16:07 +0000365 flow_id = platform.mk_flow_id(intf_id, onu_id, DHCP_FLOW_INDEX)
366
367 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400368 onu_id=onu_id, flow_id=flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400369 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700370 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400371 priority=logical_flow.priority,
372 classifier=self.mk_classifier(classifier),
Shad Ansarif9d2d102018-06-13 02:15:26 +0000373 action=self.mk_action(action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000374
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400375 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000376
Shad Ansari47e0c392018-07-17 23:55:07 +0000377 # FIXME - ONOS should send explicit upstream and downstream
378 # exact dhcp trap flow.
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400379
380 downstream_logical_flow = copy.deepcopy(logical_flow)
381 for oxm_field in downstream_logical_flow.match.oxm_fields:
382 if oxm_field.ofb_field.type == OFPXMT_OFB_IN_PORT:
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400383 oxm_field.ofb_field.port = \
384 platform.intf_id_to_port_no(0, Port.ETHERNET_NNI)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400385
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400386 classifier[UDP_SRC] = 67
387 classifier[UDP_DST] = 68
388 classifier[PACKET_TAG_TYPE] = DOUBLE_TAG
389 action.pop(PUSH_VLAN, None)
Shad Ansari47e0c392018-07-17 23:55:07 +0000390
391 flow_id = platform.mk_flow_id(intf_id, onu_id,
392 DHCP_DOWNLINK_FLOW_INDEX)
393
394 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400395 onu_id=onu_id, flow_id=flow_id, flow_type=DOWNSTREAM,
Shad Ansari47e0c392018-07-17 23:55:07 +0000396 access_intf_id=intf_id, network_intf_id=0, gemport_id=gemport_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400397 priority=logical_flow.priority, classifier=self.mk_classifier(
398 classifier),
Shad Ansari47e0c392018-07-17 23:55:07 +0000399 action=self.mk_action(action))
400
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400401 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
402
Girish Gowdru141ced82018-09-17 20:19:14 -0700403 @inlineCallbacks
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400404 def add_eapol_flow(self, intf_id, onu_id, logical_flow,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400405 eapol_id=EAPOL_FLOW_INDEX,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000406 vlan_id=DEFAULT_MGMT_VLAN):
Shad Ansari2dda4f32018-05-17 07:16:07 +0000407
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400408 uplink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400409 uplink_classifier[ETH_TYPE] = EAP_ETH_TYPE
410 uplink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
411 uplink_classifier[VLAN_VID] = vlan_id
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400412
413 uplink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400414 uplink_action[TRAP_TO_HOST] = True
Shad Ansari2dda4f32018-05-17 07:16:07 +0000415
Girish Gowdru141ced82018-09-17 20:19:14 -0700416 pon_intf_onu_id = (intf_id, onu_id)
417 gemport_id = yield self.resource_mgr.get_gemport_id(
418 pon_intf_onu_id=pon_intf_onu_id
419 )
420 alloc_id = yield self.resource_mgr.get_alloc_id(
421 pon_intf_onu_id=pon_intf_onu_id
422 )
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400423
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400424 # Add Upstream EAPOL Flow.
425
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400426 uplink_flow_id = platform.mk_flow_id(intf_id, onu_id, eapol_id)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400427
Shad Ansari2dda4f32018-05-17 07:16:07 +0000428 upstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400429 onu_id=onu_id, flow_id=uplink_flow_id, flow_type=UPSTREAM,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400430 access_intf_id=intf_id, gemport_id=gemport_id,
Girish Gowdru141ced82018-09-17 20:19:14 -0700431 alloc_id=alloc_id,
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400432 priority=logical_flow.priority,
Shad Ansarif9d2d102018-06-13 02:15:26 +0000433 classifier=self.mk_classifier(uplink_classifier),
434 action=self.mk_action(uplink_action))
Shad Ansari2dda4f32018-05-17 07:16:07 +0000435
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400436 logical_flow = copy.deepcopy(logical_flow)
437 logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([fd.vlan_vid(
438 vlan_id | 0x1000)]))
439 logical_flow.match.type = OFPMT_OXM
440
441 self.add_flow_to_device(upstream_flow, logical_flow)
Shad Ansari2dda4f32018-05-17 07:16:07 +0000442
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400443 if vlan_id == DEFAULT_MGMT_VLAN:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000444
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400445 # Add Downstream EAPOL Flow, Only for first EAP flow (BAL
446 # requirement)
447 special_vlan_downstream_flow = 4000 - onu_id
Shad Ansari2dda4f32018-05-17 07:16:07 +0000448
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400449 downlink_classifier = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400450 downlink_classifier[PACKET_TAG_TYPE] = SINGLE_TAG
451 downlink_classifier[VLAN_VID] = special_vlan_downstream_flow
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400452
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400453 downlink_action = {}
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400454 downlink_action[PUSH_VLAN] = True
455 downlink_action[VLAN_VID] = vlan_id
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400456
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400457 downlink_flow_id = platform.mk_flow_id(intf_id, onu_id,
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400458 eapol_id)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400459
460 downstream_flow = openolt_pb2.Flow(
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400461 onu_id=onu_id, flow_id=downlink_flow_id, flow_type=DOWNSTREAM,
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400462 access_intf_id=intf_id, gemport_id=gemport_id,
463 priority=logical_flow.priority,
464 classifier=self.mk_classifier(downlink_classifier),
465 action=self.mk_action(downlink_action))
466
Shad Ansarifd0111d2018-09-13 21:33:06 +0000467 downstream_logical_flow = ofp_flow_stats(
468 id=logical_flow.id, cookie=logical_flow.cookie,
469 table_id=logical_flow.table_id, priority=logical_flow.priority,
470 flags=logical_flow.flags)
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400471
472 downstream_logical_flow.match.oxm_fields.extend(fd.mk_oxm_fields([
473 fd.in_port(fd.get_out_port(logical_flow)),
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400474 fd.vlan_vid((special_vlan_downstream_flow) | 0x1000)]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400475 downstream_logical_flow.match.type = OFPMT_OXM
476
477 downstream_logical_flow.instructions.extend(
478 fd.mk_instructions_from_actions([fd.output(
Shad Ansarifd0111d2018-09-13 21:33:06 +0000479 platform.mk_uni_port_num(intf_id, onu_id))]))
Nicolas Palpacuer6152a322018-09-05 10:52:15 -0400480
481 self.add_flow_to_device(downstream_flow, downstream_logical_flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400482
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400483 def repush_all_different_flows(self):
484 # Check if the device is supposed to have flows, if so add them
485 # Recover static flows after a reboot
486 logical_flows = self.logical_flows_proxy.get('/').items
487 devices_flows = self.flows_proxy.get('/').items
488 logical_flows_ids_provisioned = [f.cookie for f in devices_flows]
489 for logical_flow in logical_flows:
490 try:
491 if logical_flow.id not in logical_flows_ids_provisioned:
492 self.add_flow(logical_flow)
493 except Exception as e:
494 self.log.debug('Problem readding this flow', error=e)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400495
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400496 def reset_flows(self):
497 self.flows_proxy.update('/', Flows())
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400498
Shad Ansarifd0111d2018-09-13 21:33:06 +0000499
500 """ Add a downstream LLDP trap flow on the NNI interface
501 """
Girish Gowdru141ced82018-09-17 20:19:14 -0700502 @inlineCallbacks
Shad Ansarifd0111d2018-09-13 21:33:06 +0000503 def add_lldp_flow(self, logical_flow, network_intf_id=0):
Jonathan Hart5b435642018-08-20 08:50:05 -0700504
Shad Ansarifd0111d2018-09-13 21:33:06 +0000505 classifier = {}
506 classifier[ETH_TYPE] = LLDP_ETH_TYPE
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400507 classifier[PACKET_TAG_TYPE] = UNTAGGED
Shad Ansarifd0111d2018-09-13 21:33:06 +0000508 action = {}
509 action[TRAP_TO_HOST] = True
Jonathan Hart5b435642018-08-20 08:50:05 -0700510
Shad Ansarifd0111d2018-09-13 21:33:06 +0000511 flow_id = LLDP_FLOW_ID # FIXME
Jonathan Hart5b435642018-08-20 08:50:05 -0700512
513 downstream_flow = openolt_pb2.Flow(
Shad Ansarifd0111d2018-09-13 21:33:06 +0000514 onu_id=-1, # onu_id not required
515 gemport_id=-1, # gemport_id not required
516 access_intf_id=-1, # access_intf_id not required
517 flow_id=flow_id,
518 flow_type=DOWNSTREAM,
Jonathan Hart5b435642018-08-20 08:50:05 -0700519 priority=logical_flow.priority,
Shad Ansarifd0111d2018-09-13 21:33:06 +0000520 network_intf_id=network_intf_id,
Jonathan Hart5b435642018-08-20 08:50:05 -0700521 classifier=self.mk_classifier(classifier),
522 action=self.mk_action(action))
523
Shad Ansarifd0111d2018-09-13 21:33:06 +0000524 self.log.debug('add lldp downstream trap', classifier=classifier,
525 action=action, flow=downstream_flow)
526 self.add_flow_to_device(downstream_flow, logical_flow)
Jonathan Hart5b435642018-08-20 08:50:05 -0700527
Shad Ansari2dda4f32018-05-17 07:16:07 +0000528 def mk_classifier(self, classifier_info):
529
530 classifier = openolt_pb2.Classifier()
531
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400532 if ETH_TYPE in classifier_info:
533 classifier.eth_type = classifier_info[ETH_TYPE]
534 if IP_PROTO in classifier_info:
535 classifier.ip_proto = classifier_info[IP_PROTO]
536 if VLAN_VID in classifier_info:
537 classifier.o_vid = classifier_info[VLAN_VID]
538 if METADATA in classifier_info:
539 classifier.i_vid = classifier_info[METADATA]
540 if VLAN_PCP in classifier_info:
541 classifier.o_pbits = classifier_info[VLAN_PCP]
542 if UDP_SRC in classifier_info:
543 classifier.src_port = classifier_info[UDP_SRC]
544 if UDP_DST in classifier_info:
545 classifier.dst_port = classifier_info[UDP_DST]
546 if IPV4_DST in classifier_info:
547 classifier.dst_ip = classifier_info[IPV4_DST]
548 if IPV4_SRC in classifier_info:
549 classifier.src_ip = classifier_info[IPV4_SRC]
550 if PACKET_TAG_TYPE in classifier_info:
551 if classifier_info[PACKET_TAG_TYPE] == SINGLE_TAG:
552 classifier.pkt_tag_type = SINGLE_TAG
553 elif classifier_info[PACKET_TAG_TYPE] == DOUBLE_TAG:
554 classifier.pkt_tag_type = DOUBLE_TAG
555 elif classifier_info[PACKET_TAG_TYPE] == UNTAGGED:
556 classifier.pkt_tag_type = UNTAGGED
Shad Ansari2dda4f32018-05-17 07:16:07 +0000557 else:
558 classifier.pkt_tag_type = 'none'
559
560 return classifier
561
562 def mk_action(self, action_info):
563 action = openolt_pb2.Action()
564
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400565 if POP_VLAN in action_info:
566 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000567 action.cmd.remove_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400568 elif PUSH_VLAN in action_info:
569 action.o_vid = action_info[VLAN_VID]
Shad Ansari2dda4f32018-05-17 07:16:07 +0000570 action.cmd.add_outer_tag = True
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400571 elif TRAP_TO_HOST in action_info:
Shad Ansari2dda4f32018-05-17 07:16:07 +0000572 action.cmd.trap_to_host = True
Shad Ansarif9d2d102018-06-13 02:15:26 +0000573 else:
Nicolas Palpacuer324dcae2018-08-02 11:12:22 -0400574 self.log.info('Invalid-action-field', action_info=action_info)
Shad Ansarif9d2d102018-06-13 02:15:26 +0000575 return
Shad Ansari2dda4f32018-05-17 07:16:07 +0000576 return action
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400577
578 def is_eap_enabled(self, intf_id, onu_id):
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400579 flows = self.logical_flows_proxy.get('/').items
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400580
581 for flow in flows:
582 eap_flow = False
583 eap_intf_id = None
584 eap_onu_id = None
585 for field in fd.get_ofb_fields(flow):
586 if field.type == fd.ETH_TYPE:
587 if field.eth_type == EAP_ETH_TYPE:
588 eap_flow = True
589 if field.type == fd.IN_PORT:
Shad Ansari47e0c392018-07-17 23:55:07 +0000590 eap_intf_id = platform.intf_id_from_uni_port_num(
591 field.port)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400592 eap_onu_id = platform.onu_id_from_port_num(field.port)
593
594 if eap_flow:
595 self.log.debug('eap flow detected', onu_id=onu_id,
596 intf_id=intf_id, eap_intf_id=eap_intf_id,
597 eap_onu_id=eap_onu_id)
598 if eap_flow and intf_id == eap_intf_id and onu_id == eap_onu_id:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400599 return (True, flow)
Nicolas Palpacuer61815162018-06-20 18:12:04 -0400600
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400601 return (False, None)
602
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400603 def get_subscriber_vlan(self, port):
604 self.log.debug('looking from subscriber flow for port', port=port)
605
606 flows = self.logical_flows_proxy.get('/').items
607 for flow in flows:
608 in_port = fd.get_in_port(flow)
609 out_port = fd.get_out_port(flow)
Nicolas Palpacuer5780e152018-09-05 17:25:42 -0400610 if in_port == port and \
Shad Ansarifd0111d2018-09-13 21:33:06 +0000611 platform.intf_id_to_port_type_name(out_port) \
612 == Port.ETHERNET_NNI:
Nicolas Palpacuer41141352018-08-31 14:11:38 -0400613 fields = fd.get_ofb_fields(flow)
614 self.log.debug('subscriber flow found', fields=fields)
615 for field in fields:
616 if field.type == OFPXMT_OFB_VLAN_VID:
617 self.log.debug('subscriber vlan found',
618 vlan_id=field.vlan_vid)
619 return field.vlan_vid & 0x0fff
620 self.log.debug('No subscriber flow found', port=port)
621 return None
622
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400623 def add_flow_to_device(self, flow, logical_flow):
624 self.log.debug('pushing flow to device', flow=flow)
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400625 try:
626 self.stub.FlowAdd(flow)
627 except grpc.RpcError as grpc_e:
628 if grpc_e.code() == grpc.StatusCode.ALREADY_EXISTS:
629 self.log.warn('flow already exists', e=grpc_e, flow=flow)
630 else:
631 self.log.error('failed to add flow',
632 logical_flow=logical_flow, flow=flow,
633 grpc_error=grpc_e)
634 else:
635 self.register_flow(logical_flow, flow)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400636
637 def register_flow(self, logical_flow, device_flow):
638 self.log.debug('registering flow in device',
639 logical_flow=logical_flow, device_flow=device_flow)
640 stored_flow = copy.deepcopy(logical_flow)
641 stored_flow.id = self.generate_stored_id(device_flow.flow_id,
642 device_flow.flow_type)
643 self.log.debug('generated device flow id', id=stored_flow.id,
644 flow_id=device_flow.flow_id,
645 direction=device_flow.flow_type)
646 stored_flow.cookie = logical_flow.id
647 flows = self.flows_proxy.get('/')
648 flows.items.extend([stored_flow])
649 self.flows_proxy.update('/', flows)
650
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400651 def find_next_flow(self, flow):
652 table_id = fd.get_goto_table_id(flow)
653 metadata = 0
654 for field in fd.get_ofb_fields(flow):
655 if field.type == fd.METADATA:
656 metadata = field.table_metadata
657 if table_id is None:
658 return None
659 flows = self.logical_flows_proxy.get('/').items
660 next_flows = []
661 for f in flows:
662 if f.table_id == table_id:
Jonathan Hart5b435642018-08-20 08:50:05 -0700663 # FIXME
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400664 if fd.get_in_port(f) == fd.get_in_port(flow) and \
665 fd.get_out_port(f) == metadata:
666
667 next_flows.append(f)
668
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400669 if len(next_flows) == 0:
670 self.log.warning('no next flow found, it may be a timing issue',
671 flow=flow, number_of_flows=len(flows))
672 reactor.callLater(5, self.add_flow, flow)
673 return None
674
Jonathan Hart5b435642018-08-20 08:50:05 -0700675 next_flows.sort(key=lambda f: f.priority, reverse=True)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400676
677 return next_flows[0]
678
679 def update_children_flows(self, device_rules_map):
680
681 for device_id, (flows, groups) in device_rules_map.iteritems():
682 if device_id != self.device_id:
683 self.root_proxy.update('/devices/{}/flows'.format(device_id),
684 Flows(items=flows.values()))
685 self.root_proxy.update('/devices/{}/flow_groups'.format(
686 device_id), FlowGroups(items=groups.values()))
687
688 def generate_stored_id(self, flow_id, direction):
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400689 if direction == UPSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400690 self.log.debug('upstream flow, shifting id')
691 return 0x1 << 15 | flow_id
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400692 elif direction == DOWNSTREAM:
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400693 self.log.debug('downstream flow, not shifting id')
694 return flow_id
695 else:
696 self.log.warn('Unrecognized direction', direction=direction)
697 return flow_id
698
699 def decode_stored_id(self, id):
700 if id >> 15 == 0x1:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400701 return (id & 0x7fff, UPSTREAM)
Nicolas Palpacuer0c7c3162018-08-08 11:27:57 -0400702 else:
Nicolas Palpacuer2789f042018-09-17 09:10:29 -0400703 return (id, DOWNSTREAM)