Blame doc/actions/mirred-usage

Packit d3f73b
Packit d3f73b
Very funky action. I do plan to add to a few more things to it
Packit d3f73b
This is the basic stuff. Idea borrowed from the way ethernet switches
Packit d3f73b
mirror and redirect packets. The main difference with say a vannila
Packit d3f73b
ethernet switch is that you can use u32 classifier to select a
Packit d3f73b
flow to be mirrored. High end switches typically can select based
Packit d3f73b
on more than just a port (eg a 5 tuple classifier). They may also be
Packit d3f73b
capable of redirecting.
Packit d3f73b
Packit d3f73b
Usage:
Packit d3f73b
Packit d3f73b
mirred <DIRECTION> <ACTION> [index INDEX] <dev DEVICENAME>
Packit d3f73b
where:
Packit d3f73b
DIRECTION := <ingress | egress>
Packit d3f73b
ACTION := <mirror | redirect>
Packit d3f73b
INDEX is the specific policy instance id
Packit d3f73b
DEVICENAME is the devicename
Packit d3f73b
Packit d3f73b
Direction:
Packit d3f73b
- Ingress is not supported at the moment. It will be in the
Packit d3f73b
future as well as mirror/redirecting to a socket.
Packit d3f73b
Packit d3f73b
Action:
Packit d3f73b
- Mirror takes a copy of the packet and sends it to specified
Packit d3f73b
dev ("port" in ethernet switch/bridging terminology)
Packit d3f73b
- redirect
Packit d3f73b
steals the packet and redirects to specified destination dev.
Packit d3f73b
Packit d3f73b
What NOT to do if you don't want your machine to crash:
Packit d3f73b
------------------------------------------------------
Packit d3f73b
Packit d3f73b
Do not create loops!
Packit d3f73b
Loops are not hard to create in the egress qdiscs.
Packit d3f73b
Packit d3f73b
Here are simple rules to follow if you don't want to get
Packit d3f73b
hurt:
Packit d3f73b
A) Do not have the same packet go to same netdevice twice
Packit d3f73b
in a single graph of policies. Your machine will just hang!
Packit d3f73b
This is design intent _not a bug_ to teach you some lessons.
Packit d3f73b
Packit d3f73b
In the future if there are easy ways to do this in the kernel
Packit d3f73b
without affecting other packets not interested in this feature
Packit d3f73b
I will add them. At the moment that is not clear.
Packit d3f73b
Packit d3f73b
Some examples of bad things NOT to do:
Packit d3f73b
1) redirecting eth0 to eth0
Packit d3f73b
2) eth0->eth1-> eth0
Packit d3f73b
3) eth0->lo-> eth1-> eth0
Packit d3f73b
Packit d3f73b
B) Do not redirect from one IFB device to another.
Packit d3f73b
Remember that IFB is a very specialized case of packet redirecting
Packit d3f73b
device. Instead of redirecting it puts packets at the exact spot
Packit d3f73b
on the stack it found them from.
Packit d3f73b
Redirecting from ifbX->ifbY will actually not crash your machine but your
Packit d3f73b
packets will all be dropped (this is much simpler to detect
Packit d3f73b
and resolve and is only affecting users of ifb as opposed to the
Packit d3f73b
whole stack).
Packit d3f73b
Packit d3f73b
In the case of A) the problem has to do with a recursive contention
Packit d3f73b
for the devices queue lock and in the second case for the transmit lock.
Packit d3f73b
Packit d3f73b
Some examples:
Packit d3f73b
-------------
Packit d3f73b
Packit d3f73b
1) Mirror all packets arriving on eth0 to be sent out on eth1.
Packit d3f73b
You may have a sniffer or some accounting box hooked up on eth1.
Packit d3f73b
Packit d3f73b
---
Packit d3f73b
tc qdisc add dev eth0 ingress
Packit d3f73b
tc filter add dev eth0 parent ffff: protocol ip prio 10 u32 \
Packit d3f73b
match u32 0 0 flowid 1:2 action mirred egress mirror dev eth1
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
If you replace "mirror" with "redirect" then not a copy but rather
Packit d3f73b
the original packet is sent to eth1.
Packit d3f73b
Packit d3f73b
2) Host A is hooked  up to us on eth0
Packit d3f73b
Packit d3f73b
# redirect all packets arriving on ingress of lo to eth0
Packit d3f73b
---
Packit d3f73b
tc qdisc add dev lo ingress
Packit d3f73b
tc filter add dev lo parent ffff: protocol ip prio 10 u32 \
Packit d3f73b
match u32 0 0 flowid 1:2 action mirred egress redirect dev eth0
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
On host A start a tcpdump on interface connecting to us.
Packit d3f73b
Packit d3f73b
on our host ping -c 2 127.0.0.1
Packit d3f73b
Packit d3f73b
Ping would fail since all packets are heading out eth0
Packit d3f73b
tcpudmp on host A would show them
Packit d3f73b
Packit d3f73b
if you substitute the redirect with mirror above as in:
Packit d3f73b
tc filter add dev lo parent ffff: protocol ip prio 10 u32 \
Packit d3f73b
match u32 0 0 flowid 1:2 action mirred egress mirror dev eth0
Packit d3f73b
Packit d3f73b
Then you should see the packets on both host A and the local
Packit d3f73b
stack (i.e ping would work).
Packit d3f73b
Packit d3f73b
3) Even more funky example:
Packit d3f73b
Packit d3f73b
#
Packit d3f73b
#allow 1 out 10 packets on ingress of lo to randomly make it to the
Packit d3f73b
# host A (Randomness uses the netrand generator)
Packit d3f73b
#
Packit d3f73b
---
Packit d3f73b
tc filter add dev lo parent ffff: protocol ip prio 10 u32 \
Packit d3f73b
match u32 0 0 flowid 1:2 \
Packit d3f73b
action drop random determ ok 10\
Packit d3f73b
action mirred egress mirror dev eth0
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
4)
Packit d3f73b
# for packets from 10.0.0.9 going out on eth0 (could be local
Packit d3f73b
# IP or something # we are forwarding) -
Packit d3f73b
# if exceeding a 100Kbps rate, then redirect to eth1
Packit d3f73b
#
Packit d3f73b
Packit d3f73b
---
Packit d3f73b
tc qdisc add dev eth0 handle 1:0 root prio
Packit d3f73b
tc filter add dev eth0 parent 1:0 protocol ip prio 6 u32 \
Packit d3f73b
match ip src 10.0.0.9/32 flowid 1:16 \
Packit d3f73b
action police rate 100kbit burst 90k ok \
Packit d3f73b
action mirred egress mirror dev eth1
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
A more interesting example is when you mirror flows to a dummy device
Packit d3f73b
so you could tcpdump them (dummy by defaults drops all packets it sees).
Packit d3f73b
This is a very useful debug feature.
Packit d3f73b
Packit d3f73b
Lets say you are policing packets from alias 192.168.200.200/32
Packit d3f73b
you don't want those to exceed 100kbps going out.
Packit d3f73b
Packit d3f73b
---
Packit d3f73b
tc qdisc add dev eth0 handle 1:0 root prio
Packit d3f73b
tc filter add dev eth0 parent 1: protocol ip prio 10 u32 \
Packit d3f73b
match ip src 192.168.200.200/32 flowid 1:2 \
Packit d3f73b
action police rate 100kbit burst 90k drop
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
If you run tcpdump on eth0 you will see all packets going out
Packit d3f73b
with src 192.168.200.200/32 dropped or not (since tcpdump shows
Packit d3f73b
all packets being egressed).
Packit d3f73b
Extend the rule a little to see only the packets making it out.
Packit d3f73b
Packit d3f73b
---
Packit d3f73b
tc qdisc add dev eth0 handle 1:0 root prio
Packit d3f73b
tc filter add dev eth0 parent 1: protocol ip prio 10 u32 \
Packit d3f73b
match ip src 192.168.200.200/32 flowid 1:2 \
Packit d3f73b
action police rate 10kbit burst 90k drop \
Packit d3f73b
action mirred egress mirror dev dummy0
Packit d3f73b
---
Packit d3f73b
Packit d3f73b
Now fire tcpdump on dummy0 to see only those packets ..
Packit d3f73b
tcpdump -n -i dummy0 -x -e -t
Packit d3f73b
Packit d3f73b
Essentially a good debugging/logging interface (sort of like
Packit d3f73b
BSDs speacialized log device does without needing one).
Packit d3f73b
Packit d3f73b
If you replace mirror with redirect, those packets will be
Packit d3f73b
blackholed and will never make it out.
Packit d3f73b
Packit d3f73b
cheers,
Packit d3f73b
jamal