-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
2389 lines (1788 loc) · 65.9 KB
/
Controller.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
# Copyright 2017 Federica Mesolella
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
from pox.core import core
from pox.lib.revent import EventRemove
from pox.lib.addresses import IPAddr,EthAddr
from pox.lib.util import dpid_to_str
from pox.lib.util import str_to_bool
from pox.lib.packet.arp import arp
from pox.lib.packet.ipv4 import ipv4
from pox.openflow.of_json import *
from pox.lib.recoco import Timer
from pox.lib.revent import *
from collections import defaultdict
from threading import Thread, Lock
from pox.openflow.discovery import Discovery
import pox.openflow.libopenflow_01 as of
import pox.openflow.nicira as nx
import pox.lib.packet as pkt
import networkx as NX
import socket
import time
import sys
import math
import json
import signal
import threading
#count the number of matching rules inserted in switch S
num_ruleS=0
#count the number of matching rules inserted in switch T
num_ruleT=0
#variable to store the old value of dpid
old=0
#logger
log = core.getLogger()
#delay
_flood_delay = 0
#flag in the set function
flag_set=1
#flag in the match function
flag_match_S=0
#flag in the match function
flag_match_T=0
#flag in the read counter function
flag_read=0
#flag in the forwarding_high_priority function
flag_forward_hp=0
#flag in the forwarding_low_priority function
flag_forward_lp1=0
flag_forward_lp2=0
#flag in the set_port_in_TOS function
flag_set_port=0
#flag in the create_flow_stats_list function
flag_flow_stats=0
#counter in the set function
C_set=0
#counter in the match function
C_match_S=0
#counter in the match function
C_match_T=0
#counter in the read counter function
C_read=0
#counter in the get_labels function
C_get_labels=0
#counter in the forwarding_high_priority function
C_forward_hp=0
#counter in the forwarding_low_priority function
C_forward_lp1=0
C_forward_lp2=0
#counter in the set_port_in_TOS function
C_set_port=0
#counter in the create_flow_stats_list function
C_flow_stats=0
C_flow_stats_1=0
#counter used in the case of reading of flag 0 in the create_flow_stats_list function
cont1=0
#counter used in the case of reading of flag 1 in the create_flow_stats_list function
cont2=0
#counter in the link_event function
C_link_event=0
#costant to define the number of switch S in the topology
N=40
#costant useful to verify the end of link detection depends on the value of N
M=202
#costant variable used to create the mac address
Z=10
#variable used to read values foreach switch connected
VAR=0
#variable used to run once
run_once=0
#variables used for traffic monitoring
ip_src=0
ip_dst=0
mac_src=0
mac_dst=0
#variable used for traffic monitoring
var1=0
var2=0
var3=0
var4=0
#variables used to get switch informations
switch1_dpid=0
switch2_dpid=0
switch1_port=0
switch2_port=0
#array with port numbers
Array=[]
#array with shortest path foreach switch
AllShortPath=[]
#graph
G=[]
G = NX.Graph()
#variables with the sum of counters of all switch in the topology
sum_output1_port52=0
sum_input1_port52=0
sum_output0_port52=0
sum_input0_port52=0
#array with the counters in input and output of 52 port
Count_in1_PORT52=[0]*41
Count_out1_PORT52=[0]*41
Count_in0_PORT52=[0]*41
Count_out0_PORT52=[0]*41
#array with the counters in input and output .... 41x41=1681
Count_in1_PORTS=[0]*1681
Count_out1_PORTS=[0]*1681
Count_in0_PORTS=[0]*1681
Count_out0_PORTS=[0]*1681
#array with the partial sum
somma_parziale_IN_0=[0] * 1681
somma_parziale_OUT_0=[0] * 1681
somma_parziale_IN_1=[0] * 1681
somma_parziale_OUT_1=[0] * 1681
#count the number of low priority rules inserted in all switch in the topology
count_rules_low_priority=0
Array_Country=[0]*41
dict1={}
dict2={}
#dictiionary with the Results printed on a json file
Results=[]
#costants
port52=52
bit_value0=0
bit_value1=1
direct1="output"
direct2="input"
periodDiz=0
period=0
class LearningSwitch (object):
global G
def __init__ (self, connection, transparent):
self.connection = connection
self.transparent = transparent
connection.addListeners(self)
self.hold_down_expired = _flood_delay == 0
#set flag TOS to 0 or 1 every T=N seconds
self._set_flag(60)
#read the counters every T=N/2 seconds
self._read_counters(30)
#just once insert in the switches S the rules to match the packets with flag set to 0 or 1
self._match_flag_S(40)
#just once insert in the switches T the rules to forward packets
self._forwarding_T(40)
#just once insert in the switches S the rules in order to set the port number in the TOS field
self._set_port_in_TOS_field(30)
#just once insert in the switches S the forwarding rules with low priority
self._forwarding_low_priority(25)
#get the labels of all nodes in the topology
self._get_labels_from_topology(20)
#just once insert in the switches S the forwarding rules with high priority
self._forwarding_high_priority(35)
def _set_flag(self, dt):
#every T=N second set bit TOS to 1 or 0
Timer(dt, self.set_bit, recurring=True)
def _match_flag_S(self, dt):
#Just once insert in the switches S the rules to match the bit/flag to 0 and 1
Timer(dt, self.match_bit_S,recurring=False)
def _forwarding_T(self, dt):
#Just once insert in the switches T the rules to match the bit/flag to 0 and 1
Timer(dt, self.forwarding_lowp_T,recurring=False)
def _read_counters(self,dt):
#Listen for flow stats
core.openflow.addListenerByName("FlowStatsReceived",create_flow_stats_list)
#every T=N/2 seconds read the counters
Timer(dt, self.read_counter, recurring=True)
def _set_port_in_TOS_field(self,dt):
#just once insert in the switches S the rules in order to set the port number in the TOS field
Timer(dt, self.set_port_in_TOS, recurring=False)
def _forwarding_low_priority(self, dt):
#just once insert in the switches S the forwarding rules with low priority
Timer(dt, self.forwarding_low_p, recurring=False)
def _get_labels_from_topology(self,dt):
#get the labels of all nodes in the topology
Timer(dt, self.get_labels_nodes, recurring=False)
def _forwarding_high_priority(self,dt):
#just once insert in the switches S the forwarding rules with high priority
Timer(dt, self.forwarding_high_p, recurring=False)
#rules forwarding low priority in switch T
def forwarding_rules_lowpriority_switchT(self):
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=1
msg.priority = 1
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=2
msg.priority = 1
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.actions.append(of.ofp_action_output(port = 1))
self.connection.send(msg)
#rules setting flag in swicth T
def setting_flag_rule_in_switch_T_macs_macd_ips_ipd(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
def setting_flag_rule_in_switch_T_macs_macd(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
def setting_flag_rule_in_switch_T_ips_ipd(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
def setting_flag_rule_in_switch_T_macs_ips(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
def setting_flag_rule_in_switch_T_macd_ipd(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
def setting_flag_rule_in_switch_T(self,tos):
t=tos
msg = nx.nx_flow_mod()
msg.command = of.OFPFC_MODIFY
msg.table_id = 0
msg.match.in_port=1
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_ip_proto = 17
msg.actions.append(of.ofp_action_nw_tos(nw_tos=t))
msg.actions.append(of.ofp_action_output(port = 2))
self.connection.send(msg)
#rules matching flag in swicth S
def matching_flag_rule_in_switchS_macs_macd_ips_ipd(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def matching_flag_rule_in_switchS_macs_macd(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def matching_flag_rule_in_switchS_ips_ipd(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def matching_flag_rule_in_switchS_macs_ips(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_ip_proto = 17
msg.match.of_ip_src = IPAddr(ip_src)
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def matching_flag_rule_in_switchS_macd_ipd(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_dst)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_dst)
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def matching_flag_rule_in_switchS(self,port,tos):
t=tos
p=port
msg = nx.nx_flow_mod()
msg.table_id = 0
msg.match.in_port=p
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_ip_proto = 17
msg.match.of_ip_tos=t
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 2))
self.connection.send(msg)
def set_bit(self):
if self.connection.dpid > 100:
localtime = time.asctime( time.localtime(time.time()) )
global flag_set
global N
global C_set
global VAR
global ip_src, ip_dst, mac_src, mac_dst_
global var1,var2,var3,var4
C_set=C_set+1
msg = nx.nx_flow_mod_table_id()
self.connection.send(msg)
if C_set==1:
VAR=1
if flag_set==0:
flag_set=1
elif flag_set==1:
flag_set=0
if flag_set==0:
if var1==1 and var2==1 and var3==1 and var4==1:
tos=0x04
self.setting_flag_rule_in_switch_T_macs_macd_ips_ipd(tos)
elif var1==1 and var2==1:
tos=0x04
self.setting_flag_rule_in_switch_T_ips_ipd(tos)
elif var3==1 and var4==1:
tos=0x04
self.setting_flag_rule_in_switch_T_macs_macd(tos)
elif var1==1 and var3==1:
tos=0x04
self.setting_flag_rule_in_switch_T_macs_ips(tos)
elif var2==1 and var4==1:
tos=0x04
self.setting_flag_rule_in_switch_T_macd_ipd(tos)
else:
print "Insert rules of setting in switch T start at : ",localtime," Switch :",self.connection.dpid,"Count : ",C_set
tos=0x04
self.setting_flag_rule_in_switch_T(tos)
elif flag_set==1:
if var1==1 and var2==1 and var3==1 and var4==1:
tos=0
self.setting_flag_rule_in_switch_T_macs_macd_ips_ipd(tos)
elif var1==1 and var2==1:
tos=0
self.setting_flag_rule_in_switch_T_ips_ipd(tos)
elif var3==1 and var4==1:
tos=0
self.setting_flag_rule_in_switch_T_macs_macd(tos)
elif var1==1 and var3==1:
tos=0
self.setting_flag_rule_in_switch_T_macs_ips(tos)
elif var2==1 and var4==1:
tos=0
self.setting_flag_rule_in_switch_T_macd_ipd(tos)
else:
print "Insert rules of setting in switch T start at : ",localtime," Switch :",self.connection.dpid,"Count : ",C_set
tos=0
self.setting_flag_rule_in_switch_T(tos)
if C_set==N:
C_set=0
def match_bit_S(self):
localtime = time.asctime( time.localtime(time.time()) )
global flag_match_S
global C_match_S
global num_ruleS
global ip_src, ip_dst, mac_src, mac_dst
global var1,var2,var3,var4
ports = []
ports_switch=[]
limit=65534
for m in self.connection.features.ports:
ports.append(m.port_no)
for element in ports:
if element < limit:
ports_switch.append(element)
max = ports_switch[0]
pos = 1
while pos < len(ports_switch) :
if ports_switch[pos] > max :
max = ports_switch[pos]
pos = pos + 1
ports_switch.remove(max)
if self.connection.dpid <=40:
C_match_S=C_match_S+1
msg = nx.nx_flow_mod_table_id()
self.connection.send(msg)
if C_match_S==1:
if flag_match_S==0:
flag_match_S=1
if flag_match_S==1:
if var1==1 and var2==1 and var3==1 and var4==1:
port=52
tos=0x04
self.matching_flag_rule_in_switchS_macs_macd_ips_ipd(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS_macs_macd_ips_ipd(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS_macs_macd_ips_ipd(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS_macs_macd_ips_ipd(port,tos)
elif var1==1 and var2==1:
port=52
tos=0x04
self.matching_flag_rule_in_switchS_ips_ipd(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS_ips_ipd(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS_ips_ipd(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS_ips_ipd(port,tos)
elif var3==1 and var4==1:
port=52
tos=0x04
self.matching_flag_rule_in_switchS_macs_macd(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS_macs_macd(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS_macs_macd(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS_macs_macd(port,tos)
elif var1==1 and var3==1:
port=52
tos=0x04
self.matching_flag_rule_in_switchS_macs_ips(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS_macs_ips(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS_macs_ips(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS_macs_ips(port,tos)
elif var2==1 and var4==1:
port=52
tos=0x04
self.matching_flag_rule_in_switchS_macd_ipd(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS_macd_ipd(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS_macd_ipd(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS_macd_ipd(port,tos)
else:
num_ruleS=num_ruleS+1
print "Insert rules of matching switch S start at : ",localtime," Switch :",self.connection.dpid,"Count : ",num_ruleS
port=52
tos=0x04
self.matching_flag_rule_in_switchS(port,tos)
port=52
tos=0
self.matching_flag_rule_in_switchS(port,tos)
for p in ports_switch:
porta=p
port=porta
tos=0x04
self.matching_flag_rule_in_switchS(port,tos)
port=porta
tos=0
self.matching_flag_rule_in_switchS(port,tos)
if C_match_S==N:
C_match_S=0
def forwarding_lowp_T(self):
localtime = time.asctime( time.localtime(time.time()) )
global flag_match_T
global num_ruleT
global C_match_T
global ip_src, ip_dst, mac_src, mac_dst
global var1,var2,var3,var4
if self.connection.dpid > 100:
C_match_T=C_match_T+1
msg = nx.nx_flow_mod_table_id()
self.connection.send(msg)
if C_match_T==1:
if flag_match_T==0:
flag_match_T=1
if flag_match_T==1:
if var1==1 and var2==1 and var3==1 and var4==1:
self.forwarding_rules_lowpriority_switchT()
elif var1==1 and var2==1:
self.forwarding_rules_lowpriority_switchT()
elif var3==1 and var4==1:
self.forwarding_rules_lowpriority_switchT()
elif var1==1 and var3==1:
self.forwarding_rules_lowpriority_switchT()
elif var2==1 and var4==1:
self.forwarding_rules_lowpriority_switchT()
else:
num_ruleT=num_ruleT+1
print "Insert rules matching switch T : ",localtime," Switch :",self.connection.dpid,"Count : ",num_ruleT
self.forwarding_rules_lowpriority_switchT()
if C_match_T==N:
C_match_T=0
def read_counter(self):
#sends the requests to all the switches in the topology connected to the controller
if self.connection.dpid <=40:
global flag_read
global C_read
localtime = time.asctime( time.localtime(time.time()) )
C_read=C_read+1
if C_read==1:
if flag_read==0:
flag_read=1
elif flag_read==1:
flag_read=0
if flag_read==1:
if self.connection.dpid==1 :
for connection in core.openflow._connections.values():
connection.send(of.ofp_stats_request(body=of.ofp_flow_stats_request()))
elif flag_read==0:
print "Waiting for other seconds in order to read counter ... "
if C_read==N:
C_read=0
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring without filters
def func1(self,mac_address_D,ip_address_D,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring with filter on ips and ipd
def func1_ips_ipd(self,mac_address_D,ip_address_D,ips,ipd,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
IP_s=ips
IP_d=ipd
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring with filter on macs and macd
def func1_macs_macd(self, mac_address_D, ip_address_D, macs,macd,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
MAC_s=macs
MAC_d=macd
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring with filter on ips and macs
def func1_ips_macs(self, mac_address_D, ip_address_D,ips,macs,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
IP_s=ips
MAC_s=macs
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt ))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring with filter on ipd and macd
def func1_ipd_macd(self, mac_address_D, ip_address_D,ipd,macd,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
IP_d=ipd
MAC_d=macd
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#match the flag in the packet and set the number of port of switch in the TOS field.....monitoring with filter on ips and macs and ipd and macd
def func1_ips_ipd_macs_macd(self, mac_address_D, ip_address_D,ips,ipd,macs,macd,tos,nwtos):
mac_D=mac_address_D
ip_D=ip_address_D
IP_s=ips
IP_d=ipd
MAC_s=macs
MAC_d=macd
t=tos
nt=nwtos
msg = nx.nx_flow_mod()
msg.table_id = 2
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_dst = EthAddr(mac_D)
msg.match.of_ip_proto = 17
msg.match.of_ip_dst = IPAddr(ip_D)
msg.match.of_ip_tos=t
msg.actions.append(of.ofp_action_nw_tos(nw_tos= nt))
msg.actions.append(nx.nx_action_resubmit.resubmit_table(table = 3))
self.connection.send(msg)
#rules forwarding in switch S
def ForwardingRule_SwitchS_macs_macd_ips_ipd(self,port,tos,outport):
t=tos
p=port
out=outport
msg = nx.nx_flow_mod()
msg.table_id = 3
msg.priority = 10
msg.idle_timeout = of.OFP_FLOW_PERMANENT
msg.hard_timeout = of.OFP_FLOW_PERMANENT
msg.match.of_eth_type = pkt.ethernet.IP_TYPE
msg.match.of_eth_src = EthAddr(mac_src)
msg.match.of_eth_dst = EthAddr(mac_dst)