blob: 83c73c4d017ad9ec91b4f1895344749c51b9839b [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>
22#include <linux/slab.h>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040023#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/pagemap.h>
26#include <linux/errno.h>
27#include <linux/socket.h>
28#include <linux/in.h>
29#include <linux/net.h>
30#include <linux/mm.h>
31#include <linux/udp.h>
32#include <linux/tcp.h>
33#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010034#include <linux/sunrpc/sched.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040035#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040036#include <linux/file.h>
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040037#ifdef CONFIG_NFS_V4_1
38#include <linux/sunrpc/bc_xprt.h>
39#endif
Chuck Levera246b012005-08-11 16:25:23 -040040
41#include <net/sock.h>
42#include <net/checksum.h>
43#include <net/udp.h>
44#include <net/tcp.h>
45
Chuck Lever9903cd12005-08-11 16:25:26 -040046/*
Chuck Leverc556b752005-11-01 12:24:48 -050047 * xprtsock tunables
48 */
49unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
50unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
51
52unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
53unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
54
Trond Myklebust7d1e8252009-03-11 14:38:03 -040055#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040056static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040057
Chuck Leverc556b752005-11-01 12:24:48 -050058/*
Chuck Leverfbf76682006-12-05 16:35:54 -050059 * We can register our own files under /proc/sys/sunrpc by
60 * calling register_sysctl_table() again. The files in that
61 * directory become the union of all files registered there.
62 *
63 * We simply need to make sure that we don't collide with
64 * someone else's file names!
65 */
66
67#ifdef RPC_DEBUG
68
69static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
70static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
71static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
72static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
73
74static struct ctl_table_header *sunrpc_table_header;
75
76/*
77 * FIXME: changing the UDP slot table size should also resize the UDP
78 * socket buffers for existing UDP transports
79 */
80static ctl_table xs_tunables_table[] = {
81 {
82 .ctl_name = CTL_SLOTTABLE_UDP,
83 .procname = "udp_slot_table_entries",
84 .data = &xprt_udp_slot_table_entries,
85 .maxlen = sizeof(unsigned int),
86 .mode = 0644,
87 .proc_handler = &proc_dointvec_minmax,
88 .strategy = &sysctl_intvec,
89 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
91 },
92 {
93 .ctl_name = CTL_SLOTTABLE_TCP,
94 .procname = "tcp_slot_table_entries",
95 .data = &xprt_tcp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec_minmax,
99 .strategy = &sysctl_intvec,
100 .extra1 = &min_slot_table_size,
101 .extra2 = &max_slot_table_size
102 },
103 {
104 .ctl_name = CTL_MIN_RESVPORT,
105 .procname = "min_resvport",
106 .data = &xprt_min_resvport,
107 .maxlen = sizeof(unsigned int),
108 .mode = 0644,
109 .proc_handler = &proc_dointvec_minmax,
110 .strategy = &sysctl_intvec,
111 .extra1 = &xprt_min_resvport_limit,
112 .extra2 = &xprt_max_resvport_limit
113 },
114 {
115 .ctl_name = CTL_MAX_RESVPORT,
116 .procname = "max_resvport",
117 .data = &xprt_max_resvport,
118 .maxlen = sizeof(unsigned int),
119 .mode = 0644,
120 .proc_handler = &proc_dointvec_minmax,
121 .strategy = &sysctl_intvec,
122 .extra1 = &xprt_min_resvport_limit,
123 .extra2 = &xprt_max_resvport_limit
124 },
125 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400126 .procname = "tcp_fin_timeout",
127 .data = &xs_tcp_fin_timeout,
128 .maxlen = sizeof(xs_tcp_fin_timeout),
129 .mode = 0644,
130 .proc_handler = &proc_dointvec_jiffies,
131 .strategy = sysctl_jiffies
132 },
133 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500134 .ctl_name = 0,
135 },
136};
137
138static ctl_table sunrpc_table[] = {
139 {
140 .ctl_name = CTL_SUNRPC,
141 .procname = "sunrpc",
142 .mode = 0555,
143 .child = xs_tunables_table
144 },
145 {
146 .ctl_name = 0,
147 },
148};
149
150#endif
151
152/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700153 * Time out for an RPC UDP socket connect. UDP socket connects are
154 * synchronous, but we set a timeout anyway in case of resource
155 * exhaustion on the local host.
156 */
157#define XS_UDP_CONN_TO (5U * HZ)
158
159/*
160 * Wait duration for an RPC TCP connection to be established. Solaris
161 * NFS over TCP uses 60 seconds, for example, which is in line with how
162 * long a server takes to reboot.
163 */
164#define XS_TCP_CONN_TO (60U * HZ)
165
166/*
167 * Wait duration for a reply from the RPC portmapper.
168 */
169#define XS_BIND_TO (60U * HZ)
170
171/*
172 * Delay if a UDP socket connect error occurs. This is most likely some
173 * kind of resource problem on the local host.
174 */
175#define XS_UDP_REEST_TO (2U * HZ)
176
177/*
178 * The reestablish timeout allows clients to delay for a bit before attempting
179 * to reconnect to a server that just dropped our connection.
180 *
181 * We implement an exponential backoff when trying to reestablish a TCP
182 * transport connection with the server. Some servers like to drop a TCP
183 * connection when they are overworked, so we start with a short timeout and
184 * increase over time if the server is down or not responding.
185 */
186#define XS_TCP_INIT_REEST_TO (3U * HZ)
187#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
188
189/*
190 * TCP idle timeout; client drops the transport socket if it is idle
191 * for this long. Note that we also timeout UDP sockets to prevent
192 * holding port numbers when there is no RPC traffic.
193 */
194#define XS_IDLE_DISC_TO (5U * 60 * HZ)
195
Chuck Levera246b012005-08-11 16:25:23 -0400196#ifdef RPC_DEBUG
197# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400198# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400199#endif
200
Chuck Levera246b012005-08-11 16:25:23 -0400201#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400202static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400203{
Chuck Lever9903cd12005-08-11 16:25:26 -0400204 u8 *buf = (u8 *) packet;
205 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400206
Chuck Lever46121cf2007-01-31 12:14:08 -0500207 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400208 for (j = 0; j < count && j < 128; j += 4) {
209 if (!(j & 31)) {
210 if (j)
211 dprintk("\n");
212 dprintk("0x%04x ", j);
213 }
214 dprintk("%02x%02x%02x%02x ",
215 buf[j], buf[j+1], buf[j+2], buf[j+3]);
216 }
217 dprintk("\n");
218}
219#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400220static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400221{
222 /* NOP */
223}
224#endif
225
Chuck Leverffc2e512006-12-05 16:35:11 -0500226struct sock_xprt {
227 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500228
229 /*
230 * Network layer
231 */
232 struct socket * sock;
233 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500234
235 /*
236 * State of TCP reply receive
237 */
238 __be32 tcp_fraghdr,
239 tcp_xid;
240
241 u32 tcp_offset,
242 tcp_reclen;
243
244 unsigned long tcp_copied,
245 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500246
247 /*
248 * Connection of transports
249 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500250 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200251 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500252 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500253
254 /*
255 * UDP socket buffer size parameters
256 */
257 size_t rcvsize,
258 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500259
260 /*
261 * Saved socket callback addresses
262 */
263 void (*old_data_ready)(struct sock *, int);
264 void (*old_state_change)(struct sock *);
265 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400266 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500267};
268
Chuck Levere136d092006-12-05 16:35:23 -0500269/*
270 * TCP receive state flags
271 */
272#define TCP_RCV_LAST_FRAG (1UL << 0)
273#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
274#define TCP_RCV_COPY_XID (1UL << 2)
275#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400276#define TCP_RCV_READ_CALLDIR (1UL << 4)
277#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400278
279/*
280 * TCP RPC flags
281 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400282#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500283
Chuck Lever95392c52007-08-06 11:57:58 -0400284static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400285{
Chuck Lever95392c52007-08-06 11:57:58 -0400286 return (struct sockaddr *) &xprt->addr;
287}
288
289static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
290{
291 return (struct sockaddr_in *) &xprt->addr;
292}
293
294static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
295{
296 return (struct sockaddr_in6 *) &xprt->addr;
297}
298
Chuck Leverb454ae92008-01-07 18:34:48 -0500299static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
300 const char *protocol,
301 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400302{
Chuck Lever95392c52007-08-06 11:57:58 -0400303 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400304 char *buf;
305
306 buf = kzalloc(20, GFP_KERNEL);
307 if (buf) {
David S. Millere0db4a72008-11-02 23:57:06 -0800308 snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
Chuck Leveredb267a2006-08-22 20:06:18 -0400309 }
310 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
311
312 buf = kzalloc(8, GFP_KERNEL);
313 if (buf) {
314 snprintf(buf, 8, "%u",
315 ntohs(addr->sin_port));
316 }
317 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
318
Chuck Leverb454ae92008-01-07 18:34:48 -0500319 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leveredb267a2006-08-22 20:06:18 -0400320
321 buf = kzalloc(48, GFP_KERNEL);
322 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700323 snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
324 &addr->sin_addr.s_addr,
Chuck Leveredb267a2006-08-22 20:06:18 -0400325 ntohs(addr->sin_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500326 protocol);
Chuck Leveredb267a2006-08-22 20:06:18 -0400327 }
328 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400329
330 buf = kzalloc(10, GFP_KERNEL);
331 if (buf) {
332 snprintf(buf, 10, "%02x%02x%02x%02x",
333 NIPQUAD(addr->sin_addr.s_addr));
334 }
335 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
336
337 buf = kzalloc(8, GFP_KERNEL);
338 if (buf) {
339 snprintf(buf, 8, "%4hx",
340 ntohs(addr->sin_port));
341 }
342 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400343
344 buf = kzalloc(30, GFP_KERNEL);
345 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700346 snprintf(buf, 30, "%pI4.%u.%u",
347 &addr->sin_addr.s_addr,
Chuck Lever756805e2007-08-16 16:03:26 -0400348 ntohs(addr->sin_port) >> 8,
349 ntohs(addr->sin_port) & 0xff);
350 }
351 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400352
Chuck Leverb454ae92008-01-07 18:34:48 -0500353 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400354}
355
Chuck Leverb454ae92008-01-07 18:34:48 -0500356static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
357 const char *protocol,
358 const char *netid)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400359{
Chuck Lever95392c52007-08-06 11:57:58 -0400360 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400361 char *buf;
362
363 buf = kzalloc(40, GFP_KERNEL);
364 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700365 snprintf(buf, 40, "%pI6",&addr->sin6_addr);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400366 }
367 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
368
369 buf = kzalloc(8, GFP_KERNEL);
370 if (buf) {
371 snprintf(buf, 8, "%u",
372 ntohs(addr->sin6_port));
373 }
374 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
375
Chuck Leverb454ae92008-01-07 18:34:48 -0500376 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Lever4b6473f2007-08-06 11:57:12 -0400377
378 buf = kzalloc(64, GFP_KERNEL);
379 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700380 snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700381 &addr->sin6_addr,
Chuck Lever4b6473f2007-08-06 11:57:12 -0400382 ntohs(addr->sin6_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500383 protocol);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400384 }
385 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
386
387 buf = kzalloc(36, GFP_KERNEL);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700388 if (buf)
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700389 snprintf(buf, 36, "%pi6", &addr->sin6_addr);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700390
Chuck Lever4b6473f2007-08-06 11:57:12 -0400391 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
392
393 buf = kzalloc(8, GFP_KERNEL);
394 if (buf) {
395 snprintf(buf, 8, "%4hx",
396 ntohs(addr->sin6_port));
397 }
398 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400399
400 buf = kzalloc(50, GFP_KERNEL);
401 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700402 snprintf(buf, 50, "%pI6.%u.%u",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700403 &addr->sin6_addr,
404 ntohs(addr->sin6_port) >> 8,
405 ntohs(addr->sin6_port) & 0xff);
Chuck Lever756805e2007-08-16 16:03:26 -0400406 }
407 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400408
Chuck Leverb454ae92008-01-07 18:34:48 -0500409 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400410}
411
412static void xs_free_peer_addresses(struct rpc_xprt *xprt)
413{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500414 unsigned int i;
415
416 for (i = 0; i < RPC_DISPLAY_MAX; i++)
417 switch (i) {
418 case RPC_DISPLAY_PROTO:
419 case RPC_DISPLAY_NETID:
420 continue;
421 default:
422 kfree(xprt->address_strings[i]);
423 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400424}
425
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400426#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
427
Trond Myklebust24c56842006-10-17 15:06:22 -0400428static 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 -0400429{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400430 struct msghdr msg = {
431 .msg_name = addr,
432 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400433 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
434 };
435 struct kvec iov = {
436 .iov_base = vec->iov_base + base,
437 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400438 };
439
Trond Myklebust24c56842006-10-17 15:06:22 -0400440 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400441 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
442 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
443}
444
Trond Myklebust24c56842006-10-17 15:06:22 -0400445static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400446{
Trond Myklebust24c56842006-10-17 15:06:22 -0400447 struct page **ppage;
448 unsigned int remainder;
449 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400450
Trond Myklebust24c56842006-10-17 15:06:22 -0400451 remainder = xdr->page_len - base;
452 base += xdr->page_base;
453 ppage = xdr->pages + (base >> PAGE_SHIFT);
454 base &= ~PAGE_MASK;
455 for(;;) {
456 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
457 int flags = XS_SENDMSG_FLAGS;
458
459 remainder -= len;
460 if (remainder != 0 || more)
461 flags |= MSG_MORE;
462 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
463 if (remainder == 0 || err != len)
464 break;
465 sent += err;
466 ppage++;
467 base = 0;
468 }
469 if (sent == 0)
470 return err;
471 if (err > 0)
472 sent += err;
473 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400474}
475
Chuck Lever9903cd12005-08-11 16:25:26 -0400476/**
477 * xs_sendpages - write pages directly to a socket
478 * @sock: socket to send on
479 * @addr: UDP only -- address of destination
480 * @addrlen: UDP only -- length of destination address
481 * @xdr: buffer containing this request
482 * @base: starting position in the buffer
483 *
Chuck Levera246b012005-08-11 16:25:23 -0400484 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400485static 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 -0400486{
Trond Myklebust24c56842006-10-17 15:06:22 -0400487 unsigned int remainder = xdr->len - base;
488 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400489
Chuck Lever262965f2005-08-11 16:25:56 -0400490 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400491 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400492
493 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400494 if (base != 0) {
495 addr = NULL;
496 addrlen = 0;
497 }
Chuck Lever262965f2005-08-11 16:25:56 -0400498
Trond Myklebust24c56842006-10-17 15:06:22 -0400499 if (base < xdr->head[0].iov_len || addr != NULL) {
500 unsigned int len = xdr->head[0].iov_len - base;
501 remainder -= len;
502 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
503 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400504 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400505 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400506 base = 0;
507 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400508 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400509
Trond Myklebust24c56842006-10-17 15:06:22 -0400510 if (base < xdr->page_len) {
511 unsigned int len = xdr->page_len - base;
512 remainder -= len;
513 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
514 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400515 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400516 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400517 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400518 } else
519 base -= xdr->page_len;
520
521 if (base >= xdr->tail[0].iov_len)
522 return sent;
523 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400524out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400525 if (sent == 0)
526 return err;
527 if (err > 0)
528 sent += err;
529 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400530}
531
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400532static void xs_nospace_callback(struct rpc_task *task)
533{
534 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
535
536 transport->inet->sk_write_pending--;
537 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
538}
539
Chuck Lever9903cd12005-08-11 16:25:26 -0400540/**
Chuck Lever262965f2005-08-11 16:25:56 -0400541 * xs_nospace - place task on wait queue if transmit was incomplete
542 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400543 *
Chuck Levera246b012005-08-11 16:25:23 -0400544 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400545static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400546{
Chuck Lever262965f2005-08-11 16:25:56 -0400547 struct rpc_rqst *req = task->tk_rqstp;
548 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500549 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400550 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400551
Chuck Lever46121cf2007-01-31 12:14:08 -0500552 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400553 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
554 req->rq_slen);
555
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400556 /* Protect against races with write_space */
557 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400558
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400559 /* Don't race with disconnect */
560 if (xprt_connected(xprt)) {
561 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400562 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400563 /*
564 * Notify TCP that we're limited by the application
565 * window size
566 */
567 set_bit(SOCK_NOSPACE, &transport->sock->flags);
568 transport->inet->sk_write_pending++;
569 /* ...and wait for more buffer space */
570 xprt_wait_for_buffer_space(task, xs_nospace_callback);
571 }
572 } else {
573 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400574 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400575 }
Chuck Lever262965f2005-08-11 16:25:56 -0400576
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400577 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400578 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400579}
580
581/**
582 * xs_udp_send_request - write an RPC request to a UDP socket
583 * @task: address of RPC task that manages the state of an RPC request
584 *
585 * Return values:
586 * 0: The request has been sent
587 * EAGAIN: The socket was blocked, please call again later to
588 * complete the request
589 * ENOTCONN: Caller needs to invoke connect logic then call again
590 * other: Some other error occured, the request was not sent
591 */
592static int xs_udp_send_request(struct rpc_task *task)
593{
594 struct rpc_rqst *req = task->tk_rqstp;
595 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500596 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400597 struct xdr_buf *xdr = &req->rq_snd_buf;
598 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400599
Chuck Lever9903cd12005-08-11 16:25:26 -0400600 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400601 req->rq_svec->iov_base,
602 req->rq_svec->iov_len);
603
Trond Myklebust01d37c42009-03-11 14:09:39 -0400604 if (!xprt_bound(xprt))
605 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500606 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400607 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500608 xprt->addrlen, xdr,
609 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400610
Chuck Lever46121cf2007-01-31 12:14:08 -0500611 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400612 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400613
Trond Myklebust21997002007-10-01 11:43:37 -0400614 if (status >= 0) {
615 task->tk_bytes_sent += status;
616 if (status >= req->rq_slen)
617 return 0;
618 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400619 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400620 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400621 if (!transport->sock)
622 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400623
Chuck Lever262965f2005-08-11 16:25:56 -0400624 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400625 case -ENOTSOCK:
626 status = -ENOTCONN;
627 /* Should we call xs_close() here? */
628 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400629 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400630 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400631 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400632 default:
633 dprintk("RPC: sendmsg returned unrecognized error %d\n",
634 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400635 case -ENETUNREACH:
636 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400637 case -ECONNREFUSED:
638 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400639 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400640 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400641 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400642out:
Chuck Lever262965f2005-08-11 16:25:56 -0400643 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400644}
645
Trond Myklebuste06799f2007-11-05 15:44:12 -0500646/**
647 * xs_tcp_shutdown - gracefully shut down a TCP socket
648 * @xprt: transport
649 *
650 * Initiates a graceful shutdown of the TCP socket by calling the
651 * equivalent of shutdown(SHUT_WR);
652 */
653static void xs_tcp_shutdown(struct rpc_xprt *xprt)
654{
655 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
656 struct socket *sock = transport->sock;
657
658 if (sock != NULL)
659 kernel_sock_shutdown(sock, SHUT_WR);
660}
661
Chuck Lever808012f2005-08-25 16:25:49 -0700662static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
663{
664 u32 reclen = buf->len - sizeof(rpc_fraghdr);
665 rpc_fraghdr *base = buf->head[0].iov_base;
666 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
667}
668
Chuck Lever9903cd12005-08-11 16:25:26 -0400669/**
Chuck Lever262965f2005-08-11 16:25:56 -0400670 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400671 * @task: address of RPC task that manages the state of an RPC request
672 *
673 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400674 * 0: The request has been sent
675 * EAGAIN: The socket was blocked, please call again later to
676 * complete the request
677 * ENOTCONN: Caller needs to invoke connect logic then call again
678 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400679 *
680 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400681 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 */
Chuck Lever262965f2005-08-11 16:25:56 -0400683static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400684{
685 struct rpc_rqst *req = task->tk_rqstp;
686 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500687 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400688 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400689 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400690
Chuck Lever808012f2005-08-25 16:25:49 -0700691 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400692
Chuck Lever262965f2005-08-11 16:25:56 -0400693 xs_pktdump("packet data:",
694 req->rq_svec->iov_base,
695 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400696
697 /* Continue transmitting the packet/record. We must be careful
698 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400699 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400700 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500701 status = xs_sendpages(transport->sock,
702 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400703
Chuck Lever46121cf2007-01-31 12:14:08 -0500704 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400705 xdr->len - req->rq_bytes_sent, status);
706
707 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400708 break;
709
Chuck Lever262965f2005-08-11 16:25:56 -0400710 /* If we've sent the entire packet, immediately
711 * reset the count of bytes sent. */
712 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500713 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400714 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
715 req->rq_bytes_sent = 0;
716 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400717 }
718
Trond Myklebust06b4b682008-04-16 16:51:38 -0400719 if (status != 0)
720 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400721 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400722 break;
Chuck Levera246b012005-08-11 16:25:23 -0400723 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400724 if (!transport->sock)
725 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400726
Chuck Lever262965f2005-08-11 16:25:56 -0400727 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400728 case -ENOTSOCK:
729 status = -ENOTCONN;
730 /* Should we call xs_close() here? */
731 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400732 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400733 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400734 break;
735 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500736 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400737 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400738 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400739 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500740 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400741 case -ECONNREFUSED:
742 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400743 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400744 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400745out:
Chuck Levera246b012005-08-11 16:25:23 -0400746 return status;
747}
748
Chuck Lever9903cd12005-08-11 16:25:26 -0400749/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400750 * xs_tcp_release_xprt - clean up after a tcp transmission
751 * @xprt: transport
752 * @task: rpc task
753 *
754 * This cleans up if an error causes us to abort the transmission of a request.
755 * In this case, the socket may need to be reset in order to avoid confusing
756 * the server.
757 */
758static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
759{
760 struct rpc_rqst *req;
761
762 if (task != xprt->snd_task)
763 return;
764 if (task == NULL)
765 goto out_release;
766 req = task->tk_rqstp;
767 if (req->rq_bytes_sent == 0)
768 goto out_release;
769 if (req->rq_bytes_sent == req->rq_snd_buf.len)
770 goto out_release;
771 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
772out_release:
773 xprt_release_xprt(xprt, task);
774}
775
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400776static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
777{
778 transport->old_data_ready = sk->sk_data_ready;
779 transport->old_state_change = sk->sk_state_change;
780 transport->old_write_space = sk->sk_write_space;
781 transport->old_error_report = sk->sk_error_report;
782}
783
784static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
785{
786 sk->sk_data_ready = transport->old_data_ready;
787 sk->sk_state_change = transport->old_state_change;
788 sk->sk_write_space = transport->old_write_space;
789 sk->sk_error_report = transport->old_error_report;
790}
791
Chuck Leverfe315e72009-03-11 14:10:21 -0400792static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400793{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500794 struct socket *sock = transport->sock;
795 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400796
Chuck Leverfe315e72009-03-11 14:10:21 -0400797 if (sk == NULL)
798 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400799
Chuck Levera246b012005-08-11 16:25:23 -0400800 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500801 transport->inet = NULL;
802 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400803
Chuck Lever9903cd12005-08-11 16:25:26 -0400804 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400805
806 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400807 write_unlock_bh(&sk->sk_callback_lock);
808
Chuck Lever9903cd12005-08-11 16:25:26 -0400809 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400810
811 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400812}
813
814/**
815 * xs_close - close a socket
816 * @xprt: transport
817 *
818 * This is used when all requests are complete; ie, no DRC state remains
819 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400820 *
821 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
822 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400823 */
824static void xs_close(struct rpc_xprt *xprt)
825{
826 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
827
828 dprintk("RPC: xs_close xprt %p\n", xprt);
829
830 xs_reset_transport(transport);
831
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100832 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400833 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100834 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500835 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100836 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500837 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400838}
839
Trond Myklebustf75e6742009-04-21 17:18:20 -0400840static void xs_tcp_close(struct rpc_xprt *xprt)
841{
842 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
843 xs_close(xprt);
844 else
845 xs_tcp_shutdown(xprt);
846}
847
Chuck Lever9903cd12005-08-11 16:25:26 -0400848/**
849 * xs_destroy - prepare to shutdown a transport
850 * @xprt: doomed transport
851 *
852 */
853static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400854{
Chuck Leverc8475462006-12-05 16:35:26 -0500855 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
856
Chuck Lever46121cf2007-01-31 12:14:08 -0500857 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400858
Trond Myklebustc1384c92007-06-14 18:00:42 -0400859 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400860
Chuck Lever9903cd12005-08-11 16:25:26 -0400861 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400862 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400863 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400864 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400865 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400866}
867
Chuck Lever9903cd12005-08-11 16:25:26 -0400868static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400869{
Chuck Lever9903cd12005-08-11 16:25:26 -0400870 return (struct rpc_xprt *) sk->sk_user_data;
871}
872
873/**
874 * xs_udp_data_ready - "data ready" callback for UDP sockets
875 * @sk: socket with data to read
876 * @len: how much data to read
877 *
878 */
879static void xs_udp_data_ready(struct sock *sk, int len)
880{
881 struct rpc_task *task;
882 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400883 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400884 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400885 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700886 u32 _xid;
887 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400888
889 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500890 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400891 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400892 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400893
894 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
895 goto out;
896
897 if (xprt->shutdown)
898 goto dropit;
899
900 repsize = skb->len - sizeof(struct udphdr);
901 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500902 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400903 goto dropit;
904 }
905
906 /* Copy the XID from the skb... */
907 xp = skb_header_pointer(skb, sizeof(struct udphdr),
908 sizeof(_xid), &_xid);
909 if (xp == NULL)
910 goto dropit;
911
912 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400913 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400914 rovr = xprt_lookup_rqst(xprt, *xp);
915 if (!rovr)
916 goto out_unlock;
917 task = rovr->rq_task;
918
Chuck Levera246b012005-08-11 16:25:23 -0400919 if ((copied = rovr->rq_private_buf.buflen) > repsize)
920 copied = repsize;
921
922 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800923 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
924 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400925 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800926 }
927
928 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400929
930 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000931 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400932
Chuck Lever1570c1e2005-08-25 16:25:52 -0700933 xprt_adjust_cwnd(task, copied);
934 xprt_update_rtt(task);
935 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400936
937 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400938 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400939 dropit:
940 skb_free_datagram(sk, skb);
941 out:
942 read_unlock(&sk->sk_callback_lock);
943}
944
Chuck Leverdd456472006-12-05 16:35:44 -0500945static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400946{
Chuck Lever51971132006-12-05 16:35:19 -0500947 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400948 size_t len, used;
949 char *p;
950
Chuck Lever51971132006-12-05 16:35:19 -0500951 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
952 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500953 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500954 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400955 if (used != len)
956 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700957
Chuck Lever51971132006-12-05 16:35:19 -0500958 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
959 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500960 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400961 else
Chuck Levere136d092006-12-05 16:35:23 -0500962 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500963 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700964
Chuck Levere136d092006-12-05 16:35:23 -0500965 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500966 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700967
Chuck Levera246b012005-08-11 16:25:23 -0400968 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400969 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500970 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500971 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400972 return;
Chuck Levera246b012005-08-11 16:25:23 -0400973 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500974 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500975 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400976}
977
Chuck Lever51971132006-12-05 16:35:19 -0500978static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400979{
Chuck Lever51971132006-12-05 16:35:19 -0500980 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500981 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500982 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500983 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
984 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
985 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500986 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400987 }
988 }
989}
990
Chuck Leverdd456472006-12-05 16:35:44 -0500991static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400992{
993 size_t len, used;
994 char *p;
995
Chuck Lever51971132006-12-05 16:35:19 -0500996 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500997 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500998 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500999 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -05001000 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -04001001 if (used != len)
1002 return;
Chuck Levere136d092006-12-05 16:35:23 -05001003 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001004 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001005 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001006 dprintk("RPC: reading %s XID %08x\n",
1007 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1008 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001009 ntohl(transport->tcp_xid));
1010 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001011}
1012
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001013static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1014 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001015{
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001016 size_t len, used;
1017 u32 offset;
1018 __be32 calldir;
1019
1020 /*
1021 * We want transport->tcp_offset to be 8 at the end of this routine
1022 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1023 * When this function is called for the first time,
1024 * transport->tcp_offset is 4 (after having already read the xid).
1025 */
1026 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1027 len = sizeof(calldir) - offset;
1028 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1029 used = xdr_skb_read_bits(desc, &calldir, len);
1030 transport->tcp_offset += used;
1031 if (used != len)
1032 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001033 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1034 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001035 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001036 /*
1037 * We don't yet have the XDR buffer, so we will write the calldir
1038 * out after we get the buffer from the 'struct rpc_rqst'
1039 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001040 if (ntohl(calldir) == RPC_REPLY)
1041 transport->tcp_flags |= TCP_RPC_REPLY;
1042 else
1043 transport->tcp_flags &= ~TCP_RPC_REPLY;
1044 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
1045 (transport->tcp_flags & TCP_RPC_REPLY) ?
1046 "reply for" : "request with", calldir);
1047 xs_tcp_check_fraghdr(transport);
1048}
1049
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001050static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1051 struct xdr_skb_reader *desc,
1052 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -04001053{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001054 struct sock_xprt *transport =
1055 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001056 struct xdr_buf *rcvbuf;
1057 size_t len;
1058 ssize_t r;
1059
Chuck Levera246b012005-08-11 16:25:23 -04001060 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001061
1062 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1063 /*
1064 * Save the RPC direction in the XDR buffer
1065 */
1066 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1067 htonl(RPC_REPLY) : 0;
1068
1069 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1070 &calldir, sizeof(calldir));
1071 transport->tcp_copied += sizeof(calldir);
1072 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001073 }
1074
Chuck Levera246b012005-08-11 16:25:23 -04001075 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001076 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001077 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001078
Chuck Lever51971132006-12-05 16:35:19 -05001079 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001080 memcpy(&my_desc, desc, sizeof(my_desc));
1081 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001082 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001083 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001084 desc->count -= r;
1085 desc->offset += r;
1086 } else
Chuck Lever51971132006-12-05 16:35:19 -05001087 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001088 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001089
1090 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001091 transport->tcp_copied += r;
1092 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001093 }
1094 if (r != len) {
1095 /* Error when copying to the receive buffer,
1096 * usually because we weren't able to allocate
1097 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001098 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001099 * will not receive any additional updates,
1100 * and time out.
1101 * Any remaining data from this record will
1102 * be discarded.
1103 */
Chuck Levere136d092006-12-05 16:35:23 -05001104 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001105 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001106 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001107 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1108 "tcp_offset = %u, tcp_reclen = %u\n",
1109 xprt, transport->tcp_copied,
1110 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001111 return;
Chuck Levera246b012005-08-11 16:25:23 -04001112 }
1113
Chuck Lever46121cf2007-01-31 12:14:08 -05001114 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001115 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001116 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1117 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1118 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001119
Chuck Lever51971132006-12-05 16:35:19 -05001120 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001121 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001122 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001123 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1124 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001125 }
1126
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001127 return;
1128}
1129
1130/*
1131 * Finds the request corresponding to the RPC xid and invokes the common
1132 * tcp read code to read the data.
1133 */
1134static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1135 struct xdr_skb_reader *desc)
1136{
1137 struct sock_xprt *transport =
1138 container_of(xprt, struct sock_xprt, xprt);
1139 struct rpc_rqst *req;
1140
1141 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1142
1143 /* Find and lock the request corresponding to this xid */
1144 spin_lock(&xprt->transport_lock);
1145 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1146 if (!req) {
1147 dprintk("RPC: XID %08x request not found!\n",
1148 ntohl(transport->tcp_xid));
1149 spin_unlock(&xprt->transport_lock);
1150 return -1;
1151 }
1152
1153 xs_tcp_read_common(xprt, desc, req);
1154
Chuck Levere136d092006-12-05 16:35:23 -05001155 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001156 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001157
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001158 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001159 return 0;
1160}
1161
1162#if defined(CONFIG_NFS_V4_1)
1163/*
1164 * Obtains an rpc_rqst previously allocated and invokes the common
1165 * tcp read code to read the data. The result is placed in the callback
1166 * queue.
1167 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1168 * connection and return -1.
1169 */
1170static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1171 struct xdr_skb_reader *desc)
1172{
1173 struct sock_xprt *transport =
1174 container_of(xprt, struct sock_xprt, xprt);
1175 struct rpc_rqst *req;
1176
1177 req = xprt_alloc_bc_request(xprt);
1178 if (req == NULL) {
1179 printk(KERN_WARNING "Callback slot table overflowed\n");
1180 xprt_force_disconnect(xprt);
1181 return -1;
1182 }
1183
1184 req->rq_xid = transport->tcp_xid;
1185 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1186 xs_tcp_read_common(xprt, desc, req);
1187
1188 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1189 struct svc_serv *bc_serv = xprt->bc_serv;
1190
1191 /*
1192 * Add callback request to callback list. The callback
1193 * service sleeps on the sv_cb_waitq waiting for new
1194 * requests. Wake it up after adding enqueing the
1195 * request.
1196 */
1197 dprintk("RPC: add callback request to list\n");
1198 spin_lock(&bc_serv->sv_cb_lock);
1199 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1200 spin_unlock(&bc_serv->sv_cb_lock);
1201 wake_up(&bc_serv->sv_cb_waitq);
1202 }
1203
1204 req->rq_private_buf.len = transport->tcp_copied;
1205
1206 return 0;
1207}
1208
1209static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1210 struct xdr_skb_reader *desc)
1211{
1212 struct sock_xprt *transport =
1213 container_of(xprt, struct sock_xprt, xprt);
1214
1215 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1216 xs_tcp_read_reply(xprt, desc) :
1217 xs_tcp_read_callback(xprt, desc);
1218}
1219#else
1220static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1221 struct xdr_skb_reader *desc)
1222{
1223 return xs_tcp_read_reply(xprt, desc);
1224}
1225#endif /* CONFIG_NFS_V4_1 */
1226
1227/*
1228 * Read data off the transport. This can be either an RPC_CALL or an
1229 * RPC_REPLY. Relay the processing to helper functions.
1230 */
1231static void xs_tcp_read_data(struct rpc_xprt *xprt,
1232 struct xdr_skb_reader *desc)
1233{
1234 struct sock_xprt *transport =
1235 container_of(xprt, struct sock_xprt, xprt);
1236
1237 if (_xs_tcp_read_data(xprt, desc) == 0)
1238 xs_tcp_check_fraghdr(transport);
1239 else {
1240 /*
1241 * The transport_lock protects the request handling.
1242 * There's no need to hold it to update the tcp_flags.
1243 */
1244 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1245 }
Chuck Levera246b012005-08-11 16:25:23 -04001246}
1247
Chuck Leverdd456472006-12-05 16:35:44 -05001248static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001249{
1250 size_t len;
1251
Chuck Lever51971132006-12-05 16:35:19 -05001252 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001253 if (len > desc->count)
1254 len = desc->count;
1255 desc->count -= len;
1256 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001257 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001258 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001259 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001260}
1261
Chuck Lever9903cd12005-08-11 16:25:26 -04001262static 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 -04001263{
1264 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001265 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001266 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001267 .skb = skb,
1268 .offset = offset,
1269 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001270 };
Chuck Levera246b012005-08-11 16:25:23 -04001271
Chuck Lever46121cf2007-01-31 12:14:08 -05001272 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001273 do {
1274 /* Read in a new fragment marker if necessary */
1275 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001276 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001277 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001278 continue;
1279 }
1280 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001281 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001282 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001283 continue;
1284 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001285 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001286 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001287 xs_tcp_read_calldir(transport, &desc);
1288 continue;
1289 }
Chuck Levera246b012005-08-11 16:25:23 -04001290 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001291 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001292 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001293 continue;
1294 }
1295 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001296 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001297 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001298 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001299 return len - desc.count;
1300}
1301
Chuck Lever9903cd12005-08-11 16:25:26 -04001302/**
1303 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1304 * @sk: socket with data to read
1305 * @bytes: how much data to read
1306 *
1307 */
1308static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001309{
1310 struct rpc_xprt *xprt;
1311 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001312 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001313
Chuck Lever46121cf2007-01-31 12:14:08 -05001314 dprintk("RPC: xs_tcp_data_ready...\n");
1315
Chuck Levera246b012005-08-11 16:25:23 -04001316 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001317 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001318 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001319 if (xprt->shutdown)
1320 goto out;
1321
Chuck Lever9903cd12005-08-11 16:25:26 -04001322 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001323 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001324 do {
1325 rd_desc.count = 65536;
1326 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1327 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001328out:
1329 read_unlock(&sk->sk_callback_lock);
1330}
1331
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001332/*
1333 * Do the equivalent of linger/linger2 handling for dealing with
1334 * broken servers that don't close the socket in a timely
1335 * fashion
1336 */
1337static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1338 unsigned long timeout)
1339{
1340 struct sock_xprt *transport;
1341
1342 if (xprt_test_and_set_connecting(xprt))
1343 return;
1344 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1345 transport = container_of(xprt, struct sock_xprt, xprt);
1346 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1347 timeout);
1348}
1349
1350static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1351{
1352 struct sock_xprt *transport;
1353
1354 transport = container_of(xprt, struct sock_xprt, xprt);
1355
1356 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1357 !cancel_delayed_work(&transport->connect_worker))
1358 return;
1359 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1360 xprt_clear_connecting(xprt);
1361}
1362
1363static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1364{
1365 smp_mb__before_clear_bit();
1366 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1367 clear_bit(XPRT_CLOSING, &xprt->state);
1368 smp_mb__after_clear_bit();
1369 /* Mark transport as closed and wake up all pending tasks */
1370 xprt_disconnect_done(xprt);
1371}
1372
Chuck Lever9903cd12005-08-11 16:25:26 -04001373/**
1374 * xs_tcp_state_change - callback to handle TCP socket state changes
1375 * @sk: socket whose state has changed
1376 *
1377 */
1378static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001379{
Chuck Lever9903cd12005-08-11 16:25:26 -04001380 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001381
1382 read_lock(&sk->sk_callback_lock);
1383 if (!(xprt = xprt_from_sock(sk)))
1384 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001385 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1386 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1387 sk->sk_state, xprt_connected(xprt),
1388 sock_flag(sk, SOCK_DEAD),
1389 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001390
1391 switch (sk->sk_state) {
1392 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001393 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001394 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001395 struct sock_xprt *transport = container_of(xprt,
1396 struct sock_xprt, xprt);
1397
Chuck Levera246b012005-08-11 16:25:23 -04001398 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001399 transport->tcp_offset = 0;
1400 transport->tcp_reclen = 0;
1401 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001402 transport->tcp_flags =
1403 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001404
Trond Myklebust2a491992009-03-11 14:38:00 -04001405 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001406 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001407 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001408 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001409 case TCP_FIN_WAIT1:
1410 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001411 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001412 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001413 set_bit(XPRT_CLOSING, &xprt->state);
1414 smp_mb__before_clear_bit();
1415 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001416 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001417 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001418 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001419 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001420 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001421 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001422 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001423 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001424 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001425 case TCP_CLOSING:
1426 /*
1427 * If the server closed down the connection, make sure that
1428 * we back off before reconnecting
1429 */
1430 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1431 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001432 break;
1433 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001434 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001435 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001436 smp_mb__before_clear_bit();
1437 clear_bit(XPRT_CONNECTED, &xprt->state);
1438 smp_mb__after_clear_bit();
1439 break;
1440 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001441 xs_tcp_cancel_linger_timeout(xprt);
1442 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001443 }
1444 out:
1445 read_unlock(&sk->sk_callback_lock);
1446}
1447
Chuck Lever9903cd12005-08-11 16:25:26 -04001448/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001449 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001450 * @sk: socket
1451 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001452static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001453{
1454 struct rpc_xprt *xprt;
1455
1456 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001457 if (!(xprt = xprt_from_sock(sk)))
1458 goto out;
1459 dprintk("RPC: %s client %p...\n"
1460 "RPC: error %d\n",
1461 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001462 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001463out:
1464 read_unlock(&sk->sk_callback_lock);
1465}
1466
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001467static void xs_write_space(struct sock *sk)
1468{
1469 struct socket *sock;
1470 struct rpc_xprt *xprt;
1471
1472 if (unlikely(!(sock = sk->sk_socket)))
1473 return;
1474 clear_bit(SOCK_NOSPACE, &sock->flags);
1475
1476 if (unlikely(!(xprt = xprt_from_sock(sk))))
1477 return;
1478 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1479 return;
1480
1481 xprt_write_space(xprt);
1482}
1483
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001484/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001485 * xs_udp_write_space - callback invoked when socket buffer space
1486 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001487 * @sk: socket whose state has changed
1488 *
Chuck Levera246b012005-08-11 16:25:23 -04001489 * Called when more output buffer space is available for this socket.
1490 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001491 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001492 * with a bunch of small requests.
1493 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001494static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001495{
Chuck Levera246b012005-08-11 16:25:23 -04001496 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001497
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001498 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001499 if (sock_writeable(sk))
1500 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001501
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001502 read_unlock(&sk->sk_callback_lock);
1503}
Chuck Levera246b012005-08-11 16:25:23 -04001504
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001505/**
1506 * xs_tcp_write_space - callback invoked when socket buffer space
1507 * becomes available
1508 * @sk: socket whose state has changed
1509 *
1510 * Called when more output buffer space is available for this socket.
1511 * We try not to wake our writers until they can make "significant"
1512 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1513 * with a bunch of small requests.
1514 */
1515static void xs_tcp_write_space(struct sock *sk)
1516{
1517 read_lock(&sk->sk_callback_lock);
1518
1519 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001520 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1521 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001522
Chuck Levera246b012005-08-11 16:25:23 -04001523 read_unlock(&sk->sk_callback_lock);
1524}
1525
Chuck Lever470056c2005-08-25 16:25:56 -07001526static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001527{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001528 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1529 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001530
Chuck Lever7c6e0662006-12-05 16:35:30 -05001531 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001532 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001533 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001534 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001535 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001536 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001537 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001538 sk->sk_write_space(sk);
1539 }
1540}
1541
Chuck Lever43118c22005-08-25 16:25:49 -07001542/**
Chuck Lever470056c2005-08-25 16:25:56 -07001543 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001544 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001545 * @sndsize: requested size of send buffer, in bytes
1546 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001547 *
Chuck Lever470056c2005-08-25 16:25:56 -07001548 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001549 */
Chuck Lever470056c2005-08-25 16:25:56 -07001550static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001551{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001552 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1553
1554 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001555 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001556 transport->sndsize = sndsize + 1024;
1557 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001558 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001559 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001560
1561 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001562}
1563
Chuck Lever46c0ee82005-08-25 16:25:52 -07001564/**
1565 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1566 * @task: task that timed out
1567 *
1568 * Adjust the congestion window after a retransmit timeout has occurred.
1569 */
1570static void xs_udp_timer(struct rpc_task *task)
1571{
1572 xprt_adjust_cwnd(task, -ETIMEDOUT);
1573}
1574
Chuck Leverb85d8802006-05-25 01:40:49 -04001575static unsigned short xs_get_random_port(void)
1576{
1577 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1578 unsigned short rand = (unsigned short) net_random() % range;
1579 return rand + xprt_min_resvport;
1580}
1581
Chuck Lever92200412006-01-03 09:55:51 +01001582/**
1583 * xs_set_port - reset the port number in the remote endpoint address
1584 * @xprt: generic transport
1585 * @port: new port number
1586 *
1587 */
1588static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1589{
Chuck Lever95392c52007-08-06 11:57:58 -04001590 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001591
Chuck Lever46121cf2007-01-31 12:14:08 -05001592 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001593
Chuck Lever20612002007-08-06 11:57:23 -04001594 switch (addr->sa_family) {
1595 case AF_INET:
1596 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1597 break;
1598 case AF_INET6:
1599 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1600 break;
1601 default:
1602 BUG();
1603 }
Chuck Lever92200412006-01-03 09:55:51 +01001604}
1605
Trond Myklebust67a391d2007-11-05 17:40:58 -05001606static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1607{
1608 unsigned short port = transport->port;
1609
1610 if (port == 0 && transport->xprt.resvport)
1611 port = xs_get_random_port();
1612 return port;
1613}
1614
1615static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1616{
1617 if (transport->port != 0)
1618 transport->port = 0;
1619 if (!transport->xprt.resvport)
1620 return 0;
1621 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1622 return xprt_max_resvport;
1623 return --port;
1624}
1625
Chuck Lever7dc753f2007-08-06 11:57:28 -04001626static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001627{
1628 struct sockaddr_in myaddr = {
1629 .sin_family = AF_INET,
1630 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001631 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001632 int err, nloop = 0;
1633 unsigned short port = xs_get_srcport(transport, sock);
1634 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001635
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001636 sa = (struct sockaddr_in *)&transport->addr;
1637 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001638 do {
1639 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001640 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001641 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001642 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001643 break;
Chuck Levera246b012005-08-11 16:25:23 -04001644 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001645 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001646 break;
Chuck Levera246b012005-08-11 16:25:23 -04001647 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001648 last = port;
1649 port = xs_next_srcport(transport, sock, port);
1650 if (port > last)
1651 nloop++;
1652 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001653 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1654 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001655 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001656 return err;
1657}
1658
Chuck Lever90058d32007-08-06 11:57:33 -04001659static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1660{
1661 struct sockaddr_in6 myaddr = {
1662 .sin6_family = AF_INET6,
1663 };
1664 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001665 int err, nloop = 0;
1666 unsigned short port = xs_get_srcport(transport, sock);
1667 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001668
Chuck Lever90058d32007-08-06 11:57:33 -04001669 sa = (struct sockaddr_in6 *)&transport->addr;
1670 myaddr.sin6_addr = sa->sin6_addr;
1671 do {
1672 myaddr.sin6_port = htons(port);
1673 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1674 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001675 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001676 break;
1677 if (err == 0) {
1678 transport->port = port;
1679 break;
1680 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001681 last = port;
1682 port = xs_next_srcport(transport, sock, port);
1683 if (port > last)
1684 nloop++;
1685 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001686 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001687 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001688 return err;
1689}
1690
Peter Zijlstraed075362006-12-06 20:35:24 -08001691#ifdef CONFIG_DEBUG_LOCK_ALLOC
1692static struct lock_class_key xs_key[2];
1693static struct lock_class_key xs_slock_key[2];
1694
Chuck Lever8945ee52007-08-06 11:58:04 -04001695static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001696{
1697 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001698
John Heffner02b3d342007-09-12 10:42:12 +02001699 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001700 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1701 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1702}
Peter Zijlstraed075362006-12-06 20:35:24 -08001703
Chuck Lever8945ee52007-08-06 11:58:04 -04001704static inline void xs_reclassify_socket6(struct socket *sock)
1705{
1706 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001707
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001708 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001709 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1710 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001711}
1712#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001713static inline void xs_reclassify_socket4(struct socket *sock)
1714{
1715}
1716
1717static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001718{
1719}
1720#endif
1721
Chuck Lever16be2d22007-08-06 11:57:38 -04001722static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001723{
Chuck Lever16be2d22007-08-06 11:57:38 -04001724 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001725
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001726 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001727 struct sock *sk = sock->sk;
1728
1729 write_lock_bh(&sk->sk_callback_lock);
1730
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001731 xs_save_old_callbacks(transport, sk);
1732
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001733 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001734 sk->sk_data_ready = xs_udp_data_ready;
1735 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001736 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001737 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001738 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001739
1740 xprt_set_connected(xprt);
1741
1742 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001743 transport->sock = sock;
1744 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001745
1746 write_unlock_bh(&sk->sk_callback_lock);
1747 }
Chuck Lever470056c2005-08-25 16:25:56 -07001748 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001749}
1750
Chuck Levera246b012005-08-11 16:25:23 -04001751/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001752 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001753 * @work: RPC transport to connect
1754 *
1755 * Invoked by a work queue tasklet.
1756 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001757static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001758{
1759 struct sock_xprt *transport =
1760 container_of(work, struct sock_xprt, connect_worker.work);
1761 struct rpc_xprt *xprt = &transport->xprt;
1762 struct socket *sock = transport->sock;
1763 int err, status = -EIO;
1764
Trond Myklebust01d37c42009-03-11 14:09:39 -04001765 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001766 goto out;
1767
Chuck Levera246b012005-08-11 16:25:23 -04001768 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001769 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001770
Chuck Leverfe315e72009-03-11 14:10:21 -04001771 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1772 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001773 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001774 goto out;
1775 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001776 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001777
Chuck Lever7dc753f2007-08-06 11:57:28 -04001778 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001779 sock_release(sock);
1780 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001781 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001782
1783 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1784 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1785
Chuck Lever16be2d22007-08-06 11:57:38 -04001786 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001787 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001788out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001789 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001790 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001791}
1792
Chuck Lever68e220b2007-08-06 11:57:48 -04001793/**
1794 * xs_udp_connect_worker6 - set up a UDP socket
1795 * @work: RPC transport to connect
1796 *
1797 * Invoked by a work queue tasklet.
1798 */
1799static void xs_udp_connect_worker6(struct work_struct *work)
1800{
1801 struct sock_xprt *transport =
1802 container_of(work, struct sock_xprt, connect_worker.work);
1803 struct rpc_xprt *xprt = &transport->xprt;
1804 struct socket *sock = transport->sock;
1805 int err, status = -EIO;
1806
Trond Myklebust01d37c42009-03-11 14:09:39 -04001807 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001808 goto out;
1809
1810 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001811 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001812
Chuck Leverfe315e72009-03-11 14:10:21 -04001813 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1814 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001815 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1816 goto out;
1817 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001818 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001819
1820 if (xs_bind6(transport, sock) < 0) {
1821 sock_release(sock);
1822 goto out;
1823 }
1824
1825 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1826 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1827
1828 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001829 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001830out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001831 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001832 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001833}
1834
Chuck Lever3167e122005-08-25 16:25:55 -07001835/*
1836 * We need to preserve the port number so the reply cache on the server can
1837 * find our cached RPC replies when we get around to reconnecting.
1838 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001839static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001840{
1841 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001842 struct sockaddr any;
1843
Chuck Lever46121cf2007-01-31 12:14:08 -05001844 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001845
1846 /*
1847 * Disconnect the transport socket by doing a connect operation
1848 * with AF_UNSPEC. This should return immediately...
1849 */
1850 memset(&any, 0, sizeof(any));
1851 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001852 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001853 if (!result)
1854 xs_sock_mark_closed(xprt);
1855 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001856 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001857 result);
1858}
1859
Trond Myklebust40d25492009-03-11 14:37:58 -04001860static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1861{
1862 unsigned int state = transport->inet->sk_state;
1863
1864 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1865 return;
1866 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1867 return;
1868 xs_abort_connection(xprt, transport);
1869}
1870
Chuck Lever16be2d22007-08-06 11:57:38 -04001871static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001872{
Chuck Lever16be2d22007-08-06 11:57:38 -04001873 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001874
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001875 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001876 struct sock *sk = sock->sk;
1877
1878 write_lock_bh(&sk->sk_callback_lock);
1879
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001880 xs_save_old_callbacks(transport, sk);
1881
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001882 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001883 sk->sk_data_ready = xs_tcp_data_ready;
1884 sk->sk_state_change = xs_tcp_state_change;
1885 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001886 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001887 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001888
1889 /* socket options */
1890 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1891 sock_reset_flag(sk, SOCK_LINGER);
1892 tcp_sk(sk)->linger2 = 0;
1893 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001894
1895 xprt_clear_connected(xprt);
1896
1897 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001898 transport->sock = sock;
1899 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001900
1901 write_unlock_bh(&sk->sk_callback_lock);
1902 }
1903
Trond Myklebust01d37c42009-03-11 14:09:39 -04001904 if (!xprt_bound(xprt))
1905 return -ENOTCONN;
1906
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001907 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001908 xprt->stat.connect_count++;
1909 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001910 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001911}
1912
1913/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001914 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1915 * @xprt: RPC transport to connect
1916 * @transport: socket transport to connect
1917 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001918 *
1919 * Invoked by a work queue tasklet.
1920 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001921static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1922 struct sock_xprt *transport,
1923 struct socket *(*create_sock)(struct rpc_xprt *,
1924 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001925{
Chuck Lever16be2d22007-08-06 11:57:38 -04001926 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001927 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001928
Trond Myklebust01d37c42009-03-11 14:09:39 -04001929 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001930 goto out;
1931
1932 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001933 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001934 sock = create_sock(xprt, transport);
1935 if (IS_ERR(sock)) {
1936 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001937 goto out;
1938 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001939 } else {
1940 int abort_and_exit;
1941
1942 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1943 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001944 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001945 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001946
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001947 if (abort_and_exit)
1948 goto out_eagain;
1949 }
1950
Chuck Lever16be2d22007-08-06 11:57:38 -04001951 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1952 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1953
1954 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001955 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1956 xprt, -status, xprt_connected(xprt),
1957 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001958 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001959 default:
1960 printk("%s: connect returned unhandled error %d\n",
1961 __func__, status);
1962 case -EADDRNOTAVAIL:
1963 /* We're probably in TIME_WAIT. Get rid of existing socket,
1964 * and retry
1965 */
1966 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1967 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001968 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001969 case -ECONNREFUSED:
1970 case -ECONNRESET:
1971 case -ENETUNREACH:
1972 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001973 case 0:
1974 case -EINPROGRESS:
1975 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001976 xprt_clear_connecting(xprt);
1977 return;
Chuck Levera246b012005-08-11 16:25:23 -04001978 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001979out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001980 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001981out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001982 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001983 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001984}
1985
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001986static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1987 struct sock_xprt *transport)
1988{
1989 struct socket *sock;
1990 int err;
1991
1992 /* start from scratch */
1993 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1994 if (err < 0) {
1995 dprintk("RPC: can't create TCP transport socket (%d).\n",
1996 -err);
1997 goto out_err;
1998 }
1999 xs_reclassify_socket4(sock);
2000
2001 if (xs_bind4(transport, sock) < 0) {
2002 sock_release(sock);
2003 goto out_err;
2004 }
2005 return sock;
2006out_err:
2007 return ERR_PTR(-EIO);
2008}
2009
2010/**
Chuck Levera246b012005-08-11 16:25:23 -04002011 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
2012 * @work: RPC transport to connect
2013 *
2014 * Invoked by a work queue tasklet.
2015 */
2016static void xs_tcp_connect_worker4(struct work_struct *work)
2017{
2018 struct sock_xprt *transport =
2019 container_of(work, struct sock_xprt, connect_worker.work);
2020 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04002021
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002022 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
2023}
Chuck Levera246b012005-08-11 16:25:23 -04002024
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002025static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
2026 struct sock_xprt *transport)
2027{
2028 struct socket *sock;
2029 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04002030
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002031 /* start from scratch */
2032 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
2033 if (err < 0) {
2034 dprintk("RPC: can't create TCP transport socket (%d).\n",
2035 -err);
2036 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04002037 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002038 xs_reclassify_socket6(sock);
2039
2040 if (xs_bind6(transport, sock) < 0) {
2041 sock_release(sock);
2042 goto out_err;
2043 }
2044 return sock;
2045out_err:
2046 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04002047}
2048
Chuck Lever9903cd12005-08-11 16:25:26 -04002049/**
Chuck Lever68e220b2007-08-06 11:57:48 -04002050 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2051 * @work: RPC transport to connect
2052 *
2053 * Invoked by a work queue tasklet.
2054 */
2055static void xs_tcp_connect_worker6(struct work_struct *work)
2056{
2057 struct sock_xprt *transport =
2058 container_of(work, struct sock_xprt, connect_worker.work);
2059 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04002060
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002061 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002062}
2063
2064/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002065 * xs_connect - connect a socket to a remote endpoint
2066 * @task: address of RPC task that manages state of connect request
2067 *
2068 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002069 *
2070 * UDP socket connects are synchronous, but we use a work queue anyway
2071 * to guarantee that even unprivileged user processes can set up a
2072 * socket on a privileged port.
2073 *
2074 * If a UDP socket connect fails, the delay behavior here prevents
2075 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002076 */
2077static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002078{
2079 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002080 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002081
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002082 if (xprt_test_and_set_connecting(xprt))
2083 return;
2084
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002085 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002086 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2087 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002088 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002089 queue_delayed_work(rpciod_workqueue,
2090 &transport->connect_worker,
2091 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002092 xprt->reestablish_timeout <<= 1;
2093 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2094 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002095 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002096 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002097 queue_delayed_work(rpciod_workqueue,
2098 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002099 }
2100}
2101
Trond Myklebuste06799f2007-11-05 15:44:12 -05002102static void xs_tcp_connect(struct rpc_task *task)
2103{
2104 struct rpc_xprt *xprt = task->tk_xprt;
2105
Trond Myklebuste06799f2007-11-05 15:44:12 -05002106 /* Exit if we need to wait for socket shutdown to complete */
2107 if (test_bit(XPRT_CLOSING, &xprt->state))
2108 return;
2109 xs_connect(task);
2110}
2111
Chuck Lever262ca072006-03-20 13:44:16 -05002112/**
2113 * xs_udp_print_stats - display UDP socket-specifc stats
2114 * @xprt: rpc_xprt struct containing statistics
2115 * @seq: output file
2116 *
2117 */
2118static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2119{
Chuck Leverc8475462006-12-05 16:35:26 -05002120 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2121
Chuck Lever262ca072006-03-20 13:44:16 -05002122 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002123 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002124 xprt->stat.bind_count,
2125 xprt->stat.sends,
2126 xprt->stat.recvs,
2127 xprt->stat.bad_xids,
2128 xprt->stat.req_u,
2129 xprt->stat.bklog_u);
2130}
2131
2132/**
2133 * xs_tcp_print_stats - display TCP socket-specifc stats
2134 * @xprt: rpc_xprt struct containing statistics
2135 * @seq: output file
2136 *
2137 */
2138static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2139{
Chuck Leverc8475462006-12-05 16:35:26 -05002140 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002141 long idle_time = 0;
2142
2143 if (xprt_connected(xprt))
2144 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2145
2146 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002147 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002148 xprt->stat.bind_count,
2149 xprt->stat.connect_count,
2150 xprt->stat.connect_time,
2151 idle_time,
2152 xprt->stat.sends,
2153 xprt->stat.recvs,
2154 xprt->stat.bad_xids,
2155 xprt->stat.req_u,
2156 xprt->stat.bklog_u);
2157}
2158
Chuck Lever262965f2005-08-11 16:25:56 -04002159static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002160 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002161 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002162 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002163 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002164 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002165 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002166 .buf_alloc = rpc_malloc,
2167 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002168 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002169 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002170 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002171 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002172 .close = xs_close,
2173 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002174 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002175};
2176
2177static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002178 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002179 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002180 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002181 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002182 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002183 .buf_alloc = rpc_malloc,
2184 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002185 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002186 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002187#if defined(CONFIG_NFS_V4_1)
2188 .release_request = bc_release_request,
2189#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002190 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002191 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002192 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002193};
2194
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002195static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002196 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002197{
2198 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002199 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002200
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002201 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002202 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002203 return ERR_PTR(-EBADF);
2204 }
2205
Chuck Leverffc2e512006-12-05 16:35:11 -05002206 new = kzalloc(sizeof(*new), GFP_KERNEL);
2207 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002208 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2209 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002210 return ERR_PTR(-ENOMEM);
2211 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002212 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002213
2214 xprt->max_reqs = slot_table_size;
2215 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2216 if (xprt->slot == NULL) {
2217 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002218 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2219 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002220 return ERR_PTR(-ENOMEM);
2221 }
2222
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002223 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2224 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002225 if (args->srcaddr)
2226 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002227
2228 return xprt;
2229}
2230
Trond Myklebust2881ae72007-12-20 16:03:54 -05002231static const struct rpc_timeout xs_udp_default_timeout = {
2232 .to_initval = 5 * HZ,
2233 .to_maxval = 30 * HZ,
2234 .to_increment = 5 * HZ,
2235 .to_retries = 5,
2236};
2237
Chuck Lever9903cd12005-08-11 16:25:26 -04002238/**
2239 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002240 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002241 *
2242 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002243static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002244{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002245 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002246 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002247 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002248
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002249 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002250 if (IS_ERR(xprt))
2251 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002252 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002253
Chuck Leverec739ef2006-08-22 20:06:15 -04002254 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002255 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002256 /* XXX: header size can vary due to auth type, IPv6, etc. */
2257 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2258
Chuck Lever03bf4b72005-08-25 16:25:55 -07002259 xprt->bind_timeout = XS_BIND_TO;
2260 xprt->connect_timeout = XS_UDP_CONN_TO;
2261 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2262 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002263
Chuck Lever262965f2005-08-11 16:25:56 -04002264 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002265
Trond Myklebustba7392b2007-12-20 16:03:55 -05002266 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002267
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002268 switch (addr->sa_family) {
2269 case AF_INET:
2270 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2271 xprt_set_bound(xprt);
2272
2273 INIT_DELAYED_WORK(&transport->connect_worker,
2274 xs_udp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002275 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002276 break;
2277 case AF_INET6:
2278 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2279 xprt_set_bound(xprt);
2280
2281 INIT_DELAYED_WORK(&transport->connect_worker,
2282 xs_udp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002283 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002284 break;
2285 default:
2286 kfree(xprt);
2287 return ERR_PTR(-EAFNOSUPPORT);
2288 }
2289
Chuck Lever46121cf2007-01-31 12:14:08 -05002290 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002291 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002292
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002293 if (try_module_get(THIS_MODULE))
2294 return xprt;
2295
2296 kfree(xprt->slot);
2297 kfree(xprt);
2298 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002299}
2300
Trond Myklebust2881ae72007-12-20 16:03:54 -05002301static const struct rpc_timeout xs_tcp_default_timeout = {
2302 .to_initval = 60 * HZ,
2303 .to_maxval = 60 * HZ,
2304 .to_retries = 2,
2305};
2306
Chuck Lever9903cd12005-08-11 16:25:26 -04002307/**
2308 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002309 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002310 *
2311 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002312static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002313{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002314 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002315 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002316 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002317
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002318 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002319 if (IS_ERR(xprt))
2320 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002321 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002322
Chuck Leverec739ef2006-08-22 20:06:15 -04002323 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002324 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002325 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002326
Chuck Lever03bf4b72005-08-25 16:25:55 -07002327 xprt->bind_timeout = XS_BIND_TO;
2328 xprt->connect_timeout = XS_TCP_CONN_TO;
2329 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2330 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002331
Chuck Lever262965f2005-08-11 16:25:56 -04002332 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002333 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002334
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002335 switch (addr->sa_family) {
2336 case AF_INET:
2337 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2338 xprt_set_bound(xprt);
2339
2340 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002341 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002342 break;
2343 case AF_INET6:
2344 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2345 xprt_set_bound(xprt);
2346
2347 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002348 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002349 break;
2350 default:
2351 kfree(xprt);
2352 return ERR_PTR(-EAFNOSUPPORT);
2353 }
2354
Chuck Lever46121cf2007-01-31 12:14:08 -05002355 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002356 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002357
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002358 if (try_module_get(THIS_MODULE))
2359 return xprt;
2360
2361 kfree(xprt->slot);
2362 kfree(xprt);
2363 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002364}
Chuck Lever282b32e2006-12-05 16:35:51 -05002365
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002366static struct xprt_class xs_udp_transport = {
2367 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2368 .name = "udp",
2369 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002370 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002371 .setup = xs_setup_udp,
2372};
2373
2374static struct xprt_class xs_tcp_transport = {
2375 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2376 .name = "tcp",
2377 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002378 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002379 .setup = xs_setup_tcp,
2380};
2381
Chuck Lever282b32e2006-12-05 16:35:51 -05002382/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002383 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002384 *
2385 */
2386int init_socket_xprt(void)
2387{
Chuck Leverfbf76682006-12-05 16:35:54 -05002388#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002389 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002390 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002391#endif
2392
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002393 xprt_register_transport(&xs_udp_transport);
2394 xprt_register_transport(&xs_tcp_transport);
2395
Chuck Lever282b32e2006-12-05 16:35:51 -05002396 return 0;
2397}
2398
2399/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002400 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002401 *
2402 */
2403void cleanup_socket_xprt(void)
2404{
Chuck Leverfbf76682006-12-05 16:35:54 -05002405#ifdef RPC_DEBUG
2406 if (sunrpc_table_header) {
2407 unregister_sysctl_table(sunrpc_table_header);
2408 sunrpc_table_header = NULL;
2409 }
2410#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002411
2412 xprt_unregister_transport(&xs_udp_transport);
2413 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002414}