summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/ntb/ntb_test.sh
blob: a3942f31c057f9af9446d10b1060bc793f0638e6 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/bin/bash
# Copyright (c) 2016 Microsemi. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Author: Logan Gunthorpe <logang@deltatee.com>

REMOTE_HOST=
LIST_DEVS=FALSE

DEBUGFS=${DEBUGFS-/sys/kernel/debug}

DB_BITMASK=0x7FFF
PERF_RUN_ORDER=32
MAX_MW_SIZE=0
RUN_DMA_TESTS=
DONT_CLEANUP=
MW_SIZE=65536

function show_help()
{
	echo "Usage: $0 [OPTIONS] LOCAL_DEV REMOTE_DEV"
	echo "Run tests on a pair of NTB endpoints."
	echo
	echo "If the NTB device loops back to the same host then,"
	echo "just specifying the two PCI ids on the command line is"
	echo "sufficient. Otherwise, if the NTB link spans two hosts"
	echo "use the -r option to specify the hostname for the remote"
	echo "device. SSH will then be used to test the remote side."
	echo "An SSH key between the root users of the host would then"
	echo "be highly recommended."
	echo
	echo "Options:"
	echo "  -b BITMASK      doorbell clear bitmask for ntb_tool"
	echo "  -C              don't cleanup ntb modules on exit"
	echo "  -d              run dma tests"
	echo "  -h              show this help message"
	echo "  -l              list available local and remote PCI ids"
	echo "  -r REMOTE_HOST  specify the remote's hostname to connect"
        echo "                  to for the test (using ssh)"
	echo "  -p NUM          ntb_perf run order (default: $PERF_RUN_ORDER)"
	echo "  -w max_mw_size  maxmium memory window size"
	echo
}

function parse_args()
{
	OPTIND=0
	while getopts "b:Cdhlm:r:p:w:" opt; do
		case "$opt" in
		b)  DB_BITMASK=${OPTARG} ;;
		C)  DONT_CLEANUP=1 ;;
		d)  RUN_DMA_TESTS=1 ;;
		h)  show_help; exit 0 ;;
		l)  LIST_DEVS=TRUE ;;
		m)  MW_SIZE=${OPTARG} ;;
		r)  REMOTE_HOST=${OPTARG} ;;
		p)  PERF_RUN_ORDER=${OPTARG} ;;
		w)  MAX_MW_SIZE=${OPTARG} ;;
		\?)
		    echo "Invalid option: -$OPTARG" >&2
		    exit 1
		    ;;
		esac
	done
}

parse_args "$@"
shift $((OPTIND-1))
LOCAL_DEV=$1
shift
parse_args "$@"
shift $((OPTIND-1))
REMOTE_DEV=$1
shift
parse_args "$@"

set -e

function _modprobe()
{
	modprobe "$@"

	if [[ "$REMOTE_HOST" != "" ]]; then
		ssh "$REMOTE_HOST" modprobe "$@"
	fi
}

