Mathematics, philosophy, code, travel and everything in between. More about me…

I write about

Visualizing Linux Traffic Control Setup

In the past six months I had to deal with Linux traffic control (TC) a lot. I was literally blown away by the advanced features of the system. Of course, the versatility comes for the usual price: complexity. I definitely do not regret the time spent on understanding the principles of classes, qdiscs, filters, major and minor numbers, etc. However, I think I have found a way to work with them more easily.

When you want to see your TC configuration in full, you normally have to list qdiscs, classes, and filters separately and mentally piece the outputs together. For example:

$ tc qdisc show dev eth0
qdisc htb 1: root r2q 10 default 10 direct_packets_stat 0
qdisc sfq 10: parent 1:10 limit 127p quantum 1514b perturb 10sec
qdisc sfq 11: parent 1:11 limit 127p quantum 1514b perturb 10sec
qdisc sfq 19: parent 1:19 limit 127p quantum 1514b perturb 10sec
qdisc sfq 31: parent 1:31 limit 127p quantum 1514b perturb 10sec
$ tc class show dev eth0
class htb 1:11 parent 1:1 leaf 11: prio 0 rate 256000bit ceil 256000bit burst 15Kb cburst 1599b
class htb 1:10 parent 1:1 leaf 10: prio 0 rate 128000bit ceil 128000bit burst 15Kb cburst 1599b
class htb 1:1 root rate 10000Kbit ceil 10000Kbit burst 15Kb cburst 1600b
class htb 1:31 parent 1:1 leaf 31: prio 0 rate 128000bit ceil 128000bit burst 15Kb cburst 1599b
class htb 1:19 parent 1:1 leaf 19: prio 0 rate 512000bit ceil 512000bit burst 15Kb cburst 1599b
$ tc filter show dev eth0
filter parent 1: protocol ip pref 1 fw
filter parent 1: protocol ip pref 1 fw handle 0x1 classid 1:11
filter parent 1: protocol ip pref 1 fw handle 0x9 classid 1:19
filter parent 1: protocol ip pref 1 fw handle 0x15 classid 1:31

Since there is a certain hierarchy in TC configurations, I soon found myself drawing TC setups in trees. The idea to replace paper and pencil with Graphviz came not long after. Writing a simple script that parses the output of tc and converts it into a graph turned out to be surprisingly easy. Behold the glory of 181 lines of Python that beareth the name tcviz.

Note: Actually, I found a PERL script somewhere on the net that was doing precisely the same. However, the output was dead ugly for my eye. Tweaking the script was out of question since the PERL source looked even uglier. Hence, I took owls to Athens and wrote my own piece of software. It was fun.

The above configuration can be magically translated by tcviz into this picture:

tcviz-generated graph example

Qdiscs and classes are displayed as boxes and ellipses, respectively. Filters are represented by arrows that point from the parent to the target of the filter.

The script only generates commands for dot (a part of Graphviz). To actually generate the graph, the output of tcviz has to be passed to dot. For instance:

$ ./tcviz.py eth0 | dot -Tpng > tc.png

To display the graph without saving it, one could use display from the ImageMagick package:

$ ./tcviz.py eth0 | dot -Tpng | display

tcviz is still in early stage of development. I have only tested it with tc versions iproute2-ss060323 and iproute2-ss071016. It’s possible that the format of tc’s output is different in other versions and tcviz won’t understand it. Bug reports are welcome. I even have a vague development plan, so there might be some new features in the future (not just bug-fix releases).

Feel free to give tcviz a try: github.com/ze-phyr-us/tcviz

P.S.: The best resource on TC out there is Linux Advanced Routing & Traffic Control.

April 5, MMIX — Programming, Linux, Projects, Python.