blob: df53dc55841d7e257f18ebea71520d4eeb7b4c58 [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 * Wait duration for a reply from the RPC portmapper.
142 */
143#define XS_BIND_TO (60U * HZ)
144
145/*
146 * Delay if a UDP socket connect error occurs. This is most likely some
147 * kind of resource problem on the local host.
148 */
149#define XS_UDP_REEST_TO (2U * HZ)
150
151/*
152 * The reestablish timeout allows clients to delay for a bit before attempting
153 * to reconnect to a server that just dropped our connection.
154 *
155 * We implement an exponential backoff when trying to reestablish a TCP
156 * transport connection with the server. Some servers like to drop a TCP
157 * connection when they are overworked, so we start with a short timeout and
158 * increase over time if the server is down or not responding.
159 */
160#define XS_TCP_INIT_REEST_TO (3U * HZ)
161#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
162
163/*
164 * TCP idle timeout; client drops the transport socket if it is idle
165 * for this long. Note that we also timeout UDP sockets to prevent
166 * holding port numbers when there is no RPC traffic.
167 */
168#define XS_IDLE_DISC_TO (5U * 60 * HZ)
169
Chuck Levera246b012005-08-11 16:25:23 -0400170#ifdef RPC_DEBUG
171# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400172# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400173#endif
174
Chuck Levera246b012005-08-11 16:25:23 -0400175#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400176static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400177{
Chuck Lever9903cd12005-08-11 16:25:26 -0400178 u8 *buf = (u8 *) packet;
179 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400180
Chuck Lever46121cf2007-01-31 12:14:08 -0500181 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400182 for (j = 0; j < count && j < 128; j += 4) {
183 if (!(j & 31)) {
184 if (j)
185 dprintk("\n");
186 dprintk("0x%04x ", j);
187 }
188 dprintk("%02x%02x%02x%02x ",
189 buf[j], buf[j+1], buf[j+2], buf[j+3]);
190 }
191 dprintk("\n");
192}
193#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400194static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400195{
196 /* NOP */
197}
198#endif
199
Chuck Leverffc2e512006-12-05 16:35:11 -0500200struct sock_xprt {
201 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500202
203 /*
204 * Network layer
205 */
206 struct socket * sock;
207 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500208
209 /*
210 * State of TCP reply receive
211 */
212 __be32 tcp_fraghdr,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400213 tcp_xid,
214 tcp_calldir;
Chuck Lever51971132006-12-05 16:35:19 -0500215
216 u32 tcp_offset,
217 tcp_reclen;
218
219 unsigned long tcp_copied,
220 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500221
222 /*
223 * Connection of transports
224 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500225 struct delayed_work connect_worker;
Chuck Leverfbfffbd2009-08-09 15:09:46 -0400226 struct sockaddr_storage srcaddr;
227 unsigned short srcport;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500228
229 /*
230 * UDP socket buffer size parameters
231 */
232 size_t rcvsize,
233 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500234
235 /*
236 * Saved socket callback addresses
237 */
238 void (*old_data_ready)(struct sock *, int);
239 void (*old_state_change)(struct sock *);
240 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400241 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500242};
243
Chuck Levere136d092006-12-05 16:35:23 -0500244/*
245 * TCP receive state flags
246 */
247#define TCP_RCV_LAST_FRAG (1UL << 0)
248#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
249#define TCP_RCV_COPY_XID (1UL << 2)
250#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400251#define TCP_RCV_READ_CALLDIR (1UL << 4)
252#define TCP_RCV_COPY_CALLDIR (1UL << 5)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400253
254/*
255 * TCP RPC flags
256 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400257#define TCP_RPC_REPLY (1UL << 6)
Chuck Levere136d092006-12-05 16:35:23 -0500258
Chuck Lever95392c52007-08-06 11:57:58 -0400259static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400260{
Chuck Lever95392c52007-08-06 11:57:58 -0400261 return (struct sockaddr *) &xprt->addr;
262}
263
264static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
265{
266 return (struct sockaddr_in *) &xprt->addr;
267}
268
269static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
270{
271 return (struct sockaddr_in6 *) &xprt->addr;
272}
273
Chuck Leverc877b842009-08-09 15:09:36 -0400274static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400275{
Chuck Leverc877b842009-08-09 15:09:36 -0400276 struct sockaddr *sap = xs_addr(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400277 struct sockaddr_in6 *sin6;
278 struct sockaddr_in *sin;
Chuck Leverc877b842009-08-09 15:09:36 -0400279 char buf[128];
Chuck Leveredb267a2006-08-22 20:06:18 -0400280
Chuck Leverc877b842009-08-09 15:09:36 -0400281 (void)rpc_ntop(sap, buf, sizeof(buf));
282 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
283
Chuck Lever9dc3b092009-08-09 15:09:46 -0400284 switch (sap->sa_family) {
285 case AF_INET:
286 sin = xs_addr_in(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800287 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Lever9dc3b092009-08-09 15:09:46 -0400288 break;
289 case AF_INET6:
290 sin6 = xs_addr_in6(xprt);
Joe Perchesfc0b57912010-03-08 12:15:28 -0800291 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400292 break;
293 default:
294 BUG();
Chuck Leveredb267a2006-08-22 20:06:18 -0400295 }
Chuck Lever9dc3b092009-08-09 15:09:46 -0400296 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400297}
298
Chuck Lever9dc3b092009-08-09 15:09:46 -0400299static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400300{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400301 struct sockaddr *sap = xs_addr(xprt);
302 char buf[128];
Chuck Lever4b6473f2007-08-06 11:57:12 -0400303
Joe Perches81160e662010-03-08 12:15:59 -0800304 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400305 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400306
Joe Perches81160e662010-03-08 12:15:59 -0800307 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400308 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
309}
Chuck Lever4b6473f2007-08-06 11:57:12 -0400310
Chuck Lever9dc3b092009-08-09 15:09:46 -0400311static void xs_format_peer_addresses(struct rpc_xprt *xprt,
312 const char *protocol,
313 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400314{
Chuck Leverb454ae92008-01-07 18:34:48 -0500315 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leverb454ae92008-01-07 18:34:48 -0500316 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leverc877b842009-08-09 15:09:36 -0400317 xs_format_common_peer_addresses(xprt);
Chuck Lever9dc3b092009-08-09 15:09:46 -0400318 xs_format_common_peer_ports(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400319}
320
Chuck Lever9dc3b092009-08-09 15:09:46 -0400321static void xs_update_peer_port(struct rpc_xprt *xprt)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400322{
Chuck Lever9dc3b092009-08-09 15:09:46 -0400323 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
324 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400325
Chuck Lever9dc3b092009-08-09 15:09:46 -0400326 xs_format_common_peer_ports(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400327}
328
329static void xs_free_peer_addresses(struct rpc_xprt *xprt)
330{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500331 unsigned int i;
332
333 for (i = 0; i < RPC_DISPLAY_MAX; i++)
334 switch (i) {
335 case RPC_DISPLAY_PROTO:
336 case RPC_DISPLAY_NETID:
337 continue;
338 default:
339 kfree(xprt->address_strings[i]);
340 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400341}
342
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400343#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
344
Trond Myklebust24c56842006-10-17 15:06:22 -0400345static 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 -0400346{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400347 struct msghdr msg = {
348 .msg_name = addr,
349 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400350 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
351 };
352 struct kvec iov = {
353 .iov_base = vec->iov_base + base,
354 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400355 };
356
Trond Myklebust24c56842006-10-17 15:06:22 -0400357 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400358 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
359 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
360}
361
Trond Myklebust24c56842006-10-17 15:06:22 -0400362static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400363{
Trond Myklebust24c56842006-10-17 15:06:22 -0400364 struct page **ppage;
365 unsigned int remainder;
366 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400367
Trond Myklebust24c56842006-10-17 15:06:22 -0400368 remainder = xdr->page_len - base;
369 base += xdr->page_base;
370 ppage = xdr->pages + (base >> PAGE_SHIFT);
371 base &= ~PAGE_MASK;
372 for(;;) {
373 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
374 int flags = XS_SENDMSG_FLAGS;
375
376 remainder -= len;
377 if (remainder != 0 || more)
378 flags |= MSG_MORE;
379 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
380 if (remainder == 0 || err != len)
381 break;
382 sent += err;
383 ppage++;
384 base = 0;
385 }
386 if (sent == 0)
387 return err;
388 if (err > 0)
389 sent += err;
390 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400391}
392
Chuck Lever9903cd12005-08-11 16:25:26 -0400393/**
394 * xs_sendpages - write pages directly to a socket
395 * @sock: socket to send on
396 * @addr: UDP only -- address of destination
397 * @addrlen: UDP only -- length of destination address
398 * @xdr: buffer containing this request
399 * @base: starting position in the buffer
400 *
Chuck Levera246b012005-08-11 16:25:23 -0400401 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400402static 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 -0400403{
Trond Myklebust24c56842006-10-17 15:06:22 -0400404 unsigned int remainder = xdr->len - base;
405 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400406
Chuck Lever262965f2005-08-11 16:25:56 -0400407 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400408 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400409
410 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400411 if (base != 0) {
412 addr = NULL;
413 addrlen = 0;
414 }
Chuck Lever262965f2005-08-11 16:25:56 -0400415
Trond Myklebust24c56842006-10-17 15:06:22 -0400416 if (base < xdr->head[0].iov_len || addr != NULL) {
417 unsigned int len = xdr->head[0].iov_len - base;
418 remainder -= len;
419 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
420 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400421 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400422 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400423 base = 0;
424 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400425 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400426
Trond Myklebust24c56842006-10-17 15:06:22 -0400427 if (base < xdr->page_len) {
428 unsigned int len = xdr->page_len - base;
429 remainder -= len;
430 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
431 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400432 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400433 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400434 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400435 } else
436 base -= xdr->page_len;
437
438 if (base >= xdr->tail[0].iov_len)
439 return sent;
440 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400441out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400442 if (sent == 0)
443 return err;
444 if (err > 0)
445 sent += err;
446 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400447}
448
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400449static void xs_nospace_callback(struct rpc_task *task)
450{
451 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
452
453 transport->inet->sk_write_pending--;
454 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
455}
456
Chuck Lever9903cd12005-08-11 16:25:26 -0400457/**
Chuck Lever262965f2005-08-11 16:25:56 -0400458 * xs_nospace - place task on wait queue if transmit was incomplete
459 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400460 *
Chuck Levera246b012005-08-11 16:25:23 -0400461 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400462static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400463{
Chuck Lever262965f2005-08-11 16:25:56 -0400464 struct rpc_rqst *req = task->tk_rqstp;
465 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500466 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400467 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400468
Chuck Lever46121cf2007-01-31 12:14:08 -0500469 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400470 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
471 req->rq_slen);
472
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400473 /* Protect against races with write_space */
474 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400475
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400476 /* Don't race with disconnect */
477 if (xprt_connected(xprt)) {
478 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400479 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400480 /*
481 * Notify TCP that we're limited by the application
482 * window size
483 */
484 set_bit(SOCK_NOSPACE, &transport->sock->flags);
485 transport->inet->sk_write_pending++;
486 /* ...and wait for more buffer space */
487 xprt_wait_for_buffer_space(task, xs_nospace_callback);
488 }
489 } else {
490 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400491 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400492 }
Chuck Lever262965f2005-08-11 16:25:56 -0400493
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400494 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400495 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400496}
497
498/**
499 * xs_udp_send_request - write an RPC request to a UDP socket
500 * @task: address of RPC task that manages the state of an RPC request
501 *
502 * Return values:
503 * 0: The request has been sent
504 * EAGAIN: The socket was blocked, please call again later to
505 * complete the request
506 * ENOTCONN: Caller needs to invoke connect logic then call again
507 * other: Some other error occured, the request was not sent
508 */
509static int xs_udp_send_request(struct rpc_task *task)
510{
511 struct rpc_rqst *req = task->tk_rqstp;
512 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500513 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400514 struct xdr_buf *xdr = &req->rq_snd_buf;
515 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400516
Chuck Lever9903cd12005-08-11 16:25:26 -0400517 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400518 req->rq_svec->iov_base,
519 req->rq_svec->iov_len);
520
Trond Myklebust01d37c42009-03-11 14:09:39 -0400521 if (!xprt_bound(xprt))
522 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500523 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400524 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500525 xprt->addrlen, xdr,
526 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400527
Chuck Lever46121cf2007-01-31 12:14:08 -0500528 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400529 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400530
Trond Myklebust21997002007-10-01 11:43:37 -0400531 if (status >= 0) {
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400532 req->rq_xmit_bytes_sent += status;
Trond Myklebust21997002007-10-01 11:43:37 -0400533 if (status >= req->rq_slen)
534 return 0;
535 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400536 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400537 }
Chuck Levera246b012005-08-11 16:25:23 -0400538
Chuck Lever262965f2005-08-11 16:25:56 -0400539 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400540 case -ENOTSOCK:
541 status = -ENOTCONN;
542 /* Should we call xs_close() here? */
543 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400544 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400545 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400546 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400547 default:
548 dprintk("RPC: sendmsg returned unrecognized error %d\n",
549 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400550 case -ENETUNREACH:
551 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400552 case -ECONNREFUSED:
553 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400554 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400555 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400556 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800557
Chuck Lever262965f2005-08-11 16:25:56 -0400558 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400559}
560
Trond Myklebuste06799f2007-11-05 15:44:12 -0500561/**
562 * xs_tcp_shutdown - gracefully shut down a TCP socket
563 * @xprt: transport
564 *
565 * Initiates a graceful shutdown of the TCP socket by calling the
566 * equivalent of shutdown(SHUT_WR);
567 */
568static void xs_tcp_shutdown(struct rpc_xprt *xprt)
569{
570 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
571 struct socket *sock = transport->sock;
572
573 if (sock != NULL)
574 kernel_sock_shutdown(sock, SHUT_WR);
575}
576
Chuck Lever808012f2005-08-25 16:25:49 -0700577static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
578{
579 u32 reclen = buf->len - sizeof(rpc_fraghdr);
580 rpc_fraghdr *base = buf->head[0].iov_base;
581 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
582}
583
Chuck Lever9903cd12005-08-11 16:25:26 -0400584/**
Chuck Lever262965f2005-08-11 16:25:56 -0400585 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400586 * @task: address of RPC task that manages the state of an RPC request
587 *
588 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400589 * 0: The request has been sent
590 * EAGAIN: The socket was blocked, please call again later to
591 * complete the request
592 * ENOTCONN: Caller needs to invoke connect logic then call again
593 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400594 *
595 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400596 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400597 */
Chuck Lever262965f2005-08-11 16:25:56 -0400598static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400599{
600 struct rpc_rqst *req = task->tk_rqstp;
601 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500602 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400603 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400604 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400605
Chuck Lever808012f2005-08-25 16:25:49 -0700606 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400607
Chuck Lever262965f2005-08-11 16:25:56 -0400608 xs_pktdump("packet data:",
609 req->rq_svec->iov_base,
610 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400611
612 /* Continue transmitting the packet/record. We must be careful
613 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400614 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400615 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500616 status = xs_sendpages(transport->sock,
617 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400618
Chuck Lever46121cf2007-01-31 12:14:08 -0500619 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400620 xdr->len - req->rq_bytes_sent, status);
621
622 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400623 break;
624
Chuck Lever262965f2005-08-11 16:25:56 -0400625 /* If we've sent the entire packet, immediately
626 * reset the count of bytes sent. */
627 req->rq_bytes_sent += status;
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400628 req->rq_xmit_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400629 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
630 req->rq_bytes_sent = 0;
631 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400632 }
633
Trond Myklebust06b4b682008-04-16 16:51:38 -0400634 if (status != 0)
635 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400636 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400637 break;
Chuck Levera246b012005-08-11 16:25:23 -0400638 }
639
Chuck Lever262965f2005-08-11 16:25:56 -0400640 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400641 case -ENOTSOCK:
642 status = -ENOTCONN;
643 /* Should we call xs_close() here? */
644 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400645 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400646 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400647 break;
648 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500649 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400650 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400651 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400652 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500653 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400654 case -ECONNREFUSED:
655 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400656 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400657 }
Bian Naimeng5fe46e92010-03-08 14:49:01 +0800658
Chuck Levera246b012005-08-11 16:25:23 -0400659 return status;
660}
661
Chuck Lever9903cd12005-08-11 16:25:26 -0400662/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400663 * xs_tcp_release_xprt - clean up after a tcp transmission
664 * @xprt: transport
665 * @task: rpc task
666 *
667 * This cleans up if an error causes us to abort the transmission of a request.
668 * In this case, the socket may need to be reset in order to avoid confusing
669 * the server.
670 */
671static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
672{
673 struct rpc_rqst *req;
674
675 if (task != xprt->snd_task)
676 return;
677 if (task == NULL)
678 goto out_release;
679 req = task->tk_rqstp;
680 if (req->rq_bytes_sent == 0)
681 goto out_release;
682 if (req->rq_bytes_sent == req->rq_snd_buf.len)
683 goto out_release;
684 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
685out_release:
686 xprt_release_xprt(xprt, task);
687}
688
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400689static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
690{
691 transport->old_data_ready = sk->sk_data_ready;
692 transport->old_state_change = sk->sk_state_change;
693 transport->old_write_space = sk->sk_write_space;
694 transport->old_error_report = sk->sk_error_report;
695}
696
697static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
698{
699 sk->sk_data_ready = transport->old_data_ready;
700 sk->sk_state_change = transport->old_state_change;
701 sk->sk_write_space = transport->old_write_space;
702 sk->sk_error_report = transport->old_error_report;
703}
704
Chuck Leverfe315e72009-03-11 14:10:21 -0400705static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400706{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500707 struct socket *sock = transport->sock;
708 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400709
Chuck Leverfe315e72009-03-11 14:10:21 -0400710 if (sk == NULL)
711 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400712
Chuck Levera246b012005-08-11 16:25:23 -0400713 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500714 transport->inet = NULL;
715 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400716
Chuck Lever9903cd12005-08-11 16:25:26 -0400717 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400718
719 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400720 write_unlock_bh(&sk->sk_callback_lock);
721
Chuck Lever9903cd12005-08-11 16:25:26 -0400722 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400723
724 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400725}
726
727/**
728 * xs_close - close a socket
729 * @xprt: transport
730 *
731 * This is used when all requests are complete; ie, no DRC state remains
732 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400733 *
734 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
735 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400736 */
737static void xs_close(struct rpc_xprt *xprt)
738{
739 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
740
741 dprintk("RPC: xs_close xprt %p\n", xprt);
742
743 xs_reset_transport(transport);
Neil Brown61d0a8e2009-09-23 14:36:37 -0400744 xprt->reestablish_timeout = 0;
Chuck Leverfe315e72009-03-11 14:10:21 -0400745
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100746 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400747 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100748 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500749 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100750 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500751 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400752}
753
Trond Myklebustf75e6742009-04-21 17:18:20 -0400754static void xs_tcp_close(struct rpc_xprt *xprt)
755{
756 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
757 xs_close(xprt);
758 else
759 xs_tcp_shutdown(xprt);
760}
761
Chuck Lever9903cd12005-08-11 16:25:26 -0400762/**
763 * xs_destroy - prepare to shutdown a transport
764 * @xprt: doomed transport
765 *
766 */
767static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400768{
Chuck Leverc8475462006-12-05 16:35:26 -0500769 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
770
Chuck Lever46121cf2007-01-31 12:14:08 -0500771 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400772
Trond Myklebustc1384c92007-06-14 18:00:42 -0400773 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400774
Chuck Lever9903cd12005-08-11 16:25:26 -0400775 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400776 xs_free_peer_addresses(xprt);
Pavel Emelyanove204e622010-09-29 16:03:13 +0400777 xprt_free(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400778 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400779}
780
Chuck Lever9903cd12005-08-11 16:25:26 -0400781static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400782{
Chuck Lever9903cd12005-08-11 16:25:26 -0400783 return (struct rpc_xprt *) sk->sk_user_data;
784}
785
786/**
787 * xs_udp_data_ready - "data ready" callback for UDP sockets
788 * @sk: socket with data to read
789 * @len: how much data to read
790 *
791 */
792static void xs_udp_data_ready(struct sock *sk, int len)
793{
794 struct rpc_task *task;
795 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400796 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400798 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700799 u32 _xid;
800 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400801
802 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500803 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400804 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400805 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400806
807 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
808 goto out;
809
810 if (xprt->shutdown)
811 goto dropit;
812
813 repsize = skb->len - sizeof(struct udphdr);
814 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500815 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400816 goto dropit;
817 }
818
819 /* Copy the XID from the skb... */
820 xp = skb_header_pointer(skb, sizeof(struct udphdr),
821 sizeof(_xid), &_xid);
822 if (xp == NULL)
823 goto dropit;
824
825 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400826 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400827 rovr = xprt_lookup_rqst(xprt, *xp);
828 if (!rovr)
829 goto out_unlock;
830 task = rovr->rq_task;
831
Chuck Levera246b012005-08-11 16:25:23 -0400832 if ((copied = rovr->rq_private_buf.buflen) > repsize)
833 copied = repsize;
834
835 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800836 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
837 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400838 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800839 }
840
841 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400842
843 /* Something worked... */
Eric Dumazetadf30902009-06-02 05:19:30 +0000844 dst_confirm(skb_dst(skb));
Chuck Levera246b012005-08-11 16:25:23 -0400845
Chuck Lever1570c1e2005-08-25 16:25:52 -0700846 xprt_adjust_cwnd(task, copied);
Chuck Lever1570c1e2005-08-25 16:25:52 -0700847 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400848
849 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400850 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400851 dropit:
852 skb_free_datagram(sk, skb);
853 out:
854 read_unlock(&sk->sk_callback_lock);
855}
856
Chuck Leverdd456472006-12-05 16:35:44 -0500857static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400858{
Chuck Lever51971132006-12-05 16:35:19 -0500859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400860 size_t len, used;
861 char *p;
862
Chuck Lever51971132006-12-05 16:35:19 -0500863 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
864 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500865 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500866 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400867 if (used != len)
868 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700869
Chuck Lever51971132006-12-05 16:35:19 -0500870 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
871 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500872 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400873 else
Chuck Levere136d092006-12-05 16:35:23 -0500874 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500875 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700876
Chuck Levere136d092006-12-05 16:35:23 -0500877 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500878 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700879
Chuck Levera246b012005-08-11 16:25:23 -0400880 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400881 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500882 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500883 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400884 return;
Chuck Levera246b012005-08-11 16:25:23 -0400885 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500886 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500887 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400888}
889
Chuck Lever51971132006-12-05 16:35:19 -0500890static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400891{
Chuck Lever51971132006-12-05 16:35:19 -0500892 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500893 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500894 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500895 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
896 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
897 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500898 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400899 }
900 }
901}
902
Chuck Leverdd456472006-12-05 16:35:44 -0500903static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400904{
905 size_t len, used;
906 char *p;
907
Chuck Lever51971132006-12-05 16:35:19 -0500908 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500909 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500910 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500911 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500912 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400913 if (used != len)
914 return;
Chuck Levere136d092006-12-05 16:35:23 -0500915 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400916 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -0500917 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400918 dprintk("RPC: reading %s XID %08x\n",
919 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
920 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -0500921 ntohl(transport->tcp_xid));
922 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400923}
924
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400925static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
926 struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400927{
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400928 size_t len, used;
929 u32 offset;
Trond Myklebustb76ce562010-06-16 13:57:32 -0400930 char *p;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400931
932 /*
933 * We want transport->tcp_offset to be 8 at the end of this routine
934 * (4 bytes for the xid and 4 bytes for the call/reply flag).
935 * When this function is called for the first time,
936 * transport->tcp_offset is 4 (after having already read the xid).
937 */
938 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
Trond Myklebustb76ce562010-06-16 13:57:32 -0400939 len = sizeof(transport->tcp_calldir) - offset;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400940 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
Trond Myklebustb76ce562010-06-16 13:57:32 -0400941 p = ((char *) &transport->tcp_calldir) + offset;
942 used = xdr_skb_read_bits(desc, p, len);
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400943 transport->tcp_offset += used;
944 if (used != len)
945 return;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400946 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400947 /*
948 * We don't yet have the XDR buffer, so we will write the calldir
949 * out after we get the buffer from the 'struct rpc_rqst'
950 */
Trond Myklebustb76ce562010-06-16 13:57:32 -0400951 switch (ntohl(transport->tcp_calldir)) {
952 case RPC_REPLY:
953 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
954 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400955 transport->tcp_flags |= TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -0400956 break;
957 case RPC_CALL:
958 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
959 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400960 transport->tcp_flags &= ~TCP_RPC_REPLY;
Trond Myklebustb76ce562010-06-16 13:57:32 -0400961 break;
962 default:
963 dprintk("RPC: invalid request message type\n");
964 xprt_force_disconnect(&transport->xprt);
965 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400966 xs_tcp_check_fraghdr(transport);
967}
968
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400969static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
970 struct xdr_skb_reader *desc,
971 struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400972{
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -0400973 struct sock_xprt *transport =
974 container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400975 struct xdr_buf *rcvbuf;
976 size_t len;
977 ssize_t r;
978
Chuck Levera246b012005-08-11 16:25:23 -0400979 rcvbuf = &req->rq_private_buf;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400980
981 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
982 /*
983 * Save the RPC direction in the XDR buffer
984 */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400985 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
Trond Myklebustb76ce562010-06-16 13:57:32 -0400986 &transport->tcp_calldir,
987 sizeof(transport->tcp_calldir));
988 transport->tcp_copied += sizeof(transport->tcp_calldir);
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -0400989 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
Chuck Levera246b012005-08-11 16:25:23 -0400990 }
991
Chuck Levera246b012005-08-11 16:25:23 -0400992 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500993 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500994 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400995
Chuck Lever51971132006-12-05 16:35:19 -0500996 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400997 memcpy(&my_desc, desc, sizeof(my_desc));
998 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500999 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001000 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001001 desc->count -= r;
1002 desc->offset += r;
1003 } else
Chuck Lever51971132006-12-05 16:35:19 -05001004 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001005 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001006
1007 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001008 transport->tcp_copied += r;
1009 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001010 }
1011 if (r != len) {
1012 /* Error when copying to the receive buffer,
1013 * usually because we weren't able to allocate
1014 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001015 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001016 * will not receive any additional updates,
1017 * and time out.
1018 * Any remaining data from this record will
1019 * be discarded.
1020 */
Chuck Levere136d092006-12-05 16:35:23 -05001021 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001022 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001023 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001024 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1025 "tcp_offset = %u, tcp_reclen = %u\n",
1026 xprt, transport->tcp_copied,
1027 transport->tcp_offset, transport->tcp_reclen);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001028 return;
Chuck Levera246b012005-08-11 16:25:23 -04001029 }
1030
Chuck Lever46121cf2007-01-31 12:14:08 -05001031 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001032 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001033 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1034 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1035 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001036
Chuck Lever51971132006-12-05 16:35:19 -05001037 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001038 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001039 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001040 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1041 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001042 }
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001043}
1044
1045/*
1046 * Finds the request corresponding to the RPC xid and invokes the common
1047 * tcp read code to read the data.
1048 */
1049static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1050 struct xdr_skb_reader *desc)
1051{
1052 struct sock_xprt *transport =
1053 container_of(xprt, struct sock_xprt, xprt);
1054 struct rpc_rqst *req;
1055
1056 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1057
1058 /* Find and lock the request corresponding to this xid */
1059 spin_lock(&xprt->transport_lock);
1060 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1061 if (!req) {
1062 dprintk("RPC: XID %08x request not found!\n",
1063 ntohl(transport->tcp_xid));
1064 spin_unlock(&xprt->transport_lock);
1065 return -1;
1066 }
1067
1068 xs_tcp_read_common(xprt, desc, req);
1069
Chuck Levere136d092006-12-05 16:35:23 -05001070 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001071 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001072
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001073 spin_unlock(&xprt->transport_lock);
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001074 return 0;
1075}
1076
1077#if defined(CONFIG_NFS_V4_1)
1078/*
1079 * Obtains an rpc_rqst previously allocated and invokes the common
1080 * tcp read code to read the data. The result is placed in the callback
1081 * queue.
1082 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1083 * connection and return -1.
1084 */
1085static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1086 struct xdr_skb_reader *desc)
1087{
1088 struct sock_xprt *transport =
1089 container_of(xprt, struct sock_xprt, xprt);
1090 struct rpc_rqst *req;
1091
1092 req = xprt_alloc_bc_request(xprt);
1093 if (req == NULL) {
1094 printk(KERN_WARNING "Callback slot table overflowed\n");
1095 xprt_force_disconnect(xprt);
1096 return -1;
1097 }
1098
1099 req->rq_xid = transport->tcp_xid;
1100 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1101 xs_tcp_read_common(xprt, desc, req);
1102
1103 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1104 struct svc_serv *bc_serv = xprt->bc_serv;
1105
1106 /*
1107 * Add callback request to callback list. The callback
1108 * service sleeps on the sv_cb_waitq waiting for new
1109 * requests. Wake it up after adding enqueing the
1110 * request.
1111 */
1112 dprintk("RPC: add callback request to list\n");
1113 spin_lock(&bc_serv->sv_cb_lock);
1114 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1115 spin_unlock(&bc_serv->sv_cb_lock);
1116 wake_up(&bc_serv->sv_cb_waitq);
1117 }
1118
1119 req->rq_private_buf.len = transport->tcp_copied;
1120
1121 return 0;
1122}
1123
1124static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1125 struct xdr_skb_reader *desc)
1126{
1127 struct sock_xprt *transport =
1128 container_of(xprt, struct sock_xprt, xprt);
1129
1130 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1131 xs_tcp_read_reply(xprt, desc) :
1132 xs_tcp_read_callback(xprt, desc);
1133}
1134#else
1135static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1136 struct xdr_skb_reader *desc)
1137{
1138 return xs_tcp_read_reply(xprt, desc);
1139}
1140#endif /* CONFIG_NFS_V4_1 */
1141
1142/*
1143 * Read data off the transport. This can be either an RPC_CALL or an
1144 * RPC_REPLY. Relay the processing to helper functions.
1145 */
1146static void xs_tcp_read_data(struct rpc_xprt *xprt,
1147 struct xdr_skb_reader *desc)
1148{
1149 struct sock_xprt *transport =
1150 container_of(xprt, struct sock_xprt, xprt);
1151
1152 if (_xs_tcp_read_data(xprt, desc) == 0)
1153 xs_tcp_check_fraghdr(transport);
1154 else {
1155 /*
1156 * The transport_lock protects the request handling.
1157 * There's no need to hold it to update the tcp_flags.
1158 */
1159 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1160 }
Chuck Levera246b012005-08-11 16:25:23 -04001161}
1162
Chuck Leverdd456472006-12-05 16:35:44 -05001163static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001164{
1165 size_t len;
1166
Chuck Lever51971132006-12-05 16:35:19 -05001167 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001168 if (len > desc->count)
1169 len = desc->count;
1170 desc->count -= len;
1171 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001172 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001173 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001174 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001175}
1176
Chuck Lever9903cd12005-08-11 16:25:26 -04001177static 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 -04001178{
1179 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001180 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001181 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001182 .skb = skb,
1183 .offset = offset,
1184 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001185 };
Chuck Levera246b012005-08-11 16:25:23 -04001186
Chuck Lever46121cf2007-01-31 12:14:08 -05001187 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001188 do {
1189 /* Read in a new fragment marker if necessary */
1190 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001191 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001192 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001193 continue;
1194 }
1195 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001196 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001197 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001198 continue;
1199 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001200 /* Read in the call/reply flag */
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001201 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001202 xs_tcp_read_calldir(transport, &desc);
1203 continue;
1204 }
Chuck Levera246b012005-08-11 16:25:23 -04001205 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001206 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Ricardo Labiaga44b98ef2009-04-01 09:23:02 -04001207 xs_tcp_read_data(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001208 continue;
1209 }
1210 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001211 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001212 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001213 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001214 return len - desc.count;
1215}
1216
Chuck Lever9903cd12005-08-11 16:25:26 -04001217/**
1218 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1219 * @sk: socket with data to read
1220 * @bytes: how much data to read
1221 *
1222 */
1223static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001224{
1225 struct rpc_xprt *xprt;
1226 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001227 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001228
Chuck Lever46121cf2007-01-31 12:14:08 -05001229 dprintk("RPC: xs_tcp_data_ready...\n");
1230
Chuck Levera246b012005-08-11 16:25:23 -04001231 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001232 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001233 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001234 if (xprt->shutdown)
1235 goto out;
1236
Neil Brown61d0a8e2009-09-23 14:36:37 -04001237 /* Any data means we had a useful conversation, so
1238 * the we don't need to delay the next reconnect
1239 */
1240 if (xprt->reestablish_timeout)
1241 xprt->reestablish_timeout = 0;
1242
Chuck Lever9903cd12005-08-11 16:25:26 -04001243 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001244 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001245 do {
1246 rd_desc.count = 65536;
1247 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1248 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001249out:
1250 read_unlock(&sk->sk_callback_lock);
1251}
1252
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001253/*
1254 * Do the equivalent of linger/linger2 handling for dealing with
1255 * broken servers that don't close the socket in a timely
1256 * fashion
1257 */
1258static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1259 unsigned long timeout)
1260{
1261 struct sock_xprt *transport;
1262
1263 if (xprt_test_and_set_connecting(xprt))
1264 return;
1265 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1266 transport = container_of(xprt, struct sock_xprt, xprt);
1267 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1268 timeout);
1269}
1270
1271static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1272{
1273 struct sock_xprt *transport;
1274
1275 transport = container_of(xprt, struct sock_xprt, xprt);
1276
1277 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1278 !cancel_delayed_work(&transport->connect_worker))
1279 return;
1280 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1281 xprt_clear_connecting(xprt);
1282}
1283
1284static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1285{
1286 smp_mb__before_clear_bit();
1287 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1288 clear_bit(XPRT_CLOSING, &xprt->state);
1289 smp_mb__after_clear_bit();
1290 /* Mark transport as closed and wake up all pending tasks */
1291 xprt_disconnect_done(xprt);
1292}
1293
Chuck Lever9903cd12005-08-11 16:25:26 -04001294/**
1295 * xs_tcp_state_change - callback to handle TCP socket state changes
1296 * @sk: socket whose state has changed
1297 *
1298 */
1299static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001300{
Chuck Lever9903cd12005-08-11 16:25:26 -04001301 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001302
1303 read_lock(&sk->sk_callback_lock);
1304 if (!(xprt = xprt_from_sock(sk)))
1305 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001306 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Andy Chittenden669502f2010-08-10 10:19:53 -04001307 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
Chuck Lever46121cf2007-01-31 12:14:08 -05001308 sk->sk_state, xprt_connected(xprt),
1309 sock_flag(sk, SOCK_DEAD),
Andy Chittenden669502f2010-08-10 10:19:53 -04001310 sock_flag(sk, SOCK_ZAPPED),
1311 sk->sk_shutdown);
Chuck Levera246b012005-08-11 16:25:23 -04001312
1313 switch (sk->sk_state) {
1314 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001315 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001316 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001317 struct sock_xprt *transport = container_of(xprt,
1318 struct sock_xprt, xprt);
1319
Chuck Levera246b012005-08-11 16:25:23 -04001320 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001321 transport->tcp_offset = 0;
1322 transport->tcp_reclen = 0;
1323 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001324 transport->tcp_flags =
1325 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001326
Trond Myklebust2a491992009-03-11 14:38:00 -04001327 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001328 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001329 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001330 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001331 case TCP_FIN_WAIT1:
1332 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001333 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001334 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001335 set_bit(XPRT_CLOSING, &xprt->state);
1336 smp_mb__before_clear_bit();
1337 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001338 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001339 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001340 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001341 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001342 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001343 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001344 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001345 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001346 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001347 case TCP_CLOSING:
1348 /*
1349 * If the server closed down the connection, make sure that
1350 * we back off before reconnecting
1351 */
1352 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1353 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001354 break;
1355 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001356 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001357 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001358 smp_mb__before_clear_bit();
1359 clear_bit(XPRT_CONNECTED, &xprt->state);
1360 smp_mb__after_clear_bit();
1361 break;
1362 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001363 xs_tcp_cancel_linger_timeout(xprt);
1364 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001365 }
1366 out:
1367 read_unlock(&sk->sk_callback_lock);
1368}
1369
Chuck Lever9903cd12005-08-11 16:25:26 -04001370/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001371 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001372 * @sk: socket
1373 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001374static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001375{
1376 struct rpc_xprt *xprt;
1377
1378 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001379 if (!(xprt = xprt_from_sock(sk)))
1380 goto out;
1381 dprintk("RPC: %s client %p...\n"
1382 "RPC: error %d\n",
1383 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001384 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001385out:
1386 read_unlock(&sk->sk_callback_lock);
1387}
1388
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001389static void xs_write_space(struct sock *sk)
1390{
1391 struct socket *sock;
1392 struct rpc_xprt *xprt;
1393
1394 if (unlikely(!(sock = sk->sk_socket)))
1395 return;
1396 clear_bit(SOCK_NOSPACE, &sock->flags);
1397
1398 if (unlikely(!(xprt = xprt_from_sock(sk))))
1399 return;
1400 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1401 return;
1402
1403 xprt_write_space(xprt);
1404}
1405
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001406/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001407 * xs_udp_write_space - callback invoked when socket buffer space
1408 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001409 * @sk: socket whose state has changed
1410 *
Chuck Levera246b012005-08-11 16:25:23 -04001411 * Called when more output buffer space is available for this socket.
1412 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001413 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001414 * with a bunch of small requests.
1415 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001416static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001417{
Chuck Levera246b012005-08-11 16:25:23 -04001418 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001419
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001420 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001421 if (sock_writeable(sk))
1422 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001423
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001424 read_unlock(&sk->sk_callback_lock);
1425}
Chuck Levera246b012005-08-11 16:25:23 -04001426
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001427/**
1428 * xs_tcp_write_space - callback invoked when socket buffer space
1429 * becomes available
1430 * @sk: socket whose state has changed
1431 *
1432 * Called when more output buffer space is available for this socket.
1433 * We try not to wake our writers until they can make "significant"
1434 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1435 * with a bunch of small requests.
1436 */
1437static void xs_tcp_write_space(struct sock *sk)
1438{
1439 read_lock(&sk->sk_callback_lock);
1440
1441 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001442 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1443 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001444
Chuck Levera246b012005-08-11 16:25:23 -04001445 read_unlock(&sk->sk_callback_lock);
1446}
1447
Chuck Lever470056c2005-08-25 16:25:56 -07001448static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001449{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001450 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1451 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001452
Chuck Lever7c6e0662006-12-05 16:35:30 -05001453 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001454 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001455 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001456 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001457 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001458 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001459 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001460 sk->sk_write_space(sk);
1461 }
1462}
1463
Chuck Lever43118c22005-08-25 16:25:49 -07001464/**
Chuck Lever470056c2005-08-25 16:25:56 -07001465 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001466 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001467 * @sndsize: requested size of send buffer, in bytes
1468 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001469 *
Chuck Lever470056c2005-08-25 16:25:56 -07001470 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001471 */
Chuck Lever470056c2005-08-25 16:25:56 -07001472static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001473{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001474 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1475
1476 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001477 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001478 transport->sndsize = sndsize + 1024;
1479 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001480 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001481 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001482
1483 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001484}
1485
Chuck Lever46c0ee82005-08-25 16:25:52 -07001486/**
1487 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1488 * @task: task that timed out
1489 *
1490 * Adjust the congestion window after a retransmit timeout has occurred.
1491 */
1492static void xs_udp_timer(struct rpc_task *task)
1493{
1494 xprt_adjust_cwnd(task, -ETIMEDOUT);
1495}
1496
Chuck Leverb85d8802006-05-25 01:40:49 -04001497static unsigned short xs_get_random_port(void)
1498{
1499 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1500 unsigned short rand = (unsigned short) net_random() % range;
1501 return rand + xprt_min_resvport;
1502}
1503
Chuck Lever92200412006-01-03 09:55:51 +01001504/**
1505 * xs_set_port - reset the port number in the remote endpoint address
1506 * @xprt: generic transport
1507 * @port: new port number
1508 *
1509 */
1510static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1511{
Chuck Lever46121cf2007-01-31 12:14:08 -05001512 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001513
Chuck Lever9dc3b092009-08-09 15:09:46 -04001514 rpc_set_port(xs_addr(xprt), port);
1515 xs_update_peer_port(xprt);
Chuck Lever92200412006-01-03 09:55:51 +01001516}
1517
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001518static unsigned short xs_get_srcport(struct sock_xprt *transport)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001519{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001520 unsigned short port = transport->srcport;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001521
1522 if (port == 0 && transport->xprt.resvport)
1523 port = xs_get_random_port();
1524 return port;
1525}
1526
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001527static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
Trond Myklebust67a391d2007-11-05 17:40:58 -05001528{
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001529 if (transport->srcport != 0)
1530 transport->srcport = 0;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001531 if (!transport->xprt.resvport)
1532 return 0;
1533 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1534 return xprt_max_resvport;
1535 return --port;
1536}
1537
Chuck Lever7dc753f2007-08-06 11:57:28 -04001538static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001539{
1540 struct sockaddr_in myaddr = {
1541 .sin_family = AF_INET,
1542 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001543 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001544 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001545 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001546 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001547
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001548 sa = (struct sockaddr_in *)&transport->srcaddr;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001549 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001550 do {
1551 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001552 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001553 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001554 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001555 break;
Chuck Levera246b012005-08-11 16:25:23 -04001556 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001557 transport->srcport = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001558 break;
Chuck Levera246b012005-08-11 16:25:23 -04001559 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001560 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001561 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001562 if (port > last)
1563 nloop++;
1564 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001565 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1566 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001567 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001568 return err;
1569}
1570
Chuck Lever90058d32007-08-06 11:57:33 -04001571static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1572{
1573 struct sockaddr_in6 myaddr = {
1574 .sin6_family = AF_INET6,
1575 };
1576 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001577 int err, nloop = 0;
Pavel Emelyanov5d4ec932010-10-04 16:51:23 +04001578 unsigned short port = xs_get_srcport(transport);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001579 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001580
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001581 sa = (struct sockaddr_in6 *)&transport->srcaddr;
Chuck Lever90058d32007-08-06 11:57:33 -04001582 myaddr.sin6_addr = sa->sin6_addr;
1583 do {
1584 myaddr.sin6_port = htons(port);
1585 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1586 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001587 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001588 break;
1589 if (err == 0) {
Chuck Leverfbfffbd2009-08-09 15:09:46 -04001590 transport->srcport = port;
Chuck Lever90058d32007-08-06 11:57:33 -04001591 break;
1592 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001593 last = port;
Pavel Emelyanovbaaf4e42010-10-04 16:51:56 +04001594 port = xs_next_srcport(transport, port);
Trond Myklebust67a391d2007-11-05 17:40:58 -05001595 if (port > last)
1596 nloop++;
1597 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001598 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001599 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001600 return err;
1601}
1602
Peter Zijlstraed075362006-12-06 20:35:24 -08001603#ifdef CONFIG_DEBUG_LOCK_ALLOC
1604static struct lock_class_key xs_key[2];
1605static struct lock_class_key xs_slock_key[2];
1606
Chuck Lever8945ee52007-08-06 11:58:04 -04001607static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001608{
1609 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001610
John Heffner02b3d342007-09-12 10:42:12 +02001611 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001612 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1613 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1614}
Peter Zijlstraed075362006-12-06 20:35:24 -08001615
Chuck Lever8945ee52007-08-06 11:58:04 -04001616static inline void xs_reclassify_socket6(struct socket *sock)
1617{
1618 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001619
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001620 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001621 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1622 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001623}
1624#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001625static inline void xs_reclassify_socket4(struct socket *sock)
1626{
1627}
1628
1629static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001630{
1631}
1632#endif
1633
Chuck Lever16be2d22007-08-06 11:57:38 -04001634static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001635{
Chuck Lever16be2d22007-08-06 11:57:38 -04001636 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001637
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001638 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001639 struct sock *sk = sock->sk;
1640
1641 write_lock_bh(&sk->sk_callback_lock);
1642
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001643 xs_save_old_callbacks(transport, sk);
1644
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001645 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001646 sk->sk_data_ready = xs_udp_data_ready;
1647 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001648 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001649 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001650 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001651
1652 xprt_set_connected(xprt);
1653
1654 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001655 transport->sock = sock;
1656 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001657
1658 write_unlock_bh(&sk->sk_callback_lock);
1659 }
Chuck Lever470056c2005-08-25 16:25:56 -07001660 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001661}
1662
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001663static void xs_udp_setup_socket(struct sock_xprt *transport,
1664 struct socket *(*create_sock)(struct rpc_xprt *,
1665 struct sock_xprt *))
Chuck Levera246b012005-08-11 16:25:23 -04001666{
Chuck Levera246b012005-08-11 16:25:23 -04001667 struct rpc_xprt *xprt = &transport->xprt;
1668 struct socket *sock = transport->sock;
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001669 int status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001670
Trond Myklebust01d37c42009-03-11 14:09:39 -04001671 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001672 goto out;
1673
Chuck Levera246b012005-08-11 16:25:23 -04001674 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001675 xs_reset_transport(transport);
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001676 sock = create_sock(xprt, transport);
1677 if (IS_ERR(sock))
Chuck Levera246b012005-08-11 16:25:23 -04001678 goto out;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001679
Chuck Leverc740eff2009-08-09 15:09:46 -04001680 dprintk("RPC: worker connecting xprt %p via %s to "
1681 "%s (port %s)\n", xprt,
1682 xprt->address_strings[RPC_DISPLAY_PROTO],
1683 xprt->address_strings[RPC_DISPLAY_ADDR],
1684 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001685
Chuck Lever16be2d22007-08-06 11:57:38 -04001686 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001687 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001688out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001689 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001690 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001691}
1692
Chuck Lever68e220b2007-08-06 11:57:48 -04001693/**
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001694 * xs_udp_connect_worker4 - set up a UDP socket
1695 * @work: RPC transport to connect
1696 *
1697 * Invoked by a work queue tasklet.
1698 */
1699
1700static struct socket *xs_create_udp_sock4(struct rpc_xprt *xprt,
1701 struct sock_xprt *transport)
1702{
1703 struct socket *sock;
1704 int err;
1705
1706 err = __sock_create(xprt->xprt_net, PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1707 if (err < 0) {
1708 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1709 goto out;
1710 }
1711 xs_reclassify_socket4(sock);
1712
1713 if (xs_bind4(transport, sock)) {
1714 sock_release(sock);
1715 goto out;
1716 }
1717
1718 return sock;
1719out:
1720 return ERR_PTR(err);
1721}
1722
1723static void xs_udp_connect_worker4(struct work_struct *work)
1724{
1725 struct sock_xprt *transport =
1726 container_of(work, struct sock_xprt, connect_worker.work);
1727
1728 xs_udp_setup_socket(transport, xs_create_udp_sock4);
1729}
1730
1731/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001732 * xs_udp_connect_worker6 - set up a UDP socket
1733 * @work: RPC transport to connect
1734 *
1735 * Invoked by a work queue tasklet.
1736 */
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001737
1738static struct socket *xs_create_udp_sock6(struct rpc_xprt *xprt,
1739 struct sock_xprt *transport)
Chuck Lever68e220b2007-08-06 11:57:48 -04001740{
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001741 struct socket *sock;
1742 int err;
Chuck Lever68e220b2007-08-06 11:57:48 -04001743
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001744 err = __sock_create(xprt->xprt_net, PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
Chuck Leverfe315e72009-03-11 14:10:21 -04001745 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001746 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1747 goto out;
1748 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001749 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001750
1751 if (xs_bind6(transport, sock) < 0) {
1752 sock_release(sock);
1753 goto out;
1754 }
1755
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001756 return sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001757out:
Pavel Emelyanovb65c0312010-10-04 16:53:46 +04001758 return ERR_PTR(err);
1759}
1760
1761static void xs_udp_connect_worker6(struct work_struct *work)
1762{
1763 struct sock_xprt *transport =
1764 container_of(work, struct sock_xprt, connect_worker.work);
1765
1766 xs_udp_setup_socket(transport, xs_create_udp_sock6);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001767}
1768
Chuck Lever3167e122005-08-25 16:25:55 -07001769/*
1770 * We need to preserve the port number so the reply cache on the server can
1771 * find our cached RPC replies when we get around to reconnecting.
1772 */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001773static void xs_abort_connection(struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001774{
1775 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001776 struct sockaddr any;
1777
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001778 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
Chuck Lever3167e122005-08-25 16:25:55 -07001779
1780 /*
1781 * Disconnect the transport socket by doing a connect operation
1782 * with AF_UNSPEC. This should return immediately...
1783 */
1784 memset(&any, 0, sizeof(any));
1785 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001786 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001787 if (!result)
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001788 xs_sock_mark_closed(&transport->xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001789 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001790 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001791 result);
1792}
1793
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001794static void xs_tcp_reuse_connection(struct sock_xprt *transport)
Trond Myklebust40d25492009-03-11 14:37:58 -04001795{
1796 unsigned int state = transport->inet->sk_state;
1797
Andy Chittenden669502f2010-08-10 10:19:53 -04001798 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
1799 /* we don't need to abort the connection if the socket
1800 * hasn't undergone a shutdown
1801 */
1802 if (transport->inet->sk_shutdown == 0)
1803 return;
1804 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
1805 __func__, transport->inet->sk_shutdown);
1806 }
1807 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
1808 /* we don't need to abort the connection if the socket
1809 * hasn't undergone a shutdown
1810 */
1811 if (transport->inet->sk_shutdown == 0)
1812 return;
1813 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
1814 "sk_shutdown set to %d\n",
1815 __func__, transport->inet->sk_shutdown);
1816 }
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001817 xs_abort_connection(transport);
Trond Myklebust40d25492009-03-11 14:37:58 -04001818}
1819
Chuck Lever16be2d22007-08-06 11:57:38 -04001820static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001821{
Chuck Lever16be2d22007-08-06 11:57:38 -04001822 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001823
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001824 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001825 struct sock *sk = sock->sk;
1826
1827 write_lock_bh(&sk->sk_callback_lock);
1828
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001829 xs_save_old_callbacks(transport, sk);
1830
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001831 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001832 sk->sk_data_ready = xs_tcp_data_ready;
1833 sk->sk_state_change = xs_tcp_state_change;
1834 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001835 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001836 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001837
1838 /* socket options */
1839 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1840 sock_reset_flag(sk, SOCK_LINGER);
1841 tcp_sk(sk)->linger2 = 0;
1842 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001843
1844 xprt_clear_connected(xprt);
1845
1846 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001847 transport->sock = sock;
1848 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001849
1850 write_unlock_bh(&sk->sk_callback_lock);
1851 }
1852
Trond Myklebust01d37c42009-03-11 14:09:39 -04001853 if (!xprt_bound(xprt))
1854 return -ENOTCONN;
1855
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001856 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001857 xprt->stat.connect_count++;
1858 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001859 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001860}
1861
1862/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001863 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1864 * @xprt: RPC transport to connect
1865 * @transport: socket transport to connect
1866 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001867 *
1868 * Invoked by a work queue tasklet.
1869 */
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04001870static void xs_tcp_setup_socket(struct sock_xprt *transport,
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001871 struct socket *(*create_sock)(struct rpc_xprt *,
1872 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001873{
Chuck Lever16be2d22007-08-06 11:57:38 -04001874 struct socket *sock = transport->sock;
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04001875 struct rpc_xprt *xprt = &transport->xprt;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001876 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001877
Trond Myklebust01d37c42009-03-11 14:09:39 -04001878 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001879 goto out;
1880
1881 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001882 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001883 sock = create_sock(xprt, transport);
1884 if (IS_ERR(sock)) {
1885 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001886 goto out;
1887 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001888 } else {
1889 int abort_and_exit;
1890
1891 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1892 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001893 /* "close" the socket, preserving the local port */
Pavel Emelyanov58dddac2010-10-04 16:52:55 +04001894 xs_tcp_reuse_connection(transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001895
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001896 if (abort_and_exit)
1897 goto out_eagain;
1898 }
1899
Chuck Leverc740eff2009-08-09 15:09:46 -04001900 dprintk("RPC: worker connecting xprt %p via %s to "
1901 "%s (port %s)\n", xprt,
1902 xprt->address_strings[RPC_DISPLAY_PROTO],
1903 xprt->address_strings[RPC_DISPLAY_ADDR],
1904 xprt->address_strings[RPC_DISPLAY_PORT]);
Chuck Lever16be2d22007-08-06 11:57:38 -04001905
1906 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001907 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1908 xprt, -status, xprt_connected(xprt),
1909 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001910 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001911 default:
1912 printk("%s: connect returned unhandled error %d\n",
1913 __func__, status);
1914 case -EADDRNOTAVAIL:
1915 /* We're probably in TIME_WAIT. Get rid of existing socket,
1916 * and retry
1917 */
1918 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1919 xprt_force_disconnect(xprt);
Trond Myklebust88b5ed72009-06-17 13:22:57 -07001920 break;
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001921 case -ECONNREFUSED:
1922 case -ECONNRESET:
1923 case -ENETUNREACH:
1924 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001925 case 0:
1926 case -EINPROGRESS:
1927 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001928 xprt_clear_connecting(xprt);
1929 return;
Trond Myklebust9fcfe0c2010-03-02 13:06:21 -05001930 case -EINVAL:
1931 /* Happens, for instance, if the user specified a link
1932 * local IPv6 address without a scope-id.
1933 */
1934 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001935 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001936out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001937 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001938out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001939 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001940 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001941}
1942
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001943static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1944 struct sock_xprt *transport)
1945{
1946 struct socket *sock;
1947 int err;
1948
1949 /* start from scratch */
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001950 err = __sock_create(xprt->xprt_net, PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock, 1);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001951 if (err < 0) {
1952 dprintk("RPC: can't create TCP transport socket (%d).\n",
1953 -err);
1954 goto out_err;
1955 }
1956 xs_reclassify_socket4(sock);
1957
1958 if (xs_bind4(transport, sock) < 0) {
1959 sock_release(sock);
1960 goto out_err;
1961 }
1962 return sock;
1963out_err:
1964 return ERR_PTR(-EIO);
1965}
1966
1967/**
Chuck Levera246b012005-08-11 16:25:23 -04001968 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1969 * @work: RPC transport to connect
1970 *
1971 * Invoked by a work queue tasklet.
1972 */
1973static void xs_tcp_connect_worker4(struct work_struct *work)
1974{
1975 struct sock_xprt *transport =
1976 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Levera246b012005-08-11 16:25:23 -04001977
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04001978 xs_tcp_setup_socket(transport, xs_create_tcp_sock4);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001979}
Chuck Levera246b012005-08-11 16:25:23 -04001980
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001981static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1982 struct sock_xprt *transport)
1983{
1984 struct socket *sock;
1985 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001986
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001987 /* start from scratch */
Pavel Emelyanov14ec63c2010-09-29 16:06:57 +04001988 err = __sock_create(xprt->xprt_net, PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock, 1);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001989 if (err < 0) {
1990 dprintk("RPC: can't create TCP transport socket (%d).\n",
1991 -err);
1992 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001993 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001994 xs_reclassify_socket6(sock);
1995
1996 if (xs_bind6(transport, sock) < 0) {
1997 sock_release(sock);
1998 goto out_err;
1999 }
2000 return sock;
2001out_err:
2002 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04002003}
2004
Chuck Lever9903cd12005-08-11 16:25:26 -04002005/**
Chuck Lever68e220b2007-08-06 11:57:48 -04002006 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2007 * @work: RPC transport to connect
2008 *
2009 * Invoked by a work queue tasklet.
2010 */
2011static void xs_tcp_connect_worker6(struct work_struct *work)
2012{
2013 struct sock_xprt *transport =
2014 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Lever68e220b2007-08-06 11:57:48 -04002015
Pavel Emelyanova9f5f0f2010-10-04 16:52:25 +04002016 xs_tcp_setup_socket(transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04002017}
2018
2019/**
Chuck Lever9903cd12005-08-11 16:25:26 -04002020 * xs_connect - connect a socket to a remote endpoint
2021 * @task: address of RPC task that manages state of connect request
2022 *
2023 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07002024 *
2025 * UDP socket connects are synchronous, but we use a work queue anyway
2026 * to guarantee that even unprivileged user processes can set up a
2027 * socket on a privileged port.
2028 *
2029 * If a UDP socket connect fails, the delay behavior here prevents
2030 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04002031 */
2032static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04002033{
2034 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05002035 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002036
Chuck Lever09a21c42009-12-03 15:58:56 -05002037 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002038 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2039 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07002040 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002041 queue_delayed_work(rpciod_workqueue,
2042 &transport->connect_worker,
2043 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07002044 xprt->reestablish_timeout <<= 1;
Neil Brown61d0a8e2009-09-23 14:36:37 -04002045 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2046 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002047 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2048 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04002049 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05002050 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04002051 queue_delayed_work(rpciod_workqueue,
2052 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04002053 }
2054}
2055
Chuck Lever262ca072006-03-20 13:44:16 -05002056/**
2057 * xs_udp_print_stats - display UDP socket-specifc stats
2058 * @xprt: rpc_xprt struct containing statistics
2059 * @seq: output file
2060 *
2061 */
2062static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2063{
Chuck Leverc8475462006-12-05 16:35:26 -05002064 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2065
Chuck Lever262ca072006-03-20 13:44:16 -05002066 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002067 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002068 xprt->stat.bind_count,
2069 xprt->stat.sends,
2070 xprt->stat.recvs,
2071 xprt->stat.bad_xids,
2072 xprt->stat.req_u,
2073 xprt->stat.bklog_u);
2074}
2075
2076/**
2077 * xs_tcp_print_stats - display TCP socket-specifc stats
2078 * @xprt: rpc_xprt struct containing statistics
2079 * @seq: output file
2080 *
2081 */
2082static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2083{
Chuck Leverc8475462006-12-05 16:35:26 -05002084 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002085 long idle_time = 0;
2086
2087 if (xprt_connected(xprt))
2088 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2089
2090 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002091 transport->srcport,
Chuck Lever262ca072006-03-20 13:44:16 -05002092 xprt->stat.bind_count,
2093 xprt->stat.connect_count,
2094 xprt->stat.connect_time,
2095 idle_time,
2096 xprt->stat.sends,
2097 xprt->stat.recvs,
2098 xprt->stat.bad_xids,
2099 xprt->stat.req_u,
2100 xprt->stat.bklog_u);
2101}
2102
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002103/*
2104 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2105 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2106 * to use the server side send routines.
2107 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002108static void *bc_malloc(struct rpc_task *task, size_t size)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002109{
2110 struct page *page;
2111 struct rpc_buffer *buf;
2112
2113 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2114 page = alloc_page(GFP_KERNEL);
2115
2116 if (!page)
2117 return NULL;
2118
2119 buf = page_address(page);
2120 buf->len = PAGE_SIZE;
2121
2122 return buf->data;
2123}
2124
2125/*
2126 * Free the space allocated in the bc_alloc routine
2127 */
H Hartley Sweeten5a51f132010-01-14 15:38:31 -07002128static void bc_free(void *buffer)
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002129{
2130 struct rpc_buffer *buf;
2131
2132 if (!buffer)
2133 return;
2134
2135 buf = container_of(buffer, struct rpc_buffer, data);
2136 free_page((unsigned long)buf);
2137}
2138
2139/*
2140 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2141 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2142 */
2143static int bc_sendto(struct rpc_rqst *req)
2144{
2145 int len;
2146 struct xdr_buf *xbufp = &req->rq_snd_buf;
2147 struct rpc_xprt *xprt = req->rq_xprt;
2148 struct sock_xprt *transport =
2149 container_of(xprt, struct sock_xprt, xprt);
2150 struct socket *sock = transport->sock;
2151 unsigned long headoff;
2152 unsigned long tailoff;
2153
2154 /*
2155 * Set up the rpc header and record marker stuff
2156 */
2157 xs_encode_tcp_record_marker(xbufp);
2158
2159 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2160 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2161 len = svc_send_common(sock, xbufp,
2162 virt_to_page(xbufp->head[0].iov_base), headoff,
2163 xbufp->tail[0].iov_base, tailoff);
2164
2165 if (len != xbufp->len) {
2166 printk(KERN_NOTICE "Error sending entire callback!\n");
2167 len = -EAGAIN;
2168 }
2169
2170 return len;
2171}
2172
2173/*
2174 * The send routine. Borrows from svc_send
2175 */
2176static int bc_send_request(struct rpc_task *task)
2177{
2178 struct rpc_rqst *req = task->tk_rqstp;
2179 struct svc_xprt *xprt;
2180 struct svc_sock *svsk;
2181 u32 len;
2182
2183 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2184 /*
2185 * Get the server socket associated with this callback xprt
2186 */
2187 xprt = req->rq_xprt->bc_xprt;
2188 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2189
2190 /*
2191 * Grab the mutex to serialize data as the connection is shared
2192 * with the fore channel
2193 */
2194 if (!mutex_trylock(&xprt->xpt_mutex)) {
2195 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2196 if (!mutex_trylock(&xprt->xpt_mutex))
2197 return -EAGAIN;
2198 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2199 }
2200 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2201 len = -ENOTCONN;
2202 else
2203 len = bc_sendto(req);
2204 mutex_unlock(&xprt->xpt_mutex);
2205
2206 if (len > 0)
2207 len = 0;
2208
2209 return len;
2210}
2211
2212/*
2213 * The close routine. Since this is client initiated, we do nothing
2214 */
2215
2216static void bc_close(struct rpc_xprt *xprt)
2217{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002218}
2219
2220/*
2221 * The xprt destroy routine. Again, because this connection is client
2222 * initiated, we do nothing
2223 */
2224
2225static void bc_destroy(struct rpc_xprt *xprt)
2226{
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002227}
2228
Chuck Lever262965f2005-08-11 16:25:56 -04002229static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002230 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002231 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002232 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002233 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002234 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002235 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002236 .buf_alloc = rpc_malloc,
2237 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002238 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002239 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002240 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002241 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002242 .close = xs_close,
2243 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002244 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002245};
2246
2247static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002248 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002249 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002250 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002251 .set_port = xs_set_port,
Trond Myklebust0b9e7942010-04-16 16:41:57 -04002252 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002253 .buf_alloc = rpc_malloc,
2254 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002255 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002256 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002257 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002258 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002259 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002260};
2261
Rahul Iyer4cfc7e62009-09-10 17:32:28 +03002262/*
2263 * The rpc_xprt_ops for the server backchannel
2264 */
2265
2266static struct rpc_xprt_ops bc_tcp_ops = {
2267 .reserve_xprt = xprt_reserve_xprt,
2268 .release_xprt = xprt_release_xprt,
2269 .buf_alloc = bc_malloc,
2270 .buf_free = bc_free,
2271 .send_request = bc_send_request,
2272 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2273 .close = bc_close,
2274 .destroy = bc_destroy,
2275 .print_stats = xs_tcp_print_stats,
2276};
2277
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -04002278static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002279 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002280{
2281 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002282 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002283
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002284 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002285 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002286 return ERR_PTR(-EBADF);
2287 }
2288
Pavel Emelyanov37aa2132010-09-29 16:05:43 +04002289 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size);
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002290 if (xprt == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002291 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2292 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002293 return ERR_PTR(-ENOMEM);
2294 }
2295
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +04002296 new = container_of(xprt, struct sock_xprt, xprt);
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002297 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2298 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002299 if (args->srcaddr)
Chuck Leverfbfffbd2009-08-09 15:09:46 -04002300 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002301
2302 return xprt;
2303}
2304
Trond Myklebust2881ae72007-12-20 16:03:54 -05002305static const struct rpc_timeout xs_udp_default_timeout = {
2306 .to_initval = 5 * HZ,
2307 .to_maxval = 30 * HZ,
2308 .to_increment = 5 * HZ,
2309 .to_retries = 5,
2310};
2311
Chuck Lever9903cd12005-08-11 16:25:26 -04002312/**
2313 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002314 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002315 *
2316 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002317static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002318{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002319 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002320 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002321 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002322 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002323
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002324 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002325 if (IS_ERR(xprt))
2326 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002327 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002328
Chuck Leverec739ef2006-08-22 20:06:15 -04002329 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002330 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002331 /* XXX: header size can vary due to auth type, IPv6, etc. */
2332 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2333
Chuck Lever03bf4b72005-08-25 16:25:55 -07002334 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002335 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2336 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002337
Chuck Lever262965f2005-08-11 16:25:56 -04002338 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002339
Trond Myklebustba7392b2007-12-20 16:03:55 -05002340 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002341
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002342 switch (addr->sa_family) {
2343 case AF_INET:
2344 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2345 xprt_set_bound(xprt);
2346
2347 INIT_DELAYED_WORK(&transport->connect_worker,
2348 xs_udp_connect_worker4);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002349 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002350 break;
2351 case AF_INET6:
2352 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2353 xprt_set_bound(xprt);
2354
2355 INIT_DELAYED_WORK(&transport->connect_worker,
2356 xs_udp_connect_worker6);
Chuck Lever9dc3b092009-08-09 15:09:46 -04002357 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002358 break;
2359 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002360 ret = ERR_PTR(-EAFNOSUPPORT);
2361 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002362 }
2363
Chuck Leverc740eff2009-08-09 15:09:46 -04002364 if (xprt_bound(xprt))
2365 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2366 xprt->address_strings[RPC_DISPLAY_ADDR],
2367 xprt->address_strings[RPC_DISPLAY_PORT],
2368 xprt->address_strings[RPC_DISPLAY_PROTO]);
2369 else
2370 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2371 xprt->address_strings[RPC_DISPLAY_ADDR],
2372 xprt->address_strings[RPC_DISPLAY_PROTO]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002373
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002374 if (try_module_get(THIS_MODULE))
2375 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002376 ret = ERR_PTR(-EINVAL);
2377out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002378 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002379 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002380}
2381
Trond Myklebust2881ae72007-12-20 16:03:54 -05002382static const struct rpc_timeout xs_tcp_default_timeout = {
2383 .to_initval = 60 * HZ,
2384 .to_maxval = 60 * HZ,
2385 .to_retries = 2,
2386};
2387
Chuck Lever9903cd12005-08-11 16:25:26 -04002388/**
2389 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002390 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002391 *
2392 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002393static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002394{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002395 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002396 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002397 struct sock_xprt *transport;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002398 struct rpc_xprt *ret;
Chuck Levera246b012005-08-11 16:25:23 -04002399
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002400 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002401 if (IS_ERR(xprt))
2402 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002403 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002404
Chuck Leverec739ef2006-08-22 20:06:15 -04002405 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002406 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002407 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002408
Chuck Lever03bf4b72005-08-25 16:25:55 -07002409 xprt->bind_timeout = XS_BIND_TO;
Chuck Lever03bf4b72005-08-25 16:25:55 -07002410 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2411 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002412
Chuck Lever262965f2005-08-11 16:25:56 -04002413 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002414 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002415
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002416 switch (addr->sa_family) {
2417 case AF_INET:
2418 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2419 xprt_set_bound(xprt);
2420
Chuck Lever9dc3b092009-08-09 15:09:46 -04002421 INIT_DELAYED_WORK(&transport->connect_worker,
2422 xs_tcp_connect_worker4);
2423 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002424 break;
2425 case AF_INET6:
2426 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2427 xprt_set_bound(xprt);
2428
Chuck Lever9dc3b092009-08-09 15:09:46 -04002429 INIT_DELAYED_WORK(&transport->connect_worker,
2430 xs_tcp_connect_worker6);
2431 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002432 break;
2433 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002434 ret = ERR_PTR(-EAFNOSUPPORT);
2435 goto out_err;
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002436 }
2437
Chuck Leverc740eff2009-08-09 15:09:46 -04002438 if (xprt_bound(xprt))
2439 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2440 xprt->address_strings[RPC_DISPLAY_ADDR],
2441 xprt->address_strings[RPC_DISPLAY_PORT],
2442 xprt->address_strings[RPC_DISPLAY_PROTO]);
2443 else
2444 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2445 xprt->address_strings[RPC_DISPLAY_ADDR],
2446 xprt->address_strings[RPC_DISPLAY_PROTO]);
2447
Chuck Leveredb267a2006-08-22 20:06:18 -04002448
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002449 if (try_module_get(THIS_MODULE))
2450 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002451 ret = ERR_PTR(-EINVAL);
2452out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002453 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002454 return ret;
Chuck Levera246b012005-08-11 16:25:23 -04002455}
Chuck Lever282b32e2006-12-05 16:35:51 -05002456
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002457/**
2458 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2459 * @args: rpc transport creation arguments
2460 *
2461 */
2462static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2463{
2464 struct sockaddr *addr = args->dstaddr;
2465 struct rpc_xprt *xprt;
2466 struct sock_xprt *transport;
2467 struct svc_sock *bc_sock;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002468 struct rpc_xprt *ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002469
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002470 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2471 if (IS_ERR(xprt))
2472 return xprt;
2473 transport = container_of(xprt, struct sock_xprt, xprt);
2474
2475 xprt->prot = IPPROTO_TCP;
2476 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2477 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2478 xprt->timeout = &xs_tcp_default_timeout;
2479
2480 /* backchannel */
2481 xprt_set_bound(xprt);
2482 xprt->bind_timeout = 0;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002483 xprt->reestablish_timeout = 0;
2484 xprt->idle_timeout = 0;
2485
2486 /*
2487 * The backchannel uses the same socket connection as the
2488 * forechannel
2489 */
2490 xprt->bc_xprt = args->bc_xprt;
2491 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2492 bc_sock->sk_bc_xprt = xprt;
2493 transport->sock = bc_sock->sk_sock;
2494 transport->inet = bc_sock->sk_sk;
2495
2496 xprt->ops = &bc_tcp_ops;
2497
2498 switch (addr->sa_family) {
2499 case AF_INET:
2500 xs_format_peer_addresses(xprt, "tcp",
2501 RPCBIND_NETID_TCP);
2502 break;
2503 case AF_INET6:
2504 xs_format_peer_addresses(xprt, "tcp",
2505 RPCBIND_NETID_TCP6);
2506 break;
2507 default:
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002508 ret = ERR_PTR(-EAFNOSUPPORT);
2509 goto out_err;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002510 }
2511
2512 if (xprt_bound(xprt))
2513 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2514 xprt->address_strings[RPC_DISPLAY_ADDR],
2515 xprt->address_strings[RPC_DISPLAY_PORT],
2516 xprt->address_strings[RPC_DISPLAY_PROTO]);
2517 else
2518 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2519 xprt->address_strings[RPC_DISPLAY_ADDR],
2520 xprt->address_strings[RPC_DISPLAY_PROTO]);
2521
2522 /*
2523 * Since we don't want connections for the backchannel, we set
2524 * the xprt status to connected
2525 */
2526 xprt_set_connected(xprt);
2527
2528
2529 if (try_module_get(THIS_MODULE))
2530 return xprt;
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002531 ret = ERR_PTR(-EINVAL);
2532out_err:
Pavel Emelyanove204e622010-09-29 16:03:13 +04002533 xprt_free(xprt);
J. Bruce Fields0a68b0b2010-05-26 08:42:24 -04002534 return ret;
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002535}
2536
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002537static struct xprt_class xs_udp_transport = {
2538 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2539 .name = "udp",
2540 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002541 .ident = XPRT_TRANSPORT_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002542 .setup = xs_setup_udp,
2543};
2544
2545static struct xprt_class xs_tcp_transport = {
2546 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2547 .name = "tcp",
2548 .owner = THIS_MODULE,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002549 .ident = XPRT_TRANSPORT_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002550 .setup = xs_setup_tcp,
2551};
2552
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002553static struct xprt_class xs_bc_tcp_transport = {
2554 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2555 .name = "tcp NFSv4.1 backchannel",
2556 .owner = THIS_MODULE,
2557 .ident = XPRT_TRANSPORT_BC_TCP,
2558 .setup = xs_setup_bc_tcp,
2559};
2560
Chuck Lever282b32e2006-12-05 16:35:51 -05002561/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002562 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002563 *
2564 */
2565int init_socket_xprt(void)
2566{
Chuck Leverfbf76682006-12-05 16:35:54 -05002567#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002568 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002569 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002570#endif
2571
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002572 xprt_register_transport(&xs_udp_transport);
2573 xprt_register_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002574 xprt_register_transport(&xs_bc_tcp_transport);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002575
Chuck Lever282b32e2006-12-05 16:35:51 -05002576 return 0;
2577}
2578
2579/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002580 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002581 *
2582 */
2583void cleanup_socket_xprt(void)
2584{
Chuck Leverfbf76682006-12-05 16:35:54 -05002585#ifdef RPC_DEBUG
2586 if (sunrpc_table_header) {
2587 unregister_sysctl_table(sunrpc_table_header);
2588 sunrpc_table_header = NULL;
2589 }
2590#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002591
2592 xprt_unregister_transport(&xs_udp_transport);
2593 xprt_unregister_transport(&xs_tcp_transport);
Alexandros Batsakisf300bab2009-09-10 17:33:30 +03002594 xprt_unregister_transport(&xs_bc_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002595}
Trond Myklebustcbf11072009-08-09 15:06:19 -04002596
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002597static int param_set_uint_minmax(const char *val,
2598 const struct kernel_param *kp,
Trond Myklebustcbf11072009-08-09 15:06:19 -04002599 unsigned int min, unsigned int max)
2600{
2601 unsigned long num;
2602 int ret;
2603
2604 if (!val)
2605 return -EINVAL;
2606 ret = strict_strtoul(val, 0, &num);
2607 if (ret == -EINVAL || num < min || num > max)
2608 return -EINVAL;
2609 *((unsigned int *)kp->arg) = num;
2610 return 0;
2611}
2612
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002613static int param_set_portnr(const char *val, const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002614{
2615 return param_set_uint_minmax(val, kp,
2616 RPC_MIN_RESVPORT,
2617 RPC_MAX_RESVPORT);
2618}
2619
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002620static struct kernel_param_ops param_ops_portnr = {
2621 .set = param_set_portnr,
2622 .get = param_get_uint,
2623};
2624
Trond Myklebustcbf11072009-08-09 15:06:19 -04002625#define param_check_portnr(name, p) \
2626 __param_check(name, p, unsigned int);
2627
2628module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2629module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2630
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002631static int param_set_slot_table_size(const char *val,
2632 const struct kernel_param *kp)
Trond Myklebustcbf11072009-08-09 15:06:19 -04002633{
2634 return param_set_uint_minmax(val, kp,
2635 RPC_MIN_SLOT_TABLE,
2636 RPC_MAX_SLOT_TABLE);
2637}
2638
Rusty Russell9bbb9e52010-08-11 23:04:12 -06002639static struct kernel_param_ops param_ops_slot_table_size = {
2640 .set = param_set_slot_table_size,
2641 .get = param_get_uint,
2642};
2643
Trond Myklebustcbf11072009-08-09 15:06:19 -04002644#define param_check_slot_table_size(name, p) \
2645 __param_check(name, p, unsigned int);
2646
2647module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2648 slot_table_size, 0644);
2649module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2650 slot_table_size, 0644);
2651