• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015-2018 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) International Business Machines  Corp., 2001
5
6VERSION=${VERSION:=3}
7NFILES=${NFILES:=1000}
8SOCKET_TYPE="${SOCKET_TYPE:-udp}"
9NFS_TYPE=${NFS_TYPE:=nfs}
10
11nfs_usage()
12{
13	echo "-t x    Socket type, tcp or udp, default is udp"
14	echo "-v x    NFS version, default is '3'"
15}
16
17nfs_parse_args()
18{
19	case "$1" in
20	v) VERSION="$(echo $2 | tr ',' ' ')";;
21	t) SOCKET_TYPE="$(echo $2 | tr ',' ' ')";;
22	*) [ "$NFS_PARSE_ARGS_CALLER" ] && $NFS_PARSE_ARGS_CALLER "$@";;
23	esac
24}
25
26NFS_PARSE_ARGS_CALLER="$TST_PARSE_ARGS"
27TST_OPTS="v:t:$TST_OPTS"
28TST_PARSE_ARGS=nfs_parse_args
29TST_USAGE=nfs_usage
30TST_NEEDS_TMPDIR=1
31TST_NEEDS_ROOT=1
32TST_NEEDS_CMDS="$TST_NEEDS_CMDS mount exportfs"
33TST_SETUP="${TST_SETUP:-nfs_setup}"
34TST_CLEANUP="${TST_CLEANUP:-nfs_cleanup}"
35TST_NEEDS_DRIVERS="nfsd"
36
37# When set and test is using netns ($TST_USE_NETNS set) NFS traffic will go
38# through lo interface instead of ltp_ns_veth* netns interfaces (useful for
39# debugging whether test failures are related to veth/netns).
40LTP_NFS_NETNS_USE_LO=${LTP_NFS_NETNS_USE_LO:-}
41
42. tst_net.sh
43
44get_socket_type()
45{
46	local t
47	local k=0
48	for t in $SOCKET_TYPE; do
49		if [ "$k" -eq "$1" ]; then
50			echo "${t}${TST_IPV6}"
51			return
52		fi
53		k=$(( k + 1 ))
54	done
55}
56
57nfs_get_remote_path()
58{
59	local v
60	local type=$(get_socket_type ${2:-0})
61
62	for v in $VERSION; do
63		break;
64	done
65
66	v=${1:-$v}
67	echo "$TST_TMPDIR/$v/$type"
68}
69
70nfs_server_udp_enabled()
71{
72	local config f
73
74	tst_rhost_run -c "[ -f /etc/nfs.conf ]" || return 0
75	config=$(tst_rhost_run -c 'for f in $(grep ^include.*= '/etc/nfs.conf' | cut -d = -f2); do [ -f $f ] && printf "$f "; done')
76
77	tst_rhost_run -c "grep -q \"^[# ]*udp *= *y\" /etc/nfs.conf $config"
78}
79
80nfs_setup_server()
81{
82	local export_cmd="exportfs -i -o fsid=$$,no_root_squash,rw *:$remote_dir"
83
84	if tst_net_use_netns; then
85		if ! test -d $remote_dir; then
86			mkdir -p $remote_dir; $export_cmd
87		fi
88	else
89		if ! tst_rhost_run -c "test -d $remote_dir"; then
90			tst_rhost_run -s -c "mkdir -p $remote_dir; $export_cmd"
91		fi
92	fi
93}
94
95nfs_mount()
96{
97	local host_type=rhost
98	local mount_dir
99
100	tst_net_use_netns && host_type=
101
102	if [ $TST_IPV6 ]; then
103		mount_dir="[$(tst_ipaddr $host_type)]:$remote_dir"
104	else
105		mount_dir="$(tst_ipaddr $host_type):$remote_dir"
106	fi
107
108	local mnt_cmd="mount -v -t nfs $opts $mount_dir $local_dir"
109
110	tst_res TINFO "Mounting NFS: $mnt_cmd"
111	if tst_net_use_netns && [ -z "$LTP_NFS_NETNS_USE_LO" ]; then
112		tst_rhost_run -c "$mnt_cmd" > mount.log
113	else
114		$mnt_cmd > mount.log
115	fi
116
117	if [ $? -ne 0 ]; then
118		cat mount.log
119
120		if [ "$type" = "udp" -o "$type" = "udp6" ] && tst_kvcmp -ge 5.6; then
121			tst_brk TCONF "UDP support disabled with the kernel config NFS_DISABLE_UDP_SUPPORT?"
122		fi
123
124		if grep -iq "Protocol not supported" mount.log; then
125			tst_brk TCONF "Protocol not supported"
126		fi
127
128		tst_brk TBROK "mount command failed"
129	fi
130}
131
132nfs_setup()
133{
134	local i
135	local type
136	local n=0
137	local opts
138	local local_dir
139	local remote_dir
140	local mount_dir
141
142	if [ "$(stat -f . | grep "Type: nfs")" ]; then
143		tst_brk TCONF "Cannot run nfs-stress test on mounted NFS"
144	fi
145
146	if tst_cmd_available pgrep; then
147		for i in rpc.mountd rpc.statd; do
148			pgrep $i > /dev/null || tst_brk TCONF "$i not running"
149		done
150	fi
151
152	for i in $VERSION; do
153		type=$(get_socket_type $n)
154		tst_res TINFO "setup NFSv$i, socket type $type"
155
156		if [ "$type" = "udp" -o "$type" = "udp6" ] && ! nfs_server_udp_enabled; then
157			tst_brk TCONF "UDP support disabled on NFS server"
158		fi
159
160		local_dir="$TST_TMPDIR/$i/$n"
161		remote_dir="$TST_TMPDIR/$i/$type"
162		mkdir -p $local_dir
163
164		nfs_setup_server
165
166		opts="-o proto=$type,vers=$i"
167		nfs_mount
168
169		n=$(( n + 1 ))
170	done
171
172	if [ "$n" -eq 1 ]; then
173		cd ${VERSION}/0
174	fi
175}
176
177nfs_cleanup()
178{
179	tst_res TINFO "Cleaning up testcase"
180	cd $LTPROOT
181
182	local i
183	local type
184	local local_dir
185	local remote_dir
186
187	local n=0
188	for i in $VERSION; do
189		local_dir="$TST_TMPDIR/$i/$n"
190		grep -q "$local_dir" /proc/mounts && umount $local_dir
191		n=$(( n + 1 ))
192	done
193
194	n=0
195	for i in $VERSION; do
196		type=$(get_socket_type $n)
197		remote_dir="$TST_TMPDIR/$i/$type"
198		tst_rhost_run -c "test -d $remote_dir && exportfs -u *:$remote_dir"
199		tst_rhost_run -c "test -d $remote_dir && rm -rf $remote_dir"
200		n=$(( n + 1 ))
201	done
202}
203