This is a tiny example that shows how tcc and tcsim are typically used with the traffic control subsystem in the Linux kernel.
The configuration shall drop all packets leaving the router via the interface eth0, unless they originate from the HTTP port.
dev eth0 { egress { drop if tcp_sport != PORT_HTTP; } }
tcc -r example.tc >example.sh
The -r switch tells tcc to emit a command to remove any traffic control setup that may be in use at eth0 before trying to add the new configuration.
You can look at the resulting output with cat example.sh
,
but it will look rather cryptic.
First, we create a file example.tcsim:
#include "packet.def" #include "ports.tc" dev eth0 100 Mbps { #include "example.tc" } send TCP_PCK($tcp_sport = PORT_HTTP); send TCP_PCK($tcp_sport = PORT_SSH); end
This defines a simulation environment with one interface called eth0, running at 100 Mbps, to which the example configuration is applied. The simulation consists of sending two packets, and waiting until the system is idle again.
tcsim -s 22 example.tcsim
We use the -s option to reduce the output generated by tcsim. The output looks like this:
0.000000 E : 0x80bd560 40 : eth0: 45000028 00000000 40060000 0a000001 0a000002 0050 ... 0.000000 D : 0x80bd560 40 : eth0: 45000028 00000000 40060000 0a000001 0a000002 0050 ... 0.000000 E : 0x80bd870 40 : eth0: 45000028 00000000 40060000 0a000001 0a000002 0016 ... 0.000000 * : 0x80bd870 40 : eth0: enqueue returns POLICED (3)
sh example.sh
Note: this step only works if traffic control is configured into the kernel and if a recent version of iproute2/tc with support for Differentiated Services is installed.1.1
More information about writing tcng configurations can be found in chapter 2 and the following chapters. tcc usage is described in detail in chapter 5, and tcsim is described in chapter 6.