1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Test that filters that match on the same port range, but with different
5# combination of IPv4/IPv6 and TCP/UDP all use the same port range register by
6# observing port range registers' occupancy via devlink-resource.
7
8lib_dir=$(dirname $0)/../../../net/forwarding
9
10ALL_TESTS="
11	port_range_occ_test
12"
13NUM_NETIFS=2
14source $lib_dir/lib.sh
15source $lib_dir/devlink_lib.sh
16
17h1_create()
18{
19	simple_if_init $h1
20}
21
22h1_destroy()
23{
24	simple_if_fini $h1
25}
26
27switch_create()
28{
29	simple_if_init $swp1
30	tc qdisc add dev $swp1 clsact
31}
32
33switch_destroy()
34{
35	tc qdisc del dev $swp1 clsact
36	simple_if_fini $swp1
37}
38
39setup_prepare()
40{
41	h1=${NETIFS[p1]}
42	swp1=${NETIFS[p2]}
43
44	vrf_prepare
45
46	h1_create
47	switch_create
48}
49
50cleanup()
51{
52	pre_cleanup
53
54	switch_destroy
55	h1_destroy
56
57	vrf_cleanup
58}
59
60port_range_occ_get()
61{
62	devlink_resource_occ_get port_range_registers
63}
64
65port_range_occ_test()
66{
67	RET=0
68
69	local occ=$(port_range_occ_get)
70
71	# Two port range registers are used, for source and destination port
72	# ranges.
73	tc filter add dev $swp1 ingress pref 1 handle 101 proto ip \
74		flower skip_sw ip_proto udp src_port 1-100 dst_port 1-100 \
75		action pass
76	(( occ + 2 == $(port_range_occ_get) ))
77	check_err $? "Got occupancy $(port_range_occ_get), expected $((occ + 2))"
78
79	tc filter add dev $swp1 ingress pref 1 handle 102 proto ip \
80		flower skip_sw ip_proto tcp src_port 1-100 dst_port 1-100 \
81		action pass
82	tc filter add dev $swp1 ingress pref 2 handle 103 proto ipv6 \
83		flower skip_sw ip_proto udp src_port 1-100 dst_port 1-100 \
84		action pass
85	tc filter add dev $swp1 ingress pref 2 handle 104 proto ipv6 \
86		flower skip_sw ip_proto tcp src_port 1-100 dst_port 1-100 \
87		action pass
88	(( occ + 2 == $(port_range_occ_get) ))
89	check_err $? "Got occupancy $(port_range_occ_get), expected $((occ + 2))"
90
91	tc filter del dev $swp1 ingress pref 2 handle 104 flower
92	tc filter del dev $swp1 ingress pref 2 handle 103 flower
93	tc filter del dev $swp1 ingress pref 1 handle 102 flower
94	(( occ + 2 == $(port_range_occ_get) ))
95	check_err $? "Got occupancy $(port_range_occ_get), expected $((occ + 2))"
96
97	tc filter del dev $swp1 ingress pref 1 handle 101 flower
98	(( occ == $(port_range_occ_get) ))
99	check_err $? "Got occupancy $(port_range_occ_get), expected $occ"
100
101	log_test "port range occupancy"
102}
103
104trap cleanup EXIT
105
106setup_prepare
107setup_wait
108
109tests_run
110
111exit $EXIT_STATUS
112