blob: 1780244d94bb499d8f505efff7213065b77cb83d [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07003#
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#
16import structlog
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070017from twisted.internet.defer import inlineCallbacks, returnValue
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070018
19import loxi.of13 as ofp
20from converter import to_loxi, pb2dict, to_grpc
21
22log = structlog.get_logger()
23
24
25class OpenFlowProtocolError(Exception): pass
26
27
28class OpenFlowProtocolHandler(object):
29
sathishgbbb90992017-06-23 09:52:29 +053030 ofp_version = [4] # OFAgent supported versions
31
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070032 def __init__(self, datapath_id, device_id, agent, cxn, rpc):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070033 """
34 The upper half of the OpenFlow protocol, focusing on message
35 exchanges.
36 :param agent: Reference to the Agent() instance, can be used to
37 indicate critical errors to break the connection.
38 :param cxn: The lower level message serdes part of the OF protocol.
39 :param rpc: The application level stub on which RPC calls
40 are made as result of processing incoming OpenFlow request messages.
41 """
42 self.datapath_id = datapath_id
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070043 self.device_id = device_id
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070044 self.agent = agent
45 self.cxn = cxn
46 self.rpc = rpc
sgovindacc736782017-05-02 20:06:37 +053047 self.role = None
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070048
49 @inlineCallbacks
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070050 def start(self):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070051 """A new call is made after a fresh reconnect"""
52
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070053 log.debug('starting')
54
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070055 try:
sathishgbbb90992017-06-23 09:52:29 +053056 support = False
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070057 # send initial hello message
sathishgbbb90992017-06-23 09:52:29 +053058 self.cxn.send(ofp.message.hello(elements=[ofp.common.hello_elem_versionbitmap(
59 bitmaps = [ofp.common.hello_elem_bitmap(self.ofp_version)])]))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070060 # expect to receive a hello message
61 msg = yield self.cxn.recv_class(ofp.message.hello)
sathishgbbb90992017-06-23 09:52:29 +053062 # supports only ofp_versions till 31 and single bitmap.
63 if msg:
64 support = ofp.util.verify_version_support(msg,self.ofp_version)
65 if not support:
66 self.cxn.send(ofp.message.hello_failed_error_msg(
67 xid=msg.xid, code=ofp.OFPHFC_INCOMPATIBLE,
68 data='i support only 1.3'))
69 log.error('peer-do-not-support-OpenFlow-version',self.ofp_version)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070070
sathishgbbb90992017-06-23 09:52:29 +053071 while support:
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070072 req = yield self.cxn.recv_any()
73 handler = self.main_handlers.get(req.type, None)
74 if handler:
75 handler(self, req)
76 else:
77 log.error('cannot-handle',
78 request=req, xid=req.xid, type=req.type)
79
80 except Exception, e:
81 log.exception('exception', e=e)
82
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070083 log.info('started')
84 returnValue(self)
85
86 def stop(self):
87 log.debug('stopping')
88 pass # nothing to do yet
89 log.info('stopped')
90
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070091 def handle_echo_request(self, req):
92 self.cxn.send(ofp.message.echo_reply(xid=req.xid))
93
94 @inlineCallbacks
95 def handle_feature_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070096 device_info = yield self.rpc.get_device_info(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070097 kw = pb2dict(device_info.switch_features)
98 self.cxn.send(ofp.message.features_reply(
99 xid=req.xid,
100 datapath_id=self.datapath_id,
101 **kw))
102
103 def handle_stats_request(self, req):
104 handler = self.stats_handlers.get(req.stats_type, None)
105 if handler:
106 handler(self, req)
107 else:
108 raise OpenFlowProtocolError(
109 'Cannot handle stats request type "{}"'.format(req.stats_type))
110
111 def handle_barrier_request(self, req):
alshabibc3fb4942017-01-26 15:34:24 -0800112 # not really doing barrier yet, but we respond
113 # see https://jira.opencord.org/browse/CORD-823
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700114 self.cxn.send(ofp.message.barrier_reply(xid=req.xid))
115
116 def handle_experimenter_request(self, req):
117 raise NotImplementedError()
118
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700119 def handle_flow_mod_request(self, req):
sgovindacc736782017-05-02 20:06:37 +0530120 if self.role == ofp.OFPCR_ROLE_MASTER or self.role == ofp.OFPCR_ROLE_EQUAL:
121 try:
122 grpc_req = to_grpc(req)
123 except Exception, e:
124 log.exception('failed-to-convert', e=e)
125 else:
126 return self.rpc.update_flow_table(self.device_id, grpc_req)
127
128 elif self.role == ofp.OFPCR_ROLE_SLAVE:
129 self.cxn.send(ofp.message.bad_request_error_msg(code=ofp.OFPBRC_IS_SLAVE))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700130
131 def handle_get_async_request(self, req):
132 raise NotImplementedError()
133
134 def handle_get_config_request(self, req):
135 self.cxn.send(ofp.message.get_config_reply(
136 xid=req.xid,
137 miss_send_len=ofp.OFPCML_NO_BUFFER
138 ))
139
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700140 @inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700141 def handle_group_mod_request(self, req):
sgovindacc736782017-05-02 20:06:37 +0530142 if self.role == ofp.OFPCR_ROLE_MASTER or self.role == ofp.OFPCR_ROLE_EQUAL:
143 yield self.rpc.update_group_table(self.device_id, to_grpc(req))
144 elif self.role == ofp.OFPCR_ROLE_SLAVE:
145 self.cxn.send(ofp.message.bad_request_error_msg(code=ofp.OFPBRC_IS_SLAVE))
146
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700147
148 def handle_meter_mod_request(self, req):
149 raise NotImplementedError()
150
151 def handle_role_request(self, req):
sgovindacc736782017-05-02 20:06:37 +0530152 if req.role == ofp.OFPCR_ROLE_MASTER or req.role == ofp.OFPCR_ROLE_SLAVE:
sathishg00433e52017-05-23 16:07:41 +0530153 if self.agent.generation_is_defined and (
154 ((req.generation_id - self.agent.cached_generation_id) & 0xffffffffffffffff) if abs(
155 req.generation_id - self.agent.cached_generation_id) > 0x7fffffffffffffff else (
156 req.generation_id - self.agent.cached_generation_id)) < 0:
157 self.cxn.send(ofp.message.bad_request_error_msg(code=ofp.OFPRRFC_STALE))
158 else:
159 self.agent.generation_is_defined = True
160 self.agent.cached_generation_id = req.generation_id
161 self.role = req.role
162 self.cxn.send(ofp.message.role_reply(
163 xid=req.xid, role=req.role, generation_id=req.generation_id))
sgovindacc736782017-05-02 20:06:37 +0530164 elif req.role == ofp.OFPCR_ROLE_EQUAL:
sathishg00433e52017-05-23 16:07:41 +0530165 self.role = req.role
166 self.cxn.send(ofp.message.role_reply(
167 xid=req.xid, role=req.role))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700168
169 def handle_packet_out_request(self, req):
sgovindacc736782017-05-02 20:06:37 +0530170 if self.role == ofp.OFPCR_ROLE_MASTER or self.role == ofp.OFPCR_ROLE_EQUAL:
171 self.rpc.send_packet_out(self.device_id, to_grpc(req))
172
173 elif self.role == ofp.OFPCR_ROLE_SLAVE:
174 self.cxn.send(ofp.message.bad_request_error_msg(code=ofp.OFPBRC_IS_SLAVE))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700175
176 def handle_set_config_request(self, req):
alshabibc3fb4942017-01-26 15:34:24 -0800177 # Handle set config appropriately
178 # https://jira.opencord.org/browse/CORD-826
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700179 pass
180
181 def handle_port_mod_request(self, req):
182 raise NotImplementedError()
183
184 def handle_table_mod_request(self, req):
185 raise NotImplementedError()
186
187 def handle_queue_get_config_request(self, req):
188 raise NotImplementedError()
189
190 def handle_set_async_request(self, req):
191 raise NotImplementedError()
192
193 def handle_aggregate_request(self, req):
194 raise NotImplementedError
195
196 @inlineCallbacks
197 def handle_device_description_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700198 device_info = yield self.rpc.get_device_info(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700199 kw = pb2dict(device_info.desc)
200 self.cxn.send(ofp.message.desc_stats_reply(xid=req.xid, **kw))
201
202 def handle_experimenter_stats_request(self, req):
203 raise NotImplementedError()
204
205 @inlineCallbacks
206 def handle_flow_stats_request(self, req):
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800207 try:
208 flow_stats = yield self.rpc.list_flows(self.device_id)
209 self.cxn.send(ofp.message.flow_stats_reply(
210 xid=req.xid, entries=[to_loxi(f) for f in flow_stats]))
211 except Exception, e:
212 log.exception('failed-flow-stats-request', req=req)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700213
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700214 @inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700215 def handle_group_stats_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700216 group_stats = yield self.rpc.list_groups(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700217 self.cxn.send(ofp.message.group_stats_reply(
alshabibf4fb2682017-01-12 00:32:56 -0600218 xid=req.xid, entries=[to_loxi(g.stats) for g in group_stats]))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700219
alshabibf4fb2682017-01-12 00:32:56 -0600220 @inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700221 def handle_group_descriptor_request(self, req):
alshabibf4fb2682017-01-12 00:32:56 -0600222 group_stats = yield self.rpc.list_groups(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700223 self.cxn.send(ofp.message.group_desc_stats_reply(
alshabibf4fb2682017-01-12 00:32:56 -0600224 xid=req.xid, entries=[to_loxi(g.desc) for g in group_stats]))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700225
226 def handle_group_features_request(self, req):
227 raise NotImplementedError()
228
229 def handle_meter_stats_request(self, req):
alshabibc3fb4942017-01-26 15:34:24 -0800230 meter_stats = [] # see https://jira.opencord.org/browse/CORD-825
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700231 self.cxn.send(ofp.message.meter_stats_reply(
232 xid=req.xid, entries=meter_stats))
233
234 def handle_meter_config_request(self, req):
235 raise NotImplementedError()
236
237 def handle_meter_features_request(self, req):
alshabib81824e32016-12-21 21:43:45 -0800238 self.cxn.send(ofp.message.bad_request_error_msg())
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700239
240 def handle_port_stats_request(self, req):
alshabibc3fb4942017-01-26 15:34:24 -0800241 port_stats = [] # see https://jira.opencord.org/browse/CORD-825
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700242 self.cxn.send(ofp.message.port_stats_reply(
243 xid=req.xid,entries=port_stats))
244
245 @inlineCallbacks
246 def handle_port_desc_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700247 port_list = yield self.rpc.get_port_list(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700248 self.cxn.send(ofp.message.port_desc_stats_reply(
249 xid=req.xid,
250 #flags=None,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800251 entries=[to_loxi(port.ofp_port) for port in port_list]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700252 ))
253
254 def handle_queue_stats_request(self, req):
255 raise NotImplementedError()
256
257 def handle_table_stats_request(self, req):
alshabibc3fb4942017-01-26 15:34:24 -0800258 table_stats = [] # see https://jira.opencord.org/browse/CORD-825
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700259 self.cxn.send(ofp.message.table_stats_reply(
260 xid=req.xid, entries=table_stats))
261
262 def handle_table_features_request(self, req):
263 raise NotImplementedError()
264
265 stats_handlers = {
266 ofp.OFPST_AGGREGATE: handle_aggregate_request,
267 ofp.OFPST_DESC: handle_device_description_request,
268 ofp.OFPST_EXPERIMENTER: handle_experimenter_stats_request,
269 ofp.OFPST_FLOW: handle_flow_stats_request,
270 ofp.OFPST_GROUP: handle_group_stats_request,
271 ofp.OFPST_GROUP_DESC: handle_group_descriptor_request,
272 ofp.OFPST_GROUP_FEATURES: handle_group_features_request,
273 ofp.OFPST_METER: handle_meter_stats_request,
274 ofp.OFPST_METER_CONFIG: handle_meter_config_request,
275 ofp.OFPST_METER_FEATURES: handle_meter_features_request,
276 ofp.OFPST_PORT: handle_port_stats_request,
277 ofp.OFPST_PORT_DESC: handle_port_desc_request,
278 ofp.OFPST_QUEUE: handle_queue_stats_request,
279 ofp.OFPST_TABLE: handle_table_stats_request,
280 ofp.OFPST_TABLE_FEATURES: handle_table_features_request
281 }
282
283 main_handlers = {
284 ofp.OFPT_BARRIER_REQUEST: handle_barrier_request,
285 ofp.OFPT_ECHO_REQUEST: handle_echo_request,
286 ofp.OFPT_FEATURES_REQUEST: handle_feature_request,
287 ofp.OFPT_EXPERIMENTER: handle_experimenter_request,
288 ofp.OFPT_FLOW_MOD: handle_flow_mod_request,
289 ofp.OFPT_GET_ASYNC_REQUEST: handle_get_async_request,
290 ofp.OFPT_GET_CONFIG_REQUEST: handle_get_config_request,
291 ofp.OFPT_GROUP_MOD: handle_group_mod_request,
292 ofp.OFPT_METER_MOD: handle_meter_mod_request,
293 ofp.OFPT_PACKET_OUT: handle_packet_out_request,
294 ofp.OFPT_PORT_MOD: handle_port_mod_request,
295 ofp.OFPT_QUEUE_GET_CONFIG_REQUEST: handle_queue_get_config_request,
296 ofp.OFPT_ROLE_REQUEST: handle_role_request,
297 ofp.OFPT_SET_ASYNC: handle_set_async_request,
298 ofp.OFPT_SET_CONFIG: handle_set_config_request,
299 ofp.OFPT_STATS_REQUEST: handle_stats_request,
300 ofp.OFPT_TABLE_MOD: handle_table_mod_request,
301 }
302
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700303 def forward_packet_in(self, ofp_packet_in):
sgovindacc736782017-05-02 20:06:37 +0530304 if self.role == ofp.OFPCR_ROLE_MASTER or self.role == ofp.OFPCR_ROLE_EQUAL:
305 log.info('sending-packet-in', ofp_packet_in=ofp_packet_in)
306 self.cxn.send(to_loxi(ofp_packet_in))
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800307
308 def forward_port_status(self, ofp_port_status):
309 self.cxn.send(to_loxi(ofp_port_status))