blob: 4f55ab7ec1b1fdbad401519eda1c68627027e108 [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);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800300 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400301 break;
302 case AF_INET6:
303 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800304 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400305 break;
306 default:
307 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400308 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400309 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400310}
311
Chuck Lever9dc3b092009-08-09 15:09:46 -0400312static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400313{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400314 struct sockaddr *sap = xs_addr(xprt);
315 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400316
Joe Perches81160e662010-03-08 12:15:59 -0800317 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400318 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400319
Joe Perches81160e662010-03-08 12:15:59 -0800320 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400321 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
322}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400323
Chuck Lever9dc3b092009-08-09 15:09:46 -0400324static void xs_format_peer_addresses(struct rpc_xprt *xprt,
325 const char *protocol,
326 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400327{
Chuck Leverb454ae92008-01-07 18:34:48 -0500328 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500329 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400330 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400331 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400332}
333
Chuck Lever9dc3b092009-08-09 15:09:46 -0400334static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400335{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400336 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
337 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400338
Chuck Lever9dc3b092009-08-09 15:09:46 -0400339 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400340}
341
342static void xs_free_peer_addresses(struct rpc_xprt *xprt)
343{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500344 unsigned int i;
345
346 for (i = 0; i < RPC_DISPLAY_MAX; i++)
347 switch (i) {
348 case RPC_DISPLAY_PROTO:
349 case RPC_DISPLAY_NETID:
350 continue;
351 default:
352 kfree(xprt->address_strings[i]);
353 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400354}
355
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400356#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
357
Trond Myklebust24c56842006-10-17 15:06:22 -0400358static 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 -0400359{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400360 struct msghdr msg = {
361 .msg_name = addr,
362 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400363 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
364 };
365 struct kvec iov = {
366 .iov_base = vec->iov_base + base,
367 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400368 };
369
Trond Myklebust24c56842006-10-17 15:06:22 -0400370 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400371 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
372 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
373}
374
Trond Myklebust24c56842006-10-17 15:06:22 -0400375static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400376{
Trond Myklebust24c56842006-10-17 15:06:22 -0400377 struct page **ppage;
378 unsigned int remainder;
379 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400380
Trond Myklebust24c56842006-10-17 15:06:22 -0400381 remainder = xdr->page_len - base;
382 base += xdr->page_base;
383 ppage = xdr->pages + (base >> PAGE_SHIFT);
384 base &= ~PAGE_MASK;
385 for(;;) {
386 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
387 int flags = XS_SENDMSG_FLAGS;
388
389 remainder -= len;
390 if (remainder != 0 || more)
391 flags |= MSG_MORE;
392 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
393 if (remainder == 0 || err != len)
394 break;
395 sent += err;
396 ppage++;
397 base = 0;
398 }
399 if (sent == 0)
400 return err;
401 if (err > 0)
402 sent += err;
403 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400404}
405
Chuck Lever9903cd12005-08-11 16:25:26 -0400406/**
407 * xs_sendpages - write pages directly to a socket
408 * @sock: socket to send on
409 * @addr: UDP only -- address of destination
410 * @addrlen: UDP only -- length of destination address
411 * @xdr: buffer containing this request
412 * @base: starting position in the buffer
413 *
Chuck Levera246b012005-08-11 16:25:23 -0400414 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400415static 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 -0400416{
Trond Myklebust24c56842006-10-17 15:06:22 -0400417 unsigned int remainder = xdr->len - base;
418 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400419
Chuck Lever262965f2005-08-11 16:25:56 -0400420 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400421 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400422
423 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400424 if (base != 0) {
425 addr = NULL;
426 addrlen = 0;
427 }
Chuck Lever262965f2005-08-11 16:25:56 -0400428
Trond Myklebust24c56842006-10-17 15:06:22 -0400429 if (base < xdr->head[0].iov_len || addr != NULL) {
430 unsigned int len = xdr->head[0].iov_len - base;
431 remainder -= len;
432 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
433 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400434 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400435 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400436 base = 0;
437 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400438 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400439
Trond Myklebust24c56842006-10-17 15:06:22 -0400440 if (base < xdr->page_len) {
441 unsigned int len = xdr->page_len - base;
442 remainder -= len;
443 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
444 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400445 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400446 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400447 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 } else
449 base -= xdr->page_len;
450
451 if (base >= xdr->tail[0].iov_len)
452 return sent;
453 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400454out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400455 if (sent == 0)
456 return err;
457 if (err > 0)
458 sent += err;
459 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400460}
461
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400462static void xs_nospace_callback(struct rpc_task *task)
463{
464 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
465
466 transport->inet->sk_write_pending--;
467 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
468}
469
Chuck Lever9903cd12005-08-11 16:25:26 -0400470/**
Chuck Lever262965f2005-08-11 16:25:56 -0400471 * xs_nospace - place task on wait queue if transmit was incomplete
472 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400473 *
Chuck Levera246b012005-08-11 16:25:23 -0400474 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400475static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400476{
Chuck Lever262965f2005-08-11 16:25:56 -0400477 struct rpc_rqst *req = task->tk_rqstp;
478 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500479 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400480 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400481
Chuck Lever46121cf2007-01-31 12:14:08 -0500482 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400483 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
484 req->rq_slen);
485
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400486 /* Protect against races with write_space */
487 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400488
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400489 /* Don't race with disconnect */
490 if (xprt_connected(xprt)) {
491 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400492 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400493 /*
494 * Notify TCP that we're limited by the application
495 * window size
496 */
497 set_bit(SOCK_NOSPACE, &transport->sock->flags);
498 transport->inet->sk_write_pending++;
499 /* ...and wait for more buffer space */
500 xprt_wait_for_buffer_space(task, xs_nospace_callback);
501 }
502 } else {
503 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400504 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400505 }
Chuck Lever262965f2005-08-11 16:25:56 -0400506
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400507 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400508 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400509}
510
511/**
512 * xs_udp_send_request - write an RPC request to a UDP socket
513 * @task: address of RPC task that manages the state of an RPC request
514 *
515 * Return values:
516 * 0: The request has been sent
517 * EAGAIN: The socket was blocked, please call again later to
518 * complete the request
519 * ENOTCONN: Caller needs to invoke connect logic then call again
520 * other: Some other error occured, the request was not sent
521 */
522static int xs_udp_send_request(struct rpc_task *task)
523{
524 struct rpc_rqst *req = task->tk_rqstp;
525 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500526 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400527 struct xdr_buf *xdr = &req->rq_snd_buf;
528 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400529
Chuck Lever9903cd12005-08-11 16:25:26 -0400530 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400531 req->rq_svec->iov_base,
532 req->rq_svec->iov_len);
533
Trond Myklebust01d37c42009-03-11 14:09:39 -0400534 if (!xprt_bound(xprt))
535 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500536 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400537 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500538 xprt->addrlen, xdr,
539 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400540
Chuck Lever46121cf2007-01-31 12:14:08 -0500541 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400542 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400543
Trond Myklebust21997002007-10-01 11:43:37 -0400544 if (status >= 0) {
545 task->tk_bytes_sent += status;
546 if (status >= req->rq_slen)
547 return 0;
548 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400549 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400550 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400551 if (!transport->sock)
552 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400553
Chuck Lever262965f2005-08-11 16:25:56 -0400554 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400555 case -ENOTSOCK:
556 status = -ENOTCONN;
557 /* Should we call xs_close() here? */
558 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400559 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400560 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400561 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400562 default:
563 dprintk("RPC: sendmsg returned unrecognized error %d\n",
564 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400565 case -ENETUNREACH:
566 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400567 case -ECONNREFUSED:
568 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400569 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400570 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400571 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400572out:
Chuck Lever262965f2005-08-11 16:25:56 -0400573 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400574}
575
Trond Myklebuste06799f2007-11-05 15:44:12 -0500576/**
577 * xs_tcp_shutdown - gracefully shut down a TCP socket
578 * @xprt: transport
579 *
580 * Initiates a graceful shutdown of the TCP socket by calling the
581 * equivalent of shutdown(SHUT_WR);
582 */
583static void xs_tcp_shutdown(struct rpc_xprt *xprt)
584{
585 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
586 struct socket *sock = transport->sock;
587
588 if (sock != NULL)
589 kernel_sock_shutdown(sock, SHUT_WR);
590}
591
Chuck Lever808012f2005-08-25 16:25:49 -0700592static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
593{
594 u32 reclen = buf->len - sizeof(rpc_fraghdr);
595 rpc_fraghdr *base = buf->head[0].iov_base;
596 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
597}
598
Chuck Lever9903cd12005-08-11 16:25:26 -0400599/**
Chuck Lever262965f2005-08-11 16:25:56 -0400600 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400601 * @task: address of RPC task that manages the state of an RPC request
602 *
603 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400604 * 0: The request has been sent
605 * EAGAIN: The socket was blocked, please call again later to
606 * complete the request
607 * ENOTCONN: Caller needs to invoke connect logic then call again
608 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400609 *
610 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400611 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400612 */
Chuck Lever262965f2005-08-11 16:25:56 -0400613static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400614{
615 struct rpc_rqst *req = task->tk_rqstp;
616 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500617 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400618 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400619 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400620
Chuck Lever808012f2005-08-25 16:25:49 -0700621 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400622
Chuck Lever262965f2005-08-11 16:25:56 -0400623 xs_pktdump("packet data:",
624 req->rq_svec->iov_base,
625 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400626
627 /* Continue transmitting the packet/record. We must be careful
628 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400629 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400630 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500631 status = xs_sendpages(transport->sock,
632 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400633
Chuck Lever46121cf2007-01-31 12:14:08 -0500634 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400635 xdr->len - req->rq_bytes_sent, status);
636
637 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400638 break;
639
Chuck Lever262965f2005-08-11 16:25:56 -0400640 /* If we've sent the entire packet, immediately
641 * reset the count of bytes sent. */
642 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500643 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400644 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
645 req->rq_bytes_sent = 0;
646 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400647 }
648
Trond Myklebust06b4b682008-04-16 16:51:38 -0400649 if (status != 0)
650 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400651 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400652 break;
Chuck Levera246b012005-08-11 16:25:23 -0400653 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400654 if (!transport->sock)
655 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400656
Chuck Lever262965f2005-08-11 16:25:56 -0400657 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400658 case -ENOTSOCK:
659 status = -ENOTCONN;
660 /* Should we call xs_close() here? */
661 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400662 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400663 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400664 break;
665 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500666 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400667 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400668 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400669 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500670 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400671 case -ECONNREFUSED:
672 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400673 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400674 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400675out:
Chuck Levera246b012005-08-11 16:25:23 -0400676 return status;
677}
678
Chuck Lever9903cd12005-08-11 16:25:26 -0400679/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400680 * xs_tcp_release_xprt - clean up after a tcp transmission
681 * @xprt: transport
682 * @task: rpc task
683 *
684 * This cleans up if an error causes us to abort the transmission of a request.
685 * In this case, the socket may need to be reset in order to avoid confusing
686 * the server.
687 */
688static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
689{
690 struct rpc_rqst *req;
691
692 if (task != xprt->snd_task)
693 return;
694 if (task == NULL)
695 goto out_release;
696 req = task->tk_rqstp;
697 if (req->rq_bytes_sent == 0)
698 goto out_release;
699 if (req->rq_bytes_sent == req->rq_snd_buf.len)
700 goto out_release;
701 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
702out_release:
703 xprt_release_xprt(xprt, task);
704}
705
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400706static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
707{
708 transport->old_data_ready = sk->sk_data_ready;
709 transport->old_state_change = sk->sk_state_change;
710 transport->old_write_space = sk->sk_write_space;
711 transport->old_error_report = sk->sk_error_report;
712}
713
714static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
715{
716 sk->sk_data_ready = transport->old_data_ready;
717 sk->sk_state_change = transport->old_state_change;
718 sk->sk_write_space = transport->old_write_space;
719 sk->sk_error_report = transport->old_error_report;
720}
721
Chuck Leverfe315e72009-03-11 14:10:21 -0400722static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400723{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500724 struct socket *sock = transport->sock;
725 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400726
Chuck Leverfe315e72009-03-11 14:10:21 -0400727 if (sk == NULL)
728 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400729
Chuck Levera246b012005-08-11 16:25:23 -0400730 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500731 transport->inet = NULL;
732 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400733
Chuck Lever9903cd12005-08-11 16:25:26 -0400734 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400735
736 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400737 write_unlock_bh(&sk->sk_callback_lock);
738
Chuck Lever9903cd12005-08-11 16:25:26 -0400739 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400740
741 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400742}
743
744/**
745 * xs_close - close a socket
746 * @xprt: transport
747 *
748 * This is used when all requests are complete; ie, no DRC state remains
749 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400750 *
751 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
752 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400753 */
754static void xs_close(struct rpc_xprt *xprt)
755{
756 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
757
758 dprintk("RPC: xs_close xprt %p\n", xprt);
759
760 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400761 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400762
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100763 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400764 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100765 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500766 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100767 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500768 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400769}
770
Trond Myklebustf75e6742009-04-21 17:18:20 -0400771static void xs_tcp_close(struct rpc_xprt *xprt)
772{
773 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
774 xs_close(xprt);
775 else
776 xs_tcp_shutdown(xprt);
777}
778
Chuck Lever9903cd12005-08-11 16:25:26 -0400779/**
780 * xs_destroy - prepare to shutdown a transport
781 * @xprt: doomed transport
782 *
783 */
784static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400785{
Chuck Leverc8475462006-12-05 16:35:26 -0500786 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
787
Chuck Lever46121cf2007-01-31 12:14:08 -0500788 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400789
Trond Myklebustc1384c92007-06-14 18:00:42 -0400790 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400791
Chuck Lever9903cd12005-08-11 16:25:26 -0400792 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400793 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400794 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400795 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400796 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400797}
798
Chuck Lever9903cd12005-08-11 16:25:26 -0400799static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400800{
Chuck Lever9903cd12005-08-11 16:25:26 -0400801 return (struct rpc_xprt *) sk->sk_user_data;
802}
803
804/**
805 * xs_udp_data_ready - "data ready" callback for UDP sockets
806 * @sk: socket with data to read
807 * @len: how much data to read
808 *
809 */
810static void xs_udp_data_ready(struct sock *sk, int len)
811{
812 struct rpc_task *task;
813 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400814 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400815 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400816 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700817 u32 _xid;
818 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400819
820 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500821 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400822 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400823 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400824
825 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
826 goto out;
827
828 if (xprt->shutdown)
829 goto dropit;
830
831 repsize = skb->len - sizeof(struct udphdr);
832 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500833 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400834 goto dropit;
835 }
836
837 /* Copy the XID from the skb... */
838 xp = skb_header_pointer(skb, sizeof(struct udphdr),
839 sizeof(_xid), &_xid);
840 if (xp == NULL)
841 goto dropit;
842
843 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400844 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400845 rovr = xprt_lookup_rqst(xprt, *xp);
846 if (!rovr)
847 goto out_unlock;
848 task = rovr->rq_task;
849
Chuck Levera246b012005-08-11 16:25:23 -0400850 if ((copied = rovr->rq_private_buf.buflen) > repsize)
851 copied = repsize;
852
853 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800854 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
855 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400856 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800857 }
858
859 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400860
861 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000862 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400863
Chuck Lever1570c1e2005-08-25 16:25:52 -0700864 xprt_adjust_cwnd(task, copied);
865 xprt_update_rtt(task);
866 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400867
868 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400869 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400870 dropit:
871 skb_free_datagram(sk, skb);
872 out:
873 read_unlock(&sk->sk_callback_lock);
874}
875
Chuck Leverdd456472006-12-05 16:35:44 -0500876static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400877{
Chuck Lever51971132006-12-05 16:35:19 -0500878 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400879 size_t len, used;
880 char *p;
881
Chuck Lever51971132006-12-05 16:35:19 -0500882 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
883 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500884 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500885 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400886 if (used != len)
887 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700888
Chuck Lever51971132006-12-05 16:35:19 -0500889 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
890 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500891 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400892 else
Chuck Levere136d092006-12-05 16:35:23 -0500893 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500894 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700895
Chuck Levere136d092006-12-05 16:35:23 -0500896 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500897 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700898
Chuck Levera246b012005-08-11 16:25:23 -0400899 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400900 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500901 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500902 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400903 return;
Chuck Levera246b012005-08-11 16:25:23 -0400904 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500905 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500906 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400907}
908
Chuck Lever51971132006-12-05 16:35:19 -0500909static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400910{
Chuck Lever51971132006-12-05 16:35:19 -0500911 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500912 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500913 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500914 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
915 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
916 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500917 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400918 }
919 }
920}
921
Chuck Leverdd456472006-12-05 16:35:44 -0500922static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400923{
924 size_t len, used;
925 char *p;
926
Chuck Lever51971132006-12-05 16:35:19 -0500927 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500928 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500929 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500930 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500931 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400932 if (used != len)
933 return;
Chuck Levere136d092006-12-05 16:35:23 -0500934 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400935 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500936 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400937 dprintk("RPC: reading %s XID %08x\n",
938 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
939 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500940 ntohl(transport->tcp_xid));
941 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400942}
943
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400944static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
945 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400946{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400947 size_t len, used;
948 u32 offset;
949 __be32 calldir;
950
951 /*
952 * We want transport->tcp_offset to be 8 at the end of this routine
953 * (4 bytes for the xid and 4 bytes for the call/reply flag).
954 * When this function is called for the first time,
955 * transport->tcp_offset is 4 (after having already read the xid).
956 */
957 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
958 len = sizeof(calldir) - offset;
959 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
960 used = xdr_skb_read_bits(desc, &calldir, len);
961 transport->tcp_offset += used;
962 if (used != len)
963 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400964 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
965 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400966 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400967 /*
968 * We don't yet have the XDR buffer, so we will write the calldir
969 * out after we get the buffer from the 'struct rpc_rqst'
970 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400971 if (ntohl(calldir) == RPC_REPLY)
972 transport->tcp_flags |= TCP_RPC_REPLY;
973 else
974 transport->tcp_flags &= ~TCP_RPC_REPLY;
975 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
976 (transport->tcp_flags & TCP_RPC_REPLY) ?
977 "reply for" : "request with", calldir);
978 xs_tcp_check_fraghdr(transport);
979}
980
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400981static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
982 struct xdr_skb_reader *desc,
983 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400984{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400985 struct sock_xprt *transport =
986 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400987 struct xdr_buf *rcvbuf;
988 size_t len;
989 ssize_t r;
990
Chuck Levera246b012005-08-11 16:25:23 -0400991 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400992
993 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
994 /*
995 * Save the RPC direction in the XDR buffer
996 */
997 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
998 htonl(RPC_REPLY) : 0;
999
1000 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1001 &calldir, sizeof(calldir));
1002 transport->tcp_copied += sizeof(calldir);
1003 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001004 }
1005
Chuck Levera246b012005-08-11 16:25:23 -04001006 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001007 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001008 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001009
Chuck Lever51971132006-12-05 16:35:19 -05001010 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001011 memcpy(&my_desc, desc, sizeof(my_desc));
1012 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001013 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001014 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001015 desc->count -= r;
1016 desc->offset += r;
1017 } else
Chuck Lever51971132006-12-05 16:35:19 -05001018 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001019 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001020
1021 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001022 transport->tcp_copied += r;
1023 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001024 }
1025 if (r != len) {
1026 /* Error when copying to the receive buffer,
1027 * usually because we weren't able to allocate
1028 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001029 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001030 * will not receive any additional updates,
1031 * and time out.
1032 * Any remaining data from this record will
1033 * be discarded.
1034 */
Chuck Levere136d092006-12-05 16:35:23 -05001035 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001036 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001037 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001038 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1039 "tcp_offset = %u, tcp_reclen = %u\n",
1040 xprt, transport->tcp_copied,
1041 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001042 return;
Chuck Levera246b012005-08-11 16:25:23 -04001043 }
1044
Chuck Lever46121cf2007-01-31 12:14:08 -05001045 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001046 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001047 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1048 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1049 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001050
Chuck Lever51971132006-12-05 16:35:19 -05001051 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001052 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001053 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001054 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1055 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001056 }
1057
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001058 return;
1059}
1060
1061/*
1062 * Finds the request corresponding to the RPC xid and invokes the common
1063 * tcp read code to read the data.
1064 */
1065static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1066 struct xdr_skb_reader *desc)
1067{
1068 struct sock_xprt *transport =
1069 container_of(xprt, struct sock_xprt, xprt);
1070 struct rpc_rqst *req;
1071
1072 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1073
1074 /* Find and lock the request corresponding to this xid */
1075 spin_lock(&xprt->transport_lock);
1076 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1077 if (!req) {
1078 dprintk("RPC: XID %08x request not found!\n",
1079 ntohl(transport->tcp_xid));
1080 spin_unlock(&xprt->transport_lock);
1081 return -1;
1082 }
1083
1084 xs_tcp_read_common(xprt, desc, req);
1085
Chuck Levere136d092006-12-05 16:35:23 -05001086 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001087 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001088
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001089 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001090 return 0;
1091}
1092
1093#if defined(CONFIG_NFS_V4_1)
1094/*
1095 * Obtains an rpc_rqst previously allocated and invokes the common
1096 * tcp read code to read the data. The result is placed in the callback
1097 * queue.
1098 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1099 * connection and return -1.
1100 */
1101static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1102 struct xdr_skb_reader *desc)
1103{
1104 struct sock_xprt *transport =
1105 container_of(xprt, struct sock_xprt, xprt);
1106 struct rpc_rqst *req;
1107
1108 req = xprt_alloc_bc_request(xprt);
1109 if (req == NULL) {
1110 printk(KERN_WARNING "Callback slot table overflowed\n");
1111 xprt_force_disconnect(xprt);
1112 return -1;
1113 }
1114
1115 req->rq_xid = transport->tcp_xid;
1116 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1117 xs_tcp_read_common(xprt, desc, req);
1118
1119 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1120 struct svc_serv *bc_serv = xprt->bc_serv;
1121
1122 /*
1123 * Add callback request to callback list. The callback
1124 * service sleeps on the sv_cb_waitq waiting for new
1125 * requests. Wake it up after adding enqueing the
1126 * request.
1127 */
1128 dprintk("RPC: add callback request to list\n");
1129 spin_lock(&bc_serv->sv_cb_lock);
1130 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1131 spin_unlock(&bc_serv->sv_cb_lock);
1132 wake_up(&bc_serv->sv_cb_waitq);
1133 }
1134
1135 req->rq_private_buf.len = transport->tcp_copied;
1136
1137 return 0;
1138}
1139
1140static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1141 struct xdr_skb_reader *desc)
1142{
1143 struct sock_xprt *transport =
1144 container_of(xprt, struct sock_xprt, xprt);
1145
1146 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1147 xs_tcp_read_reply(xprt, desc) :
1148 xs_tcp_read_callback(xprt, desc);
1149}
1150#else
1151static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1152 struct xdr_skb_reader *desc)
1153{
1154 return xs_tcp_read_reply(xprt, desc);
1155}
1156#endif /* CONFIG_NFS_V4_1 */
1157
1158/*
1159 * Read data off the transport. This can be either an RPC_CALL or an
1160 * RPC_REPLY. Relay the processing to helper functions.
1161 */
1162static void xs_tcp_read_data(struct rpc_xprt *xprt,
1163 struct xdr_skb_reader *desc)
1164{
1165 struct sock_xprt *transport =
1166 container_of(xprt, struct sock_xprt, xprt);
1167
1168 if (_xs_tcp_read_data(xprt, desc) == 0)
1169 xs_tcp_check_fraghdr(transport);
1170 else {
1171 /*
1172 * The transport_lock protects the request handling.
1173 * There's no need to hold it to update the tcp_flags.
1174 */
1175 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1176 }
Chuck Levera246b012005-08-11 16:25:23 -04001177}
1178
Chuck Leverdd456472006-12-05 16:35:44 -05001179static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001180{
1181 size_t len;
1182
Chuck Lever51971132006-12-05 16:35:19 -05001183 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001184 if (len > desc->count)
1185 len = desc->count;
1186 desc->count -= len;
1187 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001188 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001189 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001190 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001191}
1192
Chuck Lever9903cd12005-08-11 16:25:26 -04001193static 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 -04001194{
1195 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001196 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001197 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001198 .skb = skb,
1199 .offset = offset,
1200 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001201 };
Chuck Levera246b012005-08-11 16:25:23 -04001202
Chuck Lever46121cf2007-01-31 12:14:08 -05001203 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001204 do {
1205 /* Read in a new fragment marker if necessary */
1206 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001207 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001208 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001209 continue;
1210 }
1211 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001212 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001213 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001214 continue;
1215 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001216 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001217 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001218 xs_tcp_read_calldir(transport, &desc);
1219 continue;
1220 }
Chuck Levera246b012005-08-11 16:25:23 -04001221 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001222 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001223 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001224 continue;
1225 }
1226 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001227 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001228 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001229 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001230 return len - desc.count;
1231}
1232
Chuck Lever9903cd12005-08-11 16:25:26 -04001233/**
1234 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1235 * @sk: socket with data to read
1236 * @bytes: how much data to read
1237 *
1238 */
1239static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001240{
1241 struct rpc_xprt *xprt;
1242 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001243 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001244
Chuck Lever46121cf2007-01-31 12:14:08 -05001245 dprintk("RPC: xs_tcp_data_ready...\n");
1246
Chuck Levera246b012005-08-11 16:25:23 -04001247 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001248 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001249 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001250 if (xprt->shutdown)
1251 goto out;
1252
Neil Brown61d0a8e2009-09-23 14:36:37 -04001253 /* Any data means we had a useful conversation, so
1254 * the we don't need to delay the next reconnect
1255 */
1256 if (xprt->reestablish_timeout)
1257 xprt->reestablish_timeout = 0;
1258
Chuck Lever9903cd12005-08-11 16:25:26 -04001259 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001260 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001261 do {
1262 rd_desc.count = 65536;
1263 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1264 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001265out:
1266 read_unlock(&sk->sk_callback_lock);
1267}
1268
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001269/*
1270 * Do the equivalent of linger/linger2 handling for dealing with
1271 * broken servers that don't close the socket in a timely
1272 * fashion
1273 */
1274static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1275 unsigned long timeout)
1276{
1277 struct sock_xprt *transport;
1278
1279 if (xprt_test_and_set_connecting(xprt))
1280 return;
1281 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1282 transport = container_of(xprt, struct sock_xprt, xprt);
1283 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1284 timeout);
1285}
1286
1287static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1288{
1289 struct sock_xprt *transport;
1290
1291 transport = container_of(xprt, struct sock_xprt, xprt);
1292
1293 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1294 !cancel_delayed_work(&transport->connect_worker))
1295 return;
1296 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1297 xprt_clear_connecting(xprt);
1298}
1299
1300static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1301{
1302 smp_mb__before_clear_bit();
1303 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1304 clear_bit(XPRT_CLOSING, &xprt->state);
1305 smp_mb__after_clear_bit();
1306 /* Mark transport as closed and wake up all pending tasks */
1307 xprt_disconnect_done(xprt);
1308}
1309
Chuck Lever9903cd12005-08-11 16:25:26 -04001310/**
1311 * xs_tcp_state_change - callback to handle TCP socket state changes
1312 * @sk: socket whose state has changed
1313 *
1314 */
1315static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001316{
Chuck Lever9903cd12005-08-11 16:25:26 -04001317 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001318
1319 read_lock(&sk->sk_callback_lock);
1320 if (!(xprt = xprt_from_sock(sk)))
1321 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001322 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1323 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1324 sk->sk_state, xprt_connected(xprt),
1325 sock_flag(sk, SOCK_DEAD),
1326 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001327
1328 switch (sk->sk_state) {
1329 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001330 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001331 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001332 struct sock_xprt *transport = container_of(xprt,
1333 struct sock_xprt, xprt);
1334
Chuck Levera246b012005-08-11 16:25:23 -04001335 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001336 transport->tcp_offset = 0;
1337 transport->tcp_reclen = 0;
1338 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001339 transport->tcp_flags =
1340 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001341
Trond Myklebust2a491992009-03-11 14:38:00 -04001342 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001343 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001344 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001345 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001346 case TCP_FIN_WAIT1:
1347 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001348 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001349 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001350 set_bit(XPRT_CLOSING, &xprt->state);
1351 smp_mb__before_clear_bit();
1352 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001353 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001354 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001355 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001356 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001357 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001358 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001359 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001360 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001361 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001362 case TCP_CLOSING:
1363 /*
1364 * If the server closed down the connection, make sure that
1365 * we back off before reconnecting
1366 */
1367 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1368 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001369 break;
1370 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001371 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001372 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001373 smp_mb__before_clear_bit();
1374 clear_bit(XPRT_CONNECTED, &xprt->state);
1375 smp_mb__after_clear_bit();
1376 break;
1377 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001378 xs_tcp_cancel_linger_timeout(xprt);
1379 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001380 }
1381 out:
1382 read_unlock(&sk->sk_callback_lock);
1383}
1384
Chuck Lever9903cd12005-08-11 16:25:26 -04001385/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001386 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001387 * @sk: socket
1388 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001389static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001390{
1391 struct rpc_xprt *xprt;
1392
1393 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001394 if (!(xprt = xprt_from_sock(sk)))
1395 goto out;
1396 dprintk("RPC: %s client %p...\n"
1397 "RPC: error %d\n",
1398 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001399 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001400out:
1401 read_unlock(&sk->sk_callback_lock);
1402}
1403
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001404static void xs_write_space(struct sock *sk)
1405{
1406 struct socket *sock;
1407 struct rpc_xprt *xprt;
1408
1409 if (unlikely(!(sock = sk->sk_socket)))
1410 return;
1411 clear_bit(SOCK_NOSPACE, &sock->flags);
1412
1413 if (unlikely(!(xprt = xprt_from_sock(sk))))
1414 return;
1415 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1416 return;
1417
1418 xprt_write_space(xprt);
1419}
1420
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001421/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001422 * xs_udp_write_space - callback invoked when socket buffer space
1423 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001424 * @sk: socket whose state has changed
1425 *
Chuck Levera246b012005-08-11 16:25:23 -04001426 * Called when more output buffer space is available for this socket.
1427 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001428 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001429 * with a bunch of small requests.
1430 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001431static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001432{
Chuck Levera246b012005-08-11 16:25:23 -04001433 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001434
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001435 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001436 if (sock_writeable(sk))
1437 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001438
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001439 read_unlock(&sk->sk_callback_lock);
1440}
Chuck Levera246b012005-08-11 16:25:23 -04001441
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001442/**
1443 * xs_tcp_write_space - callback invoked when socket buffer space
1444 * becomes available
1445 * @sk: socket whose state has changed
1446 *
1447 * Called when more output buffer space is available for this socket.
1448 * We try not to wake our writers until they can make "significant"
1449 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1450 * with a bunch of small requests.
1451 */
1452static void xs_tcp_write_space(struct sock *sk)
1453{
1454 read_lock(&sk->sk_callback_lock);
1455
1456 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001457 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1458 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001459
Chuck Levera246b012005-08-11 16:25:23 -04001460 read_unlock(&sk->sk_callback_lock);
1461}
1462
Chuck Lever470056c2005-08-25 16:25:56 -07001463static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001464{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001465 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1466 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001467
Chuck Lever7c6e0662006-12-05 16:35:30 -05001468 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001469 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001470 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001471 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001472 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001473 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001474 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001475 sk->sk_write_space(sk);
1476 }
1477}
1478
Chuck Lever43118c22005-08-25 16:25:49 -07001479/**
Chuck Lever470056c2005-08-25 16:25:56 -07001480 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001481 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001482 * @sndsize: requested size of send buffer, in bytes
1483 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001484 *
Chuck Lever470056c2005-08-25 16:25:56 -07001485 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001486 */
Chuck Lever470056c2005-08-25 16:25:56 -07001487static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001488{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001489 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1490
1491 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001492 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001493 transport->sndsize = sndsize + 1024;
1494 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001495 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001496 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001497
1498 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001499}
1500
Chuck Lever46c0ee82005-08-25 16:25:52 -07001501/**
1502 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1503 * @task: task that timed out
1504 *
1505 * Adjust the congestion window after a retransmit timeout has occurred.
1506 */
1507static void xs_udp_timer(struct rpc_task *task)
1508{
1509 xprt_adjust_cwnd(task, -ETIMEDOUT);
1510}
1511
Chuck Leverb85d8802006-05-25 01:40:49 -04001512static unsigned short xs_get_random_port(void)
1513{
1514 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1515 unsigned short rand = (unsigned short) net_random() % range;
1516 return rand + xprt_min_resvport;
1517}
1518
Chuck Lever92200412006-01-03 09:55:51 +01001519/**
1520 * xs_set_port - reset the port number in the remote endpoint address
1521 * @xprt: generic transport
1522 * @port: new port number
1523 *
1524 */
1525static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1526{
Chuck Lever46121cf2007-01-31 12:14:08 -05001527 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001528
Chuck Lever9dc3b092009-08-09 15:09:46 -04001529 rpc_set_port(xs_addr(xprt), port);
1530 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001531}
1532
Trond Myklebust67a391d2007-11-05 17:40:58 -05001533static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1534{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001535 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001536
1537 if (port == 0 && transport->xprt.resvport)
1538 port = xs_get_random_port();
1539 return port;
1540}
1541
1542static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1543{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001544 if (transport->srcport != 0)
1545 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001546 if (!transport->xprt.resvport)
1547 return 0;
1548 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1549 return xprt_max_resvport;
1550 return --port;
1551}
1552
Chuck Lever7dc753f2007-08-06 11:57:28 -04001553static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001554{
1555 struct sockaddr_in myaddr = {
1556 .sin_family = AF_INET,
1557 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001558 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001559 int err, nloop = 0;
1560 unsigned short port = xs_get_srcport(transport, sock);
1561 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001562
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001563 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001564 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001565 do {
1566 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001567 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001568 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001569 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001570 break;
Chuck Levera246b012005-08-11 16:25:23 -04001571 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001572 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001573 break;
Chuck Levera246b012005-08-11 16:25:23 -04001574 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001575 last = port;
1576 port = xs_next_srcport(transport, sock, port);
1577 if (port > last)
1578 nloop++;
1579 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001580 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1581 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001582 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001583 return err;
1584}
1585
Chuck Lever90058d32007-08-06 11:57:33 -04001586static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1587{
1588 struct sockaddr_in6 myaddr = {
1589 .sin6_family = AF_INET6,
1590 };
1591 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001592 int err, nloop = 0;
1593 unsigned short port = xs_get_srcport(transport, sock);
1594 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001595
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001596 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001597 myaddr.sin6_addr = sa->sin6_addr;
1598 do {
1599 myaddr.sin6_port = htons(port);
1600 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1601 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001602 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001603 break;
1604 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001605 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001606 break;
1607 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001608 last = port;
1609 port = xs_next_srcport(transport, sock, port);
1610 if (port > last)
1611 nloop++;
1612 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001613 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001614 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001615 return err;
1616}
1617
Peter Zijlstraed075362006-12-06 20:35:24 -08001618#ifdef CONFIG_DEBUG_LOCK_ALLOC
1619static struct lock_class_key xs_key[2];
1620static struct lock_class_key xs_slock_key[2];
1621
Chuck Lever8945ee52007-08-06 11:58:04 -04001622static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001623{
1624 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001625
John Heffner02b3d342007-09-12 10:42:12 +02001626 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001627 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1628 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1629}
Peter Zijlstraed075362006-12-06 20:35:24 -08001630
Chuck Lever8945ee52007-08-06 11:58:04 -04001631static inline void xs_reclassify_socket6(struct socket *sock)
1632{
1633 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001634
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001635 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001636 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1637 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001638}
1639#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001640static inline void xs_reclassify_socket4(struct socket *sock)
1641{
1642}
1643
1644static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001645{
1646}
1647#endif
1648
Chuck Lever16be2d22007-08-06 11:57:38 -04001649static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001650{
Chuck Lever16be2d22007-08-06 11:57:38 -04001651 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001652
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001653 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001654 struct sock *sk = sock->sk;
1655
1656 write_lock_bh(&sk->sk_callback_lock);
1657
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001658 xs_save_old_callbacks(transport, sk);
1659
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001660 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001661 sk->sk_data_ready = xs_udp_data_ready;
1662 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001663 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001664 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001665 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001666
1667 xprt_set_connected(xprt);
1668
1669 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001670 transport->sock = sock;
1671 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001672
1673 write_unlock_bh(&sk->sk_callback_lock);
1674 }
Chuck Lever470056c2005-08-25 16:25:56 -07001675 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001676}
1677
Chuck Levera246b012005-08-11 16:25:23 -04001678/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001679 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001680 * @work: RPC transport to connect
1681 *
1682 * Invoked by a work queue tasklet.
1683 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001684static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001685{
1686 struct sock_xprt *transport =
1687 container_of(work, struct sock_xprt, connect_worker.work);
1688 struct rpc_xprt *xprt = &transport->xprt;
1689 struct socket *sock = transport->sock;
1690 int err, status = -EIO;
1691
Trond Myklebust01d37c42009-03-11 14:09:39 -04001692 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001693 goto out;
1694
Chuck Levera246b012005-08-11 16:25:23 -04001695 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001696 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001697
Chuck Leverfe315e72009-03-11 14:10:21 -04001698 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1699 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001700 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001701 goto out;
1702 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001703 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001704
Chuck Lever7dc753f2007-08-06 11:57:28 -04001705 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001706 sock_release(sock);
1707 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001708 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001709
Chuck Leverc740eff2009-08-09 15:09:46 -04001710 dprintk("RPC: worker connecting xprt %p via %s to "
1711 "%s (port %s)\n", xprt,
1712 xprt->address_strings[RPC_DISPLAY_PROTO],
1713 xprt->address_strings[RPC_DISPLAY_ADDR],
1714 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001715
Chuck Lever16be2d22007-08-06 11:57:38 -04001716 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001717 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001718out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001719 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001720 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001721}
1722
Chuck Lever68e220b2007-08-06 11:57:48 -04001723/**
1724 * xs_udp_connect_worker6 - set up a UDP socket
1725 * @work: RPC transport to connect
1726 *
1727 * Invoked by a work queue tasklet.
1728 */
1729static void xs_udp_connect_worker6(struct work_struct *work)
1730{
1731 struct sock_xprt *transport =
1732 container_of(work, struct sock_xprt, connect_worker.work);
1733 struct rpc_xprt *xprt = &transport->xprt;
1734 struct socket *sock = transport->sock;
1735 int err, status = -EIO;
1736
Trond Myklebust01d37c42009-03-11 14:09:39 -04001737 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001738 goto out;
1739
1740 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001741 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001742
Chuck Leverfe315e72009-03-11 14:10:21 -04001743 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1744 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001745 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1746 goto out;
1747 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001748 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001749
1750 if (xs_bind6(transport, sock) < 0) {
1751 sock_release(sock);
1752 goto out;
1753 }
1754
Chuck Leverc740eff2009-08-09 15:09:46 -04001755 dprintk("RPC: worker connecting xprt %p via %s to "
1756 "%s (port %s)\n", xprt,
1757 xprt->address_strings[RPC_DISPLAY_PROTO],
1758 xprt->address_strings[RPC_DISPLAY_ADDR],
1759 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001760
1761 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001762 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001763out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001764 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001765 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001766}
1767
Chuck Lever3167e122005-08-25 16:25:55 -07001768/*
1769 * We need to preserve the port number so the reply cache on the server can
1770 * find our cached RPC replies when we get around to reconnecting.
1771 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001772static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001773{
1774 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001775 struct sockaddr any;
1776
Chuck Lever46121cf2007-01-31 12:14:08 -05001777 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001778
1779 /*
1780 * Disconnect the transport socket by doing a connect operation
1781 * with AF_UNSPEC. This should return immediately...
1782 */
1783 memset(&any, 0, sizeof(any));
1784 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001785 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001786 if (!result)
1787 xs_sock_mark_closed(xprt);
1788 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001789 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001790 result);
1791}
1792
Trond Myklebust40d25492009-03-11 14:37:58 -04001793static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1794{
1795 unsigned int state = transport->inet->sk_state;
1796
1797 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1798 return;
1799 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1800 return;
1801 xs_abort_connection(xprt, transport);
1802}
1803
Chuck Lever16be2d22007-08-06 11:57:38 -04001804static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001805{
Chuck Lever16be2d22007-08-06 11:57:38 -04001806 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001807
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001808 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001809 struct sock *sk = sock->sk;
1810
1811 write_lock_bh(&sk->sk_callback_lock);
1812
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001813 xs_save_old_callbacks(transport, sk);
1814
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001815 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001816 sk->sk_data_ready = xs_tcp_data_ready;
1817 sk->sk_state_change = xs_tcp_state_change;
1818 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001819 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001820 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001821
1822 /* socket options */
1823 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1824 sock_reset_flag(sk, SOCK_LINGER);
1825 tcp_sk(sk)->linger2 = 0;
1826 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001827
1828 xprt_clear_connected(xprt);
1829
1830 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001831 transport->sock = sock;
1832 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001833
1834 write_unlock_bh(&sk->sk_callback_lock);
1835 }
1836
Trond Myklebust01d37c42009-03-11 14:09:39 -04001837 if (!xprt_bound(xprt))
1838 return -ENOTCONN;
1839
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001840 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001841 xprt->stat.connect_count++;
1842 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001843 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001844}
1845
1846/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001847 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1848 * @xprt: RPC transport to connect
1849 * @transport: socket transport to connect
1850 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001851 *
1852 * Invoked by a work queue tasklet.
1853 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001854static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1855 struct sock_xprt *transport,
1856 struct socket *(*create_sock)(struct rpc_xprt *,
1857 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001858{
Chuck Lever16be2d22007-08-06 11:57:38 -04001859 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001860 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001861
Trond Myklebust01d37c42009-03-11 14:09:39 -04001862 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001863 goto out;
1864
1865 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001866 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001867 sock = create_sock(xprt, transport);
1868 if (IS_ERR(sock)) {
1869 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001870 goto out;
1871 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001872 } else {
1873 int abort_and_exit;
1874
1875 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1876 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001877 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001878 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001879
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001880 if (abort_and_exit)
1881 goto out_eagain;
1882 }
1883
Chuck Leverc740eff2009-08-09 15:09:46 -04001884 dprintk("RPC: worker connecting xprt %p via %s to "
1885 "%s (port %s)\n", xprt,
1886 xprt->address_strings[RPC_DISPLAY_PROTO],
1887 xprt->address_strings[RPC_DISPLAY_ADDR],
1888 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001889
1890 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001891 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1892 xprt, -status, xprt_connected(xprt),
1893 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001894 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001895 default:
1896 printk("%s: connect returned unhandled error %d\n",
1897 __func__, status);
1898 case -EADDRNOTAVAIL:
1899 /* We're probably in TIME_WAIT. Get rid of existing socket,
1900 * and retry
1901 */
1902 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1903 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001904 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001905 case -ECONNREFUSED:
1906 case -ECONNRESET:
1907 case -ENETUNREACH:
1908 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001909 case 0:
1910 case -EINPROGRESS:
1911 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001912 xprt_clear_connecting(xprt);
1913 return;
Chuck Levera246b012005-08-11 16:25:23 -04001914 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001915out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001916 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001917out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001918 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001919 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001920}
1921
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001922static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1923 struct sock_xprt *transport)
1924{
1925 struct socket *sock;
1926 int err;
1927
1928 /* start from scratch */
1929 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1930 if (err < 0) {
1931 dprintk("RPC: can't create TCP transport socket (%d).\n",
1932 -err);
1933 goto out_err;
1934 }
1935 xs_reclassify_socket4(sock);
1936
1937 if (xs_bind4(transport, sock) < 0) {
1938 sock_release(sock);
1939 goto out_err;
1940 }
1941 return sock;
1942out_err:
1943 return ERR_PTR(-EIO);
1944}
1945
1946/**
Chuck Levera246b012005-08-11 16:25:23 -04001947 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1948 * @work: RPC transport to connect
1949 *
1950 * Invoked by a work queue tasklet.
1951 */
1952static void xs_tcp_connect_worker4(struct work_struct *work)
1953{
1954 struct sock_xprt *transport =
1955 container_of(work, struct sock_xprt, connect_worker.work);
1956 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001957
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001958 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1959}
Chuck Levera246b012005-08-11 16:25:23 -04001960
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001961static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1962 struct sock_xprt *transport)
1963{
1964 struct socket *sock;
1965 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001966
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001967 /* start from scratch */
1968 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1969 if (err < 0) {
1970 dprintk("RPC: can't create TCP transport socket (%d).\n",
1971 -err);
1972 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001973 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001974 xs_reclassify_socket6(sock);
1975
1976 if (xs_bind6(transport, sock) < 0) {
1977 sock_release(sock);
1978 goto out_err;
1979 }
1980 return sock;
1981out_err:
1982 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001983}
1984
Chuck Lever9903cd12005-08-11 16:25:26 -04001985/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001986 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1987 * @work: RPC transport to connect
1988 *
1989 * Invoked by a work queue tasklet.
1990 */
1991static void xs_tcp_connect_worker6(struct work_struct *work)
1992{
1993 struct sock_xprt *transport =
1994 container_of(work, struct sock_xprt, connect_worker.work);
1995 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001996
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001997 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001998}
1999
2000/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002001 * xs_connect - connect a socket to a remote endpoint
2002 * @task: address of RPC task that manages state of connect request
2003 *
2004 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002005 *
2006 * UDP socket connects are synchronous, but we use a work queue anyway
2007 * to guarantee that even unprivileged user processes can set up a
2008 * socket on a privileged port.
2009 *
2010 * If a UDP socket connect fails, the delay behavior here prevents
2011 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002012 */
2013static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002014{
2015 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002016 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002017
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002018 if (xprt_test_and_set_connecting(xprt))
2019 return;
2020
Chuck Lever09a21c42009-12-03 15:58:56 -05002021 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002022 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2023 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002024 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002025 queue_delayed_work(rpciod_workqueue,
2026 &transport->connect_worker,
2027 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002028 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002029 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2030 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002031 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2032 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002033 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002034 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002035 queue_delayed_work(rpciod_workqueue,
2036 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002037 }
2038}
2039
Trond Myklebuste06799f2007-11-05 15:44:12 -05002040static void xs_tcp_connect(struct rpc_task *task)
2041{
2042 struct rpc_xprt *xprt = task->tk_xprt;
2043
Trond Myklebuste06799f2007-11-05 15:44:12 -05002044 /* Exit if we need to wait for socket shutdown to complete */
2045 if (test_bit(XPRT_CLOSING, &xprt->state))
2046 return;
2047 xs_connect(task);
2048}
2049
Chuck Lever262ca072006-03-20 13:44:16 -05002050/**
2051 * xs_udp_print_stats - display UDP socket-specifc stats
2052 * @xprt: rpc_xprt struct containing statistics
2053 * @seq: output file
2054 *
2055 */
2056static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2057{
Chuck Leverc8475462006-12-05 16:35:26 -05002058 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2059
Chuck Lever262ca072006-03-20 13:44:16 -05002060 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002061 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002062 xprt->stat.bind_count,
2063 xprt->stat.sends,
2064 xprt->stat.recvs,
2065 xprt->stat.bad_xids,
2066 xprt->stat.req_u,
2067 xprt->stat.bklog_u);
2068}
2069
2070/**
2071 * xs_tcp_print_stats - display TCP socket-specifc stats
2072 * @xprt: rpc_xprt struct containing statistics
2073 * @seq: output file
2074 *
2075 */
2076static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2077{
Chuck Leverc8475462006-12-05 16:35:26 -05002078 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002079 long idle_time = 0;
2080
2081 if (xprt_connected(xprt))
2082 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2083
2084 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002085 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002086 xprt->stat.bind_count,
2087 xprt->stat.connect_count,
2088 xprt->stat.connect_time,
2089 idle_time,
2090 xprt->stat.sends,
2091 xprt->stat.recvs,
2092 xprt->stat.bad_xids,
2093 xprt->stat.req_u,
2094 xprt->stat.bklog_u);
2095}
2096
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002097/*
2098 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2099 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2100 * to use the server side send routines.
2101 */
2102void *bc_malloc(struct rpc_task *task, size_t size)
2103{
2104 struct page *page;
2105 struct rpc_buffer *buf;
2106
2107 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2108 page = alloc_page(GFP_KERNEL);
2109
2110 if (!page)
2111 return NULL;
2112
2113 buf = page_address(page);
2114 buf->len = PAGE_SIZE;
2115
2116 return buf->data;
2117}
2118
2119/*
2120 * Free the space allocated in the bc_alloc routine
2121 */
2122void bc_free(void *buffer)
2123{
2124 struct rpc_buffer *buf;
2125
2126 if (!buffer)
2127 return;
2128
2129 buf = container_of(buffer, struct rpc_buffer, data);
2130 free_page((unsigned long)buf);
2131}
2132
2133/*
2134 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2135 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2136 */
2137static int bc_sendto(struct rpc_rqst *req)
2138{
2139 int len;
2140 struct xdr_buf *xbufp = &req->rq_snd_buf;
2141 struct rpc_xprt *xprt = req->rq_xprt;
2142 struct sock_xprt *transport =
2143 container_of(xprt, struct sock_xprt, xprt);
2144 struct socket *sock = transport->sock;
2145 unsigned long headoff;
2146 unsigned long tailoff;
2147
2148 /*
2149 * Set up the rpc header and record marker stuff
2150 */
2151 xs_encode_tcp_record_marker(xbufp);
2152
2153 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2154 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2155 len = svc_send_common(sock, xbufp,
2156 virt_to_page(xbufp->head[0].iov_base), headoff,
2157 xbufp->tail[0].iov_base, tailoff);
2158
2159 if (len != xbufp->len) {
2160 printk(KERN_NOTICE "Error sending entire callback!\n");
2161 len = -EAGAIN;
2162 }
2163
2164 return len;
2165}
2166
2167/*
2168 * The send routine. Borrows from svc_send
2169 */
2170static int bc_send_request(struct rpc_task *task)
2171{
2172 struct rpc_rqst *req = task->tk_rqstp;
2173 struct svc_xprt *xprt;
2174 struct svc_sock *svsk;
2175 u32 len;
2176
2177 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2178 /*
2179 * Get the server socket associated with this callback xprt
2180 */
2181 xprt = req->rq_xprt->bc_xprt;
2182 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2183
2184 /*
2185 * Grab the mutex to serialize data as the connection is shared
2186 * with the fore channel
2187 */
2188 if (!mutex_trylock(&xprt->xpt_mutex)) {
2189 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2190 if (!mutex_trylock(&xprt->xpt_mutex))
2191 return -EAGAIN;
2192 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2193 }
2194 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2195 len = -ENOTCONN;
2196 else
2197 len = bc_sendto(req);
2198 mutex_unlock(&xprt->xpt_mutex);
2199
2200 if (len > 0)
2201 len = 0;
2202
2203 return len;
2204}
2205
2206/*
2207 * The close routine. Since this is client initiated, we do nothing
2208 */
2209
2210static void bc_close(struct rpc_xprt *xprt)
2211{
2212 return;
2213}
2214
2215/*
2216 * The xprt destroy routine. Again, because this connection is client
2217 * initiated, we do nothing
2218 */
2219
2220static void bc_destroy(struct rpc_xprt *xprt)
2221{
2222 return;
2223}
2224
Chuck Lever262965f2005-08-11 16:25:56 -04002225static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002226 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002227 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002228 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002229 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002230 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002231 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002232 .buf_alloc = rpc_malloc,
2233 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002234 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002235 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002236 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002237 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002238 .close = xs_close,
2239 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002240 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002241};
2242
2243static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002244 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002245 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002246 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002247 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002248 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002249 .buf_alloc = rpc_malloc,
2250 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002251 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002252 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Ricardo Labiaga0d90ba12009-04-01 09:23:04 -04002253#if defined(CONFIG_NFS_V4_1)
2254 .release_request = bc_release_request,
2255#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustf75e6742009-04-21 17:18:20 -04002256 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002257 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002258 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002259};
2260
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002261/*
2262 * The rpc_xprt_ops for the server backchannel
2263 */
2264
2265static struct rpc_xprt_ops bc_tcp_ops = {
2266 .reserve_xprt = xprt_reserve_xprt,
2267 .release_xprt = xprt_release_xprt,
2268 .buf_alloc = bc_malloc,
2269 .buf_free = bc_free,
2270 .send_request = bc_send_request,
2271 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2272 .close = bc_close,
2273 .destroy = bc_destroy,
2274 .print_stats = xs_tcp_print_stats,
2275};
2276
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002277static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002278 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002279{
2280 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002281 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002282
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002283 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002284 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002285 return ERR_PTR(-EBADF);
2286 }
2287
Chuck Leverffc2e512006-12-05 16:35:11 -05002288 new = kzalloc(sizeof(*new), GFP_KERNEL);
2289 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002290 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2291 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002292 return ERR_PTR(-ENOMEM);
2293 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002294 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002295
2296 xprt->max_reqs = slot_table_size;
2297 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2298 if (xprt->slot == NULL) {
2299 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002300 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2301 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002302 return ERR_PTR(-ENOMEM);
2303 }
2304
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002305 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2306 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002307 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002308 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002309
2310 return xprt;
2311}
2312
Trond Myklebust2881ae72007-12-20 16:03:54 -05002313static const struct rpc_timeout xs_udp_default_timeout = {
2314 .to_initval = 5 * HZ,
2315 .to_maxval = 30 * HZ,
2316 .to_increment = 5 * HZ,
2317 .to_retries = 5,
2318};
2319
Chuck Lever9903cd12005-08-11 16:25:26 -04002320/**
2321 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002322 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002323 *
2324 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002325static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002326{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002327 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002328 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002329 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002330
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002331 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002332 if (IS_ERR(xprt))
2333 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002334 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002335
Chuck Leverec739ef2006-08-22 20:06:15 -04002336 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002337 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002338 /* XXX: header size can vary due to auth type, IPv6, etc. */
2339 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2340
Chuck Lever03bf4b72005-08-25 16:25:55 -07002341 xprt->bind_timeout = XS_BIND_TO;
2342 xprt->connect_timeout = XS_UDP_CONN_TO;
2343 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2344 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002345
Chuck Lever262965f2005-08-11 16:25:56 -04002346 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002347
Trond Myklebustba7392b2007-12-20 16:03:55 -05002348 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002349
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002350 switch (addr->sa_family) {
2351 case AF_INET:
2352 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2353 xprt_set_bound(xprt);
2354
2355 INIT_DELAYED_WORK(&transport->connect_worker,
2356 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002357 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002358 break;
2359 case AF_INET6:
2360 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2361 xprt_set_bound(xprt);
2362
2363 INIT_DELAYED_WORK(&transport->connect_worker,
2364 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002365 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002366 break;
2367 default:
2368 kfree(xprt);
2369 return ERR_PTR(-EAFNOSUPPORT);
2370 }
2371
Chuck Leverc740eff2009-08-09 15:09:46 -04002372 if (xprt_bound(xprt))
2373 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2374 xprt->address_strings[RPC_DISPLAY_ADDR],
2375 xprt->address_strings[RPC_DISPLAY_PORT],
2376 xprt->address_strings[RPC_DISPLAY_PROTO]);
2377 else
2378 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2379 xprt->address_strings[RPC_DISPLAY_ADDR],
2380 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002381
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002382 if (try_module_get(THIS_MODULE))
2383 return xprt;
2384
2385 kfree(xprt->slot);
2386 kfree(xprt);
2387 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002388}
2389
Trond Myklebust2881ae72007-12-20 16:03:54 -05002390static const struct rpc_timeout xs_tcp_default_timeout = {
2391 .to_initval = 60 * HZ,
2392 .to_maxval = 60 * HZ,
2393 .to_retries = 2,
2394};
2395
Chuck Lever9903cd12005-08-11 16:25:26 -04002396/**
2397 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002398 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002399 *
2400 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002401static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002402{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002403 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002404 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002405 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002406
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002407 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002408 if (IS_ERR(xprt))
2409 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002410 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002411
Chuck Leverec739ef2006-08-22 20:06:15 -04002412 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002413 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002414 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002415
Chuck Lever03bf4b72005-08-25 16:25:55 -07002416 xprt->bind_timeout = XS_BIND_TO;
2417 xprt->connect_timeout = XS_TCP_CONN_TO;
2418 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2419 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002420
Chuck Lever262965f2005-08-11 16:25:56 -04002421 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002422 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002423
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002424 switch (addr->sa_family) {
2425 case AF_INET:
2426 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2427 xprt_set_bound(xprt);
2428
Chuck Lever9dc3b092009-08-09 15:09:46 -04002429 INIT_DELAYED_WORK(&transport->connect_worker,
2430 xs_tcp_connect_worker4);
2431 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002432 break;
2433 case AF_INET6:
2434 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2435 xprt_set_bound(xprt);
2436
Chuck Lever9dc3b092009-08-09 15:09:46 -04002437 INIT_DELAYED_WORK(&transport->connect_worker,
2438 xs_tcp_connect_worker6);
2439 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002440 break;
2441 default:
2442 kfree(xprt);
2443 return ERR_PTR(-EAFNOSUPPORT);
2444 }
2445
Chuck Leverc740eff2009-08-09 15:09:46 -04002446 if (xprt_bound(xprt))
2447 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2448 xprt->address_strings[RPC_DISPLAY_ADDR],
2449 xprt->address_strings[RPC_DISPLAY_PORT],
2450 xprt->address_strings[RPC_DISPLAY_PROTO]);
2451 else
2452 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2453 xprt->address_strings[RPC_DISPLAY_ADDR],
2454 xprt->address_strings[RPC_DISPLAY_PROTO]);
2455
Chuck Leveredb267a2006-08-22 20:06:18 -04002456
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002457 if (try_module_get(THIS_MODULE))
2458 return xprt;
2459
2460 kfree(xprt->slot);
2461 kfree(xprt);
2462 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002463}
Chuck Lever282b32e2006-12-05 16:35:51 -05002464
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002465/**
2466 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2467 * @args: rpc transport creation arguments
2468 *
2469 */
2470static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2471{
2472 struct sockaddr *addr = args->dstaddr;
2473 struct rpc_xprt *xprt;
2474 struct sock_xprt *transport;
2475 struct svc_sock *bc_sock;
2476
2477 if (!args->bc_xprt)
2478 ERR_PTR(-EINVAL);
2479
2480 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2481 if (IS_ERR(xprt))
2482 return xprt;
2483 transport = container_of(xprt, struct sock_xprt, xprt);
2484
2485 xprt->prot = IPPROTO_TCP;
2486 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2487 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2488 xprt->timeout = &xs_tcp_default_timeout;
2489
2490 /* backchannel */
2491 xprt_set_bound(xprt);
2492 xprt->bind_timeout = 0;
2493 xprt->connect_timeout = 0;
2494 xprt->reestablish_timeout = 0;
2495 xprt->idle_timeout = 0;
2496
2497 /*
2498 * The backchannel uses the same socket connection as the
2499 * forechannel
2500 */
2501 xprt->bc_xprt = args->bc_xprt;
2502 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2503 bc_sock->sk_bc_xprt = xprt;
2504 transport->sock = bc_sock->sk_sock;
2505 transport->inet = bc_sock->sk_sk;
2506
2507 xprt->ops = &bc_tcp_ops;
2508
2509 switch (addr->sa_family) {
2510 case AF_INET:
2511 xs_format_peer_addresses(xprt, "tcp",
2512 RPCBIND_NETID_TCP);
2513 break;
2514 case AF_INET6:
2515 xs_format_peer_addresses(xprt, "tcp",
2516 RPCBIND_NETID_TCP6);
2517 break;
2518 default:
2519 kfree(xprt);
2520 return ERR_PTR(-EAFNOSUPPORT);
2521 }
2522
2523 if (xprt_bound(xprt))
2524 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2525 xprt->address_strings[RPC_DISPLAY_ADDR],
2526 xprt->address_strings[RPC_DISPLAY_PORT],
2527 xprt->address_strings[RPC_DISPLAY_PROTO]);
2528 else
2529 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2530 xprt->address_strings[RPC_DISPLAY_ADDR],
2531 xprt->address_strings[RPC_DISPLAY_PROTO]);
2532
2533 /*
2534 * Since we don't want connections for the backchannel, we set
2535 * the xprt status to connected
2536 */
2537 xprt_set_connected(xprt);
2538
2539
2540 if (try_module_get(THIS_MODULE))
2541 return xprt;
2542 kfree(xprt->slot);
2543 kfree(xprt);
2544 return ERR_PTR(-EINVAL);
2545}
2546
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002547static struct xprt_class xs_udp_transport = {
2548 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2549 .name = "udp",
2550 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002551 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002552 .setup = xs_setup_udp,
2553};
2554
2555static struct xprt_class xs_tcp_transport = {
2556 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2557 .name = "tcp",
2558 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002559 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002560 .setup = xs_setup_tcp,
2561};
2562
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002563static struct xprt_class xs_bc_tcp_transport = {
2564 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2565 .name = "tcp NFSv4.1 backchannel",
2566 .owner = THIS_MODULE,
2567 .ident = XPRT_TRANSPORT_BC_TCP,
2568 .setup = xs_setup_bc_tcp,
2569};
2570
Chuck Lever282b32e2006-12-05 16:35:51 -05002571/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002572 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002573 *
2574 */
2575int init_socket_xprt(void)
2576{
Chuck Leverfbf76682006-12-05 16:35:54 -05002577#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002578 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002579 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002580#endif
2581
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002582 xprt_register_transport(&xs_udp_transport);
2583 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002584 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002585
Chuck Lever282b32e2006-12-05 16:35:51 -05002586 return 0;
2587}
2588
2589/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002590 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002591 *
2592 */
2593void cleanup_socket_xprt(void)
2594{
Chuck Leverfbf76682006-12-05 16:35:54 -05002595#ifdef RPC_DEBUG
2596 if (sunrpc_table_header) {
2597 unregister_sysctl_table(sunrpc_table_header);
2598 sunrpc_table_header = NULL;
2599 }
2600#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002601
2602 xprt_unregister_transport(&xs_udp_transport);
2603 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002604 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002605}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002606
2607static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2608 unsigned int min, unsigned int max)
2609{
2610 unsigned long num;
2611 int ret;
2612
2613 if (!val)
2614 return -EINVAL;
2615 ret = strict_strtoul(val, 0, &num);
2616 if (ret == -EINVAL || num < min || num > max)
2617 return -EINVAL;
2618 *((unsigned int *)kp->arg) = num;
2619 return 0;
2620}
2621
2622static int param_set_portnr(const char *val, struct kernel_param *kp)
2623{
2624 return param_set_uint_minmax(val, kp,
2625 RPC_MIN_RESVPORT,
2626 RPC_MAX_RESVPORT);
2627}
2628
2629static int param_get_portnr(char *buffer, struct kernel_param *kp)
2630{
2631 return param_get_uint(buffer, kp);
2632}
2633#define param_check_portnr(name, p) \
2634 __param_check(name, p, unsigned int);
2635
2636module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2637module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2638
2639static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2640{
2641 return param_set_uint_minmax(val, kp,
2642 RPC_MIN_SLOT_TABLE,
2643 RPC_MAX_SLOT_TABLE);
2644}
2645
2646static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2647{
2648 return param_get_uint(buffer, kp);
2649}
2650#define param_check_slot_table_size(name, p) \
2651 __param_check(name, p, unsigned int);
2652
2653module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2654 slot_table_size, 0644);
2655module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2656 slot_table_size, 0644);
2657