summaryrefslogtreecommitdiff
path: root/tools/net/ynl/tests/test_ynl_ethtool.sh
blob: b826269017f46f6cecfc5875886eb5a31e3a2a3b (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Test YNL ethtool functionality

# Load KTAP test helpers
KSELFTEST_KTAP_HELPERS="$(dirname "$(realpath "$0")")/../../../testing/selftests/kselftest/ktap_helpers.sh"
# shellcheck source=../../../testing/selftests/kselftest/ktap_helpers.sh
source "$KSELFTEST_KTAP_HELPERS"

# Default ynl-ethtool path for direct execution, can be overridden by make install
ynl_ethtool="../pyynl/ethtool.py"

readonly NSIM_ID="1337"
readonly NSIM_DEV_NAME="nsim${NSIM_ID}"
readonly VETH_A="veth_a"
readonly VETH_B="veth_b"

testns="ynl-ethtool-$(mktemp -u XXXXXX)"
TESTS_NO=0

# Uses veth device as netdevsim doesn't support basic ethtool device info
ethtool_device_info()
{
	local info_output

	info_output=$(ip netns exec "$testns" $ynl_ethtool "$VETH_A" 2>/dev/null)

	if ! echo "$info_output" | grep -q "Settings for"; then
		ktap_test_fail "YNL ethtool device info (device info output missing expected content)"
		return
	fi

	ktap_test_pass "YNL ethtool device info"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_statistics()
{
	local stats_output

	stats_output=$(ip netns exec "$testns" $ynl_ethtool --statistics "$NSIM_DEV_NAME" 2>/dev/null)

	if ! echo "$stats_output" | grep -q -E "(NIC statistics|packets|bytes)"; then
		ktap_test_fail "YNL ethtool statistics (statistics output missing expected content)"
		return
	fi

	ktap_test_pass "YNL ethtool statistics"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_ring_params()
{
	local ring_output

	ring_output=$(ip netns exec "$testns" $ynl_ethtool --show-ring "$NSIM_DEV_NAME" 2>/dev/null)

	if ! echo "$ring_output" | grep -q -E "(Ring parameters|RX|TX)"; then
		ktap_test_fail "YNL ethtool ring parameters (ring parameters output missing expected content)"
		return
	fi

	if ! ip netns exec "$testns" $ynl_ethtool --set-ring "$NSIM_DEV_NAME" rx 64 2>/dev/null; then
		ktap_test_fail "YNL ethtool ring parameters (set-ring command failed unexpectedly)"
		return
	fi

	ktap_test_pass "YNL ethtool ring parameters (show/set)"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_coalesce_params()
{
	if ! ip netns exec "$testns" $ynl_ethtool --show-coalesce "$NSIM_DEV_NAME" &>/dev/null; then
		ktap_test_fail "YNL ethtool coalesce parameters (failed to get coalesce parameters)"
		return
	fi

	if ! ip netns exec "$testns" $ynl_ethtool --set-coalesce "$NSIM_DEV_NAME" rx-usecs 50 2>/dev/null; then
		ktap_test_fail "YNL ethtool coalesce parameters (set-coalesce command failed unexpectedly)"
		return
	fi

	ktap_test_pass "YNL ethtool coalesce parameters (show/set)"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_pause_params()
{
	if ! ip netns exec "$testns" $ynl_ethtool --show-pause "$NSIM_DEV_NAME" &>/dev/null; then
		ktap_test_fail "YNL ethtool pause parameters (failed to get pause parameters)"
		return
	fi

	if ! ip netns exec "$testns" $ynl_ethtool --set-pause "$NSIM_DEV_NAME" tx 1 rx 1 2>/dev/null; then
		ktap_test_fail "YNL ethtool pause parameters (set-pause command failed unexpectedly)"
		return
	fi

	ktap_test_pass "YNL ethtool pause parameters (show/set)"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_features_info()
{
	local features_output

	features_output=$(ip netns exec "$testns" $ynl_ethtool --show-features "$NSIM_DEV_NAME" 2>/dev/null)

	if ! echo "$features_output" | grep -q -E "(Features|offload)"; then
		ktap_test_fail "YNL ethtool features info (features output missing expected content)"
		return
	fi

	ktap_test_pass "YNL ethtool features info (show/set)"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_channels_info()
{
	local channels_output

	channels_output=$(ip netns exec "$testns" $ynl_ethtool --show-channels "$NSIM_DEV_NAME" 2>/dev/null)

	if ! echo "$channels_output" | grep -q -E "(Channel|Combined|RX|TX)"; then
		ktap_test_fail "YNL ethtool channels info (channels output missing expected content)"
		return
	fi

	if ! ip netns exec "$testns" $ynl_ethtool --set-channels "$NSIM_DEV_NAME" combined-count 1 2>/dev/null; then
		ktap_test_fail "YNL ethtool channels info (set-channels command failed unexpectedly)"
		return
	fi

	ktap_test_pass "YNL ethtool channels info (show/set)"
}
TESTS_NO=$((TESTS_NO + 1))

ethtool_time_stamping()
{
	local ts_output

	ts_output=$(ip netns exec "$testns" $ynl_ethtool --show-time-stamping "$NSIM_DEV_NAME" 2>/dev/null)

	if ! echo "$ts_output" | grep -q -E "(Time stamping|timestamping|SOF_TIMESTAMPING)"; then
		ktap_test_fail "YNL ethtool time stamping (time stamping output missing expected content)"
		return
	fi

	ktap_test_pass "YNL ethtool time stamping"
}
TESTS_NO=$((TESTS_NO + 1))

setup()
{
	modprobe netdevsim &> /dev/null
	if ! [ -f /sys/bus/netdevsim/new_device ]; then
		ktap_skip_all "netdevsim module not available"
		exit "$KSFT_SKIP"
	fi

	if ! ip netns add "$testns" 2>/dev/null; then
		ktap_skip_all "failed to create test namespace"
		exit "$KSFT_SKIP"
	fi

	echo "$NSIM_ID 1" | ip netns exec "$testns" tee /sys/bus/netdevsim/new_device >/dev/null 2>&1 || {
		ktap_skip_all "failed to create netdevsim device"
		exit "$KSFT_SKIP"
	}

	local dev
	dev=$(ip netns exec "$testns" ls /sys/bus/netdevsim/devices/netdevsim$NSIM_ID/net 2>/dev/null | head -1)
	if [[ -z "$dev" ]]; then
		ktap_skip_all "failed to find netdevsim device"
		exit "$KSFT_SKIP"
	fi

	ip -netns "$testns" link set dev "$dev" name "$NSIM_DEV_NAME" 2>/dev/null || {
		ktap_skip_all "failed to rename netdevsim device"
		exit "$KSFT_SKIP"
	}

	ip -netns "$testns" link set dev "$NSIM_DEV_NAME" up 2>/dev/null

	if ! ip -n "$testns" link add "$VETH_A" type veth peer name "$VETH_B" 2>/dev/null; then
		ktap_skip_all "failed to create veth pair"
		exit "$KSFT_SKIP"
	fi

	ip -n "$testns" link set "$VETH_A" up 2>/dev/null
	ip -n "$testns" link set "$VETH_B" up 2>/dev/null
}

cleanup()
{
	ip netns exec "$testns" bash -c "echo $NSIM_ID > /sys/bus/netdevsim/del_device" 2>/dev/null || true
	ip netns del "$testns" 2>/dev/null || true
}

# Check if ynl-ethtool command is available
if ! command -v $ynl_ethtool &>/dev/null && [[ ! -x $ynl_ethtool ]]; then
	ktap_skip_all "ynl-ethtool command not found: $ynl_ethtool"
	exit "$KSFT_SKIP"
fi

trap cleanup EXIT

ktap_print_header
setup
ktap_set_plan "${TESTS_NO}"

ethtool_device_info
ethtool_statistics
ethtool_ring_params
ethtool_coalesce_params
ethtool_pause_params
ethtool_features_info
ethtool_channels_info
ethtool_time_stamping

ktap_finished