blob: 3bc9c921c82ff9755f6eb826048fb1b263290f55 [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>
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -040023#include <linux/module.h>
Chuck Levera246b012005-08-11 16:25:23 -040024#include <linux/capability.h>
Chuck Levera246b012005-08-11 16:25:23 -040025#include <linux/pagemap.h>
26#include <linux/errno.h>
27#include <linux/socket.h>
28#include <linux/in.h>
29#include <linux/net.h>
30#include <linux/mm.h>
31#include <linux/udp.h>
32#include <linux/tcp.h>
33#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010034#include <linux/sunrpc/sched.h>
Chuck Levera246b012005-08-11 16:25:23 -040035#include <linux/file.h>
36
37#include <net/sock.h>
38#include <net/checksum.h>
39#include <net/udp.h>
40#include <net/tcp.h>
41
Chuck Lever9903cd12005-08-11 16:25:26 -040042/*
Chuck Leverc556b752005-11-01 12:24:48 -050043 * xprtsock tunables
44 */
45unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
46unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47
48unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
49unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
50
51/*
Chuck Leverfbf76682006-12-05 16:35:54 -050052 * We can register our own files under /proc/sys/sunrpc by
53 * calling register_sysctl_table() again. The files in that
54 * directory become the union of all files registered there.
55 *
56 * We simply need to make sure that we don't collide with
57 * someone else's file names!
58 */
59
60#ifdef RPC_DEBUG
61
62static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
63static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
64static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
65static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
66
67static struct ctl_table_header *sunrpc_table_header;
68
69/*
70 * FIXME: changing the UDP slot table size should also resize the UDP
71 * socket buffers for existing UDP transports
72 */
73static ctl_table xs_tunables_table[] = {
74 {
75 .ctl_name = CTL_SLOTTABLE_UDP,
76 .procname = "udp_slot_table_entries",
77 .data = &xprt_udp_slot_table_entries,
78 .maxlen = sizeof(unsigned int),
79 .mode = 0644,
80 .proc_handler = &proc_dointvec_minmax,
81 .strategy = &sysctl_intvec,
82 .extra1 = &min_slot_table_size,
83 .extra2 = &max_slot_table_size
84 },
85 {
86 .ctl_name = CTL_SLOTTABLE_TCP,
87 .procname = "tcp_slot_table_entries",
88 .data = &xprt_tcp_slot_table_entries,
89 .maxlen = sizeof(unsigned int),
90 .mode = 0644,
91 .proc_handler = &proc_dointvec_minmax,
92 .strategy = &sysctl_intvec,
93 .extra1 = &min_slot_table_size,
94 .extra2 = &max_slot_table_size
95 },
96 {
97 .ctl_name = CTL_MIN_RESVPORT,
98 .procname = "min_resvport",
99 .data = &xprt_min_resvport,
100 .maxlen = sizeof(unsigned int),
101 .mode = 0644,
102 .proc_handler = &proc_dointvec_minmax,
103 .strategy = &sysctl_intvec,
104 .extra1 = &xprt_min_resvport_limit,
105 .extra2 = &xprt_max_resvport_limit
106 },
107 {
108 .ctl_name = CTL_MAX_RESVPORT,
109 .procname = "max_resvport",
110 .data = &xprt_max_resvport,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
113 .proc_handler = &proc_dointvec_minmax,
114 .strategy = &sysctl_intvec,
115 .extra1 = &xprt_min_resvport_limit,
116 .extra2 = &xprt_max_resvport_limit
117 },
118 {
119 .ctl_name = 0,
120 },
121};
122
123static ctl_table sunrpc_table[] = {
124 {
125 .ctl_name = CTL_SUNRPC,
126 .procname = "sunrpc",
127 .mode = 0555,
128 .child = xs_tunables_table
129 },
130 {
131 .ctl_name = 0,
132 },
133};
134
135#endif
136
137/*
Chuck Lever262965f2005-08-11 16:25:56 -0400138 * How many times to try sending a request on a socket before waiting
139 * for the socket buffer to clear.
140 */
141#define XS_SENDMSG_RETRY (10U)
142
Chuck Lever03bf4b72005-08-25 16:25:55 -0700143/*
144 * Time out for an RPC UDP socket connect. UDP socket connects are
145 * synchronous, but we set a timeout anyway in case of resource
146 * exhaustion on the local host.
147 */
148#define XS_UDP_CONN_TO (5U * HZ)
149
150/*
151 * Wait duration for an RPC TCP connection to be established. Solaris
152 * NFS over TCP uses 60 seconds, for example, which is in line with how
153 * long a server takes to reboot.
154 */
155#define XS_TCP_CONN_TO (60U * HZ)
156
157/*
158 * Wait duration for a reply from the RPC portmapper.
159 */
160#define XS_BIND_TO (60U * HZ)
161
162/*
163 * Delay if a UDP socket connect error occurs. This is most likely some
164 * kind of resource problem on the local host.
165 */
166#define XS_UDP_REEST_TO (2U * HZ)
167
168/*
169 * The reestablish timeout allows clients to delay for a bit before attempting
170 * to reconnect to a server that just dropped our connection.
171 *
172 * We implement an exponential backoff when trying to reestablish a TCP
173 * transport connection with the server. Some servers like to drop a TCP
174 * connection when they are overworked, so we start with a short timeout and
175 * increase over time if the server is down or not responding.
176 */
177#define XS_TCP_INIT_REEST_TO (3U * HZ)
178#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
179
180/*
181 * TCP idle timeout; client drops the transport socket if it is idle
182 * for this long. Note that we also timeout UDP sockets to prevent
183 * holding port numbers when there is no RPC traffic.
184 */
185#define XS_IDLE_DISC_TO (5U * 60 * HZ)
186
Chuck Levera246b012005-08-11 16:25:23 -0400187#ifdef RPC_DEBUG
188# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400189# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400190#endif
191
Chuck Levera246b012005-08-11 16:25:23 -0400192#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400193static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400194{
Chuck Lever9903cd12005-08-11 16:25:26 -0400195 u8 *buf = (u8 *) packet;
196 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400197
Chuck Lever46121cf2007-01-31 12:14:08 -0500198 dprintk("RPC: %s\n", msg);
Chuck Levera246b012005-08-11 16:25:23 -0400199 for (j = 0; j < count && j < 128; j += 4) {
200 if (!(j & 31)) {
201 if (j)
202 dprintk("\n");
203 dprintk("0x%04x ", j);
204 }
205 dprintk("%02x%02x%02x%02x ",
206 buf[j], buf[j+1], buf[j+2], buf[j+3]);
207 }
208 dprintk("\n");
209}
210#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400211static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400212{
213 /* NOP */
214}
215#endif
216
Chuck Leverffc2e512006-12-05 16:35:11 -0500217struct sock_xprt {
218 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500219
220 /*
221 * Network layer
222 */
223 struct socket * sock;
224 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500225
226 /*
227 * State of TCP reply receive
228 */
229 __be32 tcp_fraghdr,
230 tcp_xid;
231
232 u32 tcp_offset,
233 tcp_reclen;
234
235 unsigned long tcp_copied,
236 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500237
238 /*
239 * Connection of transports
240 */
Trond Myklebust34161db2006-12-07 15:48:15 -0500241 struct delayed_work connect_worker;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200242 struct sockaddr_storage addr;
Chuck Leverc8475462006-12-05 16:35:26 -0500243 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500244
245 /*
246 * UDP socket buffer size parameters
247 */
248 size_t rcvsize,
249 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500250
251 /*
252 * Saved socket callback addresses
253 */
254 void (*old_data_ready)(struct sock *, int);
255 void (*old_state_change)(struct sock *);
256 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500257};
258
Chuck Levere136d092006-12-05 16:35:23 -0500259/*
260 * TCP receive state flags
261 */
262#define TCP_RCV_LAST_FRAG (1UL << 0)
263#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
264#define TCP_RCV_COPY_XID (1UL << 2)
265#define TCP_RCV_COPY_DATA (1UL << 3)
266
Chuck Lever95392c52007-08-06 11:57:58 -0400267static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
268{
269 return (struct sockaddr *) &xprt->addr;
270}
271
272static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
273{
274 return (struct sockaddr_in *) &xprt->addr;
275}
276
277static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
278{
279 return (struct sockaddr_in6 *) &xprt->addr;
280}
281
Chuck Leverba10f2c2007-08-06 11:57:07 -0400282static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt)
Chuck Leveredb267a2006-08-22 20:06:18 -0400283{
Chuck Lever95392c52007-08-06 11:57:58 -0400284 struct sockaddr_in *addr = xs_addr_in(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400285 char *buf;
286
287 buf = kzalloc(20, GFP_KERNEL);
288 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400289 snprintf(buf, 20, NIPQUAD_FMT,
Chuck Leveredb267a2006-08-22 20:06:18 -0400290 NIPQUAD(addr->sin_addr.s_addr));
291 }
292 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
293
294 buf = kzalloc(8, GFP_KERNEL);
295 if (buf) {
296 snprintf(buf, 8, "%u",
297 ntohs(addr->sin_port));
298 }
299 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
300
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400301 buf = kzalloc(8, GFP_KERNEL);
302 if (buf) {
303 if (xprt->prot == IPPROTO_UDP)
304 snprintf(buf, 8, "udp");
305 else
306 snprintf(buf, 8, "tcp");
307 }
308 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
Chuck Leveredb267a2006-08-22 20:06:18 -0400309
310 buf = kzalloc(48, GFP_KERNEL);
311 if (buf) {
Chuck Leverbda243d2007-08-06 11:56:52 -0400312 snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
Chuck Leveredb267a2006-08-22 20:06:18 -0400313 NIPQUAD(addr->sin_addr.s_addr),
314 ntohs(addr->sin_port),
315 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
316 }
317 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
Chuck Leverfbfe3cc2007-08-06 11:57:02 -0400318
319 buf = kzalloc(10, GFP_KERNEL);
320 if (buf) {
321 snprintf(buf, 10, "%02x%02x%02x%02x",
322 NIPQUAD(addr->sin_addr.s_addr));
323 }
324 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
325
326 buf = kzalloc(8, GFP_KERNEL);
327 if (buf) {
328 snprintf(buf, 8, "%4hx",
329 ntohs(addr->sin_port));
330 }
331 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400332
333 buf = kzalloc(30, GFP_KERNEL);
334 if (buf) {
335 snprintf(buf, 30, NIPQUAD_FMT".%u.%u",
336 NIPQUAD(addr->sin_addr.s_addr),
337 ntohs(addr->sin_port) >> 8,
338 ntohs(addr->sin_port) & 0xff);
339 }
340 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400341
342 xprt->address_strings[RPC_DISPLAY_NETID] =
343 kstrdup(xprt->prot == IPPROTO_UDP ?
344 RPCBIND_NETID_UDP : RPCBIND_NETID_TCP, GFP_KERNEL);
Chuck Leveredb267a2006-08-22 20:06:18 -0400345}
346
Chuck Lever4b6473f2007-08-06 11:57:12 -0400347static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt)
348{
Chuck Lever95392c52007-08-06 11:57:58 -0400349 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400350 char *buf;
351
352 buf = kzalloc(40, GFP_KERNEL);
353 if (buf) {
354 snprintf(buf, 40, NIP6_FMT,
355 NIP6(addr->sin6_addr));
356 }
357 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
358
359 buf = kzalloc(8, GFP_KERNEL);
360 if (buf) {
361 snprintf(buf, 8, "%u",
362 ntohs(addr->sin6_port));
363 }
364 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
365
366 buf = kzalloc(8, GFP_KERNEL);
367 if (buf) {
368 if (xprt->prot == IPPROTO_UDP)
369 snprintf(buf, 8, "udp");
370 else
371 snprintf(buf, 8, "tcp");
372 }
373 xprt->address_strings[RPC_DISPLAY_PROTO] = buf;
374
375 buf = kzalloc(64, GFP_KERNEL);
376 if (buf) {
377 snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
378 NIP6(addr->sin6_addr),
379 ntohs(addr->sin6_port),
380 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
381 }
382 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
383
384 buf = kzalloc(36, GFP_KERNEL);
385 if (buf) {
386 snprintf(buf, 36, NIP6_SEQFMT,
387 NIP6(addr->sin6_addr));
388 }
389 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
390
391 buf = kzalloc(8, GFP_KERNEL);
392 if (buf) {
393 snprintf(buf, 8, "%4hx",
394 ntohs(addr->sin6_port));
395 }
396 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
Chuck Lever756805e2007-08-16 16:03:26 -0400397
398 buf = kzalloc(50, GFP_KERNEL);
399 if (buf) {
400 snprintf(buf, 50, NIP6_FMT".%u.%u",
401 NIP6(addr->sin6_addr),
402 ntohs(addr->sin6_port) >> 8,
403 ntohs(addr->sin6_port) & 0xff);
404 }
405 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400406
407 xprt->address_strings[RPC_DISPLAY_NETID] =
408 kstrdup(xprt->prot == IPPROTO_UDP ?
409 RPCBIND_NETID_UDP6 : RPCBIND_NETID_TCP6, GFP_KERNEL);
Chuck Lever4b6473f2007-08-06 11:57:12 -0400410}
411
Chuck Leveredb267a2006-08-22 20:06:18 -0400412static void xs_free_peer_addresses(struct rpc_xprt *xprt)
413{
Chuck Lever0c43b3d2007-08-06 11:56:57 -0400414 int i;
415
416 for (i = 0; i < RPC_DISPLAY_MAX; i++)
417 kfree(xprt->address_strings[i]);
Chuck Leveredb267a2006-08-22 20:06:18 -0400418}
419
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400420#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
421
Trond Myklebust24c56842006-10-17 15:06:22 -0400422static 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 -0400423{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400424 struct msghdr msg = {
425 .msg_name = addr,
426 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400427 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
428 };
429 struct kvec iov = {
430 .iov_base = vec->iov_base + base,
431 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400432 };
433
Trond Myklebust24c56842006-10-17 15:06:22 -0400434 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400435 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
436 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
437}
438
Trond Myklebust24c56842006-10-17 15:06:22 -0400439static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400440{
Trond Myklebust24c56842006-10-17 15:06:22 -0400441 struct page **ppage;
442 unsigned int remainder;
443 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400444
Trond Myklebust24c56842006-10-17 15:06:22 -0400445 remainder = xdr->page_len - base;
446 base += xdr->page_base;
447 ppage = xdr->pages + (base >> PAGE_SHIFT);
448 base &= ~PAGE_MASK;
449 for(;;) {
450 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
451 int flags = XS_SENDMSG_FLAGS;
452
453 remainder -= len;
454 if (remainder != 0 || more)
455 flags |= MSG_MORE;
456 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
457 if (remainder == 0 || err != len)
458 break;
459 sent += err;
460 ppage++;
461 base = 0;
462 }
463 if (sent == 0)
464 return err;
465 if (err > 0)
466 sent += err;
467 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400468}
469
Chuck Lever9903cd12005-08-11 16:25:26 -0400470/**
471 * xs_sendpages - write pages directly to a socket
472 * @sock: socket to send on
473 * @addr: UDP only -- address of destination
474 * @addrlen: UDP only -- length of destination address
475 * @xdr: buffer containing this request
476 * @base: starting position in the buffer
477 *
Chuck Levera246b012005-08-11 16:25:23 -0400478 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400479static 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 -0400480{
Trond Myklebust24c56842006-10-17 15:06:22 -0400481 unsigned int remainder = xdr->len - base;
482 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400483
Chuck Lever262965f2005-08-11 16:25:56 -0400484 if (unlikely(!sock))
485 return -ENOTCONN;
486
487 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400488 if (base != 0) {
489 addr = NULL;
490 addrlen = 0;
491 }
Chuck Lever262965f2005-08-11 16:25:56 -0400492
Trond Myklebust24c56842006-10-17 15:06:22 -0400493 if (base < xdr->head[0].iov_len || addr != NULL) {
494 unsigned int len = xdr->head[0].iov_len - base;
495 remainder -= len;
496 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
497 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400498 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400499 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400500 base = 0;
501 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400502 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400503
Trond Myklebust24c56842006-10-17 15:06:22 -0400504 if (base < xdr->page_len) {
505 unsigned int len = xdr->page_len - base;
506 remainder -= len;
507 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
508 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400509 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400510 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400511 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400512 } else
513 base -= xdr->page_len;
514
515 if (base >= xdr->tail[0].iov_len)
516 return sent;
517 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400518out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400519 if (sent == 0)
520 return err;
521 if (err > 0)
522 sent += err;
523 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400524}
525
Chuck Lever9903cd12005-08-11 16:25:26 -0400526/**
Chuck Lever262965f2005-08-11 16:25:56 -0400527 * xs_nospace - place task on wait queue if transmit was incomplete
528 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400529 *
Chuck Levera246b012005-08-11 16:25:23 -0400530 */
Chuck Lever262965f2005-08-11 16:25:56 -0400531static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400532{
Chuck Lever262965f2005-08-11 16:25:56 -0400533 struct rpc_rqst *req = task->tk_rqstp;
534 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500535 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400536
Chuck Lever46121cf2007-01-31 12:14:08 -0500537 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400538 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
539 req->rq_slen);
540
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500541 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400542 /* Protect against races with write_space */
543 spin_lock_bh(&xprt->transport_lock);
544
545 /* Don't race with disconnect */
546 if (!xprt_connected(xprt))
547 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500548 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400549 xprt_wait_for_buffer_space(task);
550
551 spin_unlock_bh(&xprt->transport_lock);
552 } else
553 /* Keep holding the socket if it is blocked */
554 rpc_delay(task, HZ>>4);
555}
556
557/**
558 * xs_udp_send_request - write an RPC request to a UDP socket
559 * @task: address of RPC task that manages the state of an RPC request
560 *
561 * Return values:
562 * 0: The request has been sent
563 * EAGAIN: The socket was blocked, please call again later to
564 * complete the request
565 * ENOTCONN: Caller needs to invoke connect logic then call again
566 * other: Some other error occured, the request was not sent
567 */
568static int xs_udp_send_request(struct rpc_task *task)
569{
570 struct rpc_rqst *req = task->tk_rqstp;
571 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500572 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400573 struct xdr_buf *xdr = &req->rq_snd_buf;
574 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400575
Chuck Lever9903cd12005-08-11 16:25:26 -0400576 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400577 req->rq_svec->iov_base,
578 req->rq_svec->iov_len);
579
Chuck Lever262965f2005-08-11 16:25:56 -0400580 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500581 status = xs_sendpages(transport->sock,
Chuck Lever95392c52007-08-06 11:57:58 -0400582 xs_addr(xprt),
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500583 xprt->addrlen, xdr,
584 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400585
Chuck Lever46121cf2007-01-31 12:14:08 -0500586 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400587 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400588
Chuck Lever262965f2005-08-11 16:25:56 -0400589 if (likely(status >= (int) req->rq_slen))
590 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400591
Chuck Lever262965f2005-08-11 16:25:56 -0400592 /* Still some bytes left; set up for a retry later. */
593 if (status > 0)
594 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400595
Chuck Lever262965f2005-08-11 16:25:56 -0400596 switch (status) {
597 case -ENETUNREACH:
598 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400599 case -ECONNREFUSED:
600 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400601 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400602 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400603 case -EAGAIN:
604 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400605 break;
606 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500607 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400608 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400609 break;
Chuck Levera246b012005-08-11 16:25:23 -0400610 }
Chuck Lever262965f2005-08-11 16:25:56 -0400611
612 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400613}
614
Chuck Lever808012f2005-08-25 16:25:49 -0700615static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
616{
617 u32 reclen = buf->len - sizeof(rpc_fraghdr);
618 rpc_fraghdr *base = buf->head[0].iov_base;
619 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
620}
621
Chuck Lever9903cd12005-08-11 16:25:26 -0400622/**
Chuck Lever262965f2005-08-11 16:25:56 -0400623 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400624 * @task: address of RPC task that manages the state of an RPC request
625 *
626 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400627 * 0: The request has been sent
628 * EAGAIN: The socket was blocked, please call again later to
629 * complete the request
630 * ENOTCONN: Caller needs to invoke connect logic then call again
631 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400632 *
633 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400634 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400635 */
Chuck Lever262965f2005-08-11 16:25:56 -0400636static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400637{
638 struct rpc_rqst *req = task->tk_rqstp;
639 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500640 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400641 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Leverb595bb12007-08-06 11:56:42 -0400642 int status;
643 unsigned int retry = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400644
Chuck Lever808012f2005-08-25 16:25:49 -0700645 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400646
Chuck Lever262965f2005-08-11 16:25:56 -0400647 xs_pktdump("packet data:",
648 req->rq_svec->iov_base,
649 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400650
651 /* Continue transmitting the packet/record. We must be careful
652 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400653 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400654 while (1) {
655 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500656 status = xs_sendpages(transport->sock,
657 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400658
Chuck Lever46121cf2007-01-31 12:14:08 -0500659 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400660 xdr->len - req->rq_bytes_sent, status);
661
662 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400663 break;
664
Chuck Lever262965f2005-08-11 16:25:56 -0400665 /* If we've sent the entire packet, immediately
666 * reset the count of bytes sent. */
667 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500668 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400669 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
670 req->rq_bytes_sent = 0;
671 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400672 }
673
Chuck Levera246b012005-08-11 16:25:23 -0400674 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400675 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400676 break;
677 }
678
Chuck Lever262965f2005-08-11 16:25:56 -0400679 switch (status) {
680 case -EAGAIN:
681 xs_nospace(task);
682 break;
683 case -ECONNREFUSED:
684 case -ECONNRESET:
685 case -ENOTCONN:
686 case -EPIPE:
687 status = -ENOTCONN;
688 break;
689 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500690 dprintk("RPC: sendmsg returned unrecognized error %d\n",
Chuck Lever262965f2005-08-11 16:25:56 -0400691 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700692 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400693 break;
Chuck Levera246b012005-08-11 16:25:23 -0400694 }
Chuck Lever262965f2005-08-11 16:25:56 -0400695
Chuck Levera246b012005-08-11 16:25:23 -0400696 return status;
697}
698
Chuck Lever9903cd12005-08-11 16:25:26 -0400699/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400700 * xs_tcp_release_xprt - clean up after a tcp transmission
701 * @xprt: transport
702 * @task: rpc task
703 *
704 * This cleans up if an error causes us to abort the transmission of a request.
705 * In this case, the socket may need to be reset in order to avoid confusing
706 * the server.
707 */
708static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
709{
710 struct rpc_rqst *req;
711
712 if (task != xprt->snd_task)
713 return;
714 if (task == NULL)
715 goto out_release;
716 req = task->tk_rqstp;
717 if (req->rq_bytes_sent == 0)
718 goto out_release;
719 if (req->rq_bytes_sent == req->rq_snd_buf.len)
720 goto out_release;
721 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
722out_release:
723 xprt_release_xprt(xprt, task);
724}
725
726/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400727 * xs_close - close a socket
728 * @xprt: transport
729 *
Chuck Lever3167e122005-08-25 16:25:55 -0700730 * This is used when all requests are complete; ie, no DRC state remains
731 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400732 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400733static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400734{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500735 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
736 struct socket *sock = transport->sock;
737 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400738
739 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100740 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400741
Chuck Lever46121cf2007-01-31 12:14:08 -0500742 dprintk("RPC: xs_close xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400743
Chuck Levera246b012005-08-11 16:25:23 -0400744 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500745 transport->inet = NULL;
746 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400747
Chuck Lever9903cd12005-08-11 16:25:26 -0400748 sk->sk_user_data = NULL;
Chuck Lever314dfd72006-12-05 16:35:34 -0500749 sk->sk_data_ready = transport->old_data_ready;
750 sk->sk_state_change = transport->old_state_change;
751 sk->sk_write_space = transport->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400752 write_unlock_bh(&sk->sk_callback_lock);
753
Chuck Lever9903cd12005-08-11 16:25:26 -0400754 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400755
756 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100757clear_close_wait:
758 smp_mb__before_clear_bit();
759 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
760 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400761}
762
Chuck Lever9903cd12005-08-11 16:25:26 -0400763/**
764 * xs_destroy - prepare to shutdown a transport
765 * @xprt: doomed transport
766 *
767 */
768static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400769{
Chuck Leverc8475462006-12-05 16:35:26 -0500770 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
771
Chuck Lever46121cf2007-01-31 12:14:08 -0500772 dprintk("RPC: xs_destroy xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400773
Trond Myklebustc1384c92007-06-14 18:00:42 -0400774 cancel_rearming_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400775
776 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400777 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400778 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400779 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400780 kfree(xprt);
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -0400781 module_put(THIS_MODULE);
Chuck Levera246b012005-08-11 16:25:23 -0400782}
783
Chuck Lever9903cd12005-08-11 16:25:26 -0400784static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400785{
Chuck Lever9903cd12005-08-11 16:25:26 -0400786 return (struct rpc_xprt *) sk->sk_user_data;
787}
788
789/**
790 * xs_udp_data_ready - "data ready" callback for UDP sockets
791 * @sk: socket with data to read
792 * @len: how much data to read
793 *
794 */
795static void xs_udp_data_ready(struct sock *sk, int len)
796{
797 struct rpc_task *task;
798 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400799 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400800 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400801 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700802 u32 _xid;
803 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400804
805 read_lock(&sk->sk_callback_lock);
Chuck Lever46121cf2007-01-31 12:14:08 -0500806 dprintk("RPC: xs_udp_data_ready...\n");
Chuck Lever9903cd12005-08-11 16:25:26 -0400807 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400808 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400809
810 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
811 goto out;
812
813 if (xprt->shutdown)
814 goto dropit;
815
816 repsize = skb->len - sizeof(struct udphdr);
817 if (repsize < 4) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500818 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400819 goto dropit;
820 }
821
822 /* Copy the XID from the skb... */
823 xp = skb_header_pointer(skb, sizeof(struct udphdr),
824 sizeof(_xid), &_xid);
825 if (xp == NULL)
826 goto dropit;
827
828 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400829 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400830 rovr = xprt_lookup_rqst(xprt, *xp);
831 if (!rovr)
832 goto out_unlock;
833 task = rovr->rq_task;
834
Chuck Levera246b012005-08-11 16:25:23 -0400835 if ((copied = rovr->rq_private_buf.buflen) > repsize)
836 copied = repsize;
837
838 /* Suck it into the iovec, verify checksum if not done by hw. */
839 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
840 goto out_unlock;
841
842 /* Something worked... */
843 dst_confirm(skb->dst);
844
Chuck Lever1570c1e2005-08-25 16:25:52 -0700845 xprt_adjust_cwnd(task, copied);
846 xprt_update_rtt(task);
847 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400848
849 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400850 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400851 dropit:
852 skb_free_datagram(sk, skb);
853 out:
854 read_unlock(&sk->sk_callback_lock);
855}
856
Chuck Leverdd456472006-12-05 16:35:44 -0500857static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400858{
Chuck Lever51971132006-12-05 16:35:19 -0500859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400860 size_t len, used;
861 char *p;
862
Chuck Lever51971132006-12-05 16:35:19 -0500863 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
864 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500865 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500866 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400867 if (used != len)
868 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700869
Chuck Lever51971132006-12-05 16:35:19 -0500870 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
871 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500872 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400873 else
Chuck Levere136d092006-12-05 16:35:23 -0500874 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500875 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700876
Chuck Levere136d092006-12-05 16:35:23 -0500877 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500878 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700879
Chuck Levera246b012005-08-11 16:25:23 -0400880 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500881 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500882 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400883 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400884 return;
Chuck Levera246b012005-08-11 16:25:23 -0400885 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500886 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500887 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400888}
889
Chuck Lever51971132006-12-05 16:35:19 -0500890static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400891{
Chuck Lever51971132006-12-05 16:35:19 -0500892 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500893 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500894 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500895 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
896 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
897 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500898 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400899 }
900 }
901}
902
Chuck Leverdd456472006-12-05 16:35:44 -0500903static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400904{
905 size_t len, used;
906 char *p;
907
Chuck Lever51971132006-12-05 16:35:19 -0500908 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Lever46121cf2007-01-31 12:14:08 -0500909 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500910 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500911 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500912 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400913 if (used != len)
914 return;
Chuck Levere136d092006-12-05 16:35:23 -0500915 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
916 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500917 transport->tcp_copied = 4;
Chuck Lever46121cf2007-01-31 12:14:08 -0500918 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500919 ntohl(transport->tcp_xid));
920 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400921}
922
Chuck Leverdd456472006-12-05 16:35:44 -0500923static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400924{
Chuck Lever51971132006-12-05 16:35:19 -0500925 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400926 struct rpc_rqst *req;
927 struct xdr_buf *rcvbuf;
928 size_t len;
929 ssize_t r;
930
931 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400932 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500933 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400934 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500935 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500936 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500937 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400938 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400939 return;
940 }
941
942 rcvbuf = &req->rq_private_buf;
943 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500944 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500945 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400946
Chuck Lever51971132006-12-05 16:35:19 -0500947 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400948 memcpy(&my_desc, desc, sizeof(my_desc));
949 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500950 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500951 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400952 desc->count -= r;
953 desc->offset += r;
954 } else
Chuck Lever51971132006-12-05 16:35:19 -0500955 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500956 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400957
958 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500959 transport->tcp_copied += r;
960 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400961 }
962 if (r != len) {
963 /* Error when copying to the receive buffer,
964 * usually because we weren't able to allocate
965 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500966 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400967 * will not receive any additional updates,
968 * and time out.
969 * Any remaining data from this record will
970 * be discarded.
971 */
Chuck Levere136d092006-12-05 16:35:23 -0500972 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever46121cf2007-01-31 12:14:08 -0500973 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500974 ntohl(transport->tcp_xid));
Chuck Lever46121cf2007-01-31 12:14:08 -0500975 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
976 "tcp_offset = %u, tcp_reclen = %u\n",
977 xprt, transport->tcp_copied,
978 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400979 goto out;
980 }
981
Chuck Lever46121cf2007-01-31 12:14:08 -0500982 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500983 ntohl(transport->tcp_xid), r);
Chuck Lever46121cf2007-01-31 12:14:08 -0500984 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
985 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
986 transport->tcp_offset, transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400987
Chuck Lever51971132006-12-05 16:35:19 -0500988 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500989 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500990 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500991 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
992 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400993 }
994
995out:
Chuck Levere136d092006-12-05 16:35:23 -0500996 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500997 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400998 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500999 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001000}
1001
Chuck Leverdd456472006-12-05 16:35:44 -05001002static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -04001003{
1004 size_t len;
1005
Chuck Lever51971132006-12-05 16:35:19 -05001006 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -04001007 if (len > desc->count)
1008 len = desc->count;
1009 desc->count -= len;
1010 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -05001011 transport->tcp_offset += len;
Chuck Lever46121cf2007-01-31 12:14:08 -05001012 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -05001013 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -04001014}
1015
Chuck Lever9903cd12005-08-11 16:25:26 -04001016static 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 -04001017{
1018 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -05001019 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -05001020 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -04001021 .skb = skb,
1022 .offset = offset,
1023 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -04001024 };
Chuck Levera246b012005-08-11 16:25:23 -04001025
Chuck Lever46121cf2007-01-31 12:14:08 -05001026 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -04001027 do {
1028 /* Read in a new fragment marker if necessary */
1029 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -05001030 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001031 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001032 continue;
1033 }
1034 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -05001035 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -05001036 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001037 continue;
1038 }
1039 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -05001040 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -04001041 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001042 continue;
1043 }
1044 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -05001045 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -04001046 } while (desc.count);
Chuck Lever46121cf2007-01-31 12:14:08 -05001047 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -04001048 return len - desc.count;
1049}
1050
Chuck Lever9903cd12005-08-11 16:25:26 -04001051/**
1052 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1053 * @sk: socket with data to read
1054 * @bytes: how much data to read
1055 *
1056 */
1057static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -04001058{
1059 struct rpc_xprt *xprt;
1060 read_descriptor_t rd_desc;
1061
Chuck Lever46121cf2007-01-31 12:14:08 -05001062 dprintk("RPC: xs_tcp_data_ready...\n");
1063
Chuck Levera246b012005-08-11 16:25:23 -04001064 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -04001066 goto out;
Chuck Levera246b012005-08-11 16:25:23 -04001067 if (xprt->shutdown)
1068 goto out;
1069
Chuck Lever9903cd12005-08-11 16:25:26 -04001070 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -04001071 rd_desc.arg.data = xprt;
1072 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -04001073 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -04001074out:
1075 read_unlock(&sk->sk_callback_lock);
1076}
1077
Chuck Lever9903cd12005-08-11 16:25:26 -04001078/**
1079 * xs_tcp_state_change - callback to handle TCP socket state changes
1080 * @sk: socket whose state has changed
1081 *
1082 */
1083static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001084{
Chuck Lever9903cd12005-08-11 16:25:26 -04001085 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001086
1087 read_lock(&sk->sk_callback_lock);
1088 if (!(xprt = xprt_from_sock(sk)))
1089 goto out;
Chuck Lever46121cf2007-01-31 12:14:08 -05001090 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1091 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1092 sk->sk_state, xprt_connected(xprt),
1093 sock_flag(sk, SOCK_DEAD),
1094 sock_flag(sk, SOCK_ZAPPED));
Chuck Levera246b012005-08-11 16:25:23 -04001095
1096 switch (sk->sk_state) {
1097 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001098 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001099 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -05001100 struct sock_xprt *transport = container_of(xprt,
1101 struct sock_xprt, xprt);
1102
Chuck Levera246b012005-08-11 16:25:23 -04001103 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -05001104 transport->tcp_offset = 0;
1105 transport->tcp_reclen = 0;
1106 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -05001107 transport->tcp_flags =
1108 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -05001109
Chuck Lever03bf4b72005-08-25 16:25:55 -07001110 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -04001111 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001112 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -04001113 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001114 break;
1115 case TCP_SYN_SENT:
1116 case TCP_SYN_RECV:
1117 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +01001118 case TCP_CLOSE_WAIT:
1119 /* Try to schedule an autoclose RPC calls */
1120 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1121 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
Trond Myklebustc1384c92007-06-14 18:00:42 -04001122 queue_work(rpciod_workqueue, &xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -04001123 default:
1124 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001125 }
1126 out:
1127 read_unlock(&sk->sk_callback_lock);
1128}
1129
Chuck Lever9903cd12005-08-11 16:25:26 -04001130/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001131 * xs_udp_write_space - callback invoked when socket buffer space
1132 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001133 * @sk: socket whose state has changed
1134 *
Chuck Levera246b012005-08-11 16:25:23 -04001135 * Called when more output buffer space is available for this socket.
1136 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001137 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001138 * with a bunch of small requests.
1139 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001140static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001141{
Chuck Levera246b012005-08-11 16:25:23 -04001142 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001143
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001144 /* from net/core/sock.c:sock_def_write_space */
1145 if (sock_writeable(sk)) {
1146 struct socket *sock;
1147 struct rpc_xprt *xprt;
1148
1149 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -04001150 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001151 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -04001152 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001153 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1154 goto out;
1155
1156 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001157 }
1158
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001159 out:
1160 read_unlock(&sk->sk_callback_lock);
1161}
Chuck Levera246b012005-08-11 16:25:23 -04001162
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001163/**
1164 * xs_tcp_write_space - callback invoked when socket buffer space
1165 * becomes available
1166 * @sk: socket whose state has changed
1167 *
1168 * Called when more output buffer space is available for this socket.
1169 * We try not to wake our writers until they can make "significant"
1170 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1171 * with a bunch of small requests.
1172 */
1173static void xs_tcp_write_space(struct sock *sk)
1174{
1175 read_lock(&sk->sk_callback_lock);
1176
1177 /* from net/core/stream.c:sk_stream_write_space */
1178 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1179 struct socket *sock;
1180 struct rpc_xprt *xprt;
1181
1182 if (unlikely(!(sock = sk->sk_socket)))
1183 goto out;
1184 if (unlikely(!(xprt = xprt_from_sock(sk))))
1185 goto out;
1186 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1187 goto out;
1188
1189 xprt_write_space(xprt);
1190 }
1191
1192 out:
Chuck Levera246b012005-08-11 16:25:23 -04001193 read_unlock(&sk->sk_callback_lock);
1194}
1195
Chuck Lever470056c2005-08-25 16:25:56 -07001196static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001197{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001198 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1199 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001200
Chuck Lever7c6e0662006-12-05 16:35:30 -05001201 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001202 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001203 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001204 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001205 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001206 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001207 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001208 sk->sk_write_space(sk);
1209 }
1210}
1211
Chuck Lever43118c22005-08-25 16:25:49 -07001212/**
Chuck Lever470056c2005-08-25 16:25:56 -07001213 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001214 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001215 * @sndsize: requested size of send buffer, in bytes
1216 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001217 *
Chuck Lever470056c2005-08-25 16:25:56 -07001218 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001219 */
Chuck Lever470056c2005-08-25 16:25:56 -07001220static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001221{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001222 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1223
1224 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001225 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001226 transport->sndsize = sndsize + 1024;
1227 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001228 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001229 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001230
1231 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001232}
1233
Chuck Lever46c0ee82005-08-25 16:25:52 -07001234/**
1235 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1236 * @task: task that timed out
1237 *
1238 * Adjust the congestion window after a retransmit timeout has occurred.
1239 */
1240static void xs_udp_timer(struct rpc_task *task)
1241{
1242 xprt_adjust_cwnd(task, -ETIMEDOUT);
1243}
1244
Chuck Leverb85d8802006-05-25 01:40:49 -04001245static unsigned short xs_get_random_port(void)
1246{
1247 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1248 unsigned short rand = (unsigned short) net_random() % range;
1249 return rand + xprt_min_resvport;
1250}
1251
Chuck Lever92200412006-01-03 09:55:51 +01001252/**
1253 * xs_set_port - reset the port number in the remote endpoint address
1254 * @xprt: generic transport
1255 * @port: new port number
1256 *
1257 */
1258static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1259{
Chuck Lever95392c52007-08-06 11:57:58 -04001260 struct sockaddr *addr = xs_addr(xprt);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001261
Chuck Lever46121cf2007-01-31 12:14:08 -05001262 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001263
Chuck Lever20612002007-08-06 11:57:23 -04001264 switch (addr->sa_family) {
1265 case AF_INET:
1266 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1267 break;
1268 case AF_INET6:
1269 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1270 break;
1271 default:
1272 BUG();
1273 }
Chuck Lever92200412006-01-03 09:55:51 +01001274}
1275
Chuck Lever7dc753f2007-08-06 11:57:28 -04001276static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001277{
1278 struct sockaddr_in myaddr = {
1279 .sin_family = AF_INET,
1280 };
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001281 struct sockaddr_in *sa;
Chuck Lever529b33c2005-08-25 16:25:54 -07001282 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001283 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001284
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001285 if (!transport->xprt.resvport)
1286 port = 0;
1287 sa = (struct sockaddr_in *)&transport->addr;
1288 myaddr.sin_addr = sa->sin_addr;
Chuck Levera246b012005-08-11 16:25:23 -04001289 do {
1290 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001291 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001292 sizeof(myaddr));
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001293 if (!transport->xprt.resvport)
1294 break;
Chuck Levera246b012005-08-11 16:25:23 -04001295 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001296 transport->port = port;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001297 break;
Chuck Levera246b012005-08-11 16:25:23 -04001298 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001299 if (port <= xprt_min_resvport)
1300 port = xprt_max_resvport;
1301 else
1302 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001303 } while (err == -EADDRINUSE && port != transport->port);
Chuck Lever7dc753f2007-08-06 11:57:28 -04001304 dprintk("RPC: %s "NIPQUAD_FMT":%u: %s (%d)\n",
1305 __FUNCTION__, NIPQUAD(myaddr.sin_addr),
1306 port, err ? "failed" : "ok", err);
Chuck Levera246b012005-08-11 16:25:23 -04001307 return err;
1308}
1309
Chuck Lever90058d32007-08-06 11:57:33 -04001310static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1311{
1312 struct sockaddr_in6 myaddr = {
1313 .sin6_family = AF_INET6,
1314 };
1315 struct sockaddr_in6 *sa;
1316 int err;
1317 unsigned short port = transport->port;
1318
1319 if (!transport->xprt.resvport)
1320 port = 0;
1321 sa = (struct sockaddr_in6 *)&transport->addr;
1322 myaddr.sin6_addr = sa->sin6_addr;
1323 do {
1324 myaddr.sin6_port = htons(port);
1325 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1326 sizeof(myaddr));
1327 if (!transport->xprt.resvport)
1328 break;
1329 if (err == 0) {
1330 transport->port = port;
1331 break;
1332 }
1333 if (port <= xprt_min_resvport)
1334 port = xprt_max_resvport;
1335 else
1336 port--;
1337 } while (err == -EADDRINUSE && port != transport->port);
1338 dprintk("RPC: xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
1339 NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
1340 return err;
1341}
1342
Peter Zijlstraed075362006-12-06 20:35:24 -08001343#ifdef CONFIG_DEBUG_LOCK_ALLOC
1344static struct lock_class_key xs_key[2];
1345static struct lock_class_key xs_slock_key[2];
1346
Chuck Lever8945ee52007-08-06 11:58:04 -04001347static inline void xs_reclassify_socket4(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001348{
1349 struct sock *sk = sock->sk;
Chuck Lever8945ee52007-08-06 11:58:04 -04001350
Peter Zijlstraed075362006-12-06 20:35:24 -08001351 BUG_ON(sk->sk_lock.owner != NULL);
Chuck Lever8945ee52007-08-06 11:58:04 -04001352 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1353 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1354}
Peter Zijlstraed075362006-12-06 20:35:24 -08001355
Chuck Lever8945ee52007-08-06 11:58:04 -04001356static inline void xs_reclassify_socket6(struct socket *sock)
1357{
1358 struct sock *sk = sock->sk;
Peter Zijlstraed075362006-12-06 20:35:24 -08001359
Chuck Lever8945ee52007-08-06 11:58:04 -04001360 BUG_ON(sk->sk_lock.owner != NULL);
1361 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1362 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
Peter Zijlstraed075362006-12-06 20:35:24 -08001363}
1364#else
Chuck Lever8945ee52007-08-06 11:58:04 -04001365static inline void xs_reclassify_socket4(struct socket *sock)
1366{
1367}
1368
1369static inline void xs_reclassify_socket6(struct socket *sock)
Peter Zijlstraed075362006-12-06 20:35:24 -08001370{
1371}
1372#endif
1373
Chuck Lever16be2d22007-08-06 11:57:38 -04001374static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1375{
1376 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1377
1378 if (!transport->inet) {
1379 struct sock *sk = sock->sk;
1380
1381 write_lock_bh(&sk->sk_callback_lock);
1382
1383 sk->sk_user_data = xprt;
1384 transport->old_data_ready = sk->sk_data_ready;
1385 transport->old_state_change = sk->sk_state_change;
1386 transport->old_write_space = sk->sk_write_space;
1387 sk->sk_data_ready = xs_udp_data_ready;
1388 sk->sk_write_space = xs_udp_write_space;
1389 sk->sk_no_check = UDP_CSUM_NORCV;
1390 sk->sk_allocation = GFP_ATOMIC;
1391
1392 xprt_set_connected(xprt);
1393
1394 /* Reset to new socket */
1395 transport->sock = sock;
1396 transport->inet = sk;
1397
1398 write_unlock_bh(&sk->sk_callback_lock);
1399 }
1400 xs_udp_do_set_buffer_size(xprt);
1401}
1402
Chuck Lever9903cd12005-08-11 16:25:26 -04001403/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001404 * xs_udp_connect_worker4 - set up a UDP socket
David Howells65f27f32006-11-22 14:55:48 +00001405 * @work: RPC transport to connect
Chuck Lever9903cd12005-08-11 16:25:26 -04001406 *
1407 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001408 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001409static void xs_udp_connect_worker4(struct work_struct *work)
Chuck Levera246b012005-08-11 16:25:23 -04001410{
Trond Myklebust34161db2006-12-07 15:48:15 -05001411 struct sock_xprt *transport =
1412 container_of(work, struct sock_xprt, connect_worker.work);
Chuck Leverc8475462006-12-05 16:35:26 -05001413 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001414 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001415 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001416
Chuck Leverec739ef2006-08-22 20:06:15 -04001417 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001418 goto out;
1419
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001420 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001421 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001422
1423 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001424 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001425 goto out;
1426 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001427 xs_reclassify_socket4(sock);
Chuck Levera246b012005-08-11 16:25:23 -04001428
Chuck Lever7dc753f2007-08-06 11:57:28 -04001429 if (xs_bind4(transport, sock)) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001430 sock_release(sock);
1431 goto out;
1432 }
1433
Chuck Lever46121cf2007-01-31 12:14:08 -05001434 dprintk("RPC: worker connecting xprt %p to address: %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001435 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001436
Chuck Lever16be2d22007-08-06 11:57:38 -04001437 xs_udp_finish_connecting(xprt, sock);
Chuck Levera246b012005-08-11 16:25:23 -04001438 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001439out:
1440 xprt_wake_pending_tasks(xprt, status);
1441 xprt_clear_connecting(xprt);
1442}
1443
Chuck Lever68e220b2007-08-06 11:57:48 -04001444/**
1445 * xs_udp_connect_worker6 - set up a UDP socket
1446 * @work: RPC transport to connect
1447 *
1448 * Invoked by a work queue tasklet.
1449 */
1450static void xs_udp_connect_worker6(struct work_struct *work)
1451{
1452 struct sock_xprt *transport =
1453 container_of(work, struct sock_xprt, connect_worker.work);
1454 struct rpc_xprt *xprt = &transport->xprt;
1455 struct socket *sock = transport->sock;
1456 int err, status = -EIO;
1457
1458 if (xprt->shutdown || !xprt_bound(xprt))
1459 goto out;
1460
1461 /* Start by resetting any existing state */
1462 xs_close(xprt);
1463
1464 if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1465 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1466 goto out;
1467 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001468 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001469
1470 if (xs_bind6(transport, sock) < 0) {
1471 sock_release(sock);
1472 goto out;
1473 }
1474
1475 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1476 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1477
1478 xs_udp_finish_connecting(xprt, sock);
1479 status = 0;
1480out:
1481 xprt_wake_pending_tasks(xprt, status);
1482 xprt_clear_connecting(xprt);
1483}
1484
Chuck Lever3167e122005-08-25 16:25:55 -07001485/*
1486 * We need to preserve the port number so the reply cache on the server can
1487 * find our cached RPC replies when we get around to reconnecting.
1488 */
1489static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1490{
1491 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001492 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001493 struct sockaddr any;
1494
Chuck Lever46121cf2007-01-31 12:14:08 -05001495 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001496
1497 /*
1498 * Disconnect the transport socket by doing a connect operation
1499 * with AF_UNSPEC. This should return immediately...
1500 */
1501 memset(&any, 0, sizeof(any));
1502 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001503 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001504 if (result)
Chuck Lever46121cf2007-01-31 12:14:08 -05001505 dprintk("RPC: AF_UNSPEC connect return code %d\n",
Chuck Lever3167e122005-08-25 16:25:55 -07001506 result);
1507}
1508
Chuck Lever16be2d22007-08-06 11:57:38 -04001509static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001510{
Chuck Lever16be2d22007-08-06 11:57:38 -04001511 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -04001512
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001513 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001514 struct sock *sk = sock->sk;
1515
1516 write_lock_bh(&sk->sk_callback_lock);
1517
1518 sk->sk_user_data = xprt;
Chuck Lever314dfd72006-12-05 16:35:34 -05001519 transport->old_data_ready = sk->sk_data_ready;
1520 transport->old_state_change = sk->sk_state_change;
1521 transport->old_write_space = sk->sk_write_space;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001522 sk->sk_data_ready = xs_tcp_data_ready;
1523 sk->sk_state_change = xs_tcp_state_change;
1524 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001525 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001526
1527 /* socket options */
1528 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1529 sock_reset_flag(sk, SOCK_LINGER);
1530 tcp_sk(sk)->linger2 = 0;
1531 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001532
1533 xprt_clear_connected(xprt);
1534
1535 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001536 transport->sock = sock;
1537 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001538
1539 write_unlock_bh(&sk->sk_callback_lock);
1540 }
1541
1542 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001543 xprt->stat.connect_count++;
1544 xprt->stat.connect_start = jiffies;
Chuck Lever95392c52007-08-06 11:57:58 -04001545 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
Chuck Lever16be2d22007-08-06 11:57:38 -04001546}
1547
1548/**
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001549 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
Chuck Lever16be2d22007-08-06 11:57:38 -04001550 * @work: RPC transport to connect
1551 *
1552 * Invoked by a work queue tasklet.
1553 */
Chuck Lever9c3d72d2007-08-06 11:57:43 -04001554static void xs_tcp_connect_worker4(struct work_struct *work)
Chuck Lever16be2d22007-08-06 11:57:38 -04001555{
1556 struct sock_xprt *transport =
1557 container_of(work, struct sock_xprt, connect_worker.work);
1558 struct rpc_xprt *xprt = &transport->xprt;
1559 struct socket *sock = transport->sock;
1560 int err, status = -EIO;
1561
1562 if (xprt->shutdown || !xprt_bound(xprt))
1563 goto out;
1564
1565 if (!sock) {
1566 /* start from scratch */
1567 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1568 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1569 goto out;
1570 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001571 xs_reclassify_socket4(sock);
Chuck Lever16be2d22007-08-06 11:57:38 -04001572
1573 if (xs_bind4(transport, sock) < 0) {
1574 sock_release(sock);
1575 goto out;
1576 }
1577 } else
1578 /* "close" the socket, preserving the local port */
1579 xs_tcp_reuse_connection(xprt);
1580
1581 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1582 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1583
1584 status = xs_tcp_finish_connecting(xprt, sock);
Chuck Lever46121cf2007-01-31 12:14:08 -05001585 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1586 xprt, -status, xprt_connected(xprt),
1587 sock->sk->sk_state);
Chuck Levera246b012005-08-11 16:25:23 -04001588 if (status < 0) {
1589 switch (status) {
1590 case -EINPROGRESS:
1591 case -EALREADY:
1592 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001593 case -ECONNREFUSED:
1594 case -ECONNRESET:
1595 /* retry with existing socket, after a delay */
1596 break;
1597 default:
1598 /* get rid of existing socket, and retry */
1599 xs_close(xprt);
1600 break;
Chuck Levera246b012005-08-11 16:25:23 -04001601 }
1602 }
1603out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001604 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001605out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001606 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001607}
1608
Chuck Lever9903cd12005-08-11 16:25:26 -04001609/**
Chuck Lever68e220b2007-08-06 11:57:48 -04001610 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1611 * @work: RPC transport to connect
1612 *
1613 * Invoked by a work queue tasklet.
1614 */
1615static void xs_tcp_connect_worker6(struct work_struct *work)
1616{
1617 struct sock_xprt *transport =
1618 container_of(work, struct sock_xprt, connect_worker.work);
1619 struct rpc_xprt *xprt = &transport->xprt;
1620 struct socket *sock = transport->sock;
1621 int err, status = -EIO;
1622
1623 if (xprt->shutdown || !xprt_bound(xprt))
1624 goto out;
1625
1626 if (!sock) {
1627 /* start from scratch */
1628 if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1629 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1630 goto out;
1631 }
Chuck Lever8945ee52007-08-06 11:58:04 -04001632 xs_reclassify_socket6(sock);
Chuck Lever68e220b2007-08-06 11:57:48 -04001633
1634 if (xs_bind6(transport, sock) < 0) {
1635 sock_release(sock);
1636 goto out;
1637 }
1638 } else
1639 /* "close" the socket, preserving the local port */
1640 xs_tcp_reuse_connection(xprt);
1641
1642 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1643 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1644
1645 status = xs_tcp_finish_connecting(xprt, sock);
1646 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1647 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1648 if (status < 0) {
1649 switch (status) {
1650 case -EINPROGRESS:
1651 case -EALREADY:
1652 goto out_clear;
1653 case -ECONNREFUSED:
1654 case -ECONNRESET:
1655 /* retry with existing socket, after a delay */
1656 break;
1657 default:
1658 /* get rid of existing socket, and retry */
1659 xs_close(xprt);
1660 break;
1661 }
1662 }
1663out:
1664 xprt_wake_pending_tasks(xprt, status);
1665out_clear:
1666 xprt_clear_connecting(xprt);
1667}
1668
1669/**
Chuck Lever9903cd12005-08-11 16:25:26 -04001670 * xs_connect - connect a socket to a remote endpoint
1671 * @task: address of RPC task that manages state of connect request
1672 *
1673 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001674 *
1675 * UDP socket connects are synchronous, but we use a work queue anyway
1676 * to guarantee that even unprivileged user processes can set up a
1677 * socket on a privileged port.
1678 *
1679 * If a UDP socket connect fails, the delay behavior here prevents
1680 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001681 */
1682static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001683{
1684 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001685 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001686
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001687 if (xprt_test_and_set_connecting(xprt))
1688 return;
1689
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001690 if (transport->sock != NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001691 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1692 "seconds\n",
Chuck Lever03bf4b72005-08-25 16:25:55 -07001693 xprt, xprt->reestablish_timeout / HZ);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001694 queue_delayed_work(rpciod_workqueue,
1695 &transport->connect_worker,
1696 xprt->reestablish_timeout);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001697 xprt->reestablish_timeout <<= 1;
1698 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1699 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001700 } else {
Chuck Lever46121cf2007-01-31 12:14:08 -05001701 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Trond Myklebustc1384c92007-06-14 18:00:42 -04001702 queue_delayed_work(rpciod_workqueue,
1703 &transport->connect_worker, 0);
Chuck Levera246b012005-08-11 16:25:23 -04001704 }
1705}
1706
Chuck Lever262ca072006-03-20 13:44:16 -05001707/**
1708 * xs_udp_print_stats - display UDP socket-specifc stats
1709 * @xprt: rpc_xprt struct containing statistics
1710 * @seq: output file
1711 *
1712 */
1713static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1714{
Chuck Leverc8475462006-12-05 16:35:26 -05001715 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1716
Chuck Lever262ca072006-03-20 13:44:16 -05001717 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001718 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001719 xprt->stat.bind_count,
1720 xprt->stat.sends,
1721 xprt->stat.recvs,
1722 xprt->stat.bad_xids,
1723 xprt->stat.req_u,
1724 xprt->stat.bklog_u);
1725}
1726
1727/**
1728 * xs_tcp_print_stats - display TCP socket-specifc stats
1729 * @xprt: rpc_xprt struct containing statistics
1730 * @seq: output file
1731 *
1732 */
1733static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1734{
Chuck Leverc8475462006-12-05 16:35:26 -05001735 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001736 long idle_time = 0;
1737
1738 if (xprt_connected(xprt))
1739 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1740
1741 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001742 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001743 xprt->stat.bind_count,
1744 xprt->stat.connect_count,
1745 xprt->stat.connect_time,
1746 idle_time,
1747 xprt->stat.sends,
1748 xprt->stat.recvs,
1749 xprt->stat.bad_xids,
1750 xprt->stat.req_u,
1751 xprt->stat.bklog_u);
1752}
1753
Chuck Lever262965f2005-08-11 16:25:56 -04001754static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001755 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001756 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001757 .release_xprt = xprt_release_xprt_cong,
Chuck Lever45160d62007-07-01 12:13:17 -04001758 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001759 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001760 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001761 .buf_alloc = rpc_malloc,
1762 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001763 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001764 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001765 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001766 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001767 .close = xs_close,
1768 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001769 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001770};
1771
1772static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001773 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001774 .release_xprt = xs_tcp_release_xprt,
Chuck Lever45160d62007-07-01 12:13:17 -04001775 .rpcbind = rpcb_getport_async,
Chuck Lever92200412006-01-03 09:55:51 +01001776 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001777 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001778 .buf_alloc = rpc_malloc,
1779 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001780 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001781 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001782 .close = xs_close,
1783 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001784 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001785};
1786
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001787static struct rpc_xprt *xs_setup_xprt(struct rpc_xprtsock_create *args,
1788 unsigned int slot_table_size)
Chuck Leverc8541ec2006-10-17 14:44:27 -04001789{
1790 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001791 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001792
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001793 if (args->addrlen > sizeof(xprt->addr)) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001794 dprintk("RPC: xs_setup_xprt: address too large\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001795 return ERR_PTR(-EBADF);
1796 }
1797
Chuck Leverffc2e512006-12-05 16:35:11 -05001798 new = kzalloc(sizeof(*new), GFP_KERNEL);
1799 if (new == NULL) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001800 dprintk("RPC: xs_setup_xprt: couldn't allocate "
1801 "rpc_xprt\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001802 return ERR_PTR(-ENOMEM);
1803 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001804 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001805
1806 xprt->max_reqs = slot_table_size;
1807 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1808 if (xprt->slot == NULL) {
1809 kfree(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001810 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
1811 "table\n");
Chuck Leverc8541ec2006-10-17 14:44:27 -04001812 return ERR_PTR(-ENOMEM);
1813 }
1814
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001815 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
1816 xprt->addrlen = args->addrlen;
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +02001817 if (args->srcaddr)
1818 memcpy(&new->addr, args->srcaddr, args->addrlen);
Chuck Leverc8475462006-12-05 16:35:26 -05001819 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001820
1821 return xprt;
1822}
1823
Chuck Lever9903cd12005-08-11 16:25:26 -04001824/**
1825 * xs_setup_udp - Set up transport to use a UDP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001826 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001827 *
1828 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001829struct rpc_xprt *xs_setup_udp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001830{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001831 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001832 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001833 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001834
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001835 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001836 if (IS_ERR(xprt))
1837 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001838 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001839
Chuck Leverec739ef2006-08-22 20:06:15 -04001840 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001841 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001842 /* XXX: header size can vary due to auth type, IPv6, etc. */
1843 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1844
Chuck Lever03bf4b72005-08-25 16:25:55 -07001845 xprt->bind_timeout = XS_BIND_TO;
1846 xprt->connect_timeout = XS_UDP_CONN_TO;
1847 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1848 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001849
Chuck Lever262965f2005-08-11 16:25:56 -04001850 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001851
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001852 if (args->timeout)
1853 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001854 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001855 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001856
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001857 switch (addr->sa_family) {
1858 case AF_INET:
1859 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1860 xprt_set_bound(xprt);
1861
1862 INIT_DELAYED_WORK(&transport->connect_worker,
1863 xs_udp_connect_worker4);
1864 xs_format_ipv4_peer_addresses(xprt);
1865 break;
1866 case AF_INET6:
1867 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1868 xprt_set_bound(xprt);
1869
1870 INIT_DELAYED_WORK(&transport->connect_worker,
1871 xs_udp_connect_worker6);
1872 xs_format_ipv6_peer_addresses(xprt);
1873 break;
1874 default:
1875 kfree(xprt);
1876 return ERR_PTR(-EAFNOSUPPORT);
1877 }
1878
Chuck Lever46121cf2007-01-31 12:14:08 -05001879 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001880 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001881
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001882 if (try_module_get(THIS_MODULE))
1883 return xprt;
1884
1885 kfree(xprt->slot);
1886 kfree(xprt);
1887 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04001888}
1889
Chuck Lever9903cd12005-08-11 16:25:26 -04001890/**
1891 * xs_setup_tcp - Set up transport to use a TCP socket
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001892 * @args: rpc transport creation arguments
Chuck Lever9903cd12005-08-11 16:25:26 -04001893 *
1894 */
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001895struct rpc_xprt *xs_setup_tcp(struct rpc_xprtsock_create *args)
Chuck Levera246b012005-08-11 16:25:23 -04001896{
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001897 struct sockaddr *addr = args->dstaddr;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001898 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001899 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001900
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001901 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
Chuck Leverc8541ec2006-10-17 14:44:27 -04001902 if (IS_ERR(xprt))
1903 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001904 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001905
Chuck Leverec739ef2006-08-22 20:06:15 -04001906 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001907 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001908 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001909
Chuck Lever03bf4b72005-08-25 16:25:55 -07001910 xprt->bind_timeout = XS_BIND_TO;
1911 xprt->connect_timeout = XS_TCP_CONN_TO;
1912 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1913 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001914
Chuck Lever262965f2005-08-11 16:25:56 -04001915 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001916
Frank van Maarseveen96802a02007-07-08 13:08:54 +02001917 if (args->timeout)
1918 xprt->timeout = *args->timeout;
Chuck Levera246b012005-08-11 16:25:23 -04001919 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001920 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001921
Chuck Lever8f9d5b12007-08-06 11:57:53 -04001922 switch (addr->sa_family) {
1923 case AF_INET:
1924 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1925 xprt_set_bound(xprt);
1926
1927 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
1928 xs_format_ipv4_peer_addresses(xprt);
1929 break;
1930 case AF_INET6:
1931 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1932 xprt_set_bound(xprt);
1933
1934 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
1935 xs_format_ipv6_peer_addresses(xprt);
1936 break;
1937 default:
1938 kfree(xprt);
1939 return ERR_PTR(-EAFNOSUPPORT);
1940 }
1941
Chuck Lever46121cf2007-01-31 12:14:08 -05001942 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001943 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001944
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001945 if (try_module_get(THIS_MODULE))
1946 return xprt;
1947
1948 kfree(xprt->slot);
1949 kfree(xprt);
1950 return ERR_PTR(-EINVAL);
Chuck Levera246b012005-08-11 16:25:23 -04001951}
Chuck Lever282b32e2006-12-05 16:35:51 -05001952
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001953static struct xprt_class xs_udp_transport = {
1954 .list = LIST_HEAD_INIT(xs_udp_transport.list),
1955 .name = "udp",
1956 .owner = THIS_MODULE,
1957 .family = AF_INET,
1958 .protocol = IPPROTO_UDP,
1959 .setup = xs_setup_udp,
1960};
1961
1962static struct xprt_class xs_tcp_transport = {
1963 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
1964 .name = "tcp",
1965 .owner = THIS_MODULE,
1966 .family = AF_INET,
1967 .protocol = IPPROTO_TCP,
1968 .setup = xs_setup_tcp,
1969};
1970
Chuck Lever282b32e2006-12-05 16:35:51 -05001971/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001972 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
Chuck Lever282b32e2006-12-05 16:35:51 -05001973 *
1974 */
1975int init_socket_xprt(void)
1976{
Chuck Leverfbf76682006-12-05 16:35:54 -05001977#ifdef RPC_DEBUG
Eric W. Biederman2b1bec52007-02-14 00:33:24 -08001978 if (!sunrpc_table_header)
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08001979 sunrpc_table_header = register_sysctl_table(sunrpc_table);
Chuck Leverfbf76682006-12-05 16:35:54 -05001980#endif
1981
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001982 xprt_register_transport(&xs_udp_transport);
1983 xprt_register_transport(&xs_tcp_transport);
1984
Chuck Lever282b32e2006-12-05 16:35:51 -05001985 return 0;
1986}
1987
1988/**
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04001989 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
Chuck Lever282b32e2006-12-05 16:35:51 -05001990 *
1991 */
1992void cleanup_socket_xprt(void)
1993{
Chuck Leverfbf76682006-12-05 16:35:54 -05001994#ifdef RPC_DEBUG
1995 if (sunrpc_table_header) {
1996 unregister_sysctl_table(sunrpc_table_header);
1997 sunrpc_table_header = NULL;
1998 }
1999#endif
\"Talpey, Thomas\bc255712007-09-10 13:46:39 -04002000
2001 xprt_unregister_transport(&xs_udp_transport);
2002 xprt_unregister_transport(&xs_tcp_transport);
Chuck Lever282b32e2006-12-05 16:35:51 -05002003}