forked from mehdiBezahaf/intent-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batfish_mininet.py
454 lines (372 loc) · 14.6 KB
/
batfish_mininet.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
451
452
453
454
"""Testing script for the combined batfish topology,
./batfish_mininet_template.py is the file used by the intent deployer during
testing.
"""
#!/usr/bin/env python
from __future__ import annotations
import os
from pathlib import Path
import re
import json
import signal
from typing import TYPE_CHECKING, TypedDict, Optional
from dataclasses import dataclass
from threading import Event
from mtv.link import TCLink
from mtv.net import Mininet
from mtv.rest import REST
from mtv.cli import CLI
from mtv.node import (
RemoteController,
OVSSwitch,
DefaultController,
OVSKernelSwitch,
Switch,
DynamipsRouter,
DynamipsHostRouter,
)
from mtv.node import Node as MTVNode
class Topology(TypedDict):
edges: list[Link]
class Link(TypedDict):
node1: Node
node2: Node
class Node(TypedDict):
hostname: str
interfaceName: str
@dataclass
class Router:
hostname: str
cfg: str
interfaces: dict[int, tuple[str, str, list[tuple[int, str]]]]
instance: Optional[Switch]
def add_interface(
self, pd: str, type_: str, slot: int, index: int, other_side: str
):
if slot in self.interfaces:
e_pd, e_type_, ports = self.interfaces[slot]
assert (e_pd == pd) and (e_type_ == type_)
ports.append((index, other_side))
else:
self.interfaces[slot] = (pd, type_, [(index, other_side)])
@dataclass
class Host:
hostname: str
interfaces: dict[str, tuple[str, str]]
instance: Optional[MTVNode]
net = Mininet(switch=OVSSwitch, build=False, topo=None, link=TCLink)
def do_inferred_topo():
switches = {
"s5": {"params": {"name": "s5", "dpid": "0000000000000005"}},
"s7": {"params": {"name": "s7", "dpid": "0000000000000007"}},
"s2": {"params": {"name": "s2", "dpid": "0000000000000002"}},
"s3": {"params": {"name": "s3", "dpid": "0000000000000003"}},
"s1": {"params": {"name": "s1", "dpid": "0000000000000001"}},
"s8": {"params": {"name": "s8", "dpid": "0000000000000008"}},
"s11": {"params": {"name": "s11", "dpid": "000000000000000b"}},
"s14": {"params": {"name": "s14", "dpid": "000000000000000e"}},
"s12": {"params": {"name": "s12", "dpid": "000000000000000c"}},
"s9": {"params": {"name": "s9", "dpid": "0000000000000009"}},
"s10": {"params": {"name": "s10", "dpid": "000000000000000a"}},
"s4": {"params": {"name": "s4", "dpid": "0000000000000004"}},
"s6": {"params": {"name": "s6", "dpid": "0000000000000006"}},
"s13": {"params": {"name": "s13", "dpid": "000000000000000d"}},
}
hosts = {
"h7": {"params": {"name": "h7", "ip": "10.0.0.7", "mac": "00:00:00:00:00:07"}},
"h9": {"params": {"name": "h9", "ip": "10.0.0.9", "mac": "00:00:00:00:00:09"}},
"h6": {"params": {"name": "h6", "ip": "10.0.0.6", "mac": "00:00:00:00:00:06"}},
"h5": {"params": {"name": "h5", "ip": "10.0.0.5", "mac": "00:00:00:00:00:05"}},
"h3": {"params": {"name": "h3", "ip": "10.0.0.3", "mac": "00:00:00:00:00:03"}},
"h4": {"params": {"name": "h4", "ip": "10.0.0.4", "mac": "00:00:00:00:00:04"}},
"h1": {"params": {"name": "h1", "ip": "10.0.0.1", "mac": "00:00:00:00:00:01"}},
"h2": {"params": {"name": "h2", "ip": "10.0.0.2", "mac": "00:00:00:00:00:02"}},
"h8": {"params": {"name": "h8", "ip": "10.0.0.8", "mac": "00:00:00:00:00:08"}},
}
switch_links = [
[{"name": "s10", "port": 6}, {"name": "s13", "port": 3}],
[{"name": "s10", "port": 3}, {"name": "s5", "port": 3}],
[{"name": "s2", "port": 3}, {"name": "s8", "port": 2}],
[{"name": "s1", "port": 2}, {"name": "s7", "port": 1}],
[{"name": "s12", "port": 8, "bw": 1000}, {"name": "s14", "port": 3}],
[{"name": "s6", "port": 3}, {"name": "s10", "port": 4}],
[{"name": "s7", "port": 3}, {"name": "s12", "port": 2}],
[{"name": "s6", "port": 2}, {"name": "s11", "port": 2}],
[{"name": "s12", "port": 3}, {"name": "s8", "port": 3}],
[{"name": "s7", "port": 2}, {"name": "s2", "port": 2}],
[{"name": "s14", "port": 4, "bw": 1000}, {"name": "s13", "port": 6}],
[{"name": "s13", "port": 4}, {"name": "s11", "port": 4}],
[{"name": "s10", "port": 5}, {"name": "s12", "port": 5}],
[{"name": "s11", "port": 5}, {"name": "s14", "port": 2}],
[{"name": "s9", "port": 5}, {"name": "s13", "port": 2}],
[{"name": "s10", "port": 2}, {"name": "s4", "port": 3}],
[{"name": "s9", "port": 3}, {"name": "s4", "port": 2}],
[{"name": "s12", "port": 7, "bw": 1000}, {"name": "s13", "port": 5}],
[{"name": "s3", "port": 2}, {"name": "s9", "port": 2}],
[{"name": "s3", "port": 3}, {"name": "s10", "port": 1}],
[{"name": "s9", "port": 4}, {"name": "s12", "port": 4}],
[{"name": "s11", "port": 1}, {"name": "s5", "port": 2}],
[{"name": "s8", "port": 1}, {"name": "s1", "port": 3}],
[{"name": "s11", "port": 3}, {"name": "s12", "port": 6}],
[{"name": "s9", "port": 1}, {"name": "s2", "port": 4}],
]
host_links = [
[
{"name": "h7", "mac": "00:00:00:00:00:07", "intf": "h7-eth0", "bw": 1000},
{"name": "s12", "port": 1},
],
[
{"name": "h9", "mac": "00:00:00:00:00:09", "intf": "h9-eth0", "bw": 1000},
{"name": "s14", "port": 1},
],
[
{"name": "h6", "mac": "00:00:00:00:00:06", "intf": "h6-eth0"},
{"name": "s6", "port": 1},
],
[
{"name": "h5", "mac": "00:00:00:00:00:05", "intf": "h5-eth0"},
{"name": "s5", "port": 1},
],
[
{"name": "h3", "mac": "00:00:00:00:00:03", "intf": "h3-eth0"},
{"name": "s3", "port": 1},
],
[
{"name": "h4", "mac": "00:00:00:00:00:04", "intf": "h4-eth0"},
{"name": "s4", "port": 1},
],
[
{"name": "h1", "mac": "00:00:00:00:00:01", "intf": "h1-eth0"},
{"name": "s1", "port": 1},
],
[
{"name": "h2", "mac": "00:00:00:00:00:02", "intf": "h2-eth0"},
{"name": "s2", "port": 1},
],
[
{"name": "h8", "mac": "00:00:00:00:00:08", "intf": "h8-eth0", "bw": 1000},
{"name": "s13", "port": 1},
],
]
net.addController("c0", controller=RemoteController, ip="127.0.0.1", port=6653)
for switch in switches.values():
switch["instance"] = net.addSwitch(cls=OVSKernelSwitch, batch=True, failMode="standalone", **switch["params"])
for host in hosts.values():
host["instance"] = net.addHost(**host["params"])
for src, dst in switch_links:
net.addLink(
switches[src["name"]]["instance"],
switches[dst["name"]]["instance"],
port1=src["port"],
port2=dst["port"],
bw=src.get("bw", 100),
)
for src, dst in host_links:
net.addLink(
hosts[src["name"]]["instance"],
switches[dst["name"]]["instance"],
port2=dst["port"],
bw=src.get("bw", 100),
)
def post_build():
for src, _ in host_links:
host = hosts[src["name"]]["instance"]
host.intf(src["intf"]).setMAC(src["mac"])
for name, host in hosts.items():
if name in ["h7", "h8", "h9"]:
continue
inst = host["instance"]
inst.cmd(f"ip route add 2.128.0.0/24 via 10.0.0.7")
inst.cmd(f"ip route add 2.128.1.0/24 via 10.0.0.7")
hosts["h7"]["instance"].cmd(f"iptables -t nat -I POSTROUTING -o h7-eth1 -j SNAT --to 2.128.0.101")
return post_build, hosts
############################
#
# batfish stuff
#
############################
dynamips_image = str(Path(__file__).parent / "batfish" / "c7200-adventerprisek9-mz.153-3.XB12.image")
def do_batfish():
root_dir = Path(__file__).parent / "batfish"
with open(root_dir / "example_layer1_topology.json") as f:
topo: Topology = json.load(f)
routers: dict[str, Router] = {}
hosts: dict[str, Host] = {}
links: list[tuple[str, str, Optional[int], Optional[int]]] = []
pd_map = {"GigabitEthernet": "PA-GE"}
def parse_host_interface(intf: str) -> Optional[int]:
m = re.match(r"eth(?P<port>\d+)", intf)
if m is None:
return None
return int(m.group("port"))
def parse_router_interface(intf: str) -> tuple[str, str, int, int]:
m = re.match(r"(?P<port_driver>\w+)(?P<slot>\d+)/(?P<index>\d+)", intf)
assert m is not None
type_ = m.group("port_driver")
slot = int(m.group("slot"))
index = int(m.group("index"))
pd = pd_map[type_]
return pd, type_, slot, index
def insert_router(node: Node, cfg_path: Path, other_side: str):
if node["hostname"] not in routers:
with open(cfg_path) as f:
cfg = f.read()
routers[node["hostname"]] = Router(node["hostname"], cfg, {}, None)
pd, type_, slot, index = parse_router_interface(node["interfaceName"])
routers[node["hostname"]].add_interface(pd, type_, slot, index, other_side)
def insert_host(node: Node):
if node["hostname"] not in hosts:
cfg_path = root_dir / "live" / "hosts" / f'{node["hostname"]}.json'
with open(cfg_path) as f:
cfg = json.load(f)
interfaces = {
o["name"]: (o["prefix"], o["gateway"])
for o in cfg["hostInterfaces"].values()
}
hosts[node["hostname"]] = Host(node["hostname"], interfaces, None)
def insert_node(node: Node, other_side: Node):
router_cfg_path = root_dir / "live" / "configs" / f'{node["hostname"]}.cfg'
if router_cfg_path.exists():
insert_router(node, router_cfg_path, other_side["hostname"])
else:
insert_host(node)
seen_links = set()
for link in topo["edges"]:
node1 = link["node1"]
node2 = link["node2"]
if (node1["hostname"], node2["hostname"]) in seen_links:
continue
if (node2["hostname"], node1["hostname"]) in seen_links:
continue
seen_links.add((node1["hostname"], node2["hostname"]))
links.append(
(
node1["hostname"],
node2["hostname"],
parse_host_interface(node1["interfaceName"]),
parse_host_interface(node2["interfaceName"]),
)
)
insert_node(node1, node2)
insert_node(node2, node1)
for i, router in enumerate(routers.values()):
ports = [
(pd, type_, slot, links)
for slot, (pd, type_, links) in router.interfaces.items()
]
router.instance = net.addSwitch(
router.hostname,
cls=DynamipsRouter,
dynamips_config=router.cfg,
dynamips_ports=ports,
dynamips_platform="7200",
dynamips_image=dynamips_image,
dynamips_args=[
"-i {}".format(i),
"--idle-pc=0x60630338",
"-P 7200",
"-o 64",
"-r 200",
],
)
for host in hosts.values():
host.instance = net.get(host.hostname) # type: ignore
for l, r, lp, rp in links:
if l == r:
print("skipping {}-{}, loop".format(l, r))
continue
net.addLink(l, r, lp, rp)
def post_start():
net.waitConnected()
for host in hosts.values():
for name, (prefix, gateway) in host.interfaces.items():
assert host.instance is not None
iname = f"{host.hostname}-{name}"
host.instance.setIP(prefix, intf=iname)
#host.instance.cmd("ip route add 11.0.0.0/8 via 10.0.0.50")
#host.instance.setDefaultRoute(f"dev {iname} via {gateway}")
# host.instance.cmd("ip route add 10.0.0.0/24 via eth0")
return post_start
def add_inter():
config = """hostname h50r
ip routing
router rip
version 2
network 10.0.0.0
network 11.0.0.0
interface FastEthernet 1/0
no cdp enable
ip address 10.0.0.50 255.0.0.0
no shut
interface GigabitEthernet 2/0
no cdp enable
ip address 10.0.0.50 255.0.0.0
shut
interface FastEthernet 3/0
no cdp enable
ip address 11.0.0.1 255.0.0.0
no shut
end"""
h50r = net.addHost(
"h50r",
ip="10.0.0.50",
cls=DynamipsHostRouter,
dynamips_platform="7200",
dynamips_image=dynamips_image,
dynamips_console_port=7123,
dynamips_args=[
"-i {}".format("h10"),
"--idle-pc=0x60630338",
"-P 7200",
"-o 64",
"-r 200",
],
dynamips_config=config,
dynamips_ports=[("PA-FE-TX", "FastEthernet", 1, [(0, "s6")]),
("PA-GE", "GigabitEthernet", 2, [(0, "s7")]),
("PA-FE-TX", "FastEthernet", 3, [(0, "h52")])],
)
h52 = net.addHost("h52", ip="11.0.0.52/8")
s6_h50r_link = net.addLink("s6", h50r, addr2="00:00:00:00:00:32")
s7_h50r_link = net.addLink("s7", h50r, addr2="00:00:00:00:00:33")
h52_h50r_link = net.addLink(h52, h50r)
def post_build(hosts):
pass
# h50r.setIP("10.0.0.50", intf=)
# h50.setIP("11.0.0.50/8", intf=h50_h50r_link.intf1)
# h51.setIP("11.1.0.51/8", intf=h51_h50r_link.intf1)
# h52.setIP("11.2.0.52/8", intf=h52_h50r_link.intf1)
# h52.cmd("ip route add 10.0.0.0/8 via 11.0.0.1")
# h50.cmd(f"iptables -t nat -I POSTROUTING -o {h50_h50r_link.intf1} -j SNAT --to 11.0.0.50")
# h51.cmd(f"iptables -t nat -I POSTROUTING -o {h51_h50r_link.intf1} -j SNAT --to 11.1.0.51")
def post_start(hosts): #
for host in hosts.values(): #
inst = host["instance"]
inst.cmd("ip route add 11.0.0.0/8 via 10.0.0.50")
h52.cmd("ip route add 10.0.0.0/8 via 11.0.0.1")
h50r.start()
h50r.cmd("nohup socat unix-listen:/tmp/h50r-sock,fork tcp-connect:127.0.0.1:7123 &")
# stupid hack, run the comand in s1
s1 = net.getNodeByName("s1")
s1.cmd("nohup socat tcp-listen:7123,reuseaddr,fork unix-connect:/tmp/h50r-sock &")
# for host in net.hosts:
# host.cmd("arp -s ")
return post_build, post_start
# h1 ip r add 2.128.0.0/24 via 10.0.0.2
# h1 ip r add 2.128.1.0/24 via 10.0.0.2
post_build, hosts = do_inferred_topo()
post_start = do_batfish()
post_build_2, post_start_2 = add_inter()
print("Added everything, starting up")
net.build()
post_build()
post_build_2(hosts)
net.start()
net.staticArp()
post_start()
post_start_2(hosts)
# net.pingAll()
print("Finished startup")
REST(net)
CLI(net)
net.stop()