blob: 37c5475ba258b51b22c6722aeb605741c2732051 [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>
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030035#include <linux/sunrpc/svcsock.h>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040036#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040037#include <linux/file.h>
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -040038#ifdef CONFIG_NFS_V4_1
39#include <linux/sunrpc/bc_xprt.h>
40#endif
Chuck Levera246b012005-08-11 16:25:23 -040041
42#include <net/sock.h>
43#include <net/checksum.h>
44#include <net/udp.h>
45#include <net/tcp.h>
46
Rahul Iyer4cfc7e62009-09-10 17:32:28 +030047#include "sunrpc.h"
Chuck Lever9903cd12005-08-11 16:25:26 -040048/*
Chuck Leverc556b752005-11-01 12:24:48 -050049 * xprtsock tunables
50 */
51unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
52unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
53
54unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
55unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
56
Trond Myklebust7d1e8252009-03-11 14:38:03 -040057#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040058static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040059
Chuck Leverc556b752005-11-01 12:24:48 -050060/*
Chuck Leverfbf76682006-12-05 16:35:54 -050061 * We can register our own files under /proc/sys/sunrpc by
62 * calling register_sysctl_table() again. The files in that
63 * directory become the union of all files registered there.
64 *
65 * We simply need to make sure that we don't collide with
66 * someone else's file names!
67 */
68
69#ifdef RPC_DEBUG
70
71static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
72static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
73static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
74static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
75
76static struct ctl_table_header *sunrpc_table_header;
77
78/*
79 * FIXME: changing the UDP slot table size should also resize the UDP
80 * socket buffers for existing UDP transports
81 */
82static ctl_table xs_tunables_table[] = {
83 {
84 .ctl_name = CTL_SLOTTABLE_UDP,
85 .procname = "udp_slot_table_entries",
86 .data = &xprt_udp_slot_table_entries,
87 .maxlen = sizeof(unsigned int),
88 .mode = 0644,
89 .proc_handler = &proc_dointvec_minmax,
90 .strategy = &sysctl_intvec,
91 .extra1 = &min_slot_table_size,
92 .extra2 = &max_slot_table_size
93 },
94 {
95 .ctl_name = CTL_SLOTTABLE_TCP,
96 .procname = "tcp_slot_table_entries",
97 .data = &xprt_tcp_slot_table_entries,
98 .maxlen = sizeof(unsigned int),
99 .mode = 0644,
100 .proc_handler = &proc_dointvec_minmax,
101 .strategy = &sysctl_intvec,
102 .extra1 = &min_slot_table_size,
103 .extra2 = &max_slot_table_size
104 },
105 {
106 .ctl_name = CTL_MIN_RESVPORT,
107 .procname = "min_resvport",
108 .data = &xprt_min_resvport,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
111 .proc_handler = &proc_dointvec_minmax,
112 .strategy = &sysctl_intvec,
113 .extra1 = &xprt_min_resvport_limit,
114 .extra2 = &xprt_max_resvport_limit
115 },
116 {
117 .ctl_name = CTL_MAX_RESVPORT,
118 .procname = "max_resvport",
119 .data = &xprt_max_resvport,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
122 .proc_handler = &proc_dointvec_minmax,
123 .strategy = &sysctl_intvec,
124 .extra1 = &xprt_min_resvport_limit,
125 .extra2 = &xprt_max_resvport_limit
126 },
127 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400128 .procname = "tcp_fin_timeout",
129 .data = &xs_tcp_fin_timeout,
130 .maxlen = sizeof(xs_tcp_fin_timeout),
131 .mode = 0644,
132 .proc_handler = &proc_dointvec_jiffies,
133 .strategy = sysctl_jiffies
134 },
135 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500136 .ctl_name = 0,
137 },
138};
139
140static ctl_table sunrpc_table[] = {
141 {
142 .ctl_name = CTL_SUNRPC,
143 .procname = "sunrpc",
144 .mode = 0555,
145 .child = xs_tunables_table
146 },
147 {
148 .ctl_name = 0,
149 },
150};
151
152#endif
153
154/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700155 * Time out for an RPC UDP socket connect. UDP socket connects are
156 * synchronous, but we set a timeout anyway in case of resource
157 * exhaustion on the local host.
158 */
159#define XS_UDP_CONN_TO (5U * HZ)
160
161/*
162 * Wait duration for an RPC TCP connection to be established. Solaris
163 * NFS over TCP uses 60 seconds, for example, which is in line with how
164 * long a server takes to reboot.
165 */
166#define XS_TCP_CONN_TO (60U * HZ)
167
168/*
169 * Wait duration for a reply from the RPC portmapper.
170 */
171#define XS_BIND_TO (60U * HZ)
172
173/*
174 * Delay if a UDP socket connect error occurs. This is most likely some
175 * kind of resource problem on the local host.
176 */
177#define XS_UDP_REEST_TO (2U * HZ)
178
179/*
180 * The reestablish timeout allows clients to delay for a bit before attempting
181 * to reconnect to a server that just dropped our connection.
182 *
183 * We implement an exponential backoff when trying to reestablish a TCP
184 * transport connection with the server. Some servers like to drop a TCP
185 * connection when they are overworked, so we start with a short timeout and
186 * increase over time if the server is down or not responding.
187 */
188#define XS_TCP_INIT_REEST_TO (3U * HZ)
189#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
190
191/*
192 * TCP idle timeout; client drops the transport socket if it is idle
193 * for this long. Note that we also timeout UDP sockets to prevent
194 * holding port numbers when there is no RPC traffic.
195 */
196#define XS_IDLE_DISC_TO (5U * 60 * HZ)
197
Chuck Levera246b012005-08-11 16:25:23 -0400198#ifdef RPC_DEBUG
199# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400200# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400201#endif
202
Chuck Levera246b012005-08-11 16:25:23 -0400203#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400204static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400205{
Chuck Lever9903cd12005-08-11 16:25:26 -0400206 u8 *buf = (u8 *) packet;
207 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400208
Chuck Lever46121cf2007-01-31 12:14:08 -0500209 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400210 for (j = 0; j < count && j < 128; j += 4) {
211 if (!(j & 31)) {
212 if (j)
213 dprintk("\n");
214 dprintk("0x%04x ", j);
215 }
216 dprintk("%02x%02x%02x%02x ",
217 buf[j], buf[j+1], buf[j+2], buf[j+3]);
218 }
219 dprintk("\n");
220}
221#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400222static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400223{
224 /* NOP */
225}
226#endif
227
Chuck Leverffc2e512006-12-05 16:35:11 -0500228struct sock_xprt {
229 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500230
231 /*
232 * Network layer
233 */
234 struct socket * sock;
235 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500236
237 /*
238 * State of TCP reply receive
239 */
240 __be32 tcp_fraghdr,
241 tcp_xid;
242
243 u32 tcp_offset,
244 tcp_reclen;
245
246 unsigned long tcp_copied,
247 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500248
249 /*
250 * Connection of transports
251 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500252 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400253 struct sockaddr_storage srcaddr;
254 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500255
256 /*
257 * UDP socket buffer size parameters
258 */
259 size_t rcvsize,
260 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500261
262 /*
263 * Saved socket callback addresses
264 */
265 void (*old_data_ready)(struct sock *, int);
266 void (*old_state_change)(struct sock *);
267 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400268 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500269};
270
Chuck Levere136d092006-12-05 16:35:23 -0500271/*
272 * TCP receive state flags
273 */
274#define TCP_RCV_LAST_FRAG (1UL << 0)
275#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
276#define TCP_RCV_COPY_XID (1UL << 2)
277#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400278#define TCP_RCV_READ_CALLDIR (1UL << 4)
279#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400280
281/*
282 * TCP RPC flags
283 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400284#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500285
Chuck Lever95392c52007-08-06 11:57:58 -0400286static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400287{
Chuck Lever95392c52007-08-06 11:57:58 -0400288 return (struct sockaddr *) &xprt->addr;
289}
290
291static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
292{
293 return (struct sockaddr_in *) &xprt->addr;
294}
295
296static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
297{
298 return (struct sockaddr_in6 *) &xprt->addr;
299}
300
Chuck Leverc877b842009-08-09 15:09:36 -0400301static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400302{
Chuck Leverc877b842009-08-09 15:09:36 -0400303 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400304 struct sockaddr_in6 *sin6;
305 struct sockaddr_in *sin;
Chuck Leverc877b842009-08-09 15:09:36 -0400306 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400307
Chuck Leverc877b842009-08-09 15:09:36 -0400308 (void)rpc_ntop(sap, buf, sizeof(buf));
309 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
310
Chuck Lever9dc3b092009-08-09 15:09:46 -0400311 switch (sap->sa_family) {
312 case AF_INET:
313 sin = xs_addr_in(xprt);
314 (void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
315 NIPQUAD(sin->sin_addr.s_addr));
316 break;
317 case AF_INET6:
318 sin6 = xs_addr_in6(xprt);
319 (void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
320 break;
321 default:
322 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400323 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400324 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400325}
326
Chuck Lever9dc3b092009-08-09 15:09:46 -0400327static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400328{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400329 struct sockaddr *sap = xs_addr(xprt);
330 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400331
Chuck Leverc877b842009-08-09 15:09:36 -0400332 (void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
333 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400334
Chuck Leverc877b842009-08-09 15:09:36 -0400335 (void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
336 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
337}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400338
Chuck Lever9dc3b092009-08-09 15:09:46 -0400339static void xs_format_peer_addresses(struct rpc_xprt *xprt,
340 const char *protocol,
341 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400342{
Chuck Leverb454ae92008-01-07 18:34:48 -0500343 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500344 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400345 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400346 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400347}
348
Chuck Lever9dc3b092009-08-09 15:09:46 -0400349static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400350{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400351 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
352 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400353
Chuck Lever9dc3b092009-08-09 15:09:46 -0400354 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400355}
356
357static void xs_free_peer_addresses(struct rpc_xprt *xprt)
358{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500359 unsigned int i;
360
361 for (i = 0; i < RPC_DISPLAY_MAX; i++)
362 switch (i) {
363 case RPC_DISPLAY_PROTO:
364 case RPC_DISPLAY_NETID:
365 continue;
366 default:
367 kfree(xprt->address_strings[i]);
368 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400369}
370
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400371#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
372
Trond Myklebust24c56842006-10-17 15:06:22 -0400373static 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 -0400374{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400375 struct msghdr msg = {
376 .msg_name = addr,
377 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400378 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
379 };
380 struct kvec iov = {
381 .iov_base = vec->iov_base + base,
382 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400383 };
384
Trond Myklebust24c56842006-10-17 15:06:22 -0400385 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400386 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
387 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
388}
389
Trond Myklebust24c56842006-10-17 15:06:22 -0400390static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400391{
Trond Myklebust24c56842006-10-17 15:06:22 -0400392 struct page **ppage;
393 unsigned int remainder;
394 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400395
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 remainder = xdr->page_len - base;
397 base += xdr->page_base;
398 ppage = xdr->pages + (base >> PAGE_SHIFT);
399 base &= ~PAGE_MASK;
400 for(;;) {
401 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
402 int flags = XS_SENDMSG_FLAGS;
403
404 remainder -= len;
405 if (remainder != 0 || more)
406 flags |= MSG_MORE;
407 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
408 if (remainder == 0 || err != len)
409 break;
410 sent += err;
411 ppage++;
412 base = 0;
413 }
414 if (sent == 0)
415 return err;
416 if (err > 0)
417 sent += err;
418 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400419}
420
Chuck Lever9903cd12005-08-11 16:25:26 -0400421/**
422 * xs_sendpages - write pages directly to a socket
423 * @sock: socket to send on
424 * @addr: UDP only -- address of destination
425 * @addrlen: UDP only -- length of destination address
426 * @xdr: buffer containing this request
427 * @base: starting position in the buffer
428 *
Chuck Levera246b012005-08-11 16:25:23 -0400429 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400430static 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 -0400431{
Trond Myklebust24c56842006-10-17 15:06:22 -0400432 unsigned int remainder = xdr->len - base;
433 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400434
Chuck Lever262965f2005-08-11 16:25:56 -0400435 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400436 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400437
438 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400439 if (base != 0) {
440 addr = NULL;
441 addrlen = 0;
442 }
Chuck Lever262965f2005-08-11 16:25:56 -0400443
Trond Myklebust24c56842006-10-17 15:06:22 -0400444 if (base < xdr->head[0].iov_len || addr != NULL) {
445 unsigned int len = xdr->head[0].iov_len - base;
446 remainder -= len;
447 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
448 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400449 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400450 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400451 base = 0;
452 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400453 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400454
Trond Myklebust24c56842006-10-17 15:06:22 -0400455 if (base < xdr->page_len) {
456 unsigned int len = xdr->page_len - base;
457 remainder -= len;
458 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
459 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400460 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400461 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400462 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400463 } else
464 base -= xdr->page_len;
465
466 if (base >= xdr->tail[0].iov_len)
467 return sent;
468 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400469out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400470 if (sent == 0)
471 return err;
472 if (err > 0)
473 sent += err;
474 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400475}
476
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400477static void xs_nospace_callback(struct rpc_task *task)
478{
479 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
480
481 transport->inet->sk_write_pending--;
482 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
483}
484
Chuck Lever9903cd12005-08-11 16:25:26 -0400485/**
Chuck Lever262965f2005-08-11 16:25:56 -0400486 * xs_nospace - place task on wait queue if transmit was incomplete
487 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400488 *
Chuck Levera246b012005-08-11 16:25:23 -0400489 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400490static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400491{
Chuck Lever262965f2005-08-11 16:25:56 -0400492 struct rpc_rqst *req = task->tk_rqstp;
493 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500494 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400495 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400496
Chuck Lever46121cf2007-01-31 12:14:08 -0500497 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400498 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
499 req->rq_slen);
500
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400501 /* Protect against races with write_space */
502 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400503
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400504 /* Don't race with disconnect */
505 if (xprt_connected(xprt)) {
506 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400507 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400508 /*
509 * Notify TCP that we're limited by the application
510 * window size
511 */
512 set_bit(SOCK_NOSPACE, &transport->sock->flags);
513 transport->inet->sk_write_pending++;
514 /* ...and wait for more buffer space */
515 xprt_wait_for_buffer_space(task, xs_nospace_callback);
516 }
517 } else {
518 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400519 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400520 }
Chuck Lever262965f2005-08-11 16:25:56 -0400521
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400522 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400523 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400524}
525
526/**
527 * xs_udp_send_request - write an RPC request to a UDP socket
528 * @task: address of RPC task that manages the state of an RPC request
529 *
530 * Return values:
531 * 0: The request has been sent
532 * EAGAIN: The socket was blocked, please call again later to
533 * complete the request
534 * ENOTCONN: Caller needs to invoke connect logic then call again
535 * other: Some other error occured, the request was not sent
536 */
537static int xs_udp_send_request(struct rpc_task *task)
538{
539 struct rpc_rqst *req = task->tk_rqstp;
540 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500541 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400542 struct xdr_buf *xdr = &req->rq_snd_buf;
543 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400544
Chuck Lever9903cd12005-08-11 16:25:26 -0400545 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400546 req->rq_svec->iov_base,
547 req->rq_svec->iov_len);
548
Trond Myklebust01d37c42009-03-11 14:09:39 -0400549 if (!xprt_bound(xprt))
550 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500551 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400552 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500553 xprt->addrlen, xdr,
554 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400555
Chuck Lever46121cf2007-01-31 12:14:08 -0500556 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400557 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400558
Trond Myklebust21997002007-10-01 11:43:37 -0400559 if (status >= 0) {
560 task->tk_bytes_sent += status;
561 if (status >= req->rq_slen)
562 return 0;
563 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400564 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400565 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400566 if (!transport->sock)
567 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400568
Chuck Lever262965f2005-08-11 16:25:56 -0400569 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400570 case -ENOTSOCK:
571 status = -ENOTCONN;
572 /* Should we call xs_close() here? */
573 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400574 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400575 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400576 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400577 default:
578 dprintk("RPC: sendmsg returned unrecognized error %d\n",
579 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400580 case -ENETUNREACH:
581 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400582 case -ECONNREFUSED:
583 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400584 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400585 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400586 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400587out:
Chuck Lever262965f2005-08-11 16:25:56 -0400588 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400589}
590
Trond Myklebuste06799f2007-11-05 15:44:12 -0500591/**
592 * xs_tcp_shutdown - gracefully shut down a TCP socket
593 * @xprt: transport
594 *
595 * Initiates a graceful shutdown of the TCP socket by calling the
596 * equivalent of shutdown(SHUT_WR);
597 */
598static void xs_tcp_shutdown(struct rpc_xprt *xprt)
599{
600 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
601 struct socket *sock = transport->sock;
602
603 if (sock != NULL)
604 kernel_sock_shutdown(sock, SHUT_WR);
605}
606
Chuck Lever808012f2005-08-25 16:25:49 -0700607static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
608{
609 u32 reclen = buf->len - sizeof(rpc_fraghdr);
610 rpc_fraghdr *base = buf->head[0].iov_base;
611 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
612}
613
Chuck Lever9903cd12005-08-11 16:25:26 -0400614/**
Chuck Lever262965f2005-08-11 16:25:56 -0400615 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400616 * @task: address of RPC task that manages the state of an RPC request
617 *
618 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400619 * 0: The request has been sent
620 * EAGAIN: The socket was blocked, please call again later to
621 * complete the request
622 * ENOTCONN: Caller needs to invoke connect logic then call again
623 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400624 *
625 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400626 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400627 */
Chuck Lever262965f2005-08-11 16:25:56 -0400628static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400629{
630 struct rpc_rqst *req = task->tk_rqstp;
631 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500632 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400633 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400634 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400635
Chuck Lever808012f2005-08-25 16:25:49 -0700636 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400637
Chuck Lever262965f2005-08-11 16:25:56 -0400638 xs_pktdump("packet data:",
639 req->rq_svec->iov_base,
640 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400641
642 /* Continue transmitting the packet/record. We must be careful
643 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400644 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400645 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500646 status = xs_sendpages(transport->sock,
647 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400648
Chuck Lever46121cf2007-01-31 12:14:08 -0500649 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400650 xdr->len - req->rq_bytes_sent, status);
651
652 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400653 break;
654
Chuck Lever262965f2005-08-11 16:25:56 -0400655 /* If we've sent the entire packet, immediately
656 * reset the count of bytes sent. */
657 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500658 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400659 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
660 req->rq_bytes_sent = 0;
661 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400662 }
663
Trond Myklebust06b4b682008-04-16 16:51:38 -0400664 if (status != 0)
665 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400666 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400667 break;
Chuck Levera246b012005-08-11 16:25:23 -0400668 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400669 if (!transport->sock)
670 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400671
Chuck Lever262965f2005-08-11 16:25:56 -0400672 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400673 case -ENOTSOCK:
674 status = -ENOTCONN;
675 /* Should we call xs_close() here? */
676 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400677 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400678 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400679 break;
680 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500681 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400682 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400683 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400684 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500685 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400686 case -ECONNREFUSED:
687 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400688 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400689 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400690out:
Chuck Levera246b012005-08-11 16:25:23 -0400691 return status;
692}
693
Chuck Lever9903cd12005-08-11 16:25:26 -0400694/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400695 * xs_tcp_release_xprt - clean up after a tcp transmission
696 * @xprt: transport
697 * @task: rpc task
698 *
699 * This cleans up if an error causes us to abort the transmission of a request.
700 * In this case, the socket may need to be reset in order to avoid confusing
701 * the server.
702 */
703static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
704{
705 struct rpc_rqst *req;
706
707 if (task != xprt->snd_task)
708 return;
709 if (task == NULL)
710 goto out_release;
711 req = task->tk_rqstp;
712 if (req->rq_bytes_sent == 0)
713 goto out_release;
714 if (req->rq_bytes_sent == req->rq_snd_buf.len)
715 goto out_release;
716 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
717out_release:
718 xprt_release_xprt(xprt, task);
719}
720
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400721static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
722{
723 transport->old_data_ready = sk->sk_data_ready;
724 transport->old_state_change = sk->sk_state_change;
725 transport->old_write_space = sk->sk_write_space;
726 transport->old_error_report = sk->sk_error_report;
727}
728
729static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
730{
731 sk->sk_data_ready = transport->old_data_ready;
732 sk->sk_state_change = transport->old_state_change;
733 sk->sk_write_space = transport->old_write_space;
734 sk->sk_error_report = transport->old_error_report;
735}
736
Chuck Leverfe315e72009-03-11 14:10:21 -0400737static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400738{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500739 struct socket *sock = transport->sock;
740 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400741
Chuck Leverfe315e72009-03-11 14:10:21 -0400742 if (sk == NULL)
743 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400744
Chuck Levera246b012005-08-11 16:25:23 -0400745 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500746 transport->inet = NULL;
747 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400748
Chuck Lever9903cd12005-08-11 16:25:26 -0400749 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400750
751 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400752 write_unlock_bh(&sk->sk_callback_lock);
753
Chuck Lever9903cd12005-08-11 16:25:26 -0400754 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400755
756 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400757}
758
759/**
760 * xs_close - close a socket
761 * @xprt: transport
762 *
763 * This is used when all requests are complete; ie, no DRC state remains
764 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400765 *
766 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
767 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400768 */
769static void xs_close(struct rpc_xprt *xprt)
770{
771 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
772
773 dprintk("RPC: xs_close xprt %p\n", xprt);
774
775 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400776 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400777
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100778 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400779 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100780 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500781 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100782 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500783 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400784}
785
Trond Myklebustf75e6742009-04-21 17:18:20 -0400786static void xs_tcp_close(struct rpc_xprt *xprt)
787{
788 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
789 xs_close(xprt);
790 else
791 xs_tcp_shutdown(xprt);
792}
793
Chuck Lever9903cd12005-08-11 16:25:26 -0400794/**
795 * xs_destroy - prepare to shutdown a transport
796 * @xprt: doomed transport
797 *
798 */
799static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400800{
Chuck Leverc8475462006-12-05 16:35:26 -0500801 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
802
Chuck Lever46121cf2007-01-31 12:14:08 -0500803 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400804
Trond Myklebustc1384c92007-06-14 18:00:42 -0400805 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400806
Chuck Lever9903cd12005-08-11 16:25:26 -0400807 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400808 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400809 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400810 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400811 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400812}
813
Chuck Lever9903cd12005-08-11 16:25:26 -0400814static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400815{
Chuck Lever9903cd12005-08-11 16:25:26 -0400816 return (struct rpc_xprt *) sk->sk_user_data;
817}
818
819/**
820 * xs_udp_data_ready - "data ready" callback for UDP sockets
821 * @sk: socket with data to read
822 * @len: how much data to read
823 *
824 */
825static void xs_udp_data_ready(struct sock *sk, int len)
826{
827 struct rpc_task *task;
828 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400829 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400830 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400831 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700832 u32 _xid;
833 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400834
835 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500836 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400837 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400838 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400839
840 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
841 goto out;
842
843 if (xprt->shutdown)
844 goto dropit;
845
846 repsize = skb->len - sizeof(struct udphdr);
847 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500848 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400849 goto dropit;
850 }
851
852 /* Copy the XID from the skb... */
853 xp = skb_header_pointer(skb, sizeof(struct udphdr),
854 sizeof(_xid), &_xid);
855 if (xp == NULL)
856 goto dropit;
857
858 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400859 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400860 rovr = xprt_lookup_rqst(xprt, *xp);
861 if (!rovr)
862 goto out_unlock;
863 task = rovr->rq_task;
864
Chuck Levera246b012005-08-11 16:25:23 -0400865 if ((copied = rovr->rq_private_buf.buflen) > repsize)
866 copied = repsize;
867
868 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800869 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
870 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400871 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800872 }
873
874 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400875
876 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000877 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400878
Chuck Lever1570c1e2005-08-25 16:25:52 -0700879 xprt_adjust_cwnd(task, copied);
880 xprt_update_rtt(task);
881 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400882
883 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400884 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400885 dropit:
886 skb_free_datagram(sk, skb);
887 out:
888 read_unlock(&sk->sk_callback_lock);
889}
890
Chuck Leverdd456472006-12-05 16:35:44 -0500891static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400892{
Chuck Lever51971132006-12-05 16:35:19 -0500893 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400894 size_t len, used;
895 char *p;
896
Chuck Lever51971132006-12-05 16:35:19 -0500897 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
898 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500899 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500900 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400901 if (used != len)
902 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700903
Chuck Lever51971132006-12-05 16:35:19 -0500904 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
905 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500906 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400907 else
Chuck Levere136d092006-12-05 16:35:23 -0500908 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500909 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700910
Chuck Levere136d092006-12-05 16:35:23 -0500911 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500912 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700913
Chuck Levera246b012005-08-11 16:25:23 -0400914 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400915 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500916 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500917 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400918 return;
Chuck Levera246b012005-08-11 16:25:23 -0400919 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500920 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500921 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400922}
923
Chuck Lever51971132006-12-05 16:35:19 -0500924static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400925{
Chuck Lever51971132006-12-05 16:35:19 -0500926 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500927 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500928 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500929 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
930 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
931 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500932 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400933 }
934 }
935}
936
Chuck Leverdd456472006-12-05 16:35:44 -0500937static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400938{
939 size_t len, used;
940 char *p;
941
Chuck Lever51971132006-12-05 16:35:19 -0500942 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500943 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500944 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500945 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500946 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400947 if (used != len)
948 return;
Chuck Levere136d092006-12-05 16:35:23 -0500949 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400950 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500951 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400952 dprintk("RPC: reading %s XID %08x\n",
953 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
954 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500955 ntohl(transport->tcp_xid));
956 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400957}
958
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400959static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
960 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400961{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400962 size_t len, used;
963 u32 offset;
964 __be32 calldir;
965
966 /*
967 * We want transport->tcp_offset to be 8 at the end of this routine
968 * (4 bytes for the xid and 4 bytes for the call/reply flag).
969 * When this function is called for the first time,
970 * transport->tcp_offset is 4 (after having already read the xid).
971 */
972 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
973 len = sizeof(calldir) - offset;
974 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
975 used = xdr_skb_read_bits(desc, &calldir, len);
976 transport->tcp_offset += used;
977 if (used != len)
978 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400979 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
980 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400981 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400982 /*
983 * We don't yet have the XDR buffer, so we will write the calldir
984 * out after we get the buffer from the 'struct rpc_rqst'
985 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400986 if (ntohl(calldir) == RPC_REPLY)
987 transport->tcp_flags |= TCP_RPC_REPLY;
988 else
989 transport->tcp_flags &= ~TCP_RPC_REPLY;
990 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
991 (transport->tcp_flags & TCP_RPC_REPLY) ?
992 "reply for" : "request with", calldir);
993 xs_tcp_check_fraghdr(transport);
994}
995
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400996static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
997 struct xdr_skb_reader *desc,
998 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400999{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001000 struct sock_xprt *transport =
1001 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001002 struct xdr_buf *rcvbuf;
1003 size_t len;
1004 ssize_t r;
1005
Chuck Levera246b012005-08-11 16:25:23 -04001006 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001007
1008 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1009 /*
1010 * Save the RPC direction in the XDR buffer
1011 */
1012 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1013 htonl(RPC_REPLY) : 0;
1014
1015 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1016 &calldir, sizeof(calldir));
1017 transport->tcp_copied += sizeof(calldir);
1018 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001019 }
1020
Chuck Levera246b012005-08-11 16:25:23 -04001021 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001022 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001023 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001024
Chuck Lever51971132006-12-05 16:35:19 -05001025 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001026 memcpy(&my_desc, desc, sizeof(my_desc));
1027 my_desc.count = len;
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 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001030 desc->count -= r;
1031 desc->offset += r;
1032 } else
Chuck Lever51971132006-12-05 16:35:19 -05001033 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001034 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001035
1036 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001037 transport->tcp_copied += r;
1038 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001039 }
1040 if (r != len) {
1041 /* Error when copying to the receive buffer,
1042 * usually because we weren't able to allocate
1043 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001044 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001045 * will not receive any additional updates,
1046 * and time out.
1047 * Any remaining data from this record will
1048 * be discarded.
1049 */
Chuck Levere136d092006-12-05 16:35:23 -05001050 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001051 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001052 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001053 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1054 "tcp_offset = %u, tcp_reclen = %u\n",
1055 xprt, transport->tcp_copied,
1056 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001057 return;
Chuck Levera246b012005-08-11 16:25:23 -04001058 }
1059
Chuck Lever46121cf2007-01-31 12:14:08 -05001060 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001061 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001062 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1063 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1064 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001065
Chuck Lever51971132006-12-05 16:35:19 -05001066 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001067 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001068 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001069 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1070 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001071 }
1072
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001073 return;
1074}
1075
1076/*
1077 * Finds the request corresponding to the RPC xid and invokes the common
1078 * tcp read code to read the data.
1079 */
1080static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1081 struct xdr_skb_reader *desc)
1082{
1083 struct sock_xprt *transport =
1084 container_of(xprt, struct sock_xprt, xprt);
1085 struct rpc_rqst *req;
1086
1087 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1088
1089 /* Find and lock the request corresponding to this xid */
1090 spin_lock(&xprt->transport_lock);
1091 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1092 if (!req) {
1093 dprintk("RPC: XID %08x request not found!\n",
1094 ntohl(transport->tcp_xid));
1095 spin_unlock(&xprt->transport_lock);
1096 return -1;
1097 }
1098
1099 xs_tcp_read_common(xprt, desc, req);
1100
Chuck Levere136d092006-12-05 16:35:23 -05001101 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001102 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001103
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001104 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001105 return 0;
1106}
1107
1108#if defined(CONFIG_NFS_V4_1)
1109/*
1110 * Obtains an rpc_rqst previously allocated and invokes the common
1111 * tcp read code to read the data. The result is placed in the callback
1112 * queue.
1113 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1114 * connection and return -1.
1115 */
1116static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1117 struct xdr_skb_reader *desc)
1118{
1119 struct sock_xprt *transport =
1120 container_of(xprt, struct sock_xprt, xprt);
1121 struct rpc_rqst *req;
1122
1123 req = xprt_alloc_bc_request(xprt);
1124 if (req == NULL) {
1125 printk(KERN_WARNING "Callback slot table overflowed\n");
1126 xprt_force_disconnect(xprt);
1127 return -1;
1128 }
1129
1130 req->rq_xid = transport->tcp_xid;
1131 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1132 xs_tcp_read_common(xprt, desc, req);
1133
1134 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1135 struct svc_serv *bc_serv = xprt->bc_serv;
1136
1137 /*
1138 * Add callback request to callback list. The callback
1139 * service sleeps on the sv_cb_waitq waiting for new
1140 * requests. Wake it up after adding enqueing the
1141 * request.
1142 */
1143 dprintk("RPC: add callback request to list\n");
1144 spin_lock(&bc_serv->sv_cb_lock);
1145 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1146 spin_unlock(&bc_serv->sv_cb_lock);
1147 wake_up(&bc_serv->sv_cb_waitq);
1148 }
1149
1150 req->rq_private_buf.len = transport->tcp_copied;
1151
1152 return 0;
1153}
1154
1155static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1156 struct xdr_skb_reader *desc)
1157{
1158 struct sock_xprt *transport =
1159 container_of(xprt, struct sock_xprt, xprt);
1160
1161 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1162 xs_tcp_read_reply(xprt, desc) :
1163 xs_tcp_read_callback(xprt, desc);
1164}
1165#else
1166static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1167 struct xdr_skb_reader *desc)
1168{
1169 return xs_tcp_read_reply(xprt, desc);
1170}
1171#endif /* CONFIG_NFS_V4_1 */
1172
1173/*
1174 * Read data off the transport. This can be either an RPC_CALL or an
1175 * RPC_REPLY. Relay the processing to helper functions.
1176 */
1177static void xs_tcp_read_data(struct rpc_xprt *xprt,
1178 struct xdr_skb_reader *desc)
1179{
1180 struct sock_xprt *transport =
1181 container_of(xprt, struct sock_xprt, xprt);
1182
1183 if (_xs_tcp_read_data(xprt, desc) == 0)
1184 xs_tcp_check_fraghdr(transport);
1185 else {
1186 /*
1187 * The transport_lock protects the request handling.
1188 * There's no need to hold it to update the tcp_flags.
1189 */
1190 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1191 }
Chuck Levera246b012005-08-11 16:25:23 -04001192}
1193
Chuck Leverdd456472006-12-05 16:35:44 -05001194static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001195{
1196 size_t len;
1197
Chuck Lever51971132006-12-05 16:35:19 -05001198 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001199 if (len > desc->count)
1200 len = desc->count;
1201 desc->count -= len;
1202 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001203 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001204 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001205 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001206}
1207
Chuck Lever9903cd12005-08-11 16:25:26 -04001208static 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 -04001209{
1210 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001211 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001212 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001213 .skb = skb,
1214 .offset = offset,
1215 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001216 };
Chuck Levera246b012005-08-11 16:25:23 -04001217
Chuck Lever46121cf2007-01-31 12:14:08 -05001218 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001219 do {
1220 /* Read in a new fragment marker if necessary */
1221 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001222 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001223 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001224 continue;
1225 }
1226 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001227 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001228 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001229 continue;
1230 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001231 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001232 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001233 xs_tcp_read_calldir(transport, &desc);
1234 continue;
1235 }
Chuck Levera246b012005-08-11 16:25:23 -04001236 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001237 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001238 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001239 continue;
1240 }
1241 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001242 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001243 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001244 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001245 return len - desc.count;
1246}
1247
Chuck Lever9903cd12005-08-11 16:25:26 -04001248/**
1249 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1250 * @sk: socket with data to read
1251 * @bytes: how much data to read
1252 *
1253 */
1254static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001255{
1256 struct rpc_xprt *xprt;
1257 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001258 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001259
Chuck Lever46121cf2007-01-31 12:14:08 -05001260 dprintk("RPC: xs_tcp_data_ready...\n");
1261
Chuck Levera246b012005-08-11 16:25:23 -04001262 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001263 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001264 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001265 if (xprt->shutdown)
1266 goto out;
1267
Neil Brown61d0a8e2009-09-23 14:36:37 -04001268 /* Any data means we had a useful conversation, so
1269 * the we don't need to delay the next reconnect
1270 */
1271 if (xprt->reestablish_timeout)
1272 xprt->reestablish_timeout = 0;
1273
Chuck Lever9903cd12005-08-11 16:25:26 -04001274 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001275 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001276 do {
1277 rd_desc.count = 65536;
1278 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1279 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001280out:
1281 read_unlock(&sk->sk_callback_lock);
1282}
1283
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001284/*
1285 * Do the equivalent of linger/linger2 handling for dealing with
1286 * broken servers that don't close the socket in a timely
1287 * fashion
1288 */
1289static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1290 unsigned long timeout)
1291{
1292 struct sock_xprt *transport;
1293
1294 if (xprt_test_and_set_connecting(xprt))
1295 return;
1296 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1297 transport = container_of(xprt, struct sock_xprt, xprt);
1298 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1299 timeout);
1300}
1301
1302static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1303{
1304 struct sock_xprt *transport;
1305
1306 transport = container_of(xprt, struct sock_xprt, xprt);
1307
1308 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1309 !cancel_delayed_work(&transport->connect_worker))
1310 return;
1311 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1312 xprt_clear_connecting(xprt);
1313}
1314
1315static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1316{
1317 smp_mb__before_clear_bit();
1318 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1319 clear_bit(XPRT_CLOSING, &xprt->state);
1320 smp_mb__after_clear_bit();
1321 /* Mark transport as closed and wake up all pending tasks */
1322 xprt_disconnect_done(xprt);
1323}
1324
Chuck Lever9903cd12005-08-11 16:25:26 -04001325/**
1326 * xs_tcp_state_change - callback to handle TCP socket state changes
1327 * @sk: socket whose state has changed
1328 *
1329 */
1330static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001331{
Chuck Lever9903cd12005-08-11 16:25:26 -04001332 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001333
1334 read_lock(&sk->sk_callback_lock);
1335 if (!(xprt = xprt_from_sock(sk)))
1336 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001337 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1338 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1339 sk->sk_state, xprt_connected(xprt),
1340 sock_flag(sk, SOCK_DEAD),
1341 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001342
1343 switch (sk->sk_state) {
1344 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001345 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001346 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001347 struct sock_xprt *transport = container_of(xprt,
1348 struct sock_xprt, xprt);
1349
Chuck Levera246b012005-08-11 16:25:23 -04001350 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001351 transport->tcp_offset = 0;
1352 transport->tcp_reclen = 0;
1353 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001354 transport->tcp_flags =
1355 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001356
Trond Myklebust2a491992009-03-11 14:38:00 -04001357 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001358 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001359 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001360 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001361 case TCP_FIN_WAIT1:
1362 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001363 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001364 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001365 set_bit(XPRT_CLOSING, &xprt->state);
1366 smp_mb__before_clear_bit();
1367 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001368 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001369 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001370 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001371 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001372 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001373 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001374 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001375 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001376 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001377 case TCP_CLOSING:
1378 /*
1379 * If the server closed down the connection, make sure that
1380 * we back off before reconnecting
1381 */
1382 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1383 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001384 break;
1385 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001386 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001387 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001388 smp_mb__before_clear_bit();
1389 clear_bit(XPRT_CONNECTED, &xprt->state);
1390 smp_mb__after_clear_bit();
1391 break;
1392 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001393 xs_tcp_cancel_linger_timeout(xprt);
1394 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001395 }
1396 out:
1397 read_unlock(&sk->sk_callback_lock);
1398}
1399
Chuck Lever9903cd12005-08-11 16:25:26 -04001400/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001401 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001402 * @sk: socket
1403 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001404static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001405{
1406 struct rpc_xprt *xprt;
1407
1408 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001409 if (!(xprt = xprt_from_sock(sk)))
1410 goto out;
1411 dprintk("RPC: %s client %p...\n"
1412 "RPC: error %d\n",
1413 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001414 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001415out:
1416 read_unlock(&sk->sk_callback_lock);
1417}
1418
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001419static void xs_write_space(struct sock *sk)
1420{
1421 struct socket *sock;
1422 struct rpc_xprt *xprt;
1423
1424 if (unlikely(!(sock = sk->sk_socket)))
1425 return;
1426 clear_bit(SOCK_NOSPACE, &sock->flags);
1427
1428 if (unlikely(!(xprt = xprt_from_sock(sk))))
1429 return;
1430 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1431 return;
1432
1433 xprt_write_space(xprt);
1434}
1435
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001436/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001437 * xs_udp_write_space - callback invoked when socket buffer space
1438 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001439 * @sk: socket whose state has changed
1440 *
Chuck Levera246b012005-08-11 16:25:23 -04001441 * Called when more output buffer space is available for this socket.
1442 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001443 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001444 * with a bunch of small requests.
1445 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001446static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001447{
Chuck Levera246b012005-08-11 16:25:23 -04001448 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001449
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001450 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001451 if (sock_writeable(sk))
1452 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001453
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001454 read_unlock(&sk->sk_callback_lock);
1455}
Chuck Levera246b012005-08-11 16:25:23 -04001456
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001457/**
1458 * xs_tcp_write_space - callback invoked when socket buffer space
1459 * becomes available
1460 * @sk: socket whose state has changed
1461 *
1462 * Called when more output buffer space is available for this socket.
1463 * We try not to wake our writers until they can make "significant"
1464 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1465 * with a bunch of small requests.
1466 */
1467static void xs_tcp_write_space(struct sock *sk)
1468{
1469 read_lock(&sk->sk_callback_lock);
1470
1471 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001472 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1473 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001474
Chuck Levera246b012005-08-11 16:25:23 -04001475 read_unlock(&sk->sk_callback_lock);
1476}
1477
Chuck Lever470056c2005-08-25 16:25:56 -07001478static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001479{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001480 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1481 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001482
Chuck Lever7c6e0662006-12-05 16:35:30 -05001483 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001484 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001485 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001486 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001487 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001488 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001489 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001490 sk->sk_write_space(sk);
1491 }
1492}
1493
Chuck Lever43118c22005-08-25 16:25:49 -07001494/**
Chuck Lever470056c2005-08-25 16:25:56 -07001495 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001496 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001497 * @sndsize: requested size of send buffer, in bytes
1498 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001499 *
Chuck Lever470056c2005-08-25 16:25:56 -07001500 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001501 */
Chuck Lever470056c2005-08-25 16:25:56 -07001502static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001503{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001504 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1505
1506 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001507 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001508 transport->sndsize = sndsize + 1024;
1509 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001510 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001511 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001512
1513 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001514}
1515
Chuck Lever46c0ee82005-08-25 16:25:52 -07001516/**
1517 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1518 * @task: task that timed out
1519 *
1520 * Adjust the congestion window after a retransmit timeout has occurred.
1521 */
1522static void xs_udp_timer(struct rpc_task *task)
1523{
1524 xprt_adjust_cwnd(task, -ETIMEDOUT);
1525}
1526
Chuck Leverb85d8802006-05-25 01:40:49 -04001527static unsigned short xs_get_random_port(void)
1528{
1529 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1530 unsigned short rand = (unsigned short) net_random() % range;
1531 return rand + xprt_min_resvport;
1532}
1533
Chuck Lever92200412006-01-03 09:55:51 +01001534/**
1535 * xs_set_port - reset the port number in the remote endpoint address
1536 * @xprt: generic transport
1537 * @port: new port number
1538 *
1539 */
1540static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1541{
Chuck Lever46121cf2007-01-31 12:14:08 -05001542 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001543
Chuck Lever9dc3b092009-08-09 15:09:46 -04001544 rpc_set_port(xs_addr(xprt), port);
1545 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001546}
1547
Trond Myklebust67a391d2007-11-05 17:40:58 -05001548static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1549{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001550 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001551
1552 if (port == 0 && transport->xprt.resvport)
1553 port = xs_get_random_port();
1554 return port;
1555}
1556
1557static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1558{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001559 if (transport->srcport != 0)
1560 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001561 if (!transport->xprt.resvport)
1562 return 0;
1563 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1564 return xprt_max_resvport;
1565 return --port;
1566}
1567
Chuck Lever7dc753f2007-08-06 11:57:28 -04001568static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001569{
1570 struct sockaddr_in myaddr = {
1571 .sin_family = AF_INET,
1572 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001573 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001574 int err, nloop = 0;
1575 unsigned short port = xs_get_srcport(transport, sock);
1576 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001577
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001578 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001579 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001580 do {
1581 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001582 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001583 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001584 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001585 break;
Chuck Levera246b012005-08-11 16:25:23 -04001586 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001587 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001588 break;
Chuck Levera246b012005-08-11 16:25:23 -04001589 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001590 last = port;
1591 port = xs_next_srcport(transport, sock, port);
1592 if (port > last)
1593 nloop++;
1594 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001595 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1596 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001597 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001598 return err;
1599}
1600
Chuck Lever90058d32007-08-06 11:57:33 -04001601static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1602{
1603 struct sockaddr_in6 myaddr = {
1604 .sin6_family = AF_INET6,
1605 };
1606 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001607 int err, nloop = 0;
1608 unsigned short port = xs_get_srcport(transport, sock);
1609 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001610
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001611 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001612 myaddr.sin6_addr = sa->sin6_addr;
1613 do {
1614 myaddr.sin6_port = htons(port);
1615 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1616 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001617 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001618 break;
1619 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001620 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001621 break;
1622 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001623 last = port;
1624 port = xs_next_srcport(transport, sock, port);
1625 if (port > last)
1626 nloop++;
1627 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001628 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001629 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001630 return err;
1631}
1632
Peter Zijlstraed075362006-12-06 20:35:24 -08001633#ifdef CONFIG_DEBUG_LOCK_ALLOC
1634static struct lock_class_key xs_key[2];
1635static struct lock_class_key xs_slock_key[2];
1636
Chuck Lever8945ee52007-08-06 11:58:04 -04001637static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001638{
1639 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001640
John Heffner02b3d342007-09-12 10:42:12 +02001641 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001642 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1643 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1644}
Peter Zijlstraed075362006-12-06 20:35:24 -08001645
Chuck Lever8945ee52007-08-06 11:58:04 -04001646static inline void xs_reclassify_socket6(struct socket *sock)
1647{
1648 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001649
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001650 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001651 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1652 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001653}
1654#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001655static inline void xs_reclassify_socket4(struct socket *sock)
1656{
1657}
1658
1659static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001660{
1661}
1662#endif
1663
Chuck Lever16be2d22007-08-06 11:57:38 -04001664static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001665{
Chuck Lever16be2d22007-08-06 11:57:38 -04001666 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001667
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001668 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001669 struct sock *sk = sock->sk;
1670
1671 write_lock_bh(&sk->sk_callback_lock);
1672
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001673 xs_save_old_callbacks(transport, sk);
1674
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001675 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001676 sk->sk_data_ready = xs_udp_data_ready;
1677 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001678 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001679 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001680 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001681
1682 xprt_set_connected(xprt);
1683
1684 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001685 transport->sock = sock;
1686 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001687
1688 write_unlock_bh(&sk->sk_callback_lock);
1689 }
Chuck Lever470056c2005-08-25 16:25:56 -07001690 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001691}
1692
Chuck Levera246b012005-08-11 16:25:23 -04001693/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001694 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001695 * @work: RPC transport to connect
1696 *
1697 * Invoked by a work queue tasklet.
1698 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001699static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001700{
1701 struct sock_xprt *transport =
1702 container_of(work, struct sock_xprt, connect_worker.work);
1703 struct rpc_xprt *xprt = &transport->xprt;
1704 struct socket *sock = transport->sock;
1705 int err, status = -EIO;
1706
Trond Myklebust01d37c42009-03-11 14:09:39 -04001707 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001708 goto out;
1709
Chuck Levera246b012005-08-11 16:25:23 -04001710 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001711 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001712
Chuck Leverfe315e72009-03-11 14:10:21 -04001713 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1714 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001715 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001716 goto out;
1717 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001718 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001719
Chuck Lever7dc753f2007-08-06 11:57:28 -04001720 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001721 sock_release(sock);
1722 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001723 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001724
Chuck Leverc740eff2009-08-09 15:09:46 -04001725 dprintk("RPC: worker connecting xprt %p via %s to "
1726 "%s (port %s)\n", xprt,
1727 xprt->address_strings[RPC_DISPLAY_PROTO],
1728 xprt->address_strings[RPC_DISPLAY_ADDR],
1729 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001730
Chuck Lever16be2d22007-08-06 11:57:38 -04001731 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001732 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001733out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001734 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001735 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001736}
1737
Chuck Lever68e220b2007-08-06 11:57:48 -04001738/**
1739 * xs_udp_connect_worker6 - set up a UDP socket
1740 * @work: RPC transport to connect
1741 *
1742 * Invoked by a work queue tasklet.
1743 */
1744static void xs_udp_connect_worker6(struct work_struct *work)
1745{
1746 struct sock_xprt *transport =
1747 container_of(work, struct sock_xprt, connect_worker.work);
1748 struct rpc_xprt *xprt = &transport->xprt;
1749 struct socket *sock = transport->sock;
1750 int err, status = -EIO;
1751
Trond Myklebust01d37c42009-03-11 14:09:39 -04001752 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001753 goto out;
1754
1755 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001756 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001757
Chuck Leverfe315e72009-03-11 14:10:21 -04001758 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1759 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001760 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1761 goto out;
1762 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001763 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001764
1765 if (xs_bind6(transport, sock) < 0) {
1766 sock_release(sock);
1767 goto out;
1768 }
1769
Chuck Leverc740eff2009-08-09 15:09:46 -04001770 dprintk("RPC: worker connecting xprt %p via %s to "
1771 "%s (port %s)\n", xprt,
1772 xprt->address_strings[RPC_DISPLAY_PROTO],
1773 xprt->address_strings[RPC_DISPLAY_ADDR],
1774 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001775
1776 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001777 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001778out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001779 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001780 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001781}
1782
Chuck Lever3167e122005-08-25 16:25:55 -07001783/*
1784 * We need to preserve the port number so the reply cache on the server can
1785 * find our cached RPC replies when we get around to reconnecting.
1786 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001787static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001788{
1789 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001790 struct sockaddr any;
1791
Chuck Lever46121cf2007-01-31 12:14:08 -05001792 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001793
1794 /*
1795 * Disconnect the transport socket by doing a connect operation
1796 * with AF_UNSPEC. This should return immediately...
1797 */
1798 memset(&any, 0, sizeof(any));
1799 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001800 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001801 if (!result)
1802 xs_sock_mark_closed(xprt);
1803 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001804 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001805 result);
1806}
1807
Trond Myklebust40d25492009-03-11 14:37:58 -04001808static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1809{
1810 unsigned int state = transport->inet->sk_state;
1811
1812 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1813 return;
1814 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1815 return;
1816 xs_abort_connection(xprt, transport);
1817}
1818
Chuck Lever16be2d22007-08-06 11:57:38 -04001819static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001820{
Chuck Lever16be2d22007-08-06 11:57:38 -04001821 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001822
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001823 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001824 struct sock *sk = sock->sk;
1825
1826 write_lock_bh(&sk->sk_callback_lock);
1827
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001828 xs_save_old_callbacks(transport, sk);
1829
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001830 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001831 sk->sk_data_ready = xs_tcp_data_ready;
1832 sk->sk_state_change = xs_tcp_state_change;
1833 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001834 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001835 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001836
1837 /* socket options */
1838 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1839 sock_reset_flag(sk, SOCK_LINGER);
1840 tcp_sk(sk)->linger2 = 0;
1841 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001842
1843 xprt_clear_connected(xprt);
1844
1845 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001846 transport->sock = sock;
1847 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001848
1849 write_unlock_bh(&sk->sk_callback_lock);
1850 }
1851
Trond Myklebust01d37c42009-03-11 14:09:39 -04001852 if (!xprt_bound(xprt))
1853 return -ENOTCONN;
1854
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001855 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001856 xprt->stat.connect_count++;
1857 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001858 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001859}
1860
1861/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001862 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1863 * @xprt: RPC transport to connect
1864 * @transport: socket transport to connect
1865 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001866 *
1867 * Invoked by a work queue tasklet.
1868 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001869static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1870 struct sock_xprt *transport,
1871 struct socket *(*create_sock)(struct rpc_xprt *,
1872 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001873{
Chuck Lever16be2d22007-08-06 11:57:38 -04001874 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001875 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001876
Trond Myklebust01d37c42009-03-11 14:09:39 -04001877 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001878 goto out;
1879
1880 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001881 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001882 sock = create_sock(xprt, transport);
1883 if (IS_ERR(sock)) {
1884 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001885 goto out;
1886 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001887 } else {
1888 int abort_and_exit;
1889
1890 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1891 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001892 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001893 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001894
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001895 if (abort_and_exit)
1896 goto out_eagain;
1897 }
1898
Chuck Leverc740eff2009-08-09 15:09:46 -04001899 dprintk("RPC: worker connecting xprt %p via %s to "
1900 "%s (port %s)\n", xprt,
1901 xprt->address_strings[RPC_DISPLAY_PROTO],
1902 xprt->address_strings[RPC_DISPLAY_ADDR],
1903 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001904
1905 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001906 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1907 xprt, -status, xprt_connected(xprt),
1908 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001909 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001910 default:
1911 printk("%s: connect returned unhandled error %d\n",
1912 __func__, status);
1913 case -EADDRNOTAVAIL:
1914 /* We're probably in TIME_WAIT. Get rid of existing socket,
1915 * and retry
1916 */
1917 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1918 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001919 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001920 case -ECONNREFUSED:
1921 case -ECONNRESET:
1922 case -ENETUNREACH:
1923 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001924 case 0:
1925 case -EINPROGRESS:
1926 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001927 xprt_clear_connecting(xprt);
1928 return;
Chuck Levera246b012005-08-11 16:25:23 -04001929 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001930out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001931 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001932out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001933 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001934 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001935}
1936
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001937static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1938 struct sock_xprt *transport)
1939{
1940 struct socket *sock;
1941 int err;
1942
1943 /* start from scratch */
1944 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1945 if (err < 0) {
1946 dprintk("RPC: can't create TCP transport socket (%d).\n",
1947 -err);
1948 goto out_err;
1949 }
1950 xs_reclassify_socket4(sock);
1951
1952 if (xs_bind4(transport, sock) < 0) {
1953 sock_release(sock);
1954 goto out_err;
1955 }
1956 return sock;
1957out_err:
1958 return ERR_PTR(-EIO);
1959}
1960
1961/**
Chuck Levera246b012005-08-11 16:25:23 -04001962 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1963 * @work: RPC transport to connect
1964 *
1965 * Invoked by a work queue tasklet.
1966 */
1967static void xs_tcp_connect_worker4(struct work_struct *work)
1968{
1969 struct sock_xprt *transport =
1970 container_of(work, struct sock_xprt, connect_worker.work);
1971 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001972
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001973 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1974}
Chuck Levera246b012005-08-11 16:25:23 -04001975
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001976static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1977 struct sock_xprt *transport)
1978{
1979 struct socket *sock;
1980 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001981
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001982 /* start from scratch */
1983 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1984 if (err < 0) {
1985 dprintk("RPC: can't create TCP transport socket (%d).\n",
1986 -err);
1987 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001988 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001989 xs_reclassify_socket6(sock);
1990
1991 if (xs_bind6(transport, sock) < 0) {
1992 sock_release(sock);
1993 goto out_err;
1994 }
1995 return sock;
1996out_err:
1997 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001998}
1999
Chuck Lever9903cd12005-08-11 16:25:26 -04002000/**
Chuck Lever68e220b2007-08-06 11:57:48 -04002001 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2002 * @work: RPC transport to connect
2003 *
2004 * Invoked by a work queue tasklet.
2005 */
2006static void xs_tcp_connect_worker6(struct work_struct *work)
2007{
2008 struct sock_xprt *transport =
2009 container_of(work, struct sock_xprt, connect_worker.work);
2010 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04002011
Trond Myklebustb61d59f2009-03-11 14:38:04 -04002012 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002013}
2014
2015/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002016 * xs_connect - connect a socket to a remote endpoint
2017 * @task: address of RPC task that manages state of connect request
2018 *
2019 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002020 *
2021 * UDP socket connects are synchronous, but we use a work queue anyway
2022 * to guarantee that even unprivileged user processes can set up a
2023 * socket on a privileged port.
2024 *
2025 * If a UDP socket connect fails, the delay behavior here prevents
2026 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002027 */
2028static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002029{
2030 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002031 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002032
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002033 if (xprt_test_and_set_connecting(xprt))
2034 return;
2035
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002036 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002037 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2038 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002039 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002040 queue_delayed_work(rpciod_workqueue,
2041 &transport->connect_worker,
2042 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002043 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002044 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2045 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002046 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2047 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002048 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002049 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002050 queue_delayed_work(rpciod_workqueue,
2051 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002052 }
2053}
2054
Trond Myklebuste06799f2007-11-05 15:44:12 -05002055static void xs_tcp_connect(struct rpc_task *task)
2056{
2057 struct rpc_xprt *xprt = task->tk_xprt;
2058
Trond Myklebuste06799f2007-11-05 15:44:12 -05002059 /* Exit if we need to wait for socket shutdown to complete */
2060 if (test_bit(XPRT_CLOSING, &xprt->state))
2061 return;
2062 xs_connect(task);
2063}
2064
Chuck Lever262ca072006-03-20 13:44:16 -05002065/**
2066 * xs_udp_print_stats - display UDP socket-specifc stats
2067 * @xprt: rpc_xprt struct containing statistics
2068 * @seq: output file
2069 *
2070 */
2071static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2072{
Chuck Leverc8475462006-12-05 16:35:26 -05002073 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2074
Chuck Lever262ca072006-03-20 13:44:16 -05002075 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002076 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002077 xprt->stat.bind_count,
2078 xprt->stat.sends,
2079 xprt->stat.recvs,
2080 xprt->stat.bad_xids,
2081 xprt->stat.req_u,
2082 xprt->stat.bklog_u);
2083}
2084
2085/**
2086 * xs_tcp_print_stats - display TCP socket-specifc stats
2087 * @xprt: rpc_xprt struct containing statistics
2088 * @seq: output file
2089 *
2090 */
2091static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2092{
Chuck Leverc8475462006-12-05 16:35:26 -05002093 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002094 long idle_time = 0;
2095
2096 if (xprt_connected(xprt))
2097 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2098
2099 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002100 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002101 xprt->stat.bind_count,
2102 xprt->stat.connect_count,
2103 xprt->stat.connect_time,
2104 idle_time,
2105 xprt->stat.sends,
2106 xprt->stat.recvs,
2107 xprt->stat.bad_xids,
2108 xprt->stat.req_u,
2109 xprt->stat.bklog_u);
2110}
2111
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002112/*
2113 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2114 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2115 * to use the server side send routines.
2116 */
2117void *bc_malloc(struct rpc_task *task, size_t size)
2118{
2119 struct page *page;
2120 struct rpc_buffer *buf;
2121
2122 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2123 page = alloc_page(GFP_KERNEL);
2124
2125 if (!page)
2126 return NULL;
2127
2128 buf = page_address(page);
2129 buf->len = PAGE_SIZE;
2130
2131 return buf->data;
2132}
2133
2134/*
2135 * Free the space allocated in the bc_alloc routine
2136 */
2137void bc_free(void *buffer)
2138{
2139 struct rpc_buffer *buf;
2140
2141 if (!buffer)
2142 return;
2143
2144 buf = container_of(buffer, struct rpc_buffer, data);
2145 free_page((unsigned long)buf);
2146}
2147
2148/*
2149 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2150 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2151 */
2152static int bc_sendto(struct rpc_rqst *req)
2153{
2154 int len;
2155 struct xdr_buf *xbufp = &req->rq_snd_buf;
2156 struct rpc_xprt *xprt = req->rq_xprt;
2157 struct sock_xprt *transport =
2158 container_of(xprt, struct sock_xprt, xprt);
2159 struct socket *sock = transport->sock;
2160 unsigned long headoff;
2161 unsigned long tailoff;
2162
2163 /*
2164 * Set up the rpc header and record marker stuff
2165 */
2166 xs_encode_tcp_record_marker(xbufp);
2167
2168 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2169 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2170 len = svc_send_common(sock, xbufp,
2171 virt_to_page(xbufp->head[0].iov_base), headoff,
2172 xbufp->tail[0].iov_base, tailoff);
2173
2174 if (len != xbufp->len) {
2175 printk(KERN_NOTICE "Error sending entire callback!\n");
2176 len = -EAGAIN;
2177 }
2178
2179 return len;
2180}
2181
2182/*
2183 * The send routine. Borrows from svc_send
2184 */
2185static int bc_send_request(struct rpc_task *task)
2186{
2187 struct rpc_rqst *req = task->tk_rqstp;
2188 struct svc_xprt *xprt;
2189 struct svc_sock *svsk;
2190 u32 len;
2191
2192 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2193 /*
2194 * Get the server socket associated with this callback xprt
2195 */
2196 xprt = req->rq_xprt->bc_xprt;
2197 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2198
2199 /*
2200 * Grab the mutex to serialize data as the connection is shared
2201 * with the fore channel
2202 */
2203 if (!mutex_trylock(&xprt->xpt_mutex)) {
2204 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2205 if (!mutex_trylock(&xprt->xpt_mutex))
2206 return -EAGAIN;
2207 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2208 }
2209 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2210 len = -ENOTCONN;
2211 else
2212 len = bc_sendto(req);
2213 mutex_unlock(&xprt->xpt_mutex);
2214
2215 if (len > 0)
2216 len = 0;
2217
2218 return len;
2219}
2220
2221/*
2222 * The close routine. Since this is client initiated, we do nothing
2223 */
2224
2225static void bc_close(struct rpc_xprt *xprt)
2226{
2227 return;
2228}
2229
2230/*
2231 * The xprt destroy routine. Again, because this connection is client
2232 * initiated, we do nothing
2233 */
2234
2235static void bc_destroy(struct rpc_xprt *xprt)
2236{
2237 return;
2238}
2239
Chuck Lever262965f2005-08-11 16:25:56 -04002240static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002241 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002242 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002243 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002244 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002245 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002246 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002247 .buf_alloc = rpc_malloc,
2248 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002249 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002250 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002251 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002252 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002253 .close = xs_close,
2254 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002255 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002256};
2257
2258static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002259 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002260 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002261 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002262 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002263 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002264 .buf_alloc = rpc_malloc,
2265 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002266 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002267 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002268#if defined(CONFIG_NFS_V4_1)
2269 .release_request = bc_release_request,
2270#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002271 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002272 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002273 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002274};
2275
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002276/*
2277 * The rpc_xprt_ops for the server backchannel
2278 */
2279
2280static struct rpc_xprt_ops bc_tcp_ops = {
2281 .reserve_xprt = xprt_reserve_xprt,
2282 .release_xprt = xprt_release_xprt,
2283 .buf_alloc = bc_malloc,
2284 .buf_free = bc_free,
2285 .send_request = bc_send_request,
2286 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2287 .close = bc_close,
2288 .destroy = bc_destroy,
2289 .print_stats = xs_tcp_print_stats,
2290};
2291
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002292static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002293 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002294{
2295 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002296 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002297
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002298 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002299 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002300 return ERR_PTR(-EBADF);
2301 }
2302
Chuck Leverffc2e512006-12-05 16:35:11 -05002303 new = kzalloc(sizeof(*new), GFP_KERNEL);
2304 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002305 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2306 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002307 return ERR_PTR(-ENOMEM);
2308 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002309 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002310
2311 xprt->max_reqs = slot_table_size;
2312 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2313 if (xprt->slot == NULL) {
2314 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002315 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2316 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002317 return ERR_PTR(-ENOMEM);
2318 }
2319
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002320 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2321 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002322 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002323 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002324
2325 return xprt;
2326}
2327
Trond Myklebust2881ae72007-12-20 16:03:54 -05002328static const struct rpc_timeout xs_udp_default_timeout = {
2329 .to_initval = 5 * HZ,
2330 .to_maxval = 30 * HZ,
2331 .to_increment = 5 * HZ,
2332 .to_retries = 5,
2333};
2334
Chuck Lever9903cd12005-08-11 16:25:26 -04002335/**
2336 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002337 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002338 *
2339 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002340static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002341{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002342 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002343 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002344 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002345
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002346 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002347 if (IS_ERR(xprt))
2348 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002349 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002350
Chuck Leverec739ef2006-08-22 20:06:15 -04002351 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002352 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002353 /* XXX: header size can vary due to auth type, IPv6, etc. */
2354 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2355
Chuck Lever03bf4b72005-08-25 16:25:55 -07002356 xprt->bind_timeout = XS_BIND_TO;
2357 xprt->connect_timeout = XS_UDP_CONN_TO;
2358 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2359 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002360
Chuck Lever262965f2005-08-11 16:25:56 -04002361 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002362
Trond Myklebustba7392b2007-12-20 16:03:55 -05002363 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002364
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002365 switch (addr->sa_family) {
2366 case AF_INET:
2367 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2368 xprt_set_bound(xprt);
2369
2370 INIT_DELAYED_WORK(&transport->connect_worker,
2371 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002372 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002373 break;
2374 case AF_INET6:
2375 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2376 xprt_set_bound(xprt);
2377
2378 INIT_DELAYED_WORK(&transport->connect_worker,
2379 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002380 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002381 break;
2382 default:
2383 kfree(xprt);
2384 return ERR_PTR(-EAFNOSUPPORT);
2385 }
2386
Chuck Leverc740eff2009-08-09 15:09:46 -04002387 if (xprt_bound(xprt))
2388 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2389 xprt->address_strings[RPC_DISPLAY_ADDR],
2390 xprt->address_strings[RPC_DISPLAY_PORT],
2391 xprt->address_strings[RPC_DISPLAY_PROTO]);
2392 else
2393 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2394 xprt->address_strings[RPC_DISPLAY_ADDR],
2395 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002396
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002397 if (try_module_get(THIS_MODULE))
2398 return xprt;
2399
2400 kfree(xprt->slot);
2401 kfree(xprt);
2402 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002403}
2404
Trond Myklebust2881ae72007-12-20 16:03:54 -05002405static const struct rpc_timeout xs_tcp_default_timeout = {
2406 .to_initval = 60 * HZ,
2407 .to_maxval = 60 * HZ,
2408 .to_retries = 2,
2409};
2410
Chuck Lever9903cd12005-08-11 16:25:26 -04002411/**
2412 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002413 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002414 *
2415 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002416static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002417{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002418 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002419 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002420 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002421
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002422 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002423 if (IS_ERR(xprt))
2424 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002425 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002426
Chuck Leverec739ef2006-08-22 20:06:15 -04002427 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002428 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002429 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002430
Chuck Lever03bf4b72005-08-25 16:25:55 -07002431 xprt->bind_timeout = XS_BIND_TO;
2432 xprt->connect_timeout = XS_TCP_CONN_TO;
2433 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2434 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002435
Chuck Lever262965f2005-08-11 16:25:56 -04002436 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002437 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002438
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002439 switch (addr->sa_family) {
2440 case AF_INET:
2441 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2442 xprt_set_bound(xprt);
2443
Chuck Lever9dc3b092009-08-09 15:09:46 -04002444 INIT_DELAYED_WORK(&transport->connect_worker,
2445 xs_tcp_connect_worker4);
2446 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002447 break;
2448 case AF_INET6:
2449 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2450 xprt_set_bound(xprt);
2451
Chuck Lever9dc3b092009-08-09 15:09:46 -04002452 INIT_DELAYED_WORK(&transport->connect_worker,
2453 xs_tcp_connect_worker6);
2454 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002455 break;
2456 default:
2457 kfree(xprt);
2458 return ERR_PTR(-EAFNOSUPPORT);
2459 }
2460
Chuck Leverc740eff2009-08-09 15:09:46 -04002461 if (xprt_bound(xprt))
2462 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2463 xprt->address_strings[RPC_DISPLAY_ADDR],
2464 xprt->address_strings[RPC_DISPLAY_PORT],
2465 xprt->address_strings[RPC_DISPLAY_PROTO]);
2466 else
2467 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2468 xprt->address_strings[RPC_DISPLAY_ADDR],
2469 xprt->address_strings[RPC_DISPLAY_PROTO]);
2470
Chuck Leveredb267a2006-08-22 20:06:18 -04002471
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002472 if (try_module_get(THIS_MODULE))
2473 return xprt;
2474
2475 kfree(xprt->slot);
2476 kfree(xprt);
2477 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002478}
Chuck Lever282b32e2006-12-05 16:35:51 -05002479
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002480/**
2481 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2482 * @args: rpc transport creation arguments
2483 *
2484 */
2485static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2486{
2487 struct sockaddr *addr = args->dstaddr;
2488 struct rpc_xprt *xprt;
2489 struct sock_xprt *transport;
2490 struct svc_sock *bc_sock;
2491
2492 if (!args->bc_xprt)
2493 ERR_PTR(-EINVAL);
2494
2495 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2496 if (IS_ERR(xprt))
2497 return xprt;
2498 transport = container_of(xprt, struct sock_xprt, xprt);
2499
2500 xprt->prot = IPPROTO_TCP;
2501 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2502 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2503 xprt->timeout = &xs_tcp_default_timeout;
2504
2505 /* backchannel */
2506 xprt_set_bound(xprt);
2507 xprt->bind_timeout = 0;
2508 xprt->connect_timeout = 0;
2509 xprt->reestablish_timeout = 0;
2510 xprt->idle_timeout = 0;
2511
2512 /*
2513 * The backchannel uses the same socket connection as the
2514 * forechannel
2515 */
2516 xprt->bc_xprt = args->bc_xprt;
2517 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2518 bc_sock->sk_bc_xprt = xprt;
2519 transport->sock = bc_sock->sk_sock;
2520 transport->inet = bc_sock->sk_sk;
2521
2522 xprt->ops = &bc_tcp_ops;
2523
2524 switch (addr->sa_family) {
2525 case AF_INET:
2526 xs_format_peer_addresses(xprt, "tcp",
2527 RPCBIND_NETID_TCP);
2528 break;
2529 case AF_INET6:
2530 xs_format_peer_addresses(xprt, "tcp",
2531 RPCBIND_NETID_TCP6);
2532 break;
2533 default:
2534 kfree(xprt);
2535 return ERR_PTR(-EAFNOSUPPORT);
2536 }
2537
2538 if (xprt_bound(xprt))
2539 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2540 xprt->address_strings[RPC_DISPLAY_ADDR],
2541 xprt->address_strings[RPC_DISPLAY_PORT],
2542 xprt->address_strings[RPC_DISPLAY_PROTO]);
2543 else
2544 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2545 xprt->address_strings[RPC_DISPLAY_ADDR],
2546 xprt->address_strings[RPC_DISPLAY_PROTO]);
2547
2548 /*
2549 * Since we don't want connections for the backchannel, we set
2550 * the xprt status to connected
2551 */
2552 xprt_set_connected(xprt);
2553
2554
2555 if (try_module_get(THIS_MODULE))
2556 return xprt;
2557 kfree(xprt->slot);
2558 kfree(xprt);
2559 return ERR_PTR(-EINVAL);
2560}
2561
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002562static struct xprt_class xs_udp_transport = {
2563 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2564 .name = "udp",
2565 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002566 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002567 .setup = xs_setup_udp,
2568};
2569
2570static struct xprt_class xs_tcp_transport = {
2571 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2572 .name = "tcp",
2573 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002574 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002575 .setup = xs_setup_tcp,
2576};
2577
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002578static struct xprt_class xs_bc_tcp_transport = {
2579 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2580 .name = "tcp NFSv4.1 backchannel",
2581 .owner = THIS_MODULE,
2582 .ident = XPRT_TRANSPORT_BC_TCP,
2583 .setup = xs_setup_bc_tcp,
2584};
2585
Chuck Lever282b32e2006-12-05 16:35:51 -05002586/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002587 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002588 *
2589 */
2590int init_socket_xprt(void)
2591{
Chuck Leverfbf76682006-12-05 16:35:54 -05002592#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002593 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002594 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002595#endif
2596
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002597 xprt_register_transport(&xs_udp_transport);
2598 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002599 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002600
Chuck Lever282b32e2006-12-05 16:35:51 -05002601 return 0;
2602}
2603
2604/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002605 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002606 *
2607 */
2608void cleanup_socket_xprt(void)
2609{
Chuck Leverfbf76682006-12-05 16:35:54 -05002610#ifdef RPC_DEBUG
2611 if (sunrpc_table_header) {
2612 unregister_sysctl_table(sunrpc_table_header);
2613 sunrpc_table_header = NULL;
2614 }
2615#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002616
2617 xprt_unregister_transport(&xs_udp_transport);
2618 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002619 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002620}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002621
2622static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2623 unsigned int min, unsigned int max)
2624{
2625 unsigned long num;
2626 int ret;
2627
2628 if (!val)
2629 return -EINVAL;
2630 ret = strict_strtoul(val, 0, &num);
2631 if (ret == -EINVAL || num < min || num > max)
2632 return -EINVAL;
2633 *((unsigned int *)kp->arg) = num;
2634 return 0;
2635}
2636
2637static int param_set_portnr(const char *val, struct kernel_param *kp)
2638{
2639 return param_set_uint_minmax(val, kp,
2640 RPC_MIN_RESVPORT,
2641 RPC_MAX_RESVPORT);
2642}
2643
2644static int param_get_portnr(char *buffer, struct kernel_param *kp)
2645{
2646 return param_get_uint(buffer, kp);
2647}
2648#define param_check_portnr(name, p) \
2649 __param_check(name, p, unsigned int);
2650
2651module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2652module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2653
2654static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2655{
2656 return param_set_uint_minmax(val, kp,
2657 RPC_MIN_SLOT_TABLE,
2658 RPC_MAX_SLOT_TABLE);
2659}
2660
2661static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2662{
2663 return param_get_uint(buffer, kp);
2664}
2665#define param_check_slot_table_size(name, p) \
2666 __param_check(name, p, unsigned int);
2667
2668module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2669 slot_table_size, 0644);
2670module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2671 slot_table_size, 0644);
2672