forked from taikitada/Network_Monitoring_with_Pox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l2_multi_slice.py
451 lines (381 loc) · 13.6 KB
/
l2_multi_slice.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
# Copyright 2012 James McCauley
#
# 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/>.
"""
A messy first shot at a standalone L2 switch that learns ethernet
addresses across the entire network and picks short paths between
them.
Depends on openflow.discovery
Works with openflow.spanning_tree
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.revent import *
from collections import defaultdict
from pox.openflow.discovery import Discovery
from pox.lib.util import dpidToStr
import time
log = core.getLogger()
# Adjacency map. [sw1][sw2] -> port from sw1 to sw2
# adjacency[sw1][sw2] -> port_number
# "adjacency[sw1][sw2] = 3" is way for registering the port number
adjacency = defaultdict(lambda:defaultdict(lambda:None))
# Switches we know of. [dpid] -> Switch
switches = {}
# ethaddr -> (switch, port)
mac_map = {}
# ADD by tada
# mac_addr1 -> slice_name, mac_adddr2->slice_name, ...
import sqlite3
slice_map = {}
# [sw1][sw2] -> (distance, intermediate)
# path_map[sw1][sw2] = (3, 4)
path_map = defaultdict(lambda:defaultdict(lambda:(None,None)))
# Time to not flood in seconds
FLOOD_HOLDDOWN = 5
def _calc_paths ():
####### CHANGED PART ########
# access monitoring server and get distance_map
import xmlrpclib
# TODO change the Server IP and Port to the command line option
monitoring_server = xmlrpclib.ServerProxy("http://133.1.134.225:8000")
distance_map = monitoring_server.request_link_distance()
##############################
"""
Essentially Floyd-Warshall algorithm
"""
sws = switches.values()
# return list of values to sws [dpid1,dpid2,dpid3,dpid3]
path_map.clear()
for k in sws:
# pick up the [sw1][sw2] , port_number
for j,port in adjacency[k].iteritems():
if port is None: continue
####### CHANGED PART #############
k_dpid = (dpidToStr(k.dpid)).replace("-","")
j_dpid = (dpidToStr(j.dpid)).replace("-","")
if j_dpid not in distance_map[k_dpid]:
path_distance = distance_map[j_dpid][k_dpid]
else:
path_distance = distance_map[k_dpid][j_dpid]
path_map[k][j] = (path_distance, None)
####################################
path_map[k][k] = (0,None) # distance, intermediate
"""
for i in sws:
for j in sws:
a = path_map[i][j][0]
#a = adjacency[i][j]
if a is None: a = "*"
print a,
print
"""
for k in sws:
for i in sws:
for j in sws:
if path_map[i][k][0] is not None:
if path_map[k][j][0] is not None:
# i -> k -> j exists
ikj_dist = path_map[i][k][0]+path_map[k][j][0]
if path_map[i][j][0] is None or ikj_dist < path_map[i][j][0]:
# i -> k -> j is better than existing
path_map[i][j] = (ikj_dist, k)
print "--------------------"
for i in sws:
for j in sws:
print path_map[i][j][0],
print
def _get_raw_path (src, dst):
if len(path_map) == 0: _calc_paths()
if src is dst:
# We're here!
return []
if path_map[src][dst][0] is None:
return None
intermediate = path_map[src][dst][1]
if intermediate is None:
# Directly connected
return []
return _get_raw_path(src, intermediate) + [intermediate] + \
_get_raw_path(intermediate, dst)
def _check_path (p):
for i in range(len(p) - 1):
if adjacency[p[i][0]][p[i+1][0]] != p[i][1]:
return False
return True
def _get_path (src, dst, final_port):
#print "path from",src,"to",dst
if src == dst:
path = [src]
else:
path = _get_raw_path(src, dst)
if path is None: return None
path = [src] + path + [dst]
# print "raw: ",path
r = []
for s1,s2 in zip(path[:-1],path[1:]):
port = adjacency[s1][s2]
r.append((s1,port))
r.append((dst, final_port))
# print "cooked: ",r
assert _check_path(r)
return r
class PathInstalled (Event):
"""
Fired when a path is installed
"""
def __init__ (self, path):
Event.__init__(self)
self.path = path
class Switch (EventMixin):
def __init__ (self):
self.connection = None
self.ports = None
self.dpid = None
self._listeners = None
self._connected_at = None
def __repr__ (self):
return dpidToStr(self.dpid)
def _install (self, switch, port, match, buf = None):
msg = of.ofp_flow_mod()
msg.match = match
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.actions.append(of.ofp_action_output(port = port))
msg.buffer_id = buf
switch.connection.send(msg)
def _install_path (self, p, match, buffer_id = None):
for sw,port in p[1:]:
self._install(sw, port, match)
self._install(p[0][0], p[0][1], match, buffer_id)
"""
Path is installed and raise Event
"""
core.l2_multi.raiseEvent(PathInstalled(p))
def install_path (self, dst_sw, last_port, match, event):#buffer_id, packet):
p = _get_path(self, dst_sw, last_port)
if p is None:
log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst)
import pox.lib.packet as pkt
if (match.dl_type == pkt.ethernet.IP_TYPE and
event.parsed.find('ipv4')):
# It's IP -- let's send a destination unreachable
log.debug("Dest unreachable (%s -> %s)",
match.dl_src, match.dl_dst)
from pox.lib.addresses import EthAddr
e = pkt.ethernet()
e.src = EthAddr(dpidToStr(self.dpid)) #FIXME: Hmm...
e.dst = match.dl_src
e.type = e.IP_TYPE
ipp = pkt.ipv4()
ipp.protocol = ipp.ICMP_PROTOCOL
ipp.srcip = match.nw_dst #FIXME: Ridiculous
ipp.dstip = match.nw_src
icmp = pkt.icmp()
icmp.type = pkt.ICMP.TYPE_DEST_UNREACH
icmp.code = pkt.ICMP.CODE_UNREACH_HOST
orig_ip = event.parsed.find('ipv4')
d = orig_ip.pack()
d = d[:orig_ip.hl * 4 + 8]
import struct
d = struct.pack("!HH", 0,0) + d #FIXME: MTU
icmp.payload = d
ipp.payload = icmp
e.payload = ipp
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = event.port))
msg.data = e.pack()
self.connection.send(msg)
return
self._install_path(p, match, event.ofp.buffer_id)
log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src, match.dl_dst, match.dl_type, len(p))
#log.debug("installing path for %s.%i -> %s.%i" %
# (src[0].dpid, src[1], dst[0].dpid, dst[1]))
def _handle_PacketIn (self, event):
def flood ():
""" Floods the packet """
if self.is_holding_down:
log.warning("Not flooding -- holddown active")
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
msg.buffer_id = event.ofp.buffer_id
msg.in_port = event.port
self.connection.send(msg)
def drop ():
# Kill the buffer
if event.ofp.buffer_id is not None:
msg = of.ofp_packet_out()
msg.buffer_id = event.ofp.buffer_id
event.ofp.buffer_id = None # Mark is dead
msg.in_port = event.port
self.connection.send(msg)
packet = event.parsed
loc = (self, event.port) # Place we saw this ethaddr
oldloc = mac_map.get(packet.src) # Place we last saw this ethaddr
if packet.effective_ethertype == packet.LLDP_TYPE:
drop()
return
#print packet.src,"*",loc,oldloc
if oldloc is None:
if packet.src.isMulticast() == False:
mac_map[packet.src] = loc # Learn position for ethaddr
log.debug("Learned %s at %s.%i", packet.src, loc[0], loc[1])
elif oldloc != loc:
# ethaddr seen at different place!
if loc[1] not in adjacency[loc[0]].values():
# New place is another "plain" port (probably)
log.debug("%s moved from %s.%i to %s.%i?", packet.src,
dpidToStr(oldloc[0].connection.dpid), oldloc[1],
dpidToStr( loc[0].connection.dpid), loc[1])
if packet.src.isMulticast() == False:
mac_map[packet.src] = loc # Learn position for ethaddr
log.debug("Learned %s at %s.%i", packet.src, loc[0], loc[1])
elif packet.dst.isMulticast() == False:
# New place is a switch-to-switch port!
#TODO: This should be a flood. It'd be nice if we knew. We could
# check if the port is in the spanning tree if it's available.
# Or maybe we should flood more carefully?
log.warning("Packet from %s arrived at %s.%i without flow",# -- dropping",
packet.src, dpidToStr(self.dpid), event.port)
#drop()
#return
if packet.dst.isMulticast():
log.debug("Flood multicast from %s", packet.src)
flood()
else:
if packet.dst not in mac_map:
#TODO
#if slice_map[packet.dst] == slice_map[packet.src]:
# log.debug("%s different slice -- drop" % (packet.dst,))
# drop()
#else:
# flood()
#
log.debug("%s unknown -- flooding" % (packet.dst,))
flood()
else:
dest = mac_map[packet.dst]
#print packet.dst, "is on", dest
match = of.ofp_match.from_packet(packet)
self.install_path(dest[0], dest[1], match, event)
def disconnect (self):
if self.connection is not None:
log.debug("Disconnect %s" % (self.connection,))
self.connection.removeListeners(self._listeners)
self.connection = None
self._listeners = None
def connect (self, connection):
if self.dpid is None:
self.dpid = connection.dpid
print "connection.dpid"
print connection.dpid
assert self.dpid == connection.dpid
if self.ports is None:
self.ports = connection.features.ports
self.disconnect()
log.debug("Connect %s" % (connection,))
self.connection = connection
self._listeners = self.listenTo(connection)
self._connected_at = time.time()
@property
def is_holding_down (self):
if self._connected_at is None: return True
if time.time() - self._connected_at > FLOOD_HOLDDOWN:
return False
return True
def _handle_ConnectionDown (self, event):
self.disconnect()
pass
class l2_multi (EventMixin):
_eventMixin_events = set([
PathInstalled,
])
def __init__ (self):
self.listenTo(core.openflow, priority=0)
self.listenTo(core.openflow_discovery)
def _handle_LinkEvent (self, event):
def flip (link):
return Discovery.Link(link[2],link[3], link[0],link[1])
l = event.link
sw1 = switches[l.dpid1]
sw2 = switches[l.dpid2]
# Invalidate all flows and path info.
# For link adds, this makes sure that if a new link leads to an
# improved path, we use it.
# For link removals, this makes sure that we don't use a
# path that may have been broken.
#NOTE: This could be radically improved! (e.g., not *ALL* paths break)
## delete all flows of all switch
clear = of.ofp_flow_mod(match=of.ofp_match(),command=of.OFPFC_DELETE)
for sw in switches.itervalues():
if sw.connection is None: continue
sw.connection.send(clear)
path_map.clear() # clear method for dictionary
if event.removed:
# This link no longer okay
if sw2 in adjacency[sw1]: del adjacency[sw1][sw2]
if sw1 in adjacency[sw2]: del adjacency[sw2][sw1]
# But maybe there's another way to connect these...
for ll in core.openflow_discovery.adjacency:
if ll.dpid1 == l.dpid1 and ll.dpid2 == l.dpid2:
if flip(ll) in core.openflow_discovery.adjacency:
# Yup, link goes both ways
adjacency[sw1][sw2] = ll.port1
adjacency[sw2][sw1] = ll.port2
# Fixed -- new link chosen to connect these
break
else:
# If we already consider these nodes connected, we can
# ignore this link up.
# Otherwise, we might be interested...
if adjacency[sw1][sw2] is None:
# These previously weren't connected. If the link
# exists in both directions, we consider them connected now.
if flip(l) in core.openflow_discovery.adjacency:
# Yup, link goes both ways -- connected!
adjacency[sw1][sw2] = l.port1
adjacency[sw2][sw1] = l.port2
# If we have learned a MAC on this port which we now know to
# be connected to a switch, unlearn it.
bad_macs = set()
for mac,(sw,port) in mac_map.iteritems():
#print sw,sw1,port,l.port1
if sw is sw1 and port == l.port1:
if mac not in bad_macs:
log.debug("Unlearned %s", mac)
bad_macs.add(mac)
if sw is sw2 and port == l.port2:
if mac not in bad_macs:
log.debug("Unlearned %s", mac)
bad_macs.add(mac)
for mac in bad_macs:
del mac_map[mac]
def _handle_ConnectionUp (self, event):
sw = switches.get(event.dpid)
print "switch dpid? : "
print event.dpid
if sw is None:
# New switch
sw = Switch()
switches[event.dpid] = sw
sw.connect(event.connection)
else:
sw.connect(event.connection)
def launch ():
if 'openflow_discovery' not in core.components:
import pox.openflow.discovery as discovery
core.registerNew(discovery.Discovery)
core.registerNew(l2_multi)