TC-MIRRED(8)

๐Ÿ“ฆ
version 7.3.0 ยท 2 versions
  • 7.3.0 ยท alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, debian trixie, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 6.1.0 ยท debian bookworm, ubuntu jammy, ubuntu noble
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, debian trixie, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

๐Ÿฆ‹ TC-MIRRED(8) ๐Ÿฆ‹

okay bestie, this is the tc-mirred page, aka the mirror/redirect action in tc(8) ๐Ÿ’– It's the thing that lets you copy or steal packets as they come in, and it's lowkey iconic ๐Ÿ’…

โœจ NAME โœจ

mirred - mirror/redirect action ๐Ÿงš

๐Ÿ’œ SYNOPSIS ๐Ÿ’œ

tc ... action mirred DIRECTION ACTION [ index INDEX ] TARGET

DIRECTION := { ingress | egress }

TARGET := { DEV | BLOCK }

DEV := dev DEVICENAME

BLOCK := blockid BLOCKID

ACTION := { mirror | redirect }

๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so the mirred action lets you do packet mirroring (copying) or redirecting (stealing) whatever packet it receives ๐Ÿ”ฅ Mirroring is what people call Switch Port Analyzer (SPAN), and it's super popular for analyzing and/or debugging flows ๐Ÿ™ˆ

when you mirror to a tc block, the packet gets copied to every port in the block EXCEPT the port it came in on (only if that port is actually part of the tc block) โœจ Redirecting is basically the same vibe, except it mirrors to the first N - 1 ports in the block and then redirects to the last one ๐Ÿ’€ The port the packet arrived on is never mirrored or redirected to, so don't even try ๐Ÿ™…โ€โ™€๏ธ

๐ŸŽ€ OPTIONS ๐ŸŽ€

  • ingress / egress โ€” pick the direction the packet shows up in on the destination interface ๐Ÿฆ‹
  • mirror / redirect โ€” decide whether the packet gets copied (mirror) or moved (redirect) to the destination interface or block ๐Ÿ’…
  • index INDEX โ€” give this action its own unique ID instead of letting the kernel pick one for you ๐Ÿงš INDEX is a 32bit unsigned integer that has to be greater than zero
  • dev DEVICENAME โ€” the network interface you want to redirect or mirror to ๐ŸŒˆ
  • blockid BLOCKID โ€” the tc block you want to redirect or mirror to โญ

๐Ÿ’– EXAMPLES ๐Ÿ’–

okay here are the examples, bestie, they're giving practical energy ๐Ÿซถ

Limit ingress bandwidth on eth0 to 1mbit/s, and redirect the exceeding traffic to lo for debugging purposes ๐Ÿ“

# tc qdisc add dev eth0 handle ffff: clsact
# tc filter add dev eth0 ingress u32 \
     match u32 0 0 \
     action police rate 1mbit burst 100k conform-exceed pipe \
     action mirred egress redirect dev lo

Mirror all incoming ICMP packets on eth0 to a dummy interface so you can examine them with e.g. tcpdump ๐Ÿ‘€

# ip link add dummy0 type dummy
# ip link set dummy0 up
# tc qdisc add dev eth0 handle ffff: clsact
# tc filter add dev eth0 ingress protocol ip \
     u32 match ip protocol 1 0xff \
     action mirred egress mirror dev dummy0

Using an ifb interface, you can send ingress traffic through an instance of sfq ๐Ÿงšโ€โ™€๏ธโœจ

# modprobe ifb
# ip link set ifb0 up
# tc qdisc add dev ifb0 root sfq
# tc qdisc add dev eth0 handle ffff: clsact
# tc filter add dev eth0 ingress u32 \
     match u32 0 0 \
     action mirred egress redirect dev ifb0

๐Ÿ˜ญ LIMITATIONS ๐Ÿ˜ญ

the kernel caps nesting at four levels so nesting loops can't happen ๐Ÿ”’

โš ๏ธ Do not redirect from one IFB device to another. IFB is a very specialized case of packet redirecting device, and redirecting from ifbX->ifbY will cause ALL packets to be dropped. literally the worst, don't do it ๐Ÿ’€

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

tc(8), tc-u32(8) ๐Ÿฆ‹


that's a wrap, bestie! ๐Ÿ’œ Source: iproute2, version dated 11 Jan 2015 (Linux) ๐Ÿซถโœจ now go mirror something iconic ๐Ÿ‘‘๐Ÿ’