Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 from ryu.base import app_manager | 1 from ryu.base import app_manager |
Vasileios Papageorgiou
2017/12/06 15:24:25
I am adding your own draft comments here so that i
| |
2 from ryu.controller import ofp_event | 2 from ryu.controller import ofp_event |
3 from ryu.controller.handler import set_ev_cls | 3 from ryu.controller.handler import set_ev_cls |
4 from ryu.ofproto import ofproto_v1_0 | 4 from ryu.ofproto import ofproto_v1_0 |
5 from ryu.ofproto import ofproto_v1_3 | 5 from ryu.ofproto import ofproto_v1_3 |
6 from ryu.ofproto import ofproto_v1_2 | |
7 from ryu.ofproto import ofproto_v1_2_parser | |
6 from ryu.ofproto import ofproto_v1_3_parser | 8 from ryu.ofproto import ofproto_v1_3_parser |
7 from ryu.topology import event, switches | 9 from ryu.topology import event, switches |
8 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER | 10 from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER |
9 import time | 11 import time,math, random |
10 | 12 from threading import Thread |
11 class L2Switch(app_manager.RyuApp): | 13 import json, urllib2,requests, sys |
Vasileios Papageorgiou
2017/10/12 20:16:19
Probably change the name of the class to RPM
| |
12 » OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] | 14 from ryu.lib import hub |
15 from ryu.app import simple_switch_13 | |
16 from test import * | |
17 from threading import Timer | |
18 import matplotlib.pyplot as plt· | |
19 import numpy as np | |
20 | |
21 | |
22 class RPM(app_manager.RyuApp): | |
23 » OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] | |
13 | 24 |
14 def __init__(self, *args, **kwargs): | 25 def __init__(self, *args, **kwargs): |
15 » » super(L2Switch, self).__init__(*args, **kwargs) | 26 » » super(RPM, self).__init__(*args, **kwargs) |
Vasileios Papageorgiou
2017/10/12 20:16:18
here as well change it to RPM
| |
16 » » self.switches = {} | 27 » » self.switches = {} #switches[id]=datapath |
17 » » self.sending_time={} #liste with the time t0 for each switch | 28 » » self.sending_time={} #list with the time t0 for each switch |
18 » » self.reception_time={} #liste of t1 | 29 » » self.reception_time={} #list of t1 |
19 self.RTT={} #t1-t0 | 30 self.RTT={} #t1-t0 |
20 | 31 » » self.maxRTT=0······· |
21 » @set_ev_cls(event.EventSwitchEnter) #get the datapaths for each swi tch· | 32 » » self.id_maxRTT=0 |
33 » » self.minRTT=10 | |
34 » » self.id_minRTT=0 | |
35 » » self.meanRTT=0 | |
36 » » self.id_meanRTT=0 | |
37 » » self.period=2 #sleeping time of the threads | |
38 » » self.url = 'http://192.16.125.183/API_REST/request.php' | |
39 » » self.iterator=0 | |
40 » » self.RTTsum=0 # used to compute the mean RTT | |
41 » » self.ratio=0 | |
42 » » self.number_of_max_302=0 | |
43 » » self.total_iterations=0 | |
44 » » #self.only_once=1 | |
45 » » self.table_RTT_302={} | |
46 » » self.iterator_302=0 | |
47 » » self.sumRTT302=0 #used for traffic | |
48 » » self.avgRTT302=0 | |
49 » » self.sumRTT302bis=0 #used for nb of commands | |
50 » » self.avgRTT302bis=0 | |
51 » » self.sumRTT302bisbis=0 #used for nb of flows | |
52 » » self.avgRTT302bisbis=0 | |
53 » » self.monitor_thread = hub.spawn(self._monitor) | |
54 » » self.RTT302_rules={} #values of RTT302, function of number of rules | |
55 » » self.RTT302_flows={} #values of RTT302, function of number of flows | |
56 » » self.timer_rules=Timer(10,self.addRTT302_rules) | |
57 » » self.timer_flows=Timer(50,self.addRTT302_flows) | |
58 » » self.nbOfRules=0 #nb of rules sent to s302 | |
59 » » self.nbOfFlows=0 #nb od flows going through s302 | |
60 » » self.lastvalue=0 | |
61 » » self.lastvaluebis=0 | |
62 » » self.plotTimer_rules=Timer(169,self.plot_RTTrules) #timer tha t will plot the graph (RTT function of nb of rules) in 169s | |
63 » » self.plotTimer_flows=Timer(189,self.plot_RTTflows) #timer tha t will plot the graph (RTT function of nb of rules) in 330s | |
64 » »······· | |
65 » » self.table_RTT_314={} | |
66 » » self.iterator_314=0 | |
67 » » self.sumRTT314=0 #used for traffic | |
68 » » self.avgRTT314=0 | |
69 » » self.sumRTT314bis=0 #used for nb of commands | |
70 » » self.avgRTT314bis=0 | |
71 » » self.sumRTT314bisbis=0 #used for nb of flows | |
72 » » self.avgRTT314bisbis=0 | |
73 » » self.RTT314_rules={} #values of RTT302, function of number of rules | |
74 » » self.RTT314_flows={} #values of RTT302, function of number of flows | |
75 » » self.lastvalue_314=0 | |
76 » » self.lastvaluebis_314=0 | |
77 | |
78 | |
79 » @set_ev_cls(event.EventSwitchEnter) #get the datapath for each swit ch· | |
22 def get_datapaths(self, ev): | 80 def get_datapaths(self, ev): |
23 new_switch = ev.switch.dp | 81 new_switch = ev.switch.dp |
24 self.switches[new_switch.id] = ev.switch.dp | 82 self.switches[new_switch.id] = ev.switch.dp |
25 | 83 » » self.send_barrier_request(new_switch) |
84 » » self.sending_time[new_switch.id]=time.time() | |
85 » » if new_switch.id == 302: | |
86 » » » #self.timer_rules.start() #launch the timer t hat will call the function addRTT302_rules in 10s | |
87 » » » self.timer_flows.start() #launch the timer tha t will call the function addRTT302_flows in 10s | |
88 » » » #self.plotTimer_rules.start() #launch the timer t hat will plot the graph (RTT function of nb of rules) in 169s | |
89 » » » self.plotTimer_flows.start() #launch the timer tha t will plot the graph (RTT function of nb of rules) in 330s | |
90 » »······· | |
26 def send_barrier_request(self, datapath): #a barrier request will g et the switches to send back a barrier reply to the controller | 91 def send_barrier_request(self, datapath): #a barrier request will g et the switches to send back a barrier reply to the controller |
27 ofp_parser = datapath.ofproto_parser | 92 ofp_parser = datapath.ofproto_parser |
28 req = ofp_parser.OFPBarrierRequest(datapath) | 93 req = ofp_parser.OFPBarrierRequest(datapath) |
29 » datapath.send_msg(req) | 94 » datapath.send_msg(req)··· |
30 »·· | 95 #------------------------------------------------------------------------------- --------------# |
31 » @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) #sen ding a barrier request to all the switches | 96 » »······· |
32 » def _send_message(self, ev): | 97 » def addRTT302_rules(self): #Computes avg RTT for s302 and s314 each time we add rules(here, 5 sets of rules are added each iteration(10s))» »······· |
33 » » msg = ev.msg | 98 » » for i in range(self.lastvalue, len(self.table_RTT_302)) : #Get ting the sum of the new values in the list of RTTs of s302 |
34 » » datapath = msg.datapath | 99 » » » self.sumRTT302bis=self.sumRTT302bis+self.table_RTT_302[i ] |
35 » » switch_id= datapath.id | 100 » » self.avgRTT302bis=self.sumRTT302bis/(len(self.table_RTT_302)+1-s elf.lastvalue) #Getting the average of these values |
36 » » #for i in self.switches: | 101 » » self.RTT302_rules[self.nbOfRules*20]=self.avgRTT302bis |
Vasileios Papageorgiou
2017/10/12 20:16:18
do we need for loop?
| |
37 » » self.send_barrier_request(datapath) | 102 » » for i in range(self.lastvalue_314, len(self.table_RTT_314)) : #Doing the same for s314 |
38 » » self.sending_time[switch_id]=time.time() | 103 » » » self.sumRTT314bis=self.sumRTT314bis+self.table_RTT_314[i ] |
39 » » » #print 'The controller sent a barrier request to switch ' , i | 104 » » self.avgRTT314bis=self.sumRTT314bis/(len(self.table_RTT_314)+1-s elf.lastvalue_314) |
40 »······· | 105 » » self.RTT314_rules[self.nbOfRules*20]=self.avgRTT314bis |
41 | 106 » » self.lastvalue=len(self.table_RTT_302) |
107 » » self.lastvalue_314=len(self.table_RTT_314) | |
108 » » self.nbOfRules=self.nbOfRules+5 # increasing the frequency of nb of rules sent· | |
109 » » self.timer_rules.cancel() | |
110 » » self.timer_rules=Timer(10,self.addRTT302_rules) #Calling this function again in 10s | |
111 » » self.timer_rules.start() | |
112 | |
113 » def addRTT302_flows(self): » #Computes avg RTT for s302 and s314 each time we add flows(here, 1 iperf flow added each 30s)» » »······· | |
Vasileios Papageorgiou
2017/12/06 15:24:25
What do you mean by 1 iperf flow added each 30s?
younes.tahri3
2017/12/06 16:00:42
I use iperf_traffic file for this one. And in this
| |
114 » » for i in range(self.lastvaluebis, len(self.table_RTT_302)) : | |
115 » » » self.sumRTT302bisbis=self.sumRTT302bisbis+self.table_RTT _302[i] | |
116 » » self.avgRTT302bisbis=self.sumRTT302bisbis/(len(self.table_RTT_30 2)+1-self.lastvaluebis) | |
117 » » self.RTT302_flows[self.nbOfFlows]=self.avgRTT302bisbis | |
118 » » self.lastvaluebis=len(self.table_RTT_302) | |
119 » » for i in range(self.lastvaluebis_314, len(self.table_RTT_314)) : | |
120 » » » self.sumRTT314bisbis=self.sumRTT314bisbis+self.table_RTT _314[i] | |
121 » » self.avgRTT314bisbis=self.sumRTT314bisbis/(len(self.table_RTT_31 4)+1-self.lastvaluebis_314) | |
122 » » self.RTT314_flows[self.nbOfFlows]=self.avgRTT314bisbis | |
123 » » self.lastvaluebis_314=len(self.table_RTT_314) | |
124 » » self.nbOfFlows=self.nbOfFlows+1 #Here we add a new iperf flow each 50s | |
125 » » self.timer_flows.cancel() | |
126 » » self.timer_flows=Timer(30,self.addRTT302_flows) # Calling this function again in 30s | |
127 » » self.timer_flows.start() | |
128 | |
129 #------------------------------------------------------------------------------- ----------------# | |
130 » def plot_RTTrules(self): #plot graph for s302 and s314 : RTT function o f nb of rules sent to s302,no rules sent ro s314 | |
131 » » fig=plt.figure() | |
132 » » s302 = fig.add_subplot(221) | |
133 » » s314 = fig.add_subplot(222) | |
134 » » number_of_rules_per_second=list(self.RTT302_rules.keys()) | |
135 » » RTT=list(self.RTT302_rules.values()) | |
136 » » RTT314=list(self.RTT314_rules.values()) | |
137 » » s302.plot(number_of_rules_per_second,RTT,'ro') | |
138 » » s302.axis([0,1600,0,2])» | |
139 » » s314.plot(number_of_rules_per_second,RTT314,'bo') | |
140 » » s314.axis([0,1600,0,2]) | |
141 » » fig.suptitle('RTT of s302(red) and s314(blue),function of nb of rules/s sent to s302')· | |
142 » » fig.show() | |
143 | |
144 » def plot_RTTflows(self): #plot graph for s302 and s314 : RTT functio n of nb of flows that go through s302. These flows don't go through s314 (cf top ology) | |
145 » » fig=plt.figure() | |
146 » » s302 = fig.add_subplot(221) | |
147 » » s314 = fig.add_subplot(222) | |
148 » » number_of_flows=list(self.RTT302_flows.keys()) | |
149 » » RTT=list(self.RTT302_flows.values()) | |
150 » » RTT314=list(self.RTT314_flows.values()) | |
151 » » s302.plot(number_of_flows,RTT,'ro') | |
152 » » s302.axis([0,5,0,10])»·· | |
153 » » s314.plot(number_of_flows,RTT314,'bo') | |
154 » » s314.axis([0,5,0,10]) | |
155 » » fig.suptitle('RTT of s302(red) and s314(blue),function of nb of flows going through s302')· | |
156 » » fig.show() | |
157 | |
158 » » »······· | |
159 #------------------------------------------------------------------------------- ---------------# | |
160 | |
161 » def _monitor(self): #sends barrier request to all switches each period | |
162 » » while True:» » » » » » » » » »······· | |
163 » » » for switch in self.switches:»··· | |
164 » » » » self.send_barrier_request(self.switches[switch]) | |
165 » » » » self.sending_time[switch]=time.time() | |
166 » » » hub.sleep(self.period)»· | |
167 | |
168 #------------------------------------------------------------------------------- ----------# | |
169 | |
170 » » » » » » » » » » » »······· | |
42 @set_ev_cls(ofp_event.EventOFPBarrierReply, MAIN_DISPATCHER) #what to do when you receive a barrier request | 171 @set_ev_cls(ofp_event.EventOFPBarrierReply, MAIN_DISPATCHER) #what to do when you receive a barrier request |
43 def barrier_reply_handler(self, ev): | 172 def barrier_reply_handler(self, ev): |
173 self.iterator=self.iterator+1··········· | |
44 msg = ev.msg | 174 msg = ev.msg |
45 datapath = msg.datapath | 175 datapath = msg.datapath |
46 switch_id= datapath.id | 176 switch_id= datapath.id |
47 self.reception_time[switch_id]=time.time() | 177 self.reception_time[switch_id]=time.time() |
48 #print 'OFPBarrierReply from switch', switch_id, 'received' | 178 #print 'OFPBarrierReply from switch', switch_id, 'received' |
49 » » self.RTT[switch_id]=self.reception_time[switch_id]-self.sending_ time[switch_id] | 179 » » self.RTT[switch_id]=self.reception_time[switch_id]-self.sending_ time[switch_id] # compute the RTt» »······· |
50 » » print 'The RTT for the switch', switch_id, 'is', self.RTT[switch _id], 'seconds' | 180 » » #print 'The RTT for the switch', switch_id, 'is', self.RTT[switc h_id], 'seconds' |
51 | 181 » » if switch_id==302: |
52 | 182 » » #» print 'The RTT for the switch', switch_id, 'is', self.RT T[switch_id], 'seconds' |
53 | 183 » » » self.table_RTT_302[self.iterator_302]=self.RTT[switch_id ] |
54 | 184 » » » self.iterator_302=self.iterator_302+1 |
55 | 185 » » if switch_id==314: |
56 | 186 » » » self.table_RTT_314[self.iterator_314]=self.RTT[switch_id ] |
57 | 187 » » » self.iterator_314=self.iterator_314+1 |
58 | 188 » »······· |
59 | 189 » » » »······· |
60 | 190 » » if (self.RTT[switch_id]>self.maxRTT): #update the maxRTT |
191 » » » self.maxRTT=self.RTT[switch_id] | |
192 » » » self.id_maxRTT= switch_id | |
193 » » if (self.RTT[switch_id]<self.minRTT): #update the minRTT | |
194 » » » self.minRTT=self.RTT[switch_id] | |
195 » » » self.id_minRTT= switch_id | |
196 »······· | |
197 » » if self.iterator== len(self.switches): #print the info when the controller gets the RTT for every switch | |
198 » » » for i in self.RTT: | |
199 » » » » self.RTTsum=self.RTTsum+self.RTT[i] | |
200 » » » self.meanRTT=self.RTTsum/len(self.switches) | |
201 » » » #print 'The switch', self.id_maxRTT,'has the max RTT for this periode:', self.maxRTT | |
202 » » » #print 'The mean RTT for this periode is', self.meanRTT | |
203 » » » self.total_iterations=self.total_iterations+1 | |
204 » » » if self.id_maxRTT==302: | |
205 » » » » self.number_of_max_302=self.number_of_max_302+1 | |
206 » » » self.ratio=(self.number_of_max_302*100/self.total_iterat ions) | |
207 » » » #print 'the number of times where s302 had the max RTT i s', self.number_of_max_302 | |
208 » » » #print 'the ratio of times where s302 had the max RTT is :', self.ratio,'%' | |
209 » » » #print '------------------------------------------------ ----------------' | |
210 » » » #print '------------------------------------------------ ----------------' | |
211 » » » #print 'The switch', self.id_minRTT,'has the min RTT for this periode:', self.minRTT | |
212 » » » self.maxRTT=0 | |
213 » » » self.minRTT=10 | |
214 » » » self.RTTsum=0 | |
215 » » » self.iterator=0 | |
216 | |
217 » » json_formatted = {"switch_id": switch_id, "RTT": self.RTT[switch _id]} | |
218 » » data = json.dumps(json_formatted) | |
219 » » clen = len(data) | |
220 » » req = urllib2.Request(self.url, data, {'Content-Type': 'applicat ion/json', 'Content-Length': clen}) # sending the RTT to the cache | |
221 » » f = urllib2.urlopen(req) | |
222 » » response = f.read() | |
223 | |
224 » »······· | |
225 | |
LEFT | RIGHT |