-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopoDiscoveryController.py
1613 lines (1288 loc) · 63 KB
/
TopoDiscoveryController.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
# Topology discovery
from topo_discovery import event
from topo_discovery.lldp_discovery import SwitchesDiscovery
from topo_discovery.api import get_switch
from topo_discovery.api import pause_topo_discovery, resume_topo_discovery
# Topology ingress change detection
import six
from ryu.lib.packet import vlan
from ryu.lib.packet import packet, ethernet
# OFPErrorMsg output handler
from ryu import utils
from ryu.ofproto import ofproto_parser
from ryu import cfg
from threading import Timer
import re
import csv
import signal
import os
import logging
import time
from ShortestPath.dijkstra_te import Graph
import OFP_Helper
# TE optimisation and inter controller communication
from TE import TEOptimisation
from InterControllerCommunication import ControllerCommunication
# Private global mapping variable of GIDs to host pairs
__GID_MAP__ = {}
class TopoDiscoveryController(app_manager.RyuApp):
""" Base controller that handles topo discovery callbacks, stats collection, role changes
and other helpfull methods to allow controller switch interaction.
Attributes:
OFP_VERSIONS (array of ofproto.OFP_VERSION): Supported OF versions.
CONTROLLER_NAME (str): Name of the controller, inherting class should change!
graph (ShortestPath.dijkstra): Topology information graph object
hosts (list of obj): List of connected hosts
TE (obj): TE optimisation module instance
ctrl_com (obj): Controller communication module instance
__stats_timer (threading.Timer): Timer instance that triggers stats polling
__rebuild_state_timeout (int): Rebuild state timout count
__rebuild_state_sw (dict): Rebuild state switches recovered state info
__ctrl_role (str): Current controller role (unknown, slave, master)
unknown_links (dict): List of unknown links in format
{(<src sw>, <src pn>, <dst pn>): <cid or [timeout value]>}
__unknown_links_timer (threading.Timer): Timer instance that handles CID resolution for
unknown links.
__ing_change_detect_wait (dict): List of paths ingress change detection is temporary
disabled to prevent swapping due to in-flight packets. {(h1, h2): <Timer Object>}.
"""
CONTROLLER_NAME = ""
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
# XXX NOTE: SETTING FLAG TO TRUE WILL ENABLE TESTING MODE WHERE NO RYU OR MULTI-CONTROLLER
# FUNCTIONALITY IS LOADED. THIS MODE SHOULD ONLY BE USED WHEN TESTING THE BARE BONE FUNCTIONS
# OF THE IMPLEMENTATION. DO NOT SET THIS FLAG TO TRUE UNLESS YOU ARE TESTING THE CLASS
TESTING_MODE = False
def __init__(self, *args, **kwargs):
""" Initiate arguments, topology discovery, controller communication and TE
optimisation isntance. Process the config file and other attributes """
# Check for TESTING MODE
if self.TESTING_MODE:
# Initiate dummy conf parser and logger
self.CONF = cfg.CONF
if "logger" in kwargs:
self.logger = kwargs["logger"]
else:
logging.basicConfig(level=logging.CRITICAL)
self.logger = logging
self.logger.warning("CONTROLER RUNNING IN TESTING MODE!")
# Initiate default attributes
if "topo" in kwargs:
self.graph = Graph(kwargs["topo"])
else:
self.graph = Graph()
if "hosts" in kwargs:
self.hosts = kwargs["hosts"]
else:
self.hosts = []
self.__stats_timer = None
self.__rebuild_state_timeout = 0
self.__rebuild_state_sw = {}
self.__ctrl_role = "unknown"
self.unknown_links = {}
self.__unknown_links_timer = None
self.__ing_change_detect_wait = {}
self.__cleanup_handlers = []
# Initiate the inter controller communication module without starting
# the thread.
self.ctrl_com = ControllerCommunication(self.logger, self)
return
super(TopoDiscoveryController, self).__init__(*args, **kwargs)
# If a log file was provided log information only to the file
#if self.CONF.log_file:
# root_logger = logging.getLogger()
# for handler in root_logger.handlers:
# if not isinstance(handler, logging.handlers.WatchedFileHandler):
# root_logger.removeHandler(handler)
# Stop execution if the the base controller is loaded or name is not modified
if self.CONTROLLER_NAME == "":
self.logger.critical("Loaded base controller or did not overwrite controller name!")
exit(0)
# Register application-stats arguments
self.CONF.register_opts([
cfg.BoolOpt("collect", default=True,
help="Query and collect stats from the switches and network"),
cfg.BoolOpt("collect_port", default=True,
help="Collect, process and output to the console OpenFlow port stats"),
cfg.FloatOpt("interval", default=10.0, min=0.5, max=600.0,
help="Query and collect stats from the switches and network (1 to 600)"),
cfg.BoolOpt("out_port", default=False,
help="Output port stats on stats out signal recived")
], group="stats")
self.CONF.register_opts([
cfg.BoolOpt("start_com", default=True,
help="Initiate the multi-controller communication module (defaults True)"),
cfg.IntOpt("domain_id", default=0,
help="ID of the domain that contains the controller (defaults to 0)"),
cfg.IntOpt("inst_id", default=None,
help="ID of controller instances (defaults to -1, gen random id)")
], group="multi_ctrl")
self.CONF.register_opts([
cfg.StrOpt("static_port_desc", default="",
help="File that contains the static port descriptions")
], group="application")
self.CONF.register_opts([
cfg.FloatOpt("utilisation_threshold", default=0.90, min = 0.0, max = 1.0,
help="Threshold used to decide if a port is congested (defaults to 0.90)"),
cfg.FloatOpt("consolidate_time", default=1.0, min=0.1,
help="TE optimisation consolidation timeout (defaults 1.0)"),
cfg.StrOpt("opti_method", default="FirstSol",
help="TE Opti Method: FirstSol (default), BestSolUsage, BestSolPLen, CSPFRecomp"),
cfg.BoolOpt("candidate_sort_rev", default=True,
help="Consider src-dest pairs (candidates) in decending order"),
cfg.BoolOpt("pot_path_sort_rev", default=False,
help="Reverse potential path set. Only applies to some opti methods!")
], group="te")
# Instantiate topo discovery module instance
app_mngr = app_manager.AppManager.get_instance()
# If multi-controller communications have been disabled make sure
# topo discovery is not paused by default. Promoet to master is not
# called
if not self.CONF.multi_ctrl.start_com:
self.logger.warning("Multi-controller communications turned off!")
self.logger.warning("\tCtrlCom module init but not started")
self.logger.warning("\tTopology discovery is started by default")
self.logger.warning("\tMultiple instances disabled (no election)")
app_mngr.instantiate(SwitchesDiscovery, pause_detection=False)
else:
app_mngr.instantiate(SwitchesDiscovery)
# Initiate default attributes
self.graph = Graph()
self.hosts = []
self.__stats_timer = None
self.__rebuild_state_timeout = 0
self.__rebuild_state_sw = {}
self.__ctrl_role = "unknown"
self.unknown_links = {}
self.__unknown_links_timer = None
self.__ing_change_detect_wait = {}
self.__cleanup_handlers = []
# Inter-controller communication module initiation
self.ctrl_com = ControllerCommunication(self.logger, self,
dom_id=self.CONF.multi_ctrl.domain_id)
if self.CONF.multi_ctrl.start_com:
if self.CONF.multi_ctrl.inst_id is None:
self.ctrl_com.start()
else:
self.ctrl_com.start(self.CONF.multi_ctrl.inst_id)
else:
# Do not start CtrlCom module and force ctrl role to master
self.logger.warning("\tController will start in master role")
self.__ctrl_role = "master"
# If inter-controller communications disabled and TE enabled, trigger
# the stats timmer. Normally `promote_master()` will start the timmer
# when the instance becomes master. Because inter-ctrl coms are
# disabled, no leader elections => method never triggers!
if not self.CONF.multi_ctrl.start_com and self.CONF.stats.collect == True:
self.logger.info("\tStarting stats collection (ctrl-com off)!")
self.__trigger_stats_timer()
# Output the multi-controller config attributes
self.logger.info("-" * 50)
self.logger.info("Multi-Controller Config:")
self.logger.info("\tdomain_id: %s" % self.CONF.multi_ctrl.domain_id)
if self.CONF.multi_ctrl.inst_id is not None:
self.logger.info("\tinst_id: %s" % self.CONF.multi_ctrl.inst_id)
self.logger.info("\tstart_com: %s" % self.CONF.multi_ctrl.start_com)
# TE optimisation module initiation
self.logger.info("TE Config:")
self.logger.info("\tutilisation_threshold: %s" % self.CONF.te.utilisation_threshold)
self.logger.info("\tconsolidate_time: %s" % self.CONF.te.consolidate_time)
self.logger.info("\topti_method: %s" % self.CONF.te.opti_method)
self.logger.info("\tcandidate_sort_rev: %s" % self.CONF.te.candidate_sort_rev)
self.logger.info("\tpot_path_sort_rev: %s" % self.CONF.te.pot_path_sort_rev)
self.TE = TEOptimisation(self, self.CONF.te.utilisation_threshold,
self.CONF.te.consolidate_time,
opti_method = self.CONF.te.opti_method,
candidate_sort_rev = self.CONF.te.candidate_sort_rev,
pot_path_sort_rev = self.CONF.te.pot_path_sort_rev)
# If we are collecting stats bind the output signal handler
self.logger.info("Stats Config:")
self.logger.info("\tcollect_stats: %s" % self.CONF.stats.collect)
if self.CONF.stats.collect == True:
self.logger.info("\tcollect_port_stats: %s" % self.CONF.stats.collect_port)
self.logger.info("\tout_port: %s" % self.CONF.stats.out_port)
self.logger.info("\tstats_interval: %s" % self.CONF.stats.interval)
self.logger.info("\tSignal for stats: kill -USR1 %s" % os.getpid())
signal.signal(signal.SIGUSR1, self.__stats_signal)
# Output the config end indicator
self.logger.info("-" * 50)
# Process the static port description file
if not self.CONF.application.static_port_desc == "":
dat = {}
# Try to load the file relative to the working dir, if that fails
# try to load it relative to the config file parent directory
if not os.path.isfile(self.CONF.application.static_port_desc):
tmp_path = os.path.dirname(self.CONF.config_file[0])
self.CONF.application.static_port_desc = os.path.join(tmp_path,
self.CONF.application.static_port_desc)
self.logger.info("static_port_desc: %s" % self.CONF.application.static_port_desc)
try:
with open(self.CONF.application.static_port_desc) as f:
csv_reader = csv.DictReader(f)
for line in csv_reader:
src = int(line["dpid"])
port = int(line["port"])
speed = int(line["speed"])
if src not in dat:
dat[src] = {}
dat[src][port] = speed
self.graph.fixed_speed = dat
self.logger.info("PORT DESC DICT: %s" % self.graph.fixed_speed)
except Exception as e:
self.logger.error("Error occured while reading static port description")
self.logger.error(e)
def register_cleanup(self, method):
""" Register a new cleanup method to execute on controller stop """
self.__cleanup_handlers.append(method)
def get_topo(self):
""" Return the gurrent network topology
Returns:
(obj): Topology of the network
"""
return self.graph
def get_paths(self):
""" Get the currently installed paths
Returns:
(dict): Currently installed paths dictionary
"""
return self.paths
def get_poll_rate(self):
""" Get the current stats poll interval in seconds
Returns:
(int): Poll rate in seconds
"""
return self.CONF.stats.interval
def __stop_stats_timer(self):
""" Stop any in progress stats poll interval """
if self.__stats_timer is not None:
self.__stats_timer.cancel()
self.__stats_timer = None
def __trigger_stats_timer(self):
""" Start (or reset) the stats poll interval """
self.__stop_stats_timer()
self.__stats_timer = Timer(self.get_poll_rate(), self.__get_stats)
self.__stats_timer.start()
def __get_stats(self):
""" Request port and flow stats for all connected switches. """
self.__stats_timer = None
self.logger.info("Sending stats request to connected switches")
for sw in get_switch(self):
dp = sw.dp
ofp = dp.ofproto
parser = dp.ofproto_parser
match = parser.OFPMatch()
# Request the port and datapath stats
req = parser.OFPFlowStatsRequest(dp, 0, ofp.OFPTT_ALL, ofp.OFPP_ANY,
ofp.OFPG_ANY, 0, 0, match)
dp.send_msg(req)
# Check if we have to collect port stats
if self.CONF.stats.collect_port == True:
req = parser.OFPPortStatsRequest(dp, 0, ofp.OFPP_ANY)
dp.send_msg(req)
self.logger.debug("Request stats from switch with DPID %s" % dp.id)
# Reset the stats request timer to re-trigger
self.__trigger_stats_timer()
def __stats_signal(self, signum, stack):
""" Stats signal handler that outputs collected statistics """
# Create format string used to output values and output headers
fstr = "{:^16} {:>3} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6} {:>6}"
self.logger.info(fstr.format("Path(src, dst)", "gid", "pkt", "bt", "t_pkt",
"t_bt", "time", "pkt/s", "bt/s",
"tpkt/s", "tbt/s"))
self.logger.info(fstr.format("----------------", "---", "------", "------", "------",
"------", "------", "------", "------",
"------", "------"))
# Iterate through the paths and output the stats counts
for key,val in self.paths.iteritems():
gid = "na" if "gid" not in val else val["gid"]
stats = {} if "stats" not in val else val["stats"]
total_time = "na" if "total_time" not in stats else stats["total_time"]
pkts = "na" if "pkts" not in stats else stats["pkts"]
bytes = "na" if "bytes" not in stats else stats["bytes"]
total_pkts = "na" if "total_pkts" not in stats else stats["total_pkts"]
total_bytes = "na" if "total_bytes" not in stats else stats["total_bytes"]
pkts_persec = "na" if "pkts_persec" not in stats else stats["pkts_persec"]
bytes_persec = "na" if "bytes_persec" not in stats else stats["bytes_persec"]
total_pkts_persec = ("na" if "total_pkts_persec" not in stats else
stats["total_pkts_persec"])
total_bytes_persec = ("na" if "total_bytes_persec" not in stats else
stats["total_bytes_persec"])
self.logger.info(fstr.format(
key, gid, __hum_read(pkts), __hum_read(bytes), __hum_read(total_pkts),
__hum_read(total_bytes), __hum_read(total_time), __hum_read(pkts_persec),
__hum_read(bytes_persec), __hum_read(total_pkts_persec),
__hum_read(total_bytes_persec)))
self.logger.info("")
if self.CONF.stats.out_port == False:
return
# Iterate through all switches in topo and output port stats of each
for dpid,dpid_val in self.graph.topo.iteritems():
# Ignore showing stats for the host switches (as none exist)
if -1 in dpid_val:
continue
self.logger.info("DPID: %s" % dpid)
for pn,pn_val in dpid_val.iteritems():
self.logger.info("\t+ PORT: %s, SPEED: %sb" % (pn, __hum_read(pn_val["speed"])))
# If there are no poll stats don't output
if "poll_stats" in pn_val:
pstV = pn_val["poll_stats"]
self.logger.info("\t| tx_packets: %s, tx_bytes: %sB, tx_errors: %s" % (
__hum_read(pstV["tx_packets"]), __hum_read(pstV["tx_bytes"]),
__hum_read(pstV["tx_errors"])))
self.logger.info("\t| tx_rate: %s" % (pstV["tx_rate"]))
# If there are no total stats don't output
if "total_stats" in pn_val:
pstV = pn_val["total_stats"]
self.logger.info("\t| TOTAL tx_packets: %s, tx_bytes: %sB, tx_errors: %s"
% (__hum_read(pstV["tx_packets"]), __hum_read(pstV["tx_bytes"]),
__hum_read(pstV["tx_errors"])))
# Output a new line to make things cleaner
self.logger.info("")
def __stop_unknown_links_timer(self):
""" Stop the unknown links timer """
if self.__unknown_links_timer is not None:
self.__unknown_links_timer.cancel()
self.__unknown_links_timer = None
def __trigger_unknown_links_timer(self):
""" Start the unknown links timmer for CID resolution """
self.__stop_unknown_links_timer()
self.__unknown_links_timer = Timer(1, self.__unknown_links_loop)
self.__unknown_links_timer.start()
def __unknown_links_loop(self):
""" unknown links resolution callback. For every unresolved link, where the standown
period is exceeded (10 timer ticks), the CID is request from the root controller.
If no more unresolved unknown links exist, timer is stopped, otherwise its restarted
automatically.
"""
# Clear the timer and process the unknown links
self.__unknown_links_timer = None
in_progress = False
for key in self.unknown_links:
if isinstance(self.unknown_links[key], list):
in_progress = True
if self.unknown_links[key][0] < 10:
# If we are in a standown period just increment counter
self.unknown_links[key][0] += 1
else:
# Resolve the unknown links CID
self.unknown_links[key] = [0]
(src_sw, src_pn, dst_sw) = key
src_port = self.graph.get_port_info(src_sw, src_pn)
speed = 0
if src_port is not None:
speed = src_port["speed"]
else:
self.logger.error("Inter-domain link %s %s has no speed!" % (src_sw, src_pn))
continue
self.ctrl_com.notify_outside_link(src_sw, src_pn, dst_sw, speed)
# If we still have unknown links that need to be resolved reset the timer
if in_progress:
self.__trigger_unknown_links_timer()
def _init_ing_change_wait(self, hkey):
""" Initiate a lockut timer for ingress change detection to prevent triggering back and
forth swapping due to in-flight packets that are being dequed.
Args:
hkey (tuple): Path pair key
"""
timer = Timer(2, self.__exp_ing_change_wait, args=hkey)
if self._is_ing_change_wait(hkey):
# Stop in progress ingress change instance and restart
self.__ing_change_detect_wait[hkey].cancel()
self.__ing_change_detect_wait[hkey] = timer
timer.start()
def __exp_ing_change_wait(self, h1, h2):
""" Callback executed by the ingress change wait timer. Remove the path from wait list.
Args:
h1 (str): First host in pair key
h2 (str): Second host in pair key
"""
hkey = (h1, h2)
del self.__ing_change_detect_wait[hkey]
self.logger.info("Ingress Change Wait Expired for %s-%s" % hkey)
def __clear_ing_change_wait(self):
""" Stop all in progress ingress change detection wait timer """
for hkey, timer in self.__ing_change_detect_wait.items():
timer.stop()
del self.__ing_change_detect_wait[hkey]
self.__ing_change_detect_wait = {}
def _is_ing_change_wait(self, hkey):
""" Check if the path pair is in the ingress change wait phase.
Args:
hkey (tuple): Host pair key of path
Returns:
bool: True if the path is in the wait pahse, False otherwise.
"""
return hkey in self.__ing_change_detect_wait
def __save_port_speed(self, dp, p):
""" Process a port info object and extract the speed of the port """
# Check if this is not a local port
# XXX: OFP has a limit of local ports of OFPP_MAX, if we have
# a port over this, we have a OFP specific port
if p.port_no > dp.ofproto.OFPP_MAX:
return
# Convert the ryu speed to bits from kbits and save it
p_speed = p.curr_speed * 1000
# XXX FIXME: This is a dirty hack to deal with mininet links always being 10G and
# not being able to specify TC limit of 10G (link can handle more 50G). If the link
# is 10G then set it toa speed of 1G
if p_speed >= 10000000000:
p_speed = 1000000000
self.graph.update_port_info(dp.id, p.port_no, speed=p_speed)
def stop(self):
""" Callback triggered on controller stop. Stop the inter ctrl com and timers """
super(TopoDiscoveryController, self).stop()
self.__stop_stats_timer()
self.__stop_unknown_links_timer()
self.__clear_ing_change_wait()
self.ctrl_com.stop()
self.logger.info("\n\nRunning cleanup handlers ...\n\n")
for func in self.__cleanup_handlers:
func()
# ------------------------- RYU API METHODS --------------------------
@set_ev_cls(event.EventSwitchEnter)
@set_ev_cls(event.EventSwitchReconnected)
def switch_enter_handler(self, ev):
""" Handler called on switch enter/reconnect. Assign switch current
controller role `:cls:attr:(__ctrl_role)`. If current role is master, on
role reply, controller will automatically initiate state rebuild from
switch.
"""
dp = ev.switch.dp
self.logger.info("SW %s has entered topo at %f" % (dp.id, time.time()))
self.logger.critical("XXXEMUL,%f,sw_enter,%s" % (time.time(), dp.id))
# Request port description of switch to get port capacity
self._req_port_desc(dp)
# Send current controller role to switch and apply barrier
if not self.__ctrl_role == "unknown":
self.__send_ctrl_role(dp, self.__ctrl_role)
self._send_barrier(dp)
@set_ev_cls(event.EventLinkAdd)
def event_link_add_handler(self, ev):
""" Handler called when a new link is added. Add the link to the topology and trigger
``topo_change`` if graph modified.
"""
src_sw = ev.link.src.dpid
dst_sw = ev.link.dst.dpid
src_pn = ev.link.src.port_no
dst_pn = ev.link.dst.port_no
self.logger.info("Link added %s(%s) to %s(%s)" % (src_sw, src_pn, dst_sw, dst_pn))
modified = False
if self.graph.add_link(src_sw, dst_sw, src_pn, dst_pn) == True:
modified = True
if self.graph.add_link(dst_sw, src_sw, dst_pn, src_pn) == True:
modified = True
if modified == True:
self.topo_changed()
@set_ev_cls(event.EventLinkDelete)
def event_link_delete_handler(self, ev):
""" Handler called when a link is removed. Remove the link from the topology and
trigger ``topo_change`` if graph modified. If the controller uses protection and
optimise_protection is false, ``topo_change`` is not triggered.
"""
src_sw = ev.link.src.dpid
dst_sw = ev.link.dst.dpid
src_pn = ev.link.src.port_no
dst_pn = ev.link.dst.port_no
self.logger.info("Link del %s(%s) to %s(%s)" % (src_sw, src_pn, dst_sw, dst_pn))
# Remove the link that was deleted from the model
modified = False
if self.graph.remove_port(src_sw, dst_sw, src_pn, dst_pn):
modified = True
if self.graph.remove_port(dst_sw, src_sw, dst_pn, dst_pn):
modified = True
# Check if protection optimisation is disabled
if self.CONTROLLER_NAME == "PROACTIVE" and self.CONF.application.optimise_protection == False:
# XXX: We could consider unregistering the handler at this
# point as all further requests to the link delete will result
# in it stopping early. This may not be esential though.
return
# If a topo modification occured start topo timer to optimise path
if modified == True:
self.topo_changed()
@set_ev_cls(event.EventHostAdd)
def event_host_add_handler(self, ev):
""" Handler called on new host add. Add host to the topology and store details. """
host = ev.host
host_name = host.name
dst_sw = host.port.dpid
dst_pn = host.port.port_no
src_addr = host.ipv4[0]
src_eth = host.mac
self.logger.info("Host link added %s to %s(%s)" % (host_name, dst_sw, dst_pn))
modified = False
if self.graph.add_link(host_name, dst_sw, -1, dst_pn) == True:
modified = True
if self.graph.add_link(dst_sw, host_name, dst_pn, -1) == True:
modified = True
self.logger.debug("Host Address is IP: %s, ETH: %s" % (src_addr, src_eth))
self.graph.update_port_info(host_name, -1, addr=src_addr, eth_addr=src_eth)
# Append host name to list (if not exist) and trigger topo change if topo modified
if host_name not in self.hosts:
self.hosts.append(host_name)
if modified == True:
self.topo_changed()
@set_ev_cls(event.EventInterDomLinkAdd)
def event_inter_dom_link_add_handler(self, ev):
""" Handler called on new inter domain link add. Notify the root controller of the link and
initiate CID resolution for link.
"""
src_sw = ev.link.src.dpid
dst_sw = ev.link.dst.dpid
src_pn = ev.link.src.port_no
dst_pn = ev.link.dst.port_no
self.logger.info("Inter domain link added %s(%s) to %s(%s)" % (src_sw, src_pn, dst_sw, dst_pn))
# If the link already added, ignore the reequest
key = (src_sw, src_pn, dst_sw)
if key in self.unknown_links:
return
# Add the details of the unkown links and stop operation if not master
self.unknown_links[key] = [0]
if not self.is_master():
self.logger.info("Controller is not master, supress unknown link")
return
# Initiate unkown links timer, get port speed and notify root controller
if self.__unknown_links_timer is None:
self.__trigger_unknown_links_timer()
src_port = self.graph.get_port_info(src_sw, src_pn)
speed = 0
if src_port is not None:
speed = src_port["speed"]
else:
return
self.ctrl_com.notify_outside_link(src_sw, src_pn, dst_sw, speed)
@set_ev_cls(event.EventInterDomLinkDelete)
def event_inter_dom_link_delete_handler(self, ev):
""" Handler called on inter domain link remove. Notify the root controller of the removed link """
src_sw = ev.link.src.dpid
dst_sw = ev.link.dst.dpid
src_pn = ev.link.src.port_no
dst_pn = ev.link.dst.port_no
self.logger.info("Inter domain link deleted %s(%s) to %s(%s)" % (src_sw, src_pn, dst_sw, dst_pn))
key = (src_sw, src_pn, dst_sw)
if key not in self.unknown_links:
return
self.ctrl_com.notify_inter_domain_port_down(key)
@set_ev_cls(event.EventHostDelete)
def event_host_delete_handler(self, ev):
""" Handler called on host delete. Remove host from topology and delete details. """
host = ev.host
host_name = host.name
dst_sw = host.port.dpid
dst_pn = host.port.port_no
src_addr = host.ipv4[0]
src_eth = host.mac
self.logger.info("Host link Deleted %s to %s(%s)" % (host_name, dst_sw, dst_pn))
del_host = self.graph.remove_host_link(dst_sw, dst_pn)
if del_host is not None:
if del_host in self.hosts:
self.hosts.remove(del_host)
self.topo_changed()
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
""" Handler called on packet in received by controller. Method implements ingress change
detection for inter-domain paths.
Args:
ev (ofp_event.EventOFPPacketIn): Packet in event from the controller.
"""
msg = ev.msg
pkt = packet.Packet(msg.data)
i = iter(pkt)
eth_pkt = six.next(i)
if not type(eth_pkt) == ethernet.ethernet:
return
# Is this an ingress change for a inter-domain link?
next_pkt = six.next(i)
if type(next_pkt) == vlan.vlan:
vid = next_pkt.vid
sw = msg.datapath.id
pn = msg.match["in_port"]
self.logger.info("INGRESS_CHANGE_DETECT_PKT")
self._ingress_change(vid, sw, pn)
return
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def port_status_handler(self, ev):
""" Handler called on port status change. On status add get and save the ports speed """
dp = ev.msg.datapath
ofp = dp.ofproto
# Check if port state changed to up
if (ev.msg.reason == ofp.OFPPR_ADD or (ev.msg.reason == ofp.OFPPR_MODIFY and
ev.msg.desc.state == ofp.OFPPS_LIVE)):
self.__save_port_speed(ev.msg.datapath, ev.msg.desc)
@set_ev_cls(ofp_event.EventOFPFlowStatsReply, MAIN_DISPATCHER)
def flow_stats_reply_handler(self, ev):
""" Handler called on flow stats reply from a switch. Calls ``_process_flow_stats``. """
dp = ev.msg.datapath
body = ev.msg.body
self.logger.debug("FlowStats received from SW DPID:%s" % dp.id)
self._process_flow_stats(dp, body)
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def port_stats_reply_handler(self, ev):
""" Handler called on port stats reply from switch. Get port stats
and extract relevant metrics from counters. Reserved ports (number
> OFPP_MAX) are ignored.
"""
self.logger.debug("PortStats received from SW DPID:%s" %
ev.msg.datapath.id)
for p in ev.msg.body:
# Check if this is not a local port
# XXX: OFP has a limit of local ports of OFPP_MAX, if we have
# a port over this, we have a OFP specific port
if p.port_no > ev.msg.datapath.ofproto.OFPP_MAX:
continue
# If port is not in topology just jump to the next one
pInfo = self.graph.get_port_info(ev.msg.datapath.id, p.port_no)
if pInfo is None:
continue
old = pInfo["total_stats"] if "total_stats" in pInfo else None
# If we have no previous count info do not compute poll metric
if (old is not None and
old["rx_packets"] is not None and
old["rx_bytes"] is not None and
old["rx_errors"] is not None and
old["tx_packets"] is not None and
old["tx_bytes"] is not None and
old["tx_errors"] is not None
):
# Compute the stat counts and rates for the current poll
rx_packets = p.rx_packets - old["rx_packets"]
rx_bytes = p.rx_bytes - old["rx_bytes"]
rx_errors = p.rx_errors - old["rx_errors"]
tx_packets = p.tx_packets - old["tx_packets"]
tx_bytes = p.tx_bytes - old["tx_bytes"]
tx_errors = p.tx_errors - old["tx_errors"]
rx_rate = None
tx_rate = None
if not pInfo["speed"] == 0:
# XXX: The (tx/rx)_bytes is in bytes while the max speed
# is in bits so convert (8x numerator). Compute the
# average per second value (account for the poll rate)
conv = 8.0 / self.get_poll_rate()
rx_rate = round(float(rx_bytes*conv)/float(pInfo["speed"]), 2)
tx_rate = round(float(tx_bytes*conv)/float(pInfo["speed"]), 2)
# Is this a inter-domain link we requested a root
# ctrl optimisation?
skip_check = False
key = (ev.msg.datapath.id, p.port_no)
if key in self.TE.inter_domain_over_util:
val = self.TE.inter_domain_over_util[key]
self.logger.info("Inter-domain link opti request still"
" outstanding (count %s)" % val)
if val <= 0:
# Wait counter elapsed, allow checking if link
# congested
del self.TE.inter_domain_over_util[key]
else:
# Decrement the counter, link optimisation still
# in progress
val -= 1
self.TE.inter_domain_over_util[key] = val
skip_check = True
if skip_check:
self.logger.info("Suppressed con check for idl")
else:
# Check if the link is congested
self.TE.check_link_congested(
ev.msg.datapath.id, p.port_no, tx_rate
)
# Notify the root controller of idl traffic
if self.is_inter_domain_link(ev.msg.datapath.id, p.port_no):
self.ctrl_com.notify_inter_domain_link_traffic(
ev.msg.datapath.id, p.port_no, (tx_bytes * conv)
)
# Update the port counts for the poll interval
self.graph.update_port_info(ev.msg.datapath.id, p.port_no,
rx_packets=rx_packets, rx_bytes=rx_bytes,
rx_errors=rx_errors, tx_packets=tx_packets,
tx_bytes=tx_bytes, tx_errors=tx_errors,
rx_rate=rx_rate, tx_rate=tx_rate,
is_total=False)
# Update the port total counters
self.graph.update_port_info(ev.msg.datapath.id, p.port_no,
rx_packets=p.rx_packets, rx_bytes=p.rx_bytes,
rx_errors=p.rx_errors, tx_packets=p.tx_packets,
tx_bytes=p.tx_bytes, tx_errors=p.tx_errors,
is_total=True)
@set_ev_cls(ofp_event.EventOFPPortDescStatsReply, MAIN_DISPATCHER)
def port_desc_stats_reply_handler(self, ev):
""" Handler called on port description received from switch. Extract and save port
speed (given in kbits) to topology. Non local ports (number > OFPP_MAX) are ingnored.
"""
self.logger.debug("PortDesc received from SW DPID:%s" % ev.msg.datapath.id)
for p in ev.msg.body:
self.__save_port_speed(ev.msg.datapath, p)
@set_ev_cls(ofp_event.EventOFPGroupDescStatsReply, MAIN_DISPATCHER)
def group_desc_reply_handler(self, ev):
""" Handler called on group description reply received from switch. Calls
``_process_group_desc`` to recover state from group entries.
"""
self._process_group_desc(ev.msg.datapath, ev.msg.body)
@set_ev_cls(ofp_event.EventOFPErrorMsg, MAIN_DISPATCHER)
def error_msg_handler(self, ev):
""" Handler called on OFP error message received from switch. Code is copied from the ryu
module `controller/ofp_handeler.py` line 269. Code outputs any errors message received.
Original code logged debug messages, which outputs a lot of garbage. To avoid using level
DEBUG, message changes log severity to info.
"""
msg = ev.msg
ofp = msg.datapath.ofproto
self.logger.info(
"EventOFPErrorMsg received.\n"
"version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
" `-- msg_type: %s",
hex(msg.version), hex(msg.msg_type), hex(msg.msg_len),
hex(msg.xid),
ofp.ofp_msg_type_to_str(msg.msg_type))
if msg.type == ofp.OFPET_EXPERIMENTER:
self.logger.info(
"OFPErrorExperimenterMsg(type=%s, exp_type=%s,"
" experimenter=%s, data=b'%s')",
hex(msg.type), hex(msg.exp_type),
hex(msg.experimenter), utils.binary_str(msg.data))
else:
self.logger.info(
"OFPErrorMsg(type=%s, code=%s, data=b'%s')\n"
" |-- type: %s\n"
" |-- code: %s",
hex(msg.type), hex(msg.code), utils.binary_str(msg.data),
ofp.ofp_error_type_to_str(msg.type),
ofp.ofp_error_code_to_str(msg.type, msg.code))
if msg.type == ofp.OFPET_HELLO_FAILED:
self.logger.info(
" `-- data: %s", msg.data.decode('ascii'))
elif len(msg.data) >= ofp.OFP_HEADER_SIZE:
(version, msg_type, msg_len, xid) = ofproto_parser.header(msg.data)
self.logger.info(
" `-- data: version=%s, msg_type=%s, msg_len=%s, xid=%s\n"
" `-- msg_type: %s",
hex(version), hex(msg_type), hex(msg_len), hex(xid),
ofp.ofp_msg_type_to_str(msg_type))
else:
self.logger.warning(
"The data field sent from the switch is too short: "
"len(msg.data) < OFP_HEADER_SIZE\n"
"The OpenFlow Spec says that the data field should contain "
"at least 64 bytes of the failed request.\n"
"Please check the settings or implementation of your switch.")
@set_ev_cls(ofp_event.EventOFPRoleReply, MAIN_DISPATCHER)
def role_reply_handler(self, ev):
""" Handler called on a role change reply from a switch. If the switch
role is master, initiate state rebuild for the switch by calling
``_request_sw_state``.
"""
dp = ev.msg.datapath
ofp = dp.ofproto
role = ""
if ev.msg.role == ofp.OFPCR_ROLE_MASTER:
role = "master"
# If swith role was set to master, request its current state
self._request_sw_state(dp)
if ev.msg.role == ofp.OFPCR_ROLE_EQUAL:
role = "equal"
elif ev.msg.role == ofp.OFPCR_ROLE_MASTER:
role = "master"
elif ev.msg.role == ofp.OFPCR_ROLE_SLAVE:
role = "slave"
self.logger.info('OFPRoleReply received: role=%s gen_id=%d dpid=%s',
role, ev.msg.generation_id, dp.id)
# -------------------------- HELPER METHODS --------------------------
def _get_gid(self, host_1, host_2):
""" Compute a unique GID for a host pair. Method calls the static ``get_gid``
with an approrpriate number of host value.
Args:
host_1 (str): First host name
host_2 (str): Second host name
Returns:
int: GID for host pair
"""
n = 64
return TopoDiscoveryController.get_gid(host_1, host_2, n)
def _get_reverse_gid(self, gid):
""" Compute the host pair for a GID. Method calls the static ``get_reverse_gid``.
Args:
gid (int): GID to recover switch pair of
Returns:
(str, str): Host pair if GID is valid or None.
"""