blob: 302a40904923b9f9add1b06f7e909a3a3c08e403 [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 Leverc877b842009-08-09 15:09:36 -0400299static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
300{
301 struct sockaddr *sap = xs_addr(xprt);
302 char buf[128];
303
304 (void)rpc_ntop(sap, buf, sizeof(buf));
305 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
306
307 (void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
308 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
309
310 (void)snprintf(buf, sizeof(buf), "addr=%s port=%s proto=%s",
311 xprt->address_strings[RPC_DISPLAY_ADDR],
312 xprt->address_strings[RPC_DISPLAY_PORT],
313 xprt->address_strings[RPC_DISPLAY_PROTO]);
314 xprt->address_strings[RPC_DISPLAY_ALL] = kstrdup(buf, GFP_KERNEL);
315
316 (void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
317 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
318}
319
Chuck Leverb454ae92008-01-07 18:34:48 -0500320static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
321 const char *protocol,
322 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400323{
Chuck Leverc877b842009-08-09 15:09:36 -0400324 struct sockaddr_in *sin = xs_addr_in(xprt);
325 char buf[16];
Chuck Leveredb267a2006-08-22 20:06:18 -0400326
Chuck Leverb454ae92008-01-07 18:34:48 -0500327 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500328 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400329
330 (void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
331 NIPQUAD(sin->sin_addr.s_addr));
332 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
333
334 xs_format_common_peer_addresses(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400335}
336
Chuck Leverb454ae92008-01-07 18:34:48 -0500337static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
338 const char *protocol,
339 const char *netid)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400340{
Chuck Leverc877b842009-08-09 15:09:36 -0400341 struct sockaddr_in6 *sin6 = xs_addr_in6(xprt);
342 char buf[48];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400343
Chuck Leverb454ae92008-01-07 18:34:48 -0500344 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500345 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400346
347 (void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
348 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
349
350 xs_format_common_peer_addresses(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400351}
352
353static void xs_free_peer_addresses(struct rpc_xprt *xprt)
354{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500355 unsigned int i;
356
357 for (i = 0; i < RPC_DISPLAY_MAX; i++)
358 switch (i) {
359 case RPC_DISPLAY_PROTO:
360 case RPC_DISPLAY_NETID:
361 continue;
362 default:
363 kfree(xprt->address_strings[i]);
364 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400365}
366
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400367#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
368
Trond Myklebust24c56842006-10-17 15:06:22 -0400369static 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 -0400370{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400371 struct msghdr msg = {
372 .msg_name = addr,
373 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400374 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
375 };
376 struct kvec iov = {
377 .iov_base = vec->iov_base + base,
378 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 };
380
Trond Myklebust24c56842006-10-17 15:06:22 -0400381 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400382 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
383 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
384}
385
Trond Myklebust24c56842006-10-17 15:06:22 -0400386static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400387{
Trond Myklebust24c56842006-10-17 15:06:22 -0400388 struct page **ppage;
389 unsigned int remainder;
390 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400391
Trond Myklebust24c56842006-10-17 15:06:22 -0400392 remainder = xdr->page_len - base;
393 base += xdr->page_base;
394 ppage = xdr->pages + (base >> PAGE_SHIFT);
395 base &= ~PAGE_MASK;
396 for(;;) {
397 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
398 int flags = XS_SENDMSG_FLAGS;
399
400 remainder -= len;
401 if (remainder != 0 || more)
402 flags |= MSG_MORE;
403 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
404 if (remainder == 0 || err != len)
405 break;
406 sent += err;
407 ppage++;
408 base = 0;
409 }
410 if (sent == 0)
411 return err;
412 if (err > 0)
413 sent += err;
414 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400415}
416
Chuck Lever9903cd12005-08-11 16:25:26 -0400417/**
418 * xs_sendpages - write pages directly to a socket
419 * @sock: socket to send on
420 * @addr: UDP only -- address of destination
421 * @addrlen: UDP only -- length of destination address
422 * @xdr: buffer containing this request
423 * @base: starting position in the buffer
424 *
Chuck Levera246b012005-08-11 16:25:23 -0400425 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400426static 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 -0400427{
Trond Myklebust24c56842006-10-17 15:06:22 -0400428 unsigned int remainder = xdr->len - base;
429 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400430
Chuck Lever262965f2005-08-11 16:25:56 -0400431 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400432 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400433
434 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400435 if (base != 0) {
436 addr = NULL;
437 addrlen = 0;
438 }
Chuck Lever262965f2005-08-11 16:25:56 -0400439
Trond Myklebust24c56842006-10-17 15:06:22 -0400440 if (base < xdr->head[0].iov_len || addr != NULL) {
441 unsigned int len = xdr->head[0].iov_len - base;
442 remainder -= len;
443 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
444 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400445 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400446 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400447 base = 0;
448 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400449 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400450
Trond Myklebust24c56842006-10-17 15:06:22 -0400451 if (base < xdr->page_len) {
452 unsigned int len = xdr->page_len - base;
453 remainder -= len;
454 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
455 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400456 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400457 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400458 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 } else
460 base -= xdr->page_len;
461
462 if (base >= xdr->tail[0].iov_len)
463 return sent;
464 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400465out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400466 if (sent == 0)
467 return err;
468 if (err > 0)
469 sent += err;
470 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400471}
472
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400473static void xs_nospace_callback(struct rpc_task *task)
474{
475 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
476
477 transport->inet->sk_write_pending--;
478 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
479}
480
Chuck Lever9903cd12005-08-11 16:25:26 -0400481/**
Chuck Lever262965f2005-08-11 16:25:56 -0400482 * xs_nospace - place task on wait queue if transmit was incomplete
483 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400484 *
Chuck Levera246b012005-08-11 16:25:23 -0400485 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400486static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400487{
Chuck Lever262965f2005-08-11 16:25:56 -0400488 struct rpc_rqst *req = task->tk_rqstp;
489 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500490 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400491 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400492
Chuck Lever46121cf2007-01-31 12:14:08 -0500493 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400494 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
495 req->rq_slen);
496
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400497 /* Protect against races with write_space */
498 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400499
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400500 /* Don't race with disconnect */
501 if (xprt_connected(xprt)) {
502 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400503 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400504 /*
505 * Notify TCP that we're limited by the application
506 * window size
507 */
508 set_bit(SOCK_NOSPACE, &transport->sock->flags);
509 transport->inet->sk_write_pending++;
510 /* ...and wait for more buffer space */
511 xprt_wait_for_buffer_space(task, xs_nospace_callback);
512 }
513 } else {
514 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400515 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400516 }
Chuck Lever262965f2005-08-11 16:25:56 -0400517
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400518 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400519 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400520}
521
522/**
523 * xs_udp_send_request - write an RPC request to a UDP socket
524 * @task: address of RPC task that manages the state of an RPC request
525 *
526 * Return values:
527 * 0: The request has been sent
528 * EAGAIN: The socket was blocked, please call again later to
529 * complete the request
530 * ENOTCONN: Caller needs to invoke connect logic then call again
531 * other: Some other error occured, the request was not sent
532 */
533static int xs_udp_send_request(struct rpc_task *task)
534{
535 struct rpc_rqst *req = task->tk_rqstp;
536 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500537 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400538 struct xdr_buf *xdr = &req->rq_snd_buf;
539 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400540
Chuck Lever9903cd12005-08-11 16:25:26 -0400541 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400542 req->rq_svec->iov_base,
543 req->rq_svec->iov_len);
544
Trond Myklebust01d37c42009-03-11 14:09:39 -0400545 if (!xprt_bound(xprt))
546 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500547 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400548 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500549 xprt->addrlen, xdr,
550 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400551
Chuck Lever46121cf2007-01-31 12:14:08 -0500552 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400553 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400554
Trond Myklebust21997002007-10-01 11:43:37 -0400555 if (status >= 0) {
556 task->tk_bytes_sent += status;
557 if (status >= req->rq_slen)
558 return 0;
559 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400560 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400561 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400562 if (!transport->sock)
563 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400564
Chuck Lever262965f2005-08-11 16:25:56 -0400565 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400566 case -ENOTSOCK:
567 status = -ENOTCONN;
568 /* Should we call xs_close() here? */
569 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400570 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400571 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400572 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400573 default:
574 dprintk("RPC: sendmsg returned unrecognized error %d\n",
575 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400576 case -ENETUNREACH:
577 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400578 case -ECONNREFUSED:
579 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400580 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400581 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400582 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400583out:
Chuck Lever262965f2005-08-11 16:25:56 -0400584 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400585}
586
Trond Myklebuste06799f2007-11-05 15:44:12 -0500587/**
588 * xs_tcp_shutdown - gracefully shut down a TCP socket
589 * @xprt: transport
590 *
591 * Initiates a graceful shutdown of the TCP socket by calling the
592 * equivalent of shutdown(SHUT_WR);
593 */
594static void xs_tcp_shutdown(struct rpc_xprt *xprt)
595{
596 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
597 struct socket *sock = transport->sock;
598
599 if (sock != NULL)
600 kernel_sock_shutdown(sock, SHUT_WR);
601}
602
Chuck Lever808012f2005-08-25 16:25:49 -0700603static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
604{
605 u32 reclen = buf->len - sizeof(rpc_fraghdr);
606 rpc_fraghdr *base = buf->head[0].iov_base;
607 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
608}
609
Chuck Lever9903cd12005-08-11 16:25:26 -0400610/**
Chuck Lever262965f2005-08-11 16:25:56 -0400611 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400612 * @task: address of RPC task that manages the state of an RPC request
613 *
614 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400615 * 0: The request has been sent
616 * EAGAIN: The socket was blocked, please call again later to
617 * complete the request
618 * ENOTCONN: Caller needs to invoke connect logic then call again
619 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400620 *
621 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400622 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400623 */
Chuck Lever262965f2005-08-11 16:25:56 -0400624static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400625{
626 struct rpc_rqst *req = task->tk_rqstp;
627 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500628 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400629 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400630 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400631
Chuck Lever808012f2005-08-25 16:25:49 -0700632 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400633
Chuck Lever262965f2005-08-11 16:25:56 -0400634 xs_pktdump("packet data:",
635 req->rq_svec->iov_base,
636 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400637
638 /* Continue transmitting the packet/record. We must be careful
639 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400640 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400641 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500642 status = xs_sendpages(transport->sock,
643 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400644
Chuck Lever46121cf2007-01-31 12:14:08 -0500645 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400646 xdr->len - req->rq_bytes_sent, status);
647
648 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400649 break;
650
Chuck Lever262965f2005-08-11 16:25:56 -0400651 /* If we've sent the entire packet, immediately
652 * reset the count of bytes sent. */
653 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500654 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400655 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
656 req->rq_bytes_sent = 0;
657 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400658 }
659
Trond Myklebust06b4b682008-04-16 16:51:38 -0400660 if (status != 0)
661 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400662 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400663 break;
Chuck Levera246b012005-08-11 16:25:23 -0400664 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400665 if (!transport->sock)
666 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400667
Chuck Lever262965f2005-08-11 16:25:56 -0400668 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400669 case -ENOTSOCK:
670 status = -ENOTCONN;
671 /* Should we call xs_close() here? */
672 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400673 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400674 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400675 break;
676 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500677 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400678 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400679 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400680 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500681 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400682 case -ECONNREFUSED:
683 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400684 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400685 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400686out:
Chuck Levera246b012005-08-11 16:25:23 -0400687 return status;
688}
689
Chuck Lever9903cd12005-08-11 16:25:26 -0400690/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400691 * xs_tcp_release_xprt - clean up after a tcp transmission
692 * @xprt: transport
693 * @task: rpc task
694 *
695 * This cleans up if an error causes us to abort the transmission of a request.
696 * In this case, the socket may need to be reset in order to avoid confusing
697 * the server.
698 */
699static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
700{
701 struct rpc_rqst *req;
702
703 if (task != xprt->snd_task)
704 return;
705 if (task == NULL)
706 goto out_release;
707 req = task->tk_rqstp;
708 if (req->rq_bytes_sent == 0)
709 goto out_release;
710 if (req->rq_bytes_sent == req->rq_snd_buf.len)
711 goto out_release;
712 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
713out_release:
714 xprt_release_xprt(xprt, task);
715}
716
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400717static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
718{
719 transport->old_data_ready = sk->sk_data_ready;
720 transport->old_state_change = sk->sk_state_change;
721 transport->old_write_space = sk->sk_write_space;
722 transport->old_error_report = sk->sk_error_report;
723}
724
725static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
726{
727 sk->sk_data_ready = transport->old_data_ready;
728 sk->sk_state_change = transport->old_state_change;
729 sk->sk_write_space = transport->old_write_space;
730 sk->sk_error_report = transport->old_error_report;
731}
732
Chuck Leverfe315e762009-03-11 14:10:21 -0400733static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400734{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500735 struct socket *sock = transport->sock;
736 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400737
Chuck Leverfe315e762009-03-11 14:10:21 -0400738 if (sk == NULL)
739 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400740
Chuck Levera246b012005-08-11 16:25:23 -0400741 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500742 transport->inet = NULL;
743 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400744
Chuck Lever9903cd12005-08-11 16:25:26 -0400745 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400746
747 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400748 write_unlock_bh(&sk->sk_callback_lock);
749
Chuck Lever9903cd12005-08-11 16:25:26 -0400750 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400751
752 sock_release(sock);
Chuck Leverfe315e762009-03-11 14:10:21 -0400753}
754
755/**
756 * xs_close - close a socket
757 * @xprt: transport
758 *
759 * This is used when all requests are complete; ie, no DRC state remains
760 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400761 *
762 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
763 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e762009-03-11 14:10:21 -0400764 */
765static void xs_close(struct rpc_xprt *xprt)
766{
767 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
768
769 dprintk("RPC: xs_close xprt %p\n", xprt);
770
771 xs_reset_transport(transport);
772
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100773 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400774 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100775 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500776 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100777 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500778 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400779}
780
Trond Myklebustf75e6742009-04-21 17:18:20 -0400781static void xs_tcp_close(struct rpc_xprt *xprt)
782{
783 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
784 xs_close(xprt);
785 else
786 xs_tcp_shutdown(xprt);
787}
788
Chuck Lever9903cd12005-08-11 16:25:26 -0400789/**
790 * xs_destroy - prepare to shutdown a transport
791 * @xprt: doomed transport
792 *
793 */
794static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400795{
Chuck Leverc8475462006-12-05 16:35:26 -0500796 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
797
Chuck Lever46121cf2007-01-31 12:14:08 -0500798 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400799
Trond Myklebustc1384c92007-06-14 18:00:42 -0400800 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400801
Chuck Lever9903cd12005-08-11 16:25:26 -0400802 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400803 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400804 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400805 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400806 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400807}
808
Chuck Lever9903cd12005-08-11 16:25:26 -0400809static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400810{
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 return (struct rpc_xprt *) sk->sk_user_data;
812}
813
814/**
815 * xs_udp_data_ready - "data ready" callback for UDP sockets
816 * @sk: socket with data to read
817 * @len: how much data to read
818 *
819 */
820static void xs_udp_data_ready(struct sock *sk, int len)
821{
822 struct rpc_task *task;
823 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400824 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400826 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700827 u32 _xid;
828 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400829
830 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500831 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400832 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400833 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400834
835 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
836 goto out;
837
838 if (xprt->shutdown)
839 goto dropit;
840
841 repsize = skb->len - sizeof(struct udphdr);
842 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500843 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400844 goto dropit;
845 }
846
847 /* Copy the XID from the skb... */
848 xp = skb_header_pointer(skb, sizeof(struct udphdr),
849 sizeof(_xid), &_xid);
850 if (xp == NULL)
851 goto dropit;
852
853 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400854 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400855 rovr = xprt_lookup_rqst(xprt, *xp);
856 if (!rovr)
857 goto out_unlock;
858 task = rovr->rq_task;
859
Chuck Levera246b012005-08-11 16:25:23 -0400860 if ((copied = rovr->rq_private_buf.buflen) > repsize)
861 copied = repsize;
862
863 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800864 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
865 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400866 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800867 }
868
869 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400870
871 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000872 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400873
Chuck Lever1570c1e2005-08-25 16:25:52 -0700874 xprt_adjust_cwnd(task, copied);
875 xprt_update_rtt(task);
876 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400877
878 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400879 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400880 dropit:
881 skb_free_datagram(sk, skb);
882 out:
883 read_unlock(&sk->sk_callback_lock);
884}
885
Chuck Leverdd456472006-12-05 16:35:44 -0500886static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400887{
Chuck Lever51971132006-12-05 16:35:19 -0500888 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400889 size_t len, used;
890 char *p;
891
Chuck Lever51971132006-12-05 16:35:19 -0500892 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
893 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500894 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500895 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400896 if (used != len)
897 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700898
Chuck Lever51971132006-12-05 16:35:19 -0500899 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
900 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500901 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400902 else
Chuck Levere136d092006-12-05 16:35:23 -0500903 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500904 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700905
Chuck Levere136d092006-12-05 16:35:23 -0500906 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500907 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700908
Chuck Levera246b012005-08-11 16:25:23 -0400909 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400910 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500911 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500912 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400913 return;
Chuck Levera246b012005-08-11 16:25:23 -0400914 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500915 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500916 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400917}
918
Chuck Lever51971132006-12-05 16:35:19 -0500919static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400920{
Chuck Lever51971132006-12-05 16:35:19 -0500921 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500922 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500923 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500924 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
925 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
926 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500927 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400928 }
929 }
930}
931
Chuck Leverdd456472006-12-05 16:35:44 -0500932static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400933{
934 size_t len, used;
935 char *p;
936
Chuck Lever51971132006-12-05 16:35:19 -0500937 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500938 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500939 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500940 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500941 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400942 if (used != len)
943 return;
Chuck Levere136d092006-12-05 16:35:23 -0500944 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400945 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500946 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400947 dprintk("RPC: reading %s XID %08x\n",
948 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
949 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500950 ntohl(transport->tcp_xid));
951 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400952}
953
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400954static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
955 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400956{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400957 size_t len, used;
958 u32 offset;
959 __be32 calldir;
960
961 /*
962 * We want transport->tcp_offset to be 8 at the end of this routine
963 * (4 bytes for the xid and 4 bytes for the call/reply flag).
964 * When this function is called for the first time,
965 * transport->tcp_offset is 4 (after having already read the xid).
966 */
967 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
968 len = sizeof(calldir) - offset;
969 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
970 used = xdr_skb_read_bits(desc, &calldir, len);
971 transport->tcp_offset += used;
972 if (used != len)
973 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400974 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
975 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400976 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400977 /*
978 * We don't yet have the XDR buffer, so we will write the calldir
979 * out after we get the buffer from the 'struct rpc_rqst'
980 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400981 if (ntohl(calldir) == RPC_REPLY)
982 transport->tcp_flags |= TCP_RPC_REPLY;
983 else
984 transport->tcp_flags &= ~TCP_RPC_REPLY;
985 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
986 (transport->tcp_flags & TCP_RPC_REPLY) ?
987 "reply for" : "request with", calldir);
988 xs_tcp_check_fraghdr(transport);
989}
990
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400991static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
992 struct xdr_skb_reader *desc,
993 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400994{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400995 struct sock_xprt *transport =
996 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400997 struct xdr_buf *rcvbuf;
998 size_t len;
999 ssize_t r;
1000
Chuck Levera246b012005-08-11 16:25:23 -04001001 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001002
1003 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1004 /*
1005 * Save the RPC direction in the XDR buffer
1006 */
1007 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1008 htonl(RPC_REPLY) : 0;
1009
1010 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1011 &calldir, sizeof(calldir));
1012 transport->tcp_copied += sizeof(calldir);
1013 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001014 }
1015
Chuck Levera246b012005-08-11 16:25:23 -04001016 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001017 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001018 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001019
Chuck Lever51971132006-12-05 16:35:19 -05001020 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001021 memcpy(&my_desc, desc, sizeof(my_desc));
1022 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001023 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001024 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001025 desc->count -= r;
1026 desc->offset += r;
1027 } else
Chuck Lever51971132006-12-05 16:35:19 -05001028 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001029 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001030
1031 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001032 transport->tcp_copied += r;
1033 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001034 }
1035 if (r != len) {
1036 /* Error when copying to the receive buffer,
1037 * usually because we weren't able to allocate
1038 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001039 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001040 * will not receive any additional updates,
1041 * and time out.
1042 * Any remaining data from this record will
1043 * be discarded.
1044 */
Chuck Levere136d092006-12-05 16:35:23 -05001045 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001046 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001047 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001048 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1049 "tcp_offset = %u, tcp_reclen = %u\n",
1050 xprt, transport->tcp_copied,
1051 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001052 return;
Chuck Levera246b012005-08-11 16:25:23 -04001053 }
1054
Chuck Lever46121cf2007-01-31 12:14:08 -05001055 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001056 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001057 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1058 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1059 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001060
Chuck Lever51971132006-12-05 16:35:19 -05001061 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001062 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001063 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001064 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1065 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
1067
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001068 return;
1069}
1070
1071/*
1072 * Finds the request corresponding to the RPC xid and invokes the common
1073 * tcp read code to read the data.
1074 */
1075static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1076 struct xdr_skb_reader *desc)
1077{
1078 struct sock_xprt *transport =
1079 container_of(xprt, struct sock_xprt, xprt);
1080 struct rpc_rqst *req;
1081
1082 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1083
1084 /* Find and lock the request corresponding to this xid */
1085 spin_lock(&xprt->transport_lock);
1086 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1087 if (!req) {
1088 dprintk("RPC: XID %08x request not found!\n",
1089 ntohl(transport->tcp_xid));
1090 spin_unlock(&xprt->transport_lock);
1091 return -1;
1092 }
1093
1094 xs_tcp_read_common(xprt, desc, req);
1095
Chuck Levere136d092006-12-05 16:35:23 -05001096 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001097 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001098
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001099 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001100 return 0;
1101}
1102
1103#if defined(CONFIG_NFS_V4_1)
1104/*
1105 * Obtains an rpc_rqst previously allocated and invokes the common
1106 * tcp read code to read the data. The result is placed in the callback
1107 * queue.
1108 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1109 * connection and return -1.
1110 */
1111static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1112 struct xdr_skb_reader *desc)
1113{
1114 struct sock_xprt *transport =
1115 container_of(xprt, struct sock_xprt, xprt);
1116 struct rpc_rqst *req;
1117
1118 req = xprt_alloc_bc_request(xprt);
1119 if (req == NULL) {
1120 printk(KERN_WARNING "Callback slot table overflowed\n");
1121 xprt_force_disconnect(xprt);
1122 return -1;
1123 }
1124
1125 req->rq_xid = transport->tcp_xid;
1126 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1127 xs_tcp_read_common(xprt, desc, req);
1128
1129 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1130 struct svc_serv *bc_serv = xprt->bc_serv;
1131
1132 /*
1133 * Add callback request to callback list. The callback
1134 * service sleeps on the sv_cb_waitq waiting for new
1135 * requests. Wake it up after adding enqueing the
1136 * request.
1137 */
1138 dprintk("RPC: add callback request to list\n");
1139 spin_lock(&bc_serv->sv_cb_lock);
1140 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1141 spin_unlock(&bc_serv->sv_cb_lock);
1142 wake_up(&bc_serv->sv_cb_waitq);
1143 }
1144
1145 req->rq_private_buf.len = transport->tcp_copied;
1146
1147 return 0;
1148}
1149
1150static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1151 struct xdr_skb_reader *desc)
1152{
1153 struct sock_xprt *transport =
1154 container_of(xprt, struct sock_xprt, xprt);
1155
1156 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1157 xs_tcp_read_reply(xprt, desc) :
1158 xs_tcp_read_callback(xprt, desc);
1159}
1160#else
1161static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1162 struct xdr_skb_reader *desc)
1163{
1164 return xs_tcp_read_reply(xprt, desc);
1165}
1166#endif /* CONFIG_NFS_V4_1 */
1167
1168/*
1169 * Read data off the transport. This can be either an RPC_CALL or an
1170 * RPC_REPLY. Relay the processing to helper functions.
1171 */
1172static void xs_tcp_read_data(struct rpc_xprt *xprt,
1173 struct xdr_skb_reader *desc)
1174{
1175 struct sock_xprt *transport =
1176 container_of(xprt, struct sock_xprt, xprt);
1177
1178 if (_xs_tcp_read_data(xprt, desc) == 0)
1179 xs_tcp_check_fraghdr(transport);
1180 else {
1181 /*
1182 * The transport_lock protects the request handling.
1183 * There's no need to hold it to update the tcp_flags.
1184 */
1185 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1186 }
Chuck Levera246b012005-08-11 16:25:23 -04001187}
1188
Chuck Leverdd456472006-12-05 16:35:44 -05001189static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001190{
1191 size_t len;
1192
Chuck Lever51971132006-12-05 16:35:19 -05001193 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001194 if (len > desc->count)
1195 len = desc->count;
1196 desc->count -= len;
1197 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001198 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001199 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001200 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001201}
1202
Chuck Lever9903cd12005-08-11 16:25:26 -04001203static 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 -04001204{
1205 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001206 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001207 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001208 .skb = skb,
1209 .offset = offset,
1210 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001211 };
Chuck Levera246b012005-08-11 16:25:23 -04001212
Chuck Lever46121cf2007-01-31 12:14:08 -05001213 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001214 do {
1215 /* Read in a new fragment marker if necessary */
1216 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001217 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001218 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001219 continue;
1220 }
1221 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001222 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001223 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001224 continue;
1225 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001226 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001227 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001228 xs_tcp_read_calldir(transport, &desc);
1229 continue;
1230 }
Chuck Levera246b012005-08-11 16:25:23 -04001231 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001232 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001233 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001234 continue;
1235 }
1236 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001237 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001238 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001239 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001240 return len - desc.count;
1241}
1242
Chuck Lever9903cd12005-08-11 16:25:26 -04001243/**
1244 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1245 * @sk: socket with data to read
1246 * @bytes: how much data to read
1247 *
1248 */
1249static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001250{
1251 struct rpc_xprt *xprt;
1252 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001253 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001254
Chuck Lever46121cf2007-01-31 12:14:08 -05001255 dprintk("RPC: xs_tcp_data_ready...\n");
1256
Chuck Levera246b012005-08-11 16:25:23 -04001257 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001258 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001259 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001260 if (xprt->shutdown)
1261 goto out;
1262
Chuck Lever9903cd12005-08-11 16:25:26 -04001263 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001264 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001265 do {
1266 rd_desc.count = 65536;
1267 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1268 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001269out:
1270 read_unlock(&sk->sk_callback_lock);
1271}
1272
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001273/*
1274 * Do the equivalent of linger/linger2 handling for dealing with
1275 * broken servers that don't close the socket in a timely
1276 * fashion
1277 */
1278static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1279 unsigned long timeout)
1280{
1281 struct sock_xprt *transport;
1282
1283 if (xprt_test_and_set_connecting(xprt))
1284 return;
1285 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1286 transport = container_of(xprt, struct sock_xprt, xprt);
1287 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1288 timeout);
1289}
1290
1291static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1292{
1293 struct sock_xprt *transport;
1294
1295 transport = container_of(xprt, struct sock_xprt, xprt);
1296
1297 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1298 !cancel_delayed_work(&transport->connect_worker))
1299 return;
1300 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1301 xprt_clear_connecting(xprt);
1302}
1303
1304static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1305{
1306 smp_mb__before_clear_bit();
1307 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1308 clear_bit(XPRT_CLOSING, &xprt->state);
1309 smp_mb__after_clear_bit();
1310 /* Mark transport as closed and wake up all pending tasks */
1311 xprt_disconnect_done(xprt);
1312}
1313
Chuck Lever9903cd12005-08-11 16:25:26 -04001314/**
1315 * xs_tcp_state_change - callback to handle TCP socket state changes
1316 * @sk: socket whose state has changed
1317 *
1318 */
1319static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001320{
Chuck Lever9903cd12005-08-11 16:25:26 -04001321 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001322
1323 read_lock(&sk->sk_callback_lock);
1324 if (!(xprt = xprt_from_sock(sk)))
1325 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001326 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1327 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1328 sk->sk_state, xprt_connected(xprt),
1329 sock_flag(sk, SOCK_DEAD),
1330 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001331
1332 switch (sk->sk_state) {
1333 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001334 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001335 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001336 struct sock_xprt *transport = container_of(xprt,
1337 struct sock_xprt, xprt);
1338
Chuck Levera246b012005-08-11 16:25:23 -04001339 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001340 transport->tcp_offset = 0;
1341 transport->tcp_reclen = 0;
1342 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001343 transport->tcp_flags =
1344 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001345
Trond Myklebust2a491992009-03-11 14:38:00 -04001346 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001347 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001348 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001349 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001350 case TCP_FIN_WAIT1:
1351 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001352 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001353 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001354 set_bit(XPRT_CLOSING, &xprt->state);
1355 smp_mb__before_clear_bit();
1356 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001357 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001358 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001359 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001360 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001361 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001362 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e552007-11-06 10:18:36 -05001363 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001364 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001365 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001366 case TCP_CLOSING:
1367 /*
1368 * If the server closed down the connection, make sure that
1369 * we back off before reconnecting
1370 */
1371 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1372 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001373 break;
1374 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001375 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001376 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001377 smp_mb__before_clear_bit();
1378 clear_bit(XPRT_CONNECTED, &xprt->state);
1379 smp_mb__after_clear_bit();
1380 break;
1381 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001382 xs_tcp_cancel_linger_timeout(xprt);
1383 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001384 }
1385 out:
1386 read_unlock(&sk->sk_callback_lock);
1387}
1388
Chuck Lever9903cd12005-08-11 16:25:26 -04001389/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001390 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001391 * @sk: socket
1392 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001393static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001394{
1395 struct rpc_xprt *xprt;
1396
1397 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001398 if (!(xprt = xprt_from_sock(sk)))
1399 goto out;
1400 dprintk("RPC: %s client %p...\n"
1401 "RPC: error %d\n",
1402 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001403 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001404out:
1405 read_unlock(&sk->sk_callback_lock);
1406}
1407
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001408static void xs_write_space(struct sock *sk)
1409{
1410 struct socket *sock;
1411 struct rpc_xprt *xprt;
1412
1413 if (unlikely(!(sock = sk->sk_socket)))
1414 return;
1415 clear_bit(SOCK_NOSPACE, &sock->flags);
1416
1417 if (unlikely(!(xprt = xprt_from_sock(sk))))
1418 return;
1419 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1420 return;
1421
1422 xprt_write_space(xprt);
1423}
1424
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001425/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001426 * xs_udp_write_space - callback invoked when socket buffer space
1427 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001428 * @sk: socket whose state has changed
1429 *
Chuck Levera246b012005-08-11 16:25:23 -04001430 * Called when more output buffer space is available for this socket.
1431 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001432 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001433 * with a bunch of small requests.
1434 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001435static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001436{
Chuck Levera246b012005-08-11 16:25:23 -04001437 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001438
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001439 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001440 if (sock_writeable(sk))
1441 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001442
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001443 read_unlock(&sk->sk_callback_lock);
1444}
Chuck Levera246b012005-08-11 16:25:23 -04001445
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001446/**
1447 * xs_tcp_write_space - callback invoked when socket buffer space
1448 * becomes available
1449 * @sk: socket whose state has changed
1450 *
1451 * Called when more output buffer space is available for this socket.
1452 * We try not to wake our writers until they can make "significant"
1453 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1454 * with a bunch of small requests.
1455 */
1456static void xs_tcp_write_space(struct sock *sk)
1457{
1458 read_lock(&sk->sk_callback_lock);
1459
1460 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001461 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1462 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001463
Chuck Levera246b012005-08-11 16:25:23 -04001464 read_unlock(&sk->sk_callback_lock);
1465}
1466
Chuck Lever470056c2005-08-25 16:25:56 -07001467static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001468{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001469 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1470 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001471
Chuck Lever7c6e0662006-12-05 16:35:30 -05001472 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001473 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001474 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001475 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001476 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001477 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001478 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001479 sk->sk_write_space(sk);
1480 }
1481}
1482
Chuck Lever43118c22005-08-25 16:25:49 -07001483/**
Chuck Lever470056c2005-08-25 16:25:56 -07001484 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001485 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001486 * @sndsize: requested size of send buffer, in bytes
1487 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001488 *
Chuck Lever470056c2005-08-25 16:25:56 -07001489 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001490 */
Chuck Lever470056c2005-08-25 16:25:56 -07001491static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001492{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001493 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1494
1495 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001496 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001497 transport->sndsize = sndsize + 1024;
1498 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001499 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001500 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001501
1502 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001503}
1504
Chuck Lever46c0ee82005-08-25 16:25:52 -07001505/**
1506 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1507 * @task: task that timed out
1508 *
1509 * Adjust the congestion window after a retransmit timeout has occurred.
1510 */
1511static void xs_udp_timer(struct rpc_task *task)
1512{
1513 xprt_adjust_cwnd(task, -ETIMEDOUT);
1514}
1515
Chuck Leverb85d8802006-05-25 01:40:49 -04001516static unsigned short xs_get_random_port(void)
1517{
1518 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1519 unsigned short rand = (unsigned short) net_random() % range;
1520 return rand + xprt_min_resvport;
1521}
1522
Chuck Lever92200412006-01-03 09:55:51 +01001523/**
1524 * xs_set_port - reset the port number in the remote endpoint address
1525 * @xprt: generic transport
1526 * @port: new port number
1527 *
1528 */
1529static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1530{
Chuck Lever95392c52007-08-06 11:57:58 -04001531 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001532
Chuck Lever46121cf2007-01-31 12:14:08 -05001533 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001534
Chuck Lever20612002007-08-06 11:57:23 -04001535 switch (addr->sa_family) {
1536 case AF_INET:
1537 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1538 break;
1539 case AF_INET6:
1540 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1541 break;
1542 default:
1543 BUG();
1544 }
Chuck Lever92200412006-01-03 09:55:51 +01001545}
1546
Trond Myklebust67a391d2007-11-05 17:40:58 -05001547static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1548{
1549 unsigned short port = transport->port;
1550
1551 if (port == 0 && transport->xprt.resvport)
1552 port = xs_get_random_port();
1553 return port;
1554}
1555
1556static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1557{
1558 if (transport->port != 0)
1559 transport->port = 0;
1560 if (!transport->xprt.resvport)
1561 return 0;
1562 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1563 return xprt_max_resvport;
1564 return --port;
1565}
1566
Chuck Lever7dc753f2007-08-06 11:57:28 -04001567static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001568{
1569 struct sockaddr_in myaddr = {
1570 .sin_family = AF_INET,
1571 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001572 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001573 int err, nloop = 0;
1574 unsigned short port = xs_get_srcport(transport, sock);
1575 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001576
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001577 sa = (struct sockaddr_in *)&transport->addr;
1578 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001579 do {
1580 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001581 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001582 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001583 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001584 break;
Chuck Levera246b012005-08-11 16:25:23 -04001585 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001586 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001587 break;
Chuck Levera246b012005-08-11 16:25:23 -04001588 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001589 last = port;
1590 port = xs_next_srcport(transport, sock, port);
1591 if (port > last)
1592 nloop++;
1593 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001594 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1595 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001596 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001597 return err;
1598}
1599
Chuck Lever90058d32007-08-06 11:57:33 -04001600static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1601{
1602 struct sockaddr_in6 myaddr = {
1603 .sin6_family = AF_INET6,
1604 };
1605 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001606 int err, nloop = 0;
1607 unsigned short port = xs_get_srcport(transport, sock);
1608 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001609
Chuck Lever90058d32007-08-06 11:57:33 -04001610 sa = (struct sockaddr_in6 *)&transport->addr;
1611 myaddr.sin6_addr = sa->sin6_addr;
1612 do {
1613 myaddr.sin6_port = htons(port);
1614 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1615 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001616 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001617 break;
1618 if (err == 0) {
1619 transport->port = port;
1620 break;
1621 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001622 last = port;
1623 port = xs_next_srcport(transport, sock, port);
1624 if (port > last)
1625 nloop++;
1626 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001627 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001628 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001629 return err;
1630}
1631
Peter Zijlstraed075362006-12-06 20:35:24 -08001632#ifdef CONFIG_DEBUG_LOCK_ALLOC
1633static struct lock_class_key xs_key[2];
1634static struct lock_class_key xs_slock_key[2];
1635
Chuck Lever8945ee52007-08-06 11:58:04 -04001636static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001637{
1638 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001639
John Heffner02b3d342007-09-12 10:42:12 +02001640 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001641 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1642 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1643}
Peter Zijlstraed075362006-12-06 20:35:24 -08001644
Chuck Lever8945ee52007-08-06 11:58:04 -04001645static inline void xs_reclassify_socket6(struct socket *sock)
1646{
1647 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001648
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001649 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001650 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1651 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001652}
1653#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001654static inline void xs_reclassify_socket4(struct socket *sock)
1655{
1656}
1657
1658static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001659{
1660}
1661#endif
1662
Chuck Lever16be2d22007-08-06 11:57:38 -04001663static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001664{
Chuck Lever16be2d22007-08-06 11:57:38 -04001665 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001666
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001667 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001668 struct sock *sk = sock->sk;
1669
1670 write_lock_bh(&sk->sk_callback_lock);
1671
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001672 xs_save_old_callbacks(transport, sk);
1673
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001674 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001675 sk->sk_data_ready = xs_udp_data_ready;
1676 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001677 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001678 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001679 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001680
1681 xprt_set_connected(xprt);
1682
1683 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001684 transport->sock = sock;
1685 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001686
1687 write_unlock_bh(&sk->sk_callback_lock);
1688 }
Chuck Lever470056c2005-08-25 16:25:56 -07001689 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001690}
1691
Chuck Levera246b012005-08-11 16:25:23 -04001692/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001693 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001694 * @work: RPC transport to connect
1695 *
1696 * Invoked by a work queue tasklet.
1697 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001698static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001699{
1700 struct sock_xprt *transport =
1701 container_of(work, struct sock_xprt, connect_worker.work);
1702 struct rpc_xprt *xprt = &transport->xprt;
1703 struct socket *sock = transport->sock;
1704 int err, status = -EIO;
1705
Trond Myklebust01d37c42009-03-11 14:09:39 -04001706 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001707 goto out;
1708
Chuck Levera246b012005-08-11 16:25:23 -04001709 /* Start by resetting any existing state */
Chuck Leverfe315e762009-03-11 14:10:21 -04001710 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001711
Chuck Leverfe315e762009-03-11 14:10:21 -04001712 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1713 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001714 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001715 goto out;
1716 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001717 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001718
Chuck Lever7dc753f2007-08-06 11:57:28 -04001719 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001720 sock_release(sock);
1721 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001722 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001723
1724 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1725 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1726
Chuck Lever16be2d22007-08-06 11:57:38 -04001727 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001728 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001729out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001730 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001731 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001732}
1733
Chuck Lever68e220b2007-08-06 11:57:48 -04001734/**
1735 * xs_udp_connect_worker6 - set up a UDP socket
1736 * @work: RPC transport to connect
1737 *
1738 * Invoked by a work queue tasklet.
1739 */
1740static void xs_udp_connect_worker6(struct work_struct *work)
1741{
1742 struct sock_xprt *transport =
1743 container_of(work, struct sock_xprt, connect_worker.work);
1744 struct rpc_xprt *xprt = &transport->xprt;
1745 struct socket *sock = transport->sock;
1746 int err, status = -EIO;
1747
Trond Myklebust01d37c42009-03-11 14:09:39 -04001748 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001749 goto out;
1750
1751 /* Start by resetting any existing state */
Chuck Leverfe315e762009-03-11 14:10:21 -04001752 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001753
Chuck Leverfe315e762009-03-11 14:10:21 -04001754 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1755 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001756 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1757 goto out;
1758 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001759 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001760
1761 if (xs_bind6(transport, sock) < 0) {
1762 sock_release(sock);
1763 goto out;
1764 }
1765
1766 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1767 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1768
1769 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001770 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001771out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001772 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001773 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001774}
1775
Chuck Lever3167e122005-08-25 16:25:55 -07001776/*
1777 * We need to preserve the port number so the reply cache on the server can
1778 * find our cached RPC replies when we get around to reconnecting.
1779 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001780static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001781{
1782 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001783 struct sockaddr any;
1784
Chuck Lever46121cf2007-01-31 12:14:08 -05001785 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001786
1787 /*
1788 * Disconnect the transport socket by doing a connect operation
1789 * with AF_UNSPEC. This should return immediately...
1790 */
1791 memset(&any, 0, sizeof(any));
1792 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001793 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001794 if (!result)
1795 xs_sock_mark_closed(xprt);
1796 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001797 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001798 result);
1799}
1800
Trond Myklebust40d25492009-03-11 14:37:58 -04001801static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1802{
1803 unsigned int state = transport->inet->sk_state;
1804
1805 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1806 return;
1807 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1808 return;
1809 xs_abort_connection(xprt, transport);
1810}
1811
Chuck Lever16be2d22007-08-06 11:57:38 -04001812static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001813{
Chuck Lever16be2d22007-08-06 11:57:38 -04001814 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001815
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001816 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001817 struct sock *sk = sock->sk;
1818
1819 write_lock_bh(&sk->sk_callback_lock);
1820
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001821 xs_save_old_callbacks(transport, sk);
1822
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001823 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001824 sk->sk_data_ready = xs_tcp_data_ready;
1825 sk->sk_state_change = xs_tcp_state_change;
1826 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001827 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001828 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001829
1830 /* socket options */
1831 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1832 sock_reset_flag(sk, SOCK_LINGER);
1833 tcp_sk(sk)->linger2 = 0;
1834 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001835
1836 xprt_clear_connected(xprt);
1837
1838 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001839 transport->sock = sock;
1840 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001841
1842 write_unlock_bh(&sk->sk_callback_lock);
1843 }
1844
Trond Myklebust01d37c42009-03-11 14:09:39 -04001845 if (!xprt_bound(xprt))
1846 return -ENOTCONN;
1847
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001848 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001849 xprt->stat.connect_count++;
1850 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001851 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001852}
1853
1854/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001855 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1856 * @xprt: RPC transport to connect
1857 * @transport: socket transport to connect
1858 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001859 *
1860 * Invoked by a work queue tasklet.
1861 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001862static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1863 struct sock_xprt *transport,
1864 struct socket *(*create_sock)(struct rpc_xprt *,
1865 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001866{
Chuck Lever16be2d22007-08-06 11:57:38 -04001867 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001868 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001869
Trond Myklebust01d37c42009-03-11 14:09:39 -04001870 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001871 goto out;
1872
1873 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001874 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001875 sock = create_sock(xprt, transport);
1876 if (IS_ERR(sock)) {
1877 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001878 goto out;
1879 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001880 } else {
1881 int abort_and_exit;
1882
1883 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1884 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001885 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001886 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001887
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001888 if (abort_and_exit)
1889 goto out_eagain;
1890 }
1891
Chuck Lever16be2d22007-08-06 11:57:38 -04001892 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1893 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1894
1895 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001896 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1897 xprt, -status, xprt_connected(xprt),
1898 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001899 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001900 default:
1901 printk("%s: connect returned unhandled error %d\n",
1902 __func__, status);
1903 case -EADDRNOTAVAIL:
1904 /* We're probably in TIME_WAIT. Get rid of existing socket,
1905 * and retry
1906 */
1907 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1908 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001909 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001910 case -ECONNREFUSED:
1911 case -ECONNRESET:
1912 case -ENETUNREACH:
1913 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001914 case 0:
1915 case -EINPROGRESS:
1916 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001917 xprt_clear_connecting(xprt);
1918 return;
Chuck Levera246b012005-08-11 16:25:23 -04001919 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001920out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001921 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001922out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001923 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001924 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001925}
1926
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001927static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1928 struct sock_xprt *transport)
1929{
1930 struct socket *sock;
1931 int err;
1932
1933 /* start from scratch */
1934 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1935 if (err < 0) {
1936 dprintk("RPC: can't create TCP transport socket (%d).\n",
1937 -err);
1938 goto out_err;
1939 }
1940 xs_reclassify_socket4(sock);
1941
1942 if (xs_bind4(transport, sock) < 0) {
1943 sock_release(sock);
1944 goto out_err;
1945 }
1946 return sock;
1947out_err:
1948 return ERR_PTR(-EIO);
1949}
1950
1951/**
Chuck Levera246b012005-08-11 16:25:23 -04001952 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1953 * @work: RPC transport to connect
1954 *
1955 * Invoked by a work queue tasklet.
1956 */
1957static void xs_tcp_connect_worker4(struct work_struct *work)
1958{
1959 struct sock_xprt *transport =
1960 container_of(work, struct sock_xprt, connect_worker.work);
1961 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001962
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001963 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1964}
Chuck Levera246b012005-08-11 16:25:23 -04001965
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001966static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1967 struct sock_xprt *transport)
1968{
1969 struct socket *sock;
1970 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001971
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001972 /* start from scratch */
1973 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1974 if (err < 0) {
1975 dprintk("RPC: can't create TCP transport socket (%d).\n",
1976 -err);
1977 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001978 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001979 xs_reclassify_socket6(sock);
1980
1981 if (xs_bind6(transport, sock) < 0) {
1982 sock_release(sock);
1983 goto out_err;
1984 }
1985 return sock;
1986out_err:
1987 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001988}
1989
Chuck Lever9903cd12005-08-11 16:25:26 -04001990/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001991 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1992 * @work: RPC transport to connect
1993 *
1994 * Invoked by a work queue tasklet.
1995 */
1996static void xs_tcp_connect_worker6(struct work_struct *work)
1997{
1998 struct sock_xprt *transport =
1999 container_of(work, struct sock_xprt, connect_worker.work);
2000 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04002001
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002002 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002003}
2004
2005/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002006 * xs_connect - connect a socket to a remote endpoint
2007 * @task: address of RPC task that manages state of connect request
2008 *
2009 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002010 *
2011 * UDP socket connects are synchronous, but we use a work queue anyway
2012 * to guarantee that even unprivileged user processes can set up a
2013 * socket on a privileged port.
2014 *
2015 * If a UDP socket connect fails, the delay behavior here prevents
2016 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002017 */
2018static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002019{
2020 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002021 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002022
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002023 if (xprt_test_and_set_connecting(xprt))
2024 return;
2025
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002026 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002027 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2028 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002029 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002030 queue_delayed_work(rpciod_workqueue,
2031 &transport->connect_worker,
2032 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002033 xprt->reestablish_timeout <<= 1;
2034 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2035 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002036 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002037 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002038 queue_delayed_work(rpciod_workqueue,
2039 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002040 }
2041}
2042
Trond Myklebuste06799f2007-11-05 15:44:12 -05002043static void xs_tcp_connect(struct rpc_task *task)
2044{
2045 struct rpc_xprt *xprt = task->tk_xprt;
2046
Trond Myklebuste06799f2007-11-05 15:44:12 -05002047 /* Exit if we need to wait for socket shutdown to complete */
2048 if (test_bit(XPRT_CLOSING, &xprt->state))
2049 return;
2050 xs_connect(task);
2051}
2052
Chuck Lever262ca072006-03-20 13:44:16 -05002053/**
2054 * xs_udp_print_stats - display UDP socket-specifc stats
2055 * @xprt: rpc_xprt struct containing statistics
2056 * @seq: output file
2057 *
2058 */
2059static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2060{
Chuck Leverc8475462006-12-05 16:35:26 -05002061 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2062
Chuck Lever262ca072006-03-20 13:44:16 -05002063 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002064 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002065 xprt->stat.bind_count,
2066 xprt->stat.sends,
2067 xprt->stat.recvs,
2068 xprt->stat.bad_xids,
2069 xprt->stat.req_u,
2070 xprt->stat.bklog_u);
2071}
2072
2073/**
2074 * xs_tcp_print_stats - display TCP socket-specifc stats
2075 * @xprt: rpc_xprt struct containing statistics
2076 * @seq: output file
2077 *
2078 */
2079static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2080{
Chuck Leverc8475462006-12-05 16:35:26 -05002081 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002082 long idle_time = 0;
2083
2084 if (xprt_connected(xprt))
2085 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2086
2087 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002088 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002089 xprt->stat.bind_count,
2090 xprt->stat.connect_count,
2091 xprt->stat.connect_time,
2092 idle_time,
2093 xprt->stat.sends,
2094 xprt->stat.recvs,
2095 xprt->stat.bad_xids,
2096 xprt->stat.req_u,
2097 xprt->stat.bklog_u);
2098}
2099
Chuck Lever262965f2005-08-11 16:25:56 -04002100static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002101 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002102 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002103 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002104 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002105 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002106 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002107 .buf_alloc = rpc_malloc,
2108 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002109 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002110 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002111 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002112 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002113 .close = xs_close,
2114 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002115 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002116};
2117
2118static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002119 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002120 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002121 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002122 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002123 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002124 .buf_alloc = rpc_malloc,
2125 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002126 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002127 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002128#if defined(CONFIG_NFS_V4_1)
2129 .release_request = bc_release_request,
2130#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002131 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002132 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002133 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002134};
2135
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002136static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002137 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002138{
2139 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002140 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002141
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002142 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002143 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002144 return ERR_PTR(-EBADF);
2145 }
2146
Chuck Leverffc2e512006-12-05 16:35:11 -05002147 new = kzalloc(sizeof(*new), GFP_KERNEL);
2148 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002149 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2150 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002151 return ERR_PTR(-ENOMEM);
2152 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002153 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002154
2155 xprt->max_reqs = slot_table_size;
2156 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2157 if (xprt->slot == NULL) {
2158 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002159 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2160 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002161 return ERR_PTR(-ENOMEM);
2162 }
2163
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002164 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2165 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002166 if (args->srcaddr)
2167 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002168
2169 return xprt;
2170}
2171
Trond Myklebust2881ae72007-12-20 16:03:54 -05002172static const struct rpc_timeout xs_udp_default_timeout = {
2173 .to_initval = 5 * HZ,
2174 .to_maxval = 30 * HZ,
2175 .to_increment = 5 * HZ,
2176 .to_retries = 5,
2177};
2178
Chuck Lever9903cd12005-08-11 16:25:26 -04002179/**
2180 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002181 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002182 *
2183 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002184static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002185{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002186 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002187 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002188 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002189
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002190 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002191 if (IS_ERR(xprt))
2192 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002193 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002194
Chuck Leverec739ef2006-08-22 20:06:15 -04002195 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002196 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002197 /* XXX: header size can vary due to auth type, IPv6, etc. */
2198 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2199
Chuck Lever03bf4b72005-08-25 16:25:55 -07002200 xprt->bind_timeout = XS_BIND_TO;
2201 xprt->connect_timeout = XS_UDP_CONN_TO;
2202 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2203 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002204
Chuck Lever262965f2005-08-11 16:25:56 -04002205 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002206
Trond Myklebustba7392b2007-12-20 16:03:55 -05002207 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002208
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002209 switch (addr->sa_family) {
2210 case AF_INET:
2211 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2212 xprt_set_bound(xprt);
2213
2214 INIT_DELAYED_WORK(&transport->connect_worker,
2215 xs_udp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002216 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002217 break;
2218 case AF_INET6:
2219 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2220 xprt_set_bound(xprt);
2221
2222 INIT_DELAYED_WORK(&transport->connect_worker,
2223 xs_udp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002224 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002225 break;
2226 default:
2227 kfree(xprt);
2228 return ERR_PTR(-EAFNOSUPPORT);
2229 }
2230
Chuck Lever46121cf2007-01-31 12:14:08 -05002231 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002232 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002233
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002234 if (try_module_get(THIS_MODULE))
2235 return xprt;
2236
2237 kfree(xprt->slot);
2238 kfree(xprt);
2239 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002240}
2241
Trond Myklebust2881ae72007-12-20 16:03:54 -05002242static const struct rpc_timeout xs_tcp_default_timeout = {
2243 .to_initval = 60 * HZ,
2244 .to_maxval = 60 * HZ,
2245 .to_retries = 2,
2246};
2247
Chuck Lever9903cd12005-08-11 16:25:26 -04002248/**
2249 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002250 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002251 *
2252 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002253static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002254{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002255 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002256 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002257 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002258
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002259 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002260 if (IS_ERR(xprt))
2261 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002262 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002263
Chuck Leverec739ef2006-08-22 20:06:15 -04002264 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002265 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002266 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002267
Chuck Lever03bf4b72005-08-25 16:25:55 -07002268 xprt->bind_timeout = XS_BIND_TO;
2269 xprt->connect_timeout = XS_TCP_CONN_TO;
2270 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2271 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002272
Chuck Lever262965f2005-08-11 16:25:56 -04002273 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002274 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002275
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002276 switch (addr->sa_family) {
2277 case AF_INET:
2278 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2279 xprt_set_bound(xprt);
2280
2281 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002282 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002283 break;
2284 case AF_INET6:
2285 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2286 xprt_set_bound(xprt);
2287
2288 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002289 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002290 break;
2291 default:
2292 kfree(xprt);
2293 return ERR_PTR(-EAFNOSUPPORT);
2294 }
2295
Chuck Lever46121cf2007-01-31 12:14:08 -05002296 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002297 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002298
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002299 if (try_module_get(THIS_MODULE))
2300 return xprt;
2301
2302 kfree(xprt->slot);
2303 kfree(xprt);
2304 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002305}
Chuck Lever282b32e2006-12-05 16:35:51 -05002306
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002307static struct xprt_class xs_udp_transport = {
2308 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2309 .name = "udp",
2310 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002311 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002312 .setup = xs_setup_udp,
2313};
2314
2315static struct xprt_class xs_tcp_transport = {
2316 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2317 .name = "tcp",
2318 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002319 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002320 .setup = xs_setup_tcp,
2321};
2322
Chuck Lever282b32e2006-12-05 16:35:51 -05002323/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002324 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002325 *
2326 */
2327int init_socket_xprt(void)
2328{
Chuck Leverfbf76682006-12-05 16:35:54 -05002329#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002330 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002331 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002332#endif
2333
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002334 xprt_register_transport(&xs_udp_transport);
2335 xprt_register_transport(&xs_tcp_transport);
2336
Chuck Lever282b32e2006-12-05 16:35:51 -05002337 return 0;
2338}
2339
2340/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002341 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002342 *
2343 */
2344void cleanup_socket_xprt(void)
2345{
Chuck Leverfbf76682006-12-05 16:35:54 -05002346#ifdef RPC_DEBUG
2347 if (sunrpc_table_header) {
2348 unregister_sysctl_table(sunrpc_table_header);
2349 sunrpc_table_header = NULL;
2350 }
2351#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002352
2353 xprt_unregister_transport(&xs_udp_transport);
2354 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002355}