-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_test.py
executable file
·411 lines (351 loc) · 12.9 KB
/
path_test.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
#!/usr/bin/python
# (C) 2015-2018 Lumina Networks, Inc.
# 2077 Gateway Place, Suite 500, San Jose, CA 95110
# Use of the software files and documentation is subject to license terms.
# Author: Luis Gomez
"""
Overview: Script to analyze network impact of single failure (link or node) when
devices are programmed for fast failover (max two paths for every destination).
Intructions:
1) Fill topology using topo.node_add and topo.link_add below.
2) Run script and check results report, the script will:
- fill the routing table for all nodes in the network.
- simulate all posssible network single failres (link or node).
- analize the impact of the failure (ok, drop, loop) in all paths.
"""
import collections
import copy
import time
class RoutingTable(set):
"""
Overview: Class to store the routing table for all nodes in the network:
routes = {node:{dst_node:[next_hop_node]}}
Methods:
- add_route: add route to node = dst_node -> next_hop.
- delete_route_link: delete all concerned routes when link goes down.
- delete_route_node: delete all concerned routes when node goes down.
- print_table: print all nodes routing table.
"""
def __init__(self, nodes):
self.routes = {}
for node in nodes:
self.routes[node] = collections.defaultdict(list)
def add_route(self, node, dst_node, next_hop):
self.routes[node][dst_node].append(next_hop)
def delete_route_link(self, node1, node2):
# delete all routes in node1 with next_hop node2
for dst_node in self.routes[node1]:
try:
self.routes[node1][dst_node].remove(node2)
except:
continue
# delete all routes in node2 with next_hop node1
for dst_node in self.routes[node2]:
try:
self.routes[node2][dst_node].remove(node1)
except:
continue
def delete_route_node(self, node):
for src_node in self.routes:
if src_node is node:
# delete all routes from node to neighbors
for dst_node in self.routes[src_node]:
self.routes[src_node][dst_node][:] = []
else:
# delete all routes from neighbors to node
for dst_node in self.routes[src_node]:
try:
self.routes[src_node][dst_node].remove(node)
except:
continue
def print_table(self):
for node in self.routes:
print "** " + node + " routes:"
for dst_node in rt.routes[node]:
print dst_node + ": " + str(rt.routes[node][dst_node])
print ""
class Graph:
"""
Overview: Class to store the graph nodes, links, node edges and distances:
nodes = (nodes)
links = ((node1,node2))
edges = {node:[next_hop_list]}
distances = {(node1,node2):distance}
Methods:
- add_node: add node to topology.
- add_link: add link with cost to topology.
- delete_link: delete link from topology.
"""
def __init__(self):
self.nodes = set()
self.links = set()
self.edges = collections.defaultdict(list)
self.distances = {}
def add_node(self, value):
self.nodes.add(value)
def add_link(self, node1, node2, distance):
self.links.add((node1, node2))
self.edges[node1].append(node2)
self.edges[node2].append(node1)
self.distances[(node1, node2)] = distance
self.distances[(node2, node1)] = distance
def delete_link(self, node1, node2):
try:
self.links.remove((node1, node2))
except:
self.links.remove((node2, node1))
self.edges[node1].remove(node2)
self.edges[node2].remove(node1)
self.distances.pop((node1, node2), None)
self.distances.pop((node2, node1), None)
def dijsktra(graph, initial):
"""
Overview: Function to calculate best path to node in a graph.
Arguments:
- graph: topology to calculate best path.
- initial: node to calculate best path to.
Returns:
- visited: nodes best path cost to initial.
visited = {src_node:path_cost}
- path: nodes best path next_hop to initial.
path = {src_node:next_hop_node}
"""
visited = {initial: 0}
path = {}
nodes = set(graph.nodes)
while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node
if min_node is None:
break
nodes.remove(min_node)
current_weight = visited[min_node]
for edge in graph.edges[min_node]:
try:
weight = current_weight + graph.distances[(min_node, edge)]
except:
continue
if edge not in visited or weight < visited[edge]:
visited[edge] = weight
path[edge] = min_node
return visited, path
def test_path (routes, src_node, dst_node):
"""
Overview: Function to test a path from src_node to dst_node.
Arguments:
- routes: nodes routing table.
- src_node: path source.
- dst_node: path destination.
Returns:
- status: path status (e.g. ok, drop, loop).
- path: current path sequence.
path = [node]
"""
status = "ok"
last_node = None
current_node = src_node
path = [current_node]
while current_node is not dst_node:
try:
# try best next_hop
next_node = routes[current_node][dst_node][0]
except:
status = "drop"
break
if next_node is last_node:
try:
# if traffic is coming from best next_hop try second best next_hop
next_node = routes[current_node][dst_node][1]
except:
status = "drop"
break
if next_node in path:
# if next_hop is stored in path we have a loop
path.append(next_node)
status = "loop"
break
path.append(next_node)
last_node = current_node
current_node = next_node
return status, path
def test_paths (routes, nodes, down_node=None):
"""
Overview: Function to test all paths in the network.
Arguments:
- routes: nodes routing table.
- nodes: nodes in the network.
- down_node: in case a node goes down, there are expected path drops.
Returns:
- count_exp_drop: expected path drops due to node down
- count_drops: path drops due to fast failover
- count_loops: path loops due to fast failover
- count_ok: path ok count
"""
count_exp_drop = 0
count_drop = 0
count_loop = 0
count_ok = 0
for src_node in nodes:
other_nodes = set(nodes)
other_nodes.remove(src_node)
for dst_node in other_nodes:
status, path = test_path(routes, src_node, dst_node)
print src_node + "->" + dst_node + ":" + str(path) + " status:" + status
if status == "drop" and (src_node is down_node or dst_node is down_node):
# if the path drop is because src or dst node is down it is expected
count_exp_drop += 1
elif status == "drop":
count_drop += 1
elif status == "loop":
count_loop += 1
else:
count_ok += 1
return count_exp_drop, count_drop, count_loop, count_ok
if __name__ == "__main__":
# fill the graph
topo = Graph()
# nodes
topo.add_node("1010001")
topo.add_node("1020001")
topo.add_node("1030001")
topo.add_node("1040001")
topo.add_node("2010001")
topo.add_node("2010002")
topo.add_node("2020001")
topo.add_node("2020002")
topo.add_node("2030001")
topo.add_node("2030002")
topo.add_node("2090001")
topo.add_node("2090002")
topo.add_node("3010001")
topo.add_node("3020001")
topo.add_node("3020002")
topo.add_node("3030001")
topo.add_node("3030002")
topo.add_node("3080001")
topo.add_node("3090001")
topo.add_node("3090002")
topo.add_node("5010001")
topo.add_node("5010002")
topo.add_node("5020001")
topo.add_node("7010001")
topo.add_node("7010002")
topo.add_node("7020001")
topo.add_node("7020002")
topo.add_node("8010001")
topo.add_node("8020001")
# links
topo.add_link("1010001", "1020001", 1)
topo.add_link("1040001", "1030001", 1)
topo.add_link("1010001", "1040001", 1)
topo.add_link("1020001", "1030001", 1)
topo.add_link("1010001", "2010001", 1)
topo.add_link("1020001", "2020001", 1)
topo.add_link("2010001", "2020001", 1)
topo.add_link("2010002", "2020002", 1)
topo.add_link("2030001", "2030002", 1)
topo.add_link("2090001", "2090002", 1)
topo.add_link("2010001", "2010002", 1)
topo.add_link("2010001", "2030001", 1)
topo.add_link("2010001", "2090001", 1)
topo.add_link("2010001", "8010001", 1)
topo.add_link("2010001", "7010001", 1)
topo.add_link("2020001", "2020002", 1)
topo.add_link("2020001", "2030002", 1)
topo.add_link("2020001", "2090002", 1)
topo.add_link("2020001", "8020001", 1)
topo.add_link("2020001", "7020001", 1)
topo.add_link("2020001", "3020001", 1)
topo.add_link("3010001", "3020001", 1)
topo.add_link("3030001", "3030002", 1)
topo.add_link("3090001", "3090002", 1)
topo.add_link("3010001", "3020002", 1)
topo.add_link("3010001", "3030001", 1)
topo.add_link("3010001", "3080001", 1)
topo.add_link("3010001", "3090001", 1)
topo.add_link("3010001", "5010001", 1)
topo.add_link("3020001", "3020002", 1)
topo.add_link("3020001", "3030002", 1)
topo.add_link("3020001", "3080001", 1)
topo.add_link("3020001", "3090002", 1)
topo.add_link("3020001", "5020001", 1)
topo.add_link("5010001", "5020001", 1)
topo.add_link("5010001", "5010002", 1)
topo.add_link("5010001", "7010001", 1)
topo.add_link("5020001", "7020002", 1)
topo.add_link("7010001", "7020001", 1)
topo.add_link("7010002", "7020002", 1)
topo.add_link("7010001", "7010002", 1)
topo.add_link("7020001", "7020002", 1)
topo.add_link("8010001", "8020001", 1)
# fill the routing tables
start_time = time.time()
compute_count = 0
rt = RoutingTable(topo.nodes)
for dst_node in topo.nodes:
visited, path = dijsktra(topo, dst_node)
compute_count += 1
for src_node in path:
# add best path to dst_node
rt.add_route(src_node, dst_node, path[src_node])
# add second best path (if available) to dst_node
topo.delete_link(src_node, path[src_node])
second_visited, second_path = dijsktra(topo, dst_node)
compute_count += 1
if src_node in second_path:
rt.add_route(src_node, dst_node, second_path[src_node])
topo.add_link(src_node, path[src_node], 1)
elapsed_time = time.time() - start_time
# print routing tables
rt.print_table()
# test paths
total_exp_drop=0
total_drop=0
total_loop=0
total_ok=0
# test all link failures
for node1, node2 in topo.links:
rt_temp = copy.deepcopy(rt)
print "** fail link " + node1 + "-" + node2
rt_temp.delete_route_link(node1, node2)
count_exp_drop, count_drop, count_loop, count_ok = test_paths(rt_temp.routes, topo.nodes)
total_exp_drop += count_exp_drop
total_drop += count_drop
total_loop += count_loop
total_ok += count_ok
# test all node failures
for node in topo.nodes:
rt_temp = copy.deepcopy(rt)
print "** fail node " + node
rt_temp.delete_route_node(node)
count_exp_drop, count_drop, count_loop, count_ok = test_paths(rt_temp.routes, topo.nodes, node)
total_exp_drop += count_exp_drop
total_drop += count_drop
total_loop += count_loop
total_ok += count_ok
total_tested = total_exp_drop + total_drop + total_loop + total_ok
# print results
print ""
print "** Test Results:"
print "number of nodes: " + str(len(topo.nodes))
print "number of links: " + str(len(topo.links))
print ""
print "path compute cycles: " + str (compute_count)
print "path compute time: " + str(elapsed_time)
print ""
print "path tested: " + str(total_tested)
print "path expected drop: " + str(total_exp_drop)
print "path drop: " + str(total_drop)
print "path loop: " + str(total_loop)
print "path ok: " + str(total_ok)
print ""
print "expected drop: " + str("{:.1%}".format(float(total_exp_drop) / float(total_tested)))
print "drop: " + str("{:.1%}".format(float(total_drop) / float(total_tested)))
print "loop: " + str("{:.1%}".format(float(total_loop) / float(total_tested)))
print "ok: " + str("{:.1%}".format(float(total_ok) / float(total_tested)))
print ""