blob: 01121a4f0851711062b1d517a2a1b3cb394338a9 [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 *
6 * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com>
7 * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com>
8 * 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>
23#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/pagemap.h>
25#include <linux/errno.h>
26#include <linux/socket.h>
27#include <linux/in.h>
28#include <linux/net.h>
29#include <linux/mm.h>
30#include <linux/udp.h>
31#include <linux/tcp.h>
32#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010033#include <linux/sunrpc/sched.h>
Chuck Levera246b012005-08-11 16:25:23 -040034#include <linux/file.h>
35
36#include <net/sock.h>
37#include <net/checksum.h>
38#include <net/udp.h>
39#include <net/tcp.h>
40
Chuck Lever9903cd12005-08-11 16:25:26 -040041/*
Chuck Leverc556b752005-11-01 12:24:48 -050042 * xprtsock tunables
43 */
44unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
45unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
46
47unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
48unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
49
50/*
Chuck Leverfbf76682006-12-05 16:35:54 -050051 * We can register our own files under /proc/sys/sunrpc by
52 * calling register_sysctl_table() again. The files in that
53 * directory become the union of all files registered there.
54 *
55 * We simply need to make sure that we don't collide with
56 * someone else's file names!
57 */
58
59#ifdef RPC_DEBUG
60
61static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
62static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
63static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
64static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
65
66static struct ctl_table_header *sunrpc_table_header;
67
68/*
69 * FIXME: changing the UDP slot table size should also resize the UDP
70 * socket buffers for existing UDP transports
71 */
72static ctl_table xs_tunables_table[] = {
73 {
74 .ctl_name = CTL_SLOTTABLE_UDP,
75 .procname = "udp_slot_table_entries",
76 .data = &xprt_udp_slot_table_entries,
77 .maxlen = sizeof(unsigned int),
78 .mode = 0644,
79 .proc_handler = &proc_dointvec_minmax,
80 .strategy = &sysctl_intvec,
81 .extra1 = &min_slot_table_size,
82 .extra2 = &max_slot_table_size
83 },
84 {
85 .ctl_name = CTL_SLOTTABLE_TCP,
86 .procname = "tcp_slot_table_entries",
87 .data = &xprt_tcp_slot_table_entries,
88 .maxlen = sizeof(unsigned int),
89 .mode = 0644,
90 .proc_handler = &proc_dointvec_minmax,
91 .strategy = &sysctl_intvec,
92 .extra1 = &min_slot_table_size,
93 .extra2 = &max_slot_table_size
94 },
95 {
96 .ctl_name = CTL_MIN_RESVPORT,
97 .procname = "min_resvport",
98 .data = &xprt_min_resvport,
99 .maxlen = sizeof(unsigned int),
100 .mode = 0644,
101 .proc_handler = &proc_dointvec_minmax,
102 .strategy = &sysctl_intvec,
103 .extra1 = &xprt_min_resvport_limit,
104 .extra2 = &xprt_max_resvport_limit
105 },
106 {
107 .ctl_name = CTL_MAX_RESVPORT,
108 .procname = "max_resvport",
109 .data = &xprt_max_resvport,
110 .maxlen = sizeof(unsigned int),
111 .mode = 0644,
112 .proc_handler = &proc_dointvec_minmax,
113 .strategy = &sysctl_intvec,
114 .extra1 = &xprt_min_resvport_limit,
115 .extra2 = &xprt_max_resvport_limit
116 },
117 {
118 .ctl_name = 0,
119 },
120};
121
122static ctl_table sunrpc_table[] = {
123 {
124 .ctl_name = CTL_SUNRPC,
125 .procname = "sunrpc",
126 .mode = 0555,
127 .child = xs_tunables_table
128 },
129 {
130 .ctl_name = 0,
131 },
132};
133
134#endif
135
136/*
Chuck Lever262965f2005-08-11 16:25:56 -0400137 * How many times to try sending a request on a socket before waiting
138 * for the socket buffer to clear.
139 */
140#define XS_SENDMSG_RETRY (10U)
141
Chuck Lever03bf4b72005-08-25 16:25:55 -0700142/*
143 * Time out for an RPC UDP socket connect. UDP socket connects are
144 * synchronous, but we set a timeout anyway in case of resource
145 * exhaustion on the local host.
146 */
147#define XS_UDP_CONN_TO (5U * HZ)
148
149/*
150 * Wait duration for an RPC TCP connection to be established. Solaris
151 * NFS over TCP uses 60 seconds, for example, which is in line with how
152 * long a server takes to reboot.
153 */
154#define XS_TCP_CONN_TO (60U * HZ)
155
156/*
157 * Wait duration for a reply from the RPC portmapper.
158 */
159#define XS_BIND_TO (60U * HZ)
160
161/*
162 * Delay if a UDP socket connect error occurs. This is most likely some
163 * kind of resource problem on the local host.
164 */
165#define XS_UDP_REEST_TO (2U * HZ)
166
167/*
168 * The reestablish timeout allows clients to delay for a bit before attempting
169 * to reconnect to a server that just dropped our connection.
170 *
171 * We implement an exponential backoff when trying to reestablish a TCP
172 * transport connection with the server. Some servers like to drop a TCP
173 * connection when they are overworked, so we start with a short timeout and
174 * increase over time if the server is down or not responding.
175 */
176#define XS_TCP_INIT_REEST_TO (3U * HZ)
177#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
178
179/*
180 * TCP idle timeout; client drops the transport socket if it is idle
181 * for this long. Note that we also timeout UDP sockets to prevent
182 * holding port numbers when there is no RPC traffic.
183 */
184#define XS_IDLE_DISC_TO (5U * 60 * HZ)
185
Chuck Levera246b012005-08-11 16:25:23 -0400186#ifdef RPC_DEBUG
187# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400188# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400189#endif
190
Chuck Levera246b012005-08-11 16:25:23 -0400191#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400192static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400193{
Chuck Lever9903cd12005-08-11 16:25:26 -0400194 u8 *buf = (u8 *) packet;
195 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400196
Chuck Lever46121cf2007-01-31 12:14:08 -0500197 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400198 for (j = 0; j < count && j < 128; j += 4) {
199 if (!(j & 31)) {
200 if (j)
201 dprintk("\n");
202 dprintk("0x%04x ", j);
203 }
204 dprintk("%02x%02x%02x%02x ",
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
206 }
207 dprintk("\n");
208}
209#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400210static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400211{
212 /* NOP */
213}
214#endif
215
Chuck Leverffc2e512006-12-05 16:35:11 -0500216struct sock_xprt {
217 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500218
219 /*
220 * Network layer
221 */
222 struct socket * sock;
223 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500224
225 /*
226 * State of TCP reply receive
227 */
228 __be32 tcp_fraghdr,
229 tcp_xid;
230
231 u32 tcp_offset,
232 tcp_reclen;
233
234 unsigned long tcp_copied,
235 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500236
237 /*
238 * Connection of transports
239 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500240 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200241 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500242 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500243
244 /*
245 * UDP socket buffer size parameters
246 */
247 size_t rcvsize,
248 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500249
250 /*
251 * Saved socket callback addresses
252 */
253 void (*old_data_ready)(struct sock *, int);
254 void (*old_state_change)(struct sock *);
255 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500256};
257
Chuck Levere136d092006-12-05 16:35:23 -0500258/*
259 * TCP receive state flags
260 */
261#define TCP_RCV_LAST_FRAG (1UL << 0)
262#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
263#define TCP_RCV_COPY_XID (1UL << 2)
264#define TCP_RCV_COPY_DATA (1UL << 3)
265
Chuck Lever95392c52007-08-06 11:57:58 -0400266static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
267{
268 return (struct sockaddr *) &xprt->addr;
269}
270
271static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
272{
273 return (struct sockaddr_in *) &xprt->addr;
274}
275
276static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
277{
278 return (struct sockaddr_in6 *) &xprt->addr;
279}
280
Chuck Leverba10f2c2007-08-06 11:57:07 -0400281static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400282{
Chuck Lever95392c52007-08-06 11:57:58 -0400283 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400284 char *buf;
285
286 buf = kzalloc(20, GFP_KERNEL);
287 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400288 snprintf(buf, 20, NIPQUAD_FMT,
Chuck Leveredb267a2006-08-22 20:06:18 -0400289 NIPQUAD(addr->sin_addr.s_addr));
290 }
291 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
292
293 buf = kzalloc(8, GFP_KERNEL);
294 if (buf) {
295 snprintf(buf, 8, "%u",
296 ntohs(addr->sin_port));
297 }
298 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
299
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400300 buf = kzalloc(8, GFP_KERNEL);
301 if (buf) {
302 if (xprt->prot == IPPROTO_UDP)
303 snprintf(buf, 8, "udp");
304 else
305 snprintf(buf, 8, "tcp");
306 }
307 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
Chuck Leveredb267a2006-08-22 20:06:18 -0400308
309 buf = kzalloc(48, GFP_KERNEL);
310 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400311 snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
Chuck Leveredb267a2006-08-22 20:06:18 -0400312 NIPQUAD(addr->sin_addr.s_addr),
313 ntohs(addr->sin_port),
314 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
315 }
316 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400317
318 buf = kzalloc(10, GFP_KERNEL);
319 if (buf) {
320 snprintf(buf, 10, "%02x%02x%02x%02x",
321 NIPQUAD(addr->sin_addr.s_addr));
322 }
323 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
324
325 buf = kzalloc(8, GFP_KERNEL);
326 if (buf) {
327 snprintf(buf, 8, "%4hx",
328 ntohs(addr->sin_port));
329 }
330 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Leveredb267a2006-08-22 20:06:18 -0400331}
332
Chuck Lever4b6473f2007-08-06 11:57:12 -0400333static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt)
334{
Chuck Lever95392c52007-08-06 11:57:58 -0400335 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400336 char *buf;
337
338 buf = kzalloc(40, GFP_KERNEL);
339 if (buf) {
340 snprintf(buf, 40, NIP6_FMT,
341 NIP6(addr->sin6_addr));
342 }
343 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
344
345 buf = kzalloc(8, GFP_KERNEL);
346 if (buf) {
347 snprintf(buf, 8, "%u",
348 ntohs(addr->sin6_port));
349 }
350 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
351
352 buf = kzalloc(8, GFP_KERNEL);
353 if (buf) {
354 if (xprt->prot == IPPROTO_UDP)
355 snprintf(buf, 8, "udp");
356 else
357 snprintf(buf, 8, "tcp");
358 }
359 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
360
361 buf = kzalloc(64, GFP_KERNEL);
362 if (buf) {
363 snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
364 NIP6(addr->sin6_addr),
365 ntohs(addr->sin6_port),
366 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
367 }
368 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
369
370 buf = kzalloc(36, GFP_KERNEL);
371 if (buf) {
372 snprintf(buf, 36, NIP6_SEQFMT,
373 NIP6(addr->sin6_addr));
374 }
375 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
376
377 buf = kzalloc(8, GFP_KERNEL);
378 if (buf) {
379 snprintf(buf, 8, "%4hx",
380 ntohs(addr->sin6_port));
381 }
382 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
383}
384
Chuck Leveredb267a2006-08-22 20:06:18 -0400385static void xs_free_peer_addresses(struct rpc_xprt *xprt)
386{
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400387 int i;
388
389 for (i = 0; i < RPC_DISPLAY_MAX; i++)
390 kfree(xprt->address_strings[i]);
Chuck Leveredb267a2006-08-22 20:06:18 -0400391}
392
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400393#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
394
Trond Myklebust24c56842006-10-17 15:06:22 -0400395static 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 -0400396{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400397 struct msghdr msg = {
398 .msg_name = addr,
399 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
401 };
402 struct kvec iov = {
403 .iov_base = vec->iov_base + base,
404 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400405 };
406
Trond Myklebust24c56842006-10-17 15:06:22 -0400407 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400408 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
409 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
410}
411
Trond Myklebust24c56842006-10-17 15:06:22 -0400412static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400413{
Trond Myklebust24c56842006-10-17 15:06:22 -0400414 struct page **ppage;
415 unsigned int remainder;
416 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400417
Trond Myklebust24c56842006-10-17 15:06:22 -0400418 remainder = xdr->page_len - base;
419 base += xdr->page_base;
420 ppage = xdr->pages + (base >> PAGE_SHIFT);
421 base &= ~PAGE_MASK;
422 for(;;) {
423 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
424 int flags = XS_SENDMSG_FLAGS;
425
426 remainder -= len;
427 if (remainder != 0 || more)
428 flags |= MSG_MORE;
429 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
430 if (remainder == 0 || err != len)
431 break;
432 sent += err;
433 ppage++;
434 base = 0;
435 }
436 if (sent == 0)
437 return err;
438 if (err > 0)
439 sent += err;
440 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400441}
442
Chuck Lever9903cd12005-08-11 16:25:26 -0400443/**
444 * xs_sendpages - write pages directly to a socket
445 * @sock: socket to send on
446 * @addr: UDP only -- address of destination
447 * @addrlen: UDP only -- length of destination address
448 * @xdr: buffer containing this request
449 * @base: starting position in the buffer
450 *
Chuck Levera246b012005-08-11 16:25:23 -0400451 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400452static 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 -0400453{
Trond Myklebust24c56842006-10-17 15:06:22 -0400454 unsigned int remainder = xdr->len - base;
455 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400456
Chuck Lever262965f2005-08-11 16:25:56 -0400457 if (unlikely(!sock))
458 return -ENOTCONN;
459
460 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400461 if (base != 0) {
462 addr = NULL;
463 addrlen = 0;
464 }
Chuck Lever262965f2005-08-11 16:25:56 -0400465
Trond Myklebust24c56842006-10-17 15:06:22 -0400466 if (base < xdr->head[0].iov_len || addr != NULL) {
467 unsigned int len = xdr->head[0].iov_len - base;
468 remainder -= len;
469 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
470 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400471 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400472 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400473 base = 0;
474 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400475 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400476
Trond Myklebust24c56842006-10-17 15:06:22 -0400477 if (base < xdr->page_len) {
478 unsigned int len = xdr->page_len - base;
479 remainder -= len;
480 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
481 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400482 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400483 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400484 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400485 } else
486 base -= xdr->page_len;
487
488 if (base >= xdr->tail[0].iov_len)
489 return sent;
490 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400491out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400492 if (sent == 0)
493 return err;
494 if (err > 0)
495 sent += err;
496 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400497}
498
Chuck Lever9903cd12005-08-11 16:25:26 -0400499/**
Chuck Lever262965f2005-08-11 16:25:56 -0400500 * xs_nospace - place task on wait queue if transmit was incomplete
501 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400502 *
Chuck Levera246b012005-08-11 16:25:23 -0400503 */
Chuck Lever262965f2005-08-11 16:25:56 -0400504static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400505{
Chuck Lever262965f2005-08-11 16:25:56 -0400506 struct rpc_rqst *req = task->tk_rqstp;
507 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500508 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400509
Chuck Lever46121cf2007-01-31 12:14:08 -0500510 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400511 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
512 req->rq_slen);
513
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500514 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400515 /* Protect against races with write_space */
516 spin_lock_bh(&xprt->transport_lock);
517
518 /* Don't race with disconnect */
519 if (!xprt_connected(xprt))
520 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500521 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400522 xprt_wait_for_buffer_space(task);
523
524 spin_unlock_bh(&xprt->transport_lock);
525 } else
526 /* Keep holding the socket if it is blocked */
527 rpc_delay(task, HZ>>4);
528}
529
530/**
531 * xs_udp_send_request - write an RPC request to a UDP socket
532 * @task: address of RPC task that manages the state of an RPC request
533 *
534 * Return values:
535 * 0: The request has been sent
536 * EAGAIN: The socket was blocked, please call again later to
537 * complete the request
538 * ENOTCONN: Caller needs to invoke connect logic then call again
539 * other: Some other error occured, the request was not sent
540 */
541static int xs_udp_send_request(struct rpc_task *task)
542{
543 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);
Chuck Lever262965f2005-08-11 16:25:56 -0400546 struct xdr_buf *xdr = &req->rq_snd_buf;
547 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400548
Chuck Lever9903cd12005-08-11 16:25:26 -0400549 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400550 req->rq_svec->iov_base,
551 req->rq_svec->iov_len);
552
Chuck Lever262965f2005-08-11 16:25:56 -0400553 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500554 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400555 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500556 xprt->addrlen, xdr,
557 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400558
Chuck Lever46121cf2007-01-31 12:14:08 -0500559 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400560 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400561
Chuck Lever262965f2005-08-11 16:25:56 -0400562 if (likely(status >= (int) req->rq_slen))
563 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400564
Chuck Lever262965f2005-08-11 16:25:56 -0400565 /* Still some bytes left; set up for a retry later. */
566 if (status > 0)
567 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400568
Chuck Lever262965f2005-08-11 16:25:56 -0400569 switch (status) {
570 case -ENETUNREACH:
571 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400572 case -ECONNREFUSED:
573 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400574 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400575 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400576 case -EAGAIN:
577 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400578 break;
579 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500580 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400581 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400582 break;
Chuck Levera246b012005-08-11 16:25:23 -0400583 }
Chuck Lever262965f2005-08-11 16:25:56 -0400584
585 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400586}
587
Chuck Lever808012f2005-08-25 16:25:49 -0700588static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
589{
590 u32 reclen = buf->len - sizeof(rpc_fraghdr);
591 rpc_fraghdr *base = buf->head[0].iov_base;
592 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
593}
594
Chuck Lever9903cd12005-08-11 16:25:26 -0400595/**
Chuck Lever262965f2005-08-11 16:25:56 -0400596 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400597 * @task: address of RPC task that manages the state of an RPC request
598 *
599 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400600 * 0: The request has been sent
601 * EAGAIN: The socket was blocked, please call again later to
602 * complete the request
603 * ENOTCONN: Caller needs to invoke connect logic then call again
604 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400605 *
606 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400607 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400608 */
Chuck Lever262965f2005-08-11 16:25:56 -0400609static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400610{
611 struct rpc_rqst *req = task->tk_rqstp;
612 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500613 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400614 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400615 int status;
616 unsigned int retry = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400617
Chuck Lever808012f2005-08-25 16:25:49 -0700618 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400619
Chuck Lever262965f2005-08-11 16:25:56 -0400620 xs_pktdump("packet data:",
621 req->rq_svec->iov_base,
622 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400623
624 /* Continue transmitting the packet/record. We must be careful
625 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400626 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400627 while (1) {
628 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500629 status = xs_sendpages(transport->sock,
630 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400631
Chuck Lever46121cf2007-01-31 12:14:08 -0500632 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400633 xdr->len - req->rq_bytes_sent, status);
634
635 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400636 break;
637
Chuck Lever262965f2005-08-11 16:25:56 -0400638 /* If we've sent the entire packet, immediately
639 * reset the count of bytes sent. */
640 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500641 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400642 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
643 req->rq_bytes_sent = 0;
644 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400645 }
646
Chuck Levera246b012005-08-11 16:25:23 -0400647 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400648 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400649 break;
650 }
651
Chuck Lever262965f2005-08-11 16:25:56 -0400652 switch (status) {
653 case -EAGAIN:
654 xs_nospace(task);
655 break;
656 case -ECONNREFUSED:
657 case -ECONNRESET:
658 case -ENOTCONN:
659 case -EPIPE:
660 status = -ENOTCONN;
661 break;
662 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500663 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400664 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700665 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400666 break;
Chuck Levera246b012005-08-11 16:25:23 -0400667 }
Chuck Lever262965f2005-08-11 16:25:56 -0400668
Chuck Levera246b012005-08-11 16:25:23 -0400669 return status;
670}
671
Chuck Lever9903cd12005-08-11 16:25:26 -0400672/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400673 * xs_tcp_release_xprt - clean up after a tcp transmission
674 * @xprt: transport
675 * @task: rpc task
676 *
677 * This cleans up if an error causes us to abort the transmission of a request.
678 * In this case, the socket may need to be reset in order to avoid confusing
679 * the server.
680 */
681static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
682{
683 struct rpc_rqst *req;
684
685 if (task != xprt->snd_task)
686 return;
687 if (task == NULL)
688 goto out_release;
689 req = task->tk_rqstp;
690 if (req->rq_bytes_sent == 0)
691 goto out_release;
692 if (req->rq_bytes_sent == req->rq_snd_buf.len)
693 goto out_release;
694 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
695out_release:
696 xprt_release_xprt(xprt, task);
697}
698
699/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400700 * xs_close - close a socket
701 * @xprt: transport
702 *
Chuck Lever3167e122005-08-25 16:25:55 -0700703 * This is used when all requests are complete; ie, no DRC state remains
704 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400705 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400706static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400707{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500708 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
709 struct socket *sock = transport->sock;
710 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400711
712 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100713 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400714
Chuck Lever46121cf2007-01-31 12:14:08 -0500715 dprintk("RPC: xs_close xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400716
Chuck Levera246b012005-08-11 16:25:23 -0400717 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500718 transport->inet = NULL;
719 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400720
Chuck Lever9903cd12005-08-11 16:25:26 -0400721 sk->sk_user_data = NULL;
Chuck Lever314dfd72006-12-05 16:35:34 -0500722 sk->sk_data_ready = transport->old_data_ready;
723 sk->sk_state_change = transport->old_state_change;
724 sk->sk_write_space = transport->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400725 write_unlock_bh(&sk->sk_callback_lock);
726
Chuck Lever9903cd12005-08-11 16:25:26 -0400727 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400728
729 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100730clear_close_wait:
731 smp_mb__before_clear_bit();
732 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
733 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400734}
735
Chuck Lever9903cd12005-08-11 16:25:26 -0400736/**
737 * xs_destroy - prepare to shutdown a transport
738 * @xprt: doomed transport
739 *
740 */
741static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400742{
Chuck Leverc8475462006-12-05 16:35:26 -0500743 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
744
Chuck Lever46121cf2007-01-31 12:14:08 -0500745 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400746
Trond Myklebustc1384c92007-06-14 18:00:42 -0400747 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400748
749 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400750 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400751 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400752 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400753 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400754}
755
Chuck Lever9903cd12005-08-11 16:25:26 -0400756static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400757{
Chuck Lever9903cd12005-08-11 16:25:26 -0400758 return (struct rpc_xprt *) sk->sk_user_data;
759}
760
761/**
762 * xs_udp_data_ready - "data ready" callback for UDP sockets
763 * @sk: socket with data to read
764 * @len: how much data to read
765 *
766 */
767static void xs_udp_data_ready(struct sock *sk, int len)
768{
769 struct rpc_task *task;
770 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400771 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400772 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400773 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700774 u32 _xid;
775 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400776
777 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500778 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400779 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400780 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400781
782 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
783 goto out;
784
785 if (xprt->shutdown)
786 goto dropit;
787
788 repsize = skb->len - sizeof(struct udphdr);
789 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500790 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400791 goto dropit;
792 }
793
794 /* Copy the XID from the skb... */
795 xp = skb_header_pointer(skb, sizeof(struct udphdr),
796 sizeof(_xid), &_xid);
797 if (xp == NULL)
798 goto dropit;
799
800 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400801 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400802 rovr = xprt_lookup_rqst(xprt, *xp);
803 if (!rovr)
804 goto out_unlock;
805 task = rovr->rq_task;
806
Chuck Levera246b012005-08-11 16:25:23 -0400807 if ((copied = rovr->rq_private_buf.buflen) > repsize)
808 copied = repsize;
809
810 /* Suck it into the iovec, verify checksum if not done by hw. */
811 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
812 goto out_unlock;
813
814 /* Something worked... */
815 dst_confirm(skb->dst);
816
Chuck Lever1570c1e2005-08-25 16:25:52 -0700817 xprt_adjust_cwnd(task, copied);
818 xprt_update_rtt(task);
819 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400820
821 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400822 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400823 dropit:
824 skb_free_datagram(sk, skb);
825 out:
826 read_unlock(&sk->sk_callback_lock);
827}
828
Chuck Leverdd456472006-12-05 16:35:44 -0500829static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400830{
Chuck Lever51971132006-12-05 16:35:19 -0500831 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400832 size_t len, used;
833 char *p;
834
Chuck Lever51971132006-12-05 16:35:19 -0500835 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
836 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500837 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500838 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400839 if (used != len)
840 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700841
Chuck Lever51971132006-12-05 16:35:19 -0500842 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
843 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500844 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400845 else
Chuck Levere136d092006-12-05 16:35:23 -0500846 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500847 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700848
Chuck Levere136d092006-12-05 16:35:23 -0500849 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500850 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700851
Chuck Levera246b012005-08-11 16:25:23 -0400852 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500853 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500854 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400855 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400856 return;
Chuck Levera246b012005-08-11 16:25:23 -0400857 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500858 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500859 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400860}
861
Chuck Lever51971132006-12-05 16:35:19 -0500862static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400863{
Chuck Lever51971132006-12-05 16:35:19 -0500864 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500865 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500866 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500867 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
868 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
869 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500870 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400871 }
872 }
873}
874
Chuck Leverdd456472006-12-05 16:35:44 -0500875static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400876{
877 size_t len, used;
878 char *p;
879
Chuck Lever51971132006-12-05 16:35:19 -0500880 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500881 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500882 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500883 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500884 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400885 if (used != len)
886 return;
Chuck Levere136d092006-12-05 16:35:23 -0500887 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
888 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500889 transport->tcp_copied = 4;
Chuck Lever46121cf2007-01-31 12:14:08 -0500890 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500891 ntohl(transport->tcp_xid));
892 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400893}
894
Chuck Leverdd456472006-12-05 16:35:44 -0500895static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400896{
Chuck Lever51971132006-12-05 16:35:19 -0500897 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400898 struct rpc_rqst *req;
899 struct xdr_buf *rcvbuf;
900 size_t len;
901 ssize_t r;
902
903 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400904 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500905 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400906 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500907 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500908 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500909 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400910 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400911 return;
912 }
913
914 rcvbuf = &req->rq_private_buf;
915 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500916 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500917 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400918
Chuck Lever51971132006-12-05 16:35:19 -0500919 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400920 memcpy(&my_desc, desc, sizeof(my_desc));
921 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500922 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500923 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400924 desc->count -= r;
925 desc->offset += r;
926 } else
Chuck Lever51971132006-12-05 16:35:19 -0500927 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500928 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400929
930 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500931 transport->tcp_copied += r;
932 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400933 }
934 if (r != len) {
935 /* Error when copying to the receive buffer,
936 * usually because we weren't able to allocate
937 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500938 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400939 * will not receive any additional updates,
940 * and time out.
941 * Any remaining data from this record will
942 * be discarded.
943 */
Chuck Levere136d092006-12-05 16:35:23 -0500944 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500945 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500946 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -0500947 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
948 "tcp_offset = %u, tcp_reclen = %u\n",
949 xprt, transport->tcp_copied,
950 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400951 goto out;
952 }
953
Chuck Lever46121cf2007-01-31 12:14:08 -0500954 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500955 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -0500956 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
957 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
958 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400959
Chuck Lever51971132006-12-05 16:35:19 -0500960 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500961 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500962 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500963 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
964 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400965 }
966
967out:
Chuck Levere136d092006-12-05 16:35:23 -0500968 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500969 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400970 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500971 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400972}
973
Chuck Leverdd456472006-12-05 16:35:44 -0500974static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400975{
976 size_t len;
977
Chuck Lever51971132006-12-05 16:35:19 -0500978 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400979 if (len > desc->count)
980 len = desc->count;
981 desc->count -= len;
982 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -0500983 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -0500984 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500985 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400986}
987
Chuck Lever9903cd12005-08-11 16:25:26 -0400988static 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 -0400989{
990 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -0500991 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -0500992 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -0400993 .skb = skb,
994 .offset = offset,
995 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -0400996 };
Chuck Levera246b012005-08-11 16:25:23 -0400997
Chuck Lever46121cf2007-01-31 12:14:08 -0500998 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400999 do {
1000 /* Read in a new fragment marker if necessary */
1001 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001002 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001003 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001004 continue;
1005 }
1006 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001007 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001008 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001009 continue;
1010 }
1011 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001012 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001013 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001014 continue;
1015 }
1016 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001017 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001018 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001019 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001020 return len - desc.count;
1021}
1022
Chuck Lever9903cd12005-08-11 16:25:26 -04001023/**
1024 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1025 * @sk: socket with data to read
1026 * @bytes: how much data to read
1027 *
1028 */
1029static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001030{
1031 struct rpc_xprt *xprt;
1032 read_descriptor_t rd_desc;
1033
Chuck Lever46121cf2007-01-31 12:14:08 -05001034 dprintk("RPC: xs_tcp_data_ready...\n");
1035
Chuck Levera246b012005-08-11 16:25:23 -04001036 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001037 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001038 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001039 if (xprt->shutdown)
1040 goto out;
1041
Chuck Lever9903cd12005-08-11 16:25:26 -04001042 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001043 rd_desc.arg.data = xprt;
1044 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -04001045 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -04001046out:
1047 read_unlock(&sk->sk_callback_lock);
1048}
1049
Chuck Lever9903cd12005-08-11 16:25:26 -04001050/**
1051 * xs_tcp_state_change - callback to handle TCP socket state changes
1052 * @sk: socket whose state has changed
1053 *
1054 */
1055static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001056{
Chuck Lever9903cd12005-08-11 16:25:26 -04001057 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001058
1059 read_lock(&sk->sk_callback_lock);
1060 if (!(xprt = xprt_from_sock(sk)))
1061 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001062 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1063 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1064 sk->sk_state, xprt_connected(xprt),
1065 sock_flag(sk, SOCK_DEAD),
1066 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001067
1068 switch (sk->sk_state) {
1069 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001070 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001071 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001072 struct sock_xprt *transport = container_of(xprt,
1073 struct sock_xprt, xprt);
1074
Chuck Levera246b012005-08-11 16:25:23 -04001075 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001076 transport->tcp_offset = 0;
1077 transport->tcp_reclen = 0;
1078 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001079 transport->tcp_flags =
1080 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001081
Chuck Lever03bf4b72005-08-25 16:25:55 -07001082 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -04001083 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001084 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001085 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001086 break;
1087 case TCP_SYN_SENT:
1088 case TCP_SYN_RECV:
1089 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001090 case TCP_CLOSE_WAIT:
1091 /* Try to schedule an autoclose RPC calls */
1092 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1093 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebustc1384c92007-06-14 18:00:42 -04001094 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -04001095 default:
1096 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001097 }
1098 out:
1099 read_unlock(&sk->sk_callback_lock);
1100}
1101
Chuck Lever9903cd12005-08-11 16:25:26 -04001102/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001103 * xs_udp_write_space - callback invoked when socket buffer space
1104 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001105 * @sk: socket whose state has changed
1106 *
Chuck Levera246b012005-08-11 16:25:23 -04001107 * Called when more output buffer space is available for this socket.
1108 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001109 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001110 * with a bunch of small requests.
1111 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001112static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001113{
Chuck Levera246b012005-08-11 16:25:23 -04001114 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001115
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001116 /* from net/core/sock.c:sock_def_write_space */
1117 if (sock_writeable(sk)) {
1118 struct socket *sock;
1119 struct rpc_xprt *xprt;
1120
1121 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -04001122 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001123 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -04001124 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001125 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1126 goto out;
1127
1128 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001129 }
1130
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001131 out:
1132 read_unlock(&sk->sk_callback_lock);
1133}
Chuck Levera246b012005-08-11 16:25:23 -04001134
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001135/**
1136 * xs_tcp_write_space - callback invoked when socket buffer space
1137 * becomes available
1138 * @sk: socket whose state has changed
1139 *
1140 * Called when more output buffer space is available for this socket.
1141 * We try not to wake our writers until they can make "significant"
1142 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1143 * with a bunch of small requests.
1144 */
1145static void xs_tcp_write_space(struct sock *sk)
1146{
1147 read_lock(&sk->sk_callback_lock);
1148
1149 /* from net/core/stream.c:sk_stream_write_space */
1150 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1151 struct socket *sock;
1152 struct rpc_xprt *xprt;
1153
1154 if (unlikely(!(sock = sk->sk_socket)))
1155 goto out;
1156 if (unlikely(!(xprt = xprt_from_sock(sk))))
1157 goto out;
1158 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1159 goto out;
1160
1161 xprt_write_space(xprt);
1162 }
1163
1164 out:
Chuck Levera246b012005-08-11 16:25:23 -04001165 read_unlock(&sk->sk_callback_lock);
1166}
1167
Chuck Lever470056c2005-08-25 16:25:56 -07001168static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001169{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001170 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1171 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001172
Chuck Lever7c6e0662006-12-05 16:35:30 -05001173 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001174 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001175 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001176 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001177 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001178 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001179 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001180 sk->sk_write_space(sk);
1181 }
1182}
1183
Chuck Lever43118c22005-08-25 16:25:49 -07001184/**
Chuck Lever470056c2005-08-25 16:25:56 -07001185 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001186 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001187 * @sndsize: requested size of send buffer, in bytes
1188 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001189 *
Chuck Lever470056c2005-08-25 16:25:56 -07001190 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001191 */
Chuck Lever470056c2005-08-25 16:25:56 -07001192static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001193{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001194 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1195
1196 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001197 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001198 transport->sndsize = sndsize + 1024;
1199 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001200 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001201 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001202
1203 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001204}
1205
Chuck Lever46c0ee82005-08-25 16:25:52 -07001206/**
1207 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1208 * @task: task that timed out
1209 *
1210 * Adjust the congestion window after a retransmit timeout has occurred.
1211 */
1212static void xs_udp_timer(struct rpc_task *task)
1213{
1214 xprt_adjust_cwnd(task, -ETIMEDOUT);
1215}
1216
Chuck Leverb85d8802006-05-25 01:40:49 -04001217static unsigned short xs_get_random_port(void)
1218{
1219 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1220 unsigned short rand = (unsigned short) net_random() % range;
1221 return rand + xprt_min_resvport;
1222}
1223
Chuck Lever92200412006-01-03 09:55:51 +01001224/**
1225 * xs_set_port - reset the port number in the remote endpoint address
1226 * @xprt: generic transport
1227 * @port: new port number
1228 *
1229 */
1230static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1231{
Chuck Lever95392c52007-08-06 11:57:58 -04001232 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001233
Chuck Lever46121cf2007-01-31 12:14:08 -05001234 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001235
Chuck Lever20612002007-08-06 11:57:23 -04001236 switch (addr->sa_family) {
1237 case AF_INET:
1238 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1239 break;
1240 case AF_INET6:
1241 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1242 break;
1243 default:
1244 BUG();
1245 }
Chuck Lever92200412006-01-03 09:55:51 +01001246}
1247
Chuck Lever7dc753f2007-08-06 11:57:28 -04001248static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001249{
1250 struct sockaddr_in myaddr = {
1251 .sin_family = AF_INET,
1252 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001253 struct sockaddr_in *sa;
Chuck Lever529b33c2005-08-25 16:25:54 -07001254 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001255 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001256
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001257 if (!transport->xprt.resvport)
1258 port = 0;
1259 sa = (struct sockaddr_in *)&transport->addr;
1260 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001261 do {
1262 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001263 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001264 sizeof(myaddr));
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001265 if (!transport->xprt.resvport)
1266 break;
Chuck Levera246b012005-08-11 16:25:23 -04001267 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001268 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001269 break;
Chuck Levera246b012005-08-11 16:25:23 -04001270 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001271 if (port <= xprt_min_resvport)
1272 port = xprt_max_resvport;
1273 else
1274 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001275 } while (err == -EADDRINUSE && port != transport->port);
Chuck Lever7dc753f2007-08-06 11:57:28 -04001276 dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n",
1277 __FUNCTION__, NIPQUAD(myaddr.sin_addr),
1278 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001279 return err;
1280}
1281
Chuck Lever90058d32007-08-06 11:57:33 -04001282static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1283{
1284 struct sockaddr_in6 myaddr = {
1285 .sin6_family = AF_INET6,
1286 };
1287 struct sockaddr_in6 *sa;
1288 int err;
1289 unsigned short port = transport->port;
1290
1291 if (!transport->xprt.resvport)
1292 port = 0;
1293 sa = (struct sockaddr_in6 *)&transport->addr;
1294 myaddr.sin6_addr = sa->sin6_addr;
1295 do {
1296 myaddr.sin6_port = htons(port);
1297 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1298 sizeof(myaddr));
1299 if (!transport->xprt.resvport)
1300 break;
1301 if (err == 0) {
1302 transport->port = port;
1303 break;
1304 }
1305 if (port <= xprt_min_resvport)
1306 port = xprt_max_resvport;
1307 else
1308 port--;
1309 } while (err == -EADDRINUSE && port != transport->port);
1310 dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
1311 NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
1312 return err;
1313}
1314
Peter Zijlstraed075362006-12-06 20:35:24 -08001315#ifdef CONFIG_DEBUG_LOCK_ALLOC
1316static struct lock_class_key xs_key[2];
1317static struct lock_class_key xs_slock_key[2];
1318
Chuck Lever8945ee52007-08-06 11:58:04 -04001319static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001320{
1321 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001322
Peter Zijlstraed075362006-12-06 20:35:24 -08001323 BUG_ON(sk->sk_lock.owner != NULL);
Chuck Lever8945ee52007-08-06 11:58:04 -04001324 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1325 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1326}
Peter Zijlstraed075362006-12-06 20:35:24 -08001327
Chuck Lever8945ee52007-08-06 11:58:04 -04001328static inline void xs_reclassify_socket6(struct socket *sock)
1329{
1330 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001331
Chuck Lever8945ee52007-08-06 11:58:04 -04001332 BUG_ON(sk->sk_lock.owner != NULL);
1333 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1334 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001335}
1336#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001337static inline void xs_reclassify_socket4(struct socket *sock)
1338{
1339}
1340
1341static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001342{
1343}
1344#endif
1345
Chuck Lever16be2d22007-08-06 11:57:38 -04001346static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1347{
1348 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1349
1350 if (!transport->inet) {
1351 struct sock *sk = sock->sk;
1352
1353 write_lock_bh(&sk->sk_callback_lock);
1354
1355 sk->sk_user_data = xprt;
1356 transport->old_data_ready = sk->sk_data_ready;
1357 transport->old_state_change = sk->sk_state_change;
1358 transport->old_write_space = sk->sk_write_space;
1359 sk->sk_data_ready = xs_udp_data_ready;
1360 sk->sk_write_space = xs_udp_write_space;
1361 sk->sk_no_check = UDP_CSUM_NORCV;
1362 sk->sk_allocation = GFP_ATOMIC;
1363
1364 xprt_set_connected(xprt);
1365
1366 /* Reset to new socket */
1367 transport->sock = sock;
1368 transport->inet = sk;
1369
1370 write_unlock_bh(&sk->sk_callback_lock);
1371 }
1372 xs_udp_do_set_buffer_size(xprt);
1373}
1374
Chuck Lever9903cd12005-08-11 16:25:26 -04001375/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001376 * xs_udp_connect_worker4 - set up a UDP socket
David Howells65f27f32006-11-22 14:55:48 +00001377 * @work: RPC transport to connect
Chuck Lever9903cd12005-08-11 16:25:26 -04001378 *
1379 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001380 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001381static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001382{
Trond Myklebust34161db2006-12-07 15:48:15 -05001383 struct sock_xprt *transport =
1384 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Leverc8475462006-12-05 16:35:26 -05001385 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001386 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001387 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001388
Chuck Leverec739ef2006-08-22 20:06:15 -04001389 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001390 goto out;
1391
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001392 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001393 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001394
1395 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001396 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001397 goto out;
1398 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001399 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001400
Chuck Lever7dc753f2007-08-06 11:57:28 -04001401 if (xs_bind4(transport, sock)) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001402 sock_release(sock);
1403 goto out;
1404 }
1405
Chuck Lever46121cf2007-01-31 12:14:08 -05001406 dprintk("RPC: worker connecting xprt %p to address: %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001407 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001408
Chuck Lever16be2d22007-08-06 11:57:38 -04001409 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001410 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001411out:
1412 xprt_wake_pending_tasks(xprt, status);
1413 xprt_clear_connecting(xprt);
1414}
1415
Chuck Lever68e220b2007-08-06 11:57:48 -04001416/**
1417 * xs_udp_connect_worker6 - set up a UDP socket
1418 * @work: RPC transport to connect
1419 *
1420 * Invoked by a work queue tasklet.
1421 */
1422static void xs_udp_connect_worker6(struct work_struct *work)
1423{
1424 struct sock_xprt *transport =
1425 container_of(work, struct sock_xprt, connect_worker.work);
1426 struct rpc_xprt *xprt = &transport->xprt;
1427 struct socket *sock = transport->sock;
1428 int err, status = -EIO;
1429
1430 if (xprt->shutdown || !xprt_bound(xprt))
1431 goto out;
1432
1433 /* Start by resetting any existing state */
1434 xs_close(xprt);
1435
1436 if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1437 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1438 goto out;
1439 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001440 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001441
1442 if (xs_bind6(transport, sock) < 0) {
1443 sock_release(sock);
1444 goto out;
1445 }
1446
1447 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1448 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1449
1450 xs_udp_finish_connecting(xprt, sock);
1451 status = 0;
1452out:
1453 xprt_wake_pending_tasks(xprt, status);
1454 xprt_clear_connecting(xprt);
1455}
1456
Chuck Lever3167e122005-08-25 16:25:55 -07001457/*
1458 * We need to preserve the port number so the reply cache on the server can
1459 * find our cached RPC replies when we get around to reconnecting.
1460 */
1461static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1462{
1463 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001464 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001465 struct sockaddr any;
1466
Chuck Lever46121cf2007-01-31 12:14:08 -05001467 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001468
1469 /*
1470 * Disconnect the transport socket by doing a connect operation
1471 * with AF_UNSPEC. This should return immediately...
1472 */
1473 memset(&any, 0, sizeof(any));
1474 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001475 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001476 if (result)
Chuck Lever46121cf2007-01-31 12:14:08 -05001477 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001478 result);
1479}
1480
Chuck Lever16be2d22007-08-06 11:57:38 -04001481static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001482{
Chuck Lever16be2d22007-08-06 11:57:38 -04001483 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001484
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001485 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001486 struct sock *sk = sock->sk;
1487
1488 write_lock_bh(&sk->sk_callback_lock);
1489
1490 sk->sk_user_data = xprt;
Chuck Lever314dfd72006-12-05 16:35:34 -05001491 transport->old_data_ready = sk->sk_data_ready;
1492 transport->old_state_change = sk->sk_state_change;
1493 transport->old_write_space = sk->sk_write_space;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001494 sk->sk_data_ready = xs_tcp_data_ready;
1495 sk->sk_state_change = xs_tcp_state_change;
1496 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001497 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001498
1499 /* socket options */
1500 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1501 sock_reset_flag(sk, SOCK_LINGER);
1502 tcp_sk(sk)->linger2 = 0;
1503 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001504
1505 xprt_clear_connected(xprt);
1506
1507 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001508 transport->sock = sock;
1509 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001510
1511 write_unlock_bh(&sk->sk_callback_lock);
1512 }
1513
1514 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001515 xprt->stat.connect_count++;
1516 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001517 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001518}
1519
1520/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001521 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04001522 * @work: RPC transport to connect
1523 *
1524 * Invoked by a work queue tasklet.
1525 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001526static void xs_tcp_connect_worker4(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04001527{
1528 struct sock_xprt *transport =
1529 container_of(work, struct sock_xprt, connect_worker.work);
1530 struct rpc_xprt *xprt = &transport->xprt;
1531 struct socket *sock = transport->sock;
1532 int err, status = -EIO;
1533
1534 if (xprt->shutdown || !xprt_bound(xprt))
1535 goto out;
1536
1537 if (!sock) {
1538 /* start from scratch */
1539 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1540 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1541 goto out;
1542 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001543 xs_reclassify_socket4(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001544
1545 if (xs_bind4(transport, sock) < 0) {
1546 sock_release(sock);
1547 goto out;
1548 }
1549 } else
1550 /* "close" the socket, preserving the local port */
1551 xs_tcp_reuse_connection(xprt);
1552
1553 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1554 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1555
1556 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001557 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1558 xprt, -status, xprt_connected(xprt),
1559 sock->sk->sk_state);
Chuck Levera246b012005-08-11 16:25:23 -04001560 if (status < 0) {
1561 switch (status) {
1562 case -EINPROGRESS:
1563 case -EALREADY:
1564 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001565 case -ECONNREFUSED:
1566 case -ECONNRESET:
1567 /* retry with existing socket, after a delay */
1568 break;
1569 default:
1570 /* get rid of existing socket, and retry */
1571 xs_close(xprt);
1572 break;
Chuck Levera246b012005-08-11 16:25:23 -04001573 }
1574 }
1575out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001576 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001577out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001578 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001579}
1580
Chuck Lever9903cd12005-08-11 16:25:26 -04001581/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001582 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1583 * @work: RPC transport to connect
1584 *
1585 * Invoked by a work queue tasklet.
1586 */
1587static void xs_tcp_connect_worker6(struct work_struct *work)
1588{
1589 struct sock_xprt *transport =
1590 container_of(work, struct sock_xprt, connect_worker.work);
1591 struct rpc_xprt *xprt = &transport->xprt;
1592 struct socket *sock = transport->sock;
1593 int err, status = -EIO;
1594
1595 if (xprt->shutdown || !xprt_bound(xprt))
1596 goto out;
1597
1598 if (!sock) {
1599 /* start from scratch */
1600 if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1601 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1602 goto out;
1603 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001604 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001605
1606 if (xs_bind6(transport, sock) < 0) {
1607 sock_release(sock);
1608 goto out;
1609 }
1610 } else
1611 /* "close" the socket, preserving the local port */
1612 xs_tcp_reuse_connection(xprt);
1613
1614 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1615 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1616
1617 status = xs_tcp_finish_connecting(xprt, sock);
1618 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1619 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1620 if (status < 0) {
1621 switch (status) {
1622 case -EINPROGRESS:
1623 case -EALREADY:
1624 goto out_clear;
1625 case -ECONNREFUSED:
1626 case -ECONNRESET:
1627 /* retry with existing socket, after a delay */
1628 break;
1629 default:
1630 /* get rid of existing socket, and retry */
1631 xs_close(xprt);
1632 break;
1633 }
1634 }
1635out:
1636 xprt_wake_pending_tasks(xprt, status);
1637out_clear:
1638 xprt_clear_connecting(xprt);
1639}
1640
1641/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001642 * xs_connect - connect a socket to a remote endpoint
1643 * @task: address of RPC task that manages state of connect request
1644 *
1645 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001646 *
1647 * UDP socket connects are synchronous, but we use a work queue anyway
1648 * to guarantee that even unprivileged user processes can set up a
1649 * socket on a privileged port.
1650 *
1651 * If a UDP socket connect fails, the delay behavior here prevents
1652 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001653 */
1654static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001655{
1656 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001657 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001658
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001659 if (xprt_test_and_set_connecting(xprt))
1660 return;
1661
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001662 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001663 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1664 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001665 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001666 queue_delayed_work(rpciod_workqueue,
1667 &transport->connect_worker,
1668 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001669 xprt->reestablish_timeout <<= 1;
1670 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1671 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001672 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001673 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001674 queue_delayed_work(rpciod_workqueue,
1675 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001676 }
1677}
1678
Chuck Lever262ca072006-03-20 13:44:16 -05001679/**
1680 * xs_udp_print_stats - display UDP socket-specifc stats
1681 * @xprt: rpc_xprt struct containing statistics
1682 * @seq: output file
1683 *
1684 */
1685static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1686{
Chuck Leverc8475462006-12-05 16:35:26 -05001687 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1688
Chuck Lever262ca072006-03-20 13:44:16 -05001689 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001690 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001691 xprt->stat.bind_count,
1692 xprt->stat.sends,
1693 xprt->stat.recvs,
1694 xprt->stat.bad_xids,
1695 xprt->stat.req_u,
1696 xprt->stat.bklog_u);
1697}
1698
1699/**
1700 * xs_tcp_print_stats - display TCP socket-specifc stats
1701 * @xprt: rpc_xprt struct containing statistics
1702 * @seq: output file
1703 *
1704 */
1705static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1706{
Chuck Leverc8475462006-12-05 16:35:26 -05001707 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001708 long idle_time = 0;
1709
1710 if (xprt_connected(xprt))
1711 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1712
1713 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001714 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001715 xprt->stat.bind_count,
1716 xprt->stat.connect_count,
1717 xprt->stat.connect_time,
1718 idle_time,
1719 xprt->stat.sends,
1720 xprt->stat.recvs,
1721 xprt->stat.bad_xids,
1722 xprt->stat.req_u,
1723 xprt->stat.bklog_u);
1724}
1725
Chuck Lever262965f2005-08-11 16:25:56 -04001726static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001727 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001728 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001729 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04001730 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001731 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001732 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001733 .buf_alloc = rpc_malloc,
1734 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001735 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001736 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001737 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001738 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001739 .close = xs_close,
1740 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001741 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001742};
1743
1744static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001745 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001746 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04001747 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001748 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001749 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001750 .buf_alloc = rpc_malloc,
1751 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001752 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001753 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001754 .close = xs_close,
1755 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001756 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001757};
1758
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001759static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04001760{
1761 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001762 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001763
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001764 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001765 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001766 return ERR_PTR(-EBADF);
1767 }
1768
Chuck Leverffc2e512006-12-05 16:35:11 -05001769 new = kzalloc(sizeof(*new), GFP_KERNEL);
1770 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001771 dprintk("RPC: xs_setup_xprt: couldn't allocate "
1772 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001773 return ERR_PTR(-ENOMEM);
1774 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001775 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001776
1777 xprt->max_reqs = slot_table_size;
1778 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1779 if (xprt->slot == NULL) {
1780 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001781 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
1782 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001783 return ERR_PTR(-ENOMEM);
1784 }
1785
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001786 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
1787 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001788 if (args->srcaddr)
1789 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8475462006-12-05 16:35:26 -05001790 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001791
1792 return xprt;
1793}
1794
Chuck Lever9903cd12005-08-11 16:25:26 -04001795/**
1796 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001797 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001798 *
1799 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001800struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001801{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001802 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001803 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001804 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001805
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001806 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001807 if (IS_ERR(xprt))
1808 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001809 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001810
Chuck Leverec739ef2006-08-22 20:06:15 -04001811 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001812 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001813 /* XXX: header size can vary due to auth type, IPv6, etc. */
1814 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1815
Chuck Lever03bf4b72005-08-25 16:25:55 -07001816 xprt->bind_timeout = XS_BIND_TO;
1817 xprt->connect_timeout = XS_UDP_CONN_TO;
1818 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1819 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001820
Chuck Lever262965f2005-08-11 16:25:56 -04001821 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001822
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001823 if (args->timeout)
1824 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001825 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001826 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001827
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001828 switch (addr->sa_family) {
1829 case AF_INET:
1830 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1831 xprt_set_bound(xprt);
1832
1833 INIT_DELAYED_WORK(&transport->connect_worker,
1834 xs_udp_connect_worker4);
1835 xs_format_ipv4_peer_addresses(xprt);
1836 break;
1837 case AF_INET6:
1838 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1839 xprt_set_bound(xprt);
1840
1841 INIT_DELAYED_WORK(&transport->connect_worker,
1842 xs_udp_connect_worker6);
1843 xs_format_ipv6_peer_addresses(xprt);
1844 break;
1845 default:
1846 kfree(xprt);
1847 return ERR_PTR(-EAFNOSUPPORT);
1848 }
1849
Chuck Lever46121cf2007-01-31 12:14:08 -05001850 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001851 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001852
Chuck Leverc8541ec2006-10-17 14:44:27 -04001853 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001854}
1855
Chuck Lever9903cd12005-08-11 16:25:26 -04001856/**
1857 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001858 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001859 *
1860 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001861struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001862{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001863 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001864 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001865 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001866
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001867 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001868 if (IS_ERR(xprt))
1869 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001870 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001871
Chuck Leverec739ef2006-08-22 20:06:15 -04001872 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001873 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001874 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001875
Chuck Lever03bf4b72005-08-25 16:25:55 -07001876 xprt->bind_timeout = XS_BIND_TO;
1877 xprt->connect_timeout = XS_TCP_CONN_TO;
1878 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1879 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001880
Chuck Lever262965f2005-08-11 16:25:56 -04001881 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001882
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001883 if (args->timeout)
1884 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001885 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001886 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001887
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001888 switch (addr->sa_family) {
1889 case AF_INET:
1890 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1891 xprt_set_bound(xprt);
1892
1893 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
1894 xs_format_ipv4_peer_addresses(xprt);
1895 break;
1896 case AF_INET6:
1897 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1898 xprt_set_bound(xprt);
1899
1900 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
1901 xs_format_ipv6_peer_addresses(xprt);
1902 break;
1903 default:
1904 kfree(xprt);
1905 return ERR_PTR(-EAFNOSUPPORT);
1906 }
1907
Chuck Lever46121cf2007-01-31 12:14:08 -05001908 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001909 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001910
Chuck Leverc8541ec2006-10-17 14:44:27 -04001911 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001912}
Chuck Lever282b32e2006-12-05 16:35:51 -05001913
1914/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001915 * init_socket_xprt - set up xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001916 *
1917 */
1918int init_socket_xprt(void)
1919{
Chuck Leverfbf76682006-12-05 16:35:54 -05001920#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08001921 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001922 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05001923#endif
1924
Chuck Lever282b32e2006-12-05 16:35:51 -05001925 return 0;
1926}
1927
1928/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001929 * cleanup_socket_xprt - remove xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001930 *
1931 */
1932void cleanup_socket_xprt(void)
1933{
Chuck Leverfbf76682006-12-05 16:35:54 -05001934#ifdef RPC_DEBUG
1935 if (sunrpc_table_header) {
1936 unregister_sysctl_table(sunrpc_table_header);
1937 sunrpc_table_header = NULL;
1938 }
1939#endif
Chuck Lever282b32e2006-12-05 16:35:51 -05001940}