-
Notifications
You must be signed in to change notification settings - Fork 1
/
sch4_mon.py
executable file
·315 lines (241 loc) · 7.11 KB
/
sch4_mon.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
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Controller, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from threading import Thread
import time
import os,sys
import subprocess
import csv
import StringIO
import iptc
import socket, errno
MON_TIMEOUT = 1.0
MON_SLEEP = 3
HOSTS = 2
p1_log = open('logs-example/log.p1.txt', 'w')
p2_log = open('logs-example/log.p2.txt', 'w')
flag_mon66 = 0
flag_mon67 = 0
flag_exit = False
def closePort(port):
rule=iptc.Rule()
rule.protocol = "tcp"
match = rule.create_match("tcp")
match.dport = str(port)
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
rule.target = rule.create_target("DROP")
chain.insert_rule(rule)
def unClosePort(port):
rule=iptc.Rule()
rule.protocol = "tcp"
match = rule.create_match("tcp")
match.dport = str(port)
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
rule.target = rule.create_target("DROP")
chain.delete_rule(rule)
def ft66(threadname, ports, portd):
print threadname , "is running"
global flag_mon66
global p2
global MON_SLEEP
global MON_TIMEOUT
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
sock.settimeout(MON_TIMEOUT)
tmp = sock.connect_ex(('127.0.0.1', ports))
if tmp != 0 and flag_mon66 != -1:
print 'Controller 1 detected down %s' %errno.errorcode[tmp]
flag_mon66 = -1
print 'SET ROLE C2 AS MASTER at %.10f' %time.time()
p2.stdin.write("import pox.openflow.nicira as nx\n")
p2.stdin.write("for connection in core.openflow.connections:\n")
p2.stdin.write("\tconnection.send(nx.nx_role_request(master='true'))\n")
p2.stdin.write('\n')
flag_mon67 = 0
while True:
p = subprocess.Popen(["ovs-vsctl", "-f", "csv", "list", "controller"], stdout=subprocess.PIPE)
output, err = p.communicate()
f = StringIO.StringIO(output)
reader = csv.reader(f, delimiter=',')
rownum = 0
con67 = []
for row in reader:
uuid = row[0]
target = row[15]
role = row[13]
i = target.find(str(portd))
if i != -1:
if (role == 'master'):
con67.append(uuid)
if len(con67) == HOSTS:
uptime = time.time()
print 'new master ready at %.10f' %uptime
flag_mon67 = 1
f.close()
break
sock.close()
if flag_exit:
print 'exiting ',threadname
sys.exit()
time.sleep(MON_SLEEP)
def ft67(threadname,ports, portd):
print threadname , "is running"
global flag_mon67
global MON_SLEEP
global MON_TIMEOUT
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(0)
sock.settimeout(MON_TIMEOUT)
tmp = sock.connect_ex(('127.0.0.1', ports))
if tmp != 0:
print 'Controller 2 detected down %s' %errno.errorcode[tmp]
flag_mon67 = -1
sock.close()
if flag_exit:
print 'exiting ',threadname
sys.exit()
time.sleep(MON_SLEEP)
def myNet():
global p1
global p2
global p3
global p4
global flag_exit
cPort1=6666
cPort2=6667
hosts=[]
kill = 0
net = Mininet( topo=None, build=False, autoSetMacs=True)
con1 = net.addController( 'c0', controller=RemoteController, ip='127.0.0.1', port=cPort1)
con2 = net.addController( 'c1', controller=RemoteController, ip='127.0.0.1', port=cPort2)
for x in range(0, HOSTS):
hostname = "h%d" %(x)
switchname = "s%d" %(x)
host = net.addHost(hostname)
switch = net.addSwitch(switchname)
if (x!=0):
net.addLink(switch, lastswitch)
lastswitch = switch
net.addLink(host,switch)
net.build()
switch.start([con1,con2])
hosts.append(host)
net.start()
flag_mon66 = flag_mon67 = 0
tmon66 = Thread( target=ft66, args=("ThreadMon66", cPort1, cPort2) )
tmon67 = Thread( target=ft67, args=("ThreadMon67", cPort2, cPort1) )
print "start monitoring thread"
tmon66.start()
tmon67.start()
# we shall wait for original master to become ready
while True:
p = subprocess.Popen(["ovs-vsctl", "-f", "csv", "list", "controller"], stdout=subprocess.PIPE)
output, err = p.communicate()
f = StringIO.StringIO(output)
reader = csv.reader(f, delimiter=',')
rownum = 0
con66 = [] # not using this for now
con67 = []
for row in reader:
uuid = row[0]
target = row[15]
role = row[13]
i = target.find(str(cPort1))
if i != -1:
if (role == 'master'):
con66.append(uuid)
f.close()
if len(con66) == HOSTS:
starttime = time.time()
print 'original master ready at %.10f' %starttime
flag_mon66 = 1
break
tping = time.time()
# print 'h0 ping : %.10f' % tping
# CLI(net)
for i in range(0, HOSTS/2):
tmping = time.time()
print i
dr = 2*i
print dr
to = dr+1
print to
print 'h%d ping h%d: %.10f' % (dr, to, tmping)
cmd = 'hping3 -c 500 -i u20000 %s > logs-example/log.ping%d%d.txt 2>&1 &' %(hosts[to].IP(), dr,to)
hosts[dr].cmdPrint(cmd)
while True:
tcur = time.time()
if tcur - tping > 3: # after 2s running
# print 'SET ROLE C1 SLAVE '
# p1.stdin.write("import pox.openflow.nicira as nx\n")
# p1.stdin.write("for connection in core.openflow.connections:\n")
# p1.stdin.write("\tconnection.send(nx.nx_role_request(slave='true'))\n")
# p1.stdin.write('\n')
print 'close port %i in %.10f' %(cPort1,tcur)
closePort(cPort1)
break
# CLI(net)
# print 'SET ROLE C2 AS MASTER at %.10f' %time.time()
# p2.stdin.write("import pox.openflow.nicira as nx\n")
# p2.stdin.write("for connection in core.openflow.connections:\n")
# p2.stdin.write("\tconnection.send(nx.nx_role_request(master='true'))\n")
# p2.stdin.write('\n')
# while True:
# p = subprocess.Popen(["ovs-vsctl", "-f", "csv", "list", "controller"], stdout=subprocess.PIPE)
# output, err = p.communicate()
# f = StringIO.StringIO(output)
# reader = csv.reader(f, delimiter=',')
# rownum = 0
# con66 = [] # not using this for now
# con67 = []
# # print output
# # print '========================================================'
# for row in reader:
# uuid = row[0]
# target = row[15]
# role = row[13]
# i = target.find(str(cPort2))
# if i != -1:
# if (role == 'master'):
# con67.append(uuid)
# if len(con67) == HOSTS:
# uptime = time.time()
# print 'new master ready at %.10f' %uptime
# f.close()
# break
print 'now wait for hping3 to finish..'
for i in range(0, HOSTS/2):
tmping = time.time()
dr = 2*i
hosts[dr].cmdPrint('wait %hping3')
print 'hping3 finished at %.10f' %time.time()
print 'open the port..'
unClosePort(cPort1)
print 'stopping mininet'
net.stop()
print 'stopping pox(s)..'
p1.terminate()
p2.terminate()
print 'stopping monitoring module(s)..'
flag_exit = True
tmon66.join()
tmon67.join()
# print 'timestamp difference %.10f' %(uptime-tcur)
if __name__ == '__main__':
setLogLevel( 'info' )
p1 = subprocess.Popen(['pox/pox.py', "master66"],stdin=subprocess.PIPE, stdout=p1_log,stderr=p1_log,preexec_fn=os.setpgrp)
print 'c1 runs, master'
p2 = subprocess.Popen(['pox/pox.py',"slave67"],stdin=subprocess.PIPE, stdout=p2_log,stderr=p2_log,preexec_fn=os.setpgrp)
print 'c2 runs, slave'
print 'wait for 3 seconds...'
time.sleep(3)
myNet()
print 'close pox logs..'
p1_log.close()
p2_log.close()
print 'bye'
# t.process.terminate()