xref: /linux/tools/testing/selftests/net/forwarding/mirror_lib.sh (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
1# SPDX-License-Identifier: GPL-2.0
2
3mirror_install()
4{
5	local from_dev=$1; shift
6	local direction=$1; shift
7	local to_dev=$1; shift
8	local filter=$1; shift
9
10	tc filter add dev $from_dev $direction \
11	   pref 1000 $filter \
12	   action mirred egress mirror dev $to_dev
13}
14
15mirror_uninstall()
16{
17	local from_dev=$1; shift
18	local direction=$1; shift
19
20	tc filter del dev $swp1 $direction pref 1000
21}
22
23mirror_test()
24{
25	local vrf_name=$1; shift
26	local sip=$1; shift
27	local dip=$1; shift
28	local dev=$1; shift
29	local pref=$1; shift
30	local expect=$1; shift
31
32	local t0=$(tc_rule_stats_get $dev $pref)
33	ip vrf exec $vrf_name \
34	   ${PING} ${sip:+-I $sip} $dip -c 10 -i 0.1 -w 2 &> /dev/null
35	local t1=$(tc_rule_stats_get $dev $pref)
36	local delta=$((t1 - t0))
37	# Tolerate a couple stray extra packets.
38	((expect <= delta && delta <= expect + 2))
39	check_err $? "Expected to capture $expect packets, got $delta."
40}
41
42do_test_span_dir_ips()
43{
44	local expect=$1; shift
45	local dev=$1; shift
46	local direction=$1; shift
47	local ip1=$1; shift
48	local ip2=$1; shift
49
50	icmp_capture_install $dev
51	mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
52	mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
53	icmp_capture_uninstall $dev
54}
55
56quick_test_span_dir_ips()
57{
58	do_test_span_dir_ips 10 "$@"
59}
60
61fail_test_span_dir_ips()
62{
63	do_test_span_dir_ips 0 "$@"
64}
65
66test_span_dir_ips()
67{
68	local dev=$1; shift
69	local direction=$1; shift
70	local forward_type=$1; shift
71	local backward_type=$1; shift
72	local ip1=$1; shift
73	local ip2=$1; shift
74
75	quick_test_span_dir_ips "$dev" "$direction" "$ip1" "$ip2"
76
77	icmp_capture_install $dev "type $forward_type"
78	mirror_test v$h1 $ip1 $ip2 $dev 100 10
79	icmp_capture_uninstall $dev
80
81	icmp_capture_install $dev "type $backward_type"
82	mirror_test v$h2 $ip2 $ip1 $dev 100 10
83	icmp_capture_uninstall $dev
84}
85
86fail_test_span_dir()
87{
88	fail_test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
89}
90
91test_span_dir()
92{
93	test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
94}
95
96do_test_span_vlan_dir_ips()
97{
98	local expect=$1; shift
99	local dev=$1; shift
100	local vid=$1; shift
101	local direction=$1; shift
102	local ip1=$1; shift
103	local ip2=$1; shift
104
105	# Install the capture as skip_hw to avoid double-counting of packets.
106	# The traffic is meant for local box anyway, so will be trapped to
107	# kernel.
108	vlan_capture_install $dev "skip_hw vlan_id $vid vlan_ethtype ip"
109	mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
110	mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
111	vlan_capture_uninstall $dev
112}
113
114quick_test_span_vlan_dir_ips()
115{
116	do_test_span_vlan_dir_ips 10 "$@"
117}
118
119fail_test_span_vlan_dir_ips()
120{
121	do_test_span_vlan_dir_ips 0 "$@"
122}
123
124quick_test_span_vlan_dir()
125{
126	quick_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
127}
128
129fail_test_span_vlan_dir()
130{
131	fail_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
132}
133