Blame doc/actions/actions-general

Packit Service 3880ab
Packit Service 3880ab
This documented is slightly dated but should give you idea of how things
Packit Service 3880ab
work.
Packit Service 3880ab
Packit Service 3880ab
What is it?
Packit Service 3880ab
-----------
Packit Service 3880ab
Packit Service 3880ab
An extension to the filtering/classification architecture of Linux Traffic
Packit Service 3880ab
Control.
Packit Service 3880ab
Up to 2.6.8 the only action that could be "attached" to a filter was policing.
Packit Service 3880ab
i.e you could say something like:
Packit Service 3880ab
Packit Service 3880ab
-----
Packit Service 3880ab
tc filter add dev lo parent ffff: protocol ip prio 10 u32 match ip src \
Packit Service 3880ab
127.0.0.1/32 flowid 1:1 police mtu 4000 rate 1500kbit burst 90k
Packit Service 3880ab
-----
Packit Service 3880ab
Packit Service 3880ab
which implies "if a packet is seen on the ingress of the lo device with
Packit Service 3880ab
a source IP address of 127.0.0.1/32 we give it a classification id  of 1:1 and
Packit Service 3880ab
we execute a policing action which rate limits its bandwidth utilization
Packit Service 3880ab
to 1.5Mbps".
Packit Service 3880ab
Packit Service 3880ab
The new extensions allow for more than just policing actions to be added.
Packit Service 3880ab
They are also fully backward compatible. If you have a kernel that doesn't
Packit Service 3880ab
understand them, then the effect is null i.e if you have a newer tc
Packit Service 3880ab
but older kernel, the actions are not installed. Likewise if you
Packit Service 3880ab
have a newer kernel but older tc, obviously the tc will use current
Packit Service 3880ab
syntax which will work fine. Of course to get the required effect you need
Packit Service 3880ab
both newer tc and kernel. If you are reading this you have the
Packit Service 3880ab
right tc ;->
Packit Service 3880ab
Packit Service 3880ab
A side effect is that we can now get stateless firewalling to work with tc.
Packit Service 3880ab
Essentially this is now an alternative to iptables.
Packit Service 3880ab
I won't go into details of my dislike for iptables at times, but
Packit Service 3880ab
scalability is one of the main issues; however, if you need stateful
Packit Service 3880ab
classification - use netfilter (for now).
Packit Service 3880ab
Packit Service 3880ab
This stuff works on both ingress and egress qdiscs.
Packit Service 3880ab
Packit Service 3880ab
Features
Packit Service 3880ab
--------
Packit Service 3880ab
Packit Service 3880ab
1) new additional syntax and actions enabled. Note old syntax is still valid.
Packit Service 3880ab
Packit Service 3880ab
Essentially this is still the same syntax as tc with a new construct
Packit Service 3880ab
"action". The syntax is of the form:
Packit Service 3880ab
tc filter add <DEVICE> parent 1:0 protocol ip prio 10 <Filter description>
Packit Service 3880ab
flowid 1:1 action <ACTION description>*
Packit Service 3880ab
Packit Service 3880ab
You can have as many actions as you want (within sensible reasoning).
Packit Service 3880ab
Packit Service 3880ab
In the past the only real action was the policer; i.e you could do something
Packit Service 3880ab
along the lines of:
Packit Service 3880ab
tc filter add dev lo parent ffff: protocol ip prio 10 u32 \
Packit Service 3880ab
match ip src 127.0.0.1/32 flowid 1:1 \
Packit Service 3880ab
police mtu 4000 rate 1500kbit burst 90k
Packit Service 3880ab
Packit Service 3880ab
Although you can still use the same syntax, now you can say:
Packit Service 3880ab
Packit Service 3880ab
tc filter add dev lo parent 1:0 protocol ip prio 10 u32 \
Packit Service 3880ab
match ip src 127.0.0.1/32 flowid 1:1 \
Packit Service 3880ab
action police mtu 4000 rate 1500kbit burst 90k
Packit Service 3880ab
Packit Service 3880ab
" generic Actions" (gact) at the moment are:
Packit Service 3880ab
{ drop, pass, reclassify, continue}
Packit Service 3880ab
(If you have others, no listed here give me a reason and we will add them)
Packit Service 3880ab
+drop says to drop the packet
Packit Service 3880ab
+pass and ok (are equivalent) says to accept it
Packit Service 3880ab
+reclassify requests for reclassification of the packet
Packit Service 3880ab
+continue requests for next lookup to match
Packit Service 3880ab
Packit Service 3880ab
2)In order to take advantage of some of the targets written by the
Packit Service 3880ab
iptables people, a classifier can have a packet being massaged by an
Packit Service 3880ab
iptable target. I have only tested with mangler targets up to now.
Packit Service 3880ab
(infact anything that is not in the mangling table is disabled right now)
Packit Service 3880ab
Packit Service 3880ab
In terms of hooks:
Packit Service 3880ab
*ingress is mapped to pre-routing hook
Packit Service 3880ab
*egress is mapped to post-routing hook
Packit Service 3880ab
I don't see much value in the other hooks, if you see it and email me good
Packit Service 3880ab
reasons, the addition is trivial.
Packit Service 3880ab
Packit Service 3880ab
Example syntax for iptables targets usage becomes:
Packit Service 3880ab
tc filter add ..... u32 <u32 syntax> action ipt -j <iptables target syntax>
Packit Service 3880ab
Packit Service 3880ab
example:
Packit Service 3880ab
tc filter add dev lo parent ffff: protocol ip prio 8 u32 \
Packit Service 3880ab
match ip dst 127.0.0.8/32 flowid 1:12 \
Packit Service 3880ab
action ipt -j mark --set-mark 2
Packit Service 3880ab
Packit Service 3880ab
NOTE: flowid 1:12 is parsed flowid 0x1:0x12.  Make sure if you want flowid
Packit Service 3880ab
decimal 12, then use flowid 1:c.
Packit Service 3880ab
Packit Service 3880ab
3) A feature i call pipe
Packit Service 3880ab
The motivation is derived from Unix pipe mechanism but applied to packets.
Packit Service 3880ab
Essentially take a matching packet and pass it through
Packit Service 3880ab
action1 | action2 | action3 etc.
Packit Service 3880ab
You could do something similar to this with the tc policer and the "continue"
Packit Service 3880ab
operator but this rather restricts it to just the policer and requires
Packit Service 3880ab
multiple rules (and lookups, hence quiet inefficient);
Packit Service 3880ab
Packit Service 3880ab
as an example -- and please note that this is just an example _not_ The
Packit Service 3880ab
Word Youve Been Waiting For (yes i have had problems giving examples
Packit Service 3880ab
which ended becoming dogma in documents and people modifying them a little
Packit Service 3880ab
to look clever);
Packit Service 3880ab
Packit Service 3880ab
i selected the metering rates to be small so that i can show better how
Packit Service 3880ab
things work.
Packit Service 3880ab
Packit Service 3880ab
The script below does the following:
Packit Service 3880ab
- an incoming packet from 10.0.0.21 is first given a firewall mark of 1.
Packit Service 3880ab
Packit Service 3880ab
- It is then metered to make sure it does not exceed its allocated rate of
Packit Service 3880ab
1Kbps. If it doesn't exceed rate, this is where we terminate action execution.
Packit Service 3880ab
Packit Service 3880ab
- If it does exceed its rate, its "color" changes to a mark of 2 and it is
Packit Service 3880ab
then passed through a second meter.
Packit Service 3880ab
Packit Service 3880ab
-The second meter is shared across all flows on that device [i am surpised
Packit Service 3880ab
that this seems to be not a well know feature of the policer; Bert was telling
Packit Service 3880ab
me that someone was writing a qdisc just to do sharing across multiple devices;
Packit Service 3880ab
it must be the summer heat again; weve had someone doing that every year around
Packit Service 3880ab
summer  -- the key to sharing is to use a operator "index" in your policer
Packit Service 3880ab
rules (example "index 20"). All your rules have to use the same index to
Packit Service 3880ab
share.]
Packit Service 3880ab
Packit Service 3880ab
-If the second meter is exceeded the color of the flow changes further to 3.
Packit Service 3880ab
Packit Service 3880ab
-We then pass the packet to another meter which is shared across all devices
Packit Service 3880ab
in the system. If this meter is exceeded we drop the packet.
Packit Service 3880ab
Packit Service 3880ab
Note the mark can be used further up the system to do things like policy
Packit Service 3880ab
or more interesting things on the egress.
Packit Service 3880ab
Packit Service 3880ab
------------------ cut here -------------------------------
Packit Service 3880ab
#
Packit Service 3880ab
# Add an ingress qdisc on eth0
Packit Service 3880ab
tc qdisc add dev eth0 ingress
Packit Service 3880ab
#
Packit Service 3880ab
#if you see an incoming packet from 10.0.0.21
Packit Service 3880ab
tc filter add dev eth0 parent ffff: protocol ip prio 1 \
Packit Service 3880ab
u32 match ip src 10.0.0.21/32 flowid 1:15 \
Packit Service 3880ab
#
Packit Service 3880ab
# first give it a mark of 1
Packit Service 3880ab
action ipt -j mark --set-mark 1 index 2 \
Packit Service 3880ab
#
Packit Service 3880ab
# then pass it through a policer which allows 1kbps; if the flow
Packit Service 3880ab
# doesn't exceed that rate, this is where we stop, if it exceeds we
Packit Service 3880ab
# pipe the packet to the next action
Packit Service 3880ab
action police rate 1kbit burst 9k pipe \
Packit Service 3880ab
#
Packit Service 3880ab
# which marks the packet fwmark as 2 and pipes
Packit Service 3880ab
action ipt -j mark --set-mark 2 \
Packit Service 3880ab
#
Packit Service 3880ab
# next attempt to borrow b/width from a meter
Packit Service 3880ab
# used across all flows incoming on eth0("index 30")
Packit Service 3880ab
# and if that is exceeded we pipe to the next action
Packit Service 3880ab
action police index 30 mtu 5000 rate 1kbit burst 10k pipe \
Packit Service 3880ab
# mark it as fwmark 3 if exceeded
Packit Service 3880ab
action ipt -j mark --set-mark 3 \
Packit Service 3880ab
# and then attempt to borrow from a meter used by all devices in the
Packit Service 3880ab
# system. Should this be exceeded, drop the packet on the floor.
Packit Service 3880ab
action police index 20 mtu 5000 rate 1kbit burst 90k drop
Packit Service 3880ab
---------------------------------
Packit Service 3880ab
Packit Service 3880ab
Now lets see the actions installed with
Packit Service 3880ab
"tc filter show parent ffff: dev eth0"
Packit Service 3880ab
Packit Service 3880ab
-------- output -----------
Packit Service 3880ab
jroot# tc filter show parent ffff: dev eth0
Packit Service 3880ab
filter protocol ip pref 1 u32
Packit Service 3880ab
filter protocol ip pref 1 u32 fh 800: ht divisor 1
Packit Service 3880ab
filter protocol ip pref 1 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:15
Packit Service 3880ab
Packit Service 3880ab
   action order 1: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x1  index 2
Packit Service 3880ab
Packit Service 3880ab
   action order 2: police 1 action pipe rate 1Kbit burst 9Kb mtu 2Kb
Packit Service 3880ab
Packit Service 3880ab
   action order 3: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x2  index 1
Packit Service 3880ab
Packit Service 3880ab
   action order 4: police 30 action pipe rate 1Kbit burst 10Kb mtu 5000b
Packit Service 3880ab
Packit Service 3880ab
   action order 5: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x3  index 3
Packit Service 3880ab
Packit Service 3880ab
   action order 6: police 20 action drop rate 1Kbit burst 90Kb mtu 5000b
Packit Service 3880ab
Packit Service 3880ab
  match 0a000015/ffffffff at 12
Packit Service 3880ab
-------------------------------
Packit Service 3880ab
Packit Service 3880ab
Note the ordering of the actions is based on the order in which we entered
Packit Service 3880ab
them. In the future i will add explicit priorities.
Packit Service 3880ab
Packit Service 3880ab
Now lets run a ping -f from 10.0.0.21 to this host; stop the ping after
Packit Service 3880ab
you see a few lines of dots
Packit Service 3880ab
Packit Service 3880ab
----
Packit Service 3880ab
[root@jzny hadi]# ping -f  10.0.0.22
Packit Service 3880ab
PING 10.0.0.22 (10.0.0.22): 56 data bytes
Packit Service 3880ab
....................................................................................................................................................................................................................................................................................................................................................................................................................................................
Packit Service 3880ab
--- 10.0.0.22 ping statistics ---
Packit Service 3880ab
2248 packets transmitted, 1811 packets received, 19% packet loss
Packit Service 3880ab
round-trip min/avg/max = 0.7/9.3/20.1 ms
Packit Service 3880ab
-----------------------------
Packit Service 3880ab
Packit Service 3880ab
Now lets take a look at the stats with "tc -s filter show parent ffff: dev eth0"
Packit Service 3880ab
Packit Service 3880ab
--------------
Packit Service 3880ab
jroot# tc -s filter show parent ffff: dev eth0
Packit Service 3880ab
filter protocol ip pref 1 u32
Packit Service 3880ab
filter protocol ip pref 1 u32 fh 800: ht divisor 1
Packit Service 3880ab
filter protocol ip pref 1 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1
Packit Service 3880ab
5
Packit Service 3880ab
Packit Service 3880ab
   action order 1: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x1  index 2
