-
Notifications
You must be signed in to change notification settings - Fork 1
/
birdviz.py
executable file
·216 lines (175 loc) · 7.89 KB
/
birdviz.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import birdconfig
from collections import defaultdict
import pygraphviz as pgv
import argparse
parser = argparse.ArgumentParser(description="BIRD/BIRD6 config visualizer")
parser.add_argument("--compress", "-c", action="count", default=0, help="compress templated protocols into single nodes and approximate their imports/exports")
parser.add_argument("--group", "-g", metavar="TYPE", type=lambda x: set(x.split(',')), default=set(), help="group nodes of the given comma-separated types (table, template, static, kernel, bgp, ...) on the same rank")
parser.add_argument("infile", metavar="FILE", help="config file to visualize")
args = parser.parse_args()
config = birdconfig.parse(open(args.infile))
graph = pgv.AGraph(layout="dot", rankdir="LR", fontname="Monospace", fontsize=22, label="<<b>Router {}</b>>".format(config["router"][-1][1]), labelloc="t", directed=True, strict=False)
graph.node_attr["fontname"] = "Monospace"
graph.edge_attr["fontname"] = "Monospace"
graph.edge_attr["fontsize"] = 8
def parse_filter(p):
if p is None:
return None
if p[0] == "all":
return True
if p[0] == "none":
return False
if p[0] == "filter":
if isinstance(p[1], str):
return p[1]
else:
return "(filter)"
if p[0] == "where":
return "(filter)"
def filter_edge(flt):
res = dict(key=flt)
if isinstance(flt, str):
res.update(style="dashed")
if flt != "(filter)":
res.update(label="<<i>{}</i>>".format(flt))
return res
def find_key(p, key, default=None):
def recur(p):
if key in p:
return p[key]
elif "_template" in p:
return recur(templates[p["_template"]])
else:
return default
return recur(p)
def find_option(p, default_template, use_default=True):
option = birdconfig.parse(default_template)
# default_template may only contain one config line
assert(len(option.keys()) == 1)
option_name = next(iter(option.keys()))
default = option[option_name] if use_default else [None]
option = find_key(p, option_name, default=default)
return option[-1]
def find_switch(p, option_name):
return find_option(p, option_name, use_default=False) != None
# create table nodes
rank_groups = defaultdict(lambda: graph)
for g in args.group:
rank_groups[g] = graph.add_subgraph(name="group_" + g, rank="same")
tables = set(table[0] for table in config["table"])
tables.add("master")
for table in tables:
rank_groups["table"].add_node("table_{}".format(table), label="<<b>table {}</b>>".format(table), color="red", shape="oval")
# create template clusters/nodes
template_ids = defaultdict(lambda: 0)
templates = {}
for _t in config["template"]:
protocol = _t[0]
if len(_t) > 2:
name = _t[1]
else:
template_ids[protocol] += 1
name = protocol + str(template_ids[protocol])
template = _t[-1]
template["_protocol"] = protocol
if len(_t) > 3 and _t[2] == "from":
template["_template"] = _t[3]
label = "<font point-size='16'><b>template {}</b></font>".format(name)
if args.compress >= 1:
template["_node"] = "template_" + name
graph.add_node(template["_node"], label="<{}<br/>>".format(label), shape="box")
rank_groups["template"].add_node(graph.get_node(template["_node"]))
if "_template" in template:
# TODO: test this
graph.add_edge(template["_node"], template["_template"]["_node"])
else:
subgraph = find_key(template, "_subgraph", default=graph)
template["_subgraph"] = subgraph.add_subgraph(name="cluster_" + name, label="<{}>".format(label))
templates[name] = template
# create instance nodes
instance_ids = defaultdict(lambda: 0)
instances = {}
for _p in config["protocol"]:
protocol = _p[0]
if len(_p) > 2:
name = _p[1]
else:
instance_ids[protocol] += 1
name = protocol + str(instance_ids[protocol])
instance = _p[-1]
instance["_protocol"] = protocol
if len(_p) > 3 and _p[2] == "from":
instance["_template"] = _p[3]
instances[name] = instance
if protocol != "pipe":
if protocol == "static":
label = "<br/>".join(" ".join(route) for route in find_key(instance, "route", default=set()))
elif protocol == "device":
label = ""
elif protocol == "direct":
label = "<br/>".join("<br/>".join(interfaces) for interfaces in find_key(instance, "interface", default=set()))
elif protocol == "radv":
label = "<br/>".join("<br/>".join(interfaces) for interfaces in find_key(instance, "interface", default=set()))
elif protocol == "kernel":
kernel_table = find_option(instance, "kernel table none;", use_default=False)
if kernel_table:
label = "kernel table " + kernel_table[1]
else:
label = ""
elif protocol == "bgp":
label = "neighbor " + " ".join(find_option(instance, "neighbor none;", use_default=False))
elif protocol == "ospf":
label = "<br/>".join(area + ": " + " ".join(interface for interface, interface_config in area_config["interface"])
for area, area_config in find_key(instance, "area", default=set()))
else:
label = ""
label = "<b>{} {}</b><br/>{}".format(protocol, name, label)
if args.compress >= 1 and "_template" in instance:
node = graph.get_node(find_key(instance, "_node"))
if args.compress == 1:
node.attr["label"] = "<{}<br/>{}>".format(node.attr["label"], label)
else:
instance["_node"] = "proto_" + name
subgraph = find_key(instance, "_subgraph", default=graph)
subgraph.add_node(instance["_node"], label="<{}>".format(label), color="blue", shape="box")
rank_groups[protocol].add_node(subgraph.get_node(instance["_node"]))
# create edges
for name, instance in instances.items():
table = find_option(instance, "table master;", use_default=True)[0]
import_mode = parse_filter(find_option(instance, "import all;", use_default=True))
export_mode = parse_filter(find_option(instance, "export none;", use_default=True))
if instance["_protocol"] == "pipe":
peer_table = find_option(instance, "peer table master;", use_default=True)[1]
if import_mode:
graph.add_edge("table_" + peer_table, "table_" + table, **filter_edge(import_mode))
if export_mode:
graph.add_edge("table_" + table, "table_" + peer_table, **filter_edge(export_mode))
else:
if instance["_protocol"] == "device":
# device protocol never exports or import routes
export_mode = False
import_mode = False
if instance["_protocol"] == "radv":
# radv protocol never import routes
import_mode = False
if instance["_protocol"] == "kernel":
# kernel protocol imports alien routes only when learn; is specified
if not find_switch(instance, "learn;"):
if import_mode == True:
import_mode = "(no alien)"
elif isinstance(import_mode, str):
import_mode = import_mode + " w/o alien"
# ... and exports device routes only when device routes; is specified
if not find_switch(instance, "device routes;"):
if export_mode == True:
export_mode = "(no device routes)"
elif isinstance(export_mode, str):
export_mode = export_mode + " w/o device routes"
if import_mode:
graph.add_edge(find_key(instance, "_node"), "table_" + table, **filter_edge(import_mode))
if export_mode:
graph.add_edge("table_" + table, find_key(instance, "_node"), **filter_edge(export_mode))
print(graph.string())