-
Notifications
You must be signed in to change notification settings - Fork 4
/
mt_controller_withqueue.py
178 lines (167 loc) · 6.38 KB
/
mt_controller_withqueue.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
import Queue
import select
import socket
import libopenflow as of
from functools import partial
from tornado.ioloop import IOLoop
import threading
fd_map = {}
event_map = {}
thread_map = {}
message_queue_map = {}
pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
class switch(threading.Thread):
def __init__(self, sock, event):
print "init for connection", sock.fileno()
self.sock = sock # for sending pkts
self.fd = sock.fileno()
self.event = event # for waiting pkt_in
self._quit = False
super(switch, self).__init__()
def send(self, pkt):
select.select([], [self.sock], [])
self.sock.send(pkt)
def run(self):
while not self._quit:
#print "waiting", sock.getsockname()
#sock.send("123")
self.event.wait() #waiting for a packet
if self._quit:
print "receive quit signal"
self.event.clear()
return
#need something to ?
#print self.sock, self.sock.getsockname(), self.sock.getpeername()
try:
data = message_queue_map[self.fd].get_nowait()
except Queue.Empty:
continue
#print "%s queue empty" % str(address)
#io_loop.update_handler(fd, io_loop.READ)
#print 'sending "%s" to %s' % (of.ofp_header(next_msg).type, address)
#data = self.receive()
#print len(data), data
if len(data)>=8:
body = data[8:]
msg = of.ofp_header(data)
#msg.show()
if msg.type == 0:
print "OFPT_HELLO"
msg = of.ofp_header(type = 5)
self.send(data)
self.send(str(msg))
elif msg.type == 6:
print "OFPT_FEATURES_REPLY"
#feature = of.ofp_features_reply(body[0:24])
#feature.show()
elif msg.type == 2:
print "OFPT_ECHO_REQUEST"
msg = of.ofp_header(type=3, xid=msg.xid)
self.send(str(msg))
elif msg.type == 10:
#print "OFPT_PACKET_IN"
#rmsg.show()
pkt_in_msg = of.ofp_packet_in(body)
#pkt_in_msg.show()
raw = pkt_in_msg.load
pkt_parsed = of.Ether(raw)
#pkt_parsed.payload.show()
#print "to see if the payload of ether is IP"
#if isinstance(pkt_parsed.payload, of.IP):
#pkt_parsed.show()
if isinstance(pkt_parsed.payload, of.ARP):
#pkt_parsed.show()
#pkt_out = of.ofp_header()/of.ofp_pktout_header()/of.ofp_action_output()
pkt_out.payload.payload.port = 0xfffb
pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
pkt_out.payload.in_port = pkt_in_msg.in_port
pkt_out.length = 24
#pkt_out.show()
self.send(str(pkt_out))
if isinstance(pkt_parsed.payload, of.IP):
if isinstance(pkt_parsed.payload.payload, of.ICMP):
#print "from", pkt_parsed.src, "to", pkt_parsed.dst
pkt_out.payload.payload.port = 0xfffb
pkt_out.payload.buffer_id = pkt_in_msg.buffer_id
pkt_out.payload.in_port = pkt_in_msg.in_port
pkt_out.length = 24
self.send(str(pkt_out))
#io_loop.stop()
self.event.clear()
#except
#data = ""
elif data == "":
print "close connection"
self.stop()
return
else:
self.send(data)
self.event.clear()
print "quit", self._quit
def stop(self):
print "closing thread & connection"
print thread_map
del thread_map[self.sock.fileno()]
self.event.set()
del event_map[self.sock.fileno()]
print thread_map
self.sock.close()
self._quit = True
def handle_pkt(cli_addr, fd, event):
if event & IOLoop.READ:
#data = sock.recv(1024)
if fd in event_map.keys():
data = fd_map[fd].recv(1024)
message_queue_map[fd].put(data)
event_map[fd].set()
#print s
#ioloop.update_handler(fd, IOLoop.WRITE)
#return
if event & IOLoop.ERROR:
print " exception on %s" % cli_addr
io_loop.remove_handler(fd)
fd_map[fd].close()
#del message_queue_map[s]
def agent(sock, fd, events):
#print fd, sock, events
try:
connection, address = sock.accept()
except socket.error, e:
if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
raise
return
connection.setblocking(0)
connection_fd = connection.fileno()
fd_map[connection_fd] = connection
print "connection fd:", connection.fileno()
client_handle = partial(handle_pkt, address)
io_loop.add_handler(connection.fileno(), client_handle, io_loop.READ)
print "in agent: new switch", connection.fileno(), client_handle
event_map[connection_fd] = threading.Event()
message_queue_map[connection_fd] = Queue.Queue()
thread_map[connection_fd] = switch(connection, event_map[connection_fd])
print thread_map
thread_map[connection_fd].start()
def new_sock(block):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(block)
return sock
if __name__ == '__main__':
sock = new_sock(0)
sock.bind(("", 6633))
sock.listen(6633)
fd = sock.fileno()
fd_map[fd] = sock
io_loop = IOLoop.instance()
#callback = functools.partial(connection_ready, sock)
callback = partial(agent, sock)
print sock, sock.getsockname()
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
try:
io_loop.start()
except KeyboardInterrupt:
for i in thread_map.keys():
thread_map[i].stop()
io_loop.stop()
print "quit"