blob: abb8a589555f6763b48a11e4881287503b6e754b [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 {
Chuck Leverfbf76682006-12-05 16:35:54 -050084 .procname = "udp_slot_table_entries",
85 .data = &xprt_udp_slot_table_entries,
86 .maxlen = sizeof(unsigned int),
87 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080088 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050089 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
91 },
92 {
Chuck Leverfbf76682006-12-05 16:35:54 -050093 .procname = "tcp_slot_table_entries",
94 .data = &xprt_tcp_slot_table_entries,
95 .maxlen = sizeof(unsigned int),
96 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080097 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -050098 .extra1 = &min_slot_table_size,
99 .extra2 = &max_slot_table_size
100 },
101 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800106 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500107 .extra1 = &xprt_min_resvport_limit,
108 .extra2 = &xprt_max_resvport_limit
109 },
110 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500111 .procname = "max_resvport",
112 .data = &xprt_max_resvport,
113 .maxlen = sizeof(unsigned int),
114 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800115 .proc_handler = proc_dointvec_minmax,
Chuck Leverfbf76682006-12-05 16:35:54 -0500116 .extra1 = &xprt_min_resvport_limit,
117 .extra2 = &xprt_max_resvport_limit
118 },
119 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400120 .procname = "tcp_fin_timeout",
121 .data = &xs_tcp_fin_timeout,
122 .maxlen = sizeof(xs_tcp_fin_timeout),
123 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800124 .proc_handler = proc_dointvec_jiffies,
Trond Myklebust25fe6142009-03-11 14:38:03 -0400125 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800126 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500127};
128
129static ctl_table sunrpc_table[] = {
130 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .procname = "sunrpc",
132 .mode = 0555,
133 .child = xs_tunables_table
134 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800135 { },
Chuck Leverfbf76682006-12-05 16:35:54 -0500136};
137
138#endif
139
140/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700141 * Time out for an RPC UDP socket connect. UDP socket connects are
142 * synchronous, but we set a timeout anyway in case of resource
143 * exhaustion on the local host.
144 */
145#define XS_UDP_CONN_TO (5U * HZ)
146
147/*
148 * Wait duration for an RPC TCP connection to be established. Solaris
149 * NFS over TCP uses 60 seconds, for example, which is in line with how
150 * long a server takes to reboot.
151 */
152#define XS_TCP_CONN_TO (60U * HZ)
153
154/*
155 * Wait duration for a reply from the RPC portmapper.
156 */
157#define XS_BIND_TO (60U * HZ)
158
159/*
160 * Delay if a UDP socket connect error occurs. This is most likely some
161 * kind of resource problem on the local host.
162 */
163#define XS_UDP_REEST_TO (2U * HZ)
164
165/*
166 * The reestablish timeout allows clients to delay for a bit before attempting
167 * to reconnect to a server that just dropped our connection.
168 *
169 * We implement an exponential backoff when trying to reestablish a TCP
170 * transport connection with the server. Some servers like to drop a TCP
171 * connection when they are overworked, so we start with a short timeout and
172 * increase over time if the server is down or not responding.
173 */
174#define XS_TCP_INIT_REEST_TO (3U * HZ)
175#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
176
177/*
178 * TCP idle timeout; client drops the transport socket if it is idle
179 * for this long. Note that we also timeout UDP sockets to prevent
180 * holding port numbers when there is no RPC traffic.
181 */
182#define XS_IDLE_DISC_TO (5U * 60 * HZ)
183
Chuck Levera246b012005-08-11 16:25:23 -0400184#ifdef RPC_DEBUG
185# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400186# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400187#endif
188
Chuck Levera246b012005-08-11 16:25:23 -0400189#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400190static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400191{
Chuck Lever9903cd12005-08-11 16:25:26 -0400192 u8 *buf = (u8 *) packet;
193 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400194
Chuck Lever46121cf2007-01-31 12:14:08 -0500195 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400196 for (j = 0; j < count && j < 128; j += 4) {
197 if (!(j & 31)) {
198 if (j)
199 dprintk("\n");
200 dprintk("0x%04x ", j);
201 }
202 dprintk("%02x%02x%02x%02x ",
203 buf[j], buf[j+1], buf[j+2], buf[j+3]);
204 }
205 dprintk("\n");
206}
207#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400208static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400209{
210 /* NOP */
211}
212#endif
213
Chuck Leverffc2e512006-12-05 16:35:11 -0500214struct sock_xprt {
215 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500216
217 /*
218 * Network layer
219 */
220 struct socket * sock;
221 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500222
223 /*
224 * State of TCP reply receive
225 */
226 __be32 tcp_fraghdr,
227 tcp_xid;
228
229 u32 tcp_offset,
230 tcp_reclen;
231
232 unsigned long tcp_copied,
233 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500234
235 /*
236 * Connection of transports
237 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500238 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400239 struct sockaddr_storage srcaddr;
240 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500241
242 /*
243 * UDP socket buffer size parameters
244 */
245 size_t rcvsize,
246 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500247
248 /*
249 * Saved socket callback addresses
250 */
251 void (*old_data_ready)(struct sock *, int);
252 void (*old_state_change)(struct sock *);
253 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400254 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500255};
256
Chuck Levere136d092006-12-05 16:35:23 -0500257/*
258 * TCP receive state flags
259 */
260#define TCP_RCV_LAST_FRAG (1UL << 0)
261#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
262#define TCP_RCV_COPY_XID (1UL << 2)
263#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400264#define TCP_RCV_READ_CALLDIR (1UL << 4)
265#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400266
267/*
268 * TCP RPC flags
269 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400270#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500271
Chuck Lever95392c52007-08-06 11:57:58 -0400272static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400273{
Chuck Lever95392c52007-08-06 11:57:58 -0400274 return (struct sockaddr *) &xprt->addr;
275}
276
277static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
278{
279 return (struct sockaddr_in *) &xprt->addr;
280}
281
282static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
283{
284 return (struct sockaddr_in6 *) &xprt->addr;
285}
286
Chuck Leverc877b842009-08-09 15:09:36 -0400287static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400288{
Chuck Leverc877b842009-08-09 15:09:36 -0400289 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400290 struct sockaddr_in6 *sin6;
291 struct sockaddr_in *sin;
Chuck Leverc877b842009-08-09 15:09:36 -0400292 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400293
Chuck Leverc877b842009-08-09 15:09:36 -0400294 (void)rpc_ntop(sap, buf, sizeof(buf));
295 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
296
Chuck Lever9dc3b092009-08-09 15:09:46 -0400297 switch (sap->sa_family) {
298 case AF_INET:
299 sin = xs_addr_in(xprt);
300 (void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
301 NIPQUAD(sin->sin_addr.s_addr));
302 break;
303 case AF_INET6:
304 sin6 = xs_addr_in6(xprt);
305 (void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
306 break;
307 default:
308 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400309 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400310 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400311}
312
Chuck Lever9dc3b092009-08-09 15:09:46 -0400313static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400314{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400315 struct sockaddr *sap = xs_addr(xprt);
316 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400317
Chuck Leverc877b842009-08-09 15:09:36 -0400318 (void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
319 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400320
Chuck Leverc877b842009-08-09 15:09:36 -0400321 (void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
322 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
323}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400324
Chuck Lever9dc3b092009-08-09 15:09:46 -0400325static void xs_format_peer_addresses(struct rpc_xprt *xprt,
326 const char *protocol,
327 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400328{
Chuck Leverb454ae92008-01-07 18:34:48 -0500329 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500330 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400331 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400332 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400333}
334
Chuck Lever9dc3b092009-08-09 15:09:46 -0400335static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400336{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400337 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
338 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400339
Chuck Lever9dc3b092009-08-09 15:09:46 -0400340 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400341}
342
343static void xs_free_peer_addresses(struct rpc_xprt *xprt)
344{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500345 unsigned int i;
346
347 for (i = 0; i < RPC_DISPLAY_MAX; i++)
348 switch (i) {
349 case RPC_DISPLAY_PROTO:
350 case RPC_DISPLAY_NETID:
351 continue;
352 default:
353 kfree(xprt->address_strings[i]);
354 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400355}
356
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400357#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
358
Trond Myklebust24c56842006-10-17 15:06:22 -0400359static 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 -0400360{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400361 struct msghdr msg = {
362 .msg_name = addr,
363 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400364 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
365 };
366 struct kvec iov = {
367 .iov_base = vec->iov_base + base,
368 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400369 };
370
Trond Myklebust24c56842006-10-17 15:06:22 -0400371 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400372 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
373 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
374}
375
Trond Myklebust24c56842006-10-17 15:06:22 -0400376static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400377{
Trond Myklebust24c56842006-10-17 15:06:22 -0400378 struct page **ppage;
379 unsigned int remainder;
380 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400381
Trond Myklebust24c56842006-10-17 15:06:22 -0400382 remainder = xdr->page_len - base;
383 base += xdr->page_base;
384 ppage = xdr->pages + (base >> PAGE_SHIFT);
385 base &= ~PAGE_MASK;
386 for(;;) {
387 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
388 int flags = XS_SENDMSG_FLAGS;
389
390 remainder -= len;
391 if (remainder != 0 || more)
392 flags |= MSG_MORE;
393 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
394 if (remainder == 0 || err != len)
395 break;
396 sent += err;
397 ppage++;
398 base = 0;
399 }
400 if (sent == 0)
401 return err;
402 if (err > 0)
403 sent += err;
404 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400405}
406
Chuck Lever9903cd12005-08-11 16:25:26 -0400407/**
408 * xs_sendpages - write pages directly to a socket
409 * @sock: socket to send on
410 * @addr: UDP only -- address of destination
411 * @addrlen: UDP only -- length of destination address
412 * @xdr: buffer containing this request
413 * @base: starting position in the buffer
414 *
Chuck Levera246b012005-08-11 16:25:23 -0400415 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400416static 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 -0400417{
Trond Myklebust24c56842006-10-17 15:06:22 -0400418 unsigned int remainder = xdr->len - base;
419 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400420
Chuck Lever262965f2005-08-11 16:25:56 -0400421 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400422 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400423
424 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400425 if (base != 0) {
426 addr = NULL;
427 addrlen = 0;
428 }
Chuck Lever262965f2005-08-11 16:25:56 -0400429
Trond Myklebust24c56842006-10-17 15:06:22 -0400430 if (base < xdr->head[0].iov_len || addr != NULL) {
431 unsigned int len = xdr->head[0].iov_len - base;
432 remainder -= len;
433 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
434 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400435 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400437 base = 0;
438 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400439 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400440
Trond Myklebust24c56842006-10-17 15:06:22 -0400441 if (base < xdr->page_len) {
442 unsigned int len = xdr->page_len - base;
443 remainder -= len;
444 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
445 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400446 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400447 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400448 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400449 } else
450 base -= xdr->page_len;
451
452 if (base >= xdr->tail[0].iov_len)
453 return sent;
454 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400455out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400456 if (sent == 0)
457 return err;
458 if (err > 0)
459 sent += err;
460 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400461}
462
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400463static void xs_nospace_callback(struct rpc_task *task)
464{
465 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
466
467 transport->inet->sk_write_pending--;
468 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
469}
470
Chuck Lever9903cd12005-08-11 16:25:26 -0400471/**
Chuck Lever262965f2005-08-11 16:25:56 -0400472 * xs_nospace - place task on wait queue if transmit was incomplete
473 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400474 *
Chuck Levera246b012005-08-11 16:25:23 -0400475 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400476static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400477{
Chuck Lever262965f2005-08-11 16:25:56 -0400478 struct rpc_rqst *req = task->tk_rqstp;
479 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500480 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400481 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400482
Chuck Lever46121cf2007-01-31 12:14:08 -0500483 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400484 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
485 req->rq_slen);
486
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400487 /* Protect against races with write_space */
488 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400489
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400490 /* Don't race with disconnect */
491 if (xprt_connected(xprt)) {
492 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400493 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400494 /*
495 * Notify TCP that we're limited by the application
496 * window size
497 */
498 set_bit(SOCK_NOSPACE, &transport->sock->flags);
499 transport->inet->sk_write_pending++;
500 /* ...and wait for more buffer space */
501 xprt_wait_for_buffer_space(task, xs_nospace_callback);
502 }
503 } else {
504 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400505 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400506 }
Chuck Lever262965f2005-08-11 16:25:56 -0400507
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400508 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400509 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400510}
511
512/**
513 * xs_udp_send_request - write an RPC request to a UDP socket
514 * @task: address of RPC task that manages the state of an RPC request
515 *
516 * Return values:
517 * 0: The request has been sent
518 * EAGAIN: The socket was blocked, please call again later to
519 * complete the request
520 * ENOTCONN: Caller needs to invoke connect logic then call again
521 * other: Some other error occured, the request was not sent
522 */
523static int xs_udp_send_request(struct rpc_task *task)
524{
525 struct rpc_rqst *req = task->tk_rqstp;
526 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500527 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400528 struct xdr_buf *xdr = &req->rq_snd_buf;
529 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400530
Chuck Lever9903cd12005-08-11 16:25:26 -0400531 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400532 req->rq_svec->iov_base,
533 req->rq_svec->iov_len);
534
Trond Myklebust01d37c42009-03-11 14:09:39 -0400535 if (!xprt_bound(xprt))
536 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500537 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400538 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500539 xprt->addrlen, xdr,
540 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400541
Chuck Lever46121cf2007-01-31 12:14:08 -0500542 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400543 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400544
Trond Myklebust21997002007-10-01 11:43:37 -0400545 if (status >= 0) {
546 task->tk_bytes_sent += status;
547 if (status >= req->rq_slen)
548 return 0;
549 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400550 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400551 }
Chuck Levera246b012005-08-11 16:25:23 -0400552
Chuck Lever262965f2005-08-11 16:25:56 -0400553 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400554 case -ENOTSOCK:
555 status = -ENOTCONN;
556 /* Should we call xs_close() here? */
557 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400558 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400559 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400560 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400561 default:
562 dprintk("RPC: sendmsg returned unrecognized error %d\n",
563 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400564 case -ENETUNREACH:
565 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400566 case -ECONNREFUSED:
567 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400568 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400569 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400570 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800571
Chuck Lever262965f2005-08-11 16:25:56 -0400572 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400573}
574
Trond Myklebuste06799f2007-11-05 15:44:12 -0500575/**
576 * xs_tcp_shutdown - gracefully shut down a TCP socket
577 * @xprt: transport
578 *
579 * Initiates a graceful shutdown of the TCP socket by calling the
580 * equivalent of shutdown(SHUT_WR);
581 */
582static void xs_tcp_shutdown(struct rpc_xprt *xprt)
583{
584 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
585 struct socket *sock = transport->sock;
586
587 if (sock != NULL)
588 kernel_sock_shutdown(sock, SHUT_WR);
589}
590
Chuck Lever808012f2005-08-25 16:25:49 -0700591static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
592{
593 u32 reclen = buf->len - sizeof(rpc_fraghdr);
594 rpc_fraghdr *base = buf->head[0].iov_base;
595 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
596}
597
Chuck Lever9903cd12005-08-11 16:25:26 -0400598/**
Chuck Lever262965f2005-08-11 16:25:56 -0400599 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400600 * @task: address of RPC task that manages the state of an RPC request
601 *
602 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400603 * 0: The request has been sent
604 * EAGAIN: The socket was blocked, please call again later to
605 * complete the request
606 * ENOTCONN: Caller needs to invoke connect logic then call again
607 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400608 *
609 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400610 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400611 */
Chuck Lever262965f2005-08-11 16:25:56 -0400612static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400613{
614 struct rpc_rqst *req = task->tk_rqstp;
615 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500616 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400617 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400618 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400619
Chuck Lever808012f2005-08-25 16:25:49 -0700620 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400621
Chuck Lever262965f2005-08-11 16:25:56 -0400622 xs_pktdump("packet data:",
623 req->rq_svec->iov_base,
624 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400625
626 /* Continue transmitting the packet/record. We must be careful
627 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400628 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400629 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500630 status = xs_sendpages(transport->sock,
631 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400632
Chuck Lever46121cf2007-01-31 12:14:08 -0500633 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400634 xdr->len - req->rq_bytes_sent, status);
635
636 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400637 break;
638
Chuck Lever262965f2005-08-11 16:25:56 -0400639 /* If we've sent the entire packet, immediately
640 * reset the count of bytes sent. */
641 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500642 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400643 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
644 req->rq_bytes_sent = 0;
645 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400646 }
647
Trond Myklebust06b4b682008-04-16 16:51:38 -0400648 if (status != 0)
649 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400650 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400651 break;
Chuck Levera246b012005-08-11 16:25:23 -0400652 }
653
Chuck Lever262965f2005-08-11 16:25:56 -0400654 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400655 case -ENOTSOCK:
656 status = -ENOTCONN;
657 /* Should we call xs_close() here? */
658 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400659 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400660 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400661 break;
662 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500663 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400664 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400665 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400666 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500667 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400668 case -ECONNREFUSED:
669 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400670 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400671 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800672
Chuck Levera246b012005-08-11 16:25:23 -0400673 return status;
674}
675
Chuck Lever9903cd12005-08-11 16:25:26 -0400676/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400677 * xs_tcp_release_xprt - clean up after a tcp transmission
678 * @xprt: transport
679 * @task: rpc task
680 *
681 * This cleans up if an error causes us to abort the transmission of a request.
682 * In this case, the socket may need to be reset in order to avoid confusing
683 * the server.
684 */
685static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
686{
687 struct rpc_rqst *req;
688
689 if (task != xprt->snd_task)
690 return;
691 if (task == NULL)
692 goto out_release;
693 req = task->tk_rqstp;
694 if (req->rq_bytes_sent == 0)
695 goto out_release;
696 if (req->rq_bytes_sent == req->rq_snd_buf.len)
697 goto out_release;
698 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
699out_release:
700 xprt_release_xprt(xprt, task);
701}
702
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400703static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
704{
705 transport->old_data_ready = sk->sk_data_ready;
706 transport->old_state_change = sk->sk_state_change;
707 transport->old_write_space = sk->sk_write_space;
708 transport->old_error_report = sk->sk_error_report;
709}
710
711static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
712{
713 sk->sk_data_ready = transport->old_data_ready;
714 sk->sk_state_change = transport->old_state_change;
715 sk->sk_write_space = transport->old_write_space;
716 sk->sk_error_report = transport->old_error_report;
717}
718
Chuck Leverfe315e72009-03-11 14:10:21 -0400719static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400720{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500721 struct socket *sock = transport->sock;
722 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400723
Chuck Leverfe315e72009-03-11 14:10:21 -0400724 if (sk == NULL)
725 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400726
Chuck Levera246b012005-08-11 16:25:23 -0400727 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500728 transport->inet = NULL;
729 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400730
Chuck Lever9903cd12005-08-11 16:25:26 -0400731 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400732
733 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400734 write_unlock_bh(&sk->sk_callback_lock);
735
Chuck Lever9903cd12005-08-11 16:25:26 -0400736 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400737
738 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400739}
740
741/**
742 * xs_close - close a socket
743 * @xprt: transport
744 *
745 * This is used when all requests are complete; ie, no DRC state remains
746 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400747 *
748 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
749 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400750 */
751static void xs_close(struct rpc_xprt *xprt)
752{
753 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
754
755 dprintk("RPC: xs_close xprt %p\n", xprt);
756
757 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400758 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400759
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100760 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400761 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100762 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500763 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100764 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500765 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400766}
767
Trond Myklebustf75e6742009-04-21 17:18:20 -0400768static void xs_tcp_close(struct rpc_xprt *xprt)
769{
770 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
771 xs_close(xprt);
772 else
773 xs_tcp_shutdown(xprt);
774}
775
Chuck Lever9903cd12005-08-11 16:25:26 -0400776/**
777 * xs_destroy - prepare to shutdown a transport
778 * @xprt: doomed transport
779 *
780 */
781static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400782{
Chuck Leverc8475462006-12-05 16:35:26 -0500783 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
784
Chuck Lever46121cf2007-01-31 12:14:08 -0500785 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400786
Trond Myklebustc1384c92007-06-14 18:00:42 -0400787 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400788
Chuck Lever9903cd12005-08-11 16:25:26 -0400789 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400790 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400791 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400792 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400793 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400794}
795
Chuck Lever9903cd12005-08-11 16:25:26 -0400796static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400797{
Chuck Lever9903cd12005-08-11 16:25:26 -0400798 return (struct rpc_xprt *) sk->sk_user_data;
799}
800
801/**
802 * xs_udp_data_ready - "data ready" callback for UDP sockets
803 * @sk: socket with data to read
804 * @len: how much data to read
805 *
806 */
807static void xs_udp_data_ready(struct sock *sk, int len)
808{
809 struct rpc_task *task;
810 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400811 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400812 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400813 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700814 u32 _xid;
815 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400816
817 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500818 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400819 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400820 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400821
822 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
823 goto out;
824
825 if (xprt->shutdown)
826 goto dropit;
827
828 repsize = skb->len - sizeof(struct udphdr);
829 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500830 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400831 goto dropit;
832 }
833
834 /* Copy the XID from the skb... */
835 xp = skb_header_pointer(skb, sizeof(struct udphdr),
836 sizeof(_xid), &_xid);
837 if (xp == NULL)
838 goto dropit;
839
840 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400841 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400842 rovr = xprt_lookup_rqst(xprt, *xp);
843 if (!rovr)
844 goto out_unlock;
845 task = rovr->rq_task;
846
Chuck Levera246b012005-08-11 16:25:23 -0400847 if ((copied = rovr->rq_private_buf.buflen) > repsize)
848 copied = repsize;
849
850 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800851 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
852 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400853 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800854 }
855
856 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400857
858 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000859 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400860
Chuck Lever1570c1e2005-08-25 16:25:52 -0700861 xprt_adjust_cwnd(task, copied);
862 xprt_update_rtt(task);
863 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400864
865 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400866 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400867 dropit:
868 skb_free_datagram(sk, skb);
869 out:
870 read_unlock(&sk->sk_callback_lock);
871}
872
Chuck Leverdd456472006-12-05 16:35:44 -0500873static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400874{
Chuck Lever51971132006-12-05 16:35:19 -0500875 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400876 size_t len, used;
877 char *p;
878
Chuck Lever51971132006-12-05 16:35:19 -0500879 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
880 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500881 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500882 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400883 if (used != len)
884 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700885
Chuck Lever51971132006-12-05 16:35:19 -0500886 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
887 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500888 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400889 else
Chuck Levere136d092006-12-05 16:35:23 -0500890 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500891 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700892
Chuck Levere136d092006-12-05 16:35:23 -0500893 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500894 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700895
Chuck Levera246b012005-08-11 16:25:23 -0400896 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400897 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500898 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500899 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400900 return;
Chuck Levera246b012005-08-11 16:25:23 -0400901 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500902 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500903 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400904}
905
Chuck Lever51971132006-12-05 16:35:19 -0500906static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400907{
Chuck Lever51971132006-12-05 16:35:19 -0500908 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500909 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500910 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500911 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
912 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
913 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500914 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400915 }
916 }
917}
918
Chuck Leverdd456472006-12-05 16:35:44 -0500919static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400920{
921 size_t len, used;
922 char *p;
923
Chuck Lever51971132006-12-05 16:35:19 -0500924 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500925 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500926 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500927 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500928 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400929 if (used != len)
930 return;
Chuck Levere136d092006-12-05 16:35:23 -0500931 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400932 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500933 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400934 dprintk("RPC: reading %s XID %08x\n",
935 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
936 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500937 ntohl(transport->tcp_xid));
938 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400939}
940
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400941static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
942 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400943{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400944 size_t len, used;
945 u32 offset;
946 __be32 calldir;
947
948 /*
949 * We want transport->tcp_offset to be 8 at the end of this routine
950 * (4 bytes for the xid and 4 bytes for the call/reply flag).
951 * When this function is called for the first time,
952 * transport->tcp_offset is 4 (after having already read the xid).
953 */
954 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
955 len = sizeof(calldir) - offset;
956 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
957 used = xdr_skb_read_bits(desc, &calldir, len);
958 transport->tcp_offset += used;
959 if (used != len)
960 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400961 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
962 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400963 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400964 /*
965 * We don't yet have the XDR buffer, so we will write the calldir
966 * out after we get the buffer from the 'struct rpc_rqst'
967 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400968 if (ntohl(calldir) == RPC_REPLY)
969 transport->tcp_flags |= TCP_RPC_REPLY;
970 else
971 transport->tcp_flags &= ~TCP_RPC_REPLY;
972 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
973 (transport->tcp_flags & TCP_RPC_REPLY) ?
974 "reply for" : "request with", calldir);
975 xs_tcp_check_fraghdr(transport);
976}
977
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400978static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
979 struct xdr_skb_reader *desc,
980 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400981{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400982 struct sock_xprt *transport =
983 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400984 struct xdr_buf *rcvbuf;
985 size_t len;
986 ssize_t r;
987
Chuck Levera246b012005-08-11 16:25:23 -0400988 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400989
990 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
991 /*
992 * Save the RPC direction in the XDR buffer
993 */
994 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
995 htonl(RPC_REPLY) : 0;
996
997 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
998 &calldir, sizeof(calldir));
999 transport->tcp_copied += sizeof(calldir);
1000 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001001 }
1002
Chuck Levera246b012005-08-11 16:25:23 -04001003 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001004 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001005 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001006
Chuck Lever51971132006-12-05 16:35:19 -05001007 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001008 memcpy(&my_desc, desc, sizeof(my_desc));
1009 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001010 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001011 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001012 desc->count -= r;
1013 desc->offset += r;
1014 } else
Chuck Lever51971132006-12-05 16:35:19 -05001015 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001016 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001017
1018 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001019 transport->tcp_copied += r;
1020 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001021 }
1022 if (r != len) {
1023 /* Error when copying to the receive buffer,
1024 * usually because we weren't able to allocate
1025 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001026 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001027 * will not receive any additional updates,
1028 * and time out.
1029 * Any remaining data from this record will
1030 * be discarded.
1031 */
Chuck Levere136d092006-12-05 16:35:23 -05001032 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001033 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001034 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001035 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1036 "tcp_offset = %u, tcp_reclen = %u\n",
1037 xprt, transport->tcp_copied,
1038 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001039 return;
Chuck Levera246b012005-08-11 16:25:23 -04001040 }
1041
Chuck Lever46121cf2007-01-31 12:14:08 -05001042 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001043 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001044 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1045 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1046 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001047
Chuck Lever51971132006-12-05 16:35:19 -05001048 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001049 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001050 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001051 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1052 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001053 }
1054
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001055 return;
1056}
1057
1058/*
1059 * Finds the request corresponding to the RPC xid and invokes the common
1060 * tcp read code to read the data.
1061 */
1062static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1063 struct xdr_skb_reader *desc)
1064{
1065 struct sock_xprt *transport =
1066 container_of(xprt, struct sock_xprt, xprt);
1067 struct rpc_rqst *req;
1068
1069 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1070
1071 /* Find and lock the request corresponding to this xid */
1072 spin_lock(&xprt->transport_lock);
1073 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1074 if (!req) {
1075 dprintk("RPC: XID %08x request not found!\n",
1076 ntohl(transport->tcp_xid));
1077 spin_unlock(&xprt->transport_lock);
1078 return -1;
1079 }
1080
1081 xs_tcp_read_common(xprt, desc, req);
1082
Chuck Levere136d092006-12-05 16:35:23 -05001083 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001084 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001085
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001086 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001087 return 0;
1088}
1089
1090#if defined(CONFIG_NFS_V4_1)
1091/*
1092 * Obtains an rpc_rqst previously allocated and invokes the common
1093 * tcp read code to read the data. The result is placed in the callback
1094 * queue.
1095 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1096 * connection and return -1.
1097 */
1098static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1099 struct xdr_skb_reader *desc)
1100{
1101 struct sock_xprt *transport =
1102 container_of(xprt, struct sock_xprt, xprt);
1103 struct rpc_rqst *req;
1104
1105 req = xprt_alloc_bc_request(xprt);
1106 if (req == NULL) {
1107 printk(KERN_WARNING "Callback slot table overflowed\n");
1108 xprt_force_disconnect(xprt);
1109 return -1;
1110 }
1111
1112 req->rq_xid = transport->tcp_xid;
1113 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1114 xs_tcp_read_common(xprt, desc, req);
1115
1116 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1117 struct svc_serv *bc_serv = xprt->bc_serv;
1118
1119 /*
1120 * Add callback request to callback list. The callback
1121 * service sleeps on the sv_cb_waitq waiting for new
1122 * requests. Wake it up after adding enqueing the
1123 * request.
1124 */
1125 dprintk("RPC: add callback request to list\n");
1126 spin_lock(&bc_serv->sv_cb_lock);
1127 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1128 spin_unlock(&bc_serv->sv_cb_lock);
1129 wake_up(&bc_serv->sv_cb_waitq);
1130 }
1131
1132 req->rq_private_buf.len = transport->tcp_copied;
1133
1134 return 0;
1135}
1136
1137static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1138 struct xdr_skb_reader *desc)
1139{
1140 struct sock_xprt *transport =
1141 container_of(xprt, struct sock_xprt, xprt);
1142
1143 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1144 xs_tcp_read_reply(xprt, desc) :
1145 xs_tcp_read_callback(xprt, desc);
1146}
1147#else
1148static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1149 struct xdr_skb_reader *desc)
1150{
1151 return xs_tcp_read_reply(xprt, desc);
1152}
1153#endif /* CONFIG_NFS_V4_1 */
1154
1155/*
1156 * Read data off the transport. This can be either an RPC_CALL or an
1157 * RPC_REPLY. Relay the processing to helper functions.
1158 */
1159static void xs_tcp_read_data(struct rpc_xprt *xprt,
1160 struct xdr_skb_reader *desc)
1161{
1162 struct sock_xprt *transport =
1163 container_of(xprt, struct sock_xprt, xprt);
1164
1165 if (_xs_tcp_read_data(xprt, desc) == 0)
1166 xs_tcp_check_fraghdr(transport);
1167 else {
1168 /*
1169 * The transport_lock protects the request handling.
1170 * There's no need to hold it to update the tcp_flags.
1171 */
1172 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1173 }
Chuck Levera246b012005-08-11 16:25:23 -04001174}
1175
Chuck Leverdd456472006-12-05 16:35:44 -05001176static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001177{
1178 size_t len;
1179
Chuck Lever51971132006-12-05 16:35:19 -05001180 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001181 if (len > desc->count)
1182 len = desc->count;
1183 desc->count -= len;
1184 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001185 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001186 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001187 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001188}
1189
Chuck Lever9903cd12005-08-11 16:25:26 -04001190static 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 -04001191{
1192 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001193 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001194 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001195 .skb = skb,
1196 .offset = offset,
1197 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001198 };
Chuck Levera246b012005-08-11 16:25:23 -04001199
Chuck Lever46121cf2007-01-31 12:14:08 -05001200 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001201 do {
1202 /* Read in a new fragment marker if necessary */
1203 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001204 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001205 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001206 continue;
1207 }
1208 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001209 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001210 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001211 continue;
1212 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001213 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001214 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001215 xs_tcp_read_calldir(transport, &desc);
1216 continue;
1217 }
Chuck Levera246b012005-08-11 16:25:23 -04001218 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001219 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001220 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001221 continue;
1222 }
1223 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001224 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001225 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001226 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001227 return len - desc.count;
1228}
1229
Chuck Lever9903cd12005-08-11 16:25:26 -04001230/**
1231 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1232 * @sk: socket with data to read
1233 * @bytes: how much data to read
1234 *
1235 */
1236static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001237{
1238 struct rpc_xprt *xprt;
1239 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001240 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001241
Chuck Lever46121cf2007-01-31 12:14:08 -05001242 dprintk("RPC: xs_tcp_data_ready...\n");
1243
Chuck Levera246b012005-08-11 16:25:23 -04001244 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001245 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001246 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001247 if (xprt->shutdown)
1248 goto out;
1249
Neil Brown61d0a8e2009-09-23 14:36:37 -04001250 /* Any data means we had a useful conversation, so
1251 * the we don't need to delay the next reconnect
1252 */
1253 if (xprt->reestablish_timeout)
1254 xprt->reestablish_timeout = 0;
1255
Chuck Lever9903cd12005-08-11 16:25:26 -04001256 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001257 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001258 do {
1259 rd_desc.count = 65536;
1260 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1261 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001262out:
1263 read_unlock(&sk->sk_callback_lock);
1264}
1265
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001266/*
1267 * Do the equivalent of linger/linger2 handling for dealing with
1268 * broken servers that don't close the socket in a timely
1269 * fashion
1270 */
1271static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1272 unsigned long timeout)
1273{
1274 struct sock_xprt *transport;
1275
1276 if (xprt_test_and_set_connecting(xprt))
1277 return;
1278 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1279 transport = container_of(xprt, struct sock_xprt, xprt);
1280 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1281 timeout);
1282}
1283
1284static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1285{
1286 struct sock_xprt *transport;
1287
1288 transport = container_of(xprt, struct sock_xprt, xprt);
1289
1290 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1291 !cancel_delayed_work(&transport->connect_worker))
1292 return;
1293 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1294 xprt_clear_connecting(xprt);
1295}
1296
1297static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1298{
1299 smp_mb__before_clear_bit();
1300 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1301 clear_bit(XPRT_CLOSING, &xprt->state);
1302 smp_mb__after_clear_bit();
1303 /* Mark transport as closed and wake up all pending tasks */
1304 xprt_disconnect_done(xprt);
1305}
1306
Chuck Lever9903cd12005-08-11 16:25:26 -04001307/**
1308 * xs_tcp_state_change - callback to handle TCP socket state changes
1309 * @sk: socket whose state has changed
1310 *
1311 */
1312static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001313{
Chuck Lever9903cd12005-08-11 16:25:26 -04001314 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001315
1316 read_lock(&sk->sk_callback_lock);
1317 if (!(xprt = xprt_from_sock(sk)))
1318 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001319 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1320 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1321 sk->sk_state, xprt_connected(xprt),
1322 sock_flag(sk, SOCK_DEAD),
1323 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001324
1325 switch (sk->sk_state) {
1326 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001327 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001328 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001329 struct sock_xprt *transport = container_of(xprt,
1330 struct sock_xprt, xprt);
1331
Chuck Levera246b012005-08-11 16:25:23 -04001332 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001333 transport->tcp_offset = 0;
1334 transport->tcp_reclen = 0;
1335 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001336 transport->tcp_flags =
1337 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001338
Trond Myklebust2a491992009-03-11 14:38:00 -04001339 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001340 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001341 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001342 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001343 case TCP_FIN_WAIT1:
1344 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001345 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001346 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001347 set_bit(XPRT_CLOSING, &xprt->state);
1348 smp_mb__before_clear_bit();
1349 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001350 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001351 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001352 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001353 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001354 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001355 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001356 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001357 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001358 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001359 case TCP_CLOSING:
1360 /*
1361 * If the server closed down the connection, make sure that
1362 * we back off before reconnecting
1363 */
1364 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1365 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001366 break;
1367 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001368 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001369 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001370 smp_mb__before_clear_bit();
1371 clear_bit(XPRT_CONNECTED, &xprt->state);
1372 smp_mb__after_clear_bit();
1373 break;
1374 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001375 xs_tcp_cancel_linger_timeout(xprt);
1376 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001377 }
1378 out:
1379 read_unlock(&sk->sk_callback_lock);
1380}
1381
Chuck Lever9903cd12005-08-11 16:25:26 -04001382/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001383 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001384 * @sk: socket
1385 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001386static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001387{
1388 struct rpc_xprt *xprt;
1389
1390 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001391 if (!(xprt = xprt_from_sock(sk)))
1392 goto out;
1393 dprintk("RPC: %s client %p...\n"
1394 "RPC: error %d\n",
1395 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001396 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001397out:
1398 read_unlock(&sk->sk_callback_lock);
1399}
1400
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001401static void xs_write_space(struct sock *sk)
1402{
1403 struct socket *sock;
1404 struct rpc_xprt *xprt;
1405
1406 if (unlikely(!(sock = sk->sk_socket)))
1407 return;
1408 clear_bit(SOCK_NOSPACE, &sock->flags);
1409
1410 if (unlikely(!(xprt = xprt_from_sock(sk))))
1411 return;
1412 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1413 return;
1414
1415 xprt_write_space(xprt);
1416}
1417
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001418/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001419 * xs_udp_write_space - callback invoked when socket buffer space
1420 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001421 * @sk: socket whose state has changed
1422 *
Chuck Levera246b012005-08-11 16:25:23 -04001423 * Called when more output buffer space is available for this socket.
1424 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001425 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001426 * with a bunch of small requests.
1427 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001428static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001429{
Chuck Levera246b012005-08-11 16:25:23 -04001430 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001431
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001432 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001433 if (sock_writeable(sk))
1434 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001435
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001436 read_unlock(&sk->sk_callback_lock);
1437}
Chuck Levera246b012005-08-11 16:25:23 -04001438
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001439/**
1440 * xs_tcp_write_space - callback invoked when socket buffer space
1441 * becomes available
1442 * @sk: socket whose state has changed
1443 *
1444 * Called when more output buffer space is available for this socket.
1445 * We try not to wake our writers until they can make "significant"
1446 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1447 * with a bunch of small requests.
1448 */
1449static void xs_tcp_write_space(struct sock *sk)
1450{
1451 read_lock(&sk->sk_callback_lock);
1452
1453 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001454 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1455 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001456
Chuck Levera246b012005-08-11 16:25:23 -04001457 read_unlock(&sk->sk_callback_lock);
1458}
1459
Chuck Lever470056c2005-08-25 16:25:56 -07001460static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001461{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001462 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1463 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001464
Chuck Lever7c6e0662006-12-05 16:35:30 -05001465 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001466 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001467 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001468 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001469 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001470 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001471 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001472 sk->sk_write_space(sk);
1473 }
1474}
1475
Chuck Lever43118c22005-08-25 16:25:49 -07001476/**
Chuck Lever470056c2005-08-25 16:25:56 -07001477 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001478 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001479 * @sndsize: requested size of send buffer, in bytes
1480 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001481 *
Chuck Lever470056c2005-08-25 16:25:56 -07001482 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001483 */
Chuck Lever470056c2005-08-25 16:25:56 -07001484static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001485{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001486 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1487
1488 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001489 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001490 transport->sndsize = sndsize + 1024;
1491 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001492 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001493 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001494
1495 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001496}
1497
Chuck Lever46c0ee82005-08-25 16:25:52 -07001498/**
1499 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1500 * @task: task that timed out
1501 *
1502 * Adjust the congestion window after a retransmit timeout has occurred.
1503 */
1504static void xs_udp_timer(struct rpc_task *task)
1505{
1506 xprt_adjust_cwnd(task, -ETIMEDOUT);
1507}
1508
Chuck Leverb85d8802006-05-25 01:40:49 -04001509static unsigned short xs_get_random_port(void)
1510{
1511 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1512 unsigned short rand = (unsigned short) net_random() % range;
1513 return rand + xprt_min_resvport;
1514}
1515
Chuck Lever92200412006-01-03 09:55:51 +01001516/**
1517 * xs_set_port - reset the port number in the remote endpoint address
1518 * @xprt: generic transport
1519 * @port: new port number
1520 *
1521 */
1522static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1523{
Chuck Lever46121cf2007-01-31 12:14:08 -05001524 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001525
Chuck Lever9dc3b092009-08-09 15:09:46 -04001526 rpc_set_port(xs_addr(xprt), port);
1527 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001528}
1529
Trond Myklebust67a391d2007-11-05 17:40:58 -05001530static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1531{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001532 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001533
1534 if (port == 0 && transport->xprt.resvport)
1535 port = xs_get_random_port();
1536 return port;
1537}
1538
1539static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1540{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001541 if (transport->srcport != 0)
1542 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001543 if (!transport->xprt.resvport)
1544 return 0;
1545 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1546 return xprt_max_resvport;
1547 return --port;
1548}
1549
Chuck Lever7dc753f2007-08-06 11:57:28 -04001550static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001551{
1552 struct sockaddr_in myaddr = {
1553 .sin_family = AF_INET,
1554 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001555 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001556 int err, nloop = 0;
1557 unsigned short port = xs_get_srcport(transport, sock);
1558 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001559
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001560 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001561 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001562 do {
1563 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001564 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001565 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001566 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001567 break;
Chuck Levera246b012005-08-11 16:25:23 -04001568 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001569 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001570 break;
Chuck Levera246b012005-08-11 16:25:23 -04001571 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001572 last = port;
1573 port = xs_next_srcport(transport, sock, port);
1574 if (port > last)
1575 nloop++;
1576 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001577 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1578 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001579 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001580 return err;
1581}
1582
Chuck Lever90058d32007-08-06 11:57:33 -04001583static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1584{
1585 struct sockaddr_in6 myaddr = {
1586 .sin6_family = AF_INET6,
1587 };
1588 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001589 int err, nloop = 0;
1590 unsigned short port = xs_get_srcport(transport, sock);
1591 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001592
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001593 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001594 myaddr.sin6_addr = sa->sin6_addr;
1595 do {
1596 myaddr.sin6_port = htons(port);
1597 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1598 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001599 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001600 break;
1601 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001602 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001603 break;
1604 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001605 last = port;
1606 port = xs_next_srcport(transport, sock, port);
1607 if (port > last)
1608 nloop++;
1609 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001610 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001611 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001612 return err;
1613}
1614
Peter Zijlstraed075362006-12-06 20:35:24 -08001615#ifdef CONFIG_DEBUG_LOCK_ALLOC
1616static struct lock_class_key xs_key[2];
1617static struct lock_class_key xs_slock_key[2];
1618
Chuck Lever8945ee52007-08-06 11:58:04 -04001619static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001620{
1621 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001622
John Heffner02b3d342007-09-12 10:42:12 +02001623 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001624 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1625 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1626}
Peter Zijlstraed075362006-12-06 20:35:24 -08001627
Chuck Lever8945ee52007-08-06 11:58:04 -04001628static inline void xs_reclassify_socket6(struct socket *sock)
1629{
1630 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001631
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001632 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001633 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1634 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001635}
1636#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001637static inline void xs_reclassify_socket4(struct socket *sock)
1638{
1639}
1640
1641static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001642{
1643}
1644#endif
1645
Chuck Lever16be2d22007-08-06 11:57:38 -04001646static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001647{
Chuck Lever16be2d22007-08-06 11:57:38 -04001648 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001649
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001650 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001651 struct sock *sk = sock->sk;
1652
1653 write_lock_bh(&sk->sk_callback_lock);
1654
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001655 xs_save_old_callbacks(transport, sk);
1656
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001657 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001658 sk->sk_data_ready = xs_udp_data_ready;
1659 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001660 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001661 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001662 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001663
1664 xprt_set_connected(xprt);
1665
1666 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001667 transport->sock = sock;
1668 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001669
1670 write_unlock_bh(&sk->sk_callback_lock);
1671 }
Chuck Lever470056c2005-08-25 16:25:56 -07001672 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001673}
1674
Chuck Levera246b012005-08-11 16:25:23 -04001675/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001676 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001677 * @work: RPC transport to connect
1678 *
1679 * Invoked by a work queue tasklet.
1680 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001681static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001682{
1683 struct sock_xprt *transport =
1684 container_of(work, struct sock_xprt, connect_worker.work);
1685 struct rpc_xprt *xprt = &transport->xprt;
1686 struct socket *sock = transport->sock;
1687 int err, status = -EIO;
1688
Trond Myklebust01d37c42009-03-11 14:09:39 -04001689 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001690 goto out;
1691
Chuck Levera246b012005-08-11 16:25:23 -04001692 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001693 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001694
Chuck Leverfe315e72009-03-11 14:10:21 -04001695 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1696 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001697 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001698 goto out;
1699 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001700 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001701
Chuck Lever7dc753f2007-08-06 11:57:28 -04001702 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001703 sock_release(sock);
1704 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001705 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001706
Chuck Leverc740eff2009-08-09 15:09:46 -04001707 dprintk("RPC: worker connecting xprt %p via %s to "
1708 "%s (port %s)\n", xprt,
1709 xprt->address_strings[RPC_DISPLAY_PROTO],
1710 xprt->address_strings[RPC_DISPLAY_ADDR],
1711 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001712
Chuck Lever16be2d22007-08-06 11:57:38 -04001713 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001714 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001715out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001716 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001717 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001718}
1719
Chuck Lever68e220b2007-08-06 11:57:48 -04001720/**
1721 * xs_udp_connect_worker6 - set up a UDP socket
1722 * @work: RPC transport to connect
1723 *
1724 * Invoked by a work queue tasklet.
1725 */
1726static void xs_udp_connect_worker6(struct work_struct *work)
1727{
1728 struct sock_xprt *transport =
1729 container_of(work, struct sock_xprt, connect_worker.work);
1730 struct rpc_xprt *xprt = &transport->xprt;
1731 struct socket *sock = transport->sock;
1732 int err, status = -EIO;
1733
Trond Myklebust01d37c42009-03-11 14:09:39 -04001734 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001735 goto out;
1736
1737 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001738 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001739
Chuck Leverfe315e72009-03-11 14:10:21 -04001740 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1741 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001742 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1743 goto out;
1744 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001745 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001746
1747 if (xs_bind6(transport, sock) < 0) {
1748 sock_release(sock);
1749 goto out;
1750 }
1751
Chuck Leverc740eff2009-08-09 15:09:46 -04001752 dprintk("RPC: worker connecting xprt %p via %s to "
1753 "%s (port %s)\n", xprt,
1754 xprt->address_strings[RPC_DISPLAY_PROTO],
1755 xprt->address_strings[RPC_DISPLAY_ADDR],
1756 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001757
1758 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001759 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001760out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001761 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001762 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001763}
1764
Chuck Lever3167e122005-08-25 16:25:55 -07001765/*
1766 * We need to preserve the port number so the reply cache on the server can
1767 * find our cached RPC replies when we get around to reconnecting.
1768 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001769static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001770{
1771 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001772 struct sockaddr any;
1773
Chuck Lever46121cf2007-01-31 12:14:08 -05001774 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001775
1776 /*
1777 * Disconnect the transport socket by doing a connect operation
1778 * with AF_UNSPEC. This should return immediately...
1779 */
1780 memset(&any, 0, sizeof(any));
1781 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001782 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001783 if (!result)
1784 xs_sock_mark_closed(xprt);
1785 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001786 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001787 result);
1788}
1789
Trond Myklebust40d25492009-03-11 14:37:58 -04001790static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1791{
1792 unsigned int state = transport->inet->sk_state;
1793
1794 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1795 return;
1796 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1797 return;
1798 xs_abort_connection(xprt, transport);
1799}
1800
Chuck Lever16be2d22007-08-06 11:57:38 -04001801static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001802{
Chuck Lever16be2d22007-08-06 11:57:38 -04001803 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001804
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001805 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001806 struct sock *sk = sock->sk;
1807
1808 write_lock_bh(&sk->sk_callback_lock);
1809
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001810 xs_save_old_callbacks(transport, sk);
1811
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001812 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001813 sk->sk_data_ready = xs_tcp_data_ready;
1814 sk->sk_state_change = xs_tcp_state_change;
1815 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001816 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001817 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001818
1819 /* socket options */
1820 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1821 sock_reset_flag(sk, SOCK_LINGER);
1822 tcp_sk(sk)->linger2 = 0;
1823 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001824
1825 xprt_clear_connected(xprt);
1826
1827 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001828 transport->sock = sock;
1829 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001830
1831 write_unlock_bh(&sk->sk_callback_lock);
1832 }
1833
Trond Myklebust01d37c42009-03-11 14:09:39 -04001834 if (!xprt_bound(xprt))
1835 return -ENOTCONN;
1836
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001837 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001838 xprt->stat.connect_count++;
1839 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001840 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001841}
1842
1843/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001844 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1845 * @xprt: RPC transport to connect
1846 * @transport: socket transport to connect
1847 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001848 *
1849 * Invoked by a work queue tasklet.
1850 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001851static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1852 struct sock_xprt *transport,
1853 struct socket *(*create_sock)(struct rpc_xprt *,
1854 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001855{
Chuck Lever16be2d22007-08-06 11:57:38 -04001856 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001857 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001858
Trond Myklebust01d37c42009-03-11 14:09:39 -04001859 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001860 goto out;
1861
1862 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001863 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001864 sock = create_sock(xprt, transport);
1865 if (IS_ERR(sock)) {
1866 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001867 goto out;
1868 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001869 } else {
1870 int abort_and_exit;
1871
1872 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1873 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001874 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001875 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001876
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001877 if (abort_and_exit)
1878 goto out_eagain;
1879 }
1880
Chuck Leverc740eff2009-08-09 15:09:46 -04001881 dprintk("RPC: worker connecting xprt %p via %s to "
1882 "%s (port %s)\n", xprt,
1883 xprt->address_strings[RPC_DISPLAY_PROTO],
1884 xprt->address_strings[RPC_DISPLAY_ADDR],
1885 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001886
1887 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001888 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1889 xprt, -status, xprt_connected(xprt),
1890 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001891 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001892 default:
1893 printk("%s: connect returned unhandled error %d\n",
1894 __func__, status);
1895 case -EADDRNOTAVAIL:
1896 /* We're probably in TIME_WAIT. Get rid of existing socket,
1897 * and retry
1898 */
1899 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1900 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001901 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001902 case -ECONNREFUSED:
1903 case -ECONNRESET:
1904 case -ENETUNREACH:
1905 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001906 case 0:
1907 case -EINPROGRESS:
1908 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001909 xprt_clear_connecting(xprt);
1910 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05001911 case -EINVAL:
1912 /* Happens, for instance, if the user specified a link
1913 * local IPv6 address without a scope-id.
1914 */
1915 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001916 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001917out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001918 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001919out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001920 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001921 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001922}
1923
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001924static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1925 struct sock_xprt *transport)
1926{
1927 struct socket *sock;
1928 int err;
1929
1930 /* start from scratch */
1931 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1932 if (err < 0) {
1933 dprintk("RPC: can't create TCP transport socket (%d).\n",
1934 -err);
1935 goto out_err;
1936 }
1937 xs_reclassify_socket4(sock);
1938
1939 if (xs_bind4(transport, sock) < 0) {
1940 sock_release(sock);
1941 goto out_err;
1942 }
1943 return sock;
1944out_err:
1945 return ERR_PTR(-EIO);
1946}
1947
1948/**
Chuck Levera246b012005-08-11 16:25:23 -04001949 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1950 * @work: RPC transport to connect
1951 *
1952 * Invoked by a work queue tasklet.
1953 */
1954static void xs_tcp_connect_worker4(struct work_struct *work)
1955{
1956 struct sock_xprt *transport =
1957 container_of(work, struct sock_xprt, connect_worker.work);
1958 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001959
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001960 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1961}
Chuck Levera246b012005-08-11 16:25:23 -04001962
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001963static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1964 struct sock_xprt *transport)
1965{
1966 struct socket *sock;
1967 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001968
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001969 /* start from scratch */
1970 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1971 if (err < 0) {
1972 dprintk("RPC: can't create TCP transport socket (%d).\n",
1973 -err);
1974 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001975 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001976 xs_reclassify_socket6(sock);
1977
1978 if (xs_bind6(transport, sock) < 0) {
1979 sock_release(sock);
1980 goto out_err;
1981 }
1982 return sock;
1983out_err:
1984 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001985}
1986
Chuck Lever9903cd12005-08-11 16:25:26 -04001987/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001988 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1989 * @work: RPC transport to connect
1990 *
1991 * Invoked by a work queue tasklet.
1992 */
1993static void xs_tcp_connect_worker6(struct work_struct *work)
1994{
1995 struct sock_xprt *transport =
1996 container_of(work, struct sock_xprt, connect_worker.work);
1997 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001998
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001999 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002000}
2001
2002/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002003 * xs_connect - connect a socket to a remote endpoint
2004 * @task: address of RPC task that manages state of connect request
2005 *
2006 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002007 *
2008 * UDP socket connects are synchronous, but we use a work queue anyway
2009 * to guarantee that even unprivileged user processes can set up a
2010 * socket on a privileged port.
2011 *
2012 * If a UDP socket connect fails, the delay behavior here prevents
2013 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002014 */
2015static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002016{
2017 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002018 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002019
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002020 if (xprt_test_and_set_connecting(xprt))
2021 return;
2022
Chuck Lever09a21c42009-12-03 15:58:56 -05002023 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002024 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2025 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002026 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002027 queue_delayed_work(rpciod_workqueue,
2028 &transport->connect_worker,
2029 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002030 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002031 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2032 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002033 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2034 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002035 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002036 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002037 queue_delayed_work(rpciod_workqueue,
2038 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002039 }
2040}
2041
Trond Myklebuste06799f2007-11-05 15:44:12 -05002042static void xs_tcp_connect(struct rpc_task *task)
2043{
2044 struct rpc_xprt *xprt = task->tk_xprt;
2045
Trond Myklebuste06799f2007-11-05 15:44:12 -05002046 /* Exit if we need to wait for socket shutdown to complete */
2047 if (test_bit(XPRT_CLOSING, &xprt->state))
2048 return;
2049 xs_connect(task);
2050}
2051
Chuck Lever262ca072006-03-20 13:44:16 -05002052/**
2053 * xs_udp_print_stats - display UDP socket-specifc stats
2054 * @xprt: rpc_xprt struct containing statistics
2055 * @seq: output file
2056 *
2057 */
2058static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2059{
Chuck Leverc8475462006-12-05 16:35:26 -05002060 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2061
Chuck Lever262ca072006-03-20 13:44:16 -05002062 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002063 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002064 xprt->stat.bind_count,
2065 xprt->stat.sends,
2066 xprt->stat.recvs,
2067 xprt->stat.bad_xids,
2068 xprt->stat.req_u,
2069 xprt->stat.bklog_u);
2070}
2071
2072/**
2073 * xs_tcp_print_stats - display TCP socket-specifc stats
2074 * @xprt: rpc_xprt struct containing statistics
2075 * @seq: output file
2076 *
2077 */
2078static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2079{
Chuck Leverc8475462006-12-05 16:35:26 -05002080 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002081 long idle_time = 0;
2082
2083 if (xprt_connected(xprt))
2084 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2085
2086 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002087 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002088 xprt->stat.bind_count,
2089 xprt->stat.connect_count,
2090 xprt->stat.connect_time,
2091 idle_time,
2092 xprt->stat.sends,
2093 xprt->stat.recvs,
2094 xprt->stat.bad_xids,
2095 xprt->stat.req_u,
2096 xprt->stat.bklog_u);
2097}
2098
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002099/*
2100 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2101 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2102 * to use the server side send routines.
2103 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002104static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002105{
2106 struct page *page;
2107 struct rpc_buffer *buf;
2108
2109 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2110 page = alloc_page(GFP_KERNEL);
2111
2112 if (!page)
2113 return NULL;
2114
2115 buf = page_address(page);
2116 buf->len = PAGE_SIZE;
2117
2118 return buf->data;
2119}
2120
2121/*
2122 * Free the space allocated in the bc_alloc routine
2123 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002124static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002125{
2126 struct rpc_buffer *buf;
2127
2128 if (!buffer)
2129 return;
2130
2131 buf = container_of(buffer, struct rpc_buffer, data);
2132 free_page((unsigned long)buf);
2133}
2134
2135/*
2136 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2137 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2138 */
2139static int bc_sendto(struct rpc_rqst *req)
2140{
2141 int len;
2142 struct xdr_buf *xbufp = &req->rq_snd_buf;
2143 struct rpc_xprt *xprt = req->rq_xprt;
2144 struct sock_xprt *transport =
2145 container_of(xprt, struct sock_xprt, xprt);
2146 struct socket *sock = transport->sock;
2147 unsigned long headoff;
2148 unsigned long tailoff;
2149
2150 /*
2151 * Set up the rpc header and record marker stuff
2152 */
2153 xs_encode_tcp_record_marker(xbufp);
2154
2155 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2156 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2157 len = svc_send_common(sock, xbufp,
2158 virt_to_page(xbufp->head[0].iov_base), headoff,
2159 xbufp->tail[0].iov_base, tailoff);
2160
2161 if (len != xbufp->len) {
2162 printk(KERN_NOTICE "Error sending entire callback!\n");
2163 len = -EAGAIN;
2164 }
2165
2166 return len;
2167}
2168
2169/*
2170 * The send routine. Borrows from svc_send
2171 */
2172static int bc_send_request(struct rpc_task *task)
2173{
2174 struct rpc_rqst *req = task->tk_rqstp;
2175 struct svc_xprt *xprt;
2176 struct svc_sock *svsk;
2177 u32 len;
2178
2179 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2180 /*
2181 * Get the server socket associated with this callback xprt
2182 */
2183 xprt = req->rq_xprt->bc_xprt;
2184 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2185
2186 /*
2187 * Grab the mutex to serialize data as the connection is shared
2188 * with the fore channel
2189 */
2190 if (!mutex_trylock(&xprt->xpt_mutex)) {
2191 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2192 if (!mutex_trylock(&xprt->xpt_mutex))
2193 return -EAGAIN;
2194 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2195 }
2196 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2197 len = -ENOTCONN;
2198 else
2199 len = bc_sendto(req);
2200 mutex_unlock(&xprt->xpt_mutex);
2201
2202 if (len > 0)
2203 len = 0;
2204
2205 return len;
2206}
2207
2208/*
2209 * The close routine. Since this is client initiated, we do nothing
2210 */
2211
2212static void bc_close(struct rpc_xprt *xprt)
2213{
2214 return;
2215}
2216
2217/*
2218 * The xprt destroy routine. Again, because this connection is client
2219 * initiated, we do nothing
2220 */
2221
2222static void bc_destroy(struct rpc_xprt *xprt)
2223{
2224 return;
2225}
2226
Chuck Lever262965f2005-08-11 16:25:56 -04002227static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002228 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002229 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002230 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002231 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002232 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002233 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002234 .buf_alloc = rpc_malloc,
2235 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002236 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002237 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002238 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002239 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002240 .close = xs_close,
2241 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002242 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002243};
2244
2245static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002246 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002247 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002248 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002249 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002250 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002251 .buf_alloc = rpc_malloc,
2252 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002253 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002254 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002255#if defined(CONFIG_NFS_V4_1)
2256 .release_request = bc_release_request,
2257#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002258 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002259 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002260 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002261};
2262
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002263/*
2264 * The rpc_xprt_ops for the server backchannel
2265 */
2266
2267static struct rpc_xprt_ops bc_tcp_ops = {
2268 .reserve_xprt = xprt_reserve_xprt,
2269 .release_xprt = xprt_release_xprt,
2270 .buf_alloc = bc_malloc,
2271 .buf_free = bc_free,
2272 .send_request = bc_send_request,
2273 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2274 .close = bc_close,
2275 .destroy = bc_destroy,
2276 .print_stats = xs_tcp_print_stats,
2277};
2278
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002279static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002280 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002281{
2282 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002283 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002284
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002285 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002286 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002287 return ERR_PTR(-EBADF);
2288 }
2289
Chuck Leverffc2e512006-12-05 16:35:11 -05002290 new = kzalloc(sizeof(*new), GFP_KERNEL);
2291 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002292 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2293 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002294 return ERR_PTR(-ENOMEM);
2295 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002296 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002297
2298 xprt->max_reqs = slot_table_size;
2299 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2300 if (xprt->slot == NULL) {
2301 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002302 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2303 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002304 return ERR_PTR(-ENOMEM);
2305 }
2306
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002307 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2308 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002309 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002310 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002311
2312 return xprt;
2313}
2314
Trond Myklebust2881ae72007-12-20 16:03:54 -05002315static const struct rpc_timeout xs_udp_default_timeout = {
2316 .to_initval = 5 * HZ,
2317 .to_maxval = 30 * HZ,
2318 .to_increment = 5 * HZ,
2319 .to_retries = 5,
2320};
2321
Chuck Lever9903cd12005-08-11 16:25:26 -04002322/**
2323 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002324 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002325 *
2326 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002327static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002328{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002329 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002330 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002331 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002332
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002333 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002334 if (IS_ERR(xprt))
2335 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002336 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002337
Chuck Leverec739ef2006-08-22 20:06:15 -04002338 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002339 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002340 /* XXX: header size can vary due to auth type, IPv6, etc. */
2341 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2342
Chuck Lever03bf4b72005-08-25 16:25:55 -07002343 xprt->bind_timeout = XS_BIND_TO;
2344 xprt->connect_timeout = XS_UDP_CONN_TO;
2345 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2346 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002347
Chuck Lever262965f2005-08-11 16:25:56 -04002348 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002349
Trond Myklebustba7392b2007-12-20 16:03:55 -05002350 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002351
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002352 switch (addr->sa_family) {
2353 case AF_INET:
2354 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2355 xprt_set_bound(xprt);
2356
2357 INIT_DELAYED_WORK(&transport->connect_worker,
2358 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002359 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002360 break;
2361 case AF_INET6:
2362 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2363 xprt_set_bound(xprt);
2364
2365 INIT_DELAYED_WORK(&transport->connect_worker,
2366 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002367 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002368 break;
2369 default:
2370 kfree(xprt);
2371 return ERR_PTR(-EAFNOSUPPORT);
2372 }
2373
Chuck Leverc740eff2009-08-09 15:09:46 -04002374 if (xprt_bound(xprt))
2375 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2376 xprt->address_strings[RPC_DISPLAY_ADDR],
2377 xprt->address_strings[RPC_DISPLAY_PORT],
2378 xprt->address_strings[RPC_DISPLAY_PROTO]);
2379 else
2380 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2381 xprt->address_strings[RPC_DISPLAY_ADDR],
2382 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002383
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002384 if (try_module_get(THIS_MODULE))
2385 return xprt;
2386
2387 kfree(xprt->slot);
2388 kfree(xprt);
2389 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002390}
2391
Trond Myklebust2881ae72007-12-20 16:03:54 -05002392static const struct rpc_timeout xs_tcp_default_timeout = {
2393 .to_initval = 60 * HZ,
2394 .to_maxval = 60 * HZ,
2395 .to_retries = 2,
2396};
2397
Chuck Lever9903cd12005-08-11 16:25:26 -04002398/**
2399 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002400 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002401 *
2402 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002403static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002404{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002405 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002406 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002407 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002408
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002409 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002410 if (IS_ERR(xprt))
2411 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002412 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002413
Chuck Leverec739ef2006-08-22 20:06:15 -04002414 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002415 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002416 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002417
Chuck Lever03bf4b72005-08-25 16:25:55 -07002418 xprt->bind_timeout = XS_BIND_TO;
2419 xprt->connect_timeout = XS_TCP_CONN_TO;
2420 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2421 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002422
Chuck Lever262965f2005-08-11 16:25:56 -04002423 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002424 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002425
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002426 switch (addr->sa_family) {
2427 case AF_INET:
2428 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2429 xprt_set_bound(xprt);
2430
Chuck Lever9dc3b092009-08-09 15:09:46 -04002431 INIT_DELAYED_WORK(&transport->connect_worker,
2432 xs_tcp_connect_worker4);
2433 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002434 break;
2435 case AF_INET6:
2436 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2437 xprt_set_bound(xprt);
2438
Chuck Lever9dc3b092009-08-09 15:09:46 -04002439 INIT_DELAYED_WORK(&transport->connect_worker,
2440 xs_tcp_connect_worker6);
2441 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002442 break;
2443 default:
2444 kfree(xprt);
2445 return ERR_PTR(-EAFNOSUPPORT);
2446 }
2447
Chuck Leverc740eff2009-08-09 15:09:46 -04002448 if (xprt_bound(xprt))
2449 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2450 xprt->address_strings[RPC_DISPLAY_ADDR],
2451 xprt->address_strings[RPC_DISPLAY_PORT],
2452 xprt->address_strings[RPC_DISPLAY_PROTO]);
2453 else
2454 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2455 xprt->address_strings[RPC_DISPLAY_ADDR],
2456 xprt->address_strings[RPC_DISPLAY_PROTO]);
2457
Chuck Leveredb267a2006-08-22 20:06:18 -04002458
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002459 if (try_module_get(THIS_MODULE))
2460 return xprt;
2461
2462 kfree(xprt->slot);
2463 kfree(xprt);
2464 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002465}
Chuck Lever282b32e2006-12-05 16:35:51 -05002466
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002467/**
2468 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2469 * @args: rpc transport creation arguments
2470 *
2471 */
2472static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2473{
2474 struct sockaddr *addr = args->dstaddr;
2475 struct rpc_xprt *xprt;
2476 struct sock_xprt *transport;
2477 struct svc_sock *bc_sock;
2478
2479 if (!args->bc_xprt)
2480 ERR_PTR(-EINVAL);
2481
2482 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2483 if (IS_ERR(xprt))
2484 return xprt;
2485 transport = container_of(xprt, struct sock_xprt, xprt);
2486
2487 xprt->prot = IPPROTO_TCP;
2488 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2489 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2490 xprt->timeout = &xs_tcp_default_timeout;
2491
2492 /* backchannel */
2493 xprt_set_bound(xprt);
2494 xprt->bind_timeout = 0;
2495 xprt->connect_timeout = 0;
2496 xprt->reestablish_timeout = 0;
2497 xprt->idle_timeout = 0;
2498
2499 /*
2500 * The backchannel uses the same socket connection as the
2501 * forechannel
2502 */
2503 xprt->bc_xprt = args->bc_xprt;
2504 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2505 bc_sock->sk_bc_xprt = xprt;
2506 transport->sock = bc_sock->sk_sock;
2507 transport->inet = bc_sock->sk_sk;
2508
2509 xprt->ops = &bc_tcp_ops;
2510
2511 switch (addr->sa_family) {
2512 case AF_INET:
2513 xs_format_peer_addresses(xprt, "tcp",
2514 RPCBIND_NETID_TCP);
2515 break;
2516 case AF_INET6:
2517 xs_format_peer_addresses(xprt, "tcp",
2518 RPCBIND_NETID_TCP6);
2519 break;
2520 default:
2521 kfree(xprt);
2522 return ERR_PTR(-EAFNOSUPPORT);
2523 }
2524
2525 if (xprt_bound(xprt))
2526 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2527 xprt->address_strings[RPC_DISPLAY_ADDR],
2528 xprt->address_strings[RPC_DISPLAY_PORT],
2529 xprt->address_strings[RPC_DISPLAY_PROTO]);
2530 else
2531 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2532 xprt->address_strings[RPC_DISPLAY_ADDR],
2533 xprt->address_strings[RPC_DISPLAY_PROTO]);
2534
2535 /*
2536 * Since we don't want connections for the backchannel, we set
2537 * the xprt status to connected
2538 */
2539 xprt_set_connected(xprt);
2540
2541
2542 if (try_module_get(THIS_MODULE))
2543 return xprt;
2544 kfree(xprt->slot);
2545 kfree(xprt);
2546 return ERR_PTR(-EINVAL);
2547}
2548
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002549static struct xprt_class xs_udp_transport = {
2550 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2551 .name = "udp",
2552 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002553 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002554 .setup = xs_setup_udp,
2555};
2556
2557static struct xprt_class xs_tcp_transport = {
2558 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2559 .name = "tcp",
2560 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002561 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002562 .setup = xs_setup_tcp,
2563};
2564
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002565static struct xprt_class xs_bc_tcp_transport = {
2566 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2567 .name = "tcp NFSv4.1 backchannel",
2568 .owner = THIS_MODULE,
2569 .ident = XPRT_TRANSPORT_BC_TCP,
2570 .setup = xs_setup_bc_tcp,
2571};
2572
Chuck Lever282b32e2006-12-05 16:35:51 -05002573/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002574 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002575 *
2576 */
2577int init_socket_xprt(void)
2578{
Chuck Leverfbf76682006-12-05 16:35:54 -05002579#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002580 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002581 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002582#endif
2583
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002584 xprt_register_transport(&xs_udp_transport);
2585 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002586 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002587
Chuck Lever282b32e2006-12-05 16:35:51 -05002588 return 0;
2589}
2590
2591/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002592 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002593 *
2594 */
2595void cleanup_socket_xprt(void)
2596{
Chuck Leverfbf76682006-12-05 16:35:54 -05002597#ifdef RPC_DEBUG
2598 if (sunrpc_table_header) {
2599 unregister_sysctl_table(sunrpc_table_header);
2600 sunrpc_table_header = NULL;
2601 }
2602#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002603
2604 xprt_unregister_transport(&xs_udp_transport);
2605 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002606 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002607}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002608
2609static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2610 unsigned int min, unsigned int max)
2611{
2612 unsigned long num;
2613 int ret;
2614
2615 if (!val)
2616 return -EINVAL;
2617 ret = strict_strtoul(val, 0, &num);
2618 if (ret == -EINVAL || num < min || num > max)
2619 return -EINVAL;
2620 *((unsigned int *)kp->arg) = num;
2621 return 0;
2622}
2623
2624static int param_set_portnr(const char *val, struct kernel_param *kp)
2625{
2626 return param_set_uint_minmax(val, kp,
2627 RPC_MIN_RESVPORT,
2628 RPC_MAX_RESVPORT);
2629}
2630
2631static int param_get_portnr(char *buffer, struct kernel_param *kp)
2632{
2633 return param_get_uint(buffer, kp);
2634}
2635#define param_check_portnr(name, p) \
2636 __param_check(name, p, unsigned int);
2637
2638module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2639module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2640
2641static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2642{
2643 return param_set_uint_minmax(val, kp,
2644 RPC_MIN_SLOT_TABLE,
2645 RPC_MAX_SLOT_TABLE);
2646}
2647
2648static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2649{
2650 return param_get_uint(buffer, kp);
2651}
2652#define param_check_slot_table_size(name, p) \
2653 __param_check(name, p, unsigned int);
2654
2655module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2656 slot_table_size, 0644);
2657module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2658 slot_table_size, 0644);
2659