-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutingTable.py
More file actions
217 lines (181 loc) · 6.3 KB
/
RoutingTable.py
File metadata and controls
217 lines (181 loc) · 6.3 KB
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
def ip_to_int(ip):
parts = ip.split(".")
value = 0
for p in parts:
value = (value << 8) | int(p)
return value
def int_to_ip(value):
return ".".join(str((value >> shift) & 255) for shift in (24, 16, 8, 0))
def prefixlen_to_mask(prefixlen):
if prefixlen == 0:
return 0
return (0xFFFFFFFF << (32 - prefixlen)) & 0xFFFFFFFF
class Route:
def __init__(self, network, netmask, peer, localpref, selfOrigin, ASPath, origin):
self.network = network
self.netmask = netmask
self.peer = peer
self.localpref = localpref
self.selfOrigin = selfOrigin
self.ASPath = ASPath
self.origin = origin
def to_dict(self):
return {
"network": self.network,
"netmask": self.netmask,
"peer": self.peer,
"localpref": self.localpref,
"selfOrigin": self.selfOrigin,
"ASPath": self.ASPath,
"origin": self.origin,
}
def attrs_match(self, other):
return (
self.peer == other.peer
and self.localpref == other.localpref
and self.selfOrigin == other.selfOrigin
and self.ASPath == other.ASPath
and self.origin == other.origin
)
class RoutingTable:
def __init__(self):
self.routes = []
self._update_cache = []
self._withdraw_cache = []
def add_route(self, updateMsg):
msg = updateMsg["msg"]
peer = updateMsg["src"]
route = Route(
network=msg["network"],
netmask=msg["netmask"],
peer=peer,
localpref=msg["localpref"],
selfOrigin=msg["selfOrigin"],
ASPath=list(msg["ASPath"]),
origin=msg["origin"],
)
self._update_cache.append(updateMsg)
self.routes.append(route)
self._aggregate()
def remove_route(self, withdrawMsg):
self._withdraw_cache.append(withdrawMsg)
self._rebuild()
def lookup(self, dst_ip):
dst_int = ip_to_int(dst_ip)
best = []
best_prefix_len = -1
for route in self.routes:
net_int = ip_to_int(route.network)
mask_int = ip_to_int(route.netmask)
prefix_len = bin(mask_int).count("1")
if (dst_int & mask_int) == net_int:
if prefix_len > best_prefix_len:
best_prefix_len = prefix_len
best = [route]
elif prefix_len == best_prefix_len:
best.append(route)
if not best:
return None
if len(best) == 1:
return best[0]
return self._break_tie(best)
def dump(self):
return [r.to_dict() for r in self.routes]
def _rebuild(self):
withdrawn = set()
for w in self._withdraw_cache:
peer = w["src"]
for entry in w["msg"]:
withdrawn.add((peer, entry["network"], entry["netmask"]))
self.routes = []
for update in self._update_cache:
peer = update["src"]
msg = update["msg"]
key = (peer, msg["network"], msg["netmask"])
if key not in withdrawn:
self.routes.append(
Route(
network=msg["network"],
netmask=msg["netmask"],
peer=peer,
localpref=msg["localpref"],
selfOrigin=msg["selfOrigin"],
ASPath=list(msg["ASPath"]),
origin=msg["origin"],
)
)
self._aggregate()
def _aggregate(self):
changed = True
while changed:
changed = False
new_routes = []
used = set()
for i in range(len(self.routes)):
if i in used:
continue
r1 = self.routes[i]
merged = False
for j in range(i + 1, len(self.routes)):
if j in used:
continue
agg = self._try_aggregate(r1, self.routes[j])
if agg is not None:
new_routes.append(agg)
used.add(i)
used.add(j)
merged = True
changed = True
break
if not merged:
new_routes.append(r1)
self.routes = new_routes
def _try_aggregate(self, r1, r2):
if not r1.attrs_match(r2):
return None
n1 = ip_to_int(r1.network)
n2 = ip_to_int(r2.network)
m1 = ip_to_int(r1.netmask)
m2 = ip_to_int(r2.netmask)
if m1 != m2:
return None
prefix_len = bin(m1).count("1")
if prefix_len == 0:
return None
diff_bit = 1 << (32 - prefix_len)
if n1 ^ n2 != diff_bit:
return None
parent = min(n1, n2)
new_mask = prefixlen_to_mask(prefix_len - 1)
return Route(
network=int_to_ip(parent),
netmask=int_to_ip(new_mask),
peer=r1.peer,
localpref=r1.localpref,
selfOrigin=r1.selfOrigin,
ASPath=r1.ASPath,
origin=r1.origin,
)
def _break_tie(self, candidates):
max_lp = max(r.localpref for r in candidates)
candidates = [r for r in candidates if r.localpref == max_lp]
if len(candidates) == 1:
return candidates[0]
self_true = [r for r in candidates if r.selfOrigin]
if self_true:
candidates = self_true
if len(candidates) == 1:
return candidates[0]
min_len = min(len(r.ASPath) for r in candidates)
candidates = [r for r in candidates if len(r.ASPath) == min_len]
if len(candidates) == 1:
return candidates[0]
origin_rank = {"IGP": 0, "EGP": 1, "UNK": 2}
best_orig = min(origin_rank.get(r.origin, 2) for r in candidates)
candidates = [
r for r in candidates if origin_rank.get(r.origin, 2) == best_orig
]
if len(candidates) == 1:
return candidates[0]
candidates.sort(key=lambda r: ip_to_int(r.peer))
return candidates[0]