Skip to content

Instantly share code, notes, and snippets.

@junka
Forked from lonelymtn/mirror-port.sh
Last active September 26, 2024 14:00
Show Gist options
  • Save junka/7ec14094a67130bc5b4e5b7f41101de2 to your computer and use it in GitHub Desktop.
Save junka/7ec14094a67130bc5b4e5b7f41101de2 to your computer and use it in GitHub Desktop.
Mirror offloaded traffic between two interfaces using Linux's TC, for HW like DPAA2
#!/usr/bin/env bash
# Time-stamp: <2014-07-31 13:31:43 (ryanc)>
# Time-stamp: <2024-09-26 21:52:50 (junjie.wan)>
#
# Description: Mirror traffic between two interfaces using Linux's
# traffic control subsystem (tc)
trap cleanup EXIT
CLEANUP=1
SRC_IFACE=$1
DST_IFACE=$2
VID=0
function cleanup() {
if [ $CLEANUP -eq 1 ]; then
if [ $VID -eq 0 ]; then
tc qdisc del dev $SRC_IFACE handle ffff: clsact
else
tc qdisc del dev $SRC_IFACE ingress_block 1 clsact
bridge vlan del dev $DST_IFACE vid $VID
fi
fi
echo
}
if [ $# -lt 2 ]; then
echo "Usage: ${0/*\//} <src interface> <dst interface> [vlan id]"
CLEANUP=0
exit 1
fi
if [ $# -eq 3 ]; then
VID=$3
fi
echo
echo "Mirroring traffic from $SRC_IFACE to $DST_IFACE"
if [ $VID -eq 0 ]; then
# ingress
tc qdisc add dev $SRC_IFACE handle ffff: clsact
tc filter add dev $SRC_IFACE ingress matchall skip_sw action mirred egress mirror dev $DST_IFACE
# egress not supported for now
# tc filter add dev $SRC_IFACE egress matchall skip_sw action mirred egress mirror dev $DST_IFACE
else
# ingress with VLAN id
tc qdisc add dev $SRC_IFACE ingress_block 1 clsact
tc filter add block 1 ingress protocol 802.1q flower skip_sw vlan_id $VID action mirred egress mirror dev $DST_IFACE
bridge vlan add dev $DST_IFACE vid $VID
fi
echo "Hit Ctrl-C or kill this session to end port mirroring"
sleep infinity
trap - EXIT
cleanup
exit 0
# End of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment