xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision b61d59ff)
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)
273e136d092SChuck Lever 
27495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
275edb267a6SChuck Lever {
27695392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
27795392c59SChuck Lever }
27895392c59SChuck Lever 
27995392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
28095392c59SChuck Lever {
28195392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
28295392c59SChuck Lever }
28395392c59SChuck Lever 
28495392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
28595392c59SChuck Lever {
28695392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
28795392c59SChuck Lever }
28895392c59SChuck Lever 
289b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
290b454ae90SChuck Lever 					  const char *protocol,
291b454ae90SChuck Lever 					  const char *netid)
292edb267a6SChuck Lever {
29395392c59SChuck Lever 	struct sockaddr_in *addr = xs_addr_in(xprt);
294edb267a6SChuck Lever 	char *buf;
295edb267a6SChuck Lever 
296edb267a6SChuck Lever 	buf = kzalloc(20, GFP_KERNEL);
297edb267a6SChuck Lever 	if (buf) {
298e0db4a78SDavid S. Miller 		snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
299edb267a6SChuck Lever 	}
300edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
301edb267a6SChuck Lever 
302edb267a6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
303edb267a6SChuck Lever 	if (buf) {
304edb267a6SChuck Lever 		snprintf(buf, 8, "%u",
305edb267a6SChuck Lever 				ntohs(addr->sin_port));
306edb267a6SChuck Lever 	}
307edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
308edb267a6SChuck Lever 
309b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
310edb267a6SChuck Lever 
311edb267a6SChuck Lever 	buf = kzalloc(48, GFP_KERNEL);
312edb267a6SChuck Lever 	if (buf) {
31321454aaaSHarvey Harrison 		snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
31421454aaaSHarvey Harrison 			&addr->sin_addr.s_addr,
315edb267a6SChuck Lever 			ntohs(addr->sin_port),
316b454ae90SChuck Lever 			protocol);
317edb267a6SChuck Lever 	}
318edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
319fbfe3cc6SChuck Lever 
320fbfe3cc6SChuck Lever 	buf = kzalloc(10, GFP_KERNEL);
321fbfe3cc6SChuck Lever 	if (buf) {
322fbfe3cc6SChuck Lever 		snprintf(buf, 10, "%02x%02x%02x%02x",
323fbfe3cc6SChuck Lever 				NIPQUAD(addr->sin_addr.s_addr));
324fbfe3cc6SChuck Lever 	}
325fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
326fbfe3cc6SChuck Lever 
327fbfe3cc6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
328fbfe3cc6SChuck Lever 	if (buf) {
329fbfe3cc6SChuck Lever 		snprintf(buf, 8, "%4hx",
330fbfe3cc6SChuck Lever 				ntohs(addr->sin_port));
331fbfe3cc6SChuck Lever 	}
332fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
333756805e7SChuck Lever 
334756805e7SChuck Lever 	buf = kzalloc(30, GFP_KERNEL);
335756805e7SChuck Lever 	if (buf) {
33621454aaaSHarvey Harrison 		snprintf(buf, 30, "%pI4.%u.%u",
33721454aaaSHarvey Harrison 				&addr->sin_addr.s_addr,
338756805e7SChuck Lever 				ntohs(addr->sin_port) >> 8,
339756805e7SChuck Lever 				ntohs(addr->sin_port) & 0xff);
340756805e7SChuck Lever 	}
341756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3424417c8c4S\"Talpey, Thomas\ 
343b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
344edb267a6SChuck Lever }
345edb267a6SChuck Lever 
346b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
347b454ae90SChuck Lever 					  const char *protocol,
348b454ae90SChuck Lever 					  const char *netid)
3494b6473fbSChuck Lever {
35095392c59SChuck Lever 	struct sockaddr_in6 *addr = xs_addr_in6(xprt);
3514b6473fbSChuck Lever 	char *buf;
3524b6473fbSChuck Lever 
3534b6473fbSChuck Lever 	buf = kzalloc(40, GFP_KERNEL);
3544b6473fbSChuck Lever 	if (buf) {
3555b095d98SHarvey Harrison 		snprintf(buf, 40, "%pI6",&addr->sin6_addr);
3564b6473fbSChuck Lever 	}
3574b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
3584b6473fbSChuck Lever 
3594b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3604b6473fbSChuck Lever 	if (buf) {
3614b6473fbSChuck Lever 		snprintf(buf, 8, "%u",
3624b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3634b6473fbSChuck Lever 	}
3644b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
3654b6473fbSChuck Lever 
366b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
3674b6473fbSChuck Lever 
3684b6473fbSChuck Lever 	buf = kzalloc(64, GFP_KERNEL);
3694b6473fbSChuck Lever 	if (buf) {
3705b095d98SHarvey Harrison 		snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
371fdb46ee7SHarvey Harrison 				&addr->sin6_addr,
3724b6473fbSChuck Lever 				ntohs(addr->sin6_port),
373b454ae90SChuck Lever 				protocol);
3744b6473fbSChuck Lever 	}
3754b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
3764b6473fbSChuck Lever 
3774b6473fbSChuck Lever 	buf = kzalloc(36, GFP_KERNEL);
378b071195dSHarvey Harrison 	if (buf)
3794b7a4274SHarvey Harrison 		snprintf(buf, 36, "%pi6", &addr->sin6_addr);
380b071195dSHarvey Harrison 
3814b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
3824b6473fbSChuck Lever 
3834b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3844b6473fbSChuck Lever 	if (buf) {
3854b6473fbSChuck Lever 		snprintf(buf, 8, "%4hx",
3864b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3874b6473fbSChuck Lever 	}
3884b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
389756805e7SChuck Lever 
390756805e7SChuck Lever 	buf = kzalloc(50, GFP_KERNEL);
391756805e7SChuck Lever 	if (buf) {
3925b095d98SHarvey Harrison 		snprintf(buf, 50, "%pI6.%u.%u",
393fdb46ee7SHarvey Harrison 			 &addr->sin6_addr,
394756805e7SChuck Lever 			 ntohs(addr->sin6_port) >> 8,
395756805e7SChuck Lever 			 ntohs(addr->sin6_port) & 0xff);
396756805e7SChuck Lever 	}
397756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3984417c8c4S\"Talpey, Thomas\ 
399b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
400edb267a6SChuck Lever }
401edb267a6SChuck Lever 
402edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
403edb267a6SChuck Lever {
40433e01dc7SChuck Lever 	unsigned int i;
40533e01dc7SChuck Lever 
40633e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
40733e01dc7SChuck Lever 		switch (i) {
40833e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
40933e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
41033e01dc7SChuck Lever 			continue;
41133e01dc7SChuck Lever 		default:
41233e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
41333e01dc7SChuck Lever 		}
414edb267a6SChuck Lever }
415edb267a6SChuck Lever 
416b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
417b4b5cc85SChuck Lever 
41824c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
419b4b5cc85SChuck Lever {
420b4b5cc85SChuck Lever 	struct msghdr msg = {
421b4b5cc85SChuck Lever 		.msg_name	= addr,
422b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
42324c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
42424c5684bSTrond Myklebust 	};
42524c5684bSTrond Myklebust 	struct kvec iov = {
42624c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
42724c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
428b4b5cc85SChuck Lever 	};
429b4b5cc85SChuck Lever 
43024c5684bSTrond Myklebust 	if (iov.iov_len != 0)
431b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
432b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
433b4b5cc85SChuck Lever }
434b4b5cc85SChuck Lever 
43524c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
436b4b5cc85SChuck Lever {
43724c5684bSTrond Myklebust 	struct page **ppage;
43824c5684bSTrond Myklebust 	unsigned int remainder;
43924c5684bSTrond Myklebust 	int err, sent = 0;
440b4b5cc85SChuck Lever 
44124c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
44224c5684bSTrond Myklebust 	base += xdr->page_base;
44324c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
44424c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
44524c5684bSTrond Myklebust 	for(;;) {
44624c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
44724c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
44824c5684bSTrond Myklebust 
44924c5684bSTrond Myklebust 		remainder -= len;
45024c5684bSTrond Myklebust 		if (remainder != 0 || more)
45124c5684bSTrond Myklebust 			flags |= MSG_MORE;
45224c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
45324c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
45424c5684bSTrond Myklebust 			break;
45524c5684bSTrond Myklebust 		sent += err;
45624c5684bSTrond Myklebust 		ppage++;
45724c5684bSTrond Myklebust 		base = 0;
45824c5684bSTrond Myklebust 	}
45924c5684bSTrond Myklebust 	if (sent == 0)
46024c5684bSTrond Myklebust 		return err;
46124c5684bSTrond Myklebust 	if (err > 0)
46224c5684bSTrond Myklebust 		sent += err;
46324c5684bSTrond Myklebust 	return sent;
464b4b5cc85SChuck Lever }
465b4b5cc85SChuck Lever 
4669903cd1cSChuck Lever /**
4679903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4689903cd1cSChuck Lever  * @sock: socket to send on
4699903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4709903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4719903cd1cSChuck Lever  * @xdr: buffer containing this request
4729903cd1cSChuck Lever  * @base: starting position in the buffer
4739903cd1cSChuck Lever  *
474a246b010SChuck Lever  */
47524c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
476a246b010SChuck Lever {
47724c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
47824c5684bSTrond Myklebust 	int err, sent = 0;
479a246b010SChuck Lever 
480262965f5SChuck Lever 	if (unlikely(!sock))
481fba91afbSTrond Myklebust 		return -ENOTSOCK;
482262965f5SChuck Lever 
483262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
48424c5684bSTrond Myklebust 	if (base != 0) {
48524c5684bSTrond Myklebust 		addr = NULL;
48624c5684bSTrond Myklebust 		addrlen = 0;
48724c5684bSTrond Myklebust 	}
488262965f5SChuck Lever 
48924c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
49024c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
49124c5684bSTrond Myklebust 		remainder -= len;
49224c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
49324c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
494a246b010SChuck Lever 			goto out;
49524c5684bSTrond Myklebust 		sent += err;
496a246b010SChuck Lever 		base = 0;
497a246b010SChuck Lever 	} else
49824c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
499a246b010SChuck Lever 
50024c5684bSTrond Myklebust 	if (base < xdr->page_len) {
50124c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
50224c5684bSTrond Myklebust 		remainder -= len;
50324c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
50424c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
505a246b010SChuck Lever 			goto out;
50624c5684bSTrond Myklebust 		sent += err;
507a246b010SChuck Lever 		base = 0;
50824c5684bSTrond Myklebust 	} else
50924c5684bSTrond Myklebust 		base -= xdr->page_len;
51024c5684bSTrond Myklebust 
51124c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
51224c5684bSTrond Myklebust 		return sent;
51324c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
514a246b010SChuck Lever out:
51524c5684bSTrond Myklebust 	if (sent == 0)
51624c5684bSTrond Myklebust 		return err;
51724c5684bSTrond Myklebust 	if (err > 0)
51824c5684bSTrond Myklebust 		sent += err;
51924c5684bSTrond Myklebust 	return sent;
520a246b010SChuck Lever }
521a246b010SChuck Lever 
522b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
523b6ddf64fSTrond Myklebust {
524b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
525b6ddf64fSTrond Myklebust 
526b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
527b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
528b6ddf64fSTrond Myklebust }
529b6ddf64fSTrond Myklebust 
5309903cd1cSChuck Lever /**
531262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
532262965f5SChuck Lever  * @task: task to put to sleep
5339903cd1cSChuck Lever  *
534a246b010SChuck Lever  */
5355e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
536a246b010SChuck Lever {
537262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
538262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
539ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
5405e3771ceSTrond Myklebust 	int ret = 0;
541a246b010SChuck Lever 
54246121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
543262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
544262965f5SChuck Lever 			req->rq_slen);
545a246b010SChuck Lever 
546262965f5SChuck Lever 	/* Protect against races with write_space */
547262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
548a246b010SChuck Lever 
549262965f5SChuck Lever 	/* Don't race with disconnect */
550b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
551b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
5525e3771ceSTrond Myklebust 			ret = -EAGAIN;
553b6ddf64fSTrond Myklebust 			/*
554b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
555b6ddf64fSTrond Myklebust 			 * window size
556b6ddf64fSTrond Myklebust 			 */
557b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
558b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
559b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
560b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
561b6ddf64fSTrond Myklebust 		}
562b6ddf64fSTrond Myklebust 	} else {
563b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5645e3771ceSTrond Myklebust 		ret = -ENOTCONN;
565b6ddf64fSTrond Myklebust 	}
566a246b010SChuck Lever 
567262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5685e3771ceSTrond Myklebust 	return ret;
569a246b010SChuck Lever }
570a246b010SChuck Lever 
5719903cd1cSChuck Lever /**
572262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5739903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5749903cd1cSChuck Lever  *
5759903cd1cSChuck Lever  * Return values:
5769903cd1cSChuck Lever  *        0:	The request has been sent
5779903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5789903cd1cSChuck Lever  *		complete the request
579262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5809903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5819903cd1cSChuck Lever  */
582262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
583a246b010SChuck Lever {
584a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
585a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
586ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
587262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
588262965f5SChuck Lever 	int status;
589262965f5SChuck Lever 
590262965f5SChuck Lever 	xs_pktdump("packet data:",
591262965f5SChuck Lever 				req->rq_svec->iov_base,
592262965f5SChuck Lever 				req->rq_svec->iov_len);
593262965f5SChuck Lever 
59401d37c42STrond Myklebust 	if (!xprt_bound(xprt))
59501d37c42STrond Myklebust 		return -ENOTCONN;
596ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
59795392c59SChuck Lever 			      xs_addr(xprt),
598ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
599ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
600262965f5SChuck Lever 
601262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
602262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
603262965f5SChuck Lever 
6042199700fSTrond Myklebust 	if (status >= 0) {
6051321d8d9SChuck Lever 		task->tk_bytes_sent += status;
6062199700fSTrond Myklebust 		if (status >= req->rq_slen)
607262965f5SChuck Lever 			return 0;
608262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
609262965f5SChuck Lever 		status = -EAGAIN;
6102199700fSTrond Myklebust 	}
611c8485e4dSTrond Myklebust 	if (!transport->sock)
612c8485e4dSTrond Myklebust 		goto out;
613262965f5SChuck Lever 
614262965f5SChuck Lever 	switch (status) {
615fba91afbSTrond Myklebust 	case -ENOTSOCK:
616fba91afbSTrond Myklebust 		status = -ENOTCONN;
617fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
618fba91afbSTrond Myklebust 		break;
619b6ddf64fSTrond Myklebust 	case -EAGAIN:
6205e3771ceSTrond Myklebust 		status = xs_nospace(task);
621b6ddf64fSTrond Myklebust 		break;
622c8485e4dSTrond Myklebust 	default:
623c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
624c8485e4dSTrond Myklebust 			-status);
625262965f5SChuck Lever 	case -ENETUNREACH:
626262965f5SChuck Lever 	case -EPIPE:
627262965f5SChuck Lever 	case -ECONNREFUSED:
628262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
629262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
630b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
631262965f5SChuck Lever 	}
632c8485e4dSTrond Myklebust out:
633262965f5SChuck Lever 	return status;
634262965f5SChuck Lever }
635262965f5SChuck Lever 
636e06799f9STrond Myklebust /**
637e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
638e06799f9STrond Myklebust  * @xprt: transport
639e06799f9STrond Myklebust  *
640e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
641e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
642e06799f9STrond Myklebust  */
643e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
644e06799f9STrond Myklebust {
645e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
646e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
647e06799f9STrond Myklebust 
648e06799f9STrond Myklebust 	if (sock != NULL)
649e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
650e06799f9STrond Myklebust }
651e06799f9STrond Myklebust 
652808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
653808012fbSChuck Lever {
654808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
655808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
656808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
657808012fbSChuck Lever }
658808012fbSChuck Lever 
659262965f5SChuck Lever /**
660262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
661262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
662262965f5SChuck Lever  *
663262965f5SChuck Lever  * Return values:
664262965f5SChuck Lever  *        0:	The request has been sent
665262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
666262965f5SChuck Lever  *		complete the request
667262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
668262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
669262965f5SChuck Lever  *
670262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
671262965f5SChuck Lever  *	if sendmsg is not able to make progress?
672262965f5SChuck Lever  */
673262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
674262965f5SChuck Lever {
675262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
676262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
677ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
678262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
679b595bb15SChuck Lever 	int status;
680a246b010SChuck Lever 
681808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
682262965f5SChuck Lever 
683262965f5SChuck Lever 	xs_pktdump("packet data:",
684262965f5SChuck Lever 				req->rq_svec->iov_base,
685262965f5SChuck Lever 				req->rq_svec->iov_len);
686a246b010SChuck Lever 
687a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
688a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
689262965f5SChuck Lever 	 * called sendmsg(). */
690a246b010SChuck Lever 	while (1) {
691ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
692ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
693a246b010SChuck Lever 
694262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
695262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
696262965f5SChuck Lever 
697262965f5SChuck Lever 		if (unlikely(status < 0))
698a246b010SChuck Lever 			break;
699a246b010SChuck Lever 
700a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
701a246b010SChuck Lever 		 * reset the count of bytes sent. */
702262965f5SChuck Lever 		req->rq_bytes_sent += status;
703ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
704262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
705a246b010SChuck Lever 			req->rq_bytes_sent = 0;
706a246b010SChuck Lever 			return 0;
707a246b010SChuck Lever 		}
708262965f5SChuck Lever 
70906b4b681STrond Myklebust 		if (status != 0)
71006b4b681STrond Myklebust 			continue;
711a246b010SChuck Lever 		status = -EAGAIN;
712a246b010SChuck Lever 		break;
713a246b010SChuck Lever 	}
714c8485e4dSTrond Myklebust 	if (!transport->sock)
715c8485e4dSTrond Myklebust 		goto out;
716a246b010SChuck Lever 
717262965f5SChuck Lever 	switch (status) {
718fba91afbSTrond Myklebust 	case -ENOTSOCK:
719fba91afbSTrond Myklebust 		status = -ENOTCONN;
720fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
721fba91afbSTrond Myklebust 		break;
722262965f5SChuck Lever 	case -EAGAIN:
7235e3771ceSTrond Myklebust 		status = xs_nospace(task);
724262965f5SChuck Lever 		break;
725c8485e4dSTrond Myklebust 	default:
726c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
727c8485e4dSTrond Myklebust 			-status);
728262965f5SChuck Lever 	case -ECONNRESET:
7292a9e1cfaSTrond Myklebust 		xs_tcp_shutdown(xprt);
7302a9e1cfaSTrond Myklebust 	case -ECONNREFUSED:
731262965f5SChuck Lever 	case -ENOTCONN:
732262965f5SChuck Lever 	case -EPIPE:
733b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
734a246b010SChuck Lever 	}
735c8485e4dSTrond Myklebust out:
736a246b010SChuck Lever 	return status;
737a246b010SChuck Lever }
738a246b010SChuck Lever 
7399903cd1cSChuck Lever /**
740e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
741e0ab53deSTrond Myklebust  * @xprt: transport
742e0ab53deSTrond Myklebust  * @task: rpc task
743e0ab53deSTrond Myklebust  *
744e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
745e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
746e0ab53deSTrond Myklebust  * the server.
747e0ab53deSTrond Myklebust  */
748e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
749e0ab53deSTrond Myklebust {
750e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
751e0ab53deSTrond Myklebust 
752e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
753e0ab53deSTrond Myklebust 		return;
754e0ab53deSTrond Myklebust 	if (task == NULL)
755e0ab53deSTrond Myklebust 		goto out_release;
756e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
757e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
758e0ab53deSTrond Myklebust 		goto out_release;
759e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
760e0ab53deSTrond Myklebust 		goto out_release;
761e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
762e0ab53deSTrond Myklebust out_release:
763e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
764e0ab53deSTrond Myklebust }
765e0ab53deSTrond Myklebust 
7662a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7672a9e1cfaSTrond Myklebust {
7682a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7692a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7702a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7712a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7722a9e1cfaSTrond Myklebust }
7732a9e1cfaSTrond Myklebust 
7742a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7752a9e1cfaSTrond Myklebust {
7762a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7772a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7782a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7792a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7802a9e1cfaSTrond Myklebust }
7812a9e1cfaSTrond Myklebust 
782fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
783a246b010SChuck Lever {
784ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
785ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
786a246b010SChuck Lever 
787fe315e76SChuck Lever 	if (sk == NULL)
788fe315e76SChuck Lever 		return;
7899903cd1cSChuck Lever 
790a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
791ee0ac0c2SChuck Lever 	transport->inet = NULL;
792ee0ac0c2SChuck Lever 	transport->sock = NULL;
793a246b010SChuck Lever 
794a246b010SChuck Lever 	sk->sk_user_data = NULL;
7952a9e1cfaSTrond Myklebust 
7962a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
797a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
798a246b010SChuck Lever 
799a246b010SChuck Lever 	sk->sk_no_check = 0;
800a246b010SChuck Lever 
801a246b010SChuck Lever 	sock_release(sock);
802fe315e76SChuck Lever }
803fe315e76SChuck Lever 
804fe315e76SChuck Lever /**
805fe315e76SChuck Lever  * xs_close - close a socket
806fe315e76SChuck Lever  * @xprt: transport
807fe315e76SChuck Lever  *
808fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
809fe315e76SChuck Lever  * on the server we want to save.
810fe315e76SChuck Lever  */
811fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
812fe315e76SChuck Lever {
813fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
814fe315e76SChuck Lever 
815fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
816fe315e76SChuck Lever 
817fe315e76SChuck Lever 	xs_reset_transport(transport);
818fe315e76SChuck Lever 
819632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
8207d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
821632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
8223b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
823632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
82462da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
825a246b010SChuck Lever }
826a246b010SChuck Lever 
8279903cd1cSChuck Lever /**
8289903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8299903cd1cSChuck Lever  * @xprt: doomed transport
8309903cd1cSChuck Lever  *
8319903cd1cSChuck Lever  */
8329903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
833a246b010SChuck Lever {
834c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
835c8475461SChuck Lever 
8369903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
8379903cd1cSChuck Lever 
838c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
839a246b010SChuck Lever 
8409903cd1cSChuck Lever 	xs_close(xprt);
841edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
842a246b010SChuck Lever 	kfree(xprt->slot);
843c8541ecdSChuck Lever 	kfree(xprt);
844bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
845a246b010SChuck Lever }
846a246b010SChuck Lever 
8479903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8489903cd1cSChuck Lever {
8499903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8509903cd1cSChuck Lever }
8519903cd1cSChuck Lever 
8529903cd1cSChuck Lever /**
8539903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8549903cd1cSChuck Lever  * @sk: socket with data to read
8559903cd1cSChuck Lever  * @len: how much data to read
8569903cd1cSChuck Lever  *
857a246b010SChuck Lever  */
8589903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
859a246b010SChuck Lever {
860a246b010SChuck Lever 	struct rpc_task *task;
861a246b010SChuck Lever 	struct rpc_xprt *xprt;
862a246b010SChuck Lever 	struct rpc_rqst *rovr;
863a246b010SChuck Lever 	struct sk_buff *skb;
864a246b010SChuck Lever 	int err, repsize, copied;
865d8ed029dSAlexey Dobriyan 	u32 _xid;
866d8ed029dSAlexey Dobriyan 	__be32 *xp;
867a246b010SChuck Lever 
868a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8699903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8709903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
871a246b010SChuck Lever 		goto out;
872a246b010SChuck Lever 
873a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
874a246b010SChuck Lever 		goto out;
875a246b010SChuck Lever 
876a246b010SChuck Lever 	if (xprt->shutdown)
877a246b010SChuck Lever 		goto dropit;
878a246b010SChuck Lever 
879a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
880a246b010SChuck Lever 	if (repsize < 4) {
8819903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
882a246b010SChuck Lever 		goto dropit;
883a246b010SChuck Lever 	}
884a246b010SChuck Lever 
885a246b010SChuck Lever 	/* Copy the XID from the skb... */
886a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
887a246b010SChuck Lever 				sizeof(_xid), &_xid);
888a246b010SChuck Lever 	if (xp == NULL)
889a246b010SChuck Lever 		goto dropit;
890a246b010SChuck Lever 
891a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8924a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
893a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
894a246b010SChuck Lever 	if (!rovr)
895a246b010SChuck Lever 		goto out_unlock;
896a246b010SChuck Lever 	task = rovr->rq_task;
897a246b010SChuck Lever 
898a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
899a246b010SChuck Lever 		copied = repsize;
900a246b010SChuck Lever 
901a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
9021781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
9031781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
904a246b010SChuck Lever 		goto out_unlock;
9051781f7f5SHerbert Xu 	}
9061781f7f5SHerbert Xu 
9071781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
908a246b010SChuck Lever 
909a246b010SChuck Lever 	/* Something worked... */
910a246b010SChuck Lever 	dst_confirm(skb->dst);
911a246b010SChuck Lever 
9121570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
9131570c1e4SChuck Lever 	xprt_update_rtt(task);
9141570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
915a246b010SChuck Lever 
916a246b010SChuck Lever  out_unlock:
9174a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
918a246b010SChuck Lever  dropit:
919a246b010SChuck Lever 	skb_free_datagram(sk, skb);
920a246b010SChuck Lever  out:
921a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
922a246b010SChuck Lever }
923a246b010SChuck Lever 
924dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
925a246b010SChuck Lever {
92651971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
927a246b010SChuck Lever 	size_t len, used;
928a246b010SChuck Lever 	char *p;
929a246b010SChuck Lever 
93051971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
93151971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
9329d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
93351971139SChuck Lever 	transport->tcp_offset += used;
934a246b010SChuck Lever 	if (used != len)
935a246b010SChuck Lever 		return;
936808012fbSChuck Lever 
93751971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
93851971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
939e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
940a246b010SChuck Lever 	else
941e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
94251971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
943808012fbSChuck Lever 
944e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
94551971139SChuck Lever 	transport->tcp_offset = 0;
946808012fbSChuck Lever 
947a246b010SChuck Lever 	/* Sanity check of the record length */
94851971139SChuck Lever 	if (unlikely(transport->tcp_reclen < 4)) {
9499903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
9503ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9519903cd1cSChuck Lever 		return;
952a246b010SChuck Lever 	}
953a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
95451971139SChuck Lever 			transport->tcp_reclen);
955a246b010SChuck Lever }
956a246b010SChuck Lever 
95751971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
958a246b010SChuck Lever {
95951971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
960e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
96151971139SChuck Lever 		transport->tcp_offset = 0;
962e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
963e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
964e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
96551971139SChuck Lever 			transport->tcp_copied = 0;
966a246b010SChuck Lever 		}
967a246b010SChuck Lever 	}
968a246b010SChuck Lever }
969a246b010SChuck Lever 
970dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
971a246b010SChuck Lever {
972a246b010SChuck Lever 	size_t len, used;
973a246b010SChuck Lever 	char *p;
974a246b010SChuck Lever 
97551971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
976a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
97751971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9789d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
97951971139SChuck Lever 	transport->tcp_offset += used;
980a246b010SChuck Lever 	if (used != len)
981a246b010SChuck Lever 		return;
982e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
983e136d092SChuck Lever 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
98451971139SChuck Lever 	transport->tcp_copied = 4;
985a246b010SChuck Lever 	dprintk("RPC:       reading reply for XID %08x\n",
98651971139SChuck Lever 			ntohl(transport->tcp_xid));
98751971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
988a246b010SChuck Lever }
989a246b010SChuck Lever 
990dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
991a246b010SChuck Lever {
99251971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
993a246b010SChuck Lever 	struct rpc_rqst *req;
994a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
995a246b010SChuck Lever 	size_t len;
996a246b010SChuck Lever 	ssize_t r;
997a246b010SChuck Lever 
998a246b010SChuck Lever 	/* Find and lock the request corresponding to this xid */
9994a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
100051971139SChuck Lever 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1001a246b010SChuck Lever 	if (!req) {
1002e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1003a246b010SChuck Lever 		dprintk("RPC:       XID %08x request not found!\n",
100451971139SChuck Lever 				ntohl(transport->tcp_xid));
10054a0f8c04SChuck Lever 		spin_unlock(&xprt->transport_lock);
1006a246b010SChuck Lever 		return;
1007a246b010SChuck Lever 	}
1008a246b010SChuck Lever 
1009a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1010a246b010SChuck Lever 	len = desc->count;
101151971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1012dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1013a246b010SChuck Lever 
101451971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1015a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1016a246b010SChuck Lever 		my_desc.count = len;
101751971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10189d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1019a246b010SChuck Lever 		desc->count -= r;
1020a246b010SChuck Lever 		desc->offset += r;
1021a246b010SChuck Lever 	} else
102251971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10239d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1024a246b010SChuck Lever 
1025a246b010SChuck Lever 	if (r > 0) {
102651971139SChuck Lever 		transport->tcp_copied += r;
102751971139SChuck Lever 		transport->tcp_offset += r;
1028a246b010SChuck Lever 	}
1029a246b010SChuck Lever 	if (r != len) {
1030a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1031a246b010SChuck Lever 		 * usually because we weren't able to allocate
1032a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1033e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1034a246b010SChuck Lever 		 * will not receive any additional updates,
1035a246b010SChuck Lever 		 * and time out.
1036a246b010SChuck Lever 		 * Any remaining data from this record will
1037a246b010SChuck Lever 		 * be discarded.
1038a246b010SChuck Lever 		 */
1039e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1040a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
104151971139SChuck Lever 				ntohl(transport->tcp_xid));
104246121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
104346121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
104446121cf7SChuck Lever 				xprt, transport->tcp_copied,
104546121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
1046a246b010SChuck Lever 		goto out;
1047a246b010SChuck Lever 	}
1048a246b010SChuck Lever 
1049a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
105051971139SChuck Lever 			ntohl(transport->tcp_xid), r);
105146121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
105246121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
105346121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1054a246b010SChuck Lever 
105551971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1056e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
105751971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1058e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1059e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1060a246b010SChuck Lever 	}
1061a246b010SChuck Lever 
1062a246b010SChuck Lever out:
1063e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
106451971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
10654a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
106651971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1067a246b010SChuck Lever }
1068a246b010SChuck Lever 
1069dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1070a246b010SChuck Lever {
1071a246b010SChuck Lever 	size_t len;
1072a246b010SChuck Lever 
107351971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1074a246b010SChuck Lever 	if (len > desc->count)
1075a246b010SChuck Lever 		len = desc->count;
1076a246b010SChuck Lever 	desc->count -= len;
1077a246b010SChuck Lever 	desc->offset += len;
107851971139SChuck Lever 	transport->tcp_offset += len;
1079a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
108051971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1081a246b010SChuck Lever }
1082a246b010SChuck Lever 
10839903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1084a246b010SChuck Lever {
1085a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
108651971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1087dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1088a246b010SChuck Lever 		.skb	= skb,
1089a246b010SChuck Lever 		.offset	= offset,
1090a246b010SChuck Lever 		.count	= len,
1091a246b010SChuck Lever 	};
1092a246b010SChuck Lever 
10939903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1094a246b010SChuck Lever 	do {
1095a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1096a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1097e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
10989903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1099a246b010SChuck Lever 			continue;
1100a246b010SChuck Lever 		}
1101a246b010SChuck Lever 		/* Read in the xid if necessary */
1102e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
110351971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1104a246b010SChuck Lever 			continue;
1105a246b010SChuck Lever 		}
1106a246b010SChuck Lever 		/* Read in the request data */
1107e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
11089903cd1cSChuck Lever 			xs_tcp_read_request(xprt, &desc);
1109a246b010SChuck Lever 			continue;
1110a246b010SChuck Lever 		}
1111a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
111251971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1113a246b010SChuck Lever 	} while (desc.count);
11149903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1115a246b010SChuck Lever 	return len - desc.count;
1116a246b010SChuck Lever }
1117a246b010SChuck Lever 
11189903cd1cSChuck Lever /**
11199903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
11209903cd1cSChuck Lever  * @sk: socket with data to read
11219903cd1cSChuck Lever  * @bytes: how much data to read
11229903cd1cSChuck Lever  *
11239903cd1cSChuck Lever  */
11249903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1125a246b010SChuck Lever {
1126a246b010SChuck Lever 	struct rpc_xprt *xprt;
1127a246b010SChuck Lever 	read_descriptor_t rd_desc;
1128ff2d7db8STrond Myklebust 	int read;
1129a246b010SChuck Lever 
11309903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
113146121cf7SChuck Lever 
113246121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
11339903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1134a246b010SChuck Lever 		goto out;
1135a246b010SChuck Lever 	if (xprt->shutdown)
1136a246b010SChuck Lever 		goto out;
1137a246b010SChuck Lever 
11389903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1139a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1140ff2d7db8STrond Myklebust 	do {
1141a246b010SChuck Lever 		rd_desc.count = 65536;
1142ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1143ff2d7db8STrond Myklebust 	} while (read > 0);
1144a246b010SChuck Lever out:
1145a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1146a246b010SChuck Lever }
1147a246b010SChuck Lever 
11487d1e8255STrond Myklebust /*
11497d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
11507d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
11517d1e8255STrond Myklebust  * fashion
11527d1e8255STrond Myklebust  */
11537d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
11547d1e8255STrond Myklebust 		unsigned long timeout)
11557d1e8255STrond Myklebust {
11567d1e8255STrond Myklebust 	struct sock_xprt *transport;
11577d1e8255STrond Myklebust 
11587d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
11597d1e8255STrond Myklebust 		return;
11607d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
11617d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
11627d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
11637d1e8255STrond Myklebust 			   timeout);
11647d1e8255STrond Myklebust }
11657d1e8255STrond Myklebust 
11667d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
11677d1e8255STrond Myklebust {
11687d1e8255STrond Myklebust 	struct sock_xprt *transport;
11697d1e8255STrond Myklebust 
11707d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
11717d1e8255STrond Myklebust 
11727d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
11737d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
11747d1e8255STrond Myklebust 		return;
11757d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
11767d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
11777d1e8255STrond Myklebust }
11787d1e8255STrond Myklebust 
11797d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
11807d1e8255STrond Myklebust {
11817d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
11827d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
11837d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
11847d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
11857d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
11867d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
11877d1e8255STrond Myklebust }
11887d1e8255STrond Myklebust 
11899903cd1cSChuck Lever /**
11909903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
11919903cd1cSChuck Lever  * @sk: socket whose state has changed
11929903cd1cSChuck Lever  *
11939903cd1cSChuck Lever  */
11949903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1195a246b010SChuck Lever {
1196a246b010SChuck Lever 	struct rpc_xprt *xprt;
1197a246b010SChuck Lever 
1198a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1199a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1200a246b010SChuck Lever 		goto out;
12019903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1202a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1203a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1204a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1205a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1206a246b010SChuck Lever 
1207a246b010SChuck Lever 	switch (sk->sk_state) {
1208a246b010SChuck Lever 	case TCP_ESTABLISHED:
12094a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1210a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
121151971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
121251971139SChuck Lever 					struct sock_xprt, xprt);
121351971139SChuck Lever 
1214a246b010SChuck Lever 			/* Reset TCP record info */
121551971139SChuck Lever 			transport->tcp_offset = 0;
121651971139SChuck Lever 			transport->tcp_reclen = 0;
121751971139SChuck Lever 			transport->tcp_copied = 0;
1218e136d092SChuck Lever 			transport->tcp_flags =
1219e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
122051971139SChuck Lever 
12212a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1222a246b010SChuck Lever 		}
12234a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1224a246b010SChuck Lever 		break;
12253b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
12263b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
12277c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1228663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
12293b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
12303b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
12313b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1232ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12333b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
123425fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1235a246b010SChuck Lever 		break;
1236632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
12373b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
123866af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1239663b8858STrond Myklebust 	case TCP_SYN_SENT:
12407c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1241663b8858STrond Myklebust 	case TCP_CLOSING:
1242663b8858STrond Myklebust 		/*
1243663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1244663b8858STrond Myklebust 		 * we back off before reconnecting
1245663b8858STrond Myklebust 		 */
1246663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1247663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
12483b948ae5STrond Myklebust 		break;
12493b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1250670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
125125fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
12523b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
12533b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
12543b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
12553b948ae5STrond Myklebust 		break;
12563b948ae5STrond Myklebust 	case TCP_CLOSE:
12577d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
12587d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1259a246b010SChuck Lever 	}
1260a246b010SChuck Lever  out:
1261a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1262a246b010SChuck Lever }
1263a246b010SChuck Lever 
12649903cd1cSChuck Lever /**
1265482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
12662a9e1cfaSTrond Myklebust  * @sk: socket
12672a9e1cfaSTrond Myklebust  */
1268482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
12692a9e1cfaSTrond Myklebust {
12702a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
12712a9e1cfaSTrond Myklebust 
12722a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
12732a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
12742a9e1cfaSTrond Myklebust 		goto out;
12752a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
12762a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
12772a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1278482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
12792a9e1cfaSTrond Myklebust out:
12802a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
12812a9e1cfaSTrond Myklebust }
12822a9e1cfaSTrond Myklebust 
12832a9e1cfaSTrond Myklebust /**
1284c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1285c7b2cae8SChuck Lever  *                             becomes available
12869903cd1cSChuck Lever  * @sk: socket whose state has changed
12879903cd1cSChuck Lever  *
1288a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1289a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1290c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1291a246b010SChuck Lever  * with a bunch of small requests.
1292a246b010SChuck Lever  */
1293c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1294a246b010SChuck Lever {
1295a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1296c7b2cae8SChuck Lever 
1297c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
1298c7b2cae8SChuck Lever 	if (sock_writeable(sk)) {
1299c7b2cae8SChuck Lever 		struct socket *sock;
1300c7b2cae8SChuck Lever 		struct rpc_xprt *xprt;
1301c7b2cae8SChuck Lever 
1302c7b2cae8SChuck Lever 		if (unlikely(!(sock = sk->sk_socket)))
1303a246b010SChuck Lever 			goto out;
1304b6ddf64fSTrond Myklebust 		clear_bit(SOCK_NOSPACE, &sock->flags);
1305b6ddf64fSTrond Myklebust 
1306c7b2cae8SChuck Lever 		if (unlikely(!(xprt = xprt_from_sock(sk))))
1307c7b2cae8SChuck Lever 			goto out;
1308b6ddf64fSTrond Myklebust 		if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1309a246b010SChuck Lever 			goto out;
1310a246b010SChuck Lever 
1311c7b2cae8SChuck Lever 		xprt_write_space(xprt);
1312a246b010SChuck Lever 	}
1313a246b010SChuck Lever 
1314c7b2cae8SChuck Lever  out:
1315c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1316c7b2cae8SChuck Lever }
1317c7b2cae8SChuck Lever 
1318c7b2cae8SChuck Lever /**
1319c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1320c7b2cae8SChuck Lever  *                             becomes available
1321c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1322c7b2cae8SChuck Lever  *
1323c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1324c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1325c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1326c7b2cae8SChuck Lever  * with a bunch of small requests.
1327c7b2cae8SChuck Lever  */
1328c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1329c7b2cae8SChuck Lever {
1330c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1331c7b2cae8SChuck Lever 
1332c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
1333c7b2cae8SChuck Lever 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1334c7b2cae8SChuck Lever 		struct socket *sock;
1335c7b2cae8SChuck Lever 		struct rpc_xprt *xprt;
1336c7b2cae8SChuck Lever 
1337c7b2cae8SChuck Lever 		if (unlikely(!(sock = sk->sk_socket)))
1338c7b2cae8SChuck Lever 			goto out;
1339b6ddf64fSTrond Myklebust 		clear_bit(SOCK_NOSPACE, &sock->flags);
1340b6ddf64fSTrond Myklebust 
1341c7b2cae8SChuck Lever 		if (unlikely(!(xprt = xprt_from_sock(sk))))
1342c7b2cae8SChuck Lever 			goto out;
1343b6ddf64fSTrond Myklebust 		if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1344a246b010SChuck Lever 			goto out;
1345a246b010SChuck Lever 
1346c7b2cae8SChuck Lever 		xprt_write_space(xprt);
1347c7b2cae8SChuck Lever 	}
1348c7b2cae8SChuck Lever 
1349a246b010SChuck Lever  out:
1350a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1351a246b010SChuck Lever }
1352a246b010SChuck Lever 
1353470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1354a246b010SChuck Lever {
1355ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1356ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1357a246b010SChuck Lever 
13587c6e066eSChuck Lever 	if (transport->rcvsize) {
1359a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
13607c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1361a246b010SChuck Lever 	}
13627c6e066eSChuck Lever 	if (transport->sndsize) {
1363a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
13647c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1365a246b010SChuck Lever 		sk->sk_write_space(sk);
1366a246b010SChuck Lever 	}
1367a246b010SChuck Lever }
1368a246b010SChuck Lever 
136943118c29SChuck Lever /**
1370470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
137143118c29SChuck Lever  * @xprt: generic transport
1372470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1373470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
137443118c29SChuck Lever  *
1375470056c2SChuck Lever  * Set socket send and receive buffer size limits.
137643118c29SChuck Lever  */
1377470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
137843118c29SChuck Lever {
13797c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
13807c6e066eSChuck Lever 
13817c6e066eSChuck Lever 	transport->sndsize = 0;
1382470056c2SChuck Lever 	if (sndsize)
13837c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
13847c6e066eSChuck Lever 	transport->rcvsize = 0;
1385470056c2SChuck Lever 	if (rcvsize)
13867c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1387470056c2SChuck Lever 
1388470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
138943118c29SChuck Lever }
139043118c29SChuck Lever 
139146c0ee8bSChuck Lever /**
139246c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
139346c0ee8bSChuck Lever  * @task: task that timed out
139446c0ee8bSChuck Lever  *
139546c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
139646c0ee8bSChuck Lever  */
139746c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
139846c0ee8bSChuck Lever {
139946c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
140046c0ee8bSChuck Lever }
140146c0ee8bSChuck Lever 
1402b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1403b85d8806SChuck Lever {
1404b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1405b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1406b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1407b85d8806SChuck Lever }
1408b85d8806SChuck Lever 
140992200412SChuck Lever /**
141092200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
141192200412SChuck Lever  * @xprt: generic transport
141292200412SChuck Lever  * @port: new port number
141392200412SChuck Lever  *
141492200412SChuck Lever  */
141592200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
141692200412SChuck Lever {
141795392c59SChuck Lever 	struct sockaddr *addr = xs_addr(xprt);
1418c4efcb1dSChuck Lever 
141992200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1420c4efcb1dSChuck Lever 
142120612005SChuck Lever 	switch (addr->sa_family) {
142220612005SChuck Lever 	case AF_INET:
142320612005SChuck Lever 		((struct sockaddr_in *)addr)->sin_port = htons(port);
142420612005SChuck Lever 		break;
142520612005SChuck Lever 	case AF_INET6:
142620612005SChuck Lever 		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
142720612005SChuck Lever 		break;
142820612005SChuck Lever 	default:
142920612005SChuck Lever 		BUG();
143020612005SChuck Lever 	}
143192200412SChuck Lever }
143292200412SChuck Lever 
143367a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
143467a391d7STrond Myklebust {
143567a391d7STrond Myklebust 	unsigned short port = transport->port;
143667a391d7STrond Myklebust 
143767a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
143867a391d7STrond Myklebust 		port = xs_get_random_port();
143967a391d7STrond Myklebust 	return port;
144067a391d7STrond Myklebust }
144167a391d7STrond Myklebust 
144267a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
144367a391d7STrond Myklebust {
144467a391d7STrond Myklebust 	if (transport->port != 0)
144567a391d7STrond Myklebust 		transport->port = 0;
144667a391d7STrond Myklebust 	if (!transport->xprt.resvport)
144767a391d7STrond Myklebust 		return 0;
144867a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
144967a391d7STrond Myklebust 		return xprt_max_resvport;
145067a391d7STrond Myklebust 	return --port;
145167a391d7STrond Myklebust }
145267a391d7STrond Myklebust 
14537dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1454a246b010SChuck Lever {
1455a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1456a246b010SChuck Lever 		.sin_family = AF_INET,
1457a246b010SChuck Lever 	};
1458d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
145967a391d7STrond Myklebust 	int err, nloop = 0;
146067a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
146167a391d7STrond Myklebust 	unsigned short last;
1462a246b010SChuck Lever 
1463d3bc9a1dSFrank van Maarseveen 	sa = (struct sockaddr_in *)&transport->addr;
1464d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1465a246b010SChuck Lever 	do {
1466a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1467e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1468a246b010SChuck Lever 						sizeof(myaddr));
146967a391d7STrond Myklebust 		if (port == 0)
1470d3bc9a1dSFrank van Maarseveen 			break;
1471a246b010SChuck Lever 		if (err == 0) {
1472c8475461SChuck Lever 			transport->port = port;
1473d3bc9a1dSFrank van Maarseveen 			break;
1474a246b010SChuck Lever 		}
147567a391d7STrond Myklebust 		last = port;
147667a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
147767a391d7STrond Myklebust 		if (port > last)
147867a391d7STrond Myklebust 			nloop++;
147967a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
148021454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
148121454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
14827dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1483a246b010SChuck Lever 	return err;
1484a246b010SChuck Lever }
1485a246b010SChuck Lever 
148690058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
148790058d37SChuck Lever {
148890058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
148990058d37SChuck Lever 		.sin6_family = AF_INET6,
149090058d37SChuck Lever 	};
149190058d37SChuck Lever 	struct sockaddr_in6 *sa;
149267a391d7STrond Myklebust 	int err, nloop = 0;
149367a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
149467a391d7STrond Myklebust 	unsigned short last;
149590058d37SChuck Lever 
149690058d37SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->addr;
149790058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
149890058d37SChuck Lever 	do {
149990058d37SChuck Lever 		myaddr.sin6_port = htons(port);
150090058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
150190058d37SChuck Lever 						sizeof(myaddr));
150267a391d7STrond Myklebust 		if (port == 0)
150390058d37SChuck Lever 			break;
150490058d37SChuck Lever 		if (err == 0) {
150590058d37SChuck Lever 			transport->port = port;
150690058d37SChuck Lever 			break;
150790058d37SChuck Lever 		}
150867a391d7STrond Myklebust 		last = port;
150967a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
151067a391d7STrond Myklebust 		if (port > last)
151167a391d7STrond Myklebust 			nloop++;
151267a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
15135b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1514fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1515a246b010SChuck Lever 	return err;
1516a246b010SChuck Lever }
1517a246b010SChuck Lever 
1518ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1519ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1520ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1521ed07536eSPeter Zijlstra 
15228945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1523ed07536eSPeter Zijlstra {
1524ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
15258945ee5eSChuck Lever 
152602b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
15278945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
15288945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1529ed07536eSPeter Zijlstra }
15308945ee5eSChuck Lever 
15318945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
15328945ee5eSChuck Lever {
15338945ee5eSChuck Lever 	struct sock *sk = sock->sk;
15348945ee5eSChuck Lever 
1535f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
15368945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
15378945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1538ed07536eSPeter Zijlstra }
1539ed07536eSPeter Zijlstra #else
15408945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
15418945ee5eSChuck Lever {
15428945ee5eSChuck Lever }
15438945ee5eSChuck Lever 
15448945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1545ed07536eSPeter Zijlstra {
1546ed07536eSPeter Zijlstra }
1547ed07536eSPeter Zijlstra #endif
1548ed07536eSPeter Zijlstra 
154916be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1550a246b010SChuck Lever {
155116be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1552edb267a6SChuck Lever 
1553ee0ac0c2SChuck Lever 	if (!transport->inet) {
1554b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1555b0d93ad5SChuck Lever 
1556b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1557b0d93ad5SChuck Lever 
15582a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
15592a9e1cfaSTrond Myklebust 
1560b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1561b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1562b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1563482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1564b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1565b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1566b0d93ad5SChuck Lever 
1567b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1568b0d93ad5SChuck Lever 
1569b0d93ad5SChuck Lever 		/* Reset to new socket */
1570ee0ac0c2SChuck Lever 		transport->sock = sock;
1571ee0ac0c2SChuck Lever 		transport->inet = sk;
1572b0d93ad5SChuck Lever 
1573b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1574b0d93ad5SChuck Lever 	}
1575470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
157616be2d20SChuck Lever }
157716be2d20SChuck Lever 
1578a246b010SChuck Lever /**
15799c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1580a246b010SChuck Lever  * @work: RPC transport to connect
1581a246b010SChuck Lever  *
1582a246b010SChuck Lever  * Invoked by a work queue tasklet.
1583a246b010SChuck Lever  */
15849c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1585a246b010SChuck Lever {
1586a246b010SChuck Lever 	struct sock_xprt *transport =
1587a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1588a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1589a246b010SChuck Lever 	struct socket *sock = transport->sock;
1590a246b010SChuck Lever 	int err, status = -EIO;
1591a246b010SChuck Lever 
159201d37c42STrond Myklebust 	if (xprt->shutdown)
15939903cd1cSChuck Lever 		goto out;
15949903cd1cSChuck Lever 
1595a246b010SChuck Lever 	/* Start by resetting any existing state */
1596fe315e76SChuck Lever 	xs_reset_transport(transport);
1597a246b010SChuck Lever 
1598fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1599fe315e76SChuck Lever 	if (err < 0) {
16009903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1601a246b010SChuck Lever 		goto out;
1602a246b010SChuck Lever 	}
16038945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1604a246b010SChuck Lever 
16057dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
16069903cd1cSChuck Lever 		sock_release(sock);
16079903cd1cSChuck Lever 		goto out;
1608a246b010SChuck Lever 	}
1609b0d93ad5SChuck Lever 
1610b0d93ad5SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1611b0d93ad5SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1612b0d93ad5SChuck Lever 
161316be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1614a246b010SChuck Lever 	status = 0;
1615b0d93ad5SChuck Lever out:
1616b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
16177d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1618b0d93ad5SChuck Lever }
1619b0d93ad5SChuck Lever 
162068e220bdSChuck Lever /**
162168e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
162268e220bdSChuck Lever  * @work: RPC transport to connect
162368e220bdSChuck Lever  *
162468e220bdSChuck Lever  * Invoked by a work queue tasklet.
162568e220bdSChuck Lever  */
162668e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
162768e220bdSChuck Lever {
162868e220bdSChuck Lever 	struct sock_xprt *transport =
162968e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
163068e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
163168e220bdSChuck Lever 	struct socket *sock = transport->sock;
163268e220bdSChuck Lever 	int err, status = -EIO;
163368e220bdSChuck Lever 
163401d37c42STrond Myklebust 	if (xprt->shutdown)
163568e220bdSChuck Lever 		goto out;
163668e220bdSChuck Lever 
163768e220bdSChuck Lever 	/* Start by resetting any existing state */
1638fe315e76SChuck Lever 	xs_reset_transport(transport);
163968e220bdSChuck Lever 
1640fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1641fe315e76SChuck Lever 	if (err < 0) {
164268e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
164368e220bdSChuck Lever 		goto out;
164468e220bdSChuck Lever 	}
16458945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
164668e220bdSChuck Lever 
164768e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
164868e220bdSChuck Lever 		sock_release(sock);
164968e220bdSChuck Lever 		goto out;
165068e220bdSChuck Lever 	}
165168e220bdSChuck Lever 
165268e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
165368e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
165468e220bdSChuck Lever 
165568e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1656a246b010SChuck Lever 	status = 0;
1657b0d93ad5SChuck Lever out:
1658b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
16597d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1660b0d93ad5SChuck Lever }
1661b0d93ad5SChuck Lever 
16623167e12cSChuck Lever /*
16633167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
16643167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
16653167e12cSChuck Lever  */
166640d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
16673167e12cSChuck Lever {
16683167e12cSChuck Lever 	int result;
16693167e12cSChuck Lever 	struct sockaddr any;
16703167e12cSChuck Lever 
16713167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
16723167e12cSChuck Lever 
16733167e12cSChuck Lever 	/*
16743167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
16753167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
16763167e12cSChuck Lever 	 */
16773167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
16783167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1679ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
16807d1e8255STrond Myklebust 	if (!result)
16817d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
16827d1e8255STrond Myklebust 	else
16833167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
16843167e12cSChuck Lever 				result);
16853167e12cSChuck Lever }
16863167e12cSChuck Lever 
168740d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
168840d2549dSTrond Myklebust {
168940d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
169040d2549dSTrond Myklebust 
169140d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
169240d2549dSTrond Myklebust 		return;
169340d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
169440d2549dSTrond Myklebust 		return;
169540d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
169640d2549dSTrond Myklebust }
169740d2549dSTrond Myklebust 
169816be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1699b0d93ad5SChuck Lever {
170016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1701edb267a6SChuck Lever 
1702ee0ac0c2SChuck Lever 	if (!transport->inet) {
1703b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1704b0d93ad5SChuck Lever 
1705b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1706b0d93ad5SChuck Lever 
17072a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
17082a9e1cfaSTrond Myklebust 
1709b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1710b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1711b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1712b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1713482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1714b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
17153167e12cSChuck Lever 
17163167e12cSChuck Lever 		/* socket options */
17173167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
17183167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
17193167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
17203167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1721b0d93ad5SChuck Lever 
1722b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1723b0d93ad5SChuck Lever 
1724b0d93ad5SChuck Lever 		/* Reset to new socket */
1725ee0ac0c2SChuck Lever 		transport->sock = sock;
1726ee0ac0c2SChuck Lever 		transport->inet = sk;
1727b0d93ad5SChuck Lever 
1728b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1729b0d93ad5SChuck Lever 	}
1730b0d93ad5SChuck Lever 
173101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
173201d37c42STrond Myklebust 		return -ENOTCONN;
173301d37c42STrond Myklebust 
1734b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1735262ca07dSChuck Lever 	xprt->stat.connect_count++;
1736262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
173795392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
173816be2d20SChuck Lever }
173916be2d20SChuck Lever 
174016be2d20SChuck Lever /**
1741*b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1742*b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1743*b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1744*b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
174516be2d20SChuck Lever  *
174616be2d20SChuck Lever  * Invoked by a work queue tasklet.
174716be2d20SChuck Lever  */
1748*b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1749*b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1750*b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1751*b61d59ffSTrond Myklebust 			struct sock_xprt *))
175216be2d20SChuck Lever {
175316be2d20SChuck Lever 	struct socket *sock = transport->sock;
1754*b61d59ffSTrond Myklebust 	int status = -EIO;
175516be2d20SChuck Lever 
175601d37c42STrond Myklebust 	if (xprt->shutdown)
175716be2d20SChuck Lever 		goto out;
175816be2d20SChuck Lever 
175916be2d20SChuck Lever 	if (!sock) {
17607d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1761*b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1762*b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1763*b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
176416be2d20SChuck Lever 			goto out;
176516be2d20SChuck Lever 		}
17667d1e8255STrond Myklebust 	} else {
17677d1e8255STrond Myklebust 		int abort_and_exit;
17687d1e8255STrond Myklebust 
17697d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
17707d1e8255STrond Myklebust 				&xprt->state);
177116be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
177240d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
177316be2d20SChuck Lever 
17747d1e8255STrond Myklebust 		if (abort_and_exit)
17757d1e8255STrond Myklebust 			goto out_eagain;
17767d1e8255STrond Myklebust 	}
17777d1e8255STrond Myklebust 
177816be2d20SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
177916be2d20SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
178016be2d20SChuck Lever 
178116be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1782a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
178346121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
178446121cf7SChuck Lever 			sock->sk->sk_state);
1785a246b010SChuck Lever 	switch (status) {
17868a2cec29STrond Myklebust 	case -ECONNREFUSED:
17878a2cec29STrond Myklebust 	case -ECONNRESET:
17888a2cec29STrond Myklebust 	case -ENETUNREACH:
17898a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
17902a491991STrond Myklebust 	case 0:
1791a246b010SChuck Lever 	case -EINPROGRESS:
1792a246b010SChuck Lever 	case -EALREADY:
17937d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
17947d1e8255STrond Myklebust 		return;
17958a2cec29STrond Myklebust 	}
17963167e12cSChuck Lever 	/* get rid of existing socket, and retry */
1797e06799f9STrond Myklebust 	xs_tcp_shutdown(xprt);
17982a491991STrond Myklebust 	printk("%s: connect returned unhandled error %d\n",
17992a491991STrond Myklebust 			__func__, status);
18007d1e8255STrond Myklebust out_eagain:
18012a491991STrond Myklebust 	status = -EAGAIN;
1802a246b010SChuck Lever out:
18032226feb6SChuck Lever 	xprt_clear_connecting(xprt);
18047d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1805a246b010SChuck Lever }
1806a246b010SChuck Lever 
1807*b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1808*b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1809*b61d59ffSTrond Myklebust {
1810*b61d59ffSTrond Myklebust 	struct socket *sock;
1811*b61d59ffSTrond Myklebust 	int err;
1812*b61d59ffSTrond Myklebust 
1813*b61d59ffSTrond Myklebust 	/* start from scratch */
1814*b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1815*b61d59ffSTrond Myklebust 	if (err < 0) {
1816*b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1817*b61d59ffSTrond Myklebust 				-err);
1818*b61d59ffSTrond Myklebust 		goto out_err;
1819*b61d59ffSTrond Myklebust 	}
1820*b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1821*b61d59ffSTrond Myklebust 
1822*b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1823*b61d59ffSTrond Myklebust 		sock_release(sock);
1824*b61d59ffSTrond Myklebust 		goto out_err;
1825*b61d59ffSTrond Myklebust 	}
1826*b61d59ffSTrond Myklebust 	return sock;
1827*b61d59ffSTrond Myklebust out_err:
1828*b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1829*b61d59ffSTrond Myklebust }
1830*b61d59ffSTrond Myklebust 
1831*b61d59ffSTrond Myklebust /**
1832*b61d59ffSTrond Myklebust  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1833*b61d59ffSTrond Myklebust  * @work: RPC transport to connect
1834*b61d59ffSTrond Myklebust  *
1835*b61d59ffSTrond Myklebust  * Invoked by a work queue tasklet.
1836*b61d59ffSTrond Myklebust  */
1837*b61d59ffSTrond Myklebust static void xs_tcp_connect_worker4(struct work_struct *work)
1838*b61d59ffSTrond Myklebust {
1839*b61d59ffSTrond Myklebust 	struct sock_xprt *transport =
1840*b61d59ffSTrond Myklebust 		container_of(work, struct sock_xprt, connect_worker.work);
1841*b61d59ffSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1842*b61d59ffSTrond Myklebust 
1843*b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1844*b61d59ffSTrond Myklebust }
1845*b61d59ffSTrond Myklebust 
1846*b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1847*b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1848*b61d59ffSTrond Myklebust {
1849*b61d59ffSTrond Myklebust 	struct socket *sock;
1850*b61d59ffSTrond Myklebust 	int err;
1851*b61d59ffSTrond Myklebust 
1852*b61d59ffSTrond Myklebust 	/* start from scratch */
1853*b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1854*b61d59ffSTrond Myklebust 	if (err < 0) {
1855*b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1856*b61d59ffSTrond Myklebust 				-err);
1857*b61d59ffSTrond Myklebust 		goto out_err;
1858*b61d59ffSTrond Myklebust 	}
1859*b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
1860*b61d59ffSTrond Myklebust 
1861*b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1862*b61d59ffSTrond Myklebust 		sock_release(sock);
1863*b61d59ffSTrond Myklebust 		goto out_err;
1864*b61d59ffSTrond Myklebust 	}
1865*b61d59ffSTrond Myklebust 	return sock;
1866*b61d59ffSTrond Myklebust out_err:
1867*b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1868*b61d59ffSTrond Myklebust }
1869*b61d59ffSTrond Myklebust 
18709903cd1cSChuck Lever /**
187168e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
187268e220bdSChuck Lever  * @work: RPC transport to connect
187368e220bdSChuck Lever  *
187468e220bdSChuck Lever  * Invoked by a work queue tasklet.
187568e220bdSChuck Lever  */
187668e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
187768e220bdSChuck Lever {
187868e220bdSChuck Lever 	struct sock_xprt *transport =
187968e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
188068e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
188168e220bdSChuck Lever 
1882*b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
188368e220bdSChuck Lever }
188468e220bdSChuck Lever 
188568e220bdSChuck Lever /**
18869903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
18879903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
18889903cd1cSChuck Lever  *
18899903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
189003bf4b70SChuck Lever  *
189103bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
189203bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
189303bf4b70SChuck Lever  * socket on a privileged port.
189403bf4b70SChuck Lever  *
189503bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
189603bf4b70SChuck Lever  * retry floods (hard mounts).
18979903cd1cSChuck Lever  */
18989903cd1cSChuck Lever static void xs_connect(struct rpc_task *task)
1899a246b010SChuck Lever {
1900a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
1901ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1902a246b010SChuck Lever 
1903b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
1904b0d93ad5SChuck Lever 		return;
1905b0d93ad5SChuck Lever 
1906ee0ac0c2SChuck Lever 	if (transport->sock != NULL) {
190746121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
190846121cf7SChuck Lever 				"seconds\n",
190903bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
1910c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1911c1384c9cSTrond Myklebust 				   &transport->connect_worker,
191203bf4b70SChuck Lever 				   xprt->reestablish_timeout);
191303bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
191403bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
191503bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
19169903cd1cSChuck Lever 	} else {
19179903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
1918c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1919c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
1920a246b010SChuck Lever 	}
1921a246b010SChuck Lever }
1922a246b010SChuck Lever 
1923e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
1924e06799f9STrond Myklebust {
1925e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1926e06799f9STrond Myklebust 
1927e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
1928e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
1929e06799f9STrond Myklebust 		return;
1930e06799f9STrond Myklebust 	xs_connect(task);
1931e06799f9STrond Myklebust }
1932e06799f9STrond Myklebust 
1933262ca07dSChuck Lever /**
1934262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
1935262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
1936262ca07dSChuck Lever  * @seq: output file
1937262ca07dSChuck Lever  *
1938262ca07dSChuck Lever  */
1939262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1940262ca07dSChuck Lever {
1941c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1942c8475461SChuck Lever 
1943262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1944c8475461SChuck Lever 			transport->port,
1945262ca07dSChuck Lever 			xprt->stat.bind_count,
1946262ca07dSChuck Lever 			xprt->stat.sends,
1947262ca07dSChuck Lever 			xprt->stat.recvs,
1948262ca07dSChuck Lever 			xprt->stat.bad_xids,
1949262ca07dSChuck Lever 			xprt->stat.req_u,
1950262ca07dSChuck Lever 			xprt->stat.bklog_u);
1951262ca07dSChuck Lever }
1952262ca07dSChuck Lever 
1953262ca07dSChuck Lever /**
1954262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
1955262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
1956262ca07dSChuck Lever  * @seq: output file
1957262ca07dSChuck Lever  *
1958262ca07dSChuck Lever  */
1959262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1960262ca07dSChuck Lever {
1961c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1962262ca07dSChuck Lever 	long idle_time = 0;
1963262ca07dSChuck Lever 
1964262ca07dSChuck Lever 	if (xprt_connected(xprt))
1965262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
1966262ca07dSChuck Lever 
1967262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1968c8475461SChuck Lever 			transport->port,
1969262ca07dSChuck Lever 			xprt->stat.bind_count,
1970262ca07dSChuck Lever 			xprt->stat.connect_count,
1971262ca07dSChuck Lever 			xprt->stat.connect_time,
1972262ca07dSChuck Lever 			idle_time,
1973262ca07dSChuck Lever 			xprt->stat.sends,
1974262ca07dSChuck Lever 			xprt->stat.recvs,
1975262ca07dSChuck Lever 			xprt->stat.bad_xids,
1976262ca07dSChuck Lever 			xprt->stat.req_u,
1977262ca07dSChuck Lever 			xprt->stat.bklog_u);
1978262ca07dSChuck Lever }
1979262ca07dSChuck Lever 
1980262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
198143118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
198212a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
198349e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
198445160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
198592200412SChuck Lever 	.set_port		= xs_set_port,
19869903cd1cSChuck Lever 	.connect		= xs_connect,
198702107148SChuck Lever 	.buf_alloc		= rpc_malloc,
198802107148SChuck Lever 	.buf_free		= rpc_free,
1989262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
1990fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
199146c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
1992a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
1993262965f5SChuck Lever 	.close			= xs_close,
1994262965f5SChuck Lever 	.destroy		= xs_destroy,
1995262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
1996262965f5SChuck Lever };
1997262965f5SChuck Lever 
1998262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
199912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2000e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
200145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
200292200412SChuck Lever 	.set_port		= xs_set_port,
2003e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
200402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
200502107148SChuck Lever 	.buf_free		= rpc_free,
2006262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2007fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2008e06799f9STrond Myklebust 	.close			= xs_tcp_shutdown,
20099903cd1cSChuck Lever 	.destroy		= xs_destroy,
2010262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2011a246b010SChuck Lever };
2012a246b010SChuck Lever 
20133c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2014bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2015c8541ecdSChuck Lever {
2016c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2017ffc2e518SChuck Lever 	struct sock_xprt *new;
2018c8541ecdSChuck Lever 
201996802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2020c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2021c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2022c8541ecdSChuck Lever 	}
2023c8541ecdSChuck Lever 
2024ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2025ffc2e518SChuck Lever 	if (new == NULL) {
202646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
202746121cf7SChuck Lever 				"rpc_xprt\n");
2028c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2029c8541ecdSChuck Lever 	}
2030ffc2e518SChuck Lever 	xprt = &new->xprt;
2031c8541ecdSChuck Lever 
2032c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2033c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2034c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2035c8541ecdSChuck Lever 		kfree(xprt);
203646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
203746121cf7SChuck Lever 				"table\n");
2038c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2039c8541ecdSChuck Lever 	}
2040c8541ecdSChuck Lever 
204196802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
204296802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2043d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2044d3bc9a1dSFrank van Maarseveen 		memcpy(&new->addr, args->srcaddr, args->addrlen);
2045c8541ecdSChuck Lever 
2046c8541ecdSChuck Lever 	return xprt;
2047c8541ecdSChuck Lever }
2048c8541ecdSChuck Lever 
20492881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
20502881ae74STrond Myklebust 	.to_initval = 5 * HZ,
20512881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
20522881ae74STrond Myklebust 	.to_increment = 5 * HZ,
20532881ae74STrond Myklebust 	.to_retries = 5,
20542881ae74STrond Myklebust };
20552881ae74STrond Myklebust 
20569903cd1cSChuck Lever /**
20579903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
205896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
20599903cd1cSChuck Lever  *
20609903cd1cSChuck Lever  */
2061483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2062a246b010SChuck Lever {
20638f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2064c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2065c8475461SChuck Lever 	struct sock_xprt *transport;
2066a246b010SChuck Lever 
206796802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2068c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2069c8541ecdSChuck Lever 		return xprt;
2070c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2071a246b010SChuck Lever 
2072ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2073808012fbSChuck Lever 	xprt->tsh_size = 0;
2074a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2075a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2076a246b010SChuck Lever 
207703bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
207803bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
207903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
208003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2081a246b010SChuck Lever 
2082262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2083a246b010SChuck Lever 
2084ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2085a246b010SChuck Lever 
20868f9d5b1aSChuck Lever 	switch (addr->sa_family) {
20878f9d5b1aSChuck Lever 	case AF_INET:
20888f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
20898f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
20908f9d5b1aSChuck Lever 
20918f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
20928f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
2093b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
20948f9d5b1aSChuck Lever 		break;
20958f9d5b1aSChuck Lever 	case AF_INET6:
20968f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
20978f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
20988f9d5b1aSChuck Lever 
20998f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
21008f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
2101b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
21028f9d5b1aSChuck Lever 		break;
21038f9d5b1aSChuck Lever 	default:
21048f9d5b1aSChuck Lever 		kfree(xprt);
21058f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
21068f9d5b1aSChuck Lever 	}
21078f9d5b1aSChuck Lever 
2108edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
21097559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2110edb267a6SChuck Lever 
2111bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2112c8541ecdSChuck Lever 		return xprt;
2113bc25571eS\"Talpey, Thomas\ 
2114bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2115bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2116bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2117a246b010SChuck Lever }
2118a246b010SChuck Lever 
21192881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
21202881ae74STrond Myklebust 	.to_initval = 60 * HZ,
21212881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
21222881ae74STrond Myklebust 	.to_retries = 2,
21232881ae74STrond Myklebust };
21242881ae74STrond Myklebust 
21259903cd1cSChuck Lever /**
21269903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
212796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21289903cd1cSChuck Lever  *
21299903cd1cSChuck Lever  */
2130483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2131a246b010SChuck Lever {
21328f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2133c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2134c8475461SChuck Lever 	struct sock_xprt *transport;
2135a246b010SChuck Lever 
213696802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2137c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2138c8541ecdSChuck Lever 		return xprt;
2139c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2140a246b010SChuck Lever 
2141ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2142808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2143808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2144a246b010SChuck Lever 
214503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
214603bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
214703bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
214803bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2149a246b010SChuck Lever 
2150262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2151ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2152a246b010SChuck Lever 
21538f9d5b1aSChuck Lever 	switch (addr->sa_family) {
21548f9d5b1aSChuck Lever 	case AF_INET:
21558f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
21568f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21578f9d5b1aSChuck Lever 
21588f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2159b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
21608f9d5b1aSChuck Lever 		break;
21618f9d5b1aSChuck Lever 	case AF_INET6:
21628f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
21638f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
21648f9d5b1aSChuck Lever 
21658f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2166b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
21678f9d5b1aSChuck Lever 		break;
21688f9d5b1aSChuck Lever 	default:
21698f9d5b1aSChuck Lever 		kfree(xprt);
21708f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
21718f9d5b1aSChuck Lever 	}
21728f9d5b1aSChuck Lever 
2173edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
21747559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2175edb267a6SChuck Lever 
2176bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2177c8541ecdSChuck Lever 		return xprt;
2178bc25571eS\"Talpey, Thomas\ 
2179bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2180bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2181bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2182a246b010SChuck Lever }
2183282b32e1SChuck Lever 
2184bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2185bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2186bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2187bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
21884fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_UDP,
2189bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2190bc25571eS\"Talpey, Thomas\ };
2191bc25571eS\"Talpey, Thomas\ 
2192bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2193bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2194bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2195bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
21964fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_TCP,
2197bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2198bc25571eS\"Talpey, Thomas\ };
2199bc25571eS\"Talpey, Thomas\ 
2200282b32e1SChuck Lever /**
2201bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2202282b32e1SChuck Lever  *
2203282b32e1SChuck Lever  */
2204282b32e1SChuck Lever int init_socket_xprt(void)
2205282b32e1SChuck Lever {
2206fbf76683SChuck Lever #ifdef RPC_DEBUG
22072b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
22080b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2209fbf76683SChuck Lever #endif
2210fbf76683SChuck Lever 
2211bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2212bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2213bc25571eS\"Talpey, Thomas\ 
2214282b32e1SChuck Lever 	return 0;
2215282b32e1SChuck Lever }
2216282b32e1SChuck Lever 
2217282b32e1SChuck Lever /**
2218bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2219282b32e1SChuck Lever  *
2220282b32e1SChuck Lever  */
2221282b32e1SChuck Lever void cleanup_socket_xprt(void)
2222282b32e1SChuck Lever {
2223fbf76683SChuck Lever #ifdef RPC_DEBUG
2224fbf76683SChuck Lever 	if (sunrpc_table_header) {
2225fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2226fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2227fbf76683SChuck Lever 	}
2228fbf76683SChuck Lever #endif
2229bc25571eS\"Talpey, Thomas\ 
2230bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2231bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2232282b32e1SChuck Lever }
2233