Packit Service 3880ab
         Sent 188832 bytes 2248 pkts (dropped 0, overlimits 0)
Packit Service 3880ab
Packit Service 3880ab
   action order 2: police 1 action pipe rate 1Kbit burst 9Kb mtu 2Kb
Packit Service 3880ab
         Sent 188832 bytes 2248 pkts (dropped 0, overlimits 2122)
Packit Service 3880ab
Packit Service 3880ab
   action order 3: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x2  index 1
Packit Service 3880ab
         Sent 178248 bytes 2122 pkts (dropped 0, overlimits 0)
Packit Service 3880ab
Packit Service 3880ab
   action order 4: police 30 action pipe rate 1Kbit burst 10Kb mtu 5000b
Packit Service 3880ab
         Sent 178248 bytes 2122 pkts (dropped 0, overlimits 1945)
Packit Service 3880ab
Packit Service 3880ab
   action order 5: tablename: mangle  hook: NF_IP_PRE_ROUTING
Packit Service 3880ab
        target MARK set 0x3  index 3
Packit Service 3880ab
         Sent 163380 bytes 1945 pkts (dropped 0, overlimits 0)
Packit Service 3880ab
Packit Service 3880ab
   action order 6: police 20 action drop rate 1Kbit burst 90Kb mtu 5000b
Packit Service 3880ab
         Sent 163380 bytes 1945 pkts (dropped 0, overlimits 437)
Packit Service 3880ab
Packit Service 3880ab
  match 0a000015/ffffffff at 12
Packit Service 3880ab
-------------------------------
Packit Service 3880ab
Packit Service 3880ab
Neat, eh?
Packit Service 3880ab
Packit Service 3880ab
Packit Service 3880ab
Want to  write an action module?
Packit Service 3880ab
------------------------------
Packit Service 3880ab
Its easy. Either look at the code or send me email. I will document at
Packit Service 3880ab
some point; will also accept documentation.
Packit Service 3880ab
Packit Service 3880ab
TODO
Packit Service 3880ab
----
Packit Service 3880ab
Packit Service 3880ab
Lotsa goodies/features coming. Requests also being accepted.
Packit Service 3880ab
At the moment the focus has been on getting the architecture in place.
Packit Service 3880ab
Expect new things in the spurious time i have to work on this
Packit Service 3880ab
(particularly around end of year when i have typically get time off
Packit Service 3880ab
from work).