blob: cc4db1763aa476fe082a6fbb62dbe56b1841db75 [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 Levera246b012005-08-11 16:25:23 -040016 */
17
18#include <linux/types.h>
19#include <linux/slab.h>
20#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040021#include <linux/pagemap.h>
22#include <linux/errno.h>
23#include <linux/socket.h>
24#include <linux/in.h>
25#include <linux/net.h>
26#include <linux/mm.h>
27#include <linux/udp.h>
28#include <linux/tcp.h>
29#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010030#include <linux/sunrpc/sched.h>
Chuck Levera246b012005-08-11 16:25:23 -040031#include <linux/file.h>
32
33#include <net/sock.h>
34#include <net/checksum.h>
35#include <net/udp.h>
36#include <net/tcp.h>
37
Chuck Lever9903cd12005-08-11 16:25:26 -040038/*
Chuck Leverc556b752005-11-01 12:24:48 -050039 * xprtsock tunables
40 */
41unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
42unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
43
44unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
45unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
46
47/*
Chuck Leverfbf76682006-12-05 16:35:54 -050048 * We can register our own files under /proc/sys/sunrpc by
49 * calling register_sysctl_table() again. The files in that
50 * directory become the union of all files registered there.
51 *
52 * We simply need to make sure that we don't collide with
53 * someone else's file names!
54 */
55
56#ifdef RPC_DEBUG
57
58static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
59static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
60static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
61static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
62
63static struct ctl_table_header *sunrpc_table_header;
64
65/*
66 * FIXME: changing the UDP slot table size should also resize the UDP
67 * socket buffers for existing UDP transports
68 */
69static ctl_table xs_tunables_table[] = {
70 {
71 .ctl_name = CTL_SLOTTABLE_UDP,
72 .procname = "udp_slot_table_entries",
73 .data = &xprt_udp_slot_table_entries,
74 .maxlen = sizeof(unsigned int),
75 .mode = 0644,
76 .proc_handler = &proc_dointvec_minmax,
77 .strategy = &sysctl_intvec,
78 .extra1 = &min_slot_table_size,
79 .extra2 = &max_slot_table_size
80 },
81 {
82 .ctl_name = CTL_SLOTTABLE_TCP,
83 .procname = "tcp_slot_table_entries",
84 .data = &xprt_tcp_slot_table_entries,
85 .maxlen = sizeof(unsigned int),
86 .mode = 0644,
87 .proc_handler = &proc_dointvec_minmax,
88 .strategy = &sysctl_intvec,
89 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
91 },
92 {
93 .ctl_name = CTL_MIN_RESVPORT,
94 .procname = "min_resvport",
95 .data = &xprt_min_resvport,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = &proc_dointvec_minmax,
99 .strategy = &sysctl_intvec,
100 .extra1 = &xprt_min_resvport_limit,
101 .extra2 = &xprt_max_resvport_limit
102 },
103 {
104 .ctl_name = CTL_MAX_RESVPORT,
105 .procname = "max_resvport",
106 .data = &xprt_max_resvport,
107 .maxlen = sizeof(unsigned int),
108 .mode = 0644,
109 .proc_handler = &proc_dointvec_minmax,
110 .strategy = &sysctl_intvec,
111 .extra1 = &xprt_min_resvport_limit,
112 .extra2 = &xprt_max_resvport_limit
113 },
114 {
115 .ctl_name = 0,
116 },
117};
118
119static ctl_table sunrpc_table[] = {
120 {
121 .ctl_name = CTL_SUNRPC,
122 .procname = "sunrpc",
123 .mode = 0555,
124 .child = xs_tunables_table
125 },
126 {
127 .ctl_name = 0,
128 },
129};
130
131#endif
132
133/*
Chuck Lever262965f2005-08-11 16:25:56 -0400134 * How many times to try sending a request on a socket before waiting
135 * for the socket buffer to clear.
136 */
137#define XS_SENDMSG_RETRY (10U)
138
Chuck Lever03bf4b72005-08-25 16:25:55 -0700139/*
140 * Time out for an RPC UDP socket connect. UDP socket connects are
141 * synchronous, but we set a timeout anyway in case of resource
142 * exhaustion on the local host.
143 */
144#define XS_UDP_CONN_TO (5U * HZ)
145
146/*
147 * Wait duration for an RPC TCP connection to be established. Solaris
148 * NFS over TCP uses 60 seconds, for example, which is in line with how
149 * long a server takes to reboot.
150 */
151#define XS_TCP_CONN_TO (60U * HZ)
152
153/*
154 * Wait duration for a reply from the RPC portmapper.
155 */
156#define XS_BIND_TO (60U * HZ)
157
158/*
159 * Delay if a UDP socket connect error occurs. This is most likely some
160 * kind of resource problem on the local host.
161 */
162#define XS_UDP_REEST_TO (2U * HZ)
163
164/*
165 * The reestablish timeout allows clients to delay for a bit before attempting
166 * to reconnect to a server that just dropped our connection.
167 *
168 * We implement an exponential backoff when trying to reestablish a TCP
169 * transport connection with the server. Some servers like to drop a TCP
170 * connection when they are overworked, so we start with a short timeout and
171 * increase over time if the server is down or not responding.
172 */
173#define XS_TCP_INIT_REEST_TO (3U * HZ)
174#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
175
176/*
177 * TCP idle timeout; client drops the transport socket if it is idle
178 * for this long. Note that we also timeout UDP sockets to prevent
179 * holding port numbers when there is no RPC traffic.
180 */
181#define XS_IDLE_DISC_TO (5U * 60 * HZ)
182
Chuck Levera246b012005-08-11 16:25:23 -0400183#ifdef RPC_DEBUG
184# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400185# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400186#endif
187
Chuck Levera246b012005-08-11 16:25:23 -0400188#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400189static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400190{
Chuck Lever9903cd12005-08-11 16:25:26 -0400191 u8 *buf = (u8 *) packet;
192 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400193
Chuck Lever46121cf2007-01-31 12:14:08 -0500194 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400195 for (j = 0; j < count && j < 128; j += 4) {
196 if (!(j & 31)) {
197 if (j)
198 dprintk("\n");
199 dprintk("0x%04x ", j);
200 }
201 dprintk("%02x%02x%02x%02x ",
202 buf[j], buf[j+1], buf[j+2], buf[j+3]);
203 }
204 dprintk("\n");
205}
206#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400207static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400208{
209 /* NOP */
210}
211#endif
212
Chuck Leverffc2e512006-12-05 16:35:11 -0500213struct sock_xprt {
214 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500215
216 /*
217 * Network layer
218 */
219 struct socket * sock;
220 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500221
222 /*
223 * State of TCP reply receive
224 */
225 __be32 tcp_fraghdr,
226 tcp_xid;
227
228 u32 tcp_offset,
229 tcp_reclen;
230
231 unsigned long tcp_copied,
232 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500233
234 /*
235 * Connection of transports
236 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500237 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200238 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500239 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500240
241 /*
242 * UDP socket buffer size parameters
243 */
244 size_t rcvsize,
245 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500246
247 /*
248 * Saved socket callback addresses
249 */
250 void (*old_data_ready)(struct sock *, int);
251 void (*old_state_change)(struct sock *);
252 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500253};
254
Chuck Levere136d092006-12-05 16:35:23 -0500255/*
256 * TCP receive state flags
257 */
258#define TCP_RCV_LAST_FRAG (1UL << 0)
259#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
260#define TCP_RCV_COPY_XID (1UL << 2)
261#define TCP_RCV_COPY_DATA (1UL << 3)
262
Chuck Leverba10f2c2007-08-06 11:57:07 -0400263static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400264{
265 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
266 char *buf;
267
268 buf = kzalloc(20, GFP_KERNEL);
269 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400270 snprintf(buf, 20, NIPQUAD_FMT,
Chuck Leveredb267a2006-08-22 20:06:18 -0400271 NIPQUAD(addr->sin_addr.s_addr));
272 }
273 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
274
275 buf = kzalloc(8, GFP_KERNEL);
276 if (buf) {
277 snprintf(buf, 8, "%u",
278 ntohs(addr->sin_port));
279 }
280 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
281
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400282 buf = kzalloc(8, GFP_KERNEL);
283 if (buf) {
284 if (xprt->prot == IPPROTO_UDP)
285 snprintf(buf, 8, "udp");
286 else
287 snprintf(buf, 8, "tcp");
288 }
289 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
Chuck Leveredb267a2006-08-22 20:06:18 -0400290
291 buf = kzalloc(48, GFP_KERNEL);
292 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400293 snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
Chuck Leveredb267a2006-08-22 20:06:18 -0400294 NIPQUAD(addr->sin_addr.s_addr),
295 ntohs(addr->sin_port),
296 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
297 }
298 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400299
300 buf = kzalloc(10, GFP_KERNEL);
301 if (buf) {
302 snprintf(buf, 10, "%02x%02x%02x%02x",
303 NIPQUAD(addr->sin_addr.s_addr));
304 }
305 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
306
307 buf = kzalloc(8, GFP_KERNEL);
308 if (buf) {
309 snprintf(buf, 8, "%4hx",
310 ntohs(addr->sin_port));
311 }
312 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Leveredb267a2006-08-22 20:06:18 -0400313}
314
Chuck Lever4b6473f2007-08-06 11:57:12 -0400315static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt)
316{
317 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &xprt->addr;
318 char *buf;
319
320 buf = kzalloc(40, GFP_KERNEL);
321 if (buf) {
322 snprintf(buf, 40, NIP6_FMT,
323 NIP6(addr->sin6_addr));
324 }
325 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
326
327 buf = kzalloc(8, GFP_KERNEL);
328 if (buf) {
329 snprintf(buf, 8, "%u",
330 ntohs(addr->sin6_port));
331 }
332 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
333
334 buf = kzalloc(8, GFP_KERNEL);
335 if (buf) {
336 if (xprt->prot == IPPROTO_UDP)
337 snprintf(buf, 8, "udp");
338 else
339 snprintf(buf, 8, "tcp");
340 }
341 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
342
343 buf = kzalloc(64, GFP_KERNEL);
344 if (buf) {
345 snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
346 NIP6(addr->sin6_addr),
347 ntohs(addr->sin6_port),
348 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
349 }
350 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
351
352 buf = kzalloc(36, GFP_KERNEL);
353 if (buf) {
354 snprintf(buf, 36, NIP6_SEQFMT,
355 NIP6(addr->sin6_addr));
356 }
357 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
358
359 buf = kzalloc(8, GFP_KERNEL);
360 if (buf) {
361 snprintf(buf, 8, "%4hx",
362 ntohs(addr->sin6_port));
363 }
364 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
365}
366
Chuck Leveredb267a2006-08-22 20:06:18 -0400367static void xs_free_peer_addresses(struct rpc_xprt *xprt)
368{
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400369 int i;
370
371 for (i = 0; i < RPC_DISPLAY_MAX; i++)
372 kfree(xprt->address_strings[i]);
Chuck Leveredb267a2006-08-22 20:06:18 -0400373}
374
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400375#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
376
Trond Myklebust24c56842006-10-17 15:06:22 -0400377static 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 -0400378{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400379 struct msghdr msg = {
380 .msg_name = addr,
381 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400382 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
383 };
384 struct kvec iov = {
385 .iov_base = vec->iov_base + base,
386 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400387 };
388
Trond Myklebust24c56842006-10-17 15:06:22 -0400389 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400390 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
392}
393
Trond Myklebust24c56842006-10-17 15:06:22 -0400394static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400395{
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 struct page **ppage;
397 unsigned int remainder;
398 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400399
Trond Myklebust24c56842006-10-17 15:06:22 -0400400 remainder = xdr->page_len - base;
401 base += xdr->page_base;
402 ppage = xdr->pages + (base >> PAGE_SHIFT);
403 base &= ~PAGE_MASK;
404 for(;;) {
405 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
406 int flags = XS_SENDMSG_FLAGS;
407
408 remainder -= len;
409 if (remainder != 0 || more)
410 flags |= MSG_MORE;
411 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
412 if (remainder == 0 || err != len)
413 break;
414 sent += err;
415 ppage++;
416 base = 0;
417 }
418 if (sent == 0)
419 return err;
420 if (err > 0)
421 sent += err;
422 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400423}
424
Chuck Lever9903cd12005-08-11 16:25:26 -0400425/**
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
432 *
Chuck Levera246b012005-08-11 16:25:23 -0400433 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400434static 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 -0400435{
Trond Myklebust24c56842006-10-17 15:06:22 -0400436 unsigned int remainder = xdr->len - base;
437 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400438
Chuck Lever262965f2005-08-11 16:25:56 -0400439 if (unlikely(!sock))
440 return -ENOTCONN;
441
442 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400443 if (base != 0) {
444 addr = NULL;
445 addrlen = 0;
446 }
Chuck Lever262965f2005-08-11 16:25:56 -0400447
Trond Myklebust24c56842006-10-17 15:06:22 -0400448 if (base < xdr->head[0].iov_len || addr != NULL) {
449 unsigned int len = xdr->head[0].iov_len - base;
450 remainder -= len;
451 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
452 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400453 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400454 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400455 base = 0;
456 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400457 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400458
Trond Myklebust24c56842006-10-17 15:06:22 -0400459 if (base < xdr->page_len) {
460 unsigned int len = xdr->page_len - base;
461 remainder -= len;
462 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
463 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400464 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400465 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400466 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400467 } else
468 base -= xdr->page_len;
469
470 if (base >= xdr->tail[0].iov_len)
471 return sent;
472 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400473out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400474 if (sent == 0)
475 return err;
476 if (err > 0)
477 sent += err;
478 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400479}
480
Chuck Lever9903cd12005-08-11 16:25:26 -0400481/**
Chuck Lever262965f2005-08-11 16:25:56 -0400482 * xs_nospace - place task on wait queue if transmit was incomplete
483 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400484 *
Chuck Levera246b012005-08-11 16:25:23 -0400485 */
Chuck Lever262965f2005-08-11 16:25:56 -0400486static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400487{
Chuck Lever262965f2005-08-11 16:25:56 -0400488 struct rpc_rqst *req = task->tk_rqstp;
489 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500490 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400491
Chuck Lever46121cf2007-01-31 12:14:08 -0500492 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400493 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
494 req->rq_slen);
495
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500496 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400497 /* Protect against races with write_space */
498 spin_lock_bh(&xprt->transport_lock);
499
500 /* Don't race with disconnect */
501 if (!xprt_connected(xprt))
502 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500503 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400504 xprt_wait_for_buffer_space(task);
505
506 spin_unlock_bh(&xprt->transport_lock);
507 } else
508 /* Keep holding the socket if it is blocked */
509 rpc_delay(task, HZ>>4);
510}
511
512/**
513 * xs_udp_send_request - write an RPC request to a UDP socket
514 * @task: address of RPC task that manages the state of an RPC request
515 *
516 * Return values:
517 * 0: The request has been sent
518 * EAGAIN: The socket was blocked, please call again later to
519 * complete the request
520 * ENOTCONN: Caller needs to invoke connect logic then call again
521 * other: Some other error occured, the request was not sent
522 */
523static int xs_udp_send_request(struct rpc_task *task)
524{
525 struct rpc_rqst *req = task->tk_rqstp;
526 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500527 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400528 struct xdr_buf *xdr = &req->rq_snd_buf;
529 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400530
Chuck Lever9903cd12005-08-11 16:25:26 -0400531 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400532 req->rq_svec->iov_base,
533 req->rq_svec->iov_len);
534
Chuck Lever262965f2005-08-11 16:25:56 -0400535 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500536 status = xs_sendpages(transport->sock,
537 (struct sockaddr *) &xprt->addr,
538 xprt->addrlen, xdr,
539 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400540
Chuck Lever46121cf2007-01-31 12:14:08 -0500541 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400542 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400543
Chuck Lever262965f2005-08-11 16:25:56 -0400544 if (likely(status >= (int) req->rq_slen))
545 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400546
Chuck Lever262965f2005-08-11 16:25:56 -0400547 /* Still some bytes left; set up for a retry later. */
548 if (status > 0)
549 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400550
Chuck Lever262965f2005-08-11 16:25:56 -0400551 switch (status) {
552 case -ENETUNREACH:
553 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400554 case -ECONNREFUSED:
555 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400556 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400557 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400558 case -EAGAIN:
559 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400560 break;
561 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500562 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400563 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400564 break;
Chuck Levera246b012005-08-11 16:25:23 -0400565 }
Chuck Lever262965f2005-08-11 16:25:56 -0400566
567 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400568}
569
Chuck Lever808012f2005-08-25 16:25:49 -0700570static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
571{
572 u32 reclen = buf->len - sizeof(rpc_fraghdr);
573 rpc_fraghdr *base = buf->head[0].iov_base;
574 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
575}
576
Chuck Lever9903cd12005-08-11 16:25:26 -0400577/**
Chuck Lever262965f2005-08-11 16:25:56 -0400578 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400579 * @task: address of RPC task that manages the state of an RPC request
580 *
581 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400582 * 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
Chuck Lever9903cd12005-08-11 16:25:26 -0400587 *
588 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400589 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400590 */
Chuck Lever262965f2005-08-11 16:25:56 -0400591static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400592{
593 struct rpc_rqst *req = task->tk_rqstp;
594 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500595 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400596 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400597 int status;
598 unsigned int retry = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400599
Chuck Lever808012f2005-08-25 16:25:49 -0700600 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400601
Chuck Lever262965f2005-08-11 16:25:56 -0400602 xs_pktdump("packet data:",
603 req->rq_svec->iov_base,
604 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400605
606 /* Continue transmitting the packet/record. We must be careful
607 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400608 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400609 while (1) {
610 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500611 status = xs_sendpages(transport->sock,
612 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400613
Chuck Lever46121cf2007-01-31 12:14:08 -0500614 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400615 xdr->len - req->rq_bytes_sent, status);
616
617 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400618 break;
619
Chuck Lever262965f2005-08-11 16:25:56 -0400620 /* If we've sent the entire packet, immediately
621 * reset the count of bytes sent. */
622 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500623 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400624 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
625 req->rq_bytes_sent = 0;
626 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400627 }
628
Chuck Levera246b012005-08-11 16:25:23 -0400629 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400630 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400631 break;
632 }
633
Chuck Lever262965f2005-08-11 16:25:56 -0400634 switch (status) {
635 case -EAGAIN:
636 xs_nospace(task);
637 break;
638 case -ECONNREFUSED:
639 case -ECONNRESET:
640 case -ENOTCONN:
641 case -EPIPE:
642 status = -ENOTCONN;
643 break;
644 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500645 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400646 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700647 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400648 break;
Chuck Levera246b012005-08-11 16:25:23 -0400649 }
Chuck Lever262965f2005-08-11 16:25:56 -0400650
Chuck Levera246b012005-08-11 16:25:23 -0400651 return status;
652}
653
Chuck Lever9903cd12005-08-11 16:25:26 -0400654/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400655 * xs_tcp_release_xprt - clean up after a tcp transmission
656 * @xprt: transport
657 * @task: rpc task
658 *
659 * This cleans up if an error causes us to abort the transmission of a request.
660 * In this case, the socket may need to be reset in order to avoid confusing
661 * the server.
662 */
663static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
664{
665 struct rpc_rqst *req;
666
667 if (task != xprt->snd_task)
668 return;
669 if (task == NULL)
670 goto out_release;
671 req = task->tk_rqstp;
672 if (req->rq_bytes_sent == 0)
673 goto out_release;
674 if (req->rq_bytes_sent == req->rq_snd_buf.len)
675 goto out_release;
676 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
677out_release:
678 xprt_release_xprt(xprt, task);
679}
680
681/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 * xs_close - close a socket
683 * @xprt: transport
684 *
Chuck Lever3167e122005-08-25 16:25:55 -0700685 * This is used when all requests are complete; ie, no DRC state remains
686 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400687 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400688static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400689{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500690 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
691 struct socket *sock = transport->sock;
692 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400693
694 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100695 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400696
Chuck Lever46121cf2007-01-31 12:14:08 -0500697 dprintk("RPC: xs_close xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400698
Chuck Levera246b012005-08-11 16:25:23 -0400699 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500700 transport->inet = NULL;
701 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400702
Chuck Lever9903cd12005-08-11 16:25:26 -0400703 sk->sk_user_data = NULL;
Chuck Lever314dfd72006-12-05 16:35:34 -0500704 sk->sk_data_ready = transport->old_data_ready;
705 sk->sk_state_change = transport->old_state_change;
706 sk->sk_write_space = transport->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400707 write_unlock_bh(&sk->sk_callback_lock);
708
Chuck Lever9903cd12005-08-11 16:25:26 -0400709 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400710
711 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100712clear_close_wait:
713 smp_mb__before_clear_bit();
714 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
715 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400716}
717
Chuck Lever9903cd12005-08-11 16:25:26 -0400718/**
719 * xs_destroy - prepare to shutdown a transport
720 * @xprt: doomed transport
721 *
722 */
723static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400724{
Chuck Leverc8475462006-12-05 16:35:26 -0500725 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
726
Chuck Lever46121cf2007-01-31 12:14:08 -0500727 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400728
Trond Myklebustc1384c92007-06-14 18:00:42 -0400729 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400730
731 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400732 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400733 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400734 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400735 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400736}
737
Chuck Lever9903cd12005-08-11 16:25:26 -0400738static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400739{
Chuck Lever9903cd12005-08-11 16:25:26 -0400740 return (struct rpc_xprt *) sk->sk_user_data;
741}
742
743/**
744 * xs_udp_data_ready - "data ready" callback for UDP sockets
745 * @sk: socket with data to read
746 * @len: how much data to read
747 *
748 */
749static void xs_udp_data_ready(struct sock *sk, int len)
750{
751 struct rpc_task *task;
752 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400753 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400754 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400755 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700756 u32 _xid;
757 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400758
759 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500760 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400761 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400762 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400763
764 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
765 goto out;
766
767 if (xprt->shutdown)
768 goto dropit;
769
770 repsize = skb->len - sizeof(struct udphdr);
771 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500772 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400773 goto dropit;
774 }
775
776 /* Copy the XID from the skb... */
777 xp = skb_header_pointer(skb, sizeof(struct udphdr),
778 sizeof(_xid), &_xid);
779 if (xp == NULL)
780 goto dropit;
781
782 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400783 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400784 rovr = xprt_lookup_rqst(xprt, *xp);
785 if (!rovr)
786 goto out_unlock;
787 task = rovr->rq_task;
788
Chuck Levera246b012005-08-11 16:25:23 -0400789 if ((copied = rovr->rq_private_buf.buflen) > repsize)
790 copied = repsize;
791
792 /* Suck it into the iovec, verify checksum if not done by hw. */
793 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
794 goto out_unlock;
795
796 /* Something worked... */
797 dst_confirm(skb->dst);
798
Chuck Lever1570c1e2005-08-25 16:25:52 -0700799 xprt_adjust_cwnd(task, copied);
800 xprt_update_rtt(task);
801 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400802
803 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400804 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400805 dropit:
806 skb_free_datagram(sk, skb);
807 out:
808 read_unlock(&sk->sk_callback_lock);
809}
810
Chuck Leverdd456472006-12-05 16:35:44 -0500811static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400812{
Chuck Lever51971132006-12-05 16:35:19 -0500813 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400814 size_t len, used;
815 char *p;
816
Chuck Lever51971132006-12-05 16:35:19 -0500817 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
818 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500819 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500820 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400821 if (used != len)
822 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700823
Chuck Lever51971132006-12-05 16:35:19 -0500824 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
825 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500826 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400827 else
Chuck Levere136d092006-12-05 16:35:23 -0500828 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500829 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700830
Chuck Levere136d092006-12-05 16:35:23 -0500831 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500832 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700833
Chuck Levera246b012005-08-11 16:25:23 -0400834 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500835 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500836 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400837 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400838 return;
Chuck Levera246b012005-08-11 16:25:23 -0400839 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500840 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500841 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400842}
843
Chuck Lever51971132006-12-05 16:35:19 -0500844static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400845{
Chuck Lever51971132006-12-05 16:35:19 -0500846 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500847 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500848 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500849 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
850 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
851 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500852 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400853 }
854 }
855}
856
Chuck Leverdd456472006-12-05 16:35:44 -0500857static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400858{
859 size_t len, used;
860 char *p;
861
Chuck Lever51971132006-12-05 16:35:19 -0500862 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500863 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500864 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500865 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500866 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400867 if (used != len)
868 return;
Chuck Levere136d092006-12-05 16:35:23 -0500869 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
870 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500871 transport->tcp_copied = 4;
Chuck Lever46121cf2007-01-31 12:14:08 -0500872 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500873 ntohl(transport->tcp_xid));
874 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400875}
876
Chuck Leverdd456472006-12-05 16:35:44 -0500877static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400878{
Chuck Lever51971132006-12-05 16:35:19 -0500879 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400880 struct rpc_rqst *req;
881 struct xdr_buf *rcvbuf;
882 size_t len;
883 ssize_t r;
884
885 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400886 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500887 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400888 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500889 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500890 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500891 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400892 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400893 return;
894 }
895
896 rcvbuf = &req->rq_private_buf;
897 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500898 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500899 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400900
Chuck Lever51971132006-12-05 16:35:19 -0500901 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400902 memcpy(&my_desc, desc, sizeof(my_desc));
903 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500904 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500905 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400906 desc->count -= r;
907 desc->offset += r;
908 } else
Chuck Lever51971132006-12-05 16:35:19 -0500909 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500910 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400911
912 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500913 transport->tcp_copied += r;
914 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400915 }
916 if (r != len) {
917 /* Error when copying to the receive buffer,
918 * usually because we weren't able to allocate
919 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500920 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400921 * will not receive any additional updates,
922 * and time out.
923 * Any remaining data from this record will
924 * be discarded.
925 */
Chuck Levere136d092006-12-05 16:35:23 -0500926 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500927 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500928 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -0500929 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
930 "tcp_offset = %u, tcp_reclen = %u\n",
931 xprt, transport->tcp_copied,
932 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400933 goto out;
934 }
935
Chuck Lever46121cf2007-01-31 12:14:08 -0500936 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500937 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -0500938 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
939 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
940 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400941
Chuck Lever51971132006-12-05 16:35:19 -0500942 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500943 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500944 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500945 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
946 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400947 }
948
949out:
Chuck Levere136d092006-12-05 16:35:23 -0500950 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500951 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400952 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500953 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400954}
955
Chuck Leverdd456472006-12-05 16:35:44 -0500956static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400957{
958 size_t len;
959
Chuck Lever51971132006-12-05 16:35:19 -0500960 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400961 if (len > desc->count)
962 len = desc->count;
963 desc->count -= len;
964 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -0500965 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -0500966 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500967 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400968}
969
Chuck Lever9903cd12005-08-11 16:25:26 -0400970static 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 -0400971{
972 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -0500973 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -0500974 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -0400975 .skb = skb,
976 .offset = offset,
977 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -0400978 };
Chuck Levera246b012005-08-11 16:25:23 -0400979
Chuck Lever46121cf2007-01-31 12:14:08 -0500980 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400981 do {
982 /* Read in a new fragment marker if necessary */
983 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -0500984 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400985 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400986 continue;
987 }
988 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -0500989 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -0500990 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400991 continue;
992 }
993 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -0500994 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400995 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400996 continue;
997 }
998 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -0500999 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001000 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001001 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001002 return len - desc.count;
1003}
1004
Chuck Lever9903cd12005-08-11 16:25:26 -04001005/**
1006 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1007 * @sk: socket with data to read
1008 * @bytes: how much data to read
1009 *
1010 */
1011static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001012{
1013 struct rpc_xprt *xprt;
1014 read_descriptor_t rd_desc;
1015
Chuck Lever46121cf2007-01-31 12:14:08 -05001016 dprintk("RPC: xs_tcp_data_ready...\n");
1017
Chuck Levera246b012005-08-11 16:25:23 -04001018 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001019 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001020 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001021 if (xprt->shutdown)
1022 goto out;
1023
Chuck Lever9903cd12005-08-11 16:25:26 -04001024 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001025 rd_desc.arg.data = xprt;
1026 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -04001027 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -04001028out:
1029 read_unlock(&sk->sk_callback_lock);
1030}
1031
Chuck Lever9903cd12005-08-11 16:25:26 -04001032/**
1033 * xs_tcp_state_change - callback to handle TCP socket state changes
1034 * @sk: socket whose state has changed
1035 *
1036 */
1037static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001038{
Chuck Lever9903cd12005-08-11 16:25:26 -04001039 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001040
1041 read_lock(&sk->sk_callback_lock);
1042 if (!(xprt = xprt_from_sock(sk)))
1043 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001044 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1045 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1046 sk->sk_state, xprt_connected(xprt),
1047 sock_flag(sk, SOCK_DEAD),
1048 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001049
1050 switch (sk->sk_state) {
1051 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001052 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001053 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001054 struct sock_xprt *transport = container_of(xprt,
1055 struct sock_xprt, xprt);
1056
Chuck Levera246b012005-08-11 16:25:23 -04001057 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001058 transport->tcp_offset = 0;
1059 transport->tcp_reclen = 0;
1060 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001061 transport->tcp_flags =
1062 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001063
Chuck Lever03bf4b72005-08-25 16:25:55 -07001064 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -04001065 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001066 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001067 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001068 break;
1069 case TCP_SYN_SENT:
1070 case TCP_SYN_RECV:
1071 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001072 case TCP_CLOSE_WAIT:
1073 /* Try to schedule an autoclose RPC calls */
1074 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1075 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebustc1384c92007-06-14 18:00:42 -04001076 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -04001077 default:
1078 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001079 }
1080 out:
1081 read_unlock(&sk->sk_callback_lock);
1082}
1083
Chuck Lever9903cd12005-08-11 16:25:26 -04001084/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001085 * xs_udp_write_space - callback invoked when socket buffer space
1086 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001087 * @sk: socket whose state has changed
1088 *
Chuck Levera246b012005-08-11 16:25:23 -04001089 * Called when more output buffer space is available for this socket.
1090 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001091 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001092 * with a bunch of small requests.
1093 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001094static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001095{
Chuck Levera246b012005-08-11 16:25:23 -04001096 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001097
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001098 /* from net/core/sock.c:sock_def_write_space */
1099 if (sock_writeable(sk)) {
1100 struct socket *sock;
1101 struct rpc_xprt *xprt;
1102
1103 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -04001104 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001105 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -04001106 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001107 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1108 goto out;
1109
1110 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001111 }
1112
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001113 out:
1114 read_unlock(&sk->sk_callback_lock);
1115}
Chuck Levera246b012005-08-11 16:25:23 -04001116
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001117/**
1118 * xs_tcp_write_space - callback invoked when socket buffer space
1119 * becomes available
1120 * @sk: socket whose state has changed
1121 *
1122 * Called when more output buffer space is available for this socket.
1123 * We try not to wake our writers until they can make "significant"
1124 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1125 * with a bunch of small requests.
1126 */
1127static void xs_tcp_write_space(struct sock *sk)
1128{
1129 read_lock(&sk->sk_callback_lock);
1130
1131 /* from net/core/stream.c:sk_stream_write_space */
1132 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1133 struct socket *sock;
1134 struct rpc_xprt *xprt;
1135
1136 if (unlikely(!(sock = sk->sk_socket)))
1137 goto out;
1138 if (unlikely(!(xprt = xprt_from_sock(sk))))
1139 goto out;
1140 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1141 goto out;
1142
1143 xprt_write_space(xprt);
1144 }
1145
1146 out:
Chuck Levera246b012005-08-11 16:25:23 -04001147 read_unlock(&sk->sk_callback_lock);
1148}
1149
Chuck Lever470056c2005-08-25 16:25:56 -07001150static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001151{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001152 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1153 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001154
Chuck Lever7c6e0662006-12-05 16:35:30 -05001155 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001156 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001157 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001158 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001159 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001160 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001161 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001162 sk->sk_write_space(sk);
1163 }
1164}
1165
Chuck Lever43118c22005-08-25 16:25:49 -07001166/**
Chuck Lever470056c2005-08-25 16:25:56 -07001167 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001168 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001169 * @sndsize: requested size of send buffer, in bytes
1170 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001171 *
Chuck Lever470056c2005-08-25 16:25:56 -07001172 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001173 */
Chuck Lever470056c2005-08-25 16:25:56 -07001174static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001175{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001176 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1177
1178 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001179 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001180 transport->sndsize = sndsize + 1024;
1181 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001182 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001183 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001184
1185 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001186}
1187
Chuck Lever46c0ee82005-08-25 16:25:52 -07001188/**
1189 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1190 * @task: task that timed out
1191 *
1192 * Adjust the congestion window after a retransmit timeout has occurred.
1193 */
1194static void xs_udp_timer(struct rpc_task *task)
1195{
1196 xprt_adjust_cwnd(task, -ETIMEDOUT);
1197}
1198
Chuck Leverb85d8802006-05-25 01:40:49 -04001199static unsigned short xs_get_random_port(void)
1200{
1201 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1202 unsigned short rand = (unsigned short) net_random() % range;
1203 return rand + xprt_min_resvport;
1204}
1205
Chuck Lever92200412006-01-03 09:55:51 +01001206/**
1207 * xs_set_port - reset the port number in the remote endpoint address
1208 * @xprt: generic transport
1209 * @port: new port number
1210 *
1211 */
1212static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1213{
Chuck Lever20612002007-08-06 11:57:23 -04001214 struct sockaddr *addr = (struct sockaddr *) &xprt->addr;
Chuck Leverc4efcb12006-08-22 20:06:19 -04001215
Chuck Lever46121cf2007-01-31 12:14:08 -05001216 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001217
Chuck Lever20612002007-08-06 11:57:23 -04001218 switch (addr->sa_family) {
1219 case AF_INET:
1220 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1221 break;
1222 case AF_INET6:
1223 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1224 break;
1225 default:
1226 BUG();
1227 }
Chuck Lever92200412006-01-03 09:55:51 +01001228}
1229
Chuck Lever7dc753f2007-08-06 11:57:28 -04001230static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001231{
1232 struct sockaddr_in myaddr = {
1233 .sin_family = AF_INET,
1234 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001235 struct sockaddr_in *sa;
Chuck Lever529b33c2005-08-25 16:25:54 -07001236 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001237 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001238
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001239 if (!transport->xprt.resvport)
1240 port = 0;
1241 sa = (struct sockaddr_in *)&transport->addr;
1242 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001243 do {
1244 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001245 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001246 sizeof(myaddr));
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001247 if (!transport->xprt.resvport)
1248 break;
Chuck Levera246b012005-08-11 16:25:23 -04001249 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001250 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001251 break;
Chuck Levera246b012005-08-11 16:25:23 -04001252 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001253 if (port <= xprt_min_resvport)
1254 port = xprt_max_resvport;
1255 else
1256 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001257 } while (err == -EADDRINUSE && port != transport->port);
Chuck Lever7dc753f2007-08-06 11:57:28 -04001258 dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n",
1259 __FUNCTION__, NIPQUAD(myaddr.sin_addr),
1260 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001261 return err;
1262}
1263
Chuck Lever90058d32007-08-06 11:57:33 -04001264static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1265{
1266 struct sockaddr_in6 myaddr = {
1267 .sin6_family = AF_INET6,
1268 };
1269 struct sockaddr_in6 *sa;
1270 int err;
1271 unsigned short port = transport->port;
1272
1273 if (!transport->xprt.resvport)
1274 port = 0;
1275 sa = (struct sockaddr_in6 *)&transport->addr;
1276 myaddr.sin6_addr = sa->sin6_addr;
1277 do {
1278 myaddr.sin6_port = htons(port);
1279 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1280 sizeof(myaddr));
1281 if (!transport->xprt.resvport)
1282 break;
1283 if (err == 0) {
1284 transport->port = port;
1285 break;
1286 }
1287 if (port <= xprt_min_resvport)
1288 port = xprt_max_resvport;
1289 else
1290 port--;
1291 } while (err == -EADDRINUSE && port != transport->port);
1292 dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
1293 NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
1294 return err;
1295}
1296
Peter Zijlstraed075362006-12-06 20:35:24 -08001297#ifdef CONFIG_DEBUG_LOCK_ALLOC
1298static struct lock_class_key xs_key[2];
1299static struct lock_class_key xs_slock_key[2];
1300
1301static inline void xs_reclassify_socket(struct socket *sock)
1302{
1303 struct sock *sk = sock->sk;
1304 BUG_ON(sk->sk_lock.owner != NULL);
1305 switch (sk->sk_family) {
1306 case AF_INET:
1307 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFS",
1308 &xs_slock_key[0], "sk_lock-AF_INET-NFS", &xs_key[0]);
1309 break;
1310
1311 case AF_INET6:
1312 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFS",
1313 &xs_slock_key[1], "sk_lock-AF_INET6-NFS", &xs_key[1]);
1314 break;
1315
1316 default:
1317 BUG();
1318 }
1319}
1320#else
1321static inline void xs_reclassify_socket(struct socket *sock)
1322{
1323}
1324#endif
1325
Chuck Lever16be2d22007-08-06 11:57:38 -04001326static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1327{
1328 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1329
1330 if (!transport->inet) {
1331 struct sock *sk = sock->sk;
1332
1333 write_lock_bh(&sk->sk_callback_lock);
1334
1335 sk->sk_user_data = xprt;
1336 transport->old_data_ready = sk->sk_data_ready;
1337 transport->old_state_change = sk->sk_state_change;
1338 transport->old_write_space = sk->sk_write_space;
1339 sk->sk_data_ready = xs_udp_data_ready;
1340 sk->sk_write_space = xs_udp_write_space;
1341 sk->sk_no_check = UDP_CSUM_NORCV;
1342 sk->sk_allocation = GFP_ATOMIC;
1343
1344 xprt_set_connected(xprt);
1345
1346 /* Reset to new socket */
1347 transport->sock = sock;
1348 transport->inet = sk;
1349
1350 write_unlock_bh(&sk->sk_callback_lock);
1351 }
1352 xs_udp_do_set_buffer_size(xprt);
1353}
1354
Chuck Lever9903cd12005-08-11 16:25:26 -04001355/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001356 * xs_udp_connect_worker4 - set up a UDP socket
David Howells65f27f32006-11-22 14:55:48 +00001357 * @work: RPC transport to connect
Chuck Lever9903cd12005-08-11 16:25:26 -04001358 *
1359 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001360 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001361static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001362{
Trond Myklebust34161db2006-12-07 15:48:15 -05001363 struct sock_xprt *transport =
1364 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Leverc8475462006-12-05 16:35:26 -05001365 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001366 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001367 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001368
Chuck Leverec739ef2006-08-22 20:06:15 -04001369 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001370 goto out;
1371
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001372 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001373 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001374
1375 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001376 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001377 goto out;
1378 }
Peter Zijlstraed075362006-12-06 20:35:24 -08001379 xs_reclassify_socket(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001380
Chuck Lever7dc753f2007-08-06 11:57:28 -04001381 if (xs_bind4(transport, sock)) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001382 sock_release(sock);
1383 goto out;
1384 }
1385
Chuck Lever46121cf2007-01-31 12:14:08 -05001386 dprintk("RPC: worker connecting xprt %p to address: %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001387 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001388
Chuck Lever16be2d22007-08-06 11:57:38 -04001389 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001390 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001391out:
1392 xprt_wake_pending_tasks(xprt, status);
1393 xprt_clear_connecting(xprt);
1394}
1395
Chuck Lever68e220b2007-08-06 11:57:48 -04001396/**
1397 * xs_udp_connect_worker6 - set up a UDP socket
1398 * @work: RPC transport to connect
1399 *
1400 * Invoked by a work queue tasklet.
1401 */
1402static void xs_udp_connect_worker6(struct work_struct *work)
1403{
1404 struct sock_xprt *transport =
1405 container_of(work, struct sock_xprt, connect_worker.work);
1406 struct rpc_xprt *xprt = &transport->xprt;
1407 struct socket *sock = transport->sock;
1408 int err, status = -EIO;
1409
1410 if (xprt->shutdown || !xprt_bound(xprt))
1411 goto out;
1412
1413 /* Start by resetting any existing state */
1414 xs_close(xprt);
1415
1416 if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1417 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1418 goto out;
1419 }
1420 xs_reclassify_socket(sock);
1421
1422 if (xs_bind6(transport, sock) < 0) {
1423 sock_release(sock);
1424 goto out;
1425 }
1426
1427 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1428 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1429
1430 xs_udp_finish_connecting(xprt, sock);
1431 status = 0;
1432out:
1433 xprt_wake_pending_tasks(xprt, status);
1434 xprt_clear_connecting(xprt);
1435}
1436
Chuck Lever3167e122005-08-25 16:25:55 -07001437/*
1438 * We need to preserve the port number so the reply cache on the server can
1439 * find our cached RPC replies when we get around to reconnecting.
1440 */
1441static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1442{
1443 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001444 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001445 struct sockaddr any;
1446
Chuck Lever46121cf2007-01-31 12:14:08 -05001447 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001448
1449 /*
1450 * Disconnect the transport socket by doing a connect operation
1451 * with AF_UNSPEC. This should return immediately...
1452 */
1453 memset(&any, 0, sizeof(any));
1454 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001455 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001456 if (result)
Chuck Lever46121cf2007-01-31 12:14:08 -05001457 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001458 result);
1459}
1460
Chuck Lever16be2d22007-08-06 11:57:38 -04001461static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001462{
Chuck Lever16be2d22007-08-06 11:57:38 -04001463 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001464
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001465 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001466 struct sock *sk = sock->sk;
1467
1468 write_lock_bh(&sk->sk_callback_lock);
1469
1470 sk->sk_user_data = xprt;
Chuck Lever314dfd72006-12-05 16:35:34 -05001471 transport->old_data_ready = sk->sk_data_ready;
1472 transport->old_state_change = sk->sk_state_change;
1473 transport->old_write_space = sk->sk_write_space;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001474 sk->sk_data_ready = xs_tcp_data_ready;
1475 sk->sk_state_change = xs_tcp_state_change;
1476 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001477 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001478
1479 /* socket options */
1480 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1481 sock_reset_flag(sk, SOCK_LINGER);
1482 tcp_sk(sk)->linger2 = 0;
1483 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001484
1485 xprt_clear_connected(xprt);
1486
1487 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001488 transport->sock = sock;
1489 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001490
1491 write_unlock_bh(&sk->sk_callback_lock);
1492 }
1493
1494 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001495 xprt->stat.connect_count++;
1496 xprt->stat.connect_start = jiffies;
Chuck Lever16be2d22007-08-06 11:57:38 -04001497 return kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001498 xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001499}
1500
1501/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001502 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04001503 * @work: RPC transport to connect
1504 *
1505 * Invoked by a work queue tasklet.
1506 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001507static void xs_tcp_connect_worker4(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04001508{
1509 struct sock_xprt *transport =
1510 container_of(work, struct sock_xprt, connect_worker.work);
1511 struct rpc_xprt *xprt = &transport->xprt;
1512 struct socket *sock = transport->sock;
1513 int err, status = -EIO;
1514
1515 if (xprt->shutdown || !xprt_bound(xprt))
1516 goto out;
1517
1518 if (!sock) {
1519 /* start from scratch */
1520 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1521 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1522 goto out;
1523 }
1524 xs_reclassify_socket(sock);
1525
1526 if (xs_bind4(transport, sock) < 0) {
1527 sock_release(sock);
1528 goto out;
1529 }
1530 } else
1531 /* "close" the socket, preserving the local port */
1532 xs_tcp_reuse_connection(xprt);
1533
1534 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1535 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1536
1537 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001538 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1539 xprt, -status, xprt_connected(xprt),
1540 sock->sk->sk_state);
Chuck Levera246b012005-08-11 16:25:23 -04001541 if (status < 0) {
1542 switch (status) {
1543 case -EINPROGRESS:
1544 case -EALREADY:
1545 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001546 case -ECONNREFUSED:
1547 case -ECONNRESET:
1548 /* retry with existing socket, after a delay */
1549 break;
1550 default:
1551 /* get rid of existing socket, and retry */
1552 xs_close(xprt);
1553 break;
Chuck Levera246b012005-08-11 16:25:23 -04001554 }
1555 }
1556out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001557 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001558out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001559 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001560}
1561
Chuck Lever9903cd12005-08-11 16:25:26 -04001562/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001563 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1564 * @work: RPC transport to connect
1565 *
1566 * Invoked by a work queue tasklet.
1567 */
1568static void xs_tcp_connect_worker6(struct work_struct *work)
1569{
1570 struct sock_xprt *transport =
1571 container_of(work, struct sock_xprt, connect_worker.work);
1572 struct rpc_xprt *xprt = &transport->xprt;
1573 struct socket *sock = transport->sock;
1574 int err, status = -EIO;
1575
1576 if (xprt->shutdown || !xprt_bound(xprt))
1577 goto out;
1578
1579 if (!sock) {
1580 /* start from scratch */
1581 if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1582 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1583 goto out;
1584 }
1585 xs_reclassify_socket(sock);
1586
1587 if (xs_bind6(transport, sock) < 0) {
1588 sock_release(sock);
1589 goto out;
1590 }
1591 } else
1592 /* "close" the socket, preserving the local port */
1593 xs_tcp_reuse_connection(xprt);
1594
1595 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1596 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1597
1598 status = xs_tcp_finish_connecting(xprt, sock);
1599 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1600 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1601 if (status < 0) {
1602 switch (status) {
1603 case -EINPROGRESS:
1604 case -EALREADY:
1605 goto out_clear;
1606 case -ECONNREFUSED:
1607 case -ECONNRESET:
1608 /* retry with existing socket, after a delay */
1609 break;
1610 default:
1611 /* get rid of existing socket, and retry */
1612 xs_close(xprt);
1613 break;
1614 }
1615 }
1616out:
1617 xprt_wake_pending_tasks(xprt, status);
1618out_clear:
1619 xprt_clear_connecting(xprt);
1620}
1621
1622/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001623 * xs_connect - connect a socket to a remote endpoint
1624 * @task: address of RPC task that manages state of connect request
1625 *
1626 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001627 *
1628 * UDP socket connects are synchronous, but we use a work queue anyway
1629 * to guarantee that even unprivileged user processes can set up a
1630 * socket on a privileged port.
1631 *
1632 * If a UDP socket connect fails, the delay behavior here prevents
1633 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001634 */
1635static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001636{
1637 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001638 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001639
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001640 if (xprt_test_and_set_connecting(xprt))
1641 return;
1642
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001643 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001644 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1645 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001646 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001647 queue_delayed_work(rpciod_workqueue,
1648 &transport->connect_worker,
1649 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001650 xprt->reestablish_timeout <<= 1;
1651 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1652 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001653 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001654 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001655 queue_delayed_work(rpciod_workqueue,
1656 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001657 }
1658}
1659
Chuck Lever262ca072006-03-20 13:44:16 -05001660/**
1661 * xs_udp_print_stats - display UDP socket-specifc stats
1662 * @xprt: rpc_xprt struct containing statistics
1663 * @seq: output file
1664 *
1665 */
1666static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1667{
Chuck Leverc8475462006-12-05 16:35:26 -05001668 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1669
Chuck Lever262ca072006-03-20 13:44:16 -05001670 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001671 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001672 xprt->stat.bind_count,
1673 xprt->stat.sends,
1674 xprt->stat.recvs,
1675 xprt->stat.bad_xids,
1676 xprt->stat.req_u,
1677 xprt->stat.bklog_u);
1678}
1679
1680/**
1681 * xs_tcp_print_stats - display TCP socket-specifc stats
1682 * @xprt: rpc_xprt struct containing statistics
1683 * @seq: output file
1684 *
1685 */
1686static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1687{
Chuck Leverc8475462006-12-05 16:35:26 -05001688 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001689 long idle_time = 0;
1690
1691 if (xprt_connected(xprt))
1692 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1693
1694 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001695 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001696 xprt->stat.bind_count,
1697 xprt->stat.connect_count,
1698 xprt->stat.connect_time,
1699 idle_time,
1700 xprt->stat.sends,
1701 xprt->stat.recvs,
1702 xprt->stat.bad_xids,
1703 xprt->stat.req_u,
1704 xprt->stat.bklog_u);
1705}
1706
Chuck Lever262965f2005-08-11 16:25:56 -04001707static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001708 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001709 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001710 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04001711 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001712 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001713 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001714 .buf_alloc = rpc_malloc,
1715 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001716 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001717 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001718 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001719 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001720 .close = xs_close,
1721 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001722 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001723};
1724
1725static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001726 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001727 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04001728 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001729 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001730 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001731 .buf_alloc = rpc_malloc,
1732 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001733 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001734 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001735 .close = xs_close,
1736 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001737 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001738};
1739
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001740static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args, unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04001741{
1742 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001743 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001744
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001745 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001746 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001747 return ERR_PTR(-EBADF);
1748 }
1749
Chuck Leverffc2e512006-12-05 16:35:11 -05001750 new = kzalloc(sizeof(*new), GFP_KERNEL);
1751 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001752 dprintk("RPC: xs_setup_xprt: couldn't allocate "
1753 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001754 return ERR_PTR(-ENOMEM);
1755 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001756 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001757
1758 xprt->max_reqs = slot_table_size;
1759 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1760 if (xprt->slot == NULL) {
1761 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001762 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
1763 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001764 return ERR_PTR(-ENOMEM);
1765 }
1766
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001767 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
1768 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001769 if (args->srcaddr)
1770 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8475462006-12-05 16:35:26 -05001771 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001772
1773 return xprt;
1774}
1775
Chuck Lever9903cd12005-08-11 16:25:26 -04001776/**
1777 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001778 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001779 *
1780 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001781struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001782{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001783 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001784 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001785
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001786 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001787 if (IS_ERR(xprt))
1788 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001789 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001790
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001791 if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001792 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001793
1794 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001795 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001796 /* XXX: header size can vary due to auth type, IPv6, etc. */
1797 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1798
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001799 INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_connect_worker4);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001800 xprt->bind_timeout = XS_BIND_TO;
1801 xprt->connect_timeout = XS_UDP_CONN_TO;
1802 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1803 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001804
Chuck Lever262965f2005-08-11 16:25:56 -04001805 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001806
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001807 if (args->timeout)
1808 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001809 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001810 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001811
Chuck Leverba10f2c2007-08-06 11:57:07 -04001812 xs_format_ipv4_peer_addresses(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001813 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001814 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001815
Chuck Leverc8541ec2006-10-17 14:44:27 -04001816 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001817}
1818
Chuck Lever9903cd12005-08-11 16:25:26 -04001819/**
1820 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001821 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001822 *
1823 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001824struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001825{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001826 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001827 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001828
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001829 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001830 if (IS_ERR(xprt))
1831 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001832 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001833
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001834 if (ntohs(((struct sockaddr_in *)args->dstaddr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001835 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001836
1837 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001838 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001839 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001840
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001841 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001842 xprt->bind_timeout = XS_BIND_TO;
1843 xprt->connect_timeout = XS_TCP_CONN_TO;
1844 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1845 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001846
Chuck Lever262965f2005-08-11 16:25:56 -04001847 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001848
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001849 if (args->timeout)
1850 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001851 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001852 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001853
Chuck Leverba10f2c2007-08-06 11:57:07 -04001854 xs_format_ipv4_peer_addresses(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001855 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001856 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001857
Chuck Leverc8541ec2006-10-17 14:44:27 -04001858 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001859}
Chuck Lever282b32e2006-12-05 16:35:51 -05001860
1861/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001862 * init_socket_xprt - set up xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001863 *
1864 */
1865int init_socket_xprt(void)
1866{
Chuck Leverfbf76682006-12-05 16:35:54 -05001867#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08001868 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001869 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05001870#endif
1871
Chuck Lever282b32e2006-12-05 16:35:51 -05001872 return 0;
1873}
1874
1875/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001876 * cleanup_socket_xprt - remove xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001877 *
1878 */
1879void cleanup_socket_xprt(void)
1880{
Chuck Leverfbf76682006-12-05 16:35:54 -05001881#ifdef RPC_DEBUG
1882 if (sunrpc_table_header) {
1883 unregister_sysctl_table(sunrpc_table_header);
1884 sunrpc_table_header = NULL;
1885 }
1886#endif
Chuck Lever282b32e2006-12-05 16:35:51 -05001887}