xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision f4a2e418)
1a246b010SChuck Lever /*
2a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
3a246b010SChuck Lever  *
4a246b010SChuck Lever  * Client-side transport implementation for sockets.
5a246b010SChuck Lever  *
6113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
7113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
8a246b010SChuck Lever  * TCP NFS related read + write fixes
9a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10a246b010SChuck Lever  *
11a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
13a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1455aa4f58SChuck Lever  *
1555aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
168f9d5b1aSChuck Lever  *
178f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
188f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
19a246b010SChuck Lever  */
20a246b010SChuck Lever 
21a246b010SChuck Lever #include <linux/types.h>
22a246b010SChuck Lever #include <linux/slab.h>
23bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
24a246b010SChuck Lever #include <linux/capability.h>
25a246b010SChuck Lever #include <linux/pagemap.h>
26a246b010SChuck Lever #include <linux/errno.h>
27a246b010SChuck Lever #include <linux/socket.h>
28a246b010SChuck Lever #include <linux/in.h>
29a246b010SChuck Lever #include <linux/net.h>
30a246b010SChuck Lever #include <linux/mm.h>
31a246b010SChuck Lever #include <linux/udp.h>
32a246b010SChuck Lever #include <linux/tcp.h>
33a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
3402107148SChuck Lever #include <linux/sunrpc/sched.h>
3549c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
36a246b010SChuck Lever #include <linux/file.h>
37a246b010SChuck Lever 
38a246b010SChuck Lever #include <net/sock.h>
39a246b010SChuck Lever #include <net/checksum.h>
40a246b010SChuck Lever #include <net/udp.h>
41a246b010SChuck Lever #include <net/tcp.h>
42a246b010SChuck Lever 
439903cd1cSChuck Lever /*
44c556b754SChuck Lever  * xprtsock tunables
45c556b754SChuck Lever  */
46c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
48c556b754SChuck Lever 
49c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51c556b754SChuck Lever 
527d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
5325fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
547d1e8255STrond Myklebust 
55c556b754SChuck Lever /*
56fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
57fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
58fbf76683SChuck Lever  * directory become the union of all files registered there.
59fbf76683SChuck Lever  *
60fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
61fbf76683SChuck Lever  * someone else's file names!
62fbf76683SChuck Lever  */
63fbf76683SChuck Lever 
64fbf76683SChuck Lever #ifdef RPC_DEBUG
65fbf76683SChuck Lever 
66fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
67fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
68fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
69fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
70fbf76683SChuck Lever 
71fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
72fbf76683SChuck Lever 
73fbf76683SChuck Lever /*
74fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
75fbf76683SChuck Lever  *        socket buffers for existing UDP transports
76fbf76683SChuck Lever  */
77fbf76683SChuck Lever static ctl_table xs_tunables_table[] = {
78fbf76683SChuck Lever 	{
79fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_UDP,
80fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
81fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
82fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
83fbf76683SChuck Lever 		.mode		= 0644,
84fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
85fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
86fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
87fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
88fbf76683SChuck Lever 	},
89fbf76683SChuck Lever 	{
90fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_TCP,
91fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
92fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
93fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
94fbf76683SChuck Lever 		.mode		= 0644,
95fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
96fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
97fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
98fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
99fbf76683SChuck Lever 	},
100fbf76683SChuck Lever 	{
101fbf76683SChuck Lever 		.ctl_name	= CTL_MIN_RESVPORT,
102fbf76683SChuck Lever 		.procname	= "min_resvport",
103fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
104fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
105fbf76683SChuck Lever 		.mode		= 0644,
106fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
107fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
108fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
109fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
110fbf76683SChuck Lever 	},
111fbf76683SChuck Lever 	{
112fbf76683SChuck Lever 		.ctl_name	= CTL_MAX_RESVPORT,
113fbf76683SChuck Lever 		.procname	= "max_resvport",
114fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
115fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
116fbf76683SChuck Lever 		.mode		= 0644,
117fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
118fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
119fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
120fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
121fbf76683SChuck Lever 	},
122fbf76683SChuck Lever 	{
12325fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
12425fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
12525fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
12625fe6142STrond Myklebust 		.mode		= 0644,
12725fe6142STrond Myklebust 		.proc_handler	= &proc_dointvec_jiffies,
12825fe6142STrond Myklebust 		.strategy	= sysctl_jiffies
12925fe6142STrond Myklebust 	},
13025fe6142STrond Myklebust 	{
131fbf76683SChuck Lever 		.ctl_name = 0,
132fbf76683SChuck Lever 	},
133fbf76683SChuck Lever };
134fbf76683SChuck Lever 
135fbf76683SChuck Lever static ctl_table sunrpc_table[] = {
136fbf76683SChuck Lever 	{
137fbf76683SChuck Lever 		.ctl_name	= CTL_SUNRPC,
138fbf76683SChuck Lever 		.procname	= "sunrpc",
139fbf76683SChuck Lever 		.mode		= 0555,
140fbf76683SChuck Lever 		.child		= xs_tunables_table
141fbf76683SChuck Lever 	},
142fbf76683SChuck Lever 	{
143fbf76683SChuck Lever 		.ctl_name = 0,
144fbf76683SChuck Lever 	},
145fbf76683SChuck Lever };
146fbf76683SChuck Lever 
147fbf76683SChuck Lever #endif
148fbf76683SChuck Lever 
149fbf76683SChuck Lever /*
15003bf4b70SChuck Lever  * Time out for an RPC UDP socket connect.  UDP socket connects are
15103bf4b70SChuck Lever  * synchronous, but we set a timeout anyway in case of resource
15203bf4b70SChuck Lever  * exhaustion on the local host.
15303bf4b70SChuck Lever  */
15403bf4b70SChuck Lever #define XS_UDP_CONN_TO		(5U * HZ)
15503bf4b70SChuck Lever 
15603bf4b70SChuck Lever /*
15703bf4b70SChuck Lever  * Wait duration for an RPC TCP connection to be established.  Solaris
15803bf4b70SChuck Lever  * NFS over TCP uses 60 seconds, for example, which is in line with how
15903bf4b70SChuck Lever  * long a server takes to reboot.
16003bf4b70SChuck Lever  */
16103bf4b70SChuck Lever #define XS_TCP_CONN_TO		(60U * HZ)
16203bf4b70SChuck Lever 
16303bf4b70SChuck Lever /*
16403bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16503bf4b70SChuck Lever  */
16603bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16703bf4b70SChuck Lever 
16803bf4b70SChuck Lever /*
16903bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17003bf4b70SChuck Lever  * kind of resource problem on the local host.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17303bf4b70SChuck Lever 
17403bf4b70SChuck Lever /*
17503bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17603bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17703bf4b70SChuck Lever  *
17803bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17903bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18003bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18103bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18203bf4b70SChuck Lever  */
18303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18403bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
18503bf4b70SChuck Lever 
18603bf4b70SChuck Lever /*
18703bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18803bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18903bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19003bf4b70SChuck Lever  */
19103bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19203bf4b70SChuck Lever 
193a246b010SChuck Lever #ifdef RPC_DEBUG
194a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1959903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
196a246b010SChuck Lever #endif
197a246b010SChuck Lever 
198a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1999903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
200a246b010SChuck Lever {
201a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
202a246b010SChuck Lever 	int j;
203a246b010SChuck Lever 
204a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
205a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
206a246b010SChuck Lever 		if (!(j & 31)) {
207a246b010SChuck Lever 			if (j)
208a246b010SChuck Lever 				dprintk("\n");
209a246b010SChuck Lever 			dprintk("0x%04x ", j);
210a246b010SChuck Lever 		}
211a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
212a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
213a246b010SChuck Lever 	}
214a246b010SChuck Lever 	dprintk("\n");
215a246b010SChuck Lever }
216a246b010SChuck Lever #else
2179903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
218a246b010SChuck Lever {
219a246b010SChuck Lever 	/* NOP */
220a246b010SChuck Lever }
221a246b010SChuck Lever #endif
222a246b010SChuck Lever 
223ffc2e518SChuck Lever struct sock_xprt {
224ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
225ee0ac0c2SChuck Lever 
226ee0ac0c2SChuck Lever 	/*
227ee0ac0c2SChuck Lever 	 * Network layer
228ee0ac0c2SChuck Lever 	 */
229ee0ac0c2SChuck Lever 	struct socket *		sock;
230ee0ac0c2SChuck Lever 	struct sock *		inet;
23151971139SChuck Lever 
23251971139SChuck Lever 	/*
23351971139SChuck Lever 	 * State of TCP reply receive
23451971139SChuck Lever 	 */
23551971139SChuck Lever 	__be32			tcp_fraghdr,
23651971139SChuck Lever 				tcp_xid;
23751971139SChuck Lever 
23851971139SChuck Lever 	u32			tcp_offset,
23951971139SChuck Lever 				tcp_reclen;
24051971139SChuck Lever 
24151971139SChuck Lever 	unsigned long		tcp_copied,
24251971139SChuck Lever 				tcp_flags;
243c8475461SChuck Lever 
244c8475461SChuck Lever 	/*
245c8475461SChuck Lever 	 * Connection of transports
246c8475461SChuck Lever 	 */
24734161db6STrond Myklebust 	struct delayed_work	connect_worker;
248d3bc9a1dSFrank van Maarseveen 	struct sockaddr_storage	addr;
249c8475461SChuck Lever 	unsigned short		port;
2507c6e066eSChuck Lever 
2517c6e066eSChuck Lever 	/*
2527c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2537c6e066eSChuck Lever 	 */
2547c6e066eSChuck Lever 	size_t			rcvsize,
2557c6e066eSChuck Lever 				sndsize;
256314dfd79SChuck Lever 
257314dfd79SChuck Lever 	/*
258314dfd79SChuck Lever 	 * Saved socket callback addresses
259314dfd79SChuck Lever 	 */
260314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
261314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
262314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2632a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
264ffc2e518SChuck Lever };
265ffc2e518SChuck Lever 
266e136d092SChuck Lever /*
267e136d092SChuck Lever  * TCP receive state flags
268e136d092SChuck Lever  */
269e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
270e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
271e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
272e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
273f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
274f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
27518dca02aSRicardo Labiaga 
27618dca02aSRicardo Labiaga /*
27718dca02aSRicardo Labiaga  * TCP RPC flags
27818dca02aSRicardo Labiaga  */
279f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
280e136d092SChuck Lever 
28195392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
282edb267a6SChuck Lever {
28395392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
28495392c59SChuck Lever }
28595392c59SChuck Lever 
28695392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
28795392c59SChuck Lever {
28895392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
28995392c59SChuck Lever }
29095392c59SChuck Lever 
29195392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
29295392c59SChuck Lever {
29395392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
29495392c59SChuck Lever }
29595392c59SChuck Lever 
296b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
297b454ae90SChuck Lever 					  const char *protocol,
298b454ae90SChuck Lever 					  const char *netid)
299edb267a6SChuck Lever {
30095392c59SChuck Lever 	struct sockaddr_in *addr = xs_addr_in(xprt);
301edb267a6SChuck Lever 	char *buf;
302edb267a6SChuck Lever 
303edb267a6SChuck Lever 	buf = kzalloc(20, GFP_KERNEL);
304edb267a6SChuck Lever 	if (buf) {
305e0db4a78SDavid S. Miller 		snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
306edb267a6SChuck Lever 	}
307edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
308edb267a6SChuck Lever 
309edb267a6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
310edb267a6SChuck Lever 	if (buf) {
311edb267a6SChuck Lever 		snprintf(buf, 8, "%u",
312edb267a6SChuck Lever 				ntohs(addr->sin_port));
313edb267a6SChuck Lever 	}
314edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
315edb267a6SChuck Lever 
316b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
317edb267a6SChuck Lever 
318edb267a6SChuck Lever 	buf = kzalloc(48, GFP_KERNEL);
319edb267a6SChuck Lever 	if (buf) {
32021454aaaSHarvey Harrison 		snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
32121454aaaSHarvey Harrison 			&addr->sin_addr.s_addr,
322edb267a6SChuck Lever 			ntohs(addr->sin_port),
323b454ae90SChuck Lever 			protocol);
324edb267a6SChuck Lever 	}
325edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
326fbfe3cc6SChuck Lever 
327fbfe3cc6SChuck Lever 	buf = kzalloc(10, GFP_KERNEL);
328fbfe3cc6SChuck Lever 	if (buf) {
329fbfe3cc6SChuck Lever 		snprintf(buf, 10, "%02x%02x%02x%02x",
330fbfe3cc6SChuck Lever 				NIPQUAD(addr->sin_addr.s_addr));
331fbfe3cc6SChuck Lever 	}
332fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
333fbfe3cc6SChuck Lever 
334fbfe3cc6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
335fbfe3cc6SChuck Lever 	if (buf) {
336fbfe3cc6SChuck Lever 		snprintf(buf, 8, "%4hx",
337fbfe3cc6SChuck Lever 				ntohs(addr->sin_port));
338fbfe3cc6SChuck Lever 	}
339fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
340756805e7SChuck Lever 
341756805e7SChuck Lever 	buf = kzalloc(30, GFP_KERNEL);
342756805e7SChuck Lever 	if (buf) {
34321454aaaSHarvey Harrison 		snprintf(buf, 30, "%pI4.%u.%u",
34421454aaaSHarvey Harrison 				&addr->sin_addr.s_addr,
345756805e7SChuck Lever 				ntohs(addr->sin_port) >> 8,
346756805e7SChuck Lever 				ntohs(addr->sin_port) & 0xff);
347756805e7SChuck Lever 	}
348756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3494417c8c4S\"Talpey, Thomas\ 
350b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
351edb267a6SChuck Lever }
352edb267a6SChuck Lever 
353b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
354b454ae90SChuck Lever 					  const char *protocol,
355b454ae90SChuck Lever 					  const char *netid)
3564b6473fbSChuck Lever {
35795392c59SChuck Lever 	struct sockaddr_in6 *addr = xs_addr_in6(xprt);
3584b6473fbSChuck Lever 	char *buf;
3594b6473fbSChuck Lever 
3604b6473fbSChuck Lever 	buf = kzalloc(40, GFP_KERNEL);
3614b6473fbSChuck Lever 	if (buf) {
3625b095d98SHarvey Harrison 		snprintf(buf, 40, "%pI6",&addr->sin6_addr);
3634b6473fbSChuck Lever 	}
3644b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
3654b6473fbSChuck Lever 
3664b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3674b6473fbSChuck Lever 	if (buf) {
3684b6473fbSChuck Lever 		snprintf(buf, 8, "%u",
3694b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3704b6473fbSChuck Lever 	}
3714b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
3724b6473fbSChuck Lever 
373b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
3744b6473fbSChuck Lever 
3754b6473fbSChuck Lever 	buf = kzalloc(64, GFP_KERNEL);
3764b6473fbSChuck Lever 	if (buf) {
3775b095d98SHarvey Harrison 		snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
378fdb46ee7SHarvey Harrison 				&addr->sin6_addr,
3794b6473fbSChuck Lever 				ntohs(addr->sin6_port),
380b454ae90SChuck Lever 				protocol);
3814b6473fbSChuck Lever 	}
3824b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
3834b6473fbSChuck Lever 
3844b6473fbSChuck Lever 	buf = kzalloc(36, GFP_KERNEL);
385b071195dSHarvey Harrison 	if (buf)
3864b7a4274SHarvey Harrison 		snprintf(buf, 36, "%pi6", &addr->sin6_addr);
387b071195dSHarvey Harrison 
3884b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
3894b6473fbSChuck Lever 
3904b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3914b6473fbSChuck Lever 	if (buf) {
3924b6473fbSChuck Lever 		snprintf(buf, 8, "%4hx",
3934b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3944b6473fbSChuck Lever 	}
3954b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
396756805e7SChuck Lever 
397756805e7SChuck Lever 	buf = kzalloc(50, GFP_KERNEL);
398756805e7SChuck Lever 	if (buf) {
3995b095d98SHarvey Harrison 		snprintf(buf, 50, "%pI6.%u.%u",
400fdb46ee7SHarvey Harrison 			 &addr->sin6_addr,
401756805e7SChuck Lever 			 ntohs(addr->sin6_port) >> 8,
402756805e7SChuck Lever 			 ntohs(addr->sin6_port) & 0xff);
403756805e7SChuck Lever 	}
404756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
4054417c8c4S\"Talpey, Thomas\ 
406b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
407edb267a6SChuck Lever }
408edb267a6SChuck Lever 
409edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
410edb267a6SChuck Lever {
41133e01dc7SChuck Lever 	unsigned int i;
41233e01dc7SChuck Lever 
41333e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
41433e01dc7SChuck Lever 		switch (i) {
41533e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
41633e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
41733e01dc7SChuck Lever 			continue;
41833e01dc7SChuck Lever 		default:
41933e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
42033e01dc7SChuck Lever 		}
421edb267a6SChuck Lever }
422edb267a6SChuck Lever 
423b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
424b4b5cc85SChuck Lever 
42524c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
426b4b5cc85SChuck Lever {
427b4b5cc85SChuck Lever 	struct msghdr msg = {
428b4b5cc85SChuck Lever 		.msg_name	= addr,
429b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
43024c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
43124c5684bSTrond Myklebust 	};
43224c5684bSTrond Myklebust 	struct kvec iov = {
43324c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
43424c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
435b4b5cc85SChuck Lever 	};
436b4b5cc85SChuck Lever 
43724c5684bSTrond Myklebust 	if (iov.iov_len != 0)
438b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
439b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
440b4b5cc85SChuck Lever }
441b4b5cc85SChuck Lever 
44224c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
443b4b5cc85SChuck Lever {
44424c5684bSTrond Myklebust 	struct page **ppage;
44524c5684bSTrond Myklebust 	unsigned int remainder;
44624c5684bSTrond Myklebust 	int err, sent = 0;
447b4b5cc85SChuck Lever 
44824c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
44924c5684bSTrond Myklebust 	base += xdr->page_base;
45024c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
45124c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
45224c5684bSTrond Myklebust 	for(;;) {
45324c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
45424c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
45524c5684bSTrond Myklebust 
45624c5684bSTrond Myklebust 		remainder -= len;
45724c5684bSTrond Myklebust 		if (remainder != 0 || more)
45824c5684bSTrond Myklebust 			flags |= MSG_MORE;
45924c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
46024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
46124c5684bSTrond Myklebust 			break;
46224c5684bSTrond Myklebust 		sent += err;
46324c5684bSTrond Myklebust 		ppage++;
46424c5684bSTrond Myklebust 		base = 0;
46524c5684bSTrond Myklebust 	}
46624c5684bSTrond Myklebust 	if (sent == 0)
46724c5684bSTrond Myklebust 		return err;
46824c5684bSTrond Myklebust 	if (err > 0)
46924c5684bSTrond Myklebust 		sent += err;
47024c5684bSTrond Myklebust 	return sent;
471b4b5cc85SChuck Lever }
472b4b5cc85SChuck Lever 
4739903cd1cSChuck Lever /**
4749903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4759903cd1cSChuck Lever  * @sock: socket to send on
4769903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4779903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4789903cd1cSChuck Lever  * @xdr: buffer containing this request
4799903cd1cSChuck Lever  * @base: starting position in the buffer
4809903cd1cSChuck Lever  *
481a246b010SChuck Lever  */
48224c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
483a246b010SChuck Lever {
48424c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
48524c5684bSTrond Myklebust 	int err, sent = 0;
486a246b010SChuck Lever 
487262965f5SChuck Lever 	if (unlikely(!sock))
488fba91afbSTrond Myklebust 		return -ENOTSOCK;
489262965f5SChuck Lever 
490262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
49124c5684bSTrond Myklebust 	if (base != 0) {
49224c5684bSTrond Myklebust 		addr = NULL;
49324c5684bSTrond Myklebust 		addrlen = 0;
49424c5684bSTrond Myklebust 	}
495262965f5SChuck Lever 
49624c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
49724c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
49824c5684bSTrond Myklebust 		remainder -= len;
49924c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
50024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
501a246b010SChuck Lever 			goto out;
50224c5684bSTrond Myklebust 		sent += err;
503a246b010SChuck Lever 		base = 0;
504a246b010SChuck Lever 	} else
50524c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
506a246b010SChuck Lever 
50724c5684bSTrond Myklebust 	if (base < xdr->page_len) {
50824c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
50924c5684bSTrond Myklebust 		remainder -= len;
51024c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
51124c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
512a246b010SChuck Lever 			goto out;
51324c5684bSTrond Myklebust 		sent += err;
514a246b010SChuck Lever 		base = 0;
51524c5684bSTrond Myklebust 	} else
51624c5684bSTrond Myklebust 		base -= xdr->page_len;
51724c5684bSTrond Myklebust 
51824c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
51924c5684bSTrond Myklebust 		return sent;
52024c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
521a246b010SChuck Lever out:
52224c5684bSTrond Myklebust 	if (sent == 0)
52324c5684bSTrond Myklebust 		return err;
52424c5684bSTrond Myklebust 	if (err > 0)
52524c5684bSTrond Myklebust 		sent += err;
52624c5684bSTrond Myklebust 	return sent;
527a246b010SChuck Lever }
528a246b010SChuck Lever 
529b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
530b6ddf64fSTrond Myklebust {
531b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
532b6ddf64fSTrond Myklebust 
533b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
534b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
535b6ddf64fSTrond Myklebust }
536b6ddf64fSTrond Myklebust 
5379903cd1cSChuck Lever /**
538262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
539262965f5SChuck Lever  * @task: task to put to sleep
5409903cd1cSChuck Lever  *
541a246b010SChuck Lever  */
5425e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
543a246b010SChuck Lever {
544262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
545262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
546ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
5475e3771ceSTrond Myklebust 	int ret = 0;
548a246b010SChuck Lever 
54946121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
550262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
551262965f5SChuck Lever 			req->rq_slen);
552a246b010SChuck Lever 
553262965f5SChuck Lever 	/* Protect against races with write_space */
554262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
555a246b010SChuck Lever 
556262965f5SChuck Lever 	/* Don't race with disconnect */
557b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
558b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
5595e3771ceSTrond Myklebust 			ret = -EAGAIN;
560b6ddf64fSTrond Myklebust 			/*
561b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
562b6ddf64fSTrond Myklebust 			 * window size
563b6ddf64fSTrond Myklebust 			 */
564b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
565b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
566b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
567b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
568b6ddf64fSTrond Myklebust 		}
569b6ddf64fSTrond Myklebust 	} else {
570b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5715e3771ceSTrond Myklebust 		ret = -ENOTCONN;
572b6ddf64fSTrond Myklebust 	}
573a246b010SChuck Lever 
574262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5755e3771ceSTrond Myklebust 	return ret;
576a246b010SChuck Lever }
577a246b010SChuck Lever 
5789903cd1cSChuck Lever /**
579262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5809903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5819903cd1cSChuck Lever  *
5829903cd1cSChuck Lever  * Return values:
5839903cd1cSChuck Lever  *        0:	The request has been sent
5849903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5859903cd1cSChuck Lever  *		complete the request
586262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5879903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5889903cd1cSChuck Lever  */
589262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
590a246b010SChuck Lever {
591a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
592a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
593ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
594262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
595262965f5SChuck Lever 	int status;
596262965f5SChuck Lever 
597262965f5SChuck Lever 	xs_pktdump("packet data:",
598262965f5SChuck Lever 				req->rq_svec->iov_base,
599262965f5SChuck Lever 				req->rq_svec->iov_len);
600262965f5SChuck Lever 
60101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
60201d37c42STrond Myklebust 		return -ENOTCONN;
603ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
60495392c59SChuck Lever 			      xs_addr(xprt),
605ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
606ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
607262965f5SChuck Lever 
608262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
609262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
610262965f5SChuck Lever 
6112199700fSTrond Myklebust 	if (status >= 0) {
6121321d8d9SChuck Lever 		task->tk_bytes_sent += status;
6132199700fSTrond Myklebust 		if (status >= req->rq_slen)
614262965f5SChuck Lever 			return 0;
615262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
616262965f5SChuck Lever 		status = -EAGAIN;
6172199700fSTrond Myklebust 	}
618c8485e4dSTrond Myklebust 	if (!transport->sock)
619c8485e4dSTrond Myklebust 		goto out;
620262965f5SChuck Lever 
621262965f5SChuck Lever 	switch (status) {
622fba91afbSTrond Myklebust 	case -ENOTSOCK:
623fba91afbSTrond Myklebust 		status = -ENOTCONN;
624fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
625fba91afbSTrond Myklebust 		break;
626b6ddf64fSTrond Myklebust 	case -EAGAIN:
6275e3771ceSTrond Myklebust 		status = xs_nospace(task);
628b6ddf64fSTrond Myklebust 		break;
629c8485e4dSTrond Myklebust 	default:
630c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
631c8485e4dSTrond Myklebust 			-status);
632262965f5SChuck Lever 	case -ENETUNREACH:
633262965f5SChuck Lever 	case -EPIPE:
634262965f5SChuck Lever 	case -ECONNREFUSED:
635262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
636262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
637b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
638262965f5SChuck Lever 	}
639c8485e4dSTrond Myklebust out:
640262965f5SChuck Lever 	return status;
641262965f5SChuck Lever }
642262965f5SChuck Lever 
643e06799f9STrond Myklebust /**
644e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
645e06799f9STrond Myklebust  * @xprt: transport
646e06799f9STrond Myklebust  *
647e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
648e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
649e06799f9STrond Myklebust  */
650e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
651e06799f9STrond Myklebust {
652e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
653e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
654e06799f9STrond Myklebust 
655e06799f9STrond Myklebust 	if (sock != NULL)
656e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
657e06799f9STrond Myklebust }
658e06799f9STrond Myklebust 
659808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
660808012fbSChuck Lever {
661808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
662808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
663808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
664808012fbSChuck Lever }
665808012fbSChuck Lever 
666262965f5SChuck Lever /**
667262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
668262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
669262965f5SChuck Lever  *
670262965f5SChuck Lever  * Return values:
671262965f5SChuck Lever  *        0:	The request has been sent
672262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
673262965f5SChuck Lever  *		complete the request
674262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
675262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
676262965f5SChuck Lever  *
677262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
678262965f5SChuck Lever  *	if sendmsg is not able to make progress?
679262965f5SChuck Lever  */
680262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
681262965f5SChuck Lever {
682262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
683262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
684ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
685262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
686b595bb15SChuck Lever 	int status;
687a246b010SChuck Lever 
688808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
689262965f5SChuck Lever 
690262965f5SChuck Lever 	xs_pktdump("packet data:",
691262965f5SChuck Lever 				req->rq_svec->iov_base,
692262965f5SChuck Lever 				req->rq_svec->iov_len);
693a246b010SChuck Lever 
694a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
695a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
696262965f5SChuck Lever 	 * called sendmsg(). */
697a246b010SChuck Lever 	while (1) {
698ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
699ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
700a246b010SChuck Lever 
701262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
702262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
703262965f5SChuck Lever 
704262965f5SChuck Lever 		if (unlikely(status < 0))
705a246b010SChuck Lever 			break;
706a246b010SChuck Lever 
707a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
708a246b010SChuck Lever 		 * reset the count of bytes sent. */
709262965f5SChuck Lever 		req->rq_bytes_sent += status;
710ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
711262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
712a246b010SChuck Lever 			req->rq_bytes_sent = 0;
713a246b010SChuck Lever 			return 0;
714a246b010SChuck Lever 		}
715262965f5SChuck Lever 
71606b4b681STrond Myklebust 		if (status != 0)
71706b4b681STrond Myklebust 			continue;
718a246b010SChuck Lever 		status = -EAGAIN;
719a246b010SChuck Lever 		break;
720a246b010SChuck Lever 	}
721c8485e4dSTrond Myklebust 	if (!transport->sock)
722c8485e4dSTrond Myklebust 		goto out;
723a246b010SChuck Lever 
724262965f5SChuck Lever 	switch (status) {
725fba91afbSTrond Myklebust 	case -ENOTSOCK:
726fba91afbSTrond Myklebust 		status = -ENOTCONN;
727fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
728fba91afbSTrond Myklebust 		break;
729262965f5SChuck Lever 	case -EAGAIN:
7305e3771ceSTrond Myklebust 		status = xs_nospace(task);
731262965f5SChuck Lever 		break;
732262965f5SChuck Lever 	default:
733262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
734262965f5SChuck Lever 			-status);
735a246b010SChuck Lever 	case -ECONNRESET:
73655420c24STrond Myklebust 	case -EPIPE:
737e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
738a246b010SChuck Lever 	case -ECONNREFUSED:
739a246b010SChuck Lever 	case -ENOTCONN:
740a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
741a246b010SChuck Lever 	}
742c8485e4dSTrond Myklebust out:
743a246b010SChuck Lever 	return status;
744a246b010SChuck Lever }
745a246b010SChuck Lever 
7469903cd1cSChuck Lever /**
747e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
748e0ab53deSTrond Myklebust  * @xprt: transport
749e0ab53deSTrond Myklebust  * @task: rpc task
750e0ab53deSTrond Myklebust  *
751e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
752e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
753e0ab53deSTrond Myklebust  * the server.
754e0ab53deSTrond Myklebust  */
755e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
756e0ab53deSTrond Myklebust {
757e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
758e0ab53deSTrond Myklebust 
759e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
760e0ab53deSTrond Myklebust 		return;
761e0ab53deSTrond Myklebust 	if (task == NULL)
762e0ab53deSTrond Myklebust 		goto out_release;
763e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
764e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
765e0ab53deSTrond Myklebust 		goto out_release;
766e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
767e0ab53deSTrond Myklebust 		goto out_release;
768e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
769e0ab53deSTrond Myklebust out_release:
770e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
771e0ab53deSTrond Myklebust }
772e0ab53deSTrond Myklebust 
7732a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7742a9e1cfaSTrond Myklebust {
7752a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7762a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7772a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7782a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7792a9e1cfaSTrond Myklebust }
7802a9e1cfaSTrond Myklebust 
7812a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7822a9e1cfaSTrond Myklebust {
7832a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7842a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7852a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7862a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7872a9e1cfaSTrond Myklebust }
7882a9e1cfaSTrond Myklebust 
789fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
790a246b010SChuck Lever {
791ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
792ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
793a246b010SChuck Lever 
794fe315e76SChuck Lever 	if (sk == NULL)
795fe315e76SChuck Lever 		return;
7969903cd1cSChuck Lever 
797a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
798ee0ac0c2SChuck Lever 	transport->inet = NULL;
799ee0ac0c2SChuck Lever 	transport->sock = NULL;
800a246b010SChuck Lever 
801a246b010SChuck Lever 	sk->sk_user_data = NULL;
8022a9e1cfaSTrond Myklebust 
8032a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
804a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
805a246b010SChuck Lever 
806a246b010SChuck Lever 	sk->sk_no_check = 0;
807a246b010SChuck Lever 
808a246b010SChuck Lever 	sock_release(sock);
809fe315e76SChuck Lever }
810fe315e76SChuck Lever 
811fe315e76SChuck Lever /**
812fe315e76SChuck Lever  * xs_close - close a socket
813fe315e76SChuck Lever  * @xprt: transport
814fe315e76SChuck Lever  *
815fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
816fe315e76SChuck Lever  * on the server we want to save.
817f75e6745STrond Myklebust  *
818f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
819f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
820fe315e76SChuck Lever  */
821fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
822fe315e76SChuck Lever {
823fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
824fe315e76SChuck Lever 
825fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
826fe315e76SChuck Lever 
827fe315e76SChuck Lever 	xs_reset_transport(transport);
828fe315e76SChuck Lever 
829632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
8307d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
831632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
8323b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
833632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
83462da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
835a246b010SChuck Lever }
836a246b010SChuck Lever 
837f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
838f75e6745STrond Myklebust {
839f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
840f75e6745STrond Myklebust 		xs_close(xprt);
841f75e6745STrond Myklebust 	else
842f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
843f75e6745STrond Myklebust }
844f75e6745STrond Myklebust 
8459903cd1cSChuck Lever /**
8469903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8479903cd1cSChuck Lever  * @xprt: doomed transport
8489903cd1cSChuck Lever  *
8499903cd1cSChuck Lever  */
8509903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
851a246b010SChuck Lever {
852c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
853c8475461SChuck Lever 
8549903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
8559903cd1cSChuck Lever 
856c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
857a246b010SChuck Lever 
8589903cd1cSChuck Lever 	xs_close(xprt);
859edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
860a246b010SChuck Lever 	kfree(xprt->slot);
861c8541ecdSChuck Lever 	kfree(xprt);
862bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
863a246b010SChuck Lever }
864a246b010SChuck Lever 
8659903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8669903cd1cSChuck Lever {
8679903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8689903cd1cSChuck Lever }
8699903cd1cSChuck Lever 
8709903cd1cSChuck Lever /**
8719903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8729903cd1cSChuck Lever  * @sk: socket with data to read
8739903cd1cSChuck Lever  * @len: how much data to read
8749903cd1cSChuck Lever  *
875a246b010SChuck Lever  */
8769903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
877a246b010SChuck Lever {
878a246b010SChuck Lever 	struct rpc_task *task;
879a246b010SChuck Lever 	struct rpc_xprt *xprt;
880a246b010SChuck Lever 	struct rpc_rqst *rovr;
881a246b010SChuck Lever 	struct sk_buff *skb;
882a246b010SChuck Lever 	int err, repsize, copied;
883d8ed029dSAlexey Dobriyan 	u32 _xid;
884d8ed029dSAlexey Dobriyan 	__be32 *xp;
885a246b010SChuck Lever 
886a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8879903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8889903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
889a246b010SChuck Lever 		goto out;
890a246b010SChuck Lever 
891a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
892a246b010SChuck Lever 		goto out;
893a246b010SChuck Lever 
894a246b010SChuck Lever 	if (xprt->shutdown)
895a246b010SChuck Lever 		goto dropit;
896a246b010SChuck Lever 
897a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
898a246b010SChuck Lever 	if (repsize < 4) {
8999903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
900a246b010SChuck Lever 		goto dropit;
901a246b010SChuck Lever 	}
902a246b010SChuck Lever 
903a246b010SChuck Lever 	/* Copy the XID from the skb... */
904a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
905a246b010SChuck Lever 				sizeof(_xid), &_xid);
906a246b010SChuck Lever 	if (xp == NULL)
907a246b010SChuck Lever 		goto dropit;
908a246b010SChuck Lever 
909a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
9104a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
911a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
912a246b010SChuck Lever 	if (!rovr)
913a246b010SChuck Lever 		goto out_unlock;
914a246b010SChuck Lever 	task = rovr->rq_task;
915a246b010SChuck Lever 
916a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
917a246b010SChuck Lever 		copied = repsize;
918a246b010SChuck Lever 
919a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
9201781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
9211781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
922a246b010SChuck Lever 		goto out_unlock;
9231781f7f5SHerbert Xu 	}
9241781f7f5SHerbert Xu 
9251781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
926a246b010SChuck Lever 
927a246b010SChuck Lever 	/* Something worked... */
928a246b010SChuck Lever 	dst_confirm(skb->dst);
929a246b010SChuck Lever 
9301570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
9311570c1e4SChuck Lever 	xprt_update_rtt(task);
9321570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
933a246b010SChuck Lever 
934a246b010SChuck Lever  out_unlock:
9354a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
936a246b010SChuck Lever  dropit:
937a246b010SChuck Lever 	skb_free_datagram(sk, skb);
938a246b010SChuck Lever  out:
939a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
940a246b010SChuck Lever }
941a246b010SChuck Lever 
942dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
943a246b010SChuck Lever {
94451971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
945a246b010SChuck Lever 	size_t len, used;
946a246b010SChuck Lever 	char *p;
947a246b010SChuck Lever 
94851971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
94951971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
9509d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
95151971139SChuck Lever 	transport->tcp_offset += used;
952a246b010SChuck Lever 	if (used != len)
953a246b010SChuck Lever 		return;
954808012fbSChuck Lever 
95551971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
95651971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
957e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
958a246b010SChuck Lever 	else
959e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
96051971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
961808012fbSChuck Lever 
962e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
96351971139SChuck Lever 	transport->tcp_offset = 0;
964808012fbSChuck Lever 
965a246b010SChuck Lever 	/* Sanity check of the record length */
96618dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
9679903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
9683ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9699903cd1cSChuck Lever 		return;
970a246b010SChuck Lever 	}
971a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
97251971139SChuck Lever 			transport->tcp_reclen);
973a246b010SChuck Lever }
974a246b010SChuck Lever 
97551971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
976a246b010SChuck Lever {
97751971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
978e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
97951971139SChuck Lever 		transport->tcp_offset = 0;
980e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
981e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
982e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
98351971139SChuck Lever 			transport->tcp_copied = 0;
984a246b010SChuck Lever 		}
985a246b010SChuck Lever 	}
986a246b010SChuck Lever }
987a246b010SChuck Lever 
988dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
989a246b010SChuck Lever {
990a246b010SChuck Lever 	size_t len, used;
991a246b010SChuck Lever 	char *p;
992a246b010SChuck Lever 
99351971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
994a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
99551971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9969d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
99751971139SChuck Lever 	transport->tcp_offset += used;
998a246b010SChuck Lever 	if (used != len)
999a246b010SChuck Lever 		return;
1000e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1001f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
100251971139SChuck Lever 	transport->tcp_copied = 4;
100318dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
100418dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
100518dca02aSRicardo Labiaga 							      : "request with",
100651971139SChuck Lever 			ntohl(transport->tcp_xid));
100751971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1008a246b010SChuck Lever }
1009a246b010SChuck Lever 
101018dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
101118dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
101218dca02aSRicardo Labiaga {
101318dca02aSRicardo Labiaga 	size_t len, used;
101418dca02aSRicardo Labiaga 	u32 offset;
101518dca02aSRicardo Labiaga 	__be32	calldir;
101618dca02aSRicardo Labiaga 
101718dca02aSRicardo Labiaga 	/*
101818dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
101918dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
102018dca02aSRicardo Labiaga 	 * When this function is called for the first time,
102118dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
102218dca02aSRicardo Labiaga 	 */
102318dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
102418dca02aSRicardo Labiaga 	len = sizeof(calldir) - offset;
102518dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
102618dca02aSRicardo Labiaga 	used = xdr_skb_read_bits(desc, &calldir, len);
102718dca02aSRicardo Labiaga 	transport->tcp_offset += used;
102818dca02aSRicardo Labiaga 	if (used != len)
102918dca02aSRicardo Labiaga 		return;
1030f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1031f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
103218dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
1033f4a2e418SRicardo Labiaga 	/*
1034f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
1035f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
1036f4a2e418SRicardo Labiaga 	 */
103718dca02aSRicardo Labiaga 	if (ntohl(calldir) == RPC_REPLY)
103818dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
103918dca02aSRicardo Labiaga 	else
104018dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
104118dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
104218dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ?
104318dca02aSRicardo Labiaga 				"reply for" : "request with", calldir);
104418dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
104518dca02aSRicardo Labiaga }
104618dca02aSRicardo Labiaga 
1047dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1048a246b010SChuck Lever {
104951971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1050a246b010SChuck Lever 	struct rpc_rqst *req;
1051a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
1052a246b010SChuck Lever 	size_t len;
1053a246b010SChuck Lever 	ssize_t r;
1054a246b010SChuck Lever 
1055a246b010SChuck Lever 	/* Find and lock the request corresponding to this xid */
10564a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
105751971139SChuck Lever 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1058a246b010SChuck Lever 	if (!req) {
1059e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1060a246b010SChuck Lever 		dprintk("RPC:       XID %08x request not found!\n",
106151971139SChuck Lever 				ntohl(transport->tcp_xid));
10624a0f8c04SChuck Lever 		spin_unlock(&xprt->transport_lock);
1063a246b010SChuck Lever 		return;
1064a246b010SChuck Lever 	}
1065a246b010SChuck Lever 
1066a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1067f4a2e418SRicardo Labiaga 
1068f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1069f4a2e418SRicardo Labiaga 		/*
1070f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
1071f4a2e418SRicardo Labiaga 		 */
1072f4a2e418SRicardo Labiaga 		__be32	calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1073f4a2e418SRicardo Labiaga 					htonl(RPC_REPLY) : 0;
1074f4a2e418SRicardo Labiaga 
1075f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1076f4a2e418SRicardo Labiaga 			&calldir, sizeof(calldir));
1077f4a2e418SRicardo Labiaga 		transport->tcp_copied += sizeof(calldir);
1078f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1079f4a2e418SRicardo Labiaga 	}
1080f4a2e418SRicardo Labiaga 
1081a246b010SChuck Lever 	len = desc->count;
108251971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1083dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1084a246b010SChuck Lever 
108551971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1086a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1087a246b010SChuck Lever 		my_desc.count = len;
108851971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10899d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1090a246b010SChuck Lever 		desc->count -= r;
1091a246b010SChuck Lever 		desc->offset += r;
1092a246b010SChuck Lever 	} else
109351971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10949d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1095a246b010SChuck Lever 
1096a246b010SChuck Lever 	if (r > 0) {
109751971139SChuck Lever 		transport->tcp_copied += r;
109851971139SChuck Lever 		transport->tcp_offset += r;
1099a246b010SChuck Lever 	}
1100a246b010SChuck Lever 	if (r != len) {
1101a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1102a246b010SChuck Lever 		 * usually because we weren't able to allocate
1103a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1104e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1105a246b010SChuck Lever 		 * will not receive any additional updates,
1106a246b010SChuck Lever 		 * and time out.
1107a246b010SChuck Lever 		 * Any remaining data from this record will
1108a246b010SChuck Lever 		 * be discarded.
1109a246b010SChuck Lever 		 */
1110e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1111a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
111251971139SChuck Lever 				ntohl(transport->tcp_xid));
111346121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
111446121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
111546121cf7SChuck Lever 				xprt, transport->tcp_copied,
111646121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
1117a246b010SChuck Lever 		goto out;
1118a246b010SChuck Lever 	}
1119a246b010SChuck Lever 
1120a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
112151971139SChuck Lever 			ntohl(transport->tcp_xid), r);
112246121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
112346121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
112446121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1125a246b010SChuck Lever 
112651971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1127e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
112851971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1129e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1130e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1131a246b010SChuck Lever 	}
1132a246b010SChuck Lever 
1133a246b010SChuck Lever out:
1134e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
113551971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
11364a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
113751971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1138a246b010SChuck Lever }
1139a246b010SChuck Lever 
1140dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1141a246b010SChuck Lever {
1142a246b010SChuck Lever 	size_t len;
1143a246b010SChuck Lever 
114451971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1145a246b010SChuck Lever 	if (len > desc->count)
1146a246b010SChuck Lever 		len = desc->count;
1147a246b010SChuck Lever 	desc->count -= len;
1148a246b010SChuck Lever 	desc->offset += len;
114951971139SChuck Lever 	transport->tcp_offset += len;
1150a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
115151971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1152a246b010SChuck Lever }
1153a246b010SChuck Lever 
11549903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1155a246b010SChuck Lever {
1156a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
115751971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1158dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1159a246b010SChuck Lever 		.skb	= skb,
1160a246b010SChuck Lever 		.offset	= offset,
1161a246b010SChuck Lever 		.count	= len,
1162a246b010SChuck Lever 	};
1163a246b010SChuck Lever 
11649903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1165a246b010SChuck Lever 	do {
1166a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1167a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1168e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
11699903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1170a246b010SChuck Lever 			continue;
1171a246b010SChuck Lever 		}
1172a246b010SChuck Lever 		/* Read in the xid if necessary */
1173e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
117451971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1175a246b010SChuck Lever 			continue;
1176a246b010SChuck Lever 		}
117718dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1178f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
117918dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
118018dca02aSRicardo Labiaga 			continue;
118118dca02aSRicardo Labiaga 		}
1182a246b010SChuck Lever 		/* Read in the request data */
1183e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
11849903cd1cSChuck Lever 			xs_tcp_read_request(xprt, &desc);
1185a246b010SChuck Lever 			continue;
1186a246b010SChuck Lever 		}
1187a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
118851971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1189a246b010SChuck Lever 	} while (desc.count);
11909903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1191a246b010SChuck Lever 	return len - desc.count;
1192a246b010SChuck Lever }
1193a246b010SChuck Lever 
11949903cd1cSChuck Lever /**
11959903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
11969903cd1cSChuck Lever  * @sk: socket with data to read
11979903cd1cSChuck Lever  * @bytes: how much data to read
11989903cd1cSChuck Lever  *
11999903cd1cSChuck Lever  */
12009903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1201a246b010SChuck Lever {
1202a246b010SChuck Lever 	struct rpc_xprt *xprt;
1203a246b010SChuck Lever 	read_descriptor_t rd_desc;
1204ff2d7db8STrond Myklebust 	int read;
1205a246b010SChuck Lever 
12069903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
120746121cf7SChuck Lever 
120846121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
12099903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1210a246b010SChuck Lever 		goto out;
1211a246b010SChuck Lever 	if (xprt->shutdown)
1212a246b010SChuck Lever 		goto out;
1213a246b010SChuck Lever 
12149903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1215a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1216ff2d7db8STrond Myklebust 	do {
1217a246b010SChuck Lever 		rd_desc.count = 65536;
1218ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1219ff2d7db8STrond Myklebust 	} while (read > 0);
1220a246b010SChuck Lever out:
1221a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1222a246b010SChuck Lever }
1223a246b010SChuck Lever 
12247d1e8255STrond Myklebust /*
12257d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12267d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12277d1e8255STrond Myklebust  * fashion
12287d1e8255STrond Myklebust  */
12297d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12307d1e8255STrond Myklebust 		unsigned long timeout)
12317d1e8255STrond Myklebust {
12327d1e8255STrond Myklebust 	struct sock_xprt *transport;
12337d1e8255STrond Myklebust 
12347d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12357d1e8255STrond Myklebust 		return;
12367d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12377d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12387d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12397d1e8255STrond Myklebust 			   timeout);
12407d1e8255STrond Myklebust }
12417d1e8255STrond Myklebust 
12427d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12437d1e8255STrond Myklebust {
12447d1e8255STrond Myklebust 	struct sock_xprt *transport;
12457d1e8255STrond Myklebust 
12467d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12477d1e8255STrond Myklebust 
12487d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12497d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12507d1e8255STrond Myklebust 		return;
12517d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12527d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
12537d1e8255STrond Myklebust }
12547d1e8255STrond Myklebust 
12557d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
12567d1e8255STrond Myklebust {
12577d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
12587d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12597d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
12607d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
12617d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
12627d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
12637d1e8255STrond Myklebust }
12647d1e8255STrond Myklebust 
12659903cd1cSChuck Lever /**
12669903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
12679903cd1cSChuck Lever  * @sk: socket whose state has changed
12689903cd1cSChuck Lever  *
12699903cd1cSChuck Lever  */
12709903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1271a246b010SChuck Lever {
1272a246b010SChuck Lever 	struct rpc_xprt *xprt;
1273a246b010SChuck Lever 
1274a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1275a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1276a246b010SChuck Lever 		goto out;
12779903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1278a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1279a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1280a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1281a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1282a246b010SChuck Lever 
1283a246b010SChuck Lever 	switch (sk->sk_state) {
1284a246b010SChuck Lever 	case TCP_ESTABLISHED:
12854a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1286a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
128751971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
128851971139SChuck Lever 					struct sock_xprt, xprt);
128951971139SChuck Lever 
1290a246b010SChuck Lever 			/* Reset TCP record info */
129151971139SChuck Lever 			transport->tcp_offset = 0;
129251971139SChuck Lever 			transport->tcp_reclen = 0;
129351971139SChuck Lever 			transport->tcp_copied = 0;
1294e136d092SChuck Lever 			transport->tcp_flags =
1295e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
129651971139SChuck Lever 
12972a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1298a246b010SChuck Lever 		}
12994a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1300a246b010SChuck Lever 		break;
13013b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
13023b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
13037c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1304663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
13053b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
13063b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13073b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1308ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13093b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
131025fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1311a246b010SChuck Lever 		break;
1312632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
13133b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
131466af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1315663b8858STrond Myklebust 	case TCP_SYN_SENT:
13167c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1317663b8858STrond Myklebust 	case TCP_CLOSING:
1318663b8858STrond Myklebust 		/*
1319663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1320663b8858STrond Myklebust 		 * we back off before reconnecting
1321663b8858STrond Myklebust 		 */
1322663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1323663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13243b948ae5STrond Myklebust 		break;
13253b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1326670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
132725fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13283b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13293b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13303b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13313b948ae5STrond Myklebust 		break;
13323b948ae5STrond Myklebust 	case TCP_CLOSE:
13337d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13347d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1335a246b010SChuck Lever 	}
1336a246b010SChuck Lever  out:
1337a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1338a246b010SChuck Lever }
1339a246b010SChuck Lever 
13409903cd1cSChuck Lever /**
1341482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13422a9e1cfaSTrond Myklebust  * @sk: socket
13432a9e1cfaSTrond Myklebust  */
1344482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13452a9e1cfaSTrond Myklebust {
13462a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13472a9e1cfaSTrond Myklebust 
13482a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
13492a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13502a9e1cfaSTrond Myklebust 		goto out;
13512a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
13522a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
13532a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1354482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
13552a9e1cfaSTrond Myklebust out:
13562a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
13572a9e1cfaSTrond Myklebust }
13582a9e1cfaSTrond Myklebust 
13591f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
13601f0fa154SIlpo Järvinen {
13611f0fa154SIlpo Järvinen 	struct socket *sock;
13621f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
13631f0fa154SIlpo Järvinen 
13641f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
13651f0fa154SIlpo Järvinen 		return;
13661f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
13671f0fa154SIlpo Järvinen 
13681f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
13691f0fa154SIlpo Järvinen 		return;
13701f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
13711f0fa154SIlpo Järvinen 		return;
13721f0fa154SIlpo Järvinen 
13731f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
13741f0fa154SIlpo Järvinen }
13751f0fa154SIlpo Järvinen 
13762a9e1cfaSTrond Myklebust /**
1377c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1378c7b2cae8SChuck Lever  *                             becomes available
13799903cd1cSChuck Lever  * @sk: socket whose state has changed
13809903cd1cSChuck Lever  *
1381a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1382a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1383c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1384a246b010SChuck Lever  * with a bunch of small requests.
1385a246b010SChuck Lever  */
1386c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1387a246b010SChuck Lever {
1388a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1389c7b2cae8SChuck Lever 
1390c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
13911f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
13921f0fa154SIlpo Järvinen 		xs_write_space(sk);
1393c7b2cae8SChuck Lever 
1394c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1395c7b2cae8SChuck Lever }
1396c7b2cae8SChuck Lever 
1397c7b2cae8SChuck Lever /**
1398c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1399c7b2cae8SChuck Lever  *                             becomes available
1400c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1401c7b2cae8SChuck Lever  *
1402c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1403c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1404c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1405c7b2cae8SChuck Lever  * with a bunch of small requests.
1406c7b2cae8SChuck Lever  */
1407c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1408c7b2cae8SChuck Lever {
1409c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1410c7b2cae8SChuck Lever 
1411c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
14121f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
14131f0fa154SIlpo Järvinen 		xs_write_space(sk);
1414c7b2cae8SChuck Lever 
1415a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1416a246b010SChuck Lever }
1417a246b010SChuck Lever 
1418470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1419a246b010SChuck Lever {
1420ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1421ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1422a246b010SChuck Lever 
14237c6e066eSChuck Lever 	if (transport->rcvsize) {
1424a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14257c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1426a246b010SChuck Lever 	}
14277c6e066eSChuck Lever 	if (transport->sndsize) {
1428a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14297c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1430a246b010SChuck Lever 		sk->sk_write_space(sk);
1431a246b010SChuck Lever 	}
1432a246b010SChuck Lever }
1433a246b010SChuck Lever 
143443118c29SChuck Lever /**
1435470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
143643118c29SChuck Lever  * @xprt: generic transport
1437470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1438470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
143943118c29SChuck Lever  *
1440470056c2SChuck Lever  * Set socket send and receive buffer size limits.
144143118c29SChuck Lever  */
1442470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
144343118c29SChuck Lever {
14447c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14457c6e066eSChuck Lever 
14467c6e066eSChuck Lever 	transport->sndsize = 0;
1447470056c2SChuck Lever 	if (sndsize)
14487c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14497c6e066eSChuck Lever 	transport->rcvsize = 0;
1450470056c2SChuck Lever 	if (rcvsize)
14517c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1452470056c2SChuck Lever 
1453470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
145443118c29SChuck Lever }
145543118c29SChuck Lever 
145646c0ee8bSChuck Lever /**
145746c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
145846c0ee8bSChuck Lever  * @task: task that timed out
145946c0ee8bSChuck Lever  *
146046c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
146146c0ee8bSChuck Lever  */
146246c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
146346c0ee8bSChuck Lever {
146446c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
146546c0ee8bSChuck Lever }
146646c0ee8bSChuck Lever 
1467b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1468b85d8806SChuck Lever {
1469b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1470b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1471b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1472b85d8806SChuck Lever }
1473b85d8806SChuck Lever 
147492200412SChuck Lever /**
147592200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
147692200412SChuck Lever  * @xprt: generic transport
147792200412SChuck Lever  * @port: new port number
147892200412SChuck Lever  *
147992200412SChuck Lever  */
148092200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
148192200412SChuck Lever {
148295392c59SChuck Lever 	struct sockaddr *addr = xs_addr(xprt);
1483c4efcb1dSChuck Lever 
148492200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1485c4efcb1dSChuck Lever 
148620612005SChuck Lever 	switch (addr->sa_family) {
148720612005SChuck Lever 	case AF_INET:
148820612005SChuck Lever 		((struct sockaddr_in *)addr)->sin_port = htons(port);
148920612005SChuck Lever 		break;
149020612005SChuck Lever 	case AF_INET6:
149120612005SChuck Lever 		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
149220612005SChuck Lever 		break;
149320612005SChuck Lever 	default:
149420612005SChuck Lever 		BUG();
149520612005SChuck Lever 	}
149692200412SChuck Lever }
149792200412SChuck Lever 
149867a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
149967a391d7STrond Myklebust {
150067a391d7STrond Myklebust 	unsigned short port = transport->port;
150167a391d7STrond Myklebust 
150267a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
150367a391d7STrond Myklebust 		port = xs_get_random_port();
150467a391d7STrond Myklebust 	return port;
150567a391d7STrond Myklebust }
150667a391d7STrond Myklebust 
150767a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
150867a391d7STrond Myklebust {
150967a391d7STrond Myklebust 	if (transport->port != 0)
151067a391d7STrond Myklebust 		transport->port = 0;
151167a391d7STrond Myklebust 	if (!transport->xprt.resvport)
151267a391d7STrond Myklebust 		return 0;
151367a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
151467a391d7STrond Myklebust 		return xprt_max_resvport;
151567a391d7STrond Myklebust 	return --port;
151667a391d7STrond Myklebust }
151767a391d7STrond Myklebust 
15187dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1519a246b010SChuck Lever {
1520a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1521a246b010SChuck Lever 		.sin_family = AF_INET,
1522a246b010SChuck Lever 	};
1523d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
152467a391d7STrond Myklebust 	int err, nloop = 0;
152567a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
152667a391d7STrond Myklebust 	unsigned short last;
1527a246b010SChuck Lever 
1528d3bc9a1dSFrank van Maarseveen 	sa = (struct sockaddr_in *)&transport->addr;
1529d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1530a246b010SChuck Lever 	do {
1531a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1532e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1533a246b010SChuck Lever 						sizeof(myaddr));
153467a391d7STrond Myklebust 		if (port == 0)
1535d3bc9a1dSFrank van Maarseveen 			break;
1536a246b010SChuck Lever 		if (err == 0) {
1537c8475461SChuck Lever 			transport->port = port;
1538d3bc9a1dSFrank van Maarseveen 			break;
1539a246b010SChuck Lever 		}
154067a391d7STrond Myklebust 		last = port;
154167a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
154267a391d7STrond Myklebust 		if (port > last)
154367a391d7STrond Myklebust 			nloop++;
154467a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
154521454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
154621454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15477dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1548a246b010SChuck Lever 	return err;
1549a246b010SChuck Lever }
1550a246b010SChuck Lever 
155190058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
155290058d37SChuck Lever {
155390058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
155490058d37SChuck Lever 		.sin6_family = AF_INET6,
155590058d37SChuck Lever 	};
155690058d37SChuck Lever 	struct sockaddr_in6 *sa;
155767a391d7STrond Myklebust 	int err, nloop = 0;
155867a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
155967a391d7STrond Myklebust 	unsigned short last;
156090058d37SChuck Lever 
156190058d37SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->addr;
156290058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
156390058d37SChuck Lever 	do {
156490058d37SChuck Lever 		myaddr.sin6_port = htons(port);
156590058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
156690058d37SChuck Lever 						sizeof(myaddr));
156767a391d7STrond Myklebust 		if (port == 0)
156890058d37SChuck Lever 			break;
156990058d37SChuck Lever 		if (err == 0) {
157090058d37SChuck Lever 			transport->port = port;
157190058d37SChuck Lever 			break;
157290058d37SChuck Lever 		}
157367a391d7STrond Myklebust 		last = port;
157467a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
157567a391d7STrond Myklebust 		if (port > last)
157667a391d7STrond Myklebust 			nloop++;
157767a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
15785b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1579fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1580a246b010SChuck Lever 	return err;
1581a246b010SChuck Lever }
1582a246b010SChuck Lever 
1583ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1584ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1585ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1586ed07536eSPeter Zijlstra 
15878945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1588ed07536eSPeter Zijlstra {
1589ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
15908945ee5eSChuck Lever 
159102b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
15928945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
15938945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1594ed07536eSPeter Zijlstra }
15958945ee5eSChuck Lever 
15968945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
15978945ee5eSChuck Lever {
15988945ee5eSChuck Lever 	struct sock *sk = sock->sk;
15998945ee5eSChuck Lever 
1600f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
16018945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
16028945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1603ed07536eSPeter Zijlstra }
1604ed07536eSPeter Zijlstra #else
16058945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
16068945ee5eSChuck Lever {
16078945ee5eSChuck Lever }
16088945ee5eSChuck Lever 
16098945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1610ed07536eSPeter Zijlstra {
1611ed07536eSPeter Zijlstra }
1612ed07536eSPeter Zijlstra #endif
1613ed07536eSPeter Zijlstra 
161416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1615a246b010SChuck Lever {
161616be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1617edb267a6SChuck Lever 
1618ee0ac0c2SChuck Lever 	if (!transport->inet) {
1619b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1620b0d93ad5SChuck Lever 
1621b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1622b0d93ad5SChuck Lever 
16232a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16242a9e1cfaSTrond Myklebust 
1625b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1626b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1627b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1628482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1629b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1630b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1631b0d93ad5SChuck Lever 
1632b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1633b0d93ad5SChuck Lever 
1634b0d93ad5SChuck Lever 		/* Reset to new socket */
1635ee0ac0c2SChuck Lever 		transport->sock = sock;
1636ee0ac0c2SChuck Lever 		transport->inet = sk;
1637b0d93ad5SChuck Lever 
1638b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1639b0d93ad5SChuck Lever 	}
1640470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
164116be2d20SChuck Lever }
164216be2d20SChuck Lever 
1643a246b010SChuck Lever /**
16449c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1645a246b010SChuck Lever  * @work: RPC transport to connect
1646a246b010SChuck Lever  *
1647a246b010SChuck Lever  * Invoked by a work queue tasklet.
1648a246b010SChuck Lever  */
16499c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1650a246b010SChuck Lever {
1651a246b010SChuck Lever 	struct sock_xprt *transport =
1652a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1653a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1654a246b010SChuck Lever 	struct socket *sock = transport->sock;
1655a246b010SChuck Lever 	int err, status = -EIO;
1656a246b010SChuck Lever 
165701d37c42STrond Myklebust 	if (xprt->shutdown)
16589903cd1cSChuck Lever 		goto out;
16599903cd1cSChuck Lever 
1660a246b010SChuck Lever 	/* Start by resetting any existing state */
1661fe315e76SChuck Lever 	xs_reset_transport(transport);
1662a246b010SChuck Lever 
1663fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1664fe315e76SChuck Lever 	if (err < 0) {
16659903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1666a246b010SChuck Lever 		goto out;
1667a246b010SChuck Lever 	}
16688945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1669a246b010SChuck Lever 
16707dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
16719903cd1cSChuck Lever 		sock_release(sock);
16729903cd1cSChuck Lever 		goto out;
1673a246b010SChuck Lever 	}
1674b0d93ad5SChuck Lever 
1675b0d93ad5SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1676b0d93ad5SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1677b0d93ad5SChuck Lever 
167816be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1679a246b010SChuck Lever 	status = 0;
1680b0d93ad5SChuck Lever out:
1681b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
16827d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1683b0d93ad5SChuck Lever }
1684b0d93ad5SChuck Lever 
168568e220bdSChuck Lever /**
168668e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
168768e220bdSChuck Lever  * @work: RPC transport to connect
168868e220bdSChuck Lever  *
168968e220bdSChuck Lever  * Invoked by a work queue tasklet.
169068e220bdSChuck Lever  */
169168e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
169268e220bdSChuck Lever {
169368e220bdSChuck Lever 	struct sock_xprt *transport =
169468e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
169568e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
169668e220bdSChuck Lever 	struct socket *sock = transport->sock;
169768e220bdSChuck Lever 	int err, status = -EIO;
169868e220bdSChuck Lever 
169901d37c42STrond Myklebust 	if (xprt->shutdown)
170068e220bdSChuck Lever 		goto out;
170168e220bdSChuck Lever 
170268e220bdSChuck Lever 	/* Start by resetting any existing state */
1703fe315e76SChuck Lever 	xs_reset_transport(transport);
170468e220bdSChuck Lever 
1705fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1706fe315e76SChuck Lever 	if (err < 0) {
170768e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
170868e220bdSChuck Lever 		goto out;
170968e220bdSChuck Lever 	}
17108945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
171168e220bdSChuck Lever 
171268e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
171368e220bdSChuck Lever 		sock_release(sock);
171468e220bdSChuck Lever 		goto out;
171568e220bdSChuck Lever 	}
171668e220bdSChuck Lever 
171768e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
171868e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
171968e220bdSChuck Lever 
172068e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1721a246b010SChuck Lever 	status = 0;
1722b0d93ad5SChuck Lever out:
1723b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17247d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1725b0d93ad5SChuck Lever }
1726b0d93ad5SChuck Lever 
17273167e12cSChuck Lever /*
17283167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17293167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17303167e12cSChuck Lever  */
173140d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17323167e12cSChuck Lever {
17333167e12cSChuck Lever 	int result;
17343167e12cSChuck Lever 	struct sockaddr any;
17353167e12cSChuck Lever 
17363167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17373167e12cSChuck Lever 
17383167e12cSChuck Lever 	/*
17393167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17403167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17413167e12cSChuck Lever 	 */
17423167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17433167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1744ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17457d1e8255STrond Myklebust 	if (!result)
17467d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17477d1e8255STrond Myklebust 	else
17483167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17493167e12cSChuck Lever 				result);
17503167e12cSChuck Lever }
17513167e12cSChuck Lever 
175240d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
175340d2549dSTrond Myklebust {
175440d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
175540d2549dSTrond Myklebust 
175640d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
175740d2549dSTrond Myklebust 		return;
175840d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
175940d2549dSTrond Myklebust 		return;
176040d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
176140d2549dSTrond Myklebust }
176240d2549dSTrond Myklebust 
176316be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1764b0d93ad5SChuck Lever {
176516be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1766edb267a6SChuck Lever 
1767ee0ac0c2SChuck Lever 	if (!transport->inet) {
1768b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1769b0d93ad5SChuck Lever 
1770b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1771b0d93ad5SChuck Lever 
17722a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
17732a9e1cfaSTrond Myklebust 
1774b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1775b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1776b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1777b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1778482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1779b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
17803167e12cSChuck Lever 
17813167e12cSChuck Lever 		/* socket options */
17823167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
17833167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
17843167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
17853167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1786b0d93ad5SChuck Lever 
1787b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1788b0d93ad5SChuck Lever 
1789b0d93ad5SChuck Lever 		/* Reset to new socket */
1790ee0ac0c2SChuck Lever 		transport->sock = sock;
1791ee0ac0c2SChuck Lever 		transport->inet = sk;
1792b0d93ad5SChuck Lever 
1793b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1794b0d93ad5SChuck Lever 	}
1795b0d93ad5SChuck Lever 
179601d37c42STrond Myklebust 	if (!xprt_bound(xprt))
179701d37c42STrond Myklebust 		return -ENOTCONN;
179801d37c42STrond Myklebust 
1799b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1800262ca07dSChuck Lever 	xprt->stat.connect_count++;
1801262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
180295392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
180316be2d20SChuck Lever }
180416be2d20SChuck Lever 
180516be2d20SChuck Lever /**
1806b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1807b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1808b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1809b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
181016be2d20SChuck Lever  *
181116be2d20SChuck Lever  * Invoked by a work queue tasklet.
181216be2d20SChuck Lever  */
1813b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1814b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1815b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1816b61d59ffSTrond Myklebust 			struct sock_xprt *))
181716be2d20SChuck Lever {
181816be2d20SChuck Lever 	struct socket *sock = transport->sock;
1819b61d59ffSTrond Myklebust 	int status = -EIO;
182016be2d20SChuck Lever 
182101d37c42STrond Myklebust 	if (xprt->shutdown)
182216be2d20SChuck Lever 		goto out;
182316be2d20SChuck Lever 
182416be2d20SChuck Lever 	if (!sock) {
18257d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1826b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1827b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1828b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
182916be2d20SChuck Lever 			goto out;
183016be2d20SChuck Lever 		}
18317d1e8255STrond Myklebust 	} else {
18327d1e8255STrond Myklebust 		int abort_and_exit;
18337d1e8255STrond Myklebust 
18347d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18357d1e8255STrond Myklebust 				&xprt->state);
183616be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
183740d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
183816be2d20SChuck Lever 
18397d1e8255STrond Myklebust 		if (abort_and_exit)
18407d1e8255STrond Myklebust 			goto out_eagain;
18417d1e8255STrond Myklebust 	}
18427d1e8255STrond Myklebust 
184316be2d20SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
184416be2d20SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
184516be2d20SChuck Lever 
184616be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1847a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
184846121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
184946121cf7SChuck Lever 			sock->sk->sk_state);
1850a246b010SChuck Lever 	switch (status) {
1851f75e6745STrond Myklebust 	default:
1852f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1853f75e6745STrond Myklebust 			__func__, status);
1854f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1855f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1856f75e6745STrond Myklebust 		 * and retry
1857f75e6745STrond Myklebust 		 */
1858f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1859f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
18608a2cec29STrond Myklebust 	case -ECONNREFUSED:
18618a2cec29STrond Myklebust 	case -ECONNRESET:
18628a2cec29STrond Myklebust 	case -ENETUNREACH:
18638a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
18642a491991STrond Myklebust 	case 0:
1865a246b010SChuck Lever 	case -EINPROGRESS:
1866a246b010SChuck Lever 	case -EALREADY:
18677d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
18687d1e8255STrond Myklebust 		return;
18698a2cec29STrond Myklebust 	}
18707d1e8255STrond Myklebust out_eagain:
18712a491991STrond Myklebust 	status = -EAGAIN;
1872a246b010SChuck Lever out:
18732226feb6SChuck Lever 	xprt_clear_connecting(xprt);
18747d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1875a246b010SChuck Lever }
1876a246b010SChuck Lever 
1877b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1878b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1879b61d59ffSTrond Myklebust {
1880b61d59ffSTrond Myklebust 	struct socket *sock;
1881b61d59ffSTrond Myklebust 	int err;
1882b61d59ffSTrond Myklebust 
1883b61d59ffSTrond Myklebust 	/* start from scratch */
1884b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1885b61d59ffSTrond Myklebust 	if (err < 0) {
1886b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1887b61d59ffSTrond Myklebust 				-err);
1888b61d59ffSTrond Myklebust 		goto out_err;
1889b61d59ffSTrond Myklebust 	}
1890b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1891b61d59ffSTrond Myklebust 
1892b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1893b61d59ffSTrond Myklebust 		sock_release(sock);
1894b61d59ffSTrond Myklebust 		goto out_err;
1895b61d59ffSTrond Myklebust 	}
1896b61d59ffSTrond Myklebust 	return sock;
1897b61d59ffSTrond Myklebust out_err:
1898b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1899b61d59ffSTrond Myklebust }
1900b61d59ffSTrond Myklebust 
1901b61d59ffSTrond Myklebust /**
1902a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1903a246b010SChuck Lever  * @work: RPC transport to connect
1904a246b010SChuck Lever  *
1905a246b010SChuck Lever  * Invoked by a work queue tasklet.
1906a246b010SChuck Lever  */
1907a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1908a246b010SChuck Lever {
1909a246b010SChuck Lever 	struct sock_xprt *transport =
1910a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1911a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1912a246b010SChuck Lever 
1913b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1914b61d59ffSTrond Myklebust }
1915a246b010SChuck Lever 
1916b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1917b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1918b61d59ffSTrond Myklebust {
1919b61d59ffSTrond Myklebust 	struct socket *sock;
1920b61d59ffSTrond Myklebust 	int err;
1921b61d59ffSTrond Myklebust 
1922a246b010SChuck Lever 	/* start from scratch */
1923b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1924b61d59ffSTrond Myklebust 	if (err < 0) {
1925b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1926b61d59ffSTrond Myklebust 				-err);
1927b61d59ffSTrond Myklebust 		goto out_err;
19289903cd1cSChuck Lever 	}
1929b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19309903cd1cSChuck Lever 
1931b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1932a246b010SChuck Lever 		sock_release(sock);
1933b61d59ffSTrond Myklebust 		goto out_err;
1934a246b010SChuck Lever 	}
1935b61d59ffSTrond Myklebust 	return sock;
1936b61d59ffSTrond Myklebust out_err:
1937b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1938a246b010SChuck Lever }
1939a246b010SChuck Lever 
1940a246b010SChuck Lever /**
194168e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
194268e220bdSChuck Lever  * @work: RPC transport to connect
194368e220bdSChuck Lever  *
194468e220bdSChuck Lever  * Invoked by a work queue tasklet.
194568e220bdSChuck Lever  */
194668e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
194768e220bdSChuck Lever {
194868e220bdSChuck Lever 	struct sock_xprt *transport =
194968e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
195068e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
195168e220bdSChuck Lever 
1952b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
195368e220bdSChuck Lever }
195468e220bdSChuck Lever 
195568e220bdSChuck Lever /**
1956a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
1957a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
1958a246b010SChuck Lever  *
1959a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
196003bf4b70SChuck Lever  *
196103bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
196203bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
196303bf4b70SChuck Lever  * socket on a privileged port.
196403bf4b70SChuck Lever  *
196503bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
196603bf4b70SChuck Lever  * retry floods (hard mounts).
1967a246b010SChuck Lever  */
1968a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
1969a246b010SChuck Lever {
1970a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
1971ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1972a246b010SChuck Lever 
1973b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
1974b0d93ad5SChuck Lever 		return;
1975b0d93ad5SChuck Lever 
1976ee0ac0c2SChuck Lever 	if (transport->sock != NULL) {
197746121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
197846121cf7SChuck Lever 				"seconds\n",
197903bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
1980c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1981c1384c9cSTrond Myklebust 				   &transport->connect_worker,
198203bf4b70SChuck Lever 				   xprt->reestablish_timeout);
198303bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
198403bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
198503bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
19869903cd1cSChuck Lever 	} else {
19879903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
1988c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1989c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
1990a246b010SChuck Lever 	}
1991a246b010SChuck Lever }
1992a246b010SChuck Lever 
1993e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
1994e06799f9STrond Myklebust {
1995e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1996e06799f9STrond Myklebust 
1997e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
1998e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
1999e06799f9STrond Myklebust 		return;
2000e06799f9STrond Myklebust 	xs_connect(task);
2001e06799f9STrond Myklebust }
2002e06799f9STrond Myklebust 
2003262ca07dSChuck Lever /**
2004262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2005262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2006262ca07dSChuck Lever  * @seq: output file
2007262ca07dSChuck Lever  *
2008262ca07dSChuck Lever  */
2009262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2010262ca07dSChuck Lever {
2011c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2012c8475461SChuck Lever 
2013262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2014c8475461SChuck Lever 			transport->port,
2015262ca07dSChuck Lever 			xprt->stat.bind_count,
2016262ca07dSChuck Lever 			xprt->stat.sends,
2017262ca07dSChuck Lever 			xprt->stat.recvs,
2018262ca07dSChuck Lever 			xprt->stat.bad_xids,
2019262ca07dSChuck Lever 			xprt->stat.req_u,
2020262ca07dSChuck Lever 			xprt->stat.bklog_u);
2021262ca07dSChuck Lever }
2022262ca07dSChuck Lever 
2023262ca07dSChuck Lever /**
2024262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2025262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2026262ca07dSChuck Lever  * @seq: output file
2027262ca07dSChuck Lever  *
2028262ca07dSChuck Lever  */
2029262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2030262ca07dSChuck Lever {
2031c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2032262ca07dSChuck Lever 	long idle_time = 0;
2033262ca07dSChuck Lever 
2034262ca07dSChuck Lever 	if (xprt_connected(xprt))
2035262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2036262ca07dSChuck Lever 
2037262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2038c8475461SChuck Lever 			transport->port,
2039262ca07dSChuck Lever 			xprt->stat.bind_count,
2040262ca07dSChuck Lever 			xprt->stat.connect_count,
2041262ca07dSChuck Lever 			xprt->stat.connect_time,
2042262ca07dSChuck Lever 			idle_time,
2043262ca07dSChuck Lever 			xprt->stat.sends,
2044262ca07dSChuck Lever 			xprt->stat.recvs,
2045262ca07dSChuck Lever 			xprt->stat.bad_xids,
2046262ca07dSChuck Lever 			xprt->stat.req_u,
2047262ca07dSChuck Lever 			xprt->stat.bklog_u);
2048262ca07dSChuck Lever }
2049262ca07dSChuck Lever 
2050262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
205143118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
205212a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
205349e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
205445160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
205592200412SChuck Lever 	.set_port		= xs_set_port,
20569903cd1cSChuck Lever 	.connect		= xs_connect,
205702107148SChuck Lever 	.buf_alloc		= rpc_malloc,
205802107148SChuck Lever 	.buf_free		= rpc_free,
2059262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2060fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
206146c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2062a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2063262965f5SChuck Lever 	.close			= xs_close,
2064262965f5SChuck Lever 	.destroy		= xs_destroy,
2065262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2066262965f5SChuck Lever };
2067262965f5SChuck Lever 
2068262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
206912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2070e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
207145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
207292200412SChuck Lever 	.set_port		= xs_set_port,
2073e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
207402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
207502107148SChuck Lever 	.buf_free		= rpc_free,
2076262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2077fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2078f75e6745STrond Myklebust 	.close			= xs_tcp_close,
20799903cd1cSChuck Lever 	.destroy		= xs_destroy,
2080262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2081a246b010SChuck Lever };
2082a246b010SChuck Lever 
20833c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2084bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2085c8541ecdSChuck Lever {
2086c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2087ffc2e518SChuck Lever 	struct sock_xprt *new;
2088c8541ecdSChuck Lever 
208996802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2090c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2091c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2092c8541ecdSChuck Lever 	}
2093c8541ecdSChuck Lever 
2094ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2095ffc2e518SChuck Lever 	if (new == NULL) {
209646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
209746121cf7SChuck Lever 				"rpc_xprt\n");
2098c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2099c8541ecdSChuck Lever 	}
2100ffc2e518SChuck Lever 	xprt = &new->xprt;
2101c8541ecdSChuck Lever 
2102c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2103c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2104c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2105c8541ecdSChuck Lever 		kfree(xprt);
210646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
210746121cf7SChuck Lever 				"table\n");
2108c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2109c8541ecdSChuck Lever 	}
2110c8541ecdSChuck Lever 
211196802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
211296802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2113d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2114d3bc9a1dSFrank van Maarseveen 		memcpy(&new->addr, args->srcaddr, args->addrlen);
2115c8541ecdSChuck Lever 
2116c8541ecdSChuck Lever 	return xprt;
2117c8541ecdSChuck Lever }
2118c8541ecdSChuck Lever 
21192881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
21202881ae74STrond Myklebust 	.to_initval = 5 * HZ,
21212881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
21222881ae74STrond Myklebust 	.to_increment = 5 * HZ,
21232881ae74STrond Myklebust 	.to_retries = 5,
21242881ae74STrond Myklebust };
21252881ae74STrond Myklebust 
21269903cd1cSChuck Lever /**
21279903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
212896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21299903cd1cSChuck Lever  *
21309903cd1cSChuck Lever  */
2131483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2132a246b010SChuck Lever {
21338f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2134c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2135c8475461SChuck Lever 	struct sock_xprt *transport;
2136a246b010SChuck Lever 
213796802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2138c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2139c8541ecdSChuck Lever 		return xprt;
2140c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2141a246b010SChuck Lever 
2142ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2143808012fbSChuck Lever 	xprt->tsh_size = 0;
2144a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2145a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2146a246b010SChuck Lever 
214703bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
214803bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
214903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
215003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2151a246b010SChuck Lever 
2152262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2153a246b010SChuck Lever 
2154ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2155a246b010SChuck Lever 
21568f9d5b1aSChuck Lever 	switch (addr->sa_family) {
21578f9d5b1aSChuck Lever 	case AF_INET:
21588f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
21598f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21608f9d5b1aSChuck Lever 
21618f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
21628f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
2163b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
21648f9d5b1aSChuck Lever 		break;
21658f9d5b1aSChuck Lever 	case AF_INET6:
21668f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
21678f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21688f9d5b1aSChuck Lever 
21698f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
21708f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
2171b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
21728f9d5b1aSChuck Lever 		break;
21738f9d5b1aSChuck Lever 	default:
21748f9d5b1aSChuck Lever 		kfree(xprt);
21758f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
21768f9d5b1aSChuck Lever 	}
21778f9d5b1aSChuck Lever 
2178edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
21797559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2180edb267a6SChuck Lever 
2181bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2182c8541ecdSChuck Lever 		return xprt;
2183bc25571eS\"Talpey, Thomas\ 
2184bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2185bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2186bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2187a246b010SChuck Lever }
2188a246b010SChuck Lever 
21892881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
21902881ae74STrond Myklebust 	.to_initval = 60 * HZ,
21912881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
21922881ae74STrond Myklebust 	.to_retries = 2,
21932881ae74STrond Myklebust };
21942881ae74STrond Myklebust 
21959903cd1cSChuck Lever /**
21969903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
219796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21989903cd1cSChuck Lever  *
21999903cd1cSChuck Lever  */
2200483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2201a246b010SChuck Lever {
22028f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2203c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2204c8475461SChuck Lever 	struct sock_xprt *transport;
2205a246b010SChuck Lever 
220696802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2207c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2208c8541ecdSChuck Lever 		return xprt;
2209c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2210a246b010SChuck Lever 
2211ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2212808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2213808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2214a246b010SChuck Lever 
221503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
221603bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
221703bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
221803bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2219a246b010SChuck Lever 
2220262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2221ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2222a246b010SChuck Lever 
22238f9d5b1aSChuck Lever 	switch (addr->sa_family) {
22248f9d5b1aSChuck Lever 	case AF_INET:
22258f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
22268f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22278f9d5b1aSChuck Lever 
22288f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2229b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
22308f9d5b1aSChuck Lever 		break;
22318f9d5b1aSChuck Lever 	case AF_INET6:
22328f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
22338f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22348f9d5b1aSChuck Lever 
22358f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2236b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
22378f9d5b1aSChuck Lever 		break;
22388f9d5b1aSChuck Lever 	default:
22398f9d5b1aSChuck Lever 		kfree(xprt);
22408f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
22418f9d5b1aSChuck Lever 	}
22428f9d5b1aSChuck Lever 
2243edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
22447559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2245edb267a6SChuck Lever 
2246bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2247c8541ecdSChuck Lever 		return xprt;
2248bc25571eS\"Talpey, Thomas\ 
2249bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2250bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2251bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2252a246b010SChuck Lever }
2253282b32e1SChuck Lever 
2254bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2255bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2256bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2257bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
22584fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_UDP,
2259bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2260bc25571eS\"Talpey, Thomas\ };
2261bc25571eS\"Talpey, Thomas\ 
2262bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2263bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2264bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2265bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
22664fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_TCP,
2267bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2268bc25571eS\"Talpey, Thomas\ };
2269bc25571eS\"Talpey, Thomas\ 
2270282b32e1SChuck Lever /**
2271bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2272282b32e1SChuck Lever  *
2273282b32e1SChuck Lever  */
2274282b32e1SChuck Lever int init_socket_xprt(void)
2275282b32e1SChuck Lever {
2276fbf76683SChuck Lever #ifdef RPC_DEBUG
22772b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
22780b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2279fbf76683SChuck Lever #endif
2280fbf76683SChuck Lever 
2281bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2282bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2283bc25571eS\"Talpey, Thomas\ 
2284282b32e1SChuck Lever 	return 0;
2285282b32e1SChuck Lever }
2286282b32e1SChuck Lever 
2287282b32e1SChuck Lever /**
2288bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2289282b32e1SChuck Lever  *
2290282b32e1SChuck Lever  */
2291282b32e1SChuck Lever void cleanup_socket_xprt(void)
2292282b32e1SChuck Lever {
2293fbf76683SChuck Lever #ifdef RPC_DEBUG
2294fbf76683SChuck Lever 	if (sunrpc_table_header) {
2295fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2296fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2297fbf76683SChuck Lever 	}
2298fbf76683SChuck Lever #endif
2299bc25571eS\"Talpey, Thomas\ 
2300bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2301bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2302282b32e1SChuck Lever }
2303