-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvf.py
executable file
·278 lines (193 loc) · 7.72 KB
/
vf.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
#!/usr/bin/env python3
'''
This file contains the implementation of 3 algorithms for classifying the relationships between
two Autonomous Systems (ASes), as described in
Lixin Gao. 2001. On inferring autonomous system relationships in the internet. IEEE/ACM Trans.
Netw. 9, 6 (December 2001), 733–745. DOI:https://doi.org/10.1109/90.974527
'''
import argparse
import numpy as np
import sys
from tqdm import tqdm
from collections import defaultdict
SIBLING_TO_SIBLING = 'S2S'
PROVIDER_TO_CUSTOMER = 'P2C'
CUSTOMER_TO_PROVIDER = 'C2P'
PEER_TO_PEER = 'P2P'
UNDEFINED = 'UNDEF'
PARAMETER_R = 60
class GaoGraphBasic():
def __init__(self, paths):
self.paths = paths
self.edges = self.get_classified_edges()
self.stats = self.compute_stats()
self.vf_class = self.classify_paths()
def get_classified_edges(self):
neighbors = self._phase1()
transit = self._phase2(neighbors)
edges = self._phase3(transit)
return edges
def is_valley_free(self, path):
edge_sequence = [self.edges[(path[i], path[i + 1])] for i, _ in enumerate(path[:-1])]
try:
p2c_index = edge_sequence.index(PROVIDER_TO_CUSTOMER)
if not all([e != CUSTOMER_TO_PROVIDER for e in edge_sequence[p2c_index + 1:]]):
return False
except ValueError:
pass
try:
p2p_index = edge_sequence.index(PEER_TO_PEER)
if not all([e != PEER_TO_PEER and e != CUSTOMER_TO_PROVIDER
for e in edge_sequence[p2p_index + 1:]]):
return False
except ValueError:
pass
return True
def classify_paths(self):
paths_vf_class = []
for path in tqdm(self.paths, desc="Classifying paths"):
paths_vf_class.append(self.is_valley_free(path))
nfalse = paths_vf_class.count(False)
print(f'Not vf: {nfalse}', file=sys.stderr)
return paths_vf_class
def compute_stats(self):
stats = {}
for tor in tqdm(self.edges.values(), desc='Computing stats'):
if tor not in stats:
stats[tor] = 0
stats[tor] += 1
return stats
def _phase1(self):
neighbors = defaultdict(set)
for path in tqdm(self.paths, desc="Phase 1"):
for i, u in enumerate(path[:-1]):
u_next = path[i + 1]
neighbors[u].add(u_next)
neighbors[u_next].add(u)
return neighbors
def _phase2(self, neighbors):
transit = defaultdict(bool)
def put_transit(u1, u2):
transit[(u1, u2)] = True
for path in tqdm(self.paths, desc="Phase 2"):
j_max_degree = np.argmax([len(neighbors[u]) for u in path])
for i in range(j_max_degree):
put_transit(path[i], path[i + 1])
for i in range(j_max_degree, len(path) - 1):
put_transit(path[i + 1], path[i])
return transit
def _phase3(self, transit):
edges = {}
def is_transit(u1, u2):
return transit[(u1, u2)]
for path in tqdm(self.paths, desc="Phase 3"):
for i, u in enumerate(path[:-1]):
u_next = path[i + 1]
if is_transit(u, u_next) and is_transit(u_next, u):
edges[(u, u_next)] = SIBLING_TO_SIBLING
elif is_transit(u_next, u):
edges[(u, u_next)] = PROVIDER_TO_CUSTOMER
elif is_transit(u, u_next):
edges[(u, u_next)] = CUSTOMER_TO_PROVIDER
return edges
def print_stats(self):
total = sum(self.stats.values())
for tor, count in self.stats.items():
ratio = count/total
print(f'{tor}: {count}/{total} = {ratio}', file=sys.stderr)
class GaoGraphRefined(GaoGraphBasic):
def _phase2(self, neighbors):
transit = defaultdict(int)
def put_transit(u1, u2):
transit[(u1, u2)] += 1
for path in tqdm(self.paths, desc="Phase 2"):
j_max_degree = np.argmax([len(neighbors[u]) for u in path])
for i in range(j_max_degree):
put_transit(path[i], path[i + 1])
for i in range(j_max_degree, len(path) - 1):
put_transit(path[i + 1], path[i])
return transit
def _phase3(self, transit, L=1):
edges = {}
def get_transit(u1, u2):
return transit[(u1, u2)]
for path in tqdm(self.paths, desc="Phase 3"):
for i, u in enumerate(path[:-1]):
u_next = path[i + 1]
fwd_t = get_transit(u, u_next)
bwd_t = get_transit(u_next, u)
if ((fwd_t > L and bwd_t > L) or (0 < fwd_t <= L and 0 < bwd_t <= L)):
edges[(u, u_next)] = SIBLING_TO_SIBLING
elif bwd_t > L or fwd_t == 0:
edges[(u, u_next)] = PROVIDER_TO_CUSTOMER
elif fwd_t > L or bwd_t == 0:
edges[(u, u_next)] = CUSTOMER_TO_PROVIDER
return edges
class GaoGraphHeuristic(GaoGraphRefined):
def get_classified_edges(self):
neighbors = self._phase1()
transit = self._phase2(neighbors)
edges = self._phase3(transit)
not_peering = self._heuristic_phase2(neighbors, edges)
# Adds P2P relationships to edges
self._heuristic_phase3_writing_over_edges(neighbors, not_peering, edges)
return edges
def _heuristic_phase2(self, neighbors, edges):
not_peering = defaultdict(bool)
def put_not_peering(u1, u2):
not_peering[(u1, u2)] = True
for path in tqdm(self.paths, desc="Phase 4.1"):
j_max_degree = np.argmax([len(neighbors[u]) for u in path])
for i in range(j_max_degree - 1):
put_not_peering(path[i], path[i + 1])
for i in range(j_max_degree + 1, len(path) - 1):
put_not_peering(path[i], path[i + 1])
if j_max_degree == 0 or j_max_degree == len(path) - 1:
continue
uj = path[j_max_degree]
uj_prev = path[j_max_degree - 1]
edge_uj_prev = edges[(uj_prev, uj)]
uj_next = path[j_max_degree + 1]
edge_uj_next = edges[(uj, uj_next)]
if edge_uj_prev != SIBLING_TO_SIBLING and edge_uj_next != SIBLING_TO_SIBLING:
if len(neighbors[uj_prev]) > len(neighbors[uj_next]):
put_not_peering(uj, uj_next)
else:
put_not_peering(uj_prev, uj)
return not_peering
def _heuristic_phase3_writing_over_edges(self, neighbors, not_peering, edges, R=PARAMETER_R):
def deg(u):
return len(neighbors[u])
for path in tqdm(self.paths, desc="Phase 4.2"):
for i, u in enumerate(path[:-1]):
u_next = path[i + 1]
if ((not not_peering[(u, u_next)]) and (not not_peering[(u_next, u)]) and
(1/R < deg(u)/deg(u_next) < R)):
edges[(u, u_next)] = PEER_TO_PEER
return edges
def get_paths_from_file(filepath=None):
paths = []
f = sys.stdin
if filepath:
f = open(filepath)
for line in f:
l = line.rstrip()
path = l.split(' ')
if len(path) > 2:
paths.append(path)
return paths
def classify_edges_from_stdin():
print('Parsing paths...', file=sys.stderr)
paths = get_paths_from_file()
gh = GaoGraphHeuristic(paths)
for p, c in zip(gh.paths, gh.vf_class):
color = 'GREEN'
if not c:
color = 'RED'
print(' '.join(p) + ',' + color)
gh.print_stats()
return gh
def main():
classify_edges_from_stdin()
if __name__ == '__main__':
main()