summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/forwarding/mirror_lib.sh
blob: 00797597fcf5f229c4942e671a817d2b92eb5465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# SPDX-License-Identifier: GPL-2.0

mirror_install()
{
	local from_dev=$1; shift
	local direction=$1; shift
	local to_dev=$1; shift
	local filter=$1; shift

	tc filter add dev $from_dev $direction \
	   pref 1000 $filter \
	   action mirred egress mirror dev $to_dev
}

mirror_uninstall()
{
	local from_dev=$1; shift
	local direction=$1; shift

	tc filter del dev $swp1 $direction pref 1000
}

mirror_test()
{
	local vrf_name=$1; shift
	local sip=$1; shift
	local dip=$1; shift
	local dev=$1; shift
	local pref=$1; shift
	local expect=$1; shift

	local ping_timeout=$((PING_TIMEOUT * 5))
	local t0=$(tc_rule_stats_get $dev $pref)
	ip vrf exec $vrf_name \
	   ${PING} ${sip:+-I $sip} $dip -c 10 -i 0.5 -w $ping_timeout \
		   &> /dev/null
	sleep 0.5
	local t1=$(tc_rule_stats_get $dev $pref)
	local delta=$((t1 - t0))
	# Tolerate a couple stray extra packets.
	((expect <= delta && delta <= expect + 2))
	check_err $? "Expected to capture $expect packets, got $delta."
}

do_test_span_dir_ips()
{
	local expect=$1; shift
	local dev=$1; shift
	local direction=$1; shift
	local ip1=$1; shift
	local ip2=$1; shift

	icmp_capture_install $dev
	mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
	mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
	icmp_capture_uninstall $dev
}

quick_test_span_dir_ips()
{
	do_test_span_dir_ips 10 "$@"
}

fail_test_span_dir_ips()
{
	do_test_span_dir_ips 0 "$@"
}

test_span_dir_ips()
{
	local dev=$1; shift
	local direction=$1; shift
	local forward_type=$1; shift
	local backward_type=$1; shift
	local ip1=$1; shift
	local ip2=$1; shift

	quick_test_span_dir_ips "$dev" "$direction" "$ip1" "$ip2"

	icmp_capture_install $dev "type $forward_type"
	mirror_test v$h1 $ip1 $ip2 $dev 100 10
	icmp_capture_uninstall $dev

	icmp_capture_install $dev "type $backward_type"
	mirror_test v$h2 $ip2 $ip1 $dev 100 10
	icmp_capture_uninstall $dev
}

fail_test_span_dir()
{
	fail_test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
}

test_span_dir()
{
	test_span_dir_ips "$@" 192.0.2.1 192.0.2.2
}

do_test_span_vlan_dir_ips()
{
	local expect=$1; shift
	local dev=$1; shift
	local vid=$1; shift
	local direction=$1; shift
	local ip1=$1; shift
	local ip2=$1; shift

	# Install the capture as skip_hw to avoid double-counting of packets.
	# The traffic is meant for local box anyway, so will be trapped to
	# kernel.
	vlan_capture_install $dev "skip_hw vlan_id $vid vlan_ethtype ip"
	mirror_test v$h1 $ip1 $ip2 $dev 100 $expect
	mirror_test v$h2 $ip2 $ip1 $dev 100 $expect
	vlan_capture_uninstall $dev
}

quick_test_span_vlan_dir_ips()
{
	do_test_span_vlan_dir_ips 10 "$@"
}

fail_test_span_vlan_dir_ips()
{
	do_test_span_vlan_dir_ips 0 "$@"
}

quick_test_span_vlan_dir()
{
	quick_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
}

fail_test_span_vlan_dir()
{
	fail_test_span_vlan_dir_ips "$@" 192.0.2.1 192.0.2.2
}