This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.py
566 lines (476 loc) · 22.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""NApp responsible for the main OpenFlow basic operations."""
from pyof.foundation.exceptions import UnpackException
from pyof.foundation.network_types import Ethernet, EtherType
from pyof.utils import PYOF_VERSION_LIBS, unpack
from pyof.v0x01.common.header import Type
from pyof.v0x01.controller2switch.common import StatsType
from pyof.v0x04.controller2switch.common import MultipartType
from kytos.core import KytosEvent, KytosNApp, log
from kytos.core.connection import ConnectionState
from kytos.core.helpers import listen_to
from kytos.core.interface import Interface
from napps.kytos.of_core import settings
from napps.kytos.of_core.utils import (GenericHello, NegotiationException,
emit_message_in, emit_message_out,
of_slicer)
from napps.kytos.of_core.v0x01 import utils as of_core_v0x01_utils
from napps.kytos.of_core.v0x01.flow import Flow as Flow01
from napps.kytos.of_core.v0x04 import utils as of_core_v0x04_utils
from napps.kytos.of_core.v0x04.flow import Flow as Flow04
class Main(KytosNApp):
"""Main class of the NApp responsible for OpenFlow basic operations."""
# Keep track of multiple multipart replies from our own request only.
# Assume that all replies are received before setting a new xid.
_multipart_replies_xids = {}
_multipart_replies_flows = {}
_multipart_replies_ports = {}
def setup(self):
"""App initialization (used instead of ``__init__``).
The setup method is automatically called by the run method.
Users shouldn't call this method directly.
"""
self.of_core_version_utils = {0x01: of_core_v0x01_utils,
0x04: of_core_v0x04_utils}
self.execute_as_loop(settings.STATS_INTERVAL)
def execute(self):
"""Run once on app 'start' or in a loop.
The execute method is called by the run method of KytosNApp class.
Users shouldn't call this method directly.
"""
for switch in self.controller.switches.values():
if switch.is_connected():
self._request_flow_list(switch)
if settings.SEND_ECHO_REQUESTS:
version_utils = \
self.of_core_version_utils[switch.
connection.protocol.version]
version_utils.send_echo(self.controller, switch)
def _request_flow_list(self, switch):
"""Send flow stats request to a connected switch."""
of_version = switch.connection.protocol.version
if of_version == 0x01:
of_core_v0x01_utils.update_flow_list(self.controller, switch)
of_core_v0x01_utils.request_port_stats(self.controller, switch)
elif of_version == 0x04:
xid_flows = of_core_v0x04_utils.update_flow_list(self.controller,
switch)
xid_ports = of_core_v0x04_utils.request_port_stats(self.controller,
switch)
self._multipart_replies_xids[switch.id] = {'flows': xid_flows,
'ports': xid_ports}
@listen_to('kytos/of_core.v0x01.messages.in.ofpt_stats_reply')
def handle_stats_reply(self, event):
"""Handle stats replies for v0x01 switches.
Args:
event (:class:`~kytos.core.events.KytosEvent):
Event with ofpt_stats_reply in message.
"""
switch = event.source.switch
msg = event.content['message']
if msg.body_type == StatsType.OFPST_FLOW:
switch.flows = [Flow01.from_of_flow_stats(f, switch)
for f in msg.body]
event_raw = KytosEvent(
name='kytos/of_core.flow_stats.received',
content={'switch': switch})
self.controller.buffers.app.put(event_raw)
elif msg.body_type == StatsType.OFPST_PORT:
port_stats = [of_port_stats for of_port_stats in msg.body]
port_stats_event = KytosEvent(
name=f"kytos/of_core.port_stats",
content={
'switch': switch,
'port_stats': port_stats
})
self.controller.buffers.app.put(port_stats_event)
elif msg.body_type == StatsType.OFPST_DESC:
switch.update_description(msg.body)
@listen_to('kytos/of_core.v0x0[14].messages.in.ofpt_features_reply')
def handle_features_reply(self, event):
"""Handle kytos/of_core.messages.in.ofpt_features_reply event.
This is the end of the Handshake workflow of the OpenFlow Protocol.
Args:
event (KytosEvent): Event with features reply message.
"""
connection = event.source
version_utils = self.of_core_version_utils[connection.protocol.version]
switch = version_utils.handle_features_reply(self.controller, event)
if (connection.is_during_setup() and
connection.protocol.state == 'waiting_features_reply'):
connection.protocol.state = 'handshake_complete'
connection.set_established_state()
version_utils.send_desc_request(self.controller, switch)
if settings.SEND_SET_CONFIG:
version_utils.send_set_config(self.controller, switch)
log.info('Connection %s, Switch %s: OPENFLOW HANDSHAKE COMPLETE',
connection.id, switch.dpid)
event_raw = KytosEvent(
name='kytos/of_core.handshake.completed',
content={'switch': switch})
self.controller.buffers.app.put(event_raw)
@listen_to('kytos/of_core.handshake.completed')
def on_handshake_completed_request_flow_list(self, event):
"""Request an flow list right after the handshake is completed.
Args:
event (KytosEvent): Event with the switch' handshake completed
"""
switch = event.content['switch']
if switch.is_enabled():
self._request_flow_list(switch)
@listen_to('kytos/of_core.v0x04.messages.in.ofpt_multipart_reply')
def handle_multipart_reply(self, event):
"""Handle multipart replies for v0x04 switches.
Args:
event (:class:`~kytos.core.events.KytosEvent):
Event with ofpt_multipart_reply in message.
"""
reply = event.content['message']
switch = event.source.switch
if reply.multipart_type == MultipartType.OFPMP_FLOW:
self._handle_multipart_flow_stats(reply, switch)
elif reply.multipart_type == MultipartType.OFPMP_PORT_STATS:
self._handle_multipart_port_stats(reply, switch)
elif reply.multipart_type == MultipartType.OFPMP_PORT_DESC:
of_core_v0x04_utils.handle_port_desc(self.controller, switch,
reply.body)
elif reply.multipart_type == MultipartType.OFPMP_DESC:
switch.update_description(reply.body)
def _handle_multipart_flow_stats(self, reply, switch):
"""Update switch flows after all replies are received."""
if self._is_multipart_reply_ours(reply, switch, 'flows'):
# Get all flows from the reply
flows = [Flow04.from_of_flow_stats(of_flow_stats, switch)
for of_flow_stats in reply.body]
# Get existent flows from the same xid (or create an empty list)
all_flows = self._multipart_replies_flows.setdefault(switch.id, [])
all_flows.extend(flows)
if reply.flags.value % 2 == 0: # Last bit means more replies
self._update_switch_flows(switch)
event_raw = KytosEvent(
name='kytos/of_core.flow_stats.received',
content={'switch': switch})
self.controller.buffers.app.put(event_raw)
def _handle_multipart_port_stats(self, reply, switch):
"""Emit an event about new port stats."""
if self._is_multipart_reply_ours(reply, switch, 'ports'):
port_stats = [of_port_stats for of_port_stats in reply.body]
all_port_stats = self._multipart_replies_ports.setdefault(
switch.id, []
)
all_port_stats.extend(port_stats)
if reply.flags.value % 2 == 0:
self._new_port_stats(switch)
def _update_switch_flows(self, switch):
"""Update controllers' switch flow list and clean resources."""
switch.flows = self._multipart_replies_flows[switch.id]
del self._multipart_replies_flows[switch.id]
del self._multipart_replies_xids[switch.id]['flows']
def _new_port_stats(self, switch):
"""Send an event with the new port stats and clean resources."""
all_port_stats = self._multipart_replies_ports[switch.id]
del self._multipart_replies_ports[switch.id]
del self._multipart_replies_xids[switch.id]['ports']
port_stats_event = KytosEvent(
name=f"kytos/of_core.port_stats",
content={
'switch': switch,
'port_stats': all_port_stats
})
self.controller.buffers.app.put(port_stats_event)
def _is_multipart_reply_ours(self, reply, switch, stat):
"""Return whether we are expecting the reply."""
if switch.id in self._multipart_replies_xids:
sent_xid = self._multipart_replies_xids[switch.id].get(stat)
if sent_xid == reply.header.xid:
return True
return False
@listen_to('kytos/core.openflow.raw.in')
def handle_raw_in(self, event):
"""Handle a RawEvent and generate a kytos/core.messages.in.* event.
Args:
event (KytosEvent): RawEvent with openflow message to be unpacked
"""
# If the switch is already known to the controller, update the
# 'lastseen' attribute
switch = event.source.switch
if switch:
switch.update_lastseen()
connection = event.source
data = connection.remaining_data + event.content['new_data']
packets, connection.remaining_data = of_slicer(data)
if not packets:
return
unprocessed_packets = []
for packet in packets:
if not connection.is_alive():
return
if connection.is_new():
try:
message = GenericHello(packet=packet)
self._negotiate(connection, message)
except (UnpackException, NegotiationException) as err:
if isinstance(err, UnpackException):
log.error('Connection %s: Invalid hello message',
connection.id)
else:
log.error('Connection %s: Negotiation Failed',
connection.id)
connection.protocol.state = 'hello_failed'
connection.close()
connection.state = ConnectionState.FAILED
return
connection.set_setup_state()
continue
try:
message = connection.protocol.unpack(packet)
if message.header.message_type == Type.OFPT_ERROR:
log.error("OFPT_ERROR: %s error code received from"
" switch %s with xid %s", message.code,
switch.dpid, message.header.xid)
except (UnpackException, AttributeError) as err:
log.error(err)
if isinstance(err, AttributeError):
error_msg = 'connection closed before version negotiation'
log.error('Connection %s: %s', connection.id, error_msg)
connection.close()
return
log.debug('Connection %s: IN OFP, version: %s, type: %s, xid: %s',
connection.id,
message.header.version,
message.header.message_type,
message.header.xid)
waiting_features_reply = (
str(message.header.message_type) == 'Type.OFPT_FEATURES_REPLY'
and connection.protocol.state == 'waiting_features_reply')
if connection.is_during_setup() and not waiting_features_reply:
unprocessed_packets.append(packet)
continue
self.emit_message_in(connection, message)
connection.remaining_data = b''.join(unprocessed_packets) + \
connection.remaining_data
def emit_message_in(self, connection, message):
"""Emit a KytosEvent for each incoming message.
Also update links and port status.
"""
if not connection.is_alive():
return
emit_message_in(self.controller, connection, message)
msg_type = message.header.message_type.name.lower()
if msg_type == 'ofpt_port_status':
self.update_port_status(message, connection)
elif msg_type == 'ofpt_packet_in':
self.update_links(message, connection)
def emit_message_out(self, connection, message):
"""Emit a KytosEvent for each outgoing message."""
if connection.is_alive():
emit_message_out(self.controller, connection, message)
@listen_to('kytos/of_core.v0x0[14].messages.in.ofpt_echo_request')
def handle_echo_request(self, event):
"""Handle Echo Request Messages.
This method will get a echo request sent by client and generate a
echo reply as answer.
Args:
event (:class:`~kytos.core.events.KytosEvent`):
Event with echo request in message.
"""
pyof_lib = PYOF_VERSION_LIBS[event.source.protocol.version]
echo_request = event.message
echo_reply = pyof_lib.symmetric.echo_reply.EchoReply(
xid=echo_request.header.xid,
data=echo_request.data)
self.emit_message_out(event.source, echo_reply)
def _negotiate(self, connection, message):
"""Handle hello messages.
This method will handle the incoming hello message by client
and deal with negotiation.
Parameters:
event (KytosMessageInHello): KytosMessageInHelloEvent
"""
if message.versions:
version = _get_version_from_bitmask(message.versions)
else:
version = _get_version_from_header(message.header.version)
log.debug('connection %s: negotiated version - %s',
connection.id, str(version))
if version is None:
self.fail_negotiation(connection, message)
raise NegotiationException()
version_utils = self.of_core_version_utils[version]
version_utils.say_hello(self.controller, connection)
connection.protocol.name = 'openflow'
connection.protocol.version = version
connection.protocol.unpack = unpack
connection.protocol.state = 'sending_features'
self.send_features_request(connection)
log.debug('Connection %s: Hello complete', connection.id)
def fail_negotiation(self, connection, hello_message):
"""Send Error message and emit event upon negotiation failure."""
log.warning('connection %s: version negotiation failed',
connection.id)
connection.protocol.state = 'hello_failed'
event_raw = KytosEvent(
name='kytos/of_core.hello_failed',
content={'source': connection})
self.controller.buffers.app.put(event_raw)
version = max(settings.OPENFLOW_VERSIONS)
pyof_lib = PYOF_VERSION_LIBS[version]
error_message = pyof_lib.asynchronous.error_msg.ErrorMsg(
xid=hello_message.header.xid,
error_type=pyof_lib.asynchronous.error_msg.
ErrorType.OFPET_HELLO_FAILED,
code=pyof_lib.asynchronous.error_msg.HelloFailedCode.
OFPHFC_INCOMPATIBLE)
self.emit_message_out(connection, error_message)
# May be removed
@listen_to('kytos/of_core.v0x0[14].messages.out.ofpt_echo_reply')
def handle_queued_openflow_echo_reply(self, event):
"""Handle queued OpenFlow echo reply messages.
Send a feature request message if SEND_FEATURES_REQUEST_ON_ECHO
is True (default is False).
"""
if settings.SEND_FEATURES_REQUEST_ON_ECHO:
self.send_features_request(event.destination)
def send_features_request(self, destination):
"""Send a feature request to the switch."""
version = destination.protocol.version
pyof_lib = PYOF_VERSION_LIBS[version]
features_request = pyof_lib.controller2switch.\
features_request.FeaturesRequest()
self.emit_message_out(destination, features_request)
# pylint: disable=no-self-use
@listen_to('kytos/of_core.v0x0[14].messages.out.ofpt_features_request')
def handle_features_request_sent(self, event):
"""Ensure request has actually been sent before changing state."""
if event.destination.protocol.state == 'sending_features':
event.destination.protocol.state = 'waiting_features_reply'
# pylint: enable=no-self-use
@staticmethod
@listen_to('kytos/of_core.v0x[0-9a-f]{2}.messages.in.hello_failed',
'kytos/of_core.v0x0[14].messages.out.hello_failed')
def handle_openflow_in_hello_failed(event):
"""Close the connection upon hello failure."""
event.destination.close()
log.debug("Connection %s: Connection closed.", event.destination.id)
def shutdown(self):
"""End of the application."""
log.debug('Shutting down...')
def update_links(self, message, source):
"""Dispatch 'reacheable.mac' event.
Args:
message: python openflow (pyof) PacketIn object.
source: kytos.core.switch.Connection instance.
Dispatch:
`reachable.mac`:
{
switch : <switch.id>,
port: <port.port_no>
reachable_mac: <mac_address>
}
"""
ethernet = Ethernet()
ethernet.unpack(message.data.value)
if ethernet.ether_type in (EtherType.LLDP, EtherType.IPV6):
return
try:
port = source.switch.get_interface_by_port_no(
message.in_port.value)
except AttributeError:
port = source.switch.get_interface_by_port_no(message.in_port)
name = 'kytos/of_core.reachable.mac'
content = {'switch': source.switch,
'port': port,
'reachable_mac': ethernet.source.value}
event = KytosEvent(name, content)
self.controller.buffers.app.put(event)
msg = 'The MAC %s is reachable from switch/port %s/%s.'
log.debug(msg, ethernet.source, source.switch.id,
message.in_port)
def _send_specific_port_mod(self, port, interface, current_state):
"""Dispatch port link_up/link_down events."""
event_name = 'kytos/of_core.switch.interface.'
event_content = {'interface': interface}
if port.state.value % 2:
status = 'link_down'
else:
status = 'link_up'
if current_state:
if current_state % 2:
current_status = 'link_down'
else:
current_status = 'link_up'
else:
current_status = None
if status != current_status:
event = KytosEvent(name=event_name+status, content=event_content)
self.controller.buffers.app.put(event)
def update_port_status(self, port_status, source):
"""Dispatch 'port.*' events.
Current events:
created|deleted|link_up|link_down|modified
Args:
port_status: python openflow (pyof) PortStatus object.
source: kytos.core.switch.Connection instance.
Dispatch:
`kytos/of_core.switch.port.[created|modified|deleted]`:
{
switch : <switch.id>,
port: <port.port_no>
port_description: {<description of the port>}
}
"""
reason = port_status.reason.enum_ref(port_status.reason.value).name
port = port_status.desc
port_no = port.port_no.value
event_name = 'kytos/of_core.switch.interface.'
if reason == 'OFPPR_ADD':
status = 'created'
interface = Interface(name=port.name.value,
address=port.hw_addr.value,
port_number=port_no,
switch=source.switch,
state=port.state.value,
features=port.curr)
source.switch.update_interface(interface)
elif reason == 'OFPPR_MODIFY':
status = 'modified'
interface = source.switch.get_interface_by_port_no(port_no)
current_status = None
if interface:
log.info('Modified %s %s:%s' %
(interface, interface.switch.dpid,
interface.port_number))
current_status = interface.state
interface.state = port.state.value
interface.name = port.name.value
interface.address = port.hw_addr.value
interface.features = port.curr
else:
interface = Interface(name=port.name.value,
address=port.hw_addr.value,
port_number=port_no,
switch=source.switch,
state=port.state.value,
features=port.curr)
source.switch.update_interface(interface)
self._send_specific_port_mod(port, interface, current_status)
elif reason == 'OFPPR_DELETE':
status = 'deleted'
interface = source.switch.get_interface_by_port_no(port_no)
source.switch.remove_interface(interface)
event_name += status
content = {'interface': interface}
event = KytosEvent(name=event_name, content=content)
self.controller.buffers.app.put(event)
msg = 'The port %s from switch %s was %s.'
log.debug(msg, port_status.desc.port_no, source.switch.id, status)
def _get_version_from_bitmask(message_versions):
"""Get common version from hello message version bitmap."""
try:
return max([version for version in message_versions
if version in settings.OPENFLOW_VERSIONS])
except ValueError:
return None
def _get_version_from_header(message_version):
"""Get common version from hello message header version."""
version = min(message_version, max(settings.OPENFLOW_VERSIONS))
return version if version in settings.OPENFLOW_VERSIONS else None