1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4lib_dir=$(dirname $0)/../../../../net/forwarding
5
6NUM_NETIFS=1
7source $lib_dir/lib.sh
8source devlink_lib_spectrum.sh
9
10setup_prepare()
11{
12	devlink_sp_read_kvd_defaults
13}
14
15cleanup()
16{
17	pre_cleanup
18	devlink_sp_size_kvd_to_default
19}
20
21trap cleanup EXIT
22
23setup_prepare
24
25profiles_test()
26{
27	local i
28
29	log_info "Running profile tests"
30
31	for i in $KVD_PROFILES; do
32		RET=0
33		devlink_sp_resource_kvd_profile_set $i
34		log_test "'$i' profile"
35	done
36
37	# Default is explicitly tested at end to ensure it's actually applied
38	RET=0
39	devlink_sp_resource_kvd_profile_set "default"
40	log_test "'default' profile"
41}
42
43resources_min_test()
44{
45	local size
46	local i
47	local j
48
49	log_info "Running KVD-minimum tests"
50
51	for i in $KVD_CHILDREN; do
52		RET=0
53		size=$(devlink_resource_get kvd "$i" | jq '.["size_min"]')
54		devlink_resource_size_set "$size" kvd "$i"
55
56		# In case of linear, need to minimize sub-resources as well
57		if [[ "$i" == "linear" ]]; then
58			for j in $KVDL_CHILDREN; do
59				devlink_resource_size_set 0 kvd linear "$j"
60			done
61		fi
62
63		devlink_reload
64		devlink_sp_size_kvd_to_default
65		log_test "'$i' minimize [$size]"
66	done
67}
68
69resources_max_test()
70{
71	local min_size
72	local size
73	local i
74	local j
75
76	log_info "Running KVD-maximum tests"
77	for i in $KVD_CHILDREN; do
78		RET=0
79		devlink_sp_resource_minimize
80
81		# Calculate the maximum possible size for the given partition
82		size=$(devlink_resource_size_get kvd)
83		for j in $KVD_CHILDREN; do
84			if [ "$i" != "$j" ]; then
85				min_size=$(devlink_resource_get kvd "$j" | \
86					   jq '.["size_min"]')
87				size=$((size - min_size))
88			fi
89		done
90
91		# Test almost maximum size
92		devlink_resource_size_set "$((size - 128))" kvd "$i"
93		devlink_reload
94		log_test "'$i' almost maximize [$((size - 128))]"
95
96		# Test above maximum size
97		devlink resource set "$DEVLINK_DEV" \
98			path "kvd/$i" size $((size + 128)) &> /dev/null
99		check_fail $? "Set kvd/$i to size $((size + 128)) should fail"
100		log_test "'$i' Overflow rejection [$((size + 128))]"
101
102		# Test maximum size
103		if [ "$i" == "hash_single" ] || [ "$i" == "hash_double" ]; then
104			echo "SKIP: Observed problem with exact max $i"
105			continue
106		fi
107
108		devlink_resource_size_set "$size" kvd "$i"
109		devlink_reload
110		log_test "'$i' maximize [$size]"
111
112		devlink_sp_size_kvd_to_default
113	done
114}
115
116profiles_test
117resources_min_test
118resources_max_test
119
120exit "$RET"
121