blob: 8975c10591c366ea38786b4566b4639aa9b7f101 [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>
\"Talpey, Thomas\49c36fc2007-09-10 13:47:31 -040035#include <linux/sunrpc/xprtsock.h>
Chuck Levera246b012005-08-11 16:25:23 -040036#include <linux/file.h>
37
38#include <net/sock.h>
39#include <net/checksum.h>
40#include <net/udp.h>
41#include <net/tcp.h>
42
Chuck Lever9903cd12005-08-11 16:25:26 -040043/*
Chuck Leverc556b752005-11-01 12:24:48 -050044 * xprtsock tunables
45 */
46unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
48
49unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51
Trond Myklebust7d1e8252009-03-11 14:38:03 -040052#define XS_TCP_LINGER_TO (15U * HZ)
Trond Myklebust25fe6142009-03-11 14:38:03 -040053static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
Trond Myklebust7d1e8252009-03-11 14:38:03 -040054
Chuck Leverc556b752005-11-01 12:24:48 -050055/*
Chuck Leverfbf76682006-12-05 16:35:54 -050056 * We can register our own files under /proc/sys/sunrpc by
57 * calling register_sysctl_table() again. The files in that
58 * directory become the union of all files registered there.
59 *
60 * We simply need to make sure that we don't collide with
61 * someone else's file names!
62 */
63
64#ifdef RPC_DEBUG
65
66static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
67static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
68static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
69static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
70
71static struct ctl_table_header *sunrpc_table_header;
72
73/*
74 * FIXME: changing the UDP slot table size should also resize the UDP
75 * socket buffers for existing UDP transports
76 */
77static ctl_table xs_tunables_table[] = {
78 {
79 .ctl_name = CTL_SLOTTABLE_UDP,
80 .procname = "udp_slot_table_entries",
81 .data = &xprt_udp_slot_table_entries,
82 .maxlen = sizeof(unsigned int),
83 .mode = 0644,
84 .proc_handler = &proc_dointvec_minmax,
85 .strategy = &sysctl_intvec,
86 .extra1 = &min_slot_table_size,
87 .extra2 = &max_slot_table_size
88 },
89 {
90 .ctl_name = CTL_SLOTTABLE_TCP,
91 .procname = "tcp_slot_table_entries",
92 .data = &xprt_tcp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
95 .proc_handler = &proc_dointvec_minmax,
96 .strategy = &sysctl_intvec,
97 .extra1 = &min_slot_table_size,
98 .extra2 = &max_slot_table_size
99 },
100 {
101 .ctl_name = CTL_MIN_RESVPORT,
102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
106 .proc_handler = &proc_dointvec_minmax,
107 .strategy = &sysctl_intvec,
108 .extra1 = &xprt_min_resvport_limit,
109 .extra2 = &xprt_max_resvport_limit
110 },
111 {
112 .ctl_name = CTL_MAX_RESVPORT,
113 .procname = "max_resvport",
114 .data = &xprt_max_resvport,
115 .maxlen = sizeof(unsigned int),
116 .mode = 0644,
117 .proc_handler = &proc_dointvec_minmax,
118 .strategy = &sysctl_intvec,
119 .extra1 = &xprt_min_resvport_limit,
120 .extra2 = &xprt_max_resvport_limit
121 },
122 {
Trond Myklebust25fe6142009-03-11 14:38:03 -0400123 .procname = "tcp_fin_timeout",
124 .data = &xs_tcp_fin_timeout,
125 .maxlen = sizeof(xs_tcp_fin_timeout),
126 .mode = 0644,
127 .proc_handler = &proc_dointvec_jiffies,
128 .strategy = sysctl_jiffies
129 },
130 {
Chuck Leverfbf76682006-12-05 16:35:54 -0500131 .ctl_name = 0,
132 },
133};
134
135static ctl_table sunrpc_table[] = {
136 {
137 .ctl_name = CTL_SUNRPC,
138 .procname = "sunrpc",
139 .mode = 0555,
140 .child = xs_tunables_table
141 },
142 {
143 .ctl_name = 0,
144 },
145};
146
147#endif
148
149/*
Chuck Lever03bf4b72005-08-25 16:25:55 -0700150 * Time out for an RPC UDP socket connect. UDP socket connects are
151 * synchronous, but we set a timeout anyway in case of resource
152 * exhaustion on the local host.
153 */
154#define XS_UDP_CONN_TO (5U * HZ)
155
156/*
157 * Wait duration for an RPC TCP connection to be established. Solaris
158 * NFS over TCP uses 60 seconds, for example, which is in line with how
159 * long a server takes to reboot.
160 */
161#define XS_TCP_CONN_TO (60U * HZ)
162
163/*
164 * Wait duration for a reply from the RPC portmapper.
165 */
166#define XS_BIND_TO (60U * HZ)
167
168/*
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
171 */
172#define XS_UDP_REEST_TO (2U * HZ)
173
174/*
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
177 *
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
182 */
183#define XS_TCP_INIT_REEST_TO (3U * HZ)
184#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
185
186/*
187 * TCP idle timeout; client drops the transport socket if it is idle
188 * for this long. Note that we also timeout UDP sockets to prevent
189 * holding port numbers when there is no RPC traffic.
190 */
191#define XS_IDLE_DISC_TO (5U * 60 * HZ)
192
Chuck Levera246b012005-08-11 16:25:23 -0400193#ifdef RPC_DEBUG
194# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400195# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400196#endif
197
Chuck Levera246b012005-08-11 16:25:23 -0400198#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400199static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400200{
Chuck Lever9903cd12005-08-11 16:25:26 -0400201 u8 *buf = (u8 *) packet;
202 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400203
Chuck Lever46121cf2007-01-31 12:14:08 -0500204 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400205 for (j = 0; j < count && j < 128; j += 4) {
206 if (!(j & 31)) {
207 if (j)
208 dprintk("\n");
209 dprintk("0x%04x ", j);
210 }
211 dprintk("%02x%02x%02x%02x ",
212 buf[j], buf[j+1], buf[j+2], buf[j+3]);
213 }
214 dprintk("\n");
215}
216#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400217static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400218{
219 /* NOP */
220}
221#endif
222
Chuck Leverffc2e512006-12-05 16:35:11 -0500223struct sock_xprt {
224 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500225
226 /*
227 * Network layer
228 */
229 struct socket * sock;
230 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500231
232 /*
233 * State of TCP reply receive
234 */
235 __be32 tcp_fraghdr,
236 tcp_xid;
237
238 u32 tcp_offset,
239 tcp_reclen;
240
241 unsigned long tcp_copied,
242 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500243
244 /*
245 * Connection of transports
246 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500247 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200248 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500249 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500250
251 /*
252 * UDP socket buffer size parameters
253 */
254 size_t rcvsize,
255 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500256
257 /*
258 * Saved socket callback addresses
259 */
260 void (*old_data_ready)(struct sock *, int);
261 void (*old_state_change)(struct sock *);
262 void (*old_write_space)(struct sock *);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400263 void (*old_error_report)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500264};
265
Chuck Levere136d092006-12-05 16:35:23 -0500266/*
267 * TCP receive state flags
268 */
269#define TCP_RCV_LAST_FRAG (1UL << 0)
270#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
271#define TCP_RCV_COPY_XID (1UL << 2)
272#define TCP_RCV_COPY_DATA (1UL << 3)
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400273#define TCP_RCV_COPY_CALLDIR (1UL << 4)
274
275/*
276 * TCP RPC flags
277 */
278#define TCP_RPC_REPLY (1UL << 5)
Chuck Levere136d092006-12-05 16:35:23 -0500279
Chuck Lever95392c52007-08-06 11:57:58 -0400280static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400281{
Chuck Lever95392c52007-08-06 11:57:58 -0400282 return (struct sockaddr *) &xprt->addr;
283}
284
285static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
286{
287 return (struct sockaddr_in *) &xprt->addr;
288}
289
290static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
291{
292 return (struct sockaddr_in6 *) &xprt->addr;
293}
294
Chuck Leverb454ae92008-01-07 18:34:48 -0500295static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
296 const char *protocol,
297 const char *netid)
Chuck Leveredb267a2006-08-22 20:06:18 -0400298{
Chuck Lever95392c52007-08-06 11:57:58 -0400299 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400300 char *buf;
301
302 buf = kzalloc(20, GFP_KERNEL);
303 if (buf) {
David S. Millere0db4a72008-11-02 23:57:06 -0800304 snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
Chuck Leveredb267a2006-08-22 20:06:18 -0400305 }
306 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
307
308 buf = kzalloc(8, GFP_KERNEL);
309 if (buf) {
310 snprintf(buf, 8, "%u",
311 ntohs(addr->sin_port));
312 }
313 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
314
Chuck Leverb454ae92008-01-07 18:34:48 -0500315 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Leveredb267a2006-08-22 20:06:18 -0400316
317 buf = kzalloc(48, GFP_KERNEL);
318 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700319 snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
320 &addr->sin_addr.s_addr,
Chuck Leveredb267a2006-08-22 20:06:18 -0400321 ntohs(addr->sin_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500322 protocol);
Chuck Leveredb267a2006-08-22 20:06:18 -0400323 }
324 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400325
326 buf = kzalloc(10, GFP_KERNEL);
327 if (buf) {
328 snprintf(buf, 10, "%02x%02x%02x%02x",
329 NIPQUAD(addr->sin_addr.s_addr));
330 }
331 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
332
333 buf = kzalloc(8, GFP_KERNEL);
334 if (buf) {
335 snprintf(buf, 8, "%4hx",
336 ntohs(addr->sin_port));
337 }
338 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400339
340 buf = kzalloc(30, GFP_KERNEL);
341 if (buf) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700342 snprintf(buf, 30, "%pI4.%u.%u",
343 &addr->sin_addr.s_addr,
Chuck Lever756805e2007-08-16 16:03:26 -0400344 ntohs(addr->sin_port) >> 8,
345 ntohs(addr->sin_port) & 0xff);
346 }
347 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400348
Chuck Leverb454ae92008-01-07 18:34:48 -0500349 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400350}
351
Chuck Leverb454ae92008-01-07 18:34:48 -0500352static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
353 const char *protocol,
354 const char *netid)
Chuck Lever4b6473f2007-08-06 11:57:12 -0400355{
Chuck Lever95392c52007-08-06 11:57:58 -0400356 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400357 char *buf;
358
359 buf = kzalloc(40, GFP_KERNEL);
360 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700361 snprintf(buf, 40, "%pI6",&addr->sin6_addr);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400362 }
363 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
364
365 buf = kzalloc(8, GFP_KERNEL);
366 if (buf) {
367 snprintf(buf, 8, "%u",
368 ntohs(addr->sin6_port));
369 }
370 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
371
Chuck Leverb454ae92008-01-07 18:34:48 -0500372 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
Chuck Lever4b6473f2007-08-06 11:57:12 -0400373
374 buf = kzalloc(64, GFP_KERNEL);
375 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700376 snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700377 &addr->sin6_addr,
Chuck Lever4b6473f2007-08-06 11:57:12 -0400378 ntohs(addr->sin6_port),
Chuck Leverb454ae92008-01-07 18:34:48 -0500379 protocol);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400380 }
381 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
382
383 buf = kzalloc(36, GFP_KERNEL);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700384 if (buf)
Harvey Harrison4b7a4272008-10-29 12:50:24 -0700385 snprintf(buf, 36, "%pi6", &addr->sin6_addr);
Harvey Harrisonb0711952008-10-28 16:05:40 -0700386
Chuck Lever4b6473f2007-08-06 11:57:12 -0400387 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
388
389 buf = kzalloc(8, GFP_KERNEL);
390 if (buf) {
391 snprintf(buf, 8, "%4hx",
392 ntohs(addr->sin6_port));
393 }
394 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400395
396 buf = kzalloc(50, GFP_KERNEL);
397 if (buf) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700398 snprintf(buf, 50, "%pI6.%u.%u",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -0700399 &addr->sin6_addr,
400 ntohs(addr->sin6_port) >> 8,
401 ntohs(addr->sin6_port) & 0xff);
Chuck Lever756805e2007-08-16 16:03:26 -0400402 }
403 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400404
Chuck Leverb454ae92008-01-07 18:34:48 -0500405 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
Chuck Leveredb267a2006-08-22 20:06:18 -0400406}
407
408static void xs_free_peer_addresses(struct rpc_xprt *xprt)
409{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500410 unsigned int i;
411
412 for (i = 0; i < RPC_DISPLAY_MAX; i++)
413 switch (i) {
414 case RPC_DISPLAY_PROTO:
415 case RPC_DISPLAY_NETID:
416 continue;
417 default:
418 kfree(xprt->address_strings[i]);
419 }
Chuck Leveredb267a2006-08-22 20:06:18 -0400420}
421
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400422#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
423
Trond Myklebust24c56842006-10-17 15:06:22 -0400424static 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 -0400425{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400426 struct msghdr msg = {
427 .msg_name = addr,
428 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400429 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
430 };
431 struct kvec iov = {
432 .iov_base = vec->iov_base + base,
433 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400434 };
435
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400437 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
438 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
439}
440
Trond Myklebust24c56842006-10-17 15:06:22 -0400441static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400442{
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 struct page **ppage;
444 unsigned int remainder;
445 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400446
Trond Myklebust24c56842006-10-17 15:06:22 -0400447 remainder = xdr->page_len - base;
448 base += xdr->page_base;
449 ppage = xdr->pages + (base >> PAGE_SHIFT);
450 base &= ~PAGE_MASK;
451 for(;;) {
452 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
453 int flags = XS_SENDMSG_FLAGS;
454
455 remainder -= len;
456 if (remainder != 0 || more)
457 flags |= MSG_MORE;
458 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
459 if (remainder == 0 || err != len)
460 break;
461 sent += err;
462 ppage++;
463 base = 0;
464 }
465 if (sent == 0)
466 return err;
467 if (err > 0)
468 sent += err;
469 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400470}
471
Chuck Lever9903cd12005-08-11 16:25:26 -0400472/**
473 * xs_sendpages - write pages directly to a socket
474 * @sock: socket to send on
475 * @addr: UDP only -- address of destination
476 * @addrlen: UDP only -- length of destination address
477 * @xdr: buffer containing this request
478 * @base: starting position in the buffer
479 *
Chuck Levera246b012005-08-11 16:25:23 -0400480 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400481static 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 -0400482{
Trond Myklebust24c56842006-10-17 15:06:22 -0400483 unsigned int remainder = xdr->len - base;
484 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400485
Chuck Lever262965f2005-08-11 16:25:56 -0400486 if (unlikely(!sock))
Trond Myklebustfba91af2009-03-11 14:06:41 -0400487 return -ENOTSOCK;
Chuck Lever262965f2005-08-11 16:25:56 -0400488
489 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400490 if (base != 0) {
491 addr = NULL;
492 addrlen = 0;
493 }
Chuck Lever262965f2005-08-11 16:25:56 -0400494
Trond Myklebust24c56842006-10-17 15:06:22 -0400495 if (base < xdr->head[0].iov_len || addr != NULL) {
496 unsigned int len = xdr->head[0].iov_len - base;
497 remainder -= len;
498 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
499 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400500 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400501 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400502 base = 0;
503 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400504 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400505
Trond Myklebust24c56842006-10-17 15:06:22 -0400506 if (base < xdr->page_len) {
507 unsigned int len = xdr->page_len - base;
508 remainder -= len;
509 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
510 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400511 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400512 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400513 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400514 } else
515 base -= xdr->page_len;
516
517 if (base >= xdr->tail[0].iov_len)
518 return sent;
519 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400520out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400521 if (sent == 0)
522 return err;
523 if (err > 0)
524 sent += err;
525 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400526}
527
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400528static void xs_nospace_callback(struct rpc_task *task)
529{
530 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
531
532 transport->inet->sk_write_pending--;
533 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
534}
535
Chuck Lever9903cd12005-08-11 16:25:26 -0400536/**
Chuck Lever262965f2005-08-11 16:25:56 -0400537 * xs_nospace - place task on wait queue if transmit was incomplete
538 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400539 *
Chuck Levera246b012005-08-11 16:25:23 -0400540 */
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400541static int xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400542{
Chuck Lever262965f2005-08-11 16:25:56 -0400543 struct rpc_rqst *req = task->tk_rqstp;
544 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500545 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400546 int ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400547
Chuck Lever46121cf2007-01-31 12:14:08 -0500548 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400549 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
550 req->rq_slen);
551
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400552 /* Protect against races with write_space */
553 spin_lock_bh(&xprt->transport_lock);
Chuck Lever262965f2005-08-11 16:25:56 -0400554
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400555 /* Don't race with disconnect */
556 if (xprt_connected(xprt)) {
557 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400558 ret = -EAGAIN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400559 /*
560 * Notify TCP that we're limited by the application
561 * window size
562 */
563 set_bit(SOCK_NOSPACE, &transport->sock->flags);
564 transport->inet->sk_write_pending++;
565 /* ...and wait for more buffer space */
566 xprt_wait_for_buffer_space(task, xs_nospace_callback);
567 }
568 } else {
569 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400570 ret = -ENOTCONN;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400571 }
Chuck Lever262965f2005-08-11 16:25:56 -0400572
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400573 spin_unlock_bh(&xprt->transport_lock);
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400574 return ret;
Chuck Lever262965f2005-08-11 16:25:56 -0400575}
576
577/**
578 * xs_udp_send_request - write an RPC request to a UDP socket
579 * @task: address of RPC task that manages the state of an RPC request
580 *
581 * Return values:
582 * 0: The request has been sent
583 * EAGAIN: The socket was blocked, please call again later to
584 * complete the request
585 * ENOTCONN: Caller needs to invoke connect logic then call again
586 * other: Some other error occured, the request was not sent
587 */
588static int xs_udp_send_request(struct rpc_task *task)
589{
590 struct rpc_rqst *req = task->tk_rqstp;
591 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500592 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400593 struct xdr_buf *xdr = &req->rq_snd_buf;
594 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400595
Chuck Lever9903cd12005-08-11 16:25:26 -0400596 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400597 req->rq_svec->iov_base,
598 req->rq_svec->iov_len);
599
Trond Myklebust01d37c42009-03-11 14:09:39 -0400600 if (!xprt_bound(xprt))
601 return -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500602 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400603 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500604 xprt->addrlen, xdr,
605 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400606
Chuck Lever46121cf2007-01-31 12:14:08 -0500607 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400608 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400609
Trond Myklebust21997002007-10-01 11:43:37 -0400610 if (status >= 0) {
611 task->tk_bytes_sent += status;
612 if (status >= req->rq_slen)
613 return 0;
614 /* Still some bytes left; set up for a retry later. */
Chuck Lever262965f2005-08-11 16:25:56 -0400615 status = -EAGAIN;
Trond Myklebust21997002007-10-01 11:43:37 -0400616 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400617 if (!transport->sock)
618 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400619
Chuck Lever262965f2005-08-11 16:25:56 -0400620 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400621 case -ENOTSOCK:
622 status = -ENOTCONN;
623 /* Should we call xs_close() here? */
624 break;
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400625 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400626 status = xs_nospace(task);
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400627 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -0400628 default:
629 dprintk("RPC: sendmsg returned unrecognized error %d\n",
630 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400631 case -ENETUNREACH:
632 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400633 case -ECONNREFUSED:
634 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400635 * prompts ECONNREFUSED. */
Trond Myklebustb6ddf642008-04-17 18:52:19 -0400636 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400637 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400638out:
Chuck Lever262965f2005-08-11 16:25:56 -0400639 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400640}
641
Trond Myklebuste06799f2007-11-05 15:44:12 -0500642/**
643 * xs_tcp_shutdown - gracefully shut down a TCP socket
644 * @xprt: transport
645 *
646 * Initiates a graceful shutdown of the TCP socket by calling the
647 * equivalent of shutdown(SHUT_WR);
648 */
649static void xs_tcp_shutdown(struct rpc_xprt *xprt)
650{
651 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
652 struct socket *sock = transport->sock;
653
654 if (sock != NULL)
655 kernel_sock_shutdown(sock, SHUT_WR);
656}
657
Chuck Lever808012f2005-08-25 16:25:49 -0700658static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
659{
660 u32 reclen = buf->len - sizeof(rpc_fraghdr);
661 rpc_fraghdr *base = buf->head[0].iov_base;
662 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
663}
664
Chuck Lever9903cd12005-08-11 16:25:26 -0400665/**
Chuck Lever262965f2005-08-11 16:25:56 -0400666 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400667 * @task: address of RPC task that manages the state of an RPC request
668 *
669 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400670 * 0: The request has been sent
671 * EAGAIN: The socket was blocked, please call again later to
672 * complete the request
673 * ENOTCONN: Caller needs to invoke connect logic then call again
674 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 *
676 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400677 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400678 */
Chuck Lever262965f2005-08-11 16:25:56 -0400679static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400680{
681 struct rpc_rqst *req = task->tk_rqstp;
682 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500683 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400684 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400685 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400686
Chuck Lever808012f2005-08-25 16:25:49 -0700687 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400688
Chuck Lever262965f2005-08-11 16:25:56 -0400689 xs_pktdump("packet data:",
690 req->rq_svec->iov_base,
691 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400692
693 /* Continue transmitting the packet/record. We must be careful
694 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400695 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400696 while (1) {
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500697 status = xs_sendpages(transport->sock,
698 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400699
Chuck Lever46121cf2007-01-31 12:14:08 -0500700 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400701 xdr->len - req->rq_bytes_sent, status);
702
703 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400704 break;
705
Chuck Lever262965f2005-08-11 16:25:56 -0400706 /* If we've sent the entire packet, immediately
707 * reset the count of bytes sent. */
708 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500709 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400710 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
711 req->rq_bytes_sent = 0;
712 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400713 }
714
Trond Myklebust06b4b682008-04-16 16:51:38 -0400715 if (status != 0)
716 continue;
Chuck Levera246b012005-08-11 16:25:23 -0400717 status = -EAGAIN;
Trond Myklebust06b4b682008-04-16 16:51:38 -0400718 break;
Chuck Levera246b012005-08-11 16:25:23 -0400719 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400720 if (!transport->sock)
721 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400722
Chuck Lever262965f2005-08-11 16:25:56 -0400723 switch (status) {
Trond Myklebustfba91af2009-03-11 14:06:41 -0400724 case -ENOTSOCK:
725 status = -ENOTCONN;
726 /* Should we call xs_close() here? */
727 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400728 case -EAGAIN:
Trond Myklebust5e3771c2009-03-11 14:38:01 -0400729 status = xs_nospace(task);
Chuck Lever262965f2005-08-11 16:25:56 -0400730 break;
731 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500732 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400733 -status);
Chuck Lever262965f2005-08-11 16:25:56 -0400734 case -ECONNRESET:
Trond Myklebust55420c22009-03-11 15:29:24 -0400735 case -EPIPE:
Trond Myklebuste06799f2007-11-05 15:44:12 -0500736 xs_tcp_shutdown(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400737 case -ECONNREFUSED:
738 case -ENOTCONN:
Chuck Lever262965f2005-08-11 16:25:56 -0400739 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
Chuck Levera246b012005-08-11 16:25:23 -0400740 }
Trond Myklebustc8485e42009-03-11 14:37:59 -0400741out:
Chuck Levera246b012005-08-11 16:25:23 -0400742 return status;
743}
744
Chuck Lever9903cd12005-08-11 16:25:26 -0400745/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400746 * xs_tcp_release_xprt - clean up after a tcp transmission
747 * @xprt: transport
748 * @task: rpc task
749 *
750 * This cleans up if an error causes us to abort the transmission of a request.
751 * In this case, the socket may need to be reset in order to avoid confusing
752 * the server.
753 */
754static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
755{
756 struct rpc_rqst *req;
757
758 if (task != xprt->snd_task)
759 return;
760 if (task == NULL)
761 goto out_release;
762 req = task->tk_rqstp;
763 if (req->rq_bytes_sent == 0)
764 goto out_release;
765 if (req->rq_bytes_sent == req->rq_snd_buf.len)
766 goto out_release;
767 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
768out_release:
769 xprt_release_xprt(xprt, task);
770}
771
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400772static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
773{
774 transport->old_data_ready = sk->sk_data_ready;
775 transport->old_state_change = sk->sk_state_change;
776 transport->old_write_space = sk->sk_write_space;
777 transport->old_error_report = sk->sk_error_report;
778}
779
780static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
781{
782 sk->sk_data_ready = transport->old_data_ready;
783 sk->sk_state_change = transport->old_state_change;
784 sk->sk_write_space = transport->old_write_space;
785 sk->sk_error_report = transport->old_error_report;
786}
787
Chuck Leverfe315e72009-03-11 14:10:21 -0400788static void xs_reset_transport(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400789{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500790 struct socket *sock = transport->sock;
791 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400792
Chuck Leverfe315e72009-03-11 14:10:21 -0400793 if (sk == NULL)
794 return;
Chuck Lever9903cd12005-08-11 16:25:26 -0400795
Chuck Levera246b012005-08-11 16:25:23 -0400796 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500797 transport->inet = NULL;
798 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400799
Chuck Lever9903cd12005-08-11 16:25:26 -0400800 sk->sk_user_data = NULL;
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -0400801
802 xs_restore_old_callbacks(transport, sk);
Chuck Levera246b012005-08-11 16:25:23 -0400803 write_unlock_bh(&sk->sk_callback_lock);
804
Chuck Lever9903cd12005-08-11 16:25:26 -0400805 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400806
807 sock_release(sock);
Chuck Leverfe315e72009-03-11 14:10:21 -0400808}
809
810/**
811 * xs_close - close a socket
812 * @xprt: transport
813 *
814 * This is used when all requests are complete; ie, no DRC state remains
815 * on the server we want to save.
Trond Myklebustf75e6742009-04-21 17:18:20 -0400816 *
817 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
818 * xs_reset_transport() zeroing the socket from underneath a writer.
Chuck Leverfe315e72009-03-11 14:10:21 -0400819 */
820static void xs_close(struct rpc_xprt *xprt)
821{
822 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
823
824 dprintk("RPC: xs_close xprt %p\n", xprt);
825
826 xs_reset_transport(transport);
827
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100828 smp_mb__before_clear_bit();
Trond Myklebust7d1e8252009-03-11 14:38:03 -0400829 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100830 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -0500831 clear_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100832 smp_mb__after_clear_bit();
Trond Myklebust62da3b22007-11-06 18:44:20 -0500833 xprt_disconnect_done(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400834}
835
Trond Myklebustf75e6742009-04-21 17:18:20 -0400836static void xs_tcp_close(struct rpc_xprt *xprt)
837{
838 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
839 xs_close(xprt);
840 else
841 xs_tcp_shutdown(xprt);
842}
843
Chuck Lever9903cd12005-08-11 16:25:26 -0400844/**
845 * xs_destroy - prepare to shutdown a transport
846 * @xprt: doomed transport
847 *
848 */
849static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400850{
Chuck Leverc8475462006-12-05 16:35:26 -0500851 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
852
Chuck Lever46121cf2007-01-31 12:14:08 -0500853 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400854
Trond Myklebustc1384c92007-06-14 18:00:42 -0400855 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400856
Chuck Lever9903cd12005-08-11 16:25:26 -0400857 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400858 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400859 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400860 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400861 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400862}
863
Chuck Lever9903cd12005-08-11 16:25:26 -0400864static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400865{
Chuck Lever9903cd12005-08-11 16:25:26 -0400866 return (struct rpc_xprt *) sk->sk_user_data;
867}
868
869/**
870 * xs_udp_data_ready - "data ready" callback for UDP sockets
871 * @sk: socket with data to read
872 * @len: how much data to read
873 *
874 */
875static void xs_udp_data_ready(struct sock *sk, int len)
876{
877 struct rpc_task *task;
878 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400879 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400880 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400881 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700882 u32 _xid;
883 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400884
885 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500886 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400887 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400888 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400889
890 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
891 goto out;
892
893 if (xprt->shutdown)
894 goto dropit;
895
896 repsize = skb->len - sizeof(struct udphdr);
897 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500898 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400899 goto dropit;
900 }
901
902 /* Copy the XID from the skb... */
903 xp = skb_header_pointer(skb, sizeof(struct udphdr),
904 sizeof(_xid), &_xid);
905 if (xp == NULL)
906 goto dropit;
907
908 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400909 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400910 rovr = xprt_lookup_rqst(xprt, *xp);
911 if (!rovr)
912 goto out_unlock;
913 task = rovr->rq_task;
914
Chuck Levera246b012005-08-11 16:25:23 -0400915 if ((copied = rovr->rq_private_buf.buflen) > repsize)
916 copied = repsize;
917
918 /* Suck it into the iovec, verify checksum if not done by hw. */
Herbert Xu1781f7f2007-12-11 11:30:32 -0800919 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
920 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
Chuck Levera246b012005-08-11 16:25:23 -0400921 goto out_unlock;
Herbert Xu1781f7f2007-12-11 11:30:32 -0800922 }
923
924 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
Chuck Levera246b012005-08-11 16:25:23 -0400925
926 /* Something worked... */
927 dst_confirm(skb->dst);
928
Chuck Lever1570c1e2005-08-25 16:25:52 -0700929 xprt_adjust_cwnd(task, copied);
930 xprt_update_rtt(task);
931 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400932
933 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400934 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400935 dropit:
936 skb_free_datagram(sk, skb);
937 out:
938 read_unlock(&sk->sk_callback_lock);
939}
940
Chuck Leverdd456472006-12-05 16:35:44 -0500941static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400942{
Chuck Lever51971132006-12-05 16:35:19 -0500943 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400944 size_t len, used;
945 char *p;
946
Chuck Lever51971132006-12-05 16:35:19 -0500947 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
948 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500949 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500950 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400951 if (used != len)
952 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700953
Chuck Lever51971132006-12-05 16:35:19 -0500954 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
955 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500956 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400957 else
Chuck Levere136d092006-12-05 16:35:23 -0500958 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500959 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700960
Chuck Levere136d092006-12-05 16:35:23 -0500961 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500962 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700963
Chuck Levera246b012005-08-11 16:25:23 -0400964 /* Sanity check of the record length */
Ricardo Labiaga18dca022009-04-01 09:22:53 -0400965 if (unlikely(transport->tcp_reclen < 8)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500966 dprintk("RPC: invalid TCP record fragment length\n");
Trond Myklebust3ebb0672007-11-06 18:40:12 -0500967 xprt_force_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400968 return;
Chuck Levera246b012005-08-11 16:25:23 -0400969 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500970 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500971 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400972}
973
Chuck Lever51971132006-12-05 16:35:19 -0500974static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400975{
Chuck Lever51971132006-12-05 16:35:19 -0500976 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500977 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500978 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500979 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
980 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
981 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500982 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400983 }
984 }
985}
986
Chuck Leverdd456472006-12-05 16:35:44 -0500987static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400988{
989 size_t len, used;
990 char *p;
991
Chuck Lever51971132006-12-05 16:35:19 -0500992 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500993 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500994 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500995 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500996 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400997 if (used != len)
998 return;
Chuck Levere136d092006-12-05 16:35:23 -0500999 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001000 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
Chuck Lever51971132006-12-05 16:35:19 -05001001 transport->tcp_copied = 4;
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001002 dprintk("RPC: reading %s XID %08x\n",
1003 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1004 : "request with",
Chuck Lever51971132006-12-05 16:35:19 -05001005 ntohl(transport->tcp_xid));
1006 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001007}
1008
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001009static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1010 struct xdr_skb_reader *desc)
1011{
1012 size_t len, used;
1013 u32 offset;
1014 __be32 calldir;
1015
1016 /*
1017 * We want transport->tcp_offset to be 8 at the end of this routine
1018 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1019 * When this function is called for the first time,
1020 * transport->tcp_offset is 4 (after having already read the xid).
1021 */
1022 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1023 len = sizeof(calldir) - offset;
1024 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1025 used = xdr_skb_read_bits(desc, &calldir, len);
1026 transport->tcp_offset += used;
1027 if (used != len)
1028 return;
1029 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1030 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1031 transport->tcp_copied += 4;
1032 if (ntohl(calldir) == RPC_REPLY)
1033 transport->tcp_flags |= TCP_RPC_REPLY;
1034 else
1035 transport->tcp_flags &= ~TCP_RPC_REPLY;
1036 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
1037 (transport->tcp_flags & TCP_RPC_REPLY) ?
1038 "reply for" : "request with", calldir);
1039 xs_tcp_check_fraghdr(transport);
1040}
1041
Chuck Leverdd456472006-12-05 16:35:44 -05001042static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001043{
Chuck Lever51971132006-12-05 16:35:19 -05001044 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001045 struct rpc_rqst *req;
1046 struct xdr_buf *rcvbuf;
1047 size_t len;
1048 ssize_t r;
1049
1050 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001051 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001052 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -04001053 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -05001054 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001055 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -05001056 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001057 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001058 return;
1059 }
1060
1061 rcvbuf = &req->rq_private_buf;
1062 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -05001063 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -05001064 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -04001065
Chuck Lever51971132006-12-05 16:35:19 -05001066 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001067 memcpy(&my_desc, desc, sizeof(my_desc));
1068 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -05001069 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001070 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001071 desc->count -= r;
1072 desc->offset += r;
1073 } else
Chuck Lever51971132006-12-05 16:35:19 -05001074 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -05001075 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -04001076
1077 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -05001078 transport->tcp_copied += r;
1079 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -04001080 }
1081 if (r != len) {
1082 /* Error when copying to the receive buffer,
1083 * usually because we weren't able to allocate
1084 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -05001085 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -04001086 * will not receive any additional updates,
1087 * and time out.
1088 * Any remaining data from this record will
1089 * be discarded.
1090 */
Chuck Levere136d092006-12-05 16:35:23 -05001091 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -05001092 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -05001093 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -05001094 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1095 "tcp_offset = %u, tcp_reclen = %u\n",
1096 xprt, transport->tcp_copied,
1097 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001098 goto out;
1099 }
1100
Chuck Lever46121cf2007-01-31 12:14:08 -05001101 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -05001102 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -05001103 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1104 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1105 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -04001106
Chuck Lever51971132006-12-05 16:35:19 -05001107 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -05001108 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -05001109 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -05001110 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1111 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -04001112 }
1113
1114out:
Chuck Levere136d092006-12-05 16:35:23 -05001115 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -05001116 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001117 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -05001118 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001119}
1120
Chuck Leverdd456472006-12-05 16:35:44 -05001121static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001122{
1123 size_t len;
1124
Chuck Lever51971132006-12-05 16:35:19 -05001125 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001126 if (len > desc->count)
1127 len = desc->count;
1128 desc->count -= len;
1129 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001130 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001131 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001132 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001133}
1134
Chuck Lever9903cd12005-08-11 16:25:26 -04001135static 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 -04001136{
1137 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001138 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001139 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001140 .skb = skb,
1141 .offset = offset,
1142 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001143 };
Chuck Levera246b012005-08-11 16:25:23 -04001144
Chuck Lever46121cf2007-01-31 12:14:08 -05001145 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001146 do {
1147 /* Read in a new fragment marker if necessary */
1148 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001149 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001150 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001151 continue;
1152 }
1153 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001154 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001155 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001156 continue;
1157 }
Ricardo Labiaga18dca022009-04-01 09:22:53 -04001158 /* Read in the call/reply flag */
1159 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1160 xs_tcp_read_calldir(transport, &desc);
1161 continue;
1162 }
Chuck Levera246b012005-08-11 16:25:23 -04001163 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001164 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001165 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001166 continue;
1167 }
1168 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001169 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001170 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001171 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001172 return len - desc.count;
1173}
1174
Chuck Lever9903cd12005-08-11 16:25:26 -04001175/**
1176 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1177 * @sk: socket with data to read
1178 * @bytes: how much data to read
1179 *
1180 */
1181static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001182{
1183 struct rpc_xprt *xprt;
1184 read_descriptor_t rd_desc;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001185 int read;
Chuck Levera246b012005-08-11 16:25:23 -04001186
Chuck Lever46121cf2007-01-31 12:14:08 -05001187 dprintk("RPC: xs_tcp_data_ready...\n");
1188
Chuck Levera246b012005-08-11 16:25:23 -04001189 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001190 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001191 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001192 if (xprt->shutdown)
1193 goto out;
1194
Chuck Lever9903cd12005-08-11 16:25:26 -04001195 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001196 rd_desc.arg.data = xprt;
Trond Myklebustff2d7db2008-02-25 21:40:51 -08001197 do {
1198 rd_desc.count = 65536;
1199 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1200 } while (read > 0);
Chuck Levera246b012005-08-11 16:25:23 -04001201out:
1202 read_unlock(&sk->sk_callback_lock);
1203}
1204
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001205/*
1206 * Do the equivalent of linger/linger2 handling for dealing with
1207 * broken servers that don't close the socket in a timely
1208 * fashion
1209 */
1210static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1211 unsigned long timeout)
1212{
1213 struct sock_xprt *transport;
1214
1215 if (xprt_test_and_set_connecting(xprt))
1216 return;
1217 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1218 transport = container_of(xprt, struct sock_xprt, xprt);
1219 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1220 timeout);
1221}
1222
1223static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1224{
1225 struct sock_xprt *transport;
1226
1227 transport = container_of(xprt, struct sock_xprt, xprt);
1228
1229 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1230 !cancel_delayed_work(&transport->connect_worker))
1231 return;
1232 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1233 xprt_clear_connecting(xprt);
1234}
1235
1236static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1237{
1238 smp_mb__before_clear_bit();
1239 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1240 clear_bit(XPRT_CLOSING, &xprt->state);
1241 smp_mb__after_clear_bit();
1242 /* Mark transport as closed and wake up all pending tasks */
1243 xprt_disconnect_done(xprt);
1244}
1245
Chuck Lever9903cd12005-08-11 16:25:26 -04001246/**
1247 * xs_tcp_state_change - callback to handle TCP socket state changes
1248 * @sk: socket whose state has changed
1249 *
1250 */
1251static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001252{
Chuck Lever9903cd12005-08-11 16:25:26 -04001253 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001254
1255 read_lock(&sk->sk_callback_lock);
1256 if (!(xprt = xprt_from_sock(sk)))
1257 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001258 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1259 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1260 sk->sk_state, xprt_connected(xprt),
1261 sock_flag(sk, SOCK_DEAD),
1262 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001263
1264 switch (sk->sk_state) {
1265 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001266 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001267 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001268 struct sock_xprt *transport = container_of(xprt,
1269 struct sock_xprt, xprt);
1270
Chuck Levera246b012005-08-11 16:25:23 -04001271 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001272 transport->tcp_offset = 0;
1273 transport->tcp_reclen = 0;
1274 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001275 transport->tcp_flags =
1276 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001277
Trond Myklebust2a491992009-03-11 14:38:00 -04001278 xprt_wake_pending_tasks(xprt, -EAGAIN);
Chuck Levera246b012005-08-11 16:25:23 -04001279 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001280 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001281 break;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001282 case TCP_FIN_WAIT1:
1283 /* The client initiated a shutdown of the socket */
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001284 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001285 xprt->reestablish_timeout = 0;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001286 set_bit(XPRT_CLOSING, &xprt->state);
1287 smp_mb__before_clear_bit();
1288 clear_bit(XPRT_CONNECTED, &xprt->state);
Trond Myklebustef803672007-12-31 16:19:17 -05001289 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001290 smp_mb__after_clear_bit();
Trond Myklebust25fe6142009-03-11 14:38:03 -04001291 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Chuck Levera246b012005-08-11 16:25:23 -04001292 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001293 case TCP_CLOSE_WAIT:
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001294 /* The server initiated a shutdown of the socket */
Trond Myklebust66af1e52007-11-06 10:18:36 -05001295 xprt_force_disconnect(xprt);
Trond Myklebust663b8852008-01-01 18:42:12 -05001296 case TCP_SYN_SENT:
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001297 xprt->connect_cookie++;
Trond Myklebust663b8852008-01-01 18:42:12 -05001298 case TCP_CLOSING:
1299 /*
1300 * If the server closed down the connection, make sure that
1301 * we back off before reconnecting
1302 */
1303 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1304 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001305 break;
1306 case TCP_LAST_ACK:
Trond Myklebust670f9452009-03-11 14:37:58 -04001307 set_bit(XPRT_CLOSING, &xprt->state);
Trond Myklebust25fe6142009-03-11 14:38:03 -04001308 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
Trond Myklebust3b948ae2007-11-05 17:42:39 -05001309 smp_mb__before_clear_bit();
1310 clear_bit(XPRT_CONNECTED, &xprt->state);
1311 smp_mb__after_clear_bit();
1312 break;
1313 case TCP_CLOSE:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001314 xs_tcp_cancel_linger_timeout(xprt);
1315 xs_sock_mark_closed(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001316 }
1317 out:
1318 read_unlock(&sk->sk_callback_lock);
1319}
1320
Chuck Lever9903cd12005-08-11 16:25:26 -04001321/**
Trond Myklebust482f32e2009-03-11 14:38:00 -04001322 * xs_error_report - callback mainly for catching socket errors
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001323 * @sk: socket
1324 */
Trond Myklebust482f32e2009-03-11 14:38:00 -04001325static void xs_error_report(struct sock *sk)
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001326{
1327 struct rpc_xprt *xprt;
1328
1329 read_lock(&sk->sk_callback_lock);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001330 if (!(xprt = xprt_from_sock(sk)))
1331 goto out;
1332 dprintk("RPC: %s client %p...\n"
1333 "RPC: error %d\n",
1334 __func__, xprt, sk->sk_err);
Trond Myklebust482f32e2009-03-11 14:38:00 -04001335 xprt_wake_pending_tasks(xprt, -EAGAIN);
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001336out:
1337 read_unlock(&sk->sk_callback_lock);
1338}
1339
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001340static void xs_write_space(struct sock *sk)
1341{
1342 struct socket *sock;
1343 struct rpc_xprt *xprt;
1344
1345 if (unlikely(!(sock = sk->sk_socket)))
1346 return;
1347 clear_bit(SOCK_NOSPACE, &sock->flags);
1348
1349 if (unlikely(!(xprt = xprt_from_sock(sk))))
1350 return;
1351 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1352 return;
1353
1354 xprt_write_space(xprt);
1355}
1356
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001357/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001358 * xs_udp_write_space - callback invoked when socket buffer space
1359 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001360 * @sk: socket whose state has changed
1361 *
Chuck Levera246b012005-08-11 16:25:23 -04001362 * Called when more output buffer space is available for this socket.
1363 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001364 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001365 * with a bunch of small requests.
1366 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001367static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001368{
Chuck Levera246b012005-08-11 16:25:23 -04001369 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001370
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001371 /* from net/core/sock.c:sock_def_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001372 if (sock_writeable(sk))
1373 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001374
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001375 read_unlock(&sk->sk_callback_lock);
1376}
Chuck Levera246b012005-08-11 16:25:23 -04001377
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001378/**
1379 * xs_tcp_write_space - callback invoked when socket buffer space
1380 * becomes available
1381 * @sk: socket whose state has changed
1382 *
1383 * Called when more output buffer space is available for this socket.
1384 * We try not to wake our writers until they can make "significant"
1385 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1386 * with a bunch of small requests.
1387 */
1388static void xs_tcp_write_space(struct sock *sk)
1389{
1390 read_lock(&sk->sk_callback_lock);
1391
1392 /* from net/core/stream.c:sk_stream_write_space */
Ilpo Järvinen1f0fa152009-02-06 23:48:33 -08001393 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1394 xs_write_space(sk);
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001395
Chuck Levera246b012005-08-11 16:25:23 -04001396 read_unlock(&sk->sk_callback_lock);
1397}
1398
Chuck Lever470056c2005-08-25 16:25:56 -07001399static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001400{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001401 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1402 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001403
Chuck Lever7c6e0662006-12-05 16:35:30 -05001404 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001405 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001406 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001407 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001408 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001409 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001410 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001411 sk->sk_write_space(sk);
1412 }
1413}
1414
Chuck Lever43118c22005-08-25 16:25:49 -07001415/**
Chuck Lever470056c2005-08-25 16:25:56 -07001416 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001417 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001418 * @sndsize: requested size of send buffer, in bytes
1419 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001420 *
Chuck Lever470056c2005-08-25 16:25:56 -07001421 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001422 */
Chuck Lever470056c2005-08-25 16:25:56 -07001423static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001424{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001425 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1426
1427 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001428 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001429 transport->sndsize = sndsize + 1024;
1430 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001431 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001432 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001433
1434 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001435}
1436
Chuck Lever46c0ee82005-08-25 16:25:52 -07001437/**
1438 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1439 * @task: task that timed out
1440 *
1441 * Adjust the congestion window after a retransmit timeout has occurred.
1442 */
1443static void xs_udp_timer(struct rpc_task *task)
1444{
1445 xprt_adjust_cwnd(task, -ETIMEDOUT);
1446}
1447
Chuck Leverb85d8802006-05-25 01:40:49 -04001448static unsigned short xs_get_random_port(void)
1449{
1450 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1451 unsigned short rand = (unsigned short) net_random() % range;
1452 return rand + xprt_min_resvport;
1453}
1454
Chuck Lever92200412006-01-03 09:55:51 +01001455/**
1456 * xs_set_port - reset the port number in the remote endpoint address
1457 * @xprt: generic transport
1458 * @port: new port number
1459 *
1460 */
1461static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1462{
Chuck Lever95392c52007-08-06 11:57:58 -04001463 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001464
Chuck Lever46121cf2007-01-31 12:14:08 -05001465 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001466
Chuck Lever20612002007-08-06 11:57:23 -04001467 switch (addr->sa_family) {
1468 case AF_INET:
1469 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1470 break;
1471 case AF_INET6:
1472 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1473 break;
1474 default:
1475 BUG();
1476 }
Chuck Lever92200412006-01-03 09:55:51 +01001477}
1478
Trond Myklebust67a391d2007-11-05 17:40:58 -05001479static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1480{
1481 unsigned short port = transport->port;
1482
1483 if (port == 0 && transport->xprt.resvport)
1484 port = xs_get_random_port();
1485 return port;
1486}
1487
1488static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1489{
1490 if (transport->port != 0)
1491 transport->port = 0;
1492 if (!transport->xprt.resvport)
1493 return 0;
1494 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1495 return xprt_max_resvport;
1496 return --port;
1497}
1498
Chuck Lever7dc753f2007-08-06 11:57:28 -04001499static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001500{
1501 struct sockaddr_in myaddr = {
1502 .sin_family = AF_INET,
1503 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001504 struct sockaddr_in *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001505 int err, nloop = 0;
1506 unsigned short port = xs_get_srcport(transport, sock);
1507 unsigned short last;
Chuck Levera246b012005-08-11 16:25:23 -04001508
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001509 sa = (struct sockaddr_in *)&transport->addr;
1510 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001511 do {
1512 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001513 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001514 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001515 if (port == 0)
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001516 break;
Chuck Levera246b012005-08-11 16:25:23 -04001517 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001518 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001519 break;
Chuck Levera246b012005-08-11 16:25:23 -04001520 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001521 last = port;
1522 port = xs_next_srcport(transport, sock, port);
1523 if (port > last)
1524 nloop++;
1525 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison21454aa2008-10-31 00:54:56 -07001526 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1527 __func__, &myaddr.sin_addr,
Chuck Lever7dc753f2007-08-06 11:57:28 -04001528 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001529 return err;
1530}
1531
Chuck Lever90058d32007-08-06 11:57:33 -04001532static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1533{
1534 struct sockaddr_in6 myaddr = {
1535 .sin6_family = AF_INET6,
1536 };
1537 struct sockaddr_in6 *sa;
Trond Myklebust67a391d2007-11-05 17:40:58 -05001538 int err, nloop = 0;
1539 unsigned short port = xs_get_srcport(transport, sock);
1540 unsigned short last;
Chuck Lever90058d32007-08-06 11:57:33 -04001541
Chuck Lever90058d32007-08-06 11:57:33 -04001542 sa = (struct sockaddr_in6 *)&transport->addr;
1543 myaddr.sin6_addr = sa->sin6_addr;
1544 do {
1545 myaddr.sin6_port = htons(port);
1546 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1547 sizeof(myaddr));
Trond Myklebust67a391d2007-11-05 17:40:58 -05001548 if (port == 0)
Chuck Lever90058d32007-08-06 11:57:33 -04001549 break;
1550 if (err == 0) {
1551 transport->port = port;
1552 break;
1553 }
Trond Myklebust67a391d2007-11-05 17:40:58 -05001554 last = port;
1555 port = xs_next_srcport(transport, sock, port);
1556 if (port > last)
1557 nloop++;
1558 } while (err == -EADDRINUSE && nloop != 2);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001559 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001560 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001561 return err;
1562}
1563
Peter Zijlstraed075362006-12-06 20:35:24 -08001564#ifdef CONFIG_DEBUG_LOCK_ALLOC
1565static struct lock_class_key xs_key[2];
1566static struct lock_class_key xs_slock_key[2];
1567
Chuck Lever8945ee52007-08-06 11:58:04 -04001568static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001569{
1570 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001571
John Heffner02b3d342007-09-12 10:42:12 +02001572 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001573 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1574 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1575}
Peter Zijlstraed075362006-12-06 20:35:24 -08001576
Chuck Lever8945ee52007-08-06 11:58:04 -04001577static inline void xs_reclassify_socket6(struct socket *sock)
1578{
1579 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001580
Linus Torvaldsf4921af2007-10-15 10:46:05 -07001581 BUG_ON(sock_owned_by_user(sk));
Chuck Lever8945ee52007-08-06 11:58:04 -04001582 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1583 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001584}
1585#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001586static inline void xs_reclassify_socket4(struct socket *sock)
1587{
1588}
1589
1590static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001591{
1592}
1593#endif
1594
Chuck Lever16be2d22007-08-06 11:57:38 -04001595static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001596{
Chuck Lever16be2d22007-08-06 11:57:38 -04001597 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001598
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001599 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001600 struct sock *sk = sock->sk;
1601
1602 write_lock_bh(&sk->sk_callback_lock);
1603
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001604 xs_save_old_callbacks(transport, sk);
1605
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001606 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001607 sk->sk_data_ready = xs_udp_data_ready;
1608 sk->sk_write_space = xs_udp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001609 sk->sk_error_report = xs_error_report;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001610 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001611 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001612
1613 xprt_set_connected(xprt);
1614
1615 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001616 transport->sock = sock;
1617 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001618
1619 write_unlock_bh(&sk->sk_callback_lock);
1620 }
Chuck Lever470056c2005-08-25 16:25:56 -07001621 xs_udp_do_set_buffer_size(xprt);
Chuck Lever16be2d22007-08-06 11:57:38 -04001622}
1623
Chuck Levera246b012005-08-11 16:25:23 -04001624/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001625 * xs_udp_connect_worker4 - set up a UDP socket
Chuck Levera246b012005-08-11 16:25:23 -04001626 * @work: RPC transport to connect
1627 *
1628 * Invoked by a work queue tasklet.
1629 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001630static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001631{
1632 struct sock_xprt *transport =
1633 container_of(work, struct sock_xprt, connect_worker.work);
1634 struct rpc_xprt *xprt = &transport->xprt;
1635 struct socket *sock = transport->sock;
1636 int err, status = -EIO;
1637
Trond Myklebust01d37c42009-03-11 14:09:39 -04001638 if (xprt->shutdown)
Chuck Lever9903cd12005-08-11 16:25:26 -04001639 goto out;
1640
Chuck Levera246b012005-08-11 16:25:23 -04001641 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001642 xs_reset_transport(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001643
Chuck Leverfe315e72009-03-11 14:10:21 -04001644 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1645 if (err < 0) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001646 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001647 goto out;
1648 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001649 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001650
Chuck Lever7dc753f2007-08-06 11:57:28 -04001651 if (xs_bind4(transport, sock)) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001652 sock_release(sock);
1653 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001654 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001655
1656 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1657 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1658
Chuck Lever16be2d22007-08-06 11:57:38 -04001659 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001660 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001661out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001662 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001663 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001664}
1665
Chuck Lever68e220b2007-08-06 11:57:48 -04001666/**
1667 * xs_udp_connect_worker6 - set up a UDP socket
1668 * @work: RPC transport to connect
1669 *
1670 * Invoked by a work queue tasklet.
1671 */
1672static void xs_udp_connect_worker6(struct work_struct *work)
1673{
1674 struct sock_xprt *transport =
1675 container_of(work, struct sock_xprt, connect_worker.work);
1676 struct rpc_xprt *xprt = &transport->xprt;
1677 struct socket *sock = transport->sock;
1678 int err, status = -EIO;
1679
Trond Myklebust01d37c42009-03-11 14:09:39 -04001680 if (xprt->shutdown)
Chuck Lever68e220b2007-08-06 11:57:48 -04001681 goto out;
1682
1683 /* Start by resetting any existing state */
Chuck Leverfe315e72009-03-11 14:10:21 -04001684 xs_reset_transport(transport);
Chuck Lever68e220b2007-08-06 11:57:48 -04001685
Chuck Leverfe315e72009-03-11 14:10:21 -04001686 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1687 if (err < 0) {
Chuck Lever68e220b2007-08-06 11:57:48 -04001688 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1689 goto out;
1690 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001691 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001692
1693 if (xs_bind6(transport, sock) < 0) {
1694 sock_release(sock);
1695 goto out;
1696 }
1697
1698 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1699 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1700
1701 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001702 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001703out:
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001704 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001705 xprt_wake_pending_tasks(xprt, status);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001706}
1707
Chuck Lever3167e122005-08-25 16:25:55 -07001708/*
1709 * We need to preserve the port number so the reply cache on the server can
1710 * find our cached RPC replies when we get around to reconnecting.
1711 */
Trond Myklebust40d25492009-03-11 14:37:58 -04001712static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
Chuck Lever3167e122005-08-25 16:25:55 -07001713{
1714 int result;
Chuck Lever3167e122005-08-25 16:25:55 -07001715 struct sockaddr any;
1716
Chuck Lever46121cf2007-01-31 12:14:08 -05001717 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001718
1719 /*
1720 * Disconnect the transport socket by doing a connect operation
1721 * with AF_UNSPEC. This should return immediately...
1722 */
1723 memset(&any, 0, sizeof(any));
1724 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001725 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001726 if (!result)
1727 xs_sock_mark_closed(xprt);
1728 else
Chuck Lever46121cf2007-01-31 12:14:08 -05001729 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001730 result);
1731}
1732
Trond Myklebust40d25492009-03-11 14:37:58 -04001733static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1734{
1735 unsigned int state = transport->inet->sk_state;
1736
1737 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1738 return;
1739 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1740 return;
1741 xs_abort_connection(xprt, transport);
1742}
1743
Chuck Lever16be2d22007-08-06 11:57:38 -04001744static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001745{
Chuck Lever16be2d22007-08-06 11:57:38 -04001746 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001747
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001748 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001749 struct sock *sk = sock->sk;
1750
1751 write_lock_bh(&sk->sk_callback_lock);
1752
Trond Myklebust2a9e1cf2008-10-28 15:21:39 -04001753 xs_save_old_callbacks(transport, sk);
1754
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001755 sk->sk_user_data = xprt;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001756 sk->sk_data_ready = xs_tcp_data_ready;
1757 sk->sk_state_change = xs_tcp_state_change;
1758 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebust482f32e2009-03-11 14:38:00 -04001759 sk->sk_error_report = xs_error_report;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001760 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001761
1762 /* socket options */
1763 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1764 sock_reset_flag(sk, SOCK_LINGER);
1765 tcp_sk(sk)->linger2 = 0;
1766 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001767
1768 xprt_clear_connected(xprt);
1769
1770 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001771 transport->sock = sock;
1772 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001773
1774 write_unlock_bh(&sk->sk_callback_lock);
1775 }
1776
Trond Myklebust01d37c42009-03-11 14:09:39 -04001777 if (!xprt_bound(xprt))
1778 return -ENOTCONN;
1779
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001780 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001781 xprt->stat.connect_count++;
1782 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001783 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001784}
1785
1786/**
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001787 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1788 * @xprt: RPC transport to connect
1789 * @transport: socket transport to connect
1790 * @create_sock: function to create a socket of the correct type
Chuck Lever16be2d22007-08-06 11:57:38 -04001791 *
1792 * Invoked by a work queue tasklet.
1793 */
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001794static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1795 struct sock_xprt *transport,
1796 struct socket *(*create_sock)(struct rpc_xprt *,
1797 struct sock_xprt *))
Chuck Lever16be2d22007-08-06 11:57:38 -04001798{
Chuck Lever16be2d22007-08-06 11:57:38 -04001799 struct socket *sock = transport->sock;
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001800 int status = -EIO;
Chuck Lever16be2d22007-08-06 11:57:38 -04001801
Trond Myklebust01d37c42009-03-11 14:09:39 -04001802 if (xprt->shutdown)
Chuck Lever16be2d22007-08-06 11:57:38 -04001803 goto out;
1804
1805 if (!sock) {
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001806 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001807 sock = create_sock(xprt, transport);
1808 if (IS_ERR(sock)) {
1809 status = PTR_ERR(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001810 goto out;
1811 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001812 } else {
1813 int abort_and_exit;
1814
1815 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1816 &xprt->state);
Chuck Lever16be2d22007-08-06 11:57:38 -04001817 /* "close" the socket, preserving the local port */
Trond Myklebust40d25492009-03-11 14:37:58 -04001818 xs_tcp_reuse_connection(xprt, transport);
Chuck Lever16be2d22007-08-06 11:57:38 -04001819
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001820 if (abort_and_exit)
1821 goto out_eagain;
1822 }
1823
Chuck Lever16be2d22007-08-06 11:57:38 -04001824 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1825 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1826
1827 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001828 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1829 xprt, -status, xprt_connected(xprt),
1830 sock->sk->sk_state);
Trond Myklebust2a491992009-03-11 14:38:00 -04001831 switch (status) {
Trond Myklebustf75e6742009-04-21 17:18:20 -04001832 default:
1833 printk("%s: connect returned unhandled error %d\n",
1834 __func__, status);
1835 case -EADDRNOTAVAIL:
1836 /* We're probably in TIME_WAIT. Get rid of existing socket,
1837 * and retry
1838 */
1839 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1840 xprt_force_disconnect(xprt);
Trond Myklebust8a2cec22009-03-11 14:38:01 -04001841 case -ECONNREFUSED:
1842 case -ECONNRESET:
1843 case -ENETUNREACH:
1844 /* retry with existing socket, after a delay */
Trond Myklebust2a491992009-03-11 14:38:00 -04001845 case 0:
1846 case -EINPROGRESS:
1847 case -EALREADY:
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001848 xprt_clear_connecting(xprt);
1849 return;
Chuck Levera246b012005-08-11 16:25:23 -04001850 }
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001851out_eagain:
Trond Myklebust2a491992009-03-11 14:38:00 -04001852 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -04001853out:
Chuck Lever2226feb2005-08-11 16:25:38 -04001854 xprt_clear_connecting(xprt);
Trond Myklebust7d1e8252009-03-11 14:38:03 -04001855 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001856}
1857
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001858static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1859 struct sock_xprt *transport)
1860{
1861 struct socket *sock;
1862 int err;
1863
1864 /* start from scratch */
1865 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1866 if (err < 0) {
1867 dprintk("RPC: can't create TCP transport socket (%d).\n",
1868 -err);
1869 goto out_err;
1870 }
1871 xs_reclassify_socket4(sock);
1872
1873 if (xs_bind4(transport, sock) < 0) {
1874 sock_release(sock);
1875 goto out_err;
1876 }
1877 return sock;
1878out_err:
1879 return ERR_PTR(-EIO);
1880}
1881
1882/**
Chuck Levera246b012005-08-11 16:25:23 -04001883 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1884 * @work: RPC transport to connect
1885 *
1886 * Invoked by a work queue tasklet.
1887 */
1888static void xs_tcp_connect_worker4(struct work_struct *work)
1889{
1890 struct sock_xprt *transport =
1891 container_of(work, struct sock_xprt, connect_worker.work);
1892 struct rpc_xprt *xprt = &transport->xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001893
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001894 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1895}
Chuck Levera246b012005-08-11 16:25:23 -04001896
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001897static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1898 struct sock_xprt *transport)
1899{
1900 struct socket *sock;
1901 int err;
Chuck Lever9903cd12005-08-11 16:25:26 -04001902
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001903 /* start from scratch */
1904 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1905 if (err < 0) {
1906 dprintk("RPC: can't create TCP transport socket (%d).\n",
1907 -err);
1908 goto out_err;
Chuck Levera246b012005-08-11 16:25:23 -04001909 }
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001910 xs_reclassify_socket6(sock);
1911
1912 if (xs_bind6(transport, sock) < 0) {
1913 sock_release(sock);
1914 goto out_err;
1915 }
1916 return sock;
1917out_err:
1918 return ERR_PTR(-EIO);
Chuck Levera246b012005-08-11 16:25:23 -04001919}
1920
Chuck Lever9903cd12005-08-11 16:25:26 -04001921/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001922 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1923 * @work: RPC transport to connect
1924 *
1925 * Invoked by a work queue tasklet.
1926 */
1927static void xs_tcp_connect_worker6(struct work_struct *work)
1928{
1929 struct sock_xprt *transport =
1930 container_of(work, struct sock_xprt, connect_worker.work);
1931 struct rpc_xprt *xprt = &transport->xprt;
Chuck Lever68e220b2007-08-06 11:57:48 -04001932
Trond Myklebustb61d59f2009-03-11 14:38:04 -04001933 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
Chuck Lever68e220b2007-08-06 11:57:48 -04001934}
1935
1936/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001937 * xs_connect - connect a socket to a remote endpoint
1938 * @task: address of RPC task that manages state of connect request
1939 *
1940 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001941 *
1942 * UDP socket connects are synchronous, but we use a work queue anyway
1943 * to guarantee that even unprivileged user processes can set up a
1944 * socket on a privileged port.
1945 *
1946 * If a UDP socket connect fails, the delay behavior here prevents
1947 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001948 */
1949static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001950{
1951 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001952 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001953
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001954 if (xprt_test_and_set_connecting(xprt))
1955 return;
1956
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001957 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001958 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1959 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001960 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001961 queue_delayed_work(rpciod_workqueue,
1962 &transport->connect_worker,
1963 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001964 xprt->reestablish_timeout <<= 1;
1965 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1966 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001967 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001968 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001969 queue_delayed_work(rpciod_workqueue,
1970 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001971 }
1972}
1973
Trond Myklebuste06799f2007-11-05 15:44:12 -05001974static void xs_tcp_connect(struct rpc_task *task)
1975{
1976 struct rpc_xprt *xprt = task->tk_xprt;
1977
Trond Myklebuste06799f2007-11-05 15:44:12 -05001978 /* Exit if we need to wait for socket shutdown to complete */
1979 if (test_bit(XPRT_CLOSING, &xprt->state))
1980 return;
1981 xs_connect(task);
1982}
1983
Chuck Lever262ca072006-03-20 13:44:16 -05001984/**
1985 * xs_udp_print_stats - display UDP socket-specifc stats
1986 * @xprt: rpc_xprt struct containing statistics
1987 * @seq: output file
1988 *
1989 */
1990static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1991{
Chuck Leverc8475462006-12-05 16:35:26 -05001992 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1993
Chuck Lever262ca072006-03-20 13:44:16 -05001994 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001995 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001996 xprt->stat.bind_count,
1997 xprt->stat.sends,
1998 xprt->stat.recvs,
1999 xprt->stat.bad_xids,
2000 xprt->stat.req_u,
2001 xprt->stat.bklog_u);
2002}
2003
2004/**
2005 * xs_tcp_print_stats - display TCP socket-specifc stats
2006 * @xprt: rpc_xprt struct containing statistics
2007 * @seq: output file
2008 *
2009 */
2010static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2011{
Chuck Leverc8475462006-12-05 16:35:26 -05002012 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05002013 long idle_time = 0;
2014
2015 if (xprt_connected(xprt))
2016 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2017
2018 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05002019 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05002020 xprt->stat.bind_count,
2021 xprt->stat.connect_count,
2022 xprt->stat.connect_time,
2023 idle_time,
2024 xprt->stat.sends,
2025 xprt->stat.recvs,
2026 xprt->stat.bad_xids,
2027 xprt->stat.req_u,
2028 xprt->stat.bklog_u);
2029}
2030
Chuck Lever262965f2005-08-11 16:25:56 -04002031static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07002032 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07002033 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07002034 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04002035 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002036 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04002037 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002038 .buf_alloc = rpc_malloc,
2039 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002040 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002041 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07002042 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07002043 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04002044 .close = xs_close,
2045 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002046 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04002047};
2048
2049static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07002050 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002051 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04002052 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01002053 .set_port = xs_set_port,
Trond Myklebuste06799f2007-11-05 15:44:12 -05002054 .connect = xs_tcp_connect,
Chuck Lever02107142006-01-03 09:55:49 +01002055 .buf_alloc = rpc_malloc,
2056 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04002057 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07002058 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Trond Myklebustf75e6742009-04-21 17:18:20 -04002059 .close = xs_tcp_close,
Chuck Lever9903cd12005-08-11 16:25:26 -04002060 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05002061 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04002062};
2063
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -04002064static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002065 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04002066{
2067 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05002068 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002069
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002070 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002071 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002072 return ERR_PTR(-EBADF);
2073 }
2074
Chuck Leverffc2e512006-12-05 16:35:11 -05002075 new = kzalloc(sizeof(*new), GFP_KERNEL);
2076 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002077 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2078 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002079 return ERR_PTR(-ENOMEM);
2080 }
Chuck Leverffc2e512006-12-05 16:35:11 -05002081 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002082
2083 xprt->max_reqs = slot_table_size;
2084 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2085 if (xprt->slot == NULL) {
2086 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05002087 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2088 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04002089 return ERR_PTR(-ENOMEM);
2090 }
2091
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002092 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2093 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02002094 if (args->srcaddr)
2095 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002096
2097 return xprt;
2098}
2099
Trond Myklebust2881ae72007-12-20 16:03:54 -05002100static const struct rpc_timeout xs_udp_default_timeout = {
2101 .to_initval = 5 * HZ,
2102 .to_maxval = 30 * HZ,
2103 .to_increment = 5 * HZ,
2104 .to_retries = 5,
2105};
2106
Chuck Lever9903cd12005-08-11 16:25:26 -04002107/**
2108 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002109 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002110 *
2111 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002112static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002113{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002114 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002115 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002116 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002117
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002118 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002119 if (IS_ERR(xprt))
2120 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002121 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002122
Chuck Leverec739ef2006-08-22 20:06:15 -04002123 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07002124 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04002125 /* XXX: header size can vary due to auth type, IPv6, etc. */
2126 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2127
Chuck Lever03bf4b72005-08-25 16:25:55 -07002128 xprt->bind_timeout = XS_BIND_TO;
2129 xprt->connect_timeout = XS_UDP_CONN_TO;
2130 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2131 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002132
Chuck Lever262965f2005-08-11 16:25:56 -04002133 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04002134
Trond Myklebustba7392b2007-12-20 16:03:55 -05002135 xprt->timeout = &xs_udp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002136
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002137 switch (addr->sa_family) {
2138 case AF_INET:
2139 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2140 xprt_set_bound(xprt);
2141
2142 INIT_DELAYED_WORK(&transport->connect_worker,
2143 xs_udp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002144 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002145 break;
2146 case AF_INET6:
2147 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2148 xprt_set_bound(xprt);
2149
2150 INIT_DELAYED_WORK(&transport->connect_worker,
2151 xs_udp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002152 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002153 break;
2154 default:
2155 kfree(xprt);
2156 return ERR_PTR(-EAFNOSUPPORT);
2157 }
2158
Chuck Lever46121cf2007-01-31 12:14:08 -05002159 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002160 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002161
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002162 if (try_module_get(THIS_MODULE))
2163 return xprt;
2164
2165 kfree(xprt->slot);
2166 kfree(xprt);
2167 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002168}
2169
Trond Myklebust2881ae72007-12-20 16:03:54 -05002170static const struct rpc_timeout xs_tcp_default_timeout = {
2171 .to_initval = 60 * HZ,
2172 .to_maxval = 60 * HZ,
2173 .to_retries = 2,
2174};
2175
Chuck Lever9903cd12005-08-11 16:25:26 -04002176/**
2177 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002178 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04002179 *
2180 */
Adrian Bunk483066d2007-10-24 18:24:02 +02002181static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04002182{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002183 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04002184 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002185 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04002186
Frank van Maarseveen96802a02007-07-08 13:08:54 +02002187 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04002188 if (IS_ERR(xprt))
2189 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05002190 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04002191
Chuck Leverec739ef2006-08-22 20:06:15 -04002192 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07002193 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07002194 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04002195
Chuck Lever03bf4b72005-08-25 16:25:55 -07002196 xprt->bind_timeout = XS_BIND_TO;
2197 xprt->connect_timeout = XS_TCP_CONN_TO;
2198 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2199 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04002200
Chuck Lever262965f2005-08-11 16:25:56 -04002201 xprt->ops = &xs_tcp_ops;
Trond Myklebustba7392b2007-12-20 16:03:55 -05002202 xprt->timeout = &xs_tcp_default_timeout;
Chuck Levera246b012005-08-11 16:25:23 -04002203
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002204 switch (addr->sa_family) {
2205 case AF_INET:
2206 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2207 xprt_set_bound(xprt);
2208
2209 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Leverb454ae92008-01-07 18:34:48 -05002210 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002211 break;
2212 case AF_INET6:
2213 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2214 xprt_set_bound(xprt);
2215
2216 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
Chuck Leverb454ae92008-01-07 18:34:48 -05002217 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
Chuck Lever8f9d5b12007-08-06 11:57:53 -04002218 break;
2219 default:
2220 kfree(xprt);
2221 return ERR_PTR(-EAFNOSUPPORT);
2222 }
2223
Chuck Lever46121cf2007-01-31 12:14:08 -05002224 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05002225 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04002226
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002227 if (try_module_get(THIS_MODULE))
2228 return xprt;
2229
2230 kfree(xprt->slot);
2231 kfree(xprt);
2232 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04002233}
Chuck Lever282b32e2006-12-05 16:35:51 -05002234
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002235static struct xprt_class xs_udp_transport = {
2236 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2237 .name = "udp",
2238 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002239 .ident = IPPROTO_UDP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002240 .setup = xs_setup_udp,
2241};
2242
2243static struct xprt_class xs_tcp_transport = {
2244 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2245 .name = "tcp",
2246 .owner = THIS_MODULE,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -04002247 .ident = IPPROTO_TCP,
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002248 .setup = xs_setup_tcp,
2249};
2250
Chuck Lever282b32e2006-12-05 16:35:51 -05002251/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002252 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05002253 *
2254 */
2255int init_socket_xprt(void)
2256{
Chuck Leverfbf76682006-12-05 16:35:54 -05002257#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08002258 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08002259 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05002260#endif
2261
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002262 xprt_register_transport(&xs_udp_transport);
2263 xprt_register_transport(&xs_tcp_transport);
2264
Chuck Lever282b32e2006-12-05 16:35:51 -05002265 return 0;
2266}
2267
2268/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002269 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05002270 *
2271 */
2272void cleanup_socket_xprt(void)
2273{
Chuck Leverfbf76682006-12-05 16:35:54 -05002274#ifdef RPC_DEBUG
2275 if (sunrpc_table_header) {
2276 unregister_sysctl_table(sunrpc_table_header);
2277 sunrpc_table_header = NULL;
2278 }
2279#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002280
2281 xprt_unregister_transport(&xs_udp_transport);
2282 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002283}