blob: 97f8918169edb423e7e2c8487e13758cee97c905 [file] [log] [blame]
Chuck Levera246b012005-08-11 16:25:23 -04001/*
2 * linux/net/sunrpc/xprtsock.c
3 *
4 * Client-side transport implementation for sockets.
5 *
Alan Cox113aa832008-10-13 19:01:08 -07006 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
Chuck Levera246b012005-08-11 16:25:23 -04008 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10 *
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 *
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Lever8f9d5b12007-08-06 11:57:53 -040016 *
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
Chuck Levera246b012005-08-11 16:25:23 -040019 */
20
21#include <linux/types.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040022#include <linux/string.h>
Chuck Levera246b012005-08-11 16:25:23 -040023#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040024#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040026#include <linux/pagemap.h>
27#include <linux/errno.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/net.h>
31#include <linux/mm.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/un.h>
Chuck Levera246b012005-08-11 16:25:23 -040033#include <linux/udp.h>
34#include <linux/tcp.h>
35#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010036#include <linux/sunrpc/sched.h>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030037#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040038#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040039#include <linux/file.h>
Trond Myklebust9e00abc2011-07-13 19:20:49 -040040#ifdef CONFIG_SUNRPC_BACKCHANNEL
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040041#include <linux/sunrpc/bc_xprt.h>
42#endif
Chuck Levera246b012005-08-11 16:25:23 -040043
44#include <net/sock.h>
45#include <net/checksum.h>
46#include <net/udp.h>
47#include <net/tcp.h>
48
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030049#include "sunrpc.h"
Chuck Lever176e21e2011-05-09 15:22:44 -040050
51static void xs_close(struct rpc_xprt *xprt);
52
Chuck Lever9903cd12005-08-11 16:25:26 -040053/*
Chuck Leverc556b752005-11-01 12:24:48 -050054 * xprtsock tunables
55 */
Trond Myklebust09acfea2012-03-11 15:22:54 -040056static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
57static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
58static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
Chuck Leverc556b752005-11-01 12:24:48 -050059
Trond Myklebust09acfea2012-03-11 15:22:54 -040060static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
61static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
Chuck Leverc556b752005-11-01 12:24:48 -050062
Trond Myklebust7d1e8252009-03-11 14:38:03 -040063#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040064static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040065
Chuck Leverc556b752005-11-01 12:24:48 -050066/*
Chuck Leverfbf76682006-12-05 16:35:54 -050067 * We can register our own files under /proc/sys/sunrpc by
68 * calling register_sysctl_table() again. The files in that
69 * directory become the union of all files registered there.
70 *
71 * We simply need to make sure that we don't collide with
72 * someone else's file names!
73 */
74
75#ifdef RPC_DEBUG
76
77static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
78static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
Trond Myklebustd9ba1312011-07-17 18:11:30 -040079static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
Chuck Leverfbf76682006-12-05 16:35:54 -050080static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
81static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
82
83static struct ctl_table_header *sunrpc_table_header;
84
85/*
86 * FIXME: changing the UDP slot table size should also resize the UDP
87 * socket buffers for existing UDP transports
88 */
89static ctl_table xs_tunables_table[] = {
90 {
Chuck Leverfbf76682006-12-05 16:35:54 -050091 .procname = "udp_slot_table_entries",
92 .data = &xprt_udp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080095 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050096 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
98 },
99 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500100 .procname = "tcp_slot_table_entries",
101 .data = &xprt_tcp_slot_table_entries,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800104 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
107 },
108 {
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400109 .procname = "tcp_max_slot_table_entries",
110 .data = &xprt_max_tcp_slot_table_entries,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
113 .proc_handler = proc_dointvec_minmax,
114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_tcp_slot_table_limit
116 },
117 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500118 .procname = "min_resvport",
119 .data = &xprt_min_resvport,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800122 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500123 .extra1 = &xprt_min_resvport_limit,
124 .extra2 = &xprt_max_resvport_limit
125 },
126 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500127 .procname = "max_resvport",
128 .data = &xprt_max_resvport,
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800131 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500132 .extra1 = &xprt_min_resvport_limit,
133 .extra2 = &xprt_max_resvport_limit
134 },
135 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400136 .procname = "tcp_fin_timeout",
137 .data = &xs_tcp_fin_timeout,
138 .maxlen = sizeof(xs_tcp_fin_timeout),
139 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800140 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400141 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800142 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500143};
144
145static ctl_table sunrpc_table[] = {
146 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500147 .procname = "sunrpc",
148 .mode = 0555,
149 .child = xs_tunables_table
150 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800151 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500152};
153
154#endif
155
156/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700157 * Wait duration for a reply from the RPC portmapper.
158 */
159#define XS_BIND_TO (60U * HZ)
160
161/*
162 * Delay if a UDP socket connect error occurs. This is most likely some
163 * kind of resource problem on the local host.
164 */
165#define XS_UDP_REEST_TO (2U * HZ)
166
167/*
168 * The reestablish timeout allows clients to delay for a bit before attempting
169 * to reconnect to a server that just dropped our connection.
170 *
171 * We implement an exponential backoff when trying to reestablish a TCP
172 * transport connection with the server. Some servers like to drop a TCP
173 * connection when they are overworked, so we start with a short timeout and
174 * increase over time if the server is down or not responding.
175 */
176#define XS_TCP_INIT_REEST_TO (3U * HZ)
177#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
178
179/*
180 * TCP idle timeout; client drops the transport socket if it is idle
181 * for this long. Note that we also timeout UDP sockets to prevent
182 * holding port numbers when there is no RPC traffic.
183 */
184#define XS_IDLE_DISC_TO (5U * 60 * HZ)
185
Chuck Levera246b012005-08-11 16:25:23 -0400186#ifdef RPC_DEBUG
187# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400188# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400189#endif
190
Chuck Levera246b012005-08-11 16:25:23 -0400191#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400192static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400193{
Chuck Lever9903cd12005-08-11 16:25:26 -0400194 u8 *buf = (u8 *) packet;
195 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400196
Chuck Lever46121cf2007-01-31 12:14:08 -0500197 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400198 for (j = 0; j < count && j < 128; j += 4) {
199 if (!(j & 31)) {
200 if (j)
201 dprintk("\n");
202 dprintk("0x%04x ", j);
203 }
204 dprintk("%02x%02x%02x%02x ",
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
206 }
207 dprintk("\n");
208}
209#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400210static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400211{
212 /* NOP */
213}
214#endif
215
Chuck Leverffc2e512006-12-05 16:35:11 -0500216struct sock_xprt {
217 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500218
219 /*
220 * Network layer
221 */
222 struct socket * sock;
223 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500224
225 /*
226 * State of TCP reply receive
227 */
228 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400229 tcp_xid,
230 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500231
232 u32 tcp_offset,
233 tcp_reclen;
234
235 unsigned long tcp_copied,
236 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500237
238 /*
239 * Connection of transports
240 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500241 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400242 struct sockaddr_storage srcaddr;
243 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500244
245 /*
246 * UDP socket buffer size parameters
247 */
248 size_t rcvsize,
249 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500250
251 /*
252 * Saved socket callback addresses
253 */
254 void (*old_data_ready)(struct sock *, int);
255 void (*old_state_change)(struct sock *);
256 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400257 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500258};
259
Chuck Levere136d092006-12-05 16:35:23 -0500260/*
261 * TCP receive state flags
262 */
263#define TCP_RCV_LAST_FRAG (1UL << 0)
264#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
265#define TCP_RCV_COPY_XID (1UL << 2)
266#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400267#define TCP_RCV_READ_CALLDIR (1UL << 4)
268#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400269
270/*
271 * TCP RPC flags
272 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400273#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500274
Chuck Lever95392c52007-08-06 11:57:58 -0400275static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400276{
Chuck Lever95392c52007-08-06 11:57:58 -0400277 return (struct sockaddr *) &xprt->addr;
278}
279
Chuck Lever176e21e2011-05-09 15:22:44 -0400280static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
281{
282 return (struct sockaddr_un *) &xprt->addr;
283}
284
Chuck Lever95392c52007-08-06 11:57:58 -0400285static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
286{
287 return (struct sockaddr_in *) &xprt->addr;
288}
289
290static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
291{
292 return (struct sockaddr_in6 *) &xprt->addr;
293}
294
Chuck Leverc877b842009-08-09 15:09:36 -0400295static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400296{
Chuck Leverc877b842009-08-09 15:09:36 -0400297 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400298 struct sockaddr_in6 *sin6;
299 struct sockaddr_in *sin;
Chuck Lever176e21e2011-05-09 15:22:44 -0400300 struct sockaddr_un *sun;
Chuck Leverc877b842009-08-09 15:09:36 -0400301 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400302
Chuck Lever9dc3b092009-08-09 15:09:46 -0400303 switch (sap->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400304 case AF_LOCAL:
305 sun = xs_addr_un(xprt);
306 strlcpy(buf, sun->sun_path, sizeof(buf));
307 xprt->address_strings[RPC_DISPLAY_ADDR] =
308 kstrdup(buf, GFP_KERNEL);
309 break;
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 case AF_INET:
Chuck Lever176e21e2011-05-09 15:22:44 -0400311 (void)rpc_ntop(sap, buf, sizeof(buf));
312 xprt->address_strings[RPC_DISPLAY_ADDR] =
313 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400314 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800315 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400316 break;
317 case AF_INET6:
Chuck Lever176e21e2011-05-09 15:22:44 -0400318 (void)rpc_ntop(sap, buf, sizeof(buf));
319 xprt->address_strings[RPC_DISPLAY_ADDR] =
320 kstrdup(buf, GFP_KERNEL);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400321 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800322 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 break;
324 default:
325 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400326 }
Chuck Lever176e21e2011-05-09 15:22:44 -0400327
Chuck Lever9dc3b092009-08-09 15:09:46 -0400328 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400329}
330
Chuck Lever9dc3b092009-08-09 15:09:46 -0400331static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400332{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400333 struct sockaddr *sap = xs_addr(xprt);
334 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400335
Joe Perches81160e662010-03-08 12:15:59 -0800336 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400337 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400338
Joe Perches81160e662010-03-08 12:15:59 -0800339 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400340 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
341}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400342
Chuck Lever9dc3b092009-08-09 15:09:46 -0400343static void xs_format_peer_addresses(struct rpc_xprt *xprt,
344 const char *protocol,
345 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400346{
Chuck Leverb454ae92008-01-07 18:34:48 -0500347 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500348 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400349 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400350 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400351}
352
Chuck Lever9dc3b092009-08-09 15:09:46 -0400353static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400354{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400355 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
356 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400357
Chuck Lever9dc3b092009-08-09 15:09:46 -0400358 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400359}
360
361static void xs_free_peer_addresses(struct rpc_xprt *xprt)
362{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500363 unsigned int i;
364
365 for (i = 0; i < RPC_DISPLAY_MAX; i++)
366 switch (i) {
367 case RPC_DISPLAY_PROTO:
368 case RPC_DISPLAY_NETID:
369 continue;
370 default:
371 kfree(xprt->address_strings[i]);
372 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400373}
374
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400375#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
376
Trond Myklebust24c56842006-10-17 15:06:22 -0400377static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400378{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 struct msghdr msg = {
380 .msg_name = addr,
381 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400382 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
383 };
384 struct kvec iov = {
385 .iov_base = vec->iov_base + base,
386 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400387 };
388
Trond Myklebust24c56842006-10-17 15:06:22 -0400389 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400390 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
392}
393
Trond Myklebust24c56842006-10-17 15:06:22 -0400394static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400395{
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 struct page **ppage;
397 unsigned int remainder;
398 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400399
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 remainder = xdr->page_len - base;
401 base += xdr->page_base;
402 ppage = xdr->pages + (base >> PAGE_SHIFT);
403 base &= ~PAGE_MASK;
404 for(;;) {
405 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
406 int flags = XS_SENDMSG_FLAGS;
407
408 remainder -= len;
409 if (remainder != 0 || more)
410 flags |= MSG_MORE;
411 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
412 if (remainder == 0 || err != len)
413 break;
414 sent += err;
415 ppage++;
416 base = 0;
417 }
418 if (sent == 0)
419 return err;
420 if (err > 0)
421 sent += err;
422 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400423}
424
Chuck Lever9903cd12005-08-11 16:25:26 -0400425/**
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
432 *
Chuck Levera246b012005-08-11 16:25:23 -0400433 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400434static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
Chuck Levera246b012005-08-11 16:25:23 -0400435{
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 unsigned int remainder = xdr->len - base;
437 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400438
Chuck Lever262965f2005-08-11 16:25:56 -0400439 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400440 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400441
442 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 if (base != 0) {
444 addr = NULL;
445 addrlen = 0;
446 }
Chuck Lever262965f2005-08-11 16:25:56 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base < xdr->head[0].iov_len || addr != NULL) {
449 unsigned int len = xdr->head[0].iov_len - base;
450 remainder -= len;
451 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
452 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400453 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400454 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400455 base = 0;
456 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400457 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400458
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 if (base < xdr->page_len) {
460 unsigned int len = xdr->page_len - base;
461 remainder -= len;
462 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
463 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400464 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400465 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400466 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 } else
468 base -= xdr->page_len;
469
470 if (base >= xdr->tail[0].iov_len)
471 return sent;
472 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400473out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400474 if (sent == 0)
475 return err;
476 if (err > 0)
477 sent += err;
478 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400479}
480
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400481static void xs_nospace_callback(struct rpc_task *task)
482{
483 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
484
485 transport->inet->sk_write_pending--;
486 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
487}
488
Chuck Lever9903cd12005-08-11 16:25:26 -0400489/**
Chuck Lever262965f2005-08-11 16:25:56 -0400490 * xs_nospace - place task on wait queue if transmit was incomplete
491 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400492 *
Chuck Levera246b012005-08-11 16:25:23 -0400493 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400494static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400495{
Chuck Lever262965f2005-08-11 16:25:56 -0400496 struct rpc_rqst *req = task->tk_rqstp;
497 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500498 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust24ca9a82011-11-22 14:44:28 +0200499 int ret = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400500
Chuck Lever46121cf2007-01-31 12:14:08 -0500501 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400502 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
503 req->rq_slen);
504
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400505 /* Protect against races with write_space */
506 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400507
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400508 /* Don't race with disconnect */
509 if (xprt_connected(xprt)) {
510 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
511 /*
512 * Notify TCP that we're limited by the application
513 * window size
514 */
515 set_bit(SOCK_NOSPACE, &transport->sock->flags);
516 transport->inet->sk_write_pending++;
517 /* ...and wait for more buffer space */
518 xprt_wait_for_buffer_space(task, xs_nospace_callback);
519 }
520 } else {
521 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400522 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400523 }
Chuck Lever262965f2005-08-11 16:25:56 -0400524
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400525 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400526 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400527}
528
Chuck Lever61677ee2011-05-09 15:22:34 -0400529/*
530 * Construct a stream transport record marker in @buf.
531 */
532static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
533{
534 u32 reclen = buf->len - sizeof(rpc_fraghdr);
535 rpc_fraghdr *base = buf->head[0].iov_base;
536 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
537}
538
Chuck Lever262965f2005-08-11 16:25:56 -0400539/**
Chuck Lever176e21e2011-05-09 15:22:44 -0400540 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
541 * @task: RPC task that manages the state of an RPC request
542 *
543 * Return values:
544 * 0: The request has been sent
545 * EAGAIN: The socket was blocked, please call again later to
546 * complete the request
547 * ENOTCONN: Caller needs to invoke connect logic then call again
548 * other: Some other error occured, the request was not sent
549 */
550static int xs_local_send_request(struct rpc_task *task)
551{
552 struct rpc_rqst *req = task->tk_rqstp;
553 struct rpc_xprt *xprt = req->rq_xprt;
554 struct sock_xprt *transport =
555 container_of(xprt, struct sock_xprt, xprt);
556 struct xdr_buf *xdr = &req->rq_snd_buf;
557 int status;
558
559 xs_encode_stream_record_marker(&req->rq_snd_buf);
560
561 xs_pktdump("packet data:",
562 req->rq_svec->iov_base, req->rq_svec->iov_len);
563
564 status = xs_sendpages(transport->sock, NULL, 0,
565 xdr, req->rq_bytes_sent);
566 dprintk("RPC: %s(%u) = %d\n",
567 __func__, xdr->len - req->rq_bytes_sent, status);
568 if (likely(status >= 0)) {
569 req->rq_bytes_sent += status;
570 req->rq_xmit_bytes_sent += status;
571 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
572 req->rq_bytes_sent = 0;
573 return 0;
574 }
575 status = -EAGAIN;
576 }
577
578 switch (status) {
579 case -EAGAIN:
580 status = xs_nospace(task);
581 break;
582 default:
583 dprintk("RPC: sendmsg returned unrecognized error %d\n",
584 -status);
585 case -EPIPE:
586 xs_close(xprt);
587 status = -ENOTCONN;
588 }
589
590 return status;
591}
592
593/**
Chuck Lever262965f2005-08-11 16:25:56 -0400594 * xs_udp_send_request - write an RPC request to a UDP socket
595 * @task: address of RPC task that manages the state of an RPC request
596 *
597 * Return values:
598 * 0: The request has been sent
599 * EAGAIN: The socket was blocked, please call again later to
600 * complete the request
601 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300602 * other: Some other error occurred, the request was not sent
Chuck Lever262965f2005-08-11 16:25:56 -0400603 */
604static int xs_udp_send_request(struct rpc_task *task)
605{
606 struct rpc_rqst *req = task->tk_rqstp;
607 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500608 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400609 struct xdr_buf *xdr = &req->rq_snd_buf;
610 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400611
Chuck Lever9903cd12005-08-11 16:25:26 -0400612 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400613 req->rq_svec->iov_base,
614 req->rq_svec->iov_len);
615
Trond Myklebust01d37c42009-03-11 14:09:39 -0400616 if (!xprt_bound(xprt))
617 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500618 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400619 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500620 xprt->addrlen, xdr,
621 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400622
Chuck Lever46121cf2007-01-31 12:14:08 -0500623 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400624 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400625
Trond Myklebust21997002007-10-01 11:43:37 -0400626 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400627 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400628 if (status >= req->rq_slen)
629 return 0;
630 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400631 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400632 }
Chuck Levera246b012005-08-11 16:25:23 -0400633
Chuck Lever262965f2005-08-11 16:25:56 -0400634 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400635 case -ENOTSOCK:
636 status = -ENOTCONN;
637 /* Should we call xs_close() here? */
638 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400639 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400640 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400641 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400642 default:
643 dprintk("RPC: sendmsg returned unrecognized error %d\n",
644 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400645 case -ENETUNREACH:
646 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400647 case -ECONNREFUSED:
648 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400649 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400650 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400651 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800652
Chuck Lever262965f2005-08-11 16:25:56 -0400653 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400654}
655
Trond Myklebuste06799f2007-11-05 15:44:12 -0500656/**
657 * xs_tcp_shutdown - gracefully shut down a TCP socket
658 * @xprt: transport
659 *
660 * Initiates a graceful shutdown of the TCP socket by calling the
661 * equivalent of shutdown(SHUT_WR);
662 */
663static void xs_tcp_shutdown(struct rpc_xprt *xprt)
664{
665 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
666 struct socket *sock = transport->sock;
667
668 if (sock != NULL)
669 kernel_sock_shutdown(sock, SHUT_WR);
670}
671
Chuck Lever9903cd12005-08-11 16:25:26 -0400672/**
Chuck Lever262965f2005-08-11 16:25:56 -0400673 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400674 * @task: address of RPC task that manages the state of an RPC request
675 *
676 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400677 * 0: The request has been sent
678 * EAGAIN: The socket was blocked, please call again later to
679 * complete the request
680 * ENOTCONN: Caller needs to invoke connect logic then call again
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300681 * other: Some other error occurred, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 *
683 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400684 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400685 */
Chuck Lever262965f2005-08-11 16:25:56 -0400686static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400687{
688 struct rpc_rqst *req = task->tk_rqstp;
689 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500690 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400691 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400692 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400693
Chuck Lever61677ee2011-05-09 15:22:34 -0400694 xs_encode_stream_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400695
Chuck Lever262965f2005-08-11 16:25:56 -0400696 xs_pktdump("packet data:",
697 req->rq_svec->iov_base,
698 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400699
700 /* Continue transmitting the packet/record. We must be careful
701 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400702 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400703 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500704 status = xs_sendpages(transport->sock,
705 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400706
Chuck Lever46121cf2007-01-31 12:14:08 -0500707 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400708 xdr->len - req->rq_bytes_sent, status);
709
710 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400711 break;
712
Chuck Lever262965f2005-08-11 16:25:56 -0400713 /* If we've sent the entire packet, immediately
714 * reset the count of bytes sent. */
715 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400716 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400717 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
718 req->rq_bytes_sent = 0;
719 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400720 }
721
Trond Myklebust06b4b682008-04-16 16:51:38 -0400722 if (status != 0)
723 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400724 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400725 break;
Chuck Levera246b012005-08-11 16:25:23 -0400726 }
727
Chuck Lever262965f2005-08-11 16:25:56 -0400728 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400729 case -ENOTSOCK:
730 status = -ENOTCONN;
731 /* Should we call xs_close() here? */
732 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400733 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400734 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400735 break;
736 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500737 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400738 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400739 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400740 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500741 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400742 case -ECONNREFUSED:
743 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400744 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400745 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800746
Chuck Levera246b012005-08-11 16:25:23 -0400747 return status;
748}
749
Chuck Lever9903cd12005-08-11 16:25:26 -0400750/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400751 * xs_tcp_release_xprt - clean up after a tcp transmission
752 * @xprt: transport
753 * @task: rpc task
754 *
755 * This cleans up if an error causes us to abort the transmission of a request.
756 * In this case, the socket may need to be reset in order to avoid confusing
757 * the server.
758 */
759static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
760{
761 struct rpc_rqst *req;
762
763 if (task != xprt->snd_task)
764 return;
765 if (task == NULL)
766 goto out_release;
767 req = task->tk_rqstp;
Trond Myklebust43cedbf0e2011-07-17 16:01:03 -0400768 if (req == NULL)
769 goto out_release;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400770 if (req->rq_bytes_sent == 0)
771 goto out_release;
772 if (req->rq_bytes_sent == req->rq_snd_buf.len)
773 goto out_release;
774 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
775out_release:
776 xprt_release_xprt(xprt, task);
777}
778
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400779static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
780{
781 transport->old_data_ready = sk->sk_data_ready;
782 transport->old_state_change = sk->sk_state_change;
783 transport->old_write_space = sk->sk_write_space;
784 transport->old_error_report = sk->sk_error_report;
785}
786
787static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
788{
789 sk->sk_data_ready = transport->old_data_ready;
790 sk->sk_state_change = transport->old_state_change;
791 sk->sk_write_space = transport->old_write_space;
792 sk->sk_error_report = transport->old_error_report;
793}
794
Chuck Leverfe315e762009-03-11 14:10:21 -0400795static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400796{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500797 struct socket *sock = transport->sock;
798 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400799
Chuck Leverfe315e762009-03-11 14:10:21 -0400800 if (sk == NULL)
801 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400802
Trond Myklebust246408d2011-03-22 18:40:10 -0400803 transport->srcport = 0;
804
Chuck Levera246b012005-08-11 16:25:23 -0400805 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500806 transport->inet = NULL;
807 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400808
Chuck Lever9903cd12005-08-11 16:25:26 -0400809 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400810
811 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400812 write_unlock_bh(&sk->sk_callback_lock);
813
Chuck Lever9903cd12005-08-11 16:25:26 -0400814 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400815
816 sock_release(sock);
Chuck Leverfe315e762009-03-11 14:10:21 -0400817}
818
819/**
820 * xs_close - close a socket
821 * @xprt: transport
822 *
823 * This is used when all requests are complete; ie, no DRC state remains
824 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400825 *
826 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
827 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -0400828 */
829static void xs_close(struct rpc_xprt *xprt)
830{
831 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
832
833 dprintk("RPC: xs_close xprt %p\n", xprt);
834
835 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400836 xprt->reestablish_timeout = 0;
Chuck Leverfe315e762009-03-11 14:10:21 -0400837
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100838 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400839 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100840 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500841 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100842 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500843 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400844}
845
Trond Myklebustf75e6742009-04-21 17:18:20 -0400846static void xs_tcp_close(struct rpc_xprt *xprt)
847{
848 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
849 xs_close(xprt);
850 else
851 xs_tcp_shutdown(xprt);
852}
853
Chuck Lever9903cd12005-08-11 16:25:26 -0400854/**
855 * xs_destroy - prepare to shutdown a transport
856 * @xprt: doomed transport
857 *
858 */
859static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400860{
Chuck Leverc8475462006-12-05 16:35:26 -0500861 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
862
Chuck Lever46121cf2007-01-31 12:14:08 -0500863 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400864
Tejun Heoafe2c512010-12-14 16:21:17 +0100865 cancel_delayed_work_sync(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400866
Chuck Lever9903cd12005-08-11 16:25:26 -0400867 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400868 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400869 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400870 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400871}
872
Chuck Lever9903cd12005-08-11 16:25:26 -0400873static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400874{
Chuck Lever9903cd12005-08-11 16:25:26 -0400875 return (struct rpc_xprt *) sk->sk_user_data;
876}
877
Chuck Lever176e21e2011-05-09 15:22:44 -0400878static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
879{
880 struct xdr_skb_reader desc = {
881 .skb = skb,
882 .offset = sizeof(rpc_fraghdr),
883 .count = skb->len - sizeof(rpc_fraghdr),
884 };
885
886 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
887 return -1;
888 if (desc.count)
889 return -1;
890 return 0;
891}
892
893/**
894 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
895 * @sk: socket with data to read
896 * @len: how much data to read
897 *
898 * Currently this assumes we can read the whole reply in a single gulp.
899 */
900static void xs_local_data_ready(struct sock *sk, int len)
901{
902 struct rpc_task *task;
903 struct rpc_xprt *xprt;
904 struct rpc_rqst *rovr;
905 struct sk_buff *skb;
906 int err, repsize, copied;
907 u32 _xid;
908 __be32 *xp;
909
910 read_lock_bh(&sk->sk_callback_lock);
911 dprintk("RPC: %s...\n", __func__);
912 xprt = xprt_from_sock(sk);
913 if (xprt == NULL)
914 goto out;
915
916 skb = skb_recv_datagram(sk, 0, 1, &err);
917 if (skb == NULL)
918 goto out;
919
920 if (xprt->shutdown)
921 goto dropit;
922
923 repsize = skb->len - sizeof(rpc_fraghdr);
924 if (repsize < 4) {
925 dprintk("RPC: impossible RPC reply size %d\n", repsize);
926 goto dropit;
927 }
928
929 /* Copy the XID from the skb... */
930 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
931 if (xp == NULL)
932 goto dropit;
933
934 /* Look up and lock the request corresponding to the given XID */
935 spin_lock(&xprt->transport_lock);
936 rovr = xprt_lookup_rqst(xprt, *xp);
937 if (!rovr)
938 goto out_unlock;
939 task = rovr->rq_task;
940
941 copied = rovr->rq_private_buf.buflen;
942 if (copied > repsize)
943 copied = repsize;
944
945 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
946 dprintk("RPC: sk_buff copy failed\n");
947 goto out_unlock;
948 }
949
950 xprt_complete_rqst(task, copied);
951
952 out_unlock:
953 spin_unlock(&xprt->transport_lock);
954 dropit:
955 skb_free_datagram(sk, skb);
956 out:
957 read_unlock_bh(&sk->sk_callback_lock);
958}
959
Chuck Lever9903cd12005-08-11 16:25:26 -0400960/**
961 * xs_udp_data_ready - "data ready" callback for UDP sockets
962 * @sk: socket with data to read
963 * @len: how much data to read
964 *
965 */
966static void xs_udp_data_ready(struct sock *sk, int len)
967{
968 struct rpc_task *task;
969 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400970 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400971 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400972 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700973 u32 _xid;
974 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400975
Eric Dumazetf064af12010-09-22 12:43:39 +0000976 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500977 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400978 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400979 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400980
981 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
982 goto out;
983
984 if (xprt->shutdown)
985 goto dropit;
986
987 repsize = skb->len - sizeof(struct udphdr);
988 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500989 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400990 goto dropit;
991 }
992
993 /* Copy the XID from the skb... */
994 xp = skb_header_pointer(skb, sizeof(struct udphdr),
995 sizeof(_xid), &_xid);
996 if (xp == NULL)
997 goto dropit;
998
999 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001000 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001001 rovr = xprt_lookup_rqst(xprt, *xp);
1002 if (!rovr)
1003 goto out_unlock;
1004 task = rovr->rq_task;
1005
Chuck Levera246b012005-08-11 16:25:23 -04001006 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1007 copied = repsize;
1008
1009 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -08001010 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1011 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -04001012 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -08001013 }
1014
1015 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -04001016
Chuck Lever1570c1e2005-08-25 16:25:52 -07001017 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -07001018 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -04001019
1020 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001021 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001022 dropit:
1023 skb_free_datagram(sk, skb);
1024 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001025 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001026}
1027
Trond Myklebusta519fc72012-09-12 16:49:15 -04001028/*
1029 * Helper function to force a TCP close if the server is sending
1030 * junk and/or it has put us in CLOSE_WAIT
1031 */
1032static void xs_tcp_force_close(struct rpc_xprt *xprt)
1033{
1034 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1035 xprt_force_disconnect(xprt);
1036}
1037
Chuck Leverdd456472006-12-05 16:35:44 -05001038static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001039{
Chuck Lever51971132006-12-05 16:35:19 -05001040 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001041 size_t len, used;
1042 char *p;
1043
Chuck Lever51971132006-12-05 16:35:19 -05001044 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1045 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001046 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001047 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001048 if (used != len)
1049 return;
Chuck Lever808012f2005-08-25 16:25:49 -07001050
Chuck Lever51971132006-12-05 16:35:19 -05001051 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1052 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -05001053 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -04001054 else
Chuck Levere136d092006-12-05 16:35:23 -05001055 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -05001056 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -07001057
Chuck Levere136d092006-12-05 16:35:23 -05001058 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001059 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001060
Chuck Levera246b012005-08-11 16:25:23 -04001061 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001062 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001063 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001064 xs_tcp_force_close(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 return;
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001067 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -05001068 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001069}
1070
Chuck Lever51971132006-12-05 16:35:19 -05001071static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -04001072{
Chuck Lever51971132006-12-05 16:35:19 -05001073 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001074 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -05001075 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001076 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1077 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1078 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001079 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001080 }
1081 }
1082}
1083
Chuck Leverdd456472006-12-05 16:35:44 -05001084static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001085{
1086 size_t len, used;
1087 char *p;
1088
Chuck Lever51971132006-12-05 16:35:19 -05001089 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -05001090 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001091 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -05001092 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001093 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001094 if (used != len)
1095 return;
Chuck Levere136d092006-12-05 16:35:23 -05001096 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001097 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001098 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001099 dprintk("RPC: reading %s XID %08x\n",
1100 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1101 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001102 ntohl(transport->tcp_xid));
1103 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001104}
1105
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001106static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1107 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001108{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001109 size_t len, used;
1110 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001111 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001112
1113 /*
1114 * We want transport->tcp_offset to be 8 at the end of this routine
1115 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1116 * When this function is called for the first time,
1117 * transport->tcp_offset is 4 (after having already read the xid).
1118 */
1119 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001120 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001121 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001122 p = ((char *) &transport->tcp_calldir) + offset;
1123 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001124 transport->tcp_offset += used;
1125 if (used != len)
1126 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001127 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001128 /*
1129 * We don't yet have the XDR buffer, so we will write the calldir
1130 * out after we get the buffer from the 'struct rpc_rqst'
1131 */
Trond Myklebustb76ce562010-06-16 13:57:32 -04001132 switch (ntohl(transport->tcp_calldir)) {
1133 case RPC_REPLY:
1134 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1135 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001136 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001137 break;
1138 case RPC_CALL:
1139 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1140 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001141 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -04001142 break;
1143 default:
1144 dprintk("RPC: invalid request message type\n");
Trond Myklebusta519fc72012-09-12 16:49:15 -04001145 xs_tcp_force_close(&transport->xprt);
Trond Myklebustb76ce562010-06-16 13:57:32 -04001146 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001147 xs_tcp_check_fraghdr(transport);
1148}
1149
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001150static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1151 struct xdr_skb_reader *desc,
1152 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001153{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001154 struct sock_xprt *transport =
1155 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001156 struct xdr_buf *rcvbuf;
1157 size_t len;
1158 ssize_t r;
1159
Chuck Levera246b012005-08-11 16:25:23 -04001160 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001161
1162 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1163 /*
1164 * Save the RPC direction in the XDR buffer
1165 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001166 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -04001167 &transport->tcp_calldir,
1168 sizeof(transport->tcp_calldir));
1169 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001170 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001171 }
1172
Chuck Levera246b012005-08-11 16:25:23 -04001173 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001174 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001175 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001176
Chuck Lever51971132006-12-05 16:35:19 -05001177 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001178 memcpy(&my_desc, desc, sizeof(my_desc));
1179 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001180 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001181 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001182 desc->count -= r;
1183 desc->offset += r;
1184 } else
Chuck Lever51971132006-12-05 16:35:19 -05001185 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001186 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001187
1188 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001189 transport->tcp_copied += r;
1190 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001191 }
1192 if (r != len) {
1193 /* Error when copying to the receive buffer,
1194 * usually because we weren't able to allocate
1195 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001196 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001197 * will not receive any additional updates,
1198 * and time out.
1199 * Any remaining data from this record will
1200 * be discarded.
1201 */
Chuck Levere136d092006-12-05 16:35:23 -05001202 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001203 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001204 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1206 "tcp_offset = %u, tcp_reclen = %u\n",
1207 xprt, transport->tcp_copied,
1208 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001209 return;
Chuck Levera246b012005-08-11 16:25:23 -04001210 }
1211
Chuck Lever46121cf2007-01-31 12:14:08 -05001212 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001213 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001214 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1215 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1216 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001217
Chuck Lever51971132006-12-05 16:35:19 -05001218 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001219 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001220 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001221 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1222 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001223 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001224}
1225
1226/*
1227 * Finds the request corresponding to the RPC xid and invokes the common
1228 * tcp read code to read the data.
1229 */
1230static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1231 struct xdr_skb_reader *desc)
1232{
1233 struct sock_xprt *transport =
1234 container_of(xprt, struct sock_xprt, xprt);
1235 struct rpc_rqst *req;
1236
1237 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1238
1239 /* Find and lock the request corresponding to this xid */
1240 spin_lock(&xprt->transport_lock);
1241 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1242 if (!req) {
1243 dprintk("RPC: XID %08x request not found!\n",
1244 ntohl(transport->tcp_xid));
1245 spin_unlock(&xprt->transport_lock);
1246 return -1;
1247 }
1248
1249 xs_tcp_read_common(xprt, desc, req);
1250
Chuck Levere136d092006-12-05 16:35:23 -05001251 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001252 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001253
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001254 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001255 return 0;
1256}
1257
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001258#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001259/*
1260 * Obtains an rpc_rqst previously allocated and invokes the common
1261 * tcp read code to read the data. The result is placed in the callback
1262 * queue.
1263 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1264 * connection and return -1.
1265 */
1266static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1267 struct xdr_skb_reader *desc)
1268{
1269 struct sock_xprt *transport =
1270 container_of(xprt, struct sock_xprt, xprt);
1271 struct rpc_rqst *req;
1272
1273 req = xprt_alloc_bc_request(xprt);
1274 if (req == NULL) {
1275 printk(KERN_WARNING "Callback slot table overflowed\n");
1276 xprt_force_disconnect(xprt);
1277 return -1;
1278 }
1279
1280 req->rq_xid = transport->tcp_xid;
1281 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1282 xs_tcp_read_common(xprt, desc, req);
1283
1284 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1285 struct svc_serv *bc_serv = xprt->bc_serv;
1286
1287 /*
1288 * Add callback request to callback list. The callback
1289 * service sleeps on the sv_cb_waitq waiting for new
1290 * requests. Wake it up after adding enqueing the
1291 * request.
1292 */
1293 dprintk("RPC: add callback request to list\n");
1294 spin_lock(&bc_serv->sv_cb_lock);
1295 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1296 spin_unlock(&bc_serv->sv_cb_lock);
1297 wake_up(&bc_serv->sv_cb_waitq);
1298 }
1299
1300 req->rq_private_buf.len = transport->tcp_copied;
1301
1302 return 0;
1303}
1304
1305static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1306 struct xdr_skb_reader *desc)
1307{
1308 struct sock_xprt *transport =
1309 container_of(xprt, struct sock_xprt, xprt);
1310
1311 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1312 xs_tcp_read_reply(xprt, desc) :
1313 xs_tcp_read_callback(xprt, desc);
1314}
1315#else
1316static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1317 struct xdr_skb_reader *desc)
1318{
1319 return xs_tcp_read_reply(xprt, desc);
1320}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001321#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001322
1323/*
1324 * Read data off the transport. This can be either an RPC_CALL or an
1325 * RPC_REPLY. Relay the processing to helper functions.
1326 */
1327static void xs_tcp_read_data(struct rpc_xprt *xprt,
1328 struct xdr_skb_reader *desc)
1329{
1330 struct sock_xprt *transport =
1331 container_of(xprt, struct sock_xprt, xprt);
1332
1333 if (_xs_tcp_read_data(xprt, desc) == 0)
1334 xs_tcp_check_fraghdr(transport);
1335 else {
1336 /*
1337 * The transport_lock protects the request handling.
1338 * There's no need to hold it to update the tcp_flags.
1339 */
1340 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1341 }
Chuck Levera246b012005-08-11 16:25:23 -04001342}
1343
Chuck Leverdd456472006-12-05 16:35:44 -05001344static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001345{
1346 size_t len;
1347
Chuck Lever51971132006-12-05 16:35:19 -05001348 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001349 if (len > desc->count)
1350 len = desc->count;
1351 desc->count -= len;
1352 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001353 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001354 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001355 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001356}
1357
Chuck Lever9903cd12005-08-11 16:25:26 -04001358static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -04001359{
1360 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001361 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001362 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001363 .skb = skb,
1364 .offset = offset,
1365 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001366 };
Chuck Levera246b012005-08-11 16:25:23 -04001367
Chuck Lever46121cf2007-01-31 12:14:08 -05001368 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001369 do {
1370 /* Read in a new fragment marker if necessary */
1371 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001372 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001373 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001374 continue;
1375 }
1376 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001377 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001378 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001379 continue;
1380 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001381 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001382 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001383 xs_tcp_read_calldir(transport, &desc);
1384 continue;
1385 }
Chuck Levera246b012005-08-11 16:25:23 -04001386 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001387 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001388 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001389 continue;
1390 }
1391 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001392 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001393 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001394 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001395 return len - desc.count;
1396}
1397
Chuck Lever9903cd12005-08-11 16:25:26 -04001398/**
1399 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1400 * @sk: socket with data to read
1401 * @bytes: how much data to read
1402 *
1403 */
1404static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001405{
1406 struct rpc_xprt *xprt;
1407 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001408 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001409
Chuck Lever46121cf2007-01-31 12:14:08 -05001410 dprintk("RPC: xs_tcp_data_ready...\n");
1411
Eric Dumazetf064af12010-09-22 12:43:39 +00001412 read_lock_bh(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001413 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001414 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001415 if (xprt->shutdown)
1416 goto out;
1417
Neil Brown61d0a8e2009-09-23 14:36:37 -04001418 /* Any data means we had a useful conversation, so
1419 * the we don't need to delay the next reconnect
1420 */
1421 if (xprt->reestablish_timeout)
1422 xprt->reestablish_timeout = 0;
1423
Chuck Lever9903cd12005-08-11 16:25:26 -04001424 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001425 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001426 do {
1427 rd_desc.count = 65536;
1428 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1429 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001430out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001431 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001432}
1433
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001434/*
1435 * Do the equivalent of linger/linger2 handling for dealing with
1436 * broken servers that don't close the socket in a timely
1437 * fashion
1438 */
1439static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1440 unsigned long timeout)
1441{
1442 struct sock_xprt *transport;
1443
1444 if (xprt_test_and_set_connecting(xprt))
1445 return;
1446 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1447 transport = container_of(xprt, struct sock_xprt, xprt);
1448 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1449 timeout);
1450}
1451
1452static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1453{
1454 struct sock_xprt *transport;
1455
1456 transport = container_of(xprt, struct sock_xprt, xprt);
1457
1458 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1459 !cancel_delayed_work(&transport->connect_worker))
1460 return;
1461 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1462 xprt_clear_connecting(xprt);
1463}
1464
1465static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1466{
1467 smp_mb__before_clear_bit();
Trond Myklebusta519fc72012-09-12 16:49:15 -04001468 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1469 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001470 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1471 clear_bit(XPRT_CLOSING, &xprt->state);
1472 smp_mb__after_clear_bit();
1473 /* Mark transport as closed and wake up all pending tasks */
1474 xprt_disconnect_done(xprt);
1475}
1476
Chuck Lever9903cd12005-08-11 16:25:26 -04001477/**
1478 * xs_tcp_state_change - callback to handle TCP socket state changes
1479 * @sk: socket whose state has changed
1480 *
1481 */
1482static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001483{
Chuck Lever9903cd12005-08-11 16:25:26 -04001484 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001485
Eric Dumazetf064af12010-09-22 12:43:39 +00001486 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001487 if (!(xprt = xprt_from_sock(sk)))
1488 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001489 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001490 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001491 sk->sk_state, xprt_connected(xprt),
1492 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001493 sock_flag(sk, SOCK_ZAPPED),
1494 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001495
1496 switch (sk->sk_state) {
1497 case TCP_ESTABLISHED:
Eric Dumazetf064af12010-09-22 12:43:39 +00001498 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001499 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001500 struct sock_xprt *transport = container_of(xprt,
1501 struct sock_xprt, xprt);
1502
Chuck Levera246b012005-08-11 16:25:23 -04001503 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001504 transport->tcp_offset = 0;
1505 transport->tcp_reclen = 0;
1506 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001507 transport->tcp_flags =
1508 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001509
Trond Myklebust2a491992009-03-11 14:38:00 -04001510 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001511 }
Eric Dumazetf064af12010-09-22 12:43:39 +00001512 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001513 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001514 case TCP_FIN_WAIT1:
1515 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001516 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001517 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001518 set_bit(XPRT_CLOSING, &xprt->state);
1519 smp_mb__before_clear_bit();
1520 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001521 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001522 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001523 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001524 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001525 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001526 /* The server initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001527 xprt->connect_cookie++;
Trond Myklebusta519fc72012-09-12 16:49:15 -04001528 xs_tcp_force_close(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001529 case TCP_CLOSING:
1530 /*
1531 * If the server closed down the connection, make sure that
1532 * we back off before reconnecting
1533 */
1534 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1535 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001536 break;
1537 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001538 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001539 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001540 smp_mb__before_clear_bit();
1541 clear_bit(XPRT_CONNECTED, &xprt->state);
1542 smp_mb__after_clear_bit();
1543 break;
1544 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001545 xs_tcp_cancel_linger_timeout(xprt);
1546 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001547 }
1548 out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001549 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001550}
1551
Chuck Lever9903cd12005-08-11 16:25:26 -04001552/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001553 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001554 * @sk: socket
1555 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001556static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001557{
1558 struct rpc_xprt *xprt;
1559
Eric Dumazetf064af12010-09-22 12:43:39 +00001560 read_lock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001561 if (!(xprt = xprt_from_sock(sk)))
1562 goto out;
1563 dprintk("RPC: %s client %p...\n"
1564 "RPC: error %d\n",
1565 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001566 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001567out:
Eric Dumazetf064af12010-09-22 12:43:39 +00001568 read_unlock_bh(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001569}
1570
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001571static void xs_write_space(struct sock *sk)
1572{
1573 struct socket *sock;
1574 struct rpc_xprt *xprt;
1575
1576 if (unlikely(!(sock = sk->sk_socket)))
1577 return;
1578 clear_bit(SOCK_NOSPACE, &sock->flags);
1579
1580 if (unlikely(!(xprt = xprt_from_sock(sk))))
1581 return;
1582 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1583 return;
1584
1585 xprt_write_space(xprt);
1586}
1587
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001588/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001589 * xs_udp_write_space - callback invoked when socket buffer space
1590 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001591 * @sk: socket whose state has changed
1592 *
Chuck Levera246b012005-08-11 16:25:23 -04001593 * Called when more output buffer space is available for this socket.
1594 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001595 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001596 * with a bunch of small requests.
1597 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001598static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001599{
Eric Dumazetf064af12010-09-22 12:43:39 +00001600 read_lock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001601
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001602 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001603 if (sock_writeable(sk))
1604 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001605
Eric Dumazetf064af12010-09-22 12:43:39 +00001606 read_unlock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001607}
Chuck Levera246b012005-08-11 16:25:23 -04001608
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001609/**
1610 * xs_tcp_write_space - callback invoked when socket buffer space
1611 * becomes available
1612 * @sk: socket whose state has changed
1613 *
1614 * Called when more output buffer space is available for this socket.
1615 * We try not to wake our writers until they can make "significant"
1616 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1617 * with a bunch of small requests.
1618 */
1619static void xs_tcp_write_space(struct sock *sk)
1620{
Eric Dumazetf064af12010-09-22 12:43:39 +00001621 read_lock_bh(&sk->sk_callback_lock);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001622
1623 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001624 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1625 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001626
Eric Dumazetf064af12010-09-22 12:43:39 +00001627 read_unlock_bh(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001628}
1629
Chuck Lever470056c2005-08-25 16:25:56 -07001630static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001631{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001632 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1633 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001634
Chuck Lever7c6e0662006-12-05 16:35:30 -05001635 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001636 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001637 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001638 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001639 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001640 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001641 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001642 sk->sk_write_space(sk);
1643 }
1644}
1645
Chuck Lever43118c22005-08-25 16:25:49 -07001646/**
Chuck Lever470056c2005-08-25 16:25:56 -07001647 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001648 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001649 * @sndsize: requested size of send buffer, in bytes
1650 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001651 *
Chuck Lever470056c2005-08-25 16:25:56 -07001652 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001653 */
Chuck Lever470056c2005-08-25 16:25:56 -07001654static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001655{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001656 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1657
1658 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001659 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001660 transport->sndsize = sndsize + 1024;
1661 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001662 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001663 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001664
1665 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001666}
1667
Chuck Lever46c0ee82005-08-25 16:25:52 -07001668/**
1669 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1670 * @task: task that timed out
1671 *
1672 * Adjust the congestion window after a retransmit timeout has occurred.
1673 */
1674static void xs_udp_timer(struct rpc_task *task)
1675{
1676 xprt_adjust_cwnd(task, -ETIMEDOUT);
1677}
1678
Chuck Leverb85d8802006-05-25 01:40:49 -04001679static unsigned short xs_get_random_port(void)
1680{
1681 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1682 unsigned short rand = (unsigned short) net_random() % range;
1683 return rand + xprt_min_resvport;
1684}
1685
Chuck Lever92200412006-01-03 09:55:51 +01001686/**
1687 * xs_set_port - reset the port number in the remote endpoint address
1688 * @xprt: generic transport
1689 * @port: new port number
1690 *
1691 */
1692static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1693{
Chuck Lever46121cf2007-01-31 12:14:08 -05001694 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001695
Chuck Lever9dc3b092009-08-09 15:09:46 -04001696 rpc_set_port(xs_addr(xprt), port);
1697 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001698}
1699
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001700static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001701{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001702 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001703
1704 if (port == 0 && transport->xprt.resvport)
1705 port = xs_get_random_port();
1706 return port;
1707}
1708
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001709static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001710{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001711 if (transport->srcport != 0)
1712 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001713 if (!transport->xprt.resvport)
1714 return 0;
1715 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1716 return xprt_max_resvport;
1717 return --port;
1718}
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001719static int xs_bind(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001720{
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001721 struct sockaddr_storage myaddr;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001722 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001723 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001724 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001725
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001726 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
Chuck Levera246b012005-08-11 16:25:23 -04001727 do {
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001728 rpc_set_port((struct sockaddr *)&myaddr, port);
1729 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1730 transport->xprt.addrlen);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001731 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001732 break;
Chuck Levera246b012005-08-11 16:25:23 -04001733 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001734 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001735 break;
Chuck Levera246b012005-08-11 16:25:23 -04001736 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001737 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001738 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001739 if (port > last)
1740 nloop++;
1741 } while (err == -EADDRINUSE && nloop != 2);
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001742
Chuck Lever4232e862010-10-20 11:52:51 -04001743 if (myaddr.ss_family == AF_INET)
Pavel Emelyanovbeb59b62010-10-05 15:53:08 +04001744 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1745 &((struct sockaddr_in *)&myaddr)->sin_addr,
1746 port, err ? "failed" : "ok", err);
1747 else
1748 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1749 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1750 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001751 return err;
1752}
1753
Chuck Lever176e21e2011-05-09 15:22:44 -04001754/*
1755 * We don't support autobind on AF_LOCAL sockets
1756 */
1757static void xs_local_rpcbind(struct rpc_task *task)
1758{
1759 xprt_set_bound(task->tk_xprt);
1760}
1761
1762static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1763{
1764}
Chuck Levera246b012005-08-11 16:25:23 -04001765
Peter Zijlstraed075362006-12-06 20:35:24 -08001766#ifdef CONFIG_DEBUG_LOCK_ALLOC
1767static struct lock_class_key xs_key[2];
1768static struct lock_class_key xs_slock_key[2];
1769
Chuck Lever176e21e2011-05-09 15:22:44 -04001770static inline void xs_reclassify_socketu(struct socket *sock)
1771{
1772 struct sock *sk = sock->sk;
1773
1774 BUG_ON(sock_owned_by_user(sk));
1775 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1776 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1777}
1778
Chuck Lever8945ee52007-08-06 11:58:04 -04001779static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001780{
1781 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001782
John Heffner02b3d342007-09-12 10:42:12 +02001783 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001784 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1785 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1786}
Peter Zijlstraed075362006-12-06 20:35:24 -08001787
Chuck Lever8945ee52007-08-06 11:58:04 -04001788static inline void xs_reclassify_socket6(struct socket *sock)
1789{
1790 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001791
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001792 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001793 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1794 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001795}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001796
1797static inline void xs_reclassify_socket(int family, struct socket *sock)
1798{
Chuck Lever4232e862010-10-20 11:52:51 -04001799 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04001800 case AF_LOCAL:
1801 xs_reclassify_socketu(sock);
1802 break;
Chuck Lever4232e862010-10-20 11:52:51 -04001803 case AF_INET:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001804 xs_reclassify_socket4(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001805 break;
1806 case AF_INET6:
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001807 xs_reclassify_socket6(sock);
Chuck Lever4232e862010-10-20 11:52:51 -04001808 break;
1809 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001810}
Peter Zijlstraed075362006-12-06 20:35:24 -08001811#else
Chuck Lever176e21e2011-05-09 15:22:44 -04001812static inline void xs_reclassify_socketu(struct socket *sock)
1813{
1814}
1815
Chuck Lever8945ee52007-08-06 11:58:04 -04001816static inline void xs_reclassify_socket4(struct socket *sock)
1817{
1818}
1819
1820static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001821{
1822}
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001823
1824static inline void xs_reclassify_socket(int family, struct socket *sock)
1825{
1826}
Peter Zijlstraed075362006-12-06 20:35:24 -08001827#endif
1828
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001829static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1830 struct sock_xprt *transport, int family, int type, int protocol)
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001831{
1832 struct socket *sock;
1833 int err;
1834
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001835 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001836 if (err < 0) {
1837 dprintk("RPC: can't create %d transport socket (%d).\n",
1838 protocol, -err);
1839 goto out;
1840 }
Pavel Emelyanov6bc96382010-10-04 16:56:38 +04001841 xs_reclassify_socket(family, sock);
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001842
Ben Hutchings4cea2882011-02-22 21:54:34 +00001843 err = xs_bind(transport, sock);
1844 if (err) {
Pavel Emelyanov22f79322010-10-04 16:54:26 +04001845 sock_release(sock);
1846 goto out;
1847 }
1848
1849 return sock;
1850out:
1851 return ERR_PTR(err);
1852}
1853
Chuck Lever176e21e2011-05-09 15:22:44 -04001854static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1855 struct socket *sock)
1856{
1857 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1858 xprt);
1859
1860 if (!transport->inet) {
1861 struct sock *sk = sock->sk;
1862
1863 write_lock_bh(&sk->sk_callback_lock);
1864
1865 xs_save_old_callbacks(transport, sk);
1866
1867 sk->sk_user_data = xprt;
1868 sk->sk_data_ready = xs_local_data_ready;
1869 sk->sk_write_space = xs_udp_write_space;
1870 sk->sk_error_report = xs_error_report;
1871 sk->sk_allocation = GFP_ATOMIC;
1872
1873 xprt_clear_connected(xprt);
1874
1875 /* Reset to new socket */
1876 transport->sock = sock;
1877 transport->inet = sk;
1878
1879 write_unlock_bh(&sk->sk_callback_lock);
1880 }
1881
1882 /* Tell the socket layer to start connecting... */
1883 xprt->stat.connect_count++;
1884 xprt->stat.connect_start = jiffies;
1885 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1886}
1887
1888/**
1889 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1890 * @xprt: RPC transport to connect
1891 * @transport: socket transport to connect
1892 * @create_sock: function to create a socket of the correct type
1893 *
1894 * Invoked by a work queue tasklet.
1895 */
1896static void xs_local_setup_socket(struct work_struct *work)
1897{
1898 struct sock_xprt *transport =
1899 container_of(work, struct sock_xprt, connect_worker.work);
1900 struct rpc_xprt *xprt = &transport->xprt;
1901 struct socket *sock;
1902 int status = -EIO;
1903
1904 if (xprt->shutdown)
1905 goto out;
1906
Jeff Layton5cf02d02012-07-23 13:58:51 -04001907 current->flags |= PF_FSTRANS;
1908
Chuck Lever176e21e2011-05-09 15:22:44 -04001909 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1910 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1911 SOCK_STREAM, 0, &sock, 1);
1912 if (status < 0) {
1913 dprintk("RPC: can't create AF_LOCAL "
1914 "transport socket (%d).\n", -status);
1915 goto out;
1916 }
1917 xs_reclassify_socketu(sock);
1918
1919 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1920 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1921
1922 status = xs_local_finish_connecting(xprt, sock);
1923 switch (status) {
1924 case 0:
1925 dprintk("RPC: xprt %p connected to %s\n",
1926 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1927 xprt_set_connected(xprt);
1928 break;
1929 case -ENOENT:
1930 dprintk("RPC: xprt %p: socket %s does not exist\n",
1931 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1932 break;
1933 default:
1934 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1935 __func__, -status,
1936 xprt->address_strings[RPC_DISPLAY_ADDR]);
1937 }
1938
1939out:
1940 xprt_clear_connecting(xprt);
1941 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04001942 current->flags &= ~PF_FSTRANS;
Chuck Lever176e21e2011-05-09 15:22:44 -04001943}
1944
Mel Gormana564b8f2012-07-31 16:45:12 -07001945#ifdef CONFIG_SUNRPC_SWAP
1946static void xs_set_memalloc(struct rpc_xprt *xprt)
1947{
1948 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1949 xprt);
1950
1951 if (xprt->swapper)
1952 sk_set_memalloc(transport->inet);
1953}
1954
1955/**
1956 * xs_swapper - Tag this transport as being used for swap.
1957 * @xprt: transport to tag
1958 * @enable: enable/disable
1959 *
1960 */
1961int xs_swapper(struct rpc_xprt *xprt, int enable)
1962{
1963 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1964 xprt);
1965 int err = 0;
1966
1967 if (enable) {
1968 xprt->swapper++;
1969 xs_set_memalloc(xprt);
1970 } else if (xprt->swapper) {
1971 xprt->swapper--;
1972 sk_clear_memalloc(transport->inet);
1973 }
1974
1975 return err;
1976}
1977EXPORT_SYMBOL_GPL(xs_swapper);
1978#else
1979static void xs_set_memalloc(struct rpc_xprt *xprt)
1980{
1981}
1982#endif
1983
Chuck Lever16be2d22007-08-06 11:57:38 -04001984static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001985{
Chuck Lever16be2d22007-08-06 11:57:38 -04001986 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001987
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001988 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001989 struct sock *sk = sock->sk;
1990
1991 write_lock_bh(&sk->sk_callback_lock);
1992
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001993 xs_save_old_callbacks(transport, sk);
1994
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001995 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001996 sk->sk_data_ready = xs_udp_data_ready;
1997 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001998 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001999 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002000 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002001
2002 xprt_set_connected(xprt);
2003
2004 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002005 transport->sock = sock;
2006 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002007
Mel Gormana564b8f2012-07-31 16:45:12 -07002008 xs_set_memalloc(xprt);
2009
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002010 write_unlock_bh(&sk->sk_callback_lock);
2011 }
Chuck Lever470056c2005-08-25 16:25:56 -07002012 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04002013}
2014
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002015static void xs_udp_setup_socket(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04002016{
2017 struct sock_xprt *transport =
2018 container_of(work, struct sock_xprt, connect_worker.work);
2019 struct rpc_xprt *xprt = &transport->xprt;
2020 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002021 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04002022
Trond Myklebust01d37c42009-03-11 14:09:39 -04002023 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04002024 goto out;
2025
Jeff Layton5cf02d02012-07-23 13:58:51 -04002026 current->flags |= PF_FSTRANS;
2027
Chuck Levera246b012005-08-11 16:25:23 -04002028 /* Start by resetting any existing state */
Chuck Leverfe315e762009-03-11 14:10:21 -04002029 xs_reset_transport(transport);
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002030 sock = xs_create_sock(xprt, transport,
2031 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04002032 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04002033 goto out;
Chuck Lever68e220b2007-08-06 11:57:48 -04002034
Chuck Leverc740eff2009-08-09 15:09:46 -04002035 dprintk("RPC: worker connecting xprt %p via %s to "
2036 "%s (port %s)\n", xprt,
2037 xprt->address_strings[RPC_DISPLAY_PROTO],
2038 xprt->address_strings[RPC_DISPLAY_ADDR],
2039 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04002040
2041 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04002042 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002043out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002044 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002045 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002046 current->flags &= ~PF_FSTRANS;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002047}
2048
Chuck Lever3167e122005-08-25 16:25:55 -07002049/*
2050 * We need to preserve the port number so the reply cache on the server can
2051 * find our cached RPC replies when we get around to reconnecting.
2052 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002053static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07002054{
2055 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07002056 struct sockaddr any;
2057
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002058 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07002059
2060 /*
2061 * Disconnect the transport socket by doing a connect operation
2062 * with AF_UNSPEC. This should return immediately...
2063 */
2064 memset(&any, 0, sizeof(any));
2065 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002066 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002067 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002068 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002069 else
Chuck Lever46121cf2007-01-31 12:14:08 -05002070 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07002071 result);
2072}
2073
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002074static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04002075{
2076 unsigned int state = transport->inet->sk_state;
2077
Andy Chittenden669502f2010-08-10 10:19:53 -04002078 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2079 /* we don't need to abort the connection if the socket
2080 * hasn't undergone a shutdown
2081 */
2082 if (transport->inet->sk_shutdown == 0)
2083 return;
2084 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2085 __func__, transport->inet->sk_shutdown);
2086 }
2087 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2088 /* we don't need to abort the connection if the socket
2089 * hasn't undergone a shutdown
2090 */
2091 if (transport->inet->sk_shutdown == 0)
2092 return;
2093 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2094 "sk_shutdown set to %d\n",
2095 __func__, transport->inet->sk_shutdown);
2096 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002097 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04002098}
2099
Chuck Lever16be2d22007-08-06 11:57:38 -04002100static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002101{
Chuck Lever16be2d22007-08-06 11:57:38 -04002102 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebustfe19a962011-03-18 20:21:23 -04002103 int ret = -ENOTCONN;
Chuck Leveredb267a2006-08-22 20:06:18 -04002104
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002105 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002106 struct sock *sk = sock->sk;
2107
2108 write_lock_bh(&sk->sk_callback_lock);
2109
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04002110 xs_save_old_callbacks(transport, sk);
2111
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002112 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002113 sk->sk_data_ready = xs_tcp_data_ready;
2114 sk->sk_state_change = xs_tcp_state_change;
2115 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04002116 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05002117 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07002118
2119 /* socket options */
2120 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2121 sock_reset_flag(sk, SOCK_LINGER);
2122 tcp_sk(sk)->linger2 = 0;
2123 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002124
2125 xprt_clear_connected(xprt);
2126
2127 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002128 transport->sock = sock;
2129 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002130
2131 write_unlock_bh(&sk->sk_callback_lock);
2132 }
2133
Trond Myklebust01d37c42009-03-11 14:09:39 -04002134 if (!xprt_bound(xprt))
Trond Myklebustfe19a962011-03-18 20:21:23 -04002135 goto out;
Trond Myklebust01d37c42009-03-11 14:09:39 -04002136
Mel Gormana564b8f2012-07-31 16:45:12 -07002137 xs_set_memalloc(xprt);
2138
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002139 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05002140 xprt->stat.connect_count++;
2141 xprt->stat.connect_start = jiffies;
Trond Myklebustfe19a962011-03-18 20:21:23 -04002142 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2143 switch (ret) {
2144 case 0:
2145 case -EINPROGRESS:
2146 /* SYN_SENT! */
2147 xprt->connect_cookie++;
2148 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2149 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2150 }
2151out:
2152 return ret;
Chuck Lever16be2d22007-08-06 11:57:38 -04002153}
2154
2155/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002156 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2157 * @xprt: RPC transport to connect
2158 * @transport: socket transport to connect
2159 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04002160 *
2161 * Invoked by a work queue tasklet.
2162 */
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002163static void xs_tcp_setup_socket(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04002164{
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002165 struct sock_xprt *transport =
2166 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever16be2d22007-08-06 11:57:38 -04002167 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002168 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002169 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04002170
Trond Myklebust01d37c42009-03-11 14:09:39 -04002171 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04002172 goto out;
2173
Jeff Layton5cf02d02012-07-23 13:58:51 -04002174 current->flags |= PF_FSTRANS;
2175
Chuck Lever16be2d22007-08-06 11:57:38 -04002176 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002177 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002178 sock = xs_create_sock(xprt, transport,
2179 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002180 if (IS_ERR(sock)) {
2181 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04002182 goto out;
2183 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002184 } else {
2185 int abort_and_exit;
2186
2187 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2188 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04002189 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04002190 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04002191
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002192 if (abort_and_exit)
2193 goto out_eagain;
2194 }
2195
Chuck Leverc740eff2009-08-09 15:09:46 -04002196 dprintk("RPC: worker connecting xprt %p via %s to "
2197 "%s (port %s)\n", xprt,
2198 xprt->address_strings[RPC_DISPLAY_PROTO],
2199 xprt->address_strings[RPC_DISPLAY_ADDR],
2200 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04002201
2202 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05002203 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2204 xprt, -status, xprt_connected(xprt),
2205 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04002206 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04002207 default:
2208 printk("%s: connect returned unhandled error %d\n",
2209 __func__, status);
2210 case -EADDRNOTAVAIL:
2211 /* We're probably in TIME_WAIT. Get rid of existing socket,
2212 * and retry
2213 */
Trond Myklebusta519fc72012-09-12 16:49:15 -04002214 xs_tcp_force_close(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07002215 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04002216 case -ECONNREFUSED:
2217 case -ECONNRESET:
2218 case -ENETUNREACH:
2219 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04002220 case 0:
2221 case -EINPROGRESS:
2222 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002223 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002224 current->flags &= ~PF_FSTRANS;
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002225 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05002226 case -EINVAL:
2227 /* Happens, for instance, if the user specified a link
2228 * local IPv6 address without a scope-id.
2229 */
2230 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04002231 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002232out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04002233 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04002234out:
Chuck Lever2226feb2005-08-11 16:25:38 -04002235 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04002236 xprt_wake_pending_tasks(xprt, status);
Jeff Layton5cf02d02012-07-23 13:58:51 -04002237 current->flags &= ~PF_FSTRANS;
Chuck Levera246b012005-08-11 16:25:23 -04002238}
2239
Chuck Lever68e220b2007-08-06 11:57:48 -04002240/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002241 * xs_connect - connect a socket to a remote endpoint
2242 * @task: address of RPC task that manages state of connect request
2243 *
2244 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002245 *
2246 * UDP socket connects are synchronous, but we use a work queue anyway
2247 * to guarantee that even unprivileged user processes can set up a
2248 * socket on a privileged port.
2249 *
2250 * If a UDP socket connect fails, the delay behavior here prevents
2251 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002252 */
2253static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002254{
2255 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002256 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002257
Chuck Lever09a21c42009-12-03 15:58:56 -05002258 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002259 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2260 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002261 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002262 queue_delayed_work(rpciod_workqueue,
2263 &transport->connect_worker,
2264 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002265 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002266 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2267 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002268 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2269 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002270 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002271 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002272 queue_delayed_work(rpciod_workqueue,
2273 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002274 }
2275}
2276
Chuck Lever262ca072006-03-20 13:44:16 -05002277/**
Chuck Lever176e21e2011-05-09 15:22:44 -04002278 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2279 * @xprt: rpc_xprt struct containing statistics
2280 * @seq: output file
2281 *
2282 */
2283static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2284{
2285 long idle_time = 0;
2286
2287 if (xprt_connected(xprt))
2288 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2289
2290 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
Andy Adamson15a45202012-02-14 16:19:18 -05002291 "%llu %llu %lu %llu %llu\n",
Chuck Lever176e21e2011-05-09 15:22:44 -04002292 xprt->stat.bind_count,
2293 xprt->stat.connect_count,
2294 xprt->stat.connect_time,
2295 idle_time,
2296 xprt->stat.sends,
2297 xprt->stat.recvs,
2298 xprt->stat.bad_xids,
2299 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002300 xprt->stat.bklog_u,
2301 xprt->stat.max_slots,
2302 xprt->stat.sending_u,
2303 xprt->stat.pending_u);
Chuck Lever176e21e2011-05-09 15:22:44 -04002304}
2305
2306/**
Chuck Lever262ca072006-03-20 13:44:16 -05002307 * xs_udp_print_stats - display UDP socket-specifc stats
2308 * @xprt: rpc_xprt struct containing statistics
2309 * @seq: output file
2310 *
2311 */
2312static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2313{
Chuck Leverc8475462006-12-05 16:35:26 -05002314 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2315
Andy Adamson15a45202012-02-14 16:19:18 -05002316 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2317 "%lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002318 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002319 xprt->stat.bind_count,
2320 xprt->stat.sends,
2321 xprt->stat.recvs,
2322 xprt->stat.bad_xids,
2323 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002324 xprt->stat.bklog_u,
2325 xprt->stat.max_slots,
2326 xprt->stat.sending_u,
2327 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002328}
2329
2330/**
2331 * xs_tcp_print_stats - display TCP socket-specifc stats
2332 * @xprt: rpc_xprt struct containing statistics
2333 * @seq: output file
2334 *
2335 */
2336static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2337{
Chuck Leverc8475462006-12-05 16:35:26 -05002338 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002339 long idle_time = 0;
2340
2341 if (xprt_connected(xprt))
2342 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2343
Andy Adamson15a45202012-02-14 16:19:18 -05002344 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2345 "%llu %llu %lu %llu %llu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002346 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002347 xprt->stat.bind_count,
2348 xprt->stat.connect_count,
2349 xprt->stat.connect_time,
2350 idle_time,
2351 xprt->stat.sends,
2352 xprt->stat.recvs,
2353 xprt->stat.bad_xids,
2354 xprt->stat.req_u,
Andy Adamson15a45202012-02-14 16:19:18 -05002355 xprt->stat.bklog_u,
2356 xprt->stat.max_slots,
2357 xprt->stat.sending_u,
2358 xprt->stat.pending_u);
Chuck Lever262ca072006-03-20 13:44:16 -05002359}
2360
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002361/*
2362 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2363 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2364 * to use the server side send routines.
2365 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002366static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002367{
2368 struct page *page;
2369 struct rpc_buffer *buf;
2370
2371 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2372 page = alloc_page(GFP_KERNEL);
2373
2374 if (!page)
2375 return NULL;
2376
2377 buf = page_address(page);
2378 buf->len = PAGE_SIZE;
2379
2380 return buf->data;
2381}
2382
2383/*
2384 * Free the space allocated in the bc_alloc routine
2385 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002386static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002387{
2388 struct rpc_buffer *buf;
2389
2390 if (!buffer)
2391 return;
2392
2393 buf = container_of(buffer, struct rpc_buffer, data);
2394 free_page((unsigned long)buf);
2395}
2396
2397/*
2398 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2399 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2400 */
2401static int bc_sendto(struct rpc_rqst *req)
2402{
2403 int len;
2404 struct xdr_buf *xbufp = &req->rq_snd_buf;
2405 struct rpc_xprt *xprt = req->rq_xprt;
2406 struct sock_xprt *transport =
2407 container_of(xprt, struct sock_xprt, xprt);
2408 struct socket *sock = transport->sock;
2409 unsigned long headoff;
2410 unsigned long tailoff;
2411
Chuck Lever61677ee2011-05-09 15:22:34 -04002412 xs_encode_stream_record_marker(xbufp);
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002413
2414 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2415 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2416 len = svc_send_common(sock, xbufp,
2417 virt_to_page(xbufp->head[0].iov_base), headoff,
2418 xbufp->tail[0].iov_base, tailoff);
2419
2420 if (len != xbufp->len) {
2421 printk(KERN_NOTICE "Error sending entire callback!\n");
2422 len = -EAGAIN;
2423 }
2424
2425 return len;
2426}
2427
2428/*
2429 * The send routine. Borrows from svc_send
2430 */
2431static int bc_send_request(struct rpc_task *task)
2432{
2433 struct rpc_rqst *req = task->tk_rqstp;
2434 struct svc_xprt *xprt;
2435 struct svc_sock *svsk;
2436 u32 len;
2437
2438 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2439 /*
2440 * Get the server socket associated with this callback xprt
2441 */
2442 xprt = req->rq_xprt->bc_xprt;
2443 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2444
2445 /*
2446 * Grab the mutex to serialize data as the connection is shared
2447 * with the fore channel
2448 */
2449 if (!mutex_trylock(&xprt->xpt_mutex)) {
2450 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2451 if (!mutex_trylock(&xprt->xpt_mutex))
2452 return -EAGAIN;
2453 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2454 }
2455 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2456 len = -ENOTCONN;
2457 else
2458 len = bc_sendto(req);
2459 mutex_unlock(&xprt->xpt_mutex);
2460
2461 if (len > 0)
2462 len = 0;
2463
2464 return len;
2465}
2466
2467/*
2468 * The close routine. Since this is client initiated, we do nothing
2469 */
2470
2471static void bc_close(struct rpc_xprt *xprt)
2472{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002473}
2474
2475/*
2476 * The xprt destroy routine. Again, because this connection is client
2477 * initiated, we do nothing
2478 */
2479
2480static void bc_destroy(struct rpc_xprt *xprt)
2481{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002482}
2483
Chuck Lever176e21e2011-05-09 15:22:44 -04002484static struct rpc_xprt_ops xs_local_ops = {
2485 .reserve_xprt = xprt_reserve_xprt,
2486 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002487 .alloc_slot = xprt_alloc_slot,
Chuck Lever176e21e2011-05-09 15:22:44 -04002488 .rpcbind = xs_local_rpcbind,
2489 .set_port = xs_local_set_port,
2490 .connect = xs_connect,
2491 .buf_alloc = rpc_malloc,
2492 .buf_free = rpc_free,
2493 .send_request = xs_local_send_request,
2494 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2495 .close = xs_close,
2496 .destroy = xs_destroy,
2497 .print_stats = xs_local_print_stats,
2498};
2499
Chuck Lever262965f2005-08-11 16:25:56 -04002500static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002501 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002502 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002503 .release_xprt = xprt_release_xprt_cong,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002504 .alloc_slot = xprt_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002505 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002506 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002507 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002508 .buf_alloc = rpc_malloc,
2509 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002510 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002511 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002512 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002513 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002514 .close = xs_close,
2515 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002516 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002517};
2518
2519static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002520 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002521 .release_xprt = xs_tcp_release_xprt,
Trond Myklebustf39c1bf2012-09-07 11:08:50 -04002522 .alloc_slot = xprt_lock_and_alloc_slot,
Chuck Lever45160d62007-07-01 12:13:17 -04002523 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002524 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002525 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002526 .buf_alloc = rpc_malloc,
2527 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002528 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002529 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002530 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002531 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002532 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002533};
2534
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002535/*
2536 * The rpc_xprt_ops for the server backchannel
2537 */
2538
2539static struct rpc_xprt_ops bc_tcp_ops = {
2540 .reserve_xprt = xprt_reserve_xprt,
2541 .release_xprt = xprt_release_xprt,
Bryan Schumaker84e28a32012-09-24 13:39:01 -04002542 .alloc_slot = xprt_alloc_slot,
J. Bruce Fields92769102012-03-23 15:25:03 -04002543 .rpcbind = xs_local_rpcbind,
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002544 .buf_alloc = bc_malloc,
2545 .buf_free = bc_free,
2546 .send_request = bc_send_request,
2547 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2548 .close = bc_close,
2549 .destroy = bc_destroy,
2550 .print_stats = xs_tcp_print_stats,
2551};
2552
Chuck Lever92476852010-10-20 11:53:01 -04002553static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2554{
2555 static const struct sockaddr_in sin = {
2556 .sin_family = AF_INET,
2557 .sin_addr.s_addr = htonl(INADDR_ANY),
2558 };
2559 static const struct sockaddr_in6 sin6 = {
2560 .sin6_family = AF_INET6,
2561 .sin6_addr = IN6ADDR_ANY_INIT,
2562 };
2563
2564 switch (family) {
Chuck Lever176e21e2011-05-09 15:22:44 -04002565 case AF_LOCAL:
2566 break;
Chuck Lever92476852010-10-20 11:53:01 -04002567 case AF_INET:
2568 memcpy(sap, &sin, sizeof(sin));
2569 break;
2570 case AF_INET6:
2571 memcpy(sap, &sin6, sizeof(sin6));
2572 break;
2573 default:
2574 dprintk("RPC: %s: Bad address family\n", __func__);
2575 return -EAFNOSUPPORT;
2576 }
2577 return 0;
2578}
2579
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002580static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002581 unsigned int slot_table_size,
2582 unsigned int max_slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002583{
2584 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002585 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002586
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002587 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002588 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002589 return ERR_PTR(-EBADF);
2590 }
2591
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002592 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2593 max_slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002594 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002595 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2596 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002597 return ERR_PTR(-ENOMEM);
2598 }
2599
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002600 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002601 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2602 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002603 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002604 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Lever92476852010-10-20 11:53:01 -04002605 else {
2606 int err;
2607 err = xs_init_anyaddr(args->dstaddr->sa_family,
2608 (struct sockaddr *)&new->srcaddr);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002609 if (err != 0) {
2610 xprt_free(xprt);
Chuck Lever92476852010-10-20 11:53:01 -04002611 return ERR_PTR(err);
Stanislav Kinsbursky2aa13532011-11-10 14:33:23 +03002612 }
Chuck Lever92476852010-10-20 11:53:01 -04002613 }
Chuck Leverc8541ec2006-10-17 14:44:27 -04002614
2615 return xprt;
2616}
2617
Chuck Lever176e21e2011-05-09 15:22:44 -04002618static const struct rpc_timeout xs_local_default_timeout = {
2619 .to_initval = 10 * HZ,
2620 .to_maxval = 10 * HZ,
2621 .to_retries = 2,
2622};
2623
2624/**
2625 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2626 * @args: rpc transport creation arguments
2627 *
2628 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2629 */
2630static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2631{
2632 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2633 struct sock_xprt *transport;
2634 struct rpc_xprt *xprt;
2635 struct rpc_xprt *ret;
2636
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002637 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2638 xprt_max_tcp_slot_table_entries);
Chuck Lever176e21e2011-05-09 15:22:44 -04002639 if (IS_ERR(xprt))
2640 return xprt;
2641 transport = container_of(xprt, struct sock_xprt, xprt);
2642
2643 xprt->prot = 0;
2644 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2645 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2646
2647 xprt->bind_timeout = XS_BIND_TO;
2648 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2649 xprt->idle_timeout = XS_IDLE_DISC_TO;
2650
2651 xprt->ops = &xs_local_ops;
2652 xprt->timeout = &xs_local_default_timeout;
2653
2654 switch (sun->sun_family) {
2655 case AF_LOCAL:
2656 if (sun->sun_path[0] != '/') {
2657 dprintk("RPC: bad AF_LOCAL address: %s\n",
2658 sun->sun_path);
2659 ret = ERR_PTR(-EINVAL);
2660 goto out_err;
2661 }
2662 xprt_set_bound(xprt);
2663 INIT_DELAYED_WORK(&transport->connect_worker,
2664 xs_local_setup_socket);
2665 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2666 break;
2667 default:
2668 ret = ERR_PTR(-EAFNOSUPPORT);
2669 goto out_err;
2670 }
2671
2672 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2673 xprt->address_strings[RPC_DISPLAY_ADDR]);
2674
2675 if (try_module_get(THIS_MODULE))
2676 return xprt;
2677 ret = ERR_PTR(-EINVAL);
2678out_err:
2679 xprt_free(xprt);
2680 return ret;
2681}
2682
Trond Myklebust2881ae72007-12-20 16:03:54 -05002683static const struct rpc_timeout xs_udp_default_timeout = {
2684 .to_initval = 5 * HZ,
2685 .to_maxval = 30 * HZ,
2686 .to_increment = 5 * HZ,
2687 .to_retries = 5,
2688};
2689
Chuck Lever9903cd12005-08-11 16:25:26 -04002690/**
2691 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002692 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002693 *
2694 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002695static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002696{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002697 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002698 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002699 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002700 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002701
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002702 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2703 xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002704 if (IS_ERR(xprt))
2705 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002706 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002707
Chuck Leverec739ef2006-08-22 20:06:15 -04002708 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002709 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002710 /* XXX: header size can vary due to auth type, IPv6, etc. */
2711 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2712
Chuck Lever03bf4b72005-08-25 16:25:55 -07002713 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002714 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2715 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002716
Chuck Lever262965f2005-08-11 16:25:56 -04002717 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002718
Trond Myklebustba7392b2007-12-20 16:03:55 -05002719 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002720
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002721 switch (addr->sa_family) {
2722 case AF_INET:
2723 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2724 xprt_set_bound(xprt);
2725
2726 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002727 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002728 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002729 break;
2730 case AF_INET6:
2731 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2732 xprt_set_bound(xprt);
2733
2734 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanov8c14ff22010-10-04 16:58:02 +04002735 xs_udp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002736 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002737 break;
2738 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002739 ret = ERR_PTR(-EAFNOSUPPORT);
2740 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002741 }
2742
Chuck Leverc740eff2009-08-09 15:09:46 -04002743 if (xprt_bound(xprt))
2744 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2745 xprt->address_strings[RPC_DISPLAY_ADDR],
2746 xprt->address_strings[RPC_DISPLAY_PORT],
2747 xprt->address_strings[RPC_DISPLAY_PROTO]);
2748 else
2749 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2750 xprt->address_strings[RPC_DISPLAY_ADDR],
2751 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002752
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002753 if (try_module_get(THIS_MODULE))
2754 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002755 ret = ERR_PTR(-EINVAL);
2756out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002757 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002758 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002759}
2760
Trond Myklebust2881ae72007-12-20 16:03:54 -05002761static const struct rpc_timeout xs_tcp_default_timeout = {
2762 .to_initval = 60 * HZ,
2763 .to_maxval = 60 * HZ,
2764 .to_retries = 2,
2765};
2766
Chuck Lever9903cd12005-08-11 16:25:26 -04002767/**
2768 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002769 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002770 *
2771 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002772static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002773{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002774 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002775 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002776 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002777 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002778
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002779 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2780 xprt_max_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002781 if (IS_ERR(xprt))
2782 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002783 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002784
Chuck Leverec739ef2006-08-22 20:06:15 -04002785 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002786 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002787 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002788
Chuck Lever03bf4b72005-08-25 16:25:55 -07002789 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002790 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2791 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002792
Chuck Lever262965f2005-08-11 16:25:56 -04002793 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002794 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002795
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002796 switch (addr->sa_family) {
2797 case AF_INET:
2798 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2799 xprt_set_bound(xprt);
2800
Chuck Lever9dc3b092009-08-09 15:09:46 -04002801 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002802 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002803 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002804 break;
2805 case AF_INET6:
2806 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2807 xprt_set_bound(xprt);
2808
Chuck Lever9dc3b092009-08-09 15:09:46 -04002809 INIT_DELAYED_WORK(&transport->connect_worker,
Pavel Emelyanovcdd518d2010-10-04 16:57:40 +04002810 xs_tcp_setup_socket);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002811 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002812 break;
2813 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002814 ret = ERR_PTR(-EAFNOSUPPORT);
2815 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002816 }
2817
Chuck Leverc740eff2009-08-09 15:09:46 -04002818 if (xprt_bound(xprt))
2819 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2820 xprt->address_strings[RPC_DISPLAY_ADDR],
2821 xprt->address_strings[RPC_DISPLAY_PORT],
2822 xprt->address_strings[RPC_DISPLAY_PROTO]);
2823 else
2824 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2825 xprt->address_strings[RPC_DISPLAY_ADDR],
2826 xprt->address_strings[RPC_DISPLAY_PROTO]);
2827
Chuck Leveredb267a2006-08-22 20:06:18 -04002828
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002829 if (try_module_get(THIS_MODULE))
2830 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002831 ret = ERR_PTR(-EINVAL);
2832out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002833 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002834 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002835}
Chuck Lever282b32e2006-12-05 16:35:51 -05002836
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002837/**
2838 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2839 * @args: rpc transport creation arguments
2840 *
2841 */
2842static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2843{
2844 struct sockaddr *addr = args->dstaddr;
2845 struct rpc_xprt *xprt;
2846 struct sock_xprt *transport;
2847 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002848 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002849
J. Bruce Fieldsf0418aa2010-12-08 13:48:19 -05002850 if (args->bc_xprt->xpt_bc_xprt) {
2851 /*
2852 * This server connection already has a backchannel
2853 * export; we can't create a new one, as we wouldn't be
2854 * able to match replies based on xid any more. So,
2855 * reuse the already-existing one:
2856 */
2857 return args->bc_xprt->xpt_bc_xprt;
2858 }
Trond Myklebustd9ba1312011-07-17 18:11:30 -04002859 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2860 xprt_tcp_slot_table_entries);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002861 if (IS_ERR(xprt))
2862 return xprt;
2863 transport = container_of(xprt, struct sock_xprt, xprt);
2864
2865 xprt->prot = IPPROTO_TCP;
2866 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2867 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2868 xprt->timeout = &xs_tcp_default_timeout;
2869
2870 /* backchannel */
2871 xprt_set_bound(xprt);
2872 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002873 xprt->reestablish_timeout = 0;
2874 xprt->idle_timeout = 0;
2875
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002876 xprt->ops = &bc_tcp_ops;
2877
2878 switch (addr->sa_family) {
2879 case AF_INET:
2880 xs_format_peer_addresses(xprt, "tcp",
2881 RPCBIND_NETID_TCP);
2882 break;
2883 case AF_INET6:
2884 xs_format_peer_addresses(xprt, "tcp",
2885 RPCBIND_NETID_TCP6);
2886 break;
2887 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002888 ret = ERR_PTR(-EAFNOSUPPORT);
2889 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002890 }
2891
Pavel Emelyanov50fa0d42010-10-05 20:49:35 +04002892 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2893 xprt->address_strings[RPC_DISPLAY_ADDR],
2894 xprt->address_strings[RPC_DISPLAY_PORT],
2895 xprt->address_strings[RPC_DISPLAY_PROTO]);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002896
2897 /*
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002898 * Once we've associated a backchannel xprt with a connection,
2899 * we want to keep it around as long as long as the connection
2900 * lasts, in case we need to start using it for a backchannel
2901 * again; this reference won't be dropped until bc_xprt is
2902 * destroyed.
2903 */
2904 xprt_get(xprt);
2905 args->bc_xprt->xpt_bc_xprt = xprt;
2906 xprt->bc_xprt = args->bc_xprt;
2907 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2908 transport->sock = bc_sock->sk_sock;
2909 transport->inet = bc_sock->sk_sk;
2910
2911 /*
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002912 * Since we don't want connections for the backchannel, we set
2913 * the xprt status to connected
2914 */
2915 xprt_set_connected(xprt);
2916
2917
2918 if (try_module_get(THIS_MODULE))
2919 return xprt;
J. Bruce Fields99de8ea2010-12-08 12:45:44 -05002920 xprt_put(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002921 ret = ERR_PTR(-EINVAL);
2922out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002923 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002924 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002925}
2926
Chuck Lever176e21e2011-05-09 15:22:44 -04002927static struct xprt_class xs_local_transport = {
2928 .list = LIST_HEAD_INIT(xs_local_transport.list),
2929 .name = "named UNIX socket",
2930 .owner = THIS_MODULE,
2931 .ident = XPRT_TRANSPORT_LOCAL,
2932 .setup = xs_setup_local,
2933};
2934
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002935static struct xprt_class xs_udp_transport = {
2936 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2937 .name = "udp",
2938 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002939 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002940 .setup = xs_setup_udp,
2941};
2942
2943static struct xprt_class xs_tcp_transport = {
2944 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2945 .name = "tcp",
2946 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002947 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002948 .setup = xs_setup_tcp,
2949};
2950
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002951static struct xprt_class xs_bc_tcp_transport = {
2952 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2953 .name = "tcp NFSv4.1 backchannel",
2954 .owner = THIS_MODULE,
2955 .ident = XPRT_TRANSPORT_BC_TCP,
2956 .setup = xs_setup_bc_tcp,
2957};
2958
Chuck Lever282b32e2006-12-05 16:35:51 -05002959/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002960 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002961 *
2962 */
2963int init_socket_xprt(void)
2964{
Chuck Leverfbf76682006-12-05 16:35:54 -05002965#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002966 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002967 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002968#endif
2969
Chuck Lever176e21e2011-05-09 15:22:44 -04002970 xprt_register_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002971 xprt_register_transport(&xs_udp_transport);
2972 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002973 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002974
Chuck Lever282b32e2006-12-05 16:35:51 -05002975 return 0;
2976}
2977
2978/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002979 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002980 *
2981 */
2982void cleanup_socket_xprt(void)
2983{
Chuck Leverfbf76682006-12-05 16:35:54 -05002984#ifdef RPC_DEBUG
2985 if (sunrpc_table_header) {
2986 unregister_sysctl_table(sunrpc_table_header);
2987 sunrpc_table_header = NULL;
2988 }
2989#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002990
Chuck Lever176e21e2011-05-09 15:22:44 -04002991 xprt_unregister_transport(&xs_local_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002992 xprt_unregister_transport(&xs_udp_transport);
2993 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002994 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002995}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002996
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002997static int param_set_uint_minmax(const char *val,
2998 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002999 unsigned int min, unsigned int max)
3000{
3001 unsigned long num;
3002 int ret;
3003
3004 if (!val)
3005 return -EINVAL;
3006 ret = strict_strtoul(val, 0, &num);
3007 if (ret == -EINVAL || num < min || num > max)
3008 return -EINVAL;
3009 *((unsigned int *)kp->arg) = num;
3010 return 0;
3011}
3012
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003013static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003014{
3015 return param_set_uint_minmax(val, kp,
3016 RPC_MIN_RESVPORT,
3017 RPC_MAX_RESVPORT);
3018}
3019
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003020static struct kernel_param_ops param_ops_portnr = {
3021 .set = param_set_portnr,
3022 .get = param_get_uint,
3023};
3024
Trond Myklebustcbf11072009-08-09 15:06:19 -04003025#define param_check_portnr(name, p) \
3026 __param_check(name, p, unsigned int);
3027
3028module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3029module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3030
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003031static int param_set_slot_table_size(const char *val,
3032 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04003033{
3034 return param_set_uint_minmax(val, kp,
3035 RPC_MIN_SLOT_TABLE,
3036 RPC_MAX_SLOT_TABLE);
3037}
3038
Rusty Russell9bbb9e52010-08-11 23:04:12 -06003039static struct kernel_param_ops param_ops_slot_table_size = {
3040 .set = param_set_slot_table_size,
3041 .get = param_get_uint,
3042};
3043
Trond Myklebustcbf11072009-08-09 15:06:19 -04003044#define param_check_slot_table_size(name, p) \
3045 __param_check(name, p, unsigned int);
3046
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003047static int param_set_max_slot_table_size(const char *val,
3048 const struct kernel_param *kp)
3049{
3050 return param_set_uint_minmax(val, kp,
3051 RPC_MIN_SLOT_TABLE,
3052 RPC_MAX_SLOT_TABLE_LIMIT);
3053}
3054
3055static struct kernel_param_ops param_ops_max_slot_table_size = {
3056 .set = param_set_max_slot_table_size,
3057 .get = param_get_uint,
3058};
3059
3060#define param_check_max_slot_table_size(name, p) \
3061 __param_check(name, p, unsigned int);
3062
Trond Myklebustcbf11072009-08-09 15:06:19 -04003063module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3064 slot_table_size, 0644);
Trond Myklebustd9ba1312011-07-17 18:11:30 -04003065module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3066 max_slot_table_size, 0644);
Trond Myklebustcbf11072009-08-09 15:06:19 -04003067module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3068 slot_table_size, 0644);
3069