1# SPDX-License-Identifier: GPL-2.0
2
3TC_POLICE_NUM_NETIFS=2
4
5tc_police_h1_create()
6{
7	simple_if_init $h1
8}
9
10tc_police_h1_destroy()
11{
12	simple_if_fini $h1
13}
14
15tc_police_switch_create()
16{
17	simple_if_init $swp1
18	tc qdisc add dev $swp1 clsact
19}
20
21tc_police_switch_destroy()
22{
23	tc qdisc del dev $swp1 clsact
24	simple_if_fini $swp1
25}
26
27tc_police_addr()
28{
29       local num=$1; shift
30
31       printf "2001:db8:1::%x" $num
32}
33
34tc_police_rules_create()
35{
36	local count=$1; shift
37	local should_fail=$1; shift
38
39	TC_POLICE_BATCH_FILE="$(mktemp)"
40
41	for ((i = 0; i < count; ++i)); do
42		cat >> $TC_POLICE_BATCH_FILE <<-EOF
43			filter add dev $swp1 ingress \
44				prot ipv6 \
45				pref 1000 \
46				flower skip_sw dst_ip $(tc_police_addr $i) \
47				action police rate 10mbit burst 100k \
48				conform-exceed drop/ok
49		EOF
50	done
51
52	tc -b $TC_POLICE_BATCH_FILE
53	check_err_fail $should_fail $? "Rule insertion"
54}
55
56__tc_police_test()
57{
58	local count=$1; shift
59	local should_fail=$1; shift
60
61	tc_police_rules_create $count $should_fail
62
63	offload_count=$(tc filter show dev $swp1 ingress | grep in_hw | wc -l)
64	((offload_count == count))
65	check_err_fail $should_fail $? "tc police offload count"
66}
67
68tc_police_test()
69{
70	local count=$1; shift
71	local should_fail=$1; shift
72
73	if ! tc_offload_check $TC_POLICE_NUM_NETIFS; then
74		check_err 1 "Could not test offloaded functionality"
75		return
76	fi
77
78	__tc_police_test $count $should_fail
79}
80
81tc_police_setup_prepare()
82{
83	h1=${NETIFS[p1]}
84	swp1=${NETIFS[p2]}
85
86	vrf_prepare
87
88	tc_police_h1_create
89	tc_police_switch_create
90}
91
92tc_police_cleanup()
93{
94	pre_cleanup
95
96	tc_police_switch_destroy
97	tc_police_h1_destroy
98
99	vrf_cleanup
100}
101