forked from mehdiBezahaf/intent-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatfish_mininet_template.py
378 lines (306 loc) · 12.3 KB
/
batfish_mininet_template.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
"""Template mininet topology used by the intent deployer to construct the
testing network.
"""
from __future__ import annotations
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.net import Mininet
from mtv.rest import REST
from mtv.cli import CLI
from mtv.node import (
RemoteController,
OVSSwitch,
DefaultController,
OVSKernelSwitch,
Switch,
DynamipsRouter,
)
from mtv.node import Node as MTVNode
config_patches: dict[str, str] = {config_patches}
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)
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}, {"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}, {"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}, {"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"},
{"name": "s12", "port": 1},
],
[
{"name": "h9", "mac": "00:00:00:00:00:09", "intf": "h9-eth0"},
{"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"},
{"name": "s13", "port": 1},
],
]
c1 = net.addController("c1", controller=RemoteController, port=6633)
# c1 = net.addController("c1")
for switch in switches.values():
switch["instance"] = net.addSwitch(**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"],
)
for src, dst in host_links:
net.addLink(
hosts[src["name"]]["instance"],
switches[dst["name"]]["instance"],
port2=dst["port"],
)
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"]:
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.8")
hosts["h7"]["instance"].cmd(f"iptables -t nat -I POSTROUTING -o h7-eth1 -j SNAT --to 2.128.0.101")
hosts["h8"]["instance"].cmd(f"iptables -t nat -I POSTROUTING -o h8-eth1 -j SNAT --to 2.128.1.101")
hosts["h7"]["instance"].cmd(f"ip route add 2.128.1.0/24 via 10.0.0.8")
hosts["h8"]["instance"].cmd(f"ip route add 2.128.0.0/24 via 10.0.0.7")
c1.start()
for switch in switches.values():
switch["instance"].start([c1])
return post_build, hosts
############################
#
# batfish stuff
#
############################
def do_batfish():
root_dir = Path("/extra/")
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()
]
config = config_patches.get(router.hostname, router.cfg)
router.instance = net.addSwitch(
router.hostname,
cls=DynamipsRouter,
dynamips_config=config,
dynamips_ports=ports,
dynamips_platform="7200",
dynamips_image=str(root_dir / "c7200-adventerprisek9-mz.153-3.XB12.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.setDefaultRoute(f"dev {iname} via {gateway}")
# host.instance.cmd("ip route add 10.0.0.0/24 via eth0")
return 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()
net.build()
post_build()
net.start()
net.staticArp()
post_start()
REST(net)
exit = Event()
def quit(signo, _frame):
exit.set()
for sig in ("TERM", "HUP", "INT"):
signal.signal(getattr(signal, "SIG" + sig), quit)
exit.wait()
net.stop()