xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 18dca02aeb3c49dfce87c76be643b139d05cf647)
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)
273*18dca02aSRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 4)
274*18dca02aSRicardo Labiaga 
275*18dca02aSRicardo Labiaga /*
276*18dca02aSRicardo Labiaga  * TCP RPC flags
277*18dca02aSRicardo Labiaga  */
278*18dca02aSRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 5)
279e136d092SChuck Lever 
28095392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
281edb267a6SChuck Lever {
28295392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
28395392c59SChuck Lever }
28495392c59SChuck Lever 
28595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
28695392c59SChuck Lever {
28795392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
28895392c59SChuck Lever }
28995392c59SChuck Lever 
29095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
29195392c59SChuck Lever {
29295392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
29395392c59SChuck Lever }
29495392c59SChuck Lever 
295b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
296b454ae90SChuck Lever 					  const char *protocol,
297b454ae90SChuck Lever 					  const char *netid)
298edb267a6SChuck Lever {
29995392c59SChuck Lever 	struct sockaddr_in *addr = xs_addr_in(xprt);
300edb267a6SChuck Lever 	char *buf;
301edb267a6SChuck Lever 
302edb267a6SChuck Lever 	buf = kzalloc(20, GFP_KERNEL);
303edb267a6SChuck Lever 	if (buf) {
304e0db4a78SDavid S. Miller 		snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
305edb267a6SChuck Lever 	}
306edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
307edb267a6SChuck Lever 
308edb267a6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
309edb267a6SChuck Lever 	if (buf) {
310edb267a6SChuck Lever 		snprintf(buf, 8, "%u",
311edb267a6SChuck Lever 				ntohs(addr->sin_port));
312edb267a6SChuck Lever 	}
313edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
314edb267a6SChuck Lever 
315b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
316edb267a6SChuck Lever 
317edb267a6SChuck Lever 	buf = kzalloc(48, GFP_KERNEL);
318edb267a6SChuck Lever 	if (buf) {
31921454aaaSHarvey Harrison 		snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
32021454aaaSHarvey Harrison 			&addr->sin_addr.s_addr,
321edb267a6SChuck Lever 			ntohs(addr->sin_port),
322b454ae90SChuck Lever 			protocol);
323edb267a6SChuck Lever 	}
324edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
325fbfe3cc6SChuck Lever 
326fbfe3cc6SChuck Lever 	buf = kzalloc(10, GFP_KERNEL);
327fbfe3cc6SChuck Lever 	if (buf) {
328fbfe3cc6SChuck Lever 		snprintf(buf, 10, "%02x%02x%02x%02x",
329fbfe3cc6SChuck Lever 				NIPQUAD(addr->sin_addr.s_addr));
330fbfe3cc6SChuck Lever 	}
331fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
332fbfe3cc6SChuck Lever 
333fbfe3cc6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
334fbfe3cc6SChuck Lever 	if (buf) {
335fbfe3cc6SChuck Lever 		snprintf(buf, 8, "%4hx",
336fbfe3cc6SChuck Lever 				ntohs(addr->sin_port));
337fbfe3cc6SChuck Lever 	}
338fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
339756805e7SChuck Lever 
340756805e7SChuck Lever 	buf = kzalloc(30, GFP_KERNEL);
341756805e7SChuck Lever 	if (buf) {
34221454aaaSHarvey Harrison 		snprintf(buf, 30, "%pI4.%u.%u",
34321454aaaSHarvey Harrison 				&addr->sin_addr.s_addr,
344756805e7SChuck Lever 				ntohs(addr->sin_port) >> 8,
345756805e7SChuck Lever 				ntohs(addr->sin_port) & 0xff);
346756805e7SChuck Lever 	}
347756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3484417c8c4S\"Talpey, Thomas\ 
349b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
350edb267a6SChuck Lever }
351edb267a6SChuck Lever 
352b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
353b454ae90SChuck Lever 					  const char *protocol,
354b454ae90SChuck Lever 					  const char *netid)
3554b6473fbSChuck Lever {
35695392c59SChuck Lever 	struct sockaddr_in6 *addr = xs_addr_in6(xprt);
3574b6473fbSChuck Lever 	char *buf;
3584b6473fbSChuck Lever 
3594b6473fbSChuck Lever 	buf = kzalloc(40, GFP_KERNEL);
3604b6473fbSChuck Lever 	if (buf) {
3615b095d98SHarvey Harrison 		snprintf(buf, 40, "%pI6",&addr->sin6_addr);
3624b6473fbSChuck Lever 	}
3634b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
3644b6473fbSChuck Lever 
3654b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3664b6473fbSChuck Lever 	if (buf) {
3674b6473fbSChuck Lever 		snprintf(buf, 8, "%u",
3684b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3694b6473fbSChuck Lever 	}
3704b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
3714b6473fbSChuck Lever 
372b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
3734b6473fbSChuck Lever 
3744b6473fbSChuck Lever 	buf = kzalloc(64, GFP_KERNEL);
3754b6473fbSChuck Lever 	if (buf) {
3765b095d98SHarvey Harrison 		snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
377fdb46ee7SHarvey Harrison 				&addr->sin6_addr,
3784b6473fbSChuck Lever 				ntohs(addr->sin6_port),
379b454ae90SChuck Lever 				protocol);
3804b6473fbSChuck Lever 	}
3814b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
3824b6473fbSChuck Lever 
3834b6473fbSChuck Lever 	buf = kzalloc(36, GFP_KERNEL);
384b071195dSHarvey Harrison 	if (buf)
3854b7a4274SHarvey Harrison 		snprintf(buf, 36, "%pi6", &addr->sin6_addr);
386b071195dSHarvey Harrison 
3874b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
3884b6473fbSChuck Lever 
3894b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3904b6473fbSChuck Lever 	if (buf) {
3914b6473fbSChuck Lever 		snprintf(buf, 8, "%4hx",
3924b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3934b6473fbSChuck Lever 	}
3944b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
395756805e7SChuck Lever 
396756805e7SChuck Lever 	buf = kzalloc(50, GFP_KERNEL);
397756805e7SChuck Lever 	if (buf) {
3985b095d98SHarvey Harrison 		snprintf(buf, 50, "%pI6.%u.%u",
399fdb46ee7SHarvey Harrison 			 &addr->sin6_addr,
400756805e7SChuck Lever 			 ntohs(addr->sin6_port) >> 8,
401756805e7SChuck Lever 			 ntohs(addr->sin6_port) & 0xff);
402756805e7SChuck Lever 	}
403756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
4044417c8c4S\"Talpey, Thomas\ 
405b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
406edb267a6SChuck Lever }
407edb267a6SChuck Lever 
408edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
409edb267a6SChuck Lever {
41033e01dc7SChuck Lever 	unsigned int i;
41133e01dc7SChuck Lever 
41233e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
41333e01dc7SChuck Lever 		switch (i) {
41433e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
41533e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
41633e01dc7SChuck Lever 			continue;
41733e01dc7SChuck Lever 		default:
41833e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
41933e01dc7SChuck Lever 		}
420edb267a6SChuck Lever }
421edb267a6SChuck Lever 
422b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
423b4b5cc85SChuck Lever 
42424c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
425b4b5cc85SChuck Lever {
426b4b5cc85SChuck Lever 	struct msghdr msg = {
427b4b5cc85SChuck Lever 		.msg_name	= addr,
428b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
42924c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
43024c5684bSTrond Myklebust 	};
43124c5684bSTrond Myklebust 	struct kvec iov = {
43224c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
43324c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
434b4b5cc85SChuck Lever 	};
435b4b5cc85SChuck Lever 
43624c5684bSTrond Myklebust 	if (iov.iov_len != 0)
437b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
438b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
439b4b5cc85SChuck Lever }
440b4b5cc85SChuck Lever 
44124c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
442b4b5cc85SChuck Lever {
44324c5684bSTrond Myklebust 	struct page **ppage;
44424c5684bSTrond Myklebust 	unsigned int remainder;
44524c5684bSTrond Myklebust 	int err, sent = 0;
446b4b5cc85SChuck Lever 
44724c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
44824c5684bSTrond Myklebust 	base += xdr->page_base;
44924c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
45024c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
45124c5684bSTrond Myklebust 	for(;;) {
45224c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
45324c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
45424c5684bSTrond Myklebust 
45524c5684bSTrond Myklebust 		remainder -= len;
45624c5684bSTrond Myklebust 		if (remainder != 0 || more)
45724c5684bSTrond Myklebust 			flags |= MSG_MORE;
45824c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
45924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
46024c5684bSTrond Myklebust 			break;
46124c5684bSTrond Myklebust 		sent += err;
46224c5684bSTrond Myklebust 		ppage++;
46324c5684bSTrond Myklebust 		base = 0;
46424c5684bSTrond Myklebust 	}
46524c5684bSTrond Myklebust 	if (sent == 0)
46624c5684bSTrond Myklebust 		return err;
46724c5684bSTrond Myklebust 	if (err > 0)
46824c5684bSTrond Myklebust 		sent += err;
46924c5684bSTrond Myklebust 	return sent;
470b4b5cc85SChuck Lever }
471b4b5cc85SChuck Lever 
4729903cd1cSChuck Lever /**
4739903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4749903cd1cSChuck Lever  * @sock: socket to send on
4759903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4769903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4779903cd1cSChuck Lever  * @xdr: buffer containing this request
4789903cd1cSChuck Lever  * @base: starting position in the buffer
4799903cd1cSChuck Lever  *
480a246b010SChuck Lever  */
48124c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
482a246b010SChuck Lever {
48324c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
48424c5684bSTrond Myklebust 	int err, sent = 0;
485a246b010SChuck Lever 
486262965f5SChuck Lever 	if (unlikely(!sock))
487fba91afbSTrond Myklebust 		return -ENOTSOCK;
488262965f5SChuck Lever 
489262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
49024c5684bSTrond Myklebust 	if (base != 0) {
49124c5684bSTrond Myklebust 		addr = NULL;
49224c5684bSTrond Myklebust 		addrlen = 0;
49324c5684bSTrond Myklebust 	}
494262965f5SChuck Lever 
49524c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
49624c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
49724c5684bSTrond Myklebust 		remainder -= len;
49824c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
49924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
500a246b010SChuck Lever 			goto out;
50124c5684bSTrond Myklebust 		sent += err;
502a246b010SChuck Lever 		base = 0;
503a246b010SChuck Lever 	} else
50424c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
505a246b010SChuck Lever 
50624c5684bSTrond Myklebust 	if (base < xdr->page_len) {
50724c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
50824c5684bSTrond Myklebust 		remainder -= len;
50924c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
51024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
511a246b010SChuck Lever 			goto out;
51224c5684bSTrond Myklebust 		sent += err;
513a246b010SChuck Lever 		base = 0;
51424c5684bSTrond Myklebust 	} else
51524c5684bSTrond Myklebust 		base -= xdr->page_len;
51624c5684bSTrond Myklebust 
51724c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
51824c5684bSTrond Myklebust 		return sent;
51924c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
520a246b010SChuck Lever out:
52124c5684bSTrond Myklebust 	if (sent == 0)
52224c5684bSTrond Myklebust 		return err;
52324c5684bSTrond Myklebust 	if (err > 0)
52424c5684bSTrond Myklebust 		sent += err;
52524c5684bSTrond Myklebust 	return sent;
526a246b010SChuck Lever }
527a246b010SChuck Lever 
528b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
529b6ddf64fSTrond Myklebust {
530b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
531b6ddf64fSTrond Myklebust 
532b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
533b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
534b6ddf64fSTrond Myklebust }
535b6ddf64fSTrond Myklebust 
5369903cd1cSChuck Lever /**
537262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
538262965f5SChuck Lever  * @task: task to put to sleep
5399903cd1cSChuck Lever  *
540a246b010SChuck Lever  */
5415e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
542a246b010SChuck Lever {
543262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
544262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
545ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
5465e3771ceSTrond Myklebust 	int ret = 0;
547a246b010SChuck Lever 
54846121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
549262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
550262965f5SChuck Lever 			req->rq_slen);
551a246b010SChuck Lever 
552262965f5SChuck Lever 	/* Protect against races with write_space */
553262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
554a246b010SChuck Lever 
555262965f5SChuck Lever 	/* Don't race with disconnect */
556b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
557b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
5585e3771ceSTrond Myklebust 			ret = -EAGAIN;
559b6ddf64fSTrond Myklebust 			/*
560b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
561b6ddf64fSTrond Myklebust 			 * window size
562b6ddf64fSTrond Myklebust 			 */
563b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
564b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
565b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
566b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
567b6ddf64fSTrond Myklebust 		}
568b6ddf64fSTrond Myklebust 	} else {
569b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5705e3771ceSTrond Myklebust 		ret = -ENOTCONN;
571b6ddf64fSTrond Myklebust 	}
572a246b010SChuck Lever 
573262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5745e3771ceSTrond Myklebust 	return ret;
575a246b010SChuck Lever }
576a246b010SChuck Lever 
5779903cd1cSChuck Lever /**
578262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5799903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5809903cd1cSChuck Lever  *
5819903cd1cSChuck Lever  * Return values:
5829903cd1cSChuck Lever  *        0:	The request has been sent
5839903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5849903cd1cSChuck Lever  *		complete the request
585262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5869903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5879903cd1cSChuck Lever  */
588262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
589a246b010SChuck Lever {
590a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
591a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
592ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
593262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
594262965f5SChuck Lever 	int status;
595262965f5SChuck Lever 
596262965f5SChuck Lever 	xs_pktdump("packet data:",
597262965f5SChuck Lever 				req->rq_svec->iov_base,
598262965f5SChuck Lever 				req->rq_svec->iov_len);
599262965f5SChuck Lever 
60001d37c42STrond Myklebust 	if (!xprt_bound(xprt))
60101d37c42STrond Myklebust 		return -ENOTCONN;
602ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
60395392c59SChuck Lever 			      xs_addr(xprt),
604ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
605ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
606262965f5SChuck Lever 
607262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
608262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
609262965f5SChuck Lever 
6102199700fSTrond Myklebust 	if (status >= 0) {
6111321d8d9SChuck Lever 		task->tk_bytes_sent += status;
6122199700fSTrond Myklebust 		if (status >= req->rq_slen)
613262965f5SChuck Lever 			return 0;
614262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
615262965f5SChuck Lever 		status = -EAGAIN;
6162199700fSTrond Myklebust 	}
617c8485e4dSTrond Myklebust 	if (!transport->sock)
618c8485e4dSTrond Myklebust 		goto out;
619262965f5SChuck Lever 
620262965f5SChuck Lever 	switch (status) {
621fba91afbSTrond Myklebust 	case -ENOTSOCK:
622fba91afbSTrond Myklebust 		status = -ENOTCONN;
623fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
624fba91afbSTrond Myklebust 		break;
625b6ddf64fSTrond Myklebust 	case -EAGAIN:
6265e3771ceSTrond Myklebust 		status = xs_nospace(task);
627b6ddf64fSTrond Myklebust 		break;
628c8485e4dSTrond Myklebust 	default:
629c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
630c8485e4dSTrond Myklebust 			-status);
631262965f5SChuck Lever 	case -ENETUNREACH:
632262965f5SChuck Lever 	case -EPIPE:
633262965f5SChuck Lever 	case -ECONNREFUSED:
634262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
635262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
636b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
637262965f5SChuck Lever 	}
638c8485e4dSTrond Myklebust out:
639262965f5SChuck Lever 	return status;
640262965f5SChuck Lever }
641262965f5SChuck Lever 
642e06799f9STrond Myklebust /**
643e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
644e06799f9STrond Myklebust  * @xprt: transport
645e06799f9STrond Myklebust  *
646e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
647e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
648e06799f9STrond Myklebust  */
649e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
650e06799f9STrond Myklebust {
651e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
652e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
653e06799f9STrond Myklebust 
654e06799f9STrond Myklebust 	if (sock != NULL)
655e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
656e06799f9STrond Myklebust }
657e06799f9STrond Myklebust 
658808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
659808012fbSChuck Lever {
660808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
661808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
662808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
663808012fbSChuck Lever }
664808012fbSChuck Lever 
665262965f5SChuck Lever /**
666262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
667262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
668262965f5SChuck Lever  *
669262965f5SChuck Lever  * Return values:
670262965f5SChuck Lever  *        0:	The request has been sent
671262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
672262965f5SChuck Lever  *		complete the request
673262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
674262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
675262965f5SChuck Lever  *
676262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
677262965f5SChuck Lever  *	if sendmsg is not able to make progress?
678262965f5SChuck Lever  */
679262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
680262965f5SChuck Lever {
681262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
682262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
683ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
684262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
685b595bb15SChuck Lever 	int status;
686a246b010SChuck Lever 
687808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
688262965f5SChuck Lever 
689262965f5SChuck Lever 	xs_pktdump("packet data:",
690262965f5SChuck Lever 				req->rq_svec->iov_base,
691262965f5SChuck Lever 				req->rq_svec->iov_len);
692a246b010SChuck Lever 
693a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
694a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
695262965f5SChuck Lever 	 * called sendmsg(). */
696a246b010SChuck Lever 	while (1) {
697ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
698ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
699a246b010SChuck Lever 
700262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
701262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
702262965f5SChuck Lever 
703262965f5SChuck Lever 		if (unlikely(status < 0))
704a246b010SChuck Lever 			break;
705a246b010SChuck Lever 
706a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
707a246b010SChuck Lever 		 * reset the count of bytes sent. */
708262965f5SChuck Lever 		req->rq_bytes_sent += status;
709ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
710262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
711a246b010SChuck Lever 			req->rq_bytes_sent = 0;
712a246b010SChuck Lever 			return 0;
713a246b010SChuck Lever 		}
714262965f5SChuck Lever 
71506b4b681STrond Myklebust 		if (status != 0)
71606b4b681STrond Myklebust 			continue;
717a246b010SChuck Lever 		status = -EAGAIN;
718a246b010SChuck Lever 		break;
719a246b010SChuck Lever 	}
720c8485e4dSTrond Myklebust 	if (!transport->sock)
721c8485e4dSTrond Myklebust 		goto out;
722a246b010SChuck Lever 
723262965f5SChuck Lever 	switch (status) {
724fba91afbSTrond Myklebust 	case -ENOTSOCK:
725fba91afbSTrond Myklebust 		status = -ENOTCONN;
726fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
727fba91afbSTrond Myklebust 		break;
728262965f5SChuck Lever 	case -EAGAIN:
7295e3771ceSTrond Myklebust 		status = xs_nospace(task);
730262965f5SChuck Lever 		break;
731262965f5SChuck Lever 	default:
732262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
733262965f5SChuck Lever 			-status);
734a246b010SChuck Lever 	case -ECONNRESET:
73555420c24STrond Myklebust 	case -EPIPE:
736e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
737a246b010SChuck Lever 	case -ECONNREFUSED:
738a246b010SChuck Lever 	case -ENOTCONN:
739a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
740a246b010SChuck Lever 	}
741c8485e4dSTrond Myklebust out:
742a246b010SChuck Lever 	return status;
743a246b010SChuck Lever }
744a246b010SChuck Lever 
7459903cd1cSChuck Lever /**
746e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
747e0ab53deSTrond Myklebust  * @xprt: transport
748e0ab53deSTrond Myklebust  * @task: rpc task
749e0ab53deSTrond Myklebust  *
750e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
751e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
752e0ab53deSTrond Myklebust  * the server.
753e0ab53deSTrond Myklebust  */
754e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
755e0ab53deSTrond Myklebust {
756e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
757e0ab53deSTrond Myklebust 
758e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
759e0ab53deSTrond Myklebust 		return;
760e0ab53deSTrond Myklebust 	if (task == NULL)
761e0ab53deSTrond Myklebust 		goto out_release;
762e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
763e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
764e0ab53deSTrond Myklebust 		goto out_release;
765e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
766e0ab53deSTrond Myklebust 		goto out_release;
767e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
768e0ab53deSTrond Myklebust out_release:
769e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
770e0ab53deSTrond Myklebust }
771e0ab53deSTrond Myklebust 
7722a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7732a9e1cfaSTrond Myklebust {
7742a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7752a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7762a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7772a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7782a9e1cfaSTrond Myklebust }
7792a9e1cfaSTrond Myklebust 
7802a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7812a9e1cfaSTrond Myklebust {
7822a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7832a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7842a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7852a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7862a9e1cfaSTrond Myklebust }
7872a9e1cfaSTrond Myklebust 
788fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
789a246b010SChuck Lever {
790ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
791ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
792a246b010SChuck Lever 
793fe315e76SChuck Lever 	if (sk == NULL)
794fe315e76SChuck Lever 		return;
7959903cd1cSChuck Lever 
796a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
797ee0ac0c2SChuck Lever 	transport->inet = NULL;
798ee0ac0c2SChuck Lever 	transport->sock = NULL;
799a246b010SChuck Lever 
800a246b010SChuck Lever 	sk->sk_user_data = NULL;
8012a9e1cfaSTrond Myklebust 
8022a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
803a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
804a246b010SChuck Lever 
805a246b010SChuck Lever 	sk->sk_no_check = 0;
806a246b010SChuck Lever 
807a246b010SChuck Lever 	sock_release(sock);
808fe315e76SChuck Lever }
809fe315e76SChuck Lever 
810fe315e76SChuck Lever /**
811fe315e76SChuck Lever  * xs_close - close a socket
812fe315e76SChuck Lever  * @xprt: transport
813fe315e76SChuck Lever  *
814fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
815fe315e76SChuck Lever  * on the server we want to save.
816f75e6745STrond Myklebust  *
817f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
818f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
819fe315e76SChuck Lever  */
820fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
821fe315e76SChuck Lever {
822fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
823fe315e76SChuck Lever 
824fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
825fe315e76SChuck Lever 
826fe315e76SChuck Lever 	xs_reset_transport(transport);
827fe315e76SChuck Lever 
828632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
8297d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
830632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
8313b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
832632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
83362da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
834a246b010SChuck Lever }
835a246b010SChuck Lever 
836f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
837f75e6745STrond Myklebust {
838f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
839f75e6745STrond Myklebust 		xs_close(xprt);
840f75e6745STrond Myklebust 	else
841f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
842f75e6745STrond Myklebust }
843f75e6745STrond Myklebust 
8449903cd1cSChuck Lever /**
8459903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8469903cd1cSChuck Lever  * @xprt: doomed transport
8479903cd1cSChuck Lever  *
8489903cd1cSChuck Lever  */
8499903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
850a246b010SChuck Lever {
851c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
852c8475461SChuck Lever 
8539903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
8549903cd1cSChuck Lever 
855c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
856a246b010SChuck Lever 
8579903cd1cSChuck Lever 	xs_close(xprt);
858edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
859a246b010SChuck Lever 	kfree(xprt->slot);
860c8541ecdSChuck Lever 	kfree(xprt);
861bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
862a246b010SChuck Lever }
863a246b010SChuck Lever 
8649903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8659903cd1cSChuck Lever {
8669903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8679903cd1cSChuck Lever }
8689903cd1cSChuck Lever 
8699903cd1cSChuck Lever /**
8709903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8719903cd1cSChuck Lever  * @sk: socket with data to read
8729903cd1cSChuck Lever  * @len: how much data to read
8739903cd1cSChuck Lever  *
874a246b010SChuck Lever  */
8759903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
876a246b010SChuck Lever {
877a246b010SChuck Lever 	struct rpc_task *task;
878a246b010SChuck Lever 	struct rpc_xprt *xprt;
879a246b010SChuck Lever 	struct rpc_rqst *rovr;
880a246b010SChuck Lever 	struct sk_buff *skb;
881a246b010SChuck Lever 	int err, repsize, copied;
882d8ed029dSAlexey Dobriyan 	u32 _xid;
883d8ed029dSAlexey Dobriyan 	__be32 *xp;
884a246b010SChuck Lever 
885a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8869903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8879903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
888a246b010SChuck Lever 		goto out;
889a246b010SChuck Lever 
890a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
891a246b010SChuck Lever 		goto out;
892a246b010SChuck Lever 
893a246b010SChuck Lever 	if (xprt->shutdown)
894a246b010SChuck Lever 		goto dropit;
895a246b010SChuck Lever 
896a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
897a246b010SChuck Lever 	if (repsize < 4) {
8989903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
899a246b010SChuck Lever 		goto dropit;
900a246b010SChuck Lever 	}
901a246b010SChuck Lever 
902a246b010SChuck Lever 	/* Copy the XID from the skb... */
903a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
904a246b010SChuck Lever 				sizeof(_xid), &_xid);
905a246b010SChuck Lever 	if (xp == NULL)
906a246b010SChuck Lever 		goto dropit;
907a246b010SChuck Lever 
908a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
9094a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
910a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
911a246b010SChuck Lever 	if (!rovr)
912a246b010SChuck Lever 		goto out_unlock;
913a246b010SChuck Lever 	task = rovr->rq_task;
914a246b010SChuck Lever 
915a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
916a246b010SChuck Lever 		copied = repsize;
917a246b010SChuck Lever 
918a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
9191781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
9201781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
921a246b010SChuck Lever 		goto out_unlock;
9221781f7f5SHerbert Xu 	}
9231781f7f5SHerbert Xu 
9241781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
925a246b010SChuck Lever 
926a246b010SChuck Lever 	/* Something worked... */
927a246b010SChuck Lever 	dst_confirm(skb->dst);
928a246b010SChuck Lever 
9291570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
9301570c1e4SChuck Lever 	xprt_update_rtt(task);
9311570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
932a246b010SChuck Lever 
933a246b010SChuck Lever  out_unlock:
9344a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
935a246b010SChuck Lever  dropit:
936a246b010SChuck Lever 	skb_free_datagram(sk, skb);
937a246b010SChuck Lever  out:
938a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
939a246b010SChuck Lever }
940a246b010SChuck Lever 
941dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
942a246b010SChuck Lever {
94351971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
944a246b010SChuck Lever 	size_t len, used;
945a246b010SChuck Lever 	char *p;
946a246b010SChuck Lever 
94751971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
94851971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
9499d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
95051971139SChuck Lever 	transport->tcp_offset += used;
951a246b010SChuck Lever 	if (used != len)
952a246b010SChuck Lever 		return;
953808012fbSChuck Lever 
95451971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
95551971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
956e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
957a246b010SChuck Lever 	else
958e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
95951971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
960808012fbSChuck Lever 
961e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
96251971139SChuck Lever 	transport->tcp_offset = 0;
963808012fbSChuck Lever 
964a246b010SChuck Lever 	/* Sanity check of the record length */
965*18dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
9669903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
9673ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9689903cd1cSChuck Lever 		return;
969a246b010SChuck Lever 	}
970a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
97151971139SChuck Lever 			transport->tcp_reclen);
972a246b010SChuck Lever }
973a246b010SChuck Lever 
97451971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
975a246b010SChuck Lever {
97651971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
977e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
97851971139SChuck Lever 		transport->tcp_offset = 0;
979e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
980e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
981e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
98251971139SChuck Lever 			transport->tcp_copied = 0;
983a246b010SChuck Lever 		}
984a246b010SChuck Lever 	}
985a246b010SChuck Lever }
986a246b010SChuck Lever 
987dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
988a246b010SChuck Lever {
989a246b010SChuck Lever 	size_t len, used;
990a246b010SChuck Lever 	char *p;
991a246b010SChuck Lever 
99251971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
993a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
99451971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9959d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
99651971139SChuck Lever 	transport->tcp_offset += used;
997a246b010SChuck Lever 	if (used != len)
998a246b010SChuck Lever 		return;
999e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1000*18dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
100151971139SChuck Lever 	transport->tcp_copied = 4;
1002*18dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
1003*18dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1004*18dca02aSRicardo Labiaga 							      : "request with",
100551971139SChuck Lever 			ntohl(transport->tcp_xid));
100651971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1007a246b010SChuck Lever }
1008a246b010SChuck Lever 
1009*18dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1010*18dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
1011*18dca02aSRicardo Labiaga {
1012*18dca02aSRicardo Labiaga 	size_t len, used;
1013*18dca02aSRicardo Labiaga 	u32 offset;
1014*18dca02aSRicardo Labiaga 	__be32	calldir;
1015*18dca02aSRicardo Labiaga 
1016*18dca02aSRicardo Labiaga 	/*
1017*18dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
1018*18dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1019*18dca02aSRicardo Labiaga 	 * When this function is called for the first time,
1020*18dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
1021*18dca02aSRicardo Labiaga 	 */
1022*18dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1023*18dca02aSRicardo Labiaga 	len = sizeof(calldir) - offset;
1024*18dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
1025*18dca02aSRicardo Labiaga 	used = xdr_skb_read_bits(desc, &calldir, len);
1026*18dca02aSRicardo Labiaga 	transport->tcp_offset += used;
1027*18dca02aSRicardo Labiaga 	if (used != len)
1028*18dca02aSRicardo Labiaga 		return;
1029*18dca02aSRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1030*18dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
1031*18dca02aSRicardo Labiaga 	transport->tcp_copied += 4;
1032*18dca02aSRicardo Labiaga 	if (ntohl(calldir) == RPC_REPLY)
1033*18dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
1034*18dca02aSRicardo Labiaga 	else
1035*18dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
1036*18dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
1037*18dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ?
1038*18dca02aSRicardo Labiaga 				"reply for" : "request with", calldir);
1039*18dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
1040*18dca02aSRicardo Labiaga }
1041*18dca02aSRicardo Labiaga 
1042dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1043a246b010SChuck Lever {
104451971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1045a246b010SChuck Lever 	struct rpc_rqst *req;
1046a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
1047a246b010SChuck Lever 	size_t len;
1048a246b010SChuck Lever 	ssize_t r;
1049a246b010SChuck Lever 
1050a246b010SChuck Lever 	/* Find and lock the request corresponding to this xid */
10514a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
105251971139SChuck Lever 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1053a246b010SChuck Lever 	if (!req) {
1054e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1055a246b010SChuck Lever 		dprintk("RPC:       XID %08x request not found!\n",
105651971139SChuck Lever 				ntohl(transport->tcp_xid));
10574a0f8c04SChuck Lever 		spin_unlock(&xprt->transport_lock);
1058a246b010SChuck Lever 		return;
1059a246b010SChuck Lever 	}
1060a246b010SChuck Lever 
1061a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1062a246b010SChuck Lever 	len = desc->count;
106351971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1064dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1065a246b010SChuck Lever 
106651971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1067a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1068a246b010SChuck Lever 		my_desc.count = len;
106951971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10709d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1071a246b010SChuck Lever 		desc->count -= r;
1072a246b010SChuck Lever 		desc->offset += r;
1073a246b010SChuck Lever 	} else
107451971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10759d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1076a246b010SChuck Lever 
1077a246b010SChuck Lever 	if (r > 0) {
107851971139SChuck Lever 		transport->tcp_copied += r;
107951971139SChuck Lever 		transport->tcp_offset += r;
1080a246b010SChuck Lever 	}
1081a246b010SChuck Lever 	if (r != len) {
1082a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1083a246b010SChuck Lever 		 * usually because we weren't able to allocate
1084a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1085e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1086a246b010SChuck Lever 		 * will not receive any additional updates,
1087a246b010SChuck Lever 		 * and time out.
1088a246b010SChuck Lever 		 * Any remaining data from this record will
1089a246b010SChuck Lever 		 * be discarded.
1090a246b010SChuck Lever 		 */
1091e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1092a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
109351971139SChuck Lever 				ntohl(transport->tcp_xid));
109446121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
109546121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
109646121cf7SChuck Lever 				xprt, transport->tcp_copied,
109746121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
1098a246b010SChuck Lever 		goto out;
1099a246b010SChuck Lever 	}
1100a246b010SChuck Lever 
1101a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
110251971139SChuck Lever 			ntohl(transport->tcp_xid), r);
110346121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
110446121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
110546121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1106a246b010SChuck Lever 
110751971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1108e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
110951971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1110e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1111e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1112a246b010SChuck Lever 	}
1113a246b010SChuck Lever 
1114a246b010SChuck Lever out:
1115e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
111651971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
11174a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
111851971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1119a246b010SChuck Lever }
1120a246b010SChuck Lever 
1121dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1122a246b010SChuck Lever {
1123a246b010SChuck Lever 	size_t len;
1124a246b010SChuck Lever 
112551971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1126a246b010SChuck Lever 	if (len > desc->count)
1127a246b010SChuck Lever 		len = desc->count;
1128a246b010SChuck Lever 	desc->count -= len;
1129a246b010SChuck Lever 	desc->offset += len;
113051971139SChuck Lever 	transport->tcp_offset += len;
1131a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
113251971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1133a246b010SChuck Lever }
1134a246b010SChuck Lever 
11359903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1136a246b010SChuck Lever {
1137a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
113851971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1139dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1140a246b010SChuck Lever 		.skb	= skb,
1141a246b010SChuck Lever 		.offset	= offset,
1142a246b010SChuck Lever 		.count	= len,
1143a246b010SChuck Lever 	};
1144a246b010SChuck Lever 
11459903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1146a246b010SChuck Lever 	do {
1147a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1148a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1149e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
11509903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1151a246b010SChuck Lever 			continue;
1152a246b010SChuck Lever 		}
1153a246b010SChuck Lever 		/* Read in the xid if necessary */
1154e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
115551971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1156a246b010SChuck Lever 			continue;
1157a246b010SChuck Lever 		}
1158*18dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1159*18dca02aSRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1160*18dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
1161*18dca02aSRicardo Labiaga 			continue;
1162*18dca02aSRicardo Labiaga 		}
1163a246b010SChuck Lever 		/* Read in the request data */
1164e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
11659903cd1cSChuck Lever 			xs_tcp_read_request(xprt, &desc);
1166a246b010SChuck Lever 			continue;
1167a246b010SChuck Lever 		}
1168a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
116951971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1170a246b010SChuck Lever 	} while (desc.count);
11719903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1172a246b010SChuck Lever 	return len - desc.count;
1173a246b010SChuck Lever }
1174a246b010SChuck Lever 
11759903cd1cSChuck Lever /**
11769903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
11779903cd1cSChuck Lever  * @sk: socket with data to read
11789903cd1cSChuck Lever  * @bytes: how much data to read
11799903cd1cSChuck Lever  *
11809903cd1cSChuck Lever  */
11819903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1182a246b010SChuck Lever {
1183a246b010SChuck Lever 	struct rpc_xprt *xprt;
1184a246b010SChuck Lever 	read_descriptor_t rd_desc;
1185ff2d7db8STrond Myklebust 	int read;
1186a246b010SChuck Lever 
11879903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
118846121cf7SChuck Lever 
118946121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
11909903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1191a246b010SChuck Lever 		goto out;
1192a246b010SChuck Lever 	if (xprt->shutdown)
1193a246b010SChuck Lever 		goto out;
1194a246b010SChuck Lever 
11959903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1196a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1197ff2d7db8STrond Myklebust 	do {
1198a246b010SChuck Lever 		rd_desc.count = 65536;
1199ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1200ff2d7db8STrond Myklebust 	} while (read > 0);
1201a246b010SChuck Lever out:
1202a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1203a246b010SChuck Lever }
1204a246b010SChuck Lever 
12057d1e8255STrond Myklebust /*
12067d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12077d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12087d1e8255STrond Myklebust  * fashion
12097d1e8255STrond Myklebust  */
12107d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12117d1e8255STrond Myklebust 		unsigned long timeout)
12127d1e8255STrond Myklebust {
12137d1e8255STrond Myklebust 	struct sock_xprt *transport;
12147d1e8255STrond Myklebust 
12157d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12167d1e8255STrond Myklebust 		return;
12177d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12187d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12197d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12207d1e8255STrond Myklebust 			   timeout);
12217d1e8255STrond Myklebust }
12227d1e8255STrond Myklebust 
12237d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12247d1e8255STrond Myklebust {
12257d1e8255STrond Myklebust 	struct sock_xprt *transport;
12267d1e8255STrond Myklebust 
12277d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12287d1e8255STrond Myklebust 
12297d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12307d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12317d1e8255STrond Myklebust 		return;
12327d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12337d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
12347d1e8255STrond Myklebust }
12357d1e8255STrond Myklebust 
12367d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
12377d1e8255STrond Myklebust {
12387d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
12397d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12407d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
12417d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
12427d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
12437d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
12447d1e8255STrond Myklebust }
12457d1e8255STrond Myklebust 
12469903cd1cSChuck Lever /**
12479903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
12489903cd1cSChuck Lever  * @sk: socket whose state has changed
12499903cd1cSChuck Lever  *
12509903cd1cSChuck Lever  */
12519903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1252a246b010SChuck Lever {
1253a246b010SChuck Lever 	struct rpc_xprt *xprt;
1254a246b010SChuck Lever 
1255a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1256a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1257a246b010SChuck Lever 		goto out;
12589903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1259a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1260a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1261a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1262a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1263a246b010SChuck Lever 
1264a246b010SChuck Lever 	switch (sk->sk_state) {
1265a246b010SChuck Lever 	case TCP_ESTABLISHED:
12664a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1267a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
126851971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
126951971139SChuck Lever 					struct sock_xprt, xprt);
127051971139SChuck Lever 
1271a246b010SChuck Lever 			/* Reset TCP record info */
127251971139SChuck Lever 			transport->tcp_offset = 0;
127351971139SChuck Lever 			transport->tcp_reclen = 0;
127451971139SChuck Lever 			transport->tcp_copied = 0;
1275e136d092SChuck Lever 			transport->tcp_flags =
1276e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
127751971139SChuck Lever 
12782a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1279a246b010SChuck Lever 		}
12804a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1281a246b010SChuck Lever 		break;
12823b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
12833b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
12847c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1285663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
12863b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
12873b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
12883b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1289ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12903b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
129125fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1292a246b010SChuck Lever 		break;
1293632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
12943b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
129566af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1296663b8858STrond Myklebust 	case TCP_SYN_SENT:
12977c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1298663b8858STrond Myklebust 	case TCP_CLOSING:
1299663b8858STrond Myklebust 		/*
1300663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1301663b8858STrond Myklebust 		 * we back off before reconnecting
1302663b8858STrond Myklebust 		 */
1303663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1304663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13053b948ae5STrond Myklebust 		break;
13063b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1307670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
130825fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13093b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13103b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13113b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13123b948ae5STrond Myklebust 		break;
13133b948ae5STrond Myklebust 	case TCP_CLOSE:
13147d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13157d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1316a246b010SChuck Lever 	}
1317a246b010SChuck Lever  out:
1318a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1319a246b010SChuck Lever }
1320a246b010SChuck Lever 
13219903cd1cSChuck Lever /**
1322482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13232a9e1cfaSTrond Myklebust  * @sk: socket
13242a9e1cfaSTrond Myklebust  */
1325482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13262a9e1cfaSTrond Myklebust {
13272a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13282a9e1cfaSTrond Myklebust 
13292a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
13302a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13312a9e1cfaSTrond Myklebust 		goto out;
13322a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
13332a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
13342a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1335482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
13362a9e1cfaSTrond Myklebust out:
13372a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
13382a9e1cfaSTrond Myklebust }
13392a9e1cfaSTrond Myklebust 
13401f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
13411f0fa154SIlpo Järvinen {
13421f0fa154SIlpo Järvinen 	struct socket *sock;
13431f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
13441f0fa154SIlpo Järvinen 
13451f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
13461f0fa154SIlpo Järvinen 		return;
13471f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
13481f0fa154SIlpo Järvinen 
13491f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
13501f0fa154SIlpo Järvinen 		return;
13511f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
13521f0fa154SIlpo Järvinen 		return;
13531f0fa154SIlpo Järvinen 
13541f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
13551f0fa154SIlpo Järvinen }
13561f0fa154SIlpo Järvinen 
13572a9e1cfaSTrond Myklebust /**
1358c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1359c7b2cae8SChuck Lever  *                             becomes available
13609903cd1cSChuck Lever  * @sk: socket whose state has changed
13619903cd1cSChuck Lever  *
1362a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1363a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1364c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1365a246b010SChuck Lever  * with a bunch of small requests.
1366a246b010SChuck Lever  */
1367c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1368a246b010SChuck Lever {
1369a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1370c7b2cae8SChuck Lever 
1371c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
13721f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
13731f0fa154SIlpo Järvinen 		xs_write_space(sk);
1374c7b2cae8SChuck Lever 
1375c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1376c7b2cae8SChuck Lever }
1377c7b2cae8SChuck Lever 
1378c7b2cae8SChuck Lever /**
1379c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1380c7b2cae8SChuck Lever  *                             becomes available
1381c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1382c7b2cae8SChuck Lever  *
1383c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1384c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1385c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1386c7b2cae8SChuck Lever  * with a bunch of small requests.
1387c7b2cae8SChuck Lever  */
1388c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1389c7b2cae8SChuck Lever {
1390c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1391c7b2cae8SChuck Lever 
1392c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
13931f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
13941f0fa154SIlpo Järvinen 		xs_write_space(sk);
1395c7b2cae8SChuck Lever 
1396a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1397a246b010SChuck Lever }
1398a246b010SChuck Lever 
1399470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1400a246b010SChuck Lever {
1401ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1402ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1403a246b010SChuck Lever 
14047c6e066eSChuck Lever 	if (transport->rcvsize) {
1405a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14067c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1407a246b010SChuck Lever 	}
14087c6e066eSChuck Lever 	if (transport->sndsize) {
1409a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14107c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1411a246b010SChuck Lever 		sk->sk_write_space(sk);
1412a246b010SChuck Lever 	}
1413a246b010SChuck Lever }
1414a246b010SChuck Lever 
141543118c29SChuck Lever /**
1416470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
141743118c29SChuck Lever  * @xprt: generic transport
1418470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1419470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
142043118c29SChuck Lever  *
1421470056c2SChuck Lever  * Set socket send and receive buffer size limits.
142243118c29SChuck Lever  */
1423470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
142443118c29SChuck Lever {
14257c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14267c6e066eSChuck Lever 
14277c6e066eSChuck Lever 	transport->sndsize = 0;
1428470056c2SChuck Lever 	if (sndsize)
14297c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14307c6e066eSChuck Lever 	transport->rcvsize = 0;
1431470056c2SChuck Lever 	if (rcvsize)
14327c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1433470056c2SChuck Lever 
1434470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
143543118c29SChuck Lever }
143643118c29SChuck Lever 
143746c0ee8bSChuck Lever /**
143846c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
143946c0ee8bSChuck Lever  * @task: task that timed out
144046c0ee8bSChuck Lever  *
144146c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
144246c0ee8bSChuck Lever  */
144346c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
144446c0ee8bSChuck Lever {
144546c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
144646c0ee8bSChuck Lever }
144746c0ee8bSChuck Lever 
1448b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1449b85d8806SChuck Lever {
1450b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1451b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1452b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1453b85d8806SChuck Lever }
1454b85d8806SChuck Lever 
145592200412SChuck Lever /**
145692200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
145792200412SChuck Lever  * @xprt: generic transport
145892200412SChuck Lever  * @port: new port number
145992200412SChuck Lever  *
146092200412SChuck Lever  */
146192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
146292200412SChuck Lever {
146395392c59SChuck Lever 	struct sockaddr *addr = xs_addr(xprt);
1464c4efcb1dSChuck Lever 
146592200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1466c4efcb1dSChuck Lever 
146720612005SChuck Lever 	switch (addr->sa_family) {
146820612005SChuck Lever 	case AF_INET:
146920612005SChuck Lever 		((struct sockaddr_in *)addr)->sin_port = htons(port);
147020612005SChuck Lever 		break;
147120612005SChuck Lever 	case AF_INET6:
147220612005SChuck Lever 		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
147320612005SChuck Lever 		break;
147420612005SChuck Lever 	default:
147520612005SChuck Lever 		BUG();
147620612005SChuck Lever 	}
147792200412SChuck Lever }
147892200412SChuck Lever 
147967a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
148067a391d7STrond Myklebust {
148167a391d7STrond Myklebust 	unsigned short port = transport->port;
148267a391d7STrond Myklebust 
148367a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
148467a391d7STrond Myklebust 		port = xs_get_random_port();
148567a391d7STrond Myklebust 	return port;
148667a391d7STrond Myklebust }
148767a391d7STrond Myklebust 
148867a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
148967a391d7STrond Myklebust {
149067a391d7STrond Myklebust 	if (transport->port != 0)
149167a391d7STrond Myklebust 		transport->port = 0;
149267a391d7STrond Myklebust 	if (!transport->xprt.resvport)
149367a391d7STrond Myklebust 		return 0;
149467a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
149567a391d7STrond Myklebust 		return xprt_max_resvport;
149667a391d7STrond Myklebust 	return --port;
149767a391d7STrond Myklebust }
149867a391d7STrond Myklebust 
14997dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1500a246b010SChuck Lever {
1501a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1502a246b010SChuck Lever 		.sin_family = AF_INET,
1503a246b010SChuck Lever 	};
1504d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
150567a391d7STrond Myklebust 	int err, nloop = 0;
150667a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
150767a391d7STrond Myklebust 	unsigned short last;
1508a246b010SChuck Lever 
1509d3bc9a1dSFrank van Maarseveen 	sa = (struct sockaddr_in *)&transport->addr;
1510d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1511a246b010SChuck Lever 	do {
1512a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1513e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1514a246b010SChuck Lever 						sizeof(myaddr));
151567a391d7STrond Myklebust 		if (port == 0)
1516d3bc9a1dSFrank van Maarseveen 			break;
1517a246b010SChuck Lever 		if (err == 0) {
1518c8475461SChuck Lever 			transport->port = port;
1519d3bc9a1dSFrank van Maarseveen 			break;
1520a246b010SChuck Lever 		}
152167a391d7STrond Myklebust 		last = port;
152267a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
152367a391d7STrond Myklebust 		if (port > last)
152467a391d7STrond Myklebust 			nloop++;
152567a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
152621454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
152721454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15287dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1529a246b010SChuck Lever 	return err;
1530a246b010SChuck Lever }
1531a246b010SChuck Lever 
153290058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
153390058d37SChuck Lever {
153490058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
153590058d37SChuck Lever 		.sin6_family = AF_INET6,
153690058d37SChuck Lever 	};
153790058d37SChuck Lever 	struct sockaddr_in6 *sa;
153867a391d7STrond Myklebust 	int err, nloop = 0;
153967a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
154067a391d7STrond Myklebust 	unsigned short last;
154190058d37SChuck Lever 
154290058d37SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->addr;
154390058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
154490058d37SChuck Lever 	do {
154590058d37SChuck Lever 		myaddr.sin6_port = htons(port);
154690058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
154790058d37SChuck Lever 						sizeof(myaddr));
154867a391d7STrond Myklebust 		if (port == 0)
154990058d37SChuck Lever 			break;
155090058d37SChuck Lever 		if (err == 0) {
155190058d37SChuck Lever 			transport->port = port;
155290058d37SChuck Lever 			break;
155390058d37SChuck Lever 		}
155467a391d7STrond Myklebust 		last = port;
155567a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
155667a391d7STrond Myklebust 		if (port > last)
155767a391d7STrond Myklebust 			nloop++;
155867a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
15595b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1560fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1561a246b010SChuck Lever 	return err;
1562a246b010SChuck Lever }
1563a246b010SChuck Lever 
1564ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1565ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1566ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1567ed07536eSPeter Zijlstra 
15688945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1569ed07536eSPeter Zijlstra {
1570ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
15718945ee5eSChuck Lever 
157202b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
15738945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
15748945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1575ed07536eSPeter Zijlstra }
15768945ee5eSChuck Lever 
15778945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
15788945ee5eSChuck Lever {
15798945ee5eSChuck Lever 	struct sock *sk = sock->sk;
15808945ee5eSChuck Lever 
1581f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
15828945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
15838945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1584ed07536eSPeter Zijlstra }
1585ed07536eSPeter Zijlstra #else
15868945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
15878945ee5eSChuck Lever {
15888945ee5eSChuck Lever }
15898945ee5eSChuck Lever 
15908945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1591ed07536eSPeter Zijlstra {
1592ed07536eSPeter Zijlstra }
1593ed07536eSPeter Zijlstra #endif
1594ed07536eSPeter Zijlstra 
159516be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1596a246b010SChuck Lever {
159716be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1598edb267a6SChuck Lever 
1599ee0ac0c2SChuck Lever 	if (!transport->inet) {
1600b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1601b0d93ad5SChuck Lever 
1602b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1603b0d93ad5SChuck Lever 
16042a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16052a9e1cfaSTrond Myklebust 
1606b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1607b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1608b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1609482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1610b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1611b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1612b0d93ad5SChuck Lever 
1613b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1614b0d93ad5SChuck Lever 
1615b0d93ad5SChuck Lever 		/* Reset to new socket */
1616ee0ac0c2SChuck Lever 		transport->sock = sock;
1617ee0ac0c2SChuck Lever 		transport->inet = sk;
1618b0d93ad5SChuck Lever 
1619b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1620b0d93ad5SChuck Lever 	}
1621470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
162216be2d20SChuck Lever }
162316be2d20SChuck Lever 
1624a246b010SChuck Lever /**
16259c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1626a246b010SChuck Lever  * @work: RPC transport to connect
1627a246b010SChuck Lever  *
1628a246b010SChuck Lever  * Invoked by a work queue tasklet.
1629a246b010SChuck Lever  */
16309c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1631a246b010SChuck Lever {
1632a246b010SChuck Lever 	struct sock_xprt *transport =
1633a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1634a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1635a246b010SChuck Lever 	struct socket *sock = transport->sock;
1636a246b010SChuck Lever 	int err, status = -EIO;
1637a246b010SChuck Lever 
163801d37c42STrond Myklebust 	if (xprt->shutdown)
16399903cd1cSChuck Lever 		goto out;
16409903cd1cSChuck Lever 
1641a246b010SChuck Lever 	/* Start by resetting any existing state */
1642fe315e76SChuck Lever 	xs_reset_transport(transport);
1643a246b010SChuck Lever 
1644fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1645fe315e76SChuck Lever 	if (err < 0) {
16469903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1647a246b010SChuck Lever 		goto out;
1648a246b010SChuck Lever 	}
16498945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1650a246b010SChuck Lever 
16517dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
16529903cd1cSChuck Lever 		sock_release(sock);
16539903cd1cSChuck Lever 		goto out;
1654a246b010SChuck Lever 	}
1655b0d93ad5SChuck Lever 
1656b0d93ad5SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1657b0d93ad5SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1658b0d93ad5SChuck Lever 
165916be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1660a246b010SChuck Lever 	status = 0;
1661b0d93ad5SChuck Lever out:
1662b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
16637d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1664b0d93ad5SChuck Lever }
1665b0d93ad5SChuck Lever 
166668e220bdSChuck Lever /**
166768e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
166868e220bdSChuck Lever  * @work: RPC transport to connect
166968e220bdSChuck Lever  *
167068e220bdSChuck Lever  * Invoked by a work queue tasklet.
167168e220bdSChuck Lever  */
167268e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
167368e220bdSChuck Lever {
167468e220bdSChuck Lever 	struct sock_xprt *transport =
167568e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
167668e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
167768e220bdSChuck Lever 	struct socket *sock = transport->sock;
167868e220bdSChuck Lever 	int err, status = -EIO;
167968e220bdSChuck Lever 
168001d37c42STrond Myklebust 	if (xprt->shutdown)
168168e220bdSChuck Lever 		goto out;
168268e220bdSChuck Lever 
168368e220bdSChuck Lever 	/* Start by resetting any existing state */
1684fe315e76SChuck Lever 	xs_reset_transport(transport);
168568e220bdSChuck Lever 
1686fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1687fe315e76SChuck Lever 	if (err < 0) {
168868e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
168968e220bdSChuck Lever 		goto out;
169068e220bdSChuck Lever 	}
16918945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
169268e220bdSChuck Lever 
169368e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
169468e220bdSChuck Lever 		sock_release(sock);
169568e220bdSChuck Lever 		goto out;
169668e220bdSChuck Lever 	}
169768e220bdSChuck Lever 
169868e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
169968e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
170068e220bdSChuck Lever 
170168e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1702a246b010SChuck Lever 	status = 0;
1703b0d93ad5SChuck Lever out:
1704b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17057d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1706b0d93ad5SChuck Lever }
1707b0d93ad5SChuck Lever 
17083167e12cSChuck Lever /*
17093167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17103167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17113167e12cSChuck Lever  */
171240d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17133167e12cSChuck Lever {
17143167e12cSChuck Lever 	int result;
17153167e12cSChuck Lever 	struct sockaddr any;
17163167e12cSChuck Lever 
17173167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17183167e12cSChuck Lever 
17193167e12cSChuck Lever 	/*
17203167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17213167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17223167e12cSChuck Lever 	 */
17233167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17243167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1725ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17267d1e8255STrond Myklebust 	if (!result)
17277d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17287d1e8255STrond Myklebust 	else
17293167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17303167e12cSChuck Lever 				result);
17313167e12cSChuck Lever }
17323167e12cSChuck Lever 
173340d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
173440d2549dSTrond Myklebust {
173540d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
173640d2549dSTrond Myklebust 
173740d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
173840d2549dSTrond Myklebust 		return;
173940d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
174040d2549dSTrond Myklebust 		return;
174140d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
174240d2549dSTrond Myklebust }
174340d2549dSTrond Myklebust 
174416be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1745b0d93ad5SChuck Lever {
174616be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1747edb267a6SChuck Lever 
1748ee0ac0c2SChuck Lever 	if (!transport->inet) {
1749b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1750b0d93ad5SChuck Lever 
1751b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1752b0d93ad5SChuck Lever 
17532a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
17542a9e1cfaSTrond Myklebust 
1755b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1756b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1757b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1758b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1759482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1760b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
17613167e12cSChuck Lever 
17623167e12cSChuck Lever 		/* socket options */
17633167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
17643167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
17653167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
17663167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1767b0d93ad5SChuck Lever 
1768b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1769b0d93ad5SChuck Lever 
1770b0d93ad5SChuck Lever 		/* Reset to new socket */
1771ee0ac0c2SChuck Lever 		transport->sock = sock;
1772ee0ac0c2SChuck Lever 		transport->inet = sk;
1773b0d93ad5SChuck Lever 
1774b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1775b0d93ad5SChuck Lever 	}
1776b0d93ad5SChuck Lever 
177701d37c42STrond Myklebust 	if (!xprt_bound(xprt))
177801d37c42STrond Myklebust 		return -ENOTCONN;
177901d37c42STrond Myklebust 
1780b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1781262ca07dSChuck Lever 	xprt->stat.connect_count++;
1782262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
178395392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
178416be2d20SChuck Lever }
178516be2d20SChuck Lever 
178616be2d20SChuck Lever /**
1787b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1788b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1789b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1790b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
179116be2d20SChuck Lever  *
179216be2d20SChuck Lever  * Invoked by a work queue tasklet.
179316be2d20SChuck Lever  */
1794b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1795b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1796b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1797b61d59ffSTrond Myklebust 			struct sock_xprt *))
179816be2d20SChuck Lever {
179916be2d20SChuck Lever 	struct socket *sock = transport->sock;
1800b61d59ffSTrond Myklebust 	int status = -EIO;
180116be2d20SChuck Lever 
180201d37c42STrond Myklebust 	if (xprt->shutdown)
180316be2d20SChuck Lever 		goto out;
180416be2d20SChuck Lever 
180516be2d20SChuck Lever 	if (!sock) {
18067d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1807b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1808b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1809b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
181016be2d20SChuck Lever 			goto out;
181116be2d20SChuck Lever 		}
18127d1e8255STrond Myklebust 	} else {
18137d1e8255STrond Myklebust 		int abort_and_exit;
18147d1e8255STrond Myklebust 
18157d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18167d1e8255STrond Myklebust 				&xprt->state);
181716be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
181840d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
181916be2d20SChuck Lever 
18207d1e8255STrond Myklebust 		if (abort_and_exit)
18217d1e8255STrond Myklebust 			goto out_eagain;
18227d1e8255STrond Myklebust 	}
18237d1e8255STrond Myklebust 
182416be2d20SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
182516be2d20SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
182616be2d20SChuck Lever 
182716be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1828a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
182946121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
183046121cf7SChuck Lever 			sock->sk->sk_state);
1831a246b010SChuck Lever 	switch (status) {
1832f75e6745STrond Myklebust 	default:
1833f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1834f75e6745STrond Myklebust 			__func__, status);
1835f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1836f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1837f75e6745STrond Myklebust 		 * and retry
1838f75e6745STrond Myklebust 		 */
1839f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1840f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
18418a2cec29STrond Myklebust 	case -ECONNREFUSED:
18428a2cec29STrond Myklebust 	case -ECONNRESET:
18438a2cec29STrond Myklebust 	case -ENETUNREACH:
18448a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
18452a491991STrond Myklebust 	case 0:
1846a246b010SChuck Lever 	case -EINPROGRESS:
1847a246b010SChuck Lever 	case -EALREADY:
18487d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
18497d1e8255STrond Myklebust 		return;
18508a2cec29STrond Myklebust 	}
18517d1e8255STrond Myklebust out_eagain:
18522a491991STrond Myklebust 	status = -EAGAIN;
1853a246b010SChuck Lever out:
18542226feb6SChuck Lever 	xprt_clear_connecting(xprt);
18557d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1856a246b010SChuck Lever }
1857a246b010SChuck Lever 
1858b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1859b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1860b61d59ffSTrond Myklebust {
1861b61d59ffSTrond Myklebust 	struct socket *sock;
1862b61d59ffSTrond Myklebust 	int err;
1863b61d59ffSTrond Myklebust 
1864b61d59ffSTrond Myklebust 	/* start from scratch */
1865b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1866b61d59ffSTrond Myklebust 	if (err < 0) {
1867b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1868b61d59ffSTrond Myklebust 				-err);
1869b61d59ffSTrond Myklebust 		goto out_err;
1870b61d59ffSTrond Myklebust 	}
1871b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1872b61d59ffSTrond Myklebust 
1873b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1874b61d59ffSTrond Myklebust 		sock_release(sock);
1875b61d59ffSTrond Myklebust 		goto out_err;
1876b61d59ffSTrond Myklebust 	}
1877b61d59ffSTrond Myklebust 	return sock;
1878b61d59ffSTrond Myklebust out_err:
1879b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1880b61d59ffSTrond Myklebust }
1881b61d59ffSTrond Myklebust 
1882b61d59ffSTrond Myklebust /**
1883a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1884a246b010SChuck Lever  * @work: RPC transport to connect
1885a246b010SChuck Lever  *
1886a246b010SChuck Lever  * Invoked by a work queue tasklet.
1887a246b010SChuck Lever  */
1888a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1889a246b010SChuck Lever {
1890a246b010SChuck Lever 	struct sock_xprt *transport =
1891a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1892a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1893a246b010SChuck Lever 
1894b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1895b61d59ffSTrond Myklebust }
1896a246b010SChuck Lever 
1897b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1898b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1899b61d59ffSTrond Myklebust {
1900b61d59ffSTrond Myklebust 	struct socket *sock;
1901b61d59ffSTrond Myklebust 	int err;
1902b61d59ffSTrond Myklebust 
1903a246b010SChuck Lever 	/* start from scratch */
1904b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1905b61d59ffSTrond Myklebust 	if (err < 0) {
1906b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1907b61d59ffSTrond Myklebust 				-err);
1908b61d59ffSTrond Myklebust 		goto out_err;
19099903cd1cSChuck Lever 	}
1910b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19119903cd1cSChuck Lever 
1912b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1913a246b010SChuck Lever 		sock_release(sock);
1914b61d59ffSTrond Myklebust 		goto out_err;
1915a246b010SChuck Lever 	}
1916b61d59ffSTrond Myklebust 	return sock;
1917b61d59ffSTrond Myklebust out_err:
1918b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1919a246b010SChuck Lever }
1920a246b010SChuck Lever 
1921a246b010SChuck Lever /**
192268e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
192368e220bdSChuck Lever  * @work: RPC transport to connect
192468e220bdSChuck Lever  *
192568e220bdSChuck Lever  * Invoked by a work queue tasklet.
192668e220bdSChuck Lever  */
192768e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
192868e220bdSChuck Lever {
192968e220bdSChuck Lever 	struct sock_xprt *transport =
193068e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
193168e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
193268e220bdSChuck Lever 
1933b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
193468e220bdSChuck Lever }
193568e220bdSChuck Lever 
193668e220bdSChuck Lever /**
1937a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
1938a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
1939a246b010SChuck Lever  *
1940a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
194103bf4b70SChuck Lever  *
194203bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
194303bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
194403bf4b70SChuck Lever  * socket on a privileged port.
194503bf4b70SChuck Lever  *
194603bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
194703bf4b70SChuck Lever  * retry floods (hard mounts).
1948a246b010SChuck Lever  */
1949a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
1950a246b010SChuck Lever {
1951a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
1952ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1953a246b010SChuck Lever 
1954b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
1955b0d93ad5SChuck Lever 		return;
1956b0d93ad5SChuck Lever 
1957ee0ac0c2SChuck Lever 	if (transport->sock != NULL) {
195846121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
195946121cf7SChuck Lever 				"seconds\n",
196003bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
1961c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1962c1384c9cSTrond Myklebust 				   &transport->connect_worker,
196303bf4b70SChuck Lever 				   xprt->reestablish_timeout);
196403bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
196503bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
196603bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
19679903cd1cSChuck Lever 	} else {
19689903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
1969c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1970c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
1971a246b010SChuck Lever 	}
1972a246b010SChuck Lever }
1973a246b010SChuck Lever 
1974e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
1975e06799f9STrond Myklebust {
1976e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1977e06799f9STrond Myklebust 
1978e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
1979e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
1980e06799f9STrond Myklebust 		return;
1981e06799f9STrond Myklebust 	xs_connect(task);
1982e06799f9STrond Myklebust }
1983e06799f9STrond Myklebust 
1984262ca07dSChuck Lever /**
1985262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
1986262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
1987262ca07dSChuck Lever  * @seq: output file
1988262ca07dSChuck Lever  *
1989262ca07dSChuck Lever  */
1990262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1991262ca07dSChuck Lever {
1992c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1993c8475461SChuck Lever 
1994262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1995c8475461SChuck Lever 			transport->port,
1996262ca07dSChuck Lever 			xprt->stat.bind_count,
1997262ca07dSChuck Lever 			xprt->stat.sends,
1998262ca07dSChuck Lever 			xprt->stat.recvs,
1999262ca07dSChuck Lever 			xprt->stat.bad_xids,
2000262ca07dSChuck Lever 			xprt->stat.req_u,
2001262ca07dSChuck Lever 			xprt->stat.bklog_u);
2002262ca07dSChuck Lever }
2003262ca07dSChuck Lever 
2004262ca07dSChuck Lever /**
2005262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2006262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2007262ca07dSChuck Lever  * @seq: output file
2008262ca07dSChuck Lever  *
2009262ca07dSChuck Lever  */
2010262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2011262ca07dSChuck Lever {
2012c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2013262ca07dSChuck Lever 	long idle_time = 0;
2014262ca07dSChuck Lever 
2015262ca07dSChuck Lever 	if (xprt_connected(xprt))
2016262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2017262ca07dSChuck Lever 
2018262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2019c8475461SChuck Lever 			transport->port,
2020262ca07dSChuck Lever 			xprt->stat.bind_count,
2021262ca07dSChuck Lever 			xprt->stat.connect_count,
2022262ca07dSChuck Lever 			xprt->stat.connect_time,
2023262ca07dSChuck Lever 			idle_time,
2024262ca07dSChuck Lever 			xprt->stat.sends,
2025262ca07dSChuck Lever 			xprt->stat.recvs,
2026262ca07dSChuck Lever 			xprt->stat.bad_xids,
2027262ca07dSChuck Lever 			xprt->stat.req_u,
2028262ca07dSChuck Lever 			xprt->stat.bklog_u);
2029262ca07dSChuck Lever }
2030262ca07dSChuck Lever 
2031262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
203243118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
203312a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
203449e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
203545160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
203692200412SChuck Lever 	.set_port		= xs_set_port,
20379903cd1cSChuck Lever 	.connect		= xs_connect,
203802107148SChuck Lever 	.buf_alloc		= rpc_malloc,
203902107148SChuck Lever 	.buf_free		= rpc_free,
2040262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2041fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
204246c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2043a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2044262965f5SChuck Lever 	.close			= xs_close,
2045262965f5SChuck Lever 	.destroy		= xs_destroy,
2046262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2047262965f5SChuck Lever };
2048262965f5SChuck Lever 
2049262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
205012a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2051e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
205245160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
205392200412SChuck Lever 	.set_port		= xs_set_port,
2054e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
205502107148SChuck Lever 	.buf_alloc		= rpc_malloc,
205602107148SChuck Lever 	.buf_free		= rpc_free,
2057262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2058fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2059f75e6745STrond Myklebust 	.close			= xs_tcp_close,
20609903cd1cSChuck Lever 	.destroy		= xs_destroy,
2061262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2062a246b010SChuck Lever };
2063a246b010SChuck Lever 
20643c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2065bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2066c8541ecdSChuck Lever {
2067c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2068ffc2e518SChuck Lever 	struct sock_xprt *new;
2069c8541ecdSChuck Lever 
207096802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2071c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2072c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2073c8541ecdSChuck Lever 	}
2074c8541ecdSChuck Lever 
2075ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2076ffc2e518SChuck Lever 	if (new == NULL) {
207746121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
207846121cf7SChuck Lever 				"rpc_xprt\n");
2079c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2080c8541ecdSChuck Lever 	}
2081ffc2e518SChuck Lever 	xprt = &new->xprt;
2082c8541ecdSChuck Lever 
2083c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2084c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2085c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2086c8541ecdSChuck Lever 		kfree(xprt);
208746121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
208846121cf7SChuck Lever 				"table\n");
2089c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2090c8541ecdSChuck Lever 	}
2091c8541ecdSChuck Lever 
209296802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
209396802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2094d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2095d3bc9a1dSFrank van Maarseveen 		memcpy(&new->addr, args->srcaddr, args->addrlen);
2096c8541ecdSChuck Lever 
2097c8541ecdSChuck Lever 	return xprt;
2098c8541ecdSChuck Lever }
2099c8541ecdSChuck Lever 
21002881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
21012881ae74STrond Myklebust 	.to_initval = 5 * HZ,
21022881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
21032881ae74STrond Myklebust 	.to_increment = 5 * HZ,
21042881ae74STrond Myklebust 	.to_retries = 5,
21052881ae74STrond Myklebust };
21062881ae74STrond Myklebust 
21079903cd1cSChuck Lever /**
21089903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
210996802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21109903cd1cSChuck Lever  *
21119903cd1cSChuck Lever  */
2112483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2113a246b010SChuck Lever {
21148f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2115c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2116c8475461SChuck Lever 	struct sock_xprt *transport;
2117a246b010SChuck Lever 
211896802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2119c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2120c8541ecdSChuck Lever 		return xprt;
2121c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2122a246b010SChuck Lever 
2123ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2124808012fbSChuck Lever 	xprt->tsh_size = 0;
2125a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2126a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2127a246b010SChuck Lever 
212803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
212903bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
213003bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
213103bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2132a246b010SChuck Lever 
2133262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2134a246b010SChuck Lever 
2135ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2136a246b010SChuck Lever 
21378f9d5b1aSChuck Lever 	switch (addr->sa_family) {
21388f9d5b1aSChuck Lever 	case AF_INET:
21398f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
21408f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21418f9d5b1aSChuck Lever 
21428f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
21438f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
2144b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
21458f9d5b1aSChuck Lever 		break;
21468f9d5b1aSChuck Lever 	case AF_INET6:
21478f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
21488f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21498f9d5b1aSChuck Lever 
21508f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
21518f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
2152b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
21538f9d5b1aSChuck Lever 		break;
21548f9d5b1aSChuck Lever 	default:
21558f9d5b1aSChuck Lever 		kfree(xprt);
21568f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
21578f9d5b1aSChuck Lever 	}
21588f9d5b1aSChuck Lever 
2159edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
21607559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2161edb267a6SChuck Lever 
2162bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2163c8541ecdSChuck Lever 		return xprt;
2164bc25571eS\"Talpey, Thomas\ 
2165bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2166bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2167bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2168a246b010SChuck Lever }
2169a246b010SChuck Lever 
21702881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
21712881ae74STrond Myklebust 	.to_initval = 60 * HZ,
21722881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
21732881ae74STrond Myklebust 	.to_retries = 2,
21742881ae74STrond Myklebust };
21752881ae74STrond Myklebust 
21769903cd1cSChuck Lever /**
21779903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
217896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21799903cd1cSChuck Lever  *
21809903cd1cSChuck Lever  */
2181483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2182a246b010SChuck Lever {
21838f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2184c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2185c8475461SChuck Lever 	struct sock_xprt *transport;
2186a246b010SChuck Lever 
218796802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2188c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2189c8541ecdSChuck Lever 		return xprt;
2190c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2191a246b010SChuck Lever 
2192ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2193808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2194808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2195a246b010SChuck Lever 
219603bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
219703bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
219803bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
219903bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2200a246b010SChuck Lever 
2201262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2202ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2203a246b010SChuck Lever 
22048f9d5b1aSChuck Lever 	switch (addr->sa_family) {
22058f9d5b1aSChuck Lever 	case AF_INET:
22068f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
22078f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22088f9d5b1aSChuck Lever 
22098f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2210b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
22118f9d5b1aSChuck Lever 		break;
22128f9d5b1aSChuck Lever 	case AF_INET6:
22138f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
22148f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22158f9d5b1aSChuck Lever 
22168f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2217b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
22188f9d5b1aSChuck Lever 		break;
22198f9d5b1aSChuck Lever 	default:
22208f9d5b1aSChuck Lever 		kfree(xprt);
22218f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
22228f9d5b1aSChuck Lever 	}
22238f9d5b1aSChuck Lever 
2224edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
22257559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2226edb267a6SChuck Lever 
2227bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2228c8541ecdSChuck Lever 		return xprt;
2229bc25571eS\"Talpey, Thomas\ 
2230bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2231bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2232bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2233a246b010SChuck Lever }
2234282b32e1SChuck Lever 
2235bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2236bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2237bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2238bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
22394fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_UDP,
2240bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2241bc25571eS\"Talpey, Thomas\ };
2242bc25571eS\"Talpey, Thomas\ 
2243bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2244bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2245bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2246bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
22474fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_TCP,
2248bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2249bc25571eS\"Talpey, Thomas\ };
2250bc25571eS\"Talpey, Thomas\ 
2251282b32e1SChuck Lever /**
2252bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2253282b32e1SChuck Lever  *
2254282b32e1SChuck Lever  */
2255282b32e1SChuck Lever int init_socket_xprt(void)
2256282b32e1SChuck Lever {
2257fbf76683SChuck Lever #ifdef RPC_DEBUG
22582b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
22590b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2260fbf76683SChuck Lever #endif
2261fbf76683SChuck Lever 
2262bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2263bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2264bc25571eS\"Talpey, Thomas\ 
2265282b32e1SChuck Lever 	return 0;
2266282b32e1SChuck Lever }
2267282b32e1SChuck Lever 
2268282b32e1SChuck Lever /**
2269bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2270282b32e1SChuck Lever  *
2271282b32e1SChuck Lever  */
2272282b32e1SChuck Lever void cleanup_socket_xprt(void)
2273282b32e1SChuck Lever {
2274fbf76683SChuck Lever #ifdef RPC_DEBUG
2275fbf76683SChuck Lever 	if (sunrpc_table_header) {
2276fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2277fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2278fbf76683SChuck Lever 	}
2279fbf76683SChuck Lever #endif
2280bc25571eS\"Talpey, Thomas\ 
2281bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2282bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2283282b32e1SChuck Lever }
2284