-
Notifications
You must be signed in to change notification settings - Fork 1
/
ryu.patch
89 lines (82 loc) · 3.52 KB
/
ryu.patch
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
diff --git a/ryu/app/simple_switch.py b/ryu/app/simple_switch.py
index 33c4f7d0..40cabf9a 100644
--- a/ryu/app/simple_switch.py
+++ b/ryu/app/simple_switch.py
@@ -27,7 +27,9 @@ from ryu.lib.mac import haddr_to_bin
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
-
+from datetime import datetime
+import time
+import threading
class SimpleSwitch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
@@ -35,6 +37,11 @@ class SimpleSwitch(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(SimpleSwitch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
+ self.measurements = []
+ self.lastPacketReceived = self.getCurrentMicroSeconds()
+ thread = threading.Thread(target=self.checkFileWriteNecessary)
+ thread.daemon = True
+ thread.start()
def add_flow(self, datapath, in_port, dst, src, actions):
ofproto = datapath.ofproto
@@ -50,9 +57,39 @@ class SimpleSwitch(app_manager.RyuApp):
flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
datapath.send_msg(mod)
+ def writeToFile(self):
+ measurementsToWrite = self.measurements.copy()
+ self.measurements = []
+ f = open("ryu_times.txt", "a+")
+ for measurement in measurementsToWrite:
+ f.write(str(measurement[0]) + (" in " if measurement[1] else " out ") + str(measurement[2]) + "\n")
+ f.flush()
+ f.close()
+
+ def checkFileWriteNecessary(self):
+ time.sleep(3)
+ if self.getCurrentMicroSeconds() - self.lastPacketReceived > 1000000 * 3 and len(self.measurements) > 0:
+ self.writeToFile()
+
+ thread = threading.Thread(target=self.checkFileWriteNecessary)
+ thread.daemon = True
+ thread.start()
+
+ def getCurrentMicroSeconds(self):
+ return int(datetime.timestamp(datetime.now()) * 1000000)
+
+
+ @set_ev_cls(ofp_event.EventOFPEchoRequest, MAIN_DISPATCHER)
+ def _ech_request_handler(self, ev):
+ msg = ev.msg
+ self.measurements.append([msg.xid, True, self.getCurrentMicroSeconds()])
+ self.measurements.append([msg.xid, False, self.getCurrentMicroSeconds()])
+
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
+ self.lastPacketReceived = self.getCurrentMicroSeconds()
msg = ev.msg
+ self.measurements.append([msg.xid, True, self.lastPacketReceived])
datapath = msg.datapath
ofproto = datapath.ofproto
@@ -81,8 +118,8 @@ class SimpleSwitch(app_manager.RyuApp):
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
- if out_port != ofproto.OFPP_FLOOD:
- self.add_flow(datapath, msg.in_port, dst, src, actions)
+ #if out_port != ofproto.OFPP_FLOOD:
+ # self.add_flow(datapath, msg.in_port, dst, src, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
@@ -91,6 +128,10 @@ class SimpleSwitch(app_manager.RyuApp):
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
actions=actions, data=data)
+ out.set_xid(msg.xid)
+ self.logger.info("%s %s", out.xid, msg.xid)
+
+ self.measurements.append([msg.xid, False, self.getCurrentMicroSeconds()])
datapath.send_msg(out)
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)