function split_remote()
{
	VPATH=$1
	REMOTE=

	if [[ "$VPATH" == *":/"* ]]; then
		REMOTE=${VPATH%%:*}
		VPATH=${VPATH#*:}
	fi
}

function read_file()
{
	split_remote $1
	if [[ "$REMOTE" != "" ]]; then
		ssh "$REMOTE" cat "$VPATH"
	else
		cat "$VPATH"
	fi
}

function write_file()
{
	split_remote $2
	VALUE=$1

	if [[ "$REMOTE" != "" ]]; then
		ssh "$REMOTE" "echo \"$VALUE\" > \"$VPATH\""
	else
		echo "$VALUE" > "$VPATH"
	fi
}

function check_file()
{
	split_remote $1

	if [[ "$REMOTE" != "" ]]; then
		ssh "$REMOTE" "[[ -e ${VPATH} ]]"
	else
		[[ -e ${VPATH} ]]
	fi
}

function subdirname()
{
	echo $(basename $(dirname $1)) 2> /dev/null
}

function find_pidx()
{
	PORT=$1
	PPATH=$2

	for ((i = 0; i < 64; i++)); do
		PEER_DIR="$PPATH/peer$i"

		check_file ${PEER_DIR} || break

		PEER_PORT=$(read_file "${PEER_DIR}/port")
		if [[ ${PORT} -eq $PEER_PORT ]]; then
			echo $i
			return 0
		fi
	done

	return 1
}

function port_test()
{
	LOC=$1
	REM=$2

	echo "Running port tests on: $(basename $LOC) / $(basename $REM)"

	LOCAL_PORT=$(read_file "$LOC/port")
	REMOTE_PORT=$(read_file "$REM/port")

	LOCAL_PIDX=$(find_pidx ${REMOTE_PORT} "$LOC")
	REMOTE_PIDX=$(find_pidx ${LOCAL_PORT} "$REM")

	echo "Local port ${LOCAL_PORT} with index ${REMOTE_PIDX} on remote host"
	echo "Peer port ${REMOTE_PORT} with index ${LOCAL_PIDX} on local host"

	echo "  Passed"
}

function link_test()
{
	LOC=$1
	REM=$2
	EXP=0

	echo "Running link tests on: $(subdirname $LOC) / $(subdirname $REM)"

	if ! write_file "N" "$LOC/../link" 2> /dev/null; then
		echo "  Unsupported"
		return
	fi

	write_file "N" "$LOC/link_event"

	if [[ $(read_file "$REM/link") != "N" ]]; then
		echo "Expected link to be down in $REM/link" >&2
		exit -1
	fi

	write_file "Y" "$LOC/../link"

	echo "  Passed"
}

function doorbell_test()
{
	LOC=$1
	REM=$2
	EXP=0

	echo "Running db tests on: $(basename $LOC) / $(basename $REM)"

	write_file "c $DB_BITMASK" "$REM/db"

	for ((i=1; i <= 8; i++)); do
		let DB=$(read_file "$REM/db") || true
		if [[ "$DB" != "$EXP" ]]; then
			echo "Doorbell doesn't match expected value $EXP " \
			     "in $REM/db" >&2
			exit -1
		fi

		let "MASK=1 << ($i-1)" || true
		let "EXP=$EXP | $MASK" || true
		write_file "s $MASK" "$LOC/peer_db"
	done

	echo "  Passed"
}

function read_spad()
{
       VPATH=$1
       IDX=$2

       ROW=($(read_file "$VPATH" | grep -e "^$IDX"))
       let VAL=${ROW[1]} || true
       echo $VAL
}

function scratchpad_test()
{
	LOC=$1
	REM=$2
	CNT=$(read_file "$LOC/spad" | wc -l)

	echo "Running spad tests on: $(basename $LOC) / $(basename $REM)"

	for ((i = 0; i < $CNT; i++)); do
		VAL=$RANDOM
		write_file "$i $VAL" "$LOC/peer_spad"
		RVAL=$(read_spad "$REM/spad" $i)

		if [[ "$VAL" != "$RVAL" ]]; then
			echo "Scratchpad doesn't match expected value $VAL " \
			     "in $REM/spad, got $RVAL" >&2
			exit -1
		fi

	done

	echo "  Passed"
}

function write_mw()
{
	split_remote $2

	if [[ "$REMOTE" != "" ]]; then
		ssh "$REMOTE" \
			dd if=/dev/urandom "of=$VPATH" 2> /dev/null || true
	else
		dd if=/dev/urandom "of=$VPATH" 2> /dev/null || true
	fi
}

function mw_test()
{
	IDX=$1
	LOC=$2
	REM=$3

	echo "Running $IDX tests on: $(basename $LOC) / $(basename $REM)"

	write_mw "$LOC/$IDX"

	split_remote "$LOC/$IDX"
	if [[ "$REMOTE" == "" ]]; then
		A=$VPATH
	else
		A=/tmp/ntb_test.$$.A
		ssh "$REMOTE" cat "$VPATH" > "$A"
	fi

	split_remote "$REM/peer_$IDX"
	if [[ "$REMOTE" == "" ]]; then
		B=$VPATH
	else
		B=/tmp/ntb_test.$$.B
		ssh "$REMOTE" cat "$VPATH" > "$B"
	fi

	cmp -n $MW_SIZE "$A" "$B"
	if [[ $? != 0 ]]; then
		echo "Memory window $MW did not match!" >&2
	fi

	if [[ "$A" == "/tmp/*" ]]; then
		rm "$A"
	fi

	if [[ "$B" == "/tmp/*" ]]; then
		rm "$B"
	fi

	echo "  Passed"
}

function pingpong_test()
{
	LOC=$1
	REM=$2

	echo "Running ping pong tests on: $(basename $LOC) / $(basename $REM)"

	LOC_START=$(read_file "$LOC/count")
	REM_START=$(read_file "$REM/count")

	sleep 7

	LOC_END=$(read_file "$LOC/count")
	REM_END=$(read_file "$REM/count")

	if [[ $LOC_START == $LOC_END ]] || [[ $REM_START == $REM_END ]]; then
		echo "Ping pong counter not incrementing!" >&2
		exit 1
	fi

	echo "  Passed"
}

function perf_test()
{
	USE_DMA=$1

	if [[ $USE_DMA == "1" ]]; then
		WITH="with"
	else
		WITH="without"
	fi

	_modprobe ntb_perf run_order=$PERF_RUN_ORDER \
		max_mw_size=$MAX_MW_SIZE use_dma=$USE_DMA

	echo "Running local perf test $WITH DMA"
	write_file "" "$LOCAL_PERF/run"
	echo -n "  "
	read_file "$LOCAL_PERF/run"
	echo "  Passed"

	echo "Running remote perf test $WITH DMA"
	write_file "" "$REMOTE_PERF/run"
	echo -n "  "
	read_file "$REMOTE_PERF/run"
	echo "  Passed"

	_modprobe -r ntb_perf
}

function ntb_tool_tests()
{
	LOCAL_TOOL="$DEBUGFS/ntb_tool/$LOCAL_DEV"
	REMOTE_TOOL="$REMOTE_HOST:$DEBUGFS/ntb_tool/$REMOTE_DEV"

	echo "Starting ntb_tool tests..."

	_modprobe ntb_tool

	port_test "$LOCAL_TOOL" "$REMOTE_TOOL"

	LOCAL_PEER_TOOL="$LOCAL_TOOL/peer$LOCAL_PIDX"
	REMOTE_PEER_TOOL="$REMOTE_TOOL/peer$REMOTE_PIDX"

	link_test "$LOCAL_PEER_TOOL" "$REMOTE_PEER_TOOL"
	link_test "$REMOTE_PEER_TOOL" "$LOCAL_PEER_TOOL"

	#Ensure the link is up on both sides before continuing
	write_file "Y" "$LOCAL_PEER_TOOL/link_event"
	write_file "Y" "$REMOTE_PEER_TOOL/link_event"

	for PEER_TRANS in $(ls "$LOCAL_TOOL"/peer_trans*); do
		PT=$(basename $PEER_TRANS)
		write_file $MW_SIZE "$LOCAL_TOOL/$PT"
		write_file $MW_SIZE "$REMOTE_TOOL/$PT"
	done

	doorbell_test "$LOCAL_TOOL" "$REMOTE_TOOL"
	doorbell_test "$REMOTE_TOOL" "$LOCAL_TOOL"
	scratchpad_test "$LOCAL_TOOL" "$REMOTE_TOOL"
	scratchpad_test "$REMOTE_TOOL" "$LOCAL_TOOL"

	for MW in $(ls "$LOCAL_TOOL"/mw*); do
		MW=$(basename $MW)

		mw_test $MW "$LOCAL_TOOL" "$REMOTE_TOOL"
		mw_test $MW "$REMOTE_TOOL" "$LOCAL_TOOL"
	done

	_modprobe -r ntb_tool
}

function ntb_pingpong_tests()
{
	LOCAL_PP="$DEBUGFS/ntb_pingpong/$LOCAL_DEV"
	REMOTE_PP="$REMOTE_HOST:$DEBUGFS/ntb_pingpong/$REMOTE_DEV"

	echo "Starting ntb_pingpong tests..."

	_modprobe ntb_pingpong

	pingpong_test $LOCAL_PP $REMOTE_PP

	_modprobe -r ntb_pingpong
}

function ntb_perf_tests()
{
	LOCAL_PERF="$DEBUGFS/ntb_perf/$LOCAL_DEV"
	REMOTE_PERF="$REMOTE_HOST:$DEBUGFS/ntb_perf/$REMOTE_DEV"

	echo "Starting ntb_perf tests..."

	perf_test 0

	if [[ $RUN_DMA_TESTS ]]; then
		perf_test 1
	fi
}

function cleanup()
{
	set +e
	_modprobe -r ntb_tool 2> /dev/null
	_modprobe -r ntb_perf 2> /dev/null
	_modprobe -r ntb_pingpong 2> /dev/null
	_modprobe -r ntb_transport 2> /dev/null
	set -e
}

cleanup

if ! [[ $$DONT_CLEANUP ]]; then
	trap cleanup EXIT
fi

if [ "$(id -u)" != "0" ]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi

if [[ "$LIST_DEVS" == TRUE ]]; then
	echo "Local Devices:"
	ls -1 /sys/bus/ntb/devices
	echo

	if [[ "$REMOTE_HOST" != "" ]]; then
		echo "Remote Devices:"
		ssh $REMOTE_HOST ls -1 /sys/bus/ntb/devices
	fi

	exit 0
fi

if [[ "$LOCAL_DEV" == $"" ]] || [[ "$REMOTE_DEV" == $"" ]]; then
	show_help
	exit 1
fi

ntb_tool_tests
echo
ntb_pingpong_tests
echo
ntb_perf_tests
echo