xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 3851f1cdb2b8d507b10395fc110d4c37d6121285)
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>
22176e21eeSChuck Lever #include <linux/string.h>
23a246b010SChuck Lever #include <linux/slab.h>
24bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
25a246b010SChuck Lever #include <linux/capability.h>
26a246b010SChuck Lever #include <linux/pagemap.h>
27a246b010SChuck Lever #include <linux/errno.h>
28a246b010SChuck Lever #include <linux/socket.h>
29a246b010SChuck Lever #include <linux/in.h>
30a246b010SChuck Lever #include <linux/net.h>
31a246b010SChuck Lever #include <linux/mm.h>
32176e21eeSChuck Lever #include <linux/un.h>
33a246b010SChuck Lever #include <linux/udp.h>
34a246b010SChuck Lever #include <linux/tcp.h>
35a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
365976687aSJeff Layton #include <linux/sunrpc/addr.h>
3702107148SChuck Lever #include <linux/sunrpc/sched.h>
384cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
3949c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
40a246b010SChuck Lever #include <linux/file.h>
419e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4244b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4344b98efdSRicardo Labiaga #endif
44a246b010SChuck Lever 
45a246b010SChuck Lever #include <net/sock.h>
46a246b010SChuck Lever #include <net/checksum.h>
47a246b010SChuck Lever #include <net/udp.h>
48a246b010SChuck Lever #include <net/tcp.h>
49a246b010SChuck Lever 
5040b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5140b5ea0cSTrond Myklebust 
524cfc7e60SRahul Iyer #include "sunrpc.h"
53176e21eeSChuck Lever 
54176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
55176e21eeSChuck Lever 
569903cd1cSChuck Lever /*
57c556b754SChuck Lever  * xprtsock tunables
58c556b754SChuck Lever  */
5909acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6009acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
6109acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
62c556b754SChuck Lever 
6309acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
6409acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
65c556b754SChuck Lever 
6654c09874STrond Myklebust #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
6754c09874STrond Myklebust 
687d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
6925fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
707d1e8255STrond Myklebust 
71c556b754SChuck Lever /*
72fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
73fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
74fbf76683SChuck Lever  * directory become the union of all files registered there.
75fbf76683SChuck Lever  *
76fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
77fbf76683SChuck Lever  * someone else's file names!
78fbf76683SChuck Lever  */
79fbf76683SChuck Lever 
80fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
82d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
83fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
85fbf76683SChuck Lever 
86fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
87fbf76683SChuck Lever 
88fbf76683SChuck Lever /*
89fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
90fbf76683SChuck Lever  *        socket buffers for existing UDP transports
91fbf76683SChuck Lever  */
92fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
93fbf76683SChuck Lever 	{
94fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
95fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
96fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
97fbf76683SChuck Lever 		.mode		= 0644,
986d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
99fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
100fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
101fbf76683SChuck Lever 	},
102fbf76683SChuck Lever 	{
103fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
104fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
105fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
106fbf76683SChuck Lever 		.mode		= 0644,
1076d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
108fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
109fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
110fbf76683SChuck Lever 	},
111fbf76683SChuck Lever 	{
112d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
113d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
114d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
115d9ba131dSTrond Myklebust 		.mode		= 0644,
116d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
117d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
118d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
119d9ba131dSTrond Myklebust 	},
120d9ba131dSTrond Myklebust 	{
121fbf76683SChuck Lever 		.procname	= "min_resvport",
122fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
123fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
124fbf76683SChuck Lever 		.mode		= 0644,
1256d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
126fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
127e08ea3a9SFrank Sorenson 		.extra2		= &xprt_max_resvport
128fbf76683SChuck Lever 	},
129fbf76683SChuck Lever 	{
130fbf76683SChuck Lever 		.procname	= "max_resvport",
131fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
132fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
133fbf76683SChuck Lever 		.mode		= 0644,
1346d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
135e08ea3a9SFrank Sorenson 		.extra1		= &xprt_min_resvport,
136fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
137fbf76683SChuck Lever 	},
138fbf76683SChuck Lever 	{
13925fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
14025fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
14125fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
14225fe6142STrond Myklebust 		.mode		= 0644,
1436d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
14425fe6142STrond Myklebust 	},
145f8572d8fSEric W. Biederman 	{ },
146fbf76683SChuck Lever };
147fbf76683SChuck Lever 
148fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
149fbf76683SChuck Lever 	{
150fbf76683SChuck Lever 		.procname	= "sunrpc",
151fbf76683SChuck Lever 		.mode		= 0555,
152fbf76683SChuck Lever 		.child		= xs_tunables_table
153fbf76683SChuck Lever 	},
154f8572d8fSEric W. Biederman 	{ },
155fbf76683SChuck Lever };
156fbf76683SChuck Lever 
157fbf76683SChuck Lever #endif
158fbf76683SChuck Lever 
159fbf76683SChuck Lever /*
16003bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16103bf4b70SChuck Lever  */
16203bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16303bf4b70SChuck Lever 
16403bf4b70SChuck Lever /*
16503bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
16603bf4b70SChuck Lever  * kind of resource problem on the local host.
16703bf4b70SChuck Lever  */
16803bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
16903bf4b70SChuck Lever 
17003bf4b70SChuck Lever /*
17103bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17203bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17303bf4b70SChuck Lever  *
17403bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17503bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
17603bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
17703bf4b70SChuck Lever  * increase over time if the server is down or not responding.
17803bf4b70SChuck Lever  */
17903bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18003bf4b70SChuck Lever 
18103bf4b70SChuck Lever /*
18203bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18303bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18403bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18503bf4b70SChuck Lever  */
18603bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
18703bf4b70SChuck Lever 
188f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
189a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1909903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
191a246b010SChuck Lever #endif
192a246b010SChuck Lever 
193a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1949903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
195a246b010SChuck Lever {
196a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
197a246b010SChuck Lever 	int j;
198a246b010SChuck Lever 
199a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
200a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
201a246b010SChuck Lever 		if (!(j & 31)) {
202a246b010SChuck Lever 			if (j)
203a246b010SChuck Lever 				dprintk("\n");
204a246b010SChuck Lever 			dprintk("0x%04x ", j);
205a246b010SChuck Lever 		}
206a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
207a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
208a246b010SChuck Lever 	}
209a246b010SChuck Lever 	dprintk("\n");
210a246b010SChuck Lever }
211a246b010SChuck Lever #else
2129903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
213a246b010SChuck Lever {
214a246b010SChuck Lever 	/* NOP */
215a246b010SChuck Lever }
216a246b010SChuck Lever #endif
217a246b010SChuck Lever 
2182118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2192118071dSTrond Myklebust {
2202118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2212118071dSTrond Myklebust }
2222118071dSTrond Myklebust 
22395392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
224edb267a6SChuck Lever {
22595392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
22695392c59SChuck Lever }
22795392c59SChuck Lever 
228176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
229176e21eeSChuck Lever {
230176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
231176e21eeSChuck Lever }
232176e21eeSChuck Lever 
23395392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23495392c59SChuck Lever {
23595392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
23695392c59SChuck Lever }
23795392c59SChuck Lever 
23895392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
23995392c59SChuck Lever {
24095392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24195392c59SChuck Lever }
24295392c59SChuck Lever 
243c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
244c877b849SChuck Lever {
245c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2469dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2479dc3b095SChuck Lever 	struct sockaddr_in *sin;
248176e21eeSChuck Lever 	struct sockaddr_un *sun;
249c877b849SChuck Lever 	char buf[128];
250c877b849SChuck Lever 
2519dc3b095SChuck Lever 	switch (sap->sa_family) {
252176e21eeSChuck Lever 	case AF_LOCAL:
253176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
254176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
255176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
256176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
257176e21eeSChuck Lever 		break;
2589dc3b095SChuck Lever 	case AF_INET:
259176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
260176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
261176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2629dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
263fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2649dc3b095SChuck Lever 		break;
2659dc3b095SChuck Lever 	case AF_INET6:
266176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
267176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
268176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2699dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
270fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2719dc3b095SChuck Lever 		break;
2729dc3b095SChuck Lever 	default:
2739dc3b095SChuck Lever 		BUG();
2749dc3b095SChuck Lever 	}
275176e21eeSChuck Lever 
2769dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2779dc3b095SChuck Lever }
2789dc3b095SChuck Lever 
2799dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2809dc3b095SChuck Lever {
2819dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2829dc3b095SChuck Lever 	char buf[128];
2839dc3b095SChuck Lever 
28481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
285c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
286c877b849SChuck Lever 
28781160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
288c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
289c877b849SChuck Lever }
290c877b849SChuck Lever 
2919dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
292b454ae90SChuck Lever 				     const char *protocol,
293b454ae90SChuck Lever 				     const char *netid)
294edb267a6SChuck Lever {
295b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
296b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
297c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
2989dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
299edb267a6SChuck Lever }
300edb267a6SChuck Lever 
3019dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3024b6473fbSChuck Lever {
3039dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3049dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3054b6473fbSChuck Lever 
3069dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
307edb267a6SChuck Lever }
308edb267a6SChuck Lever 
309edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
310edb267a6SChuck Lever {
31133e01dc7SChuck Lever 	unsigned int i;
31233e01dc7SChuck Lever 
31333e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
31433e01dc7SChuck Lever 		switch (i) {
31533e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
31633e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
31733e01dc7SChuck Lever 			continue;
31833e01dc7SChuck Lever 		default:
31933e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32033e01dc7SChuck Lever 		}
321edb267a6SChuck Lever }
322edb267a6SChuck Lever 
323b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
324b4b5cc85SChuck Lever 
32524c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
326b4b5cc85SChuck Lever {
327b4b5cc85SChuck Lever 	struct msghdr msg = {
328b4b5cc85SChuck Lever 		.msg_name	= addr,
329b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
33024c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
33124c5684bSTrond Myklebust 	};
33224c5684bSTrond Myklebust 	struct kvec iov = {
33324c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
33424c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
335b4b5cc85SChuck Lever 	};
336b4b5cc85SChuck Lever 
33724c5684bSTrond Myklebust 	if (iov.iov_len != 0)
338b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
339b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
340b4b5cc85SChuck Lever }
341b4b5cc85SChuck Lever 
342f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
343b4b5cc85SChuck Lever {
344a6b31d18STrond Myklebust 	ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
345a6b31d18STrond Myklebust 			int offset, size_t size, int flags);
34624c5684bSTrond Myklebust 	struct page **ppage;
34724c5684bSTrond Myklebust 	unsigned int remainder;
348f279cd00SJason Baron 	int err;
349b4b5cc85SChuck Lever 
35024c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
35124c5684bSTrond Myklebust 	base += xdr->page_base;
35224c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
35324c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
354a6b31d18STrond Myklebust 	do_sendpage = sock->ops->sendpage;
355a6b31d18STrond Myklebust 	if (!zerocopy)
356a6b31d18STrond Myklebust 		do_sendpage = sock_no_sendpage;
35724c5684bSTrond Myklebust 	for(;;) {
35824c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
35924c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
36024c5684bSTrond Myklebust 
36124c5684bSTrond Myklebust 		remainder -= len;
36231303d6cSTrond Myklebust 		if (more)
36324c5684bSTrond Myklebust 			flags |= MSG_MORE;
36431303d6cSTrond Myklebust 		if (remainder != 0)
36531303d6cSTrond Myklebust 			flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
366a6b31d18STrond Myklebust 		err = do_sendpage(sock, *ppage, base, len, flags);
36724c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
36824c5684bSTrond Myklebust 			break;
369f279cd00SJason Baron 		*sent_p += err;
37024c5684bSTrond Myklebust 		ppage++;
37124c5684bSTrond Myklebust 		base = 0;
37224c5684bSTrond Myklebust 	}
373f279cd00SJason Baron 	if (err > 0) {
374f279cd00SJason Baron 		*sent_p += err;
375f279cd00SJason Baron 		err = 0;
376f279cd00SJason Baron 	}
37724c5684bSTrond Myklebust 	return err;
378b4b5cc85SChuck Lever }
379b4b5cc85SChuck Lever 
3809903cd1cSChuck Lever /**
3819903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
3829903cd1cSChuck Lever  * @sock: socket to send on
3839903cd1cSChuck Lever  * @addr: UDP only -- address of destination
3849903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
3859903cd1cSChuck Lever  * @xdr: buffer containing this request
3869903cd1cSChuck Lever  * @base: starting position in the buffer
387a6b31d18STrond Myklebust  * @zerocopy: true if it is safe to use sendpage()
388f279cd00SJason Baron  * @sent_p: return the total number of bytes successfully queued for sending
3899903cd1cSChuck Lever  *
390a246b010SChuck Lever  */
391f279cd00SJason Baron static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
392a246b010SChuck Lever {
39324c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
394f279cd00SJason Baron 	int err = 0;
395f279cd00SJason Baron 	int sent = 0;
396a246b010SChuck Lever 
397262965f5SChuck Lever 	if (unlikely(!sock))
398fba91afbSTrond Myklebust 		return -ENOTSOCK;
399262965f5SChuck Lever 
40024c5684bSTrond Myklebust 	if (base != 0) {
40124c5684bSTrond Myklebust 		addr = NULL;
40224c5684bSTrond Myklebust 		addrlen = 0;
40324c5684bSTrond Myklebust 	}
404262965f5SChuck Lever 
40524c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
40624c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
40724c5684bSTrond Myklebust 		remainder -= len;
40824c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
40924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
410a246b010SChuck Lever 			goto out;
411f279cd00SJason Baron 		*sent_p += err;
412a246b010SChuck Lever 		base = 0;
413a246b010SChuck Lever 	} else
41424c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
415a246b010SChuck Lever 
41624c5684bSTrond Myklebust 	if (base < xdr->page_len) {
41724c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
41824c5684bSTrond Myklebust 		remainder -= len;
419f279cd00SJason Baron 		err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
420f279cd00SJason Baron 		*sent_p += sent;
421f279cd00SJason Baron 		if (remainder == 0 || sent != len)
422a246b010SChuck Lever 			goto out;
423a246b010SChuck Lever 		base = 0;
42424c5684bSTrond Myklebust 	} else
42524c5684bSTrond Myklebust 		base -= xdr->page_len;
42624c5684bSTrond Myklebust 
42724c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
428f279cd00SJason Baron 		return 0;
42924c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
430a246b010SChuck Lever out:
431f279cd00SJason Baron 	if (err > 0) {
432f279cd00SJason Baron 		*sent_p += err;
433f279cd00SJason Baron 		err = 0;
434f279cd00SJason Baron 	}
43524c5684bSTrond Myklebust 	return err;
436a246b010SChuck Lever }
437a246b010SChuck Lever 
438b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
439b6ddf64fSTrond Myklebust {
440b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
441b6ddf64fSTrond Myklebust 
442b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
443b6ddf64fSTrond Myklebust }
444b6ddf64fSTrond Myklebust 
4459903cd1cSChuck Lever /**
446262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
447262965f5SChuck Lever  * @task: task to put to sleep
4489903cd1cSChuck Lever  *
449a246b010SChuck Lever  */
4505e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
451a246b010SChuck Lever {
452262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
453262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
454ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
45506ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
45624ca9a84STrond Myklebust 	int ret = -EAGAIN;
457a246b010SChuck Lever 
45846121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
459262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
460262965f5SChuck Lever 			req->rq_slen);
461a246b010SChuck Lever 
462262965f5SChuck Lever 	/* Protect against races with write_space */
463262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
464a246b010SChuck Lever 
465262965f5SChuck Lever 	/* Don't race with disconnect */
466b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
46713331a55STrond Myklebust 		/* wait for more buffer space */
46806ea0bfeSTrond Myklebust 		sk->sk_write_pending++;
469b6ddf64fSTrond Myklebust 		xprt_wait_for_buffer_space(task, xs_nospace_callback);
47013331a55STrond Myklebust 	} else
4715e3771ceSTrond Myklebust 		ret = -ENOTCONN;
472a246b010SChuck Lever 
473262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
47406ea0bfeSTrond Myklebust 
47506ea0bfeSTrond Myklebust 	/* Race breaker in case memory is freed before above code is called */
47606ea0bfeSTrond Myklebust 	sk->sk_write_space(sk);
4775e3771ceSTrond Myklebust 	return ret;
478a246b010SChuck Lever }
479a246b010SChuck Lever 
48061677eeeSChuck Lever /*
48161677eeeSChuck Lever  * Construct a stream transport record marker in @buf.
48261677eeeSChuck Lever  */
48361677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
48461677eeeSChuck Lever {
48561677eeeSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
48661677eeeSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
48761677eeeSChuck Lever 	*base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
48861677eeeSChuck Lever }
48961677eeeSChuck Lever 
4909903cd1cSChuck Lever /**
491176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
492176e21eeSChuck Lever  * @task: RPC task that manages the state of an RPC request
493176e21eeSChuck Lever  *
494176e21eeSChuck Lever  * Return values:
495176e21eeSChuck Lever  *        0:	The request has been sent
496176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
497176e21eeSChuck Lever  *		complete the request
498176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
499176e21eeSChuck Lever  *    other:	Some other error occured, the request was not sent
500176e21eeSChuck Lever  */
501176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task)
502176e21eeSChuck Lever {
503176e21eeSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
504176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
505176e21eeSChuck Lever 	struct sock_xprt *transport =
506176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
507176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
508176e21eeSChuck Lever 	int status;
509f279cd00SJason Baron 	int sent = 0;
510176e21eeSChuck Lever 
511176e21eeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
512176e21eeSChuck Lever 
513176e21eeSChuck Lever 	xs_pktdump("packet data:",
514176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
515176e21eeSChuck Lever 
516f279cd00SJason Baron 	status = xs_sendpages(transport->sock, NULL, 0, xdr, req->rq_bytes_sent,
517f279cd00SJason Baron 			      true, &sent);
518176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
519176e21eeSChuck Lever 			__func__, xdr->len - req->rq_bytes_sent, status);
520743c69e7SNeilBrown 
521743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
522743c69e7SNeilBrown 		status = -ENOBUFS;
523743c69e7SNeilBrown 
524f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
525f279cd00SJason Baron 		req->rq_bytes_sent += sent;
526f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
527176e21eeSChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
528176e21eeSChuck Lever 			req->rq_bytes_sent = 0;
529176e21eeSChuck Lever 			return 0;
530176e21eeSChuck Lever 		}
531176e21eeSChuck Lever 		status = -EAGAIN;
532176e21eeSChuck Lever 	}
533176e21eeSChuck Lever 
534176e21eeSChuck Lever 	switch (status) {
5353601c4a9STrond Myklebust 	case -ENOBUFS:
536b5872f0cSTrond Myklebust 		break;
537176e21eeSChuck Lever 	case -EAGAIN:
538176e21eeSChuck Lever 		status = xs_nospace(task);
539176e21eeSChuck Lever 		break;
540176e21eeSChuck Lever 	default:
541176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
542176e21eeSChuck Lever 			-status);
543176e21eeSChuck Lever 	case -EPIPE:
544176e21eeSChuck Lever 		xs_close(xprt);
545176e21eeSChuck Lever 		status = -ENOTCONN;
546176e21eeSChuck Lever 	}
547176e21eeSChuck Lever 
548176e21eeSChuck Lever 	return status;
549176e21eeSChuck Lever }
550176e21eeSChuck Lever 
551176e21eeSChuck Lever /**
552262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5539903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5549903cd1cSChuck Lever  *
5559903cd1cSChuck Lever  * Return values:
5569903cd1cSChuck Lever  *        0:	The request has been sent
5579903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5589903cd1cSChuck Lever  *		complete the request
559262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
56025985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
5619903cd1cSChuck Lever  */
562262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
563a246b010SChuck Lever {
564a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
565a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
566ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
567262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
568f279cd00SJason Baron 	int sent = 0;
569262965f5SChuck Lever 	int status;
570262965f5SChuck Lever 
571262965f5SChuck Lever 	xs_pktdump("packet data:",
572262965f5SChuck Lever 				req->rq_svec->iov_base,
573262965f5SChuck Lever 				req->rq_svec->iov_len);
574262965f5SChuck Lever 
57501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
57601d37c42STrond Myklebust 		return -ENOTCONN;
577f279cd00SJason Baron 	status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
578f279cd00SJason Baron 			      xdr, req->rq_bytes_sent, true, &sent);
579262965f5SChuck Lever 
580262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
581262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
582262965f5SChuck Lever 
5833dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
5843dedbb5cSJason Baron 	if (status == -EPERM)
5853dedbb5cSJason Baron 		goto process_status;
5863dedbb5cSJason Baron 
587743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
588743c69e7SNeilBrown 		status = -ENOBUFS;
589743c69e7SNeilBrown 
590f279cd00SJason Baron 	if (sent > 0 || status == 0) {
591f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
592f279cd00SJason Baron 		if (sent >= req->rq_slen)
593262965f5SChuck Lever 			return 0;
594262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
595262965f5SChuck Lever 		status = -EAGAIN;
5962199700fSTrond Myklebust 	}
597262965f5SChuck Lever 
5983dedbb5cSJason Baron process_status:
599262965f5SChuck Lever 	switch (status) {
600fba91afbSTrond Myklebust 	case -ENOTSOCK:
601fba91afbSTrond Myklebust 		status = -ENOTCONN;
602fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
603fba91afbSTrond Myklebust 		break;
604b6ddf64fSTrond Myklebust 	case -EAGAIN:
6055e3771ceSTrond Myklebust 		status = xs_nospace(task);
606b6ddf64fSTrond Myklebust 		break;
607262965f5SChuck Lever 	case -ENETUNREACH:
6083601c4a9STrond Myklebust 	case -ENOBUFS:
609262965f5SChuck Lever 	case -EPIPE:
610262965f5SChuck Lever 	case -ECONNREFUSED:
6113dedbb5cSJason Baron 	case -EPERM:
612262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
613262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
61413331a55STrond Myklebust 		break;
61513331a55STrond Myklebust 	default:
61613331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
61713331a55STrond Myklebust 			-status);
618262965f5SChuck Lever 	}
6195fe46e9dSBian Naimeng 
620262965f5SChuck Lever 	return status;
621262965f5SChuck Lever }
622262965f5SChuck Lever 
623e06799f9STrond Myklebust /**
624262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
625262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
626262965f5SChuck Lever  *
627262965f5SChuck Lever  * Return values:
628262965f5SChuck Lever  *        0:	The request has been sent
629262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
630262965f5SChuck Lever  *		complete the request
631262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
63225985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
633262965f5SChuck Lever  *
634262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
635262965f5SChuck Lever  *	if sendmsg is not able to make progress?
636262965f5SChuck Lever  */
637262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
638262965f5SChuck Lever {
639262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
640262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
641ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
642262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
643a6b31d18STrond Myklebust 	bool zerocopy = true;
6449ffadfbcSTrond Myklebust 	bool vm_wait = false;
645b595bb15SChuck Lever 	int status;
646f279cd00SJason Baron 	int sent;
647a246b010SChuck Lever 
64861677eeeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
649262965f5SChuck Lever 
650262965f5SChuck Lever 	xs_pktdump("packet data:",
651262965f5SChuck Lever 				req->rq_svec->iov_base,
652262965f5SChuck Lever 				req->rq_svec->iov_len);
653a6b31d18STrond Myklebust 	/* Don't use zero copy if this is a resend. If the RPC call
654a6b31d18STrond Myklebust 	 * completes while the socket holds a reference to the pages,
655a6b31d18STrond Myklebust 	 * then we may end up resending corrupted data.
656a6b31d18STrond Myklebust 	 */
657a6b31d18STrond Myklebust 	if (task->tk_flags & RPC_TASK_SENT)
658a6b31d18STrond Myklebust 		zerocopy = false;
659a246b010SChuck Lever 
660a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
661a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
662262965f5SChuck Lever 	 * called sendmsg(). */
663a246b010SChuck Lever 	while (1) {
664f279cd00SJason Baron 		sent = 0;
665f279cd00SJason Baron 		status = xs_sendpages(transport->sock, NULL, 0, xdr,
666f279cd00SJason Baron 				      req->rq_bytes_sent, zerocopy, &sent);
667a246b010SChuck Lever 
668262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
669262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
670262965f5SChuck Lever 
671a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
672a246b010SChuck Lever 		 * reset the count of bytes sent. */
673f279cd00SJason Baron 		req->rq_bytes_sent += sent;
674f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
675262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
676a246b010SChuck Lever 			req->rq_bytes_sent = 0;
677a246b010SChuck Lever 			return 0;
678a246b010SChuck Lever 		}
679262965f5SChuck Lever 
6809ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
6819ffadfbcSTrond Myklebust 
6829ffadfbcSTrond Myklebust 		if (status == -EAGAIN ) {
6839ffadfbcSTrond Myklebust 			/*
6849ffadfbcSTrond Myklebust 			 * Return EAGAIN if we're sure we're hitting the
6859ffadfbcSTrond Myklebust 			 * socket send buffer limits.
6869ffadfbcSTrond Myklebust 			 */
6879ffadfbcSTrond Myklebust 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
6889ffadfbcSTrond Myklebust 				break;
6899ffadfbcSTrond Myklebust 			/*
6909ffadfbcSTrond Myklebust 			 * Did we hit a memory allocation failure?
6919ffadfbcSTrond Myklebust 			 */
6929ffadfbcSTrond Myklebust 			if (sent == 0) {
6939ffadfbcSTrond Myklebust 				status = -ENOBUFS;
6949ffadfbcSTrond Myklebust 				if (vm_wait)
6959ffadfbcSTrond Myklebust 					break;
6969ffadfbcSTrond Myklebust 				/* Retry, knowing now that we're below the
6979ffadfbcSTrond Myklebust 				 * socket send buffer limit
6989ffadfbcSTrond Myklebust 				 */
6999ffadfbcSTrond Myklebust 				vm_wait = true;
7009ffadfbcSTrond Myklebust 			}
7019ffadfbcSTrond Myklebust 			continue;
7029ffadfbcSTrond Myklebust 		}
703f580dd04STrond Myklebust 		if (status < 0)
704f580dd04STrond Myklebust 			break;
7059ffadfbcSTrond Myklebust 		vm_wait = false;
706a246b010SChuck Lever 	}
707a246b010SChuck Lever 
708262965f5SChuck Lever 	switch (status) {
709fba91afbSTrond Myklebust 	case -ENOTSOCK:
710fba91afbSTrond Myklebust 		status = -ENOTCONN;
711fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
712fba91afbSTrond Myklebust 		break;
713262965f5SChuck Lever 	case -EAGAIN:
7145e3771ceSTrond Myklebust 		status = xs_nospace(task);
715262965f5SChuck Lever 		break;
716262965f5SChuck Lever 	case -ECONNRESET:
717262965f5SChuck Lever 	case -ECONNREFUSED:
718262965f5SChuck Lever 	case -ENOTCONN:
7193913c78cSTrond Myklebust 	case -EADDRINUSE:
720b5872f0cSTrond Myklebust 	case -ENOBUFS:
721b9d2bb2eSTrond Myklebust 	case -EPIPE:
72213331a55STrond Myklebust 		break;
72313331a55STrond Myklebust 	default:
72413331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
72513331a55STrond Myklebust 			-status);
726a246b010SChuck Lever 	}
7275fe46e9dSBian Naimeng 
728a246b010SChuck Lever 	return status;
729a246b010SChuck Lever }
730a246b010SChuck Lever 
7319903cd1cSChuck Lever /**
732e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
733e0ab53deSTrond Myklebust  * @xprt: transport
734e0ab53deSTrond Myklebust  * @task: rpc task
735e0ab53deSTrond Myklebust  *
736e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
737e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
738e0ab53deSTrond Myklebust  * the server.
739e0ab53deSTrond Myklebust  */
740e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
741e0ab53deSTrond Myklebust {
742e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
743e0ab53deSTrond Myklebust 
744e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
745e0ab53deSTrond Myklebust 		return;
746e0ab53deSTrond Myklebust 	if (task == NULL)
747e0ab53deSTrond Myklebust 		goto out_release;
748e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
74943cedbf0STrond Myklebust 	if (req == NULL)
75043cedbf0STrond Myklebust 		goto out_release;
751e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
752e0ab53deSTrond Myklebust 		goto out_release;
753e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
754e0ab53deSTrond Myklebust 		goto out_release;
755a4f0835cSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
756e0ab53deSTrond Myklebust out_release:
757e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
758e0ab53deSTrond Myklebust }
759e0ab53deSTrond Myklebust 
7602a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7612a9e1cfaSTrond Myklebust {
7622a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7632a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7642a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7652118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7662a9e1cfaSTrond Myklebust }
7672a9e1cfaSTrond Myklebust 
7682a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7692a9e1cfaSTrond Myklebust {
7702a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7712a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7722a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7732118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7742118071dSTrond Myklebust }
7752118071dSTrond Myklebust 
77642d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
77742d42a5bSTrond Myklebust {
77842d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
77942d42a5bSTrond Myklebust 
78042d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
78142d42a5bSTrond Myklebust }
78242d42a5bSTrond Myklebust 
783b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
784b70ae915STrond Myklebust {
785b70ae915STrond Myklebust 	smp_mb__before_atomic();
786b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
787b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
78842d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
789b70ae915STrond Myklebust 	smp_mb__after_atomic();
790b70ae915STrond Myklebust }
791b70ae915STrond Myklebust 
792b70ae915STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
793b70ae915STrond Myklebust {
794b70ae915STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
795b70ae915STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
796b70ae915STrond Myklebust 	xprt_disconnect_done(xprt);
797b70ae915STrond Myklebust }
798b70ae915STrond Myklebust 
7992118071dSTrond Myklebust /**
8002118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
8012118071dSTrond Myklebust  * @sk: socket
8022118071dSTrond Myklebust  *
8032118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
8042118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
8052118071dSTrond Myklebust  */
8062118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
8072118071dSTrond Myklebust {
8082118071dSTrond Myklebust 	struct rpc_xprt *xprt;
8092118071dSTrond Myklebust 	int err;
8102118071dSTrond Myklebust 
8112118071dSTrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
8122118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
8132118071dSTrond Myklebust 		goto out;
8142118071dSTrond Myklebust 
8152118071dSTrond Myklebust 	err = -sk->sk_err;
8162118071dSTrond Myklebust 	if (err == 0)
8172118071dSTrond Myklebust 		goto out;
818b70ae915STrond Myklebust 	/* Is this a reset event? */
819b70ae915STrond Myklebust 	if (sk->sk_state == TCP_CLOSE)
820b70ae915STrond Myklebust 		xs_sock_mark_closed(xprt);
8212118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
8222118071dSTrond Myklebust 			xprt, -err);
823e8353c76STrond Myklebust 	trace_rpc_socket_error(xprt, sk->sk_socket, err);
8242118071dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, err);
8252118071dSTrond Myklebust  out:
8262118071dSTrond Myklebust 	read_unlock_bh(&sk->sk_callback_lock);
8272a9e1cfaSTrond Myklebust }
8282a9e1cfaSTrond Myklebust 
829fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
830a246b010SChuck Lever {
831ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
832ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
8336cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
834a246b010SChuck Lever 
835fe315e76SChuck Lever 	if (sk == NULL)
836fe315e76SChuck Lever 		return;
8379903cd1cSChuck Lever 
838264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
839264d1df3SJeff Layton 		sk_clear_memalloc(sk);
840264d1df3SJeff Layton 
84109939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
84209939204STrond Myklebust 
843edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
844a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
845ee0ac0c2SChuck Lever 	transport->inet = NULL;
846ee0ac0c2SChuck Lever 	transport->sock = NULL;
847a246b010SChuck Lever 
848a246b010SChuck Lever 	sk->sk_user_data = NULL;
8492a9e1cfaSTrond Myklebust 
8502a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
8510c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
852a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
8536cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
854edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
855a246b010SChuck Lever 
8566cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
857a246b010SChuck Lever 	sock_release(sock);
858fe315e76SChuck Lever }
859fe315e76SChuck Lever 
860fe315e76SChuck Lever /**
861fe315e76SChuck Lever  * xs_close - close a socket
862fe315e76SChuck Lever  * @xprt: transport
863fe315e76SChuck Lever  *
864fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
865fe315e76SChuck Lever  * on the server we want to save.
866f75e6745STrond Myklebust  *
867f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
868f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
869fe315e76SChuck Lever  */
870fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
871fe315e76SChuck Lever {
872fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
873fe315e76SChuck Lever 
874fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
875fe315e76SChuck Lever 
876fe315e76SChuck Lever 	xs_reset_transport(transport);
87761d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
878fe315e76SChuck Lever 
87962da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
880a246b010SChuck Lever }
881a246b010SChuck Lever 
8824a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
8834a068258SChuck Lever {
8844a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
8854a068258SChuck Lever 		xprt);
8864a068258SChuck Lever 	xprt_disconnect_done(xprt);
8874a068258SChuck Lever }
8884a068258SChuck Lever 
889315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
890315f3812SKinglong Mee {
891315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
892315f3812SKinglong Mee 	xprt_free(xprt);
893315f3812SKinglong Mee }
894315f3812SKinglong Mee 
8959903cd1cSChuck Lever /**
8969903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8979903cd1cSChuck Lever  * @xprt: doomed transport
8989903cd1cSChuck Lever  *
8999903cd1cSChuck Lever  */
9009903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
901a246b010SChuck Lever {
90203c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
90303c78827STrond Myklebust 			struct sock_xprt, xprt);
9049903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
9059903cd1cSChuck Lever 
90603c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
907a1311d87STrond Myklebust 	xs_close(xprt);
908edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
909315f3812SKinglong Mee 	xs_xprt_free(xprt);
910a1311d87STrond Myklebust 	module_put(THIS_MODULE);
911a246b010SChuck Lever }
912a246b010SChuck Lever 
913176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
914176e21eeSChuck Lever {
915176e21eeSChuck Lever 	struct xdr_skb_reader desc = {
916176e21eeSChuck Lever 		.skb		= skb,
917176e21eeSChuck Lever 		.offset		= sizeof(rpc_fraghdr),
918176e21eeSChuck Lever 		.count		= skb->len - sizeof(rpc_fraghdr),
919176e21eeSChuck Lever 	};
920176e21eeSChuck Lever 
921176e21eeSChuck Lever 	if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
922176e21eeSChuck Lever 		return -1;
923176e21eeSChuck Lever 	if (desc.count)
924176e21eeSChuck Lever 		return -1;
925176e21eeSChuck Lever 	return 0;
926176e21eeSChuck Lever }
927176e21eeSChuck Lever 
928176e21eeSChuck Lever /**
929a2648094STrond Myklebust  * xs_local_data_read_skb
930a2648094STrond Myklebust  * @xprt: transport
931a2648094STrond Myklebust  * @sk: socket
932a2648094STrond Myklebust  * @skb: skbuff
933176e21eeSChuck Lever  *
934176e21eeSChuck Lever  * Currently this assumes we can read the whole reply in a single gulp.
935176e21eeSChuck Lever  */
936a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt,
937a2648094STrond Myklebust 		struct sock *sk,
938a2648094STrond Myklebust 		struct sk_buff *skb)
939176e21eeSChuck Lever {
940176e21eeSChuck Lever 	struct rpc_task *task;
941176e21eeSChuck Lever 	struct rpc_rqst *rovr;
942a2648094STrond Myklebust 	int repsize, copied;
943176e21eeSChuck Lever 	u32 _xid;
944176e21eeSChuck Lever 	__be32 *xp;
945176e21eeSChuck Lever 
946176e21eeSChuck Lever 	repsize = skb->len - sizeof(rpc_fraghdr);
947176e21eeSChuck Lever 	if (repsize < 4) {
948176e21eeSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d\n", repsize);
949a2648094STrond Myklebust 		return;
950176e21eeSChuck Lever 	}
951176e21eeSChuck Lever 
952176e21eeSChuck Lever 	/* Copy the XID from the skb... */
953176e21eeSChuck Lever 	xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
954176e21eeSChuck Lever 	if (xp == NULL)
955a2648094STrond Myklebust 		return;
956176e21eeSChuck Lever 
957176e21eeSChuck Lever 	/* Look up and lock the request corresponding to the given XID */
958a2648094STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
959176e21eeSChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
960176e21eeSChuck Lever 	if (!rovr)
961176e21eeSChuck Lever 		goto out_unlock;
962176e21eeSChuck Lever 	task = rovr->rq_task;
963176e21eeSChuck Lever 
964176e21eeSChuck Lever 	copied = rovr->rq_private_buf.buflen;
965176e21eeSChuck Lever 	if (copied > repsize)
966176e21eeSChuck Lever 		copied = repsize;
967176e21eeSChuck Lever 
968176e21eeSChuck Lever 	if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
969176e21eeSChuck Lever 		dprintk("RPC:       sk_buff copy failed\n");
970176e21eeSChuck Lever 		goto out_unlock;
971176e21eeSChuck Lever 	}
972176e21eeSChuck Lever 
973176e21eeSChuck Lever 	xprt_complete_rqst(task, copied);
974176e21eeSChuck Lever 
975176e21eeSChuck Lever  out_unlock:
976a2648094STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
977a2648094STrond Myklebust }
978a2648094STrond Myklebust 
979a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport)
980a2648094STrond Myklebust {
981a2648094STrond Myklebust 	struct sk_buff *skb;
982a2648094STrond Myklebust 	struct sock *sk;
983a2648094STrond Myklebust 	int err;
984a2648094STrond Myklebust 
985a2648094STrond Myklebust 	mutex_lock(&transport->recv_mutex);
986a2648094STrond Myklebust 	sk = transport->inet;
987a2648094STrond Myklebust 	if (sk == NULL)
988a2648094STrond Myklebust 		goto out;
989a2648094STrond Myklebust 	for (;;) {
990a2648094STrond Myklebust 		skb = skb_recv_datagram(sk, 0, 1, &err);
99142d42a5bSTrond Myklebust 		if (skb != NULL) {
992a2648094STrond Myklebust 			xs_local_data_read_skb(&transport->xprt, sk, skb);
993176e21eeSChuck Lever 			skb_free_datagram(sk, skb);
99442d42a5bSTrond Myklebust 			continue;
99542d42a5bSTrond Myklebust 		}
99642d42a5bSTrond Myklebust 		if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
99742d42a5bSTrond Myklebust 			break;
998a2648094STrond Myklebust 	}
999176e21eeSChuck Lever out:
1000a2648094STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1001a2648094STrond Myklebust }
1002a2648094STrond Myklebust 
1003a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work)
1004a2648094STrond Myklebust {
1005a2648094STrond Myklebust 	struct sock_xprt *transport =
1006a2648094STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1007a2648094STrond Myklebust 	xs_local_data_receive(transport);
1008176e21eeSChuck Lever }
1009176e21eeSChuck Lever 
10109903cd1cSChuck Lever /**
1011f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1012f9b2ee71STrond Myklebust  * @xprt: transport
1013f9b2ee71STrond Myklebust  * @sk: socket
1014f9b2ee71STrond Myklebust  * @skb: skbuff
10159903cd1cSChuck Lever  *
1016a246b010SChuck Lever  */
1017f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1018f9b2ee71STrond Myklebust 		struct sock *sk,
1019f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1020a246b010SChuck Lever {
1021a246b010SChuck Lever 	struct rpc_task *task;
1022a246b010SChuck Lever 	struct rpc_rqst *rovr;
1023f9b2ee71STrond Myklebust 	int repsize, copied;
1024d8ed029dSAlexey Dobriyan 	u32 _xid;
1025d8ed029dSAlexey Dobriyan 	__be32 *xp;
1026a246b010SChuck Lever 
10271da8c681SWillem de Bruijn 	repsize = skb->len;
1028a246b010SChuck Lever 	if (repsize < 4) {
10299903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1030f9b2ee71STrond Myklebust 		return;
1031a246b010SChuck Lever 	}
1032a246b010SChuck Lever 
1033a246b010SChuck Lever 	/* Copy the XID from the skb... */
10341da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1035a246b010SChuck Lever 	if (xp == NULL)
1036f9b2ee71STrond Myklebust 		return;
1037a246b010SChuck Lever 
1038a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
1039f9b2ee71STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
1040a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1041a246b010SChuck Lever 	if (!rovr)
1042a246b010SChuck Lever 		goto out_unlock;
1043a246b010SChuck Lever 	task = rovr->rq_task;
1044a246b010SChuck Lever 
1045a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1046a246b010SChuck Lever 		copied = repsize;
1047a246b010SChuck Lever 
1048a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
10491781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
105002c22347SEric Dumazet 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1051a246b010SChuck Lever 		goto out_unlock;
10521781f7f5SHerbert Xu 	}
10531781f7f5SHerbert Xu 
105402c22347SEric Dumazet 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1055a246b010SChuck Lever 
10566a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
10571570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
1058a246b010SChuck Lever 
1059a246b010SChuck Lever  out_unlock:
1060f9b2ee71STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
1061f9b2ee71STrond Myklebust }
1062f9b2ee71STrond Myklebust 
1063f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1064f9b2ee71STrond Myklebust {
1065f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1066f9b2ee71STrond Myklebust 	struct sock *sk;
1067f9b2ee71STrond Myklebust 	int err;
1068f9b2ee71STrond Myklebust 
1069f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1070f9b2ee71STrond Myklebust 	sk = transport->inet;
1071f9b2ee71STrond Myklebust 	if (sk == NULL)
1072f9b2ee71STrond Myklebust 		goto out;
1073f9b2ee71STrond Myklebust 	for (;;) {
1074f9b2ee71STrond Myklebust 		skb = skb_recv_datagram(sk, 0, 1, &err);
107542d42a5bSTrond Myklebust 		if (skb != NULL) {
1076f9b2ee71STrond Myklebust 			xs_udp_data_read_skb(&transport->xprt, sk, skb);
1077a246b010SChuck Lever 			skb_free_datagram(sk, skb);
107842d42a5bSTrond Myklebust 			continue;
107942d42a5bSTrond Myklebust 		}
108042d42a5bSTrond Myklebust 		if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
108142d42a5bSTrond Myklebust 			break;
1082f9b2ee71STrond Myklebust 	}
1083a246b010SChuck Lever out:
1084f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1085f9b2ee71STrond Myklebust }
1086f9b2ee71STrond Myklebust 
1087f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1088f9b2ee71STrond Myklebust {
1089f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1090f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1091f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1092f9b2ee71STrond Myklebust }
1093f9b2ee71STrond Myklebust 
1094f9b2ee71STrond Myklebust /**
1095f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1096f9b2ee71STrond Myklebust  * @sk: socket with data to read
1097f9b2ee71STrond Myklebust  *
1098f9b2ee71STrond Myklebust  */
1099f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1100f9b2ee71STrond Myklebust {
1101f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1102f9b2ee71STrond Myklebust 
1103f9b2ee71STrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
1104f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1105f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1106f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1107f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1108f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
11095157b956STrond Myklebust 		transport->old_data_ready(sk);
11105157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
11115157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
11125157b956STrond Myklebust 		 */
11135157b956STrond Myklebust 		if (xprt->reestablish_timeout)
11145157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
111542d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
111640a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1117f9b2ee71STrond Myklebust 	}
1118f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1119a246b010SChuck Lever }
1120a246b010SChuck Lever 
1121a519fc7aSTrond Myklebust /*
1122a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1123a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1124a519fc7aSTrond Myklebust  */
1125a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1126a519fc7aSTrond Myklebust {
1127a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1128a519fc7aSTrond Myklebust }
1129a519fc7aSTrond Myklebust 
1130dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1131a246b010SChuck Lever {
113251971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1133a246b010SChuck Lever 	size_t len, used;
1134a246b010SChuck Lever 	char *p;
1135a246b010SChuck Lever 
113651971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
113751971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
11389d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
113951971139SChuck Lever 	transport->tcp_offset += used;
1140a246b010SChuck Lever 	if (used != len)
1141a246b010SChuck Lever 		return;
1142808012fbSChuck Lever 
114351971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
114451971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1145e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1146a246b010SChuck Lever 	else
1147e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
114851971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1149808012fbSChuck Lever 
1150e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
115151971139SChuck Lever 	transport->tcp_offset = 0;
1152808012fbSChuck Lever 
1153a246b010SChuck Lever 	/* Sanity check of the record length */
115418dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
11559903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
1156a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
11579903cd1cSChuck Lever 		return;
1158a246b010SChuck Lever 	}
1159a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
116051971139SChuck Lever 			transport->tcp_reclen);
1161a246b010SChuck Lever }
1162a246b010SChuck Lever 
116351971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1164a246b010SChuck Lever {
116551971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
1166e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
116751971139SChuck Lever 		transport->tcp_offset = 0;
1168e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1169e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1170e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
117151971139SChuck Lever 			transport->tcp_copied = 0;
1172a246b010SChuck Lever 		}
1173a246b010SChuck Lever 	}
1174a246b010SChuck Lever }
1175a246b010SChuck Lever 
1176dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1177a246b010SChuck Lever {
1178a246b010SChuck Lever 	size_t len, used;
1179a246b010SChuck Lever 	char *p;
1180a246b010SChuck Lever 
118151971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1182a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
118351971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
11849d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
118551971139SChuck Lever 	transport->tcp_offset += used;
1186a246b010SChuck Lever 	if (used != len)
1187a246b010SChuck Lever 		return;
1188e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1189f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
119051971139SChuck Lever 	transport->tcp_copied = 4;
119118dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
119218dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
119318dca02aSRicardo Labiaga 							      : "request with",
119451971139SChuck Lever 			ntohl(transport->tcp_xid));
119551971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1196a246b010SChuck Lever }
1197a246b010SChuck Lever 
119818dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
119918dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
1200a246b010SChuck Lever {
120118dca02aSRicardo Labiaga 	size_t len, used;
120218dca02aSRicardo Labiaga 	u32 offset;
1203b76ce561STrond Myklebust 	char *p;
120418dca02aSRicardo Labiaga 
120518dca02aSRicardo Labiaga 	/*
120618dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
120718dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
120818dca02aSRicardo Labiaga 	 * When this function is called for the first time,
120918dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
121018dca02aSRicardo Labiaga 	 */
121118dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1212b76ce561STrond Myklebust 	len = sizeof(transport->tcp_calldir) - offset;
121318dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
1214b76ce561STrond Myklebust 	p = ((char *) &transport->tcp_calldir) + offset;
1215b76ce561STrond Myklebust 	used = xdr_skb_read_bits(desc, p, len);
121618dca02aSRicardo Labiaga 	transport->tcp_offset += used;
121718dca02aSRicardo Labiaga 	if (used != len)
121818dca02aSRicardo Labiaga 		return;
1219f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1220f4a2e418SRicardo Labiaga 	/*
1221f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
1222f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
1223f4a2e418SRicardo Labiaga 	 */
1224b76ce561STrond Myklebust 	switch (ntohl(transport->tcp_calldir)) {
1225b76ce561STrond Myklebust 	case RPC_REPLY:
1226b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1227b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
122818dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
1229b76ce561STrond Myklebust 		break;
1230b76ce561STrond Myklebust 	case RPC_CALL:
1231b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1232b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
123318dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
1234b76ce561STrond Myklebust 		break;
1235b76ce561STrond Myklebust 	default:
1236b76ce561STrond Myklebust 		dprintk("RPC:       invalid request message type\n");
1237a519fc7aSTrond Myklebust 		xs_tcp_force_close(&transport->xprt);
1238b76ce561STrond Myklebust 	}
123918dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
124018dca02aSRicardo Labiaga }
124118dca02aSRicardo Labiaga 
124244b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
124344b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
124444b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
1245a246b010SChuck Lever {
124644b98efdSRicardo Labiaga 	struct sock_xprt *transport =
124744b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
1248a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
1249a246b010SChuck Lever 	size_t len;
1250a246b010SChuck Lever 	ssize_t r;
1251a246b010SChuck Lever 
1252a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1253f4a2e418SRicardo Labiaga 
1254f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1255f4a2e418SRicardo Labiaga 		/*
1256f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
1257f4a2e418SRicardo Labiaga 		 */
1258f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1259b76ce561STrond Myklebust 			&transport->tcp_calldir,
1260b76ce561STrond Myklebust 			sizeof(transport->tcp_calldir));
1261b76ce561STrond Myklebust 		transport->tcp_copied += sizeof(transport->tcp_calldir);
1262f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1263a246b010SChuck Lever 	}
1264a246b010SChuck Lever 
1265a246b010SChuck Lever 	len = desc->count;
126651971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1267dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1268a246b010SChuck Lever 
126951971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1270a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1271a246b010SChuck Lever 		my_desc.count = len;
127251971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
12739d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1274a246b010SChuck Lever 		desc->count -= r;
1275a246b010SChuck Lever 		desc->offset += r;
1276a246b010SChuck Lever 	} else
127751971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
12789d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1279a246b010SChuck Lever 
1280a246b010SChuck Lever 	if (r > 0) {
128151971139SChuck Lever 		transport->tcp_copied += r;
128251971139SChuck Lever 		transport->tcp_offset += r;
1283a246b010SChuck Lever 	}
1284a246b010SChuck Lever 	if (r != len) {
1285a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1286a246b010SChuck Lever 		 * usually because we weren't able to allocate
1287a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1288e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1289a246b010SChuck Lever 		 * will not receive any additional updates,
1290a246b010SChuck Lever 		 * and time out.
1291a246b010SChuck Lever 		 * Any remaining data from this record will
1292a246b010SChuck Lever 		 * be discarded.
1293a246b010SChuck Lever 		 */
1294e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1295a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
129651971139SChuck Lever 				ntohl(transport->tcp_xid));
129746121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
129846121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
129946121cf7SChuck Lever 				xprt, transport->tcp_copied,
130046121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
130144b98efdSRicardo Labiaga 		return;
1302a246b010SChuck Lever 	}
1303a246b010SChuck Lever 
1304a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
130551971139SChuck Lever 			ntohl(transport->tcp_xid), r);
130646121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
130746121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
130846121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1309a246b010SChuck Lever 
131051971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1311e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
131251971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1313e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1314e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1315a246b010SChuck Lever 	}
131644b98efdSRicardo Labiaga }
131744b98efdSRicardo Labiaga 
131844b98efdSRicardo Labiaga /*
131944b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
132044b98efdSRicardo Labiaga  * tcp read code to read the data.
132144b98efdSRicardo Labiaga  */
132244b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
132344b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
132444b98efdSRicardo Labiaga {
132544b98efdSRicardo Labiaga 	struct sock_xprt *transport =
132644b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
132744b98efdSRicardo Labiaga 	struct rpc_rqst *req;
132844b98efdSRicardo Labiaga 
132944b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
133044b98efdSRicardo Labiaga 
133144b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
1332edc1b01cSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
133344b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
133444b98efdSRicardo Labiaga 	if (!req) {
133544b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
133644b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
1337edc1b01cSTrond Myklebust 		spin_unlock_bh(&xprt->transport_lock);
133844b98efdSRicardo Labiaga 		return -1;
133944b98efdSRicardo Labiaga 	}
134044b98efdSRicardo Labiaga 
134144b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
134244b98efdSRicardo Labiaga 
1343e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
134451971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
134544b98efdSRicardo Labiaga 
1346edc1b01cSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
134744b98efdSRicardo Labiaga 	return 0;
134844b98efdSRicardo Labiaga }
134944b98efdSRicardo Labiaga 
13509e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
135144b98efdSRicardo Labiaga /*
135244b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
135344b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
135444b98efdSRicardo Labiaga  * queue.
135544b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
135644b98efdSRicardo Labiaga  * connection and return -1.
135744b98efdSRicardo Labiaga  */
13582ea24497STrond Myklebust static int xs_tcp_read_callback(struct rpc_xprt *xprt,
135944b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
136044b98efdSRicardo Labiaga {
136144b98efdSRicardo Labiaga 	struct sock_xprt *transport =
136244b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
136344b98efdSRicardo Labiaga 	struct rpc_rqst *req;
136444b98efdSRicardo Labiaga 
13652ea24497STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
1366edc1b01cSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
13672ea24497STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
136844b98efdSRicardo Labiaga 	if (req == NULL) {
1369edc1b01cSTrond Myklebust 		spin_unlock_bh(&xprt->transport_lock);
137044b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
137144b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
137244b98efdSRicardo Labiaga 		return -1;
137344b98efdSRicardo Labiaga 	}
137444b98efdSRicardo Labiaga 
137544b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
137644b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
137744b98efdSRicardo Labiaga 
13782ea24497STrond Myklebust 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
13792ea24497STrond Myklebust 		xprt_complete_bc_request(req, transport->tcp_copied);
1380edc1b01cSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
138144b98efdSRicardo Labiaga 
138244b98efdSRicardo Labiaga 	return 0;
138344b98efdSRicardo Labiaga }
138444b98efdSRicardo Labiaga 
138544b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
138644b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
138744b98efdSRicardo Labiaga {
138844b98efdSRicardo Labiaga 	struct sock_xprt *transport =
138944b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
139044b98efdSRicardo Labiaga 
139144b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
139244b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
139344b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
139444b98efdSRicardo Labiaga }
139576566773SChuck Lever 
139676566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
139776566773SChuck Lever {
139876566773SChuck Lever 	int ret;
139976566773SChuck Lever 
140076566773SChuck Lever 	ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
140176566773SChuck Lever 			      SVC_SOCK_ANONYMOUS);
140276566773SChuck Lever 	if (ret < 0)
140376566773SChuck Lever 		return ret;
140476566773SChuck Lever 	return 0;
140576566773SChuck Lever }
14066b26cc8cSChuck Lever 
14076b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14086b26cc8cSChuck Lever {
14096b26cc8cSChuck Lever 	return PAGE_SIZE;
14106b26cc8cSChuck Lever }
141144b98efdSRicardo Labiaga #else
141244b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
141344b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
141444b98efdSRicardo Labiaga {
141544b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
141644b98efdSRicardo Labiaga }
14179e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
141844b98efdSRicardo Labiaga 
141944b98efdSRicardo Labiaga /*
142044b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
142144b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
142244b98efdSRicardo Labiaga  */
142344b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
142444b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
142544b98efdSRicardo Labiaga {
142644b98efdSRicardo Labiaga 	struct sock_xprt *transport =
142744b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
142844b98efdSRicardo Labiaga 
142944b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
143051971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
143144b98efdSRicardo Labiaga 	else {
143244b98efdSRicardo Labiaga 		/*
143344b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
143444b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
143544b98efdSRicardo Labiaga 		 */
143644b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
143744b98efdSRicardo Labiaga 	}
1438a246b010SChuck Lever }
1439a246b010SChuck Lever 
1440dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1441a246b010SChuck Lever {
1442a246b010SChuck Lever 	size_t len;
1443a246b010SChuck Lever 
144451971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1445a246b010SChuck Lever 	if (len > desc->count)
1446a246b010SChuck Lever 		len = desc->count;
1447a246b010SChuck Lever 	desc->count -= len;
1448a246b010SChuck Lever 	desc->offset += len;
144951971139SChuck Lever 	transport->tcp_offset += len;
1450a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
145151971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1452a246b010SChuck Lever }
1453a246b010SChuck Lever 
14549903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1455a246b010SChuck Lever {
1456a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
145751971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1458dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1459a246b010SChuck Lever 		.skb	= skb,
1460a246b010SChuck Lever 		.offset	= offset,
1461a246b010SChuck Lever 		.count	= len,
1462a246b010SChuck Lever 	};
1463a246b010SChuck Lever 
14649903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1465a246b010SChuck Lever 	do {
14661a867a08SJeff Layton 		trace_xs_tcp_data_recv(transport);
1467a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1468a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1469e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
14709903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1471a246b010SChuck Lever 			continue;
1472a246b010SChuck Lever 		}
1473a246b010SChuck Lever 		/* Read in the xid if necessary */
1474e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
147551971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1476a246b010SChuck Lever 			continue;
1477a246b010SChuck Lever 		}
147818dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1479f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
148018dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
148118dca02aSRicardo Labiaga 			continue;
148218dca02aSRicardo Labiaga 		}
1483a246b010SChuck Lever 		/* Read in the request data */
1484e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
148544b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1486a246b010SChuck Lever 			continue;
1487a246b010SChuck Lever 		}
1488a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
148951971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1490a246b010SChuck Lever 	} while (desc.count);
14911a867a08SJeff Layton 	trace_xs_tcp_data_recv(transport);
14929903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1493a246b010SChuck Lever 	return len - desc.count;
1494a246b010SChuck Lever }
1495a246b010SChuck Lever 
149666d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport)
149766d7a56aSTrond Myklebust {
149866d7a56aSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
149966d7a56aSTrond Myklebust 	struct sock *sk;
150066d7a56aSTrond Myklebust 	read_descriptor_t rd_desc = {
150166d7a56aSTrond Myklebust 		.count = 2*1024*1024,
150266d7a56aSTrond Myklebust 		.arg.data = xprt,
150366d7a56aSTrond Myklebust 	};
150466d7a56aSTrond Myklebust 	unsigned long total = 0;
150566d7a56aSTrond Myklebust 	int read = 0;
150666d7a56aSTrond Myklebust 
1507edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
150866d7a56aSTrond Myklebust 	sk = transport->inet;
1509edc1b01cSTrond Myklebust 	if (sk == NULL)
1510edc1b01cSTrond Myklebust 		goto out;
151166d7a56aSTrond Myklebust 
151266d7a56aSTrond Myklebust 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
151366d7a56aSTrond Myklebust 	for (;;) {
1514edc1b01cSTrond Myklebust 		lock_sock(sk);
151566d7a56aSTrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
151642d42a5bSTrond Myklebust 		if (read <= 0) {
151742d42a5bSTrond Myklebust 			clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1518edc1b01cSTrond Myklebust 			release_sock(sk);
151942d42a5bSTrond Myklebust 			if (!test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
152066d7a56aSTrond Myklebust 				break;
152142d42a5bSTrond Myklebust 		} else {
152242d42a5bSTrond Myklebust 			release_sock(sk);
152366d7a56aSTrond Myklebust 			total += read;
152442d42a5bSTrond Myklebust 		}
152566d7a56aSTrond Myklebust 		rd_desc.count = 65536;
152666d7a56aSTrond Myklebust 	}
1527edc1b01cSTrond Myklebust out:
1528edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
152966d7a56aSTrond Myklebust 	trace_xs_tcp_data_ready(xprt, read, total);
153066d7a56aSTrond Myklebust }
153166d7a56aSTrond Myklebust 
1532edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work)
1533edc1b01cSTrond Myklebust {
1534edc1b01cSTrond Myklebust 	struct sock_xprt *transport =
1535edc1b01cSTrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1536edc1b01cSTrond Myklebust 	xs_tcp_data_receive(transport);
1537edc1b01cSTrond Myklebust }
1538edc1b01cSTrond Myklebust 
15399903cd1cSChuck Lever /**
15409903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
15419903cd1cSChuck Lever  * @sk: socket whose state has changed
15429903cd1cSChuck Lever  *
15439903cd1cSChuck Lever  */
15449903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1545a246b010SChuck Lever {
1546a246b010SChuck Lever 	struct rpc_xprt *xprt;
15470fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1548a246b010SChuck Lever 
1549f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1550a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1551a246b010SChuck Lever 		goto out;
15529903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1553669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1554a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1555a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1556669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1557669502ffSAndy Chittenden 			sk->sk_shutdown);
1558a246b010SChuck Lever 
15590fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
156040b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1561a246b010SChuck Lever 	switch (sk->sk_state) {
1562a246b010SChuck Lever 	case TCP_ESTABLISHED:
1563f064af1eSEric Dumazet 		spin_lock(&xprt->transport_lock);
1564a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
156551971139SChuck Lever 
1566a246b010SChuck Lever 			/* Reset TCP record info */
156751971139SChuck Lever 			transport->tcp_offset = 0;
156851971139SChuck Lever 			transport->tcp_reclen = 0;
156951971139SChuck Lever 			transport->tcp_copied = 0;
1570e136d092SChuck Lever 			transport->tcp_flags =
1571e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
15728b71798cSTrond Myklebust 			xprt->connect_cookie++;
15730fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15740fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
157551971139SChuck Lever 
15762a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1577a246b010SChuck Lever 		}
1578f064af1eSEric Dumazet 		spin_unlock(&xprt->transport_lock);
1579a246b010SChuck Lever 		break;
15803b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15813b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15827c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1583663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15843b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15854e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15863b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1587ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15884e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1589a246b010SChuck Lever 		break;
1590632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15913b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15927c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1593d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1594a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
1595663b8858STrond Myklebust 	case TCP_CLOSING:
1596663b8858STrond Myklebust 		/*
1597663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1598663b8858STrond Myklebust 		 * we back off before reconnecting
1599663b8858STrond Myklebust 		 */
1600663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1601663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
16023b948ae5STrond Myklebust 		break;
16033b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1604670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
16054e857c58SPeter Zijlstra 		smp_mb__before_atomic();
16063b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
16074e857c58SPeter Zijlstra 		smp_mb__after_atomic();
16083b948ae5STrond Myklebust 		break;
16093b948ae5STrond Myklebust 	case TCP_CLOSE:
16100fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
16110fdea1e8STrond Myklebust 					&transport->sock_state))
16120fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
16137d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1614a246b010SChuck Lever 	}
1615a246b010SChuck Lever  out:
1616f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1617a246b010SChuck Lever }
1618a246b010SChuck Lever 
16191f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
16201f0fa154SIlpo Järvinen {
162113331a55STrond Myklebust 	struct socket_wq *wq;
16221f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
16231f0fa154SIlpo Järvinen 
162413331a55STrond Myklebust 	if (!sk->sk_socket)
16251f0fa154SIlpo Järvinen 		return;
162613331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
16271f0fa154SIlpo Järvinen 
16281f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
16291f0fa154SIlpo Järvinen 		return;
163013331a55STrond Myklebust 	rcu_read_lock();
163113331a55STrond Myklebust 	wq = rcu_dereference(sk->sk_wq);
163213331a55STrond Myklebust 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
163313331a55STrond Myklebust 		goto out;
16341f0fa154SIlpo Järvinen 
16351f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
163613331a55STrond Myklebust out:
163713331a55STrond Myklebust 	rcu_read_unlock();
16381f0fa154SIlpo Järvinen }
16391f0fa154SIlpo Järvinen 
16402a9e1cfaSTrond Myklebust /**
1641c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1642c7b2cae8SChuck Lever  *                             becomes available
16439903cd1cSChuck Lever  * @sk: socket whose state has changed
16449903cd1cSChuck Lever  *
1645a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1646a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1647c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1648a246b010SChuck Lever  * with a bunch of small requests.
1649a246b010SChuck Lever  */
1650c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1651a246b010SChuck Lever {
1652f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1653c7b2cae8SChuck Lever 
1654c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
16551f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
16561f0fa154SIlpo Järvinen 		xs_write_space(sk);
1657c7b2cae8SChuck Lever 
1658f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1659c7b2cae8SChuck Lever }
1660c7b2cae8SChuck Lever 
1661c7b2cae8SChuck Lever /**
1662c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1663c7b2cae8SChuck Lever  *                             becomes available
1664c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1665c7b2cae8SChuck Lever  *
1666c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1667c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1668c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1669c7b2cae8SChuck Lever  * with a bunch of small requests.
1670c7b2cae8SChuck Lever  */
1671c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1672c7b2cae8SChuck Lever {
1673f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1674c7b2cae8SChuck Lever 
1675c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
167664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16771f0fa154SIlpo Järvinen 		xs_write_space(sk);
1678c7b2cae8SChuck Lever 
1679f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1680a246b010SChuck Lever }
1681a246b010SChuck Lever 
1682470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1683a246b010SChuck Lever {
1684ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1685ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1686a246b010SChuck Lever 
16877c6e066eSChuck Lever 	if (transport->rcvsize) {
1688a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16897c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1690a246b010SChuck Lever 	}
16917c6e066eSChuck Lever 	if (transport->sndsize) {
1692a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16937c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1694a246b010SChuck Lever 		sk->sk_write_space(sk);
1695a246b010SChuck Lever 	}
1696a246b010SChuck Lever }
1697a246b010SChuck Lever 
169843118c29SChuck Lever /**
1699470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
170043118c29SChuck Lever  * @xprt: generic transport
1701470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1702470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
170343118c29SChuck Lever  *
1704470056c2SChuck Lever  * Set socket send and receive buffer size limits.
170543118c29SChuck Lever  */
1706470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
170743118c29SChuck Lever {
17087c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
17097c6e066eSChuck Lever 
17107c6e066eSChuck Lever 	transport->sndsize = 0;
1711470056c2SChuck Lever 	if (sndsize)
17127c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
17137c6e066eSChuck Lever 	transport->rcvsize = 0;
1714470056c2SChuck Lever 	if (rcvsize)
17157c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1716470056c2SChuck Lever 
1717470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
171843118c29SChuck Lever }
171943118c29SChuck Lever 
172046c0ee8bSChuck Lever /**
172146c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
172246c0ee8bSChuck Lever  * @task: task that timed out
172346c0ee8bSChuck Lever  *
172446c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
172546c0ee8bSChuck Lever  */
17266a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
172746c0ee8bSChuck Lever {
17286a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
172946c0ee8bSChuck Lever }
173046c0ee8bSChuck Lever 
1731b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1732b85d8806SChuck Lever {
17335d71899aSFrank Sorenson 	unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
173463862b5bSAruna-Hewapathirane 	unsigned short rand = (unsigned short) prandom_u32() % range;
1735b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1736b85d8806SChuck Lever }
1737b85d8806SChuck Lever 
173892200412SChuck Lever /**
17394dda9c8aSTrond Myklebust  * xs_set_reuseaddr_port - set the socket's port and address reuse options
17404dda9c8aSTrond Myklebust  * @sock: socket
17414dda9c8aSTrond Myklebust  *
17424dda9c8aSTrond Myklebust  * Note that this function has to be called on all sockets that share the
17434dda9c8aSTrond Myklebust  * same port, and it must be called before binding.
17444dda9c8aSTrond Myklebust  */
17454dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock)
17464dda9c8aSTrond Myklebust {
1747402e23b4STrond Myklebust 	int opt = 1;
17484dda9c8aSTrond Myklebust 
1749402e23b4STrond Myklebust 	kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1750402e23b4STrond Myklebust 			(char *)&opt, sizeof(opt));
17514dda9c8aSTrond Myklebust }
17524dda9c8aSTrond Myklebust 
17534dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
17544dda9c8aSTrond Myklebust {
17554dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
17564dda9c8aSTrond Myklebust 	int buflen;
17574dda9c8aSTrond Myklebust 	unsigned short port = 0;
17584dda9c8aSTrond Myklebust 
17594dda9c8aSTrond Myklebust 	if (kernel_getsockname(sock, (struct sockaddr *)&buf, &buflen) < 0)
17604dda9c8aSTrond Myklebust 		goto out;
17614dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
17624dda9c8aSTrond Myklebust 	case AF_INET6:
17634dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
17644dda9c8aSTrond Myklebust 		break;
17654dda9c8aSTrond Myklebust 	case AF_INET:
17664dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
17674dda9c8aSTrond Myklebust 	}
17684dda9c8aSTrond Myklebust out:
17694dda9c8aSTrond Myklebust 	return port;
17704dda9c8aSTrond Myklebust }
17714dda9c8aSTrond Myklebust 
17724dda9c8aSTrond Myklebust /**
177392200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
177492200412SChuck Lever  * @xprt: generic transport
177592200412SChuck Lever  * @port: new port number
177692200412SChuck Lever  *
177792200412SChuck Lever  */
177892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
177992200412SChuck Lever {
178092200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1781c4efcb1dSChuck Lever 
17829dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17839dc3b095SChuck Lever 	xs_update_peer_port(xprt);
178492200412SChuck Lever }
178592200412SChuck Lever 
17864dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17874dda9c8aSTrond Myklebust {
17884dda9c8aSTrond Myklebust 	if (transport->srcport == 0)
17894dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17904dda9c8aSTrond Myklebust }
17914dda9c8aSTrond Myklebust 
17925d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport)
179367a391d7STrond Myklebust {
1794fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
179567a391d7STrond Myklebust 
179667a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
179767a391d7STrond Myklebust 		port = xs_get_random_port();
179867a391d7STrond Myklebust 	return port;
179967a391d7STrond Myklebust }
180067a391d7STrond Myklebust 
1801baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
180267a391d7STrond Myklebust {
1803fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1804fbfffbd5SChuck Lever 		transport->srcport = 0;
180567a391d7STrond Myklebust 	if (!transport->xprt.resvport)
180667a391d7STrond Myklebust 		return 0;
180767a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
180867a391d7STrond Myklebust 		return xprt_max_resvport;
180967a391d7STrond Myklebust 	return --port;
181067a391d7STrond Myklebust }
1811beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1812a246b010SChuck Lever {
1813beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
181467a391d7STrond Myklebust 	int err, nloop = 0;
18155d4ec932SPavel Emelyanov 	unsigned short port = xs_get_srcport(transport);
181667a391d7STrond Myklebust 	unsigned short last;
1817a246b010SChuck Lever 
18180f7a622cSChris Perl 	/*
18190f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
18200f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
18210f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
18220f7a622cSChris Perl 	 * (for example at connect time).
18230f7a622cSChris Perl 	 *
18240f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
18250f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
18260f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
18270f7a622cSChris Perl 	 * for UDP sockets, but also doens't harm them.
18280f7a622cSChris Perl 	 *
18290f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
18300f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
18310f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
18320f7a622cSChris Perl 	 */
18330f7a622cSChris Perl 	if (port == 0)
18340f7a622cSChris Perl 		return 0;
18350f7a622cSChris Perl 
1836beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1837a246b010SChuck Lever 	do {
1838beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1839e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1840beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1841a246b010SChuck Lever 		if (err == 0) {
1842fbfffbd5SChuck Lever 			transport->srcport = port;
1843d3bc9a1dSFrank van Maarseveen 			break;
1844a246b010SChuck Lever 		}
184567a391d7STrond Myklebust 		last = port;
1846baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
184767a391d7STrond Myklebust 		if (port > last)
184867a391d7STrond Myklebust 			nloop++;
184967a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1850beb59b68SPavel Emelyanov 
18514232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1852beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1853beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1854beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1855beb59b68SPavel Emelyanov 	else
1856beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1857beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18587dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1859a246b010SChuck Lever 	return err;
1860a246b010SChuck Lever }
1861a246b010SChuck Lever 
1862176e21eeSChuck Lever /*
1863176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1864176e21eeSChuck Lever  */
1865176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1866176e21eeSChuck Lever {
1867fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1868176e21eeSChuck Lever }
1869176e21eeSChuck Lever 
1870176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1871176e21eeSChuck Lever {
1872176e21eeSChuck Lever }
1873a246b010SChuck Lever 
1874ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1875ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1876ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1877ed07536eSPeter Zijlstra 
1878176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1879176e21eeSChuck Lever {
1880176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1881176e21eeSChuck Lever 
1882176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1883176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1884176e21eeSChuck Lever }
1885176e21eeSChuck Lever 
18868945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1887ed07536eSPeter Zijlstra {
1888ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18898945ee5eSChuck Lever 
18908945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
18918945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1892ed07536eSPeter Zijlstra }
18938945ee5eSChuck Lever 
18948945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18958945ee5eSChuck Lever {
18968945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18978945ee5eSChuck Lever 
18988945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
18998945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1900ed07536eSPeter Zijlstra }
19016bc9638aSPavel Emelyanov 
19026bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
19036bc9638aSPavel Emelyanov {
1904fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
19051b7a1819SWeston Andros Adamson 		return;
19061b7a1819SWeston Andros Adamson 
19074232e863SChuck Lever 	switch (family) {
1908176e21eeSChuck Lever 	case AF_LOCAL:
1909176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1910176e21eeSChuck Lever 		break;
19114232e863SChuck Lever 	case AF_INET:
19126bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
19134232e863SChuck Lever 		break;
19144232e863SChuck Lever 	case AF_INET6:
19156bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
19164232e863SChuck Lever 		break;
19174232e863SChuck Lever 	}
19186bc9638aSPavel Emelyanov }
1919ed07536eSPeter Zijlstra #else
19206bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
19216bc9638aSPavel Emelyanov {
19226bc9638aSPavel Emelyanov }
1923ed07536eSPeter Zijlstra #endif
1924ed07536eSPeter Zijlstra 
192593dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
192693dc41bdSNeilBrown {
192793dc41bdSNeilBrown }
192893dc41bdSNeilBrown 
19296bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
19304dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
19314dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
193222f79326SPavel Emelyanov {
193322f79326SPavel Emelyanov 	struct socket *sock;
193422f79326SPavel Emelyanov 	int err;
193522f79326SPavel Emelyanov 
19366bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
193722f79326SPavel Emelyanov 	if (err < 0) {
193822f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
193922f79326SPavel Emelyanov 				protocol, -err);
194022f79326SPavel Emelyanov 		goto out;
194122f79326SPavel Emelyanov 	}
19426bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
194322f79326SPavel Emelyanov 
19444dda9c8aSTrond Myklebust 	if (reuseport)
19454dda9c8aSTrond Myklebust 		xs_sock_set_reuseport(sock);
19464dda9c8aSTrond Myklebust 
19474cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19484cea288aSBen Hutchings 	if (err) {
194922f79326SPavel Emelyanov 		sock_release(sock);
195022f79326SPavel Emelyanov 		goto out;
195122f79326SPavel Emelyanov 	}
195222f79326SPavel Emelyanov 
195322f79326SPavel Emelyanov 	return sock;
195422f79326SPavel Emelyanov out:
195522f79326SPavel Emelyanov 	return ERR_PTR(err);
195622f79326SPavel Emelyanov }
195722f79326SPavel Emelyanov 
1958176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1959176e21eeSChuck Lever 				      struct socket *sock)
1960176e21eeSChuck Lever {
1961176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1962176e21eeSChuck Lever 									xprt);
1963176e21eeSChuck Lever 
1964176e21eeSChuck Lever 	if (!transport->inet) {
1965176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1966176e21eeSChuck Lever 
1967176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1968176e21eeSChuck Lever 
1969176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1970176e21eeSChuck Lever 
1971176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1972a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1973176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1974b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
19752118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1976c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
1977176e21eeSChuck Lever 
1978176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1979176e21eeSChuck Lever 
1980176e21eeSChuck Lever 		/* Reset to new socket */
1981176e21eeSChuck Lever 		transport->sock = sock;
1982176e21eeSChuck Lever 		transport->inet = sk;
1983176e21eeSChuck Lever 
1984176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1985176e21eeSChuck Lever 	}
1986176e21eeSChuck Lever 
1987176e21eeSChuck Lever 	/* Tell the socket layer to start connecting... */
1988176e21eeSChuck Lever 	xprt->stat.connect_count++;
1989176e21eeSChuck Lever 	xprt->stat.connect_start = jiffies;
1990176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1991176e21eeSChuck Lever }
1992176e21eeSChuck Lever 
1993176e21eeSChuck Lever /**
1994176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1995176e21eeSChuck Lever  * @transport: socket transport to connect
1996176e21eeSChuck Lever  */
1997dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1998176e21eeSChuck Lever {
1999176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2000176e21eeSChuck Lever 	struct socket *sock;
2001176e21eeSChuck Lever 	int status = -EIO;
2002176e21eeSChuck Lever 
2003176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
2004176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
2005176e21eeSChuck Lever 	if (status < 0) {
2006176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
2007176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
2008176e21eeSChuck Lever 		goto out;
2009176e21eeSChuck Lever 	}
2010d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
2011176e21eeSChuck Lever 
2012176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
2013176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2014176e21eeSChuck Lever 
2015176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
201640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2017176e21eeSChuck Lever 	switch (status) {
2018176e21eeSChuck Lever 	case 0:
2019176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
2020176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2021176e21eeSChuck Lever 		xprt_set_connected(xprt);
20223601c4a9STrond Myklebust 	case -ENOBUFS:
2023176e21eeSChuck Lever 		break;
2024176e21eeSChuck Lever 	case -ENOENT:
2025176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
2026176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2027176e21eeSChuck Lever 		break;
20284a20a988STrond Myklebust 	case -ECONNREFUSED:
20294a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20304a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20314a20a988STrond Myklebust 		break;
2032176e21eeSChuck Lever 	default:
2033176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2034176e21eeSChuck Lever 				__func__, -status,
2035176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2036176e21eeSChuck Lever 	}
2037176e21eeSChuck Lever 
2038176e21eeSChuck Lever out:
2039176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2040176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2041dc107402SJ. Bruce Fields 	return status;
2042dc107402SJ. Bruce Fields }
2043dc107402SJ. Bruce Fields 
2044b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2045dc107402SJ. Bruce Fields {
2046dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2047dc107402SJ. Bruce Fields 	int ret;
2048dc107402SJ. Bruce Fields 
2049dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
2050dc107402SJ. Bruce Fields 		/*
2051dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2052dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2053dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2054dc107402SJ. Bruce Fields 		 *
2055dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2056dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2057dc107402SJ. Bruce Fields 		 * connect.
2058dc107402SJ. Bruce Fields 		 */
2059dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
2060dc107402SJ. Bruce Fields 		return;
2061dc107402SJ. Bruce Fields 	}
2062dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2063dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2064dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2065176e21eeSChuck Lever }
2066176e21eeSChuck Lever 
20673c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2068d6e971d8SJeff Layton /*
2069d6e971d8SJeff Layton  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2070d6e971d8SJeff Layton  * know that we have exclusive access to the socket), to guard against
2071d6e971d8SJeff Layton  * races with xs_reset_transport.
2072d6e971d8SJeff Layton  */
2073a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2074a564b8f0SMel Gorman {
2075a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2076a564b8f0SMel Gorman 			xprt);
2077a564b8f0SMel Gorman 
2078d6e971d8SJeff Layton 	/*
2079d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2080d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2081d6e971d8SJeff Layton 	 */
2082d6e971d8SJeff Layton 	if (!transport->inet)
2083d6e971d8SJeff Layton 		return;
20848e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2085a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2086a564b8f0SMel Gorman }
2087a564b8f0SMel Gorman 
2088a564b8f0SMel Gorman /**
2089d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2090a564b8f0SMel Gorman  * @xprt: transport to tag
2091a564b8f0SMel Gorman  *
20928e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20938e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2094a564b8f0SMel Gorman  */
2095d67fa4d8SJeff Layton static int
2096d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2097a564b8f0SMel Gorman {
2098d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2099a564b8f0SMel Gorman 
2100d6e971d8SJeff Layton 	if (atomic_inc_return(&xprt->swapper) != 1)
2101d6e971d8SJeff Layton 		return 0;
2102d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2103d6e971d8SJeff Layton 		return -ERESTARTSYS;
2104d6e971d8SJeff Layton 	if (xs->inet)
2105d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2106d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
21078e228133SJeff Layton 	return 0;
2108a564b8f0SMel Gorman }
2109a564b8f0SMel Gorman 
21108e228133SJeff Layton /**
2111d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
21128e228133SJeff Layton  * @xprt: transport to tag
21138e228133SJeff Layton  *
21148e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
21158e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
21168e228133SJeff Layton  */
2117d67fa4d8SJeff Layton static void
2118d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
21198e228133SJeff Layton {
2120d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
21218e228133SJeff Layton 
2122d6e971d8SJeff Layton 	if (!atomic_dec_and_test(&xprt->swapper))
2123d6e971d8SJeff Layton 		return;
2124d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2125d6e971d8SJeff Layton 		return;
2126d6e971d8SJeff Layton 	if (xs->inet)
2127d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2128d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
2129a564b8f0SMel Gorman }
2130a564b8f0SMel Gorman #else
2131a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2132a564b8f0SMel Gorman {
2133a564b8f0SMel Gorman }
2134d67fa4d8SJeff Layton 
2135d67fa4d8SJeff Layton static int
2136d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2137d67fa4d8SJeff Layton {
2138d67fa4d8SJeff Layton 	return -EINVAL;
2139d67fa4d8SJeff Layton }
2140d67fa4d8SJeff Layton 
2141d67fa4d8SJeff Layton static void
2142d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2143d67fa4d8SJeff Layton {
2144d67fa4d8SJeff Layton }
2145a564b8f0SMel Gorman #endif
2146a564b8f0SMel Gorman 
214716be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2148a246b010SChuck Lever {
214916be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2150edb267a6SChuck Lever 
2151ee0ac0c2SChuck Lever 	if (!transport->inet) {
2152b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2153b0d93ad5SChuck Lever 
2154b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2155b0d93ad5SChuck Lever 
21562a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21572a9e1cfaSTrond Myklebust 
2158b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2159f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2160b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2161b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
2162c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
2163b0d93ad5SChuck Lever 
2164b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2165b0d93ad5SChuck Lever 
2166b0d93ad5SChuck Lever 		/* Reset to new socket */
2167ee0ac0c2SChuck Lever 		transport->sock = sock;
2168ee0ac0c2SChuck Lever 		transport->inet = sk;
2169b0d93ad5SChuck Lever 
2170a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2171a564b8f0SMel Gorman 
2172b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2173b0d93ad5SChuck Lever 	}
2174470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
217502910177STrond Myklebust 
217602910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
217716be2d20SChuck Lever }
217816be2d20SChuck Lever 
21798c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2180a246b010SChuck Lever {
2181a246b010SChuck Lever 	struct sock_xprt *transport =
2182a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2183a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2184a246b010SChuck Lever 	struct socket *sock = transport->sock;
2185b65c0310SPavel Emelyanov 	int status = -EIO;
2186a246b010SChuck Lever 
21878c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21884dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21894dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2190b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2191a246b010SChuck Lever 		goto out;
219268e220bdSChuck Lever 
2193c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2194c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2195c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2196c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2197c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
219868e220bdSChuck Lever 
219968e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
220040b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2201a246b010SChuck Lever 	status = 0;
2202b0d93ad5SChuck Lever out:
2203718ba5b8STrond Myklebust 	xprt_unlock_connect(xprt, transport);
2204b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
22057d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2206b0d93ad5SChuck Lever }
2207b0d93ad5SChuck Lever 
22084876cc77STrond Myklebust /**
22094876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
22104876cc77STrond Myklebust  * @xprt: transport
22114876cc77STrond Myklebust  *
22124876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
22134876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
22144876cc77STrond Myklebust  */
22154876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
22164876cc77STrond Myklebust {
22174876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22184876cc77STrond Myklebust 	struct socket *sock = transport->sock;
22194876cc77STrond Myklebust 
22204876cc77STrond Myklebust 	if (sock == NULL)
22214876cc77STrond Myklebust 		return;
22224876cc77STrond Myklebust 	if (xprt_connected(xprt)) {
22234876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
22244876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22254876cc77STrond Myklebust 	} else
22264876cc77STrond Myklebust 		xs_reset_transport(transport);
22274876cc77STrond Myklebust }
22284876cc77STrond Myklebust 
222916be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2230b0d93ad5SChuck Lever {
223116be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2232fe19a96bSTrond Myklebust 	int ret = -ENOTCONN;
2233edb267a6SChuck Lever 
2234ee0ac0c2SChuck Lever 	if (!transport->inet) {
2235b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
22367f260e85STrond Myklebust 		unsigned int keepidle = xprt->timeout->to_initval / HZ;
22377f260e85STrond Myklebust 		unsigned int keepcnt = xprt->timeout->to_retries + 1;
22387f260e85STrond Myklebust 		unsigned int opt_on = 1;
2239775f06abSTrond Myklebust 		unsigned int timeo;
2240d88e4d82SNeilBrown 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
22417f260e85STrond Myklebust 
22427f260e85STrond Myklebust 		/* TCP Keepalive options */
22437f260e85STrond Myklebust 		kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
22447f260e85STrond Myklebust 				(char *)&opt_on, sizeof(opt_on));
22457f260e85STrond Myklebust 		kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
22467f260e85STrond Myklebust 				(char *)&keepidle, sizeof(keepidle));
22477f260e85STrond Myklebust 		kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
22487f260e85STrond Myklebust 				(char *)&keepidle, sizeof(keepidle));
22497f260e85STrond Myklebust 		kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
22507f260e85STrond Myklebust 				(char *)&keepcnt, sizeof(keepcnt));
2251b0d93ad5SChuck Lever 
2252d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2253d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2254d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2255d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2256d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2257d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2258d88e4d82SNeilBrown 		 */
2259d88e4d82SNeilBrown 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2260d88e4d82SNeilBrown 				(char *)&addr_pref, sizeof(addr_pref));
2261d88e4d82SNeilBrown 
2262775f06abSTrond Myklebust 		/* TCP user timeout (see RFC5482) */
2263775f06abSTrond Myklebust 		timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2264775f06abSTrond Myklebust 			(xprt->timeout->to_retries + 1);
2265775f06abSTrond Myklebust 		kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2266775f06abSTrond Myklebust 				(char *)&timeo, sizeof(timeo));
2267775f06abSTrond Myklebust 
2268b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2269b0d93ad5SChuck Lever 
22702a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22712a9e1cfaSTrond Myklebust 
2272b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
22735157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2274b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2275b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2276b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
22772118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
2278c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
22793167e12cSChuck Lever 
22803167e12cSChuck Lever 		/* socket options */
22813167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
22823167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2283b0d93ad5SChuck Lever 
2284b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2285b0d93ad5SChuck Lever 
2286b0d93ad5SChuck Lever 		/* Reset to new socket */
2287ee0ac0c2SChuck Lever 		transport->sock = sock;
2288ee0ac0c2SChuck Lever 		transport->inet = sk;
2289b0d93ad5SChuck Lever 
2290b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2291b0d93ad5SChuck Lever 	}
2292b0d93ad5SChuck Lever 
229301d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2294fe19a96bSTrond Myklebust 		goto out;
229501d37c42STrond Myklebust 
2296a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2297a564b8f0SMel Gorman 
2298b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
2299262ca07dSChuck Lever 	xprt->stat.connect_count++;
2300262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
23010fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2302fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2303fe19a96bSTrond Myklebust 	switch (ret) {
2304fe19a96bSTrond Myklebust 	case 0:
23054dda9c8aSTrond Myklebust 		xs_set_srcport(transport, sock);
2306fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2307fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2308fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2309fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
23101f4c17a0STrond Myklebust 		break;
23111f4c17a0STrond Myklebust 	case -EADDRNOTAVAIL:
23121f4c17a0STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
23131f4c17a0STrond Myklebust 		transport->srcport = 0;
2314fe19a96bSTrond Myklebust 	}
2315fe19a96bSTrond Myklebust out:
2316fe19a96bSTrond Myklebust 	return ret;
231716be2d20SChuck Lever }
231816be2d20SChuck Lever 
231916be2d20SChuck Lever /**
2320b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
232116be2d20SChuck Lever  *
232216be2d20SChuck Lever  * Invoked by a work queue tasklet.
232316be2d20SChuck Lever  */
2324cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
232516be2d20SChuck Lever {
2326cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2327cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
232816be2d20SChuck Lever 	struct socket *sock = transport->sock;
2329a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2330b61d59ffSTrond Myklebust 	int status = -EIO;
233116be2d20SChuck Lever 
233216be2d20SChuck Lever 	if (!sock) {
2333cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
23344dda9c8aSTrond Myklebust 				xs_addr(xprt)->sa_family, SOCK_STREAM,
23354dda9c8aSTrond Myklebust 				IPPROTO_TCP, true);
2336b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2337b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
233816be2d20SChuck Lever 			goto out;
233916be2d20SChuck Lever 		}
23407d1e8255STrond Myklebust 	}
23417d1e8255STrond Myklebust 
2342c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2343c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2344c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2345c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2346c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
234716be2d20SChuck Lever 
234816be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
234940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2350a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
235146121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
235246121cf7SChuck Lever 			sock->sk->sk_state);
2353a246b010SChuck Lever 	switch (status) {
2354f75e6745STrond Myklebust 	default:
2355f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2356f75e6745STrond Myklebust 			__func__, status);
2357f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2358f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2359f75e6745STrond Myklebust 		 * and retry
2360f75e6745STrond Myklebust 		 */
2361a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
236288b5ed73STrond Myklebust 		break;
23632a491991STrond Myklebust 	case 0:
2364a246b010SChuck Lever 	case -EINPROGRESS:
2365a246b010SChuck Lever 	case -EALREADY:
2366718ba5b8STrond Myklebust 		xprt_unlock_connect(xprt, transport);
23677d1e8255STrond Myklebust 		return;
23689fcfe0c8STrond Myklebust 	case -EINVAL:
23699fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23709fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23719fcfe0c8STrond Myklebust 		 */
23723ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23733ed5e2a2STrond Myklebust 	case -ECONNRESET:
23743ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23753913c78cSTrond Myklebust 	case -EADDRINUSE:
23763601c4a9STrond Myklebust 	case -ENOBUFS:
23773ed5e2a2STrond Myklebust 		/* retry with existing socket, after a delay */
23784efdd92cSTrond Myklebust 		xs_tcp_force_close(xprt);
23799fcfe0c8STrond Myklebust 		goto out;
23808a2cec29STrond Myklebust 	}
23812a491991STrond Myklebust 	status = -EAGAIN;
2382a246b010SChuck Lever out:
2383718ba5b8STrond Myklebust 	xprt_unlock_connect(xprt, transport);
23842226feb6SChuck Lever 	xprt_clear_connecting(xprt);
23857d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2386a246b010SChuck Lever }
2387a246b010SChuck Lever 
238802910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
238902910177STrond Myklebust {
239002910177STrond Myklebust 	unsigned long start, now = jiffies;
239102910177STrond Myklebust 
239202910177STrond Myklebust 	start = xprt->stat.connect_start + xprt->reestablish_timeout;
239302910177STrond Myklebust 	if (time_after(start, now))
239402910177STrond Myklebust 		return start - now;
239502910177STrond Myklebust 	return 0;
239602910177STrond Myklebust }
239702910177STrond Myklebust 
2398*3851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt)
2399*3851f1cdSTrond Myklebust {
2400*3851f1cdSTrond Myklebust 	xprt->reestablish_timeout <<= 1;
2401*3851f1cdSTrond Myklebust 	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
2402*3851f1cdSTrond Myklebust 		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
2403*3851f1cdSTrond Myklebust 	if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2404*3851f1cdSTrond Myklebust 		xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2405*3851f1cdSTrond Myklebust }
2406*3851f1cdSTrond Myklebust 
240768e220bdSChuck Lever /**
24089903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
24091b092092STrond Myklebust  * @xprt: pointer to transport structure
24109903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
24119903cd1cSChuck Lever  *
24129903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
241303bf4b70SChuck Lever  *
241403bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
241503bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
241603bf4b70SChuck Lever  * socket on a privileged port.
241703bf4b70SChuck Lever  *
241803bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
241903bf4b70SChuck Lever  * retry floods (hard mounts).
24209903cd1cSChuck Lever  */
24211b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2422a246b010SChuck Lever {
2423ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
242402910177STrond Myklebust 	unsigned long delay = 0;
2425a246b010SChuck Lever 
2426718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2427718ba5b8STrond Myklebust 
242899b1a4c3STrond Myklebust 	if (transport->sock != NULL) {
242946121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
243046121cf7SChuck Lever 				"seconds\n",
243103bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
243299b1a4c3STrond Myklebust 
243399b1a4c3STrond Myklebust 		/* Start by resetting any existing state */
243499b1a4c3STrond Myklebust 		xs_reset_transport(transport);
243599b1a4c3STrond Myklebust 
243602910177STrond Myklebust 		delay = xs_reconnect_delay(xprt);
2437*3851f1cdSTrond Myklebust 		xs_reconnect_backoff(xprt);
243802910177STrond Myklebust 
243902910177STrond Myklebust 	} else
24409903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
244102910177STrond Myklebust 
244240a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
244302910177STrond Myklebust 			&transport->connect_worker,
244402910177STrond Myklebust 			delay);
2445a246b010SChuck Lever }
2446a246b010SChuck Lever 
2447262ca07dSChuck Lever /**
2448176e21eeSChuck Lever  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2449176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2450176e21eeSChuck Lever  * @seq: output file
2451176e21eeSChuck Lever  *
2452176e21eeSChuck Lever  */
2453176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2454176e21eeSChuck Lever {
2455176e21eeSChuck Lever 	long idle_time = 0;
2456176e21eeSChuck Lever 
2457176e21eeSChuck Lever 	if (xprt_connected(xprt))
2458176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2459176e21eeSChuck Lever 
2460176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
246115a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2462176e21eeSChuck Lever 			xprt->stat.bind_count,
2463176e21eeSChuck Lever 			xprt->stat.connect_count,
2464176e21eeSChuck Lever 			xprt->stat.connect_time,
2465176e21eeSChuck Lever 			idle_time,
2466176e21eeSChuck Lever 			xprt->stat.sends,
2467176e21eeSChuck Lever 			xprt->stat.recvs,
2468176e21eeSChuck Lever 			xprt->stat.bad_xids,
2469176e21eeSChuck Lever 			xprt->stat.req_u,
247015a45206SAndy Adamson 			xprt->stat.bklog_u,
247115a45206SAndy Adamson 			xprt->stat.max_slots,
247215a45206SAndy Adamson 			xprt->stat.sending_u,
247315a45206SAndy Adamson 			xprt->stat.pending_u);
2474176e21eeSChuck Lever }
2475176e21eeSChuck Lever 
2476176e21eeSChuck Lever /**
2477262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2478262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2479262ca07dSChuck Lever  * @seq: output file
2480262ca07dSChuck Lever  *
2481262ca07dSChuck Lever  */
2482262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2483262ca07dSChuck Lever {
2484c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2485c8475461SChuck Lever 
248615a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
248715a45206SAndy Adamson 			"%lu %llu %llu\n",
2488fbfffbd5SChuck Lever 			transport->srcport,
2489262ca07dSChuck Lever 			xprt->stat.bind_count,
2490262ca07dSChuck Lever 			xprt->stat.sends,
2491262ca07dSChuck Lever 			xprt->stat.recvs,
2492262ca07dSChuck Lever 			xprt->stat.bad_xids,
2493262ca07dSChuck Lever 			xprt->stat.req_u,
249415a45206SAndy Adamson 			xprt->stat.bklog_u,
249515a45206SAndy Adamson 			xprt->stat.max_slots,
249615a45206SAndy Adamson 			xprt->stat.sending_u,
249715a45206SAndy Adamson 			xprt->stat.pending_u);
2498262ca07dSChuck Lever }
2499262ca07dSChuck Lever 
2500262ca07dSChuck Lever /**
2501262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2502262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2503262ca07dSChuck Lever  * @seq: output file
2504262ca07dSChuck Lever  *
2505262ca07dSChuck Lever  */
2506262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2507262ca07dSChuck Lever {
2508c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2509262ca07dSChuck Lever 	long idle_time = 0;
2510262ca07dSChuck Lever 
2511262ca07dSChuck Lever 	if (xprt_connected(xprt))
2512262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2513262ca07dSChuck Lever 
251415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
251515a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2516fbfffbd5SChuck Lever 			transport->srcport,
2517262ca07dSChuck Lever 			xprt->stat.bind_count,
2518262ca07dSChuck Lever 			xprt->stat.connect_count,
2519262ca07dSChuck Lever 			xprt->stat.connect_time,
2520262ca07dSChuck Lever 			idle_time,
2521262ca07dSChuck Lever 			xprt->stat.sends,
2522262ca07dSChuck Lever 			xprt->stat.recvs,
2523262ca07dSChuck Lever 			xprt->stat.bad_xids,
2524262ca07dSChuck Lever 			xprt->stat.req_u,
252515a45206SAndy Adamson 			xprt->stat.bklog_u,
252615a45206SAndy Adamson 			xprt->stat.max_slots,
252715a45206SAndy Adamson 			xprt->stat.sending_u,
252815a45206SAndy Adamson 			xprt->stat.pending_u);
2529262ca07dSChuck Lever }
2530262ca07dSChuck Lever 
25314cfc7e60SRahul Iyer /*
25324cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25334cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25344cfc7e60SRahul Iyer  * to use the server side send routines.
25354cfc7e60SRahul Iyer  */
25365a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size)
25374cfc7e60SRahul Iyer {
25384cfc7e60SRahul Iyer 	struct page *page;
25394cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25404cfc7e60SRahul Iyer 
2541b8a13d03SWeston Andros Adamson 	WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2542b8a13d03SWeston Andros Adamson 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2543b8a13d03SWeston Andros Adamson 		return NULL;
25444cfc7e60SRahul Iyer 
2545b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
25464cfc7e60SRahul Iyer 	if (!page)
25474cfc7e60SRahul Iyer 		return NULL;
25484cfc7e60SRahul Iyer 
25494cfc7e60SRahul Iyer 	buf = page_address(page);
25504cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
25514cfc7e60SRahul Iyer 
25524cfc7e60SRahul Iyer 	return buf->data;
25534cfc7e60SRahul Iyer }
25544cfc7e60SRahul Iyer 
25554cfc7e60SRahul Iyer /*
25564cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25574cfc7e60SRahul Iyer  */
25585a51f13aSH Hartley Sweeten static void bc_free(void *buffer)
25594cfc7e60SRahul Iyer {
25604cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25614cfc7e60SRahul Iyer 
25624cfc7e60SRahul Iyer 	if (!buffer)
25634cfc7e60SRahul Iyer 		return;
25644cfc7e60SRahul Iyer 
25654cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25664cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25674cfc7e60SRahul Iyer }
25684cfc7e60SRahul Iyer 
25694cfc7e60SRahul Iyer /*
25704cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
25714cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
25724cfc7e60SRahul Iyer  */
25734cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25744cfc7e60SRahul Iyer {
25754cfc7e60SRahul Iyer 	int len;
25764cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
25774cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
25784cfc7e60SRahul Iyer 	struct sock_xprt *transport =
25794cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
25804cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
25814cfc7e60SRahul Iyer 	unsigned long headoff;
25824cfc7e60SRahul Iyer 	unsigned long tailoff;
25834cfc7e60SRahul Iyer 
258461677eeeSChuck Lever 	xs_encode_stream_record_marker(xbufp);
25854cfc7e60SRahul Iyer 
25864cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
25874cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
25884cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
25894cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
25904cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
25914cfc7e60SRahul Iyer 
25924cfc7e60SRahul Iyer 	if (len != xbufp->len) {
25934cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
25944cfc7e60SRahul Iyer 		len = -EAGAIN;
25954cfc7e60SRahul Iyer 	}
25964cfc7e60SRahul Iyer 
25974cfc7e60SRahul Iyer 	return len;
25984cfc7e60SRahul Iyer }
25994cfc7e60SRahul Iyer 
26004cfc7e60SRahul Iyer /*
26014cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
26024cfc7e60SRahul Iyer  */
26034cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task)
26044cfc7e60SRahul Iyer {
26054cfc7e60SRahul Iyer 	struct rpc_rqst *req = task->tk_rqstp;
26064cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26077fc56136SAndrzej Hajda 	int len;
26084cfc7e60SRahul Iyer 
26094cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
26104cfc7e60SRahul Iyer 	/*
26114cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26124cfc7e60SRahul Iyer 	 */
26134cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26144cfc7e60SRahul Iyer 
26154cfc7e60SRahul Iyer 	/*
26164cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26174cfc7e60SRahul Iyer 	 * with the fore channel
26184cfc7e60SRahul Iyer 	 */
26194cfc7e60SRahul Iyer 	if (!mutex_trylock(&xprt->xpt_mutex)) {
26204cfc7e60SRahul Iyer 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
26214cfc7e60SRahul Iyer 		if (!mutex_trylock(&xprt->xpt_mutex))
26224cfc7e60SRahul Iyer 			return -EAGAIN;
26234cfc7e60SRahul Iyer 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
26244cfc7e60SRahul Iyer 	}
26254cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26264cfc7e60SRahul Iyer 		len = -ENOTCONN;
26274cfc7e60SRahul Iyer 	else
26284cfc7e60SRahul Iyer 		len = bc_sendto(req);
26294cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26304cfc7e60SRahul Iyer 
26314cfc7e60SRahul Iyer 	if (len > 0)
26324cfc7e60SRahul Iyer 		len = 0;
26334cfc7e60SRahul Iyer 
26344cfc7e60SRahul Iyer 	return len;
26354cfc7e60SRahul Iyer }
26364cfc7e60SRahul Iyer 
26374cfc7e60SRahul Iyer /*
26384cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
26394cfc7e60SRahul Iyer  */
26404cfc7e60SRahul Iyer 
26414cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
26424cfc7e60SRahul Iyer {
26434cfc7e60SRahul Iyer }
26444cfc7e60SRahul Iyer 
26454cfc7e60SRahul Iyer /*
26464cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
26474cfc7e60SRahul Iyer  * initiated, we do nothing
26484cfc7e60SRahul Iyer  */
26494cfc7e60SRahul Iyer 
26504cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
26514cfc7e60SRahul Iyer {
265247f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
265347f72efaSKinglong Mee 
265447f72efaSKinglong Mee 	xs_xprt_free(xprt);
265547f72efaSKinglong Mee 	module_put(THIS_MODULE);
26564cfc7e60SRahul Iyer }
26574cfc7e60SRahul Iyer 
2658176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = {
2659176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2660176e21eeSChuck Lever 	.release_xprt		= xs_tcp_release_xprt,
2661f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2662176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2663176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2664dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2665176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2666176e21eeSChuck Lever 	.buf_free		= rpc_free,
2667176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
2668176e21eeSChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2669176e21eeSChuck Lever 	.close			= xs_close,
2670a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2671176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2672d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2673d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2674176e21eeSChuck Lever };
2675176e21eeSChuck Lever 
2676262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
267743118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
267812a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
267949e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2680f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
268145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
268292200412SChuck Lever 	.set_port		= xs_set_port,
26839903cd1cSChuck Lever 	.connect		= xs_connect,
268402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
268502107148SChuck Lever 	.buf_free		= rpc_free,
2686262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2687fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
268846c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2689a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2690262965f5SChuck Lever 	.close			= xs_close,
2691262965f5SChuck Lever 	.destroy		= xs_destroy,
2692262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2693d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2694d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26954a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2696262965f5SChuck Lever };
2697262965f5SChuck Lever 
2698262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
269912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2700e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
2701f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_lock_and_alloc_slot,
270245160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
270392200412SChuck Lever 	.set_port		= xs_set_port,
27040b9e7943STrond Myklebust 	.connect		= xs_connect,
270502107148SChuck Lever 	.buf_alloc		= rpc_malloc,
270602107148SChuck Lever 	.buf_free		= rpc_free,
2707262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2708fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2709c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27109903cd1cSChuck Lever 	.destroy		= xs_destroy,
2711262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2712d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2713d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27144a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
271542e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
271642e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
271776566773SChuck Lever 	.bc_up			= xs_tcp_bc_up,
27186b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
271942e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
272042e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
272142e5c3e2SChuck Lever #endif
2722a246b010SChuck Lever };
2723a246b010SChuck Lever 
27244cfc7e60SRahul Iyer /*
27254cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27264cfc7e60SRahul Iyer  */
27274cfc7e60SRahul Iyer 
27284cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = {
27294cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27304cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
273184e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
27324cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
27334cfc7e60SRahul Iyer 	.buf_free		= bc_free,
27344cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
27354cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
27364cfc7e60SRahul Iyer 	.close			= bc_close,
27374cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
27384cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2739d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2740d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27414a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
27424cfc7e60SRahul Iyer };
27434cfc7e60SRahul Iyer 
274492476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
274592476850SChuck Lever {
274692476850SChuck Lever 	static const struct sockaddr_in sin = {
274792476850SChuck Lever 		.sin_family		= AF_INET,
274892476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
274992476850SChuck Lever 	};
275092476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
275192476850SChuck Lever 		.sin6_family		= AF_INET6,
275292476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
275392476850SChuck Lever 	};
275492476850SChuck Lever 
275592476850SChuck Lever 	switch (family) {
2756176e21eeSChuck Lever 	case AF_LOCAL:
2757176e21eeSChuck Lever 		break;
275892476850SChuck Lever 	case AF_INET:
275992476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
276092476850SChuck Lever 		break;
276192476850SChuck Lever 	case AF_INET6:
276292476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
276392476850SChuck Lever 		break;
276492476850SChuck Lever 	default:
276592476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
276692476850SChuck Lever 		return -EAFNOSUPPORT;
276792476850SChuck Lever 	}
276892476850SChuck Lever 	return 0;
276992476850SChuck Lever }
277092476850SChuck Lever 
27713c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2772d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2773d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2774c8541ecdSChuck Lever {
2775c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2776ffc2e518SChuck Lever 	struct sock_xprt *new;
2777c8541ecdSChuck Lever 
277896802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2779c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2780c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2781c8541ecdSChuck Lever 	}
2782c8541ecdSChuck Lever 
2783d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2784d9ba131dSTrond Myklebust 			max_slot_table_size);
2785bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
278646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
278746121cf7SChuck Lever 				"rpc_xprt\n");
2788c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2789c8541ecdSChuck Lever 	}
2790c8541ecdSChuck Lever 
2791bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2792edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
279396802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
279496802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2795d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2796fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
279792476850SChuck Lever 	else {
279892476850SChuck Lever 		int err;
279992476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
280092476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28012aa13531SStanislav Kinsbursky 		if (err != 0) {
28022aa13531SStanislav Kinsbursky 			xprt_free(xprt);
280392476850SChuck Lever 			return ERR_PTR(err);
280492476850SChuck Lever 		}
28052aa13531SStanislav Kinsbursky 	}
2806c8541ecdSChuck Lever 
2807c8541ecdSChuck Lever 	return xprt;
2808c8541ecdSChuck Lever }
2809c8541ecdSChuck Lever 
2810176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2811176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2812176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2813176e21eeSChuck Lever 	.to_retries = 2,
2814176e21eeSChuck Lever };
2815176e21eeSChuck Lever 
2816176e21eeSChuck Lever /**
2817176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2818176e21eeSChuck Lever  * @args: rpc transport creation arguments
2819176e21eeSChuck Lever  *
2820176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2821176e21eeSChuck Lever  */
2822176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2823176e21eeSChuck Lever {
2824176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2825176e21eeSChuck Lever 	struct sock_xprt *transport;
2826176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2827176e21eeSChuck Lever 	struct rpc_xprt *ret;
2828176e21eeSChuck Lever 
2829d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2830d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2831176e21eeSChuck Lever 	if (IS_ERR(xprt))
2832176e21eeSChuck Lever 		return xprt;
2833176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2834176e21eeSChuck Lever 
2835176e21eeSChuck Lever 	xprt->prot = 0;
2836176e21eeSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2837176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2838176e21eeSChuck Lever 
2839176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2840176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2841176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2842176e21eeSChuck Lever 
2843176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2844176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2845176e21eeSChuck Lever 
2846a2648094STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
284793dc41bdSNeilBrown 	INIT_DELAYED_WORK(&transport->connect_worker,
284893dc41bdSNeilBrown 			xs_dummy_setup_socket);
284993dc41bdSNeilBrown 
2850176e21eeSChuck Lever 	switch (sun->sun_family) {
2851176e21eeSChuck Lever 	case AF_LOCAL:
2852176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2853176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2854176e21eeSChuck Lever 					sun->sun_path);
2855176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2856176e21eeSChuck Lever 			goto out_err;
2857176e21eeSChuck Lever 		}
2858176e21eeSChuck Lever 		xprt_set_bound(xprt);
2859176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
28607073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
28617073ea87SJ. Bruce Fields 		if (ret)
28627073ea87SJ. Bruce Fields 			goto out_err;
2863176e21eeSChuck Lever 		break;
2864176e21eeSChuck Lever 	default:
2865176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2866176e21eeSChuck Lever 		goto out_err;
2867176e21eeSChuck Lever 	}
2868176e21eeSChuck Lever 
2869176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2870176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2871176e21eeSChuck Lever 
2872176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2873176e21eeSChuck Lever 		return xprt;
2874176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2875176e21eeSChuck Lever out_err:
2876315f3812SKinglong Mee 	xs_xprt_free(xprt);
2877176e21eeSChuck Lever 	return ret;
2878176e21eeSChuck Lever }
2879176e21eeSChuck Lever 
28802881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28812881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28822881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28832881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28842881ae74STrond Myklebust 	.to_retries = 5,
28852881ae74STrond Myklebust };
28862881ae74STrond Myklebust 
28879903cd1cSChuck Lever /**
28889903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
288996802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
28909903cd1cSChuck Lever  *
28919903cd1cSChuck Lever  */
2892483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2893a246b010SChuck Lever {
28948f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2895c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2896c8475461SChuck Lever 	struct sock_xprt *transport;
28970a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2898a246b010SChuck Lever 
2899d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2900d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2901c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2902c8541ecdSChuck Lever 		return xprt;
2903c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2904a246b010SChuck Lever 
2905ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2906808012fbSChuck Lever 	xprt->tsh_size = 0;
2907a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2908a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2909a246b010SChuck Lever 
291003bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
291103bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
291203bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2913a246b010SChuck Lever 
2914262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2915a246b010SChuck Lever 
2916ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2917a246b010SChuck Lever 
2918f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2919edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2920edc1b01cSTrond Myklebust 
29218f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29228f9d5b1aSChuck Lever 	case AF_INET:
29238f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29248f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29258f9d5b1aSChuck Lever 
29269dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29278f9d5b1aSChuck Lever 		break;
29288f9d5b1aSChuck Lever 	case AF_INET6:
29298f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29308f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29318f9d5b1aSChuck Lever 
29329dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
29338f9d5b1aSChuck Lever 		break;
29348f9d5b1aSChuck Lever 	default:
29350a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29360a68b0beSJ. Bruce Fields 		goto out_err;
29378f9d5b1aSChuck Lever 	}
29388f9d5b1aSChuck Lever 
2939c740eff8SChuck Lever 	if (xprt_bound(xprt))
2940c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2941c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2942c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2943c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2944c740eff8SChuck Lever 	else
2945c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2946c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2947c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2948edb267a6SChuck Lever 
2949bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2950c8541ecdSChuck Lever 		return xprt;
29510a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29520a68b0beSJ. Bruce Fields out_err:
2953315f3812SKinglong Mee 	xs_xprt_free(xprt);
29540a68b0beSJ. Bruce Fields 	return ret;
2955a246b010SChuck Lever }
2956a246b010SChuck Lever 
29572881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29582881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29592881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29602881ae74STrond Myklebust 	.to_retries = 2,
29612881ae74STrond Myklebust };
29622881ae74STrond Myklebust 
29639903cd1cSChuck Lever /**
29649903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
296596802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29669903cd1cSChuck Lever  *
29679903cd1cSChuck Lever  */
2968483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2969a246b010SChuck Lever {
29708f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2971c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2972c8475461SChuck Lever 	struct sock_xprt *transport;
29730a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2974b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2975b7993cebSTrond Myklebust 
2976b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2977b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2978a246b010SChuck Lever 
2979d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2980b7993cebSTrond Myklebust 			max_slot_table_size);
2981c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2982c8541ecdSChuck Lever 		return xprt;
2983c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2984a246b010SChuck Lever 
2985ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2986808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2987808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2988a246b010SChuck Lever 
298903bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
299003bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
299103bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2992a246b010SChuck Lever 
2993262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2994ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2995a246b010SChuck Lever 
2996*3851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
2997*3851f1cdSTrond Myklebust 
2998edc1b01cSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
2999edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3000edc1b01cSTrond Myklebust 
30018f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30028f9d5b1aSChuck Lever 	case AF_INET:
30038f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30048f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30058f9d5b1aSChuck Lever 
30069dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30078f9d5b1aSChuck Lever 		break;
30088f9d5b1aSChuck Lever 	case AF_INET6:
30098f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30108f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30118f9d5b1aSChuck Lever 
30129dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30138f9d5b1aSChuck Lever 		break;
30148f9d5b1aSChuck Lever 	default:
30150a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30160a68b0beSJ. Bruce Fields 		goto out_err;
30178f9d5b1aSChuck Lever 	}
30188f9d5b1aSChuck Lever 
3019c740eff8SChuck Lever 	if (xprt_bound(xprt))
3020c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3021c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3022c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3023c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3024c740eff8SChuck Lever 	else
3025c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3026c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3027c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3028c740eff8SChuck Lever 
3029bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3030c8541ecdSChuck Lever 		return xprt;
30310a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30320a68b0beSJ. Bruce Fields out_err:
3033315f3812SKinglong Mee 	xs_xprt_free(xprt);
30340a68b0beSJ. Bruce Fields 	return ret;
3035a246b010SChuck Lever }
3036282b32e1SChuck Lever 
3037f300babaSAlexandros Batsakis /**
3038f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3039f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3040f300babaSAlexandros Batsakis  *
3041f300babaSAlexandros Batsakis  */
3042f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3043f300babaSAlexandros Batsakis {
3044f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3045f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3046f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3047f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
30480a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3049f300babaSAlexandros Batsakis 
3050d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3051d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3052f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3053f300babaSAlexandros Batsakis 		return xprt;
3054f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3055f300babaSAlexandros Batsakis 
3056f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3057f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3058f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3059f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3060f300babaSAlexandros Batsakis 
3061f300babaSAlexandros Batsakis 	/* backchannel */
3062f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3063f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3064f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3065f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3066f300babaSAlexandros Batsakis 
3067f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3068f300babaSAlexandros Batsakis 
3069f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3070f300babaSAlexandros Batsakis 	case AF_INET:
3071f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3072f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3073f300babaSAlexandros Batsakis 		break;
3074f300babaSAlexandros Batsakis 	case AF_INET6:
3075f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3076f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3077f300babaSAlexandros Batsakis 		break;
3078f300babaSAlexandros Batsakis 	default:
30790a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30800a68b0beSJ. Bruce Fields 		goto out_err;
3081f300babaSAlexandros Batsakis 	}
3082f300babaSAlexandros Batsakis 
3083f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3084f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3085f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3086f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3087f300babaSAlexandros Batsakis 
3088f300babaSAlexandros Batsakis 	/*
308999de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
309028303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
309128303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
309228303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
309399de8ea9SJ. Bruce Fields 	 */
309499de8ea9SJ. Bruce Fields 	xprt_get(xprt);
309599de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
309699de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
309799de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
309899de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
309999de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
310099de8ea9SJ. Bruce Fields 
310199de8ea9SJ. Bruce Fields 	/*
3102f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3103f300babaSAlexandros Batsakis 	 * the xprt status to connected
3104f300babaSAlexandros Batsakis 	 */
3105f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3106f300babaSAlexandros Batsakis 
3107f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3108f300babaSAlexandros Batsakis 		return xprt;
3109642aab58SKinglong Mee 
3110642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
311139a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
311299de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31130a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31140a68b0beSJ. Bruce Fields out_err:
3115315f3812SKinglong Mee 	xs_xprt_free(xprt);
31160a68b0beSJ. Bruce Fields 	return ret;
3117f300babaSAlexandros Batsakis }
3118f300babaSAlexandros Batsakis 
3119176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3120176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3121176e21eeSChuck Lever 	.name		= "named UNIX socket",
3122176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3123176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3124176e21eeSChuck Lever 	.setup		= xs_setup_local,
3125176e21eeSChuck Lever };
3126176e21eeSChuck Lever 
3127bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3128bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3129bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3130bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3131f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3132bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3133bc25571eS\"Talpey, Thomas\ };
3134bc25571eS\"Talpey, Thomas\ 
3135bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3136bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3137bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3138bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3139f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3140bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3141bc25571eS\"Talpey, Thomas\ };
3142bc25571eS\"Talpey, Thomas\ 
3143f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3144f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3145f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3146f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3147f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3148f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3149f300babaSAlexandros Batsakis };
3150f300babaSAlexandros Batsakis 
3151282b32e1SChuck Lever /**
3152bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3153282b32e1SChuck Lever  *
3154282b32e1SChuck Lever  */
3155282b32e1SChuck Lever int init_socket_xprt(void)
3156282b32e1SChuck Lever {
3157f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
31582b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
31590b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3160fbf76683SChuck Lever #endif
3161fbf76683SChuck Lever 
3162176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3163bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3164bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3165f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3166bc25571eS\"Talpey, Thomas\ 
3167282b32e1SChuck Lever 	return 0;
3168282b32e1SChuck Lever }
3169282b32e1SChuck Lever 
3170282b32e1SChuck Lever /**
3171bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3172282b32e1SChuck Lever  *
3173282b32e1SChuck Lever  */
3174282b32e1SChuck Lever void cleanup_socket_xprt(void)
3175282b32e1SChuck Lever {
3176f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3177fbf76683SChuck Lever 	if (sunrpc_table_header) {
3178fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3179fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3180fbf76683SChuck Lever 	}
3181fbf76683SChuck Lever #endif
3182bc25571eS\"Talpey, Thomas\ 
3183176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3184bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3185bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3186f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3187282b32e1SChuck Lever }
3188cbf11071STrond Myklebust 
31899bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
31909bbb9e5aSRusty Russell 		const struct kernel_param *kp,
3191cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
3192cbf11071STrond Myklebust {
319300cfaa94SDaniel Walter 	unsigned int num;
3194cbf11071STrond Myklebust 	int ret;
3195cbf11071STrond Myklebust 
3196cbf11071STrond Myklebust 	if (!val)
3197cbf11071STrond Myklebust 		return -EINVAL;
319800cfaa94SDaniel Walter 	ret = kstrtouint(val, 0, &num);
3199cbf11071STrond Myklebust 	if (ret == -EINVAL || num < min || num > max)
3200cbf11071STrond Myklebust 		return -EINVAL;
3201cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3202cbf11071STrond Myklebust 	return 0;
3203cbf11071STrond Myklebust }
3204cbf11071STrond Myklebust 
32059bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3206cbf11071STrond Myklebust {
3207ffb6ca33SFrank Sorenson 	if (kp->arg == &xprt_min_resvport)
3208cbf11071STrond Myklebust 		return param_set_uint_minmax(val, kp,
3209cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3210ffb6ca33SFrank Sorenson 			xprt_max_resvport);
3211ffb6ca33SFrank Sorenson 	return param_set_uint_minmax(val, kp,
3212ffb6ca33SFrank Sorenson 			xprt_min_resvport,
3213cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3214cbf11071STrond Myklebust }
3215cbf11071STrond Myklebust 
32169c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32179bbb9e5aSRusty Russell 	.set = param_set_portnr,
32189bbb9e5aSRusty Russell 	.get = param_get_uint,
32199bbb9e5aSRusty Russell };
32209bbb9e5aSRusty Russell 
3221cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3222cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3223cbf11071STrond Myklebust 
3224cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3225cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3226cbf11071STrond Myklebust 
32279bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32289bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3229cbf11071STrond Myklebust {
3230cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3231cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3232cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3233cbf11071STrond Myklebust }
3234cbf11071STrond Myklebust 
32359c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
32369bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
32379bbb9e5aSRusty Russell 	.get = param_get_uint,
32389bbb9e5aSRusty Russell };
32399bbb9e5aSRusty Russell 
3240cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3241cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3242cbf11071STrond Myklebust 
3243d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3244d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3245d9ba131dSTrond Myklebust {
3246d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3247d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3248d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3249d9ba131dSTrond Myklebust }
3250d9ba131dSTrond Myklebust 
32519c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3252d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3253d9ba131dSTrond Myklebust 	.get = param_get_uint,
3254d9ba131dSTrond Myklebust };
3255d9ba131dSTrond Myklebust 
3256d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3257d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3258d9ba131dSTrond Myklebust 
3259cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3260cbf11071STrond Myklebust 		   slot_table_size, 0644);
3261d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3262d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3263cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3264cbf11071STrond Myklebust 		   slot_table_size, 0644);
3265cbf11071STrond Myklebust 
3266