blob: d138afa3bb350e877045313902af3b560077bfc4 [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 Perchesfc0b5792010-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 Perchesfc0b5792010-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 }
Chuck Levera246b012005-08-11 16:25:23 -0400551
Chuck Lever262965f2005-08-11 16:25:56 -0400552 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400553 case -ENOTSOCK:
554 status = -ENOTCONN;
555 /* Should we call xs_close() here? */
556 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400557 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400558 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400559 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400560 default:
561 dprintk("RPC: sendmsg returned unrecognized error %d\n",
562 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400563 case -ENETUNREACH:
564 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400565 case -ECONNREFUSED:
566 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400567 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400568 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400569 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800570
Chuck Lever262965f2005-08-11 16:25:56 -0400571 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400572}
573
Trond Myklebuste06799f2007-11-05 15:44:12 -0500574/**
575 * xs_tcp_shutdown - gracefully shut down a TCP socket
576 * @xprt: transport
577 *
578 * Initiates a graceful shutdown of the TCP socket by calling the
579 * equivalent of shutdown(SHUT_WR);
580 */
581static void xs_tcp_shutdown(struct rpc_xprt *xprt)
582{
583 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
584 struct socket *sock = transport->sock;
585
586 if (sock != NULL)
587 kernel_sock_shutdown(sock, SHUT_WR);
588}
589
Chuck Lever808012f2005-08-25 16:25:49 -0700590static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
591{
592 u32 reclen = buf->len - sizeof(rpc_fraghdr);
593 rpc_fraghdr *base = buf->head[0].iov_base;
594 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
595}
596
Chuck Lever9903cd12005-08-11 16:25:26 -0400597/**
Chuck Lever262965f2005-08-11 16:25:56 -0400598 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400599 * @task: address of RPC task that manages the state of an RPC request
600 *
601 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400602 * 0: The request has been sent
603 * EAGAIN: The socket was blocked, please call again later to
604 * complete the request
605 * ENOTCONN: Caller needs to invoke connect logic then call again
606 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400607 *
608 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400609 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400610 */
Chuck Lever262965f2005-08-11 16:25:56 -0400611static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400612{
613 struct rpc_rqst *req = task->tk_rqstp;
614 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500615 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400616 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400617 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400618
Chuck Lever808012f2005-08-25 16:25:49 -0700619 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400620
Chuck Lever262965f2005-08-11 16:25:56 -0400621 xs_pktdump("packet data:",
622 req->rq_svec->iov_base,
623 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400624
625 /* Continue transmitting the packet/record. We must be careful
626 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400627 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400628 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500629 status = xs_sendpages(transport->sock,
630 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400631
Chuck Lever46121cf2007-01-31 12:14:08 -0500632 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400633 xdr->len - req->rq_bytes_sent, status);
634
635 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400636 break;
637
Chuck Lever262965f2005-08-11 16:25:56 -0400638 /* If we've sent the entire packet, immediately
639 * reset the count of bytes sent. */
640 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500641 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400642 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
643 req->rq_bytes_sent = 0;
644 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400645 }
646
Trond Myklebust06b4b682008-04-16 16:51:38 -0400647 if (status != 0)
648 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400649 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400650 break;
Chuck Levera246b012005-08-11 16:25:23 -0400651 }
652
Chuck Lever262965f2005-08-11 16:25:56 -0400653 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400654 case -ENOTSOCK:
655 status = -ENOTCONN;
656 /* Should we call xs_close() here? */
657 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400658 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400659 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400660 break;
661 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500662 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400663 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400664 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400665 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500666 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400667 case -ECONNREFUSED:
668 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400669 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400670 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800671
Chuck Levera246b012005-08-11 16:25:23 -0400672 return status;
673}
674
Chuck Lever9903cd12005-08-11 16:25:26 -0400675/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400676 * xs_tcp_release_xprt - clean up after a tcp transmission
677 * @xprt: transport
678 * @task: rpc task
679 *
680 * This cleans up if an error causes us to abort the transmission of a request.
681 * In this case, the socket may need to be reset in order to avoid confusing
682 * the server.
683 */
684static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
685{
686 struct rpc_rqst *req;
687
688 if (task != xprt->snd_task)
689 return;
690 if (task == NULL)
691 goto out_release;
692 req = task->tk_rqstp;
693 if (req->rq_bytes_sent == 0)
694 goto out_release;
695 if (req->rq_bytes_sent == req->rq_snd_buf.len)
696 goto out_release;
697 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
698out_release:
699 xprt_release_xprt(xprt, task);
700}
701
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400702static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
703{
704 transport->old_data_ready = sk->sk_data_ready;
705 transport->old_state_change = sk->sk_state_change;
706 transport->old_write_space = sk->sk_write_space;
707 transport->old_error_report = sk->sk_error_report;
708}
709
710static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
711{
712 sk->sk_data_ready = transport->old_data_ready;
713 sk->sk_state_change = transport->old_state_change;
714 sk->sk_write_space = transport->old_write_space;
715 sk->sk_error_report = transport->old_error_report;
716}
717
Chuck Leverfe315e72009-03-11 14:10:21 -0400718static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400719{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500720 struct socket *sock = transport->sock;
721 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400722
Chuck Leverfe315e72009-03-11 14:10:21 -0400723 if (sk == NULL)
724 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400725
Chuck Levera246b012005-08-11 16:25:23 -0400726 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500727 transport->inet = NULL;
728 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400729
Chuck Lever9903cd12005-08-11 16:25:26 -0400730 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400731
732 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400733 write_unlock_bh(&sk->sk_callback_lock);
734
Chuck Lever9903cd12005-08-11 16:25:26 -0400735 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400736
737 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400738}
739
740/**
741 * xs_close - close a socket
742 * @xprt: transport
743 *
744 * This is used when all requests are complete; ie, no DRC state remains
745 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400746 *
747 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
748 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400749 */
750static void xs_close(struct rpc_xprt *xprt)
751{
752 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
753
754 dprintk("RPC: xs_close xprt %p\n", xprt);
755
756 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400757 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400758
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100759 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400760 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100761 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500762 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100763 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500764 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400765}
766
Trond Myklebustf75e6742009-04-21 17:18:20 -0400767static void xs_tcp_close(struct rpc_xprt *xprt)
768{
769 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
770 xs_close(xprt);
771 else
772 xs_tcp_shutdown(xprt);
773}
774
Chuck Lever9903cd12005-08-11 16:25:26 -0400775/**
776 * xs_destroy - prepare to shutdown a transport
777 * @xprt: doomed transport
778 *
779 */
780static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400781{
Chuck Leverc8475462006-12-05 16:35:26 -0500782 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
783
Chuck Lever46121cf2007-01-31 12:14:08 -0500784 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400785
Trond Myklebustc1384c92007-06-14 18:00:42 -0400786 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400787
Chuck Lever9903cd12005-08-11 16:25:26 -0400788 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400789 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400790 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400791 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400792 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400793}
794
Chuck Lever9903cd12005-08-11 16:25:26 -0400795static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400796{
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 return (struct rpc_xprt *) sk->sk_user_data;
798}
799
800/**
801 * xs_udp_data_ready - "data ready" callback for UDP sockets
802 * @sk: socket with data to read
803 * @len: how much data to read
804 *
805 */
806static void xs_udp_data_ready(struct sock *sk, int len)
807{
808 struct rpc_task *task;
809 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400810 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400812 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700813 u32 _xid;
814 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400815
816 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500817 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400818 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400819 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400820
821 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
822 goto out;
823
824 if (xprt->shutdown)
825 goto dropit;
826
827 repsize = skb->len - sizeof(struct udphdr);
828 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500829 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400830 goto dropit;
831 }
832
833 /* Copy the XID from the skb... */
834 xp = skb_header_pointer(skb, sizeof(struct udphdr),
835 sizeof(_xid), &_xid);
836 if (xp == NULL)
837 goto dropit;
838
839 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400840 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400841 rovr = xprt_lookup_rqst(xprt, *xp);
842 if (!rovr)
843 goto out_unlock;
844 task = rovr->rq_task;
845
Chuck Levera246b012005-08-11 16:25:23 -0400846 if ((copied = rovr->rq_private_buf.buflen) > repsize)
847 copied = repsize;
848
849 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800850 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
851 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400852 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800853 }
854
855 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400856
857 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000858 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400859
Chuck Lever1570c1e2005-08-25 16:25:52 -0700860 xprt_adjust_cwnd(task, copied);
861 xprt_update_rtt(task);
862 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400863
864 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400865 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400866 dropit:
867 skb_free_datagram(sk, skb);
868 out:
869 read_unlock(&sk->sk_callback_lock);
870}
871
Chuck Leverdd456472006-12-05 16:35:44 -0500872static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400873{
Chuck Lever51971132006-12-05 16:35:19 -0500874 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400875 size_t len, used;
876 char *p;
877
Chuck Lever51971132006-12-05 16:35:19 -0500878 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
879 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500880 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500881 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400882 if (used != len)
883 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700884
Chuck Lever51971132006-12-05 16:35:19 -0500885 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
886 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500887 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400888 else
Chuck Levere136d092006-12-05 16:35:23 -0500889 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500890 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700891
Chuck Levere136d092006-12-05 16:35:23 -0500892 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500893 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700894
Chuck Levera246b012005-08-11 16:25:23 -0400895 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400896 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500897 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500898 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400899 return;
Chuck Levera246b012005-08-11 16:25:23 -0400900 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500901 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500902 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400903}
904
Chuck Lever51971132006-12-05 16:35:19 -0500905static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400906{
Chuck Lever51971132006-12-05 16:35:19 -0500907 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500908 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500909 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500910 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
911 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
912 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500913 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400914 }
915 }
916}
917
Chuck Leverdd456472006-12-05 16:35:44 -0500918static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400919{
920 size_t len, used;
921 char *p;
922
Chuck Lever51971132006-12-05 16:35:19 -0500923 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500924 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500925 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500926 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500927 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400928 if (used != len)
929 return;
Chuck Levere136d092006-12-05 16:35:23 -0500930 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400931 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500932 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400933 dprintk("RPC: reading %s XID %08x\n",
934 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
935 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500936 ntohl(transport->tcp_xid));
937 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400938}
939
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400940static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
941 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400942{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400943 size_t len, used;
944 u32 offset;
945 __be32 calldir;
946
947 /*
948 * We want transport->tcp_offset to be 8 at the end of this routine
949 * (4 bytes for the xid and 4 bytes for the call/reply flag).
950 * When this function is called for the first time,
951 * transport->tcp_offset is 4 (after having already read the xid).
952 */
953 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
954 len = sizeof(calldir) - offset;
955 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
956 used = xdr_skb_read_bits(desc, &calldir, len);
957 transport->tcp_offset += used;
958 if (used != len)
959 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400960 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
961 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400962 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400963 /*
964 * We don't yet have the XDR buffer, so we will write the calldir
965 * out after we get the buffer from the 'struct rpc_rqst'
966 */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400967 if (ntohl(calldir) == RPC_REPLY)
968 transport->tcp_flags |= TCP_RPC_REPLY;
969 else
970 transport->tcp_flags &= ~TCP_RPC_REPLY;
971 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
972 (transport->tcp_flags & TCP_RPC_REPLY) ?
973 "reply for" : "request with", calldir);
974 xs_tcp_check_fraghdr(transport);
975}
976
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400977static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
978 struct xdr_skb_reader *desc,
979 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400980{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400981 struct sock_xprt *transport =
982 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400983 struct xdr_buf *rcvbuf;
984 size_t len;
985 ssize_t r;
986
Chuck Levera246b012005-08-11 16:25:23 -0400987 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400988
989 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
990 /*
991 * Save the RPC direction in the XDR buffer
992 */
993 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
994 htonl(RPC_REPLY) : 0;
995
996 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
997 &calldir, sizeof(calldir));
998 transport->tcp_copied += sizeof(calldir);
999 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -04001000 }
1001
Chuck Levera246b012005-08-11 16:25:23 -04001002 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001003 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001004 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001005
Chuck Lever51971132006-12-05 16:35:19 -05001006 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001007 memcpy(&my_desc, desc, sizeof(my_desc));
1008 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001009 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001010 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001011 desc->count -= r;
1012 desc->offset += r;
1013 } else
Chuck Lever51971132006-12-05 16:35:19 -05001014 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001015 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001016
1017 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001018 transport->tcp_copied += r;
1019 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001020 }
1021 if (r != len) {
1022 /* Error when copying to the receive buffer,
1023 * usually because we weren't able to allocate
1024 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001025 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001026 * will not receive any additional updates,
1027 * and time out.
1028 * Any remaining data from this record will
1029 * be discarded.
1030 */
Chuck Levere136d092006-12-05 16:35:23 -05001031 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001032 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001033 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001034 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1035 "tcp_offset = %u, tcp_reclen = %u\n",
1036 xprt, transport->tcp_copied,
1037 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001038 return;
Chuck Levera246b012005-08-11 16:25:23 -04001039 }
1040
Chuck Lever46121cf2007-01-31 12:14:08 -05001041 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001042 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001043 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1044 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1045 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001046
Chuck Lever51971132006-12-05 16:35:19 -05001047 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001048 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001049 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001050 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1051 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001052 }
1053
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001054 return;
1055}
1056
1057/*
1058 * Finds the request corresponding to the RPC xid and invokes the common
1059 * tcp read code to read the data.
1060 */
1061static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1062 struct xdr_skb_reader *desc)
1063{
1064 struct sock_xprt *transport =
1065 container_of(xprt, struct sock_xprt, xprt);
1066 struct rpc_rqst *req;
1067
1068 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1069
1070 /* Find and lock the request corresponding to this xid */
1071 spin_lock(&xprt->transport_lock);
1072 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1073 if (!req) {
1074 dprintk("RPC: XID %08x request not found!\n",
1075 ntohl(transport->tcp_xid));
1076 spin_unlock(&xprt->transport_lock);
1077 return -1;
1078 }
1079
1080 xs_tcp_read_common(xprt, desc, req);
1081
Chuck Levere136d092006-12-05 16:35:23 -05001082 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001083 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001084
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001085 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001086 return 0;
1087}
1088
1089#if defined(CONFIG_NFS_V4_1)
1090/*
1091 * Obtains an rpc_rqst previously allocated and invokes the common
1092 * tcp read code to read the data. The result is placed in the callback
1093 * queue.
1094 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1095 * connection and return -1.
1096 */
1097static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1098 struct xdr_skb_reader *desc)
1099{
1100 struct sock_xprt *transport =
1101 container_of(xprt, struct sock_xprt, xprt);
1102 struct rpc_rqst *req;
1103
1104 req = xprt_alloc_bc_request(xprt);
1105 if (req == NULL) {
1106 printk(KERN_WARNING "Callback slot table overflowed\n");
1107 xprt_force_disconnect(xprt);
1108 return -1;
1109 }
1110
1111 req->rq_xid = transport->tcp_xid;
1112 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1113 xs_tcp_read_common(xprt, desc, req);
1114
1115 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1116 struct svc_serv *bc_serv = xprt->bc_serv;
1117
1118 /*
1119 * Add callback request to callback list. The callback
1120 * service sleeps on the sv_cb_waitq waiting for new
1121 * requests. Wake it up after adding enqueing the
1122 * request.
1123 */
1124 dprintk("RPC: add callback request to list\n");
1125 spin_lock(&bc_serv->sv_cb_lock);
1126 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1127 spin_unlock(&bc_serv->sv_cb_lock);
1128 wake_up(&bc_serv->sv_cb_waitq);
1129 }
1130
1131 req->rq_private_buf.len = transport->tcp_copied;
1132
1133 return 0;
1134}
1135
1136static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1137 struct xdr_skb_reader *desc)
1138{
1139 struct sock_xprt *transport =
1140 container_of(xprt, struct sock_xprt, xprt);
1141
1142 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1143 xs_tcp_read_reply(xprt, desc) :
1144 xs_tcp_read_callback(xprt, desc);
1145}
1146#else
1147static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1148 struct xdr_skb_reader *desc)
1149{
1150 return xs_tcp_read_reply(xprt, desc);
1151}
1152#endif /* CONFIG_NFS_V4_1 */
1153
1154/*
1155 * Read data off the transport. This can be either an RPC_CALL or an
1156 * RPC_REPLY. Relay the processing to helper functions.
1157 */
1158static void xs_tcp_read_data(struct rpc_xprt *xprt,
1159 struct xdr_skb_reader *desc)
1160{
1161 struct sock_xprt *transport =
1162 container_of(xprt, struct sock_xprt, xprt);
1163
1164 if (_xs_tcp_read_data(xprt, desc) == 0)
1165 xs_tcp_check_fraghdr(transport);
1166 else {
1167 /*
1168 * The transport_lock protects the request handling.
1169 * There's no need to hold it to update the tcp_flags.
1170 */
1171 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1172 }
Chuck Levera246b012005-08-11 16:25:23 -04001173}
1174
Chuck Leverdd456472006-12-05 16:35:44 -05001175static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001176{
1177 size_t len;
1178
Chuck Lever51971132006-12-05 16:35:19 -05001179 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001180 if (len > desc->count)
1181 len = desc->count;
1182 desc->count -= len;
1183 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001184 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001185 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001186 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001187}
1188
Chuck Lever9903cd12005-08-11 16:25:26 -04001189static 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 -04001190{
1191 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001192 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001193 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001194 .skb = skb,
1195 .offset = offset,
1196 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001197 };
Chuck Levera246b012005-08-11 16:25:23 -04001198
Chuck Lever46121cf2007-01-31 12:14:08 -05001199 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001200 do {
1201 /* Read in a new fragment marker if necessary */
1202 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001203 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001204 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001205 continue;
1206 }
1207 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001208 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001209 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001210 continue;
1211 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001212 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001213 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001214 xs_tcp_read_calldir(transport, &desc);
1215 continue;
1216 }
Chuck Levera246b012005-08-11 16:25:23 -04001217 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001218 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001219 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001220 continue;
1221 }
1222 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001223 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001224 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001225 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001226 return len - desc.count;
1227}
1228
Chuck Lever9903cd12005-08-11 16:25:26 -04001229/**
1230 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1231 * @sk: socket with data to read
1232 * @bytes: how much data to read
1233 *
1234 */
1235static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001236{
1237 struct rpc_xprt *xprt;
1238 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001239 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001240
Chuck Lever46121cf2007-01-31 12:14:08 -05001241 dprintk("RPC: xs_tcp_data_ready...\n");
1242
Chuck Levera246b012005-08-11 16:25:23 -04001243 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001244 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001245 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001246 if (xprt->shutdown)
1247 goto out;
1248
Neil Brown61d0a8e2009-09-23 14:36:37 -04001249 /* Any data means we had a useful conversation, so
1250 * the we don't need to delay the next reconnect
1251 */
1252 if (xprt->reestablish_timeout)
1253 xprt->reestablish_timeout = 0;
1254
Chuck Lever9903cd12005-08-11 16:25:26 -04001255 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001256 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001257 do {
1258 rd_desc.count = 65536;
1259 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1260 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001261out:
1262 read_unlock(&sk->sk_callback_lock);
1263}
1264
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001265/*
1266 * Do the equivalent of linger/linger2 handling for dealing with
1267 * broken servers that don't close the socket in a timely
1268 * fashion
1269 */
1270static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1271 unsigned long timeout)
1272{
1273 struct sock_xprt *transport;
1274
1275 if (xprt_test_and_set_connecting(xprt))
1276 return;
1277 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1278 transport = container_of(xprt, struct sock_xprt, xprt);
1279 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1280 timeout);
1281}
1282
1283static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1284{
1285 struct sock_xprt *transport;
1286
1287 transport = container_of(xprt, struct sock_xprt, xprt);
1288
1289 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1290 !cancel_delayed_work(&transport->connect_worker))
1291 return;
1292 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1293 xprt_clear_connecting(xprt);
1294}
1295
1296static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1297{
1298 smp_mb__before_clear_bit();
1299 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1300 clear_bit(XPRT_CLOSING, &xprt->state);
1301 smp_mb__after_clear_bit();
1302 /* Mark transport as closed and wake up all pending tasks */
1303 xprt_disconnect_done(xprt);
1304}
1305
Chuck Lever9903cd12005-08-11 16:25:26 -04001306/**
1307 * xs_tcp_state_change - callback to handle TCP socket state changes
1308 * @sk: socket whose state has changed
1309 *
1310 */
1311static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001312{
Chuck Lever9903cd12005-08-11 16:25:26 -04001313 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001314
1315 read_lock(&sk->sk_callback_lock);
1316 if (!(xprt = xprt_from_sock(sk)))
1317 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001318 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1319 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1320 sk->sk_state, xprt_connected(xprt),
1321 sock_flag(sk, SOCK_DEAD),
1322 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001323
1324 switch (sk->sk_state) {
1325 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001326 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001327 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001328 struct sock_xprt *transport = container_of(xprt,
1329 struct sock_xprt, xprt);
1330
Chuck Levera246b012005-08-11 16:25:23 -04001331 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001332 transport->tcp_offset = 0;
1333 transport->tcp_reclen = 0;
1334 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001335 transport->tcp_flags =
1336 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001337
Trond Myklebust2a491992009-03-11 14:38:00 -04001338 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001339 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001340 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001341 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001342 case TCP_FIN_WAIT1:
1343 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001344 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001345 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001346 set_bit(XPRT_CLOSING, &xprt->state);
1347 smp_mb__before_clear_bit();
1348 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001349 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001350 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001351 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001352 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001353 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001354 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001355 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001356 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001357 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001358 case TCP_CLOSING:
1359 /*
1360 * If the server closed down the connection, make sure that
1361 * we back off before reconnecting
1362 */
1363 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1364 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001365 break;
1366 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001367 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001368 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001369 smp_mb__before_clear_bit();
1370 clear_bit(XPRT_CONNECTED, &xprt->state);
1371 smp_mb__after_clear_bit();
1372 break;
1373 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001374 xs_tcp_cancel_linger_timeout(xprt);
1375 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001376 }
1377 out:
1378 read_unlock(&sk->sk_callback_lock);
1379}
1380
Chuck Lever9903cd12005-08-11 16:25:26 -04001381/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001382 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001383 * @sk: socket
1384 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001385static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001386{
1387 struct rpc_xprt *xprt;
1388
1389 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001390 if (!(xprt = xprt_from_sock(sk)))
1391 goto out;
1392 dprintk("RPC: %s client %p...\n"
1393 "RPC: error %d\n",
1394 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001395 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001396out:
1397 read_unlock(&sk->sk_callback_lock);
1398}
1399
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001400static void xs_write_space(struct sock *sk)
1401{
1402 struct socket *sock;
1403 struct rpc_xprt *xprt;
1404
1405 if (unlikely(!(sock = sk->sk_socket)))
1406 return;
1407 clear_bit(SOCK_NOSPACE, &sock->flags);
1408
1409 if (unlikely(!(xprt = xprt_from_sock(sk))))
1410 return;
1411 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1412 return;
1413
1414 xprt_write_space(xprt);
1415}
1416
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001417/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001418 * xs_udp_write_space - callback invoked when socket buffer space
1419 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001420 * @sk: socket whose state has changed
1421 *
Chuck Levera246b012005-08-11 16:25:23 -04001422 * Called when more output buffer space is available for this socket.
1423 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001424 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001425 * with a bunch of small requests.
1426 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001427static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001428{
Chuck Levera246b012005-08-11 16:25:23 -04001429 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001430
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001431 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001432 if (sock_writeable(sk))
1433 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001434
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001435 read_unlock(&sk->sk_callback_lock);
1436}
Chuck Levera246b012005-08-11 16:25:23 -04001437
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001438/**
1439 * xs_tcp_write_space - callback invoked when socket buffer space
1440 * becomes available
1441 * @sk: socket whose state has changed
1442 *
1443 * Called when more output buffer space is available for this socket.
1444 * We try not to wake our writers until they can make "significant"
1445 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1446 * with a bunch of small requests.
1447 */
1448static void xs_tcp_write_space(struct sock *sk)
1449{
1450 read_lock(&sk->sk_callback_lock);
1451
1452 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001453 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1454 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001455
Chuck Levera246b012005-08-11 16:25:23 -04001456 read_unlock(&sk->sk_callback_lock);
1457}
1458
Chuck Lever470056c2005-08-25 16:25:56 -07001459static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001460{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001461 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1462 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001463
Chuck Lever7c6e0662006-12-05 16:35:30 -05001464 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001465 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001466 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001467 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001468 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001469 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001470 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001471 sk->sk_write_space(sk);
1472 }
1473}
1474
Chuck Lever43118c22005-08-25 16:25:49 -07001475/**
Chuck Lever470056c2005-08-25 16:25:56 -07001476 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001477 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001478 * @sndsize: requested size of send buffer, in bytes
1479 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001480 *
Chuck Lever470056c2005-08-25 16:25:56 -07001481 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001482 */
Chuck Lever470056c2005-08-25 16:25:56 -07001483static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001484{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001485 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1486
1487 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001488 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001489 transport->sndsize = sndsize + 1024;
1490 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001491 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001492 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001493
1494 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001495}
1496
Chuck Lever46c0ee82005-08-25 16:25:52 -07001497/**
1498 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1499 * @task: task that timed out
1500 *
1501 * Adjust the congestion window after a retransmit timeout has occurred.
1502 */
1503static void xs_udp_timer(struct rpc_task *task)
1504{
1505 xprt_adjust_cwnd(task, -ETIMEDOUT);
1506}
1507
Chuck Leverb85d8802006-05-25 01:40:49 -04001508static unsigned short xs_get_random_port(void)
1509{
1510 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1511 unsigned short rand = (unsigned short) net_random() % range;
1512 return rand + xprt_min_resvport;
1513}
1514
Chuck Lever92200412006-01-03 09:55:51 +01001515/**
1516 * xs_set_port - reset the port number in the remote endpoint address
1517 * @xprt: generic transport
1518 * @port: new port number
1519 *
1520 */
1521static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1522{
Chuck Lever46121cf2007-01-31 12:14:08 -05001523 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001524
Chuck Lever9dc3b092009-08-09 15:09:46 -04001525 rpc_set_port(xs_addr(xprt), port);
1526 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001527}
1528
Trond Myklebust67a391d2007-11-05 17:40:58 -05001529static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1530{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001531 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001532
1533 if (port == 0 && transport->xprt.resvport)
1534 port = xs_get_random_port();
1535 return port;
1536}
1537
1538static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1539{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001540 if (transport->srcport != 0)
1541 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001542 if (!transport->xprt.resvport)
1543 return 0;
1544 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1545 return xprt_max_resvport;
1546 return --port;
1547}
1548
Chuck Lever7dc753f2007-08-06 11:57:28 -04001549static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001550{
1551 struct sockaddr_in myaddr = {
1552 .sin_family = AF_INET,
1553 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001554 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001555 int err, nloop = 0;
1556 unsigned short port = xs_get_srcport(transport, sock);
1557 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001558
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001559 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001560 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001561 do {
1562 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001563 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001564 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001565 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001566 break;
Chuck Levera246b012005-08-11 16:25:23 -04001567 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001568 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001569 break;
Chuck Levera246b012005-08-11 16:25:23 -04001570 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001571 last = port;
1572 port = xs_next_srcport(transport, sock, port);
1573 if (port > last)
1574 nloop++;
1575 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001576 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1577 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001578 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001579 return err;
1580}
1581
Chuck Lever90058d32007-08-06 11:57:33 -04001582static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1583{
1584 struct sockaddr_in6 myaddr = {
1585 .sin6_family = AF_INET6,
1586 };
1587 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001588 int err, nloop = 0;
1589 unsigned short port = xs_get_srcport(transport, sock);
1590 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001591
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001592 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001593 myaddr.sin6_addr = sa->sin6_addr;
1594 do {
1595 myaddr.sin6_port = htons(port);
1596 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1597 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001598 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001599 break;
1600 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001601 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001602 break;
1603 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001604 last = port;
1605 port = xs_next_srcport(transport, sock, port);
1606 if (port > last)
1607 nloop++;
1608 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001609 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001610 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001611 return err;
1612}
1613
Peter Zijlstraed075362006-12-06 20:35:24 -08001614#ifdef CONFIG_DEBUG_LOCK_ALLOC
1615static struct lock_class_key xs_key[2];
1616static struct lock_class_key xs_slock_key[2];
1617
Chuck Lever8945ee52007-08-06 11:58:04 -04001618static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001619{
1620 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001621
John Heffner02b3d342007-09-12 10:42:12 +02001622 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001623 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1624 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1625}
Peter Zijlstraed075362006-12-06 20:35:24 -08001626
Chuck Lever8945ee52007-08-06 11:58:04 -04001627static inline void xs_reclassify_socket6(struct socket *sock)
1628{
1629 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001630
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001631 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001632 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1633 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001634}
1635#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001636static inline void xs_reclassify_socket4(struct socket *sock)
1637{
1638}
1639
1640static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001641{
1642}
1643#endif
1644
Chuck Lever16be2d22007-08-06 11:57:38 -04001645static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001646{
Chuck Lever16be2d22007-08-06 11:57:38 -04001647 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001648
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001649 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001650 struct sock *sk = sock->sk;
1651
1652 write_lock_bh(&sk->sk_callback_lock);
1653
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001654 xs_save_old_callbacks(transport, sk);
1655
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001656 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001657 sk->sk_data_ready = xs_udp_data_ready;
1658 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001659 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001660 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001661 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001662
1663 xprt_set_connected(xprt);
1664
1665 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001666 transport->sock = sock;
1667 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001668
1669 write_unlock_bh(&sk->sk_callback_lock);
1670 }
Chuck Lever470056c2005-08-25 16:25:56 -07001671 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001672}
1673
Chuck Levera246b012005-08-11 16:25:23 -04001674/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001675 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001676 * @work: RPC transport to connect
1677 *
1678 * Invoked by a work queue tasklet.
1679 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001680static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001681{
1682 struct sock_xprt *transport =
1683 container_of(work, struct sock_xprt, connect_worker.work);
1684 struct rpc_xprt *xprt = &transport->xprt;
1685 struct socket *sock = transport->sock;
1686 int err, status = -EIO;
1687
Trond Myklebust01d37c42009-03-11 14:09:39 -04001688 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001689 goto out;
1690
Chuck Levera246b012005-08-11 16:25:23 -04001691 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001692 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001693
Chuck Leverfe315e72009-03-11 14:10:21 -04001694 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1695 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001696 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001697 goto out;
1698 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001699 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001700
Chuck Lever7dc753f2007-08-06 11:57:28 -04001701 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001702 sock_release(sock);
1703 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001704 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001705
Chuck Leverc740eff2009-08-09 15:09:46 -04001706 dprintk("RPC: worker connecting xprt %p via %s to "
1707 "%s (port %s)\n", xprt,
1708 xprt->address_strings[RPC_DISPLAY_PROTO],
1709 xprt->address_strings[RPC_DISPLAY_ADDR],
1710 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001711
Chuck Lever16be2d22007-08-06 11:57:38 -04001712 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001713 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001714out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001715 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001716 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001717}
1718
Chuck Lever68e220b2007-08-06 11:57:48 -04001719/**
1720 * xs_udp_connect_worker6 - set up a UDP socket
1721 * @work: RPC transport to connect
1722 *
1723 * Invoked by a work queue tasklet.
1724 */
1725static void xs_udp_connect_worker6(struct work_struct *work)
1726{
1727 struct sock_xprt *transport =
1728 container_of(work, struct sock_xprt, connect_worker.work);
1729 struct rpc_xprt *xprt = &transport->xprt;
1730 struct socket *sock = transport->sock;
1731 int err, status = -EIO;
1732
Trond Myklebust01d37c42009-03-11 14:09:39 -04001733 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001734 goto out;
1735
1736 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001737 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001738
Chuck Leverfe315e72009-03-11 14:10:21 -04001739 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1740 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001741 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1742 goto out;
1743 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001744 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001745
1746 if (xs_bind6(transport, sock) < 0) {
1747 sock_release(sock);
1748 goto out;
1749 }
1750
Chuck Leverc740eff2009-08-09 15:09:46 -04001751 dprintk("RPC: worker connecting xprt %p via %s to "
1752 "%s (port %s)\n", xprt,
1753 xprt->address_strings[RPC_DISPLAY_PROTO],
1754 xprt->address_strings[RPC_DISPLAY_ADDR],
1755 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever68e220b2007-08-06 11:57:48 -04001756
1757 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001758 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001759out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001760 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001761 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001762}
1763
Chuck Lever3167e122005-08-25 16:25:55 -07001764/*
1765 * We need to preserve the port number so the reply cache on the server can
1766 * find our cached RPC replies when we get around to reconnecting.
1767 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001768static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001769{
1770 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001771 struct sockaddr any;
1772
Chuck Lever46121cf2007-01-31 12:14:08 -05001773 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001774
1775 /*
1776 * Disconnect the transport socket by doing a connect operation
1777 * with AF_UNSPEC. This should return immediately...
1778 */
1779 memset(&any, 0, sizeof(any));
1780 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001781 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001782 if (!result)
1783 xs_sock_mark_closed(xprt);
1784 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001785 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001786 result);
1787}
1788
Trond Myklebust40d25492009-03-11 14:37:58 -04001789static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1790{
1791 unsigned int state = transport->inet->sk_state;
1792
1793 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1794 return;
1795 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1796 return;
1797 xs_abort_connection(xprt, transport);
1798}
1799
Chuck Lever16be2d22007-08-06 11:57:38 -04001800static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001801{
Chuck Lever16be2d22007-08-06 11:57:38 -04001802 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001803
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001804 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001805 struct sock *sk = sock->sk;
1806
1807 write_lock_bh(&sk->sk_callback_lock);
1808
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001809 xs_save_old_callbacks(transport, sk);
1810
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001811 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001812 sk->sk_data_ready = xs_tcp_data_ready;
1813 sk->sk_state_change = xs_tcp_state_change;
1814 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001815 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001816 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001817
1818 /* socket options */
1819 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1820 sock_reset_flag(sk, SOCK_LINGER);
1821 tcp_sk(sk)->linger2 = 0;
1822 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001823
1824 xprt_clear_connected(xprt);
1825
1826 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001827 transport->sock = sock;
1828 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001829
1830 write_unlock_bh(&sk->sk_callback_lock);
1831 }
1832
Trond Myklebust01d37c42009-03-11 14:09:39 -04001833 if (!xprt_bound(xprt))
1834 return -ENOTCONN;
1835
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001836 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001837 xprt->stat.connect_count++;
1838 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001839 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001840}
1841
1842/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001843 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1844 * @xprt: RPC transport to connect
1845 * @transport: socket transport to connect
1846 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001847 *
1848 * Invoked by a work queue tasklet.
1849 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001850static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1851 struct sock_xprt *transport,
1852 struct socket *(*create_sock)(struct rpc_xprt *,
1853 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001854{
Chuck Lever16be2d22007-08-06 11:57:38 -04001855 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001856 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001857
Trond Myklebust01d37c42009-03-11 14:09:39 -04001858 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001859 goto out;
1860
1861 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001862 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001863 sock = create_sock(xprt, transport);
1864 if (IS_ERR(sock)) {
1865 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001866 goto out;
1867 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001868 } else {
1869 int abort_and_exit;
1870
1871 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1872 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001873 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001874 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001875
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001876 if (abort_and_exit)
1877 goto out_eagain;
1878 }
1879
Chuck Leverc740eff2009-08-09 15:09:46 -04001880 dprintk("RPC: worker connecting xprt %p via %s to "
1881 "%s (port %s)\n", xprt,
1882 xprt->address_strings[RPC_DISPLAY_PROTO],
1883 xprt->address_strings[RPC_DISPLAY_ADDR],
1884 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001885
1886 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001887 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1888 xprt, -status, xprt_connected(xprt),
1889 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001890 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001891 default:
1892 printk("%s: connect returned unhandled error %d\n",
1893 __func__, status);
1894 case -EADDRNOTAVAIL:
1895 /* We're probably in TIME_WAIT. Get rid of existing socket,
1896 * and retry
1897 */
1898 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1899 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001900 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001901 case -ECONNREFUSED:
1902 case -ECONNRESET:
1903 case -ENETUNREACH:
1904 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001905 case 0:
1906 case -EINPROGRESS:
1907 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001908 xprt_clear_connecting(xprt);
1909 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05001910 case -EINVAL:
1911 /* Happens, for instance, if the user specified a link
1912 * local IPv6 address without a scope-id.
1913 */
1914 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001915 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001916out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001917 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001918out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001919 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001920 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001921}
1922
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001923static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1924 struct sock_xprt *transport)
1925{
1926 struct socket *sock;
1927 int err;
1928
1929 /* start from scratch */
1930 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1931 if (err < 0) {
1932 dprintk("RPC: can't create TCP transport socket (%d).\n",
1933 -err);
1934 goto out_err;
1935 }
1936 xs_reclassify_socket4(sock);
1937
1938 if (xs_bind4(transport, sock) < 0) {
1939 sock_release(sock);
1940 goto out_err;
1941 }
1942 return sock;
1943out_err:
1944 return ERR_PTR(-EIO);
1945}
1946
1947/**
Chuck Levera246b012005-08-11 16:25:23 -04001948 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1949 * @work: RPC transport to connect
1950 *
1951 * Invoked by a work queue tasklet.
1952 */
1953static void xs_tcp_connect_worker4(struct work_struct *work)
1954{
1955 struct sock_xprt *transport =
1956 container_of(work, struct sock_xprt, connect_worker.work);
1957 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001958
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001959 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1960}
Chuck Levera246b012005-08-11 16:25:23 -04001961
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001962static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1963 struct sock_xprt *transport)
1964{
1965 struct socket *sock;
1966 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001967
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001968 /* start from scratch */
1969 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1970 if (err < 0) {
1971 dprintk("RPC: can't create TCP transport socket (%d).\n",
1972 -err);
1973 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001974 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001975 xs_reclassify_socket6(sock);
1976
1977 if (xs_bind6(transport, sock) < 0) {
1978 sock_release(sock);
1979 goto out_err;
1980 }
1981 return sock;
1982out_err:
1983 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001984}
1985
Chuck Lever9903cd12005-08-11 16:25:26 -04001986/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001987 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1988 * @work: RPC transport to connect
1989 *
1990 * Invoked by a work queue tasklet.
1991 */
1992static void xs_tcp_connect_worker6(struct work_struct *work)
1993{
1994 struct sock_xprt *transport =
1995 container_of(work, struct sock_xprt, connect_worker.work);
1996 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001997
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001998 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001999}
2000
2001/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002002 * xs_connect - connect a socket to a remote endpoint
2003 * @task: address of RPC task that manages state of connect request
2004 *
2005 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002006 *
2007 * UDP socket connects are synchronous, but we use a work queue anyway
2008 * to guarantee that even unprivileged user processes can set up a
2009 * socket on a privileged port.
2010 *
2011 * If a UDP socket connect fails, the delay behavior here prevents
2012 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002013 */
2014static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002015{
2016 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002017 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002018
Chuck Lever09a21c42009-12-03 15:58:56 -05002019 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002020 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2021 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002022 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002023 queue_delayed_work(rpciod_workqueue,
2024 &transport->connect_worker,
2025 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002026 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002027 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2028 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002029 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2030 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002031 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002032 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002033 queue_delayed_work(rpciod_workqueue,
2034 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002035 }
2036}
2037
Chuck Lever262ca072006-03-20 13:44:16 -05002038/**
2039 * xs_udp_print_stats - display UDP socket-specifc stats
2040 * @xprt: rpc_xprt struct containing statistics
2041 * @seq: output file
2042 *
2043 */
2044static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2045{
Chuck Leverc8475462006-12-05 16:35:26 -05002046 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2047
Chuck Lever262ca072006-03-20 13:44:16 -05002048 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002049 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002050 xprt->stat.bind_count,
2051 xprt->stat.sends,
2052 xprt->stat.recvs,
2053 xprt->stat.bad_xids,
2054 xprt->stat.req_u,
2055 xprt->stat.bklog_u);
2056}
2057
2058/**
2059 * xs_tcp_print_stats - display TCP socket-specifc stats
2060 * @xprt: rpc_xprt struct containing statistics
2061 * @seq: output file
2062 *
2063 */
2064static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2065{
Chuck Leverc8475462006-12-05 16:35:26 -05002066 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002067 long idle_time = 0;
2068
2069 if (xprt_connected(xprt))
2070 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2071
2072 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002073 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002074 xprt->stat.bind_count,
2075 xprt->stat.connect_count,
2076 xprt->stat.connect_time,
2077 idle_time,
2078 xprt->stat.sends,
2079 xprt->stat.recvs,
2080 xprt->stat.bad_xids,
2081 xprt->stat.req_u,
2082 xprt->stat.bklog_u);
2083}
2084
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002085/*
2086 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2087 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2088 * to use the server side send routines.
2089 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002090static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002091{
2092 struct page *page;
2093 struct rpc_buffer *buf;
2094
2095 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2096 page = alloc_page(GFP_KERNEL);
2097
2098 if (!page)
2099 return NULL;
2100
2101 buf = page_address(page);
2102 buf->len = PAGE_SIZE;
2103
2104 return buf->data;
2105}
2106
2107/*
2108 * Free the space allocated in the bc_alloc routine
2109 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002110static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002111{
2112 struct rpc_buffer *buf;
2113
2114 if (!buffer)
2115 return;
2116
2117 buf = container_of(buffer, struct rpc_buffer, data);
2118 free_page((unsigned long)buf);
2119}
2120
2121/*
2122 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2123 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2124 */
2125static int bc_sendto(struct rpc_rqst *req)
2126{
2127 int len;
2128 struct xdr_buf *xbufp = &req->rq_snd_buf;
2129 struct rpc_xprt *xprt = req->rq_xprt;
2130 struct sock_xprt *transport =
2131 container_of(xprt, struct sock_xprt, xprt);
2132 struct socket *sock = transport->sock;
2133 unsigned long headoff;
2134 unsigned long tailoff;
2135
2136 /*
2137 * Set up the rpc header and record marker stuff
2138 */
2139 xs_encode_tcp_record_marker(xbufp);
2140
2141 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2142 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2143 len = svc_send_common(sock, xbufp,
2144 virt_to_page(xbufp->head[0].iov_base), headoff,
2145 xbufp->tail[0].iov_base, tailoff);
2146
2147 if (len != xbufp->len) {
2148 printk(KERN_NOTICE "Error sending entire callback!\n");
2149 len = -EAGAIN;
2150 }
2151
2152 return len;
2153}
2154
2155/*
2156 * The send routine. Borrows from svc_send
2157 */
2158static int bc_send_request(struct rpc_task *task)
2159{
2160 struct rpc_rqst *req = task->tk_rqstp;
2161 struct svc_xprt *xprt;
2162 struct svc_sock *svsk;
2163 u32 len;
2164
2165 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2166 /*
2167 * Get the server socket associated with this callback xprt
2168 */
2169 xprt = req->rq_xprt->bc_xprt;
2170 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2171
2172 /*
2173 * Grab the mutex to serialize data as the connection is shared
2174 * with the fore channel
2175 */
2176 if (!mutex_trylock(&xprt->xpt_mutex)) {
2177 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2178 if (!mutex_trylock(&xprt->xpt_mutex))
2179 return -EAGAIN;
2180 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2181 }
2182 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2183 len = -ENOTCONN;
2184 else
2185 len = bc_sendto(req);
2186 mutex_unlock(&xprt->xpt_mutex);
2187
2188 if (len > 0)
2189 len = 0;
2190
2191 return len;
2192}
2193
2194/*
2195 * The close routine. Since this is client initiated, we do nothing
2196 */
2197
2198static void bc_close(struct rpc_xprt *xprt)
2199{
2200 return;
2201}
2202
2203/*
2204 * The xprt destroy routine. Again, because this connection is client
2205 * initiated, we do nothing
2206 */
2207
2208static void bc_destroy(struct rpc_xprt *xprt)
2209{
2210 return;
2211}
2212
Chuck Lever262965f2005-08-11 16:25:56 -04002213static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002214 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002215 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002216 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002217 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002218 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002219 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002220 .buf_alloc = rpc_malloc,
2221 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002222 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002223 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002224 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002225 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002226 .close = xs_close,
2227 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002228 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002229};
2230
2231static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002232 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002233 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002234 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002235 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002236 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002237 .buf_alloc = rpc_malloc,
2238 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002239 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002240 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002241 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002242 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002243 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002244};
2245
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002246/*
2247 * The rpc_xprt_ops for the server backchannel
2248 */
2249
2250static struct rpc_xprt_ops bc_tcp_ops = {
2251 .reserve_xprt = xprt_reserve_xprt,
2252 .release_xprt = xprt_release_xprt,
2253 .buf_alloc = bc_malloc,
2254 .buf_free = bc_free,
2255 .send_request = bc_send_request,
2256 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2257 .close = bc_close,
2258 .destroy = bc_destroy,
2259 .print_stats = xs_tcp_print_stats,
2260};
2261
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002262static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002263 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002264{
2265 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002266 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002267
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002268 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002269 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002270 return ERR_PTR(-EBADF);
2271 }
2272
Chuck Leverffc2e512006-12-05 16:35:11 -05002273 new = kzalloc(sizeof(*new), GFP_KERNEL);
2274 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002275 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2276 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002277 return ERR_PTR(-ENOMEM);
2278 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002279 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002280
2281 xprt->max_reqs = slot_table_size;
2282 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2283 if (xprt->slot == NULL) {
2284 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002285 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2286 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002287 return ERR_PTR(-ENOMEM);
2288 }
2289
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002290 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2291 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002292 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002293 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002294
2295 return xprt;
2296}
2297
Trond Myklebust2881ae72007-12-20 16:03:54 -05002298static const struct rpc_timeout xs_udp_default_timeout = {
2299 .to_initval = 5 * HZ,
2300 .to_maxval = 30 * HZ,
2301 .to_increment = 5 * HZ,
2302 .to_retries = 5,
2303};
2304
Chuck Lever9903cd12005-08-11 16:25:26 -04002305/**
2306 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002307 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002308 *
2309 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002310static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002311{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002312 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002313 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002314 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002315
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002316 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002317 if (IS_ERR(xprt))
2318 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002319 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002320
Chuck Leverec739ef2006-08-22 20:06:15 -04002321 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002322 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002323 /* XXX: header size can vary due to auth type, IPv6, etc. */
2324 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2325
Chuck Lever03bf4b72005-08-25 16:25:55 -07002326 xprt->bind_timeout = XS_BIND_TO;
2327 xprt->connect_timeout = XS_UDP_CONN_TO;
2328 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2329 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002330
Chuck Lever262965f2005-08-11 16:25:56 -04002331 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002332
Trond Myklebustba7392b2007-12-20 16:03:55 -05002333 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002334
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002335 switch (addr->sa_family) {
2336 case AF_INET:
2337 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2338 xprt_set_bound(xprt);
2339
2340 INIT_DELAYED_WORK(&transport->connect_worker,
2341 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002342 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002343 break;
2344 case AF_INET6:
2345 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2346 xprt_set_bound(xprt);
2347
2348 INIT_DELAYED_WORK(&transport->connect_worker,
2349 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002350 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002351 break;
2352 default:
2353 kfree(xprt);
2354 return ERR_PTR(-EAFNOSUPPORT);
2355 }
2356
Chuck Leverc740eff2009-08-09 15:09:46 -04002357 if (xprt_bound(xprt))
2358 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2359 xprt->address_strings[RPC_DISPLAY_ADDR],
2360 xprt->address_strings[RPC_DISPLAY_PORT],
2361 xprt->address_strings[RPC_DISPLAY_PROTO]);
2362 else
2363 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2364 xprt->address_strings[RPC_DISPLAY_ADDR],
2365 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002366
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002367 if (try_module_get(THIS_MODULE))
2368 return xprt;
2369
2370 kfree(xprt->slot);
2371 kfree(xprt);
2372 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002373}
2374
Trond Myklebust2881ae72007-12-20 16:03:54 -05002375static const struct rpc_timeout xs_tcp_default_timeout = {
2376 .to_initval = 60 * HZ,
2377 .to_maxval = 60 * HZ,
2378 .to_retries = 2,
2379};
2380
Chuck Lever9903cd12005-08-11 16:25:26 -04002381/**
2382 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002383 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002384 *
2385 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002386static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002387{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002388 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002389 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002390 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002391
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002392 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002393 if (IS_ERR(xprt))
2394 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002395 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002396
Chuck Leverec739ef2006-08-22 20:06:15 -04002397 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002398 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002399 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002400
Chuck Lever03bf4b72005-08-25 16:25:55 -07002401 xprt->bind_timeout = XS_BIND_TO;
2402 xprt->connect_timeout = XS_TCP_CONN_TO;
2403 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2404 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002405
Chuck Lever262965f2005-08-11 16:25:56 -04002406 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002407 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002408
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002409 switch (addr->sa_family) {
2410 case AF_INET:
2411 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2412 xprt_set_bound(xprt);
2413
Chuck Lever9dc3b092009-08-09 15:09:46 -04002414 INIT_DELAYED_WORK(&transport->connect_worker,
2415 xs_tcp_connect_worker4);
2416 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002417 break;
2418 case AF_INET6:
2419 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2420 xprt_set_bound(xprt);
2421
Chuck Lever9dc3b092009-08-09 15:09:46 -04002422 INIT_DELAYED_WORK(&transport->connect_worker,
2423 xs_tcp_connect_worker6);
2424 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002425 break;
2426 default:
2427 kfree(xprt);
2428 return ERR_PTR(-EAFNOSUPPORT);
2429 }
2430
Chuck Leverc740eff2009-08-09 15:09:46 -04002431 if (xprt_bound(xprt))
2432 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2433 xprt->address_strings[RPC_DISPLAY_ADDR],
2434 xprt->address_strings[RPC_DISPLAY_PORT],
2435 xprt->address_strings[RPC_DISPLAY_PROTO]);
2436 else
2437 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2438 xprt->address_strings[RPC_DISPLAY_ADDR],
2439 xprt->address_strings[RPC_DISPLAY_PROTO]);
2440
Chuck Leveredb267a2006-08-22 20:06:18 -04002441
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002442 if (try_module_get(THIS_MODULE))
2443 return xprt;
2444
2445 kfree(xprt->slot);
2446 kfree(xprt);
2447 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002448}
Chuck Lever282b32e2006-12-05 16:35:51 -05002449
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002450/**
2451 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2452 * @args: rpc transport creation arguments
2453 *
2454 */
2455static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2456{
2457 struct sockaddr *addr = args->dstaddr;
2458 struct rpc_xprt *xprt;
2459 struct sock_xprt *transport;
2460 struct svc_sock *bc_sock;
2461
2462 if (!args->bc_xprt)
2463 ERR_PTR(-EINVAL);
2464
2465 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2466 if (IS_ERR(xprt))
2467 return xprt;
2468 transport = container_of(xprt, struct sock_xprt, xprt);
2469
2470 xprt->prot = IPPROTO_TCP;
2471 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2472 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2473 xprt->timeout = &xs_tcp_default_timeout;
2474
2475 /* backchannel */
2476 xprt_set_bound(xprt);
2477 xprt->bind_timeout = 0;
2478 xprt->connect_timeout = 0;
2479 xprt->reestablish_timeout = 0;
2480 xprt->idle_timeout = 0;
2481
2482 /*
2483 * The backchannel uses the same socket connection as the
2484 * forechannel
2485 */
2486 xprt->bc_xprt = args->bc_xprt;
2487 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2488 bc_sock->sk_bc_xprt = xprt;
2489 transport->sock = bc_sock->sk_sock;
2490 transport->inet = bc_sock->sk_sk;
2491
2492 xprt->ops = &bc_tcp_ops;
2493
2494 switch (addr->sa_family) {
2495 case AF_INET:
2496 xs_format_peer_addresses(xprt, "tcp",
2497 RPCBIND_NETID_TCP);
2498 break;
2499 case AF_INET6:
2500 xs_format_peer_addresses(xprt, "tcp",
2501 RPCBIND_NETID_TCP6);
2502 break;
2503 default:
2504 kfree(xprt);
2505 return ERR_PTR(-EAFNOSUPPORT);
2506 }
2507
2508 if (xprt_bound(xprt))
2509 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2510 xprt->address_strings[RPC_DISPLAY_ADDR],
2511 xprt->address_strings[RPC_DISPLAY_PORT],
2512 xprt->address_strings[RPC_DISPLAY_PROTO]);
2513 else
2514 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2515 xprt->address_strings[RPC_DISPLAY_ADDR],
2516 xprt->address_strings[RPC_DISPLAY_PROTO]);
2517
2518 /*
2519 * Since we don't want connections for the backchannel, we set
2520 * the xprt status to connected
2521 */
2522 xprt_set_connected(xprt);
2523
2524
2525 if (try_module_get(THIS_MODULE))
2526 return xprt;
2527 kfree(xprt->slot);
2528 kfree(xprt);
2529 return ERR_PTR(-EINVAL);
2530}
2531
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002532static struct xprt_class xs_udp_transport = {
2533 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2534 .name = "udp",
2535 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002536 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002537 .setup = xs_setup_udp,
2538};
2539
2540static struct xprt_class xs_tcp_transport = {
2541 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2542 .name = "tcp",
2543 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002544 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002545 .setup = xs_setup_tcp,
2546};
2547
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002548static struct xprt_class xs_bc_tcp_transport = {
2549 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2550 .name = "tcp NFSv4.1 backchannel",
2551 .owner = THIS_MODULE,
2552 .ident = XPRT_TRANSPORT_BC_TCP,
2553 .setup = xs_setup_bc_tcp,
2554};
2555
Chuck Lever282b32e2006-12-05 16:35:51 -05002556/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002557 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002558 *
2559 */
2560int init_socket_xprt(void)
2561{
Chuck Leverfbf76682006-12-05 16:35:54 -05002562#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002563 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002564 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002565#endif
2566
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002567 xprt_register_transport(&xs_udp_transport);
2568 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002569 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002570
Chuck Lever282b32e2006-12-05 16:35:51 -05002571 return 0;
2572}
2573
2574/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002575 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002576 *
2577 */
2578void cleanup_socket_xprt(void)
2579{
Chuck Leverfbf76682006-12-05 16:35:54 -05002580#ifdef RPC_DEBUG
2581 if (sunrpc_table_header) {
2582 unregister_sysctl_table(sunrpc_table_header);
2583 sunrpc_table_header = NULL;
2584 }
2585#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002586
2587 xprt_unregister_transport(&xs_udp_transport);
2588 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002589 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002590}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002591
2592static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2593 unsigned int min, unsigned int max)
2594{
2595 unsigned long num;
2596 int ret;
2597
2598 if (!val)
2599 return -EINVAL;
2600 ret = strict_strtoul(val, 0, &num);
2601 if (ret == -EINVAL || num < min || num > max)
2602 return -EINVAL;
2603 *((unsigned int *)kp->arg) = num;
2604 return 0;
2605}
2606
2607static int param_set_portnr(const char *val, struct kernel_param *kp)
2608{
2609 return param_set_uint_minmax(val, kp,
2610 RPC_MIN_RESVPORT,
2611 RPC_MAX_RESVPORT);
2612}
2613
2614static int param_get_portnr(char *buffer, struct kernel_param *kp)
2615{
2616 return param_get_uint(buffer, kp);
2617}
2618#define param_check_portnr(name, p) \
2619 __param_check(name, p, unsigned int);
2620
2621module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2622module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2623
2624static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2625{
2626 return param_set_uint_minmax(val, kp,
2627 RPC_MIN_SLOT_TABLE,
2628 RPC_MAX_SLOT_TABLE);
2629}
2630
2631static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2632{
2633 return param_get_uint(buffer, kp);
2634}
2635#define param_check_slot_table_size(name, p) \
2636 __param_check(name, p, unsigned int);
2637
2638module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2639 slot_table_size, 0644);
2640module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2641 slot_table_size, 0644);
2642