Blame doc/actions/mirred-usage

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