blob: d0b71e3682e0a86960fd9c8920c40197c0cd922d [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
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070030 def __init__(self, datapath_id, device_id, agent, cxn, rpc):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070031 """
32 The upper half of the OpenFlow protocol, focusing on message
33 exchanges.
34 :param agent: Reference to the Agent() instance, can be used to
35 indicate critical errors to break the connection.
36 :param cxn: The lower level message serdes part of the OF protocol.
37 :param rpc: The application level stub on which RPC calls
38 are made as result of processing incoming OpenFlow request messages.
39 """
40 self.datapath_id = datapath_id
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070041 self.device_id = device_id
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070042 self.agent = agent
43 self.cxn = cxn
44 self.rpc = rpc
45
46 @inlineCallbacks
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070047 def start(self):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070048 """A new call is made after a fresh reconnect"""
49
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070050 log.debug('starting')
51
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070052 try:
53 # send initial hello message
54 self.cxn.send(ofp.message.hello())
55
56 # expect to receive a hello message
57 msg = yield self.cxn.recv_class(ofp.message.hello)
58 # TODO verify version compatibility (must list version 1.3)
59
60 while True:
61 req = yield self.cxn.recv_any()
62 handler = self.main_handlers.get(req.type, None)
63 if handler:
64 handler(self, req)
65 else:
66 log.error('cannot-handle',
67 request=req, xid=req.xid, type=req.type)
68
69 except Exception, e:
70 log.exception('exception', e=e)
71
Zsolt Haraszti2bdb6b32016-11-03 16:56:17 -070072 log.info('started')
73 returnValue(self)
74
75 def stop(self):
76 log.debug('stopping')
77 pass # nothing to do yet
78 log.info('stopped')
79
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070080 def handle_echo_request(self, req):
81 self.cxn.send(ofp.message.echo_reply(xid=req.xid))
82
83 @inlineCallbacks
84 def handle_feature_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -070085 device_info = yield self.rpc.get_device_info(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070086 kw = pb2dict(device_info.switch_features)
87 self.cxn.send(ofp.message.features_reply(
88 xid=req.xid,
89 datapath_id=self.datapath_id,
90 **kw))
91
92 def handle_stats_request(self, req):
93 handler = self.stats_handlers.get(req.stats_type, None)
94 if handler:
95 handler(self, req)
96 else:
97 raise OpenFlowProtocolError(
98 'Cannot handle stats request type "{}"'.format(req.stats_type))
99
100 def handle_barrier_request(self, req):
101 # TODO not really doing barrier yet, but we respond
102 self.cxn.send(ofp.message.barrier_reply(xid=req.xid))
103
104 def handle_experimenter_request(self, req):
105 raise NotImplementedError()
106
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700107 def handle_flow_mod_request(self, req):
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800108 try:
109 grpc_req = to_grpc(req)
110 except Exception, e:
111 log.exception('failed-to-convert', e=e)
112 else:
113 return self.rpc.update_flow_table(self.device_id, grpc_req)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700114
115 def handle_get_async_request(self, req):
116 raise NotImplementedError()
117
118 def handle_get_config_request(self, req):
119 self.cxn.send(ofp.message.get_config_reply(
120 xid=req.xid,
121 miss_send_len=ofp.OFPCML_NO_BUFFER
122 ))
123
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700124 @inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700125 def handle_group_mod_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700126 yield self.rpc.update_group_table(self.device_id, to_grpc(req))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700127
128 def handle_meter_mod_request(self, req):
129 raise NotImplementedError()
130
131 def handle_role_request(self, req):
132 # TODO this is where we need to manage which connection is active
133 if req.role != ofp.OFPCR_ROLE_MASTER:
134 raise NotImplementedError()
135 self.cxn.send(ofp.message.role_reply(
136 xid=req.xid, role=req.role, generation_id=req.generation_id))
137
138 def handle_packet_out_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700139 self.rpc.send_packet_out(self.device_id, to_grpc(req))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700140
141 def handle_set_config_request(self, req):
142 # TODO ignore for now
143 pass
144
145 def handle_port_mod_request(self, req):
146 raise NotImplementedError()
147
148 def handle_table_mod_request(self, req):
149 raise NotImplementedError()
150
151 def handle_queue_get_config_request(self, req):
152 raise NotImplementedError()
153
154 def handle_set_async_request(self, req):
155 raise NotImplementedError()
156
157 def handle_aggregate_request(self, req):
158 raise NotImplementedError
159
160 @inlineCallbacks
161 def handle_device_description_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700162 device_info = yield self.rpc.get_device_info(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700163 kw = pb2dict(device_info.desc)
164 self.cxn.send(ofp.message.desc_stats_reply(xid=req.xid, **kw))
165
166 def handle_experimenter_stats_request(self, req):
167 raise NotImplementedError()
168
169 @inlineCallbacks
170 def handle_flow_stats_request(self, req):
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800171 try:
172 flow_stats = yield self.rpc.list_flows(self.device_id)
173 self.cxn.send(ofp.message.flow_stats_reply(
174 xid=req.xid, entries=[to_loxi(f) for f in flow_stats]))
175 except Exception, e:
176 log.exception('failed-flow-stats-request', req=req)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700177
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700178 @inlineCallbacks
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700179 def handle_group_stats_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700180 group_stats = yield self.rpc.list_groups(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700181 self.cxn.send(ofp.message.group_stats_reply(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700182 xid=req.xid, entries=[to_loxi(g) for g in group_stats]))
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700183
184 def handle_group_descriptor_request(self, req):
185 group_list = [] # TODO
186 self.cxn.send(ofp.message.group_desc_stats_reply(
187 xid=req.xid, entries=group_list))
188
189 def handle_group_features_request(self, req):
190 raise NotImplementedError()
191
192 def handle_meter_stats_request(self, req):
193 meter_stats = [] # TODO
194 self.cxn.send(ofp.message.meter_stats_reply(
195 xid=req.xid, entries=meter_stats))
196
197 def handle_meter_config_request(self, req):
198 raise NotImplementedError()
199
200 def handle_meter_features_request(self, req):
alshabib81824e32016-12-21 21:43:45 -0800201 self.cxn.send(ofp.message.bad_request_error_msg())
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700202
203 def handle_port_stats_request(self, req):
204 port_stats = [] # TODO
205 self.cxn.send(ofp.message.port_stats_reply(
206 xid=req.xid,entries=port_stats))
207
208 @inlineCallbacks
209 def handle_port_desc_request(self, req):
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700210 port_list = yield self.rpc.get_port_list(self.device_id)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700211 self.cxn.send(ofp.message.port_desc_stats_reply(
212 xid=req.xid,
213 #flags=None,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800214 entries=[to_loxi(port.ofp_port) for port in port_list]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700215 ))
216
217 def handle_queue_stats_request(self, req):
218 raise NotImplementedError()
219
220 def handle_table_stats_request(self, req):
221 table_stats = [] # TODO
222 self.cxn.send(ofp.message.table_stats_reply(
223 xid=req.xid, entries=table_stats))
224
225 def handle_table_features_request(self, req):
226 raise NotImplementedError()
227
228 stats_handlers = {
229 ofp.OFPST_AGGREGATE: handle_aggregate_request,
230 ofp.OFPST_DESC: handle_device_description_request,
231 ofp.OFPST_EXPERIMENTER: handle_experimenter_stats_request,
232 ofp.OFPST_FLOW: handle_flow_stats_request,
233 ofp.OFPST_GROUP: handle_group_stats_request,
234 ofp.OFPST_GROUP_DESC: handle_group_descriptor_request,
235 ofp.OFPST_GROUP_FEATURES: handle_group_features_request,
236 ofp.OFPST_METER: handle_meter_stats_request,
237 ofp.OFPST_METER_CONFIG: handle_meter_config_request,
238 ofp.OFPST_METER_FEATURES: handle_meter_features_request,
239 ofp.OFPST_PORT: handle_port_stats_request,
240 ofp.OFPST_PORT_DESC: handle_port_desc_request,
241 ofp.OFPST_QUEUE: handle_queue_stats_request,
242 ofp.OFPST_TABLE: handle_table_stats_request,
243 ofp.OFPST_TABLE_FEATURES: handle_table_features_request
244 }
245
246 main_handlers = {
247 ofp.OFPT_BARRIER_REQUEST: handle_barrier_request,
248 ofp.OFPT_ECHO_REQUEST: handle_echo_request,
249 ofp.OFPT_FEATURES_REQUEST: handle_feature_request,
250 ofp.OFPT_EXPERIMENTER: handle_experimenter_request,
251 ofp.OFPT_FLOW_MOD: handle_flow_mod_request,
252 ofp.OFPT_GET_ASYNC_REQUEST: handle_get_async_request,
253 ofp.OFPT_GET_CONFIG_REQUEST: handle_get_config_request,
254 ofp.OFPT_GROUP_MOD: handle_group_mod_request,
255 ofp.OFPT_METER_MOD: handle_meter_mod_request,
256 ofp.OFPT_PACKET_OUT: handle_packet_out_request,
257 ofp.OFPT_PORT_MOD: handle_port_mod_request,
258 ofp.OFPT_QUEUE_GET_CONFIG_REQUEST: handle_queue_get_config_request,
259 ofp.OFPT_ROLE_REQUEST: handle_role_request,
260 ofp.OFPT_SET_ASYNC: handle_set_async_request,
261 ofp.OFPT_SET_CONFIG: handle_set_config_request,
262 ofp.OFPT_STATS_REQUEST: handle_stats_request,
263 ofp.OFPT_TABLE_MOD: handle_table_mod_request,
264 }
265
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700266 def forward_packet_in(self, ofp_packet_in):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800267 log.info('sending-packet-in', ofp_packet_in=ofp_packet_in)
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700268 self.cxn.send(to_loxi(ofp_packet_in))
Zsolt Haraszti217a12e2016-12-19 16:37:55 -0800269
270 def forward_port_status(self, ofp_port_status):
271 self.cxn.send(to_loxi(ofp_port_status))